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