402 DavLock *lock = dav_create_lock(sn, locktoken, NULL); |
402 DavLock *lock = dav_create_lock(sn, locktoken, NULL); |
403 dav_add_collection_lock(sn, "/", lock); |
403 dav_add_collection_lock(sn, "/", lock); |
404 } |
404 } |
405 } |
405 } |
406 |
406 |
|
407 |
|
408 static int get_stored_credentials(CmdArgs *a, char *credid, char **user, char **password) { |
|
409 if(!credid) { |
|
410 return 0; |
|
411 } |
|
412 |
|
413 PwdStore *secrets = get_pwdstore(); |
|
414 if(!secrets) { |
|
415 fprintf(stderr, "Error: no secrets store available\n"); |
|
416 return 0; |
|
417 } |
|
418 |
|
419 if(pwdstore_has_id(secrets, credid)) { |
|
420 if(!secrets->isdecrypted) { |
|
421 if(cmd_getoption(a, "noinput")) { |
|
422 return 0; |
|
423 } |
|
424 char *ps_password = util_password_input("Master password: "); |
|
425 if(!ps_password) { |
|
426 return 0; |
|
427 } |
|
428 if(pwdstore_setpassword(secrets, ps_password)) { |
|
429 fprintf(stderr, "Error: cannot create key from password\n"); |
|
430 return 0; |
|
431 } |
|
432 if(pwdstore_decrypt(secrets)) { |
|
433 fprintf(stderr, "Error: cannot decrypt secrets store\n"); |
|
434 return 0; |
|
435 } |
|
436 } |
|
437 |
|
438 PwdEntry *s_cred = pwdstore_get(secrets, credid); |
|
439 if(s_cred) { |
|
440 *user = s_cred->user; |
|
441 *password = s_cred->password; |
|
442 return 1; |
|
443 } |
|
444 } else { |
|
445 fprintf(stderr, "Error: credentials id '%s' not found\n", credid); |
|
446 } |
|
447 |
|
448 return 0; |
|
449 } |
|
450 |
|
451 static int get_location_credentials(CmdArgs *a, Repository *repo, char **user, char **password) { |
|
452 return 0; |
|
453 } |
|
454 |
407 static DavSession* connect_to_repo(Repository *repo, CmdArgs *a) { |
455 static DavSession* connect_to_repo(Repository *repo, CmdArgs *a) { |
408 char *user = repo->user; |
456 char *user = repo->user; |
409 char *password = repo->password; |
457 char *password = repo->password; |
410 if(repo->stored_user && !cmd_getoption(a, "noinput")) { |
458 |
411 PwdStore *pstore = get_pwdstore(); |
459 if(!user && !password) { |
412 if(pstore) { |
460 if(!get_stored_credentials(a, repo->stored_user, &user, &password)) { |
413 char *ps_password = util_password_input("Unlock password store: "); |
461 get_location_credentials(a, repo, &user, &password); |
414 if(ps_password) { |
|
415 if(!pwdstore_setpassword(pstore, ps_password)) { |
|
416 if(!pwdstore_decrypt(pstore)) { |
|
417 PwdEntry *stored_user = pwdstore_get(pstore, repo->stored_user); |
|
418 if(stored_user) { |
|
419 user = stored_user->user; |
|
420 password = stored_user->password; |
|
421 } else { |
|
422 fprintf(stderr, "Error: stored user '%s' not found\n", repo->stored_user); |
|
423 } |
|
424 } else { |
|
425 fprintf(stderr, "Error: cannot decrypt password store\n"); |
|
426 } |
|
427 } else { |
|
428 fprintf(stderr, "Error: cannot create key from password\n"); |
|
429 } |
|
430 } |
|
431 } else { |
|
432 fprintf(stderr, "Error: no password store available\n"); |
|
433 } |
462 } |
434 } |
463 } |
435 |
464 |
436 DavSession *sn = dav_session_new_auth(ctx, repo->url, user, password); |
465 DavSession *sn = dav_session_new_auth(ctx, repo->url, user, password); |
437 sn->flags = get_repository_flags(repo); |
466 sn->flags = get_repository_flags(repo); |
2061 return -1; |
2090 return -1; |
2062 } |
2091 } |
2063 } |
2092 } |
2064 |
2093 |
2065 int cmd_add_user(CmdArgs *args) { |
2094 int cmd_add_user(CmdArgs *args) { |
|
2095 char *id = assistant_getcfg("Credentials identifier"); |
|
2096 |
2066 char *user = assistant_getcfg("User"); |
2097 char *user = assistant_getcfg("User"); |
2067 char *password = util_password_input("Password: "); |
2098 char *password = util_password_input("Password: "); |
2068 if(user && password) { |
2099 if(user && password) { |
2069 PwdStore *pstore = get_pwdstore(); |
2100 PwdStore *pstore = get_pwdstore(); |
2070 if(!pstore) { |
2101 if(!pstore) { |
2071 pstore = pwdstore_new(); |
2102 pstore = pwdstore_new(); |
2072 } |
2103 } |
2073 pwdstore_put(pstore, user, password); |
2104 pwdstore_put(pstore, id, NULL, user, password); |
2074 char *master_pw = util_password_input("Master password: "); |
2105 char *master_pw = util_password_input("Master password: "); |
2075 if(!master_pw) { |
2106 if(!master_pw) { |
2076 return 1; |
2107 return 1; |
2077 } |
2108 } |
2078 if(pwdstore_setpassword(pstore, master_pw)) { |
2109 if(pwdstore_setpassword(pstore, master_pw)) { |