xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getnetgrent.c (revision 4953:0afa9b3bf89e)
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
52388Smj162486  * Common Development and Distribution License (the "License").
62388Smj162486  * 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*4953Smichen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <syslog.h>
290Sstevel@tonic-gate #include "ldap_common.h"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /* netgroup attributes filters */
320Sstevel@tonic-gate #define	_N_TRIPLE		"nisnetgrouptriple"
330Sstevel@tonic-gate #define	_N_MEMBER		"membernisnetgroup"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #define	PRINT_VAL(a)		(((a).argc == 0) || ((a).argv == NULL) || \
360Sstevel@tonic-gate 				    ((a).argv[0] == NULL)) ? "*" : (a).argv[0]
370Sstevel@tonic-gate #define	ISNULL(a)		(a == NULL ? "<NULL>" : a)
380Sstevel@tonic-gate #define	MAX_DOMAIN_LEN		1024
390Sstevel@tonic-gate #define	MAX_TRIPLE_LEN		(MAXHOSTNAMELEN + LOGNAME_MAX + \
400Sstevel@tonic-gate 					MAX_DOMAIN_LEN + 5)
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define	_F_SETMEMBER		"(&(objectClass=nisNetGroup)(cn=%s))"
430Sstevel@tonic-gate #define	_F_SETMEMBER_SSD	"(&(%%s)(cn=%s))"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	N_HASH		257
462388Smj162486 #define	COMMA		','
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static const char *netgrent_attrs[] = {
490Sstevel@tonic-gate 	_N_TRIPLE,
500Sstevel@tonic-gate 	_N_MEMBER,
510Sstevel@tonic-gate 	(char *)NULL
520Sstevel@tonic-gate };
530Sstevel@tonic-gate 
540Sstevel@tonic-gate typedef struct netgroup_name {
550Sstevel@tonic-gate 	char *name;
560Sstevel@tonic-gate 	struct netgroup_name *next;
570Sstevel@tonic-gate 	struct netgroup_name *next_hash;
580Sstevel@tonic-gate } netgroup_name_t;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate typedef struct {
610Sstevel@tonic-gate 	netgroup_name_t *hash_list[N_HASH];
620Sstevel@tonic-gate 	netgroup_name_t *to_do;
630Sstevel@tonic-gate 	netgroup_name_t *done;
640Sstevel@tonic-gate } netgroup_table_t;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate typedef struct {
670Sstevel@tonic-gate 	ns_ldap_result_t *results;
680Sstevel@tonic-gate 	ns_ldap_entry_t *entry;
690Sstevel@tonic-gate 	char **attrs;
700Sstevel@tonic-gate 	void *cookie;
710Sstevel@tonic-gate 	char *netgroup;
720Sstevel@tonic-gate 	netgroup_table_t tab;
730Sstevel@tonic-gate } getnetgrent_cookie_t;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate typedef struct {
760Sstevel@tonic-gate 	struct nss_innetgr_args *ia;
770Sstevel@tonic-gate 	const char *ssd_filter;
780Sstevel@tonic-gate 	const char *netgrname;
790Sstevel@tonic-gate 	const char *membername;
800Sstevel@tonic-gate 	netgroup_table_t tab;
810Sstevel@tonic-gate } innetgr_cookie_t;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate typedef unsigned int hash_t;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static hash_t
860Sstevel@tonic-gate get_hash(const char *s)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	unsigned int sum = 0;
890Sstevel@tonic-gate 	unsigned int i;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	for (i = 0; s[i] != '\0'; i++)
920Sstevel@tonic-gate 		sum += ((unsigned char *)s)[i];
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	return ((sum + i) % N_HASH);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Adds a name to the netgroup table
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  * Returns
1010Sstevel@tonic-gate  *	0 if successfully added or already present
1020Sstevel@tonic-gate  *	-1 if memory allocation error
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static int
1060Sstevel@tonic-gate add_netgroup_name(const char *name, netgroup_table_t *tab)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	hash_t		h;
1090Sstevel@tonic-gate 	netgroup_name_t	*ng;
1100Sstevel@tonic-gate 	netgroup_name_t	*ng_new;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (tab == NULL || name == NULL || *name == '\0')
1130Sstevel@tonic-gate 	return (NULL);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	h = get_hash(name);
1160Sstevel@tonic-gate 	ng = tab->hash_list[h];
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	while (ng != NULL) {
1190Sstevel@tonic-gate 		if (strcmp(name, ng->name) == 0)
1200Sstevel@tonic-gate 			break;
1210Sstevel@tonic-gate 		ng = ng->next_hash;
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (ng == NULL) {
1250Sstevel@tonic-gate 		ng_new = (netgroup_name_t *)
126*4953Smichen 		    calloc(1, sizeof (netgroup_name_t));
1270Sstevel@tonic-gate 		if (ng_new == NULL)
1280Sstevel@tonic-gate 			return (-1);
1290Sstevel@tonic-gate 		ng_new->name = strdup(name);
1300Sstevel@tonic-gate 		if (ng_new->name == NULL) {
1310Sstevel@tonic-gate 			free(ng_new);
1320Sstevel@tonic-gate 			return (-1);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 		ng_new->next_hash = tab->hash_list[h];
1350Sstevel@tonic-gate 		tab->hash_list[h] = ng_new;
1360Sstevel@tonic-gate 		ng_new->next = tab->to_do;
1370Sstevel@tonic-gate 		tab->to_do = ng_new;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	return (0);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static netgroup_name_t *
1430Sstevel@tonic-gate get_next_netgroup(netgroup_table_t *tab)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	netgroup_name_t *ng;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (tab == NULL)
1480Sstevel@tonic-gate 		return (NULL);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	ng = tab->to_do;
1510Sstevel@tonic-gate 	if (ng != NULL) {
1520Sstevel@tonic-gate 		tab->to_do = ng->next;
1530Sstevel@tonic-gate 		ng->next = tab->done;
1540Sstevel@tonic-gate 		tab->done = ng;
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	return (ng);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static void
1600Sstevel@tonic-gate free_netgroup_table(netgroup_table_t *tab)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	netgroup_name_t *ng, *next;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (tab == NULL)
1650Sstevel@tonic-gate 		return;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	for (ng = tab->to_do; ng != NULL; ng = next) {
1680Sstevel@tonic-gate 		if (ng->name != NULL)
1690Sstevel@tonic-gate 			free(ng->name);
1700Sstevel@tonic-gate 		next = ng->next;
1710Sstevel@tonic-gate 		free(ng);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	for (ng = tab->done; ng != NULL; ng = next) {
1750Sstevel@tonic-gate 		if (ng->name != NULL)
1760Sstevel@tonic-gate 			free(ng->name);
1770Sstevel@tonic-gate 		next = ng->next;
1780Sstevel@tonic-gate 		free(ng);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 	(void) memset(tab, 0, sizeof (*tab));
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * domain comparing routine
1850Sstevel@tonic-gate  * 	n1: See if n1 is n2 or an ancestor of it
1860Sstevel@tonic-gate  * 	n2: (in string terms, n1 is a suffix of n2)
1870Sstevel@tonic-gate  * Returns ZERO for success, -1 for failure.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate static int
1900Sstevel@tonic-gate domcmp(const char *n1, const char *n2)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate #define	PASS	0
1930Sstevel@tonic-gate #define	FAIL	-1
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	size_t		l1, l2;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if ((n1 == NULL) || (n2 == NULL))
1980Sstevel@tonic-gate 		return (FAIL);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	l1 = strlen(n1);
2010Sstevel@tonic-gate 	l2 = strlen(n2);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/* Turn a blind eye to the presence or absence of trailing periods */
2040Sstevel@tonic-gate 	if (l1 != 0 && n1[l1 - 1] == '.') {
2050Sstevel@tonic-gate 		--l1;
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	if (l2 != 0 && n2[l2 - 1] == '.') {
2080Sstevel@tonic-gate 		--l2;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	if (l1 > l2) {		/* Can't be a suffix */
2110Sstevel@tonic-gate 		return (FAIL);
2120Sstevel@tonic-gate 	} else if (l1 == 0) {	/* Trivially a suffix; */
2130Sstevel@tonic-gate 				/* (do we want this case?) */
2140Sstevel@tonic-gate 		return (PASS);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	/* So 0 < l1 <= l2 */
2170Sstevel@tonic-gate 	if (l1 < l2 && n2[l2 - l1 - 1] != '.') {
2180Sstevel@tonic-gate 		return (FAIL);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 	if (strncasecmp(n1, &n2[l2 - l1], l1) == 0) {
2210Sstevel@tonic-gate 		return (PASS);
2220Sstevel@tonic-gate 	} else {
2230Sstevel@tonic-gate 		return (FAIL);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate static int
2280Sstevel@tonic-gate split_triple(char *triple, char **hostname, char **username, char **domain)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	int	i, syntax_err;
2310Sstevel@tonic-gate 	char	*splittriple[3];
2320Sstevel@tonic-gate 	char	*p = triple;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate #ifdef	DEBUG
2350Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: split_triple]\n");
2360Sstevel@tonic-gate #endif	/* DEBUG */
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (triple == NULL)
2390Sstevel@tonic-gate 		return (-1);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	p++;
2420Sstevel@tonic-gate 	syntax_err = 0;
2430Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
2440Sstevel@tonic-gate 		char	*start;
2450Sstevel@tonic-gate 		char	*limit;
2460Sstevel@tonic-gate 		const char	*terminators = ",) \t";
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		if (i == 2) {
2490Sstevel@tonic-gate 			/* Don't allow comma */
2500Sstevel@tonic-gate 			terminators++;
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 		while (isspace(*p)) {
2530Sstevel@tonic-gate 			p++;
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 		start = p;
2560Sstevel@tonic-gate 		limit = strpbrk(start, terminators);
2570Sstevel@tonic-gate 		if (limit == 0) {
2580Sstevel@tonic-gate 			syntax_err++;
2590Sstevel@tonic-gate 			break;
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 		p = limit;
2620Sstevel@tonic-gate 		while (isspace(*p)) {
2630Sstevel@tonic-gate 			p++;
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate 		if (*p == terminators[0]) {
2660Sstevel@tonic-gate 			/*
2670Sstevel@tonic-gate 			 * Successfully parsed this name and
2680Sstevel@tonic-gate 			 * the separator after it (comma or
2690Sstevel@tonic-gate 			 * right paren); leave p ready for
2700Sstevel@tonic-gate 			 * next parse.
2710Sstevel@tonic-gate 			 */
2720Sstevel@tonic-gate 			p++;
2730Sstevel@tonic-gate 			if (start == limit) {
2740Sstevel@tonic-gate 				/* Wildcard */
2750Sstevel@tonic-gate 				splittriple[i] = NULL;
2760Sstevel@tonic-gate 			} else {
2770Sstevel@tonic-gate 				*limit = '\0';
2780Sstevel@tonic-gate 				splittriple[i] = start;
2790Sstevel@tonic-gate 			}
2800Sstevel@tonic-gate 		} else {
2810Sstevel@tonic-gate 			syntax_err++;
2820Sstevel@tonic-gate 			break;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (syntax_err != 0)
2870Sstevel@tonic-gate 		return (-1);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	*hostname = splittriple[0];
2900Sstevel@tonic-gate 	*username = splittriple[1];
2910Sstevel@tonic-gate 	*domain = splittriple[2];
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	return (0);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2972388Smj162486  * Test membership in triple
2982388Smj162486  *	return 0 = no match
2992388Smj162486  *	return 1 = match
3000Sstevel@tonic-gate  */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate static int
3030Sstevel@tonic-gate match_triple_entry(struct nss_innetgr_args *ia, const ns_ldap_entry_t *entry)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	int	ndomains;
3060Sstevel@tonic-gate 	char	**pdomains;
3070Sstevel@tonic-gate 	int	nhost;
3080Sstevel@tonic-gate 	char	**phost;
3090Sstevel@tonic-gate 	int	nusers;
3100Sstevel@tonic-gate 	char	**pusers;
3110Sstevel@tonic-gate 	char	**attr;
3120Sstevel@tonic-gate 	char	triple[MAX_TRIPLE_LEN];
3130Sstevel@tonic-gate 	char	*tuser, *thost, *tdomain;
3140Sstevel@tonic-gate 	int	i;
3152388Smj162486 	char	*current, *limit;
3162388Smj162486 	int	pulen, phlen;
3172388Smj162486 	char	*pusers0, *phost0;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	nhost = ia->arg[NSS_NETGR_MACHINE].argc;
3200Sstevel@tonic-gate 	phost = (char **)ia->arg[NSS_NETGR_MACHINE].argv;
3212388Smj162486 	if (phost == NULL || *phost == NULL) {
3220Sstevel@tonic-gate 		nhost = 0;
3232388Smj162486 	} else {
3242388Smj162486 		phost0 = phost[0];
3252388Smj162486 		phlen = strlen(phost0);
3262388Smj162486 	}
3270Sstevel@tonic-gate 	nusers = ia->arg[NSS_NETGR_USER].argc;
3280Sstevel@tonic-gate 	pusers = (char **)ia->arg[NSS_NETGR_USER].argv;
3292388Smj162486 	if (pusers == NULL || *pusers == NULL) {
3300Sstevel@tonic-gate 		nusers = 0;
3312388Smj162486 	} else {
3322388Smj162486 		pusers0 = pusers[0];
3332388Smj162486 		pulen = strlen(pusers0);
3342388Smj162486 	}
3350Sstevel@tonic-gate 	ndomains = ia->arg[NSS_NETGR_DOMAIN].argc;
3360Sstevel@tonic-gate 	pdomains = (char **)ia->arg[NSS_NETGR_DOMAIN].argv;
3370Sstevel@tonic-gate 	if (pdomains == NULL || *pdomains == NULL)
3380Sstevel@tonic-gate 		ndomains = 0;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	attr = __ns_ldap_getAttr(entry, _N_TRIPLE);
3410Sstevel@tonic-gate 	if (attr == NULL || *attr == NULL)
3420Sstevel@tonic-gate 		return (0);
3430Sstevel@tonic-gate 
3442388Smj162486 	/* Special cases for speedup */
3452388Smj162486 	if (nusers == 1 && nhost == 0 && ndomains == 0) {
3462388Smj162486 		/* Special case for finding a single user in a netgroup */
3472388Smj162486 		for (; *attr; attr++) {
3482388Smj162486 			/* jump to first comma and check next character */
3492388Smj162486 			current = *attr;
3502388Smj162486 			if ((current = strchr(current, COMMA)) == NULL)
3512388Smj162486 				continue;
3522388Smj162486 			current++;
3532388Smj162486 
3542388Smj162486 			/* skip whitespaces */
3552388Smj162486 			while (isspace(*current))
3562388Smj162486 				current++;
3572388Smj162486 
3582388Smj162486 			/* if user part is null, then treat as wildcard */
3592388Smj162486 			if (*current == COMMA)
3602388Smj162486 				return (1);
3612388Smj162486 
3622388Smj162486 			/* compare first character */
3632388Smj162486 			if (*pusers0 != *current)
3642388Smj162486 				continue;
3652388Smj162486 
3662388Smj162486 			/* limit username to COMMA */
3672388Smj162486 			if ((limit = strchr(current, COMMA)) == NULL)
3682388Smj162486 				continue;
3692388Smj162486 			*limit = '\0';
3702388Smj162486 
3712388Smj162486 			/* remove blanks before COMMA */
3722388Smj162486 			if ((limit = strpbrk(current, " \t")) != NULL)
3732388Smj162486 				*limit = '\0';
3742388Smj162486 
3752388Smj162486 			/* compare size of username */
3762388Smj162486 			if (pulen != strlen(current)) {
3772388Smj162486 				continue;
3782388Smj162486 			}
3792388Smj162486 
3802388Smj162486 			/* do actual compare */
3812388Smj162486 			if (strncmp(pusers0, current, pulen) == 0) {
3822388Smj162486 				return (1);
3832388Smj162486 			} else {
3842388Smj162486 				continue;
3852388Smj162486 			}
3862388Smj162486 		}
3872388Smj162486 	} else if (nusers == 0 && nhost == 1 && ndomains == 0) {
3882388Smj162486 		/* Special case for finding a single host in a netgroup */
3892388Smj162486 		for (; *attr; attr++) {
3902388Smj162486 
3912388Smj162486 			/* jump to first character and check */
3922388Smj162486 			current = *attr;
3932388Smj162486 			current++;
3942388Smj162486 
3952388Smj162486 			/* skip whitespaces */
3962388Smj162486 			while (isspace(*current))
3972388Smj162486 				current++;
3982388Smj162486 
3992388Smj162486 			/* if host part is null, then treat as wildcard */
4002388Smj162486 			if (*current == COMMA)
4012388Smj162486 				return (1);
4022388Smj162486 
4032388Smj162486 			/* limit hostname to COMMA */
4042388Smj162486 			if ((limit = strchr(current, COMMA)) == NULL)
4052388Smj162486 				continue;
4062388Smj162486 			*limit = '\0';
4072388Smj162486 
4082388Smj162486 			/* remove blanks before COMMA */
4092388Smj162486 			if ((limit = strpbrk(current, " \t")) != NULL)
4102388Smj162486 				*limit = '\0';
4112388Smj162486 
4122388Smj162486 			/* compare size of hostname */
4132388Smj162486 			if (phlen != strlen(current)) {
4142388Smj162486 				continue;
4152388Smj162486 			}
4162388Smj162486 
4172388Smj162486 			/* do actual compare */
4182388Smj162486 			if (strncasecmp(phost0, current, phlen) == 0) {
4192388Smj162486 				return (1);
4202388Smj162486 			} else {
4212388Smj162486 				continue;
4222388Smj162486 			}
4232388Smj162486 		}
4242388Smj162486 	} else {
4252388Smj162486 		for (; *attr; attr++) {
4262388Smj162486 			if (strlcpy(triple, *attr,
427*4953Smichen 			    sizeof (triple)) >= sizeof (triple))
4282388Smj162486 				continue;
4292388Smj162486 			if (split_triple(triple, &thost, &tuser, &tdomain) != 0)
4302388Smj162486 				continue;
4312388Smj162486 			if (thost != NULL && *thost != '\0' && nhost != 0) {
4322388Smj162486 				for (i = 0; i < nhost; i++)
4332388Smj162486 					if (strcasecmp(thost, phost[i]) == 0)
4342388Smj162486 						break;
4352388Smj162486 				if (i == nhost)
436*4953Smichen 					continue;
4372388Smj162486 			}
4382388Smj162486 			if (tuser != NULL && *tuser != '\0' && nusers != 0) {
4392388Smj162486 				for (i = 0; i < nusers; i++)
4402388Smj162486 					if (strcmp(tuser, pusers[i]) == 0)
4412388Smj162486 						break;
4422388Smj162486 				if (i == nusers)
4432388Smj162486 					continue;
4442388Smj162486 			}
4452388Smj162486 			if (tdomain != NULL && *tdomain != '\0' &&
446*4953Smichen 			    ndomains != 0) {
4472388Smj162486 				for (i = 0; i < ndomains; i++)
4482388Smj162486 					if (domcmp(tdomain, pdomains[i]) == 0)
4492388Smj162486 						break;
4502388Smj162486 				if (i == ndomains)
4512388Smj162486 					continue;
4522388Smj162486 			}
4532388Smj162486 			return (1);
4542388Smj162486 		}
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	return (0);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate static int
4610Sstevel@tonic-gate match_triple(struct nss_innetgr_args *ia, ns_ldap_result_t *result)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	ns_ldap_entry_t	*entry;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	for (entry = result->entry; entry != NULL; entry = entry->next)
466*4953Smichen 		if (match_triple_entry(ia, entry) == 1)
467*4953Smichen 			return (1);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	return (0);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate static int
4730Sstevel@tonic-gate add_netgroup_member_entry(ns_ldap_entry_t *entry, netgroup_table_t *tab)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	char		**attrs;
4760Sstevel@tonic-gate 	char		**a;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	attrs = __ns_ldap_getAttr(entry, _N_MEMBER);
4790Sstevel@tonic-gate 	if (attrs == NULL || *attrs == NULL)
4800Sstevel@tonic-gate 		return (0);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	for (a = attrs; *a != NULL; a++) {}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	do {
4850Sstevel@tonic-gate 		a--;
4860Sstevel@tonic-gate 		if (add_netgroup_name(*a, tab) != 0)
4870Sstevel@tonic-gate 			return (-1);
4880Sstevel@tonic-gate 	} while (a > attrs);
4890Sstevel@tonic-gate 	return (0);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate static int
4930Sstevel@tonic-gate add_netgroup_member(ns_ldap_result_t *result, netgroup_table_t *tab)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	ns_ldap_entry_t	*entry;
4960Sstevel@tonic-gate 	int		ret = 0;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	for (entry = result->entry; entry != NULL; entry = entry->next) {
499*4953Smichen 		ret = add_netgroup_member_entry(entry, tab);
500*4953Smichen 		if (ret != 0)
501*4953Smichen 			break;
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 	return (ret);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate  * top_down_search checks only checks the netgroup specified in netgrname
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static nss_status_t
5100Sstevel@tonic-gate top_down_search(struct nss_innetgr_args *ia, char *netgrname)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	char			searchfilter[SEARCHFILTERLEN];
5130Sstevel@tonic-gate 	char			name[SEARCHFILTERLEN];
5140Sstevel@tonic-gate 	char			userdata[SEARCHFILTERLEN];
5150Sstevel@tonic-gate 	ns_ldap_result_t	*result = NULL;
5160Sstevel@tonic-gate 	ns_ldap_error_t		*error = NULL;
5170Sstevel@tonic-gate 	int			rc;
5180Sstevel@tonic-gate 	void			*cookie = NULL;
5190Sstevel@tonic-gate 	nss_status_t		status = NSS_NOTFOUND;
520*4953Smichen 	nss_status_t		status1;
5210Sstevel@tonic-gate 	netgroup_table_t	tab;
5220Sstevel@tonic-gate 	netgroup_name_t		*ng;
5230Sstevel@tonic-gate 	int			ret;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	(void) memset(&tab, 0, sizeof (tab));
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (add_netgroup_name(netgrname, &tab) != 0)
528*4953Smichen 		return ((nss_status_t)NSS_NOTFOUND);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	while ((ng = get_next_netgroup(&tab)) != NULL) {
531*4953Smichen 		if (_ldap_filter_name(name, ng->name, sizeof (name)) != 0)
532*4953Smichen 			break;
533*4953Smichen 		ret = snprintf(searchfilter, sizeof (searchfilter),
534*4953Smichen 		    _F_SETMEMBER, name);
535*4953Smichen 		if (ret >= sizeof (searchfilter) || ret < 0)
536*4953Smichen 			break;
537*4953Smichen 
538*4953Smichen 		ret = snprintf(userdata, sizeof (userdata), _F_SETMEMBER_SSD,
5390Sstevel@tonic-gate 		    name);
540*4953Smichen 		if (ret >= sizeof (userdata) || ret < 0)
541*4953Smichen 			break;
5420Sstevel@tonic-gate 
543*4953Smichen 		rc = __ns_ldap_firstEntry(_NETGROUP, searchfilter,
544*4953Smichen 		    _merge_SSD_filter, netgrent_attrs, NULL, 0, &cookie,
545*4953Smichen 		    &result, &error, userdata);
5460Sstevel@tonic-gate 
547*4953Smichen 		if (error != NULL) {
548*4953Smichen 			status1 = switch_err(rc, error);
549*4953Smichen 			if (status1 == NSS_TRYAGAIN) {
550*4953Smichen 				(void) __ns_ldap_freeError(&error);
551*4953Smichen 				free_netgroup_table(&tab);
552*4953Smichen 				return (status1);
553*4953Smichen 			}
5540Sstevel@tonic-gate 		}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&error);
557*4953Smichen 		while (rc == NS_LDAP_SUCCESS && result != NULL) {
558*4953Smichen 			if (match_triple(ia, result) == 1) {
559*4953Smichen 				/* We found a match */
560*4953Smichen 				ia->status = NSS_NETGR_FOUND;
561*4953Smichen 				status = NSS_SUCCESS;
562*4953Smichen 				break;
563*4953Smichen 			}
564*4953Smichen 
565*4953Smichen 			rc = add_netgroup_member(result, &tab);
566*4953Smichen 			(void) __ns_ldap_freeResult(&result);
5670Sstevel@tonic-gate 
568*4953Smichen 			if (rc != NS_LDAP_SUCCESS)
569*4953Smichen 				break;
570*4953Smichen 			rc = __ns_ldap_nextEntry(cookie, &result, &error);
571*4953Smichen 			if (error != NULL) {
572*4953Smichen 				status1 = switch_err(rc, error);
573*4953Smichen 				if (status1 == NSS_TRYAGAIN) {
574*4953Smichen 					free_netgroup_table(&tab);
575*4953Smichen 					(void) __ns_ldap_freeError(&error);
576*4953Smichen 					(void) __ns_ldap_endEntry(&cookie,
577*4953Smichen 					    &error);
578*4953Smichen 					(void) __ns_ldap_freeError(&error);
579*4953Smichen 					return (status1);
580*4953Smichen 				}
581*4953Smichen 			}
582*4953Smichen 			(void) __ns_ldap_freeError(&error);
583*4953Smichen 		}
584*4953Smichen 		(void) __ns_ldap_freeResult(&result);
585*4953Smichen 		(void) __ns_ldap_endEntry(&cookie, &error);
586*4953Smichen 		(void) __ns_ldap_freeError(&error);
587*4953Smichen 
588*4953Smichen 		if (status == NSS_SUCCESS ||
589*4953Smichen 		    (rc != NS_LDAP_SUCCESS && rc != NS_LDAP_NOTFOUND))
5900Sstevel@tonic-gate 		break;
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&result);
5940Sstevel@tonic-gate 	(void) __ns_ldap_endEntry(&cookie, &error);
5950Sstevel@tonic-gate 	(void) __ns_ldap_freeError(&error);
5960Sstevel@tonic-gate 	free_netgroup_table(&tab);
5970Sstevel@tonic-gate 	return (status);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate  * __netgr_in checks only checks the netgroup specified in ngroup
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate static nss_status_t
6040Sstevel@tonic-gate __netgr_in(void *a, char *netgrname)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate 	struct nss_innetgr_args	*ia = (struct nss_innetgr_args *)a;
6070Sstevel@tonic-gate 	nss_status_t		status = NSS_NOTFOUND;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate #ifdef DEBUG
6100Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: netgr_in]\n");
6110Sstevel@tonic-gate 	(void) fprintf(stdout, "\tmachine: argc[%d]='%s' user: "
612*4953Smichen 	    "argc[%d]='%s',\n\tdomain:argc[%d]='%s' "
613*4953Smichen 	    "netgroup: argc[%d]='%s'\n",
614*4953Smichen 	    NSS_NETGR_MACHINE,
615*4953Smichen 	    PRINT_VAL(ia->arg[NSS_NETGR_MACHINE]),
616*4953Smichen 	    NSS_NETGR_USER,
617*4953Smichen 	    PRINT_VAL(ia->arg[NSS_NETGR_USER]),
618*4953Smichen 	    NSS_NETGR_DOMAIN,
619*4953Smichen 	    PRINT_VAL(ia->arg[NSS_NETGR_DOMAIN]),
620*4953Smichen 	    NSS_NETGR_N,
621*4953Smichen 	    PRINT_VAL(ia->arg[NSS_NETGR_N]));
6220Sstevel@tonic-gate 	(void) fprintf(stdout, "\tgroups='%s'\n", netgrname);
6230Sstevel@tonic-gate #endif	/* DEBUG */
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	ia->status = NSS_NETGR_NO;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (netgrname == NULL)
6280Sstevel@tonic-gate 		return (status);
6290Sstevel@tonic-gate 
6302388Smj162486 	return (top_down_search(ia, netgrname));
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate /*ARGSUSED0*/
6340Sstevel@tonic-gate static nss_status_t
6350Sstevel@tonic-gate netgr_in(ldap_backend_ptr be, void *a)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	struct nss_innetgr_args	*ia = (struct nss_innetgr_args *)a;
6380Sstevel@tonic-gate 	int	i;
6390Sstevel@tonic-gate 	nss_status_t	rc = (nss_status_t)NSS_NOTFOUND;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	ia->status = NSS_NETGR_NO;
6420Sstevel@tonic-gate 	for (i = 0; i < ia->groups.argc; i++) {
6430Sstevel@tonic-gate 		rc = __netgr_in(a, ia->groups.argv[i]);
6440Sstevel@tonic-gate 		if (ia->status == NSS_NETGR_FOUND)
6450Sstevel@tonic-gate 			return (NSS_SUCCESS);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 	return (rc);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  *
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate static nss_status_t
6550Sstevel@tonic-gate getnetgr_ldap_setent(ldap_backend_ptr be, void *a)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	const char	*netgroup = (const char *) a;
6580Sstevel@tonic-gate 	getnetgrent_cookie_t	*cookie;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate #ifdef	DEBUG
6610Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: getnetgr_ldap_setent]\n");
6620Sstevel@tonic-gate #endif	/* DEBUG */
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	cookie = (getnetgrent_cookie_t *)be->netgroup_cookie;
6650Sstevel@tonic-gate 	if (cookie != NULL && cookie->netgroup != NULL) {
6660Sstevel@tonic-gate 		/* is this another set on the same netgroup */
6670Sstevel@tonic-gate 		if (strcmp(cookie->netgroup, netgroup) == 0)
6680Sstevel@tonic-gate 			return ((nss_status_t)NSS_SUCCESS);
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	return (NSS_NOTFOUND);
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate static void
6750Sstevel@tonic-gate free_getnetgrent_cookie(getnetgrent_cookie_t **cookie)
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate 	ns_ldap_error_t	*error = NULL;
6780Sstevel@tonic-gate 	getnetgrent_cookie_t *p = *cookie;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate #ifdef DEBUG
6810Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: free_getnetgrent_cookie]\n");
6820Sstevel@tonic-gate #endif	/* DEBUG */
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	if (p == NULL)
6850Sstevel@tonic-gate 		return;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&p->results);
6880Sstevel@tonic-gate 	(void) __ns_ldap_endEntry(&p->cookie, &error);
6890Sstevel@tonic-gate 	(void) __ns_ldap_freeError(&error);
6900Sstevel@tonic-gate 	free_netgroup_table(&p->tab);
6910Sstevel@tonic-gate 	free(p->netgroup);
6920Sstevel@tonic-gate 	free(p);
6930Sstevel@tonic-gate 	*cookie = NULL;
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate /*ARGSUSED1*/
6970Sstevel@tonic-gate static nss_status_t
6980Sstevel@tonic-gate getnetgr_ldap_endent(ldap_backend_ptr be, void *a)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate #ifdef	DEBUG
7020Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: getnetgr_ldap_endent]\n");
7030Sstevel@tonic-gate #endif	/* DEBUG */
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	free_getnetgrent_cookie((getnetgrent_cookie_t **)&be->netgroup_cookie);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	return ((nss_status_t)NSS_NOTFOUND);
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate /*ARGSUSED1*/
7120Sstevel@tonic-gate static nss_status_t
7130Sstevel@tonic-gate getnetgr_ldap_destr(ldap_backend_ptr be, void *a)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate #ifdef	DEBUG
7170Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: getnetgr_ldap_destr]\n");
7180Sstevel@tonic-gate #endif	/* DEBUG */
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	free_getnetgrent_cookie((getnetgrent_cookie_t **)&be->netgroup_cookie);
7210Sstevel@tonic-gate 	free(be);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	return ((nss_status_t)NSS_NOTFOUND);
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate static nss_status_t
7280Sstevel@tonic-gate getnetgr_ldap_getent(ldap_backend_ptr be, void *a)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	struct nss_getnetgrent_args	*args;
7310Sstevel@tonic-gate 	getnetgrent_cookie_t	*p;
7320Sstevel@tonic-gate 	char			searchfilter[SEARCHFILTERLEN];
7330Sstevel@tonic-gate 	char			userdata[SEARCHFILTERLEN];
7340Sstevel@tonic-gate 	char			name[SEARCHFILTERLEN];
7350Sstevel@tonic-gate 	int			rc;
7360Sstevel@tonic-gate 	void			*cookie = NULL;
7370Sstevel@tonic-gate 	ns_ldap_result_t	*result = NULL;
7380Sstevel@tonic-gate 	ns_ldap_error_t		*error = NULL;
7390Sstevel@tonic-gate 	char			**attrs;
7400Sstevel@tonic-gate 	char			*hostname, *username, *domain;
7410Sstevel@tonic-gate 	char			*buffer;
7420Sstevel@tonic-gate 	nss_status_t		status = NSS_SUCCESS;
7430Sstevel@tonic-gate 	netgroup_name_t		*ng;
7440Sstevel@tonic-gate 	int			ret;
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate #ifdef	DEBUG
7470Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: getnetgr_ldap_getent]\n");
7480Sstevel@tonic-gate #endif	/* DEBUG */
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	args = (struct nss_getnetgrent_args *)a;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	args->status = NSS_NETGR_NO;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	p = (getnetgrent_cookie_t *)be->netgroup_cookie;
7550Sstevel@tonic-gate 	if (p == NULL)
7560Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	for (;;) {
759*4953Smichen 		while (p->cookie == NULL) {
760*4953Smichen 			ng = get_next_netgroup(&p->tab);
761*4953Smichen 			if (ng == NULL)	 /* no more */
762*4953Smichen 				break;
7630Sstevel@tonic-gate 
764*4953Smichen 			if (_ldap_filter_name(name, ng->name,
765*4953Smichen 			    sizeof (name)) != 0)
766*4953Smichen 				break;
7670Sstevel@tonic-gate 
768*4953Smichen 			ret = snprintf(searchfilter,
769*4953Smichen 			    sizeof (searchfilter),
770*4953Smichen 			    _F_SETMEMBER, name);
771*4953Smichen 			if (ret >= sizeof (searchfilter) || ret < 0)
772*4953Smichen 				break;
7730Sstevel@tonic-gate 
774*4953Smichen 			ret = snprintf(userdata, sizeof (userdata),
775*4953Smichen 			    _F_SETMEMBER_SSD, name);
776*4953Smichen 			if (ret >= sizeof (userdata) || ret < 0)
777*4953Smichen 				break;
7780Sstevel@tonic-gate 
779*4953Smichen 			result = NULL;
780*4953Smichen 			rc = __ns_ldap_firstEntry(_NETGROUP,
781*4953Smichen 			    searchfilter,
782*4953Smichen 			    _merge_SSD_filter, netgrent_attrs,
783*4953Smichen 			    NULL, 0, &cookie,
784*4953Smichen 			    &result, &error, userdata);
785*4953Smichen 			(void) __ns_ldap_freeError(&error);
7860Sstevel@tonic-gate 
787*4953Smichen 			if (rc == NS_LDAP_SUCCESS && result != NULL) {
788*4953Smichen 				p->cookie = cookie;
789*4953Smichen 				p->results = result;
790*4953Smichen 				break;
791*4953Smichen 			}
792*4953Smichen 			(void) __ns_ldap_freeResult(&result);
793*4953Smichen 			(void) __ns_ldap_endEntry(&cookie, &error);
794*4953Smichen 			(void) __ns_ldap_freeError(&error);
7950Sstevel@tonic-gate 		}
796*4953Smichen 		if (p->cookie == NULL)
797*4953Smichen 			break;
798*4953Smichen 		if (p->results == NULL) {
799*4953Smichen 			result = NULL;
800*4953Smichen 			rc = __ns_ldap_nextEntry(p->cookie, &result,
801*4953Smichen 			    &error);
802*4953Smichen 			(void) __ns_ldap_freeError(&error);
803*4953Smichen 			if (rc == NS_LDAP_SUCCESS && result != NULL)
804*4953Smichen 				p->results = result;
805*4953Smichen 			else {
806*4953Smichen 				(void) __ns_ldap_freeResult(&result);
807*4953Smichen 				(void) __ns_ldap_endEntry(&p->cookie,
808*4953Smichen 				    &error);
809*4953Smichen 				(void) __ns_ldap_freeError(&error);
810*4953Smichen 				p->cookie = NULL;
811*4953Smichen 			}
8120Sstevel@tonic-gate 		}
813*4953Smichen 		if (p->results == NULL)
814*4953Smichen 			continue;
8150Sstevel@tonic-gate 
816*4953Smichen 		if (p->entry == NULL)
817*4953Smichen 			p->entry = p->results->entry;
8180Sstevel@tonic-gate 
819*4953Smichen 		if (p->entry == NULL)
820*4953Smichen 			continue;
8210Sstevel@tonic-gate 
822*4953Smichen 		if (p->attrs == NULL) {
823*4953Smichen 			attrs = __ns_ldap_getAttr(p->entry, _N_TRIPLE);
824*4953Smichen 			if (attrs != NULL && *attrs != NULL)
825*4953Smichen 				p->attrs = attrs;
8260Sstevel@tonic-gate 		}
8270Sstevel@tonic-gate 
828*4953Smichen 		if (p->attrs != NULL) {
829*4953Smichen 			attrs = p->attrs;
830*4953Smichen 			buffer = args->buffer;
831*4953Smichen 
832*4953Smichen 			if (strlcpy(buffer, *attrs, args->buflen) >=
833*4953Smichen 			    args->buflen) {
834*4953Smichen 				status = NSS_STR_PARSE_ERANGE;
835*4953Smichen 				break;
836*4953Smichen 			}
8370Sstevel@tonic-gate 
838*4953Smichen 			rc = split_triple(buffer, &hostname, &username,
839*4953Smichen 			    &domain);
840*4953Smichen 			attrs++;
841*4953Smichen 			if (attrs != NULL && *attrs != NULL)
842*4953Smichen 				p->attrs = attrs;
843*4953Smichen 			else
844*4953Smichen 				p->attrs = NULL;
845*4953Smichen 			if (rc == 0) {
846*4953Smichen 				args->retp[NSS_NETGR_MACHINE] = hostname;
847*4953Smichen 				args->retp[NSS_NETGR_USER] = username;
848*4953Smichen 				args->retp[NSS_NETGR_DOMAIN] = domain;
849*4953Smichen 				args->status = NSS_NETGR_FOUND;
850*4953Smichen 				if (p->attrs != NULL)
851*4953Smichen 					break;
852*4953Smichen 			}
8530Sstevel@tonic-gate 		}
8540Sstevel@tonic-gate 
855*4953Smichen 		if (p->attrs == NULL) {
856*4953Smichen 			rc = add_netgroup_member_entry(p->entry, &p->tab);
857*4953Smichen 			if (rc != 0) {
858*4953Smichen 				args->status = NSS_NETGR_NO;
859*4953Smichen 				break;
860*4953Smichen 			}
861*4953Smichen 
862*4953Smichen 			p->entry = p->entry->next;
863*4953Smichen 			if (p->entry == NULL)
864*4953Smichen 				(void) __ns_ldap_freeResult(&p->results);
865*4953Smichen 			if (args->status == NSS_NETGR_FOUND)
866*4953Smichen 				break;
867*4953Smichen 		}
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	return (status);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate static ldap_backend_op_t getnetgroup_ops[] = {
8740Sstevel@tonic-gate 	getnetgr_ldap_destr,
8750Sstevel@tonic-gate 	getnetgr_ldap_endent,
8760Sstevel@tonic-gate 	getnetgr_ldap_setent,
8770Sstevel@tonic-gate 	getnetgr_ldap_getent,
8780Sstevel@tonic-gate };
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /*
8810Sstevel@tonic-gate  *
8820Sstevel@tonic-gate  */
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate static nss_status_t
8850Sstevel@tonic-gate netgr_set(ldap_backend_ptr be, void *a)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate 	struct nss_setnetgrent_args	*args =
888*4953Smichen 	    (struct nss_setnetgrent_args *)a;
8890Sstevel@tonic-gate 	ldap_backend_ptr		get_be;
8900Sstevel@tonic-gate 	getnetgrent_cookie_t		*p;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate #ifdef DEBUG
8930Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: netgr_set]\n");
8940Sstevel@tonic-gate 	(void) fprintf(stdout,
895*4953Smichen 	    "\targs->netgroup: %s\n", ISNULL(args->netgroup));
8960Sstevel@tonic-gate #endif /* DEBUG */
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if (args->netgroup == NULL)
8990Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	free_getnetgrent_cookie((getnetgrent_cookie_t **)&be->netgroup_cookie);
9020Sstevel@tonic-gate 	p = (getnetgrent_cookie_t *)calloc(1, sizeof (getnetgrent_cookie_t));
9030Sstevel@tonic-gate 	if (p == NULL)
9040Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
9050Sstevel@tonic-gate 	p->netgroup = strdup(args->netgroup);
9060Sstevel@tonic-gate 	if (p->netgroup == NULL) {
9070Sstevel@tonic-gate 		free(p);
9080Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 	if (add_netgroup_name(args->netgroup, &p->tab) == -1) {
9110Sstevel@tonic-gate 		free_getnetgrent_cookie(&p);
9120Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	/* now allocate and return iteration backend structure */
9160Sstevel@tonic-gate 	if ((get_be = (ldap_backend_ptr)malloc(sizeof (*get_be))) == NULL)
9170Sstevel@tonic-gate 		return (NSS_UNAVAIL);
9180Sstevel@tonic-gate 	get_be->ops = getnetgroup_ops;
9190Sstevel@tonic-gate 	get_be->nops = sizeof (getnetgroup_ops) / sizeof (getnetgroup_ops[0]);
9200Sstevel@tonic-gate 	get_be->tablename = NULL;
9210Sstevel@tonic-gate 	get_be->attrs = netgrent_attrs;
9220Sstevel@tonic-gate 	get_be->result = NULL;
9232830Sdjl 	get_be->ldapobj2str = NULL;
9240Sstevel@tonic-gate 	get_be->setcalled = 1;
9250Sstevel@tonic-gate 	get_be->filter = NULL;
9260Sstevel@tonic-gate 	get_be->toglue = NULL;
9270Sstevel@tonic-gate 	get_be->enumcookie = NULL;
9280Sstevel@tonic-gate 	get_be->netgroup_cookie = p;
9290Sstevel@tonic-gate 	args->iterator = (nss_backend_t *)get_be;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	return (NSS_SUCCESS);
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate /*ARGSUSED1*/
9380Sstevel@tonic-gate static nss_status_t
9390Sstevel@tonic-gate netgr_ldap_destr(ldap_backend_ptr be, void *a)
9400Sstevel@tonic-gate {
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate #ifdef	DEBUG
9430Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getnetgrent.c: netgr_ldap_destr]\n");
9440Sstevel@tonic-gate #endif	/* DEBUG */
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	(void) _clean_ldap_backend(be);
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	return ((nss_status_t)NSS_NOTFOUND);
9490Sstevel@tonic-gate }
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate static ldap_backend_op_t netgroup_ops[] = {
9550Sstevel@tonic-gate 	netgr_ldap_destr,
9560Sstevel@tonic-gate 	0,
9570Sstevel@tonic-gate 	0,
9580Sstevel@tonic-gate 	0,
9590Sstevel@tonic-gate 	netgr_in,		/*	innetgr()	*/
9600Sstevel@tonic-gate 	netgr_set		/*	setnetgrent()	*/
9610Sstevel@tonic-gate };
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate /*
9650Sstevel@tonic-gate  * _nss_ldap_netgroup_constr is where life begins. This function calls the
9660Sstevel@tonic-gate  * generic ldap constructor function to define and build the abstract data
9670Sstevel@tonic-gate  * types required to support ldap operations.
9680Sstevel@tonic-gate  */
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*ARGSUSED0*/
9710Sstevel@tonic-gate nss_backend_t *
9720Sstevel@tonic-gate _nss_ldap_netgroup_constr(const char *dummy1, const char *dummy2,
9730Sstevel@tonic-gate 			const char *dummy3)
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate #ifdef	DEBUG
9770Sstevel@tonic-gate 	(void) fprintf(stdout,
978*4953Smichen 	    "\n[getnetgrent.c: _nss_ldap_netgroup_constr]\n");
9790Sstevel@tonic-gate #endif	/* DEBUG */
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(netgroup_ops,
982*4953Smichen 	    sizeof (netgroup_ops)/sizeof (netgroup_ops[0]), _NETGROUP,
983*4953Smichen 	    netgrent_attrs, NULL));
9840Sstevel@tonic-gate }
985