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