opkg: Fix a small memory leak in opkg_download.
[oweals/opkg-lede.git] / libopkg / opkg_download.c
index d994670dbd66912e81e9194511a74d88721b1546..629aa798aa181f500b55fbc13127fd95efb987bf 100644 (file)
 #include "str_util.h"
 #include "opkg_defines.h"
 
-opkg_download_progress_callback opkg_cb_download_progress = NULL;
-
-int
-curl_progress_func (char* url,
-                   double t, /* dltotal */
-                   double d, /* dlnow */
-                   double ultotal,
-                   double ulnow)
-{
-    int p = (t) ? d*100/t : 0;
-
-    if (opkg_cb_download_progress)
-    {
-       static int prev = -1;
-
-       /* don't report the same percentage multiple times
-        * (this can occur due to rounding) */
-       if (prev == p)
-           return 0;
-       prev = p;
-
-       opkg_cb_download_progress (p, url);
-       return 0;
-    }
-    return 0;
-}
-
-int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name)
+int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name, curl_progress_func cb, void *data)
 {
     int err = 0;
 
@@ -76,6 +49,7 @@ int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name
        opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
        ret = file_copy(src + 5, dest_file_name);
        opkg_message(conf,OPKG_INFO,"Done\n");
+        free(src_basec);
        return ret;
     }
 
@@ -85,6 +59,7 @@ int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name
        opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
                __FUNCTION__, tmp_file_location, strerror(errno));
        free(tmp_file_location);
+        free(src_basec);
        return errno;
     }
 
@@ -110,9 +85,12 @@ int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name
     {
        curl_easy_setopt (curl, CURLOPT_URL, src);
        curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
-       curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
-       curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src);
-       curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
+       curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));
+       if (cb)
+       {
+               curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data);
+               curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb);
+       }
        curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
        if (conf->http_proxy || conf->ftp_proxy)
        {
@@ -123,19 +101,26 @@ int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name
            free (userpwd);
        }
        res = curl_easy_perform (curl);
-       curl_easy_cleanup (curl);
        fclose (file);
        if (res)
        {
            long error_code;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
            opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, error_code);
+           free(tmp_file_location);
+            free(src_basec);
+           curl_easy_cleanup (curl);
            return res;
        }
+       curl_easy_cleanup (curl);
 
     }
     else
+    {
+       free(tmp_file_location);
+        free(src_basec);
        return -1;
+    }
 
     err = file_move(tmp_file_location, dest_file_name);
 
@@ -179,7 +164,7 @@ int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
 
     sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
 
-    err = opkg_download(conf, url, pkg->local_filename);
+    err = opkg_download(conf, url, pkg->local_filename, NULL, NULL);
     free(url);
 
     opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);
@@ -204,7 +189,7 @@ int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **name
          char *file_base = basename(file_basec);
 
          sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
-         err = opkg_download(conf, url, tmp_file);
+         err = opkg_download(conf, url, tmp_file, NULL, NULL);
          if (err)
               return err;
 
@@ -257,27 +242,52 @@ opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
 #ifdef HAVE_GPGME
     int status = -1;
     gpgme_ctx_t ctx;
-    gpgme_data_t sig, text;
+    gpgme_data_t sig, text, key;
     gpgme_error_t err = -1;
     gpgme_verify_result_t result;
     gpgme_signature_t s;
+    char *trusted_path = NULL;
     
     err = gpgme_new (&ctx);
 
     if (err)
        return -1;
 
+    sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg");
+    err = gpgme_data_new_from_file (&key, trusted_path, 1); 
+    free (trusted_path);
+    if (err)
+    {
+      return -1;
+    }
+    err = gpgme_op_import (ctx, key);
+    if (err)
+    {
+      gpgme_data_release (key);
+      return -1;
+    }
+    gpgme_data_release (key);
+
     err = gpgme_data_new_from_file (&sig, sig_file, 1); 
     if (err)
+    {
+       gpgme_release (ctx);
        return -1;
+    }
 
     err = gpgme_data_new_from_file (&text, text_file, 1); 
     if (err)
+    {
+        gpgme_data_release (sig);
+       gpgme_release (ctx);
        return -1;
+    }
 
     err = gpgme_op_verify (ctx, sig, text, NULL);
 
     result = gpgme_op_verify_result (ctx);
+    if (!result)
+       return -1;
 
     /* see if any of the signitures matched */
     s = result->signatures;
@@ -289,6 +299,7 @@ opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
        s = s->next;
     }
 
+
     gpgme_data_release (sig);
     gpgme_data_release (text);
     gpgme_release (ctx);