dav/main.c

changeset 655
4d33b672c33a
parent 654
8f2b8f2a5cde
child 673
8e7e56cfc103
equal deleted inserted replaced
654:8f2b8f2a5cde 655:4d33b672c33a
178 ret = cmd_add_user(args); 178 ret = cmd_add_user(args);
179 } else if(!strcasecmp(cmd, "list-users")) { 179 } else if(!strcasecmp(cmd, "list-users")) {
180 ret = cmd_list_users(args); 180 ret = cmd_list_users(args);
181 } else if(!strcasecmp(cmd, "remove-user")) { 181 } else if(!strcasecmp(cmd, "remove-user")) {
182 ret = cmd_remove_user(args); 182 ret = cmd_remove_user(args);
183 } else if(!strcasecmp(cmd, "edit-user")) {
184 ret = cmd_edit_user(args);
183 } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version") 185 } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version")
184 || !strcasecmp(cmd, "--version")) { 186 || !strcasecmp(cmd, "--version")) {
185 fprintf(stderr, "dav %s\n", DAV_VERSION); 187 fprintf(stderr, "dav %s\n", DAV_VERSION);
186 } else if(!strcasecmp(cmd, "complete")) { 188 } else if(!strcasecmp(cmd, "complete")) {
187 if(args->argc < 2) { 189 if(args->argc < 2) {
298 fprintf(stderr, "\n"); 300 fprintf(stderr, "\n");
299 fprintf(stderr, "Advanced commands:\n"); 301 fprintf(stderr, "Advanced commands:\n");
300 fprintf(stderr, " versioncontrol list-versions checkout checkin uncheckout\n\n"); 302 fprintf(stderr, " versioncontrol list-versions checkout checkin uncheckout\n\n");
301 fprintf(stderr, "Config commands:\n"); 303 fprintf(stderr, "Config commands:\n");
302 fprintf(stderr, " add-repository remove-repository list-repositories repository-url\n"); 304 fprintf(stderr, " add-repository remove-repository list-repositories repository-url\n");
303 fprintf(stderr, " add-user remove-user list-users\n"); 305 fprintf(stderr, " add-user remove-user edit-user list-users\n");
304 fprintf(stderr, " check-config\n"); 306 fprintf(stderr, " check-config\n");
305 fprintf(stderr, "\n"); 307 fprintf(stderr, "\n");
306 fprintf(stderr, 308 fprintf(stderr,
307 "Instead of an url you can pass a repository name " 309 "Instead of an url you can pass a repository name "
308 "with an optional path:\n"); 310 "with an optional path:\n");
2808 fprintf(stderr, "Identifier required.\n"); 2810 fprintf(stderr, "Identifier required.\n");
2809 return 1; 2811 return 1;
2810 } 2812 }
2811 if(!pwdstore_get(secrets, id)) { 2813 if(!pwdstore_get(secrets, id)) {
2812 fprintf(stderr, "Credentials with this id doesn't exist.\n"); 2814 fprintf(stderr, "Credentials with this id doesn't exist.\n");
2815 free(id);
2813 return 1; 2816 return 1;
2814 } 2817 }
2815 2818
2816 pwdstore_remove_entry(secrets, id); 2819 pwdstore_remove_entry(secrets, id);
2817 2820
2818 int ret = pwdstore_save(secrets); 2821 int ret = pwdstore_save(secrets);
2819 if(ret) { 2822 if(ret) {
2820 fprintf(stderr, "Error: saving srcrets store failed.\n"); 2823 fprintf(stderr, "Error: saving srcrets store failed.\n");
2821 } 2824 }
2825 free(id);
2822 return ret; 2826 return ret;
2823 } 2827 }
2824 2828
2825 int cmd_remove_user(CmdArgs *args) { 2829 int cmd_remove_user(CmdArgs *args) {
2826 return secretstore_cmd(args, FALSE, NULL, cmd_ss_remove_user, NULL); 2830 return secretstore_cmd(args, FALSE, NULL, cmd_ss_remove_user, NULL);
2827 } 2831 }
2832
2833 static void secrets_print_user_info(PwdStore *secrets, const char *id) {
2834 PwdEntry *entry = pwdstore_get(secrets, id);
2835 if(!entry) {
2836 return;
2837 }
2838
2839 PwdIndexEntry *index = ucx_map_cstr_get(secrets->index, id);
2840 if(!index) {
2841 return;
2842 }
2843
2844 printf("Id: %s\n", entry->id);
2845 printf("User: %s\n", entry->user);
2846 UCX_FOREACH(elm, index->locations) {
2847 printf("Location: %s\n", (char*)elm->data);
2848 }
2849 }
2850
2851 static void secrets_remove_location(PwdIndexEntry *index) {
2852 if(!index->locations) {
2853 printf("no locations\n");
2854 return;
2855 }
2856
2857 printf("0: abort\n");
2858 int i = 1;
2859 UCX_FOREACH(elm, index->locations) {
2860 printf("%d: %s\n", i, (char*)elm->data);
2861 i++;
2862 }
2863
2864 char *input = assistant_getcfg("Choose location");
2865 if(!input) {
2866 return;
2867 }
2868
2869 int64_t ln = 0;
2870 if(util_strtoint(input, &ln) && (ln >= 0 && ln < i)) {
2871 if(ln == 0) {
2872 return;
2873 } else {
2874 UcxList *elm = ucx_list_get(index->locations, ln - 1);
2875 if(elm) {
2876 free(elm->data);
2877 index->locations = ucx_list_remove(index->locations, elm);
2878 }
2879 }
2880 } else {
2881 printf("illegal input, choose 0 - %d\n", i-1);
2882 secrets_remove_location(index); // try again
2883 }
2884 }
2885
2886 static int cmd_ss_edit_user(CmdArgs *args, PwdStore *secrets, void *ud) {
2887 char *id = assistant_getcfg("Credentials identifier");
2888 if(!id) {
2889 fprintf(stderr, "Identifier required.\n");
2890 return 1;
2891 }
2892 PwdEntry *entry = pwdstore_get(secrets, id);
2893 PwdIndexEntry *index = ucx_map_cstr_get(secrets->index, id);
2894 if(!entry || !index) {
2895 fprintf(stderr, "Credentials with this id doesn't exist.\n");
2896 return 1;
2897 }
2898
2899 secrets_print_user_info(secrets, id);
2900
2901 int save = 0;
2902 int loop = 1;
2903 while(loop) {
2904 printf("\n");
2905 printf("0: change user name\n");
2906 printf("1: change password\n");
2907 printf("2: add location\n");
2908 printf("3: remove location\n");
2909 printf("4: list locations\n");
2910 printf("5: save and exit\n");
2911 printf("6: exit without saving\n");
2912
2913 char *opt = assistant_getcfg("Choose action");
2914 if(!opt) {
2915 break;
2916 }
2917 int64_t mnu = 0;
2918 if(util_strtoint(opt, &mnu) && (mnu >= 0 && mnu <= 6)) {
2919 printf("\n");
2920 switch(mnu) {
2921 case 0: {
2922 // change user name
2923 char *user = assistant_getcfg("User");
2924 if(user) {
2925 if(entry->user) {
2926 free(entry->user);
2927 }
2928 entry->user = user;
2929 }
2930 break;
2931 }
2932 case 1: {
2933 // change password
2934 char *password = util_password_input("Password: ");
2935 if(password) {
2936 if(entry->password) {
2937 free(entry->password);
2938 }
2939 entry->password = password;
2940 }
2941 break;
2942 }
2943 case 2: {
2944 // add location
2945 char *location = assistant_getoptcfg("Location");
2946 if(location) {
2947 index->locations = ucx_list_append(index->locations, location);
2948 }
2949 break;
2950 }
2951 case 3: {
2952 // remove location
2953 secrets_remove_location(index);
2954 break;
2955 }
2956 case 4: {
2957 // list locations
2958 if(!index->locations) {
2959 printf("no locations\n");
2960 } else {
2961 UCX_FOREACH(elm, index->locations) {
2962 printf("Location: %s\n", (char*)elm->data);
2963 }
2964 }
2965 break;
2966 }
2967 case 5: {
2968 // save and exit
2969 loop = 0;
2970 save = 1;
2971 break;
2972 }
2973 case 6: {
2974 // exit without saving
2975 loop = 0;
2976 break;
2977 }
2978 }
2979 } else {
2980 printf("illegal input, choose 0 - 5\n");
2981 }
2982 free(opt);
2983 }
2984
2985 int ret = 0;
2986 if(save) {
2987 ret = pwdstore_save(secrets);
2988 if(ret) {
2989 fprintf(stderr, "Error: saving srcrets store failed.\n");
2990 }
2991 }
2992 return ret;
2993 }
2994
2995 int cmd_edit_user(CmdArgs *args) {
2996 return secretstore_cmd(args, FALSE, NULL, cmd_ss_edit_user, NULL);
2997 }
2998
2828 2999
2829 int shell_completion(CmdArgs *args, int index) { 3000 int shell_completion(CmdArgs *args, int index) {
2830 if(args->argc < 2 || args->argc < 3) { 3001 if(args->argc < 2 || args->argc < 3) {
2831 return 1; 3002 return 1;
2832 } 3003 }

mercurial