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