]> git.saurik.com Git - apple/shell_cmds.git/blob - id/id.c
shell_cmds-198.tar.gz
[apple/shell_cmds.git] / id / id.c
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
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD: src/usr.bin/id/id.c,v 1.33 2006/12/29 12:28:34 stefanf Exp $");
47
48 #include <sys/param.h>
49 #ifndef __APPLE__
50 #include <sys/mac.h>
51 #endif /* !__APPLE__ */
52
53 #ifdef USE_BSM_AUDIT
54 #include <bsm/audit.h>
55 #endif
56
57 #include <err.h>
58 #include <errno.h>
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
66 void id_print(struct passwd *, int, int, int);
67 void pline(struct passwd *);
68 void pretty(struct passwd *);
69 void auditid(void);
70 void fullname(struct passwd *);
71 void group(struct passwd *, int);
72 void maclabel(void);
73 void usage(void);
74 struct passwd *who(char *);
75
76 int isgroups, iswhoami;
77
78 #ifdef __APPLE__
79 // SPI for 5235093
80 int32_t getgrouplist_2(const char *, gid_t, gid_t **);
81 #endif
82
83 int
84 main(int argc, char *argv[])
85 {
86 struct group *gr;
87 struct passwd *pw;
88 int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
89 int Aflag;
90 int Fflag;
91 const char *myname;
92
93 Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
94 Aflag = 0;
95 Fflag = 0;
96
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 }
107
108 while ((ch = getopt(argc, argv,
109 (isgroups || iswhoami) ? "" : "AFPGMagnpru")) != -1)
110 switch(ch) {
111 #ifdef USE_BSM_AUDIT
112 case 'A':
113 Aflag = 1;
114 break;
115 #endif
116 case 'F':
117 Fflag = 1;
118 break;
119 case 'G':
120 Gflag = 1;
121 break;
122 case 'M':
123 Mflag = 1;
124 break;
125 case 'P':
126 Pflag = 1;
127 break;
128 case 'a':
129 break;
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
152 if (iswhoami && argc > 0)
153 usage();
154
155 switch(Aflag + Fflag + Gflag + Mflag + Pflag + gflag + pflag + uflag) {
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
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
178 if (Fflag) {
179 fullname(pw);
180 exit(0);
181 }
182
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
206 if (Mflag) {
207 maclabel();
208 exit(0);
209 }
210
211 if (Pflag) {
212 pline(pw);
213 exit(0);
214 }
215
216 if (pflag) {
217 pretty(pw);
218 exit(0);
219 }
220
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 }
229 exit(0);
230 }
231
232 void
233 pretty(struct passwd *pw)
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);
254
255 if ((eid = geteuid()) != rid) {
256 if ((pw = getpwuid(eid)))
257 (void)printf("euid\t%s\n", pw->pw_name);
258 else
259 (void)printf("euid\t%u\n", eid);
260 }
261 if ((rid = getgid()) != (eid = getegid())) {
262 if ((gr = getgrgid(rid)))
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
273 id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
274 {
275 struct group *gr;
276 gid_t gid, egid, lastgid;
277 uid_t uid, euid;
278 int cnt, ngroups;
279 #ifdef __APPLE__
280 gid_t *groups = NULL;
281 #else
282 gid_t groups[NGROUPS + 1];
283 #endif
284 const char *fmt;
285
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__
314 groups = malloc((NGROUPS + 1) * sizeof(gid_t));
315 #endif
316 ngroups = getgroups(NGROUPS + 1, groups);
317 }
318
319 #ifdef __APPLE__
320 if (ngroups < 0)
321 warn("failed to retrieve group list");
322 #endif
323
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);
329 if ((gr = getgrgid(gid)))
330 (void)printf("(%s)", gr->gr_name);
331 if (p_euid && (euid = geteuid()) != uid) {
332 (void)printf(" euid=%u", euid);
333 if ((pw = getpwuid(euid)))
334 (void)printf("(%s)", pw->pw_name);
335 }
336 if (p_egid && (egid = getegid()) != gid) {
337 (void)printf(" egid=%u", egid);
338 if ((gr = getgrgid(egid)))
339 (void)printf("(%s)", gr->gr_name);
340 }
341 fmt = " groups=%u";
342 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
343 if (lastgid == (gid = groups[cnt]))
344 continue;
345 printf(fmt, gid);
346 fmt = ",%u";
347 if ((gr = getgrgid(gid)))
348 printf("(%s)", gr->gr_name);
349 lastgid = gid;
350 }
351 printf("\n");
352 #ifdef __APPLE__
353 free(groups);
354 #endif
355 }
356
357 #ifdef USE_BSM_AUDIT
358 void
359 auditid(void)
360 {
361 auditinfo_addr_t auditinfo;
362
363 if (getaudit_addr(&auditinfo, sizeof(auditinfo)) < 0)
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);
368 printf("termid.port=0x%08x\n", auditinfo.ai_termid.at_port);
369 printf("asid=%d\n", auditinfo.ai_asid);
370 }
371 #endif
372
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
385 void
386 group(struct passwd *pw, int nflag)
387 {
388 struct group *gr;
389 int cnt, id, lastid, ngroups;
390 #ifdef __APPLE__
391 gid_t *groups = NULL;
392 #else
393 gid_t groups[NGROUPS + 1];
394 #endif
395 const char *fmt;
396
397 #ifdef __APPLE__
398 if (pw == NULL) {
399 pw = getpwuid(getuid());
400 }
401 #endif
402
403 if (pw) {
404 #ifdef __APPLE__
405 // 5235093
406 ngroups = getgrouplist_2(pw->pw_name, pw->pw_gid, &groups);
407 #else
408 ngroups = NGROUPS + 1;
409 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
410 #endif
411 } else {
412 #ifdef __APPLE__
413 groups = malloc((NGROUPS + 1) * sizeof(gid_t));
414 #endif
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) {
423 if ((gr = getgrgid(id)))
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");
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__ */
467 }
468
469 struct passwd *
470 who(char *u)
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 */
480 if ((pw = getpwnam(u)))
481 return(pw);
482 id = strtol(u, &ep, 10);
483 if (*u && !*ep && (pw = getpwuid(id)))
484 return(pw);
485 errx(1, "%s: no such user", u);
486 /* NOTREACHED */
487 }
488
489 void
490 pline(struct passwd *pw)
491 {
492
493 if (!pw) {
494 if ((pw = getpwuid(getuid())) == NULL)
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)
507 {
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
514 (void)fprintf(stderr, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
515 "usage: id [user]",
516 #ifdef USE_BSM_AUDIT
517 " id -A\n",
518 #else
519 "",
520 #endif
521 " id -F [user]",
522 " id -G [-n] [user]",
523 " id -M",
524 " id -P [user]",
525 " id -g [-nr] [user]",
526 " id -p [user]",
527 " id -u [-nr] [user]");
528 exit(1);
529 }