2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright
[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)id.c 8.2 (Berkeley) 2/16/94";
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 $");
48 #include <sys/param.h>
51 #endif /* !__APPLE__ */
54 #include <bsm/audit.h>
66 void id_print(struct passwd
*, int, int, int);
67 void pline(struct passwd
*);
68 void pretty(struct passwd
*);
70 void fullname(struct passwd
*);
71 void group(struct passwd
*, int);
74 struct passwd
*who(char *);
76 int isgroups
, iswhoami
;
80 int32_t getgrouplist_2(const char *, gid_t
, gid_t
**);
84 main(int argc
, char *argv
[])
88 int Gflag
, Mflag
, Pflag
, ch
, gflag
, id
, nflag
, pflag
, rflag
, uflag
;
93 Gflag
= Mflag
= Pflag
= gflag
= nflag
= pflag
= rflag
= uflag
= 0;
97 myname
= strrchr(argv
[0], '/');
98 myname
= (myname
!= NULL
) ? myname
+ 1 : argv
[0];
99 if (strcmp(myname
, "groups") == 0) {
103 else if (strcmp(myname
, "whoami") == 0) {
108 while ((ch
= getopt(argc
, argv
,
109 (isgroups
|| iswhoami
) ? "" : "AFPGMagnpru")) != -1)
152 if (iswhoami
&& argc
> 0)
155 switch(Aflag
+ Fflag
+ Gflag
+ Mflag
+ Pflag
+ gflag
+ pflag
+ uflag
) {
159 if (!nflag
&& !rflag
)
166 pw
= *argv
? who(*argv
) : NULL
;
168 if (Mflag
&& pw
!= NULL
)
184 id
= pw
? pw
->pw_gid
: rflag
? getgid() : getegid();
185 if (nflag
&& (gr
= getgrgid(id
)))
186 (void)printf("%s\n", gr
->gr_name
);
188 (void)printf("%u\n", id
);
193 id
= pw
? pw
->pw_uid
: rflag
? getuid() : geteuid();
194 if (nflag
&& (pw
= getpwuid(id
)))
195 (void)printf("%s\n", pw
->pw_name
);
197 (void)printf("%u\n", id
);
222 id_print(pw
, 1, 0, 0);
227 id_print(pw
, 0, 1, 1);
233 pretty(struct passwd
*pw
)
240 (void)printf("uid\t%s\n", pw
->pw_name
);
241 (void)printf("groups\t");
244 if ((login
= getlogin()) == NULL
)
247 pw
= getpwuid(rid
= getuid());
248 if (pw
== NULL
|| strcmp(login
, pw
->pw_name
))
249 (void)printf("login\t%s\n", login
);
251 (void)printf("uid\t%s\n", pw
->pw_name
);
253 (void)printf("uid\t%u\n", rid
);
255 if ((eid
= geteuid()) != rid
) {
256 if ((pw
= getpwuid(eid
)))
257 (void)printf("euid\t%s\n", pw
->pw_name
);
259 (void)printf("euid\t%u\n", eid
);
261 if ((rid
= getgid()) != (eid
= getegid())) {
262 if ((gr
= getgrgid(rid
)))
263 (void)printf("rgid\t%s\n", gr
->gr_name
);
265 (void)printf("rgid\t%u\n", rid
);
267 (void)printf("groups\t");
273 id_print(struct passwd
*pw
, int use_ggl
, int p_euid
, int p_egid
)
276 gid_t gid
, egid
, lastgid
;
280 gid_t
*groups
= NULL
;
282 gid_t groups
[NGROUPS
+ 1];
288 pw
= getpwuid(getuid());
303 if (use_ggl
&& pw
!= NULL
) {
306 ngroups
= getgrouplist_2(pw
->pw_name
, gid
, &groups
);
308 ngroups
= NGROUPS
+ 1;
309 getgrouplist(pw
->pw_name
, gid
, groups
, &ngroups
);
314 groups
= malloc((NGROUPS
+ 1) * sizeof(gid_t
));
316 ngroups
= getgroups(NGROUPS
+ 1, groups
);
321 warn("failed to retrieve group list");
325 printf("uid=%u(%s)", uid
, pw
->pw_name
);
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
);
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
);
342 for (lastgid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
343 if (lastgid
== (gid
= groups
[cnt
]))
347 if ((gr
= getgrgid(gid
)))
348 printf("(%s)", gr
->gr_name
);
361 auditinfo_addr_t auditinfo
;
363 if (getaudit_addr(&auditinfo
, sizeof(auditinfo
)) < 0)
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
);
374 fullname(struct passwd
*pw
)
378 if ((pw
= getpwuid(getuid())) == NULL
)
382 (void)printf("%s\n", pw
->pw_gecos
);
386 group(struct passwd
*pw
, int nflag
)
389 int cnt
, id
, lastid
, ngroups
;
391 gid_t
*groups
= NULL
;
393 gid_t groups
[NGROUPS
+ 1];
399 pw
= getpwuid(getuid());
406 ngroups
= getgrouplist_2(pw
->pw_name
, pw
->pw_gid
, &groups
);
408 ngroups
= NGROUPS
+ 1;
409 (void) getgrouplist(pw
->pw_name
, pw
->pw_gid
, groups
, &ngroups
);
413 groups
= malloc((NGROUPS
+ 1) * sizeof(gid_t
));
415 groups
[0] = getgid();
416 ngroups
= getgroups(NGROUPS
, groups
+ 1) + 1;
418 fmt
= nflag
? "%s" : "%u";
419 for (lastid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
420 if (lastid
== (id
= groups
[cnt
]))
423 if ((gr
= getgrgid(id
)))
424 (void)printf(fmt
, gr
->gr_name
);
426 (void)printf(*fmt
== ' ' ? " %u" : "%u",
430 (void)printf(fmt
, id
);
445 errx(1, "-M unsupported");
446 #else /* !__APPLE__ */
451 error
= mac_prepare_process_label(&label
);
453 errx(1, "mac_prepare_type: %s", strerror(errno
));
455 error
= mac_get_proc(label
);
457 errx(1, "mac_get_proc: %s", strerror(errno
));
459 error
= mac_to_text(label
, &string
);
461 errx(1, "mac_to_text: %s", strerror(errno
));
463 (void)printf("%s\n", string
);
466 #endif /* __APPLE__ */
477 * Translate user argument into a pw pointer. First, try to
478 * get it as specified. If that fails, try it as a number.
480 if ((pw
= getpwnam(u
)))
482 id
= strtol(u
, &ep
, 10);
483 if (*u
&& !*ep
&& (pw
= getpwuid(id
)))
485 errx(1, "%s: no such user", u
);
490 pline(struct passwd
*pw
)
494 if ((pw
= getpwuid(getuid())) == NULL
)
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
);
510 (void)fprintf(stderr
, "usage: groups [user]\n");
512 (void)fprintf(stderr
, "usage: whoami\n");
514 (void)fprintf(stderr
, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
522 " id -G [-n] [user]",
525 " id -g [-nr] [user]",
527 " id -u [-nr] [user]");