OSDN Git Service

9e3144de5daa5525f0b86aec5863289d725eb11e
[android-x86/system-extras.git] / su / su.c
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <errno.h>
18 #include <error.h>
19 #include <pwd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <private/android_filesystem_config.h>
26
27 void pwtoid(const char* tok, uid_t* uid, gid_t* gid) {
28     struct passwd* pw = getpwnam(tok);
29     if (pw) {
30         if (uid) *uid = pw->pw_uid;
31         if (gid) *gid = pw->pw_gid;
32     } else {
33         char* end;
34         errno = 0;
35         uid_t tmpid = strtoul(tok, &end, 10);
36         if (errno != 0 || end == tok) error(1, errno, "invalid uid/gid '%s'", tok);
37         if (uid) *uid = tmpid;
38         if (gid) *gid = tmpid;
39     }
40 }
41
42 void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, int* gids_count) {
43     char *clobberablegids;
44     char *nexttok;
45     char *tok;
46     int gids_found;
47
48     if (!uidgids || !*uidgids) {
49         *gid = *uid = 0;
50         *gids_count = 0;
51         return;
52     }
53
54     clobberablegids = strdup(uidgids);
55     strcpy(clobberablegids, uidgids);
56     nexttok = clobberablegids;
57     tok = strsep(&nexttok, ",");
58     pwtoid(tok, uid, gid);
59     tok = strsep(&nexttok, ",");
60     if (!tok) {
61         /* gid is already set above */
62         *gids_count = 0;
63         free(clobberablegids);
64         return;
65     }
66     pwtoid(tok, NULL, gid);
67     gids_found = 0;
68     while ((gids_found < *gids_count) && (tok = strsep(&nexttok, ","))) {
69         pwtoid(tok, NULL, gids);
70         gids_found++;
71         gids++;
72     }
73     if (nexttok && gids_found == *gids_count) {
74         fprintf(stderr, "too many group ids\n");
75     }
76     *gids_count = gids_found;
77     free(clobberablegids);
78 }
79
80 /*
81  * SU can be given a specific command to exec. UID _must_ be
82  * specified for this.
83  *
84  * Usage:
85  *   su 1000
86  *   su 1000 ls -l
87  *  or
88  *   su [uid[,gid[,group1]...] [cmd]]
89  *  E.g.
90  *  su 1000,shell,net_bw_acct,net_bw_stats id
91  * will return
92  *  uid=1000(system) gid=2000(shell) groups=3006(net_bw_stats),3007(net_bw_acct)
93  */
94 int main(int argc, char** argv) {
95     uid_t current_uid = getuid();
96     if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");
97
98     // The default user is root.
99     uid_t uid = 0;
100     gid_t gid = 0;
101
102     // TODO: use getopt and support at least -- and --help.
103
104     // If there are any arguments, the first argument is the uid/gid/supplementary groups.
105     if (argc >= 2) {
106         gid_t gids[10];
107         int gids_count = sizeof(gids)/sizeof(gids[0]);
108         extract_uidgids(argv[1], &uid, &gid, gids, &gids_count);
109         if (gids_count) {
110             if (setgroups(gids_count, gids)) {
111                 error(1, errno, "setgroups failed");
112             }
113         }
114         ++argv;
115     }
116
117     if (setgid(gid)) error(1, errno, "setgid failed");
118     if (setuid(uid)) error(1, errno, "setuid failed");
119
120     // TODO: reset $PATH.
121
122     // Set up the arguments for exec.
123     char* exec_args[argc + 1];  // Having too much space is fine.
124     // Skip "su" and copy any other args. We already skipped the optional uid above.
125     ++argv;
126     size_t i = 0;
127     for (; *argv != NULL; ++i) {
128       exec_args[i] = *argv++;
129     }
130     // Default to the standard shell.
131     if (i == 0) exec_args[i++] = "/system/bin/sh";
132     exec_args[i] = NULL;
133
134     execvp(exec_args[0], exec_args);
135     error(1, errno, "failed to exec %s", exec_args[0]);
136 }