opkg: fix nullpointer dereference
[oweals/opkg-lede.git] / libopkg / opkg.c
index a4fefa0eadb1e74c2909f5a50839fa4de6dfffe8..d6b66584317cb9666161675a815cb14abf2d23d1 100644 (file)
@@ -34,8 +34,6 @@
 
 #include <libbb/libbb.h>
 
-struct errlist* error_list;
-
 struct _opkg_t
 {
   args_t *args;
@@ -56,7 +54,7 @@ struct _opkg_t
  * Clone a pkg_t 
  */ 
 static opkg_package_t*
-pkg_clone (pkg_t *old)
+pkg_t_to_opkg_package_t (pkg_t *old)
 {
   opkg_package_t *new;
 
@@ -249,6 +247,7 @@ opkg_re_read_config_files (opkg_t *opkg)
   a->autoremove = c->autoremove;
   a->force_depends = c->force_depends;
   a->force_defaults = c->force_defaults;
+  a->force_maintainer = c->force_maintainer;
   a->force_overwrite = c->force_overwrite;
   a->force_downgrade = c->force_downgrade;
   a->force_reinstall = c->force_reinstall;
@@ -387,6 +386,12 @@ opkg_set_option (opkg_t *opkg, char *option, void *value)
 
 }
 
+/**
+ * @brief libopkg API: Install package
+ * @param opkg Then opkg handler
+ * @param package_name The name of package in which is going to install
+ * @param progress_callback The callback function that report the status to caller. 
+ */ 
 int
 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
 {
@@ -423,7 +428,7 @@ opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_call
   new->state_flag |= SF_USER;
 
   pdata.action = OPKG_INSTALL;
-  pdata.package = pkg_clone (new);
+  pdata.package = pkg_t_to_opkg_package_t (new);
 
   progress (pdata, 0);
 
@@ -453,7 +458,7 @@ opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_call
       continue;
 
     opkg_package_free (pdata.package);
-    pdata.package = pkg_clone (pkg);
+    pdata.package = pkg_t_to_opkg_package_t (pkg);
     pdata.action = OPKG_DOWNLOAD;
 
     if (pkg->src == NULL)
@@ -505,7 +510,7 @@ opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_call
 
   /* 75% of "install" progress is for downloading */
   opkg_package_free (pdata.package);
-  pdata.package = pkg_clone (new);
+  pdata.package = pkg_t_to_opkg_package_t (new);
   pdata.action = OPKG_INSTALL;
   progress (pdata, 75);
 
@@ -524,6 +529,7 @@ opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_call
       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
+      case OPKG_INSTALL_ERR_SHA256: return OPKG_SHA256_ERROR;
       default: return OPKG_UNKNOWN_ERROR;
     }
   }
@@ -569,7 +575,7 @@ opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callb
   }
 
   pdata.action = OPKG_REMOVE;
-  pdata.package = pkg_clone (pkg);
+  pdata.package = pkg_t_to_opkg_package_t (pkg);
   progress (pdata, 0);
 
 
@@ -645,7 +651,7 @@ opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_call
   }
 
   pdata.action = OPKG_INSTALL;
-  pdata.package = pkg_clone (pkg);
+  pdata.package = pkg_t_to_opkg_package_t (pkg);
   progress (pdata, 0);
 
   err = opkg_upgrade_pkg (opkg->conf, pkg);
@@ -663,6 +669,7 @@ opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_call
       case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
       case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
       case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
+      case OPKG_INSTALL_ERR_SHA256: return OPKG_SHA256_ERROR;
       default: return OPKG_UNKNOWN_ERROR;
     }
   }
@@ -706,7 +713,7 @@ opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void
   {
     pkg = installed->pkgs[i];
 
-    pdata.package = pkg_clone (pkg);
+    pdata.package = pkg_t_to_opkg_package_t (pkg);
     progress (pdata, 99 * i / installed->len);
     opkg_package_free (pdata.package);
 
@@ -779,18 +786,16 @@ opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callb
   /* count the number of sources so we can give some progress updates */
   sources_list_count = 0;
   sources_done = 0;
-  iter = opkg->conf->pkg_src_list.head;
-  while (iter)
+  list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
   {
     sources_list_count++;
-    iter = iter->next;
   }
 
-  for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
+  list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
   {
     char *url, *list_file_name = NULL;
 
-    src = iter->data;
+    src = (pkg_src_t *)iter->data;
 
     if (src->extra_data)  /* debian style? */
       sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
@@ -845,43 +850,45 @@ opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callb
     }
     free (url);
 
-#ifdef HAVE_GPGME
-    char *sig_file_name;
-    /* download detached signitures to verify the package lists */
-    /* get the url for the sig file */
-    if (src->extra_data)  /* debian style? */
-      sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
-                     "Packages.sig");
-    else
-      sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
+#if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
+    if ( opkg->conf->check_signature ) {
+        char *sig_file_name;
+        /* download detached signitures to verify the package lists */
+        /* get the url for the sig file */
+        if (src->extra_data)  /* debian style? */
+            sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
+                    "Packages.sig");
+        else
+            sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
 
-    /* create filename for signature */
-    sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
+        /* create filename for signature */
+        sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
 
-    /* make sure there is no existing signature file */
-    unlink (sig_file_name);
+        /* make sure there is no existing signature file */
+        unlink (sig_file_name);
 
-    err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
-    if (err)
-    {
-      /* XXX: Warning: Download failed */
-    }
-    else
-    {
-      int err;
-      err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
-      if (err == 0)
-      {
-        /* XXX: Notice: Signature check passed */
-      }
-      else
-      {
-        /* XXX: Warning: Signature check failed */
-      }
+        err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
+        if (err)
+        {
+            /* XXX: Warning: Download failed */
+        }
+        else
+        {
+            int err;
+            err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
+            if (err == 0)
+            {
+                /* XXX: Notice: Signature check passed */
+            }
+            else
+            {
+                /* XXX: Warning: Signature check failed */
+            }
+        }
+        free (sig_file_name);
+        free (list_file_name);
+        free (url);
     }
-    free (sig_file_name);
-    free (list_file_name);
-    free (url);
 #else
     /* XXX: Note: Signature check for %s skipped because GPG support was not
      * enabled in this build
@@ -921,7 +928,7 @@ opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_d
 
     pkg = all->pkgs[i];
 
-    package = pkg_clone (pkg);
+    package = pkg_t_to_opkg_package_t (pkg);
     callback (opkg, package, user_data);
     opkg_package_free (package);
   }
@@ -934,8 +941,11 @@ opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_d
 int
 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
 {
-    pkg_vec_t *all;
-    int i;
+    struct active_list *head;
+    struct active_list *node;
+    pkg_t *old=NULL, *new = NULL;
+    static opkg_package_t* package=NULL;
+
 
     opkg_assert (opkg);
     opkg_assert (callback);
@@ -943,23 +953,15 @@ opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, v
     /* ensure all data is valid */
     pkg_info_preinstall_check (opkg->conf);
 
-    all = opkg_upgrade_all_list_get (opkg->conf);
-    for (i = 0; i < all->len; i++)
-    {
-        pkg_t *old, *new;
-        opkg_package_t *package;
-
-        old = all->pkgs[i];
-
+    head  =  prepare_upgrade_list(opkg->conf);
+    for (node=active_list_next(head, head); node; active_list_next(head,node)) {
+        old = list_entry(node, pkg_t, list);
         new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
-
-        package = pkg_clone (new);
+        package = pkg_t_to_opkg_package_t (new);
         callback (opkg, package, user_data);
         opkg_package_free (package);
     }
-
-    pkg_vec_free (all);
-
+    active_list_head_delete(head);
     return 0;
 }
 
@@ -1010,7 +1012,7 @@ opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *
     }
 
     /* match found */
-    package = pkg_clone (pkg);
+    package = pkg_t_to_opkg_package_t (pkg);
     break;
   }
 
@@ -1019,7 +1021,9 @@ opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *
   return package;
 }
 
+#ifdef HAVE_CURL
 #include <curl/curl.h>
+#endif
 /**
  * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status. 
  * @param opkg The opkg_t
@@ -1039,17 +1043,17 @@ int opkg_repository_accessibility_check(opkg_t *opkg)
 
   src = str_list_alloc();
 
-  for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next) 
+  list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
   {
-    if (strstr(iter->data->value, "://") && 
-                   index(strstr(iter->data->value, "://") + 3, '/')) 
-      stmp = strndup(iter->data->value, 
-                     (index(strstr(iter->data->value, "://") + 3, '/') - iter->data->value)*sizeof(char));
+    if (strstr(((pkg_src_t *)iter->data)->value, "://") && 
+                   index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/')) 
+      stmp = strndup(((pkg_src_t *)iter->data)->value, 
+                     (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
 
     else
-      stmp = strdup(iter->data->value);
+      stmp = strdup(((pkg_src_t *)iter->data)->value);
 
-    for (iter1 = src->head; iter1; iter1 = iter1->next)
+    for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
     {
       if (strstr(iter1->data, stmp)) 
         break;
@@ -1061,6 +1065,7 @@ int opkg_repository_accessibility_check(opkg_t *opkg)
     free(stmp);
 
     str_list_append(src, repo_ptr);
+    free(repo_ptr);
     repositories++;
   }
   while (repositories > 0) 
@@ -1069,16 +1074,19 @@ int opkg_repository_accessibility_check(opkg_t *opkg)
     repositories--;
 
     err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
+#ifdef HAVE_CURL
     if (!(err == CURLE_OK || 
                err == CURLE_HTTP_RETURNED_ERROR || 
                err == CURLE_FILE_COULDNT_READ_FILE ||
                err == CURLE_REMOTE_FILE_NOT_FOUND || 
                err == CURLE_TFTP_NOTFOUND
                )) {
+#else
+    if (!(err == 0)) {
+#endif
            ret++;
     }
     str_list_elt_deinit(iter1);
-    free(iter1);
   }
   free(src);
   return ret;