Fri, 29 May 2015 09:48:10 +0200
added initial code for config commands
dav/config.c | file | annotate | diff | comparison | revisions | |
dav/config.h | file | annotate | diff | comparison | revisions | |
dav/main.c | file | annotate | diff | comparison | revisions | |
dav/main.h | file | annotate | diff | comparison | revisions | |
dav/optparser.c | file | annotate | diff | comparison | revisions | |
libidav/utils.c | file | annotate | diff | comparison | revisions | |
libidav/utils.h | file | annotate | diff | comparison | revisions |
--- a/dav/config.c Thu May 28 14:34:46 2015 +0200 +++ b/dav/config.c Fri May 29 09:48:10 2015 +0200 @@ -136,82 +136,30 @@ ucx_map_free(keys); } -void load_repository(xmlNode *reponode) { - xmlNode *node = reponode->children; +Repository* repository_new() { Repository *repo = calloc(1, sizeof(Repository)); repo->encrypt_name = false; repo->encrypt_content = false; repo->decrypt_name = false; repo->decrypt_content = true; repo->ssl_version = CURL_SSLVERSION_DEFAULT; + return repo; +} + +void load_repository(xmlNode *reponode) { + xmlNode *node = reponode->children; + Repository *repo = repository_new(); while(node) { if(node->type == XML_ELEMENT_NODE) { + char *name = (char*)node->name; char *value = util_xml_get_text(node); - if(!value) { - // next - } else if(xstreq(node->name, "name")) { - repo->name = strdup(value); - } else if(xstreq(node->name, "url")) { - repo->url = strdup(value); - } else if(xstreq(node->name, "user")) { - repo->user = strdup(value); - } else if(xstreq(node->name, "password")) { - repo->password = util_base64decode(value); - } else if(xstreq(node->name, "default-key")) { - repo->default_key = strdup(value); - } else if(xstreq(node->name, "full-encryption")) { - if(util_getboolean(value)) { - repo->encrypt_name = true; - repo->encrypt_content = true; - repo->decrypt_name = true; - repo->decrypt_content = true; - } - } else if(xstreq(node->name, "content-encryption")) { - if(util_getboolean(value)) { - repo->encrypt_content = true; - repo->decrypt_content = true; - } else { - repo->encrypt_content = false; + if(value) { + if(repo_add_config(repo, name, value)) { + fprintf( + stderr, + "Unkown repository config element: %s\n", + name); } - } else if(xstreq(node->name, "decrypt-content")) { - repo->decrypt_content = util_getboolean(value); - } else if(xstreq(node->name, "decrypt-name")) { - repo->decrypt_name = util_getboolean(value); - } else if(xstreq(node->name, "ssl-version")) { - if(xstrEQ(value, "TLSv1")) { - repo->ssl_version = CURL_SSLVERSION_TLSv1; - } else if(xstrEQ(value, "SSLv2")) { - repo->ssl_version = CURL_SSLVERSION_SSLv2; - } else if(xstrEQ(value, "SSLv3")) { - repo->ssl_version = CURL_SSLVERSION_SSLv3; - } -#if LIBCURL_VERSION_MAJOR >= 7 -#if LIBCURL_VERSION_MINOR >= 34 - else if(xstrEQ(value, "TLSv1.0")) { - repo->ssl_version = CURL_SSLVERSION_TLSv1_0; - } else if(xstrEQ(value, "TLSv1.1")) { - repo->ssl_version = CURL_SSLVERSION_TLSv1_1; - } else if(xstrEQ(value, "TLSv1.2")) { - repo->ssl_version = CURL_SSLVERSION_TLSv1_2; - } -#endif -#endif - else { - fprintf(stderr, "Unknown ssl version: %s\n", value); - } - } else if(xstreq(node->name, "encrypt") || xstreq(node->name, "store-key-property") || xstreq(node->name, "decrypt")) { - fprintf(stderr, "Error: config.xml contains deprecated elements\n"); - fprintf(stderr, "The elements <encrypt>, <decrypt> and <store-key-property> are removed\n"); - fprintf(stderr, "Use the following: \n\n"); - fprintf(stderr, "<content-encryption>true</content-encryption>\n"); - fprintf(stderr, "enables file content encryption and decryption\n\n"); - fprintf(stderr, "<full-encryption>true</full-encryption>\n"); - fprintf(stderr, "enables content and file name encryption/decryption\n\n"); - fprintf(stderr, "<decrypt-content>$BOOL</decrypt-content>\n"); - fprintf(stderr, "only enables/disables content decryption\n\n"); - fprintf(stderr, "<decrypt-name>$BOOL</decrypt-name>\n"); - fprintf(stderr, "only enables/disables name decryption\n\n"); - exit(-1); } } node = node->next; @@ -236,6 +184,63 @@ ucx_map_cstr_put(repos, repo->name, repo); } +int repo_add_config(Repository *repo, char *key, char *value) { + if(xstreq(key, "name")) { + repo->name = strdup(value); + } else if(xstreq(key, "url")) { + repo->url = strdup(value); + } else if(xstreq(key, "user")) { + repo->user = strdup(value); + } else if(xstreq(key, "password")) { + repo->password = util_base64decode(value); + } else if(xstreq(key, "default-key")) { + repo->default_key = strdup(value); + } else if(xstreq(key, "full-encryption")) { + if(util_getboolean(value)) { + repo->encrypt_name = true; + repo->encrypt_content = true; + repo->decrypt_name = true; + repo->decrypt_content = true; + } + } else if(xstreq(key, "content-encryption")) { + if(util_getboolean(value)) { + repo->encrypt_content = true; + repo->decrypt_content = true; + } else { + repo->encrypt_content = false; + } + } else if(xstreq(key, "decrypt-content")) { + repo->decrypt_content = util_getboolean(value); + } else if(xstreq(key, "decrypt-name")) { + repo->decrypt_name = util_getboolean(value); + } else if(xstreq(key, "ssl-version")) { + if(xstrEQ(value, "TLSv1")) { + repo->ssl_version = CURL_SSLVERSION_TLSv1; + } else if(xstrEQ(value, "SSLv2")) { + repo->ssl_version = CURL_SSLVERSION_SSLv2; + } else if(xstrEQ(value, "SSLv3")) { + repo->ssl_version = CURL_SSLVERSION_SSLv3; + } +#if LIBCURL_VERSION_MAJOR >= 7 +#if LIBCURL_VERSION_MINOR >= 34 + else if(xstrEQ(value, "TLSv1.0")) { + repo->ssl_version = CURL_SSLVERSION_TLSv1_0; + } else if(xstrEQ(value, "TLSv1.1")) { + repo->ssl_version = CURL_SSLVERSION_TLSv1_1; + } else if(xstrEQ(value, "TLSv1.2")) { + repo->ssl_version = CURL_SSLVERSION_TLSv1_2; + } +#endif +#endif + else { + fprintf(stderr, "Unknown ssl version: %s\n", value); + } + } else { + return -1; + } + return 0; +} + void load_proxy(xmlNode *proxynode, int type) { Proxy *proxy; const char *stype; @@ -393,3 +398,4 @@ Proxy* get_https_proxy() { return https_proxy; } +
--- a/dav/config.h Thu May 28 14:34:46 2015 +0200 +++ b/dav/config.h Fri May 29 09:48:10 2015 +0200 @@ -79,7 +79,10 @@ void load_proxy(xmlNode *proxynode, int type); sstr_t load_key_file(char *filename); +Repository* repository_new(); + Repository* get_repository(sstr_t name); +int repo_add_config(Repository *repo, char *key, char *value); int get_repository_flags(Repository *repo); DavSession *repository_session(Repository *repo); Key* get_key(char *name);
--- a/dav/main.c Thu May 28 14:34:46 2015 +0200 +++ b/dav/main.c Fri May 29 09:48:10 2015 +0200 @@ -109,6 +109,8 @@ ret = cmd_get_property(args); } else if(!strcasecmp(cmd, "info")) { ret = cmd_info(args); + } else if(!strcasecmp(cmd, "add-repository")) { + ret = cmd_add_repository(args); } else { print_usage(argv[0]); } @@ -1118,3 +1120,120 @@ } } +/* ---------- config commands ---------- */ + +static int getkeyvalue(char *arg, char **key, char **value) { + // splits a key=value arg + + *key = NULL; + *value = NULL; + if(!arg && !key && !value) { + return -1; + } + + int haskey = 0; + size_t len = strlen(arg); + + if(len < 3) { + return -1; + } + + int i; + for(i=0;i<len;i++) { + if(arg[i] == '=') { + haskey = 1; + break; + } + } + + if(haskey) { + sstr_t k = sstrn(arg, i); + sstr_t v = sstrn(arg+i + 1, len - i - 1); + if(k.length > 0 && v.length > 0) { + *key = sstrdup(k).ptr; + *value = sstrdup(v).ptr; + return 0; + } + } + + return -1; +} + +void repository_assistent(Repository *repo, int mode) { + char *name = NULL; + sstr_t line; + + // name + while(!name) { + if(repo->name) { + printf("repository name [%s]: ", repo->name); + } else { + printf("repository name: "); + } + fflush(stdout); + + line = util_readline(stdin); + if(line.length == 0 && repo->name) { + break; + } else { + name = line.ptr; + } + } + if(name) { + if(repo->name) { + free(repo->name); + } + repo->name = name; + } + + // TODO + + return; +} + +int cmd_add_repository(CmdArgs *a) { + char *interactive = cmd_getoption(a, "interactive"); + + if(!interactive && a->argc < 2) { + fprintf(stderr, "Too few arguments\n"); + fprintf(stderr, "Alternatively, use the interactive mode"); + return -1; + } + + Repository *repo = repository_new(); + // create repo object and fill config with cmd args + for(int i=0;i<a->argc;i++) { + char *key; + char *value; + if(!getkeyvalue(a->argv[i], &key, &value)) { + if(!strcmp(key, "password")) { + // passwords from command line args are plain text + // but the repository needs a base64 encoded password + char *pw = util_base64encode(value, strlen(value)); + free(value); + value = pw; + } + + if(repo_add_config(repo, key, value)) { + fprintf( + stderr, + "Unkown repository config element: %s\n", + key); + } + + free(key); + free(value); + } else { + fprintf(stderr, "´%s´ not in key=value format\n", a->argv[i]); + // TODO: cleanup + return -1; + } + } + + if(interactive) { + // start interactive assistent to adjust config + repository_assistent(repo, 0); + } + + return 0; +}
--- a/dav/main.h Thu May 28 14:34:46 2015 +0200 +++ b/dav/main.h Fri May 29 09:48:10 2015 +0200 @@ -62,6 +62,13 @@ char* stdin2str(); +void repository_assistent(Repository *repo, int mode); + +int cmd_add_repository(CmdArgs *args); +int cmd_modify_repository(CmdArgs *args); +int cmd_delete_repository(CmdArgs *args); +int cmd_list_repositories(CmdArgs *args); + #ifdef __cplusplus } #endif
--- a/dav/optparser.c Thu May 28 14:34:46 2015 +0200 +++ b/dav/optparser.c Fri May 29 09:48:10 2015 +0200 @@ -143,6 +143,10 @@ } break; } + case 'i': { + ucx_map_cstr_put(a->options, "interactive", NOARG); + break; + } } } } else if(option) {
--- a/libidav/utils.c Thu May 28 14:34:46 2015 +0200 +++ b/libidav/utils.c Fri May 29 09:48:10 2015 +0200 @@ -384,3 +384,16 @@ return str; } } + +sstr_t util_readline(FILE *stream) { + UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); + + int c; + while((c = fgetc(stream)) != '\n') { + ucx_buffer_putc(buf, c); + } + + sstr_t str = sstrdup(sstrtrim(sstrn(buf->space, buf->size))); + ucx_buffer_free(buf); + return str; +}