[PATCH] Fix adbd for non-Ubuntu systems

Remove glib/dbus dependencies and partially restore services.c to be
closer to the original source code in order to run on systems without
sudo.

Signed-off-by: Gary Bisson <gary.bisson@boundarydevices.com>
---
 core/adbd/adb.c          |   1 -
 core/adbd/services.c     | 160 ++++-------------------------------------------
 debian/makefiles/adbd.mk |   4 +-
 3 files changed, 14 insertions(+), 151 deletions(-)

diff --git a/core/adbd/adb.c b/core/adbd/adb.c
index d90e6b8..7fe6445 100644
--- a/core/adbd/adb.c
+++ b/core/adbd/adb.c
@@ -1165,7 +1165,6 @@ void build_local_name(char* target_str, size_t target_size, int server_port)
 
 #if !ADB_HOST
 static int should_drop_privileges() {
-    return 1;
 #ifndef ALLOW_ADBD_ROOT
     return 1;
 #else /* ALLOW_ADBD_ROOT */
diff --git a/core/adbd/services.c b/core/adbd/services.c
index 05bd0d0..5adcefe 100644
--- a/core/adbd/services.c
+++ b/core/adbd/services.c
@@ -20,15 +20,6 @@
 #include <string.h>
 #include <errno.h>
 #include <pwd.h>
-#include <glib.h>
-#include <gio/gio.h>
-
-#define UNITY_SERVICE "com.canonical.UnityGreeter"
-#define GREETER_OBJ "/"
-#define GREETER_INTERFACE "com.canonical.UnityGreeter"
-#define PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
-#define ACTIVE_PROPERTY "IsActive"
-#define UNLOCK_PATH "/userdata/.adb_onlock"
 
 #include "sysdeps.h"
 
@@ -268,11 +259,11 @@ static int create_service_thread(void (*func)(int, void *), void *cookie)
 }
 
 #if !ADB_HOST
-static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, const char *arg2, const char *arg3,  const char *arg4, pid_t *pid)
+static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
 {
 #ifdef HAVE_WIN32_PROC
-    D("create_subprocess(cmd=%s, arg0=%s, arg1=%s, arg2=%s, arg3=%, arg4=%ss)\n", cmd, arg0, arg1, arg2, arg3, arg4);
-    fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s %s %s %s)\n", cmd, arg0, arg1, arg2, arg3, arg4);
+    D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
+    fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
     return -1;
 #else /* !HAVE_WIN32_PROC */
     char *devname;
@@ -327,7 +318,7 @@ static int create_subprocess(const char *cmd, const char *arg0, const char *arg1
         } else {
            D("adb: unable to open %s\n", text);
         }
-	execl(cmd, cmd, arg0, arg1, arg2, arg3, arg4, NULL);
+        execl(cmd, cmd, arg0, arg1, NULL);
         fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
                 cmd, strerror(errno), errno);
         exit(-1);
@@ -342,7 +333,7 @@ static int create_subprocess(const char *cmd, const char *arg0, const char *arg1
 }
 #endif  /* !ABD_HOST */
 
-#if ADB_HOST
+#if ADB_HOST || ADBD_NON_ANDROID
 #define SHELL_COMMAND "/bin/sh"
 #else
 #define SHELL_COMMAND "/system/bin/sh"
@@ -380,139 +371,16 @@ static void subproc_waiter_service(int fd, void *cookie)
     }
 }
 
-int is_phone_locked() {
-    GError *error = NULL;
-    GVariant *variant = NULL;
-    GDBusConnection *connection = NULL;
-
-    if (g_file_test(UNLOCK_PATH, G_FILE_TEST_EXISTS)) {
-        D("unlock path present.");
-        return 0;
-    }
-
-    // check if the environment variable is present, if not we grab it from
-    // the phablet user
-    if (g_getenv("DBUS_SESSION_BUS_ADDRESS") == NULL) {
-        D("DBUS_SESSION_BUS_ADDRESS missing.\n");
-        struct passwd *pw = getpwuid(AID_SHELL);
-        char user_id[15];
-        gchar *path = NULL;
-        gchar *contents = NULL;
-        gchar *session_path = NULL;
-
-        snprintf(user_id, sizeof user_id, "%d", pw->pw_uid);
-
-        path = g_build_filename("/run", "user", user_id, "dbus-session", NULL);
-
-        g_file_get_contents(path, &contents, NULL, &error);
-        session_path = g_strstrip(g_strsplit(contents, "DBUS_SESSION_BUS_ADDRESS=", -1)[1]);
-        D("Session bus is %s\n", session_path);
-
-        // path is not longer used
-        g_free(path);
-
-        if (error != NULL) {
-            g_clear_error(&error);
-            D("Couldn't set session bus\n");
-            return 1;
-        }
-
-        g_setenv("DBUS_SESSION_BUS_ADDRESS", session_path, TRUE);
-        g_free(contents);
-    }
-
-    // set the uid to be able to connect to the phablet user session bus
-    setuid(AID_SHELL);
-    connection =  g_dbus_connection_new_for_address_sync(g_getenv("DBUS_SESSION_BUS_ADDRESS"),
-                                                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
-                                                         NULL,
-                                                         NULL,
-                                                         &error);
-    if (connection == NULL) {
-        D("session bus not available: %s", error->message);
-        g_error_free (error);
-        return 1;
-    }
-
-    variant = g_dbus_connection_call_sync(connection,
-                                          UNITY_SERVICE,
-                                          GREETER_OBJ,
-                                          PROPERTIES_INTERFACE,
-                                          "Get",
-                                          g_variant_new("(ss)", GREETER_INTERFACE, ACTIVE_PROPERTY),
-                                          g_variant_type_new("(v)"),
-                                          G_DBUS_CALL_FLAGS_NONE,
-                                          -1,
-                                          NULL,
-                                          &error);
-
-    if (error != NULL) {
-        D("Could not get property: %s", error->message);
-        g_object_unref(connection);
-        g_error_free(error);
-        return 1;
-    }
-
-    if (variant == NULL) {
-        D("Failed to get property '%s': %s", "IsActive", error->message);
-        g_object_unref(connection);
-        g_error_free(error);
-        return 1;
-    }
-
-    variant = g_variant_get_variant(g_variant_get_child_value(variant, 0));
-
-    int active = 1;
-    if (!g_variant_get_boolean(variant)) {
-        active = 0;
-    }
-
-    // get back to be root and return the value
-    g_object_unref(connection);
-    g_variant_unref(variant);
-    setuid(0);
-    return active;
-}
-
 static int create_subproc_thread(const char *name)
 {
-    if (is_phone_locked() ) {
-        fprintf(stderr, "device is locked\n");
-        return -1;
-    }
-
     stinfo *sti;
     adb_thread_t t;
     int ret_fd;
     pid_t pid;
-
-    struct passwd *user = getpwuid(getuid());
-    char *shell;
-    char *shellopts = "-c";
-    char *home;
-    char *sudo = "/usr/bin/sudo";
-    char useropt[256] = "-u";
-
-    if (user->pw_name)
-        strcat(useropt, user->pw_name);
-
-    if (user && user->pw_shell) {
-        shell = user->pw_shell;
-        shellopts = "-cl";
-    } else {
-        shell = SHELL_COMMAND;
-    }
-
-    if (user->pw_dir)
-        home = user->pw_dir;
-        if(chdir(home) < 0 )
-            return 1;
-
     if(name) {
-	ret_fd = create_subprocess(sudo, useropt, "-i", shell, shellopts, name, &pid);
+        ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid);
     } else {
-	shellopts = "-l";
-	ret_fd = create_subprocess(sudo, useropt, "-i", shell, shellopts, 0, &pid);
+        ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid);
     }
     D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
 
@@ -585,17 +453,13 @@ int service_to_fd(const char *name)
     } else if (!strncmp(name, "log:", 4)) {
         ret = create_service_thread(log_service, get_log_file_path(name + 4));
     } else if(!HOST && !strncmp(name, "shell:", 6)) {
-        if (!is_phone_locked() ) {
-            if(name[6]) {
-                ret = create_subproc_thread(name + 6);
-            } else {
-                ret = create_subproc_thread(0);
-            }
+        if(name[6]) {
+            ret = create_subproc_thread(name + 6);
+        } else {
+            ret = create_subproc_thread(0);
         }
     } else if(!strncmp(name, "sync:", 5)) {
-        if (!is_phone_locked() ) {
-            ret = create_service_thread(file_sync_service, NULL);
-        }
+        ret = create_service_thread(file_sync_service, NULL);
     } else if(!strncmp(name, "remount:", 8)) {
         ret = create_service_thread(remount_service, NULL);
     } else if(!strncmp(name, "reboot:", 7)) {
diff --git a/debian/makefiles/adbd.mk b/debian/makefiles/adbd.mk
index 49dab8c..22c1816 100644
--- a/debian/makefiles/adbd.mk
+++ b/debian/makefiles/adbd.mk
@@ -40,11 +40,11 @@ CPPFLAGS+= -O2 -g -Wall -Wno-unused-parameter
 CPPFLAGS+= -DADB_HOST=0 -DHAVE_FORKEXEC=1 -D_XOPEN_SOURCE -D_GNU_SOURCE -DALLOW_ADBD_ROOT=1
 CPPFLAGS+= -DHAVE_SYMLINKS -DBOARD_ALWAYS_INSECURE
 CPPFLAGS+= -DHAVE_TERMIO_H
-CPPFLAGS+= `pkg-config --cflags glib-2.0 gio-2.0`
+CPPFLAGS+= -DADBD_NON_ANDROID
 CPPFLAGS+= -I$(SRCDIR)/core/adbd
 CPPFLAGS+= -I$(SRCDIR)/core/include
 
-LIBS+= -lc -lpthread -lz -lcrypto -lcrypt `pkg-config --libs glib-2.0 gio-2.0`
+LIBS+= -lc -lpthread -lz -lcrypto -lcrypt
 
 OBJS= $(patsubst %, %.o, $(basename $(SRCS)))
 
-- 
2.5.1

