Home | History | Annotate | Line # | Download | only in id
id.c revision 1.13
      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.13 1998/10/14 21:56:57 thorpej 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 void	current __P((void));
     60 void	pretty __P((struct passwd *));
     61 int	main __P((int, char **));
     62 void	group __P((struct passwd *, int));
     63 void	usage __P((void));
     64 void	user __P((struct passwd *));
     65 struct passwd *
     66 	who __P((char *));
     67 
     68 int
     69 main(argc, argv)
     70 	int argc;
     71 	char *argv[];
     72 {
     73 	struct group *gr;
     74 	struct passwd *pw;
     75 	int ch, id;
     76 	int Gflag, gflag, nflag, pflag, rflag, uflag;
     77 	extern const char *__progname;
     78 
     79 	Gflag = gflag = nflag = pflag = rflag = uflag = 0;
     80 
     81 	if (!strcmp(__progname, "groups")) {
     82 		Gflag = 1;
     83 		nflag = 1;
     84 	} else if (!strcmp(__progname, "whoami")) {
     85 		uflag = 1;
     86 		nflag = 1;
     87 	}
     88 
     89 	while ((ch = getopt(argc, argv, "Ggnpru")) != -1)
     90 		switch(ch) {
     91 		case 'G':
     92 			Gflag = 1;
     93 			break;
     94 		case 'g':
     95 			gflag = 1;
     96 			break;
     97 		case 'n':
     98 			nflag = 1;
     99 			break;
    100 		case 'p':
    101 			pflag = 1;
    102 			break;
    103 		case 'r':
    104 			rflag = 1;
    105 			break;
    106 		case 'u':
    107 			uflag = 1;
    108 			break;
    109 		case '?':
    110 		default:
    111 			usage();
    112 		}
    113 	argc -= optind;
    114 	argv += optind;
    115 
    116 	switch(Gflag + gflag + pflag + uflag) {
    117 	case 1:
    118 		break;
    119 	case 0:
    120 		if (!nflag && !rflag)
    121 			break;
    122 		/* FALLTHROUGH */
    123 	default:
    124 		usage();
    125 	}
    126 
    127 	pw = *argv ? who(*argv) : NULL;
    128 
    129 	if (gflag) {
    130 		id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
    131 		if (nflag && (gr = getgrgid(id)))
    132 			(void)printf("%s\n", gr->gr_name);
    133 		else
    134 			(void)printf("%u\n", id);
    135 		exit(0);
    136 	}
    137 
    138 	if (uflag) {
    139 		id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
    140 		if (nflag && (pw = getpwuid(id)))
    141 			(void)printf("%s\n", pw->pw_name);
    142 		else
    143 			(void)printf("%u\n", id);
    144 		exit(0);
    145 	}
    146 
    147 	if (Gflag) {
    148 		group(pw, nflag);
    149 		exit(0);
    150 	}
    151 
    152 	if (pflag) {
    153 		pretty(pw);
    154 		exit(0);
    155 	}
    156 
    157 	if (pw)
    158 		user(pw);
    159 	else
    160 		current();
    161 	exit(0);
    162 }
    163 
    164 void
    165 pretty(pw)
    166 	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", pw->pw_name);
    191 			else
    192 				(void)printf("euid\t%u", 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 void
    206 current()
    207 {
    208 	struct group *gr;
    209 	struct passwd *pw;
    210 	int cnt, id, eid, lastid, ngroups;
    211 	gid_t groups[NGROUPS];
    212 	char *fmt;
    213 
    214 	id = getuid();
    215 	(void)printf("uid=%u", id);
    216 	if ((pw = getpwuid(id)) != NULL)
    217 		(void)printf("(%s)", pw->pw_name);
    218 	if ((eid = geteuid()) != id) {
    219 		(void)printf(" euid=%u", eid);
    220 		if ((pw = getpwuid(eid)) != NULL)
    221 			(void)printf("(%s)", pw->pw_name);
    222 	}
    223 	id = getgid();
    224 	(void)printf(" gid=%u", id);
    225 	if ((gr = getgrgid(id)) != NULL)
    226 		(void)printf("(%s)", gr->gr_name);
    227 	if ((eid = getegid()) != id) {
    228 		(void)printf(" egid=%u", eid);
    229 		if ((gr = getgrgid(eid)) != NULL)
    230 			(void)printf("(%s)", gr->gr_name);
    231 	}
    232 	if ((ngroups = getgroups(NGROUPS, groups)) != NULL) {
    233 		for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
    234 		    fmt = ", %u", lastid = id) {
    235 			id = groups[cnt++];
    236 			if (lastid == id)
    237 				continue;
    238 			(void)printf(fmt, id);
    239 			if ((gr = getgrgid(id)) != NULL)
    240 				(void)printf("(%s)", gr->gr_name);
    241 		}
    242 	}
    243 	(void)printf("\n");
    244 }
    245 
    246 void
    247 user(pw)
    248 	struct passwd *pw;
    249 {
    250 	struct group *gr;
    251 	char *fmt;
    252 	int cnt, id, lastid, ngroups, groups[NGROUPS + 1];
    253 
    254 	id = pw->pw_uid;
    255 	(void)printf("uid=%u(%s)", id, pw->pw_name);
    256 	(void)printf(" gid=%u", pw->pw_gid);
    257 	if ((gr = getgrgid(pw->pw_gid)) != NULL)
    258 		(void)printf("(%s)", gr->gr_name);
    259 	ngroups = NGROUPS + 1;
    260 	(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    261 	fmt = " groups=%u";
    262 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
    263 		if (lastid == (id = groups[cnt]))
    264 			continue;
    265 		(void)printf(fmt, id);
    266 		fmt = " %u";
    267 		if ((gr = getgrgid(id)) != NULL)
    268 			(void)printf("(%s)", gr->gr_name);
    269 		lastid = id;
    270 	}
    271 	(void)printf("\n");
    272 }
    273 
    274 void
    275 group(pw, nflag)
    276 	struct passwd *pw;
    277 	int nflag;
    278 {
    279 	struct group *gr;
    280 	int cnt, id, lastid, ngroups;
    281 	gid_t groups[NGROUPS + 1];
    282 	char *fmt;
    283 
    284 	if (pw) {
    285 		ngroups = NGROUPS + 1;
    286 		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    287 	} else {
    288 		groups[0] = getgid();
    289 		ngroups = getgroups(NGROUPS, groups + 1) + 1;
    290 	}
    291 	fmt = nflag ? "%s" : "%u";
    292 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
    293 		if (lastid == (id = groups[cnt]))
    294 			continue;
    295 		if (nflag) {
    296 			if ((gr = getgrgid(id)) != NULL)
    297 				(void)printf(fmt, gr->gr_name);
    298 			else
    299 				(void)printf(*fmt == ' ' ? " %u" : "%u",
    300 				    id);
    301 			fmt = " %s";
    302 		} else {
    303 			(void)printf(fmt, id);
    304 			fmt = " %u";
    305 		}
    306 		lastid = id;
    307 	}
    308 	(void)printf("\n");
    309 }
    310 
    311 struct passwd *
    312 who(u)
    313 	char *u;
    314 {
    315 	struct passwd *pw;
    316 	long id;
    317 	char *ep;
    318 
    319 	/*
    320 	 * Translate user argument into a pw pointer.  First, try to
    321 	 * get it as specified.  If that fails, try it as a number.
    322 	 */
    323 	if ((pw = getpwnam(u)) != NULL)
    324 		return(pw);
    325 	id = strtol(u, &ep, 10);
    326 	if (*u && !*ep && (pw = getpwuid(id)))
    327 		return(pw);
    328 	errx(1, "%s: No such user", u);
    329 	/* NOTREACHED */
    330 	return (NULL);
    331 }
    332 
    333 void
    334 usage()
    335 {
    336 	(void)fprintf(stderr, "usage: id [user]\n");
    337 	(void)fprintf(stderr, "       id -G [-n] [user]\n");
    338 	(void)fprintf(stderr, "       id -g [-nr] [user]\n");
    339 	(void)fprintf(stderr, "       id -p\n");
    340 	(void)fprintf(stderr, "       id -u [-nr] [user]\n");
    341 	exit(1);
    342 }
    343