blob: ab7ac1e559b1b188ca0b77f950b7930c2e2a54a4
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Mini id implementation for busybox |
4 | * |
5 | * Copyright (C) 2000 by Randolph Chung <tausq@debian.org> |
6 | * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it> |
7 | * |
8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
9 | */ |
10 | /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever |
11 | * length and to be more similar to GNU id. |
12 | * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp> |
13 | * Added -G option Tito Ragusa (C) 2008 for SUSv3. |
14 | */ |
15 | //config:config ID |
16 | //config: bool "id" |
17 | //config: default y |
18 | //config: help |
19 | //config: id displays the current user and group ID names. |
20 | //config: |
21 | //config:config GROUPS |
22 | //config: bool "groups" |
23 | //config: default y |
24 | //config: help |
25 | //config: Print the group names associated with current user id. |
26 | |
27 | //applet:IF_GROUPS(APPLET_NOEXEC(groups, id, BB_DIR_USR_BIN, BB_SUID_DROP, groups)) |
28 | //applet:IF_ID( APPLET_NOEXEC(id, id, BB_DIR_USR_BIN, BB_SUID_DROP, id )) |
29 | |
30 | //kbuild:lib-$(CONFIG_GROUPS) += id.o |
31 | //kbuild:lib-$(CONFIG_ID) += id.o |
32 | |
33 | /* BB_AUDIT SUSv3 compliant. */ |
34 | |
35 | //usage:#define id_trivial_usage |
36 | //usage: "[OPTIONS] [USER]" |
37 | //usage:#define id_full_usage "\n\n" |
38 | //usage: "Print information about USER or the current user\n" |
39 | //usage: IF_SELINUX( |
40 | //usage: "\n -Z Security context" |
41 | //usage: ) |
42 | //usage: "\n -u User ID" |
43 | //usage: "\n -g Group ID" |
44 | //usage: "\n -G Supplementary group IDs" |
45 | //usage: "\n -n Print names instead of numbers" |
46 | //usage: "\n -r Print real ID instead of effective ID" |
47 | //usage: |
48 | //usage:#define id_example_usage |
49 | //usage: "$ id\n" |
50 | //usage: "uid=1000(andersen) gid=1000(andersen)\n" |
51 | |
52 | //usage:#define groups_trivial_usage |
53 | //usage: "[USER]" |
54 | //usage:#define groups_full_usage "\n\n" |
55 | //usage: "Print the group memberships of USER or for the current process" |
56 | //usage: |
57 | //usage:#define groups_example_usage |
58 | //usage: "$ groups\n" |
59 | //usage: "andersen lp dialout cdrom floppy\n" |
60 | |
61 | #include "libbb.h" |
62 | |
63 | /* This is a NOEXEC applet. Be very careful! */ |
64 | |
65 | #if !ENABLE_USE_BB_PWD_GRP |
66 | #if defined(__UCLIBC__) && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 30) |
67 | #error "Sorry, you need at least uClibc version 0.9.30 for id applet to build" |
68 | #endif |
69 | #endif |
70 | |
71 | enum { |
72 | PRINT_REAL = (1 << 0), |
73 | NAME_NOT_NUMBER = (1 << 1), |
74 | JUST_USER = (1 << 2), |
75 | JUST_GROUP = (1 << 3), |
76 | JUST_ALL_GROUPS = (1 << 4), |
77 | #if ENABLE_SELINUX |
78 | JUST_CONTEXT = (1 << 5), |
79 | #endif |
80 | }; |
81 | |
82 | static int print_common(unsigned id, const char *name, const char *prefix) |
83 | { |
84 | if (prefix) { |
85 | printf("%s", prefix); |
86 | } |
87 | if (!(option_mask32 & NAME_NOT_NUMBER) || !name) { |
88 | printf("%u", id); |
89 | } |
90 | if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) { |
91 | if (name) { |
92 | printf(option_mask32 ? "%s" : "(%s)", name); |
93 | } else { |
94 | /* Don't set error status flag in default mode */ |
95 | if (option_mask32) { |
96 | if (ENABLE_DESKTOP) |
97 | bb_error_msg("unknown ID %u", id); |
98 | return EXIT_FAILURE; |
99 | } |
100 | } |
101 | } |
102 | return EXIT_SUCCESS; |
103 | } |
104 | |
105 | static int print_group(gid_t id, const char *prefix) |
106 | { |
107 | return print_common(id, gid2group(id), prefix); |
108 | } |
109 | |
110 | static int print_user(uid_t id, const char *prefix) |
111 | { |
112 | return print_common(id, uid2uname(id), prefix); |
113 | } |
114 | |
115 | /* On error set *n < 0 and return >= 0 |
116 | * If *n is too small, update it and return < 0 |
117 | * (ok to trash groups[] in both cases) |
118 | * Otherwise fill in groups[] and return >= 0 |
119 | */ |
120 | static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n) |
121 | { |
122 | int m; |
123 | |
124 | if (username) { |
125 | /* If the user is a member of more than |
126 | * *n groups, then -1 is returned. Otherwise >= 0. |
127 | * (and no defined way of detecting errors?!) */ |
128 | m = getgrouplist(username, rgid, groups, n); |
129 | /* I guess *n < 0 might indicate error. Anyway, |
130 | * malloc'ing -1 bytes won't be good, so: */ |
131 | if (*n < 0) |
132 | return 0; |
133 | return m; |
134 | } |
135 | |
136 | *n = getgroups(*n, groups); |
137 | if (*n >= 0) |
138 | return *n; |
139 | /* Error */ |
140 | if (errno == EINVAL) /* *n is too small? */ |
141 | *n = getgroups(0, groups); /* get needed *n */ |
142 | /* if *n >= 0, return -1 (got new *n), else return 0 (error): */ |
143 | return -(*n >= 0); |
144 | } |
145 | |
146 | int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
147 | int id_main(int argc UNUSED_PARAM, char **argv) |
148 | { |
149 | uid_t ruid; |
150 | gid_t rgid; |
151 | uid_t euid; |
152 | gid_t egid; |
153 | unsigned opt; |
154 | int i; |
155 | int status = EXIT_SUCCESS; |
156 | const char *prefix; |
157 | const char *username; |
158 | #if ENABLE_SELINUX |
159 | security_context_t scontext = NULL; |
160 | #endif |
161 | |
162 | if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) { |
163 | /* TODO: coreutils groups prepend "USER : " prefix, |
164 | * and accept many usernames. Example: |
165 | * # groups root root |
166 | * root : root |
167 | * root : root |
168 | */ |
169 | opt = option_mask32 = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER; |
170 | } else { |
171 | /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/ |
172 | /* Don't allow more than one username */ |
173 | opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG" |
174 | IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G"); |
175 | opt = getopt32(argv, "rnugG" IF_SELINUX("Z")); |
176 | } |
177 | |
178 | username = argv[optind]; |
179 | if (username) { |
180 | struct passwd *p = xgetpwnam(username); |
181 | euid = ruid = p->pw_uid; |
182 | egid = rgid = p->pw_gid; |
183 | } else { |
184 | egid = getegid(); |
185 | rgid = getgid(); |
186 | euid = geteuid(); |
187 | ruid = getuid(); |
188 | } |
189 | /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */ |
190 | /* id says: print the real ID instead of the effective ID, with -ugG */ |
191 | /* in fact in this case egid is always printed if egid != rgid */ |
192 | if (!opt || (opt & JUST_ALL_GROUPS)) { |
193 | gid_t *groups; |
194 | int n; |
195 | |
196 | if (!opt) { |
197 | /* Default Mode */ |
198 | status |= print_user(ruid, "uid="); |
199 | status |= print_group(rgid, " gid="); |
200 | if (euid != ruid) |
201 | status |= print_user(euid, " euid="); |
202 | if (egid != rgid) |
203 | status |= print_group(egid, " egid="); |
204 | } else { |
205 | /* JUST_ALL_GROUPS */ |
206 | status |= print_group(rgid, NULL); |
207 | if (egid != rgid) |
208 | status |= print_group(egid, " "); |
209 | } |
210 | /* We are supplying largish buffer, trying |
211 | * to not run get_groups() twice. That might be slow |
212 | * ("user database in remote SQL server" case) */ |
213 | groups = xmalloc(64 * sizeof(groups[0])); |
214 | n = 64; |
215 | if (get_groups(username, rgid, groups, &n) < 0) { |
216 | /* Need bigger buffer after all */ |
217 | groups = xrealloc(groups, n * sizeof(groups[0])); |
218 | get_groups(username, rgid, groups, &n); |
219 | } |
220 | if (n > 0) { |
221 | /* Print the list */ |
222 | prefix = " groups="; |
223 | for (i = 0; i < n; i++) { |
224 | if (opt && (groups[i] == rgid || groups[i] == egid)) |
225 | continue; |
226 | status |= print_group(groups[i], opt ? " " : prefix); |
227 | prefix = ","; |
228 | } |
229 | } else if (n < 0) { /* error in get_groups() */ |
230 | if (ENABLE_DESKTOP) |
231 | bb_error_msg_and_die("can't get groups"); |
232 | return EXIT_FAILURE; |
233 | } |
234 | if (ENABLE_FEATURE_CLEAN_UP) |
235 | free(groups); |
236 | #if ENABLE_SELINUX |
237 | if (is_selinux_enabled()) { |
238 | if (getcon(&scontext) == 0) |
239 | printf(" context=%s", scontext); |
240 | } |
241 | #endif |
242 | } else if (opt & PRINT_REAL) { |
243 | euid = ruid; |
244 | egid = rgid; |
245 | } |
246 | |
247 | if (opt & JUST_USER) |
248 | status |= print_user(euid, NULL); |
249 | else if (opt & JUST_GROUP) |
250 | status |= print_group(egid, NULL); |
251 | #if ENABLE_SELINUX |
252 | else if (opt & JUST_CONTEXT) { |
253 | selinux_or_die(); |
254 | if (username || getcon(&scontext)) { |
255 | bb_error_msg_and_die("can't get process context%s", |
256 | username ? " for a different user" : ""); |
257 | } |
258 | fputs(scontext, stdout); |
259 | } |
260 | /* freecon(NULL) seems to be harmless */ |
261 | if (ENABLE_FEATURE_CLEAN_UP) |
262 | freecon(scontext); |
263 | #endif |
264 | bb_putchar('\n'); |
265 | fflush_stdout_and_exit(status); |
266 | } |
267 |