changeset 489:a9da8e4eeff7

Fix instruction writing for Windows. Back to native winapi. What happens if you convert a Winapi handle to a c handle and how closing etc. works is to badly documented for me to be comfortable to use it.
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 24 Apr 2014 15:46:35 +0000
parents b8b0f9685ffa
children 2e78d08ff46e
files cinst/mozilla.c cinst/nssstore_win.c
diffstat 2 files changed, 45 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/cinst/mozilla.c	Thu Apr 24 14:56:13 2014 +0200
+++ b/cinst/mozilla.c	Thu Apr 24 15:46:35 2014 +0000
@@ -381,7 +381,6 @@
   if (!cn_str || !o_str)
     {
       DEBUGPRINTF("FATAL: Could not parse certificate!");
-      DEBUGPRINTF("data len: %u \n", secitemp->len);
       exit(ERR_INVALID_CERT);
     }
   name_len = strlen(cn_str) + strlen(o_str) + 4;
--- a/cinst/nssstore_win.c	Thu Apr 24 14:56:13 2014 +0200
+++ b/cinst/nssstore_win.c	Thu Apr 24 15:46:35 2014 +0000
@@ -75,8 +75,8 @@
 * Writes the null terminated list of instructions to
 * the handle.
 *
-* @param [in] base64 encoded der certificates to write
-* @param [in] write_handle to write to
+* @param [in] certificates base64 encoded der certificate to write
+* @param [in] write_handle handle to write to
 * @param [in] remove weather the certificate should be installed or removed
 *
 * @returns true on success, false on failure
@@ -85,38 +85,61 @@
 write_instructions(char **certificates, HANDLE write_handle,
                    bool remove)
 {
+  bool retval = false;
   int i = 0;
-  int cHandle = -1;
-  FILE *write_stream = NULL;
+  const char *line_end = "\r\n";
+  char *line_start = NULL;
 
   if (!certificates)
     {
       return true;
     }
 
-  cHandle = _open_osfhandle ((intptr_t)write_handle, 0);
+  line_start = remove ? "R:" : "I:";
 
-  if (cHandle == -1)
-    {
-      ERRORPRINTF ("Failed to open write handle.\n");
-    }
-
-  write_stream = _fdopen(cHandle, "w");
   for (i = 0; certificates[i]; i++)
     {
-      int ret = 0;
-      if (remove)
-        ret = fprintf (write_stream, "R:%s\n", certificates[i]);
-      else
-        ret = fprintf (write_stream, "I:%s\n", certificates[i]);
-
-      if (ret <= 0)
+      DWORD written = 0;
+      DWORD inst_len = strlen (certificates[i]);
+      retval = WriteFile (write_handle, (LPCVOID) line_start, 2, &written, NULL);
+      if (!retval)
         {
-          DEBUGPRINTF ("Failed to write everything.\n");
-          break;
+          PRINTLASTERROR ("Failed to write line start\n");
+          return false;
+        }
+      if (written != 2)
+        {
+          ERRORPRINTF ("Failed to write line start\n");
+          retval = false;
+          return false;
+        }
+      written = 0;
+      retval = WriteFile (write_handle, (LPCVOID) certificates[i], inst_len, &written, NULL);
+      if (!retval)
+        {
+          PRINTLASTERROR ("Failed to write certificate\n");
+          return false;
+        }
+      if (inst_len != written)
+        {
+          ERRORPRINTF ("Failed to write everything\n");
+          retval = false;
+          return false;
+        }
+      written = 0;
+      retval = WriteFile (write_handle, (LPCVOID) line_end, 2, &written, NULL);
+      if (!retval)
+        {
+          PRINTLASTERROR ("Failed to write line end\n");
+          return false;
+        }
+      if (written != 2)
+        {
+          ERRORPRINTF ("Failed to write full line end\n");
+          retval = false;
+          return false;
         }
     }
-
   return true;
 }
 
@@ -453,7 +476,7 @@
                       GENERIC_WRITE,
                       0, /* don't share */
                       NULL, /* use the security attributes from the folder */
-                      OPEN_ALWAYS,
+                      OPEN_ALWAYS | TRUNCATE_EXISTING,
                       0,
                       NULL);
 

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