]> git.saurik.com Git - apple/shell_cmds.git/blame - id/id.c
shell_cmds-116.tar.gz
[apple/shell_cmds.git] / id / id.c
CommitLineData
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
35static 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 42static 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 66void id_print(struct passwd *, int, int, int);
9bafe280
A
67void pline(struct passwd *);
68void pretty(struct passwd *);
e1a085ba 69void auditid(void);
9bafe280 70void group(struct passwd *, int);
e1a085ba 71void maclabel(void);
9bafe280 72void usage(void);
e1a085ba 73struct passwd *who(char *);
44bd5ea7 74
9bafe280 75int isgroups, iswhoami;
44bd5ea7 76
e1a085ba
A
77#ifdef __APPLE__
78// SPI for 5235093
79int32_t getgrouplist_2(const char *, gid_t, gid_t **);
80#endif
81
44bd5ea7 82int
9bafe280 83main(int argc, char *argv[])
44bd5ea7
A
84{
85 struct group *gr;
86 struct passwd *pw;
e1a085ba
A
87 int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
88 int Aflag;
9bafe280 89 const char *myname;
44bd5ea7 90
e1a085ba
A
91 Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
92 Aflag = 0;
44bd5ea7 93
9bafe280
A
94 myname = strrchr(argv[0], '/');
95 myname = (myname != NULL) ? myname + 1 : argv[0];
96 if (strcmp(myname, "groups") == 0) {
97 isgroups = 1;
98 Gflag = nflag = 1;
99 }
100 else if (strcmp(myname, "whoami") == 0) {
101 iswhoami = 1;
102 uflag = nflag = 1;
103 }
44bd5ea7 104
9bafe280 105 while ((ch = getopt(argc, argv,
e1a085ba 106 (isgroups || iswhoami) ? "" : "APGMagnpru")) != -1)
44bd5ea7 107 switch(ch) {
e1a085ba
A
108#ifdef USE_BSM_AUDIT
109 case 'A':
110 Aflag = 1;
111 break;
112#endif
44bd5ea7
A
113 case 'G':
114 Gflag = 1;
115 break;
e1a085ba
A
116 case 'M':
117 Mflag = 1;
118 break;
9bafe280
A
119 case 'P':
120 Pflag = 1;
121 break;
e1a085ba
A
122 case 'a':
123 break;
44bd5ea7
A
124 case 'g':
125 gflag = 1;
126 break;
127 case 'n':
128 nflag = 1;
129 break;
130 case 'p':
131 pflag = 1;
132 break;
133 case 'r':
134 rflag = 1;
135 break;
136 case 'u':
137 uflag = 1;
138 break;
139 case '?':
140 default:
141 usage();
142 }
143 argc -= optind;
144 argv += optind;
145
9bafe280
A
146 if (iswhoami && argc > 0)
147 usage();
148
e1a085ba 149 switch(Aflag + Gflag + Mflag + Pflag + gflag + pflag + uflag) {
44bd5ea7
A
150 case 1:
151 break;
152 case 0:
153 if (!nflag && !rflag)
154 break;
155 /* FALLTHROUGH */
156 default:
157 usage();
158 }
159
160 pw = *argv ? who(*argv) : NULL;
161
e1a085ba
A
162 if (Mflag && pw != NULL)
163 usage();
164
165#ifdef USE_BSM_AUDIT
166 if (Aflag) {
167 auditid();
168 exit(0);
169 }
170#endif
171
44bd5ea7
A
172 if (gflag) {
173 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
174 if (nflag && (gr = getgrgid(id)))
175 (void)printf("%s\n", gr->gr_name);
176 else
177 (void)printf("%u\n", id);
178 exit(0);
179 }
180
181 if (uflag) {
182 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
183 if (nflag && (pw = getpwuid(id)))
184 (void)printf("%s\n", pw->pw_name);
185 else
186 (void)printf("%u\n", id);
187 exit(0);
188 }
189
190 if (Gflag) {
191 group(pw, nflag);
192 exit(0);
193 }
194
e1a085ba
A
195 if (Mflag) {
196 maclabel();
197 exit(0);
198 }
199
9bafe280
A
200 if (Pflag) {
201 pline(pw);
202 exit(0);
203 }
204
44bd5ea7
A
205 if (pflag) {
206 pretty(pw);
207 exit(0);
208 }
209
e1a085ba
A
210 if (pw) {
211 id_print(pw, 1, 0, 0);
212 }
213 else {
214 id = getuid();
215 pw = getpwuid(id);
216 id_print(pw, 0, 1, 1);
217 }
44bd5ea7
A
218 exit(0);
219}
220
221void
9bafe280 222pretty(struct passwd *pw)
44bd5ea7
A
223{
224 struct group *gr;
225 u_int eid, rid;
226 char *login;
227
228 if (pw) {
229 (void)printf("uid\t%s\n", pw->pw_name);
230 (void)printf("groups\t");
231 group(pw, 1);
232 } else {
233 if ((login = getlogin()) == NULL)
234 err(1, "getlogin");
235
236 pw = getpwuid(rid = getuid());
237 if (pw == NULL || strcmp(login, pw->pw_name))
238 (void)printf("login\t%s\n", login);
239 if (pw)
240 (void)printf("uid\t%s\n", pw->pw_name);
241 else
242 (void)printf("uid\t%u\n", rid);
9bafe280 243
44bd5ea7 244 if ((eid = geteuid()) != rid) {
9bafe280 245 if ((pw = getpwuid(eid)))
c0fcf4e1 246 (void)printf("euid\t%s\n", pw->pw_name);
44bd5ea7 247 else
c0fcf4e1 248 (void)printf("euid\t%u\n", eid);
44bd5ea7
A
249 }
250 if ((rid = getgid()) != (eid = getegid())) {
9bafe280 251 if ((gr = getgrgid(rid)))
44bd5ea7
A
252 (void)printf("rgid\t%s\n", gr->gr_name);
253 else
254 (void)printf("rgid\t%u\n", rid);
255 }
256 (void)printf("groups\t");
257 group(NULL, 1);
258 }
259}
260
261void
e1a085ba 262id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
44bd5ea7
A
263{
264 struct group *gr;
e1a085ba
A
265 gid_t gid, egid, lastgid;
266 uid_t uid, euid;
267 int cnt, ngroups;
268#ifdef __APPLE__
269 gid_t *groups;
270#else
271 gid_t groups[NGROUPS + 1];
272#endif
9bafe280 273 const char *fmt;
44bd5ea7 274
e1a085ba
A
275#ifdef __APPLE__
276 if (pw == NULL) {
277 pw = getpwuid(getuid());
278 }
279
280 use_ggl = 1;
281#endif
282
283 if (pw != NULL) {
284 uid = pw->pw_uid;
285 gid = pw->pw_gid;
286 }
287 else {
288 uid = getuid();
289 gid = getgid();
290 }
291
292 if (use_ggl && pw != NULL) {
293#ifdef __APPLE__
294 // 5235093
295 ngroups = getgrouplist_2(pw->pw_name, gid, &groups);
296#else
297 ngroups = NGROUPS + 1;
298 getgrouplist(pw->pw_name, gid, groups, &ngroups);
299#endif
300 }
301 else {
302#ifdef __APPLE__
303 groups = malloc(NGROUPS + 1);
304#endif
305 ngroups = getgroups(NGROUPS + 1, groups);
306 }
307
308 if (pw != NULL)
309 printf("uid=%u(%s)", uid, pw->pw_name);
310 else
311 printf("uid=%u", getuid());
312 printf(" gid=%u", gid);
1c4c78a5
A
313 if ((gr = getgrgid(gid)))
314 (void)printf("(%s)", gr->gr_name);
e1a085ba
A
315 if (p_euid && (euid = geteuid()) != uid) {
316 (void)printf(" euid=%u", euid);
317 if ((pw = getpwuid(euid)))
44bd5ea7
A
318 (void)printf("(%s)", pw->pw_name);
319 }
e1a085ba 320 if (p_egid && (egid = getegid()) != gid) {
1c4c78a5
A
321 (void)printf(" egid=%u", egid);
322 if ((gr = getgrgid(egid)))
44bd5ea7
A
323 (void)printf("(%s)", gr->gr_name);
324 }
44bd5ea7 325 fmt = " groups=%u";
9bafe280
A
326 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
327 if (lastgid == (gid = groups[cnt]))
44bd5ea7 328 continue;
e1a085ba
A
329 printf(fmt, gid);
330 fmt = ",%u";
9bafe280 331 if ((gr = getgrgid(gid)))
e1a085ba 332 printf("(%s)", gr->gr_name);
9bafe280 333 lastgid = gid;
44bd5ea7 334 }
e1a085ba
A
335 printf("\n");
336#ifdef __APPLE__
337 free(groups);
338#endif
44bd5ea7
A
339}
340
e1a085ba
A
341#ifdef USE_BSM_AUDIT
342void
343auditid(void)
344{
345 auditinfo_t auditinfo;
346
347 if (getaudit(&auditinfo) < 0)
348 err(1, "getaudit");
349 printf("auid=%d\n", auditinfo.ai_auid);
350 printf("mask.success=0x%08x\n", auditinfo.ai_mask.am_success);
351 printf("mask.failure=0x%08x\n", auditinfo.ai_mask.am_failure);
352 printf("termid.port=0x%08x\n", auditinfo.ai_termid.port);
353 printf("asid=%d\n", auditinfo.ai_asid);
354}
355#endif
356
44bd5ea7 357void
9bafe280 358group(struct passwd *pw, int nflag)
44bd5ea7
A
359{
360 struct group *gr;
361 int cnt, id, lastid, ngroups;
e1a085ba
A
362#ifdef __APPLE__
363 gid_t *groups;
364#else
44bd5ea7 365 gid_t groups[NGROUPS + 1];
e1a085ba 366#endif
9bafe280 367 const char *fmt;
44bd5ea7 368
e1a085ba
A
369#ifdef __APPLE__
370 if (pw == NULL) {
371 pw = getpwuid(getuid());
372 }
373#endif
374
44bd5ea7 375 if (pw) {
e1a085ba
A
376#ifdef __APPLE__
377 // 5235093
378 ngroups = getgrouplist_2(pw->pw_name, pw->pw_gid, &groups);
379#else
44bd5ea7
A
380 ngroups = NGROUPS + 1;
381 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
e1a085ba 382#endif
44bd5ea7 383 } else {
e1a085ba
A
384#ifdef __APPLE__
385 groups = malloc(NGROUPS + 1);
386#endif
44bd5ea7
A
387 groups[0] = getgid();
388 ngroups = getgroups(NGROUPS, groups + 1) + 1;
389 }
390 fmt = nflag ? "%s" : "%u";
391 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
392 if (lastid == (id = groups[cnt]))
393 continue;
394 if (nflag) {
9bafe280 395 if ((gr = getgrgid(id)))
44bd5ea7
A
396 (void)printf(fmt, gr->gr_name);
397 else
398 (void)printf(*fmt == ' ' ? " %u" : "%u",
399 id);
400 fmt = " %s";
401 } else {
402 (void)printf(fmt, id);
403 fmt = " %u";
404 }
405 lastid = id;
406 }
407 (void)printf("\n");
e1a085ba
A
408#ifdef __APPLE__
409 free(groups);
410#endif
411}
412
413void
414maclabel(void)
415{
416#ifdef __APPLE__
417 errx(1, "-M unsupported");
418#else /* !__APPLE__ */
419 char *string;
420 mac_t label;
421 int error;
422
423 error = mac_prepare_process_label(&label);
424 if (error == -1)
425 errx(1, "mac_prepare_type: %s", strerror(errno));
426
427 error = mac_get_proc(label);
428 if (error == -1)
429 errx(1, "mac_get_proc: %s", strerror(errno));
430
431 error = mac_to_text(label, &string);
432 if (error == -1)
433 errx(1, "mac_to_text: %s", strerror(errno));
434
435 (void)printf("%s\n", string);
436 mac_free(label);
437 free(string);
438#endif /* __APPLE__ */
44bd5ea7
A
439}
440
441struct passwd *
9bafe280 442who(char *u)
44bd5ea7
A
443{
444 struct passwd *pw;
445 long id;
446 char *ep;
447
448 /*
449 * Translate user argument into a pw pointer. First, try to
450 * get it as specified. If that fails, try it as a number.
451 */
9bafe280 452 if ((pw = getpwnam(u)))
44bd5ea7
A
453 return(pw);
454 id = strtol(u, &ep, 10);
455 if (*u && !*ep && (pw = getpwuid(id)))
456 return(pw);
9bafe280 457 errx(1, "%s: no such user", u);
44bd5ea7 458 /* NOTREACHED */
44bd5ea7
A
459}
460
461void
9bafe280
A
462pline(struct passwd *pw)
463{
9bafe280
A
464
465 if (!pw) {
e1a085ba 466 if ((pw = getpwuid(getuid())) == NULL)
9bafe280
A
467 err(1, "getpwuid");
468 }
469
470 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
471 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
472 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
473 pw->pw_dir, pw->pw_shell);
474}
475
476
477void
478usage(void)
44bd5ea7 479{
9bafe280
A
480
481 if (isgroups)
482 (void)fprintf(stderr, "usage: groups [user]\n");
483 else if (iswhoami)
484 (void)fprintf(stderr, "usage: whoami\n");
485 else
e1a085ba 486 (void)fprintf(stderr, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n",
9bafe280 487 "usage: id [user]",
e1a085ba
A
488#ifdef USE_BSM_AUDIT
489 " id -A\n",
490#else
491 "",
492#endif
9bafe280 493 " id -G [-n] [user]",
e1a085ba 494 " id -M",
9bafe280
A
495 " id -P [user]",
496 " id -g [-nr] [user]",
497 " id -p [user]",
498 " id -u [-nr] [user]");
44bd5ea7
A
499 exit(1);
500}