libopkg: support https_proxy
[project/opkg-lede.git] / libopkg / opkg_download.c
index 148d08bb6b0861eb5c9427ba100d2a8c6850dcd2..af91f12a429ff33994c6da37d55cee45bcef97af 100644 (file)
@@ -17,7 +17,6 @@
    General Public License for more details.
 */
 
-#include <sys/wait.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <libgen.h>
@@ -36,9 +35,70 @@ static int str_starts_with(const char *str, const char *prefix)
        return (strncmp(str, prefix, strlen(prefix)) == 0);
 }
 
+int opkg_verify_integrity(pkg_t *pkg, const char *filename)
+{
+       int err = 0;
+       char *file_md5, *pkg_md5;
+       char *file_sha256, *pkg_sha256;
+       struct stat pkg_stat;
+       long long int pkg_expected_size;
+
+       /* Check file size */
+       err = lstat(filename, &pkg_stat);
+
+       if (err) {
+               opkg_msg(ERROR, "Failed to stat %s: %s\n",
+                        filename, strerror(errno));
+               return err;
+       }
+
+       pkg_expected_size = pkg_get_int(pkg, PKG_SIZE);
+
+       if (pkg_expected_size > 0 && pkg_stat.st_size != pkg_expected_size) {
+               opkg_msg(INFO,
+                        "Package size mismatch: %s is %lld bytes, expecting %lld bytes\n",
+                        pkg->name, (long long int)pkg_stat.st_size, pkg_expected_size);
+               err = -1;
+               goto out;
+       }
+
+       /* Check for md5 values */
+       pkg_md5 = pkg_get_md5(pkg);
+       if (pkg_md5) {
+               file_md5 = file_md5sum_alloc(filename);
+               if (file_md5 && strcmp(file_md5, pkg_md5)) {
+                       opkg_msg(INFO, "Package %s md5sum mismatch.\n",
+                                pkg->name);
+                       err = -1;
+                       free(file_md5);
+                       goto out;
+               }
+               if (file_md5)
+                       free(file_md5);
+       }
+
+       /* Check for sha256 value */
+       pkg_sha256 = pkg_get_sha256(pkg);
+       if (pkg_sha256) {
+               file_sha256 = file_sha256sum_alloc(filename);
+               if (file_sha256 && strcmp(file_sha256, pkg_sha256)) {
+                       opkg_msg(INFO, "Package %s sha256sum mismatch.\n",
+                                pkg->name);
+                       err = -1;
+                       free(file_sha256);
+                       goto out;
+               }
+               if (file_sha256)
+                       free(file_sha256);
+       }
+
+out:
+       return err;
+}
+
 int
 opkg_download(const char *src, const char *dest_file_name,
-             curl_progress_func cb, void *data, const short hide_error)
+              const short hide_error)
 {
        int err = 0;
 
@@ -49,11 +109,12 @@ opkg_download(const char *src, const char *dest_file_name,
        opkg_msg(NOTICE, "Downloading %s\n", src);
 
        if (str_starts_with(src, "file:")) {
-               const char *file_src = src + 5;
+               char *file_src = urldecode_path(src + 5);
                opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name);
                err = file_copy(file_src, dest_file_name);
                opkg_msg(INFO, "Done.\n");
                free(src_basec);
+               free(file_src);
                return err;
        }
 
@@ -72,6 +133,12 @@ opkg_download(const char *src, const char *dest_file_name,
                         conf->http_proxy);
                setenv("http_proxy", conf->http_proxy, 1);
        }
+       if (conf->https_proxy) {
+               opkg_msg(DEBUG,
+                        "Setting environment variable: https_proxy = %s.\n",
+                        conf->https_proxy);
+               setenv("https_proxy", conf->https_proxy, 1);
+       }
        if (conf->ftp_proxy) {
                opkg_msg(DEBUG,
                         "Setting environment variable: ftp_proxy = %s.\n",
@@ -87,12 +154,19 @@ opkg_download(const char *src, const char *dest_file_name,
 
        {
                int res;
-               const char *argv[8];
+               const char *argv[11];
                int i = 0;
 
                argv[i++] = "wget";
                argv[i++] = "-q";
-               if (conf->http_proxy || conf->ftp_proxy) {
+               if (conf->no_check_certificate) {
+                       argv[i++] = "--no-check-certificate";
+               }
+               if (conf->http_timeout) {
+                       argv[i++] = "--timeout";
+                       argv[i++] = conf->http_timeout;
+               }
+               if (conf->http_proxy || conf->https_proxy || conf->ftp_proxy) {
                        argv[i++] = "-Y";
                        argv[i++] = "on";
                }
@@ -121,16 +195,25 @@ opkg_download(const char *src, const char *dest_file_name,
        return err;
 }
 
+static char* get_cache_filename(const char *dest_file_name)
+{
+       char *cache_name;
+       char *filename = strrchr(dest_file_name, '/');
+       if (filename)
+               cache_name = xstrdup(filename + 1);     // strip leading '/'
+       else
+               cache_name = xstrdup(dest_file_name);
+       return cache_name;
+}
+
 static int
-opkg_download_cache(const char *src, const char *dest_file_name,
-                   curl_progress_func cb, void *data)
+opkg_download_cache(const char *src, const char *dest_file_name)
 {
-       char *cache_name = xstrdup(src);
-       char *cache_location, *p;
+       char *cache_name, *cache_location;
        int err = 0;
 
        if (!conf->cache || str_starts_with(src, "file:")) {
-               err = opkg_download(src, dest_file_name, cb, data, 0);
+               err = opkg_download(src, dest_file_name, 0);
                goto out1;
        }
 
@@ -140,32 +223,15 @@ opkg_download_cache(const char *src, const char *dest_file_name,
                goto out1;
        }
 
-       for (p = cache_name; *p; p++)
-               if (*p == '/')
-                       *p = ',';       /* looks nicer than | or # */
-
+       cache_name = get_cache_filename(dest_file_name);
        sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
        if (file_exists(cache_location))
                opkg_msg(NOTICE, "Copying %s.\n", cache_location);
        else {
-               /* cache file with funky name not found, try simple name */
-               free(cache_name);
-               char *filename = strrchr(dest_file_name, '/');
-               if (filename)
-                       cache_name = xstrdup(filename + 1);     // strip leading '/'
-               else
-                       cache_name = xstrdup(dest_file_name);
-               free(cache_location);
-               sprintf_alloc(&cache_location, "%s/%s", conf->cache,
-                             cache_name);
-               if (file_exists(cache_location))
-                       opkg_msg(NOTICE, "Copying %s.\n", cache_location);
-               else {
-                       err = opkg_download(src, cache_location, cb, data, 0);
-                       if (err) {
-                               (void)unlink(cache_location);
-                               goto out2;
-                       }
+               err = opkg_download(src, cache_location, 0);
+               if (err) {
+                       (void)unlink(cache_location);
+                       goto out2;
                }
        }
 
@@ -173,8 +239,8 @@ opkg_download_cache(const char *src, const char *dest_file_name,
 
 out2:
        free(cache_location);
-out1:
        free(cache_name);
+out1:
        return err;
 }
 
@@ -184,7 +250,10 @@ int opkg_download_pkg(pkg_t * pkg, const char *dir)
        char *url;
        char *local_filename;
        char *stripped_filename;
+       char *urlencoded_path;
        char *filename;
+       char *cache_name;
+       char *cache_location;
 
        if (pkg->src == NULL) {
                opkg_msg(ERROR,
@@ -202,7 +271,9 @@ int opkg_download_pkg(pkg_t * pkg, const char *dir)
                return -1;
        }
 
-       sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
+       urlencoded_path = urlencode_path(filename);
+       sprintf_alloc(&url, "%s/%s", pkg->src->value, urlencoded_path);
+       free(urlencoded_path);
 
        /* The filename might be something like
           "../../foo.opk". While this is correct, and exactly what we
@@ -216,7 +287,24 @@ int opkg_download_pkg(pkg_t * pkg, const char *dir)
        sprintf_alloc(&local_filename, "%s/%s", dir, stripped_filename);
        pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
 
-       err = opkg_download_cache(url, local_filename, NULL, NULL);
+       /* Invalidate/remove cached package if it has an incorrect checksum. */
+       if (conf->cache) {
+               cache_name = get_cache_filename(local_filename);
+               sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
+               free(cache_name);
+               if (file_exists(cache_location)) {
+                       err = opkg_verify_integrity(pkg, cache_location);
+                       if (err) {
+                               opkg_msg(NOTICE,
+                                        "Removing %s from cache because it has incorrect checksum.\n",
+                                        pkg->name);
+                               unlink(cache_location);
+                       }
+               }
+               free(cache_location);
+       }
+
+       err = opkg_download_cache(url, local_filename);
        free(url);
 
        return err;
@@ -240,7 +328,7 @@ int opkg_prepare_url_for_install(const char *url, char **namep)
                char *file_base = basename(file_basec);
 
                sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
-               err = opkg_download(url, tmp_file, NULL, NULL, 0);
+               err = opkg_download(url, tmp_file, 0);
                if (err)
                        return err;
 
@@ -289,27 +377,10 @@ int opkg_prepare_url_for_install(const char *url, char **namep)
 int opkg_verify_file(char *text_file, char *sig_file)
 {
 #if defined HAVE_USIGN
-       int status = -1;
-       int pid;
-
-       if (conf->check_signature == 0)
-               return 0;
-
-       pid = fork();
-       if (pid < 0)
-               return -1;
-
-       if (!pid) {
-               execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file,
-                     text_file, NULL);
-               exit(255);
-       }
+       const char *argv[] = { conf->verify_program, "verify", sig_file,
+                              text_file, NULL };
 
-       waitpid(pid, &status, 0);
-       if (!WIFEXITED(status) || WEXITSTATUS(status))
-               return -1;
-
-       return 0;
+       return xsystem(argv) ? -1 : 0;
 #else
        /* mute `unused variable' warnings. */
        (void)sig_file;