OSDN Git Service

remove noisy logging and cruft code
[android-x86/external-koush-Superuser.git] / Superuser / jni / su / su.c
index 4740250..d7b3a0d 100644 (file)
@@ -32,7 +32,6 @@
 #include <sys/stat.h>
 #include <stdarg.h>
 #include <sys/types.h>
-#include <pwd.h>
 
 #include "su.h"
 #include "utils.h"
@@ -49,41 +48,47 @@ int get_shell_uid() {
 void exec_log(char *priority, char* logline) {
   int pid;
   if ((pid = fork()) == 0) {
-    execl("/system/bin/log", "/system/bin/log", "-p", priority, "-t", LOG_TAG, logline);
-    exit(0);
+      int zero = open("/dev/zero", O_RDONLY | O_CLOEXEC);
+      dup2(zero, 0);
+      int null = open("/dev/null", O_WRONLY | O_CLOEXEC);
+      dup2(null, 1);
+      dup2(null, 2);
+      execl("/system/bin/log", "/system/bin/log", "-p", priority, "-t", LOG_TAG, logline);
+      _exit(0);
   }
 }
 
 void exec_loge(const char* fmt, ...) {
-  va_list args;
+    va_list args;
 
-  char logline[PATH_MAX];
-  va_start(args, fmt);
-  vsnprintf(logline, PATH_MAX, fmt, args);
-  va_end(args);
-  exec_log("e", logline);
+    char logline[PATH_MAX];
+    va_start(args, fmt);
+    vsnprintf(logline, PATH_MAX, fmt, args);
+    va_end(args);
+    exec_log("e", logline);
 }
+
 void exec_logw(const char* fmt, ...) {
-  va_list args;
+    va_list args;
 
-  char logline[PATH_MAX];
-  va_start(args, fmt);
-  vsnprintf(logline, PATH_MAX, fmt, args);
-  va_end(args);
-  exec_log("w", logline);
+    char logline[PATH_MAX];
+    va_start(args, fmt);
+    vsnprintf(logline, PATH_MAX, fmt, args);
+    va_end(args);
+    exec_log("w", logline);
 }
+
 void exec_logd(const char* fmt, ...) {
-  va_list args;
+    va_list args;
 
-  char logline[PATH_MAX];
-  va_start(args, fmt);
-  vsnprintf(logline, PATH_MAX, fmt, args);
-  va_end(args);
-  exec_log("d", logline);
+    char logline[PATH_MAX];
+    va_start(args, fmt);
+    vsnprintf(logline, PATH_MAX, fmt, args);
+    va_end(args);
+    exec_log("d", logline);
 }
 
-static int from_init(struct su_initiator *from)
-{
+static int from_init(struct su_initiator *from) {
     char path[PATH_MAX], exe[PATH_MAX];
     char args[4096], *argv0, *argv_rest;
     int fd;
@@ -144,40 +149,63 @@ static int from_init(struct su_initiator *from)
     strncpy(from->bin, argv0, sizeof(from->bin));
     from->bin[sizeof(from->bin)-1] = '\0';
 
+    struct passwd *pw;
+    pw = getpwuid(from->uid);
+    if (pw && pw->pw_name) {
+        strncpy(from->name, pw->pw_name, sizeof(from->name));
+    }
+
     return 0;
 }
 
-static void read_options(struct su_context *ctx)
-{
+static int get_multiuser_mode() {
+    char *data;
+    char sdk_ver[PROPERTY_VALUE_MAX];
+
+    data = read_file("/system/build.prop");
+    get_property(data, sdk_ver, "ro.build.version.sdk", "0");
+    free(data);
+
+    int sdk = atoi(sdk_ver);
+    if (sdk < 17)
+        return MULTIUSER_MODE_NONE;
+
+    int ret = MULTIUSER_MODE_OWNER_ONLY;
     char mode[12];
     FILE *fp;
-    if ((fp = fopen(REQUESTOR_OPTIONS, "r"))) {
+    if ((fp = fopen(REQUESTOR_MULTIUSER_MODE, "r"))) {
+        int last = strlen(mode) - 1;
+        if (mode[last] == '\n')
+            mode[last] = '\0';
         fgets(mode, sizeof(mode), fp);
-        if (strcmp(mode, "user\n") == 0) {
-            ctx->user.owner_mode = 0;
-        } else if (strcmp(mode, "owner\n") == 0) {
-            ctx->user.owner_mode = 1;
+        if (strcmp(mode, MULTIUSER_VALUE_USER) == 0) {
+            ret = MULTIUSER_MODE_USER;
+        } else if (strcmp(mode, MULTIUSER_VALUE_OWNER_MANAGED) == 0) {
+            ret = MULTIUSER_MODE_OWNER_MANAGED;
+        }
+        else {
+            ret = MULTIUSER_MODE_OWNER_ONLY;
         }
+        fclose(fp);
     }
+    return ret;
+}
+
+static void read_options(struct su_context *ctx) {
+    ctx->user.multiuser_mode = get_multiuser_mode();
 }
 
-static void user_init(struct su_context *ctx)
-{
+static void user_init(struct su_context *ctx) {
     if (ctx->from.uid > 99999) {
-        ctx->user.userid = ctx->from.uid / 100000;
-        if (!ctx->user.owner_mode) {
-            snprintf(ctx->user.data_path, PATH_MAX, "/data/user/%d/%s",
-                    ctx->user.userid, REQUESTOR);
-            snprintf(ctx->user.store_path, PATH_MAX, "/data/user/%d/%s/files/stored",
-                    ctx->user.userid, REQUESTOR);
-            snprintf(ctx->user.store_default, PATH_MAX, "/data/user/%d/%s/files/stored/default",
-                    ctx->user.userid, REQUESTOR);
+        ctx->user.android_user_id = ctx->from.uid / 100000;
+        if (ctx->user.multiuser_mode == MULTIUSER_MODE_USER) {
+            snprintf(ctx->user.database_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR_DATABASE_PATH);
+            snprintf(ctx->user.base_path, PATH_MAX, "%s/%d/%s", REQUESTOR_USER_PATH, ctx->user.android_user_id, REQUESTOR);
         }
     }
 }
 
-static void populate_environment(const struct su_context *ctx)
-{
+static void populate_environment(const struct su_context *ctx) {
     struct passwd *pw;
 
     if (ctx->to.keepenv)
@@ -194,8 +222,7 @@ static void populate_environment(const struct su_context *ctx)
     }
 }
 
-void set_identity(unsigned int uid)
-{
+void set_identity(unsigned int uid) {
     /*
      * Set effective uid back to root, otherwise setres[ug]id will fail
      * if uid isn't root.
@@ -214,8 +241,7 @@ void set_identity(unsigned int uid)
     }
 }
 
-static void socket_cleanup(struct su_context *ctx)
-{
+static void socket_cleanup(struct su_context *ctx) {
     if (ctx && ctx->sock_path[0]) {
         if (unlink(ctx->sock_path))
             PLOGE("unlink (%s)", ctx->sock_path);
@@ -230,25 +256,21 @@ static void socket_cleanup(struct su_context *ctx)
  */
 static struct su_context *su_ctx = NULL;
 
-static void cleanup(void)
-{
+static void cleanup(void) {
     socket_cleanup(su_ctx);
 }
 
-static void cleanup_signal(int sig)
-{
+static void cleanup_signal(int sig) {
     socket_cleanup(su_ctx);
     exit(128 + sig);
 }
 
-void sigchld_handler(int sig)
-{
+void sigchld_handler(int sig) {
     child_cleanup(su_ctx);
     (void)sig;
 }
 
-static int socket_create_temp(char *path, size_t len)
-{
+static int socket_create_temp(char *path, size_t len) {
     int fd;
     struct sockaddr_un sun;
 
@@ -273,7 +295,7 @@ static int socket_create_temp(char *path, size_t len)
      * something bad occured previously and the kernel reused pid from that process.
      * Small probability, isn't it.
      */
-    // unlink(sun.sun_path);
+    unlink(sun.sun_path);
 
     if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
         PLOGE("bind");
@@ -291,8 +313,7 @@ err:
     return -1;
 }
 
-static int socket_accept(int serv_fd)
-{
+static int socket_accept(int serv_fd) {
     struct timeval tv;
     fd_set fds;
     int fd, rc;
@@ -319,50 +340,52 @@ static int socket_accept(int serv_fd)
     return fd;
 }
 
-static int socket_send_request(int fd, const struct su_context *ctx)
-{
-    size_t len;
-    size_t bin_size, cmd_size;
-    char *cmd;
-
-#define write_token(fd, data)                \
-do {                            \
-    uint32_t __data = htonl(data);            \
-    size_t __count = sizeof(__data);        \
-    size_t __len = write((fd), &__data, __count);    \
-    if (__len != __count) {                \
-        PLOGE("write(" #data ")");        \
-        return -1;                \
-    }                        \
+static int socket_send_request(int fd, const struct su_context *ctx) {
+#define write_data(fd, data, data_len)              \
+do {                                                \
+    size_t __len = htonl(data_len);                 \
+    __len = write((fd), &__len, sizeof(__len));     \
+    if (__len != sizeof(__len)) {                   \
+        PLOGE("write(" #data ")");                  \
+        return -1;                                  \
+    }                                               \
+    __len = write((fd), data, data_len);            \
+    if (__len != data_len) {                        \
+        PLOGE("write(" #data ")");                  \
+        return -1;                                  \
+    }                                               \
 } while (0)
 
-    write_token(fd, PROTO_VERSION);
-    write_token(fd, PATH_MAX);
-    write_token(fd, ARG_MAX);
-    write_token(fd, ctx->from.uid);
-    write_token(fd, ctx->to.uid);
-    bin_size = strlen(ctx->from.bin) + 1;
-    write_token(fd, bin_size);
-    len = write(fd, ctx->from.bin, bin_size);
-    if (len != bin_size) {
-        PLOGE("write(bin)");
-        return -1;
-    }
-    cmd = get_command(&ctx->to);
-    cmd_size = strlen(cmd) + 1;
-    write_token(fd, cmd_size);
-    len = write(fd, cmd, cmd_size);
-    if (len != cmd_size) {
-        PLOGE("write(cmd)");
-        return -1;
-    }
+#define write_string(fd, name, data)        \
+do {                                        \
+    write_data(fd, name, strlen(name));     \
+    write_data(fd, data, strlen(data));     \
+} while (0)
+
+// stringify everything.
+#define write_token(fd, name, data)         \
+do {                                        \
+    char buf[16];                           \
+    snprintf(buf, sizeof(buf), "%d", data); \
+    write_string(fd, name, buf);            \
+} while (0)
+
+    write_token(fd, "version", PROTO_VERSION);
+    write_token(fd, "pid", ctx->from.pid);
+    write_token(fd, "from.name", ctx->from.name);
+    write_token(fd, "to.name", ctx->to.name);
+    write_token(fd, "from.uid", ctx->from.uid);
+    write_token(fd, "to.uid", ctx->to.uid);
+    write_string(fd, "from.bin", ctx->from.bin);
+    write_string(fd, "command", get_command(&ctx->to));
+    write_token(fd, "eof", PROTO_VERSION);
     return 0;
 }
 
-static int socket_receive_result(int fd, char *result, ssize_t result_len)
-{
+static int socket_receive_result(int fd, char *result, ssize_t result_len) {
     ssize_t len;
     
+    LOGD("waiting for user");
     len = read(fd, result, result_len-1);
     if (len < 0) {
         PLOGE("read(result)");
@@ -373,8 +396,7 @@ static int socket_receive_result(int fd, char *result, ssize_t result_len)
     return 0;
 }
 
-static void usage(int status)
-{
+static void usage(int status) {
     FILE *stream = (status == EXIT_SUCCESS) ? stdout : stderr;
 
     fprintf(stream,
@@ -386,35 +408,34 @@ static void usage(int status)
     "  -m, -p,\n"
     "  --preserve-environment        do not change environment variables\n"
     "  -s, --shell SHELL             use SHELL instead of the default " DEFAULT_SHELL "\n"
+    "  -u                            display the multiuser mode and exit\n"
     "  -v, --version                 display version number and exit\n"
     "  -V                            display version code and exit,\n"
     "                                this is used almost exclusively by Superuser.apk\n");
     exit(status);
 }
 
-static __attribute__ ((noreturn)) void deny(struct su_context *ctx)
-{
+static __attribute__ ((noreturn)) void deny(struct su_context *ctx) {
     char *cmd = get_command(&ctx->to);
 
     // No send to UI denied requests for shell and root users (they are in the log)
-    if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
-        send_intent(ctx, DENY, ACTION_RESULT);
-    }
+    // if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
+        send_result(ctx, DENY);
+    // }
     LOGW("request rejected (%u->%u %s)", ctx->from.uid, ctx->to.uid, cmd);
     fprintf(stderr, "%s\n", strerror(EACCES));
     exit(EXIT_FAILURE);
 }
 
-static __attribute__ ((noreturn)) void allow(struct su_context *ctx)
-{
+static __attribute__ ((noreturn)) void allow(struct su_context *ctx) {
     char *arg0;
     int argc, err;
 
     umask(ctx->umask);
     // No send to UI accepted requests for shell and root users (they are in the log)
-    if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
-        send_intent(ctx, ALLOW, ACTION_RESULT);
-    }
+    // if( ctx->from.uid != AID_SHELL && ctx->from.uid != AID_ROOT ) {
+        send_result(ctx, ALLOW);
+    // }
 
     arg0 = strrchr (ctx->to.shell, '/');
     arg0 = (arg0) ? arg0 + 1 : ctx->to.shell;
@@ -463,8 +484,7 @@ static __attribute__ ((noreturn)) void allow(struct su_context *ctx)
  * and can't trust the location of the property workspace.
  * Find the properties ourselves.
  */
-int access_disabled(const struct su_initiator *from)
-{
+int access_disabled(const struct su_initiator *from) {
     char *data;
     char build_type[PROPERTY_VALUE_MAX];
     char debuggable[PROPERTY_VALUE_MAX], enabled[PROPERTY_VALUE_MAX];
@@ -516,8 +536,7 @@ int access_disabled(const struct su_initiator *from)
     return 0;
 }
 
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
     // Sanitize all secure environment variables (from linker_environ.c in AOSP linker).
     /* The same list than GLibc at this point */
     static const char* const unsec_vars[] = {
@@ -570,6 +589,7 @@ int main(int argc, char *argv[])
             .uid = 0,
             .bin = "",
             .args = "",
+            .name = "",
         },
         .to = {
             .uid = AID_ROOT,
@@ -580,19 +600,19 @@ int main(int argc, char *argv[])
             .argv = argv,
             .argc = argc,
             .optind = 0,
+            .name = "",
         },
         .user = {
-            .userid = 0,
-            .owner_mode = -1,
-            .data_path = REQUESTOR_DATA_PATH,
-            .store_path = REQUESTOR_STORED_PATH,
-            .store_default = REQUESTOR_STORED_DEFAULT,
+            .android_user_id = 0,
+            .multiuser_mode = MULTIUSER_MODE_OWNER_ONLY,
+            .database_path = REQUESTOR_DATA_PATH REQUESTOR_DATABASE_PATH,
+            .base_path = REQUESTOR_DATA_PATH REQUESTOR
         },
     };
     struct stat st;
     int c, socket_serv_fd, fd;
     char buf[64], *result;
-    allow_t dballow;
+    policy_t dballow;
     struct option long_opts[] = {
         { "command",            required_argument,    NULL, 'c' },
         { "help",            no_argument,        NULL, 'h' },
@@ -603,7 +623,7 @@ int main(int argc, char *argv[])
         { NULL, 0, NULL, 0 },
     };
 
-    while ((c = getopt_long(argc, argv, "+c:hlmps:Vv", long_opts, NULL)) != -1) {
+    while ((c = getopt_long(argc, argv, "+c:hlmps:Vvu", long_opts, NULL)) != -1) {
         switch(c) {
         case 'c':
             ctx.to.command = optarg;
@@ -627,6 +647,22 @@ int main(int argc, char *argv[])
         case 'v':
             printf("%s\n", VERSION);
             exit(EXIT_SUCCESS);
+        case 'u':
+            switch (get_multiuser_mode()) {
+            case MULTIUSER_MODE_USER:
+                printf("%s\n", MULTIUSER_VALUE_USER);
+                break;
+            case MULTIUSER_MODE_OWNER_MANAGED:
+                printf("%s\n", MULTIUSER_VALUE_OWNER_MANAGED);
+                break;
+            case MULTIUSER_MODE_OWNER_ONLY:
+                printf("%s\n", MULTIUSER_VALUE_OWNER_ONLY);
+                break;
+            case MULTIUSER_MODE_NONE:
+                printf("%s\n", MULTIUSER_VALUE_NONE);
+                break;
+            }
+            exit(EXIT_SUCCESS);
         default:
             /* Bionic getopt_long doesn't terminate its error output by newline */
             fprintf(stderr, "\n");
@@ -654,6 +690,8 @@ int main(int argc, char *argv[])
             }
         } else {
             ctx.to.uid = pw->pw_uid;
+            if (pw->pw_name)
+                strncpy(ctx.to.name, pw->pw_name, sizeof(ctx.to.name));
         }
         optind++;
     }
@@ -670,30 +708,34 @@ int main(int argc, char *argv[])
     read_options(&ctx);
     user_init(&ctx);
     
-    if (ctx.user.owner_mode == -1 && ctx.user.userid != 0)
+    if (ctx.user.multiuser_mode == MULTIUSER_MODE_OWNER_ONLY && ctx.user.android_user_id != 0) {
         deny(&ctx);
+    }
 
-    if (access_disabled(&ctx.from))
+    if (access_disabled(&ctx.from)) {
+        LOGD("access_disabled");
         deny(&ctx);
+    }
 
     ctx.umask = umask(027);
 
-    if (ctx.from.uid == AID_ROOT || ctx.from.uid == AID_SHELL)
+    if (ctx.from.uid == AID_ROOT || ctx.from.uid == AID_SHELL) {
+        LOGD("Allowing root/shell.");
         allow(&ctx);
+    }
 
-    if (stat(ctx.user.data_path, &st) < 0) {
-        PLOGE("stat");
+    if (stat(ctx.user.base_path, &st) < 0) {
+        PLOGE("stat %s", ctx.user.base_path);
         deny(&ctx);
     }
 
-    if (st.st_gid != st.st_uid)
-    {
+    if (st.st_gid != st.st_uid) {
         LOGE("Bad uid/gid %d/%d for Superuser Requestor application",
                 (int)st.st_uid, (int)st.st_gid);
         deny(&ctx);
     }
 
-    mkdir(REQUESTOR_CACHE_PATH, 0770);
+    int ret = mkdir(REQUESTOR_CACHE_PATH, 0770);
     if (chown(REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid)) {
         PLOGE("chown (%s, %ld, %ld)", REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid);
         deny(&ctx);
@@ -714,10 +756,15 @@ int main(int argc, char *argv[])
 
     dballow = database_check(&ctx);
     switch (dballow) {
-        case INTERACTIVE: break;
-        case ALLOW: allow(&ctx);    /* never returns */
+        case INTERACTIVE:
+            break;
+        case ALLOW:
+            LOGD("db allowed");
+            allow(&ctx);    /* never returns */
         case DENY:
-        default: deny(&ctx);        /* never returns too */
+        default:
+            LOGD("db denied");
+            deny(&ctx);        /* never returns too */
     }
     
     socket_serv_fd = socket_create_temp(ctx.sock_path, sizeof(ctx.sock_path));
@@ -732,12 +779,13 @@ int main(int argc, char *argv[])
     signal(SIGQUIT, cleanup_signal);
     signal(SIGINT, cleanup_signal);
     signal(SIGABRT, cleanup_signal);
-    atexit(cleanup);
 
-    if (send_intent(&ctx, INTERACTIVE, ACTION_REQUEST) < 0) {
+    if (send_request(&ctx) < 0) {
         deny(&ctx);
     }
 
+    atexit(cleanup);
+
     fd = socket_accept(socket_serv_fd);
     if (fd < 0) {
         deny(&ctx);