Tue, 03 Sep 2013 12:08:35 +0200
code cleanup + experimental login prompt
dav/config.c | file | annotate | diff | comparison | revisions | |
dav/config.h | file | annotate | diff | comparison | revisions | |
dav/main.c | file | annotate | diff | comparison | revisions | |
libidav/methods.c | file | annotate | diff | comparison | revisions | |
libidav/webdav.c | file | annotate | diff | comparison | revisions | |
libidav/webdav.h | file | annotate | diff | comparison | revisions |
--- a/dav/config.c Mon Sep 02 10:50:29 2013 +0200 +++ b/dav/config.c Tue Sep 03 12:08:35 2013 +0200 @@ -272,11 +272,11 @@ return k; } -Repository* get_repository(char *name) { - if(!name) { +Repository* get_repository(sstr_t name) { + if(!name.ptr) { return NULL; } - return ucx_map_cstr_get(repos, name); + return ucx_map_sstr_get(repos, name); } Key* get_key(char *name) {
--- a/dav/config.h Mon Sep 02 10:50:29 2013 +0200 +++ b/dav/config.h Tue Sep 03 12:08:35 2013 +0200 @@ -29,6 +29,7 @@ #ifndef CONFIG_H #define CONFIG_H +#include <ucx/string.h> #include <stdbool.h> #ifdef __cplusplus @@ -81,7 +82,7 @@ void load_proxy(xmlNode *proxynode, int type); sstr_t load_key_file(char *filename); -Repository* get_repository(char *name); +Repository* get_repository(sstr_t name); Key* get_key(char *name); Proxy* get_http_proxy(); Proxy* get_https_proxy();
--- a/dav/main.c Mon Sep 02 10:50:29 2013 +0200 +++ b/dav/main.c Tue Sep 03 12:08:35 2013 +0200 @@ -78,7 +78,6 @@ } else if(!strcasecmp(cmd, "get")) { ret = cmd_get(args); } else if(!strcasecmp(cmd, "put")) { - printf("put\n"); ret = cmd_put(args); } else if( !strcasecmp(cmd, "remove") || @@ -133,9 +132,44 @@ fprintf(stderr, "\n"); } -void url_get_parts(char *url, char **root, char **path) { +int request_auth(Repository *repo, DavSession *sn) { + static int login = 0; + if(login) { + return 0; + } + + char *user = NULL; + char *password = NULL; + char ubuf[256]; + char pbuf[256]; + if(repo->user) { + user = repo->user; + } else { + printf("user: "); + fflush(stdout); + user = fgets(ubuf, 256, stdin); + } + + printf("password: "); + fflush(stdout); + password = fgets(pbuf, 256, stdin); + + size_t ulen = strlen(user); + if(user[ulen-1] == '\n') { + user[ulen-1] = '\0'; + } + size_t plen = strlen(password); + if(password[plen-1] == '\n') { + password[plen-1] = '\0'; + } + + dav_session_set_auth(sn, user, password); + login = 1; + return 1; +} + +Repository* url2repo(char *url, char **path) { size_t ulen = strlen(url); - *root = NULL; *path = NULL; int s; @@ -147,22 +181,33 @@ s = 1; } + sstr_t r = sstr(url); + sstr_t p = sstr("/"); for(int i=s;i<ulen;i++) { char c = url[i]; if(c == '/') { - sstr_t r = sstrn(url, i); - sstr_t p = sstrsubs(sstr(url), i); + r = sstrn(url, i); + p = sstrsubs(sstr(url), i); if(p.length == 0) { p = sstrn("/", 1); } - *root = sstrdup(r).ptr; - *path = sstrdup(p).ptr; - return; + break; } } - *root = strdup(url); - *path = strdup("/"); + Repository *repo = get_repository(r); + if(repo) { + *path = sstrdup(p).ptr; + } else { + repo = calloc(1, sizeof(Repository)); + repo->name = ""; + repo->url = strdup(url); + repo->store_key_property = true; + repo->decrypt = true; + *path = strdup("/"); + } + + return repo; } void print_resource_error(DavSession *sn, char *path) { @@ -204,21 +249,13 @@ return -1; } - DavSession *sn = NULL; char *url = a->argv[0]; - char *root = NULL; char *path = NULL; char *base = NULL; - url_get_parts(url, &root, &path); - - Repository *repo = get_repository(root); - if(repo) { - base = util_concat_path(repo->url, path); - sn = dav_session_new_auth(ctx, base, repo->user, repo->password); - } else { - base = util_concat_path(root, path); - sn = dav_session_new(ctx, base); - } + DavSession *sn = NULL; + Repository *repo = url2repo(url, &path); + base = util_concat_path(repo->url, path); + sn = dav_session_new_auth(ctx, base, repo->user, repo->password); char *update = cmd_getoption(a, "update"); time_t t = 0; @@ -226,55 +263,63 @@ t = util_parse_lastmodified(update); } + int ret = -1; DavResource *ls; - if(cmd_getoption(a, "recursive")) { - printf("base: %s\n", base); - if(update) { - ls = dav_query( - sn, - "get U:crypto-key from /* where lastmodified > %t", - t); + while(ret != 0) { + if(cmd_getoption(a, "recursive")) { + printf("base: %s\n", base); + if(update) { + ls = dav_query( + sn, + "get U:crypto-key from /* where lastmodified > %t", + t); + } else { + ls = dav_query(sn, "get U:crypto-key from /*"); + } } else { - ls = dav_query(sn, "get U:crypto-key from /*"); + if(update) { + ls = dav_query( + sn, + "get U:crypto-key from / where lastmodified > %t", t); + } else { + ls = dav_query(sn, "get U:crypto-key from /"); + } } - } else { - if(update) { - ls = dav_query( - sn, - "get U:crypto-key from / where lastmodified > %t", t); - } else { - ls = dav_query(sn, "get U:crypto-key from /"); + + if(!ls) { + if(sn->error == DAV_UNAUTHORIZED) { + if(request_auth(repo, sn)) { + continue; + } + } + print_resource_error(sn, path); + break; } - } - if(!ls) { - print_resource_error(sn, path); - free(root); - free(path); - free(base); - return -1; + + // parameters + int show_all = cmd_getoption(a, "all") ? 1 : 0; + void (*print_func)(DavResource*, CmdArgs *); + if(cmd_getoption(a, "list")) { + print_func = ls_print_list_elm; + } else { + print_func = ls_print_elm; + } + DavResource *child = ls->children; + while(child) { + if(child->name[0] != '.' || show_all) { + print_func(child, a); + } + child = child->next; + } + + // leave loop + ret = 0; } - // parameters - int show_all = cmd_getoption(a, "all") ? 1 : 0; - void (*print_func)(DavResource*, CmdArgs *); - if(cmd_getoption(a, "list")) { - print_func = ls_print_list_elm; - } else { - print_func = ls_print_elm; - } - DavResource *child = ls->children; - while(child) { - if(child->name[0] != '.' || show_all) { - print_func(child, a); - } - child = child->next; - } - - free(root); free(path); free(base); - return 0; + return ret; } static char* ls_date_str(time_t tm) { @@ -436,18 +481,11 @@ return -1; } - DavSession *sn = NULL; char *url = a->argv[0]; - char *root = NULL; char *path = NULL; - url_get_parts(url, &root, &path); - - Repository *repo = get_repository(root); - if(repo) { - sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); - } else { - sn = dav_session_new(ctx, root); - } + DavSession *sn = NULL; + Repository *repo = url2repo(url, &path); + sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); char *update = cmd_getoption(a, "update"); time_t t = 0; @@ -628,19 +666,12 @@ return -1; } - DavSession *sn = NULL; char *url = a->argv[0]; char *file = a->argv[1]; - char *root = NULL; char *path = NULL; - url_get_parts(url, &root, &path); - - Repository *repo = get_repository(root); - if(repo) { - sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); - } else { - sn = dav_session_new(ctx, root); - } + DavSession *sn = NULL; + Repository *repo = url2repo(url, &path); + sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); int ret; if(!strcmp(file, "-")) { @@ -782,18 +813,11 @@ return -1; } - DavSession *sn = NULL; char *url = a->argv[0]; - char *root = NULL; char *path = NULL; - url_get_parts(url, &root, &path); - - Repository *repo = get_repository(root); - if(repo) { - sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); - } else { - sn = dav_session_new(ctx, root); - } + DavSession *sn = NULL; + Repository *repo = url2repo(url, &path); + sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); DavResource *res = dav_resource_new(sn, path); if(!res) { @@ -817,18 +841,11 @@ return -1; } - DavSession *sn = NULL; char *url = a->argv[0]; - char *root = NULL; char *path = NULL; - url_get_parts(url, &root, &path); - - Repository *repo = get_repository(root); - if(repo) { - sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); - } else { - sn = dav_session_new(ctx, root); - } + DavSession *sn = NULL; + Repository *repo = url2repo(url, &path); + sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); DavResource *res = dav_resource_new(sn, path); if(!res) { @@ -869,18 +886,11 @@ size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT\n", date); fwrite(str, 1, len, stdout); } else if (a->argc == 1) { - DavSession *sn = NULL; char *url = a->argv[0]; - char *root = NULL; char *path = NULL; - url_get_parts(url, &root, &path); - - Repository *repo = get_repository(root); - if(repo) { - sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); - } else { - sn = dav_session_new(ctx, root); - } + DavSession *sn = NULL; + Repository *repo = url2repo(url, &path); + sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); DavResource *res = dav_resource_new(sn, path); char *date = NULL;
--- a/libidav/methods.c Mon Sep 02 10:50:29 2013 +0200 +++ b/libidav/methods.c Tue Sep 03 12:08:35 2013 +0200 @@ -439,7 +439,6 @@ /* ----------------------------- PUT ----------------------------- */ static size_t dummy_write(void *buf, size_t s, size_t n, void *data) { - fwrite(buf, s, n, stdout); return s*n; }
--- a/libidav/webdav.c Mon Sep 02 10:50:29 2013 +0200 +++ b/libidav/webdav.c Tue Sep 03 12:08:35 2013 +0200 @@ -137,6 +137,7 @@ DavSession *sn = malloc(sizeof(DavSession)); sn->errorstr = NULL; sn->error = DAV_OK; + sn->flags = 0; if(url.ptr[url.length - 1] == '/') { sn->base_url = strdup(base_url); } else { @@ -154,7 +155,7 @@ // set proxy DavProxy *proxy = sstrprefix(url, S("https")) ? context->https_proxy : context->http_proxy; - + if (proxy->url) { curl_easy_setopt(sn->handle, CURLOPT_PROXY, proxy->url); if (proxy->username) {