OSDN Git Service

wip
[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     struct passwd *pw;
154     pw = getpwuid(from->uid);
155     if (pw && pw->pw_name) {
156         strncpy(from->name, pw->pw_name, sizeof(from->name));
157     }
158
159     return 0;
160 }
161
162 static void read_options(struct su_context *ctx) {
163     char mode[12];
164     FILE *fp;
165     if ((fp = fopen(REQUESTOR_MULTIUSER_MODE, "r"))) {
166         fgets(mode, sizeof(mode), fp);
167         LOGD("multiuser mode: %s", mode);
168         if (strcmp(mode, "user\n") == 0) {
169             ctx->user.multiuser_mode = MULTIUSER_MODE_USER;
170         } else if (strcmp(mode, "owner\n") == 0) {
171             ctx->user.multiuser_mode = MULTIUSER_MODE_OWNER;
172         }
173         fclose(fp);
174     }
175 }
176
177 static void user_init(struct su_context *ctx) {
178     if (ctx->from.uid > 99999) {
179         ctx->user.android_user_id = ctx->from.uid / 100000;
180         if (ctx->user.multiuser_mode == MULTIUSER_MODE_USER) {
181             snprintf(ctx->user.database_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR_DATABASE_PATH);
182             snprintf(ctx->user.base_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR);
183         }
184     }
185 }
186
187 static void populate_environment(const struct su_context *ctx) {
188     struct passwd *pw;
189
190     if (ctx->to.keepenv)
191         return;
192
193     pw = getpwuid(ctx->to.uid);
194     if (pw) {
195         setenv("HOME", pw->pw_dir, 1);
196         setenv("SHELL", ctx->to.shell, 1);
197         if (ctx->to.login || ctx->to.uid) {
198             setenv("USER", pw->pw_name, 1);
199             setenv("LOGNAME", pw->pw_name, 1);
200         }
201     }
202 }
203
204 void set_identity(unsigned int uid) {
205     /*
206      * Set effective uid back to root, otherwise setres[ug]id will fail
207      * if uid isn't root.
208      */
209     if (seteuid(0)) {
210         PLOGE("seteuid (root)");
211         exit(EXIT_FAILURE);
212     }
213     if (setresgid(uid, uid, uid)) {
214         PLOGE("setresgid (%u)", uid);
215         exit(EXIT_FAILURE);
216     }
217     if (setresuid(uid, uid, uid)) {
218         PLOGE("setresuid (%u)", uid);
219         exit(EXIT_FAILURE);
220     }
221 }
222
223 static void socket_cleanup(struct su_context *ctx) {
224     if (ctx && ctx->sock_path[0]) {
225         if (unlink(ctx->sock_path))
226             PLOGE("unlink (%s)", ctx->sock_path);
227         ctx->sock_path[0] = 0;
228     }
229 }
230
231 /*
232  * For use in signal handlers/atexit-function
233  * NOTE: su_ctx points to main's local variable.
234  *       It's OK due to the program uses exit(3), not return from main()
235  */
236 static struct su_context *su_ctx = NULL;
237
238 static void cleanup(void) {
239     socket_cleanup(su_ctx);
240 }
241
242 static void cleanup_signal(int sig) {
243     socket_cleanup(su_ctx);
244     exit(128 + sig);
245 }
246
247 void sigchld_handler(int sig) {
248     child_cleanup(su_ctx);
249     (void)sig;
250 }
251
252 static int socket_create_temp(char *path, size_t len) {
253     int fd;
254     struct sockaddr_un sun;
255
256     fd = socket(AF_LOCAL, SOCK_STREAM, 0);
257     if (fd < 0) {
258         PLOGE("socket");
259         return -1;
260     }
261     if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
262         PLOGE("fcntl FD_CLOEXEC");
263         goto err;
264     }
265
266     memset(&sun, 0, sizeof(sun));
267     sun.sun_family = AF_LOCAL;
268     snprintf(path, len, "%s/.socket%d", REQUESTOR_CACHE_PATH, getpid());
269     memset(sun.sun_path, 0, sizeof(sun.sun_path));
270     snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
271
272     /*
273      * Delete the socket to protect from situations when
274      * something bad occured previously and the kernel reused pid from that process.
275      * Small probability, isn't it.
276      */
277     unlink(sun.sun_path);
278
279     if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
280         PLOGE("bind");
281         goto err;
282     }
283
284     if (listen(fd, 1) < 0) {
285         PLOGE("listen");
286         goto err;
287     }
288
289     return fd;
290 err:
291     close(fd);
292     return -1;
293 }
294
295 static int socket_accept(int serv_fd) {
296     struct timeval tv;
297     fd_set fds;
298     int fd, rc;
299
300     /* Wait 20 seconds for a connection, then give up. */
301     tv.tv_sec = 20;
302     tv.tv_usec = 0;
303     FD_ZERO(&fds);
304     FD_SET(serv_fd, &fds);
305     LOGD("select");
306     do {
307         rc = select(serv_fd + 1, &fds, NULL, NULL, &tv);
308     } while (rc < 0 && errno == EINTR);
309     if (rc < 1) {
310         PLOGE("select");
311         return -1;
312     }
313
314     fd = accept(serv_fd, NULL, NULL);
315     if (fd < 0) {
316         PLOGE("accept");
317         return -1;
318     }
319
320     return fd;
321 }
322
323 static int socket_send_request(int fd, const struct su_context *ctx) {
324 #define write_data(fd, data, data_len)              \
325 do {                                                \
326     size_t __len = htonl(data_len);                 \
327     __len = write((fd), &__len, sizeof(__len));     \
328     if (__len != sizeof(__len)) {                   \
329         PLOGE("write(" #data ")");                  \
330         return -1;                                  \
331     }                                               \
332     __len = write((fd), data, data_len);            \
333     if (__len != data_len) {                        \
334         PLOGE("write(" #data ")");                  \
335         return -1;                                  \
336     }                                               \
337 } while (0)
338
339 #define write_string(fd, name, data)        \
340 do {                                        \
341     write_data(fd, name, strlen(name));     \
342     write_data(fd, data, strlen(data));     \
343 } while (0)
344
345 // stringify everything.
346 #define write_token(fd, name, data)         \
347 do {                                        \
348     char buf[16];                           \
349     snprintf(buf, sizeof(buf), "%d", data); \
350     write_string(fd, name, buf);            \
351 } while (0)
352
353     write_token(fd, "version", PROTO_VERSION);
354     write_token(fd, "from.name", ctx->from.name);
355     write_token(fd, "to.name", ctx->to.name);
356     write_token(fd, "from.uid", ctx->from.uid);
357     write_token(fd, "to.uid", ctx->to.uid);
358     write_string(fd, "from.bin", ctx->from.bin);
359     write_string(fd, "command", get_command(&ctx->to));
360     write_token(fd, "eof", PROTO_VERSION);
361     return 0;
362 }
363
364 static int socket_receive_result(int fd, char *result, ssize_t result_len) {
365     ssize_t len;
366     
367     LOGD("waiting for user");
368     len = read(fd, result, result_len-1);
369     if (len < 0) {
370         PLOGE("read(result)");
371         return -1;
372     }
373     result[len] = '\0';
374
375     return 0;
376 }
377
378 static void usage(int status) {
379     FILE *stream = (status == EXIT_SUCCESS) ? stdout : stderr;
380
381     fprintf(stream,
382     "Usage: su [options] [--] [-] [LOGIN] [--] [args...]\n\n"
383     "Options:\n"
384     "  -c, --command COMMAND         pass COMMAND to the invoked shell\n"
385     "  -h, --help                    display this help message and exit\n"
386     "  -, -l, --login                pretend the shell to be a login shell\n"
387     "  -m, -p,\n"
388     "  --preserve-environment        do not change environment variables\n"
389     "  -s, --shell SHELL             use SHELL instead of the default " DEFAULT_SHELL "\n"
390     "  -v, --version                 display version number and exit\n"
391     "  -V                            display version code and exit,\n"
392     "                                this is used almost exclusively by Superuser.apk\n");
393     exit(status);
394 }
395
396 static __attribute__ ((noreturn)) void deny(struct su_context *ctx) {
397     char *cmd = get_command(&ctx->to);
398
399     // No send to UI denied requests for shell and root users (they are in the log)
400     // if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
401         send_result(ctx, DENY);
402     // }
403     LOGW("request rejected (%u->%u %s)", ctx->from.uid, ctx->to.uid, cmd);
404     fprintf(stderr, "%s\n", strerror(EACCES));
405     exit(EXIT_FAILURE);
406 }
407
408 static __attribute__ ((noreturn)) void allow(struct su_context *ctx) {
409     char *arg0;
410     int argc, err;
411
412     umask(ctx->umask);
413     // No send to UI accepted requests for shell and root users (they are in the log)
414     // if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
415         send_result(ctx, ALLOW);
416     // }
417
418     arg0 = strrchr (ctx->to.shell, '/');
419     arg0 = (arg0) ? arg0 + 1 : ctx->to.shell;
420     if (ctx->to.login) {
421         int s = strlen(arg0) + 2;
422         char *p = malloc(s);
423
424         if (!p)
425             exit(EXIT_FAILURE);
426
427         *p = '-';
428         strcpy(p + 1, arg0);
429         arg0 = p;
430     }
431
432     populate_environment(ctx);
433     set_identity(ctx->to.uid);
434
435 #define PARG(arg)                                    \
436     (ctx->to.optind + (arg) < ctx->to.argc) ? " " : "",                    \
437     (ctx->to.optind + (arg) < ctx->to.argc) ? ctx->to.argv[ctx->to.optind + (arg)] : ""
438
439     LOGD("%u %s executing %u %s using shell %s : %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
440             ctx->from.uid, ctx->from.bin,
441             ctx->to.uid, get_command(&ctx->to), ctx->to.shell,
442             arg0, PARG(0), PARG(1), PARG(2), PARG(3), PARG(4), PARG(5),
443             (ctx->to.optind + 6 < ctx->to.argc) ? " ..." : "");
444
445     argc = ctx->to.optind;
446     if (ctx->to.command) {
447         ctx->to.argv[--argc] = ctx->to.command;
448         ctx->to.argv[--argc] = "-c";
449     }
450     ctx->to.argv[--argc] = arg0;
451     execv(ctx->to.shell, ctx->to.argv + argc);
452     err = errno;
453     PLOGE("exec");
454     fprintf(stderr, "Cannot execute %s: %s\n", ctx->to.shell, strerror(err));
455     exit(EXIT_FAILURE);
456 }
457
458 /*
459  * CyanogenMod-specific behavior
460  *
461  * we can't simply use the property service, since we aren't launched from init
462  * and can't trust the location of the property workspace.
463  * Find the properties ourselves.
464  */
465 int access_disabled(const struct su_initiator *from) {
466     char *data;
467     char build_type[PROPERTY_VALUE_MAX];
468     char debuggable[PROPERTY_VALUE_MAX], enabled[PROPERTY_VALUE_MAX];
469     size_t len;
470
471     data = read_file("/system/build.prop");
472     if (check_property(data, "ro.cm.version")) {
473         get_property(data, build_type, "ro.build.type", "");
474         free(data);
475
476         data = read_file("/default.prop");
477         get_property(data, debuggable, "ro.debuggable", "0");
478         free(data);
479         /* only allow su on debuggable builds */
480         if (strcmp("1", debuggable) != 0) {
481             LOGE("Root access is disabled on non-debug builds");
482             return 1;
483         }
484
485         data = read_file("/data/property/persist.sys.root_access");
486         if (data != NULL) {
487             len = strlen(data);
488             if (len >= PROPERTY_VALUE_MAX)
489                 memcpy(enabled, "1", 2);
490             else
491                 memcpy(enabled, data, len + 1);
492             free(data);
493         } else
494             memcpy(enabled, "1", 2);
495
496         /* enforce persist.sys.root_access on non-eng builds for apps */
497         if (strcmp("eng", build_type) != 0 &&
498                 from->uid != AID_SHELL && from->uid != AID_ROOT &&
499                 (atoi(enabled) & CM_ROOT_ACCESS_APPS_ONLY) != CM_ROOT_ACCESS_APPS_ONLY ) {
500             LOGE("Apps root access is disabled by system setting - "
501                  "enable it under settings -> developer options");
502             return 1;
503         }
504
505         /* disallow su in a shell if appropriate */
506         if (from->uid == AID_SHELL &&
507                 (atoi(enabled) & CM_ROOT_ACCESS_ADB_ONLY) != CM_ROOT_ACCESS_ADB_ONLY ) {
508             LOGE("Shell root access is disabled by a system setting - "
509                  "enable it under settings -> developer options");
510             return 1;
511         }
512         
513     }
514     return 0;
515 }
516
517 int main(int argc, char *argv[]) {
518     // Sanitize all secure environment variables (from linker_environ.c in AOSP linker).
519     /* The same list than GLibc at this point */
520     static const char* const unsec_vars[] = {
521         "GCONV_PATH",
522         "GETCONF_DIR",
523         "HOSTALIASES",
524         "LD_AUDIT",
525         "LD_DEBUG",
526         "LD_DEBUG_OUTPUT",
527         "LD_DYNAMIC_WEAK",
528         "LD_LIBRARY_PATH",
529         "LD_ORIGIN_PATH",
530         "LD_PRELOAD",
531         "LD_PROFILE",
532         "LD_SHOW_AUXV",
533         "LD_USE_LOAD_BIAS",
534         "LOCALDOMAIN",
535         "LOCPATH",
536         "MALLOC_TRACE",
537         "MALLOC_CHECK_",
538         "NIS_PATH",
539         "NLSPATH",
540         "RESOLV_HOST_CONF",
541         "RES_OPTIONS",
542         "TMPDIR",
543         "TZDIR",
544         "LD_AOUT_LIBRARY_PATH",
545         "LD_AOUT_PRELOAD",
546         // not listed in linker, used due to system() call
547         "IFS",
548     };
549     const char* const* cp   = unsec_vars;
550     const char* const* endp = cp + sizeof(unsec_vars)/sizeof(unsec_vars[0]);
551     while (cp < endp) {
552         unsetenv(*cp);
553         cp++;
554     }
555
556     /*
557      * set LD_LIBRARY_PATH if the linker has wiped out it due to we're suid.
558      * This occurs on Android 4.0+
559      */
560     setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 0);
561
562     LOGD("su invoked.");
563
564     struct su_context ctx = {
565         .from = {
566             .pid = -1,
567             .uid = 0,
568             .bin = "",
569             .args = "",
570             .name = "",
571         },
572         .to = {
573             .uid = AID_ROOT,
574             .login = 0,
575             .keepenv = 0,
576             .shell = DEFAULT_SHELL,
577             .command = NULL,
578             .argv = argv,
579             .argc = argc,
580             .optind = 0,
581             .name = "",
582         },
583         .user = {
584             .android_user_id = 0,
585             .multiuser_mode = MULTIUSER_MODE_OWNER_ONLY,
586             .database_path = REQUESTOR_DATA_PATH REQUESTOR_DATABASE_PATH,
587             .base_path = REQUESTOR_DATA_PATH REQUESTOR
588         },
589     };
590     struct stat st;
591     int c, socket_serv_fd, fd;
592     char buf[64], *result;
593     policy_t dballow;
594     struct option long_opts[] = {
595         { "command",            required_argument,    NULL, 'c' },
596         { "help",            no_argument,        NULL, 'h' },
597         { "login",            no_argument,        NULL, 'l' },
598         { "preserve-environment",    no_argument,        NULL, 'p' },
599         { "shell",            required_argument,    NULL, 's' },
600         { "version",            no_argument,        NULL, 'v' },
601         { NULL, 0, NULL, 0 },
602     };
603
604     while ((c = getopt_long(argc, argv, "+c:hlmps:Vv", long_opts, NULL)) != -1) {
605         switch(c) {
606         case 'c':
607             ctx.to.command = optarg;
608             break;
609         case 'h':
610             usage(EXIT_SUCCESS);
611             break;
612         case 'l':
613             ctx.to.login = 1;
614             break;
615         case 'm':
616         case 'p':
617             ctx.to.keepenv = 1;
618             break;
619         case 's':
620             ctx.to.shell = optarg;
621             break;
622         case 'V':
623             printf("%d\n", VERSION_CODE);
624             exit(EXIT_SUCCESS);
625         case 'v':
626             printf("%s\n", VERSION);
627             exit(EXIT_SUCCESS);
628         default:
629             /* Bionic getopt_long doesn't terminate its error output by newline */
630             fprintf(stderr, "\n");
631             usage(2);
632         }
633     }
634     if (optind < argc && !strcmp(argv[optind], "-")) {
635         ctx.to.login = 1;
636         optind++;
637     }
638     /* username or uid */
639     if (optind < argc && strcmp(argv[optind], "--")) {
640         struct passwd *pw;
641         pw = getpwnam(argv[optind]);
642         if (!pw) {
643             char *endptr;
644
645             /* It seems we shouldn't do this at all */
646             errno = 0;
647             ctx.to.uid = strtoul(argv[optind], &endptr, 10);
648             if (errno || *endptr) {
649                 LOGE("Unknown id: %s\n", argv[optind]);
650                 fprintf(stderr, "Unknown id: %s\n", argv[optind]);
651                 exit(EXIT_FAILURE);
652             }
653         } else {
654             ctx.to.uid = pw->pw_uid;
655             if (pw->pw_name)
656                 strncpy(ctx.to.name, pw->pw_name, sizeof(ctx.to.name));
657         }
658         optind++;
659     }
660     if (optind < argc && !strcmp(argv[optind], "--")) {
661         optind++;
662     }
663     ctx.to.optind = optind;
664
665     su_ctx = &ctx;
666     if (from_init(&ctx.from) < 0) {
667         deny(&ctx);
668     }
669         
670     read_options(&ctx);
671     user_init(&ctx);
672     
673     if (ctx.user.multiuser_mode == MULTIUSER_MODE_OWNER_ONLY && ctx.user.android_user_id != 0) {
674         LOGD("multiuser mode: owner only");
675         deny(&ctx);
676     }
677
678     if (access_disabled(&ctx.from)) {
679         LOGD("access_disabled");
680         deny(&ctx);
681     }
682
683     ctx.umask = umask(027);
684
685     if (ctx.from.uid == AID_ROOT || ctx.from.uid == AID_SHELL) {
686         LOGD("Allowing root/shell.");
687         allow(&ctx);
688     }
689
690     if (stat(ctx.user.base_path, &st) < 0) {
691         PLOGE("stat %s", ctx.user.base_path);
692         deny(&ctx);
693     }
694
695     if (st.st_gid != st.st_uid) {
696         LOGE("Bad uid/gid %d/%d for Superuser Requestor application",
697                 (int)st.st_uid, (int)st.st_gid);
698         deny(&ctx);
699     }
700
701     int ret = mkdir(REQUESTOR_CACHE_PATH, 0770);
702     LOGD("mkdir: %d", ret);
703     if (chown(REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid)) {
704         PLOGE("chown (%s, %ld, %ld)", REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid);
705         deny(&ctx);
706     }
707
708     if (setgroups(0, NULL)) {
709         PLOGE("setgroups");
710         deny(&ctx);
711     }
712     if (setegid(st.st_gid)) {
713         PLOGE("setegid (%lu)", st.st_gid);
714         deny(&ctx);
715     }
716     if (seteuid(st.st_uid)) {
717         PLOGE("seteuid (%lu)", st.st_uid);
718         deny(&ctx);
719     }
720
721     dballow = database_check(&ctx);
722     switch (dballow) {
723         case INTERACTIVE:
724             break;
725         case ALLOW:
726             LOGD("db allowed");
727             allow(&ctx);    /* never returns */
728         case DENY:
729         default:
730             LOGD("db denied");
731             deny(&ctx);        /* never returns too */
732     }
733     
734     socket_serv_fd = socket_create_temp(ctx.sock_path, sizeof(ctx.sock_path));
735     LOGD(ctx.sock_path);
736     if (socket_serv_fd < 0) {
737         deny(&ctx);
738     }
739
740     signal(SIGHUP, cleanup_signal);
741     signal(SIGPIPE, cleanup_signal);
742     signal(SIGTERM, cleanup_signal);
743     signal(SIGQUIT, cleanup_signal);
744     signal(SIGINT, cleanup_signal);
745     signal(SIGABRT, cleanup_signal);
746
747     if (send_request(&ctx) < 0) {
748         deny(&ctx);
749     }
750
751     atexit(cleanup);
752
753     fd = socket_accept(socket_serv_fd);
754     if (fd < 0) {
755         deny(&ctx);
756     }
757     if (socket_send_request(fd, &ctx)) {
758         deny(&ctx);
759     }
760     if (socket_receive_result(fd, buf, sizeof(buf))) {
761         deny(&ctx);
762     }
763
764     close(fd);
765     close(socket_serv_fd);
766     socket_cleanup(&ctx);
767
768     result = buf;
769
770 #define SOCKET_RESPONSE    "socket:"
771     if (strncmp(result, SOCKET_RESPONSE, sizeof(SOCKET_RESPONSE) - 1))
772         LOGW("SECURITY RISK: Requestor still receives credentials in intent");
773     else
774         result += sizeof(SOCKET_RESPONSE) - 1;
775
776     if (!strcmp(result, "DENY")) {
777         deny(&ctx);
778     } else if (!strcmp(result, "ALLOW")) {
779         allow(&ctx);
780     } else {
781         LOGE("unknown response from Superuser Requestor: %s", result);
782         deny(&ctx);
783     }
784 }