changeset 132:4691d9e3b1d3

Merged
author Sascha Wilde <wilde@intevation.de>
date Mon, 24 Mar 2014 13:35:20 +0100
parents 9104b1b2e4da (current diff) ffb20e76e7d0 (diff)
children c719d3fdbc15 6232ea0c8438
files
diffstat 6 files changed, 94 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/cinst/CMakeLists.txt	Mon Mar 24 13:32:12 2014 +0100
+++ b/cinst/CMakeLists.txt	Mon Mar 24 13:35:20 2014 +0100
@@ -24,6 +24,10 @@
    ${POLARSSL_LIBRARIES}
    ${WIN_EXTRA_LIBS})
 
+install(TARGETS cinst DESTINATION bin)
+
 target_link_libraries(mozilla
    m13_common
    ${PROFILING_LIBS})
+
+install(TARGETS mozilla DESTINATION bin)
--- 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
 
--- a/cinst/mozilla.c	Mon Mar 24 13:32:12 2014 +0100
+++ b/cinst/mozilla.c	Mon Mar 24 13:35:20 2014 +0100
@@ -8,7 +8,7 @@
  * R:<base64 DER econded certificate>\r\n
  * ...
  *
- * The maximum size of an input line is 1000 characters
+ * The maximum size of an input line is 9999 characters
  * (including the \r\n) at the end of the line.
  *
  * Certificates marked with I: will be installed and the ones
--- a/common/listutil.h	Mon Mar 24 13:32:12 2014 +0100
+++ b/common/listutil.h	Mon Mar 24 13:35:20 2014 +0100
@@ -28,7 +28,7 @@
 } list_status_t;
 
 /* Definitions based on the format */
-#define MAX_LINE_LENGTH 1000
+#define MAX_LINE_LENGTH 9999
 #define MAX_LINES 1000
 
 /**
--- a/ui/CMakeLists.txt	Mon Mar 24 13:32:12 2014 +0100
+++ b/ui/CMakeLists.txt	Mon Mar 24 13:35:20 2014 +0100
@@ -81,3 +81,5 @@
 
 # Tests
 add_subdirectory(tests)
+
+install(TARGETS m13ui DESTINATION bin)
--- a/ui/listupdatedialog.cpp	Mon Mar 24 13:32:12 2014 +0100
+++ b/ui/listupdatedialog.cpp	Mon Mar 24 13:35:20 2014 +0100
@@ -1,5 +1,8 @@
 #include "listupdatedialog.h"
+#include <QCoreApplication>
 #include <QDebug>
+#include <QDir>
+#include <QFileInfo>
 #include <QProcess>
 #include <QPushButton>
 #include <QGroupBox>
@@ -40,6 +43,8 @@
         }
         QListWidgetItem* item = new QListWidgetItem(cert.shortDescription());
         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
+        item->setData(Qt::ToolTipRole, cert.details());
+        item->setData(Qt::UserRole, cert.base64Line());
         item->setCheckState(Qt::Checked);
         mRemoveListWidget->addItem(item);
     }
@@ -80,8 +85,15 @@
 void ListUpdateDialog::executeUpdate() {
     /* TODO move this in another dialog and call it async*/
     QProcess installerProcess;
+    QFileInfo fi(QCoreApplication::applicationFilePath());
+    QDir myDir = fi.absoluteDir();
+    QString instProcName = "cinst";
+    if (!fi.suffix().isEmpty()) {
+        instProcName += "." + fi.suffix();
+    }
+    QString instPath = myDir.absoluteFilePath(instProcName);
 
-    installerProcess.setProgram("../cinst/cinst");
+    installerProcess.setProgram(instPath);
     installerProcess.start();
     installerProcess.waitForStarted();
     if (installerProcess.state() == QProcess::NotRunning) {
@@ -89,14 +101,27 @@
         /* TODO ERROR message for the user */
         return;
     }
+
     installerProcess.write("-----BEGIN CERTIFICATE LIST-----\r\n");
     installerProcess.write(mCertificateList.rawData().toLatin1());
     installerProcess.write("-----END CERTIFICATE LIST-----\r\n");
 
-    QList<QListWidgetItem *> selectedItems = mInstallListWidget->selectedItems();
-    selectedItems << mRemoveListWidget->selectedItems();
+    /* Items to install */
+    for (int i = 0; i < mInstallListWidget->count(); i++) {
+        QListWidgetItem *item = mInstallListWidget->item(i);
+        if (!item->checkState() == Qt::Checked) {
+            continue;
+        }
+        installerProcess.write(item->data(Qt::UserRole).toString().toLatin1());
+        installerProcess.write("\r\n");
+    }
 
-    foreach (const QListWidgetItem * item, selectedItems) {
+    /* Items to remove */
+    for (int i = 0; i < mRemoveListWidget->count(); i++) {
+        QListWidgetItem *item = mRemoveListWidget->item(i);
+        if (!item->checkState() == Qt::Checked) {
+            continue;
+        }
         installerProcess.write(item->data(Qt::UserRole).toString().toLatin1());
         installerProcess.write("\r\n");
     }
@@ -116,6 +141,7 @@
 
     if (installerProcess.exitCode() == 0) {
         qDebug() << "Success!";
+        qDebug() << "output: " << installerProcess.readAllStandardOutput();
     } else {
         /* TODO handle errors defined by errorcodes.h */
         qDebug() << "Installer Process returned: " << installerProcess.exitCode();

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