Home | History | Annotate | Line # | Download | only in id
id.c revision 1.27
      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.27 2006/06/07 13:09:56 liamjfoy 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 	gid_t gid, egid, lastid;
    211 	uid_t uid, euid;
    212 	int cnt, ngroups;
    213 	const char *fmt;
    214 
    215 	uid = getuid();
    216 	(void)printf("uid=%ju", (uintmax_t)uid);
    217 	if ((pw = getpwuid(uid)) != NULL)
    218 		(void)printf("(%s)", pw->pw_name);
    219 	gid = getgid();
    220 	(void)printf(" gid=%ju", (uintmax_t)gid);
    221 	if ((gr = getgrgid(gid)) != NULL)
    222 		(void)printf("(%s)", gr->gr_name);
    223 	if ((euid = geteuid()) != uid) {
    224 		(void)printf(" euid=%ju", (uintmax_t)euid);
    225 		if ((pw = getpwuid(euid)) != NULL)
    226 			(void)printf("(%s)", pw->pw_name);
    227 	}
    228 	if ((egid = getegid()) != gid) {
    229 		(void)printf(" egid=%ju", (uintmax_t)egid);
    230 		if ((gr = getgrgid(egid)) != NULL)
    231 			(void)printf("(%s)", gr->gr_name);
    232 	}
    233 	if ((ngroups = getgroups(maxgroups, groups)) != 0) {
    234 		for (fmt = " groups=%ju", lastid = -1, cnt = 0; cnt < ngroups;
    235 		    fmt = ",%ju", lastid = gid, cnt++) {
    236 			gid = groups[cnt];
    237 			if (lastid == gid)
    238 				continue;
    239 			(void)printf(fmt, (uintmax_t)gid);
    240 			if ((gr = getgrgid(gid)) != NULL)
    241 				(void)printf("(%s)", gr->gr_name);
    242 		}
    243 	}
    244 	(void)printf("\n");
    245 }
    246 
    247 static void
    248 user(struct passwd *pw)
    249 {
    250 	struct group *gr;
    251 	const char *fmt;
    252 	int cnt, id, lastid, ngroups;
    253 	gid_t *glist = groups;
    254 
    255 	id = pw->pw_uid;
    256 	(void)printf("uid=%u(%s)", id, pw->pw_name);
    257 	(void)printf(" gid=%lu", (u_long)pw->pw_gid);
    258 	if ((gr = getgrgid(pw->pw_gid)) != NULL)
    259 		(void)printf("(%s)", gr->gr_name);
    260 	ngroups = maxgroups + 1;
    261 	if (getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups) == -1) {
    262 		glist = malloc(ngroups * sizeof(gid_t));
    263 		(void) getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups);
    264 	}
    265 	for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
    266 	    fmt=",%u", lastid = id, cnt++) {
    267 		id = glist[cnt];
    268 		if (lastid == id)
    269 			continue;
    270 		(void)printf(fmt, id);
    271 		if ((gr = getgrgid(id)) != NULL)
    272 			(void)printf("(%s)", gr->gr_name);
    273 	}
    274 	(void)printf("\n");
    275 	if (glist != groups)
    276 		free(glist);
    277 }
    278 
    279 static void
    280 group(struct passwd *pw, int nflag)
    281 {
    282 	struct group *gr;
    283 	int cnt, id, lastid, ngroups;
    284 	const char *fmt;
    285 	gid_t *glist = groups;
    286 
    287 	if (pw) {
    288 		ngroups = maxgroups;
    289 		if (getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups)
    290 		    == -1) {
    291 			glist = malloc(ngroups * sizeof(gid_t));
    292 			(void) getgrouplist(pw->pw_name, pw->pw_gid, glist,
    293 					    &ngroups);
    294 		}
    295 	} else {
    296 		glist[0] = getgid();
    297 		ngroups = getgroups(maxgroups, glist + 1) + 1;
    298 	}
    299 	fmt = nflag ? "%s" : "%u";
    300 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
    301 		if (lastid == (id = glist[cnt]) || (cnt && id == glist[0]))
    302 			continue;
    303 		if (nflag) {
    304 			if ((gr = getgrgid(id)) != NULL)
    305 				(void)printf(fmt, gr->gr_name);
    306 			else
    307 				(void)printf(*fmt == ' ' ? " %u" : "%u",
    308 				    id);
    309 			fmt = " %s";
    310 		} else {
    311 			(void)printf(fmt, id);
    312 			fmt = " %u";
    313 		}
    314 		lastid = id;
    315 	}
    316 	(void)printf("\n");
    317 	if (glist != groups)
    318 		free(glist);
    319 }
    320 
    321 static struct passwd *
    322 who(char *u)
    323 {
    324 	struct passwd *pw;
    325 	long id;
    326 	char *ep;
    327 
    328 	/*
    329 	 * Translate user argument into a pw pointer.  First, try to
    330 	 * get it as specified.  If that fails, try it as a number.
    331 	 */
    332 	if ((pw = getpwnam(u)) != NULL)
    333 		return pw;
    334 	id = strtol(u, &ep, 10);
    335 	if (*u && !*ep && (pw = getpwuid(id)))
    336 		return pw;
    337 	errx(1, "%s: No such user", u);
    338 	/* NOTREACHED */
    339 	return NULL;
    340 }
    341 
    342 static void
    343 usage(void)
    344 {
    345 
    346 	if (strcmp(getprogname(), "groups") == 0) {
    347 		(void)fprintf(stderr, "usage: groups [user]\n");
    348 	} else if (strcmp(getprogname(), "whoami") == 0) {
    349 		(void)fprintf(stderr, "usage: whoami\n");
    350 	} else {
    351 		(void)fprintf(stderr, "usage: id [user]\n");
    352 		(void)fprintf(stderr, "       id -G [-n] [user]\n");
    353 		(void)fprintf(stderr, "       id -g [-nr] [user]\n");
    354 		(void)fprintf(stderr, "       id -p [user]\n");
    355 		(void)fprintf(stderr, "       id -u [-nr] [user]\n");
    356 	}
    357 	exit(1);
    358 }
    359