2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <private/android_filesystem_config.h>
27 void pwtoid(const char* tok, uid_t* uid, gid_t* gid) {
28 struct passwd* pw = getpwnam(tok);
30 if (uid) *uid = pw->pw_uid;
31 if (gid) *gid = pw->pw_gid;
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;
42 void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, int* gids_count) {
43 char *clobberablegids;
48 if (!uidgids || !*uidgids) {
54 clobberablegids = strdup(uidgids);
55 strcpy(clobberablegids, uidgids);
56 nexttok = clobberablegids;
57 tok = strsep(&nexttok, ",");
58 pwtoid(tok, uid, gid);
59 tok = strsep(&nexttok, ",");
61 /* gid is already set above */
63 free(clobberablegids);
66 pwtoid(tok, NULL, gid);
68 while ((gids_found < *gids_count) && (tok = strsep(&nexttok, ","))) {
69 pwtoid(tok, NULL, gids);
73 if (nexttok && gids_found == *gids_count) {
74 fprintf(stderr, "too many group ids\n");
76 *gids_count = gids_found;
77 free(clobberablegids);
81 * SU can be given a specific command to exec. UID _must_ be
88 * su [uid[,gid[,group1]...] [cmd]]
90 * su 1000,shell,net_bw_acct,net_bw_stats id
92 * uid=1000(system) gid=2000(shell) groups=3006(net_bw_stats),3007(net_bw_acct)
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");
98 // The default user is root.
102 // TODO: use getopt and support at least -- and --help.
104 // If there are any arguments, the first argument is the uid/gid/supplementary groups.
107 int gids_count = sizeof(gids)/sizeof(gids[0]);
108 extract_uidgids(argv[1], &uid, &gid, gids, &gids_count);
110 if (setgroups(gids_count, gids)) {
111 error(1, errno, "setgroups failed");
117 if (setgid(gid)) error(1, errno, "setgid failed");
118 if (setuid(uid)) error(1, errno, "setuid failed");
120 // TODO: reset $PATH.
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.
127 for (; *argv != NULL; ++i) {
128 exec_args[i] = *argv++;
130 // Default to the standard shell.
131 if (i == 0) exec_args[i++] = "/system/bin/sh";
134 execvp(exec_args[0], exec_args);
135 error(1, errno, "failed to exec %s", exec_args[0]);