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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
33 The Regents of the University of California. All rights reserved.");
38 static char sccsid
[] = "@(#)id.c 8.3 (Berkeley) 4/28/95";
40 __RCSID("$NetBSD: id.c,v 1.32 2011/09/16 15:39:26 joerg Exp $");
44 #include <sys/param.h>
55 static void current(void);
56 static void pretty(struct passwd
*);
57 static void group(struct passwd
*, int);
58 __dead
static void usage(void);
59 static void user(struct passwd
*);
60 static struct passwd
*who(char *);
66 main(int argc
, char *argv
[])
71 int Gflag
, gflag
, nflag
, pflag
, rflag
, uflag
;
74 Gflag
= gflag
= nflag
= pflag
= rflag
= uflag
= 0;
76 if (strcmp(getprogname(), "groups") == 0) {
82 } else if (strcmp(getprogname(), "whoami") == 0) {
91 while ((ch
= getopt(argc
, argv
, opts
)) != -1)
118 switch (Gflag
+ gflag
+ pflag
+ uflag
) {
122 if (!nflag
&& !rflag
)
129 if (strcmp(opts
, "") != 0 && argc
> 1)
132 pw
= *argv
? who(*argv
) : NULL
;
134 maxgroups
= sysconf(_SC_NGROUPS_MAX
);
135 if ((groups
= malloc((maxgroups
+ 1) * sizeof(gid_t
))) == NULL
)
139 id
= pw
? pw
->pw_gid
: rflag
? getgid() : getegid();
140 if (nflag
&& (gr
= getgrgid(id
)))
141 (void)printf("%s\n", gr
->gr_name
);
143 (void)printf("%u\n", id
);
148 id
= pw
? pw
->pw_uid
: rflag
? getuid() : geteuid();
149 if (nflag
&& (pw
= getpwuid(id
)))
150 (void)printf("%s\n", pw
->pw_name
);
152 (void)printf("%u\n", id
);
177 pretty(struct passwd
*pw
)
184 (void)printf("uid\t%s\n", pw
->pw_name
);
185 (void)printf("groups\t");
188 if ((login
= getlogin()) == NULL
)
191 pw
= getpwuid(rid
= getuid());
192 if (pw
== NULL
|| strcmp(login
, pw
->pw_name
))
193 (void)printf("login\t%s\n", login
);
195 (void)printf("uid\t%s\n", pw
->pw_name
);
197 (void)printf("uid\t%u\n", rid
);
199 if ((eid
= geteuid()) != rid
) {
200 if ((pw
= getpwuid(eid
)) != NULL
)
201 (void)printf("euid\t%s\n", pw
->pw_name
);
203 (void)printf("euid\t%u\n", eid
);
205 if ((rid
= getgid()) != (eid
= getegid())) {
206 if ((gr
= getgrgid(rid
)) != NULL
)
207 (void)printf("rgid\t%s\n", gr
->gr_name
);
209 (void)printf("rgid\t%u\n", rid
);
211 (void)printf("groups\t");
221 gid_t gid
, egid
, lastid
;
227 (void)printf("uid=%ju", (uintmax_t)uid
);
228 if ((pw
= getpwuid(uid
)) != NULL
)
229 (void)printf("(%s)", pw
->pw_name
);
231 (void)printf(" gid=%ju", (uintmax_t)gid
);
232 if ((gr
= getgrgid(gid
)) != NULL
)
233 (void)printf("(%s)", gr
->gr_name
);
234 if ((euid
= geteuid()) != uid
) {
235 (void)printf(" euid=%ju", (uintmax_t)euid
);
236 if ((pw
= getpwuid(euid
)) != NULL
)
237 (void)printf("(%s)", pw
->pw_name
);
239 if ((egid
= getegid()) != gid
) {
240 (void)printf(" egid=%ju", (uintmax_t)egid
);
241 if ((gr
= getgrgid(egid
)) != NULL
)
242 (void)printf("(%s)", gr
->gr_name
);
244 if ((ngroups
= getgroups(maxgroups
, groups
)) != 0) {
245 for (fmt
= " groups=%ju", lastid
= -1, cnt
= 0; cnt
< ngroups
;
246 fmt
= ",%ju", lastid
= gid
, cnt
++) {
250 (void)printf(fmt
, (uintmax_t)gid
);
251 if ((gr
= getgrgid(gid
)) != NULL
)
252 (void)printf("(%s)", gr
->gr_name
);
259 user(struct passwd
*pw
)
263 int cnt
, id
, lastid
, ngroups
;
264 gid_t
*glist
= groups
;
267 (void)printf("uid=%u(%s)", id
, pw
->pw_name
);
268 (void)printf(" gid=%lu", (u_long
)pw
->pw_gid
);
269 if ((gr
= getgrgid(pw
->pw_gid
)) != NULL
)
270 (void)printf("(%s)", gr
->gr_name
);
271 ngroups
= maxgroups
+ 1;
272 if (getgrouplist(pw
->pw_name
, pw
->pw_gid
, glist
, &ngroups
) == -1) {
273 glist
= malloc(ngroups
* sizeof(gid_t
));
274 (void) getgrouplist(pw
->pw_name
, pw
->pw_gid
, glist
, &ngroups
);
276 for (fmt
= " groups=%u", lastid
= -1, cnt
= 0; cnt
< ngroups
;
277 fmt
=",%u", lastid
= id
, cnt
++) {
281 (void)printf(fmt
, id
);
282 if ((gr
= getgrgid(id
)) != NULL
)
283 (void)printf("(%s)", gr
->gr_name
);
291 group(struct passwd
*pw
, int nflag
)
297 gid_t
*glist
= groups
;
301 if (getgrouplist(pw
->pw_name
, pw
->pw_gid
, glist
, &ngroups
)
303 glist
= malloc(ngroups
* sizeof(gid_t
));
304 (void) getgrouplist(pw
->pw_name
, pw
->pw_gid
, glist
,
309 ngroups
= getgroups(maxgroups
, glist
+ 1) + 1;
311 fmt
= nflag
? "%s" : "%u";
312 for (lastid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
313 if (lastid
== (id
= glist
[cnt
]) || (cnt
&& id
== glist
[0]))
316 if ((gr
= getgrgid(id
)) != NULL
)
317 (void)printf(fmt
, gr
->gr_name
);
319 (void)printf(*fmt
== ' ' ? " %u" : "%u",
323 (void)printf(fmt
, id
);
333 static struct passwd
*
341 * Translate user argument into a pw pointer. First, try to
342 * get it as specified. If that fails, try it as a number.
344 if ((pw
= getpwnam(u
)) != NULL
)
346 id
= strtol(u
, &ep
, 10);
347 if (*u
&& !*ep
&& (pw
= getpwuid(id
)))
349 errx(1, "%s: No such user", u
);
358 if (strcmp(getprogname(), "groups") == 0) {
359 (void)fprintf(stderr
, "usage: groups [user]\n");
360 } else if (strcmp(getprogname(), "whoami") == 0) {
361 (void)fprintf(stderr
, "usage: whoami\n");
363 (void)fprintf(stderr
, "usage: id [user]\n");
364 (void)fprintf(stderr
, " id -G [-n] [user]\n");
365 (void)fprintf(stderr
, " id -g [-nr] [user]\n");
366 (void)fprintf(stderr
, " id -p [user]\n");
367 (void)fprintf(stderr
, " id -u [-nr] [user]\n");