[PATCH 1 of 3] Fix instruction writing for Windows

Wald Commits scm-commit at wald.intevation.org
Thu Apr 24 17:47:40 CEST 2014


# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1398354395 0
# Node ID a9da8e4eeff7d1ce2a2a5298a89c8c5262ddbd99
# Parent  b8b0f9685ffa3c9b171cb48355eefc3a07c4ac53
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.

diff -r b8b0f9685ffa -r a9da8e4eeff7 cinst/mozilla.c
--- 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;
diff -r b8b0f9685ffa -r a9da8e4eeff7 cinst/nssstore_win.c
--- 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);
 


More information about the Trustbridge-commits mailing list