dav/main.c

changeset 45
e3839719b079
parent 43
03076907b58a
child 46
0542668d0f26
equal deleted inserted replaced
44:e4e23a88d4de 45:e3839719b079
57 int main(int argc, char **argv) { 57 int main(int argc, char **argv) {
58 xmlGenericErrorFunc fnc = xmlerrorfnc; 58 xmlGenericErrorFunc fnc = xmlerrorfnc;
59 initGenericErrorDefaultFunc(&fnc); 59 initGenericErrorDefaultFunc(&fnc);
60 ctx = dav_context_new(); 60 ctx = dav_context_new();
61 load_config(ctx); 61 load_config(ctx);
62 dav_add_namespace(ctx, "U", "http://www.uap-core.de/"); 62 //dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
63 63
64 //test(); 64 //test();
65 65
66 memcpy(ctx->http_proxy, get_http_proxy(), sizeof(Proxy)); 66 memcpy(ctx->http_proxy, get_http_proxy(), sizeof(Proxy));
67 memcpy(ctx->https_proxy, get_https_proxy(), sizeof(Proxy)); 67 memcpy(ctx->https_proxy, get_https_proxy(), sizeof(Proxy));
105 } 105 }
106 106
107 void print_usage(char *cmd) { 107 void print_usage(char *cmd) {
108 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd); 108 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd);
109 fprintf(stderr, "Commands:\n"); 109 fprintf(stderr, "Commands:\n");
110 fprintf(stderr, " list [-altR] [-u <date>] <url>\n"); 110 fprintf(stderr, " list [-altpcR] [-u <date>] <url>\n");
111 fprintf( 111 fprintf(
112 stderr, 112 stderr,
113 " get [-pR] [-o <file>] [-u <date>] <url>\n"); 113 " get [-pcR] [-o <file>] [-u <date>] <url>\n");
114 fprintf(stderr, " put [-pR] [-k <key>] <url> <file>\n"); 114 fprintf(stderr, " put [-pcR] [-k <key>] <url> <file>\n");
115 fprintf(stderr, " mkdir [-p] [-k <key>] <url>\n"); 115 fprintf(stderr, " mkdir [-pc] [-k <key>] <url>\n");
116 fprintf(stderr, " remove <url>\n"); 116 fprintf(stderr, " remove [-pc] <url>\n");
117 fprintf(stderr, " date [url]\n"); 117 fprintf(stderr, " date [url]\n");
118 fprintf(stderr, "\n"); 118 fprintf(stderr, "\n");
119 fprintf(stderr, "Options:\n"); 119 fprintf(stderr, "Options:\n");
120 fprintf(stderr, 120 fprintf(stderr,
121 " -k <key> Key to use for encryption or decryption\n"); 121 " -k <key> Key to use for encryption or decryption\n");
122 fprintf(stderr, " -p Don't encrypt or decrypt files\n"); 122 fprintf(stderr, " -p Don't encrypt or decrypt files\n");
123 fprintf(stderr, " -c Enable full encryption\n");
123 fprintf(stderr, 124 fprintf(stderr,
124 " -R " 125 " -R "
125 "Recursively do the operation for all children\n"); 126 "Recursively do the operation for all children\n");
126 fprintf(stderr, " -o <file> Write output to file\n"); 127 fprintf(stderr, " -o <file> Write output to file\n");
127 fprintf( 128 fprintf(
246 } 247 }
247 } 248 }
248 free(res_url); 249 free(res_url);
249 } 250 }
250 251
252 int set_session_config(DavSession *sn, CmdArgs *a) {
253 char *plain = cmd_getoption(a, "plain");
254 char *crypt = cmd_getoption(a, "crypt");
255
256 if(plain && crypt) {
257 fprintf(stderr, "Error: -p and -c option set\n");
258 return 1;
259 }
260
261 if(plain) {
262 int flags = sn->flags;
263 dav_session_set_flags(sn, 0);
264 } else if(crypt) {
265 dav_session_set_flags(sn, DAV_SESSION_FULL_ENCRYPTION);
266 }
267
268 return 0;
269 }
270
251 271
252 int cmd_list(CmdArgs *a) { 272 int cmd_list(CmdArgs *a) {
253 if(a->argc != 1) { 273 if(a->argc != 1) {
254 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many"); 274 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
255 return -1; 275 return -1;
262 //base = util_concat_path(repo->url, path); 282 //base = util_concat_path(repo->url, path);
263 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 283 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
264 dav_session_set_flags(sn, get_repository_flags(repo)); 284 dav_session_set_flags(sn, get_repository_flags(repo));
265 sn->key = dav_context_get_key(ctx, repo->default_key); 285 sn->key = dav_context_get_key(ctx, repo->default_key);
266 286
287 if(set_session_config(sn, a)) {
288 return -1;
289 }
290
267 char *update = cmd_getoption(a, "update"); 291 char *update = cmd_getoption(a, "update");
268 time_t t = 0; 292 time_t t = 0;
269 if(update) { 293 if(update) {
270 t = util_parse_lastmodified(update); 294 t = util_parse_lastmodified(update);
271 } 295 }
274 int ret = -1; 298 int ret = -1;
275 DavResource *ls; 299 DavResource *ls;
276 while(ret != 0) { 300 while(ret != 0) {
277 ls = dav_query( 301 ls = dav_query(
278 sn, 302 sn,
279 "get - from %s where lastmodified > %t with depth %d", 303 "get idav:crypto-name,idav:crypto-key from %s where lastmodified > %t with depth %d",
280 path, 304 path,
281 t, 305 t,
282 depth); 306 depth);
283 307
284 if(!ls) { 308 if(!ls) {
481 Repository *repo = url2repo(url, &path); 505 Repository *repo = url2repo(url, &path);
482 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 506 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
483 dav_session_set_flags(sn, get_repository_flags(repo)); 507 dav_session_set_flags(sn, get_repository_flags(repo));
484 sn->key = dav_context_get_key(ctx, repo->default_key); 508 sn->key = dav_context_get_key(ctx, repo->default_key);
485 509
510 if(set_session_config(sn, a)) {
511 return -1;
512 }
513
486 char *update = cmd_getoption(a, "update"); 514 char *update = cmd_getoption(a, "update");
487 time_t t = 0; 515 time_t t = 0;
488 if(update) { 516 if(update) {
489 t = util_parse_lastmodified(update); 517 t = util_parse_lastmodified(update);
490 } 518 }
508 char *res_url = util_concat_path(sn->base_url, path); 536 char *res_url = util_concat_path(sn->base_url, path);
509 fprintf(stderr, "Resource %s is a collection.\n", res_url); 537 fprintf(stderr, "Resource %s is a collection.\n", res_url);
510 fprintf(stderr, "Use the -R option to download collections.\n"); 538 fprintf(stderr, "Use the -R option to download collections.\n");
511 free(res_url); 539 free(res_url);
512 return -1; 540 return -1;
513 }
514
515 // disable file decryption, if the -p option is specified
516 char *plain = cmd_getoption(a, "plain");
517 if(plain) {
518 int flags = sn->flags;
519 dav_session_set_flags(sn, flags ^ DAV_SESSION_DECRYPT_CONTENT);
520 } 541 }
521 542
522 /* 543 /*
523 * determine the output file 544 * determine the output file
524 * use stdout if the output file is - 545 * use stdout if the output file is -
607 Repository *repo = url2repo(url, &path); 628 Repository *repo = url2repo(url, &path);
608 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 629 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
609 dav_session_set_flags(sn, get_repository_flags(repo)); 630 dav_session_set_flags(sn, get_repository_flags(repo));
610 sn->key = dav_context_get_key(ctx, repo->default_key); 631 sn->key = dav_context_get_key(ctx, repo->default_key);
611 632
612 // disable file encryption if the -p option is specified 633 if(set_session_config(sn, a)) {
613 char *plain = cmd_getoption(a, "plain"); 634 return -1;
614 if(plain) {
615 int flags = sn->flags;
616 dav_session_set_flags(sn, flags ^ DAV_SESSION_ENCRYPT_CONTENT);
617 dav_session_set_flags(sn, flags ^ DAV_SESSION_ENCRYPT_NAME);
618 } 635 }
619 636
620 // override the session key if the -k option is specified 637 // override the session key if the -k option is specified
621 char *keyname = cmd_getoption(a, "key"); 638 char *keyname = cmd_getoption(a, "key");
622 if(keyname) { 639 if(keyname) {
752 Repository *repo = url2repo(url, &path); 769 Repository *repo = url2repo(url, &path);
753 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 770 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
754 dav_session_set_flags(sn, get_repository_flags(repo)); 771 dav_session_set_flags(sn, get_repository_flags(repo));
755 sn->key = dav_context_get_key(ctx, repo->default_key); 772 sn->key = dav_context_get_key(ctx, repo->default_key);
756 773
774 if(set_session_config(sn, a)) {
775 return -1;
776 }
777
757 DavResource *res = dav_resource_new(sn, path); 778 DavResource *res = dav_resource_new(sn, path);
758 if(!res) { 779 if(!res) {
759 fprintf(stderr, "error\n"); 780 fprintf(stderr, "error\n");
760 return -1; 781 return -1;
761 } 782 }
781 Repository *repo = url2repo(url, &path); 802 Repository *repo = url2repo(url, &path);
782 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 803 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
783 dav_session_set_flags(sn, get_repository_flags(repo)); 804 dav_session_set_flags(sn, get_repository_flags(repo));
784 sn->key = dav_context_get_key(ctx, repo->default_key); 805 sn->key = dav_context_get_key(ctx, repo->default_key);
785 806
786 // disable file encryption if the -p option is specified 807 if(set_session_config(sn, a)) {
787 char *plain = cmd_getoption(a, "plain"); 808 return -1;
788 if(plain) {
789 int flags = sn->flags;
790 dav_session_set_flags(sn, flags ^ DAV_SESSION_ENCRYPT_CONTENT);
791 dav_session_set_flags(sn, flags ^ DAV_SESSION_ENCRYPT_NAME);
792 } 809 }
793 810
794 // override the session key if the -k option is specified 811 // override the session key if the -k option is specified
795 char *keyname = cmd_getoption(a, "key"); 812 char *keyname = cmd_getoption(a, "key");
796 if(keyname) { 813 if(keyname) {

mercurial