Home | History | Annotate | Line # | Download | only in id
id.c revision 1.26
      1 /*-
      2  * Copyright (c) 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      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.
     16  *
     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
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 #ifndef lint
     32 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     33 	The Regents of the University of California.  All rights reserved.\n");
     34 #endif /* not lint */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)id.c	8.3 (Berkeley) 4/28/95";
     39 #else
     40 __RCSID("$NetBSD: id.c,v 1.26 2006/03/11 17:16:01 christos Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 
     46 #include <err.h>
     47 #include <errno.h>
     48 #include <grp.h>
     49 #include <pwd.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 
     55 static void current(void);
     56 static void pretty(struct passwd *);
     57 static void group(struct passwd *, int);
     58 static void usage(void);
     59 static void user(struct passwd *);
     60 static struct passwd *who(char *);
     61 
     62 static int maxgroups;
     63 static gid_t *groups;
     64 
     65 int
     66 main(int argc, char *argv[])
     67 {
     68 	struct group *gr;
     69 	struct passwd *pw;
     70 	int ch, id;
     71 	int Gflag, gflag, nflag, pflag, rflag, uflag;
     72 
     73 	Gflag = gflag = nflag = pflag = rflag = uflag = 0;
     74 
     75 	if (strcmp(getprogname(), "groups") == 0) {
     76 		Gflag = 1;
     77 		nflag = 1;
     78 	} else if (strcmp(getprogname(), "whoami") == 0) {
     79 		uflag = 1;
     80 		nflag = 1;
     81 	}
     82 
     83 	while ((ch = getopt(argc, argv, "Ggnpru")) != -1)
     84 		switch (ch) {
     85 		case 'G':
     86 			Gflag = 1;
     87 			break;
     88 		case 'g':
     89 			gflag = 1;
     90 			break;
     91 		case 'n':
     92 			nflag = 1;
     93 			break;
     94 		case 'p':
     95 			pflag = 1;
     96 			break;
     97 		case 'r':
     98 			rflag = 1;
     99 			break;
    100 		case 'u':
    101 			uflag = 1;
    102 			break;
    103 		case '?':
    104 		default:
    105 			usage();
    106 		}
    107 	argc -= optind;
    108 	argv += optind;
    109 
    110 	switch (Gflag + gflag + pflag + uflag) {
    111 	case 1:
    112 		break;
    113 	case 0:
    114 		if (!nflag && !rflag)
    115 			break;
    116 		/* FALLTHROUGH */
    117 	default:
    118 		usage();
    119 	}
    120 
    121 	pw = *argv ? who(*argv) : NULL;
    122 
    123 	maxgroups = sysconf(_SC_NGROUPS_MAX);
    124 	if ((groups = malloc((maxgroups + 1) * sizeof(gid_t))) == NULL)
    125 		err(1, NULL);
    126 
    127 	if (gflag) {
    128 		id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
    129 		if (nflag && (gr = getgrgid(id)))
    130 			(void)printf("%s\n", gr->gr_name);
    131 		else
    132 			(void)printf("%u\n", id);
    133 		goto done;
    134 	}
    135 
    136 	if (uflag) {
    137 		id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
    138 		if (nflag && (pw = getpwuid(id)))
    139 			(void)printf("%s\n", pw->pw_name);
    140 		else
    141 			(void)printf("%u\n", id);
    142 		goto done;
    143 	}
    144 
    145 	if (Gflag) {
    146 		group(pw, nflag);
    147 		goto done;
    148 	}
    149 
    150 	if (pflag) {
    151 		pretty(pw);
    152 		goto done;
    153 	}
    154 
    155 	if (pw)
    156 		user(pw);
    157 	else
    158 		current();
    159 done:
    160 	free(groups);
    161 
    162 	return 0;
    163 }
    164 
    165 static void
    166 pretty(struct passwd *pw)
    167 {
    168 	struct group *gr;
    169 	u_int eid, rid;
    170 	char *login;
    171 
    172 	if (pw) {
    173 		(void)printf("uid\t%s\n", pw->pw_name);
    174 		(void)printf("groups\t");
    175 		group(pw, 1);
    176 	} else {
    177 		if ((login = getlogin()) == NULL)
    178 			err(1, "getlogin");
    179 
    180 		pw = getpwuid(rid = getuid());
    181 		if (pw == NULL || strcmp(login, pw->pw_name))
    182 			(void)printf("login\t%s\n", login);
    183 		if (pw)
    184 			(void)printf("uid\t%s\n", pw->pw_name);
    185 		else
    186 			(void)printf("uid\t%u\n", rid);
    187 
    188 		if ((eid = geteuid()) != rid) {
    189 			if ((pw = getpwuid(eid)) != NULL)
    190 				(void)printf("euid\t%s\n", pw->pw_name);
    191 			else
    192 				(void)printf("euid\t%u\n", eid);
    193 		}
    194 		if ((rid = getgid()) != (eid = getegid())) {
    195 			if ((gr = getgrgid(rid)) != NULL)
    196 				(void)printf("rgid\t%s\n", gr->gr_name);
    197 			else
    198 				(void)printf("rgid\t%u\n", rid);
    199 		}
    200 		(void)printf("groups\t");
    201 		group(NULL, 1);
    202 	}
    203 }
    204 
    205 static void
    206 current(void)
    207 {
    208 	struct group *gr;
    209 	struct passwd *pw;
    210 	int cnt, id, eid, lastid, ngroups;
    211 	const char *fmt;
    212 
    213 	id = getuid();
    214 	(void)printf("uid=%u", id);
    215 	if ((pw = getpwuid(id)) != NULL)
    216 		(void)printf("(%s)", pw->pw_name);
    217 	if ((eid = geteuid()) != id) {
    218 		(void)printf(" euid=%u", eid);
    219 		if ((pw = getpwuid(eid)) != NULL)
    220 			(void)printf("(%s)", pw->pw_name);
    221 	}
    222 	id = getgid();
    223 	(void)printf(" gid=%u", id);
    224 	if ((gr = getgrgid(id)) != NULL)
    225 		(void)printf("(%s)", gr->gr_name);
    226 	if ((eid = getegid()) != id) {
    227 		(void)printf(" egid=%u", eid);
    228 		if ((gr = getgrgid(eid)) != NULL)
    229 			(void)printf("(%s)", gr->gr_name);
    230 	}
    231 	if ((ngroups = getgroups(maxgroups, groups)) != 0) {
    232 		for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
    233 		    fmt = ",%u", lastid = id, cnt++) {
    234 			id = groups[cnt];
    235 			if (lastid == id)
    236 				continue;
    237 			(void)printf(fmt, id);
    238 			if ((gr = getgrgid(id)) != NULL)
    239 				(void)printf("(%s)", gr->gr_name);
    240 		}
    241 	}
    242 	(void)printf("\n");
    243 }
    244 
    245 static void
    246 user(struct passwd *pw)
    247 {
    248 	struct group *gr;
    249 	const char *fmt;
    250 	int cnt, id, lastid, ngroups;
    251 	gid_t *glist = groups;
    252 
    253 	id = pw->pw_uid;
    254 	(void)printf("uid=%u(%s)", id, pw->pw_name);
    255 	(void)printf(" gid=%lu", (u_long)pw->pw_gid);
    256 	if ((gr = getgrgid(pw->pw_gid)) != NULL)
    257 		(void)printf("(%s)", gr->gr_name);
    258 	ngroups = maxgroups + 1;
    259 	if (getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups) == -1) {
    260 		glist = malloc(ngroups * sizeof(gid_t));
    261 		(void) getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups);
    262 	}
    263 	for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
    264 	    fmt=",%u", lastid = id, cnt++) {
    265 		id = glist[cnt];
    266 		if (lastid == id)
    267 			continue;
    268 		(void)printf(fmt, id);
    269 		if ((gr = getgrgid(id)) != NULL)
    270 			(void)printf("(%s)", gr->gr_name);
    271 	}
    272 	(void)printf("\n");
    273 	if (glist != groups)
    274 		free(glist);
    275 }
    276 
    277 static void
    278 group(struct passwd *pw, int nflag)
    279 {
    280 	struct group *gr;
    281 	int cnt, id, lastid, ngroups;
    282 	const char *fmt;
    283 	gid_t *glist = groups;
    284 
    285 	if (pw) {
    286 		ngroups = maxgroups;
    287 		if (getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups)
    288 		    == -1) {
    289 			glist = malloc(ngroups * sizeof(gid_t));
    290 			(void) getgrouplist(pw->pw_name, pw->pw_gid, glist,
    291 					    &ngroups);
    292 		}
    293 	} else {
    294 		glist[0] = getgid();
    295 		ngroups = getgroups(maxgroups, glist + 1) + 1;
    296 	}
    297 	fmt = nflag ? "%s" : "%u";
    298 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
    299 		if (lastid == (id = glist[cnt]) || (cnt && id == glist[0]))
    300 			continue;
    301 		if (nflag) {
    302 			if ((gr = getgrgid(id)) != NULL)
    303 				(void)printf(fmt, gr->gr_name);
    304 			else
    305 				(void)printf(*fmt == ' ' ? " %u" : "%u",
    306 				    id);
    307 			fmt = " %s";
    308 		} else {
    309 			(void)printf(fmt, id);
    310 			fmt = " %u";
    311 		}
    312 		lastid = id;
    313 	}
    314 	(void)printf("\n");
    315 	if (glist != groups)
    316 		free(glist);
    317 }
    318 
    319 static struct passwd *
    320 who(char *u)
    321 {
    322 	struct passwd *pw;
    323 	long id;
    324 	char *ep;
    325 
    326 	/*
    327 	 * Translate user argument into a pw pointer.  First, try to
    328 	 * get it as specified.  If that fails, try it as a number.
    329 	 */
    330 	if ((pw = getpwnam(u)) != NULL)
    331 		return pw;
    332 	id = strtol(u, &ep, 10);
    333 	if (*u && !*ep && (pw = getpwuid(id)))
    334 		return pw;
    335 	errx(1, "%s: No such user", u);
    336 	/* NOTREACHED */
    337 	return NULL;
    338 }
    339 
    340 static void
    341 usage(void)
    342 {
    343 
    344 	if (strcmp(getprogname(), "groups") == 0) {
    345 		(void)fprintf(stderr, "usage: groups [user]\n");
    346 	} else if (strcmp(getprogname(), "whoami") == 0) {
    347 		(void)fprintf(stderr, "usage: whoami\n");
    348 	} else {
    349 		(void)fprintf(stderr, "usage: id [user]\n");
    350 		(void)fprintf(stderr, "       id -G [-n] [user]\n");
    351 		(void)fprintf(stderr, "       id -g [-nr] [user]\n");
    352 		(void)fprintf(stderr, "       id -p [user]\n");
    353 		(void)fprintf(stderr, "       id -u [-nr] [user]\n");
    354 	}
    355 	exit(1);
    356 }
    357