autoreconf
[moonshot-ui.git] / libmoonshot / libmoonshot-dbus.c
index 04d6da6..ff3f186 100644 (file)
@@ -38,6 +38,9 @@
 #include "libmoonshot.h"
 #include "libmoonshot-common.h"
 
+/*30 days in ms*/
+#define INFINITE_TIMEOUT 10*24*60*60*1000
+
 #define MOONSHOT_DBUS_NAME "org.janet.Moonshot"
 #define MOONSHOT_DBUS_PATH "/org/janet/moonshot"
 
  * waiting for calls.
  */
 
-static DBusGProxy *moonshot_dbus_proxy = NULL;
+void moonshot_free (void *data)
+{
+    g_free (data);
+}
 
 static DBusGProxy *dbus_connect (MoonshotError **error)
 {
@@ -148,6 +154,29 @@ static DBusGProxy *dbus_connect (MoonshotError **error)
     return g_proxy; 
 }
 
+static DBusGProxy *get_dbus_proxy (MoonshotError **error)
+{
+    static DBusGProxy    *dbus_proxy = NULL;
+    static GStaticMutex   init_lock = G_STATIC_MUTEX_INIT;
+
+    g_static_mutex_lock (&init_lock);
+
+    if (dbus_proxy == NULL) {
+        /* Make sure GObject is initialised, in case we are the only user
+         * of GObject in the process
+         */
+        g_type_init ();
+        dbus_proxy = dbus_connect (error);
+    }
+
+    if (dbus_proxy != NULL)
+        g_object_ref (dbus_proxy);
+
+    g_static_mutex_unlock (&init_lock);
+
+    return dbus_proxy;
+}
+
 int moonshot_get_identity (const char     *nai,
                            const char     *password,
                            const char     *service,
@@ -159,20 +188,21 @@ int moonshot_get_identity (const char     *nai,
                            char          **subject_alt_name_constraint_out,
                            MoonshotError **error)
 {
-    GError   *g_error = NULL;
-    int success;
+    GError     *g_error = NULL;
+    DBusGProxy *dbus_proxy;
+    int         success;
 
-    if (moonshot_dbus_proxy == NULL)
-        moonshot_dbus_proxy = dbus_connect (error);
+    dbus_proxy = get_dbus_proxy (error);
 
     if (*error != NULL)
-        return;
+        return FALSE;
 
-    g_return_if_fail (DBUS_IS_G_PROXY (moonshot_dbus_proxy));
+    g_return_val_if_fail (DBUS_IS_G_PROXY (dbus_proxy), FALSE);
 
-    dbus_g_proxy_call (moonshot_dbus_proxy,
+    dbus_g_proxy_call_with_timeout (dbus_proxy,
                        "GetIdentity",
-                       &g_error,
+                                   INFINITE_TIMEOUT,
+                                   &g_error,
                        G_TYPE_STRING, nai,
                        G_TYPE_STRING, password,
                        G_TYPE_STRING, service,
@@ -186,6 +216,8 @@ int moonshot_get_identity (const char     *nai,
                        G_TYPE_BOOLEAN, &success,
                        G_TYPE_INVALID);
 
+    g_object_unref (dbus_proxy);
+
     if (g_error != NULL) {
         *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
                                      g_error->message);
@@ -202,13 +234,131 @@ int moonshot_get_identity (const char     *nai,
     return TRUE;
 }
 
+int moonshot_get_default_identity (char          **nai_out,
+                                   char          **password_out,
+                                   char          **server_certificate_hash_out,
+                                   char          **ca_certificate_out,
+                                   char          **subject_name_constraint_out,
+                                   char          **subject_alt_name_constraint_out,
+                                   MoonshotError **error)
+{
+    GError     *g_error = NULL;
+    DBusGProxy *dbus_proxy;
+    int         success = FALSE;
 
+    dbus_proxy = get_dbus_proxy (error);
 
-    /**
-     * Returns the default identity - most recently used.
-     *
-     * @param nai_out NAI stored in the ID card
-     * @param password_out Password stored in the ID card
-     *
-     * @return true on success, false if no identities are stored
-     */
+    if (*error != NULL)
+        return FALSE;
+
+    g_return_val_if_fail (DBUS_IS_G_PROXY (dbus_proxy), FALSE);
+
+    dbus_g_proxy_call_with_timeout (dbus_proxy,
+                       "GetDefaultIdentity",
+                                   INFINITE_TIMEOUT,
+                       &g_error,
+                       G_TYPE_INVALID,
+                       G_TYPE_STRING, nai_out,
+                       G_TYPE_STRING, password_out,
+                       G_TYPE_STRING, server_certificate_hash_out,
+                       G_TYPE_STRING, ca_certificate_out,
+                       G_TYPE_STRING, subject_name_constraint_out,
+                       G_TYPE_STRING, subject_alt_name_constraint_out,
+                       G_TYPE_BOOLEAN, &success,
+                       G_TYPE_INVALID);
+
+    g_object_unref (dbus_proxy);
+
+    if (g_error != NULL) {
+        *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                     g_error->message);
+        return FALSE;
+    }
+
+    if (success == FALSE) {
+        *error = moonshot_error_new (MOONSHOT_ERROR_NO_IDENTITY_SELECTED,
+                                     "No identity was returned by the Moonshot "
+                                     "user interface.");
+        return FALSE;
+    }
+
+    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,
+                              MoonshotError **error)
+{
+    GError      *g_error = NULL;
+    DBusGProxy  *dbus_proxy;
+    int          success = FALSE;
+    int          i;
+    const char **rules_patterns_strv,
+               **rules_always_confirm_strv,
+               **services_strv;
+
+    dbus_proxy = get_dbus_proxy (error);
+
+    if (*error != NULL)
+        return FALSE;
+
+    g_return_val_if_fail (DBUS_IS_G_PROXY (dbus_proxy), FALSE);
+    g_return_val_if_fail (rules_patterns_length == rules_always_confirm_length, FALSE);
+
+    /* Marshall array and struct parameters for DBus */
+    rules_patterns_strv = g_malloc ((rules_patterns_length + 1) * sizeof (const char *));
+    rules_always_confirm_strv = g_malloc ((rules_patterns_length + 1) * sizeof (const char *));
+    services_strv = g_malloc ((services_length + 1) * sizeof (const char *));
+
+    for (i = 0; i < rules_patterns_length; i ++) {
+        rules_patterns_strv[i] = rules_patterns[i];
+        rules_always_confirm_strv[i] = rules_always_confirm[i];
+    }
+
+    for (i = 0; i < services_length; i ++)
+        services_strv[i] = services[i];
+
+    rules_patterns_strv[rules_patterns_length] = NULL;
+    rules_always_confirm_strv[rules_patterns_length] = NULL;
+    services_strv[services_length] = NULL;
+
+    dbus_g_proxy_call (dbus_proxy,
+                       "InstallIdCard",
+                       &g_error,
+                       G_TYPE_STRING, display_name,
+                       G_TYPE_STRING, user_name,
+                       G_TYPE_STRING, password,
+                       G_TYPE_STRING, realm,
+                       G_TYPE_STRV, rules_patterns_strv,
+                       G_TYPE_STRV, rules_always_confirm_strv,
+                       G_TYPE_STRV, services_strv,
+                       G_TYPE_STRING, ca_cert,
+                       G_TYPE_STRING, subject,
+                       G_TYPE_STRING, subject_alt,
+                       G_TYPE_STRING, server_cert,
+                       G_TYPE_INVALID,
+                       G_TYPE_BOOLEAN, &success,
+                       G_TYPE_INVALID);
+
+    g_object_unref (dbus_proxy);
+
+    if (g_error != NULL) {
+        *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                     g_error->message);
+        return FALSE;
+    }
+
+    return success;
+}