diff cinst/windowsstore.c @ 1309:32a44cfb78c0

Improve debug output and fix cert context leak on removal
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 13 Oct 2014 13:29:19 +0200
parents 265583011f24
children 18211dce3106
line wrap: on
line diff
--- a/cinst/windowsstore.c	Mon Oct 13 13:25:36 2014 +0200
+++ b/cinst/windowsstore.c	Mon Oct 13 13:29:19 2014 +0200
@@ -18,6 +18,26 @@
 #include "certhelp.h"
 
 void
+debug_print_subject(PCCERT_CONTEXT pCert)
+{
+  char pszNameString[256];
+  if (!pCert || !g_debug)
+    {
+      return;
+    }
+
+  if(CertGetNameString(pCert,
+                       CERT_NAME_SIMPLE_DISPLAY_TYPE,
+                       0,
+                       NULL,
+                       pszNameString,
+                       128))
+    {
+      DEBUGPRINTF("Certificate Subject: %s", pszNameString);
+    }
+}
+
+void
 do_remove(HCERTSTORE hStore, char **to_remove)
 {
   PCCERT_CONTEXT pCert = NULL;
@@ -54,14 +74,16 @@
                                           pc_to_remove,
                                           NULL);
 
-      CertFreeCertificateContext (pc_to_remove);
-
       if (pCert == NULL)
         {
-          ERRORPRINTF ("Did not find certificate\n");
+          DEBUGPRINTF ("Did not find certificate\n");
+          debug_print_subject (pc_to_remove);
+          CertFreeCertificateContext (pc_to_remove);
           continue;
         }
 
+      CertFreeCertificateContext (pc_to_remove);
+
       if (!CertDeleteCertificateFromStore (pCert))
         {
           /* From MSDN:
@@ -70,11 +92,14 @@
              function, even if an error is encountered. */
           char *error = getLastErrorMsg();
           ERRORPRINTF ("Error deleting certificate. %s", error);
+          debug_print_subject (pCert);
+          CertFreeCertificateContext (pCert);
           free (error);
           continue;
         }
       log_certificate (elevated ? "Local Machine" : "Current User",
                        to_remove[i], false);
+      CertFreeCertificateContext (pCert);
     }
   return;
 }
@@ -112,7 +137,7 @@
                    NULL);
       if (found_cert != NULL)
         {
-          DEBUGPRINTF ("Certificate already in store\n");
+          /* DEBUGPRINTF ("Certificate already in store\n"); */
           CertFreeCertificateContext (found_cert);
           CertFreeCertificateContext (pc_to_add);
           continue;
@@ -122,16 +147,17 @@
                                               pc_to_add,
                                               CERT_STORE_ADD_ALWAYS,
                                               NULL);
-      CertFreeCertificateContext (pc_to_add);
       if (!ret)
         {
           char *error = getLastErrorMsg();
           if (error)
             {
               ERRORPRINTF ("Failed to add certificate: %s \n", error);
+              debug_print_subject (pc_to_add);
               free (error);
             }
         }
+      CertFreeCertificateContext (pc_to_add);
       log_certificate (elevated ? "Local Machine" : "Current User",
                        to_install[i], true);
     }

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