0002-Fix-adbd-for-non-Ubuntu-systems.patch 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. From d433d5c340f3b36de58ea8550fd140dbdaea13b4 Mon Sep 17 00:00:00 2001
  2. From: Gary Bisson <gary.bisson@boundarydevices.com>
  3. Date: Sun, 1 Dec 2024 15:43:33 +0100
  4. Subject: [PATCH] Fix adbd for non-Ubuntu systems
  5. Remove glib/dbus dependencies and partially restore services.c to be
  6. closer to the original source code in order to run on systems without
  7. sudo.
  8. Signed-off-by: Gary Bisson <gary.bisson@boundarydevices.com>
  9. ---
  10. core/adbd/adb.c | 1 -
  11. core/adbd/services.c | 160 +++------------------------------------
  12. debian/makefiles/adbd.mk | 4 +-
  13. 3 files changed, 14 insertions(+), 151 deletions(-)
  14. diff --git a/core/adbd/adb.c b/core/adbd/adb.c
  15. index d90e6b8..7fe6445 100644
  16. --- a/core/adbd/adb.c
  17. +++ b/core/adbd/adb.c
  18. @@ -1165,7 +1165,6 @@ void build_local_name(char* target_str, size_t target_size, int server_port)
  19. #if !ADB_HOST
  20. static int should_drop_privileges() {
  21. - return 1;
  22. #ifndef ALLOW_ADBD_ROOT
  23. return 1;
  24. #else /* ALLOW_ADBD_ROOT */
  25. diff --git a/core/adbd/services.c b/core/adbd/services.c
  26. index 05bd0d0..5adcefe 100644
  27. --- a/core/adbd/services.c
  28. +++ b/core/adbd/services.c
  29. @@ -20,15 +20,6 @@
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <pwd.h>
  33. -#include <glib.h>
  34. -#include <gio/gio.h>
  35. -
  36. -#define UNITY_SERVICE "com.canonical.UnityGreeter"
  37. -#define GREETER_OBJ "/"
  38. -#define GREETER_INTERFACE "com.canonical.UnityGreeter"
  39. -#define PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
  40. -#define ACTIVE_PROPERTY "IsActive"
  41. -#define UNLOCK_PATH "/userdata/.adb_onlock"
  42. #include "sysdeps.h"
  43. @@ -268,11 +259,11 @@ static int create_service_thread(void (*func)(int, void *), void *cookie)
  44. }
  45. #if !ADB_HOST
  46. -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)
  47. +static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
  48. {
  49. #ifdef HAVE_WIN32_PROC
  50. - D("create_subprocess(cmd=%s, arg0=%s, arg1=%s, arg2=%s, arg3=%, arg4=%ss)\n", cmd, arg0, arg1, arg2, arg3, arg4);
  51. - fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s %s %s %s)\n", cmd, arg0, arg1, arg2, arg3, arg4);
  52. + D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
  53. + fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
  54. return -1;
  55. #else /* !HAVE_WIN32_PROC */
  56. char *devname;
  57. @@ -327,7 +318,7 @@ static int create_subprocess(const char *cmd, const char *arg0, const char *arg1
  58. } else {
  59. D("adb: unable to open %s\n", text);
  60. }
  61. - execl(cmd, cmd, arg0, arg1, arg2, arg3, arg4, NULL);
  62. + execl(cmd, cmd, arg0, arg1, NULL);
  63. fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
  64. cmd, strerror(errno), errno);
  65. exit(-1);
  66. @@ -342,7 +333,7 @@ static int create_subprocess(const char *cmd, const char *arg0, const char *arg1
  67. }
  68. #endif /* !ABD_HOST */
  69. -#if ADB_HOST
  70. +#if ADB_HOST || ADBD_NON_ANDROID
  71. #define SHELL_COMMAND "/bin/sh"
  72. #else
  73. #define SHELL_COMMAND "/system/bin/sh"
  74. @@ -380,139 +371,16 @@ static void subproc_waiter_service(int fd, void *cookie)
  75. }
  76. }
  77. -int is_phone_locked() {
  78. - GError *error = NULL;
  79. - GVariant *variant = NULL;
  80. - GDBusConnection *connection = NULL;
  81. -
  82. - if (g_file_test(UNLOCK_PATH, G_FILE_TEST_EXISTS)) {
  83. - D("unlock path present.");
  84. - return 0;
  85. - }
  86. -
  87. - // check if the environment variable is present, if not we grab it from
  88. - // the phablet user
  89. - if (g_getenv("DBUS_SESSION_BUS_ADDRESS") == NULL) {
  90. - D("DBUS_SESSION_BUS_ADDRESS missing.\n");
  91. - struct passwd *pw = getpwuid(AID_SHELL);
  92. - char user_id[15];
  93. - gchar *path = NULL;
  94. - gchar *contents = NULL;
  95. - gchar *session_path = NULL;
  96. -
  97. - snprintf(user_id, sizeof user_id, "%d", pw->pw_uid);
  98. -
  99. - path = g_build_filename("/run", "user", user_id, "dbus-session", NULL);
  100. -
  101. - g_file_get_contents(path, &contents, NULL, &error);
  102. - session_path = g_strstrip(g_strsplit(contents, "DBUS_SESSION_BUS_ADDRESS=", -1)[1]);
  103. - D("Session bus is %s\n", session_path);
  104. -
  105. - // path is not longer used
  106. - g_free(path);
  107. -
  108. - if (error != NULL) {
  109. - g_clear_error(&error);
  110. - D("Couldn't set session bus\n");
  111. - return 1;
  112. - }
  113. -
  114. - g_setenv("DBUS_SESSION_BUS_ADDRESS", session_path, TRUE);
  115. - g_free(contents);
  116. - }
  117. -
  118. - // set the uid to be able to connect to the phablet user session bus
  119. - setuid(AID_SHELL);
  120. - connection = g_dbus_connection_new_for_address_sync(g_getenv("DBUS_SESSION_BUS_ADDRESS"),
  121. - G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
  122. - NULL,
  123. - NULL,
  124. - &error);
  125. - if (connection == NULL) {
  126. - D("session bus not available: %s", error->message);
  127. - g_error_free (error);
  128. - return 1;
  129. - }
  130. -
  131. - variant = g_dbus_connection_call_sync(connection,
  132. - UNITY_SERVICE,
  133. - GREETER_OBJ,
  134. - PROPERTIES_INTERFACE,
  135. - "Get",
  136. - g_variant_new("(ss)", GREETER_INTERFACE, ACTIVE_PROPERTY),
  137. - g_variant_type_new("(v)"),
  138. - G_DBUS_CALL_FLAGS_NONE,
  139. - -1,
  140. - NULL,
  141. - &error);
  142. -
  143. - if (error != NULL) {
  144. - D("Could not get property: %s", error->message);
  145. - g_object_unref(connection);
  146. - g_error_free(error);
  147. - return 1;
  148. - }
  149. -
  150. - if (variant == NULL) {
  151. - D("Failed to get property '%s': %s", "IsActive", error->message);
  152. - g_object_unref(connection);
  153. - g_error_free(error);
  154. - return 1;
  155. - }
  156. -
  157. - variant = g_variant_get_variant(g_variant_get_child_value(variant, 0));
  158. -
  159. - int active = 1;
  160. - if (!g_variant_get_boolean(variant)) {
  161. - active = 0;
  162. - }
  163. -
  164. - // get back to be root and return the value
  165. - g_object_unref(connection);
  166. - g_variant_unref(variant);
  167. - setuid(0);
  168. - return active;
  169. -}
  170. -
  171. static int create_subproc_thread(const char *name)
  172. {
  173. - if (is_phone_locked() ) {
  174. - fprintf(stderr, "device is locked\n");
  175. - return -1;
  176. - }
  177. -
  178. stinfo *sti;
  179. adb_thread_t t;
  180. int ret_fd;
  181. pid_t pid;
  182. -
  183. - struct passwd *user = getpwuid(getuid());
  184. - char *shell;
  185. - char *shellopts = "-c";
  186. - char *home;
  187. - char *sudo = "/usr/bin/sudo";
  188. - char useropt[256] = "-u";
  189. -
  190. - if (user->pw_name)
  191. - strcat(useropt, user->pw_name);
  192. -
  193. - if (user && user->pw_shell) {
  194. - shell = user->pw_shell;
  195. - shellopts = "-cl";
  196. - } else {
  197. - shell = SHELL_COMMAND;
  198. - }
  199. -
  200. - if (user->pw_dir)
  201. - home = user->pw_dir;
  202. - if(chdir(home) < 0 )
  203. - return 1;
  204. -
  205. if(name) {
  206. - ret_fd = create_subprocess(sudo, useropt, "-i", shell, shellopts, name, &pid);
  207. + ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid);
  208. } else {
  209. - shellopts = "-l";
  210. - ret_fd = create_subprocess(sudo, useropt, "-i", shell, shellopts, 0, &pid);
  211. + ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid);
  212. }
  213. D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
  214. @@ -585,17 +453,13 @@ int service_to_fd(const char *name)
  215. } else if (!strncmp(name, "log:", 4)) {
  216. ret = create_service_thread(log_service, get_log_file_path(name + 4));
  217. } else if(!HOST && !strncmp(name, "shell:", 6)) {
  218. - if (!is_phone_locked() ) {
  219. - if(name[6]) {
  220. - ret = create_subproc_thread(name + 6);
  221. - } else {
  222. - ret = create_subproc_thread(0);
  223. - }
  224. + if(name[6]) {
  225. + ret = create_subproc_thread(name + 6);
  226. + } else {
  227. + ret = create_subproc_thread(0);
  228. }
  229. } else if(!strncmp(name, "sync:", 5)) {
  230. - if (!is_phone_locked() ) {
  231. - ret = create_service_thread(file_sync_service, NULL);
  232. - }
  233. + ret = create_service_thread(file_sync_service, NULL);
  234. } else if(!strncmp(name, "remount:", 8)) {
  235. ret = create_service_thread(remount_service, NULL);
  236. } else if(!strncmp(name, "reboot:", 7)) {
  237. diff --git a/debian/makefiles/adbd.mk b/debian/makefiles/adbd.mk
  238. index 49dab8c..22c1816 100644
  239. --- a/debian/makefiles/adbd.mk
  240. +++ b/debian/makefiles/adbd.mk
  241. @@ -40,11 +40,11 @@ CPPFLAGS+= -O2 -g -Wall -Wno-unused-parameter
  242. CPPFLAGS+= -DADB_HOST=0 -DHAVE_FORKEXEC=1 -D_XOPEN_SOURCE -D_GNU_SOURCE -DALLOW_ADBD_ROOT=1
  243. CPPFLAGS+= -DHAVE_SYMLINKS -DBOARD_ALWAYS_INSECURE
  244. CPPFLAGS+= -DHAVE_TERMIO_H
  245. -CPPFLAGS+= `pkg-config --cflags glib-2.0 gio-2.0`
  246. +CPPFLAGS+= -DADBD_NON_ANDROID
  247. CPPFLAGS+= -I$(SRCDIR)/core/adbd
  248. CPPFLAGS+= -I$(SRCDIR)/core/include
  249. -LIBS+= -lc -lpthread -lz -lcrypto -lcrypt `pkg-config --libs glib-2.0 gio-2.0`
  250. +LIBS+= -lc -lpthread -lz -lcrypto -lcrypt
  251. OBJS= $(patsubst %, %.o, $(basename $(SRCS)))
  252. --
  253. 2.47.0