fixed some memory leaks, content verification and dav info

Sun, 04 Oct 2015 18:35:15 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 04 Oct 2015 18:35:15 +0200
changeset 151
a316613205dc
parent 150
37fb12574acd
child 152
ff854d3df20c

fixed some memory leaks, content verification and dav info

dav/main.c file | annotate | diff | comparison | revisions
libidav/resource.c file | annotate | diff | comparison | revisions
libidav/session.c file | annotate | diff | comparison | revisions
libidav/utils.c file | annotate | diff | comparison | revisions
libidav/utils.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Sun Oct 04 15:57:40 2015 +0200
+++ b/dav/main.c	Sun Oct 04 18:35:15 2015 +0200
@@ -1077,9 +1077,11 @@
         printf("name: %s\n", res->name);
         printf("path: %s\n", res->path);
         
-        char *url = util_path_to_url(sn, res->path);
+        char *server = util_url_base(sn->base_url);
+        char *url = util_concat_path(server, res->href);
         printf("url:  %s\n", url);
         free(url);
+        free(server);
         
         if(res->iscollection) {
             printf("type: collection\n");
--- a/libidav/resource.c	Sun Oct 04 15:57:40 2015 +0200
+++ b/libidav/resource.c	Sun Oct 04 18:35:15 2015 +0200
@@ -650,10 +650,11 @@
     
     // check encryption
     AESDecrypter *dec = NULL;
+    DavKey *key = NULL;
     if(DAV_DECRYPT_CONTENT(sn)) {
         char *keyname = dav_get_property_ns(res, DAV_NS, "crypto-key");
         if(keyname) {
-            DavKey *key = dav_context_get_key(sn->context, keyname);
+            key = dav_context_get_key(sn->context, keyname);
             if(key) {
                 dec = aes_decrypter_new(key, stream, write_fnc);
                 stream = dec;
@@ -686,13 +687,13 @@
     curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
     if(ret == CURLE_OK && (status >= 200 && status < 300)) {
         int verify_failed = 0;
-        if(DAV_DECRYPT_CONTENT(sn)) {
+        if(DAV_DECRYPT_CONTENT(sn) && key) {
             // try to verify the content
             char *res_hash = dav_get_property_ns(res, DAV_NS, "crypto-hash");
 
             if(res_hash) {
                 size_t len = 0;
-                char *dec_hash = aes_decrypt(res_hash, &len, sn->key);
+                char *dec_hash = aes_decrypt(res_hash, &len, key);
                 char *hex_hash = util_hexstr(dec_hash, 32);
                 if(strcmp(hash, hex_hash)) {
                     verify_failed = 1;
@@ -701,6 +702,9 @@
                 free(hex_hash);
             }
         }
+        if(hash) {
+            free(hash);
+        }
         
         if(verify_failed) {
             res->session->error = DAV_CONTENT_VERIFICATION_ERROR;
@@ -710,6 +714,9 @@
         res->session->error = DAV_OK;
         return 0;
     } else {
+        if(hash) {
+            free(hash);
+        }
         dav_session_set_error(res->session, ret, status);
         return 1;
     }
--- a/libidav/session.c	Sun Oct 04 15:57:40 2015 +0200
+++ b/libidav/session.c	Sun Oct 04 18:35:15 2015 +0200
@@ -304,9 +304,12 @@
                     ucx_buffer_puts(href, random_name);
                     free(random_name);
                 }
-                
             }
+            
+            // cleanup
+            free(elm.ptr);
         }
+        free(elms);
         
         // if necessary add a path separator
         if(p.ptr[p.length-1] == '/') {
--- a/libidav/utils.c	Sun Oct 04 15:57:40 2015 +0200
+++ b/libidav/utils.c	Sun Oct 04 18:35:15 2015 +0200
@@ -87,6 +87,35 @@
     }
 }
 
+char* util_url_base(char *url) {
+    sstr_t u = sstr(url);
+    int len = u.length;
+    int slashcount = 0;
+    int slmax;
+    if(len > 7 && !strncasecmp(url, "http://", 7)) {
+        slmax = 3;
+    } else if(len > 8 && !strncasecmp(url, "https://", 8)) {
+        slmax = 3;
+    } else {
+        slmax = 1;
+    }
+    char c;
+    int i = 0;
+    for(i=0;i<len;i++) {
+        c = url[i];
+        if(c == '/') {
+            slashcount++;
+            if(slashcount == slmax) {
+                i++;
+                break;
+            }
+        }
+    } 
+    sstr_t server = sstrsubsl(u, 0, i);
+    server = sstrdup(server);
+    return server.ptr;
+}
+
 char* util_url_path(char *url) { 
     char *path = NULL;
     size_t len = strlen(url);
--- a/libidav/utils.h	Sun Oct 04 15:57:40 2015 +0200
+++ b/libidav/utils.h	Sun Oct 04 18:35:15 2015 +0200
@@ -58,6 +58,7 @@
 
 int util_mkdir(char *path, mode_t mode);
 
+char* util_url_base(char *url);
 char* util_url_path(char *url);
 char* util_url_decode(DavSession *sn, char *url);
 char* util_resource_name(char *url);

mercurial