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 #include <sys/param.h>
43 static void current(void);
44 static void pretty(struct passwd
*);
45 static void group(struct passwd
*, int);
46 static void usage(void);
47 static void user(struct passwd
*);
48 static struct passwd
*who(char *);
54 main(int argc
, char *argv
[])
59 int Gflag
, gflag
, nflag
, pflag
, rflag
, uflag
;
62 Gflag
= gflag
= nflag
= pflag
= rflag
= uflag
= 0;
64 if (strcmp(getprogname(), "groups") == 0) {
70 } else if (strcmp(getprogname(), "whoami") == 0) {
79 while ((ch
= getopt(argc
, argv
, opts
)) != -1)
106 switch (Gflag
+ gflag
+ pflag
+ uflag
) {
110 if (!nflag
&& !rflag
)
117 if (strcmp(opts
, "") != 0 && argc
> 1)
120 pw
= *argv
? who(*argv
) : NULL
;
122 maxgroups
= sysconf(_SC_NGROUPS_MAX
);
123 if ((groups
= malloc((maxgroups
+ 1) * sizeof(gid_t
))) == NULL
)
127 id
= pw
? pw
->pw_gid
: rflag
? getgid() : getegid();
128 if (nflag
&& (gr
= getgrgid(id
)))
129 (void)printf("%s\n", gr
->gr_name
);
131 (void)printf("%u\n", id
);
136 id
= pw
? pw
->pw_uid
: rflag
? getuid() : geteuid();
137 if (nflag
&& (pw
= getpwuid(id
)))
138 (void)printf("%s\n", pw
->pw_name
);
140 (void)printf("%u\n", id
);
165 pretty(struct passwd
*pw
)
172 (void)printf("uid\t%s\n", pw
->pw_name
);
173 (void)printf("groups\t");
176 if ((login
= getlogin()) == NULL
)
179 pw
= getpwuid(rid
= getuid());
180 if (pw
== NULL
|| strcmp(login
, pw
->pw_name
))
181 (void)printf("login\t%s\n", login
);
183 (void)printf("uid\t%s\n", pw
->pw_name
);
185 (void)printf("uid\t%u\n", rid
);
187 if ((eid
= geteuid()) != rid
) {
188 if ((pw
= getpwuid(eid
)) != NULL
)
189 (void)printf("euid\t%s\n", pw
->pw_name
);
191 (void)printf("euid\t%u\n", eid
);
193 if ((rid
= getgid()) != (eid
= getegid())) {
194 if ((gr
= getgrgid(rid
)) != NULL
)
195 (void)printf("rgid\t%s\n", gr
->gr_name
);
197 (void)printf("rgid\t%u\n", rid
);
199 (void)printf("groups\t");
209 gid_t gid
, egid
, lastid
;
215 (void)printf("uid=%ju", (uintmax_t)uid
);
216 if ((pw
= getpwuid(uid
)) != NULL
)
217 (void)printf("(%s)", pw
->pw_name
);
219 (void)printf(" gid=%ju", (uintmax_t)gid
);
220 if ((gr
= getgrgid(gid
)) != NULL
)
221 (void)printf("(%s)", gr
->gr_name
);
222 if ((euid
= geteuid()) != uid
) {
223 (void)printf(" euid=%ju", (uintmax_t)euid
);
224 if ((pw
= getpwuid(euid
)) != NULL
)
225 (void)printf("(%s)", pw
->pw_name
);
227 if ((egid
= getegid()) != gid
) {
228 (void)printf(" egid=%ju", (uintmax_t)egid
);
229 if ((gr
= getgrgid(egid
)) != NULL
)
230 (void)printf("(%s)", gr
->gr_name
);
233 if ((ngroups
= getgroups(maxgroups
, groups
)) != 0) {
234 for (fmt
= " groups=%ju", lastid
= -1, cnt
= 0; cnt
< ngroups
;
235 fmt
= ",%ju", lastid
= gid
, cnt
++) {
239 (void)printf(fmt
, (uintmax_t)gid
);
240 if ((gr
= getgrgid(gid
)) != NULL
)
241 (void)printf("(%s)", gr
->gr_name
);
249 user(struct passwd
*pw
)
253 int cnt
, id
, lastid
, ngroups
;
254 gid_t
*glist
= groups
;
257 (void)printf("uid=%u(%s)", id
, pw
->pw_name
);
258 (void)printf(" gid=%lu", (u_long
)pw
->pw_gid
);
259 if ((gr
= getgrgid(pw
->pw_gid
)) != NULL
)
260 (void)printf("(%s)", gr
->gr_name
);
261 ngroups
= maxgroups
+ 1;
263 if (getgrouplist(pw
->pw_name
, pw
->pw_gid
, glist
, &ngroups
) == -1) {
264 glist
= malloc(ngroups
* sizeof(gid_t
));
265 (void) getgrouplist(pw
->pw_name
, pw
->pw_gid
, glist
, &ngroups
);
268 for (fmt
= " groups=%u", lastid
= -1, cnt
= 0; cnt
< ngroups
;
269 fmt
=",%u", lastid
= id
, cnt
++) {
273 (void)printf(fmt
, id
);
274 if ((gr
= getgrgid(id
)) != NULL
)
275 (void)printf("(%s)", gr
->gr_name
);
283 group(struct passwd
*pw
, int nflag
)
286 int cnt
, id
, lastid
, ngroups
;
288 gid_t
*glist
= groups
;
293 if (getgrouplist(pw
->pw_name
, pw
->pw_gid
, glist
, &ngroups
)
295 glist
= malloc(ngroups
* sizeof(gid_t
));
296 (void) getgrouplist(pw
->pw_name
, pw
->pw_gid
, glist
,
303 ngroups
= getgroups(maxgroups
, glist
+ 1) + 1;
305 fmt
= nflag
? "%s" : "%u";
306 for (lastid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
307 if (lastid
== (id
= glist
[cnt
]) || (cnt
&& id
== glist
[0]))
310 if ((gr
= getgrgid(id
)) != NULL
)
311 (void)printf(fmt
, gr
->gr_name
);
313 (void)printf(*fmt
== ' ' ? " %u" : "%u",
317 (void)printf(fmt
, id
);
327 static struct passwd
*
335 * Translate user argument into a pw pointer. First, try to
336 * get it as specified. If that fails, try it as a number.
338 if ((pw
= getpwnam(u
)) != NULL
)
340 id
= strtol(u
, &ep
, 10);
341 if (*u
&& !*ep
&& (pw
= getpwuid(id
)))
343 errx(1, "%s: No such user", u
);
352 if (strcmp(getprogname(), "groups") == 0) {
353 (void)fprintf(stderr
, "usage: groups [user]\n");
354 } else if (strcmp(getprogname(), "whoami") == 0) {
355 (void)fprintf(stderr
, "usage: whoami\n");
357 (void)fprintf(stderr
, "usage: id [user]\n");
358 (void)fprintf(stderr
, " id -G [-n] [user]\n");
359 (void)fprintf(stderr
, " id -g [-nr] [user]\n");
360 (void)fprintf(stderr
, " id -p [user]\n");
361 (void)fprintf(stderr
, " id -u [-nr] [user]\n");