comparison cinst/windowsstore.c @ 163:8cfcd38a9bb3

Change coding style for cinst main / windowsstore to GNU The kdelibs / qt coding style is not really suited for plain C and looks irritiating when combined with standard C functions.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 25 Mar 2014 10:29:12 +0000
parents a4b1c77f3e6a
children ee37c085b9f7
comparison
equal deleted inserted replaced
162:4a4b5e640d1a 163:8cfcd38a9bb3
5 #include "windowsstore.h" 5 #include "windowsstore.h"
6 #include "errorcodes.h" 6 #include "errorcodes.h"
7 #include "listutil.h" 7 #include "listutil.h"
8 #include "strhelp.h" 8 #include "strhelp.h"
9 9
10 static LPWSTR getLastErrorMsg() { 10 static LPWSTR getLastErrorMsg()
11 LPWSTR bufPtr = NULL; 11 {
12 DWORD err = GetLastError(); 12 LPWSTR bufPtr = NULL;
13 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | 13 DWORD err = GetLastError();
14 FORMAT_MESSAGE_FROM_SYSTEM | 14 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
15 FORMAT_MESSAGE_IGNORE_INSERTS, 15 FORMAT_MESSAGE_FROM_SYSTEM |
16 NULL, err, 0, (LPWSTR)&bufPtr, 0, NULL); 16 FORMAT_MESSAGE_IGNORE_INSERTS,
17 if (!bufPtr) { 17 NULL, err, 0, (LPWSTR) &bufPtr, 0, NULL);
18 HMODULE hWinhttp = GetModuleHandleW(L"winhttp"); 18 if (!bufPtr)
19 if (hWinhttp) { 19 {
20 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | 20 HMODULE hWinhttp = GetModuleHandleW (L"winhttp");
21 FORMAT_MESSAGE_FROM_HMODULE | 21 if (hWinhttp)
22 FORMAT_MESSAGE_IGNORE_INSERTS, 22 {
23 hWinhttp, HRESULT_CODE(err), 0, 23 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
24 (LPWSTR)&bufPtr, 0, NULL); 24 FORMAT_MESSAGE_FROM_HMODULE |
25 FORMAT_MESSAGE_IGNORE_INSERTS,
26 hWinhttp, HRESULT_CODE (err), 0,
27 (LPWSTR) &bufPtr, 0, NULL);
25 } 28 }
26 } 29 }
27 if (!bufPtr) 30 if (!bufPtr)
28 printf("Error getting last error\n"); 31 printf ("Error getting last error\n");
29 return bufPtr; 32 return bufPtr;
30 } 33 }
31 34
32 int write_stores_win(char **to_install, char **to_remove, bool user_store) 35 int write_stores_win (char **to_install, char **to_remove, bool user_store)
33 { 36 {
34 int i = 0; 37 int i = 0;
35 int ret = -1; 38 int ret = -1;
36 HCERTSTORE hStore = NULL; 39 HCERTSTORE hStore = NULL;
37 40
38 if (user_store) { 41 if (user_store)
39 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 42 {
40 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root"); 43 hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0,
41 } else { 44 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root");
42 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 45 }
43 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root"); 46 else
47 {
48 hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0,
49 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root");
44 } 50 }
45 51
46 if (!hStore) { 52 if (!hStore)
47 return ERR_STORE_ACCESS_DENIED; 53 {
54 return ERR_STORE_ACCESS_DENIED;
48 } 55 }
49 56
50 for (i=0; to_install[i]; i++) { 57 for (i=0; to_install[i]; i++)
51 size_t cert_len = strnlen(to_install[i], MAX_LINE_LENGTH), 58 {
52 buf_size = 0; 59 size_t cert_len = strnlen (to_install[i], MAX_LINE_LENGTH),
53 char *buf = NULL; 60 buf_size = 0;
61 char *buf = NULL;
54 62
55 ret = str_base64_decode(&buf, &buf_size, to_install[i], cert_len); 63 ret = str_base64_decode (&buf, &buf_size, to_install[i], cert_len);
56 64
57 if (ret != 0) { 65 if (ret != 0)
58 return ERR_INVALID_INSTRUCTIONS; 66 {
67 return ERR_INVALID_INSTRUCTIONS;
59 } 68 }
60 69
61 ret = CertAddEncodedCertificateToStore (hStore, 70 ret = CertAddEncodedCertificateToStore (hStore,
62 X509_ASN_ENCODING, 71 X509_ASN_ENCODING,
63 (PBYTE)buf, 72 (PBYTE) buf,
64 buf_size, 73 buf_size,
65 CERT_STORE_ADD_ALWAYS, 74 CERT_STORE_ADD_ALWAYS,
66 NULL); 75 NULL);
67 76
68 if (ret == 0) { 77 if (ret == 0)
69 LPWSTR error = getLastErrorMsg(); 78 {
70 if (error) { 79 LPWSTR error = getLastErrorMsg();
71 printf("Failed to add certificate: %S \n", error); 80 if (error)
72 LocalFree(error); 81 {
82 printf ("Failed to add certificate: %S \n", error);
83 LocalFree (error);
73 } 84 }
74 } 85 }
75 i++; 86 i++;
76 free(buf); 87 free (buf);
77 } 88 }
78 89
79 for (i=0; to_remove[i]; i++) { 90 for (i=0; to_remove[i]; i++)
80 // TODO 91 {
92 // TODO
81 } 93 }
82 94
83 if(hStore) { 95 if (hStore)
84 CertCloseStore(hStore, 0); 96 {
97 CertCloseStore (hStore, 0);
85 } 98 }
86 return 0; 99 return 0;
87 } 100 }
88 #endif // WIN32 101 #endif // WIN32

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