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