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