Home | History | Annotate | Line # | Download | only in id
id.c revision 1.18
      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. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     37 	The Regents of the University of California.  All rights reserved.\n");
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)id.c	8.3 (Berkeley) 4/28/95";
     43 #else
     44 __RCSID("$NetBSD: id.c,v 1.18 2001/02/19 23:03:47 cgd Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 #include <sys/param.h>
     49 
     50 #include <err.h>
     51 #include <errno.h>
     52 #include <grp.h>
     53 #include <pwd.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 
     59 int	main __P((int, char **));
     60 
     61 static void	current __P((void));
     62 static void	pretty __P((struct passwd *));
     63 static void	group __P((struct passwd *, int));
     64 static void	usage __P((void));
     65 static void	user __P((struct passwd *));
     66 static struct passwd *who __P((char *));
     67 
     68 static int maxgroups;
     69 static gid_t *groups;
     70 
     71 int
     72 main(argc, argv)
     73 	int argc;
     74 	char *argv[];
     75 {
     76 	struct group *gr;
     77 	struct passwd *pw;
     78 	int ch, id;
     79 	int Gflag, gflag, nflag, pflag, rflag, uflag;
     80 
     81 	Gflag = gflag = nflag = pflag = rflag = uflag = 0;
     82 
     83 	if (!strcmp(getprogname(), "groups")) {
     84 		Gflag = 1;
     85 		nflag = 1;
     86 	} else if (!strcmp(getprogname(), "whoami")) {
     87 		uflag = 1;
     88 		nflag = 1;
     89 	}
     90 
     91 	while ((ch = getopt(argc, argv, "Ggnpru")) != -1)
     92 		switch(ch) {
     93 		case 'G':
     94 			Gflag = 1;
     95 			break;
     96 		case 'g':
     97 			gflag = 1;
     98 			break;
     99 		case 'n':
    100 			nflag = 1;
    101 			break;
    102 		case 'p':
    103 			pflag = 1;
    104 			break;
    105 		case 'r':
    106 			rflag = 1;
    107 			break;
    108 		case 'u':
    109 			uflag = 1;
    110 			break;
    111 		case '?':
    112 		default:
    113 			usage();
    114 		}
    115 	argc -= optind;
    116 	argv += optind;
    117 
    118 	switch(Gflag + gflag + pflag + uflag) {
    119 	case 1:
    120 		break;
    121 	case 0:
    122 		if (!nflag && !rflag)
    123 			break;
    124 		/* FALLTHROUGH */
    125 	default:
    126 		usage();
    127 	}
    128 
    129 	pw = *argv ? who(*argv) : NULL;
    130 
    131 	maxgroups = sysconf(_SC_NGROUPS_MAX);
    132 	if ((groups = malloc((maxgroups + 1) * sizeof(gid_t))) == NULL)
    133 		err(1, NULL);
    134 
    135 	if (gflag) {
    136 		id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
    137 		if (nflag && (gr = getgrgid(id)))
    138 			(void)printf("%s\n", gr->gr_name);
    139 		else
    140 			(void)printf("%u\n", id);
    141 		goto done;
    142 	}
    143 
    144 	if (uflag) {
    145 		id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
    146 		if (nflag && (pw = getpwuid(id)))
    147 			(void)printf("%s\n", pw->pw_name);
    148 		else
    149 			(void)printf("%u\n", id);
    150 		goto done;
    151 	}
    152 
    153 	if (Gflag) {
    154 		group(pw, nflag);
    155 		goto done;
    156 	}
    157 
    158 	if (pflag) {
    159 		pretty(pw);
    160 		goto done;
    161 	}
    162 
    163 	if (pw)
    164 		user(pw);
    165 	else
    166 		current();
    167 done:
    168 	free(groups);
    169 	return(0);
    170 }
    171 
    172 static void
    173 pretty(pw)
    174 	struct passwd *pw;
    175 {
    176 	struct group *gr;
    177 	u_int eid, rid;
    178 	char *login;
    179 
    180 	if (pw) {
    181 		(void)printf("uid\t%s\n", pw->pw_name);
    182 		(void)printf("groups\t");
    183 		group(pw, 1);
    184 	} else {
    185 		if ((login = getlogin()) == NULL)
    186 			err(1, "getlogin");
    187 
    188 		pw = getpwuid(rid = getuid());
    189 		if (pw == NULL || strcmp(login, pw->pw_name))
    190 			(void)printf("login\t%s\n", login);
    191 		if (pw)
    192 			(void)printf("uid\t%s\n", pw->pw_name);
    193 		else
    194 			(void)printf("uid\t%u\n", rid);
    195 
    196 		if ((eid = geteuid()) != rid) {
    197 			if ((pw = getpwuid(eid)) != NULL)
    198 				(void)printf("euid\t%s\n", pw->pw_name);
    199 			else
    200 				(void)printf("euid\t%u\n", eid);
    201 		}
    202 		if ((rid = getgid()) != (eid = getegid())) {
    203 			if ((gr = getgrgid(rid)) != NULL)
    204 				(void)printf("rgid\t%s\n", gr->gr_name);
    205 			else
    206 				(void)printf("rgid\t%u\n", rid);
    207 		}
    208 		(void)printf("groups\t");
    209 		group(NULL, 1);
    210 	}
    211 }
    212 
    213 static void
    214 current()
    215 {
    216 	struct group *gr;
    217 	struct passwd *pw;
    218 	int cnt, id, eid, lastid, ngroups;
    219 	char *fmt;
    220 
    221 	id = getuid();
    222 	(void)printf("uid=%u", id);
    223 	if ((pw = getpwuid(id)) != NULL)
    224 		(void)printf("(%s)", pw->pw_name);
    225 	if ((eid = geteuid()) != id) {
    226 		(void)printf(" euid=%u", eid);
    227 		if ((pw = getpwuid(eid)) != NULL)
    228 			(void)printf("(%s)", pw->pw_name);
    229 	}
    230 	id = getgid();
    231 	(void)printf(" gid=%u", id);
    232 	if ((gr = getgrgid(id)) != NULL)
    233 		(void)printf("(%s)", gr->gr_name);
    234 	if ((eid = getegid()) != id) {
    235 		(void)printf(" egid=%u", eid);
    236 		if ((gr = getgrgid(eid)) != NULL)
    237 			(void)printf("(%s)", gr->gr_name);
    238 	}
    239 	if ((ngroups = getgroups(maxgroups, groups)) != NULL) {
    240 		for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
    241 		    fmt = ",%u", lastid = id, cnt++) {
    242 			id = groups[cnt];
    243 			if (lastid == id)
    244 				continue;
    245 			(void)printf(fmt, id);
    246 			if ((gr = getgrgid(id)) != NULL)
    247 				(void)printf("(%s)", gr->gr_name);
    248 		}
    249 	}
    250 	(void)printf("\n");
    251 }
    252 
    253 static void
    254 user(pw)
    255 	struct passwd *pw;
    256 {
    257 	struct group *gr;
    258 	char *fmt;
    259 	int cnt, id, lastid, ngroups;
    260 
    261 	id = pw->pw_uid;
    262 	(void)printf("uid=%u(%s)", id, pw->pw_name);
    263 	(void)printf(" gid=%lu", (u_long)pw->pw_gid);
    264 	if ((gr = getgrgid(pw->pw_gid)) != NULL)
    265 		(void)printf("(%s)", gr->gr_name);
    266 	ngroups = maxgroups + 1;
    267 	(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    268 	for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
    269 	    fmt=",%u", lastid = id, cnt++) {
    270 		id = groups[cnt];
    271 		if (lastid == id)
    272 			continue;
    273 		(void)printf(fmt, id);
    274 		if ((gr = getgrgid(id)) != NULL)
    275 			(void)printf("(%s)", gr->gr_name);
    276 	}
    277 	(void)printf("\n");
    278 }
    279 
    280 static void
    281 group(pw, nflag)
    282 	struct passwd *pw;
    283 	int nflag;
    284 {
    285 	struct group *gr;
    286 	int cnt, id, lastid, ngroups;
    287 	char *fmt;
    288 
    289 	if (pw) {
    290 		ngroups = maxgroups;
    291 		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    292 	} else {
    293 		groups[0] = getgid();
    294 		ngroups = getgroups(maxgroups, groups + 1) + 1;
    295 	}
    296 	fmt = nflag ? "%s" : "%u";
    297 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
    298 		if (lastid == (id = groups[cnt]))
    299 			continue;
    300 		if (nflag) {
    301 			if ((gr = getgrgid(id)) != NULL)
    302 				(void)printf(fmt, gr->gr_name);
    303 			else
    304 				(void)printf(*fmt == ' ' ? " %u" : "%u",
    305 				    id);
    306 			fmt = " %s";
    307 		} else {
    308 			(void)printf(fmt, id);
    309 			fmt = " %u";
    310 		}
    311 		lastid = id;
    312 	}
    313 	(void)printf("\n");
    314 }
    315 
    316 struct passwd *
    317 who(u)
    318 	char *u;
    319 {
    320 	struct passwd *pw;
    321 	long id;
    322 	char *ep;
    323 
    324 	/*
    325 	 * Translate user argument into a pw pointer.  First, try to
    326 	 * get it as specified.  If that fails, try it as a number.
    327 	 */
    328 	if ((pw = getpwnam(u)) != NULL)
    329 		return(pw);
    330 	id = strtol(u, &ep, 10);
    331 	if (*u && !*ep && (pw = getpwuid(id)))
    332 		return(pw);
    333 	errx(1, "%s: No such user", u);
    334 	/* NOTREACHED */
    335 	return (NULL);
    336 }
    337 
    338 void
    339 usage()
    340 {
    341 	(void)fprintf(stderr, "usage: id [user]\n");
    342 	(void)fprintf(stderr, "       id -G [-n] [user]\n");
    343 	(void)fprintf(stderr, "       id -g [-nr] [user]\n");
    344 	(void)fprintf(stderr, "       id -p\n");
    345 	(void)fprintf(stderr, "       id -u [-nr] [user]\n");
    346 	exit(1);
    347 }
    348