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