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