dav/main.c

changeset 12
493128ef1b12
parent 11
5db6178d8b58
child 13
8a0cc4d90de7
equal deleted inserted replaced
11:5db6178d8b58 12:493128ef1b12
40 #include "utils.h" 40 #include "utils.h"
41 #include "config.h" 41 #include "config.h"
42 #include "crypto.h" 42 #include "crypto.h"
43 #include "main.h" 43 #include "main.h"
44 44
45 void xmlerrorfnc(void * ctx, const char * msg, ... ) { 45 static DavContext *ctx;
46
47 void xmlerrorfnc(void * c, const char * msg, ... ) {
46 // nothing 48 // nothing
47 } 49 }
48 50
49 int main(int argc, char **argv) { 51 int main(int argc, char **argv) {
50 xmlGenericErrorFunc fnc = xmlerrorfnc; 52 xmlGenericErrorFunc fnc = xmlerrorfnc;
51 initGenericErrorDefaultFunc(&fnc); 53 initGenericErrorDefaultFunc(&fnc);
52 load_config(); 54 load_config();
55 ctx = dav_context_new();
56 dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
53 57
54 if(argc < 2) { 58 if(argc < 2) {
55 fprintf(stderr, "Missing command\n"); 59 fprintf(stderr, "Missing command\n");
56 print_usage(argv[0]); 60 print_usage(argv[0]);
57 return -1; 61 return -1;
132 int cmd_list(CmdArgs *a) { 136 int cmd_list(CmdArgs *a) {
133 if(a->argc == 0) { 137 if(a->argc == 0) {
134 return -1; 138 return -1;
135 } 139 }
136 140
137
138 DavContext *ctx = dav_context_new();
139 DavSession *sn = NULL; 141 DavSession *sn = NULL;
140 char *url = a->argv[0]; 142 char *url = a->argv[0];
141 char *root = NULL; 143 char *root = NULL;
142 char *path = NULL; 144 char *path = NULL;
143 url_get_parts(url, &root, &path); 145 url_get_parts(url, &root, &path);
168 int cmd_get(CmdArgs *a) { 170 int cmd_get(CmdArgs *a) {
169 if(a->argc == 0) { 171 if(a->argc == 0) {
170 return -1; 172 return -1;
171 } 173 }
172 174
173 DavContext *ctx = dav_context_new();
174 dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
175 DavSession *sn = NULL; 175 DavSession *sn = NULL;
176 char *url = a->argv[0]; 176 char *url = a->argv[0];
177 char *root = NULL; 177 char *root = NULL;
178 char *path = NULL; 178 char *path = NULL;
179 url_get_parts(url, &root, &path); 179 url_get_parts(url, &root, &path);
188 DavResource *res = dav_get(sn, path, "U:crypto-key"); 188 DavResource *res = dav_get(sn, path, "U:crypto-key");
189 if(!res) { 189 if(!res) {
190 fprintf(stderr, "error\n"); 190 fprintf(stderr, "error\n");
191 return -1; 191 return -1;
192 } 192 }
193 FILE *out = fopen(res->name, "w"); 193
194 /*
195 * determine the output file
196 * use stdout if the output file is -
197 */
198 char *outfile = cmd_getoption(a, "output");
199 if(!outfile) {
200 outfile = res->name;
201 }
202 FILE *out = !strcmp(outfile, "-") ? stdout : fopen(outfile, "w");
194 if(!out) { 203 if(!out) {
195 fprintf(stderr, "cannot open output file\n"); 204 fprintf(stderr, "cannot open output file\n");
196 return -1; 205 return -1;
197 } 206 }
198 207
208
209 /*
210 * if the -p (plain) option is specified we don't decrypt files
211 * use a key specified with the -k (key) option, a key from the
212 * key property or the repository default key
213 */
214 void *out_stream = out;
215 dav_write_func write_func = (dav_write_func)fwrite;
199 AESDecrypter *dec = NULL; 216 AESDecrypter *dec = NULL;
200 char *keyprop = dav_get_property_ns(res, "http://www.uap-core.de/", "crypto-key"); 217 char *plain = cmd_getoption(a, "plain");
201 if(repo) { 218 char *keyname = cmd_getoption(a, "key");
202 Key *key = get_key(keyprop); 219 if(!plain) {
203 if(repo->encrypt && key) { 220 char *keyprop = dav_get_property_ns(
221 res,
222 "http://www.uap-core.de/",
223 "crypto-key");
224 Key *key = NULL;
225 char *kn = NULL;
226 if(keyname) {
227 kn = keyname;
228 } else if(keyprop) {
229 kn = keyprop;
230 } else if(repo && repo->decrypt) {
231 kn = repo->default_key;
232 }
233 if(kn) {
234 key = get_key(kn);
235 if(!key) {
236 fprintf(stderr, "Key %s not found!\nAbort.\n", kn);
237 // TODO: free
238 return -1;
239 }
240 }
241
242 if(key) {
204 dec = aes_decrypter_new(key, out, (dav_write_func)fwrite); 243 dec = aes_decrypter_new(key, out, (dav_write_func)fwrite);
205 } 244 out_stream = dec;
206 } 245 write_func = (dav_write_func)aes_write;
207 246 }
208 int ret; 247 }
209 if(dec && keyprop) { 248
210 ret = dav_get_content(res, dec, (dav_write_func)aes_write); 249 int ret = dav_get_content(res, out_stream, write_func);
211 } else {
212 ret = dav_get_content(res, out, (dav_write_func)fwrite);
213 }
214 if(dec) { 250 if(dec) {
215 aes_decrypter_close(dec); 251 aes_decrypter_close(dec);
216 } 252 }
217 fclose(out); 253 fclose(out);
218 if(ret) { 254 if(ret && strcmp(outfile, "-")) {
219 unlink(res->name); 255 unlink(outfile);
220 } 256 }
221 257
222 return 0; 258 return 0;
223 } 259 }
224 260
225 int cmd_put(CmdArgs *a) { 261 int cmd_put(CmdArgs *a) {
226 if(a->argc < 2) { 262 if(a->argc < 2) {
227 return -1; 263 return -1;
228 } 264 }
229 265
230 DavContext *ctx = dav_context_new();
231 DavSession *sn = NULL; 266 DavSession *sn = NULL;
232 char *url = a->argv[0]; 267 char *url = a->argv[0];
233 char *file = a->argv[1]; 268 char *file = a->argv[1];
234 char *root = NULL; 269 char *root = NULL;
235 char *path = NULL; 270 char *path = NULL;
240 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 275 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
241 } else { 276 } else {
242 sn = dav_session_new(ctx, root); 277 sn = dav_session_new(ctx, root);
243 } 278 }
244 279
280 /*
281 * use stdin if the input file is -
282 */
283 FILE *in = !strcmp(file, "-") ? in : fopen(file, "r");
284 if(!in) {
285 fprintf(stderr, "cannot open input file\n");
286 return -1;
287 }
288
245 DavResource *res = dav_resource_new(sn, path); 289 DavResource *res = dav_resource_new(sn, path);
246 if(!res) { 290 if(!res) {
247 fprintf(stderr, "error\n"); 291 fprintf(stderr, "error\n");
248 return -1; 292 return -1;
249 } 293 }
250 FILE *in = fopen(file, "r"); 294
251 if(!in) {
252 fprintf(stderr, "cannot open input file\n");
253 return -1;
254 }
255 AESEncrypter *enc = NULL; 295 AESEncrypter *enc = NULL;
256 if(repo) { 296 char *keyname = cmd_getoption(a, "key");
257 Key *key = get_key(repo->default_key); 297 char *kn = NULL;
258 if(repo->encrypt && key) { 298 char *plain = cmd_getoption(a, "plain");
299 if(!plain && (keyname || repo)) {
300 kn = keyname ? keyname : repo->default_key;
301 Key *key = get_key(kn);
302 if(!key) {
303 fprintf(stderr, "Key %s not found!\nAbort.\n", kn);
304 // TODO: free
305 return -1;
306 }
307 if(keyname || repo->encrypt) {
259 enc = aes_encrypter_new(key, in, (dav_read_func)fread); 308 enc = aes_encrypter_new(key, in, (dav_read_func)fread);
260 } 309 }
261 } 310 }
262 if(enc) { 311 if(enc) {
263 dav_set_content(res, enc, (dav_read_func)aes_read); 312 dav_set_content(res, enc, (dav_read_func)aes_read);
264 dav_set_property_ns(res, "http://www.uap-core.de/", "crypto-key", repo->default_key); 313 dav_set_property_ns(res, "http://www.uap-core.de/", "crypto-key", kn);
265 } else { 314 } else {
266 dav_set_content(res, in, (dav_read_func)fread); 315 dav_set_content(res, in, (dav_read_func)fread);
267 } 316 }
268 317
269 if(dav_store(res)) { 318 if(dav_store(res)) {
270 fprintf(stderr, "cannot upload file\n"); 319 fprintf(stderr, "cannot upload file\n");
271 fclose(in); 320 fclose(in);
272 return -1; 321 return -1;
273 } 322 }
323 if(enc) {
324 aes_encrypter_close(enc);
325 }
274 fclose(in); 326 fclose(in);
275 return 0; 327 return 0;
276 } 328 }

mercurial