Home | History | Annotate | Line # | Download | only in gen
getgrent.c revision 1.19.2.3
      1 /*	$NetBSD: getgrent.c,v 1.19.2.3 1997/06/02 04:57:37 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.3 1997/06/02 04:57:37 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 static int		_gr_nomore;
     67 
     68 static int grscan	__P((int, gid_t, const char *));
     69 static int matchline	__P((int, gid_t, const char *));
     70 static int start_gr	__P((void));
     71 
     72 #define	MAXGRP		200
     73 #define	MAXLINELENGTH	1024
     74 
     75 static char	*members[MAXGRP];
     76 static char	line[MAXLINELENGTH];
     77 
     78 #ifdef YP
     79 enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
     80 static enum _grmode	 __grmode;
     81 static char		*__ypcurrent, *__ypdomain;
     82 static int		 __ypcurrentlen;
     83 #endif
     84 
     85 #ifdef HESIOD
     86 static int	__gr_hesnum;
     87 #endif
     88 
     89 struct group *
     90 getgrent()
     91 {
     92 	_gr_nomore = 0;
     93 	if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL) || _gr_nomore)
     94 		return NULL;
     95 	return &_gr_group;
     96 }
     97 
     98 struct group *
     99 getgrnam(name)
    100 	const char *name;
    101 {
    102 	int rval;
    103 
    104 	if (!start_gr())
    105 		return NULL;
    106 	rval = grscan(1, 0, name);
    107 	if (!_gr_stayopen)
    108 		endgrent();
    109 	return (rval) ? &_gr_group : NULL;
    110 }
    111 
    112 struct group *
    113 getgrgid(gid)
    114 	gid_t gid;
    115 {
    116 	int rval;
    117 
    118 	if (!start_gr())
    119 		return NULL;
    120 	rval = grscan(1, gid, NULL);
    121 	if (!_gr_stayopen)
    122 		endgrent();
    123 	return (rval) ? &_gr_group : NULL;
    124 }
    125 
    126 static int
    127 start_gr()
    128 {
    129 #ifdef YP
    130 	__grmode = GRMODE_NONE;
    131 	if (__ypcurrent)
    132 		free(__ypcurrent);
    133 	__ypcurrent = NULL;
    134 #endif
    135 #ifdef HESIOD
    136 	__gr_hesnum = 0;
    137 #endif
    138 	if (_gr_fp) {
    139 		rewind(_gr_fp);
    140 		return 1;
    141 	}
    142 	return (_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0;
    143 }
    144 
    145 void
    146 setgrent()
    147 {
    148 	(void) setgroupent(0);
    149 }
    150 
    151 int
    152 setgroupent(stayopen)
    153 	int stayopen;
    154 {
    155 	if (!start_gr())
    156 		return 0;
    157 	_gr_stayopen = stayopen;
    158 	return 1;
    159 }
    160 
    161 void
    162 endgrent()
    163 {
    164 #ifdef YP
    165 	__grmode = GRMODE_NONE;
    166 	if (__ypcurrent)
    167 		free(__ypcurrent);
    168 	__ypcurrent = NULL;
    169 #endif
    170 #ifdef HESIOD
    171 	__gr_hesnum = 0;
    172 #endif
    173 	if (_gr_fp) {
    174 		(void)fclose(_gr_fp);
    175 		_gr_fp = NULL;
    176 	}
    177 }
    178 
    179 static int
    180 _local_grscan(rv, cb_data, ap)
    181 	void	*rv;
    182 	void	*cb_data;
    183 	va_list	 ap;
    184 {
    185 	int		 search = va_arg(ap, int);
    186 	gid_t		 gid = va_arg(ap, gid_t);
    187 	const char	*name = va_arg(ap, const char *);
    188 
    189 	for (;;) {
    190 		if (!fgets(line, sizeof(line), _gr_fp)) {
    191 			if (!search) {
    192 				_gr_nomore = 1;
    193 				return NS_SUCCESS;
    194 			}
    195 			return NS_NOTFOUND;
    196 		}
    197 		/* skip lines that are too big */
    198 		if (!strchr(line, '\n')) {
    199 			int ch;
    200 
    201 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    202 				;
    203 			continue;
    204 		}
    205 		if (matchline(search, gid, name))
    206 			return NS_SUCCESS;
    207 	}
    208 	/* NOTREACHED */
    209 }
    210 
    211 #ifdef HESIOD
    212 static int
    213 _dns_grscan(rv, cb_data, ap)
    214 	void	*rv;
    215 	void	*cb_data;
    216 	va_list	 ap;
    217 {
    218 	int		 search = va_arg(ap, int);
    219 	gid_t		 gid = va_arg(ap, gid_t);
    220 	const char	*name = va_arg(ap, const char *);
    221 
    222 	char		**hp;
    223 
    224 	for (;;) {
    225 		if (search) {
    226 			if (name)
    227 				strncpy(line, name, sizeof(line));
    228 			else
    229 				snprintf(line, sizeof(line), "%u", gid);
    230 		} else {
    231 			snprintf(line, sizeof(line), "group-%u", __gr_hesnum);
    232 			__gr_hesnum++;
    233 		}
    234 
    235 		line[sizeof(line) - 1] = '\0';
    236 		hp = hes_resolve(line, "group");
    237 		if (hp == NULL) {
    238 			switch (hes_error()) {
    239 			case HES_ER_NOTFOUND:
    240 				if (!search) {
    241 					__gr_hesnum = 0;
    242 					_gr_nomore = 1;
    243 					return NS_SUCCESS;
    244 				}
    245 				return NS_NOTFOUND;
    246 			case HES_ER_OK:
    247 				abort();
    248 			default:
    249 				return NS_UNAVAIL;
    250 			}
    251 		}
    252 
    253 						/* only check first elem */
    254 		strncpy(line, hp[0], sizeof(line));
    255 		line[sizeof(line) - 1] = '\0';
    256 		hes_free(hp);
    257 		if (matchline(search, gid, name))
    258 			return NS_SUCCESS;
    259 		else if (search)
    260 			return NS_NOTFOUND;
    261 	}
    262 }
    263 #endif
    264 
    265 #ifdef YP
    266 static int
    267 _nis_grscan(rv, cb_data, ap)
    268 	void	*rv;
    269 	void	*cb_data;
    270 	va_list	 ap;
    271 {
    272 	int		 search = va_arg(ap, int);
    273 	gid_t		 gid = va_arg(ap, gid_t);
    274 	const char	*name = va_arg(ap, const char *);
    275 
    276 	char	*key, *data;
    277 	int	 keylen, datalen;
    278 	int	 r;
    279 
    280 	if(__ypdomain == NULL) {
    281 		switch (yp_get_default_domain(&__ypdomain)) {
    282 		case 0:
    283 			break;
    284 		case YPERR_RESRC:
    285 			return NS_TRYAGAIN;
    286 		default:
    287 			return NS_UNAVAIL;
    288 		}
    289 	}
    290 
    291 	if (search) {			/* specific group or gid */
    292 		if (name)
    293 			strncpy(line, name, sizeof(line));
    294 		else
    295 			snprintf(line, sizeof(line), "%u", gid);
    296 		line[sizeof(line) - 1] = '\0';
    297 		data = NULL;
    298 		r = yp_match(__ypdomain,
    299 				(name) ? "group.byname" : "group.bygid",
    300 				line, strlen(line), &data, &datalen);
    301 		switch (r) {
    302 		case 0:
    303 			break;
    304 		case YPERR_KEY:
    305 			if (data)
    306 				free(data);
    307 			return NS_NOTFOUND;
    308 		default:
    309 			if (data)
    310 				free(data);
    311 			return NS_UNAVAIL;
    312 		}
    313 		data[datalen] = '\0';			/* clear trailing \n */
    314 		strncpy(line, data, sizeof(line));
    315 		line[sizeof(line) - 1] = '\0';
    316 		free(data);
    317 		if (matchline(search, gid, name))
    318 			return NS_SUCCESS;
    319 		else
    320 			return NS_NOTFOUND;
    321 	}
    322 
    323 	for (;;) {			/* ! search */
    324 		data = NULL;
    325 		if(__ypcurrent) {
    326 			key = NULL;
    327 			r = yp_next(__ypdomain, "group.byname",
    328 				__ypcurrent, __ypcurrentlen,
    329 				&key, &keylen, &data, &datalen);
    330 			free(__ypcurrent);
    331 			switch (r) {
    332 			case 0:
    333 				break;
    334 			case YPERR_NOMORE:
    335 				__ypcurrent = NULL;
    336 				if (key)
    337 					free(key);
    338 				if (data)
    339 					free(data);
    340 				_gr_nomore = 1;
    341 				return NS_SUCCESS;
    342 			default:
    343 				if (key)
    344 					free(key);
    345 				if (data)
    346 					free(data);
    347 				return NS_UNAVAIL;
    348 			}
    349 			__ypcurrent = key;
    350 			__ypcurrentlen = keylen;
    351 		} else {
    352 			if (yp_first(__ypdomain, "group.byname",
    353 					&__ypcurrent, &__ypcurrentlen,
    354 					&data, &datalen)) {
    355 				if (data);
    356 					free(data);
    357 				return NS_UNAVAIL;
    358 			}
    359 		}
    360 		data[datalen] = '\0';			/* clear trailing \n */
    361 		strncpy(line, data, sizeof(line));
    362 		line[sizeof(line) - 1] = '\0';
    363 		free(data);
    364 		if (matchline(search, gid, name))
    365 			return NS_SUCCESS;
    366 	}
    367 	/* NOTREACHED */
    368 }
    369 #endif
    370 
    371 #if defined(YP) || defined(HESIOD)
    372 /*
    373  * log an error if "files" or "compat" is specified in group_compat database
    374  */
    375 static int
    376 _bad_grscan(rv, cb_data, ap)
    377 	void	*rv;
    378 	void	*cb_data;
    379 	va_list	 ap;
    380 {
    381 	static int warned;
    382 	if (!warned) {
    383 		syslog(LOG_ERR,
    384 			"nsswitch.conf group_compat database can't use '%s'",
    385 			(char *)cb_data);
    386 	}
    387 	warned = 1;
    388 	return NS_UNAVAIL;
    389 }
    390 
    391 /*
    392  * when a name lookup in compat mode is required, look it up in group_compat
    393  * nsswitch database. only Hesiod and NIS is supported - it doesn't make
    394  * sense to lookup compat names from 'files' or 'compat'
    395  */
    396 static int
    397 __grscancompat(search, gid, name)
    398 	int		 search;
    399 	gid_t		 gid;
    400 	const char	*name;
    401 {
    402 	static ns_dtab	dtab;
    403 
    404 	if (dtab[NS_FILES].cb == NULL) {
    405 		NS_FILES_CB(dtab, _bad_grscan, "files");
    406 		NS_DNS_CB(dtab, _dns_grscan, NULL);
    407 		NS_NIS_CB(dtab, _nis_grscan, NULL);
    408 		NS_COMPAT_CB(dtab, _bad_grscan, "compat");
    409 	}
    410 
    411 	return nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, search, gid, name);
    412 }
    413 
    414 
    415 static int
    416 _compat_grscan(rv, cb_data, ap)
    417 	void	*rv;
    418 	void	*cb_data;
    419 	va_list	 ap;
    420 {
    421 	int		 search = va_arg(ap, int);
    422 	gid_t		 gid = va_arg(ap, gid_t);
    423 	const char	*name = va_arg(ap, const char *);
    424 
    425 	static char	*grname = NULL;
    426 
    427 	for (;;) {
    428 		if(__grmode != GRMODE_NONE) {
    429 			int	 r;
    430 
    431 			switch(__grmode) {
    432 			case GRMODE_FULL:
    433 				r = __grscancompat(search, gid, name);
    434 				if (r == NS_SUCCESS)
    435 					return r;
    436 				__grmode = GRMODE_NONE;
    437 				break;
    438 			case GRMODE_NAME:
    439 				if(grname == (char *)NULL) {
    440 					__grmode = GRMODE_NONE;
    441 					break;
    442 				}
    443 				r = __grscancompat(1, 0, grname);
    444 				free(grname);
    445 				grname = (char *)NULL;
    446 				if (r != NS_SUCCESS)
    447 					break;
    448 				if (!search)
    449 					return NS_SUCCESS;
    450 				if (name) {
    451 					if (! strcmp(_gr_group.gr_name, name))
    452 						return NS_SUCCESS;
    453 				} else {
    454 					if (_gr_group.gr_gid == gid)
    455 						return NS_SUCCESS;
    456 				}
    457 				break;
    458 			case GRMODE_NONE:
    459 				abort();
    460 			}
    461 			continue;
    462 		}
    463 
    464 		if (!fgets(line, sizeof(line), _gr_fp))
    465 			return NS_NOTFOUND;
    466 		/* skip lines that are too big */
    467 		if (!strchr(line, '\n')) {
    468 			int ch;
    469 
    470 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    471 				;
    472 			continue;
    473 		}
    474 
    475 		if (line[0] == '+') {
    476 			char	*tptr, *bp;
    477 
    478 			switch(line[1]) {
    479 			case ':':
    480 			case '\0':
    481 			case '\n':
    482 				__grmode = GRMODE_FULL;
    483 				break;
    484 			default:
    485 				__grmode = GRMODE_NAME;
    486 				bp = line;
    487 				tptr = strsep(&bp, ":\n");
    488 				grname = strdup(tptr + 1);
    489 				break;
    490 			}
    491 			continue;
    492 		}
    493 		if (matchline(search, gid, name))
    494 			return NS_SUCCESS;
    495 	}
    496 	/* NOTREACHED */
    497 }
    498 #endif /* YP || HESIOD */
    499 
    500 static int
    501 grscan(search, gid, name)
    502 	int		 search;
    503 	gid_t		 gid;
    504 	const char	*name;
    505 {
    506 	int		r;
    507 	static ns_dtab	dtab;
    508 
    509 	if (dtab[NS_FILES].cb == NULL) {
    510 		NS_FILES_CB(dtab, _local_grscan, NULL);
    511 		NS_DNS_CB(dtab, _dns_grscan, NULL);
    512 		NS_NIS_CB(dtab, _nis_grscan, NULL);
    513 		NS_COMPAT_CB(dtab, _compat_grscan, NULL);
    514 	}
    515 
    516 	r = nsdispatch(NULL, dtab, NSDB_GROUP, search, gid, name);
    517 	return (r == NS_SUCCESS) ? 1 : 0;
    518 }
    519 
    520 static int
    521 matchline(search, gid, name)
    522 	int		 search;
    523 	gid_t		 gid;
    524 	const char	*name;
    525 {
    526 	unsigned long	id;
    527 	char		*cp, **m;
    528 	char		*bp, *ep;
    529 
    530 	if (line[0] == '+')
    531 		return 0;	/* sanity check to prevent recursion */
    532 	bp = line;
    533 	_gr_group.gr_name = strsep(&bp, ":\n");
    534 	if (search && name && strcmp(_gr_group.gr_name, name))
    535 		return 0;
    536 	_gr_group.gr_passwd = strsep(&bp, ":\n");
    537 	if (!(cp = strsep(&bp, ":\n")))
    538 		return 0;
    539 	id = strtoul(cp, &ep, 10);
    540 	if (id > GID_MAX || *ep != '\0')
    541 		return 0;
    542 	_gr_group.gr_gid = (gid_t)id;
    543 	if (search && name == NULL && _gr_group.gr_gid != gid)
    544 		return 0;
    545 	cp = NULL;
    546 	if (bp == NULL)
    547 		return 0;
    548 	for (m = _gr_group.gr_mem = members;; bp++) {
    549 		if (m == &members[MAXGRP - 1])
    550 			break;
    551 		if (*bp == ',') {
    552 			if (cp) {
    553 				*bp = '\0';
    554 				*m++ = cp;
    555 				cp = NULL;
    556 			}
    557 		} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
    558 			if (cp) {
    559 				*bp = '\0';
    560 				*m++ = cp;
    561 			}
    562 			break;
    563 		} else if (cp == NULL)
    564 			cp = bp;
    565 	}
    566 	*m = NULL;
    567 	return 1;
    568 }
    569