38 #include "crypto.h" |
38 #include "crypto.h" |
39 #include "ucx/buffer.h" |
39 #include "ucx/buffer.h" |
40 #include "ucx/utils.h" |
40 #include "ucx/utils.h" |
41 |
41 |
42 #include "resource.h" |
42 #include "resource.h" |
|
43 #include "xml.h" |
43 #include "davqlexec.h" |
44 #include "davqlexec.h" |
44 |
45 |
45 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) |
46 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) |
46 |
47 |
47 DavResource* dav_resource_new(DavSession *sn, char *path) { |
48 DavResource* dav_resource_new(DavSession *sn, char *path) { |
234 resource->path); |
235 resource->path); |
235 } |
236 } |
236 return resource->href; |
237 return resource->href; |
237 } |
238 } |
238 |
239 |
239 void resource_add_property(DavResource *res, char *ns, char *name, char *val) { |
240 void resource_add_property(DavResource *res, char *ns, char *name, xmlNode *val) { |
240 if(!val) { |
241 if(!val) { |
241 return; |
242 return; |
242 } |
243 } |
243 |
244 |
244 UcxKey key = dav_property_key(ns, name); |
245 UcxKey key = dav_property_key(ns, name); |
245 sstr_t v = sstrdup_a(res->session->mp->allocator, sstr(val)); |
246 DavXmlNode *v = dav_convert_xml(res->session, val); |
246 ucx_map_put(((DavResourceData*)res->data)->properties, key, v.ptr); |
247 ucx_map_put(((DavResourceData*)res->data)->properties, key, v); |
247 free(key.data); |
248 free(key.data); |
248 } |
249 } |
249 |
250 |
250 char* resource_get_property(DavResource *res, char *ns, char *name) { |
251 void resource_add_string_property(DavResource *res, char *ns, char *name, char *val) { |
|
252 if(!val) { |
|
253 return; |
|
254 } |
|
255 |
251 UcxKey key = dav_property_key(ns, name); |
256 UcxKey key = dav_property_key(ns, name); |
252 char *property = resource_get_property_k(res, key); |
257 DavXmlNode *v = dav_text_node(res->session, val); |
|
258 ucx_map_put(((DavResourceData*)res->data)->properties, key, v); |
|
259 free(key.data); |
|
260 } |
|
261 |
|
262 DavXmlNode* resource_get_property(DavResource *res, char *ns, char *name) { |
|
263 UcxKey key = dav_property_key(ns, name); |
|
264 DavXmlNode *property = resource_get_property_k(res, key); |
253 free(key.data); |
265 free(key.data); |
254 return property; |
266 return property; |
255 } |
267 } |
256 |
268 |
257 char* resource_get_property_k(DavResource *res, UcxKey key) { |
269 DavXmlNode* resource_get_property_k(DavResource *res, UcxKey key) { |
258 DavResourceData *data = (DavResourceData*)res->data; |
270 DavResourceData *data = (DavResourceData*)res->data; |
259 return ucx_map_get(data->properties, key); |
271 return ucx_map_get(data->properties, key); |
260 } |
272 } |
261 |
273 |
262 UcxKey dav_property_key(char *ns, char *name) { |
274 UcxKey dav_property_key(char *ns, char *name) { |
337 break; |
349 break; |
338 } |
350 } |
339 default: ret = 0; |
351 default: ret = 0; |
340 } |
352 } |
341 } else if(cr->type == 1) { |
353 } else if(cr->type == 1) { |
342 char *value1 = resource_get_property_k(res1, cr->column.property); |
354 DavXmlNode *xvalue1 = resource_get_property_k(res1, cr->column.property); |
343 char *value2 = resource_get_property_k(res2, cr->column.property); |
355 DavXmlNode *xvalue2 = resource_get_property_k(res2, cr->column.property); |
|
356 char *value1 = dav_xml_getstring(xvalue1); |
|
357 char *value2 = dav_xml_getstring(xvalue2); |
344 if(!value1) { |
358 if(!value1) { |
345 ret = value2 ? -1 : 0; |
359 ret = value2 ? -1 : 0; |
346 } else if(!value2) { |
360 } else if(!value2) { |
347 ret = value1 ? 1 : 0; |
361 ret = value1 ? 1 : 0; |
348 } else { |
362 } else { |
401 } |
415 } |
402 } |
416 } |
403 } |
417 } |
404 } |
418 } |
405 |
419 |
406 char* dav_get_property(DavResource *res, char *name) { |
420 char* dav_get_string_property(DavResource *res, char *name) { |
407 char *pns; |
421 char *pns; |
408 char *pname; |
422 char *pname; |
409 dav_get_property_namespace_str(res->session->context, name, &pns, &pname); |
423 dav_get_property_namespace_str(res->session->context, name, &pns, &pname); |
410 if(!pns || !pname) { |
424 if(!pns || !pname) { |
411 return NULL; |
425 return NULL; |
412 } |
426 } |
|
427 return dav_get_string_property_ns(res, pns, pname); |
|
428 } |
|
429 |
|
430 char* dav_get_string_property_ns(DavResource *res, char *ns, char *name) { |
|
431 DavXmlNode *prop = dav_get_property_ns(res, ns, name); |
|
432 if(!prop) { |
|
433 return NULL; |
|
434 } |
|
435 return dav_xml_getstring(prop); |
|
436 } |
|
437 |
|
438 DavXmlNode* dav_get_property(DavResource *res, char *name) { |
|
439 char *pns; |
|
440 char *pname; |
|
441 dav_get_property_namespace_str(res->session->context, name, &pns, &pname); |
|
442 if(!pns || !pname) { |
|
443 return NULL; |
|
444 } |
413 return dav_get_property_ns(res, pns, pname); |
445 return dav_get_property_ns(res, pns, pname); |
414 } |
446 } |
415 |
447 |
416 char* dav_get_property_ns(DavResource *res, char *ns, char *name) { |
448 DavXmlNode* dav_get_property_ns(DavResource *res, char *ns, char *name) { |
417 if(!ns || !name) { |
449 if(!ns || !name) { |
418 return NULL; |
450 return NULL; |
419 } |
451 } |
420 char *property = resource_get_property(res, ns, name); |
452 DavXmlNode *property = resource_get_property(res, ns, name); |
421 DavResourceData *data = res->data; |
453 DavResourceData *data = res->data; |
422 // resource_get_property only returns persistent properties |
454 // resource_get_property only returns persistent properties |
423 // check the remove and set list |
455 // check the remove and set list |
424 if(property) { |
456 if(property) { |
425 // if the property is in the remove list, we return NULL |
457 // if the property is in the remove list, we return NULL |
433 // the set list contains property updates |
465 // the set list contains property updates |
434 // we return an updated property if possible |
466 // we return an updated property if possible |
435 UCX_FOREACH(elm, data->set) { |
467 UCX_FOREACH(elm, data->set) { |
436 DavProperty *p = elm->data; |
468 DavProperty *p = elm->data; |
437 if(!strcmp(p->name, name) && !strcmp(p->ns->name, ns)) { |
469 if(!strcmp(p->name, name) && !strcmp(p->ns->name, ns)) { |
438 return p->value; |
470 return p->value; // TODO: fix |
439 } |
471 } |
440 } |
472 } |
441 // no property update |
473 // no property update |
442 return property; |
474 return property; |
443 } |
475 } |
455 |
487 |
456 DavProperty *property = dav_session_malloc( |
488 DavProperty *property = dav_session_malloc( |
457 res->session, |
489 res->session, |
458 sizeof(DavProperty)); |
490 sizeof(DavProperty)); |
459 property->name = dav_session_strdup(res->session, name); |
491 property->name = dav_session_strdup(res->session, name); |
460 property->value = dav_session_strdup(res->session, value); |
492 property->value = dav_text_node(res->session, value); |
461 |
493 |
462 DavNamespace *namespace = dav_session_malloc( |
494 DavNamespace *namespace = dav_session_malloc( |
463 res->session, |
495 res->session, |
464 sizeof(DavNamespace)); |
496 sizeof(DavNamespace)); |
465 namespace->prefix = NULL; |
497 namespace->prefix = NULL; |
644 // TODO: store the properties later |
676 // TODO: store the properties later |
645 if(resource_add_crypto_info(sn, res->href, res->name, enc_hash)) { |
677 if(resource_add_crypto_info(sn, res->href, res->name, enc_hash)) { |
646 free(enc_hash); |
678 free(enc_hash); |
647 return 1; |
679 return 1; |
648 } |
680 } |
649 resource_add_property(res, DAV_NS, "crypto-hash", enc_hash); |
681 resource_add_string_property(res, DAV_NS, "crypto-hash", enc_hash); |
650 free(enc_hash); |
682 free(enc_hash); |
651 } else { |
683 } else { |
652 ret = do_put_request( |
684 ret = do_put_request( |
653 sn->handle, |
685 sn->handle, |
654 locktoken, |
686 locktoken, |
711 |
743 |
712 // check encryption |
744 // check encryption |
713 AESDecrypter *dec = NULL; |
745 AESDecrypter *dec = NULL; |
714 DavKey *key = NULL; |
746 DavKey *key = NULL; |
715 if(DAV_DECRYPT_CONTENT(sn)) { |
747 if(DAV_DECRYPT_CONTENT(sn)) { |
716 char *keyname = dav_get_property_ns(res, DAV_NS, "crypto-key"); |
748 char *keyname = dav_get_string_property_ns(res, DAV_NS, "crypto-key"); |
717 if(keyname) { |
749 if(keyname) { |
718 key = dav_context_get_key(sn->context, keyname); |
750 key = dav_context_get_key(sn->context, keyname); |
719 if(key) { |
751 if(key) { |
720 dec = aes_decrypter_new(key, stream, write_fnc); |
752 dec = aes_decrypter_new(key, stream, write_fnc); |
721 stream = dec; |
753 stream = dec; |
750 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); |
782 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); |
751 if(ret == CURLE_OK && (status >= 200 && status < 300)) { |
783 if(ret == CURLE_OK && (status >= 200 && status < 300)) { |
752 int verify_failed = 0; |
784 int verify_failed = 0; |
753 if(DAV_DECRYPT_CONTENT(sn) && key) { |
785 if(DAV_DECRYPT_CONTENT(sn) && key) { |
754 // try to verify the content |
786 // try to verify the content |
755 char *res_hash = dav_get_property_ns(res, DAV_NS, "crypto-hash"); |
787 char *res_hash = dav_get_string_property_ns(res, DAV_NS, "crypto-hash"); |
756 |
788 |
757 if(res_hash) { |
789 if(res_hash) { |
758 size_t len = 0; |
790 size_t len = 0; |
759 char *dec_hash = aes_decrypt(res_hash, &len, key); |
791 char *dec_hash = aes_decrypt(res_hash, &len, key); |
760 char *hex_hash = util_hexstr((unsigned char*)dec_hash, 32); |
792 char *hex_hash = util_hexstr((unsigned char*)dec_hash, 32); |