464 } |
464 } |
465 |
465 |
466 return 0; |
466 return 0; |
467 } |
467 } |
468 |
468 |
469 static int cmp_url_cred_entry(PwdIndexEntry *e1, PwdIndexEntry *e2, void *n) { |
469 typedef struct CredLocation { |
|
470 char *id; |
|
471 char *location; |
|
472 } CredLocation; |
|
473 |
|
474 static int cmp_url_cred_entry(CredLocation *e1, CredLocation *e2, void *n) { |
470 return strcmp(e2->location, e1->location); |
475 return strcmp(e2->location, e1->location); |
|
476 } |
|
477 |
|
478 static void free_cred_location(CredLocation *c) { |
|
479 // c->id is not a copy, therefore we don't have to free it |
|
480 free(c->location); |
|
481 free(c); |
471 } |
482 } |
472 |
483 |
473 static int get_location_credentials(CmdArgs *a, Repository *repo, char *path, char **user, char **password) { |
484 static int get_location_credentials(CmdArgs *a, Repository *repo, char *path, char **user, char **password) { |
474 PwdStore *secrets = get_pwdstore(); |
485 PwdStore *secrets = get_pwdstore(); |
475 if(!secrets) { |
486 if(!secrets) { |
481 * location strings. We need a list, that contains only urls |
492 * location strings. We need a list, that contains only urls |
482 */ |
493 */ |
483 UcxList *locations = NULL; |
494 UcxList *locations = NULL; |
484 UCX_FOREACH(elm, secrets->locations) { |
495 UCX_FOREACH(elm, secrets->locations) { |
485 PwdIndexEntry *e = elm->data; |
496 PwdIndexEntry *e = elm->data; |
486 char *path; |
497 |
487 Repository *r = url2repo(e->location, &path); |
498 UCX_FOREACH(loc, e->locations) { |
488 PwdIndexEntry *urlentry = calloc(1, sizeof(PwdEntry)); |
499 char *path; |
489 urlentry->id = strdup(e->id); |
500 Repository *r = url2repo(loc->data, &path); |
490 urlentry->location = util_concat_path(r->url, path); |
501 CredLocation *urlentry = calloc(1, sizeof(CredLocation)); |
491 locations = ucx_list_append(locations, urlentry); |
502 urlentry->id = e->id; |
|
503 urlentry->location = util_concat_path(r->url, path); |
|
504 locations = ucx_list_append(locations, urlentry); |
|
505 } |
492 } |
506 } |
493 // the list must be sorted |
507 // the list must be sorted |
494 locations = ucx_list_sort(locations, (cmp_func)cmp_url_cred_entry, NULL); |
508 locations = ucx_list_sort(locations, (cmp_func)cmp_url_cred_entry, NULL); |
495 |
509 |
496 // create full request url string and remove protocol prefix |
510 // create full request url string and remove protocol prefix |
505 // iterate over sorted locations and check if a location is a prefix |
519 // iterate over sorted locations and check if a location is a prefix |
506 // of the requested url |
520 // of the requested url |
507 char *id = NULL; |
521 char *id = NULL; |
508 int ret = 0; |
522 int ret = 0; |
509 UCX_FOREACH(elm, locations) { |
523 UCX_FOREACH(elm, locations) { |
510 PwdIndexEntry *cred = elm->data; |
524 CredLocation *cred = elm->data; |
511 sstr_t cred_url = sstr(cred->location); |
525 sstr_t cred_url = sstr(cred->location); |
512 |
526 |
513 // remove protocol prefix |
527 // remove protocol prefix |
514 if(sstrprefix(cred_url, S("http://"))) { |
528 if(sstrprefix(cred_url, S("http://"))) { |
515 cred_url = sstrsubs(cred_url, 7); |
529 cred_url = sstrsubs(cred_url, 7); |
531 *password = cred->password; |
545 *password = cred->password; |
532 ret = 1; |
546 ret = 1; |
533 } |
547 } |
534 |
548 |
535 free(req_url_proto.ptr); |
549 free(req_url_proto.ptr); |
536 ucx_list_free_content(locations, (ucx_destructor)pwdstore_free_entry); |
550 ucx_list_free_content(locations, (ucx_destructor)free_cred_location); |
|
551 ucx_list_free(locations); |
537 |
552 |
538 return ret; |
553 return ret; |
539 } |
554 } |
540 |
555 |
541 static DavSession* connect_to_repo(Repository *repo, char *path, CmdArgs *a) { |
556 static DavSession* connect_to_repo(Repository *repo, char *path, CmdArgs *a) { |
2426 } |
2441 } |
2427 |
2442 |
2428 char *user = assistant_getcfg("User"); |
2443 char *user = assistant_getcfg("User"); |
2429 char *password = util_password_input("Password: "); |
2444 char *password = util_password_input("Password: "); |
2430 char *location = assistant_getoptcfg("Location"); |
2445 char *location = assistant_getoptcfg("Location"); |
|
2446 UcxList *locations = location ? ucx_list_append(NULL, location) : NULL; |
2431 int ret = 1; |
2447 int ret = 1; |
2432 if(user && password) { |
2448 if(user && password) { |
2433 pwdstore_put_index(secrets, id, location); |
2449 pwdstore_put_index(secrets, id, locations); |
2434 pwdstore_put(secrets, id, user, password); |
2450 pwdstore_put(secrets, id, user, password); |
2435 int ret = pwdstore_save(secrets); |
2451 int ret = pwdstore_save(secrets); |
2436 if(ret) { |
2452 if(ret) { |
2437 fprintf(stderr, "Error: saving srcrets store failed.\n"); |
2453 fprintf(stderr, "Error: saving srcrets store failed.\n"); |
2438 } |
2454 } |