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#include "libbb.h"
62
63
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
71enum {
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
82static 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
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
105static int print_group(gid_t id, const char *prefix)
106{
107 return print_common(id, gid2group(id), prefix);
108}
109
110static int print_user(uid_t id, const char *prefix)
111{
112 return print_common(id, uid2uname(id), prefix);
113}
114
115
116
117
118
119
120static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
121{
122 int m;
123
124 if (username) {
125
126
127
128 m = getgrouplist(username, rgid, groups, n);
129
130
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
140 if (errno == EINVAL)
141 *n = getgroups(0, groups);
142
143 return -(*n >= 0);
144}
145
146int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
147int 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
164
165
166
167
168
169 opt = option_mask32 = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER;
170 } else {
171
172
173 opt = getopt32(argv, "^"
174 "rnugG" IF_SELINUX("Z")
175 "\0"
176 "?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 );
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_simple_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_stdout(scontext);
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