Home | History | Annotate | Line # | Download | only in id
id.c revision 1.24
      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.24 2004/11/22 17:31:38 peter 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 
    252 	id = pw->pw_uid;
    253 	(void)printf("uid=%u(%s)", id, pw->pw_name);
    254 	(void)printf(" gid=%lu", (u_long)pw->pw_gid);
    255 	if ((gr = getgrgid(pw->pw_gid)) != NULL)
    256 		(void)printf("(%s)", gr->gr_name);
    257 	ngroups = maxgroups + 1;
    258 	(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    259 	for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
    260 	    fmt=",%u", lastid = id, cnt++) {
    261 		id = groups[cnt];
    262 		if (lastid == id)
    263 			continue;
    264 		(void)printf(fmt, id);
    265 		if ((gr = getgrgid(id)) != NULL)
    266 			(void)printf("(%s)", gr->gr_name);
    267 	}
    268 	(void)printf("\n");
    269 }
    270 
    271 static void
    272 group(struct passwd *pw, int nflag)
    273 {
    274 	struct group *gr;
    275 	int cnt, id, lastid, ngroups;
    276 	const char *fmt;
    277 
    278 	if (pw) {
    279 		ngroups = maxgroups;
    280 		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    281 	} else {
    282 		groups[0] = getgid();
    283 		ngroups = getgroups(maxgroups, groups + 1) + 1;
    284 	}
    285 	fmt = nflag ? "%s" : "%u";
    286 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
    287 		if (lastid == (id = groups[cnt]))
    288 			continue;
    289 		if (nflag) {
    290 			if ((gr = getgrgid(id)) != NULL)
    291 				(void)printf(fmt, gr->gr_name);
    292 			else
    293 				(void)printf(*fmt == ' ' ? " %u" : "%u",
    294 				    id);
    295 			fmt = " %s";
    296 		} else {
    297 			(void)printf(fmt, id);
    298 			fmt = " %u";
    299 		}
    300 		lastid = id;
    301 	}
    302 	(void)printf("\n");
    303 }
    304 
    305 static struct passwd *
    306 who(char *u)
    307 {
    308 	struct passwd *pw;
    309 	long id;
    310 	char *ep;
    311 
    312 	/*
    313 	 * Translate user argument into a pw pointer.  First, try to
    314 	 * get it as specified.  If that fails, try it as a number.
    315 	 */
    316 	if ((pw = getpwnam(u)) != NULL)
    317 		return pw;
    318 	id = strtol(u, &ep, 10);
    319 	if (*u && !*ep && (pw = getpwuid(id)))
    320 		return pw;
    321 	errx(1, "%s: No such user", u);
    322 	/* NOTREACHED */
    323 	return NULL;
    324 }
    325 
    326 static void
    327 usage(void)
    328 {
    329 
    330 	if (strcmp(getprogname(), "groups") == 0) {
    331 		(void)fprintf(stderr, "usage: groups [user]\n");
    332 	} else if (strcmp(getprogname(), "whoami") == 0) {
    333 		(void)fprintf(stderr, "usage: whoami\n");
    334 	} else {
    335 		(void)fprintf(stderr, "usage: id [user]\n");
    336 		(void)fprintf(stderr, "       id -G [-n] [user]\n");
    337 		(void)fprintf(stderr, "       id -g [-nr] [user]\n");
    338 		(void)fprintf(stderr, "       id -p [user]\n");
    339 		(void)fprintf(stderr, "       id -u [-nr] [user]\n");
    340 	}
    341 	exit(1);
    342 }
    343