diff cinst/main.c @ 132:4691d9e3b1d3

Merged
author Sascha Wilde <wilde@intevation.de>
date Mon, 24 Mar 2014 13:35:20 +0100
parents 898446d9d23e
children 4904fe01055d
line wrap: on
line diff
--- a/cinst/main.c	Mon Mar 24 13:32:12 2014 +0100
+++ b/cinst/main.c	Mon Mar 24 13:35:20 2014 +0100
@@ -40,6 +40,30 @@
 #include <wincrypt.h>
 #endif
 
+#ifdef WIN32
+LPWSTR getLastErrorMsg() {
+    LPWSTR bufPtr = NULL;
+    DWORD err = GetLastError();
+    FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                   FORMAT_MESSAGE_FROM_SYSTEM |
+                   FORMAT_MESSAGE_IGNORE_INSERTS,
+                   NULL, err, 0, (LPWSTR)&bufPtr, 0, NULL);
+    if (!bufPtr) {
+        HMODULE hWinhttp = GetModuleHandleW(L"winhttp");
+        if (hWinhttp) {
+            FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                           FORMAT_MESSAGE_FROM_HMODULE |
+                           FORMAT_MESSAGE_IGNORE_INSERTS,
+                           hWinhttp, HRESULT_CODE(err), 0,
+                           (LPWSTR)&bufPtr, 0, NULL);
+        }
+    }
+    if (!bufPtr)
+        printf("Error getting last error\n");
+    return bufPtr;
+}
+#endif
+
 /* The certificate list + instructions may only be so long as
  * twice the accepted certificatelist size */
 #define MAX_INPUT_SIZE MAX_LINE_LENGTH * MAX_LINES * 2
@@ -61,7 +85,7 @@
     int lines_read = 0;
     int readingList = 0;
     size_t list_size = 0;
-    char buf[MAX_LINE_LENGTH + 1];
+    char buf[MAX_LINE_LENGTH + 2];
 
     if (*certificate_list || *to_install || *to_remove) {
         printf("Error invalid parameters\n");
@@ -70,14 +94,26 @@
 
     while (fgets(buf, MAX_LINE_LENGTH + 1, stdin)) {
         size_t len = strlen(buf); /* fgets ensures buf is terminated */
-        if (len < 2) {
-            printf("Line to short.\n");
+        if (len <= 3) {
+            printf("Line too short.\n");
             return ERR_INVALID_INPUT;
         }
         if (lines_read ++ > MAX_LINES) {
             printf("Too many lines\n");
             return ERR_TOO_MUCH_INPUT;
         }
+
+        if (buf[len-2] != '\r') {
+            if (buf[len-1] != '\n') {
+                printf("Line too long.\n");
+                return ERR_INVALID_INPUT;
+            }
+            buf[len-1] = '\r';
+            buf[len] = '\n';
+            buf[len+1] = '\0';
+            len++;
+        }
+
         if (strcmp("-----BEGIN CERTIFICATE LIST-----\r\n", buf) == 0){
             readingList = 1;
             continue;
@@ -128,7 +164,7 @@
 /** @brief Install certificates into Windows store
  *
  * @param [in] to_install NULL terminated array of base64 encoded certificates.
- * @param [in] user_store set to True if the certificates shoudl be installed
+ * @param [in] user_store set to True if the certificates should be installed
  *             only for the current user. O for system wide installation.
  * @returns 0 on success an errorcode otherwise.
  */
@@ -137,7 +173,7 @@
     int i = 0;
     HCERTSTORE hStore = NULL;
 
-    if (!user_store) {
+    if (user_store) {
         // Access user store
         hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0,
                                0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root");
@@ -156,6 +192,7 @@
         size_t cert_len = strnlen(to_install[i], MAX_LINE_LENGTH);
         int ret = -1;
         unsigned char *buf;
+
         /* Check the needed size for the buffer */
         ret = base64_decode(NULL, &needed_len,
                             (unsigned char *)to_install[i], cert_len);
@@ -174,18 +211,21 @@
             return ERR_INVALID_INSTRUCTIONS;
         }
 
-        ret = CertAddEncodedCTLToStore (hStore,
-                                        X509_ASN_ENCODING,
-                                        (PBYTE)buf,
-                                        needed_len,
-                                        CERT_STORE_ADD_ALWAYS,
-                                        NULL);
+        ret = CertAddEncodedCertificateToStore (hStore,
+                                                X509_ASN_ENCODING,
+                                                (PBYTE)buf,
+                                                needed_len,
+                                                CERT_STORE_ADD_ALWAYS,
+                                                NULL);
 
-        if (ret != 0) {
-            printf("Failed to add certificate\n");
-            free(buf);
-            return ret;
+        if (ret == 0) {
+            LPWSTR error = getLastErrorMsg();
+            if (error) {
+                printf("Failed to add certificate: %S \n", error);
+                LocalFree(error);
+            }
         }
+        i++;
         free(buf);
     }
     if(hStore) {
@@ -250,7 +290,7 @@
 */
 
 #ifdef WIN32
-    install_certificates_win((const char**) to_install, 1);
+    return install_certificates_win((const char**) to_install, 1);
     //remove_certificates_win((const char**) to_remove, 1);
 #endif
 

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