Windows: improve error reporting
[moonshot-ui.git] / libmoonshot / libmoonshot-msrpc.c
index bfea62d..6a2010d 100644 (file)
 #define MOONSHOT_INSTALL_PATH_KEY "Software\\Moonshot"
 
 void *__RPC_USER MIDL_user_allocate (size_t size) {
-       return malloc (size);
+    return malloc (size);
 }
 
 void __RPC_USER MIDL_user_free (void *data) {
-       free (data);
+    if (data == NULL)
+        return;
+
+    free (data);
+}
+
+void moonshot_free (void *data)
+{
+    free (data);
 }
 
 static MoonshotError *moonshot_error_new_from_status (MoonshotErrorCode code,
@@ -134,7 +142,7 @@ static void launch_server (MoonshotError **error) {
     }
 
     length = 1023;
-    status = RegQueryValueEx (key, NULL, NULL, &value_type, exe_path, &length);
+    status = RegQueryValueEx (key, NULL, NULL, &value_type, (LPBYTE )exe_path, &length);
 
     if (value_type != REG_SZ) {
         *error = moonshot_error_new_with_status
@@ -157,7 +165,6 @@ static void launch_server (MoonshotError **error) {
     }
 
     startup_info.cb = sizeof (startup_info);
-
     success = CreateProcess (exe_path,
                              NULL,
                              NULL,
@@ -223,7 +230,6 @@ static void bind_rpc (MoonshotError **error)
 static void init_rpc (MoonshotError **error)
 {
     static volatile LONG binding_init_flag = 2;
-    int status;
 
     /* Hack to avoid requiring a moonshot_init() function. Windows does not
      * provide any synchronisation primitives that can be statically init'ed,
@@ -266,7 +272,7 @@ int moonshot_get_identity (const char     *nai,
                            char          **subject_alt_name_constraint_out,
                            MoonshotError **error)
 {
-    int success;
+    int success = FALSE;
     RpcAsyncCall call;
 
     init_rpc (error);
@@ -276,6 +282,10 @@ int moonshot_get_identity (const char     *nai,
 
     rpc_async_call_init (&call);
 
+    if (nai == NULL) nai = "";
+    if (password == NULL) password = "";
+    if (service == NULL) service = "";
+
     *nai_out = NULL;
     *password_out = NULL;
     *server_certificate_hash_out = NULL;
@@ -325,7 +335,7 @@ int moonshot_get_default_identity (char          **nai_out,
                                    char          **subject_alt_name_constraint_out,
                                    MoonshotError **error)
 {
-    int success;
+    int success = FALSE;
     RpcAsyncCall call;
 
     init_rpc (error);
@@ -371,3 +381,77 @@ int moonshot_get_default_identity (char          **nai_out,
 
     return TRUE;
 };
+
+int moonshot_install_id_card (const char     *display_name,
+                              const char     *user_name,
+                              const char     *password,
+                              const char     *realm,
+                              char           *rules_patterns[],
+                              int             rules_patterns_length,
+                              char           *rules_always_confirm[],
+                              int             rules_always_confirm_length,
+                              char           *services[],
+                              int             services_length,
+                              const char     *ca_cert,
+                              const char     *subject,
+                              const char     *subject_alt,
+                              const char     *server_cert,
+                              int             force_flat_file_store,
+                              MoonshotError **error)
+{
+    int success = FALSE;
+
+    init_rpc (error);
+    if (*error != NULL)
+        return FALSE;
+
+    if (user_name == NULL) user_name = "";
+    if (password == NULL) password = "";
+    if (realm == NULL) realm = "";
+    if (ca_cert == NULL) ca_cert = "";
+    if (subject == NULL) subject = "";
+    if (subject_alt == NULL) subject_alt = "";
+    if (server_cert == NULL) server_cert = "";
+
+    RPC_TRY_EXCEPT {
+        success = moonshot_install_id_card_rpc (display_name,
+                                                user_name,
+                                                password,
+                                                realm,
+                                                rules_patterns,
+                                                rules_patterns_length,
+                                                rules_always_confirm,
+                                                rules_always_confirm_length,
+                                                services,
+                                                services_length,
+                                                ca_cert,
+                                                subject,
+                                                subject_alt,
+                                                server_cert,
+                                                force_flat_file_store);
+    }
+    RPC_EXCEPT {
+        *error = moonshot_error_new_from_status (MOONSHOT_ERROR_IPC_ERROR,
+                                                 RPC_GET_EXCEPTION_CODE ());
+    }
+    RPC_END_EXCEPT
+    return success;
+}
+
+BOOL WINAPI DllMain (HINSTANCE  hinst,
+                     DWORD      reason,
+                     void      *reserved)
+{
+    if (reason == DLL_PROCESS_DETACH) {
+        /* Process exiting/DLL being unloaded. This is a good
+         * opportunity to free the RPC binding.
+         *
+         * FIXME: we can't use the msrpc-mingw routine for this in case
+         * it was already unloaded. I'd love to work out how to link
+         * that library statically into libmoonshot-0.dll.
+         */
+        RpcBindingFree (&moonshot_binding_handle);
+    }
+
+    return TRUE;
+}