Home | History | Annotate | Line # | Download | only in id
id.c revision 1.9
      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.9 1997/10/19 03:18:54 lukem 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 Gflag, ch, gflag, id, nflag, pflag, rflag, uflag;
     76 
     77 	Gflag = gflag = nflag = pflag = rflag = uflag = 0;
     78 	while ((ch = getopt(argc, argv, "Ggnpru")) != -1)
     79 		switch(ch) {
     80 		case 'G':
     81 			Gflag = 1;
     82 			break;
     83 		case 'g':
     84 			gflag = 1;
     85 			break;
     86 		case 'n':
     87 			nflag = 1;
     88 			break;
     89 		case 'p':
     90 			pflag = 1;
     91 			break;
     92 		case 'r':
     93 			rflag = 1;
     94 			break;
     95 		case 'u':
     96 			uflag = 1;
     97 			break;
     98 		case '?':
     99 		default:
    100 			usage();
    101 		}
    102 	argc -= optind;
    103 	argv += optind;
    104 
    105 	switch(Gflag + gflag + pflag + uflag) {
    106 	case 1:
    107 		break;
    108 	case 0:
    109 		if (!nflag && !rflag)
    110 			break;
    111 		/* FALLTHROUGH */
    112 	default:
    113 		usage();
    114 	}
    115 
    116 	pw = *argv ? who(*argv) : NULL;
    117 
    118 	if (gflag) {
    119 		id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
    120 		if (nflag && (gr = getgrgid(id)))
    121 			(void)printf("%s\n", gr->gr_name);
    122 		else
    123 			(void)printf("%u\n", id);
    124 		exit(0);
    125 	}
    126 
    127 	if (uflag) {
    128 		id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
    129 		if (nflag && (pw = getpwuid(id)))
    130 			(void)printf("%s\n", pw->pw_name);
    131 		else
    132 			(void)printf("%u\n", id);
    133 		exit(0);
    134 	}
    135 
    136 	if (Gflag) {
    137 		group(pw, nflag);
    138 		exit(0);
    139 	}
    140 
    141 	if (pflag) {
    142 		pretty(pw);
    143 		exit(0);
    144 	}
    145 
    146 	if (pw)
    147 		user(pw);
    148 	else
    149 		current();
    150 	exit(0);
    151 }
    152 
    153 void
    154 pretty(pw)
    155 	struct passwd *pw;
    156 {
    157 	struct group *gr;
    158 	u_int eid, rid;
    159 	char *login;
    160 
    161 	if (pw) {
    162 		(void)printf("uid\t%s\n", pw->pw_name);
    163 		(void)printf("groups\t");
    164 		group(pw, 1);
    165 	} else {
    166 		if ((login = getlogin()) == NULL)
    167 			err(1, "getlogin");
    168 
    169 		pw = getpwuid(rid = getuid());
    170 		if (pw == NULL || strcmp(login, pw->pw_name))
    171 			(void)printf("login\t%s\n", login);
    172 		if (pw)
    173 			(void)printf("uid\t%s\n", pw->pw_name);
    174 		else
    175 			(void)printf("uid\t%u\n", rid);
    176 
    177 		if ((eid = geteuid()) != rid)
    178 			if ((pw = getpwuid(eid)) != NULL)
    179 				(void)printf("euid\t%s", pw->pw_name);
    180 			else
    181 				(void)printf("euid\t%u", eid);
    182 		if ((rid = getgid()) != (eid = getegid()))
    183 			if ((gr = getgrgid(rid)) != NULL)
    184 				(void)printf("rgid\t%s\n", gr->gr_name);
    185 			else
    186 				(void)printf("rgid\t%u\n", rid);
    187 		(void)printf("groups\t");
    188 		group(NULL, 1);
    189 	}
    190 }
    191 
    192 void
    193 current()
    194 {
    195 	struct group *gr;
    196 	struct passwd *pw;
    197 	int cnt, id, eid, lastid, ngroups;
    198 	gid_t groups[NGROUPS];
    199 	char *fmt;
    200 
    201 	id = getuid();
    202 	(void)printf("uid=%u", id);
    203 	if ((pw = getpwuid(id)) != NULL)
    204 		(void)printf("(%s)", pw->pw_name);
    205 	if ((eid = geteuid()) != id) {
    206 		(void)printf(" euid=%u", eid);
    207 		if ((pw = getpwuid(eid)) != NULL)
    208 			(void)printf("(%s)", pw->pw_name);
    209 	}
    210 	id = getgid();
    211 	(void)printf(" gid=%u", id);
    212 	if ((gr = getgrgid(id)) != NULL)
    213 		(void)printf("(%s)", gr->gr_name);
    214 	if ((eid = getegid()) != id) {
    215 		(void)printf(" egid=%u", eid);
    216 		if ((gr = getgrgid(eid)) != NULL)
    217 			(void)printf("(%s)", gr->gr_name);
    218 	}
    219 	if ((ngroups = getgroups(NGROUPS, groups)) != NULL) {
    220 		for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
    221 		    fmt = ", %u", lastid = id) {
    222 			id = groups[cnt++];
    223 			if (lastid == id)
    224 				continue;
    225 			(void)printf(fmt, id);
    226 			if ((gr = getgrgid(id)) != NULL)
    227 				(void)printf("(%s)", gr->gr_name);
    228 		}
    229 	}
    230 	(void)printf("\n");
    231 }
    232 
    233 void
    234 user(pw)
    235 	struct passwd *pw;
    236 {
    237 	struct group *gr;
    238 	char *fmt;
    239 	int cnt, id, lastid, ngroups, groups[NGROUPS + 1];
    240 
    241 	id = pw->pw_uid;
    242 	(void)printf("uid=%u(%s)", id, pw->pw_name);
    243 	(void)printf(" gid=%u", pw->pw_gid);
    244 	if ((gr = getgrgid(pw->pw_gid)) != NULL)
    245 		(void)printf("(%s)", gr->gr_name);
    246 	ngroups = NGROUPS + 1;
    247 	(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    248 	fmt = " groups=%u";
    249 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
    250 		if (lastid == (id = groups[cnt]))
    251 			continue;
    252 		(void)printf(fmt, id);
    253 		fmt = " %u";
    254 		if ((gr = getgrgid(id)) != NULL)
    255 			(void)printf("(%s)", gr->gr_name);
    256 		lastid = id;
    257 	}
    258 	(void)printf("\n");
    259 }
    260 
    261 void
    262 group(pw, nflag)
    263 	struct passwd *pw;
    264 	int nflag;
    265 {
    266 	struct group *gr;
    267 	int cnt, id, lastid, ngroups;
    268 	gid_t groups[NGROUPS + 1];
    269 	char *fmt;
    270 
    271 	if (pw) {
    272 		ngroups = NGROUPS + 1;
    273 		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
    274 	} else {
    275 		groups[0] = getgid();
    276 		ngroups = getgroups(NGROUPS, groups + 1) + 1;
    277 	}
    278 	fmt = nflag ? "%s" : "%u";
    279 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
    280 		if (lastid == (id = groups[cnt]))
    281 			continue;
    282 		if (nflag) {
    283 			if ((gr = getgrgid(id)) != NULL)
    284 				(void)printf(fmt, gr->gr_name);
    285 			else
    286 				(void)printf(*fmt == ' ' ? " %u" : "%u",
    287 				    id);
    288 			fmt = " %s";
    289 		} else {
    290 			(void)printf(fmt, id);
    291 			fmt = " %u";
    292 		}
    293 		lastid = id;
    294 	}
    295 	(void)printf("\n");
    296 }
    297 
    298 struct passwd *
    299 who(u)
    300 	char *u;
    301 {
    302 	struct passwd *pw;
    303 	long id;
    304 	char *ep;
    305 
    306 	/*
    307 	 * Translate user argument into a pw pointer.  First, try to
    308 	 * get it as specified.  If that fails, try it as a number.
    309 	 */
    310 	if ((pw = getpwnam(u)) != NULL)
    311 		return(pw);
    312 	id = strtol(u, &ep, 10);
    313 	if (*u && !*ep && (pw = getpwuid(id)))
    314 		return(pw);
    315 	errx(1, "%s: No such user", u);
    316 	/* NOTREACHED */
    317 	return (NULL);
    318 }
    319 
    320 void
    321 usage()
    322 {
    323 	(void)fprintf(stderr, "usage: id [user]\n");
    324 	(void)fprintf(stderr, "       id -G [-n] [user]\n");
    325 	(void)fprintf(stderr, "       id -g [-nr] [user]\n");
    326 	(void)fprintf(stderr, "       id -u [-nr] [user]\n");
    327 	exit(1);
    328 }
    329