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$");
48 #include <sys/param.h>
60 void id_print(struct passwd *, int, int, int);
61 void pline(struct passwd *);
62 void pretty(struct passwd *);
63 void group(struct passwd *, int);
66 struct passwd *who(char *);
68 int isgroups, iswhoami;
71 main(int argc, char *argv[])
75 int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
78 Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
80 myname = strrchr(argv[0], '/');
81 myname = (myname != NULL) ? myname + 1 : argv[0];
82 if (strcmp(myname, "groups") == 0) {
86 else if (strcmp(myname, "whoami") == 0) {
91 while ((ch = getopt(argc, argv,
92 (isgroups || iswhoami) ? "" : "PGMgnpru")) != -1)
125 if (iswhoami && argc > 0)
128 switch(Gflag + Pflag + gflag + pflag + uflag) {
132 if (!nflag && !rflag)
139 pw = *argv ? who(*argv) : NULL;
141 if (Mflag && pw != NULL)
145 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
146 if (nflag && (gr = getgrgid(id)))
147 (void)printf("%s\n", gr->gr_name);
149 (void)printf("%u\n", id);
154 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
155 if (nflag && (pw = getpwuid(id)))
156 (void)printf("%s\n", pw->pw_name);
158 (void)printf("%u\n", id);
183 id_print(pw, 1, 0, 0);
187 if ((pw = getpwuid(id)) != NULL)
188 id_print(pw, 0, 1, 1);
194 pretty(struct passwd *pw)
201 (void)printf("uid\t%s\n", pw->pw_name);
202 (void)printf("groups\t");
205 if ((login = getlogin()) == NULL)
208 pw = getpwuid(rid = getuid());
209 if (pw == NULL || strcmp(login, pw->pw_name))
210 (void)printf("login\t%s\n", login);
212 (void)printf("uid\t%s\n", pw->pw_name);
214 (void)printf("uid\t%u\n", rid);
216 if ((eid = geteuid()) != rid) {
217 if ((pw = getpwuid(eid)))
218 (void)printf("euid\t%s\n", pw->pw_name);
220 (void)printf("euid\t%u\n", eid);
222 if ((rid = getgid()) != (eid = getegid())) {
223 if ((gr = getgrgid(rid)))
224 (void)printf("rgid\t%s\n", gr->gr_name);
226 (void)printf("rgid\t%u\n", rid);
228 (void)printf("groups\t");
234 id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
237 gid_t gid, egid, lastgid;
240 gid_t groups[NGROUPS + 1];
247 ngroups = NGROUPS + 1;
248 getgrouplist(pw->pw_name, gid, groups, &ngroups);
251 ngroups = getgroups(NGROUPS + 1, groups);
254 printf("uid=%u(%s)", uid, pw->pw_name);
255 if (p_euid && (euid = geteuid()) != uid) {
256 (void)printf(" euid=%u", euid);
257 if ((pw = getpwuid(euid)))
258 (void)printf("(%s)", pw->pw_name);
260 printf(" gid=%u", gid);
261 if ((gr = getgrgid(gid)))
262 (void)printf("(%s)", gr->gr_name);
263 if (p_egid && (egid = getegid()) != gid) {
264 (void)printf(" egid=%u", egid);
265 if ((gr = getgrgid(egid)))
266 (void)printf("(%s)", gr->gr_name);
269 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
270 if (lastgid == (gid = groups[cnt]))
274 if ((gr = getgrgid(gid)))
275 printf("(%s)", gr->gr_name);
282 group(struct passwd *pw, int nflag)
285 int cnt, id, lastid, ngroups;
286 gid_t groups[NGROUPS + 1];
290 ngroups = NGROUPS + 1;
291 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
293 groups[0] = getgid();
294 ngroups = getgroups(NGROUPS, groups + 1) + 1;
296 fmt = nflag ? "%s" : "%u";
297 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
298 if (lastid == (id = groups[cnt]))
301 if ((gr = getgrgid(id)))
302 (void)printf(fmt, gr->gr_name);
304 (void)printf(*fmt == ' ' ? " %u" : "%u",
308 (void)printf(fmt, id);
323 error = mac_prepare_process_label(&label);
325 errx(1, "mac_prepare_type: %s", strerror(errno));
327 error = mac_get_proc(label);
329 errx(1, "mac_get_proc: %s", strerror(errno));
331 error = mac_to_text(label, &string);
333 errx(1, "mac_to_text: %s", strerror(errno));
335 (void)printf("%s\n", string);
348 * Translate user argument into a pw pointer. First, try to
349 * get it as specified. If that fails, try it as a number.
351 if ((pw = getpwnam(u)))
353 id = strtol(u, &ep, 10);
354 if (*u && !*ep && (pw = getpwuid(id)))
356 errx(1, "%s: no such user", u);
361 pline(struct passwd *pw)
365 if ((pw = getpwuid(getuid())) == NULL)
369 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
370 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
371 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
372 pw->pw_dir, pw->pw_shell);
381 (void)fprintf(stderr, "usage: groups [user]\n");
383 (void)fprintf(stderr, "usage: whoami\n");
385 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
387 " id -G [-n] [user]",
390 " id -g [-nr] [user]",
392 " id -u [-nr] [user]");