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
29 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)id.c 8.2 (Berkeley) 2/16/94
31 * $FreeBSD: src/usr.bin/id/id.c,v 1.12.2.3 2001/12/20 12:09:03 ru Exp $
34 #include <sys/param.h>
44 static void current(void);
45 static void pline(struct passwd
*);
46 static void pretty(struct passwd
*);
47 static void group(struct passwd
*, int);
48 static void usage(void);
49 static void user(struct passwd
*);
50 static struct passwd
*
53 static int isgroups
, iswhoami
;
56 main(int argc
, char **argv
)
60 int Gflag
, Pflag
, ch
, gflag
, id
, nflag
, pflag
, rflag
, uflag
;
63 Gflag
= Pflag
= gflag
= nflag
= pflag
= rflag
= uflag
= 0;
65 myname
= strrchr(argv
[0], '/');
66 myname
= (myname
!= NULL
) ? myname
+ 1 : argv
[0];
67 if (strcmp(myname
, "groups") == 0) {
71 else if (strcmp(myname
, "whoami") == 0) {
76 while ((ch
= getopt(argc
, argv
,
77 (isgroups
|| iswhoami
) ? "" : "PGagnpru")) != -1)
109 if (iswhoami
&& argc
> 0)
112 switch(Gflag
+ Pflag
+ gflag
+ pflag
+ uflag
) {
116 if (!nflag
&& !rflag
)
123 pw
= *argv
? who(*argv
) : NULL
;
126 id
= pw
? pw
->pw_gid
: rflag
? getgid() : getegid();
127 if (nflag
&& (gr
= getgrgid(id
)))
128 printf("%s\n", gr
->gr_name
);
135 id
= pw
? pw
->pw_uid
: rflag
? getuid() : geteuid();
136 if (nflag
&& (pw
= getpwuid(id
)))
137 printf("%s\n", pw
->pw_name
);
166 pretty(struct passwd
*pw
)
173 printf("uid\t%s\n", pw
->pw_name
);
177 if ((login
= getlogin()) == NULL
)
180 pw
= getpwuid(rid
= getuid());
181 if (pw
== NULL
|| strcmp(login
, pw
->pw_name
))
182 printf("login\t%s\n", login
);
184 printf("uid\t%s\n", pw
->pw_name
);
186 printf("uid\t%u\n", rid
);
188 if ((eid
= geteuid()) != rid
) {
189 if ((pw
= getpwuid(eid
)))
190 printf("euid\t%s\n", pw
->pw_name
);
192 printf("euid\t%u\n", eid
);
194 if ((rid
= getgid()) != (eid
= getegid())) {
195 if ((gr
= getgrgid(rid
)))
196 printf("rgid\t%s\n", gr
->gr_name
);
198 printf("rgid\t%u\n", rid
);
210 int cnt
, id
, eid
, lastid
, ngroups
;
211 gid_t groups
[NGROUPS
];
215 printf("uid=%u", id
);
216 if ((pw
= getpwuid(id
)))
217 printf("(%s)", pw
->pw_name
);
218 if ((eid
= geteuid()) != id
) {
219 printf(" euid=%u", eid
);
220 if ((pw
= getpwuid(eid
)))
221 printf("(%s)", pw
->pw_name
);
224 printf(" gid=%u", id
);
225 if ((gr
= getgrgid(id
)))
226 printf("(%s)", gr
->gr_name
);
227 if ((eid
= getegid()) != id
) {
228 printf(" egid=%u", eid
);
229 if ((gr
= getgrgid(eid
)))
230 printf("(%s)", gr
->gr_name
);
232 if ((ngroups
= getgroups(NGROUPS
, groups
))) {
233 for (fmt
= " groups=%u", lastid
= -1, cnt
= 0; cnt
< ngroups
;
234 fmt
= ", %u", lastid
= id
) {
239 if ((gr
= getgrgid(id
)))
240 printf("(%s)", gr
->gr_name
);
247 user(struct passwd
*pw
)
252 gid_t gid
, lastgid
, groups
[NGROUPS
+ 1];
254 printf("uid=%u(%s)", pw
->pw_uid
, pw
->pw_name
);
256 printf(" gid=%u", gid
);
257 if ((gr
= getgrgid(gid
)))
258 printf("(%s)", gr
->gr_name
);
259 ngroups
= NGROUPS
+ 1;
260 getgrouplist(pw
->pw_name
, gid
, groups
, &ngroups
);
262 for (lastgid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
263 if (lastgid
== (gid
= groups
[cnt
]))
267 if ((gr
= getgrgid(gid
)))
268 printf("(%s)", gr
->gr_name
);
275 group(struct passwd
*pw
, int nflag
)
278 int cnt
, id
, lastid
, ngroups
;
279 gid_t groups
[NGROUPS
+ 1];
283 ngroups
= NGROUPS
+ 1;
284 getgrouplist(pw
->pw_name
, pw
->pw_gid
, groups
, &ngroups
);
286 groups
[0] = getgid();
287 ngroups
= getgroups(NGROUPS
, groups
+ 1) + 1;
289 fmt
= nflag
? "%s" : "%u";
290 for (lastid
= -1, cnt
= 0; cnt
< ngroups
; ++cnt
) {
291 if (lastid
== (id
= groups
[cnt
]))
294 if ((gr
= getgrgid(id
)))
295 printf(fmt
, gr
->gr_name
);
297 printf(*fmt
== ' ' ? " %u" : "%u",
309 static struct passwd
*
317 * Translate user argument into a pw pointer. First, try to
318 * get it as specified. If that fails, try it as a number.
320 if ((pw
= getpwnam(u
)))
322 id
= strtol(u
, &ep
, 10);
323 if (*u
&& !*ep
&& (pw
= getpwuid(id
)))
325 errx(1, "%s: no such user", u
);
330 pline(struct passwd
*pw
)
335 if ((pw
= getpwuid(rid
= getuid())) == NULL
)
339 printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw
->pw_name
,
340 pw
->pw_passwd
, pw
->pw_uid
, pw
->pw_gid
, pw
->pw_class
,
341 (long)pw
->pw_change
, (long)pw
->pw_expire
, pw
->pw_gecos
,
342 pw
->pw_dir
, pw
->pw_shell
);
351 fprintf(stderr
, "usage: groups [user]\n");
353 fprintf(stderr
, "usage: whoami\n");
355 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n",
357 " id -G [-n] [user]",
359 " id -g [-nr] [user]",
361 " id -u [-nr] [user]");