annotate cinst/windowsstore.c @ 306:9c5e6b142611

Set the home directory to a temporary location to avoid writing into real nss stores
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 03 Apr 2014 15:30:25 +0200
parents 759a898990d1
children 824ef90a6721
rev   line source
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
1 #ifdef WIN32
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
2
161
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
3 #include <stdio.h>
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
4
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
5 #include "windowsstore.h"
161
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
6 #include "errorcodes.h"
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
7 #include "listutil.h"
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
8 #include "strhelp.h"
253
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
9 #include "logging.h"
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
10
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
11 static PCCERT_CONTEXT
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
12 b64_to_cert_context(char *b64_data, size_t b64_size)
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
13 {
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
14 size_t buf_size = 0;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
15 char *buf = NULL;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
16 PCCERT_CONTEXT pCert = NULL;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
17 int ret = -1;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
18
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
19 ret = str_base64_decode (&buf, &buf_size, b64_data, b64_size);
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
20
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
21 if (ret != 0)
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
22 {
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
23 printf ("decoding certificate failed\n");
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
24 return NULL;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
25 }
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
26
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
27 pCert = CertCreateContext (CERT_STORE_CERTIFICATE_CONTEXT,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
28 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
29 (const PBYTE) buf,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
30 (DWORD) buf_size,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
31 0,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
32 NULL);
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
33 free (buf); /* Windows has a copy */
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
34
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
35 if (pCert == NULL)
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
36 {
253
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
37 char *error = getLastErrorMsg();
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
38 if (error)
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
39 {
253
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
40 printf ("Failed to create cert context: %s \n", error);
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
41 free (error);
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
42 }
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
43 return NULL;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
44 }
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
45 return pCert;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
46 }
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
47
219
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
48 void
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
49 do_remove(HCERTSTORE hStore, char **to_remove)
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
50 {
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
51 PCCERT_CONTEXT pCert = NULL;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
52 unsigned int i = 0;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
53
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
54 if (!to_remove)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
55 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
56 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
57 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
58
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
59 for (i=0; to_remove[i]; i++)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
60 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
61 PCCERT_CONTEXT pc_to_remove = NULL;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
62
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
63 pc_to_remove = b64_to_cert_context(to_remove[i],
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
64 strnlen(to_remove[i], MAX_LINE_LENGTH));
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
65
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
66 if (pc_to_remove == NULL)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
67 {
253
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
68 char *error = getLastErrorMsg();
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
69 if (error)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
70 {
253
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
71 printf ("Failed to create cert context: %s \n", error);
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
72 free (error);
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
73 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
74 continue;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
75 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
76
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
77 pCert = CertFindCertificateInStore (hStore,
218
8fb12af98960 According to MSDN you Must also provide message encoding flag.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 217
diff changeset
78 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
79 0,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
80 CERT_FIND_EXISTING,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
81 pc_to_remove,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
82 NULL);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
83
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
84 CertFreeCertificateContext (pc_to_remove);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
85
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
86 if (pCert == NULL)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
87 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
88 printf ("Did not find certificate\n");
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
89 continue;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
90 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
91
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
92 if (!CertDeleteCertificateFromStore (pCert))
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
93 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
94 /* From MSDN:
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
95 The CertDeleteCertificateFromStore function always frees
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
96 pCertContext by calling the CertFreeCertificateContext
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
97 function, even if an error is encountered. */
253
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
98 char *error = getLastErrorMsg();
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
99 printf ("Error deleting certificate. %s", error);
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
100 free (error);
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
101 continue;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
102 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
103 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
104 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
105 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
106
219
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
107 void
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
108 do_install(HCERTSTORE hStore, char **to_install)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
109 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
110 int i = 0,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
111 ret = -1;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
112
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
113 if (!to_install)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
114 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
115 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
116 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
117
217
83a015f2e078 Remove leftover incrementation of i from former loop style. Fixes a crash when adding only one
Andre Heinecke <andre.heinecke@intevation.de>
parents: 215
diff changeset
118 for (i = 0; to_install[i]; i++)
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
119 {
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
120 PCCERT_CONTEXT pc_to_add = NULL;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
121 PCCERT_CONTEXT found_cert = NULL;
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
122
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
123 pc_to_add = b64_to_cert_context(to_install[i],
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
124 strnlen(to_install[i], MAX_LINE_LENGTH));
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
125
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
126 if (pc_to_add == NULL)
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
127 {
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
128 continue;
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
129 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
130
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
131 found_cert = CertFindCertificateInStore (hStore,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
132 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
133 0,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
134 CERT_FIND_EXISTING,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
135 pc_to_add,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
136 NULL);
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
137 if (found_cert != NULL)
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
138 {
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
139 printf ("Certificate already in store\n");
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
140 CertFreeCertificateContext (found_cert);
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
141 CertFreeCertificateContext (pc_to_add);
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
142 continue;
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
143 }
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
144
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
145 ret = CertAddCertificateContextToStore (hStore,
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
146 pc_to_add,
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
147 CERT_STORE_ADD_ALWAYS,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
148 NULL);
222
53ea9b975d1c Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents: 219
diff changeset
149 CertFreeCertificateContext (pc_to_add);
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
150 if (!ret)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
151 {
253
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
152 char *error = getLastErrorMsg();
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
153 if (error)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
154 {
253
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
155 printf ("Failed to add certificate: %s \n", error);
3595ea4fd3fb Use getLastErrorMsg from logging
Andre Heinecke <aheinecke@intevation.de>
parents: 247
diff changeset
156 free (error);
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
157 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
158 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
159 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
160 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
161 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
162
247
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
163 static bool is_elevated() {
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
164 HANDLE hToken = NULL;
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
165 bool ret = false;
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
166 if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &hToken))
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
167 {
262
759a898990d1 Use DWORD value directly to dertermine elevation
Andre Heinecke <aheinecke@intevation.de>
parents: 253
diff changeset
168 DWORD elevation;
759a898990d1 Use DWORD value directly to dertermine elevation
Andre Heinecke <aheinecke@intevation.de>
parents: 253
diff changeset
169 DWORD cbSize = sizeof (DWORD);
247
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
170 if (GetTokenInformation (hToken, TokenElevation, &elevation,
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
171 sizeof (TokenElevation), &cbSize))
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
172 {
262
759a898990d1 Use DWORD value directly to dertermine elevation
Andre Heinecke <aheinecke@intevation.de>
parents: 253
diff changeset
173 ret = elevation;
247
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
174 }
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
175 }
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
176 if (hToken)
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
177 CloseHandle (hToken);
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
178
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
179 return ret;
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
180 }
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
181
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
182 int
247
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
183 write_stores_win (char **to_install, char **to_remove)
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
184 {
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
185 HCERTSTORE hStore = NULL;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
186
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
187 if (!to_install && !to_remove)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
188 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
189 /* Nothing to do */
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
190 return 0;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
191 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
192
247
4de97f74d038 Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents: 222
diff changeset
193 if (!is_elevated())
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
194 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
195 hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
196 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root");
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
197 }
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
198 else
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
199 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
200 hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
201 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root");
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
202 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
203
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
204 if (!hStore)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
205 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
206 return ERR_STORE_ACCESS_DENIED;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
207 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
208
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
209 /* Do the actual work */
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
210 do_install (hStore, to_install);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
211
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
212 do_remove (hStore, to_remove);
161
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
213
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
214 if (hStore)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
215 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
216 CertCloseStore (hStore, 0);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
217 }
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
218 return 0;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
219 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
220 #endif // WIN32

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