17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*36e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *	nis/getnetgrent.c -- "nis" backend for nsswitch "netgroup" database
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  *	The API for netgroups differs sufficiently from that for the average
307c478bd9Sstevel@tonic-gate  *	getXXXbyYYY function that we use very few of the support routines in
317c478bd9Sstevel@tonic-gate  *	nis_common.h.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *	The implementation of setnetgrent()/getnetgrent() here follows the
347c478bd9Sstevel@tonic-gate  *	the 4.x code, inasmuch as the setnetgrent() routine does all the work
357c478bd9Sstevel@tonic-gate  *	of traversing the netgroup graph and building a (potentially large)
367c478bd9Sstevel@tonic-gate  *	list in memory, and getnetgrent() just steps down the list.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *	An alternative, and probably better, implementation would lazy-eval
397c478bd9Sstevel@tonic-gate  *	the netgroup graph in response to getnetgrent() calls (though
407c478bd9Sstevel@tonic-gate  *	setnetgrent() should still check for the top-level netgroup name
417c478bd9Sstevel@tonic-gate  *	and return NSS_SUCCESS / NSS_NOTFOUND).
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include "nis_common.h"
457c478bd9Sstevel@tonic-gate #include <ctype.h>
467c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
477c478bd9Sstevel@tonic-gate #include <malloc.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #ifdef	DEBUG
507c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
517c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * The nss_backend_t for a getnetgrent() sequence;  we actually give the
557c478bd9Sstevel@tonic-gate  *   netgroup frontend a pointer to one of these structures in response to
567c478bd9Sstevel@tonic-gate  *   a (successful) setnetgrent() call on the nis_netgr_be backend
577c478bd9Sstevel@tonic-gate  *   described further down in this file.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate struct nis_getnetgr_be;
617c478bd9Sstevel@tonic-gate typedef nss_status_t	(*nis_getnetgr_op_t)(struct nis_getnetgr_be *, void *);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate struct nis_getnetgr_be {
647c478bd9Sstevel@tonic-gate 	nis_getnetgr_op_t	*ops;
657c478bd9Sstevel@tonic-gate 	nss_dbop_t		n_ops;
667c478bd9Sstevel@tonic-gate 	/*
677c478bd9Sstevel@tonic-gate 	 * State for set/get/endnetgrent()
687c478bd9Sstevel@tonic-gate 	 */
697c478bd9Sstevel@tonic-gate 	char			*netgroup;
707c478bd9Sstevel@tonic-gate 	struct grouplist	*all_members;
717c478bd9Sstevel@tonic-gate 	struct grouplist	*next_member;
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate struct grouplist {  /* One element of the list generated by a setnetgrent() */
757c478bd9Sstevel@tonic-gate 	char			*triple[NSS_NETGR_N];
767c478bd9Sstevel@tonic-gate 	struct	grouplist	*gl_nxt;
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static nss_status_t
getnetgr_set(be,a)807c478bd9Sstevel@tonic-gate getnetgr_set(be, a)
817c478bd9Sstevel@tonic-gate 	struct nis_getnetgr_be	*be;
827c478bd9Sstevel@tonic-gate 	void			*a;
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	const char		*netgroup = (const char *) a;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (be->netgroup != 0 &&
877c478bd9Sstevel@tonic-gate 	    strcmp(be->netgroup, netgroup) == 0) {
887c478bd9Sstevel@tonic-gate 		/* We already have the member-list;  regurgitate it */
897c478bd9Sstevel@tonic-gate 		be->next_member = be->all_members;
907c478bd9Sstevel@tonic-gate 		return (NSS_SUCCESS);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	return (NSS_NOTFOUND);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static nss_status_t
getnetgr_get(be,a)967c478bd9Sstevel@tonic-gate getnetgr_get(be, a)
977c478bd9Sstevel@tonic-gate 	struct nis_getnetgr_be	*be;
987c478bd9Sstevel@tonic-gate 	void			*a;
997c478bd9Sstevel@tonic-gate {
100cb5caa98Sdjl 	struct nss_getnetgrent_args *args = (struct nss_getnetgrent_args *)a;
1017c478bd9Sstevel@tonic-gate 	struct grouplist	*mem;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if ((mem = be->next_member) == 0) {
1047c478bd9Sstevel@tonic-gate 		args->status = NSS_NETGR_NO;
1057c478bd9Sstevel@tonic-gate 	} else {
1067c478bd9Sstevel@tonic-gate 		char			*buffer	= args->buffer;
1077c478bd9Sstevel@tonic-gate 		int			buflen	= args->buflen;
1087c478bd9Sstevel@tonic-gate 		enum nss_netgr_argn	i;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		args->status = NSS_NETGR_FOUND;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		for (i = 0;  i < NSS_NETGR_N;  i++) {
1137c478bd9Sstevel@tonic-gate 			const char	*str;
1147c478bd9Sstevel@tonic-gate 			ssize_t	len;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 			if ((str = mem->triple[i]) == 0) {
1177c478bd9Sstevel@tonic-gate 				args->retp[i] = 0;
1187c478bd9Sstevel@tonic-gate 			} else if ((len = strlen(str) + 1) <= buflen) {
1197c478bd9Sstevel@tonic-gate 				args->retp[i] = buffer;
120cb5caa98Sdjl 				(void) memcpy(buffer, str, len);
1217c478bd9Sstevel@tonic-gate 				buffer += len;
1227c478bd9Sstevel@tonic-gate 				buflen -= len;
1237c478bd9Sstevel@tonic-gate 			} else {
1247c478bd9Sstevel@tonic-gate 				args->status = NSS_NETGR_NOMEM;
1257c478bd9Sstevel@tonic-gate 				break;
1267c478bd9Sstevel@tonic-gate 			}
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 		be->next_member	= mem->gl_nxt;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	return (NSS_SUCCESS);	/* Yup, even for end-of-list, i.e. */
1317c478bd9Sstevel@tonic-gate 				/* do NOT advance to next backend. */
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1357c478bd9Sstevel@tonic-gate static nss_status_t
getnetgr_end(be,dummy)1367c478bd9Sstevel@tonic-gate getnetgr_end(be, dummy)
1377c478bd9Sstevel@tonic-gate 	struct nis_getnetgr_be	*be;
1387c478bd9Sstevel@tonic-gate 	void			*dummy;
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	struct grouplist	*gl;
1417c478bd9Sstevel@tonic-gate 	struct grouplist	*next;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	for (gl = be->all_members; gl != NULL; gl = next) {
1447c478bd9Sstevel@tonic-gate 		enum nss_netgr_argn	i;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		next = gl->gl_nxt;
1477c478bd9Sstevel@tonic-gate 		for (i = NSS_NETGR_MACHINE;  i < NSS_NETGR_N;  i++) {
1487c478bd9Sstevel@tonic-gate 			if (gl->triple[i] != 0) {
1497c478bd9Sstevel@tonic-gate 				free(gl->triple[i]);
1507c478bd9Sstevel@tonic-gate 			}
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 		free(gl);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	be->all_members = 0;
1557c478bd9Sstevel@tonic-gate 	be->next_member = 0;
1567c478bd9Sstevel@tonic-gate 	if (be->netgroup != 0) {
1577c478bd9Sstevel@tonic-gate 		free(be->netgroup);
1587c478bd9Sstevel@tonic-gate 		be->netgroup = 0;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	return (NSS_SUCCESS);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1647c478bd9Sstevel@tonic-gate static nss_status_t
getnetgr_destr(be,dummy)1657c478bd9Sstevel@tonic-gate getnetgr_destr(be, dummy)
1667c478bd9Sstevel@tonic-gate 	struct nis_getnetgr_be	*be;
1677c478bd9Sstevel@tonic-gate 	void			*dummy;
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	if (be != 0) {
170cb5caa98Sdjl 		(void) getnetgr_end(be, (void *)0);
1717c478bd9Sstevel@tonic-gate 		free(be);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	return (NSS_SUCCESS);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static nis_getnetgr_op_t getnetgr_ops[] = {
1777c478bd9Sstevel@tonic-gate 	getnetgr_destr,
1787c478bd9Sstevel@tonic-gate 	getnetgr_end,
1797c478bd9Sstevel@tonic-gate 	getnetgr_set,
1807c478bd9Sstevel@tonic-gate 	getnetgr_get,	/* getnetgrent_r() */
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * The nss_backend_t for innetgr() and setnetgrent().
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate struct nis_netgr_be;
1897c478bd9Sstevel@tonic-gate typedef nss_status_t	(*nis_netgr_op_t)(struct nis_netgr_be *, void *);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate struct nis_netgr_be {
1927c478bd9Sstevel@tonic-gate 	nis_netgr_op_t		*ops;
1937c478bd9Sstevel@tonic-gate 	nss_dbop_t		n_ops;
1947c478bd9Sstevel@tonic-gate 	const char		*domain;	/* (default) YP domain */
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * Code to do top-down search in the graph defined by the 'netgroup' YP map
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * ===> This code is now used for setnetgrent(), not just innetgr().
2047c478bd9Sstevel@tonic-gate  *
2057c478bd9Sstevel@tonic-gate  * If the easy way doesn't pan out, recursively search the 'netgroup' map.
2067c478bd9Sstevel@tonic-gate  * In order to do this, we:
2077c478bd9Sstevel@tonic-gate  *
2087c478bd9Sstevel@tonic-gate  *    -	remember all the netgroup names we've seen during this search,
2097c478bd9Sstevel@tonic-gate  *	whether or not we've expanded them yet (we want fast insertion
2107c478bd9Sstevel@tonic-gate  *	with duplicate-detection, so use yet another chained hash table),
2117c478bd9Sstevel@tonic-gate  *
2127c478bd9Sstevel@tonic-gate  *    -	keep a list of all the netgroups we haven't expanded yet (we just
2137c478bd9Sstevel@tonic-gate  *	want fast insertion and pop-first, so a linked list will do fine).
2147c478bd9Sstevel@tonic-gate  *	If we insert at the head, we get a depth-first search;  insertion
2157c478bd9Sstevel@tonic-gate  *	at the tail gives breadth-first (?), which seems preferable (?).
2167c478bd9Sstevel@tonic-gate  *
2177c478bd9Sstevel@tonic-gate  * A netgrnam struct contains pointers for both the hash-table and the list.
2187c478bd9Sstevel@tonic-gate  * It also contains the netgroup name;  note that we embed the name at the
2197c478bd9Sstevel@tonic-gate  * end of the structure rather than holding a pointer to yet another
2207c478bd9Sstevel@tonic-gate  * malloc()ed region.
2217c478bd9Sstevel@tonic-gate  *
2227c478bd9Sstevel@tonic-gate  * A netgrtab structure contains the hash-chain heads and the head/tail
2237c478bd9Sstevel@tonic-gate  * pointers for the expansion list.
2247c478bd9Sstevel@tonic-gate  *
225*36e852a1SRaja Andra  * Most of this code is common to at least the NIS backend;  it
2267c478bd9Sstevel@tonic-gate  * should be generalized and, presumably, moved into the frontend.
2277c478bd9Sstevel@tonic-gate  * ==> Not any longer...
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate struct netgrnam {
2317c478bd9Sstevel@tonic-gate 	struct netgrnam	*hash_chain;
2327c478bd9Sstevel@tonic-gate 	struct netgrnam	*expand_next;
2337c478bd9Sstevel@tonic-gate 	char		name[1];	/* Really [strlen(name) + 1] */
2347c478bd9Sstevel@tonic-gate };
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate #define	HASHMOD	113
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate struct netgrtab {
2397c478bd9Sstevel@tonic-gate 	struct netgrnam	*expand_first;
2407c478bd9Sstevel@tonic-gate 	struct netgrnam	**expand_lastp;
2417c478bd9Sstevel@tonic-gate 	struct netgrnam	*hash_heads[HASHMOD];
2427c478bd9Sstevel@tonic-gate };
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static void
ngt_init(ngt)2457c478bd9Sstevel@tonic-gate ngt_init(ngt)
2467c478bd9Sstevel@tonic-gate 	struct netgrtab	*ngt;
2477c478bd9Sstevel@tonic-gate {
248cb5caa98Sdjl 	(void) memset((void *)ngt, 0, sizeof (*ngt));
2497c478bd9Sstevel@tonic-gate 	ngt->expand_lastp = &ngt->expand_first;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /* === ? Change ngt_init() and ngt_destroy() to malloc/free struct netgrtab */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static void
2557c478bd9Sstevel@tonic-gate /* ==> ? Should return 'failed' (out-of-memory) status ? */
ngt_insert(ngt,name,namelen)2567c478bd9Sstevel@tonic-gate ngt_insert(ngt, name, namelen)
2577c478bd9Sstevel@tonic-gate 	struct netgrtab	*ngt;
2587c478bd9Sstevel@tonic-gate 	const char	*name;
2597c478bd9Sstevel@tonic-gate 	size_t		namelen;
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	unsigned	hashval;
2627c478bd9Sstevel@tonic-gate 	size_t		i;
2637c478bd9Sstevel@tonic-gate 	struct netgrnam	*cur;
2647c478bd9Sstevel@tonic-gate 	struct netgrnam	**head;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate #define	dummy		((struct netgrnam *)0)
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	for (hashval = 0, i = 0;  i < namelen;  i++) {
2697c478bd9Sstevel@tonic-gate 		hashval = (hashval << 2) + hashval +
2707c478bd9Sstevel@tonic-gate 			((const unsigned char *)name)[i];
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	head = &ngt->hash_heads[hashval % HASHMOD];
2737c478bd9Sstevel@tonic-gate 	for (cur = *head;  cur != 0;  cur = cur->hash_chain) {
2747c478bd9Sstevel@tonic-gate 		if (strncmp(cur->name, name, namelen) == 0 &&
2757c478bd9Sstevel@tonic-gate 		    cur->name[namelen] == 0) {
2767c478bd9Sstevel@tonic-gate 			return;		/* Already in table, do nothing */
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 	/* Create new netgrnam struct */
2807c478bd9Sstevel@tonic-gate 	cur = (struct netgrnam *)
2817c478bd9Sstevel@tonic-gate 		malloc(namelen + 1 + (char *)&dummy->name[0] - (char *)dummy);
2827c478bd9Sstevel@tonic-gate 	if (cur == 0) {
2837c478bd9Sstevel@tonic-gate 		return;			/* Out of memory, too bad */
2847c478bd9Sstevel@tonic-gate 	}
285cb5caa98Sdjl 	(void) memcpy(cur->name, name, namelen);
2867c478bd9Sstevel@tonic-gate 	cur->name[namelen] = 0;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/* Insert in hash table */
2897c478bd9Sstevel@tonic-gate 	cur->hash_chain = *head;
2907c478bd9Sstevel@tonic-gate 	*head = cur;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/* Insert in expansion list (insert at end for breadth-first search */
2937c478bd9Sstevel@tonic-gate 	cur->expand_next = 0;
2947c478bd9Sstevel@tonic-gate 	*ngt->expand_lastp = cur;
2957c478bd9Sstevel@tonic-gate 	ngt->expand_lastp = &cur->expand_next;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate #undef	dummy
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate static const char *
ngt_next(ngt)3017c478bd9Sstevel@tonic-gate ngt_next(ngt)
3027c478bd9Sstevel@tonic-gate 	struct netgrtab	*ngt;
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct netgrnam	*first;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if ((first = ngt->expand_first) == 0) {
3077c478bd9Sstevel@tonic-gate 		return (0);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	if ((ngt->expand_first = first->expand_next) == 0) {
3107c478bd9Sstevel@tonic-gate 		ngt->expand_lastp = &ngt->expand_first;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	return (first->name);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate static void
ngt_destroy(ngt)3167c478bd9Sstevel@tonic-gate ngt_destroy(ngt)
3177c478bd9Sstevel@tonic-gate 	struct netgrtab	*ngt;
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	struct netgrnam	*cur;
3207c478bd9Sstevel@tonic-gate 	struct netgrnam *next;
3217c478bd9Sstevel@tonic-gate 	int		i;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	for (i = 0;  i < HASHMOD;  i++) {
3247c478bd9Sstevel@tonic-gate 		for (cur = ngt->hash_heads[i];  cur != 0; /* cstyle */) {
3257c478bd9Sstevel@tonic-gate 			next = cur->hash_chain;
3267c478bd9Sstevel@tonic-gate 			free(cur);
3277c478bd9Sstevel@tonic-gate 			cur = next;
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	/* Don't bother zeroing pointers;  must do init if we want to reuse */
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate typedef const char *ccp;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@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)3367c478bd9Sstevel@tonic-gate top_down(struct nis_netgr_be *be, const char **groups, int ngroups,
3377c478bd9Sstevel@tonic-gate     int (*func)(ccp triple[3], void *iter_args, nss_status_t *return_val),
3387c478bd9Sstevel@tonic-gate     void *iter_args)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	struct netgrtab		*ngt;
3417c478bd9Sstevel@tonic-gate 	/* netgrtab goes on the heap, not the stack, because it's large and */
3427c478bd9Sstevel@tonic-gate 	/* stacks may not be all that big in multi-threaded programs. */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	const char		*group;
3457c478bd9Sstevel@tonic-gate 	int			nfound;
3467c478bd9Sstevel@tonic-gate 	int			done;
3477c478bd9Sstevel@tonic-gate 	nss_status_t		result;
3487c478bd9Sstevel@tonic-gate 
349cb5caa98Sdjl 	if ((ngt = (struct netgrtab *)malloc(sizeof (*ngt))) == 0) {
3507c478bd9Sstevel@tonic-gate 		return (NSS_UNAVAIL);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	ngt_init(ngt);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	while (ngroups > 0) {
3557c478bd9Sstevel@tonic-gate 		ngt_insert(ngt, *groups, strlen(*groups));
3567c478bd9Sstevel@tonic-gate 		groups++;
3577c478bd9Sstevel@tonic-gate 		ngroups--;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	done	= 0;	/* Set to 1 to indicate that we cut the iteration  */
3617c478bd9Sstevel@tonic-gate 			/*   short (and 'result' holds the return value)   */
3627c478bd9Sstevel@tonic-gate 	nfound	= 0;	/* Number of successful netgroup yp_match calls	   */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	while (!done && (group = ngt_next(ngt)) != 0) {
3657c478bd9Sstevel@tonic-gate 		char		*val;
3667c478bd9Sstevel@tonic-gate 		int		vallen;
3677c478bd9Sstevel@tonic-gate 		char		*p;
3687c478bd9Sstevel@tonic-gate 		int		yperr;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		result = _nss_nis_ypmatch(be->domain, "netgroup", group,
371*36e852a1SRaja Andra 		    &val, &vallen, &yperr);
3727c478bd9Sstevel@tonic-gate 		if (result != NSS_SUCCESS) {
373cb5caa98Sdjl 			/*LINTED E_NOP_IF_STMT*/
3747c478bd9Sstevel@tonic-gate 			if (result == NSS_NOTFOUND) {
375cb5caa98Sdjl 				;
3767c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3777c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING,
3787c478bd9Sstevel@tonic-gate 				    "NIS netgroup lookup: %s doesn't exist",
3797c478bd9Sstevel@tonic-gate 				    group);
3807c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
3817c478bd9Sstevel@tonic-gate 			} else {
3827c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3837c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING,
3847c478bd9Sstevel@tonic-gate 			"NIS netgroup lookup: yp_match returned [%s]",
3857c478bd9Sstevel@tonic-gate 				    yperr_string(yperr));
3867c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
3877c478bd9Sstevel@tonic-gate 				done = 1;	/* Give up, return result */
3887c478bd9Sstevel@tonic-gate 			}
3897c478bd9Sstevel@tonic-gate 			/* Don't need to clean up anything */
3907c478bd9Sstevel@tonic-gate 			continue;
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		nfound++;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		if ((p = strpbrk(val, "#\n")) != 0) {
3967c478bd9Sstevel@tonic-gate 			*p = '\0';
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		p = val;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		/* Parse val into triples and recursive netgroup references */
4017c478bd9Sstevel@tonic-gate 		/*CONSTCOND*/
4027c478bd9Sstevel@tonic-gate 		while (1) {
4037c478bd9Sstevel@tonic-gate 			ccp			triple[NSS_NETGR_N];
4047c478bd9Sstevel@tonic-gate 			int			syntax_err;
4057c478bd9Sstevel@tonic-gate 			enum nss_netgr_argn	i;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 			while (isspace(*p)) {
4087c478bd9Sstevel@tonic-gate 				p++;
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 			if (*p == '\0') {
4117c478bd9Sstevel@tonic-gate 				/* Finished processing this particular val */
4127c478bd9Sstevel@tonic-gate 				break;
4137c478bd9Sstevel@tonic-gate 			}
4147c478bd9Sstevel@tonic-gate 			if (*p != '(') {
4157c478bd9Sstevel@tonic-gate 				/* Doesn't look like the start of a triple, */
4167c478bd9Sstevel@tonic-gate 				/*   so assume it's a recursive netgroup.   */
4177c478bd9Sstevel@tonic-gate 				char *start = p;
4187c478bd9Sstevel@tonic-gate 				p = strpbrk(start, " \t");
4197c478bd9Sstevel@tonic-gate 				if (p == 0) {
4207c478bd9Sstevel@tonic-gate 					/* Point p at the final '\0' */
4217c478bd9Sstevel@tonic-gate 					p = start + strlen(start);
4227c478bd9Sstevel@tonic-gate 				}
4237c478bd9Sstevel@tonic-gate 				ngt_insert(ngt, start, (size_t)(p - start));
4247c478bd9Sstevel@tonic-gate 				continue;
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 			/* Main case:  a (machine, user, domain) triple */
4287c478bd9Sstevel@tonic-gate 			p++;
4297c478bd9Sstevel@tonic-gate 			syntax_err = 0;
4307c478bd9Sstevel@tonic-gate 			for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
4317c478bd9Sstevel@tonic-gate 				char		*start;
4327c478bd9Sstevel@tonic-gate 				char		*limit;
4337c478bd9Sstevel@tonic-gate 				const char	*terminators = ",) \t";
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 				if (i == NSS_NETGR_DOMAIN) {
4367c478bd9Sstevel@tonic-gate 					/* Don't allow comma */
4377c478bd9Sstevel@tonic-gate 					terminators++;
4387c478bd9Sstevel@tonic-gate 				}
4397c478bd9Sstevel@tonic-gate 				while (isspace(*p)) {
4407c478bd9Sstevel@tonic-gate 					p++;
4417c478bd9Sstevel@tonic-gate 				}
4427c478bd9Sstevel@tonic-gate 				start = p;
4437c478bd9Sstevel@tonic-gate 				limit = strpbrk(start, terminators);
4447c478bd9Sstevel@tonic-gate 				if (limit == 0) {
4457c478bd9Sstevel@tonic-gate 					syntax_err++;
4467c478bd9Sstevel@tonic-gate 					break;
4477c478bd9Sstevel@tonic-gate 				}
4487c478bd9Sstevel@tonic-gate 				p = limit;
4497c478bd9Sstevel@tonic-gate 				while (isspace(*p)) {
4507c478bd9Sstevel@tonic-gate 					p++;
4517c478bd9Sstevel@tonic-gate 				}
4527c478bd9Sstevel@tonic-gate 				if (*p == terminators[0]) {
4537c478bd9Sstevel@tonic-gate 					/*
4547c478bd9Sstevel@tonic-gate 					 * Successfully parsed this name and
4557c478bd9Sstevel@tonic-gate 					 *   the separator after it (comma or
4567c478bd9Sstevel@tonic-gate 					 *   right paren); leave p ready for
4577c478bd9Sstevel@tonic-gate 					 *   next parse.
4587c478bd9Sstevel@tonic-gate 					 */
4597c478bd9Sstevel@tonic-gate 					p++;
4607c478bd9Sstevel@tonic-gate 					if (start == limit) {
4617c478bd9Sstevel@tonic-gate 						/* Wildcard */
4627c478bd9Sstevel@tonic-gate 						triple[i] = 0;
4637c478bd9Sstevel@tonic-gate 					} else {
4647c478bd9Sstevel@tonic-gate 						*limit = '\0';
4657c478bd9Sstevel@tonic-gate 						triple[i] = start;
4667c478bd9Sstevel@tonic-gate 					}
4677c478bd9Sstevel@tonic-gate 				} else {
4687c478bd9Sstevel@tonic-gate 					syntax_err++;
4697c478bd9Sstevel@tonic-gate 					break;
4707c478bd9Sstevel@tonic-gate 				}
4717c478bd9Sstevel@tonic-gate 			}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 			if (syntax_err) {
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate  * ===> log it;
4767c478bd9Sstevel@tonic-gate  * ===> try skipping past next ')';  failing that, abandon the line;
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate 				break;	/* Abandon this line */
4797c478bd9Sstevel@tonic-gate 			} else if (!(*func)(triple, iter_args, &result)) {
4807c478bd9Sstevel@tonic-gate 				/* Return result, good or bad */
4817c478bd9Sstevel@tonic-gate 				done = 1;
4827c478bd9Sstevel@tonic-gate 				break;
4837c478bd9Sstevel@tonic-gate 			}
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 		/* End of inner loop over val[] */
4867c478bd9Sstevel@tonic-gate 		free(val);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	/* End of outer loop (!done && ngt_next(ngt) != 0) */
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	ngt_destroy(ngt);
4917c478bd9Sstevel@tonic-gate 	free(ngt);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (done) {
4947c478bd9Sstevel@tonic-gate 		return (result);
4957c478bd9Sstevel@tonic-gate 	} else if (nfound > 0) {
4967c478bd9Sstevel@tonic-gate 		/* ==== ? Should only do this if all the top-level groups */
4977c478bd9Sstevel@tonic-gate 		/*	  exist in YP?					  */
4987c478bd9Sstevel@tonic-gate 		return (NSS_SUCCESS);
4997c478bd9Sstevel@tonic-gate 	} else {
5007c478bd9Sstevel@tonic-gate 		return (NSS_NOTFOUND);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate  * Code for setnetgrent()
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * Iterator function for setnetgrent():  copy triple, add to be->all_members
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate static int
save_triple(ccp trippp[NSS_NETGR_N],void * headp_arg,nss_status_t * return_val)5137c478bd9Sstevel@tonic-gate save_triple(ccp trippp[NSS_NETGR_N], void *headp_arg,
5147c478bd9Sstevel@tonic-gate     nss_status_t *return_val)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	struct grouplist	**headp = headp_arg;
5177c478bd9Sstevel@tonic-gate 	struct grouplist	*gl;
5187c478bd9Sstevel@tonic-gate 	enum nss_netgr_argn	i;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if ((gl = (struct grouplist *)malloc(sizeof (*gl))) == 0) {
5217c478bd9Sstevel@tonic-gate 		/* Out of memory */
5227c478bd9Sstevel@tonic-gate 		*return_val = NSS_UNAVAIL;
5237c478bd9Sstevel@tonic-gate 		return (0);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 	for (i = NSS_NETGR_MACHINE;  i < NSS_NETGR_N;  i++) {
5267c478bd9Sstevel@tonic-gate 		if (trippp[i] == 0) {
5277c478bd9Sstevel@tonic-gate 			/* Wildcard */
5287c478bd9Sstevel@tonic-gate 			gl->triple[i] = 0;
5297c478bd9Sstevel@tonic-gate 		} else if ((gl->triple[i] = strdup(trippp[i])) == 0) {
5307c478bd9Sstevel@tonic-gate 			/* Out of memory.  Free any we've allocated */
5317c478bd9Sstevel@tonic-gate 			enum nss_netgr_argn	j;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 			for (j = NSS_NETGR_MACHINE;  j < i;  j++) {
5347c478bd9Sstevel@tonic-gate 				if (gl->triple[j] != 0) {
5357c478bd9Sstevel@tonic-gate 					free(gl->triple[j]);
5367c478bd9Sstevel@tonic-gate 				}
5377c478bd9Sstevel@tonic-gate 			}
5387c478bd9Sstevel@tonic-gate 			*return_val = NSS_UNAVAIL;
5397c478bd9Sstevel@tonic-gate 			return (0);
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	gl->gl_nxt = *headp;
5437c478bd9Sstevel@tonic-gate 	*headp = gl;
5447c478bd9Sstevel@tonic-gate 	return (1);	/* Tell top_down() to keep iterating */
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate static nss_status_t
netgr_set(be,a)5487c478bd9Sstevel@tonic-gate netgr_set(be, a)
5497c478bd9Sstevel@tonic-gate 	struct nis_netgr_be	*be;
5507c478bd9Sstevel@tonic-gate 	void			*a;
5517c478bd9Sstevel@tonic-gate {
552cb5caa98Sdjl 	struct nss_setnetgrent_args *args = (struct nss_setnetgrent_args *)a;
5537c478bd9Sstevel@tonic-gate 	struct nis_getnetgr_be	*get_be;
5547c478bd9Sstevel@tonic-gate 	nss_status_t		res;
5557c478bd9Sstevel@tonic-gate 
556cb5caa98Sdjl 	get_be = (struct nis_getnetgr_be *)malloc(sizeof (*get_be));
5577c478bd9Sstevel@tonic-gate 	if (get_be == 0) {
5587c478bd9Sstevel@tonic-gate 		return (NSS_UNAVAIL);
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	get_be->all_members = 0;
5627c478bd9Sstevel@tonic-gate 	res = top_down(be, &args->netgroup, 1, save_triple,
5637c478bd9Sstevel@tonic-gate 		&get_be->all_members);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (res == NSS_SUCCESS) {
5667c478bd9Sstevel@tonic-gate 		get_be->ops		= getnetgr_ops;
5677c478bd9Sstevel@tonic-gate 		get_be->n_ops		= sizeof (getnetgr_ops) /
5687c478bd9Sstevel@tonic-gate 						sizeof (getnetgr_ops[0]);
5697c478bd9Sstevel@tonic-gate 		get_be->netgroup	= strdup(args->netgroup);
5707c478bd9Sstevel@tonic-gate 		get_be->next_member	= get_be->all_members;
5717c478bd9Sstevel@tonic-gate 
572cb5caa98Sdjl 		args->iterator		= (nss_backend_t *)get_be;
5737c478bd9Sstevel@tonic-gate 	} else {
5747c478bd9Sstevel@tonic-gate 		args->iterator		= 0;
5757c478bd9Sstevel@tonic-gate 		free(get_be);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 	return (res);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * Code for innetgr()
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * Iterator function for innetgr():  Check whether triple matches args
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate static int
match_triple(ccp triple[NSS_NETGR_N],void * ia_arg,nss_status_t * return_val)5897c478bd9Sstevel@tonic-gate match_triple(ccp triple[NSS_NETGR_N], void *ia_arg, nss_status_t *return_val)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	struct nss_innetgr_args	*ia = ia_arg;
5927c478bd9Sstevel@tonic-gate 	enum nss_netgr_argn	i;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	for (i = NSS_NETGR_MACHINE;  i < NSS_NETGR_N;  i++) {
5957c478bd9Sstevel@tonic-gate 		int		(*cmpf)(const char *, const char *);
5967c478bd9Sstevel@tonic-gate 		char		**argv;
5977c478bd9Sstevel@tonic-gate 		int		n;
5987c478bd9Sstevel@tonic-gate 		const char	*name = triple[i];
5997c478bd9Sstevel@tonic-gate 		int		argc = ia->arg[i].argc;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		if (argc == 0 || name == 0) {
6027c478bd9Sstevel@tonic-gate 			/* Wildcarded on one side or t'other */
6037c478bd9Sstevel@tonic-gate 			continue;
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		argv = ia->arg[i].argv;
6067c478bd9Sstevel@tonic-gate 		cmpf = (i == NSS_NETGR_MACHINE) ? strcasecmp : strcmp;
6077c478bd9Sstevel@tonic-gate 		for (n = 0;  n < argc;  n++) {
6087c478bd9Sstevel@tonic-gate 			if ((*cmpf)(argv[n], name) == 0) {
6097c478bd9Sstevel@tonic-gate 				break;
6107c478bd9Sstevel@tonic-gate 			}
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		if (n >= argc) {
6137c478bd9Sstevel@tonic-gate 			/* Match failed, tell top_down() to keep looking */
6147c478bd9Sstevel@tonic-gate 			return (1);
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 	/* Matched on all three, so quit looking and declare victory */
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	ia->status = NSS_NETGR_FOUND;
6207c478bd9Sstevel@tonic-gate 	*return_val = NSS_SUCCESS;
6217c478bd9Sstevel@tonic-gate 	return (0);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * inlist() -- return 1 if at least one item from the "what" list
6267c478bd9Sstevel@tonic-gate  *   is in the comma-separated, newline-terminated "list"
6277c478bd9Sstevel@tonic-gate  */
6287c478bd9Sstevel@tonic-gate static const char comma = ',';	/* Don't let 'cfix' near this */
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate static int
inlist(nwhat,pwhat,list)6317c478bd9Sstevel@tonic-gate inlist(nwhat, pwhat, list)
6327c478bd9Sstevel@tonic-gate 	nss_innetgr_argc	nwhat;
6337c478bd9Sstevel@tonic-gate 	nss_innetgr_argv	pwhat;
6347c478bd9Sstevel@tonic-gate 	char			*list;
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	char			*p;
6377c478bd9Sstevel@tonic-gate 	nss_innetgr_argc	nw;
6387c478bd9Sstevel@tonic-gate 	nss_innetgr_argv	pw;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	while (*list != 0) {
6417c478bd9Sstevel@tonic-gate 		while (*list == comma || isspace(*list))
6427c478bd9Sstevel@tonic-gate 			list++;
6437c478bd9Sstevel@tonic-gate 		for (p = list;  *p != 0 && *p != comma &&
6447c478bd9Sstevel@tonic-gate 		    !isspace(*p); /* nothing */)
6457c478bd9Sstevel@tonic-gate 			p++;
6467c478bd9Sstevel@tonic-gate 		if (p != list) {
6477c478bd9Sstevel@tonic-gate 			if (*p != 0)
6487c478bd9Sstevel@tonic-gate 				*p++ = 0;
6497c478bd9Sstevel@tonic-gate 			for (pw = pwhat, nw = nwhat;  nw != 0;  pw++, nw--) {
6507c478bd9Sstevel@tonic-gate 				if (strcmp(list, *pw) == 0)
6517c478bd9Sstevel@tonic-gate 					return (1);
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 			list = p;
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	return (0);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * Generate a key for a netgroup.byXXXX NIS map
6617c478bd9Sstevel@tonic-gate  */
6627c478bd9Sstevel@tonic-gate static void
makekey(key,name,domain)6637c478bd9Sstevel@tonic-gate makekey(key, name, domain)
6647c478bd9Sstevel@tonic-gate 	char		*key;
6657c478bd9Sstevel@tonic-gate 	const char	*name;
6667c478bd9Sstevel@tonic-gate 	const char	*domain;
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate 	while (*key++ = *name++)
6697c478bd9Sstevel@tonic-gate 		;
6707c478bd9Sstevel@tonic-gate 	*(key-1) = '.';
6717c478bd9Sstevel@tonic-gate 	while (*key++ = *domain++)
6727c478bd9Sstevel@tonic-gate 		;
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate static int
makekey_lc(key,name,domain)6767c478bd9Sstevel@tonic-gate makekey_lc(key, name, domain)
6777c478bd9Sstevel@tonic-gate 	char		*key;
6787c478bd9Sstevel@tonic-gate 	const char	*name;		/* Convert this to lowercase */
6797c478bd9Sstevel@tonic-gate 	const char	*domain;	/* But not this */
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	int		found_uc = 0;
6827c478bd9Sstevel@tonic-gate 	char		c;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	while (c = *name++) {
6857c478bd9Sstevel@tonic-gate 		if (isupper(c)) {
6867c478bd9Sstevel@tonic-gate 			++found_uc;
6877c478bd9Sstevel@tonic-gate 			c = tolower(c);
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 		*key++ = c;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	*key++ = '.';
6927c478bd9Sstevel@tonic-gate 	while (*key++ = *domain++)
6937c478bd9Sstevel@tonic-gate 		;
6947c478bd9Sstevel@tonic-gate 	return (found_uc);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate /*
6987c478bd9Sstevel@tonic-gate  * easy_way() --  try to use netgroup.byuser and netgroup.byhost maps to
6997c478bd9Sstevel@tonic-gate  *		  get answers more efficiently than by recursive search.
7007c478bd9Sstevel@tonic-gate  *
7017c478bd9Sstevel@tonic-gate  * If more than one name (username or hostname) is specified, this approach
7027c478bd9Sstevel@tonic-gate  * becomes less attractive;  at some point it's probably cheaper to do the
7037c478bd9Sstevel@tonic-gate  * recursive search.  We don't know what the threshold is (among other things
7047c478bd9Sstevel@tonic-gate  * it may depend on the site-specific struucture of netgroup information),
7057c478bd9Sstevel@tonic-gate  * so here's a guesstimate.
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate #define	NNAME_THRESHOLD	5
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate static int
easy_way(be,ia,argp,map,try_lc,statusp)7117c478bd9Sstevel@tonic-gate easy_way(be, ia, argp, map, try_lc, statusp)
7127c478bd9Sstevel@tonic-gate 	struct nis_netgr_be	*be;
7137c478bd9Sstevel@tonic-gate 	struct nss_innetgr_args	*ia;
7147c478bd9Sstevel@tonic-gate 	struct nss_innetgr_1arg	*argp;
7157c478bd9Sstevel@tonic-gate 	const char		*map;
7167c478bd9Sstevel@tonic-gate 	int			try_lc;
7177c478bd9Sstevel@tonic-gate 	nss_status_t		*statusp;
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	nss_innetgr_argc	nname = argp->argc;
7207c478bd9Sstevel@tonic-gate 	nss_innetgr_argv	pname = argp->argv;
7217c478bd9Sstevel@tonic-gate 	const char		*domain = ia->arg[NSS_NETGR_DOMAIN].argv[0];
7227c478bd9Sstevel@tonic-gate 	const char		*wild = "*";
7237c478bd9Sstevel@tonic-gate 	int			yperr;
7247c478bd9Sstevel@tonic-gate 	char			*val;
7257c478bd9Sstevel@tonic-gate 	int			vallen;
7267c478bd9Sstevel@tonic-gate 	char			*key;
7277c478bd9Sstevel@tonic-gate 	int			i;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	/* Our caller guaranteed that nname >= 1 */
7307c478bd9Sstevel@tonic-gate 	while (nname > 1) {
7317c478bd9Sstevel@tonic-gate 		struct nss_innetgr_1arg	just_one;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		if (nname > NNAME_THRESHOLD) {
7347c478bd9Sstevel@tonic-gate 			return (0);	/* May be cheaper to use 'netgroup' */
7357c478bd9Sstevel@tonic-gate 		}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		just_one.argc = 1;
7387c478bd9Sstevel@tonic-gate 		just_one.argv = pname;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		if (easy_way(be, ia, &just_one, map, try_lc, statusp) &&
7417c478bd9Sstevel@tonic-gate 		    ia->status == NSS_NETGR_FOUND) {
7427c478bd9Sstevel@tonic-gate 			return (1);
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 		++pname;
7457c478bd9Sstevel@tonic-gate 		--nname;
7467c478bd9Sstevel@tonic-gate 		/* Fall through and do the last one inline */
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if ((key = malloc(strlen(*pname) + strlen(domain) + 2)) == 0) {
7507c478bd9Sstevel@tonic-gate 		return (0);	/* Or maybe (1) and NSS_UNAVAIL */
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	for (i = 0;  i < (try_lc ? 6 : 4);  i++) {
7547c478bd9Sstevel@tonic-gate 		switch (i) {
7557c478bd9Sstevel@tonic-gate 		    case 0:
7567c478bd9Sstevel@tonic-gate 			makekey(key, *pname, domain);
7577c478bd9Sstevel@tonic-gate 			break;
7587c478bd9Sstevel@tonic-gate 		    case 1:
7597c478bd9Sstevel@tonic-gate 			makekey(key, wild, domain);
7607c478bd9Sstevel@tonic-gate 			break;
7617c478bd9Sstevel@tonic-gate 		    case 2:
7627c478bd9Sstevel@tonic-gate 			makekey(key, *pname, wild);
7637c478bd9Sstevel@tonic-gate 			break;
7647c478bd9Sstevel@tonic-gate 		    case 3:
7657c478bd9Sstevel@tonic-gate 			makekey(key, wild, wild);
7667c478bd9Sstevel@tonic-gate 			break;
7677c478bd9Sstevel@tonic-gate 		    case 4:
7687c478bd9Sstevel@tonic-gate 			if (!makekey_lc(key, *pname, domain)) {
7697c478bd9Sstevel@tonic-gate 				try_lc = 0;	/* Sleazy but effective */
7707c478bd9Sstevel@tonic-gate 				continue;	/*   i.e. quit looping  */
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 			break;
7737c478bd9Sstevel@tonic-gate 		    case 5:
7747c478bd9Sstevel@tonic-gate 			(void) makekey_lc(key, *pname, wild);
7757c478bd9Sstevel@tonic-gate 			break;
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 		*statusp = _nss_nis_ypmatch(be->domain, map, key,
7787c478bd9Sstevel@tonic-gate 					&val, &vallen, &yperr);
7797c478bd9Sstevel@tonic-gate 		if (*statusp == NSS_SUCCESS) {
7807c478bd9Sstevel@tonic-gate 			if (inlist(ia->groups.argc, ia->groups.argv, val)) {
7817c478bd9Sstevel@tonic-gate 				free(val);
7827c478bd9Sstevel@tonic-gate 				free(key);
7837c478bd9Sstevel@tonic-gate 				ia->status = NSS_NETGR_FOUND;
7847c478bd9Sstevel@tonic-gate 				return (1);
7857c478bd9Sstevel@tonic-gate 			} else {
7867c478bd9Sstevel@tonic-gate 				free(val);
7877c478bd9Sstevel@tonic-gate 			}
7887c478bd9Sstevel@tonic-gate 		} else {
7897c478bd9Sstevel@tonic-gate #ifdef DEBUG
7907c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
7917c478bd9Sstevel@tonic-gate 				"innetgr: yp_match(%s,%s) failed: %s",
7927c478bd9Sstevel@tonic-gate 				map, key, yperr_string(yperr));
7937c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
7947c478bd9Sstevel@tonic-gate 			if (yperr != YPERR_KEY)  {
7957c478bd9Sstevel@tonic-gate 				free(key);
7967c478bd9Sstevel@tonic-gate 				return (0);
7977c478bd9Sstevel@tonic-gate 			}
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	free(key);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /* =====> is this (an authoritative "no") always the right thing to do?	*/
8047c478bd9Sstevel@tonic-gate /*	  Answer:  yes, except for hostnames that aren't all lowercase	*/
8057c478bd9Sstevel@tonic-gate 
806767b0abfSmichen 	*statusp = NSS_NOTFOUND;	/* Yup, three different flavours of */
8077c478bd9Sstevel@tonic-gate 	ia->status = NSS_NETGR_NO;	/*   status information, so-called. */
8087c478bd9Sstevel@tonic-gate 	return (1);			/*   Silly, innit?		    */
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate static nss_status_t
netgr_in(be,a)8137c478bd9Sstevel@tonic-gate netgr_in(be, a)
8147c478bd9Sstevel@tonic-gate 	struct nis_netgr_be	*be;
8157c478bd9Sstevel@tonic-gate 	void			*a;
8167c478bd9Sstevel@tonic-gate {
817cb5caa98Sdjl 	struct nss_innetgr_args	*ia = (struct nss_innetgr_args *)a;
8187c478bd9Sstevel@tonic-gate 	nss_status_t		res;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	ia->status = NSS_NETGR_NO;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	/* Can we use netgroup.byhost or netgroup.byuser to speed things up? */
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate /* ====> diddle this to try fast path for domains.argc == 0 too */
8257c478bd9Sstevel@tonic-gate 	if (ia->arg[NSS_NETGR_DOMAIN].argc == 1) {
8267c478bd9Sstevel@tonic-gate 		if (ia->arg[NSS_NETGR_MACHINE].argc == 0 &&
8277c478bd9Sstevel@tonic-gate 		    ia->arg[NSS_NETGR_USER   ].argc != 0) {
8287c478bd9Sstevel@tonic-gate 			if (easy_way(be, ia, &ia->arg[NSS_NETGR_USER],
8297c478bd9Sstevel@tonic-gate 			    "netgroup.byuser", 0, &res)) {
8307c478bd9Sstevel@tonic-gate 				return (res);
8317c478bd9Sstevel@tonic-gate 			}
8327c478bd9Sstevel@tonic-gate 		} else if (ia->arg[NSS_NETGR_USER].argc == 0 &&
8337c478bd9Sstevel@tonic-gate 		    ia->arg[NSS_NETGR_MACHINE].argc != 0) {
8347c478bd9Sstevel@tonic-gate 			if (easy_way(be, ia, &ia->arg[NSS_NETGR_MACHINE],
8357c478bd9Sstevel@tonic-gate 			    "netgroup.byhost", 1, &res)) {
8367c478bd9Sstevel@tonic-gate 				return (res);
8377c478bd9Sstevel@tonic-gate 			}
8387c478bd9Sstevel@tonic-gate 		}
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	/* Nope, try the slow way */
8427c478bd9Sstevel@tonic-gate 	ia->status = NSS_NETGR_NO;
8437c478bd9Sstevel@tonic-gate 	res = top_down(be, (const char **)ia->groups.argv, ia->groups.argc,
8447c478bd9Sstevel@tonic-gate 	    match_triple, ia);
8457c478bd9Sstevel@tonic-gate 	return (res);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate  * (Almost) boilerplate for a switch backend
8517c478bd9Sstevel@tonic-gate  */
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
854cb5caa98Sdjl static nss_status_t
netgr_destr(be,dummy)8557c478bd9Sstevel@tonic-gate netgr_destr(be, dummy)
8567c478bd9Sstevel@tonic-gate 	struct nis_netgr_be	*be;
8577c478bd9Sstevel@tonic-gate 	void			*dummy;
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	if (be != 0) {
8607c478bd9Sstevel@tonic-gate 		free(be);
8617c478bd9Sstevel@tonic-gate 	}
8627c478bd9Sstevel@tonic-gate 	return (NSS_SUCCESS);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate static nis_netgr_op_t netgroup_ops[] = {
8667c478bd9Sstevel@tonic-gate 	netgr_destr,
8677c478bd9Sstevel@tonic-gate 	0,		/* No endent, because no setent/getent */
8687c478bd9Sstevel@tonic-gate 	0,		/* No setent;  setnetgrent() is really a getXbyY() */
8697c478bd9Sstevel@tonic-gate 	0,		/* No getent in the normal sense */
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	netgr_in,	/* innetgr() */
8727c478bd9Sstevel@tonic-gate 	netgr_set,	/* setnetgrent() */
8737c478bd9Sstevel@tonic-gate };
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8767c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_nis_netgroup_constr(dummy1,dummy2,dummy3)8777c478bd9Sstevel@tonic-gate _nss_nis_netgroup_constr(dummy1, dummy2, dummy3)
8787c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	const char		*domain;
8817c478bd9Sstevel@tonic-gate 	struct nis_netgr_be	*be;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	if ((domain = _nss_nis_domain()) == 0 ||
884cb5caa98Sdjl 	    (be = (struct nis_netgr_be *)malloc(sizeof (*be))) == 0) {
8857c478bd9Sstevel@tonic-gate 		return (0);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 	be->ops		= netgroup_ops;
8887c478bd9Sstevel@tonic-gate 	be->n_ops	= sizeof (netgroup_ops) / sizeof (netgroup_ops[0]);
8897c478bd9Sstevel@tonic-gate 	be->domain	= domain;
8907c478bd9Sstevel@tonic-gate 
891cb5caa98Sdjl 	return ((nss_backend_t *)be);
8927c478bd9Sstevel@tonic-gate }
893