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