OSDN Git Service

work in progress...
[android-x86/external-koush-Superuser.git] / Superuser / jni / su / su.c
1 /*
2 ** Copyright 2010, Adam Shanks (@ChainsDD)
3 ** Copyright 2008, Zinx Verituse (@zinxv)
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <sys/wait.h>
22 #include <sys/select.h>
23 #include <sys/time.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <getopt.h>
30 #include <stdint.h>
31 #include <pwd.h>
32 #include <sys/stat.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <pwd.h>
36
37 #include "su.h"
38 #include "utils.h"
39
40 int get_shell_uid() {
41   struct passwd* ppwd = getpwnam("shell");
42   if (NULL == ppwd) {
43     return -1;
44   }
45   
46   return ppwd->pw_uid;
47 }
48
49 void exec_log(char *priority, char* logline) {
50   int pid;
51   if ((pid = fork()) == 0) {
52       int zero = open("/dev/zero", O_RDONLY | O_CLOEXEC);
53       dup2(zero, 0);
54       int null = open("/dev/null", O_WRONLY | O_CLOEXEC);
55       dup2(null, 1);
56       dup2(null, 2);
57       execl("/system/bin/log", "/system/bin/log", "-p", priority, "-t", LOG_TAG, logline);
58       _exit(0);
59   }
60 }
61
62 void exec_loge(const char* fmt, ...) {
63     va_list args;
64
65     char logline[PATH_MAX];
66     va_start(args, fmt);
67     vsnprintf(logline, PATH_MAX, fmt, args);
68     va_end(args);
69     exec_log("e", logline);
70 }
71
72 void exec_logw(const char* fmt, ...) {
73     va_list args;
74
75     char logline[PATH_MAX];
76     va_start(args, fmt);
77     vsnprintf(logline, PATH_MAX, fmt, args);
78     va_end(args);
79     exec_log("w", logline);
80 }
81
82 void exec_logd(const char* fmt, ...) {
83     va_list args;
84
85     char logline[PATH_MAX];
86     va_start(args, fmt);
87     vsnprintf(logline, PATH_MAX, fmt, args);
88     va_end(args);
89     exec_log("d", logline);
90 }
91
92 static int from_init(struct su_initiator *from) {
93     char path[PATH_MAX], exe[PATH_MAX];
94     char args[4096], *argv0, *argv_rest;
95     int fd;
96     ssize_t len;
97     int i;
98     int err;
99
100     from->uid = getuid();
101     from->pid = getppid();
102
103     /* Get the command line */
104     snprintf(path, sizeof(path), "/proc/%u/cmdline", from->pid);
105     fd = open(path, O_RDONLY);
106     if (fd < 0) {
107         PLOGE("Opening command line");
108         return -1;
109     }
110     len = read(fd, args, sizeof(args));
111     err = errno;
112     close(fd);
113     if (len < 0 || len == sizeof(args)) {
114         PLOGEV("Reading command line", err);
115         return -1;
116     }
117
118     argv0 = args;
119     argv_rest = NULL;
120     for (i = 0; i < len; i++) {
121         if (args[i] == '\0') {
122             if (!argv_rest) {
123                 argv_rest = &args[i+1];
124             } else {
125                 args[i] = ' ';
126             }
127         }
128     }
129     args[len] = '\0';
130
131     if (argv_rest) {
132         strncpy(from->args, argv_rest, sizeof(from->args));
133         from->args[sizeof(from->args)-1] = '\0';
134     } else {
135         from->args[0] = '\0';
136     }
137
138     /* If this isn't app_process, use the real path instead of argv[0] */
139     snprintf(path, sizeof(path), "/proc/%u/exe", from->pid);
140     len = readlink(path, exe, sizeof(exe));
141     if (len < 0) {
142         PLOGE("Getting exe path");
143         return -1;
144     }
145     exe[len] = '\0';
146     if (strcmp(exe, "/system/bin/app_process")) {
147         argv0 = exe;
148     }
149
150     strncpy(from->bin, argv0, sizeof(from->bin));
151     from->bin[sizeof(from->bin)-1] = '\0';
152
153     return 0;
154 }
155
156 static void read_options(struct su_context *ctx) {
157     char mode[12];
158     FILE *fp;
159     if ((fp = fopen(REQUESTOR_MULTIUSER_MODE, "r"))) {
160         fgets(mode, sizeof(mode), fp);
161         LOGD("multiuser mode: %s", mode);
162         if (strcmp(mode, "user\n") == 0) {
163             ctx->user.multiuser_mode = MULTIUSER_MODE_USER;
164         } else if (strcmp(mode, "owner\n") == 0) {
165             ctx->user.multiuser_mode = MULTIUSER_MODE_OWNER;
166         }
167         fclose(fp);
168     }
169 }
170
171 static void user_init(struct su_context *ctx) {
172     if (ctx->from.uid > 99999) {
173         ctx->user.android_user_id = ctx->from.uid / 100000;
174         if (ctx->user.multiuser_mode == MULTIUSER_MODE_USER) {
175             snprintf(ctx->user.database_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR_DATABASE_PATH);
176         }
177     }
178 }
179
180 static void populate_environment(const struct su_context *ctx) {
181     struct passwd *pw;
182
183     if (ctx->to.keepenv)
184         return;
185
186     pw = getpwuid(ctx->to.uid);
187     if (pw) {
188         setenv("HOME", pw->pw_dir, 1);
189         setenv("SHELL", ctx->to.shell, 1);
190         if (ctx->to.login || ctx->to.uid) {
191             setenv("USER", pw->pw_name, 1);
192             setenv("LOGNAME", pw->pw_name, 1);
193         }
194     }
195 }
196
197 void set_identity(unsigned int uid) {
198     /*
199      * Set effective uid back to root, otherwise setres[ug]id will fail
200      * if uid isn't root.
201      */
202     if (seteuid(0)) {
203         PLOGE("seteuid (root)");
204         exit(EXIT_FAILURE);
205     }
206     if (setresgid(uid, uid, uid)) {
207         PLOGE("setresgid (%u)", uid);
208         exit(EXIT_FAILURE);
209     }
210     if (setresuid(uid, uid, uid)) {
211         PLOGE("setresuid (%u)", uid);
212         exit(EXIT_FAILURE);
213     }
214 }
215
216 static void socket_cleanup(struct su_context *ctx) {
217     if (ctx && ctx->sock_path[0]) {
218         if (unlink(ctx->sock_path))
219             PLOGE("unlink (%s)", ctx->sock_path);
220         ctx->sock_path[0] = 0;
221     }
222 }
223
224 /*
225  * For use in signal handlers/atexit-function
226  * NOTE: su_ctx points to main's local variable.
227  *       It's OK due to the program uses exit(3), not return from main()
228  */
229 static struct su_context *su_ctx = NULL;
230
231 static void cleanup(void) {
232     socket_cleanup(su_ctx);
233 }
234
235 static void cleanup_signal(int sig) {
236     socket_cleanup(su_ctx);
237     exit(128 + sig);
238 }
239
240 void sigchld_handler(int sig) {
241     child_cleanup(su_ctx);
242     (void)sig;
243 }
244
245 static int socket_create_temp(char *path, size_t len) {
246     int fd;
247     struct sockaddr_un sun;
248
249     fd = socket(AF_LOCAL, SOCK_STREAM, 0);
250     if (fd < 0) {
251         PLOGE("socket");
252         return -1;
253     }
254     if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
255         PLOGE("fcntl FD_CLOEXEC");
256         goto err;
257     }
258
259     memset(&sun, 0, sizeof(sun));
260     sun.sun_family = AF_LOCAL;
261     snprintf(path, len, "%s/.socket%d", REQUESTOR_CACHE_PATH, getpid());
262     memset(sun.sun_path, 0, sizeof(sun.sun_path));
263     snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
264
265     /*
266      * Delete the socket to protect from situations when
267      * something bad occured previously and the kernel reused pid from that process.
268      * Small probability, isn't it.
269      */
270     unlink(sun.sun_path);
271
272     if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
273         PLOGE("bind");
274         goto err;
275     }
276
277     if (listen(fd, 1) < 0) {
278         PLOGE("listen");
279         goto err;
280     }
281
282     return fd;
283 err:
284     close(fd);
285     return -1;
286 }
287
288 static int socket_accept(int serv_fd) {
289     struct timeval tv;
290     fd_set fds;
291     int fd, rc;
292
293     /* Wait 20 seconds for a connection, then give up. */
294     tv.tv_sec = 20;
295     tv.tv_usec = 0;
296     FD_ZERO(&fds);
297     FD_SET(serv_fd, &fds);
298     LOGD("select");
299     do {
300         rc = select(serv_fd + 1, &fds, NULL, NULL, &tv);
301     } while (rc < 0 && errno == EINTR);
302     if (rc < 1) {
303         PLOGE("select");
304         return -1;
305     }
306
307     fd = accept(serv_fd, NULL, NULL);
308     if (fd < 0) {
309         PLOGE("accept");
310         return -1;
311     }
312
313     return fd;
314 }
315
316 static int socket_send_request(int fd, const struct su_context *ctx) {
317     size_t len;
318     size_t bin_size, cmd_size;
319     char *cmd;
320
321 #define write_token(fd, data)                \
322 do {                            \
323     uint32_t __data = htonl(data);            \
324     size_t __count = sizeof(__data);        \
325     size_t __len = write((fd), &__data, __count);    \
326     if (__len != __count) {                \
327         PLOGE("write(" #data ")");        \
328         return -1;                \
329     }                        \
330 } while (0)
331
332     write_token(fd, PROTO_VERSION);
333     write_token(fd, PATH_MAX);
334     write_token(fd, ARG_MAX);
335     write_token(fd, ctx->from.uid);
336     write_token(fd, ctx->to.uid);
337     bin_size = strlen(ctx->from.bin) + 1;
338     write_token(fd, bin_size);
339     len = write(fd, ctx->from.bin, bin_size);
340     if (len != bin_size) {
341         PLOGE("write(bin)");
342         return -1;
343     }
344     cmd = get_command(&ctx->to);
345     cmd_size = strlen(cmd) + 1;
346     write_token(fd, cmd_size);
347     len = write(fd, cmd, cmd_size);
348     if (len != cmd_size) {
349         PLOGE("write(cmd)");
350         return -1;
351     }
352     return 0;
353 }
354
355 static int socket_receive_result(int fd, char *result, ssize_t result_len) {
356     ssize_t len;
357     
358     LOGD("waiting for result");
359     len = read(fd, result, result_len-1);
360     if (len < 0) {
361         PLOGE("read(result)");
362         return -1;
363     }
364     result[len] = '\0';
365
366     return 0;
367 }
368
369 static void usage(int status) {
370     FILE *stream = (status == EXIT_SUCCESS) ? stdout : stderr;
371
372     fprintf(stream,
373     "Usage: su [options] [--] [-] [LOGIN] [--] [args...]\n\n"
374     "Options:\n"
375     "  -c, --command COMMAND         pass COMMAND to the invoked shell\n"
376     "  -h, --help                    display this help message and exit\n"
377     "  -, -l, --login                pretend the shell to be a login shell\n"
378     "  -m, -p,\n"
379     "  --preserve-environment        do not change environment variables\n"
380     "  -s, --shell SHELL             use SHELL instead of the default " DEFAULT_SHELL "\n"
381     "  -v, --version                 display version number and exit\n"
382     "  -V                            display version code and exit,\n"
383     "                                this is used almost exclusively by Superuser.apk\n");
384     exit(status);
385 }
386
387 static __attribute__ ((noreturn)) void deny(struct su_context *ctx) {
388     char *cmd = get_command(&ctx->to);
389
390     // No send to UI denied requests for shell and root users (they are in the log)
391     if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
392         send_result(ctx, DENY);
393     }
394     LOGW("request rejected (%u->%u %s)", ctx->from.uid, ctx->to.uid, cmd);
395     fprintf(stderr, "%s\n", strerror(EACCES));
396     exit(EXIT_FAILURE);
397 }
398
399 static __attribute__ ((noreturn)) void allow(struct su_context *ctx) {
400     char *arg0;
401     int argc, err;
402
403     umask(ctx->umask);
404     // No send to UI accepted requests for shell and root users (they are in the log)
405     if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
406         send_result(ctx, ALLOW);
407     }
408
409     arg0 = strrchr (ctx->to.shell, '/');
410     arg0 = (arg0) ? arg0 + 1 : ctx->to.shell;
411     if (ctx->to.login) {
412         int s = strlen(arg0) + 2;
413         char *p = malloc(s);
414
415         if (!p)
416             exit(EXIT_FAILURE);
417
418         *p = '-';
419         strcpy(p + 1, arg0);
420         arg0 = p;
421     }
422
423     populate_environment(ctx);
424     set_identity(ctx->to.uid);
425
426 #define PARG(arg)                                    \
427     (ctx->to.optind + (arg) < ctx->to.argc) ? " " : "",                    \
428     (ctx->to.optind + (arg) < ctx->to.argc) ? ctx->to.argv[ctx->to.optind + (arg)] : ""
429
430     LOGD("%u %s executing %u %s using shell %s : %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
431             ctx->from.uid, ctx->from.bin,
432             ctx->to.uid, get_command(&ctx->to), ctx->to.shell,
433             arg0, PARG(0), PARG(1), PARG(2), PARG(3), PARG(4), PARG(5),
434             (ctx->to.optind + 6 < ctx->to.argc) ? " ..." : "");
435
436     argc = ctx->to.optind;
437     if (ctx->to.command) {
438         ctx->to.argv[--argc] = ctx->to.command;
439         ctx->to.argv[--argc] = "-c";
440     }
441     ctx->to.argv[--argc] = arg0;
442     execv(ctx->to.shell, ctx->to.argv + argc);
443     err = errno;
444     PLOGE("exec");
445     fprintf(stderr, "Cannot execute %s: %s\n", ctx->to.shell, strerror(err));
446     exit(EXIT_FAILURE);
447 }
448
449 /*
450  * CyanogenMod-specific behavior
451  *
452  * we can't simply use the property service, since we aren't launched from init
453  * and can't trust the location of the property workspace.
454  * Find the properties ourselves.
455  */
456 int access_disabled(const struct su_initiator *from) {
457     char *data;
458     char build_type[PROPERTY_VALUE_MAX];
459     char debuggable[PROPERTY_VALUE_MAX], enabled[PROPERTY_VALUE_MAX];
460     size_t len;
461
462     data = read_file("/system/build.prop");
463     if (check_property(data, "ro.cm.version")) {
464         get_property(data, build_type, "ro.build.type", "");
465         free(data);
466
467         data = read_file("/default.prop");
468         get_property(data, debuggable, "ro.debuggable", "0");
469         free(data);
470         /* only allow su on debuggable builds */
471         if (strcmp("1", debuggable) != 0) {
472             LOGE("Root access is disabled on non-debug builds");
473             return 1;
474         }
475
476         data = read_file("/data/property/persist.sys.root_access");
477         if (data != NULL) {
478             len = strlen(data);
479             if (len >= PROPERTY_VALUE_MAX)
480                 memcpy(enabled, "1", 2);
481             else
482                 memcpy(enabled, data, len + 1);
483             free(data);
484         } else
485             memcpy(enabled, "1", 2);
486
487         /* enforce persist.sys.root_access on non-eng builds for apps */
488         if (strcmp("eng", build_type) != 0 &&
489                 from->uid != AID_SHELL && from->uid != AID_ROOT &&
490                 (atoi(enabled) & CM_ROOT_ACCESS_APPS_ONLY) != CM_ROOT_ACCESS_APPS_ONLY ) {
491             LOGE("Apps root access is disabled by system setting - "
492                  "enable it under settings -> developer options");
493             return 1;
494         }
495
496         /* disallow su in a shell if appropriate */
497         if (from->uid == AID_SHELL &&
498                 (atoi(enabled) & CM_ROOT_ACCESS_ADB_ONLY) != CM_ROOT_ACCESS_ADB_ONLY ) {
499             LOGE("Shell root access is disabled by a system setting - "
500                  "enable it under settings -> developer options");
501             return 1;
502         }
503         
504     }
505     return 0;
506 }
507
508 int main(int argc, char *argv[]) {
509     // Sanitize all secure environment variables (from linker_environ.c in AOSP linker).
510     /* The same list than GLibc at this point */
511     static const char* const unsec_vars[] = {
512         "GCONV_PATH",
513         "GETCONF_DIR",
514         "HOSTALIASES",
515         "LD_AUDIT",
516         "LD_DEBUG",
517         "LD_DEBUG_OUTPUT",
518         "LD_DYNAMIC_WEAK",
519         "LD_LIBRARY_PATH",
520         "LD_ORIGIN_PATH",
521         "LD_PRELOAD",
522         "LD_PROFILE",
523         "LD_SHOW_AUXV",
524         "LD_USE_LOAD_BIAS",
525         "LOCALDOMAIN",
526         "LOCPATH",
527         "MALLOC_TRACE",
528         "MALLOC_CHECK_",
529         "NIS_PATH",
530         "NLSPATH",
531         "RESOLV_HOST_CONF",
532         "RES_OPTIONS",
533         "TMPDIR",
534         "TZDIR",
535         "LD_AOUT_LIBRARY_PATH",
536         "LD_AOUT_PRELOAD",
537         // not listed in linker, used due to system() call
538         "IFS",
539     };
540     const char* const* cp   = unsec_vars;
541     const char* const* endp = cp + sizeof(unsec_vars)/sizeof(unsec_vars[0]);
542     while (cp < endp) {
543         unsetenv(*cp);
544         cp++;
545     }
546
547     /*
548      * set LD_LIBRARY_PATH if the linker has wiped out it due to we're suid.
549      * This occurs on Android 4.0+
550      */
551     setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 0);
552
553     LOGD("su invoked.");
554
555     struct su_context ctx = {
556         .from = {
557             .pid = -1,
558             .uid = 0,
559             .bin = "",
560             .args = "",
561         },
562         .to = {
563             .uid = AID_ROOT,
564             .login = 0,
565             .keepenv = 0,
566             .shell = DEFAULT_SHELL,
567             .command = NULL,
568             .argv = argv,
569             .argc = argc,
570             .optind = 0,
571         },
572         .user = {
573             .android_user_id = 0,
574             .multiuser_mode = MULTIUSER_MODE_OWNER_ONLY,
575             .database_path = REQUESTOR_DATA_PATH REQUESTOR_DATABASE_PATH,
576         },
577     };
578     struct stat st;
579     int c, socket_serv_fd, fd;
580     char buf[64], *result;
581     policy_t dballow;
582     struct option long_opts[] = {
583         { "command",            required_argument,    NULL, 'c' },
584         { "help",            no_argument,        NULL, 'h' },
585         { "login",            no_argument,        NULL, 'l' },
586         { "preserve-environment",    no_argument,        NULL, 'p' },
587         { "shell",            required_argument,    NULL, 's' },
588         { "version",            no_argument,        NULL, 'v' },
589         { NULL, 0, NULL, 0 },
590     };
591
592     while ((c = getopt_long(argc, argv, "+c:hlmps:Vv", long_opts, NULL)) != -1) {
593         switch(c) {
594         case 'c':
595             ctx.to.command = optarg;
596             break;
597         case 'h':
598             usage(EXIT_SUCCESS);
599             break;
600         case 'l':
601             ctx.to.login = 1;
602             break;
603         case 'm':
604         case 'p':
605             ctx.to.keepenv = 1;
606             break;
607         case 's':
608             ctx.to.shell = optarg;
609             break;
610         case 'V':
611             printf("%d\n", VERSION_CODE);
612             exit(EXIT_SUCCESS);
613         case 'v':
614             printf("%s\n", VERSION);
615             exit(EXIT_SUCCESS);
616         default:
617             /* Bionic getopt_long doesn't terminate its error output by newline */
618             fprintf(stderr, "\n");
619             usage(2);
620         }
621     }
622     if (optind < argc && !strcmp(argv[optind], "-")) {
623         ctx.to.login = 1;
624         optind++;
625     }
626     /* username or uid */
627     if (optind < argc && strcmp(argv[optind], "--")) {
628         struct passwd *pw;
629         pw = getpwnam(argv[optind]);
630         if (!pw) {
631             char *endptr;
632
633             /* It seems we shouldn't do this at all */
634             errno = 0;
635             ctx.to.uid = strtoul(argv[optind], &endptr, 10);
636             if (errno || *endptr) {
637                 LOGE("Unknown id: %s\n", argv[optind]);
638                 fprintf(stderr, "Unknown id: %s\n", argv[optind]);
639                 exit(EXIT_FAILURE);
640             }
641         } else {
642             ctx.to.uid = pw->pw_uid;
643         }
644         optind++;
645     }
646     if (optind < argc && !strcmp(argv[optind], "--")) {
647         optind++;
648     }
649     ctx.to.optind = optind;
650
651     su_ctx = &ctx;
652     if (from_init(&ctx.from) < 0) {
653         deny(&ctx);
654     }
655         
656     read_options(&ctx);
657     user_init(&ctx);
658     
659     if (ctx.user.multiuser_mode == MULTIUSER_MODE_OWNER_ONLY && ctx.user.android_user_id != 0) {
660         LOGD("multiuser mode: owner only");
661         deny(&ctx);
662     }
663
664     if (access_disabled(&ctx.from)) {
665         LOGD("access_disabled");
666         deny(&ctx);
667     }
668
669     ctx.umask = umask(027);
670
671     if (ctx.from.uid == AID_ROOT || ctx.from.uid == AID_SHELL)
672         allow(&ctx);
673
674     if (stat(ctx.user.database_path, &st) < 0) {
675         PLOGE("stat %s", ctx.user.database_path);
676         deny(&ctx);
677     }
678
679     if (st.st_gid != st.st_uid)
680     {
681         LOGE("Bad uid/gid %d/%d for Superuser Requestor application",
682                 (int)st.st_uid, (int)st.st_gid);
683         deny(&ctx);
684     }
685
686     int ret = mkdir(REQUESTOR_CACHE_PATH, 0770);
687     LOGD("mkdir: %d", ret);
688     if (chown(REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid)) {
689         PLOGE("chown (%s, %ld, %ld)", REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid);
690         deny(&ctx);
691     }
692
693     if (setgroups(0, NULL)) {
694         PLOGE("setgroups");
695         deny(&ctx);
696     }
697     if (setegid(st.st_gid)) {
698         PLOGE("setegid (%lu)", st.st_gid);
699         deny(&ctx);
700     }
701     if (seteuid(st.st_uid)) {
702         PLOGE("seteuid (%lu)", st.st_uid);
703         deny(&ctx);
704     }
705
706     dballow = database_check(&ctx);
707     switch (dballow) {
708         case INTERACTIVE:
709             break;
710         case ALLOW:
711             LOGD("db allowed");
712             allow(&ctx);    /* never returns */
713         case DENY:
714         default:
715             LOGD("db denied");
716             deny(&ctx);        /* never returns too */
717     }
718     
719     socket_serv_fd = socket_create_temp(ctx.sock_path, sizeof(ctx.sock_path));
720     LOGD(ctx.sock_path);
721     if (socket_serv_fd < 0) {
722         deny(&ctx);
723     }
724
725     signal(SIGHUP, cleanup_signal);
726     signal(SIGPIPE, cleanup_signal);
727     signal(SIGTERM, cleanup_signal);
728     signal(SIGQUIT, cleanup_signal);
729     signal(SIGINT, cleanup_signal);
730     signal(SIGABRT, cleanup_signal);
731
732     if (send_request(&ctx) < 0) {
733         deny(&ctx);
734     }
735
736     atexit(cleanup);
737
738     fd = socket_accept(socket_serv_fd);
739     if (fd < 0) {
740         deny(&ctx);
741     }
742     if (socket_send_request(fd, &ctx)) {
743         deny(&ctx);
744     }
745     if (socket_receive_result(fd, buf, sizeof(buf))) {
746         deny(&ctx);
747     }
748
749     close(fd);
750     close(socket_serv_fd);
751     socket_cleanup(&ctx);
752
753     result = buf;
754
755 #define SOCKET_RESPONSE    "socket:"
756     if (strncmp(result, SOCKET_RESPONSE, sizeof(SOCKET_RESPONSE) - 1))
757         LOGW("SECURITY RISK: Requestor still receives credentials in intent");
758     else
759         result += sizeof(SOCKET_RESPONSE) - 1;
760
761     if (!strcmp(result, "DENY")) {
762         deny(&ctx);
763     } else if (!strcmp(result, "ALLOW")) {
764         allow(&ctx);
765     } else {
766         LOGE("unknown response from Superuser Requestor: %s", result);
767         deny(&ctx);
768     }
769 }