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
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * 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*1676Sjpk  * Copyright 2006 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 "ldap_common.h"
290Sstevel@tonic-gate #include <malloc.h>
300Sstevel@tonic-gate #include <synch.h>
310Sstevel@tonic-gate #include <syslog.h>
320Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
330Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
340Sstevel@tonic-gate #include <thread.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <signal.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /* getent attributes filters */
410Sstevel@tonic-gate #define	_F_GETALIASENT		"(objectClass=rfc822MailGroup)"
420Sstevel@tonic-gate #define	_F_GETAUTHNAME		"(objectClass=SolarisAuthAttr)"
430Sstevel@tonic-gate #define	_F_GETAUUSERNAME	"(objectClass=SolarisAuditUser)"
440Sstevel@tonic-gate #define	_F_GETEXECNAME		"(objectClass=SolarisExecAttr)"
450Sstevel@tonic-gate #define	_F_GETGRENT		"(objectClass=posixGroup)"
460Sstevel@tonic-gate #define	_F_GETHOSTENT		"(objectClass=ipHost)"
470Sstevel@tonic-gate #define	_F_GETNETENT		"(objectClass=ipNetwork)"
480Sstevel@tonic-gate #define	_F_GETPROFNAME		"(objectClass=SolarisProfAttr)"
490Sstevel@tonic-gate #define	_F_GETPROTOENT		"(objectClass=ipProtocol)"
500Sstevel@tonic-gate #define	_F_GETPWENT		"(objectClass=posixAccount)"
510Sstevel@tonic-gate #define	_F_GETPRINTERENT	"(objectClass=sunPrinter)"
520Sstevel@tonic-gate #define	_F_GETRPCENT		"(objectClass=oncRpc)"
530Sstevel@tonic-gate #define	_F_GETSERVENT		"(objectClass=ipService)"
540Sstevel@tonic-gate #define	_F_GETSPENT		"(objectclass=shadowAccount)"
550Sstevel@tonic-gate #define	_F_GETUSERNAME		"(objectClass=SolarisUserAttr)"
560Sstevel@tonic-gate #define	_F_GETPROJENT		"(objectClass=SolarisProject)"
57*1676Sjpk #define	_F_GETTNRHDB		"(objectClass=ipTnetHost)"
58*1676Sjpk #define	_F_GETTNRHTP		"(&(objectClass=ipTnetTemplate)"\
59*1676Sjpk 				"(SolarisAttrKeyValue=*))"
600Sstevel@tonic-gate #define	_F_GETENT_SSD		"(%s)"
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static struct gettablefilter {
630Sstevel@tonic-gate 	char *tablename;
640Sstevel@tonic-gate 	char *tablefilter;
650Sstevel@tonic-gate } gettablefilterent[] = {
660Sstevel@tonic-gate 	{(char *)_PASSWD,	(char *)_F_GETPWENT},
670Sstevel@tonic-gate 	{(char *)_SHADOW,	(char *)_F_GETSPENT},
680Sstevel@tonic-gate 	{(char *)_GROUP,	(char *)_F_GETGRENT},
690Sstevel@tonic-gate 	{(char *)_HOSTS,	(char *)_F_GETHOSTENT},
700Sstevel@tonic-gate 	{(char *)_NETWORKS,	(char *)_F_GETNETENT},
710Sstevel@tonic-gate 	{(char *)_PROTOCOLS,	(char *)_F_GETPROTOENT},
720Sstevel@tonic-gate 	{(char *)_RPC,		(char *)_F_GETRPCENT},
730Sstevel@tonic-gate 	{(char *)_ALIASES,	(char *)_F_GETALIASENT},
740Sstevel@tonic-gate 	{(char *)_SERVICES,	(char *)_F_GETSERVENT},
750Sstevel@tonic-gate 	{(char *)_AUUSER,	(char *)_F_GETAUUSERNAME},
760Sstevel@tonic-gate 	{(char *)_AUTHATTR,	(char *)_F_GETAUTHNAME},
770Sstevel@tonic-gate 	{(char *)_EXECATTR,	(char *)_F_GETEXECNAME},
780Sstevel@tonic-gate 	{(char *)_PROFATTR,	(char *)_F_GETPROFNAME},
790Sstevel@tonic-gate 	{(char *)_USERATTR,	(char *)_F_GETUSERNAME},
800Sstevel@tonic-gate 	{(char *)_PROJECT,	(char *)_F_GETPROJENT},
810Sstevel@tonic-gate 	{(char *)_PRINTERS,	(char *)_F_GETPRINTERENT},
82*1676Sjpk 	{(char *)_TNRHDB,	(char *)_F_GETTNRHDB},
83*1676Sjpk 	{(char *)_TNRHTP,	(char *)_F_GETTNRHTP},
840Sstevel@tonic-gate 	{(char *)NULL,		(char *)NULL}
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 
880Sstevel@tonic-gate nss_status_t
890Sstevel@tonic-gate switch_err(int rc, ns_ldap_error_t *error)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	switch (rc) {
920Sstevel@tonic-gate 	    case NS_LDAP_SUCCESS:
930Sstevel@tonic-gate 		return (NSS_SUCCESS);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	    case NS_LDAP_NOTFOUND:
960Sstevel@tonic-gate 		return (NSS_NOTFOUND);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	    case NS_LDAP_PARTIAL:
990Sstevel@tonic-gate 		return (NSS_TRYAGAIN);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	    case NS_LDAP_INTERNAL:
1020Sstevel@tonic-gate 		    if (error && (error->status == LDAP_SERVER_DOWN ||
1030Sstevel@tonic-gate 				error->status == LDAP_TIMEOUT))
1040Sstevel@tonic-gate 			    return (NSS_TRYAGAIN);
1050Sstevel@tonic-gate 		    else
1060Sstevel@tonic-gate 			    return (NSS_UNAVAIL);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	    default:
1090Sstevel@tonic-gate 		return (NSS_UNAVAIL);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate nss_status_t
1130Sstevel@tonic-gate _nss_ldap_lookup(ldap_backend_ptr be, nss_XbyY_args_t *argp,
1140Sstevel@tonic-gate 		char *database, char *searchfilter, char *domain,
1150Sstevel@tonic-gate 		int (*init_filter_cb)(const ns_ldap_search_desc_t *desc,
1160Sstevel@tonic-gate 		char **realfilter, const void *userdata),
1170Sstevel@tonic-gate 		const void *userdata)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	int		callbackstat = 0;
1200Sstevel@tonic-gate 	ns_ldap_error_t	*error = NULL;
1210Sstevel@tonic-gate 	int		rc;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #ifdef	DEBUG
1240Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[ldap_common.c: _nss_ldap_lookup]\n");
1250Sstevel@tonic-gate 	(void) fprintf(stdout, "\tsearchfilter: %s\n", searchfilter);
1260Sstevel@tonic-gate 	(void) fprintf(stdout,
1270Sstevel@tonic-gate 		"\tuserdata: %s\n", userdata ? userdata : "NULL");
1280Sstevel@tonic-gate 	(void) fprintf(stdout, "\tdatabase: %s\n", database);
1290Sstevel@tonic-gate #endif	/* DEBUG */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if ((rc = __ns_ldap_list(database, searchfilter, init_filter_cb,
1340Sstevel@tonic-gate 		be->attrs, NULL, 0, &be->result, &error, NULL,
1350Sstevel@tonic-gate 		userdata)) != NS_LDAP_SUCCESS) {
1360Sstevel@tonic-gate 		argp->returnval = 0;
1370Sstevel@tonic-gate 		rc = switch_err(rc, error);
1380Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&error);
1390Sstevel@tonic-gate 		return (rc);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	/* callback function */
1420Sstevel@tonic-gate 	if ((callbackstat =
1430Sstevel@tonic-gate 		    be->ldapobj2ent(be, argp)) == NSS_STR_PARSE_SUCCESS) {
1440Sstevel@tonic-gate 		argp->returnval = argp->buf.result;
1450Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/* error */
1500Sstevel@tonic-gate 	if (callbackstat == NSS_STR_PARSE_PARSE) {
1510Sstevel@tonic-gate 		argp->returnval = 0;
1520Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	if (callbackstat == NSS_STR_PARSE_ERANGE) {
1550Sstevel@tonic-gate 		argp->erange = 1;
1560Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	if (callbackstat == NSS_STR_PARSE_NO_ADDR) {
1590Sstevel@tonic-gate 		/* No IPV4 address is found */
1600Sstevel@tonic-gate 		argp->h_errno = HOST_NOT_FOUND;
1610Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 	return ((nss_status_t)NSS_UNAVAIL);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  *  This function is similar to _nss_ldap_lookup except it does not
1690Sstevel@tonic-gate  *  do a callback.  It is only used by getnetgrent.c
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate nss_status_t
1730Sstevel@tonic-gate _nss_ldap_nocb_lookup(ldap_backend_ptr be, nss_XbyY_args_t *argp,
1740Sstevel@tonic-gate 		char *database, char *searchfilter, char *domain,
1750Sstevel@tonic-gate 		int (*init_filter_cb)(const ns_ldap_search_desc_t *desc,
1760Sstevel@tonic-gate 		char **realfilter, const void *userdata),
1770Sstevel@tonic-gate 		const void *userdata)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	ns_ldap_error_t	*error = NULL;
1800Sstevel@tonic-gate 	int		rc;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate #ifdef	DEBUG
1830Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[ldap_common.c: _nss_ldap_nocb_lookup]\n");
1840Sstevel@tonic-gate 	(void) fprintf(stdout, "\tsearchfilter: %s\n", searchfilter);
1850Sstevel@tonic-gate 	(void) fprintf(stdout, "\tdatabase: %s\n", database);
1860Sstevel@tonic-gate 	(void) fprintf(stdout,
1870Sstevel@tonic-gate 		"\tuserdata: %s\n", userdata ? userdata : "NULL");
1880Sstevel@tonic-gate #endif	/* DEBUG */
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if ((rc = __ns_ldap_list(database, searchfilter, init_filter_cb,
1930Sstevel@tonic-gate 		be->attrs, NULL, 0, &be->result, &error, NULL,
1940Sstevel@tonic-gate 		userdata)) != NS_LDAP_SUCCESS) {
1950Sstevel@tonic-gate 		argp->returnval = 0;
1960Sstevel@tonic-gate 		rc = switch_err(rc, error);
1970Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&error);
1980Sstevel@tonic-gate 		return (rc);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	return ((nss_status_t)NSS_SUCCESS);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  */
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate void
2100Sstevel@tonic-gate _clean_ldap_backend(ldap_backend_ptr be)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	ns_ldap_error_t *error;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate #ifdef	DEBUG
2150Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[ldap_common.c: _clean_ldap_backend]\n");
2160Sstevel@tonic-gate #endif	/* DEBUG */
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (be->tablename != NULL)
2190Sstevel@tonic-gate 		free(be->tablename);
2200Sstevel@tonic-gate 	if (be->result != NULL)
2210Sstevel@tonic-gate 		(void) __ns_ldap_freeResult(&be->result);
2220Sstevel@tonic-gate 	if (be->enumcookie != NULL)
2230Sstevel@tonic-gate 		(void) __ns_ldap_endEntry(&be->enumcookie, &error);
2240Sstevel@tonic-gate 	if (be->services_cookie != NULL)
2250Sstevel@tonic-gate 		_nss_services_cookie_free((void **)&be->services_cookie);
2260Sstevel@tonic-gate 	if (be->toglue != NULL) {
2270Sstevel@tonic-gate 		free(be->toglue);
2280Sstevel@tonic-gate 		be->toglue = NULL;
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 	free(be);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * _nss_ldap_destr will free all smalloc'ed variable strings and structures
2360Sstevel@tonic-gate  * before exiting this nsswitch shared backend library. This function is
2370Sstevel@tonic-gate  * called before returning control back to nsswitch.
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*ARGSUSED1*/
2410Sstevel@tonic-gate nss_status_t
2420Sstevel@tonic-gate _nss_ldap_destr(ldap_backend_ptr be, void *a)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate #ifdef DEBUG
2460Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[ldap_common.c: _nss_ldap_destr]\n");
2470Sstevel@tonic-gate #endif /* DEBUG */
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	(void) _clean_ldap_backend(be);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	return ((nss_status_t)NSS_SUCCESS);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate  * _nss_ldap_setent called before _nss_ldap_getent. This function is
2570Sstevel@tonic-gate  * required by POSIX.
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate nss_status_t
2610Sstevel@tonic-gate _nss_ldap_setent(ldap_backend_ptr be, void *a)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	struct gettablefilter	*gtf;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate #ifdef DEBUG
2660Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[ldap_common.c: _nss_ldap_setent]\n");
2670Sstevel@tonic-gate #endif /* DEBUG */
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (be->setcalled == 1)
2700Sstevel@tonic-gate 		(void) _nss_ldap_endent(be, a);
2710Sstevel@tonic-gate 	be->filter = NULL;
2720Sstevel@tonic-gate 	for (gtf = gettablefilterent; gtf->tablename != (char *)NULL; gtf++) {
2730Sstevel@tonic-gate 		if (strcmp(gtf->tablename, be->tablename))
2740Sstevel@tonic-gate 			continue;
2750Sstevel@tonic-gate 		be->filter = (char *)gtf->tablefilter;
2760Sstevel@tonic-gate 		break;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	be->setcalled = 1;
2800Sstevel@tonic-gate 	be->enumcookie = NULL;
2810Sstevel@tonic-gate 	be->result = NULL;
2820Sstevel@tonic-gate 	be->services_cookie = NULL;
2830Sstevel@tonic-gate 	return ((nss_status_t)NSS_SUCCESS);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * _nss_ldap_endent called after _nss_ldap_getent. This function is
2890Sstevel@tonic-gate  * required by POSIX.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*ARGSUSED1*/
2930Sstevel@tonic-gate nss_status_t
2940Sstevel@tonic-gate _nss_ldap_endent(ldap_backend_ptr be, void *a)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	ns_ldap_error_t	*error = NULL;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate #ifdef DEBUG
2990Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[ldap_common.c: _nss_ldap_endent]\n");
3000Sstevel@tonic-gate #endif /* DEBUG */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	be->setcalled = 0;
3030Sstevel@tonic-gate 	be->filter = NULL;
3040Sstevel@tonic-gate 	if (be->enumcookie != NULL) {
3050Sstevel@tonic-gate 		(void) __ns_ldap_endEntry(&be->enumcookie, &error);
3060Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&error);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 	if (be->result != NULL) {
3090Sstevel@tonic-gate 		(void) __ns_ldap_freeResult(&be->result);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 	if (be->services_cookie != NULL) {
3120Sstevel@tonic-gate 		_nss_services_cookie_free((void **)&be->services_cookie);
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	return ((nss_status_t)NSS_SUCCESS);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  *
3210Sstevel@tonic-gate  */
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate nss_status_t
3240Sstevel@tonic-gate _nss_ldap_getent(ldap_backend_ptr be, void *a)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
3270Sstevel@tonic-gate 	ns_ldap_error_t	*error = NULL;
3280Sstevel@tonic-gate 	int		parsestat = 0;
3290Sstevel@tonic-gate 	int		retcode = 0;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate #ifdef	DEBUG
3320Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[ldap_common.c: _nss_ldap_getent]\n");
3330Sstevel@tonic-gate #endif	/* DEBUG */
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (be->setcalled == 0)
3360Sstevel@tonic-gate 		(void) _nss_ldap_setent(be, a);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate next_entry:
3390Sstevel@tonic-gate 	if (be->enumcookie == NULL) {
3400Sstevel@tonic-gate 		retcode = __ns_ldap_firstEntry(be->tablename,
3410Sstevel@tonic-gate 		be->filter, _merge_SSD_filter, be->attrs, NULL,
3420Sstevel@tonic-gate 		0, &be->enumcookie,
3430Sstevel@tonic-gate 		&be->result, &error, _F_GETENT_SSD);
3440Sstevel@tonic-gate 	} else {
3450Sstevel@tonic-gate 		if (be->services_cookie == NULL) {
3460Sstevel@tonic-gate 			retcode = __ns_ldap_nextEntry(be->enumcookie,
3470Sstevel@tonic-gate 				&be->result, &error);
3480Sstevel@tonic-gate 		}
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	if (retcode != NS_LDAP_SUCCESS) {
3510Sstevel@tonic-gate 		retcode = switch_err(retcode, error);
3520Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&error);
3530Sstevel@tonic-gate 		(void) _nss_ldap_endent(be, a);
3540Sstevel@tonic-gate 		return (retcode);
3550Sstevel@tonic-gate 	} else {
3560Sstevel@tonic-gate 		if ((parsestat = be->ldapobj2ent(be, argp))
3570Sstevel@tonic-gate 			== NSS_STR_PARSE_SUCCESS) {
3580Sstevel@tonic-gate 			be->result = NULL;
3590Sstevel@tonic-gate 			argp->returnval = argp->buf.result;
3600Sstevel@tonic-gate 			return ((nss_status_t)NSS_SUCCESS);
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 		be->result = NULL;
3630Sstevel@tonic-gate 		if (parsestat == NSS_STR_PARSE_PARSE) {
3640Sstevel@tonic-gate 			argp->returnval = 0;
3650Sstevel@tonic-gate 			(void) _nss_ldap_endent(be, a);
3660Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3670Sstevel@tonic-gate 		}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 		if (parsestat == NSS_STR_PARSE_ERANGE) {
3700Sstevel@tonic-gate 			argp->erange = 1;
3710Sstevel@tonic-gate 			(void) _nss_ldap_endent(be, a);
3720Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 		if (parsestat == NSS_STR_PARSE_NO_ADDR)
3750Sstevel@tonic-gate 			/*
3760Sstevel@tonic-gate 			 * No IPV4 address is found in the current entry.
3770Sstevel@tonic-gate 			 * It indicates that the entry contains IPV6 addresses
3780Sstevel@tonic-gate 			 * only. Instead of calling _nss_ldap_endent to
3790Sstevel@tonic-gate 			 * terminate, get next entry to continue enumeration.
3800Sstevel@tonic-gate 			 * If it returned NSS_NOTFOUND here,
3810Sstevel@tonic-gate 			 * gethostent() would return NULL
3820Sstevel@tonic-gate 			 * and the enumeration would stop prematurely.
3830Sstevel@tonic-gate 			 */
3840Sstevel@tonic-gate 			goto next_entry;
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	return ((nss_status_t)NSS_SUCCESS);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  *
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate nss_backend_t *
3960Sstevel@tonic-gate _nss_ldap_constr(ldap_backend_op_t ops[], int nops, char *tablename,
3970Sstevel@tonic-gate 		const char **attrs, fnf ldapobj2ent)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	ldap_backend_ptr	be;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate #ifdef	DEBUG
4020Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[ldap_common.c: _nss_ldap_constr]\n");
4030Sstevel@tonic-gate #endif	/* DEBUG */
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if ((be = (ldap_backend_ptr) malloc(sizeof (*be))) == 0)
4060Sstevel@tonic-gate 		return (0);
4070Sstevel@tonic-gate 	be->ops = ops;
4080Sstevel@tonic-gate 	be->nops = (nss_dbop_t)nops;
4090Sstevel@tonic-gate 	be->tablename = (char *)strdup(tablename);
4100Sstevel@tonic-gate 	be->attrs = attrs;
4110Sstevel@tonic-gate 	be->result = NULL;
4120Sstevel@tonic-gate 	be->ldapobj2ent = ldapobj2ent;
4130Sstevel@tonic-gate 	be->setcalled = 0;
4140Sstevel@tonic-gate 	be->filter = NULL;
4150Sstevel@tonic-gate 	be->enumcookie = NULL;
4160Sstevel@tonic-gate 	be->netgroup_cookie = NULL;
4170Sstevel@tonic-gate 	be->services_cookie = NULL;
4180Sstevel@tonic-gate 	be->toglue = NULL;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	return ((nss_backend_t *)be);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate  *
4260Sstevel@tonic-gate  */
4270Sstevel@tonic-gate int
4280Sstevel@tonic-gate chophostdomain(char *string, char *host, char *domain)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	char	*dot;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (string == NULL)
4330Sstevel@tonic-gate 		return (-1);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if ((dot = strchr(string, '.')) == NULL) {
4360Sstevel@tonic-gate 		return (0);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	*dot = '\0';
4390Sstevel@tonic-gate 	strcpy(host, string);
4400Sstevel@tonic-gate 	strcpy(domain, ++dot);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	return (0);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  *
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate int
4500Sstevel@tonic-gate propersubdomain(char *domain, char *subdomain)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	int	domainlen, subdomainlen;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	/* sanity check */
4550Sstevel@tonic-gate 	if (domain == NULL || subdomain == NULL)
4560Sstevel@tonic-gate 		return (-1);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	domainlen = strlen(domain);
4590Sstevel@tonic-gate 	subdomainlen = strlen(subdomain);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	/* is afterdot a substring of domain? */
4620Sstevel@tonic-gate 	if ((strncasecmp(domain, subdomain, subdomainlen)) != 0)
4630Sstevel@tonic-gate 		return (-1);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (domainlen == subdomainlen)
4660Sstevel@tonic-gate 		return (1);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (subdomainlen > domainlen)
4690Sstevel@tonic-gate 		return (-1);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (*(domain + subdomainlen) != '.')
4720Sstevel@tonic-gate 		return (-1);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	return (1);
4750Sstevel@tonic-gate }
476