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