# HG changeset patch # User Olaf Wintermann # Date 1570880522 -7200 # Node ID 4d33b672c33a39c04a1a2e9ec1ebdaa98fff4e68 # Parent 8f2b8f2a5cdef0150b0f35197aae22d9286d0550 add edit-user command diff -r 8f2b8f2a5cde -r 4d33b672c33a dav/main.c --- a/dav/main.c Sat Oct 12 11:17:11 2019 +0200 +++ b/dav/main.c Sat Oct 12 13:42:02 2019 +0200 @@ -180,6 +180,8 @@ ret = cmd_list_users(args); } else if(!strcasecmp(cmd, "remove-user")) { ret = cmd_remove_user(args); + } else if(!strcasecmp(cmd, "edit-user")) { + ret = cmd_edit_user(args); } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version") || !strcasecmp(cmd, "--version")) { fprintf(stderr, "dav %s\n", DAV_VERSION); @@ -300,7 +302,7 @@ fprintf(stderr, " versioncontrol list-versions checkout checkin uncheckout\n\n"); fprintf(stderr, "Config commands:\n"); fprintf(stderr, " add-repository remove-repository list-repositories repository-url\n"); - fprintf(stderr, " add-user remove-user list-users\n"); + fprintf(stderr, " add-user remove-user edit-user list-users\n"); fprintf(stderr, " check-config\n"); fprintf(stderr, "\n"); fprintf(stderr, @@ -2810,6 +2812,7 @@ } if(!pwdstore_get(secrets, id)) { fprintf(stderr, "Credentials with this id doesn't exist.\n"); + free(id); return 1; } @@ -2819,6 +2822,7 @@ if(ret) { fprintf(stderr, "Error: saving srcrets store failed.\n"); } + free(id); return ret; } @@ -2826,6 +2830,173 @@ return secretstore_cmd(args, FALSE, NULL, cmd_ss_remove_user, NULL); } +static void secrets_print_user_info(PwdStore *secrets, const char *id) { + PwdEntry *entry = pwdstore_get(secrets, id); + if(!entry) { + return; + } + + PwdIndexEntry *index = ucx_map_cstr_get(secrets->index, id); + if(!index) { + return; + } + + printf("Id: %s\n", entry->id); + printf("User: %s\n", entry->user); + UCX_FOREACH(elm, index->locations) { + printf("Location: %s\n", (char*)elm->data); + } +} + +static void secrets_remove_location(PwdIndexEntry *index) { + if(!index->locations) { + printf("no locations\n"); + return; + } + + printf("0: abort\n"); + int i = 1; + UCX_FOREACH(elm, index->locations) { + printf("%d: %s\n", i, (char*)elm->data); + i++; + } + + char *input = assistant_getcfg("Choose location"); + if(!input) { + return; + } + + int64_t ln = 0; + if(util_strtoint(input, &ln) && (ln >= 0 && ln < i)) { + if(ln == 0) { + return; + } else { + UcxList *elm = ucx_list_get(index->locations, ln - 1); + if(elm) { + free(elm->data); + index->locations = ucx_list_remove(index->locations, elm); + } + } + } else { + printf("illegal input, choose 0 - %d\n", i-1); + secrets_remove_location(index); // try again + } +} + +static int cmd_ss_edit_user(CmdArgs *args, PwdStore *secrets, void *ud) { + char *id = assistant_getcfg("Credentials identifier"); + if(!id) { + fprintf(stderr, "Identifier required.\n"); + return 1; + } + PwdEntry *entry = pwdstore_get(secrets, id); + PwdIndexEntry *index = ucx_map_cstr_get(secrets->index, id); + if(!entry || !index) { + fprintf(stderr, "Credentials with this id doesn't exist.\n"); + return 1; + } + + secrets_print_user_info(secrets, id); + + int save = 0; + int loop = 1; + while(loop) { + printf("\n"); + printf("0: change user name\n"); + printf("1: change password\n"); + printf("2: add location\n"); + printf("3: remove location\n"); + printf("4: list locations\n"); + printf("5: save and exit\n"); + printf("6: exit without saving\n"); + + char *opt = assistant_getcfg("Choose action"); + if(!opt) { + break; + } + int64_t mnu = 0; + if(util_strtoint(opt, &mnu) && (mnu >= 0 && mnu <= 6)) { + printf("\n"); + switch(mnu) { + case 0: { + // change user name + char *user = assistant_getcfg("User"); + if(user) { + if(entry->user) { + free(entry->user); + } + entry->user = user; + } + break; + } + case 1: { + // change password + char *password = util_password_input("Password: "); + if(password) { + if(entry->password) { + free(entry->password); + } + entry->password = password; + } + break; + } + case 2: { + // add location + char *location = assistant_getoptcfg("Location"); + if(location) { + index->locations = ucx_list_append(index->locations, location); + } + break; + } + case 3: { + // remove location + secrets_remove_location(index); + break; + } + case 4: { + // list locations + if(!index->locations) { + printf("no locations\n"); + } else { + UCX_FOREACH(elm, index->locations) { + printf("Location: %s\n", (char*)elm->data); + } + } + break; + } + case 5: { + // save and exit + loop = 0; + save = 1; + break; + } + case 6: { + // exit without saving + loop = 0; + break; + } + } + } else { + printf("illegal input, choose 0 - 5\n"); + } + free(opt); + } + + int ret = 0; + if(save) { + ret = pwdstore_save(secrets); + if(ret) { + fprintf(stderr, "Error: saving srcrets store failed.\n"); + } + } + return ret; +} + +int cmd_edit_user(CmdArgs *args) { + return secretstore_cmd(args, FALSE, NULL, cmd_ss_edit_user, NULL); +} + + int shell_completion(CmdArgs *args, int index) { if(args->argc < 2 || args->argc < 3) { return 1; diff -r 8f2b8f2a5cde -r 4d33b672c33a dav/main.h --- a/dav/main.h Sat Oct 12 11:17:11 2019 +0200 +++ b/dav/main.h Sat Oct 12 13:42:02 2019 +0200 @@ -133,6 +133,7 @@ int cmd_add_user(CmdArgs *args); int cmd_list_users(CmdArgs *args); int cmd_remove_user(CmdArgs *args); +int cmd_edit_user(CmdArgs *args); int shell_completion(CmdArgs *args, int index);