diff cinst/mozilla.c @ 978:d92b1594e974

Merged.
author Emanuel Schuetze <emanuel@intevation.de>
date Fri, 29 Aug 2014 13:10:11 +0200
parents b3695a3399de
children 1743895b39b8
line wrap: on
line diff
--- a/cinst/mozilla.c	Fri Aug 29 13:09:40 2014 +0200
+++ b/cinst/mozilla.c	Fri Aug 29 13:10:11 2014 +0200
@@ -26,6 +26,10 @@
  * This tool tries to find all NSS databases the user has
  * access to and to execute the instructions on all of them.
  *
+ * If the tool is executed with a UID of 0 or with admin privileges under
+ * windows it will not look into the user directories but instead try
+ * to write the system wide defaults.
+ *
  * If there are other processes accessing the databases the caller
  * has to ensure that those are terminated before this process is
  * executed.
@@ -44,7 +48,7 @@
  */
 
 /**
- * @brief Needs to eb defined to get strnlen()
+ * @brief Needs to be defined to get strnlen()
  */
 #define _POSIX_C_SOURCE 200809L
 
@@ -71,12 +75,17 @@
 #include "portpath.h"
 #include "strhelp.h"
 #include "nss-secitemlist.h"
+#include "util.h"
 
 #ifndef _WIN32
 #define CONFDIRS ".mozilla", ".thunderbird"
+/* Default installation directory of ubuntu 14.4 is respected */
+#define MOZILLA_DEFAULTS "/etc/thunderbird", "/etc/firefox"
 #define NSSSHARED ".pki/nssdb"
+#define NSSSHARED_GLOBAL "/etc/pki/nssdb"
 #define TARGET_LINUX 1
 #else
+#define MOZILLA_DEFAULTS 0
 #define CONFDIRS "Mozilla", "Thunderbird"
 #define NSSSHARED ""
 #define TARGET_LINUX 0
@@ -199,7 +208,7 @@
                   if (relative_path)
                     xasprintf(&path, "%s/%s", inifile_dirname, value);
                   else
-                    xasprintf(&path, "%s", value); /* FIXME: LOOKS STUPID! */
+                    xasprintf(&path, "%s", value);
                   if ((fqpath = port_realpath(path)) != NULL)
                     {
                       DEBUGPRINTF("Found profile path: '%s'\n", fqpath);
@@ -294,6 +303,71 @@
 }
 
 /**
+ * @brief Collect the default profile directories for mozilla software
+ *
+ * If the default directory is found but not the profiles subdirectory
+ * this will create the profiles subdirectory.
+ *
+ * @return NULL terminated array of strings containing the absolute path
+ * to the default profile directories. Needs to be freed by the caller.
+ */
+static char**
+get_default_profile_dirs()
+{
+  char **retval = NULL;
+
+  const char *confdirs[] = { MOZILLA_DEFAULTS, NULL };
+
+  for (int i=0; confdirs[i] != NULL; i++)
+    {
+      char * realpath = port_realpath(confdirs[i]);
+      char * profile_dir = NULL;
+      if (realpath == NULL)
+        {
+          DEBUGPRINTF ("Did not find directory: '%s'\n", confdirs[i]);
+          continue;
+        }
+      xasprintf(&profile_dir, "%s/profile", realpath);
+      if (port_isdir(profile_dir))
+        {
+          DEBUGPRINTF("Found default directory: '%s'\n", profile_dir);
+          /* All is well */
+          strv_append (&retval, profile_dir, strlen(profile_dir));
+          xfree(profile_dir);
+          profile_dir = NULL;
+          continue;
+        }
+      else
+        {
+          /* Create the directory */
+          if (port_fileexits(profile_dir))
+            {
+              DEBUGPRINTF ("Path: '%s' is not a directory but it exists. Skipping.\n",
+                           profile_dir);
+              xfree(profile_dir);
+              profile_dir = NULL;
+              continue;
+            }
+          else
+            {
+              /* Lets create it */
+              if (!port_mkdir(profile_dir))
+                {
+                  ERRORPRINTF ("Failed to create directory: '%s'\n", profile_dir);
+                  xfree(profile_dir);
+                  profile_dir = NULL;
+                  continue;
+                }
+              strv_append (&retval, profile_dir, strlen(profile_dir));
+              xfree(profile_dir);
+              profile_dir = NULL;
+            }
+        }
+    }
+  return retval;
+}
+
+/**
  * @brief Collect all mozilla profile directories of current user.
  * @return NULL terminated array of strings containing the absolute
  * path of the profile directories.  The array needs to be freed by the
@@ -304,6 +378,24 @@
 {
   char **mozinis, **pdirs;
   char **alldirs = NULL;
+
+  if (is_elevated())
+    {
+#ifndef _WIN32
+      /* NSS Shared db does not exist under windows. */
+      strv_append(&alldirs, NSSSHARED_GLOBAL, strlen(NSSSHARED_GLOBAL));
+#endif
+      pdirs = get_default_profile_dirs();
+      if (pdirs != NULL)
+        {
+          for (int i=0; pdirs[i] != NULL; i++)
+            {
+              strv_append(&alldirs, pdirs[i], strlen(pdirs[i]));
+            }
+          strv_free(pdirs);
+        }
+      return alldirs;
+    }
   /* Search Mozilla/Firefox/Thunderbird profiles */
   if ((mozinis = get_profile_inis()) != NULL)
     {
@@ -363,6 +455,9 @@
 
           DEBUGPRINTF("Found certificate \"%s\"\n", name);
         }
+      /* According to valgrind this leaks memory in the list.
+         We could not find API documentation to better free this
+         so we accept the leakage here in case of debug. */
       CERT_DestroyCertList(list);
       NSS_Shutdown();
     }

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