dav/main.c

changeset 472
08d2d1263429
parent 471
d8e883bd1fd8
child 473
6740adb5fccd
--- a/dav/main.c	Sat Sep 15 11:58:17 2018 +0200
+++ b/dav/main.c	Thu Sep 20 13:07:38 2018 +0200
@@ -404,32 +404,61 @@
     }
 }
 
+
+static int get_stored_credentials(CmdArgs *a, char *credid, char **user, char **password) {
+    if(!credid) {
+        return 0;
+    }
+    
+    PwdStore *secrets = get_pwdstore();
+    if(!secrets) {
+        fprintf(stderr, "Error: no secrets store available\n");
+        return 0;
+    }
+    
+    if(pwdstore_has_id(secrets, credid)) {
+        if(!secrets->isdecrypted) {
+            if(cmd_getoption(a, "noinput")) {
+                return 0;
+            }
+            char *ps_password = util_password_input("Master password: ");
+            if(!ps_password) {
+                return 0;
+            }
+            if(pwdstore_setpassword(secrets, ps_password)) {
+                fprintf(stderr, "Error: cannot create key from password\n");
+                return 0;
+            }
+            if(pwdstore_decrypt(secrets)) {
+                fprintf(stderr, "Error: cannot decrypt secrets store\n");
+                return 0;
+            }
+        }
+        
+        PwdEntry *s_cred = pwdstore_get(secrets, credid);
+        if(s_cred) {
+            *user = s_cred->user;
+            *password = s_cred->password;
+            return 1;
+        }
+    } else {
+        fprintf(stderr, "Error: credentials id '%s' not found\n", credid);
+    }
+    
+    return 0;
+}
+
+static int get_location_credentials(CmdArgs *a, Repository *repo, char **user, char **password) {
+    return 0;
+}
+
 static DavSession* connect_to_repo(Repository *repo, CmdArgs *a) {
     char *user = repo->user;
     char *password = repo->password;
-    if(repo->stored_user && !cmd_getoption(a, "noinput")) {
-        PwdStore *pstore = get_pwdstore();
-        if(pstore) {
-            char *ps_password = util_password_input("Unlock password store: ");
-            if(ps_password) {
-                if(!pwdstore_setpassword(pstore, ps_password)) {
-                    if(!pwdstore_decrypt(pstore))  {
-                        PwdEntry *stored_user = pwdstore_get(pstore, repo->stored_user);
-                        if(stored_user) {
-                            user = stored_user->user;
-                            password = stored_user->password;
-                        } else {
-                            fprintf(stderr, "Error: stored user '%s' not found\n", repo->stored_user);
-                        }
-                    } else {
-                        fprintf(stderr, "Error: cannot decrypt password store\n");
-                    }
-                } else {
-                    fprintf(stderr, "Error: cannot create key from password\n");
-                }
-            }
-        } else {
-            fprintf(stderr, "Error: no password store available\n");
+    
+    if(!user && !password) {
+        if(!get_stored_credentials(a, repo->stored_user, &user, &password)) {
+            get_location_credentials(a, repo, &user, &password);
         }
     }
     
@@ -2063,6 +2092,8 @@
 }
 
 int cmd_add_user(CmdArgs *args) {
+    char *id = assistant_getcfg("Credentials identifier");
+    
     char *user = assistant_getcfg("User");
     char *password = util_password_input("Password: ");
     if(user && password) {
@@ -2070,7 +2101,7 @@
         if(!pstore) {
             pstore = pwdstore_new();
         }
-        pwdstore_put(pstore, user, password);
+        pwdstore_put(pstore, id, NULL, user, password);
         char *master_pw = util_password_input("Master password: ");
         if(!master_pw) {
             return 1;

mercurial