Removed printfs
[moonshot-ui.git] / libmoonshot / libmoonshot-dbus.c
index 97dba1e..52d1b0e 100644 (file)
@@ -33,6 +33,8 @@
  */
 
 #include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <dbus/dbus-glib.h>
@@ -111,6 +113,16 @@ static DBusGConnection *dbus_launch_moonshot()
        return connection;
 }
 
+static int is_setid()
+{
+#ifdef HAVE_GETEUID
+  if ((getuid() != geteuid()) || 
+      (getgid() != getegid())) {
+    return 1;
+  }
+#endif
+  return 0;
+}
 
 static DBusGProxy *dbus_connect (MoonshotError **error)
 {
@@ -130,24 +142,41 @@ static DBusGProxy *dbus_connect (MoonshotError **error)
      * If/when we move to GDBus this code can become a one-liner.
      */
 
-    connection = dbus_g_bus_get (DBUS_BUS_SESSION, &g_error);
-
-    if (g_error_matches(g_error, DBUS_GERROR, DBUS_GERROR_NOT_SUPPORTED)) {
-      /*Generally this means autolaunch failed because probably DISPLAY is unset*/
-      connection = dbus_launch_moonshot();
-      if (connection != NULL) {
-       g_error_free(g_error);
-       g_error = NULL;
-      }
-    }
-
-    if (g_error != NULL) {
-      *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
-                                  "DBus error: %s",
-                                  g_error->message);
-      g_error_free (g_error);
+    if (is_setid()) {
+        *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                    "Cannot use IPC while setid");
         return NULL;
     }
+#ifdef IPC_DBUS_GLIB
+    if (getenv("DISPLAY")==NULL) {
+        connection = dbus_launch_moonshot();
+        if (connection == NULL) {
+            *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                         "Headless dbus launch failed");
+            return NULL;
+        }
+    } else
+#endif
+    {
+        connection = dbus_g_bus_get (DBUS_BUS_SESSION, &g_error);
+
+        if (g_error_matches(g_error, DBUS_GERROR, DBUS_GERROR_NOT_SUPPORTED)) {
+            /*Generally this means autolaunch failed because probably DISPLAY is unset*/
+            connection = dbus_launch_moonshot();
+            if (connection != NULL) {
+                g_error_free(g_error);
+                g_error = NULL;
+            }
+        }
+        if (g_error != NULL) {
+            *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
+                                         "DBus error: %s",
+                                         g_error->message);
+            g_error_free (g_error);
+            return NULL;
+        }
+    }
+
 
     dbconnection = dbus_g_connection_get_connection(connection);
     name_has_owner  = dbus_bus_name_has_owner (dbconnection,
@@ -407,6 +436,9 @@ int moonshot_install_id_card (const char     *display_name,
                        G_TYPE_INVALID);
 
     g_object_unref (dbus_proxy);
+    g_free(rules_patterns_strv);
+    g_free(rules_always_confirm_strv);
+    g_free(services_strv);
 
     if (g_error != NULL) {
         *error = moonshot_error_new (MOONSHOT_ERROR_IPC_ERROR,
@@ -416,3 +448,53 @@ int moonshot_install_id_card (const char     *display_name,
 
     return success;
 }
+
+int moonshot_confirm_ca_certificate (const char           *identity_name,
+                                     const char           *realm,
+                                     const unsigned char  *ca_hash,
+                                     int                   hash_len,
+                                     MoonshotError       **error)
+{
+    GError     *g_error = NULL;
+    int         success = 99;
+    int         confirmed = 99;
+    char        hash_str[65];
+    DBusGProxy *dbus_proxy = get_dbus_proxy (error);
+    int         out = 0;
+    int         i;
+
+    if (*error != NULL) {
+        return FALSE;
+    }
+
+    g_return_val_if_fail (DBUS_IS_G_PROXY (dbus_proxy), FALSE);
+
+    /* Convert hash byte array to string */
+    out = 0;
+    for (i = 0; i < hash_len; i++) {
+        sprintf(&(hash_str[out]), "%02X", ca_hash[i]);
+        out += 2;
+    }
+
+    dbus_g_proxy_call_with_timeout (dbus_proxy,
+                                    "ConfirmCaCertificate",
+                                    INFINITE_TIMEOUT,
+                                    &g_error,
+                                    G_TYPE_STRING, identity_name,
+                                    G_TYPE_STRING, realm,
+                                    G_TYPE_STRING, hash_str,
+                                    G_TYPE_INVALID,
+                                    G_TYPE_INT,   &confirmed,
+                                    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 (int) confirmed;
+}