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
33 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)id.c 8.2 (Berkeley) 2/16/94
35 * $FreeBSD: src/usr.bin/id/id.c,v 1.12.2.3 2001/12/20 12:09:03 ru Exp $
36 * $DragonFly: src/usr.bin/id/id.c,v 1.6 2004/12/31 20:58:17 cpressey Exp $
39 #include <sys/param.h>
49 int main(int, char *[]);
50 static void current(void);
51 static void pline(struct passwd
*);
52 static void pretty(struct passwd
*);
53 static void group(struct passwd
*, int);
54 static void usage(void);
55 static void user(struct passwd
*);
56 static struct passwd
*
59 int isgroups
, iswhoami
;
62 main(int argc
, char **argv
)
66 int Gflag
, Pflag
, ch
, gflag
, id
, nflag
, pflag
, rflag
, uflag
;
69 Gflag
= Pflag
= gflag
= nflag
= pflag
= rflag
= uflag
= 0;
71 myname
= strrchr(argv
[0], '/');
72 myname
= (myname
!= NULL
) ? myname
+ 1 : argv
[0];
73 if (strcmp(myname
, "groups") == 0) {
77 else if (strcmp(myname
, "whoami") == 0) {
82 while ((ch
= getopt(argc
, argv
,
83 (isgroups
|| iswhoami
) ? "" : "PGgnpru")) != -1)
113 if (iswhoami
&& argc
> 0)
116 switch(Gflag
+ Pflag
+ gflag
+ pflag
+ uflag
) {
120 if (!nflag
&& !rflag
)
127 pw
= *argv
? who(*argv
) : NULL
;
130 id
= pw
? pw
->pw_gid
: rflag
? getgid() : getegid();
131 if (nflag
&& (gr
= getgrgid(id
)))
132 printf("%s\n", gr
->gr_name
);
139 id
= pw
? pw
->pw_uid
: rflag
? getuid() : geteuid();
140 if (nflag
&& (pw
= getpwuid(id
)))
141 printf("%s\n", pw
->pw_name
);
170 pretty(struct passwd
*pw
)
177 printf("uid\t%s\n", pw
->pw_name
);
181 if ((login
= getlogin()) == NULL
)
184 pw
= getpwuid(rid
= getuid());
185 if (pw
== NULL
|| strcmp(login
, pw
->pw_name
))
186 printf("login\t%s\n", login
);
188 printf("uid\t%s\n", pw
->pw_name
);
190 printf("uid\t%u\n", rid
);
192 if ((eid
= geteuid()) != rid
) {
193 if ((pw
= getpwuid(eid
)))
194 printf("euid\t%s\n", pw
->pw_name
);
196 printf("euid\t%u\n", eid
);
198 if ((rid
= getgid()) != (eid
= getegid())) {
199 if ((gr
= getgrgid(rid
)))
200 printf("rgid\t%s\n", gr
->gr_name
);
202 printf("rgid\t%u\n", rid
);
214 int cnt
, id
, eid
, lastid
, ngroups
;
215 gid_t groups
[NGROUPS
];
219 printf("uid=%u", id
);
220 if ((pw
= getpwuid(id
)))
221 printf("(%s)", pw
->pw_name
);
222 if ((eid
= geteuid()) != id
) {
223 printf(" euid=%u", eid
);
224 if ((pw
= getpwuid(eid
)))
225 printf("(%s)", pw
->pw_name
);
228 printf(" gid=%u", id
);
229 if ((gr
= getgrgid(id
)))
230 printf("(%s)", gr
->gr_name
);
231 if ((eid
= getegid()) != id
) {
232 printf(" egid=%u", eid
);
233 if ((gr
= getgrgid(eid
)))
234 printf("(%s)", gr
->gr_name
);
236 if ((ngroups
= getgroups(NGROUPS
, groups
))) {
237 for (fmt
= " groups=%u", lastid
= -1, cnt
= 0; cnt
< ngroups
;
238 fmt
= ", %u", lastid
= id
) {
243 if ((gr
= getgrgid(id
)))
244 printf("(%s)", gr
->gr_name
);
251 user(struct passwd
*pw
)
256 gid_t gid
, lastgid
, groups
[NGROUPS
+ 1];
258 printf("uid=%u(%s)", pw
->pw_uid
, pw
->pw_name
);
260 printf(" gid=%u", gid
);
261 if ((gr
= getgrgid(gid
)))
262 printf("(%s)", gr
->gr_name
);
263 ngroups
= NGROUPS
+ 1;
264 getgrouplist(pw
->pw_name
, gid
, groups
, &ngroups
);
266 for (lastgid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
267 if (lastgid
== (gid
= groups
[cnt
]))
271 if ((gr
= getgrgid(gid
)))
272 printf("(%s)", gr
->gr_name
);
279 group(struct passwd
*pw
, int nflag
)
282 int cnt
, id
, lastid
, ngroups
;
283 gid_t groups
[NGROUPS
+ 1];
287 ngroups
= NGROUPS
+ 1;
288 getgrouplist(pw
->pw_name
, pw
->pw_gid
, groups
, &ngroups
);
290 groups
[0] = getgid();
291 ngroups
= getgroups(NGROUPS
, groups
+ 1) + 1;
293 fmt
= nflag
? "%s" : "%u";
294 for (lastid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
295 if (lastid
== (id
= groups
[cnt
]))
298 if ((gr
= getgrgid(id
)))
299 printf(fmt
, gr
->gr_name
);
301 printf(*fmt
== ' ' ? " %u" : "%u",
313 static struct passwd
*
321 * Translate user argument into a pw pointer. First, try to
322 * get it as specified. If that fails, try it as a number.
324 if ((pw
= getpwnam(u
)))
326 id
= strtol(u
, &ep
, 10);
327 if (*u
&& !*ep
&& (pw
= getpwuid(id
)))
329 errx(1, "%s: no such user", u
);
334 pline(struct passwd
*pw
)
339 if ((pw
= getpwuid(rid
= getuid())) == NULL
)
343 printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw
->pw_name
,
344 pw
->pw_passwd
, pw
->pw_uid
, pw
->pw_gid
, pw
->pw_class
,
345 (long)pw
->pw_change
, (long)pw
->pw_expire
, pw
->pw_gecos
,
346 pw
->pw_dir
, pw
->pw_shell
);
355 fprintf(stderr
, "usage: groups [user]\n");
357 fprintf(stderr
, "usage: whoami\n");
359 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n",
361 " id -G [-n] [user]",
363 " id -g [-nr] [user]",
365 " id -u [-nr] [user]");