94 } |
94 } |
95 |
95 |
96 void print_usage(char *cmd) { |
96 void print_usage(char *cmd) { |
97 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd); |
97 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd); |
98 fprintf(stderr, "Commands:\n"); |
98 fprintf(stderr, "Commands:\n"); |
99 fprintf(stderr, " list [-alt] <url>\n"); |
99 fprintf(stderr, " list [-altR] <url>\n"); |
100 fprintf(stderr, " get [-p] [-k <key>] [-o <file>] <url>\n"); |
100 fprintf(stderr, " get [-pR] [-k <key>] [-o <file>] <url>\n"); |
101 fprintf(stderr, " put [-p] [-k <key>] <url> <file>\n"); |
101 fprintf(stderr, " put [-p] [-k <key>] <url> <file>\n"); |
102 fprintf(stderr, " mkdir <url>\n"); |
102 fprintf(stderr, " mkdir <url>\n"); |
103 fprintf(stderr, " remove <url>\n"); |
103 fprintf(stderr, " remove <url>\n"); |
104 fprintf(stderr, "\n"); |
104 fprintf(stderr, "\n"); |
105 fprintf(stderr, "Options:\n"); |
105 fprintf(stderr, "Options:\n"); |
106 fprintf(stderr, |
106 fprintf(stderr, |
107 " -k <key> Key to use for encryption or decryption\n"); |
107 " -k <key> Key to use for encryption or decryption\n"); |
108 fprintf(stderr, " -p Don't encrypt or decrypt files\n"); |
108 fprintf(stderr, " -p Don't encrypt or decrypt files\n"); |
|
109 fprintf(stderr, |
|
110 " -R " |
|
111 "Recursively do the operation for all children\n"); |
109 fprintf(stderr, " -o <file> Write output to file\n"); |
112 fprintf(stderr, " -o <file> Write output to file\n"); |
110 fprintf(stderr, " -a show all files\n"); |
113 fprintf(stderr, " -a show all files\n"); |
111 fprintf(stderr, " -l print resources in long list format\n"); |
114 fprintf(stderr, " -l print resources in long list format\n"); |
112 fprintf(stderr, " -t print content type\n"); |
115 fprintf(stderr, " -t print content type\n"); |
113 fprintf(stderr, "\n"); |
116 fprintf(stderr, "\n"); |
191 |
194 |
192 DavSession *sn = NULL; |
195 DavSession *sn = NULL; |
193 char *url = a->argv[0]; |
196 char *url = a->argv[0]; |
194 char *root = NULL; |
197 char *root = NULL; |
195 char *path = NULL; |
198 char *path = NULL; |
|
199 char *base = NULL; |
196 url_get_parts(url, &root, &path); |
200 url_get_parts(url, &root, &path); |
197 |
201 |
198 Repository *repo = get_repository(root); |
202 Repository *repo = get_repository(root); |
199 if(repo) { |
203 if(repo) { |
200 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); |
204 base = util_concat_path(repo->url, path); |
201 } else { |
205 sn = dav_session_new_auth(ctx, base, repo->user, repo->password); |
202 sn = dav_session_new(ctx, root); |
206 } else { |
203 } |
207 base = util_concat_path(root, path); |
204 |
208 sn = dav_session_new(ctx, base); |
205 //printf("baseurl: %s\n", sn->base_url); |
209 } |
206 |
210 |
207 //DavResource *ls = dav_get(sn, path, "U:crypto-key"); |
211 DavResource *ls; |
208 DavResource *ls = dav_query(sn, "get U:crypto-key from %s", path); |
212 if(cmd_getoption(a, "recursive")) { |
|
213 printf("base: %s\n", base); |
|
214 ls = dav_query(sn, "get U:crypto-key from /*"); |
|
215 } else { |
|
216 ls = dav_query(sn, "get U:crypto-key from /"); |
|
217 } |
209 if(!ls) { |
218 if(!ls) { |
210 print_resource_error(sn, path); |
219 print_resource_error(sn, path); |
|
220 free(root); |
|
221 free(path); |
|
222 free(base); |
211 return -1; |
223 return -1; |
212 } |
224 } |
213 |
225 |
214 // parameters |
226 // parameters |
215 int show_all = cmd_getoption(a, "all") ? 1 : 0; |
227 int show_all = cmd_getoption(a, "all") ? 1 : 0; |
340 type = ""; |
358 type = ""; |
341 } |
359 } |
342 |
360 |
343 char *date = ls_date_str(res->lastmodified); |
361 char *date = ls_date_str(res->lastmodified); |
344 char *size = ls_size_str(res); |
362 char *size = ls_size_str(res); |
|
363 char *name = recursive ? res->path+1 : res->name; |
345 printf( |
364 printf( |
346 "%s %*s %10s %12s %s\n", |
365 "%s %*s %10s %12s %s\n", |
347 flags, |
366 flags, |
348 type_width, type, |
367 type_width, type, |
349 size, |
368 size, |
350 date, |
369 date, |
351 res->name); |
370 name); |
352 free(date); |
371 free(date); |
353 free(size); |
372 free(size); |
|
373 |
|
374 if(recursive) { |
|
375 DavResource *child = res->children; |
|
376 while(child) { |
|
377 ls_print_list_elm(child, a); |
|
378 child = child->next; |
|
379 } |
|
380 } |
354 } |
381 } |
355 |
382 |
356 void ls_print_elm(DavResource *res, CmdArgs *a) { |
383 void ls_print_elm(DavResource *res, CmdArgs *a) { |
357 printf("%s\n", res->name); |
384 int recursive = cmd_getoption(a, "recursive") ? 1 : 0; |
|
385 char *name = recursive ? res->path+1 : res->name; |
|
386 printf("%s\n", name); |
|
387 if(recursive) { |
|
388 DavResource *child = res->children; |
|
389 while(child) { |
|
390 ls_print_elm(child, a); |
|
391 child = child->next; |
|
392 } |
|
393 } |
358 } |
394 } |
359 |
395 |
360 int cmd_get(CmdArgs *a) { |
396 int cmd_get(CmdArgs *a) { |
361 if(a->argc == 0) { |
397 if(a->argc == 0) { |
362 fprintf(stderr, "Too few arguments\n"); |
398 fprintf(stderr, "Too few arguments\n"); |
374 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); |
410 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); |
375 } else { |
411 } else { |
376 sn = dav_session_new(ctx, root); |
412 sn = dav_session_new(ctx, root); |
377 } |
413 } |
378 |
414 |
379 DavResource *res = dav_get(sn, path, "U:crypto-key"); |
415 int recursive = cmd_getoption(a, "recursive") ? 1 : 0; |
|
416 DavResource *res; |
|
417 if(recursive) { |
|
418 res = dav_query(sn, "get U:crypto-key from %s*", path); |
|
419 } else { |
|
420 res = dav_query(sn, "get U:crypto-key from %s", path); |
|
421 } |
380 if(!res) { |
422 if(!res) { |
381 print_resource_error(sn, path); |
423 print_resource_error(sn, path); |
|
424 return -1; |
|
425 } |
|
426 if(!recursive && res->iscollection) { |
|
427 char *res_url = util_concat_path(sn->base_url, path); |
|
428 fprintf(stderr, "Resource %s is a collection.\n", res_url); |
|
429 fprintf(stderr, "Use the -R option to download collections.\n"); |
|
430 free(res_url); |
382 return -1; |
431 return -1; |
383 } |
432 } |
384 |
433 |
385 /* |
434 /* |
386 * determine the output file |
435 * determine the output file |
387 * use stdout if the output file is - |
436 * use stdout if the output file is - |
388 */ |
437 */ |
389 char *outfile = cmd_getoption(a, "output"); |
438 char *outfile = cmd_getoption(a, "output"); |
390 if(!outfile) { |
439 if(!outfile) { |
391 outfile = res->name; |
440 if(res->iscollection) { |
392 } |
441 outfile = ""; |
393 FILE *out = !strcmp(outfile, "-") ? stdout : fopen(outfile, "w"); |
442 } else { |
394 if(!out) { |
443 outfile = res->name; |
|
444 } |
|
445 } else if(res->iscollection && !strcmp(outfile, "-")) { |
|
446 fprintf( |
|
447 stderr, |
|
448 "Cannot write output to stdout " |
|
449 "if the requested resource is a collection.\n"); |
|
450 return -1; |
|
451 } |
|
452 |
|
453 int ret = get_resource(repo, res, a, outfile); |
|
454 |
|
455 return ret; |
|
456 } |
|
457 |
|
458 int get_resource(Repository *repo, DavResource *res, CmdArgs *a, char *out) { |
|
459 size_t outlen = strlen(out); |
|
460 // print some status message in recursive mode |
|
461 if(cmd_getoption(a, "recursive")) { |
|
462 char *res_url = util_concat_path(res->session->base_url, res->path); |
|
463 printf("get: %s\n", res_url); |
|
464 free(res_url); |
|
465 } |
|
466 |
|
467 if(res->iscollection) { |
|
468 // create directory |
|
469 if(outlen != 0) { |
|
470 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
|
471 int ret = mkdir(out, mode); |
|
472 if(ret != 0 && errno != EEXIST) { |
|
473 return 1; |
|
474 } |
|
475 } |
|
476 |
|
477 DavResource *child = res->children; |
|
478 while(child) { |
|
479 char *newout = outlen > 0 ? |
|
480 util_concat_path(out, child->name) : child->name; |
|
481 int ret = get_resource(repo, child, a, newout); |
|
482 if(outlen > 0) { |
|
483 free(newout); |
|
484 } |
|
485 if(ret) { |
|
486 return 1; |
|
487 } |
|
488 child = child->next; |
|
489 } |
|
490 |
|
491 return 0; |
|
492 } |
|
493 |
|
494 FILE *fout = !strcmp(out, "-") ? stdout : fopen(out, "w"); |
|
495 if(!fout) { |
395 fprintf(stderr, "cannot open output file\n"); |
496 fprintf(stderr, "cannot open output file\n"); |
396 return -1; |
497 return -1; |
397 } |
498 } |
398 |
|
399 |
499 |
400 /* |
500 /* |
401 * if the -p (plain) option is specified we don't decrypt files |
501 * if the -p (plain) option is specified we don't decrypt files |
402 * use a key specified with the -k (key) option, a key from the |
502 * use a key specified with the -k (key) option, a key from the |
403 * key property or the repository default key |
503 * key property or the repository default key |
404 */ |
504 */ |
405 void *out_stream = out; |
505 void *out_stream = fout; |
406 dav_write_func write_func = (dav_write_func)fwrite; |
506 dav_write_func write_func = (dav_write_func)fwrite; |
407 AESDecrypter *dec = NULL; |
507 AESDecrypter *dec = NULL; |
408 char *plain = cmd_getoption(a, "plain"); |
508 char *plain = cmd_getoption(a, "plain"); |
409 char *keyname = cmd_getoption(a, "key"); |
509 char *keyname = cmd_getoption(a, "key"); |
410 if(!plain) { |
510 if(!plain) { |
422 kn = repo->default_key; |
522 kn = repo->default_key; |
423 } |
523 } |
424 if(kn) { |
524 if(kn) { |
425 key = get_key(kn); |
525 key = get_key(kn); |
426 if(!key) { |
526 if(!key) { |
427 fprintf(stderr, "Key %s not found!\nAbort.\n", kn); |
527 fprintf(stderr, "Key %s not found!\n", kn); |
428 // TODO: free |
528 // TODO: free |
429 return -1; |
529 if(cmd_getoption(a, "recursive")) { |
|
530 // skip the file in recursive mode |
|
531 char *res_url = util_concat_path( |
|
532 res->session->base_url, |
|
533 res->path); |
|
534 printf("Skip resource: %s\n", res_url); |
|
535 free(res_url); |
|
536 return 0; |
|
537 } else { |
|
538 printf("Abort.\n"); |
|
539 // abort |
|
540 return 1; |
|
541 } |
430 } |
542 } |
431 } |
543 } |
432 |
544 |
433 if(key) { |
545 if(key) { |
434 dec = aes_decrypter_new(key, out, (dav_write_func)fwrite); |
546 dec = aes_decrypter_new(key, fout, (dav_write_func)fwrite); |
435 out_stream = dec; |
547 out_stream = dec; |
436 write_func = (dav_write_func)aes_write; |
548 write_func = (dav_write_func)aes_write; |
437 } |
549 } |
438 } |
550 } |
439 |
551 |
440 int ret = dav_get_content(res, out_stream, write_func); |
552 int ret = dav_get_content(res, out_stream, write_func); |
441 if(dec) { |
553 if(dec) { |
442 aes_decrypter_close(dec); |
554 aes_decrypter_close(dec); |
443 } |
555 } |
444 fclose(out); |
556 fclose(fout); |
445 if(ret && strcmp(outfile, "-")) { |
557 if(ret && strcmp(out, "-")) { |
446 unlink(outfile); |
558 unlink(out); |
447 } |
559 } |
448 |
560 |
449 return 0; |
561 return 0; |
450 } |
562 } |
451 |
563 |