Remove str_util.{c,h}
[oweals/opkg-lede.git] / libopkg / opkg_download.c
index 0e6792763a04c03046b114d42e03be66ec001152..49a48a08631cdbfa308026c615abd962dfa1453d 100644 (file)
@@ -46,7 +46,6 @@
 #include "sprintf_alloc.h"
 #include "xsystem.h"
 #include "file_util.h"
-#include "str_util.h"
 #include "opkg_defines.h"
 #include "libbb/libbb.h"
 
@@ -68,10 +67,15 @@ static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath);
  * each time
  */
 static CURL *curl = NULL;
-static void opkg_curl_cleanup(void);
 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data);
 #endif
 
+static int
+str_starts_with(const char *str, const char *prefix)
+{
+    return (strncmp(str, prefix, strlen(prefix)) == 0);
+}
+
 int opkg_download(opkg_conf_t *conf, const char *src,
   const char *dest_file_name, curl_progress_func cb, void *data)
 {
@@ -117,7 +121,6 @@ int opkg_download(opkg_conf_t *conf, const char *src,
     }
 
 #ifdef HAVE_CURL
-    CURL *curl;
     CURLcode res;
     FILE * file = fopen (tmp_file_location, "w");
 
@@ -149,16 +152,21 @@ int opkg_download(opkg_conf_t *conf, const char *src,
 #else
     {
       int res;
-      char *wgetcmd;
-      char *wgetopts;
-      wgetopts = getenv("OPKG_WGETOPTS");
-      sprintf_alloc(&wgetcmd, "wget -q %s%s -O \"%s\" \"%s\"",
-                   (conf->http_proxy || conf->ftp_proxy) ? "-Y on " : "",
-                   (wgetopts!=NULL) ? wgetopts : "",
-                   tmp_file_location, src);
-      opkg_message(conf, OPKG_INFO, "Executing: %s\n", wgetcmd);
-      res = xsystem(wgetcmd);
-      free(wgetcmd);
+      const char *argv[8];
+      int i = 0;
+
+      argv[i++] = "wget";
+      argv[i++] = "-q";
+      if (conf->http_proxy || conf->ftp_proxy) {
+       argv[i++] = "-Y";
+       argv[i++] = "on";
+      }
+      argv[i++] = "-O";
+      argv[i++] = tmp_file_location;
+      argv[i++] = src;
+      argv[i++] = NULL;
+      res = xsystem(argv);
+
       if (res) {
        opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, res);
        free(tmp_file_location);
@@ -259,9 +267,8 @@ int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **name
 {
      int err = 0;
      pkg_t *pkg;
+
      pkg = pkg_new();
-     if (pkg == NULL)
-         return ENOMEM;
 
      if (str_starts_with(url, "http://")
         || str_starts_with(url, "ftp://")) {
@@ -274,7 +281,7 @@ int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **name
          if (err)
               return err;
 
-         err = pkg_init_from_file(pkg, tmp_file);
+         err = pkg_init_from_file(conf, pkg, tmp_file);
          if (err)
               return err;
 
@@ -285,7 +292,7 @@ int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **name
                 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
                || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
 
-         err = pkg_init_from_file(pkg, url);
+         err = pkg_init_from_file(conf, pkg, url);
          if (err)
               return err;
          opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
@@ -306,12 +313,9 @@ int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **name
      pkg->state_want = SW_INSTALL;
      pkg->state_flag |= SF_PREFER;
      pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);  
-     if ( pkg == NULL ){
-        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
-        return 0;
-     }
+
      if (namep) {
-         *namep = xstrdup(pkg->name);
+         *namep = pkg->name;
      }
      return 0;
 }
@@ -419,11 +423,13 @@ opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
         goto verify_file_end;
     }
 #if defined(HAVE_PATHFINDER)
-    if(!pkcs7_pathfinder_verify_signers(p7)){
-       opkg_message(conf,  OPKG_ERROR, "pkcs7_pathfinder_verify_signers: "
-               "Path verification failed\n");
+    if(conf->check_x509_path){
+       if(!pkcs7_pathfinder_verify_signers(p7)){
+           opkg_message(conf,  OPKG_ERROR, "pkcs7_pathfinder_verify_signers: "
+                   "Path verification failed\n");
+           goto verify_file_end;
+       }
     }
-
 #endif
 
     // Open the Package file to authenticate
@@ -533,7 +539,7 @@ end:
 #endif
 
 #ifdef HAVE_CURL
-static void opkg_curl_cleanup(void){
+void opkg_curl_cleanup(void){
     if(curl != NULL){
        curl_easy_cleanup (curl);
        curl = NULL;
@@ -609,13 +615,13 @@ static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
        }else{
 #ifdef HAVE_PATHFINDER
-           if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_ssl_ctx_function) != CURLE_OK){
-               opkg_message(conf, OPKG_DEBUG, "Failed to set ssl path verification callback\n");
-           }else{
-               curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, NULL);
+           if(conf->check_x509_path){
+               if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_ssl_ctx_function) != CURLE_OK){
+                   opkg_message(conf, OPKG_DEBUG, "Failed to set ssl path verification callback\n");
+               }else{
+                   curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, NULL);
+               }
            }
-
-           //curl_easy_setopt(curl, CURLOPT_SSL_CERT_VERIFY_FUNCTION, curlcb_pathfinder);
 #endif
        }
 
@@ -638,12 +644,6 @@ static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data
            curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
            free (userpwd);
        }
-
-       /* add curl cleanup callback */
-       if(!atexit(opkg_curl_cleanup)){
-           opkg_message(conf,OPKG_DEBUG, "Failed to register atexit curl cleanup function\n");
-       }
-
     }
 
     curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));