]>
Commit | Line | Data |
---|---|---|
44bd5ea7 A |
1 | /*- |
2 | * Copyright (c) 1991, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
44bd5ea7 | 34 | #ifndef lint |
9bafe280 A |
35 | static const char copyright[] = |
36 | "@(#) Copyright (c) 1991, 1993\n\ | |
37 | The Regents of the University of California. All rights reserved.\n"; | |
44bd5ea7 A |
38 | #endif /* not lint */ |
39 | ||
40 | #ifndef lint | |
41 | #if 0 | |
9bafe280 | 42 | static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94"; |
44bd5ea7 A |
43 | #endif |
44 | #endif /* not lint */ | |
9bafe280 | 45 | #include <sys/cdefs.h> |
e1a085ba | 46 | __FBSDID("$FreeBSD: src/usr.bin/id/id.c,v 1.33 2006/12/29 12:28:34 stefanf Exp $"); |
44bd5ea7 A |
47 | |
48 | #include <sys/param.h> | |
e1a085ba A |
49 | #ifndef __APPLE__ |
50 | #include <sys/mac.h> | |
51 | #endif /* !__APPLE__ */ | |
52 | ||
53 | #ifdef USE_BSM_AUDIT | |
54 | #include <bsm/audit.h> | |
55 | #endif | |
44bd5ea7 A |
56 | |
57 | #include <err.h> | |
e1a085ba | 58 | #include <errno.h> |
44bd5ea7 A |
59 | #include <grp.h> |
60 | #include <pwd.h> | |
61 | #include <stdio.h> | |
62 | #include <stdlib.h> | |
63 | #include <string.h> | |
64 | #include <unistd.h> | |
65 | ||
e1a085ba | 66 | void id_print(struct passwd *, int, int, int); |
9bafe280 A |
67 | void pline(struct passwd *); |
68 | void pretty(struct passwd *); | |
e1a085ba | 69 | void auditid(void); |
06a885f3 | 70 | void fullname(struct passwd *); |
9bafe280 | 71 | void group(struct passwd *, int); |
e1a085ba | 72 | void maclabel(void); |
9bafe280 | 73 | void usage(void); |
e1a085ba | 74 | struct passwd *who(char *); |
44bd5ea7 | 75 | |
9bafe280 | 76 | int isgroups, iswhoami; |
44bd5ea7 | 77 | |
e1a085ba A |
78 | #ifdef __APPLE__ |
79 | // SPI for 5235093 | |
80 | int32_t getgrouplist_2(const char *, gid_t, gid_t **); | |
81 | #endif | |
82 | ||
44bd5ea7 | 83 | int |
9bafe280 | 84 | main(int argc, char *argv[]) |
44bd5ea7 A |
85 | { |
86 | struct group *gr; | |
87 | struct passwd *pw; | |
e1a085ba A |
88 | int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag; |
89 | int Aflag; | |
06a885f3 | 90 | int Fflag; |
9bafe280 | 91 | const char *myname; |
44bd5ea7 | 92 | |
e1a085ba A |
93 | Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0; |
94 | Aflag = 0; | |
06a885f3 | 95 | Fflag = 0; |
44bd5ea7 | 96 | |
9bafe280 A |
97 | myname = strrchr(argv[0], '/'); |
98 | myname = (myname != NULL) ? myname + 1 : argv[0]; | |
99 | if (strcmp(myname, "groups") == 0) { | |
100 | isgroups = 1; | |
101 | Gflag = nflag = 1; | |
102 | } | |
103 | else if (strcmp(myname, "whoami") == 0) { | |
104 | iswhoami = 1; | |
105 | uflag = nflag = 1; | |
106 | } | |
44bd5ea7 | 107 | |
9bafe280 | 108 | while ((ch = getopt(argc, argv, |
06a885f3 | 109 | (isgroups || iswhoami) ? "" : "AFPGMagnpru")) != -1) |
44bd5ea7 | 110 | switch(ch) { |
e1a085ba A |
111 | #ifdef USE_BSM_AUDIT |
112 | case 'A': | |
113 | Aflag = 1; | |
114 | break; | |
115 | #endif | |
06a885f3 A |
116 | case 'F': |
117 | Fflag = 1; | |
118 | break; | |
44bd5ea7 A |
119 | case 'G': |
120 | Gflag = 1; | |
121 | break; | |
e1a085ba A |
122 | case 'M': |
123 | Mflag = 1; | |
124 | break; | |
9bafe280 A |
125 | case 'P': |
126 | Pflag = 1; | |
127 | break; | |
e1a085ba A |
128 | case 'a': |
129 | break; | |
44bd5ea7 A |
130 | case 'g': |
131 | gflag = 1; | |
132 | break; | |
133 | case 'n': | |
134 | nflag = 1; | |
135 | break; | |
136 | case 'p': | |
137 | pflag = 1; | |
138 | break; | |
139 | case 'r': | |
140 | rflag = 1; | |
141 | break; | |
142 | case 'u': | |
143 | uflag = 1; | |
144 | break; | |
145 | case '?': | |
146 | default: | |
147 | usage(); | |
148 | } | |
149 | argc -= optind; | |
150 | argv += optind; | |
151 | ||
9bafe280 A |
152 | if (iswhoami && argc > 0) |
153 | usage(); | |
154 | ||
06a885f3 | 155 | switch(Aflag + Fflag + Gflag + Mflag + Pflag + gflag + pflag + uflag) { |
44bd5ea7 A |
156 | case 1: |
157 | break; | |
158 | case 0: | |
159 | if (!nflag && !rflag) | |
160 | break; | |
161 | /* FALLTHROUGH */ | |
162 | default: | |
163 | usage(); | |
164 | } | |
165 | ||
166 | pw = *argv ? who(*argv) : NULL; | |
167 | ||
e1a085ba A |
168 | if (Mflag && pw != NULL) |
169 | usage(); | |
170 | ||
171 | #ifdef USE_BSM_AUDIT | |
172 | if (Aflag) { | |
173 | auditid(); | |
174 | exit(0); | |
175 | } | |
176 | #endif | |
177 | ||
06a885f3 A |
178 | if (Fflag) { |
179 | fullname(pw); | |
180 | exit(0); | |
181 | } | |
182 | ||
44bd5ea7 A |
183 | if (gflag) { |
184 | id = pw ? pw->pw_gid : rflag ? getgid() : getegid(); | |
185 | if (nflag && (gr = getgrgid(id))) | |
186 | (void)printf("%s\n", gr->gr_name); | |
187 | else | |
188 | (void)printf("%u\n", id); | |
189 | exit(0); | |
190 | } | |
191 | ||
192 | if (uflag) { | |
193 | id = pw ? pw->pw_uid : rflag ? getuid() : geteuid(); | |
194 | if (nflag && (pw = getpwuid(id))) | |
195 | (void)printf("%s\n", pw->pw_name); | |
196 | else | |
197 | (void)printf("%u\n", id); | |
198 | exit(0); | |
199 | } | |
200 | ||
201 | if (Gflag) { | |
202 | group(pw, nflag); | |
203 | exit(0); | |
204 | } | |
205 | ||
e1a085ba A |
206 | if (Mflag) { |
207 | maclabel(); | |
208 | exit(0); | |
209 | } | |
210 | ||
9bafe280 A |
211 | if (Pflag) { |
212 | pline(pw); | |
213 | exit(0); | |
214 | } | |
215 | ||
44bd5ea7 A |
216 | if (pflag) { |
217 | pretty(pw); | |
218 | exit(0); | |
219 | } | |
220 | ||
e1a085ba A |
221 | if (pw) { |
222 | id_print(pw, 1, 0, 0); | |
223 | } | |
224 | else { | |
225 | id = getuid(); | |
226 | pw = getpwuid(id); | |
227 | id_print(pw, 0, 1, 1); | |
228 | } | |
44bd5ea7 A |
229 | exit(0); |
230 | } | |
231 | ||
232 | void | |
9bafe280 | 233 | pretty(struct passwd *pw) |
44bd5ea7 A |
234 | { |
235 | struct group *gr; | |
236 | u_int eid, rid; | |
237 | char *login; | |
238 | ||
239 | if (pw) { | |
240 | (void)printf("uid\t%s\n", pw->pw_name); | |
241 | (void)printf("groups\t"); | |
242 | group(pw, 1); | |
243 | } else { | |
244 | if ((login = getlogin()) == NULL) | |
245 | err(1, "getlogin"); | |
246 | ||
247 | pw = getpwuid(rid = getuid()); | |
248 | if (pw == NULL || strcmp(login, pw->pw_name)) | |
249 | (void)printf("login\t%s\n", login); | |
250 | if (pw) | |
251 | (void)printf("uid\t%s\n", pw->pw_name); | |
252 | else | |
253 | (void)printf("uid\t%u\n", rid); | |
9bafe280 | 254 | |
44bd5ea7 | 255 | if ((eid = geteuid()) != rid) { |
9bafe280 | 256 | if ((pw = getpwuid(eid))) |
c0fcf4e1 | 257 | (void)printf("euid\t%s\n", pw->pw_name); |
44bd5ea7 | 258 | else |
c0fcf4e1 | 259 | (void)printf("euid\t%u\n", eid); |
44bd5ea7 A |
260 | } |
261 | if ((rid = getgid()) != (eid = getegid())) { | |
9bafe280 | 262 | if ((gr = getgrgid(rid))) |
44bd5ea7 A |
263 | (void)printf("rgid\t%s\n", gr->gr_name); |
264 | else | |
265 | (void)printf("rgid\t%u\n", rid); | |
266 | } | |
267 | (void)printf("groups\t"); | |
268 | group(NULL, 1); | |
269 | } | |
270 | } | |
271 | ||
272 | void | |
e1a085ba | 273 | id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid) |
44bd5ea7 A |
274 | { |
275 | struct group *gr; | |
e1a085ba A |
276 | gid_t gid, egid, lastgid; |
277 | uid_t uid, euid; | |
278 | int cnt, ngroups; | |
279 | #ifdef __APPLE__ | |
ddb4a88b | 280 | gid_t *groups = NULL; |
e1a085ba A |
281 | #else |
282 | gid_t groups[NGROUPS + 1]; | |
283 | #endif | |
9bafe280 | 284 | const char *fmt; |
44bd5ea7 | 285 | |
e1a085ba A |
286 | #ifdef __APPLE__ |
287 | if (pw == NULL) { | |
288 | pw = getpwuid(getuid()); | |
289 | } | |
290 | ||
291 | use_ggl = 1; | |
292 | #endif | |
293 | ||
294 | if (pw != NULL) { | |
295 | uid = pw->pw_uid; | |
296 | gid = pw->pw_gid; | |
297 | } | |
298 | else { | |
299 | uid = getuid(); | |
300 | gid = getgid(); | |
301 | } | |
302 | ||
303 | if (use_ggl && pw != NULL) { | |
304 | #ifdef __APPLE__ | |
305 | // 5235093 | |
306 | ngroups = getgrouplist_2(pw->pw_name, gid, &groups); | |
307 | #else | |
308 | ngroups = NGROUPS + 1; | |
309 | getgrouplist(pw->pw_name, gid, groups, &ngroups); | |
310 | #endif | |
311 | } | |
312 | else { | |
313 | #ifdef __APPLE__ | |
ddb4a88b | 314 | groups = malloc((NGROUPS + 1) * sizeof(gid_t)); |
e1a085ba A |
315 | #endif |
316 | ngroups = getgroups(NGROUPS + 1, groups); | |
317 | } | |
318 | ||
ddb4a88b A |
319 | #ifdef __APPLE__ |
320 | if (ngroups < 0) | |
321 | warn("failed to retrieve group list"); | |
322 | #endif | |
323 | ||
e1a085ba A |
324 | if (pw != NULL) |
325 | printf("uid=%u(%s)", uid, pw->pw_name); | |
326 | else | |
327 | printf("uid=%u", getuid()); | |
328 | printf(" gid=%u", gid); | |
1c4c78a5 A |
329 | if ((gr = getgrgid(gid))) |
330 | (void)printf("(%s)", gr->gr_name); | |
e1a085ba A |
331 | if (p_euid && (euid = geteuid()) != uid) { |
332 | (void)printf(" euid=%u", euid); | |
333 | if ((pw = getpwuid(euid))) | |
44bd5ea7 A |
334 | (void)printf("(%s)", pw->pw_name); |
335 | } | |
e1a085ba | 336 | if (p_egid && (egid = getegid()) != gid) { |
1c4c78a5 A |
337 | (void)printf(" egid=%u", egid); |
338 | if ((gr = getgrgid(egid))) | |
44bd5ea7 A |
339 | (void)printf("(%s)", gr->gr_name); |
340 | } | |
44bd5ea7 | 341 | fmt = " groups=%u"; |
9bafe280 A |
342 | for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) { |
343 | if (lastgid == (gid = groups[cnt])) | |
44bd5ea7 | 344 | continue; |
e1a085ba A |
345 | printf(fmt, gid); |
346 | fmt = ",%u"; | |
9bafe280 | 347 | if ((gr = getgrgid(gid))) |
e1a085ba | 348 | printf("(%s)", gr->gr_name); |
9bafe280 | 349 | lastgid = gid; |
44bd5ea7 | 350 | } |
e1a085ba A |
351 | printf("\n"); |
352 | #ifdef __APPLE__ | |
353 | free(groups); | |
354 | #endif | |
44bd5ea7 A |
355 | } |
356 | ||
e1a085ba A |
357 | #ifdef USE_BSM_AUDIT |
358 | void | |
359 | auditid(void) | |
360 | { | |
f14763b6 | 361 | auditinfo_addr_t auditinfo; |
e1a085ba | 362 | |
f14763b6 | 363 | if (getaudit_addr(&auditinfo, sizeof(auditinfo)) < 0) |
e1a085ba A |
364 | err(1, "getaudit"); |
365 | printf("auid=%d\n", auditinfo.ai_auid); | |
366 | printf("mask.success=0x%08x\n", auditinfo.ai_mask.am_success); | |
367 | printf("mask.failure=0x%08x\n", auditinfo.ai_mask.am_failure); | |
f14763b6 | 368 | printf("termid.port=0x%08x\n", auditinfo.ai_termid.at_port); |
e1a085ba A |
369 | printf("asid=%d\n", auditinfo.ai_asid); |
370 | } | |
371 | #endif | |
372 | ||
06a885f3 A |
373 | void |
374 | fullname(struct passwd *pw) | |
375 | { | |
376 | ||
377 | if (!pw) { | |
378 | if ((pw = getpwuid(getuid())) == NULL) | |
379 | err(1, "getpwuid"); | |
380 | } | |
381 | ||
382 | (void)printf("%s\n", pw->pw_gecos); | |
383 | } | |
384 | ||
44bd5ea7 | 385 | void |
9bafe280 | 386 | group(struct passwd *pw, int nflag) |
44bd5ea7 A |
387 | { |
388 | struct group *gr; | |
389 | int cnt, id, lastid, ngroups; | |
e1a085ba | 390 | #ifdef __APPLE__ |
1e9ba8f2 | 391 | gid_t *groups = NULL; |
e1a085ba | 392 | #else |
44bd5ea7 | 393 | gid_t groups[NGROUPS + 1]; |
e1a085ba | 394 | #endif |
9bafe280 | 395 | const char *fmt; |
44bd5ea7 | 396 | |
e1a085ba A |
397 | #ifdef __APPLE__ |
398 | if (pw == NULL) { | |
399 | pw = getpwuid(getuid()); | |
400 | } | |
401 | #endif | |
402 | ||
44bd5ea7 | 403 | if (pw) { |
e1a085ba A |
404 | #ifdef __APPLE__ |
405 | // 5235093 | |
406 | ngroups = getgrouplist_2(pw->pw_name, pw->pw_gid, &groups); | |
407 | #else | |
44bd5ea7 A |
408 | ngroups = NGROUPS + 1; |
409 | (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); | |
e1a085ba | 410 | #endif |
44bd5ea7 | 411 | } else { |
e1a085ba | 412 | #ifdef __APPLE__ |
06a885f3 | 413 | groups = malloc((NGROUPS + 1) * sizeof(gid_t)); |
e1a085ba | 414 | #endif |
44bd5ea7 A |
415 | groups[0] = getgid(); |
416 | ngroups = getgroups(NGROUPS, groups + 1) + 1; | |
417 | } | |
418 | fmt = nflag ? "%s" : "%u"; | |
419 | for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { | |
420 | if (lastid == (id = groups[cnt])) | |
421 | continue; | |
422 | if (nflag) { | |
9bafe280 | 423 | if ((gr = getgrgid(id))) |
44bd5ea7 A |
424 | (void)printf(fmt, gr->gr_name); |
425 | else | |
426 | (void)printf(*fmt == ' ' ? " %u" : "%u", | |
427 | id); | |
428 | fmt = " %s"; | |
429 | } else { | |
430 | (void)printf(fmt, id); | |
431 | fmt = " %u"; | |
432 | } | |
433 | lastid = id; | |
434 | } | |
435 | (void)printf("\n"); | |
e1a085ba A |
436 | #ifdef __APPLE__ |
437 | free(groups); | |
438 | #endif | |
439 | } | |
440 | ||
441 | void | |
442 | maclabel(void) | |
443 | { | |
444 | #ifdef __APPLE__ | |
445 | errx(1, "-M unsupported"); | |
446 | #else /* !__APPLE__ */ | |
447 | char *string; | |
448 | mac_t label; | |
449 | int error; | |
450 | ||
451 | error = mac_prepare_process_label(&label); | |
452 | if (error == -1) | |
453 | errx(1, "mac_prepare_type: %s", strerror(errno)); | |
454 | ||
455 | error = mac_get_proc(label); | |
456 | if (error == -1) | |
457 | errx(1, "mac_get_proc: %s", strerror(errno)); | |
458 | ||
459 | error = mac_to_text(label, &string); | |
460 | if (error == -1) | |
461 | errx(1, "mac_to_text: %s", strerror(errno)); | |
462 | ||
463 | (void)printf("%s\n", string); | |
464 | mac_free(label); | |
465 | free(string); | |
466 | #endif /* __APPLE__ */ | |
44bd5ea7 A |
467 | } |
468 | ||
469 | struct passwd * | |
9bafe280 | 470 | who(char *u) |
44bd5ea7 A |
471 | { |
472 | struct passwd *pw; | |
473 | long id; | |
474 | char *ep; | |
475 | ||
476 | /* | |
477 | * Translate user argument into a pw pointer. First, try to | |
478 | * get it as specified. If that fails, try it as a number. | |
479 | */ | |
9bafe280 | 480 | if ((pw = getpwnam(u))) |
44bd5ea7 A |
481 | return(pw); |
482 | id = strtol(u, &ep, 10); | |
483 | if (*u && !*ep && (pw = getpwuid(id))) | |
484 | return(pw); | |
9bafe280 | 485 | errx(1, "%s: no such user", u); |
44bd5ea7 | 486 | /* NOTREACHED */ |
44bd5ea7 A |
487 | } |
488 | ||
489 | void | |
9bafe280 A |
490 | pline(struct passwd *pw) |
491 | { | |
9bafe280 A |
492 | |
493 | if (!pw) { | |
e1a085ba | 494 | if ((pw = getpwuid(getuid())) == NULL) |
9bafe280 A |
495 | err(1, "getpwuid"); |
496 | } | |
497 | ||
498 | (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name, | |
499 | pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, | |
500 | (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, | |
501 | pw->pw_dir, pw->pw_shell); | |
502 | } | |
503 | ||
504 | ||
505 | void | |
506 | usage(void) | |
44bd5ea7 | 507 | { |
9bafe280 A |
508 | |
509 | if (isgroups) | |
510 | (void)fprintf(stderr, "usage: groups [user]\n"); | |
511 | else if (iswhoami) | |
512 | (void)fprintf(stderr, "usage: whoami\n"); | |
513 | else | |
06a885f3 | 514 | (void)fprintf(stderr, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n", |
9bafe280 | 515 | "usage: id [user]", |
e1a085ba A |
516 | #ifdef USE_BSM_AUDIT |
517 | " id -A\n", | |
518 | #else | |
519 | "", | |
520 | #endif | |
06a885f3 | 521 | " id -F [user]", |
9bafe280 | 522 | " id -G [-n] [user]", |
e1a085ba | 523 | " id -M", |
9bafe280 A |
524 | " id -P [user]", |
525 | " id -g [-nr] [user]", | |
526 | " id -p [user]", | |
527 | " id -u [-nr] [user]"); | |
44bd5ea7 A |
528 | exit(1); |
529 | } |