1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62#include "libbb.h"
63
64
65
66#if !ENABLE_USE_BB_PWD_GRP
67#if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0)
68#if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 30)
69#error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
70#endif
71#endif
72#endif
73
74enum {
75 PRINT_REAL = (1 << 0),
76 NAME_NOT_NUMBER = (1 << 1),
77 JUST_USER = (1 << 2),
78 JUST_GROUP = (1 << 3),
79 JUST_ALL_GROUPS = (1 << 4),
80#if ENABLE_SELINUX
81 JUST_CONTEXT = (1 << 5),
82#endif
83};
84
85static int print_common(unsigned id, const char *name, const char *prefix)
86{
87 if (prefix) {
88 printf("%s", prefix);
89 }
90 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
91 printf("%u", id);
92 }
93 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
94 if (name) {
95 printf(option_mask32 ? "%s" : "(%s)", name);
96 } else {
97
98 if (option_mask32) {
99 if (ENABLE_DESKTOP)
100 bb_error_msg("unknown ID %u", id);
101 return EXIT_FAILURE;
102 }
103 }
104 }
105 return EXIT_SUCCESS;
106}
107
108static int print_group(gid_t id, const char *prefix)
109{
110 return print_common(id, gid2group(id), prefix);
111}
112
113static int print_user(uid_t id, const char *prefix)
114{
115 return print_common(id, uid2uname(id), prefix);
116}
117
118
119
120
121
122
123static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
124{
125 int m;
126
127 if (username) {
128
129
130
131 m = getgrouplist(username, rgid, groups, n);
132
133
134 if (*n < 0)
135 return 0;
136 return m;
137 }
138
139 *n = getgroups(*n, groups);
140 if (*n >= 0)
141 return *n;
142
143 if (errno == EINVAL)
144 *n = getgroups(0, groups);
145
146 return -(*n >= 0);
147}
148
149int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
150int id_main(int argc UNUSED_PARAM, char **argv)
151{
152 uid_t ruid;
153 gid_t rgid;
154 uid_t euid;
155 gid_t egid;
156 unsigned opt;
157 int i;
158 int status = EXIT_SUCCESS;
159 const char *prefix;
160 const char *username;
161#if ENABLE_SELINUX
162 security_context_t scontext = NULL;
163#endif
164
165 if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) {
166
167
168
169
170
171
172 opt = option_mask32 = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER;
173 } else {
174
175
176 opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
177 IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
178 opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
179 }
180
181 username = argv[optind];
182 if (username) {
183 struct passwd *p = xgetpwnam(username);
184 euid = ruid = p->pw_uid;
185 egid = rgid = p->pw_gid;
186 } else {
187 egid = getegid();
188 rgid = getgid();
189 euid = geteuid();
190 ruid = getuid();
191 }
192
193
194
195 if (!opt || (opt & JUST_ALL_GROUPS)) {
196 gid_t *groups;
197 int n;
198
199 if (!opt) {
200
201 status |= print_user(ruid, "uid=");
202 status |= print_group(rgid, " gid=");
203 if (euid != ruid)
204 status |= print_user(euid, " euid=");
205 if (egid != rgid)
206 status |= print_group(egid, " egid=");
207 } else {
208
209 status |= print_group(rgid, NULL);
210 if (egid != rgid)
211 status |= print_group(egid, " ");
212 }
213
214
215
216 groups = xmalloc(64 * sizeof(groups[0]));
217 n = 64;
218 if (get_groups(username, rgid, groups, &n) < 0) {
219
220 groups = xrealloc(groups, n * sizeof(groups[0]));
221 get_groups(username, rgid, groups, &n);
222 }
223 if (n > 0) {
224
225 prefix = " groups=";
226 for (i = 0; i < n; i++) {
227 if (opt && (groups[i] == rgid || groups[i] == egid))
228 continue;
229 status |= print_group(groups[i], opt ? " " : prefix);
230 prefix = ",";
231 }
232 } else if (n < 0) {
233 if (ENABLE_DESKTOP)
234 bb_error_msg_and_die("can't get groups");
235 return EXIT_FAILURE;
236 }
237 if (ENABLE_FEATURE_CLEAN_UP)
238 free(groups);
239#if ENABLE_SELINUX
240 if (is_selinux_enabled()) {
241 if (getcon(&scontext) == 0)
242 printf(" context=%s", scontext);
243 }
244#endif
245 } else if (opt & PRINT_REAL) {
246 euid = ruid;
247 egid = rgid;
248 }
249
250 if (opt & JUST_USER)
251 status |= print_user(euid, NULL);
252 else if (opt & JUST_GROUP)
253 status |= print_group(egid, NULL);
254#if ENABLE_SELINUX
255 else if (opt & JUST_CONTEXT) {
256 selinux_or_die();
257 if (username || getcon(&scontext)) {
258 bb_error_msg_and_die("can't get process context%s",
259 username ? " for a different user" : "");
260 }
261 fputs(scontext, stdout);
262 }
263
264 if (ENABLE_FEATURE_CLEAN_UP)
265 freecon(scontext);
266#endif
267 bb_putchar('\n');
268 fflush_stdout_and_exit(status);
269}
270