libopkg: support https_proxy
[project/opkg-lede.git] / libopkg / opkg_download.c
index 33b6e7392d4e7395d3694f073c5d3eee32143da6..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"
 
-#ifdef HAVE_CURL
-#include <curl/curl.h>
-#endif
+static int str_starts_with(const char *str, const char *prefix)
+{
+       return (strncmp(str, prefix, strlen(prefix)) == 0);
+}
 
-#if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
-#include <openssl/conf.h>
-#include <openssl/evp.h>
-#include <openssl/err.h>
-#include <openssl/ssl.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_GPGME)
-#include <gpgme.h>
-#elif defined(HAVE_OPENSSL)
-#include <openssl/bio.h>
-#include <openssl/objects.h>
-#include <openssl/x509.h>
-#include <openssl/pem.h>
-#include <openssl/hmac.h>
-#endif
+       /* Check file size */
+       err = lstat(filename, &pkg_stat);
 
-#ifdef HAVE_PATHFINDER
-#include "opkg_pathfinder.h"
-#endif
+       if (err) {
+               opkg_msg(ERROR, "Failed to stat %s: %s\n",
+                        filename, strerror(errno));
+               return err;
+       }
 
-#if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
-static void openssl_init(void);
-#endif
+       pkg_expected_size = pkg_get_int(pkg, PKG_SIZE);
 
-#ifdef HAVE_OPENSSL
-static X509_STORE *setup_verify(char *CAfile, char *CApath);
-#endif
+       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;
+       }
 
-#ifdef HAVE_CURL
-/*
- * Make curl an instance variable so we don't have to instanciate it
- * each time
- */
-static CURL *curl = NULL;
-static CURL *opkg_curl_init(curl_progress_func cb, void *data);
-#endif
+       /* 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);
+       }
 
-static int str_starts_with(const char *str, const char *prefix)
-{
-       return (strncmp(str, prefix, strlen(prefix)) == 0);
+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;
 
@@ -91,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;
        }
 
@@ -114,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",
@@ -126,41 +151,22 @@ opkg_download(const char *src, const char *dest_file_name,
                         conf->no_proxy);
                setenv("no_proxy", conf->no_proxy, 1);
        }
-#ifdef HAVE_CURL
-       CURLcode res;
-       FILE *file = fopen(tmp_file_location, "w");
-
-       curl = opkg_curl_init(cb, data);
-       if (curl) {
-               curl_easy_setopt(curl, CURLOPT_URL, src);
-               curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
 
-               res = curl_easy_perform(curl);
-               fclose(file);
-               if (res) {
-                       long error_code;
-                       curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
-                                         &error_code);
-                       opkg_msg(hide_error ? DEBUG2 : ERROR,
-                                "Failed to download %s: %s.\n", src,
-                                curl_easy_strerror(res));
-                       free(tmp_file_location);
-                       return -1;
-               }
-
-       } else {
-               free(tmp_file_location);
-               return -1;
-       }
-#else
        {
                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";
                }
@@ -181,7 +187,6 @@ opkg_download(const char *src, const char *dest_file_name,
                        return -1;
                }
        }
-#endif
 
        err = file_move(tmp_file_location, dest_file_name);
 
@@ -190,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;
        }
 
@@ -209,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;
                }
        }
 
@@ -242,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;
 }
 
@@ -253,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,
@@ -271,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
@@ -285,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;
@@ -309,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;
 
@@ -358,152 +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);
-       }
-
-       waitpid(pid, &status, 0);
-       if (!WIFEXITED(status) || WEXITSTATUS(status))
-               return -1;
-
-       return 0;
-#elif defined HAVE_GPGME
-       if (conf->check_signature == 0)
-               return 0;
-       int status = -1;
-       gpgme_ctx_t ctx;
-       gpgme_data_t sig, text, key;
-       gpgme_error_t err;
-       gpgme_verify_result_t result;
-       gpgme_signature_t s;
-       char *trusted_path = NULL;
-
-       gpgme_check_version(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;
+       const char *argv[] = { conf->verify_program, "verify", sig_file,
+                              text_file, NULL };
 
-       /* see if any of the signitures matched */
-       s = result->signatures;
-       while (s) {
-               status = gpg_err_code(s->status);
-               if (status == GPG_ERR_NO_ERROR)
-                       break;
-               s = s->next;
-       }
-
-       gpgme_data_release(sig);
-       gpgme_data_release(text);
-       gpgme_release(ctx);
-
-       return status;
-#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;
-       }
-#if defined(HAVE_PATHFINDER)
-       if (conf->check_x509_path) {
-               if (!pkcs7_pathfinder_verify_signers(p7)) {
-                       opkg_msg(ERROR, "pkcs7_pathfinder_verify_signers: "
-                                "Path verification failed.\n");
-                       goto verify_file_end;
-               }
-       }
-#endif
-
-       // 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;
@@ -512,211 +389,3 @@ verify_file_end:
        return 0;
 #endif
 }
-
-#if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
-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
-
-#ifdef HAVE_CURL
-void opkg_curl_cleanup(void)
-{
-       if (curl != NULL) {
-               curl_easy_cleanup(curl);
-               curl = NULL;
-       }
-}
-
-static CURL *opkg_curl_init(curl_progress_func cb, void *data)
-{
-
-       if (curl == NULL) {
-               curl = curl_easy_init();
-
-#ifdef HAVE_SSLCURL
-               openssl_init();
-
-               if (conf->ssl_engine) {
-
-                       /* use crypto engine */
-                       if (curl_easy_setopt
-                           (curl, CURLOPT_SSLENGINE,
-                            conf->ssl_engine) != CURLE_OK) {
-                               opkg_msg(ERROR,
-                                        "Can't set crypto engine '%s'.\n",
-                                        conf->ssl_engine);
-
-                               opkg_curl_cleanup();
-                               return NULL;
-                       }
-                       /* set the crypto engine as default */
-                       if (curl_easy_setopt
-                           (curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK) {
-                               opkg_msg(ERROR,
-                                        "Can't set crypto engine '%s' as default.\n",
-                                        conf->ssl_engine);
-
-                               opkg_curl_cleanup();
-                               return NULL;
-                       }
-               }
-
-               /* cert & key can only be in PEM case in the same file */
-               if (conf->ssl_key_passwd) {
-                       if (curl_easy_setopt
-                           (curl, CURLOPT_SSLKEYPASSWD,
-                            conf->ssl_key_passwd) != CURLE_OK) {
-                               opkg_msg(DEBUG,
-                                        "Failed to set key password.\n");
-                       }
-               }
-
-               /* sets the client certificate and its type */
-               if (conf->ssl_cert_type) {
-                       if (curl_easy_setopt
-                           (curl, CURLOPT_SSLCERTTYPE,
-                            conf->ssl_cert_type) != CURLE_OK) {
-                               opkg_msg(DEBUG,
-                                        "Failed to set certificate format.\n");
-                       }
-               }
-               /* SSL cert name isn't mandatory */
-               if (conf->ssl_cert) {
-                       curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert);
-               }
-
-               /* sets the client key and its type */
-               if (conf->ssl_key_type) {
-                       if (curl_easy_setopt
-                           (curl, CURLOPT_SSLKEYTYPE,
-                            conf->ssl_key_type) != CURLE_OK) {
-                               opkg_msg(DEBUG, "Failed to set key format.\n");
-                       }
-               }
-               if (conf->ssl_key) {
-                       if (curl_easy_setopt
-                           (curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK) {
-                               opkg_msg(DEBUG, "Failed to set key.\n");
-                       }
-               }
-
-               /* Should we verify the peer certificate ? */
-               if (conf->ssl_dont_verify_peer) {
-                       /*
-                        * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
-                        */
-                       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
-               } else {
-#ifdef HAVE_PATHFINDER
-                       if (conf->check_x509_path) {
-                               if (curl_easy_setopt
-                                   (curl, CURLOPT_SSL_CTX_FUNCTION,
-                                    curl_ssl_ctx_function) != CURLE_OK) {
-                                       opkg_msg(DEBUG,
-                                                "Failed to set ssl path verification callback.\n");
-                               } else {
-                                       curl_easy_setopt(curl,
-                                                        CURLOPT_SSL_CTX_DATA,
-                                                        NULL);
-                               }
-                       }
-#endif
-               }
-
-               /* certification authority file and/or path */
-               if (conf->ssl_ca_file) {
-                       curl_easy_setopt(curl, CURLOPT_CAINFO,
-                                        conf->ssl_ca_file);
-               }
-               if (conf->ssl_ca_path) {
-                       curl_easy_setopt(curl, CURLOPT_CAPATH,
-                                        conf->ssl_ca_path);
-               }
-#endif
-
-               curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-               curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
-               if (conf->http_proxy || conf->ftp_proxy) {
-                       char *userpwd;
-                       sprintf_alloc(&userpwd, "%s:%s", conf->proxy_user,
-                                     conf->proxy_passwd);
-                       curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
-                       free(userpwd);
-               }
-       }
-
-       curl_easy_setopt(curl, CURLOPT_NOPROGRESS, (cb == NULL));
-       if (cb) {
-               curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, data);
-               curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, cb);
-       }
-
-       return curl;
-
-}
-#endif