Home | History | Annotate | Line # | Download | only in gen
getnetgrent.c revision 1.24
      1 /*	$NetBSD: getnetgrent.c,v 1.24 1999/09/16 11:44:59 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Christos Zoulas
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Christos Zoulas.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * 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 #if defined(LIBC_SCCS) && !defined(lint)
     36 __RCSID("$NetBSD: getnetgrent.c,v 1.24 1999/09/16 11:44:59 lukem Exp $");
     37 #endif /* LIBC_SCCS and not lint */
     38 
     39 #include "namespace.h"
     40 #include <sys/types.h>
     41 
     42 #include <assert.h>
     43 #include <ctype.h>
     44 #include <db.h>
     45 #include <err.h>
     46 #include <fcntl.h>
     47 #define _NETGROUP_PRIVATE
     48 #include <netgroup.h>
     49 #include <nsswitch.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <stringlist.h>
     54 
     55 #ifdef YP
     56 #include <rpc/rpc.h>
     57 #include <rpcsvc/ypclnt.h>
     58 #include <rpcsvc/yp_prot.h>
     59 #endif
     60 
     61 #ifdef __STDC__
     62 #include <stdarg.h>
     63 #else
     64 #include <varargs.h>
     65 #endif
     66 
     67 #ifdef __weak_alias
     68 __weak_alias(endnetgrent,_endnetgrent);
     69 __weak_alias(getnetgrent,_getnetgrent);
     70 __weak_alias(innetgr,_innetgr);
     71 __weak_alias(setnetgrent,_setnetgrent);
     72 #endif
     73 
     74 #define _NG_STAR(s)	(((s) == NULL || *(s) == '\0') ? _ngstar : s)
     75 #define _NG_EMPTY(s)	((s) == NULL ? "" : s)
     76 #define _NG_ISSPACE(p)	(isspace((unsigned char) (p)) || (p) == '\n')
     77 
     78 static const char _ngstar[] = "*";
     79 static const char _ngoomem[] = "netgroup: %m";
     80 static struct netgroup *_nghead = (struct netgroup *)NULL;
     81 static struct netgroup *_nglist = (struct netgroup *)NULL;
     82 static DB *_ng_db;
     83 
     84 static int		getstring __P((char **, int, __aconst char **));
     85 static struct netgroup	*getnetgroup __P((char **));
     86 static int		 lookup __P((char *, char **, int));
     87 static void		 addgroup __P((StringList *, char *));
     88 static int		 in_check __P((const char *, const char *,
     89 				       const char *, struct netgroup *));
     90 static int		 in_find __P((StringList *, char *, const char *,
     91 				      const char *, const char *));
     92 static char		*in_lookup1 __P((const char *, const char *, int));
     93 static int		 in_lookup __P((const char *, const char *,
     94 					const char *, int));
     95 
     96 static const ns_src default_files_nis[] = {
     97 	{ NSSRC_FILES,	NS_SUCCESS | NS_NOTFOUND },
     98 #ifdef YP
     99 	{ NSSRC_NIS,	NS_SUCCESS },
    100 #endif
    101 	{ 0 }
    102 };
    103 
    104 /*
    105  * getstring(): Get a string delimited by the character, skipping leading and
    106  * trailing blanks and advancing the pointer
    107  */
    108 static int
    109 getstring(pp, del, str)
    110 	char	**pp;
    111 	int	  del;
    112 	char	__aconst **str;
    113 {
    114 	size_t len;
    115 	char *sp, *ep, *dp;
    116 
    117 	_DIAGASSERT(pp != NULL);
    118 	_DIAGASSERT(str != NULL);
    119 
    120 	/* skip leading blanks */
    121 	for (sp = *pp; *sp && _NG_ISSPACE(*sp); sp++)
    122 		continue;
    123 
    124 	/* accumulate till delimiter or space */
    125 	for (ep = sp; *ep && *ep != del && !_NG_ISSPACE(*ep); ep++)
    126 		continue;
    127 
    128 	/* hunt for the delimiter */
    129 	for (dp = ep; *dp && *dp != del && _NG_ISSPACE(*dp); dp++)
    130 		continue;
    131 
    132 	if (*dp != del) {
    133 		*str = NULL;
    134 		return 0;
    135 	}
    136 
    137 	*pp = ++dp;
    138 
    139 	len = (ep - sp) + 1;
    140 	if (len > 1) {
    141 		dp = malloc(len);
    142 		if (dp == NULL)
    143 			err(1, _ngoomem);
    144 		memcpy(dp, sp, len);
    145 		dp[len - 1] = '\0';
    146 	} else
    147 		dp = NULL;
    148 
    149 	*str = dp;
    150 	return 1;
    151 }
    152 
    153 
    154 /*
    155  * getnetgroup(): Parse a netgroup, and advance the pointer
    156  */
    157 static struct netgroup *
    158 getnetgroup(pp)
    159 	char	**pp;
    160 {
    161 	struct netgroup *ng;
    162 
    163 	_DIAGASSERT(pp != NULL);
    164 	_DIAGASSERT(*pp != NULL);
    165 
    166 	ng = malloc(sizeof(struct netgroup));
    167 	if (ng == NULL)
    168 		err(1, _ngoomem);
    169 
    170 	(*pp)++;	/* skip '(' */
    171 	if (!getstring(pp, ',', &ng->ng_host))
    172 		goto badhost;
    173 
    174 	if (!getstring(pp, ',', &ng->ng_user))
    175 		goto baduser;
    176 
    177 	if (!getstring(pp, ')', &ng->ng_domain))
    178 		goto baddomain;
    179 
    180 #ifdef DEBUG_NG
    181 	{
    182 		char buf[1024];
    183 		(void) fprintf(stderr, "netgroup %s\n",
    184 		    _ng_print(buf, sizeof(buf), ng));
    185 	}
    186 #endif
    187 	return ng;
    188 
    189 baddomain:
    190 	if (ng->ng_user)
    191 		free((char *)ng->ng_user);
    192 baduser:
    193 	if (ng->ng_host)
    194 		free((char *)ng->ng_host);
    195 badhost:
    196 	free(ng);
    197 	return NULL;
    198 }
    199 
    200 
    201 static int _local_lookup __P((void *, void *, va_list));
    202 
    203 /*ARGSUSED*/
    204 static int
    205 _local_lookup(rv, cb_data, ap)
    206 	void	*rv;
    207 	void	*cb_data;
    208 	va_list	ap;
    209 {
    210 	char	 *name = va_arg(ap, char *);
    211 	char	**line = va_arg(ap, char **);
    212 	int	  bywhat = va_arg(ap, int);
    213 
    214 	DBT	 key, data;
    215 	size_t	 len;
    216 	char	*ks;
    217 	int	 r;
    218 
    219 	if (_ng_db == NULL)
    220 		return NS_UNAVAIL;
    221 
    222 	len = strlen(name) + 2;
    223 	ks = malloc(len);
    224 	if (ks == NULL)
    225 		err(1, _ngoomem);
    226 
    227 	ks[0] = bywhat;
    228 	memcpy(&ks[1], name, len - 1);
    229 
    230 	key.data = (u_char *) ks;
    231 	key.size = len;
    232 
    233 	r = (_ng_db->get) (_ng_db, &key, &data, 0);
    234 	free(ks);
    235 	switch (r) {
    236 	case 0:
    237 		break;
    238 	case 1:
    239 		return NS_NOTFOUND;
    240 	case -1:
    241 			/* XXX: call endnetgrent() here ? */
    242 		return NS_UNAVAIL;
    243 	}
    244 
    245 	*line = strdup(data.data);
    246 	if (*line == NULL)
    247 		return NS_UNAVAIL;
    248 	return NS_SUCCESS;
    249 }
    250 
    251 #ifdef YP
    252 static int _nis_lookup __P((void *, void *, va_list));
    253 
    254 /*ARGSUSED*/
    255 static int
    256 _nis_lookup(rv, cb_data, ap)
    257 	void	*rv;
    258 	void	*cb_data;
    259 	va_list	 ap;
    260 {
    261 	char	 *name = va_arg(ap, char *);
    262 	char	**line = va_arg(ap, char **);
    263 	int	  bywhat = va_arg(ap, int);
    264 
    265 	static char	*__ypdomain;
    266 	int              i;
    267 	char            *map = NULL;
    268 
    269 	if(__ypdomain == NULL) {
    270 		switch (yp_get_default_domain(&__ypdomain)) {
    271 		case 0:
    272 			break;
    273 		case YPERR_RESRC:
    274 			return NS_TRYAGAIN;
    275 		default:
    276 			return NS_UNAVAIL;
    277 		}
    278 	}
    279 
    280 	switch (bywhat) {
    281 	case _NG_KEYBYNAME:
    282 		map = "netgroup";
    283 		break;
    284 
    285 	case _NG_KEYBYUSER:
    286 		map = "netgroup.byuser";
    287 		break;
    288 
    289 	case _NG_KEYBYHOST:
    290 		map = "netgroup.byhost";
    291 		break;
    292 
    293 	default:
    294 		abort();
    295 		break;
    296 	}
    297 
    298 
    299 	*line = NULL;
    300 	switch (yp_match(__ypdomain, map, name, (int)strlen(name), line, &i)) {
    301 	case 0:
    302 		return NS_SUCCESS;
    303 	case YPERR_KEY:
    304 		if (*line)
    305 			free(*line);
    306 		return NS_NOTFOUND;
    307 	default:
    308 		if (*line)
    309 			free(*line);
    310 		return NS_UNAVAIL;
    311 	}
    312 	/* NOTREACHED */
    313 }
    314 #endif
    315 
    316 /*
    317  * lookup(): Find the given key in the database or yp, and return its value
    318  * in *line; returns 1 if key was found, 0 otherwise
    319  */
    320 static int
    321 lookup(name, line, bywhat)
    322 	char	 *name;
    323 	char	**line;
    324 	int	  bywhat;
    325 {
    326 	int		r;
    327 	static const ns_dtab dtab[] = {
    328 		NS_FILES_CB(_local_lookup, NULL)
    329 		NS_NIS_CB(_nis_lookup, NULL)
    330 		{ 0 }
    331 	};
    332 
    333 	_DIAGASSERT(name != NULL);
    334 	_DIAGASSERT(line != NULL);
    335 
    336 	r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "lookup", default_files_nis,
    337 	    name, line, bywhat);
    338 	return (r == NS_SUCCESS) ? 1 : 0;
    339 }
    340 
    341 /*
    342  * _ng_parse(): Parse a line and return: _NG_ERROR: Syntax Error _NG_NONE:
    343  * line was empty or a comment _NG_GROUP: line had a netgroup definition,
    344  * returned in ng _NG_NAME:  line had a netgroup name, returned in name
    345  *
    346  * Public since used by netgroup_mkdb
    347  */
    348 int
    349 _ng_parse(p, name, ng)
    350 	char		**p;
    351 	char		**name;
    352 	struct netgroup	**ng;
    353 {
    354 
    355 	_DIAGASSERT(p != NULL);
    356 	_DIAGASSERT(*p != NULL);
    357 	_DIAGASSERT(name != NULL);
    358 	_DIAGASSERT(ng != NULL);
    359 #ifdef _DIAGNOSTIC
    360 	if (p == NULL || *p == NULL || name == NULL || ng == NULL)
    361 		return _NG_NONE;
    362 #endif
    363 
    364 	while (**p) {
    365 		if (**p == '#')
    366 			/* comment */
    367 			return _NG_NONE;
    368 
    369 		while (**p && _NG_ISSPACE(**p))
    370 			/* skipblank */
    371 			(*p)++;
    372 
    373 		if (**p == '(') {
    374 			if ((*ng = getnetgroup(p)) == NULL) {
    375 				warnx("netgroup: Syntax error `%s'", *p);
    376 				return _NG_ERROR;
    377 			}
    378 			return _NG_GROUP;
    379 		} else {
    380 			char	*np;
    381 			size_t	i;
    382 
    383 			for (np = *p; **p && !_NG_ISSPACE(**p); (*p)++)
    384 				continue;
    385 			if (np != *p) {
    386 				i = (*p - np) + 1;
    387 				*name = malloc(i);
    388 				if (*name == NULL)
    389 					err(1, _ngoomem);
    390 				memcpy(*name, np, i);
    391 				(*name)[i - 1] = '\0';
    392 				return _NG_NAME;
    393 			}
    394 		}
    395 	}
    396 	return _NG_NONE;
    397 }
    398 
    399 
    400 /*
    401  * addgroup(): Recursively add all the members of the netgroup to this group
    402  */
    403 static void
    404 addgroup(sl, grp)
    405 	StringList	*sl;
    406 	char		*grp;
    407 {
    408 	char		*line, *p;
    409 	struct netgroup	*ng;
    410 	char		*name;
    411 
    412 	_DIAGASSERT(sl != NULL);
    413 	_DIAGASSERT(grp != NULL);
    414 
    415 #ifdef DEBUG_NG
    416 	(void) fprintf(stderr, "addgroup(%s)\n", grp);
    417 #endif
    418 	/* check for cycles */
    419 	if (sl_find(sl, grp) != NULL) {
    420 		free(grp);
    421 		warnx("netgroup: Cycle in group `%s'", grp);
    422 		return;
    423 	}
    424 	sl_add(sl, grp);
    425 
    426 	/* Lookup this netgroup */
    427 	line = NULL;
    428 	if (!lookup(grp, &line, _NG_KEYBYNAME)) {
    429 		if (line != NULL)
    430 			free(line);
    431 		return;
    432 	}
    433 
    434 	p = line;
    435 
    436 	for (;;) {
    437 		switch (_ng_parse(&p, &name, &ng)) {
    438 		case _NG_NONE:
    439 			/* Done with the line */
    440 			free(line);
    441 			return;
    442 
    443 		case _NG_GROUP:
    444 			/* new netgroup */
    445 			/* add to the list */
    446 			ng->ng_next = _nglist;
    447 			_nglist = ng;
    448 			break;
    449 
    450 		case _NG_NAME:
    451 			/* netgroup name */
    452 			addgroup(sl, name);
    453 			break;
    454 
    455 		case _NG_ERROR:
    456 			return;
    457 
    458 		default:
    459 			abort();
    460 			return;
    461 		}
    462 	}
    463 }
    464 
    465 
    466 /*
    467  * in_check(): Compare the spec with the netgroup
    468  */
    469 static int
    470 in_check(host, user, domain, ng)
    471 	const char	*host;
    472 	const char	*user;
    473 	const char	*domain;
    474 	struct netgroup	*ng;
    475 {
    476 
    477 	/* host may be NULL */
    478 	/* user may be NULL */
    479 	/* domain may be NULL */
    480 	_DIAGASSERT(ng != NULL);
    481 
    482 	if ((host != NULL) && (ng->ng_host != NULL)
    483 	    && strcmp(ng->ng_host, host) != 0)
    484 		return 0;
    485 
    486 	if ((user != NULL) && (ng->ng_user != NULL)
    487 	    && strcmp(ng->ng_user, user) != 0)
    488 		return 0;
    489 
    490 	if ((domain != NULL) && (ng->ng_domain != NULL)
    491 	    && strcmp(ng->ng_domain, domain) != 0)
    492 		return 0;
    493 
    494 	return 1;
    495 }
    496 
    497 
    498 /*
    499  * in_find(): Find a match for the host, user, domain spec
    500  */
    501 static int
    502 in_find(sl, grp, host, user, domain)
    503 	StringList	*sl;
    504 	char		*grp;
    505 	const char	*host;
    506 	const char	*user;
    507 	const char	*domain;
    508 {
    509 	char		*line, *p;
    510 	int		 i;
    511 	struct netgroup	*ng;
    512 	char		*name;
    513 
    514 	_DIAGASSERT(sl != NULL);
    515 	_DIAGASSERT(grp != NULL);
    516 	/* host may be NULL */
    517 	/* user may be NULL */
    518 	/* domain may be NULL */
    519 
    520 #ifdef DEBUG_NG
    521 	(void) fprintf(stderr, "in_find(%s)\n", grp);
    522 #endif
    523 	/* check for cycles */
    524 	if (sl_find(sl, grp) != NULL) {
    525 		free(grp);
    526 		warnx("netgroup: Cycle in group `%s'", grp);
    527 		return 0;
    528 	}
    529 	sl_add(sl, grp);
    530 
    531 	/* Lookup this netgroup */
    532 	line = NULL;
    533 	if (!lookup(grp, &line, _NG_KEYBYNAME)) {
    534 		if (line)
    535 			free(line);
    536 		return 0;
    537 	}
    538 
    539 	p = line;
    540 
    541 	for (;;) {
    542 		switch (_ng_parse(&p, &name, &ng)) {
    543 		case _NG_NONE:
    544 			/* Done with the line */
    545 			free(line);
    546 			return 0;
    547 
    548 		case _NG_GROUP:
    549 			/* new netgroup */
    550 			i = in_check(host, user, domain, ng);
    551 			if (ng->ng_host != NULL)
    552 				free((char *)ng->ng_host);
    553 			if (ng->ng_user != NULL)
    554 				free((char *)ng->ng_user);
    555 			if (ng->ng_domain != NULL)
    556 				free((char *)ng->ng_domain);
    557 			free(ng);
    558 			if (i) {
    559 				free(line);
    560 				return 1;
    561 			}
    562 			break;
    563 
    564 		case _NG_NAME:
    565 			/* netgroup name */
    566 			if (in_find(sl, name, host, user, domain)) {
    567 				free(line);
    568 				return 1;
    569 			}
    570 			break;
    571 
    572 		case _NG_ERROR:
    573 			free(line);
    574 			return 0;
    575 
    576 		default:
    577 			abort();
    578 			return 0;
    579 		}
    580 	}
    581 }
    582 
    583 
    584 /*
    585  * _ng_makekey(): Make a key from the two names given. The key is of the form
    586  * <name1>.<name2> Names strings are replaced with * if they are empty;
    587  */
    588 char *
    589 _ng_makekey(s1, s2, len)
    590 	const char	*s1, *s2;
    591 	size_t		 len;
    592 {
    593 	char *buf;
    594 
    595 	/* s1 may be NULL */
    596 	/* s2 may be NULL */
    597 
    598 	buf = malloc(len);
    599 	if (buf == NULL)
    600 		err(1, _ngoomem);
    601 	(void) snprintf(buf, len, "%s.%s", _NG_STAR(s1), _NG_STAR(s2));
    602 	return buf;
    603 }
    604 
    605 void
    606 _ng_print(buf, len, ng)
    607 	char *buf;
    608 	size_t len;
    609 	const struct netgroup *ng;
    610 {
    611 	_DIAGASSERT(buf != NULL);
    612 	_DIAGASSERT(ng != NULL);
    613 #ifdef _DIAGNOSTIC
    614 	if (buf == NULL || ng == NULL)
    615 		return;
    616 #endif
    617 
    618 	(void) snprintf(buf, len, "(%s,%s,%s)", _NG_EMPTY(ng->ng_host),
    619 	    _NG_EMPTY(ng->ng_user), _NG_EMPTY(ng->ng_domain));
    620 }
    621 
    622 
    623 /*
    624  * in_lookup1(): Fast lookup for a key in the appropriate map
    625  */
    626 static char *
    627 in_lookup1(key, domain, map)
    628 	const char	*key;
    629 	const char	*domain;
    630 	int		 map;
    631 {
    632 	char	*line;
    633 	size_t	 len;
    634 	char	*ptr;
    635 	int	 res;
    636 
    637 	/* key may be NULL */
    638 	/* domain may be NULL */
    639 
    640 	len = (key ? strlen(key) : 1) + (domain ? strlen(domain) : 1) + 2;
    641 	ptr = _ng_makekey(key, domain, len);
    642 	res = lookup(ptr, &line, map);
    643 	free(ptr);
    644 	return res ? line : NULL;
    645 }
    646 
    647 
    648 /*
    649  * in_lookup(): Fast lookup for a key in the appropriate map
    650  */
    651 static int
    652 in_lookup(group, key, domain, map)
    653 	const char	*group;
    654 	const char	*key;
    655 	const char	*domain;
    656 	int		 map;
    657 {
    658 	size_t	 len;
    659 	char	*ptr, *line;
    660 
    661 	_DIAGASSERT(group != NULL);
    662 	/* key may be NULL */
    663 	/* domain may be NULL */
    664 
    665 	if (domain != NULL) {
    666 		/* Domain specified; look in "group.domain" and "*.domain" */
    667 		if ((line = in_lookup1(key, domain, map)) == NULL)
    668 			line = in_lookup1(NULL, domain, map);
    669 	}
    670 	else
    671 		line = NULL;
    672 
    673 	if (line == NULL) {
    674 		/*
    675 		 * domain not specified or domain lookup failed; look in
    676 		 * "group.*" and "*.*"
    677 		 */
    678 	    if (((line = in_lookup1(key, NULL, map)) == NULL) &&
    679 		((line = in_lookup1(NULL, NULL, map)) == NULL))
    680 		return 0;
    681 	}
    682 
    683 	len = strlen(group);
    684 
    685 	for (ptr = line; (ptr = strstr(ptr, group)) != NULL;)
    686 		/* Make sure we did not find a substring */
    687 		if ((ptr != line && ptr[-1] != ',') ||
    688 		    (ptr[len] != '\0' && strchr("\n\t ,", ptr[len]) == NULL))
    689 			ptr++;
    690 		else {
    691 			free(line);
    692 			return 1;
    693 		}
    694 
    695 	free(line);
    696 	return 0;
    697 }
    698 
    699 
    700 void
    701 endnetgrent()
    702 {
    703 	for (_nglist = _nghead; _nglist != NULL; _nglist = _nghead) {
    704 		_nghead = _nglist->ng_next;
    705 		if (_nglist->ng_host != NULL)
    706 			free((char *)_nglist->ng_host);
    707 		if (_nglist->ng_user != NULL)
    708 			free((char *)_nglist->ng_user);
    709 		if (_nglist->ng_domain != NULL)
    710 			free((char *)_nglist->ng_domain);
    711 		free(_nglist);
    712 	}
    713 
    714 	if (_ng_db) {
    715 		(void) (_ng_db->close) (_ng_db);
    716 		_ng_db = NULL;
    717 	}
    718 }
    719 
    720 
    721 void
    722 setnetgrent(ng)
    723 	const char	*ng;
    724 {
    725 	StringList	*sl = sl_init();
    726 	char		*ng_copy;
    727 
    728 	_DIAGASSERT(ng != NULL);
    729 #ifdef _DIAGNOSTIC
    730 	if (ng == NULL)
    731 		return;
    732 #endif
    733 
    734 	/* Cleanup any previous storage */
    735 	if (_nghead != NULL)
    736 		endnetgrent();
    737 
    738 	if (_ng_db == NULL)
    739 		_ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
    740 
    741 	ng_copy = strdup(ng);
    742 	if (ng_copy == NULL)
    743 		err(1, _ngoomem);
    744 	addgroup(sl, ng_copy);
    745 	_nghead = _nglist;
    746 	sl_free(sl, 1);
    747 }
    748 
    749 
    750 int
    751 getnetgrent(host, user, domain)
    752 	const char	**host;
    753 	const char	**user;
    754 	const char	**domain;
    755 {
    756 	_DIAGASSERT(host != NULL);
    757 	_DIAGASSERT(user != NULL);
    758 	_DIAGASSERT(domain != NULL);
    759 #ifdef _DIAGNOSTIC
    760 	if (host == NULL || user == NULL || domain == NULL)
    761 		return 0;
    762 #endif
    763 
    764 	if (_nglist == NULL)
    765 		return 0;
    766 
    767 	*host   = _nglist->ng_host;
    768 	*user   = _nglist->ng_user;
    769 	*domain = _nglist->ng_domain;
    770 
    771 	_nglist = _nglist->ng_next;
    772 
    773 	return 1;
    774 }
    775 
    776 
    777 int
    778 innetgr(grp, host, user, domain)
    779 	const char	*grp, *host, *user, *domain;
    780 {
    781 	int	 found;
    782 	StringList *sl;
    783 
    784 	_DIAGASSERT(grp != NULL);
    785 	/* host may be NULL */
    786 	/* user may be NULL */
    787 	/* domain may be NULL */
    788 #ifdef _DIAGNOSTIC
    789 	if (grp == NULL)
    790 		return 0;
    791 #endif
    792 
    793 	if (_ng_db == NULL)
    794 		_ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
    795 
    796 	/* Try the fast lookup first */
    797 	if (host != NULL && user == NULL) {
    798 		if (in_lookup(grp, host, domain, _NG_KEYBYHOST))
    799 			return 1;
    800 	} else if (host == NULL && user != NULL) {
    801 		if (in_lookup(grp, user, domain, _NG_KEYBYUSER))
    802 			return 1;
    803 	}
    804 	/* If a domainname is given, we would have found a match */
    805 	if (domain != NULL)
    806 		return 0;
    807 
    808 	/* Too bad need the slow recursive way */
    809 	sl = sl_init();
    810 	found = in_find(sl, strdup(grp), host, user, domain);
    811 	sl_free(sl, 1);
    812 
    813 	return found;
    814 }
    815