dav/main.c

changeset 470
6bf798ad3aec
parent 458
295eaa04fc41
child 471
d8e883bd1fd8
--- a/dav/main.c	Thu Sep 06 12:51:37 2018 +0200
+++ b/dav/main.c	Sat Sep 15 11:56:36 2018 +0200
@@ -46,6 +46,7 @@
 #include "error.h"
 #include "assistant.h"
 #include "system.h"
+#include "pwd.h"
 #include "main.h"
 
 static DavContext *ctx;
@@ -152,6 +153,8 @@
         } else if(!strcasecmp(cmd, "repository-url")
                 || !strcasecmp(cmd, "repo-url")) {
             ret = cmd_repository_url(args);
+        } else if(!strcasecmp(cmd, "add-user")) {
+            ret = cmd_add_user(args);
         } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version")
                 || !strcasecmp(cmd, "--version")) {
             fprintf(stderr, "dav %s\n", DAV_VERSION);
@@ -402,7 +405,35 @@
 }
 
 static DavSession* connect_to_repo(Repository *repo, CmdArgs *a) {
-    DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
+    char *user = repo->user;
+    char *password = repo->password;
+    if(repo->stored_user) {
+        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");
+        }
+    }
+    
+    DavSession *sn = dav_session_new_auth(ctx, repo->url, user, password);
     sn->flags = get_repository_flags(repo);
     sn->key = dav_context_get_key(ctx, repo->default_key);
     curl_easy_setopt(sn->handle, CURLOPT_HTTPAUTH, repo->authmethods);
@@ -2031,6 +2062,32 @@
     }
 }
 
+int cmd_add_user(CmdArgs *args) {
+    char *user = assistant_getcfg("User");
+    char *password = util_password_input("Password: ");
+    if(user && password) {
+        PwdStore *pstore = get_pwdstore();
+        if(!pstore) {
+            pstore = pwdstore_new();
+        }
+        pwdstore_put(pstore, user, password);
+        char *master_pw = util_password_input("Master password: ");
+        if(!master_pw) {
+            return 1;
+        }
+        if(pwdstore_setpassword(pstore, master_pw)) {
+            fprintf(stderr, "Error: Cannot generate key from password.\nAbort.\n");
+            return 1;
+        }
+        int ret = pwdstore_save(pstore);
+        if(ret) {
+            fprintf(stderr, "Error: saving password store failed.\n");
+        }
+        return ret;
+    }
+    return 1;
+}
+
 
 int shell_completion(CmdArgs *args, int index) {
     if(args->argc < 2 || args->argc < 3) {

mercurial