comparison 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
comparison
equal deleted inserted replaced
1308:fce8c122cbc4 1309:32a44cfb78c0
16 #include "logging.h" 16 #include "logging.h"
17 #include "util.h" 17 #include "util.h"
18 #include "certhelp.h" 18 #include "certhelp.h"
19 19
20 void 20 void
21 debug_print_subject(PCCERT_CONTEXT pCert)
22 {
23 char pszNameString[256];
24 if (!pCert || !g_debug)
25 {
26 return;
27 }
28
29 if(CertGetNameString(pCert,
30 CERT_NAME_SIMPLE_DISPLAY_TYPE,
31 0,
32 NULL,
33 pszNameString,
34 128))
35 {
36 DEBUGPRINTF("Certificate Subject: %s", pszNameString);
37 }
38 }
39
40 void
21 do_remove(HCERTSTORE hStore, char **to_remove) 41 do_remove(HCERTSTORE hStore, char **to_remove)
22 { 42 {
23 PCCERT_CONTEXT pCert = NULL; 43 PCCERT_CONTEXT pCert = NULL;
24 unsigned int i = 0; 44 unsigned int i = 0;
25 bool elevated = is_elevated(); 45 bool elevated = is_elevated();
52 0, 72 0,
53 CERT_FIND_EXISTING, 73 CERT_FIND_EXISTING,
54 pc_to_remove, 74 pc_to_remove,
55 NULL); 75 NULL);
56 76
77 if (pCert == NULL)
78 {
79 DEBUGPRINTF ("Did not find certificate\n");
80 debug_print_subject (pc_to_remove);
81 CertFreeCertificateContext (pc_to_remove);
82 continue;
83 }
84
57 CertFreeCertificateContext (pc_to_remove); 85 CertFreeCertificateContext (pc_to_remove);
58
59 if (pCert == NULL)
60 {
61 ERRORPRINTF ("Did not find certificate\n");
62 continue;
63 }
64 86
65 if (!CertDeleteCertificateFromStore (pCert)) 87 if (!CertDeleteCertificateFromStore (pCert))
66 { 88 {
67 /* From MSDN: 89 /* From MSDN:
68 The CertDeleteCertificateFromStore function always frees 90 The CertDeleteCertificateFromStore function always frees
69 pCertContext by calling the CertFreeCertificateContext 91 pCertContext by calling the CertFreeCertificateContext
70 function, even if an error is encountered. */ 92 function, even if an error is encountered. */
71 char *error = getLastErrorMsg(); 93 char *error = getLastErrorMsg();
72 ERRORPRINTF ("Error deleting certificate. %s", error); 94 ERRORPRINTF ("Error deleting certificate. %s", error);
95 debug_print_subject (pCert);
96 CertFreeCertificateContext (pCert);
73 free (error); 97 free (error);
74 continue; 98 continue;
75 } 99 }
76 log_certificate (elevated ? "Local Machine" : "Current User", 100 log_certificate (elevated ? "Local Machine" : "Current User",
77 to_remove[i], false); 101 to_remove[i], false);
102 CertFreeCertificateContext (pCert);
78 } 103 }
79 return; 104 return;
80 } 105 }
81 106
82 void 107 void
110 CERT_FIND_EXISTING, 135 CERT_FIND_EXISTING,
111 pc_to_add, 136 pc_to_add,
112 NULL); 137 NULL);
113 if (found_cert != NULL) 138 if (found_cert != NULL)
114 { 139 {
115 DEBUGPRINTF ("Certificate already in store\n"); 140 /* DEBUGPRINTF ("Certificate already in store\n"); */
116 CertFreeCertificateContext (found_cert); 141 CertFreeCertificateContext (found_cert);
117 CertFreeCertificateContext (pc_to_add); 142 CertFreeCertificateContext (pc_to_add);
118 continue; 143 continue;
119 } 144 }
120 145
121 ret = CertAddCertificateContextToStore (hStore, 146 ret = CertAddCertificateContextToStore (hStore,
122 pc_to_add, 147 pc_to_add,
123 CERT_STORE_ADD_ALWAYS, 148 CERT_STORE_ADD_ALWAYS,
124 NULL); 149 NULL);
125 CertFreeCertificateContext (pc_to_add);
126 if (!ret) 150 if (!ret)
127 { 151 {
128 char *error = getLastErrorMsg(); 152 char *error = getLastErrorMsg();
129 if (error) 153 if (error)
130 { 154 {
131 ERRORPRINTF ("Failed to add certificate: %s \n", error); 155 ERRORPRINTF ("Failed to add certificate: %s \n", error);
156 debug_print_subject (pc_to_add);
132 free (error); 157 free (error);
133 } 158 }
134 } 159 }
160 CertFreeCertificateContext (pc_to_add);
135 log_certificate (elevated ? "Local Machine" : "Current User", 161 log_certificate (elevated ? "Local Machine" : "Current User",
136 to_install[i], true); 162 to_install[i], true);
137 } 163 }
138 return; 164 return;
139 } 165 }

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