diff cinst/mozilla.c @ 279:cb5f082e90c5

Factor out the iteration over profiles and certs.
author Sascha Wilde <wilde@intevation.de>
date Wed, 02 Apr 2014 13:46:19 +0200
parents 539c856cb5da
children 6c4b3ff4a356
line wrap: on
line diff
--- a/cinst/mozilla.c	Wed Apr 02 13:17:04 2014 +0200
+++ b/cinst/mozilla.c	Wed Apr 02 13:46:19 2014 +0200
@@ -472,6 +472,47 @@
 }
 
 /**
+ * @brief Apply a function to a list of certificates and profiles
+ *
+ * The function must have the signature:
+ *
+ *   bool function(char *pdir, SECItem der_cert)
+ *
+ * where pdir is the path of an profile and der_cert is an raw DER
+ * formatted certificate.  The function must return true on success
+ * and false on failure.
+ *
+ * This function is intended wor use with the import_cert and
+ * remove_cert functions.
+ *
+ * @param[in] fn the function to apply
+ * @param[inout] certs a secitem list holding the certificates
+ * the list will be change (emptied)!
+ * @param[in] pdirs the NULL terminated list of profile directories
+ * @returns true on success and false on failure
+ */
+bool
+apply_to_certs_and_profiles(bool fn(char *, SECItem *),
+                            seciteml_t **certs, char **pdirs)
+{
+  SECItem *cert;
+  bool success = true;
+  
+  while ((cert = seciteml_pop(certs)) != NULL)
+    {
+      for (int i=0; pdirs[i] != NULL; i++)
+        {
+          if (! (*fn)(pdirs[i], cert))
+            success = false;
+        }
+      free(cert->data);
+      free(cert);
+    }
+  
+  return success;
+}
+
+/**
  * @brief Parse IPC commands from standard input.
  *
  * Reads command lines (R: and I:) from standard input and puts the
@@ -540,7 +581,6 @@
   char **pdirs;
   seciteml_t *certs_to_remove = NULL;
   seciteml_t *certs_to_add = NULL;
-  SECItem *secitemp;
 
   pdirs =
     get_all_profile_dirs();
@@ -549,33 +589,20 @@
     {
       parse_commands(&certs_to_add, &certs_to_remove);
 
-      while ((secitemp = seciteml_pop(&certs_to_remove)) != NULL)
-        {
-          for (int i=0; pdirs[i] != NULL; i++)
-            {
-              puts(pdirs[i]);
-              if (! remove_cert(pdirs[i], secitemp))
-                return_code |= WARN_MOZ_COULD_NOT_REMOVE_CERT;
-              puts("List of installed certs:");
-              nss_list_certs(pdirs[i]);
-            }
-          free(secitemp->data);
-          free(secitemp);
-        }
+      puts("OLD List of installed certs:");
+      for (int i=0; pdirs[i] != NULL; i++)
+        nss_list_certs(pdirs[i]);
 
-      while ((secitemp = seciteml_pop(&certs_to_add)) != NULL)
-        {
-          for (int i=0; pdirs[i] != NULL; i++)
-            {
-              puts(pdirs[i]);
-              if (! import_cert(pdirs[i], secitemp))
-                return_code |= WARN_MOZ_COULD_NOT_ADD_CERT;
-              nss_list_certs(pdirs[i]);
-            }
-          free(secitemp->data);
-          free(secitemp);
-        }
-
+      if (! apply_to_certs_and_profiles(remove_cert, &certs_to_remove, pdirs))
+        return_code |= WARN_MOZ_COULD_NOT_REMOVE_CERT;
+      
+      if (! apply_to_certs_and_profiles(import_cert, &certs_to_add, pdirs))
+        return_code |= WARN_MOZ_COULD_NOT_ADD_CERT;
+      
+      puts("NEW List of installed certs:");
+      for (int i=0; pdirs[i] != NULL; i++)
+        nss_list_certs(pdirs[i]);
+      
       strv_free(pdirs);
     }
   exit(return_code);

http://wald.intevation.org/projects/trustbridge/