libopkg: support https_proxy
[project/opkg-lede.git] / libopkg / opkg_download.c
index f1553abca7ae5501d178822cdd14fe7e52d784f6..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>
 #include "opkg_defines.h"
 #include "libbb/libbb.h"
 
-#if defined(HAVE_OPENSSL)
-#include <openssl/conf.h>
-#include <openssl/evp.h>
-#include <openssl/err.h>
-#include <openssl/ssl.h>
-#endif
+static int str_starts_with(const char *str, const char *prefix)
+{
+       return (strncmp(str, prefix, strlen(prefix)) == 0);
+}
 
-#if defined(HAVE_OPENSSL)
-#include <openssl/bio.h>
-#include <openssl/objects.h>
-#include <openssl/x509.h>
-#include <openssl/pem.h>
-#include <openssl/hmac.h>
-#endif
+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;
 
-#if defined(HAVE_OPENSSL)
-static void openssl_init(void);
-#endif
+       /* Check file size */
+       err = lstat(filename, &pkg_stat);
 
-#ifdef HAVE_OPENSSL
-static X509_STORE *setup_verify(char *CAfile, char *CApath);
-#endif
+       if (err) {
+               opkg_msg(ERROR, "Failed to stat %s: %s\n",
+                        filename, strerror(errno));
+               return err;
+       }
 
-static int str_starts_with(const char *str, const char *prefix)
-{
-       return (strncmp(str, prefix, strlen(prefix)) == 0);
+       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;
 
@@ -72,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;
        }
 
@@ -95,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",
@@ -110,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";
                }
@@ -144,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;
        }
 
@@ -163,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;
                }
        }
 
@@ -196,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;
 }
 
@@ -207,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,
@@ -225,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
@@ -239,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;
@@ -263,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;
 
@@ -312,78 +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;
+       const char *argv[] = { conf->verify_program, "verify", sig_file,
+                              text_file, NULL };
 
-       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);
-       }
-
-       waitpid(pid, &status, 0);
-       if (!WIFEXITED(status) || WEXITSTATUS(status))
-               return -1;
-
-       return 0;
-#elif defined HAVE_OPENSSL
-       X509_STORE *store = NULL;
-       PKCS7 *p7 = NULL;
-       BIO *in = NULL, *indata = NULL;
-
-       // Sig check failed by default !
-       int status = -1;
-
-       openssl_init();
-
-       // Set-up the key store
-       if (!
-           (store =
-            setup_verify(conf->signature_ca_file, conf->signature_ca_path))) {
-               opkg_msg(ERROR, "Can't open CA certificates.\n");
-               goto verify_file_end;
-       }
-       // Open a BIO to read the sig file
-       if (!(in = BIO_new_file(sig_file, "rb"))) {
-               opkg_msg(ERROR, "Can't open signature file %s.\n", sig_file);
-               goto verify_file_end;
-       }
-       // Read the PKCS7 block contained in the sig file
-       p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
-       if (!p7) {
-               opkg_msg(ERROR, "Can't read signature file %s (Corrupted ?).\n",
-                        sig_file);
-               goto verify_file_end;
-       }
-
-       // Open the Package file to authenticate
-       if (!(indata = BIO_new_file(text_file, "rb"))) {
-               opkg_msg(ERROR, "Can't open file %s.\n", text_file);
-               goto verify_file_end;
-       }
-       // Let's verify the autenticity !
-       if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1) {
-               // Get Off My Lawn!
-               opkg_msg(ERROR, "Verification failure.\n");
-       } else {
-               // Victory !
-               status = 0;
-       }
-
-verify_file_end:
-       BIO_free(in);
-       BIO_free(indata);
-       PKCS7_free(p7);
-       X509_STORE_free(store);
-
-       return status;
+       return xsystem(argv) ? -1 : 0;
 #else
        /* mute `unused variable' warnings. */
        (void)sig_file;
@@ -392,73 +389,3 @@ verify_file_end:
        return 0;
 #endif
 }
-
-#if defined(HAVE_OPENSSL)
-static void openssl_init(void)
-{
-       static int init = 0;
-
-       if (!init) {
-               OPENSSL_config(NULL);
-               OpenSSL_add_all_algorithms();
-               ERR_load_crypto_strings();
-               init = 1;
-       }
-}
-
-#endif
-
-#if defined HAVE_OPENSSL
-static X509_STORE *setup_verify(char *CAfile, char *CApath)
-{
-       X509_STORE *store = NULL;
-       X509_LOOKUP *lookup = NULL;
-
-       if (!(store = X509_STORE_new())) {
-               // Something bad is happening...
-               goto end;
-       }
-       // adds the X509 file lookup method
-       lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
-       if (lookup == NULL) {
-               goto end;
-       }
-       // Autenticating against one CA file
-       if (CAfile) {
-               if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM)) {
-                       // Invalid CA => Bye bye
-                       opkg_msg(ERROR, "Error loading file %s.\n", CAfile);
-                       goto end;
-               }
-       } else {
-               X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
-       }
-
-       // Now look into CApath directory if supplied
-       lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
-       if (lookup == NULL) {
-               goto end;
-       }
-
-       if (CApath) {
-               if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) {
-                       opkg_msg(ERROR, "Error loading directory %s.\n",
-                                CApath);
-                       goto end;
-               }
-       } else {
-               X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
-       }
-
-       // All right !
-       ERR_clear_error();
-       return store;
-
-end:
-
-       X509_STORE_free(store);
-       return NULL;
-
-}
-
-#endif