Tue, 13 Aug 2013 12:44:23 +0200
added some options to put and get
dav/main.c | file | annotate | diff | comparison | revisions |
--- a/dav/main.c Tue Aug 13 11:19:22 2013 +0200 +++ b/dav/main.c Tue Aug 13 12:44:23 2013 +0200 @@ -42,7 +42,9 @@ #include "crypto.h" #include "main.h" -void xmlerrorfnc(void * ctx, const char * msg, ... ) { +static DavContext *ctx; + +void xmlerrorfnc(void * c, const char * msg, ... ) { // nothing } @@ -50,6 +52,8 @@ xmlGenericErrorFunc fnc = xmlerrorfnc; initGenericErrorDefaultFunc(&fnc); load_config(); + ctx = dav_context_new(); + dav_add_namespace(ctx, "U", "http://www.uap-core.de/"); if(argc < 2) { fprintf(stderr, "Missing command\n"); @@ -134,8 +138,6 @@ return -1; } - - DavContext *ctx = dav_context_new(); DavSession *sn = NULL; char *url = a->argv[0]; char *root = NULL; @@ -170,8 +172,6 @@ return -1; } - DavContext *ctx = dav_context_new(); - dav_add_namespace(ctx, "U", "http://www.uap-core.de/"); DavSession *sn = NULL; char *url = a->argv[0]; char *root = NULL; @@ -190,33 +190,69 @@ fprintf(stderr, "error\n"); return -1; } - FILE *out = fopen(res->name, "w"); + + /* + * determine the output file + * use stdout if the output file is - + */ + char *outfile = cmd_getoption(a, "output"); + if(!outfile) { + outfile = res->name; + } + FILE *out = !strcmp(outfile, "-") ? stdout : fopen(outfile, "w"); if(!out) { fprintf(stderr, "cannot open output file\n"); return -1; } + + /* + * if the -p (plain) option is specified we don't decrypt files + * use a key specified with the -k (key) option, a key from the + * key property or the repository default key + */ + void *out_stream = out; + dav_write_func write_func = (dav_write_func)fwrite; AESDecrypter *dec = NULL; - char *keyprop = dav_get_property_ns(res, "http://www.uap-core.de/", "crypto-key"); - if(repo) { - Key *key = get_key(keyprop); - if(repo->encrypt && key) { + char *plain = cmd_getoption(a, "plain"); + char *keyname = cmd_getoption(a, "key"); + if(!plain) { + char *keyprop = dav_get_property_ns( + res, + "http://www.uap-core.de/", + "crypto-key"); + Key *key = NULL; + char *kn = NULL; + if(keyname) { + kn = keyname; + } else if(keyprop) { + kn = keyprop; + } else if(repo && repo->decrypt) { + kn = repo->default_key; + } + if(kn) { + key = get_key(kn); + if(!key) { + fprintf(stderr, "Key %s not found!\nAbort.\n", kn); + // TODO: free + return -1; + } + } + + if(key) { dec = aes_decrypter_new(key, out, (dav_write_func)fwrite); + out_stream = dec; + write_func = (dav_write_func)aes_write; } } - int ret; - if(dec && keyprop) { - ret = dav_get_content(res, dec, (dav_write_func)aes_write); - } else { - ret = dav_get_content(res, out, (dav_write_func)fwrite); - } + int ret = dav_get_content(res, out_stream, write_func); if(dec) { aes_decrypter_close(dec); } fclose(out); - if(ret) { - unlink(res->name); + if(ret && strcmp(outfile, "-")) { + unlink(outfile); } return 0; @@ -227,7 +263,6 @@ return -1; } - DavContext *ctx = dav_context_new(); DavSession *sn = NULL; char *url = a->argv[0]; char *file = a->argv[1]; @@ -242,26 +277,40 @@ sn = dav_session_new(ctx, root); } + /* + * use stdin if the input file is - + */ + FILE *in = !strcmp(file, "-") ? in : fopen(file, "r"); + if(!in) { + fprintf(stderr, "cannot open input file\n"); + return -1; + } + DavResource *res = dav_resource_new(sn, path); if(!res) { fprintf(stderr, "error\n"); return -1; } - FILE *in = fopen(file, "r"); - if(!in) { - fprintf(stderr, "cannot open input file\n"); - return -1; - } + AESEncrypter *enc = NULL; - if(repo) { - Key *key = get_key(repo->default_key); - if(repo->encrypt && key) { + char *keyname = cmd_getoption(a, "key"); + char *kn = NULL; + char *plain = cmd_getoption(a, "plain"); + if(!plain && (keyname || repo)) { + kn = keyname ? keyname : repo->default_key; + Key *key = get_key(kn); + if(!key) { + fprintf(stderr, "Key %s not found!\nAbort.\n", kn); + // TODO: free + return -1; + } + if(keyname || repo->encrypt) { enc = aes_encrypter_new(key, in, (dav_read_func)fread); } } if(enc) { dav_set_content(res, enc, (dav_read_func)aes_read); - dav_set_property_ns(res, "http://www.uap-core.de/", "crypto-key", repo->default_key); + dav_set_property_ns(res, "http://www.uap-core.de/", "crypto-key", kn); } else { dav_set_content(res, in, (dav_read_func)fread); } @@ -271,6 +320,9 @@ fclose(in); return -1; } + if(enc) { + aes_encrypter_close(enc); + } fclose(in); return 0; }