xref: /onnv-gate/usr/src/lib/nsswitch/nis/common/getnetgrent.c (revision 11262:b7ebfbf2359e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52830Sdjl  * Common Development and Distribution License (the "License").
62830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11262SRajagopal.Andra@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  *	nis/getnetgrent.c -- "nis" backend for nsswitch "netgroup" database
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  *	The API for netgroups differs sufficiently from that for the average
300Sstevel@tonic-gate  *	getXXXbyYYY function that we use very few of the support routines in
310Sstevel@tonic-gate  *	nis_common.h.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  *	The implementation of setnetgrent()/getnetgrent() here follows the
340Sstevel@tonic-gate  *	the 4.x code, inasmuch as the setnetgrent() routine does all the work
350Sstevel@tonic-gate  *	of traversing the netgroup graph and building a (potentially large)
360Sstevel@tonic-gate  *	list in memory, and getnetgrent() just steps down the list.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  *	An alternative, and probably better, implementation would lazy-eval
390Sstevel@tonic-gate  *	the netgroup graph in response to getnetgrent() calls (though
400Sstevel@tonic-gate  *	setnetgrent() should still check for the top-level netgroup name
410Sstevel@tonic-gate  *	and return NSS_SUCCESS / NSS_NOTFOUND).
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include "nis_common.h"
450Sstevel@tonic-gate #include <ctype.h>
460Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
470Sstevel@tonic-gate #include <malloc.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #ifdef	DEBUG
500Sstevel@tonic-gate #include <sys/syslog.h>
510Sstevel@tonic-gate #endif	/* DEBUG */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * The nss_backend_t for a getnetgrent() sequence;  we actually give the
550Sstevel@tonic-gate  *   netgroup frontend a pointer to one of these structures in response to
560Sstevel@tonic-gate  *   a (successful) setnetgrent() call on the nis_netgr_be backend
570Sstevel@tonic-gate  *   described further down in this file.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate struct nis_getnetgr_be;
610Sstevel@tonic-gate typedef nss_status_t	(*nis_getnetgr_op_t)(struct nis_getnetgr_be *, void *);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate struct nis_getnetgr_be {
640Sstevel@tonic-gate 	nis_getnetgr_op_t	*ops;
650Sstevel@tonic-gate 	nss_dbop_t		n_ops;
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * State for set/get/endnetgrent()
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	char			*netgroup;
700Sstevel@tonic-gate 	struct grouplist	*all_members;
710Sstevel@tonic-gate 	struct grouplist	*next_member;
720Sstevel@tonic-gate };
730Sstevel@tonic-gate 
740Sstevel@tonic-gate struct grouplist {  /* One element of the list generated by a setnetgrent() */
750Sstevel@tonic-gate 	char			*triple[NSS_NETGR_N];
760Sstevel@tonic-gate 	struct	grouplist	*gl_nxt;
770Sstevel@tonic-gate };
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static nss_status_t
getnetgr_set(be,a)800Sstevel@tonic-gate getnetgr_set(be, a)
810Sstevel@tonic-gate 	struct nis_getnetgr_be	*be;
820Sstevel@tonic-gate 	void			*a;
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	const char		*netgroup = (const char *) a;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (be->netgroup != 0 &&
870Sstevel@tonic-gate 	    strcmp(be->netgroup, netgroup) == 0) {
880Sstevel@tonic-gate 		/* We already have the member-list;  regurgitate it */
890Sstevel@tonic-gate 		be->next_member = be->all_members;
900Sstevel@tonic-gate 		return (NSS_SUCCESS);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 	return (NSS_NOTFOUND);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static nss_status_t
getnetgr_get(be,a)960Sstevel@tonic-gate getnetgr_get(be, a)
970Sstevel@tonic-gate 	struct nis_getnetgr_be	*be;
980Sstevel@tonic-gate 	void			*a;
990Sstevel@tonic-gate {
1002830Sdjl 	struct nss_getnetgrent_args *args = (struct nss_getnetgrent_args *)a;
1010Sstevel@tonic-gate 	struct grouplist	*mem;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if ((mem = be->next_member) == 0) {
1040Sstevel@tonic-gate 		args->status = NSS_NETGR_NO;
1050Sstevel@tonic-gate 	} else {
1060Sstevel@tonic-gate 		char			*buffer	= args->buffer;
1070Sstevel@tonic-gate 		int			buflen	= args->buflen;
1080Sstevel@tonic-gate 		enum nss_netgr_argn	i;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		args->status = NSS_NETGR_FOUND;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 		for (i = 0;  i < NSS_NETGR_N;  i++) {
1130Sstevel@tonic-gate 			const char	*str;
1140Sstevel@tonic-gate 			ssize_t	len;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 			if ((str = mem->triple[i]) == 0) {
1170Sstevel@tonic-gate 				args->retp[i] = 0;
1180Sstevel@tonic-gate 			} else if ((len = strlen(str) + 1) <= buflen) {
1190Sstevel@tonic-gate 				args->retp[i] = buffer;
1202830Sdjl 				(void) memcpy(buffer, str, len);
1210Sstevel@tonic-gate 				buffer += len;
1220Sstevel@tonic-gate 				buflen -= len;
1230Sstevel@tonic-gate 			} else {
1240Sstevel@tonic-gate 				args->status = NSS_NETGR_NOMEM;
1250Sstevel@tonic-gate 				break;
1260Sstevel@tonic-gate 			}
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 		be->next_member	= mem->gl_nxt;
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 	return (NSS_SUCCESS);	/* Yup, even for end-of-list, i.e. */
1310Sstevel@tonic-gate 				/* do NOT advance to next backend. */
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*ARGSUSED*/
1350Sstevel@tonic-gate static nss_status_t
getnetgr_end(be,dummy)1360Sstevel@tonic-gate getnetgr_end(be, dummy)
1370Sstevel@tonic-gate 	struct nis_getnetgr_be	*be;
1380Sstevel@tonic-gate 	void			*dummy;
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	struct grouplist	*gl;
1410Sstevel@tonic-gate 	struct grouplist	*next;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	for (gl = be->all_members; gl != NULL; gl = next) {
1440Sstevel@tonic-gate 		enum nss_netgr_argn	i;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 		next = gl->gl_nxt;
1470Sstevel@tonic-gate 		for (i = NSS_NETGR_MACHINE;  i < NSS_NETGR_N;  i++) {
1480Sstevel@tonic-gate 			if (gl->triple[i] != 0) {
1490Sstevel@tonic-gate 				free(gl->triple[i]);
1500Sstevel@tonic-gate 			}
1510Sstevel@tonic-gate 		}
1520Sstevel@tonic-gate 		free(gl);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	be->all_members = 0;
1550Sstevel@tonic-gate 	be->next_member = 0;
1560Sstevel@tonic-gate 	if (be->netgroup != 0) {
1570Sstevel@tonic-gate 		free(be->netgroup);
1580Sstevel@tonic-gate 		be->netgroup = 0;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	return (NSS_SUCCESS);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*ARGSUSED*/
1640Sstevel@tonic-gate static nss_status_t
getnetgr_destr(be,dummy)1650Sstevel@tonic-gate getnetgr_destr(be, dummy)
1660Sstevel@tonic-gate 	struct nis_getnetgr_be	*be;
1670Sstevel@tonic-gate 	void			*dummy;
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	if (be != 0) {
1702830Sdjl 		(void) getnetgr_end(be, (void *)0);
1710Sstevel@tonic-gate 		free(be);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	return (NSS_SUCCESS);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static nis_getnetgr_op_t getnetgr_ops[] = {
1770Sstevel@tonic-gate 	getnetgr_destr,
1780Sstevel@tonic-gate 	getnetgr_end,
1790Sstevel@tonic-gate 	getnetgr_set,
1800Sstevel@tonic-gate 	getnetgr_get,	/* getnetgrent_r() */
1810Sstevel@tonic-gate };
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * The nss_backend_t for innetgr() and setnetgrent().
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate struct nis_netgr_be;
1890Sstevel@tonic-gate typedef nss_status_t	(*nis_netgr_op_t)(struct nis_netgr_be *, void *);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate struct nis_netgr_be {
1920Sstevel@tonic-gate 	nis_netgr_op_t		*ops;
1930Sstevel@tonic-gate 	nss_dbop_t		n_ops;
1940Sstevel@tonic-gate 	const char		*domain;	/* (default) YP domain */
1950Sstevel@tonic-gate };
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * Code to do top-down search in the graph defined by the 'netgroup' YP map
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate  * ===> This code is now used for setnetgrent(), not just innetgr().
2040Sstevel@tonic-gate  *
2050Sstevel@tonic-gate  * If the easy way doesn't pan out, recursively search the 'netgroup' map.
2060Sstevel@tonic-gate  * In order to do this, we:
2070Sstevel@tonic-gate  *
2080Sstevel@tonic-gate  *    -	remember all the netgroup names we've seen during this search,
2090Sstevel@tonic-gate  *	whether or not we've expanded them yet (we want fast insertion
2100Sstevel@tonic-gate  *	with duplicate-detection, so use yet another chained hash table),
2110Sstevel@tonic-gate  *
2120Sstevel@tonic-gate  *    -	keep a list of all the netgroups we haven't expanded yet (we just
2130Sstevel@tonic-gate  *	want fast insertion and pop-first, so a linked list will do fine).
2140Sstevel@tonic-gate  *	If we insert at the head, we get a depth-first search;  insertion
2150Sstevel@tonic-gate  *	at the tail gives breadth-first (?), which seems preferable (?).
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * A netgrnam struct contains pointers for both the hash-table and the list.
2180Sstevel@tonic-gate  * It also contains the netgroup name;  note that we embed the name at the
2190Sstevel@tonic-gate  * end of the structure rather than holding a pointer to yet another
2200Sstevel@tonic-gate  * malloc()ed region.
2210Sstevel@tonic-gate  *
2220Sstevel@tonic-gate  * A netgrtab structure contains the hash-chain heads and the head/tail
2230Sstevel@tonic-gate  * pointers for the expansion list.
2240Sstevel@tonic-gate  *
225*11262SRajagopal.Andra@Sun.COM  * Most of this code is common to at least the NIS backend;  it
2260Sstevel@tonic-gate  * should be generalized and, presumably, moved into the frontend.
2270Sstevel@tonic-gate  * ==> Not any longer...
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate struct netgrnam {
2310Sstevel@tonic-gate 	struct netgrnam	*hash_chain;
2320Sstevel@tonic-gate 	struct netgrnam	*expand_next;
2330Sstevel@tonic-gate 	char		name[1];	/* Really [strlen(name) + 1] */
2340Sstevel@tonic-gate };
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate #define	HASHMOD	113
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate struct netgrtab {
2390Sstevel@tonic-gate 	struct netgrnam	*expand_first;
2400Sstevel@tonic-gate 	struct netgrnam	**expand_lastp;
2410Sstevel@tonic-gate 	struct netgrnam	*hash_heads[HASHMOD];
2420Sstevel@tonic-gate };
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate static void
ngt_init(ngt)2450Sstevel@tonic-gate ngt_init(ngt)
2460Sstevel@tonic-gate 	struct netgrtab	*ngt;
2470Sstevel@tonic-gate {
2482830Sdjl 	(void) memset((void *)ngt, 0, sizeof (*ngt));
2490Sstevel@tonic-gate 	ngt->expand_lastp = &ngt->expand_first;
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /* === ? Change ngt_init() and ngt_destroy() to malloc/free struct netgrtab */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate static void
2550Sstevel@tonic-gate /* ==> ? Should return 'failed' (out-of-memory) status ? */
ngt_insert(ngt,name,namelen)2560Sstevel@tonic-gate ngt_insert(ngt, name, namelen)
2570Sstevel@tonic-gate 	struct netgrtab	*ngt;
2580Sstevel@tonic-gate 	const char	*name;
2590Sstevel@tonic-gate 	size_t		namelen;
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	unsigned	hashval;
2620Sstevel@tonic-gate 	size_t		i;
2630Sstevel@tonic-gate 	struct netgrnam	*cur;
2640Sstevel@tonic-gate 	struct netgrnam	**head;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate #define	dummy		((struct netgrnam *)0)
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	for (hashval = 0, i = 0;  i < namelen;  i++) {
2690Sstevel@tonic-gate 		hashval = (hashval << 2) + hashval +
2700Sstevel@tonic-gate 			((const unsigned char *)name)[i];
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	head = &ngt->hash_heads[hashval % HASHMOD];
2730Sstevel@tonic-gate 	for (cur = *head;  cur != 0;  cur = cur->hash_chain) {
2740Sstevel@tonic-gate 		if (strncmp(cur->name, name, namelen) == 0 &&
2750Sstevel@tonic-gate 		    cur->name[namelen] == 0) {
2760Sstevel@tonic-gate 			return;		/* Already in table, do nothing */
2770Sstevel@tonic-gate 		}
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 	/* Create new netgrnam struct */
2800Sstevel@tonic-gate 	cur = (struct netgrnam *)
2810Sstevel@tonic-gate 		malloc(namelen + 1 + (char *)&dummy->name[0] - (char *)dummy);
2820Sstevel@tonic-gate 	if (cur == 0) {
2830Sstevel@tonic-gate 		return;			/* Out of memory, too bad */
2840Sstevel@tonic-gate 	}
2852830Sdjl 	(void) memcpy(cur->name, name, namelen);
2860Sstevel@tonic-gate 	cur->name[namelen] = 0;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/* Insert in hash table */
2890Sstevel@tonic-gate 	cur->hash_chain = *head;
2900Sstevel@tonic-gate 	*head = cur;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/* Insert in expansion list (insert at end for breadth-first search */
2930Sstevel@tonic-gate 	cur->expand_next = 0;
2940Sstevel@tonic-gate 	*ngt->expand_lastp = cur;
2950Sstevel@tonic-gate 	ngt->expand_lastp = &cur->expand_next;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate #undef	dummy
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate static const char *
ngt_next(ngt)3010Sstevel@tonic-gate ngt_next(ngt)
3020Sstevel@tonic-gate 	struct netgrtab	*ngt;
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	struct netgrnam	*first;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if ((first = ngt->expand_first) == 0) {
3070Sstevel@tonic-gate 		return (0);
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 	if ((ngt->expand_first = first->expand_next) == 0) {
3100Sstevel@tonic-gate 		ngt->expand_lastp = &ngt->expand_first;
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	return (first->name);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static void
ngt_destroy(ngt)3160Sstevel@tonic-gate ngt_destroy(ngt)
3170Sstevel@tonic-gate 	struct netgrtab	*ngt;
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	struct netgrnam	*cur;
3200Sstevel@tonic-gate 	struct netgrnam *next;
3210Sstevel@tonic-gate 	int		i;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	for (i = 0;  i < HASHMOD;  i++) {
3240Sstevel@tonic-gate 		for (cur = ngt->hash_heads[i];  cur != 0; /* cstyle */) {
3250Sstevel@tonic-gate 			next = cur->hash_chain;
3260Sstevel@tonic-gate 			free(cur);
3270Sstevel@tonic-gate 			cur = next;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	/* Don't bother zeroing pointers;  must do init if we want to reuse */
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate typedef const char *ccp;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate static nss_status_t
top_down(struct nis_netgr_be * be,const char ** groups,int ngroups,int (* func)(ccp triple[3],void * iter_args,nss_status_t * return_val),void * iter_args)3360Sstevel@tonic-gate top_down(struct nis_netgr_be *be, const char **groups, int ngroups,
3370Sstevel@tonic-gate     int (*func)(ccp triple[3], void *iter_args, nss_status_t *return_val),
3380Sstevel@tonic-gate     void *iter_args)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	struct netgrtab		*ngt;
3410Sstevel@tonic-gate 	/* netgrtab goes on the heap, not the stack, because it's large and */
3420Sstevel@tonic-gate 	/* stacks may not be all that big in multi-threaded programs. */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	const char		*group;
3450Sstevel@tonic-gate 	int			nfound;
3460Sstevel@tonic-gate 	int			done;
3470Sstevel@tonic-gate 	nss_status_t		result;
3480Sstevel@tonic-gate 
3492830Sdjl 	if ((ngt = (struct netgrtab *)malloc(sizeof (*ngt))) == 0) {
3500Sstevel@tonic-gate 		return (NSS_UNAVAIL);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 	ngt_init(ngt);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	while (ngroups > 0) {
3550Sstevel@tonic-gate 		ngt_insert(ngt, *groups, strlen(*groups));
3560Sstevel@tonic-gate 		groups++;
3570Sstevel@tonic-gate 		ngroups--;
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	done	= 0;	/* Set to 1 to indicate that we cut the iteration  */
3610Sstevel@tonic-gate 			/*   short (and 'result' holds the return value)   */
3620Sstevel@tonic-gate 	nfound	= 0;	/* Number of successful netgroup yp_match calls	   */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	while (!done && (group = ngt_next(ngt)) != 0) {
3650Sstevel@tonic-gate 		char		*val;
3660Sstevel@tonic-gate 		int		vallen;
3670Sstevel@tonic-gate 		char		*p;
3680Sstevel@tonic-gate 		int		yperr;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		result = _nss_nis_ypmatch(be->domain, "netgroup", group,
371*11262SRajagopal.Andra@Sun.COM 		    &val, &vallen, &yperr);
3720Sstevel@tonic-gate 		if (result != NSS_SUCCESS) {
3732830Sdjl 			/*LINTED E_NOP_IF_STMT*/
3740Sstevel@tonic-gate 			if (result == NSS_NOTFOUND) {
3752830Sdjl 				;
3760Sstevel@tonic-gate #ifdef	DEBUG
3770Sstevel@tonic-gate 				syslog(LOG_WARNING,
3780Sstevel@tonic-gate 				    "NIS netgroup lookup: %s doesn't exist",
3790Sstevel@tonic-gate 				    group);
3800Sstevel@tonic-gate #endif	/* DEBUG */
3810Sstevel@tonic-gate 			} else {
3820Sstevel@tonic-gate #ifdef	DEBUG
3830Sstevel@tonic-gate 				syslog(LOG_WARNING,
3840Sstevel@tonic-gate 			"NIS netgroup lookup: yp_match returned [%s]",
3850Sstevel@tonic-gate 				    yperr_string(yperr));
3860Sstevel@tonic-gate #endif	/* DEBUG */
3870Sstevel@tonic-gate 				done = 1;	/* Give up, return result */
3880Sstevel@tonic-gate 			}
3890Sstevel@tonic-gate 			/* Don't need to clean up anything */
3900Sstevel@tonic-gate 			continue;
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 		nfound++;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 		if ((p = strpbrk(val, "#\n")) != 0) {
3960Sstevel@tonic-gate 			*p = '\0';
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 		p = val;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		/* Parse val into triples and recursive netgroup references */
4010Sstevel@tonic-gate 		/*CONSTCOND*/
4020Sstevel@tonic-gate 		while (1) {
4030Sstevel@tonic-gate 			ccp			triple[NSS_NETGR_N];
4040Sstevel@tonic-gate 			int			syntax_err;
4050Sstevel@tonic-gate 			enum nss_netgr_argn	i;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 			while (isspace(*p)) {
4080Sstevel@tonic-gate 				p++;
4090Sstevel@tonic-gate 			}
4100Sstevel@tonic-gate 			if (*p == '\0') {
4110Sstevel@tonic-gate 				/* Finished processing this particular val */
4120Sstevel@tonic-gate 				break;
4130Sstevel@tonic-gate 			}
4140Sstevel@tonic-gate 			if (*p != '(') {
4150Sstevel@tonic-gate 				/* Doesn't look like the start of a triple, */
4160Sstevel@tonic-gate 				/*   so assume it's a recursive netgroup.   */
4170Sstevel@tonic-gate 				char *start = p;
4180Sstevel@tonic-gate 				p = strpbrk(start, " \t");
4190Sstevel@tonic-gate 				if (p == 0) {
4200Sstevel@tonic-gate 					/* Point p at the final '\0' */
4210Sstevel@tonic-gate 					p = start + strlen(start);
4220Sstevel@tonic-gate 				}
4230Sstevel@tonic-gate 				ngt_insert(ngt, start, (size_t)(p - start));
4240Sstevel@tonic-gate 				continue;
4250Sstevel@tonic-gate 			}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 			/* Main case:  a (machine, user, domain) triple */
4280Sstevel@tonic-gate 			p++;
4290Sstevel@tonic-gate 			syntax_err = 0;
4300Sstevel@tonic-gate 			for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
4310Sstevel@tonic-gate 				char		*start;
4320Sstevel@tonic-gate 				char		*limit;
4330Sstevel@tonic-gate 				const char	*terminators = ",) \t";
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 				if (i == NSS_NETGR_DOMAIN) {
4360Sstevel@tonic-gate 					/* Don't allow comma */
4370Sstevel@tonic-gate 					terminators++;
4380Sstevel@tonic-gate 				}
4390Sstevel@tonic-gate 				while (isspace(*p)) {
4400Sstevel@tonic-gate 					p++;
4410Sstevel@tonic-gate 				}
4420Sstevel@tonic-gate 				start = p;
4430Sstevel@tonic-gate 				limit = strpbrk(start, terminators);
4440Sstevel@tonic-gate 				if (limit == 0) {
4450Sstevel@tonic-gate 					syntax_err++;
4460Sstevel@tonic-gate 					break;
4470Sstevel@tonic-gate 				}
4480Sstevel@tonic-gate 				p = limit;
4490Sstevel@tonic-gate 				while (isspace(*p)) {
4500Sstevel@tonic-gate 					p++;
4510Sstevel@tonic-gate 				}
4520Sstevel@tonic-gate 				if (*p == terminators[0]) {
4530Sstevel@tonic-gate 					/*
4540Sstevel@tonic-gate 					 * Successfully parsed this name and
4550Sstevel@tonic-gate 					 *   the separator after it (comma or
4560Sstevel@tonic-gate 					 *   right paren); leave p ready for
4570Sstevel@tonic-gate 					 *   next parse.
4580Sstevel@tonic-gate 					 */
4590Sstevel@tonic-gate 					p++;
4600Sstevel@tonic-gate 					if (start == limit) {
4610Sstevel@tonic-gate 						/* Wildcard */
4620Sstevel@tonic-gate 						triple[i] = 0;
4630Sstevel@tonic-gate 					} else {
4640Sstevel@tonic-gate 						*limit = '\0';
4650Sstevel@tonic-gate 						triple[i] = start;
4660Sstevel@tonic-gate 					}
4670Sstevel@tonic-gate 				} else {
4680Sstevel@tonic-gate 					syntax_err++;
4690Sstevel@tonic-gate 					break;
4700Sstevel@tonic-gate 				}
4710Sstevel@tonic-gate 			}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 			if (syntax_err) {
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * ===> log it;
4760Sstevel@tonic-gate  * ===> try skipping past next ')';  failing that, abandon the line;
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate 				break;	/* Abandon this line */
4790Sstevel@tonic-gate 			} else if (!(*func)(triple, iter_args, &result)) {
4800Sstevel@tonic-gate 				/* Return result, good or bad */
4810Sstevel@tonic-gate 				done = 1;
4820Sstevel@tonic-gate 				break;
4830Sstevel@tonic-gate 			}
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 		/* End of inner loop over val[] */
4860Sstevel@tonic-gate 		free(val);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	/* End of outer loop (!done && ngt_next(ngt) != 0) */
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	ngt_destroy(ngt);
4910Sstevel@tonic-gate 	free(ngt);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (done) {
4940Sstevel@tonic-gate 		return (result);
4950Sstevel@tonic-gate 	} else if (nfound > 0) {
4960Sstevel@tonic-gate 		/* ==== ? Should only do this if all the top-level groups */
4970Sstevel@tonic-gate 		/*	  exist in YP?					  */
4980Sstevel@tonic-gate 		return (NSS_SUCCESS);
4990Sstevel@tonic-gate 	} else {
5000Sstevel@tonic-gate 		return (NSS_NOTFOUND);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * Code for setnetgrent()
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate /*
5100Sstevel@tonic-gate  * Iterator function for setnetgrent():  copy triple, add to be->all_members
5110Sstevel@tonic-gate  */
5120Sstevel@tonic-gate static int
save_triple(ccp trippp[NSS_NETGR_N],void * headp_arg,nss_status_t * return_val)5130Sstevel@tonic-gate save_triple(ccp trippp[NSS_NETGR_N], void *headp_arg,
5140Sstevel@tonic-gate     nss_status_t *return_val)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	struct grouplist	**headp = headp_arg;
5170Sstevel@tonic-gate 	struct grouplist	*gl;
5180Sstevel@tonic-gate 	enum nss_netgr_argn	i;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if ((gl = (struct grouplist *)malloc(sizeof (*gl))) == 0) {
5210Sstevel@tonic-gate 		/* Out of memory */
5220Sstevel@tonic-gate 		*return_val = NSS_UNAVAIL;
5230Sstevel@tonic-gate 		return (0);
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 	for (i = NSS_NETGR_MACHINE;  i < NSS_NETGR_N;  i++) {
5260Sstevel@tonic-gate 		if (trippp[i] == 0) {
5270Sstevel@tonic-gate 			/* Wildcard */
5280Sstevel@tonic-gate 			gl->triple[i] = 0;
5290Sstevel@tonic-gate 		} else if ((gl->triple[i] = strdup(trippp[i])) == 0) {
5300Sstevel@tonic-gate 			/* Out of memory.  Free any we've allocated */
5310Sstevel@tonic-gate 			enum nss_netgr_argn	j;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 			for (j = NSS_NETGR_MACHINE;  j < i;  j++) {
5340Sstevel@tonic-gate 				if (gl->triple[j] != 0) {
5350Sstevel@tonic-gate 					free(gl->triple[j]);
5360Sstevel@tonic-gate 				}
5370Sstevel@tonic-gate 			}
5380Sstevel@tonic-gate 			*return_val = NSS_UNAVAIL;
5390Sstevel@tonic-gate 			return (0);
5400Sstevel@tonic-gate 		}
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 	gl->gl_nxt = *headp;
5430Sstevel@tonic-gate 	*headp = gl;
5440Sstevel@tonic-gate 	return (1);	/* Tell top_down() to keep iterating */
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate static nss_status_t
netgr_set(be,a)5480Sstevel@tonic-gate netgr_set(be, a)
5490Sstevel@tonic-gate 	struct nis_netgr_be	*be;
5500Sstevel@tonic-gate 	void			*a;
5510Sstevel@tonic-gate {
5522830Sdjl 	struct nss_setnetgrent_args *args = (struct nss_setnetgrent_args *)a;
5530Sstevel@tonic-gate 	struct nis_getnetgr_be	*get_be;
5540Sstevel@tonic-gate 	nss_status_t		res;
5550Sstevel@tonic-gate 
5562830Sdjl 	get_be = (struct nis_getnetgr_be *)malloc(sizeof (*get_be));
5570Sstevel@tonic-gate 	if (get_be == 0) {
5580Sstevel@tonic-gate 		return (NSS_UNAVAIL);
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	get_be->all_members = 0;
5620Sstevel@tonic-gate 	res = top_down(be, &args->netgroup, 1, save_triple,
5630Sstevel@tonic-gate 		&get_be->all_members);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if (res == NSS_SUCCESS) {
5660Sstevel@tonic-gate 		get_be->ops		= getnetgr_ops;
5670Sstevel@tonic-gate 		get_be->n_ops		= sizeof (getnetgr_ops) /
5680Sstevel@tonic-gate 						sizeof (getnetgr_ops[0]);
5690Sstevel@tonic-gate 		get_be->netgroup	= strdup(args->netgroup);
5700Sstevel@tonic-gate 		get_be->next_member	= get_be->all_members;
5710Sstevel@tonic-gate 
5722830Sdjl 		args->iterator		= (nss_backend_t *)get_be;
5730Sstevel@tonic-gate 	} else {
5740Sstevel@tonic-gate 		args->iterator		= 0;
5750Sstevel@tonic-gate 		free(get_be);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 	return (res);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate  * Code for innetgr()
5830Sstevel@tonic-gate  */
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate  * Iterator function for innetgr():  Check whether triple matches args
5870Sstevel@tonic-gate  */
5880Sstevel@tonic-gate static int
match_triple(ccp triple[NSS_NETGR_N],void * ia_arg,nss_status_t * return_val)5890Sstevel@tonic-gate match_triple(ccp triple[NSS_NETGR_N], void *ia_arg, nss_status_t *return_val)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	struct nss_innetgr_args	*ia = ia_arg;
5920Sstevel@tonic-gate 	enum nss_netgr_argn	i;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	for (i = NSS_NETGR_MACHINE;  i < NSS_NETGR_N;  i++) {
5950Sstevel@tonic-gate 		int		(*cmpf)(const char *, const char *);
5960Sstevel@tonic-gate 		char		**argv;
5970Sstevel@tonic-gate 		int		n;
5980Sstevel@tonic-gate 		const char	*name = triple[i];
5990Sstevel@tonic-gate 		int		argc = ia->arg[i].argc;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 		if (argc == 0 || name == 0) {
6020Sstevel@tonic-gate 			/* Wildcarded on one side or t'other */
6030Sstevel@tonic-gate 			continue;
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 		argv = ia->arg[i].argv;
6060Sstevel@tonic-gate 		cmpf = (i == NSS_NETGR_MACHINE) ? strcasecmp : strcmp;
6070Sstevel@tonic-gate 		for (n = 0;  n < argc;  n++) {
6080Sstevel@tonic-gate 			if ((*cmpf)(argv[n], name) == 0) {
6090Sstevel@tonic-gate 				break;
6100Sstevel@tonic-gate 			}
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 		if (n >= argc) {
6130Sstevel@tonic-gate 			/* Match failed, tell top_down() to keep looking */
6140Sstevel@tonic-gate 			return (1);
6150Sstevel@tonic-gate 		}
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 	/* Matched on all three, so quit looking and declare victory */
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	ia->status = NSS_NETGR_FOUND;
6200Sstevel@tonic-gate 	*return_val = NSS_SUCCESS;
6210Sstevel@tonic-gate 	return (0);
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate  * inlist() -- return 1 if at least one item from the "what" list
6260Sstevel@tonic-gate  *   is in the comma-separated, newline-terminated "list"
6270Sstevel@tonic-gate  */
6280Sstevel@tonic-gate static const char comma = ',';	/* Don't let 'cfix' near this */
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate static int
inlist(nwhat,pwhat,list)6310Sstevel@tonic-gate inlist(nwhat, pwhat, list)
6320Sstevel@tonic-gate 	nss_innetgr_argc	nwhat;
6330Sstevel@tonic-gate 	nss_innetgr_argv	pwhat;
6340Sstevel@tonic-gate 	char			*list;
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	char			*p;
6370Sstevel@tonic-gate 	nss_innetgr_argc	nw;
6380Sstevel@tonic-gate 	nss_innetgr_argv	pw;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	while (*list != 0) {
6410Sstevel@tonic-gate 		while (*list == comma || isspace(*list))
6420Sstevel@tonic-gate 			list++;
6430Sstevel@tonic-gate 		for (p = list;  *p != 0 && *p != comma &&
6440Sstevel@tonic-gate 		    !isspace(*p); /* nothing */)
6450Sstevel@tonic-gate 			p++;
6460Sstevel@tonic-gate 		if (p != list) {
6470Sstevel@tonic-gate 			if (*p != 0)
6480Sstevel@tonic-gate 				*p++ = 0;
6490Sstevel@tonic-gate 			for (pw = pwhat, nw = nwhat;  nw != 0;  pw++, nw--) {
6500Sstevel@tonic-gate 				if (strcmp(list, *pw) == 0)
6510Sstevel@tonic-gate 					return (1);
6520Sstevel@tonic-gate 			}
6530Sstevel@tonic-gate 			list = p;
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 	return (0);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * Generate a key for a netgroup.byXXXX NIS map
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate static void
makekey(key,name,domain)6630Sstevel@tonic-gate makekey(key, name, domain)
6640Sstevel@tonic-gate 	char		*key;
6650Sstevel@tonic-gate 	const char	*name;
6660Sstevel@tonic-gate 	const char	*domain;
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	while (*key++ = *name++)
6690Sstevel@tonic-gate 		;
6700Sstevel@tonic-gate 	*(key-1) = '.';
6710Sstevel@tonic-gate 	while (*key++ = *domain++)
6720Sstevel@tonic-gate 		;
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate static int
makekey_lc(key,name,domain)6760Sstevel@tonic-gate makekey_lc(key, name, domain)
6770Sstevel@tonic-gate 	char		*key;
6780Sstevel@tonic-gate 	const char	*name;		/* Convert this to lowercase */
6790Sstevel@tonic-gate 	const char	*domain;	/* But not this */
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate 	int		found_uc = 0;
6820Sstevel@tonic-gate 	char		c;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	while (c = *name++) {
6850Sstevel@tonic-gate 		if (isupper(c)) {
6860Sstevel@tonic-gate 			++found_uc;
6870Sstevel@tonic-gate 			c = tolower(c);
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 		*key++ = c;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 	*key++ = '.';
6920Sstevel@tonic-gate 	while (*key++ = *domain++)
6930Sstevel@tonic-gate 		;
6940Sstevel@tonic-gate 	return (found_uc);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate /*
6980Sstevel@tonic-gate  * easy_way() --  try to use netgroup.byuser and netgroup.byhost maps to
6990Sstevel@tonic-gate  *		  get answers more efficiently than by recursive search.
7000Sstevel@tonic-gate  *
7010Sstevel@tonic-gate  * If more than one name (username or hostname) is specified, this approach
7020Sstevel@tonic-gate  * becomes less attractive;  at some point it's probably cheaper to do the
7030Sstevel@tonic-gate  * recursive search.  We don't know what the threshold is (among other things
7040Sstevel@tonic-gate  * it may depend on the site-specific struucture of netgroup information),
7050Sstevel@tonic-gate  * so here's a guesstimate.
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate #define	NNAME_THRESHOLD	5
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate static int
easy_way(be,ia,argp,map,try_lc,statusp)7110Sstevel@tonic-gate easy_way(be, ia, argp, map, try_lc, statusp)
7120Sstevel@tonic-gate 	struct nis_netgr_be	*be;
7130Sstevel@tonic-gate 	struct nss_innetgr_args	*ia;
7140Sstevel@tonic-gate 	struct nss_innetgr_1arg	*argp;
7150Sstevel@tonic-gate 	const char		*map;
7160Sstevel@tonic-gate 	int			try_lc;
7170Sstevel@tonic-gate 	nss_status_t		*statusp;
7180Sstevel@tonic-gate {
7190Sstevel@tonic-gate 	nss_innetgr_argc	nname = argp->argc;
7200Sstevel@tonic-gate 	nss_innetgr_argv	pname = argp->argv;
7210Sstevel@tonic-gate 	const char		*domain = ia->arg[NSS_NETGR_DOMAIN].argv[0];
7220Sstevel@tonic-gate 	const char		*wild = "*";
7230Sstevel@tonic-gate 	int			yperr;
7240Sstevel@tonic-gate 	char			*val;
7250Sstevel@tonic-gate 	int			vallen;
7260Sstevel@tonic-gate 	char			*key;
7270Sstevel@tonic-gate 	int			i;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	/* Our caller guaranteed that nname >= 1 */
7300Sstevel@tonic-gate 	while (nname > 1) {
7310Sstevel@tonic-gate 		struct nss_innetgr_1arg	just_one;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 		if (nname > NNAME_THRESHOLD) {
7340Sstevel@tonic-gate 			return (0);	/* May be cheaper to use 'netgroup' */
7350Sstevel@tonic-gate 		}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 		just_one.argc = 1;
7380Sstevel@tonic-gate 		just_one.argv = pname;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		if (easy_way(be, ia, &just_one, map, try_lc, statusp) &&
7410Sstevel@tonic-gate 		    ia->status == NSS_NETGR_FOUND) {
7420Sstevel@tonic-gate 			return (1);
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 		++pname;
7450Sstevel@tonic-gate 		--nname;
7460Sstevel@tonic-gate 		/* Fall through and do the last one inline */
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if ((key = malloc(strlen(*pname) + strlen(domain) + 2)) == 0) {
7500Sstevel@tonic-gate 		return (0);	/* Or maybe (1) and NSS_UNAVAIL */
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	for (i = 0;  i < (try_lc ? 6 : 4);  i++) {
7540Sstevel@tonic-gate 		switch (i) {
7550Sstevel@tonic-gate 		    case 0:
7560Sstevel@tonic-gate 			makekey(key, *pname, domain);
7570Sstevel@tonic-gate 			break;
7580Sstevel@tonic-gate 		    case 1:
7590Sstevel@tonic-gate 			makekey(key, wild, domain);
7600Sstevel@tonic-gate 			break;
7610Sstevel@tonic-gate 		    case 2:
7620Sstevel@tonic-gate 			makekey(key, *pname, wild);
7630Sstevel@tonic-gate 			break;
7640Sstevel@tonic-gate 		    case 3:
7650Sstevel@tonic-gate 			makekey(key, wild, wild);
7660Sstevel@tonic-gate 			break;
7670Sstevel@tonic-gate 		    case 4:
7680Sstevel@tonic-gate 			if (!makekey_lc(key, *pname, domain)) {
7690Sstevel@tonic-gate 				try_lc = 0;	/* Sleazy but effective */
7700Sstevel@tonic-gate 				continue;	/*   i.e. quit looping  */
7710Sstevel@tonic-gate 			}
7720Sstevel@tonic-gate 			break;
7730Sstevel@tonic-gate 		    case 5:
7740Sstevel@tonic-gate 			(void) makekey_lc(key, *pname, wild);
7750Sstevel@tonic-gate 			break;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 		*statusp = _nss_nis_ypmatch(be->domain, map, key,
7780Sstevel@tonic-gate 					&val, &vallen, &yperr);
7790Sstevel@tonic-gate 		if (*statusp == NSS_SUCCESS) {
7800Sstevel@tonic-gate 			if (inlist(ia->groups.argc, ia->groups.argv, val)) {
7810Sstevel@tonic-gate 				free(val);
7820Sstevel@tonic-gate 				free(key);
7830Sstevel@tonic-gate 				ia->status = NSS_NETGR_FOUND;
7840Sstevel@tonic-gate 				return (1);
7850Sstevel@tonic-gate 			} else {
7860Sstevel@tonic-gate 				free(val);
7870Sstevel@tonic-gate 			}
7880Sstevel@tonic-gate 		} else {
7890Sstevel@tonic-gate #ifdef DEBUG
7900Sstevel@tonic-gate 			syslog(LOG_WARNING,
7910Sstevel@tonic-gate 				"innetgr: yp_match(%s,%s) failed: %s",
7920Sstevel@tonic-gate 				map, key, yperr_string(yperr));
7930Sstevel@tonic-gate #endif	/* DEBUG */
7940Sstevel@tonic-gate 			if (yperr != YPERR_KEY)  {
7950Sstevel@tonic-gate 				free(key);
7960Sstevel@tonic-gate 				return (0);
7970Sstevel@tonic-gate 			}
7980Sstevel@tonic-gate 		}
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	free(key);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate /* =====> is this (an authoritative "no") always the right thing to do?	*/
8040Sstevel@tonic-gate /*	  Answer:  yes, except for hostnames that aren't all lowercase	*/
8050Sstevel@tonic-gate 
8063386Smichen 	*statusp = NSS_NOTFOUND;	/* Yup, three different flavours of */
8070Sstevel@tonic-gate 	ia->status = NSS_NETGR_NO;	/*   status information, so-called. */
8080Sstevel@tonic-gate 	return (1);			/*   Silly, innit?		    */
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate static nss_status_t
netgr_in(be,a)8130Sstevel@tonic-gate netgr_in(be, a)
8140Sstevel@tonic-gate 	struct nis_netgr_be	*be;
8150Sstevel@tonic-gate 	void			*a;
8160Sstevel@tonic-gate {
8172830Sdjl 	struct nss_innetgr_args	*ia = (struct nss_innetgr_args *)a;
8180Sstevel@tonic-gate 	nss_status_t		res;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	ia->status = NSS_NETGR_NO;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/* Can we use netgroup.byhost or netgroup.byuser to speed things up? */
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate /* ====> diddle this to try fast path for domains.argc == 0 too */
8250Sstevel@tonic-gate 	if (ia->arg[NSS_NETGR_DOMAIN].argc == 1) {
8260Sstevel@tonic-gate 		if (ia->arg[NSS_NETGR_MACHINE].argc == 0 &&
8270Sstevel@tonic-gate 		    ia->arg[NSS_NETGR_USER   ].argc != 0) {
8280Sstevel@tonic-gate 			if (easy_way(be, ia, &ia->arg[NSS_NETGR_USER],
8290Sstevel@tonic-gate 			    "netgroup.byuser", 0, &res)) {
8300Sstevel@tonic-gate 				return (res);
8310Sstevel@tonic-gate 			}
8320Sstevel@tonic-gate 		} else if (ia->arg[NSS_NETGR_USER].argc == 0 &&
8330Sstevel@tonic-gate 		    ia->arg[NSS_NETGR_MACHINE].argc != 0) {
8340Sstevel@tonic-gate 			if (easy_way(be, ia, &ia->arg[NSS_NETGR_MACHINE],
8350Sstevel@tonic-gate 			    "netgroup.byhost", 1, &res)) {
8360Sstevel@tonic-gate 				return (res);
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/* Nope, try the slow way */
8420Sstevel@tonic-gate 	ia->status = NSS_NETGR_NO;
8430Sstevel@tonic-gate 	res = top_down(be, (const char **)ia->groups.argv, ia->groups.argc,
8440Sstevel@tonic-gate 	    match_triple, ia);
8450Sstevel@tonic-gate 	return (res);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate /*
8500Sstevel@tonic-gate  * (Almost) boilerplate for a switch backend
8510Sstevel@tonic-gate  */
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate /*ARGSUSED*/
8542830Sdjl static nss_status_t
netgr_destr(be,dummy)8550Sstevel@tonic-gate netgr_destr(be, dummy)
8560Sstevel@tonic-gate 	struct nis_netgr_be	*be;
8570Sstevel@tonic-gate 	void			*dummy;
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate 	if (be != 0) {
8600Sstevel@tonic-gate 		free(be);
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 	return (NSS_SUCCESS);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate static nis_netgr_op_t netgroup_ops[] = {
8660Sstevel@tonic-gate 	netgr_destr,
8670Sstevel@tonic-gate 	0,		/* No endent, because no setent/getent */
8680Sstevel@tonic-gate 	0,		/* No setent;  setnetgrent() is really a getXbyY() */
8690Sstevel@tonic-gate 	0,		/* No getent in the normal sense */
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	netgr_in,	/* innetgr() */
8720Sstevel@tonic-gate 	netgr_set,	/* setnetgrent() */
8730Sstevel@tonic-gate };
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate /*ARGSUSED*/
8760Sstevel@tonic-gate nss_backend_t *
_nss_nis_netgroup_constr(dummy1,dummy2,dummy3)8770Sstevel@tonic-gate _nss_nis_netgroup_constr(dummy1, dummy2, dummy3)
8780Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	const char		*domain;
8810Sstevel@tonic-gate 	struct nis_netgr_be	*be;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if ((domain = _nss_nis_domain()) == 0 ||
8842830Sdjl 	    (be = (struct nis_netgr_be *)malloc(sizeof (*be))) == 0) {
8850Sstevel@tonic-gate 		return (0);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	be->ops		= netgroup_ops;
8880Sstevel@tonic-gate 	be->n_ops	= sizeof (netgroup_ops) / sizeof (netgroup_ops[0]);
8890Sstevel@tonic-gate 	be->domain	= domain;
8900Sstevel@tonic-gate 
8912830Sdjl 	return ((nss_backend_t *)be);
8920Sstevel@tonic-gate }
893