Home | History | Annotate | Line # | Download | only in gen
getgrent.c revision 1.19.2.2
      1 /*	$NetBSD: getgrent.c,v 1.19.2.2 1997/05/26 16:33:32 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
      7  * Portions Copyright (c) 1997, Luke Mewburn. All Rights Reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 #if 0
     40 static char sccsid[] = "@(#)getgrent.c	8.2 (Berkeley) 3/21/94";
     41 #else
     42 static char rcsid[] = "$NetBSD: getgrent.c,v 1.19.2.2 1997/05/26 16:33:32 lukem Exp $";
     43 #endif
     44 #endif /* LIBC_SCCS and not lint */
     45 
     46 #include <sys/types.h>
     47 #include <grp.h>
     48 #include <limits.h>
     49 #include <nsswitch.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <syslog.h>
     54 #ifdef HESIOD
     55 #include <hesiod.h>
     56 #endif
     57 #ifdef YP
     58 #include <rpc/rpc.h>
     59 #include <rpcsvc/yp_prot.h>
     60 #include <rpcsvc/ypclnt.h>
     61 #endif
     62 
     63 static FILE		*_gr_fp;
     64 static struct group	_gr_group;
     65 static int		_gr_stayopen;
     66 
     67 static int grscan	__P((int, int, const char *));
     68 static int matchline	__P((int, int, const char *));
     69 static int start_gr	__P((void));
     70 
     71 #define	MAXGRP		200
     72 #define	MAXLINELENGTH	1024
     73 
     74 static char	*members[MAXGRP];
     75 static char	line[MAXLINELENGTH];
     76 
     77 #ifdef YP
     78 enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
     79 static enum _grmode	 __grmode;
     80 static char		*__ypcurrent, *__ypdomain;
     81 static int		 __ypcurrentlen;
     82 #endif
     83 
     84 #ifdef HESIOD
     85 static int	__hesindex;
     86 #endif
     87 
     88 struct group *
     89 getgrent()
     90 {
     91 	if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL))
     92 		return(NULL);
     93 	return(&_gr_group);
     94 }
     95 
     96 struct group *
     97 getgrnam(name)
     98 	const char *name;
     99 {
    100 	int rval;
    101 
    102 	if (!start_gr())
    103 		return(NULL);
    104 	rval = grscan(1, 0, name);
    105 	if (!_gr_stayopen)
    106 		endgrent();
    107 	return(rval ? &_gr_group : NULL);
    108 }
    109 
    110 struct group *
    111 getgrgid(gid)
    112 	gid_t gid;
    113 {
    114 	int rval;
    115 
    116 	if (!start_gr())
    117 		return(NULL);
    118 	rval = grscan(1, gid, NULL);
    119 	if (!_gr_stayopen)
    120 		endgrent();
    121 	return(rval ? &_gr_group : NULL);
    122 }
    123 
    124 static int
    125 start_gr()
    126 {
    127 #ifdef YP
    128 	__grmode = GRMODE_NONE;
    129 	if (__ypcurrent)
    130 		free(__ypcurrent);
    131 	__ypcurrent = NULL;
    132 #endif
    133 #ifdef HESIOD
    134 	__hesindex = 0;
    135 #endif
    136 	if (_gr_fp) {
    137 		rewind(_gr_fp);
    138 		return(1);
    139 	}
    140 	return((_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0);
    141 }
    142 
    143 void
    144 setgrent()
    145 {
    146 	(void) setgroupent(0);
    147 }
    148 
    149 int
    150 setgroupent(stayopen)
    151 	int stayopen;
    152 {
    153 	if (!start_gr())
    154 		return(0);
    155 	_gr_stayopen = stayopen;
    156 	return(1);
    157 }
    158 
    159 void
    160 endgrent()
    161 {
    162 #ifdef YP
    163 	__grmode = GRMODE_NONE;
    164 	if (__ypcurrent)
    165 		free(__ypcurrent);
    166 	__ypcurrent = NULL;
    167 #endif
    168 #ifdef HESIOD
    169 	__hesindex = 0;
    170 #endif
    171 	if (_gr_fp) {
    172 		(void)fclose(_gr_fp);
    173 		_gr_fp = NULL;
    174 	}
    175 }
    176 
    177 static int
    178 _local_grscan(rv, cb_data, ap)
    179 	void	*rv;
    180 	void	*cb_data;
    181 	va_list	 ap;
    182 {
    183 	int		 search = va_arg(ap, int);
    184 	int		 gid = va_arg(ap, int);
    185 	const char	*name = va_arg(ap, const char *);
    186 
    187 	for (;;) {
    188 		if (!fgets(line, sizeof(line), _gr_fp))
    189 			return(NS_NOTFOUND);
    190 		/* skip lines that are too big */
    191 		if (!strchr(line, '\n')) {
    192 			int ch;
    193 
    194 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    195 				;
    196 			continue;
    197 		}
    198 		if (matchline(search, gid, name))
    199 			return(NS_SUCCESS);
    200 	}
    201 	/* NOTREACHED */
    202 }
    203 
    204 #ifdef HESIOD
    205 static int
    206 _dns_grscan(rv, cb_data, ap)
    207 	void	*rv;
    208 	void	*cb_data;
    209 	va_list	 ap;
    210 {
    211 	int		 search = va_arg(ap, int);
    212 	int		 gid = va_arg(ap, int);
    213 	const char	*name = va_arg(ap, const char *);
    214 
    215 	char		**hp;
    216 
    217 	for (;;) {
    218 		if (search) {
    219 			if (name)
    220 				strncpy(line, name, sizeof(line));
    221 			else
    222 				snprintf(line, sizeof(line), "%u", gid);
    223 		} else {
    224 			snprintf(line, sizeof(line), "group-%u", __hesindex);
    225 			__hesindex++;
    226 		}
    227 
    228 		line[sizeof(line) - 1] = '\0';
    229 		hp = hes_resolve(line, "group");
    230 		if (hp == NULL) {
    231 			switch (hes_error()) {
    232 			case HES_ER_NOTFOUND:
    233 				if (! search)
    234 					__hesindex = 0;
    235 				return(NS_NOTFOUND);
    236 			case HES_ER_OK:
    237 				abort();
    238 			default:
    239 				return(NS_UNAVAIL);
    240 			}
    241 		}
    242 
    243 						/* only check first elem */
    244 		strncpy(line, hp[0], sizeof(line));
    245 		line[sizeof(line) - 1] = '\0';
    246 		hes_free(hp);
    247 		if (matchline(search, gid, name))
    248 			return(NS_SUCCESS);
    249 		else if (search)
    250 			return(NS_NOTFOUND);
    251 	}
    252 }
    253 #endif
    254 
    255 #ifdef YP
    256 static int
    257 _nis_grscan(rv, cb_data, ap)
    258 	void	*rv;
    259 	void	*cb_data;
    260 	va_list	 ap;
    261 {
    262 	int		 search = va_arg(ap, int);
    263 	int		 gid = va_arg(ap, int);
    264 	const char	*name = va_arg(ap, const char *);
    265 
    266 	char	*key, *data;
    267 	int	 keylen, datalen;
    268 	int	 r;
    269 
    270 	if(__ypdomain == NULL) {
    271 		switch (yp_get_default_domain(&__ypdomain)) {
    272 		case 0:
    273 			break;
    274 		case YPERR_RESRC:
    275 			return(NS_TRYAGAIN);
    276 		default:
    277 			return(NS_UNAVAIL);
    278 		}
    279 	}
    280 
    281 	if (search) {			/* specific group or gid */
    282 		if (name)
    283 			strncpy(line, name, sizeof(line));
    284 		else
    285 			snprintf(line, sizeof(line), "%u", gid);
    286 		line[sizeof(line) - 1] = '\0';
    287 		data = NULL;
    288 		r = yp_match(__ypdomain,
    289 				(name) ? "group.byname" : "group.bygid",
    290 				line, strlen(line), &data, &datalen);
    291 		switch (r) {
    292 		case 0:
    293 			break;
    294 		case YPERR_KEY:
    295 			if (data)
    296 				free(data);
    297 			return(NS_NOTFOUND);
    298 		default:
    299 			if (data)
    300 				free(data);
    301 			return(NS_UNAVAIL);
    302 		}
    303 		data[datalen] = '\0';			/* clear trailing \n */
    304 		strncpy(line, data, sizeof(line));
    305 		line[sizeof(line) - 1] = '\0';
    306 		free(data);
    307 		if (matchline(search, gid, name))
    308 			return(NS_SUCCESS);
    309 		else
    310 			return(NS_NOTFOUND);
    311 	}
    312 
    313 	for (;;) {
    314 		data = NULL;
    315 		if(__ypcurrent) {
    316 			key = NULL;
    317 			r = yp_next(__ypdomain, "group.byname",
    318 				__ypcurrent, __ypcurrentlen,
    319 				&key, &keylen, &data, &datalen);
    320 			free(__ypcurrent);
    321 			switch (r) {
    322 			case 0:
    323 				break;
    324 			case YPERR_NOMORE:
    325 				__ypcurrent = NULL;
    326 				if (key)
    327 					free(key);
    328 				if (data)
    329 					free(data);
    330 				return(NS_NOTFOUND);
    331 			default:
    332 				if (key)
    333 					free(key);
    334 				if (data)
    335 					free(data);
    336 				return(NS_UNAVAIL);
    337 			}
    338 			__ypcurrent = key;
    339 			__ypcurrentlen = keylen;
    340 		} else {
    341 			if (yp_first(__ypdomain, "group.byname",
    342 					&__ypcurrent, &__ypcurrentlen,
    343 					&data, &datalen)) {
    344 				if (data);
    345 					free(data);
    346 				return(NS_UNAVAIL);
    347 			}
    348 		}
    349 		data[datalen] = '\0';			/* clear trailing \n */
    350 		strncpy(line, data, sizeof(line));
    351 		line[sizeof(line) - 1] = '\0';
    352 		free(data);
    353 		if (matchline(search, gid, name))
    354 			return(NS_SUCCESS);
    355 	}
    356 	/* NOTREACHED */
    357 }
    358 #endif
    359 
    360 #if defined(YP) || defined(HESIOD)
    361 /*
    362  * log an error if "files" or "compat" is specified in group_compat database
    363  */
    364 static int
    365 _bad_grscan(rv, cb_data, ap)
    366 	void	*rv;
    367 	void	*cb_data;
    368 	va_list	 ap;
    369 {
    370 	static int warned;
    371 	if (!warned) {
    372 		syslog(LOG_ERR,
    373 			"nsswitch.conf group_compat database can't use '%s'",
    374 			(char *)cb_data);
    375 	}
    376 	warned = 1;
    377 	return NS_UNAVAIL;
    378 }
    379 
    380 /*
    381  * when a name lookup in compat mode is required, look it up in group_compat
    382  * nsswitch database. only Hesiod and NIS is supported - it doesn't make
    383  * sense to lookup compat names from 'files' or 'compat'
    384  */
    385 static int
    386 __grscancompat(search, gid, name)
    387 	int		 search, gid;
    388 	const char	*name;
    389 {
    390 	static ns_dtab	dtab;
    391 
    392 	if (dtab[NS_FILES].cb == NULL) {
    393 		NS_FILES_CB(dtab, _bad_grscan, "files");
    394 		NS_DNS_CB(dtab, _dns_grscan, NULL);
    395 		NS_NIS_CB(dtab, _nis_grscan, NULL);
    396 		NS_COMPAT_CB(dtab, _bad_grscan, "compat");
    397 	}
    398 
    399 	return nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, search, gid, name);
    400 }
    401 
    402 
    403 static int
    404 _compat_grscan(rv, cb_data, ap)
    405 	void	*rv;
    406 	void	*cb_data;
    407 	va_list	 ap;
    408 {
    409 	int		 search = va_arg(ap, int);
    410 	int		 gid = va_arg(ap, int);
    411 	const char	*name = va_arg(ap, const char *);
    412 
    413 	static char	*grname = NULL;
    414 
    415 	for (;;) {
    416 		if(__grmode != GRMODE_NONE) {
    417 			int	 r;
    418 
    419 			switch(__grmode) {
    420 			case GRMODE_FULL:
    421 				r = __grscancompat(search, gid, name);
    422 				if (r == NS_SUCCESS)
    423 					return(r);
    424 				__grmode = GRMODE_NONE;
    425 				break;
    426 			case GRMODE_NAME:
    427 				if(grname == (char *)NULL) {
    428 					__grmode = GRMODE_NONE;
    429 					break;
    430 				}
    431 				r = __grscancompat(1, 0, grname);
    432 				free(grname);
    433 				grname = (char *)NULL;
    434 				if (r != NS_SUCCESS)
    435 					break;
    436 				if (!search)
    437 					return NS_SUCCESS;
    438 				if (name) {
    439 					if (! strcmp(_gr_group.gr_name, name))
    440 						return NS_SUCCESS;
    441 				} else {
    442 					if (_gr_group.gr_gid == gid)
    443 						return NS_SUCCESS;
    444 				}
    445 				break;
    446 			case GRMODE_NONE:
    447 				abort();
    448 			}
    449 			continue;
    450 		}
    451 
    452 		if (!fgets(line, sizeof(line), _gr_fp))
    453 			return(NS_NOTFOUND);
    454 		/* skip lines that are too big */
    455 		if (!strchr(line, '\n')) {
    456 			int ch;
    457 
    458 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    459 				;
    460 			continue;
    461 		}
    462 
    463 		if (line[0] == '+') {
    464 			char	*tptr, *bp;
    465 
    466 			switch(line[1]) {
    467 			case ':':
    468 			case '\0':
    469 			case '\n':
    470 				__grmode = GRMODE_FULL;
    471 				break;
    472 			default:
    473 				__grmode = GRMODE_NAME;
    474 				bp = line;
    475 				tptr = strsep(&bp, ":\n");
    476 				grname = strdup(tptr + 1);
    477 				break;
    478 			}
    479 			continue;
    480 		}
    481 		if (matchline(search, gid, name))
    482 			return(NS_SUCCESS);
    483 	}
    484 	/* NOTREACHED */
    485 }
    486 #endif /* YP || HESIOD */
    487 
    488 static int
    489 grscan(search, gid, name)
    490 	int		 search, gid;
    491 	const char	*name;
    492 {
    493 	int		r;
    494 	static ns_dtab	dtab;
    495 
    496 	if (dtab[NS_FILES].cb == NULL) {
    497 		NS_FILES_CB(dtab, _local_grscan, NULL);
    498 		NS_DNS_CB(dtab, _dns_grscan, NULL);
    499 		NS_NIS_CB(dtab, _nis_grscan, NULL);
    500 		NS_COMPAT_CB(dtab, _compat_grscan, NULL);
    501 	}
    502 
    503 	r = nsdispatch(NULL, dtab, NSDB_GROUP, search, gid, name);
    504 	return (r == NS_SUCCESS) ? 1 : 0;
    505 }
    506 
    507 static int
    508 matchline(search, gid, name)
    509 	int		 search, gid;
    510 	const char	*name;
    511 {
    512 	unsigned long	id;
    513 	char		*cp, **m;
    514 	char		*bp, *ep;
    515 
    516 	if (line[0] == '+')
    517 		return(0);	/* sanity check to prevent recursion */
    518 	bp = line;
    519 	_gr_group.gr_name = strsep(&bp, ":\n");
    520 	if (search && name && strcmp(_gr_group.gr_name, name))
    521 		return(0);
    522 	_gr_group.gr_passwd = strsep(&bp, ":\n");
    523 	if (!(cp = strsep(&bp, ":\n")))
    524 		return(0);
    525 	id = strtoul(cp, &ep, 10);
    526 	if (id > GID_MAX || *ep != '\0')
    527 		return(0);
    528 	_gr_group.gr_gid = (gid_t)id;
    529 	if (search && name == NULL && _gr_group.gr_gid != gid)
    530 		return(0);
    531 	cp = NULL;
    532 	if (bp == NULL)
    533 		return(0);
    534 	for (m = _gr_group.gr_mem = members;; bp++) {
    535 		if (m == &members[MAXGRP - 1])
    536 			break;
    537 		if (*bp == ',') {
    538 			if (cp) {
    539 				*bp = '\0';
    540 				*m++ = cp;
    541 				cp = NULL;
    542 			}
    543 		} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
    544 			if (cp) {
    545 				*bp = '\0';
    546 				*m++ = cp;
    547 			}
    548 			break;
    549 		} else if (cp == NULL)
    550 			cp = bp;
    551 	}
    552 	*m = NULL;
    553 	return(1);
    554 }
    555