Mercurial > projects > pwm
comparison cmd.c @ 23:1b89066d992c
Add read-only mode
author | Guido Berhoerster <guido+pwm@berhoerster.name> |
---|---|
date | Sun, 17 Sep 2017 18:45:05 +0200 |
parents | ec01c579024a |
children | 722a45b4028b |
comparison
equal
deleted
inserted
replaced
22:ec01c579024a | 23:1b89066d992c |
---|---|
226 if (ctx->errmsg != NULL) { | 226 if (ctx->errmsg != NULL) { |
227 if (io_printf("%s\n", ctx->errmsg) == IO_SIGNAL) { | 227 if (io_printf("%s\n", ctx->errmsg) == IO_SIGNAL) { |
228 return (CMD_SIGNAL); | 228 return (CMD_SIGNAL); |
229 } | 229 } |
230 } | 230 } |
231 if (io_printf("There are%sunsaved changes\n", | 231 if (ctx->is_readonly) { |
232 ctx->unsaved_changes ? " " : " no ") == IO_SIGNAL) { | 232 if (io_printf("Read-only mode\n") == IO_SIGNAL) { |
233 return (CMD_SIGNAL); | 233 return (CMD_SIGNAL); |
234 } | |
235 } else { | |
236 if (io_printf("There are%sunsaved changes\n", | |
237 ctx->unsaved_changes ? " " : " no ") == IO_SIGNAL) { | |
238 return (CMD_SIGNAL); | |
239 } | |
234 } | 240 } |
235 | 241 |
236 return (CMD_STATUS); | 242 return (CMD_STATUS); |
237 } | 243 } |
238 | 244 |
493 if (!ctx->is_interactive && (argc < 2)) { | 499 if (!ctx->is_interactive && (argc < 2)) { |
494 retval = CMD_USAGE; | 500 retval = CMD_USAGE; |
495 goto out; | 501 goto out; |
496 } | 502 } |
497 | 503 |
504 if (ctx->is_readonly) { | |
505 pwm_err(ctx, "cannot create new entries in read-only mode"); | |
506 goto out; | |
507 } | |
508 | |
498 record = pwfile_create_record(); | 509 record = pwfile_create_record(); |
499 | 510 |
500 for (i = 1; i < argc; i++) { | 511 for (i = 1; i < argc; i++) { |
501 type = parse_arg(argv[i], field_namev, '=', &value); | 512 type = parse_arg(argv[i], field_namev, '=', &value); |
502 if (type == FIELD_UNKNOWN) { | 513 if (type == FIELD_UNKNOWN) { |
569 | 580 |
570 if (parse_id(argv[1], &id) != 0) { | 581 if (parse_id(argv[1], &id) != 0) { |
571 pwm_err(ctx, "invalid id %s", argv[1]); | 582 pwm_err(ctx, "invalid id %s", argv[1]); |
572 goto out; | 583 goto out; |
573 } | 584 } |
585 | |
586 if (ctx->is_readonly) { | |
587 pwm_err(ctx, "cannot modify entries in read-only mode"); | |
588 goto out; | |
589 } | |
590 | |
574 record = pwfile_get_record(ctx, id); | 591 record = pwfile_get_record(ctx, id); |
575 | 592 |
576 for (i = 2; i < argc; i++) { | 593 for (i = 2; i < argc; i++) { |
577 type = parse_arg(argv[i], field_namev, '=', &value); | 594 type = parse_arg(argv[i], field_namev, '=', &value); |
578 if (type == FIELD_UNKNOWN) { | 595 if (type == FIELD_UNKNOWN) { |
648 char password[PWS3_MAX_PASSWORD_LEN + 1] = { 0 }; | 665 char password[PWS3_MAX_PASSWORD_LEN + 1] = { 0 }; |
649 | 666 |
650 /* check if first argument is an id */ | 667 /* check if first argument is an id */ |
651 if ((argc > 1) && (parse_id(argv[1], &id) == 0)) { | 668 if ((argc > 1) && (parse_id(argv[1], &id) == 0)) { |
652 i++; | 669 i++; |
670 if (ctx->is_readonly) { | |
671 pwm_err(ctx, "cannot modify entries in read-only mode"); | |
672 goto out; | |
673 } | |
653 } | 674 } |
654 | 675 |
655 for (; i < argc; i++) { | 676 for (; i < argc; i++) { |
656 switch (parse_arg(argv[i], cmd_generatepassword_argv, '=', | 677 switch (parse_arg(argv[i], cmd_generatepassword_argv, '=', |
657 &value)) { | 678 &value)) { |
776 return (CMD_USAGE); | 797 return (CMD_USAGE); |
777 } | 798 } |
778 | 799 |
779 if (parse_id(argv[1], &id) != 0) { | 800 if (parse_id(argv[1], &id) != 0) { |
780 pwm_err(ctx, "invalid id %s", argv[1]); | 801 pwm_err(ctx, "invalid id %s", argv[1]); |
802 return (CMD_ERR); | |
803 } | |
804 | |
805 if (ctx->is_readonly) { | |
806 pwm_err(ctx, "cannot remove entries in read-only mode"); | |
781 return (CMD_ERR); | 807 return (CMD_ERR); |
782 } | 808 } |
783 | 809 |
784 if (pwfile_remove_pws_record(ctx, id) != 0) { | 810 if (pwfile_remove_pws_record(ctx, id) != 0) { |
785 pwm_err(ctx, "failed to remove record %u", id); | 811 pwm_err(ctx, "failed to remove record %u", id); |
957 | 983 |
958 if (!ctx->is_interactive && (argc != 2)) { | 984 if (!ctx->is_interactive && (argc != 2)) { |
959 return (CMD_USAGE); | 985 return (CMD_USAGE); |
960 } | 986 } |
961 | 987 |
988 if (ctx->is_readonly) { | |
989 pwm_err(ctx, "cannot create groups in read-only mode"); | |
990 return (CMD_ERR); | |
991 } | |
992 | |
962 if (ctx->is_interactive && (argc != 2)) { | 993 if (ctx->is_interactive && (argc != 2)) { |
963 if (io_get_line(NULL, "Group: ", 0, NULL, 0, | 994 if (io_get_line(NULL, "Group: ", 0, NULL, 0, |
964 sizeof (group_buf), group_buf) == IO_SIGNAL) { | 995 sizeof (group_buf), group_buf) == IO_SIGNAL) { |
965 return (CMD_SIGNAL); | 996 return (CMD_SIGNAL); |
966 } | 997 } |
984 { | 1015 { |
985 if (argc != 2) { | 1016 if (argc != 2) { |
986 return (CMD_USAGE); | 1017 return (CMD_USAGE); |
987 } | 1018 } |
988 | 1019 |
1020 if (ctx->is_readonly) { | |
1021 pwm_err(ctx, "cannot remove groups in read-only mode"); | |
1022 return (CMD_ERR); | |
1023 } | |
1024 | |
989 if (pwfile_remove_group(ctx, argv[1]) != 0) { | 1025 if (pwfile_remove_group(ctx, argv[1]) != 0) { |
990 pwm_err(ctx, "empty group \"%s\" does not exist", argv[1]); | 1026 pwm_err(ctx, "empty group \"%s\" does not exist", argv[1]); |
991 return (CMD_ERR); | 1027 return (CMD_ERR); |
992 } | 1028 } |
993 | 1029 |
999 { | 1035 { |
1000 size_t len; | 1036 size_t len; |
1001 | 1037 |
1002 if (argc > 2) { | 1038 if (argc > 2) { |
1003 return (CMD_USAGE); | 1039 return (CMD_USAGE); |
1004 } else if (argc == 2) { | 1040 } |
1041 | |
1042 if (ctx->is_readonly) { | |
1043 pwm_err(ctx, "cannot modify entries in read-only mode"); | |
1044 return (CMD_ERR); | |
1045 } | |
1046 | |
1047 if (argc == 2) { | |
1005 len = strlen(argv[1]); | 1048 len = strlen(argv[1]); |
1006 if (len == 0) { | 1049 if (len == 0) { |
1007 pwm_err(ctx, "password must not be empty"); | 1050 pwm_err(ctx, "password must not be empty"); |
1008 return (CMD_ERR); | 1051 return (CMD_ERR); |
1009 } else if (len + 1 > sizeof (ctx->password)) { | 1052 } else if (len + 1 > sizeof (ctx->password)) { |
1058 { | 1101 { |
1059 if (argc != 1) { | 1102 if (argc != 1) { |
1060 return (CMD_USAGE); | 1103 return (CMD_USAGE); |
1061 } | 1104 } |
1062 | 1105 |
1106 if (ctx->is_readonly) { | |
1107 pwm_err(ctx, "cannot write changes in read-only mode"); | |
1108 return (CMD_ERR); | |
1109 } | |
1110 | |
1063 return ((pwfile_write_file(ctx) == 0) ? CMD_OK : CMD_ERR); | 1111 return ((pwfile_write_file(ctx) == 0) ? CMD_OK : CMD_ERR); |
1064 } | 1112 } |
1065 | 1113 |
1066 static enum cmd_return | 1114 static enum cmd_return |
1067 cmd_quit(struct pwm_ctx *ctx, int argc, char *argv[]) | 1115 cmd_quit(struct pwm_ctx *ctx, int argc, char *argv[]) |