OSDN Git Service

08d84b9eb94da3c35fea3e8b47df92f51d24233b
[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/uio.h>
21 #include <sys/un.h>
22 #include <sys/wait.h>
23 #include <sys/select.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <getopt.h>
31 #include <stdint.h>
32 #include <pwd.h>
33 #include <sys/stat.h>
34 #include <stdarg.h>
35 #include <sys/types.h>
36
37 #include "su.h"
38 #include "utils.h"
39
40 extern int is_daemon;
41 extern int daemon_from_uid;
42 extern int daemon_from_pid;
43
44 unsigned get_shell_uid() {
45   struct passwd* ppwd = getpwnam("shell");
46   if (NULL == ppwd) {
47     return 2000;
48   }
49
50   return ppwd->pw_uid;
51 }
52
53 unsigned get_system_uid() {
54   struct passwd* ppwd = getpwnam("system");
55   if (NULL == ppwd) {
56     return 1000;
57   }
58
59   return ppwd->pw_uid;
60 }
61
62 unsigned get_radio_uid() {
63   struct passwd* ppwd = getpwnam("radio");
64   if (NULL == ppwd) {
65     return 1001;
66   }
67
68   return ppwd->pw_uid;
69 }
70
71 int fork_zero_fucks() {
72     int pid = fork();
73     if (pid) {
74         int status;
75         waitpid(pid, &status, 0);
76         return pid;
77     }
78     else {
79         if ((pid = fork()))
80             exit(0);
81         return 0;
82     }
83 }
84
85 void exec_log(int priority, const char* fmt, ...) {
86     static int log_fd = -1;
87     struct iovec vec[3];
88     va_list args;
89     char msg[PATH_MAX];
90
91     if (log_fd < 0) {
92         log_fd = open("/dev/log/main", O_WRONLY);
93         if (log_fd < 0) {
94             return;
95         }
96     }
97
98     va_start(args, fmt);
99     vsnprintf(msg, PATH_MAX, fmt, args);
100     va_end(args);
101
102     vec[0].iov_base   = (unsigned char *) &priority;
103     vec[0].iov_len    = 1;
104     vec[1].iov_base   = (void *) LOG_TAG;
105     vec[1].iov_len    = strlen(LOG_TAG) + 1;
106     vec[2].iov_base   = (void *) msg;
107     vec[2].iov_len    = strlen(msg) + 1;
108
109     writev(log_fd, vec, 3);
110 }
111
112 static int from_init(struct su_initiator *from) {
113     char path[PATH_MAX], exe[PATH_MAX];
114     char args[4096], *argv0, *argv_rest;
115     int fd;
116     ssize_t len;
117     int i;
118     int err;
119
120     from->uid = getuid();
121     from->pid = getppid();
122
123     if (is_daemon) {
124         from->uid = daemon_from_uid;
125         from->pid = daemon_from_pid;
126     }
127
128     /* Get the command line */
129     snprintf(path, sizeof(path), "/proc/%u/cmdline", from->pid);
130     fd = open(path, O_RDONLY);
131     if (fd < 0) {
132         PLOGE("Opening command line");
133         return -1;
134     }
135     len = read(fd, args, sizeof(args));
136     err = errno;
137     close(fd);
138     if (len < 0 || len == sizeof(args)) {
139         PLOGEV("Reading command line", err);
140         return -1;
141     }
142
143     argv0 = args;
144     argv_rest = NULL;
145     for (i = 0; i < len; i++) {
146         if (args[i] == '\0') {
147             if (!argv_rest) {
148                 argv_rest = &args[i+1];
149             } else {
150                 args[i] = ' ';
151             }
152         }
153     }
154     args[len] = '\0';
155
156     if (argv_rest) {
157         strncpy(from->args, argv_rest, sizeof(from->args));
158         from->args[sizeof(from->args)-1] = '\0';
159     } else {
160         from->args[0] = '\0';
161     }
162
163     /* If this isn't app_process, use the real path instead of argv[0] */
164     snprintf(path, sizeof(path), "/proc/%u/exe", from->pid);
165     len = readlink(path, exe, sizeof(exe));
166     if (len < 0) {
167         PLOGE("Getting exe path");
168         return -1;
169     }
170     exe[len] = '\0';
171     if (strcmp(exe, "/system/bin/app_process")) {
172         argv0 = exe;
173     }
174
175     strncpy(from->bin, argv0, sizeof(from->bin));
176     from->bin[sizeof(from->bin)-1] = '\0';
177
178     struct passwd *pw;
179     pw = getpwuid(from->uid);
180     if (pw && pw->pw_name) {
181         strncpy(from->name, pw->pw_name, sizeof(from->name));
182     }
183
184     return 0;
185 }
186
187 static int get_multiuser_mode() {
188     char *data;
189     char sdk_ver[PROPERTY_VALUE_MAX];
190
191     data = read_file("/system/build.prop");
192     get_property(data, sdk_ver, "ro.build.version.sdk", "0");
193     free(data);
194
195     int sdk = atoi(sdk_ver);
196     if (sdk < 17)
197         return MULTIUSER_MODE_NONE;
198
199     int ret = MULTIUSER_MODE_OWNER_ONLY;
200     char mode[12];
201     FILE *fp;
202     if ((fp = fopen(REQUESTOR_MULTIUSER_MODE, "r"))) {
203         fgets(mode, sizeof(mode), fp);
204         int last = strlen(mode) - 1;
205         if (mode[last] == '\n')
206             mode[last] = '\0';
207         if (strcmp(mode, MULTIUSER_VALUE_USER) == 0) {
208             ret = MULTIUSER_MODE_USER;
209         } else if (strcmp(mode, MULTIUSER_VALUE_OWNER_MANAGED) == 0) {
210             ret = MULTIUSER_MODE_OWNER_MANAGED;
211         }
212         else {
213             ret = MULTIUSER_MODE_OWNER_ONLY;
214         }
215         fclose(fp);
216     }
217     return ret;
218 }
219
220 static void read_options(struct su_context *ctx) {
221     ctx->user.multiuser_mode = get_multiuser_mode();
222 }
223
224 static void user_init(struct su_context *ctx) {
225     if (ctx->from.uid > 99999) {
226         ctx->user.android_user_id = ctx->from.uid / 100000;
227         if (ctx->user.multiuser_mode == MULTIUSER_MODE_USER) {
228             snprintf(ctx->user.database_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR_DATABASE_PATH);
229             snprintf(ctx->user.base_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR);
230         }
231     }
232 }
233
234 static void populate_environment(const struct su_context *ctx) {
235     struct passwd *pw;
236
237     if (ctx->to.keepenv)
238         return;
239
240     pw = getpwuid(ctx->to.uid);
241     if (pw) {
242         setenv("HOME", pw->pw_dir, 1);
243         if (ctx->to.shell)
244             setenv("SHELL", ctx->to.shell, 1);
245         else
246             setenv("SHELL", DEFAULT_SHELL, 1);
247         if (ctx->to.login || ctx->to.uid) {
248             setenv("USER", pw->pw_name, 1);
249             setenv("LOGNAME", pw->pw_name, 1);
250         }
251     }
252 }
253
254 void set_identity(unsigned int uid) {
255     /*
256      * Set effective uid back to root, otherwise setres[ug]id will fail
257      * if uid isn't root.
258      */
259     if (seteuid(0)) {
260         PLOGE("seteuid (root)");
261         exit(EXIT_FAILURE);
262     }
263     if (setresgid(uid, uid, uid)) {
264         PLOGE("setresgid (%u)", uid);
265         exit(EXIT_FAILURE);
266     }
267     if (setresuid(uid, uid, uid)) {
268         PLOGE("setresuid (%u)", uid);
269         exit(EXIT_FAILURE);
270     }
271 }
272
273 static void socket_cleanup(struct su_context *ctx) {
274     if (ctx && ctx->sock_path[0]) {
275         if (unlink(ctx->sock_path))
276             PLOGE("unlink (%s)", ctx->sock_path);
277         ctx->sock_path[0] = 0;
278     }
279 }
280
281 /*
282  * For use in signal handlers/atexit-function
283  * NOTE: su_ctx points to main's local variable.
284  *       It's OK due to the program uses exit(3), not return from main()
285  */
286 static struct su_context *su_ctx = NULL;
287
288 static void cleanup(void) {
289     socket_cleanup(su_ctx);
290 }
291
292 static void cleanup_signal(int sig) {
293     socket_cleanup(su_ctx);
294     exit(128 + sig);
295 }
296
297 static int socket_create_temp(char *path, size_t len) {
298     int fd;
299     struct sockaddr_un sun;
300
301     fd = socket(AF_LOCAL, SOCK_STREAM, 0);
302     if (fd < 0) {
303         PLOGE("socket");
304         return -1;
305     }
306     if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
307         PLOGE("fcntl FD_CLOEXEC");
308         goto err;
309     }
310
311     memset(&sun, 0, sizeof(sun));
312     sun.sun_family = AF_LOCAL;
313     snprintf(path, len, "%s/.socket%d", REQUESTOR_CACHE_PATH, getpid());
314     memset(sun.sun_path, 0, sizeof(sun.sun_path));
315     snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
316
317     /*
318      * Delete the socket to protect from situations when
319      * something bad occured previously and the kernel reused pid from that process.
320      * Small probability, isn't it.
321      */
322     unlink(sun.sun_path);
323
324     if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
325         PLOGE("bind");
326         goto err;
327     }
328
329     if (listen(fd, 1) < 0) {
330         PLOGE("listen");
331         goto err;
332     }
333
334     return fd;
335 err:
336     close(fd);
337     return -1;
338 }
339
340 static int socket_accept(int serv_fd) {
341     struct timeval tv;
342     fd_set fds;
343     int fd, rc;
344
345     /* Wait 20 seconds for a connection, then give up. */
346     tv.tv_sec = 20;
347     tv.tv_usec = 0;
348     FD_ZERO(&fds);
349     FD_SET(serv_fd, &fds);
350     do {
351         rc = select(serv_fd + 1, &fds, NULL, NULL, &tv);
352     } while (rc < 0 && errno == EINTR);
353     if (rc < 1) {
354         PLOGE("select");
355         return -1;
356     }
357
358     fd = accept(serv_fd, NULL, NULL);
359     if (fd < 0) {
360         PLOGE("accept");
361         return -1;
362     }
363
364     return fd;
365 }
366
367 static int socket_send_request(int fd, const struct su_context *ctx) {
368 #define write_data(fd, data, data_len)              \
369 do {                                                \
370     size_t __len = htonl(data_len);                 \
371     __len = write((fd), &__len, sizeof(__len));     \
372     if (__len != sizeof(__len)) {                   \
373         PLOGE("write(" #data ")");                  \
374         return -1;                                  \
375     }                                               \
376     __len = write((fd), data, data_len);            \
377     if (__len != data_len) {                        \
378         PLOGE("write(" #data ")");                  \
379         return -1;                                  \
380     }                                               \
381 } while (0)
382
383 #define write_string_data(fd, name, data)        \
384 do {                                        \
385     write_data(fd, name, strlen(name));     \
386     write_data(fd, data, strlen(data));     \
387 } while (0)
388
389 // stringify everything.
390 #define write_token(fd, name, data)         \
391 do {                                        \
392     char buf[16];                           \
393     snprintf(buf, sizeof(buf), "%d", data); \
394     write_string_data(fd, name, buf);            \
395 } while (0)
396
397     write_token(fd, "version", PROTO_VERSION);
398     write_token(fd, "binary.version", VERSION_CODE);
399     write_token(fd, "pid", ctx->from.pid);
400     write_string_data(fd, "from.name", ctx->from.name);
401     write_string_data(fd, "to.name", ctx->to.name);
402     write_token(fd, "from.uid", ctx->from.uid);
403     write_token(fd, "to.uid", ctx->to.uid);
404     write_string_data(fd, "from.bin", ctx->from.bin);
405     // TODO: Fix issue where not using -c does not result a in a command
406     write_string_data(fd, "command", get_command(&ctx->to));
407     write_token(fd, "eof", PROTO_VERSION);
408     return 0;
409 }
410
411 static int socket_receive_result(int fd, char *result, ssize_t result_len) {
412     ssize_t len;
413
414     LOGV("waiting for user");
415     len = read(fd, result, result_len-1);
416     if (len < 0) {
417         PLOGE("read(result)");
418         return -1;
419     }
420     result[len] = '\0';
421
422     return 0;
423 }
424
425 static void usage(int status) {
426     FILE *stream = (status == EXIT_SUCCESS) ? stdout : stderr;
427
428     fprintf(stream,
429     "Usage: su [options] [--] [-] [LOGIN] [--] [args...]\n\n"
430     "Options:\n"
431     "  --daemon                      start the su daemon agent\n"
432     "  -c, --command COMMAND         pass COMMAND to the invoked shell\n"
433     "  -h, --help                    display this help message and exit\n"
434     "  -, -l, --login                pretend the shell to be a login shell\n"
435     "  -m, -p,\n"
436     "  --preserve-environment        do not change environment variables\n"
437     "  -s, --shell SHELL             use SHELL instead of the default " DEFAULT_SHELL "\n"
438     "  -u                            display the multiuser mode and exit\n"
439     "  -v, --version                 display version number and exit\n"
440     "  -V                            display version code and exit,\n"
441     "                                this is used almost exclusively by Superuser.apk\n");
442     exit(status);
443 }
444
445 static __attribute__ ((noreturn)) void deny(struct su_context *ctx) {
446     char *cmd = get_command(&ctx->to);
447
448     int send_to_app = 1;
449
450     // no need to log if called by root
451     if (ctx->from.uid == AID_ROOT)
452         send_to_app = 0;
453
454     // dumpstate (which logs to logcat/shell) will spam the crap out of the system with su calls
455     if (strcmp("/system/bin/dumpstate", ctx->from.bin) == 0)
456         send_to_app = 0;
457
458     if (send_to_app)
459         send_result(ctx, DENY);
460
461     LOGW("request rejected (%u->%u %s)", ctx->from.uid, ctx->to.uid, cmd);
462     fprintf(stderr, "%s\n", strerror(EACCES));
463     exit(EXIT_FAILURE);
464 }
465
466 static __attribute__ ((noreturn)) void allow(struct su_context *ctx) {
467     char *arg0;
468     int argc, err;
469
470     umask(ctx->umask);
471     int send_to_app = 1;
472
473     // no need to log if called by root
474     if (ctx->from.uid == AID_ROOT)
475         send_to_app = 0;
476
477     // dumpstate (which logs to logcat/shell) will spam the crap out of the system with su calls
478     if (strcmp("/system/bin/dumpstate", ctx->from.bin) == 0)
479         send_to_app = 0;
480
481     if (send_to_app)
482         send_result(ctx, ALLOW);
483
484     char *binary;
485     argc = ctx->to.optind;
486     if (ctx->to.command) {
487         binary = ctx->to.shell;
488         ctx->to.argv[--argc] = ctx->to.command;
489         ctx->to.argv[--argc] = "-c";
490     }
491     else if (ctx->to.shell) {
492         binary = ctx->to.shell;
493     }
494     else {
495         if (ctx->to.argv[argc]) {
496             binary = ctx->to.argv[argc++];
497         }
498         else {
499             binary = DEFAULT_SHELL;
500         }
501     }
502
503     arg0 = strrchr (binary, '/');
504     arg0 = (arg0) ? arg0 + 1 : binary;
505     if (ctx->to.login) {
506         int s = strlen(arg0) + 2;
507         char *p = malloc(s);
508
509         if (!p)
510             exit(EXIT_FAILURE);
511
512         *p = '-';
513         strcpy(p + 1, arg0);
514         arg0 = p;
515     }
516
517     populate_environment(ctx);
518     set_identity(ctx->to.uid);
519
520 #define PARG(arg)                                    \
521     (argc + (arg) < ctx->to.argc) ? " " : "",                    \
522     (argc + (arg) < ctx->to.argc) ? ctx->to.argv[argc + (arg)] : ""
523
524     LOGD("%u %s executing %u %s using binary %s : %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
525             ctx->from.uid, ctx->from.bin,
526             ctx->to.uid, get_command(&ctx->to), binary,
527             arg0, PARG(0), PARG(1), PARG(2), PARG(3), PARG(4), PARG(5),
528             (ctx->to.optind + 6 < ctx->to.argc) ? " ..." : "");
529
530     ctx->to.argv[--argc] = arg0;
531     execvp(binary, ctx->to.argv + argc);
532     err = errno;
533     PLOGE("exec");
534     fprintf(stderr, "Cannot execute %s: %s\n", binary, strerror(err));
535     exit(EXIT_FAILURE);
536 }
537
538 /*
539  * CyanogenMod-specific behavior
540  *
541  * we can't simply use the property service, since we aren't launched from init
542  * and can't trust the location of the property workspace.
543  * Find the properties ourselves.
544  */
545 int access_disabled(const struct su_initiator *from) {
546 #ifndef SUPERUSER_EMBEDDED
547     return 0;
548 #else
549     char *data;
550     char build_type[PROPERTY_VALUE_MAX];
551     char debuggable[PROPERTY_VALUE_MAX], enabled[PROPERTY_VALUE_MAX];
552     size_t len;
553
554     data = read_file("/system/build.prop");
555     if (check_property(data, "ro.cm.version")) {
556         get_property(data, build_type, "ro.build.type", "");
557         free(data);
558
559         data = read_file("/default.prop");
560         get_property(data, debuggable, "ro.debuggable", "0");
561         free(data);
562         /* only allow su on debuggable builds */
563         if (strcmp("1", debuggable) != 0) {
564             LOGE("Root access is disabled on non-debug builds");
565             return 1;
566         }
567
568         data = read_file("/data/property/persist.sys.root_access");
569         if (data != NULL) {
570             len = strlen(data);
571             if (len >= PROPERTY_VALUE_MAX)
572                 memcpy(enabled, "1", 2);
573             else
574                 memcpy(enabled, data, len + 1);
575             free(data);
576         } else
577             memcpy(enabled, "1", 2);
578
579         /* enforce persist.sys.root_access on non-eng builds for apps */
580         if (strcmp("eng", build_type) != 0 &&
581                 from->uid != AID_SHELL && from->uid != AID_ROOT &&
582                 (atoi(enabled) & CM_ROOT_ACCESS_APPS_ONLY) != CM_ROOT_ACCESS_APPS_ONLY ) {
583             LOGE("Apps root access is disabled by system setting - "
584                  "enable it under settings -> developer options");
585             return 1;
586         }
587
588         /* disallow su in a shell if appropriate */
589         if (from->uid == AID_SHELL &&
590                 (atoi(enabled) & CM_ROOT_ACCESS_ADB_ONLY) != CM_ROOT_ACCESS_ADB_ONLY ) {
591             LOGE("Shell root access is disabled by a system setting - "
592                  "enable it under settings -> developer options");
593             return 1;
594         }
595
596     }
597     return 0;
598 #endif
599 }
600
601 static int get_api_version() {
602   char sdk_ver[PROPERTY_VALUE_MAX];
603   char *data = read_file("/system/build.prop");
604   get_property(data, sdk_ver, "ro.build.version.sdk", "0");
605   int ver = atoi(sdk_ver);
606   free(data);
607   return ver;
608 }
609
610 static void fork_for_samsung(void)
611 {
612     // Samsung CONFIG_SEC_RESTRICT_SETUID wants the parent process to have
613     // EUID 0, or else our setresuid() calls will be denied.  So make sure
614     // all such syscalls are executed by a child process.
615     int rv;
616
617     switch (fork()) {
618     case 0:
619         return;
620     case -1:
621         PLOGE("fork");
622         exit(1);
623     default:
624         if (wait(&rv) < 0) {
625             exit(1);
626         } else {
627             exit(WEXITSTATUS(rv));
628         }
629     }
630 }
631
632 int main(int argc, char *argv[]) {
633     return su_main(argc, argv, 1);
634 }
635
636 int su_main(int argc, char *argv[], int need_client) {
637     // start up in daemon mode if prompted
638     if (argc == 2 && strcmp(argv[1], "--daemon") == 0) {
639         return run_daemon();
640     }
641
642     int ppid = getppid();
643     fork_for_samsung();
644
645     // Sanitize all secure environment variables (from linker_environ.c in AOSP linker).
646     /* The same list than GLibc at this point */
647     static const char* const unsec_vars[] = {
648         "GCONV_PATH",
649         "GETCONF_DIR",
650         "HOSTALIASES",
651         "LD_AUDIT",
652         "LD_DEBUG",
653         "LD_DEBUG_OUTPUT",
654         "LD_DYNAMIC_WEAK",
655         "LD_LIBRARY_PATH",
656         "LD_ORIGIN_PATH",
657         "LD_PRELOAD",
658         "LD_PROFILE",
659         "LD_SHOW_AUXV",
660         "LD_USE_LOAD_BIAS",
661         "LOCALDOMAIN",
662         "LOCPATH",
663         "MALLOC_TRACE",
664         "MALLOC_CHECK_",
665         "NIS_PATH",
666         "NLSPATH",
667         "RESOLV_HOST_CONF",
668         "RES_OPTIONS",
669         "TMPDIR",
670         "TZDIR",
671         "LD_AOUT_LIBRARY_PATH",
672         "LD_AOUT_PRELOAD",
673         // not listed in linker, used due to system() call
674         "IFS",
675     };
676     const char* const* cp   = unsec_vars;
677     const char* const* endp = cp + sizeof(unsec_vars)/sizeof(unsec_vars[0]);
678     while (cp < endp) {
679         unsetenv(*cp);
680         cp++;
681     }
682
683     /*
684      * set LD_LIBRARY_PATH if the linker has wiped out it due to we're suid.
685      * This occurs on Android 4.0+
686      */
687     setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 0);
688
689     LOGD("su invoked.");
690
691     struct su_context ctx = {
692         .from = {
693             .pid = -1,
694             .uid = 0,
695             .bin = "",
696             .args = "",
697             .name = "",
698         },
699         .to = {
700             .uid = AID_ROOT,
701             .login = 0,
702             .keepenv = 0,
703             .shell = NULL,
704             .command = NULL,
705             .argv = argv,
706             .argc = argc,
707             .optind = 0,
708             .name = "",
709         },
710         .user = {
711             .android_user_id = 0,
712             .multiuser_mode = MULTIUSER_MODE_OWNER_ONLY,
713             .database_path = REQUESTOR_DATA_PATH REQUESTOR_DATABASE_PATH,
714             .base_path = REQUESTOR_DATA_PATH REQUESTOR
715         },
716     };
717     struct stat st;
718     int c, socket_serv_fd, fd;
719     char buf[64], *result;
720     policy_t dballow;
721     struct option long_opts[] = {
722         { "command",            required_argument,    NULL, 'c' },
723         { "help",            no_argument,        NULL, 'h' },
724         { "login",            no_argument,        NULL, 'l' },
725         { "preserve-environment",    no_argument,        NULL, 'p' },
726         { "shell",            required_argument,    NULL, 's' },
727         { "version",            no_argument,        NULL, 'v' },
728         { NULL, 0, NULL, 0 },
729     };
730
731     while ((c = getopt_long(argc, argv, "+c:hlmps:Vvu", long_opts, NULL)) != -1) {
732         switch(c) {
733         case 'c':
734             ctx.to.shell = DEFAULT_SHELL;
735             ctx.to.command = optarg;
736             break;
737         case 'h':
738             usage(EXIT_SUCCESS);
739             break;
740         case 'l':
741             ctx.to.login = 1;
742             break;
743         case 'm':
744         case 'p':
745             ctx.to.keepenv = 1;
746             break;
747         case 's':
748             ctx.to.shell = optarg;
749             break;
750         case 'V':
751             printf("%d\n", VERSION_CODE);
752             exit(EXIT_SUCCESS);
753         case 'v':
754             printf("%s\n", VERSION);
755             exit(EXIT_SUCCESS);
756         case 'u':
757             switch (get_multiuser_mode()) {
758             case MULTIUSER_MODE_USER:
759                 printf("%s\n", MULTIUSER_VALUE_USER);
760                 break;
761             case MULTIUSER_MODE_OWNER_MANAGED:
762                 printf("%s\n", MULTIUSER_VALUE_OWNER_MANAGED);
763                 break;
764             case MULTIUSER_MODE_OWNER_ONLY:
765                 printf("%s\n", MULTIUSER_VALUE_OWNER_ONLY);
766                 break;
767             case MULTIUSER_MODE_NONE:
768                 printf("%s\n", MULTIUSER_VALUE_NONE);
769                 break;
770             }
771             exit(EXIT_SUCCESS);
772         default:
773             /* Bionic getopt_long doesn't terminate its error output by newline */
774             fprintf(stderr, "\n");
775             usage(2);
776         }
777     }
778
779     if (need_client) {
780         // attempt to use the daemon client if not root,
781         // or this is api 18 and adb shell (/data is not readable even as root)
782         // or just always use it on API 19+ (ART)
783         if ((geteuid() != AID_ROOT && getuid() != AID_ROOT) ||
784             (get_api_version() >= 18 && getuid() == AID_SHELL) ||
785             get_api_version() >= 19) {
786             // attempt to connect to daemon...
787             LOGD("starting daemon client %d %d", getuid(), geteuid());
788             return connect_daemon(argc, argv, ppid);
789         }
790     }
791
792     if (optind < argc && !strcmp(argv[optind], "-")) {
793         ctx.to.login = 1;
794         optind++;
795     }
796     /* username or uid */
797     if (optind < argc && strcmp(argv[optind], "--")) {
798         struct passwd *pw;
799         pw = getpwnam(argv[optind]);
800         if (!pw) {
801             char *endptr;
802
803             /* It seems we shouldn't do this at all */
804             errno = 0;
805             ctx.to.uid = strtoul(argv[optind], &endptr, 10);
806             if (errno || *endptr) {
807                 LOGE("Unknown id: %s\n", argv[optind]);
808                 fprintf(stderr, "Unknown id: %s\n", argv[optind]);
809                 exit(EXIT_FAILURE);
810             }
811         } else {
812             ctx.to.uid = pw->pw_uid;
813             if (pw->pw_name)
814                 strncpy(ctx.to.name, pw->pw_name, sizeof(ctx.to.name));
815         }
816         optind++;
817     }
818     if (optind < argc && !strcmp(argv[optind], "--")) {
819         optind++;
820     }
821     ctx.to.optind = optind;
822
823     su_ctx = &ctx;
824     if (from_init(&ctx.from) < 0) {
825         deny(&ctx);
826     }
827
828     read_options(&ctx);
829     user_init(&ctx);
830
831     // the latter two are necessary for stock ROMs like note 2 which do dumb things with su, or crash otherwise
832     if (ctx.from.uid == AID_ROOT) {
833         LOGD("Allowing root/system/radio.");
834         allow(&ctx);
835     }
836
837     // verify superuser is installed
838     if (stat(ctx.user.base_path, &st) < 0) {
839         // send to market (disabled, because people are and think this is hijacking their su)
840         // if (0 == strcmp(JAVA_PACKAGE_NAME, REQUESTOR))
841         //     silent_run("am start -d http://www.clockworkmod.com/superuser/install.html -a android.intent.action.VIEW");
842         PLOGE("stat %s", ctx.user.base_path);
843         deny(&ctx);
844     }
845
846     // odd perms on superuser data dir
847     if (st.st_gid != st.st_uid) {
848         LOGE("Bad uid/gid %d/%d for Superuser Requestor application",
849                 (int)st.st_uid, (int)st.st_gid);
850         deny(&ctx);
851     }
852
853     // always allow if this is the superuser uid
854     // superuser needs to be able to reenable itself when disabled...
855     if (ctx.from.uid == st.st_uid) {
856         allow(&ctx);
857     }
858
859     // check if superuser is disabled completely
860     if (access_disabled(&ctx.from)) {
861         LOGD("access_disabled");
862         deny(&ctx);
863     }
864
865     // autogrant shell at this point
866     if (ctx.from.uid == AID_SHELL) {
867         LOGD("Allowing shell.");
868         allow(&ctx);
869     }
870
871     // deny if this is a non owner request and owner mode only
872     if (ctx.user.multiuser_mode == MULTIUSER_MODE_OWNER_ONLY && ctx.user.android_user_id != 0) {
873         deny(&ctx);
874     }
875
876     ctx.umask = umask(027);
877
878     int ret = mkdir(REQUESTOR_CACHE_PATH, 0770);
879     if (chown(REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid)) {
880         PLOGE("chown (%s, %ld, %ld)", REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid);
881         deny(&ctx);
882     }
883
884     if (setgroups(0, NULL)) {
885         PLOGE("setgroups");
886         deny(&ctx);
887     }
888     if (setegid(st.st_gid)) {
889         PLOGE("setegid (%lu)", st.st_gid);
890         deny(&ctx);
891     }
892     if (seteuid(st.st_uid)) {
893         PLOGE("seteuid (%lu)", st.st_uid);
894         deny(&ctx);
895     }
896
897     dballow = database_check(&ctx);
898     switch (dballow) {
899         case INTERACTIVE:
900             break;
901         case ALLOW:
902             LOGD("db allowed");
903             allow(&ctx);    /* never returns */
904         case DENY:
905         default:
906             LOGD("db denied");
907             deny(&ctx);        /* never returns too */
908     }
909
910     socket_serv_fd = socket_create_temp(ctx.sock_path, sizeof(ctx.sock_path));
911     LOGD(ctx.sock_path);
912     if (socket_serv_fd < 0) {
913         deny(&ctx);
914     }
915
916     signal(SIGHUP, cleanup_signal);
917     signal(SIGPIPE, cleanup_signal);
918     signal(SIGTERM, cleanup_signal);
919     signal(SIGQUIT, cleanup_signal);
920     signal(SIGINT, cleanup_signal);
921     signal(SIGABRT, cleanup_signal);
922
923     if (send_request(&ctx) < 0) {
924         deny(&ctx);
925     }
926
927     atexit(cleanup);
928
929     fd = socket_accept(socket_serv_fd);
930     if (fd < 0) {
931         deny(&ctx);
932     }
933     if (socket_send_request(fd, &ctx)) {
934         deny(&ctx);
935     }
936     if (socket_receive_result(fd, buf, sizeof(buf))) {
937         deny(&ctx);
938     }
939
940     close(fd);
941     close(socket_serv_fd);
942     socket_cleanup(&ctx);
943
944     result = buf;
945
946 #define SOCKET_RESPONSE    "socket:"
947     if (strncmp(result, SOCKET_RESPONSE, sizeof(SOCKET_RESPONSE) - 1))
948         LOGW("SECURITY RISK: Requestor still receives credentials in intent");
949     else
950         result += sizeof(SOCKET_RESPONSE) - 1;
951
952     if (!strcmp(result, "DENY")) {
953         deny(&ctx);
954     } else if (!strcmp(result, "ALLOW")) {
955         allow(&ctx);
956     } else {
957         LOGE("unknown response from Superuser Requestor: %s", result);
958         deny(&ctx);
959     }
960 }