dav/main.c

changeset 43
03076907b58a
parent 40
a95ee94b9204
child 45
e3839719b079
equal deleted inserted replaced
42:6518b035a9df 43:03076907b58a
47 47
48 void xmlerrorfnc(void * c, const char * msg, ... ) { 48 void xmlerrorfnc(void * c, const char * msg, ... ) {
49 // nothing 49 // nothing
50 } 50 }
51 51
52 #include <libidav/session.h>
53 void test() {
54
55 }
56
52 int main(int argc, char **argv) { 57 int main(int argc, char **argv) {
53 xmlGenericErrorFunc fnc = xmlerrorfnc; 58 xmlGenericErrorFunc fnc = xmlerrorfnc;
54 initGenericErrorDefaultFunc(&fnc); 59 initGenericErrorDefaultFunc(&fnc);
55 ctx = dav_context_new(); 60 ctx = dav_context_new();
56 load_config(ctx); 61 load_config(ctx);
57 dav_add_namespace(ctx, "U", "http://www.uap-core.de/"); 62 dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
63
64 //test();
58 65
59 memcpy(ctx->http_proxy, get_http_proxy(), sizeof(Proxy)); 66 memcpy(ctx->http_proxy, get_http_proxy(), sizeof(Proxy));
60 memcpy(ctx->https_proxy, get_https_proxy(), sizeof(Proxy)); 67 memcpy(ctx->https_proxy, get_https_proxy(), sizeof(Proxy));
61 68
62 if(argc < 2) { 69 if(argc < 2) {
101 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd); 108 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd);
102 fprintf(stderr, "Commands:\n"); 109 fprintf(stderr, "Commands:\n");
103 fprintf(stderr, " list [-altR] [-u <date>] <url>\n"); 110 fprintf(stderr, " list [-altR] [-u <date>] <url>\n");
104 fprintf( 111 fprintf(
105 stderr, 112 stderr,
106 " get [-pR] [-k <key>] [-o <file>] [-u <date>] <url>\n"); 113 " get [-pR] [-o <file>] [-u <date>] <url>\n");
107 fprintf(stderr, " put [-pR] [-k <key>] <url> <file>\n"); 114 fprintf(stderr, " put [-pR] [-k <key>] <url> <file>\n");
108 fprintf(stderr, " mkdir <url>\n"); 115 fprintf(stderr, " mkdir [-p] [-k <key>] <url>\n");
109 fprintf(stderr, " remove <url>\n"); 116 fprintf(stderr, " remove <url>\n");
110 fprintf(stderr, " date [url]\n"); 117 fprintf(stderr, " date [url]\n");
111 fprintf(stderr, "\n"); 118 fprintf(stderr, "\n");
112 fprintf(stderr, "Options:\n"); 119 fprintf(stderr, "Options:\n");
113 fprintf(stderr, 120 fprintf(stderr,
200 *path = sstrdup(p).ptr; 207 *path = sstrdup(p).ptr;
201 } else { 208 } else {
202 repo = calloc(1, sizeof(Repository)); 209 repo = calloc(1, sizeof(Repository));
203 repo->name = ""; 210 repo->name = "";
204 repo->url = strdup(url); 211 repo->url = strdup(url);
205 repo->store_key_property = true; 212 repo->decrypt_content = true;
206 repo->decrypt = true;
207 *path = strdup("/"); 213 *path = strdup("/");
208 } 214 }
209 215
210 return repo; 216 return repo;
211 } 217 }
250 } 256 }
251 257
252 char *url = a->argv[0]; 258 char *url = a->argv[0];
253 char *path = NULL; 259 char *path = NULL;
254 //char *base = NULL; 260 //char *base = NULL;
255 DavSession *sn = NULL;
256 Repository *repo = url2repo(url, &path); 261 Repository *repo = url2repo(url, &path);
257 //base = util_concat_path(repo->url, path); 262 //base = util_concat_path(repo->url, path);
258 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 263 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
264 dav_session_set_flags(sn, get_repository_flags(repo));
265 sn->key = dav_context_get_key(ctx, repo->default_key);
259 266
260 char *update = cmd_getoption(a, "update"); 267 char *update = cmd_getoption(a, "update");
261 time_t t = 0; 268 time_t t = 0;
262 if(update) { 269 if(update) {
263 t = util_parse_lastmodified(update); 270 t = util_parse_lastmodified(update);
264 } 271 }
265 272
273 int depth = cmd_getoption(a, "recursive") ? -1 : 1;
266 int ret = -1; 274 int ret = -1;
267 DavResource *ls; 275 DavResource *ls;
268 while(ret != 0) { 276 while(ret != 0) {
269 if(cmd_getoption(a, "recursive")) { 277 ls = dav_query(
270 //printf("base: %s\n", base); 278 sn,
271 if(update) { 279 "get - from %s where lastmodified > %t with depth %d",
272 ls = dav_query( 280 path,
273 sn, 281 t,
274 "get U:crypto-key from %s* where lastmodified > %t", 282 depth);
275 path,
276 t);
277 } else {
278 ls = dav_query(sn, "get U:crypto-key from %s*", path);
279 }
280 } else {
281 if(update) {
282 ls = dav_query(
283 sn,
284 "get U:crypto-key from %s where lastmodified > %t",
285 path,
286 t);
287 } else {
288 ls = dav_query(sn, "get U:crypto-key from %s", path);
289 }
290 }
291 283
292 if(!ls) { 284 if(!ls) {
293 if(sn->error == DAV_UNAUTHORIZED) { 285 if(sn->error == DAV_UNAUTHORIZED) {
294 if(request_auth(repo, sn)) { 286 if(request_auth(repo, sn)) {
295 continue; 287 continue;
427 flags[0] = 'd'; 419 flags[0] = 'd';
428 type = ""; 420 type = "";
429 } 421 }
430 char *keyprop = dav_get_property_ns( 422 char *keyprop = dav_get_property_ns(
431 res, 423 res,
432 "http://www.uap-core.de/", 424 DAV_NS,
433 "crypto-key"); 425 "crypto-key");
434 if(keyprop) { 426 if(keyprop) {
435 flags[1] = 'c'; 427 flags[1] = 'c';
436 } 428 }
437 429
484 return -1; 476 return -1;
485 } 477 }
486 478
487 char *url = a->argv[0]; 479 char *url = a->argv[0];
488 char *path = NULL; 480 char *path = NULL;
489 DavSession *sn = NULL;
490 Repository *repo = url2repo(url, &path); 481 Repository *repo = url2repo(url, &path);
491 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 482 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
483 dav_session_set_flags(sn, get_repository_flags(repo));
484 sn->key = dav_context_get_key(ctx, repo->default_key);
492 485
493 char *update = cmd_getoption(a, "update"); 486 char *update = cmd_getoption(a, "update");
494 time_t t = 0; 487 time_t t = 0;
495 if(update) { 488 if(update) {
496 t = util_parse_lastmodified(update); 489 t = util_parse_lastmodified(update);
497 } 490 }
498 491
499 int recursive = cmd_getoption(a, "recursive") ? 1 : 0; 492 int recursive = cmd_getoption(a, "recursive") ? 1 : 0;
500 DavResource *res; 493 DavResource *res;
501 494
502 if(recursive) { 495 int depth = recursive ? -1 : 1;
503 if(update) { 496 res = dav_query(
504 res = dav_query( 497 sn,
505 sn, 498 "get - from %s where lastmodified > %t with depth %d",
506 "get U:crypto-key from %s* where lastmodified > %t", 499 path,
507 path, 500 t,
508 t); 501 depth);
509 } else { 502
510 res = dav_query(sn, "get U:crypto-key from %s*", path);
511 }
512 } else {
513 if(update) {
514 res = dav_query(
515 sn,
516 "get U:crypto-key from %s where lastmodified > %t",
517 path,
518 t);
519 } else {
520 res = dav_query(sn, "get U:crypto-key from %s", path);
521 }
522 }
523 if(!res) { 503 if(!res) {
524 print_resource_error(sn, path); 504 print_resource_error(sn, path);
525 return -1; 505 return -1;
526 } 506 }
527 if(!recursive && res->iscollection) { 507 if(!recursive && res->iscollection) {
528 char *res_url = util_concat_path(sn->base_url, path); 508 char *res_url = util_concat_path(sn->base_url, path);
529 fprintf(stderr, "Resource %s is a collection.\n", res_url); 509 fprintf(stderr, "Resource %s is a collection.\n", res_url);
530 fprintf(stderr, "Use the -R option to download collections.\n"); 510 fprintf(stderr, "Use the -R option to download collections.\n");
531 free(res_url); 511 free(res_url);
532 return -1; 512 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);
533 } 520 }
534 521
535 /* 522 /*
536 * determine the output file 523 * determine the output file
537 * use stdout if the output file is - 524 * use stdout if the output file is -
596 if(!fout) { 583 if(!fout) {
597 fprintf(stderr, "cannot open output file\n"); 584 fprintf(stderr, "cannot open output file\n");
598 return -1; 585 return -1;
599 } 586 }
600 587
601 /* 588 int ret = dav_get_content(res, fout, (dav_write_func)fwrite);
602 * if the -p (plain) option is specified we don't decrypt files
603 * use a key specified with the -k (key) option, a key from the
604 * key property or the repository default key
605 */
606 void *out_stream = fout;
607 dav_write_func write_func = (dav_write_func)fwrite;
608 AESDecrypter *dec = NULL;
609 char *plain = cmd_getoption(a, "plain");
610 char *keyname = cmd_getoption(a, "key");
611 if(!plain) {
612 char *keyprop = dav_get_property_ns(
613 res,
614 "http://www.uap-core.de/",
615 "crypto-key");
616 Key *key = NULL;
617 char *kn = NULL;
618 if(keyname) {
619 kn = keyname;
620 } else if(keyprop) {
621 kn = keyprop;
622 } else if(repo && repo->decrypt) {
623 kn = repo->default_key;
624 }
625 if(kn) {
626 key = get_key(kn);
627 if(!key) {
628 fprintf(stderr, "Key %s not found!\n", kn);
629 // TODO: free
630 if(cmd_getoption(a, "recursive")) {
631 // skip the file in recursive mode
632 char *res_url = util_concat_path(
633 res->session->base_url,
634 res->path);
635 printf("Skipping resource: %s\n", res_url);
636 free(res_url);
637 return 0;
638 } else {
639 printf("Abort.\n");
640 // abort
641 return 1;
642 }
643 }
644 }
645
646 if(key) {
647 dec = aes_decrypter_new(key, fout, (dav_write_func)fwrite);
648 out_stream = dec;
649 write_func = (dav_write_func)aes_write;
650 }
651 }
652
653 int ret = dav_get_content(res, out_stream, write_func);
654 if(dec) {
655 aes_decrypter_close(dec);
656 }
657 fclose(fout); 589 fclose(fout);
658 if(ret && strcmp(out, "-")) { 590 if(ret && strcmp(out, "-")) {
659 unlink(out); 591 unlink(out);
660 } 592 }
661 593
670 } 602 }
671 603
672 char *url = a->argv[0]; 604 char *url = a->argv[0];
673 char *file = a->argv[1]; 605 char *file = a->argv[1];
674 char *path = NULL; 606 char *path = NULL;
675 DavSession *sn = NULL;
676 Repository *repo = url2repo(url, &path); 607 Repository *repo = url2repo(url, &path);
677 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 608 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
609 dav_session_set_flags(sn, get_repository_flags(repo));
610 sn->key = dav_context_get_key(ctx, repo->default_key);
611
612 // disable file encryption if the -p option is specified
613 char *plain = cmd_getoption(a, "plain");
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 }
619
620 // override the session key if the -k option is specified
621 char *keyname = cmd_getoption(a, "key");
622 if(keyname) {
623 DavKey *key = dav_context_get_key(ctx, keyname);
624 if(key) {
625 sn->key = key;
626 } else {
627 fprintf(stderr, "Key %s not found!\nAbort.\n", keyname);
628 // TODO: free
629 return -1;
630 }
631 }
678 632
679 int ret; 633 int ret;
680 if(!strcmp(file, "-")) { 634 if(!strcmp(file, "-")) {
681 FILE *in = stdin; 635 FILE *in = stdin;
682 ret = put_file(repo, a, sn, path, "stdin", in); 636 ret = put_file(repo, a, sn, path, "stdin", in);
764 // TODO: free res 718 // TODO: free res
765 char *newpath = util_concat_path(path, name); 719 char *newpath = util_concat_path(path, name);
766 free(path); 720 free(path);
767 path = newpath; 721 path = newpath;
768 res = dav_resource_new(sn, path); 722 res = dav_resource_new(sn, path);
769 } 723 int ret = put_file(repo, a, sn, res->path, NULL, in);
770 724 // TODO: free res
771 AESEncrypter *enc = NULL; 725 return ret;
772 char *keyname = cmd_getoption(a, "key"); 726 }
773 char *kn = NULL; 727
774 char *plain = cmd_getoption(a, "plain"); 728 dav_set_content(res, in, (dav_read_func)fread);
775 if(!plain && (keyname || repo)) {
776 kn = keyname ? keyname : repo->default_key;
777 if(kn) {
778 Key *key = get_key(kn);
779 if(!key) {
780 fprintf(stderr, "Key %s not found!\nAbort.\n", kn);
781 // TODO: free
782 return -1;
783 }
784 if(keyname || repo->encrypt) {
785 enc = aes_encrypter_new(key, in, (dav_read_func)fread);
786 }
787 }
788 }
789 if(enc) {
790 dav_set_content(res, enc, (dav_read_func)aes_read);
791 dav_set_property_ns(res, "http://www.uap-core.de/", "crypto-key", kn);
792 } else {
793 dav_set_content(res, in, (dav_read_func)fread);
794 }
795 729
796 if(dav_store(res)) { 730 if(dav_store(res)) {
797 print_resource_error(sn, res->path); 731 print_resource_error(sn, res->path);
798 fprintf(stderr, "Cannot upload file.\n"); 732 fprintf(stderr, "Cannot upload file.\n");
799 if(sn->errorstr) { 733 if(sn->errorstr) {
800 fprintf(stderr, "%s\n", sn->errorstr); 734 fprintf(stderr, "%s\n", sn->errorstr);
801 } 735 }
802 fclose(in); 736 fclose(in);
803 return -1; 737 return -1;
804 } 738 }
805 if(enc) {
806 aes_encrypter_close(enc);
807 }
808 return 0; 739 return 0;
809 } 740 }
810 741
811 742
812 int cmd_remove(CmdArgs *a) { 743 int cmd_remove(CmdArgs *a) {
816 return -1; 747 return -1;
817 } 748 }
818 749
819 char *url = a->argv[0]; 750 char *url = a->argv[0];
820 char *path = NULL; 751 char *path = NULL;
821 DavSession *sn = NULL;
822 Repository *repo = url2repo(url, &path); 752 Repository *repo = url2repo(url, &path);
823 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 753 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
754 dav_session_set_flags(sn, get_repository_flags(repo));
755 sn->key = dav_context_get_key(ctx, repo->default_key);
824 756
825 DavResource *res = dav_resource_new(sn, path); 757 DavResource *res = dav_resource_new(sn, path);
826 if(!res) { 758 if(!res) {
827 fprintf(stderr, "error\n"); 759 fprintf(stderr, "error\n");
828 return -1; 760 return -1;
844 return -1; 776 return -1;
845 } 777 }
846 778
847 char *url = a->argv[0]; 779 char *url = a->argv[0];
848 char *path = NULL; 780 char *path = NULL;
849 DavSession *sn = NULL;
850 Repository *repo = url2repo(url, &path); 781 Repository *repo = url2repo(url, &path);
851 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 782 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
783 dav_session_set_flags(sn, get_repository_flags(repo));
784 sn->key = dav_context_get_key(ctx, repo->default_key);
785
786 // disable file encryption if the -p option is specified
787 char *plain = cmd_getoption(a, "plain");
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 }
793
794 // override the session key if the -k option is specified
795 char *keyname = cmd_getoption(a, "key");
796 if(keyname) {
797 DavKey *key = dav_context_get_key(ctx, keyname);
798 if(key) {
799 sn->key = key;
800 } else {
801 fprintf(stderr, "Key %s not found!\nAbort.\n", keyname);
802 // TODO: free
803 return -1;
804 }
805 }
852 806
853 DavResource *res = dav_resource_new(sn, path); 807 DavResource *res = dav_resource_new(sn, path);
854 if(!res) { 808 if(!res) {
855 fprintf(stderr, "error\n"); 809 fprintf(stderr, "error\n");
856 return -1; 810 return -1;
889 size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT\n", date); 843 size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT\n", date);
890 fwrite(str, 1, len, stdout); 844 fwrite(str, 1, len, stdout);
891 } else if (a->argc == 1) { 845 } else if (a->argc == 1) {
892 char *url = a->argv[0]; 846 char *url = a->argv[0];
893 char *path = NULL; 847 char *path = NULL;
894 DavSession *sn = NULL;
895 Repository *repo = url2repo(url, &path); 848 Repository *repo = url2repo(url, &path);
896 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 849 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
850 dav_session_set_flags(sn, get_repository_flags(repo));
851 sn->key = dav_context_get_key(ctx, repo->default_key);
897 852
898 DavResource *res = dav_resource_new(sn, path); 853 DavResource *res = dav_resource_new(sn, path);
899 char *date = NULL; 854 char *date = NULL;
900 curl_easy_setopt(sn->handle, CURLOPT_HEADERFUNCTION, get_date_header_cb); 855 curl_easy_setopt(sn->handle, CURLOPT_HEADERFUNCTION, get_date_header_cb);
901 curl_easy_setopt(sn->handle, CURLOPT_WRITEHEADER, &date); 856 curl_easy_setopt(sn->handle, CURLOPT_WRITEHEADER, &date);
907 return 0; 862 return 0;
908 } else { 863 } else {
909 fprintf(stderr, "Too many arguments\n"); 864 fprintf(stderr, "Too many arguments\n");
910 return -1; 865 return -1;
911 } 866 }
867 return 0;
912 } 868 }
913 869
914 int cmd_sync(CmdArgs *a) { 870 int cmd_sync(CmdArgs *a) {
915 if(a->argc < 2) { 871 if(a->argc < 2) {
916 fprintf(stderr, "Too few arguments\n"); 872 fprintf(stderr, "Too few arguments\n");

mercurial