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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * 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*6ad9980eSJulian Pullen  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <libintl.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/mman.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <syslog.h>
387c478bd9Sstevel@tonic-gate #include <sys/socket.h>
397c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
427c478bd9Sstevel@tonic-gate #include <net/if.h>
437c478bd9Sstevel@tonic-gate #include <netdir.h>
447c478bd9Sstevel@tonic-gate #include <lber.h>
457c478bd9Sstevel@tonic-gate #include <ldap.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include "ns_sldap.h"
487c478bd9Sstevel@tonic-gate #include "ns_internal.h"
497c478bd9Sstevel@tonic-gate #include "ns_cache_door.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	UDP	"/dev/udp"
527c478bd9Sstevel@tonic-gate #define	MAXIFS	32
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate struct ifinfo {
557c478bd9Sstevel@tonic-gate 	struct in_addr addr, netmask;
567c478bd9Sstevel@tonic-gate };
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static ns_service_map ns_def_map[] = {
597c478bd9Sstevel@tonic-gate 	{ "passwd",	"ou=people,",		NULL },
607c478bd9Sstevel@tonic-gate 	{ "shadow",	"ou=people,",		"passwd" },
617c478bd9Sstevel@tonic-gate 	{ "user_attr",	"ou=people,",		"passwd" },
627c478bd9Sstevel@tonic-gate 	{ "audit_user",	"ou=people,",		"passwd" },
637c478bd9Sstevel@tonic-gate 	{ "group",	"ou=group,",		NULL },
647c478bd9Sstevel@tonic-gate 	{ "rpc",	"ou=rpc,",		NULL },
657c478bd9Sstevel@tonic-gate 	{ "project",	"ou=projects,",		NULL },
667c478bd9Sstevel@tonic-gate 	{ "protocols",	"ou=protocols,",	NULL },
677c478bd9Sstevel@tonic-gate 	{ "networks",	"ou=networks,",		NULL },
687c478bd9Sstevel@tonic-gate 	{ "netmasks",	"ou=networks,",		"networks" },
697c478bd9Sstevel@tonic-gate 	{ "netgroup",	"ou=netgroup,",		NULL },
707c478bd9Sstevel@tonic-gate 	{ "aliases",	"ou=aliases,",		NULL },
717c478bd9Sstevel@tonic-gate 	{ "Hosts",	"ou=Hosts,",		NULL },
727c478bd9Sstevel@tonic-gate 	{ "ipnodes",	"ou=Hosts,",		"hosts" },
737c478bd9Sstevel@tonic-gate 	{ "Services",	"ou=Services,",		NULL },
747c478bd9Sstevel@tonic-gate 	{ "bootparams",	"ou=ethers,",		"ethers" },
757c478bd9Sstevel@tonic-gate 	{ "ethers",	"ou=ethers,",		NULL },
767c478bd9Sstevel@tonic-gate 	{ "auth_attr",	"ou=SolarisAuthAttr,",	NULL },
777c478bd9Sstevel@tonic-gate 	{ "prof_attr",	"ou=SolarisProfAttr,",	NULL },
787c478bd9Sstevel@tonic-gate 	{ "exec_attr",	"ou=SolarisProfAttr,",	"prof_attr" },
797c478bd9Sstevel@tonic-gate 	{ "profile",	"ou=profile,",		NULL },
807c478bd9Sstevel@tonic-gate 	{ "printers",	"ou=printers,",		NULL },
817c478bd9Sstevel@tonic-gate 	{ "automount",	"",			NULL },
8245916cd2Sjpk 	{ "tnrhtp",	"ou=ipTnet,",		NULL },
8345916cd2Sjpk 	{ "tnrhdb",	"ou=ipTnet,",		"tnrhtp" },
847c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static char ** parseDN(const char *val, const char *service);
897c478bd9Sstevel@tonic-gate static char ** sortServerNet(char **srvlist);
907c478bd9Sstevel@tonic-gate static char ** sortServerPref(char **srvlist, char **preflist,
917c478bd9Sstevel@tonic-gate 		boolean_t flag, int version, int *error);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * FUNCTION:	s_api_printResult
957c478bd9Sstevel@tonic-gate  *	Given a ns_ldap_result structure print it.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate int
__s_api_printResult(ns_ldap_result_t * result)987c478bd9Sstevel@tonic-gate __s_api_printResult(ns_ldap_result_t *result)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	ns_ldap_entry_t	*curEntry;
1027c478bd9Sstevel@tonic-gate 	int		i, j, k = 0;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #ifdef DEBUG
1057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__s_api_printResult START\n");
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate 	(void) printf("--------------------------------------\n");
1087c478bd9Sstevel@tonic-gate 	if (result == NULL) {
1097c478bd9Sstevel@tonic-gate 		(void) printf("No result\n");
1107c478bd9Sstevel@tonic-gate 		return (0);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	(void) printf("entries_count %d\n", result->entries_count);
1137c478bd9Sstevel@tonic-gate 	curEntry = result->entry;
1147c478bd9Sstevel@tonic-gate 	for (i = 0; i < result->entries_count; i++) {
1157c478bd9Sstevel@tonic-gate 
1164a6b6ac4Schinlong 		(void) printf("entry %d has attr_count = %d \n", i,
1174a6b6ac4Schinlong 		    curEntry->attr_count);
1184a6b6ac4Schinlong 		for (j = 0; j < curEntry->attr_count; j++) {
1194a6b6ac4Schinlong 			(void) printf("entry %d has attr_pair[%d] = %s \n",
1204a6b6ac4Schinlong 			    i, j, curEntry->attr_pair[j]->attrname);
1214a6b6ac4Schinlong 			for (k = 0; k < 20 &&
1224a6b6ac4Schinlong 			    curEntry->attr_pair[j]->attrvalue[k]; k++)
1234a6b6ac4Schinlong 				(void) printf("entry %d has attr_pair[%d]->"
1244a6b6ac4Schinlong 				    "attrvalue[%d] = %s \n", i, j, k,
1254a6b6ac4Schinlong 				    curEntry->attr_pair[j]->attrvalue[k]);
1264a6b6ac4Schinlong 		}
1274a6b6ac4Schinlong 		(void) printf("\n--------------------------------------\n");
1284a6b6ac4Schinlong 		curEntry = curEntry->next;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	return (1);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_getSearchScope
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  *	Retrieve the search scope for ldap search from the config module.
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  * RETURN VALUES:	NS_LDAP_SUCCESS, NS_LDAP_CONFIG
1397c478bd9Sstevel@tonic-gate  * INPUT:		NONE
1407c478bd9Sstevel@tonic-gate  * OUTPUT:		searchScope, errorp
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate int
__s_api_getSearchScope(int * searchScope,ns_ldap_error_t ** errorp)1437c478bd9Sstevel@tonic-gate __s_api_getSearchScope(
1447c478bd9Sstevel@tonic-gate 	int *searchScope,
1457c478bd9Sstevel@tonic-gate 	ns_ldap_error_t **errorp)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	char		errmsg[MAXERROR];
1497c478bd9Sstevel@tonic-gate 	void		**paramVal = NULL;
1507c478bd9Sstevel@tonic-gate 	int		rc = 0;
1517c478bd9Sstevel@tonic-gate 	int		scope = 0;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #ifdef DEBUG
1547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__s_api_getSearchScope START\n");
1557c478bd9Sstevel@tonic-gate #endif
1567c478bd9Sstevel@tonic-gate 	if (*searchScope == 0) {
1577c478bd9Sstevel@tonic-gate 		if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_SCOPE_P,
1584a6b6ac4Schinlong 		    &paramVal, errorp)) != NS_LDAP_SUCCESS) {
1597c478bd9Sstevel@tonic-gate 			return (rc);
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		if (paramVal && *paramVal)
1627c478bd9Sstevel@tonic-gate 			scope = * (int *)(*paramVal);
1637c478bd9Sstevel@tonic-gate 		else
1647c478bd9Sstevel@tonic-gate 			scope = NS_LDAP_SCOPE_ONELEVEL;
1657c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
1667c478bd9Sstevel@tonic-gate 	} else {
1677c478bd9Sstevel@tonic-gate 		scope = *searchScope;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	switch (scope) {
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		case	NS_LDAP_SCOPE_ONELEVEL:
1737c478bd9Sstevel@tonic-gate 			*searchScope = LDAP_SCOPE_ONELEVEL;
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 		case	NS_LDAP_SCOPE_BASE:
1767c478bd9Sstevel@tonic-gate 			*searchScope = LDAP_SCOPE_BASE;
1777c478bd9Sstevel@tonic-gate 			break;
1787c478bd9Sstevel@tonic-gate 		case	NS_LDAP_SCOPE_SUBTREE:
1797c478bd9Sstevel@tonic-gate 			*searchScope = LDAP_SCOPE_SUBTREE;
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 		default:
1827c478bd9Sstevel@tonic-gate 			(void) snprintf(errmsg, sizeof (errmsg),
1834a6b6ac4Schinlong 			    gettext("Invalid search scope!"));
1847c478bd9Sstevel@tonic-gate 			MKERROR(LOG_ERR, *errorp, NS_CONFIG_FILE,
1854a6b6ac4Schinlong 			    strdup(errmsg), NS_LDAP_CONFIG);
1867c478bd9Sstevel@tonic-gate 			return (NS_LDAP_CONFIG);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * FUNCTION:	__ns_ldap_dupAuth
1947c478bd9Sstevel@tonic-gate  *
1957c478bd9Sstevel@tonic-gate  *	Duplicates an authentication structure.
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * RETURN VALUES:	copy of authp or NULL on error
1987c478bd9Sstevel@tonic-gate  * INPUT:		authp
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate ns_cred_t *
__ns_ldap_dupAuth(const ns_cred_t * authp)2017c478bd9Sstevel@tonic-gate __ns_ldap_dupAuth(const ns_cred_t *authp)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	ns_cred_t *ap;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate #ifdef DEBUG
2067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__ns_ldap_dupAuth START\n");
2077c478bd9Sstevel@tonic-gate #endif
2087c478bd9Sstevel@tonic-gate 	if (authp == NULL)
2097c478bd9Sstevel@tonic-gate 		return (NULL);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	ap = (ns_cred_t *)calloc(1, sizeof (ns_cred_t));
2127c478bd9Sstevel@tonic-gate 	if (ap == NULL)
2137c478bd9Sstevel@tonic-gate 		return (NULL);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (authp->hostcertpath) {
2167c478bd9Sstevel@tonic-gate 		ap->hostcertpath = strdup(authp->hostcertpath);
2177c478bd9Sstevel@tonic-gate 		if (ap->hostcertpath == NULL) {
2187c478bd9Sstevel@tonic-gate 			free(ap);
2197c478bd9Sstevel@tonic-gate 			return (NULL);
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	if (authp->cred.unix_cred.userID) {
2237c478bd9Sstevel@tonic-gate 		ap->cred.unix_cred.userID =
2244a6b6ac4Schinlong 		    strdup(authp->cred.unix_cred.userID);
2257c478bd9Sstevel@tonic-gate 		if (ap->cred.unix_cred.userID == NULL) {
2267c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeCred(&ap);
2277c478bd9Sstevel@tonic-gate 			return (NULL);
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	if (authp->cred.unix_cred.passwd) {
2317c478bd9Sstevel@tonic-gate 		ap->cred.unix_cred.passwd =
2324a6b6ac4Schinlong 		    strdup(authp->cred.unix_cred.passwd);
2337c478bd9Sstevel@tonic-gate 		if (ap->cred.unix_cred.passwd == NULL) {
2347c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeCred(&ap);
2357c478bd9Sstevel@tonic-gate 			return (NULL);
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	if (authp->cred.cert_cred.nickname) {
2397c478bd9Sstevel@tonic-gate 		ap->cred.cert_cred.nickname =
2404a6b6ac4Schinlong 		    strdup(authp->cred.cert_cred.nickname);
2417c478bd9Sstevel@tonic-gate 		if (ap->cred.cert_cred.nickname == NULL) {
2427c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeCred(&ap);
2437c478bd9Sstevel@tonic-gate 			return (NULL);
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	ap->auth.type = authp->auth.type;
2477c478bd9Sstevel@tonic-gate 	ap->auth.tlstype = authp->auth.tlstype;
2487c478bd9Sstevel@tonic-gate 	ap->auth.saslmech = authp->auth.saslmech;
2497c478bd9Sstevel@tonic-gate 	ap->auth.saslopt = authp->auth.saslopt;
2507c478bd9Sstevel@tonic-gate 	return (ap);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
253b57459abSJulian Pullen /*
254b57459abSJulian Pullen  * FUNCTION:	__ns_ldap_freeUnixCred
255b57459abSJulian Pullen  *
256b57459abSJulian Pullen  *	Frees all the memory associated with a UnixCred_t structure.
257b57459abSJulian Pullen  *
258b57459abSJulian Pullen  * RETURN VALUES:	NS_LDAP_INVALID_PARAM, NS_LDAP_SUCCESS
259b57459abSJulian Pullen  * INPUT:		UnixCred
260b57459abSJulian Pullen  */
261b57459abSJulian Pullen int
__ns_ldap_freeUnixCred(UnixCred_t ** credp)262b57459abSJulian Pullen __ns_ldap_freeUnixCred(UnixCred_t ** credp)
263b57459abSJulian Pullen {
264b57459abSJulian Pullen 	UnixCred_t *ap;
265b57459abSJulian Pullen 
266b57459abSJulian Pullen #ifdef DEBUG
267b57459abSJulian Pullen 	(void) fprintf(stderr, "__ns_ldap_freeUnixCred START\n");
268b57459abSJulian Pullen #endif
269b57459abSJulian Pullen 	if (credp == NULL || *credp == NULL)
270b57459abSJulian Pullen 		return (NS_LDAP_INVALID_PARAM);
271b57459abSJulian Pullen 
272b57459abSJulian Pullen 	ap = *credp;
273b57459abSJulian Pullen 	if (ap->userID) {
274b57459abSJulian Pullen 		(void) memset(ap->userID, 0, strlen(ap->userID));
275b57459abSJulian Pullen 		free(ap->userID);
276b57459abSJulian Pullen 	}
277b57459abSJulian Pullen 
278b57459abSJulian Pullen 	if (ap->passwd) {
279b57459abSJulian Pullen 		(void) memset(ap->passwd, 0, strlen(ap->passwd));
280b57459abSJulian Pullen 		free(ap->passwd);
281b57459abSJulian Pullen 	}
282b57459abSJulian Pullen 
283b57459abSJulian Pullen 	free(ap);
284b57459abSJulian Pullen 	*credp = NULL;
285b57459abSJulian Pullen 	return (NS_LDAP_SUCCESS);
286b57459abSJulian Pullen }
287b57459abSJulian Pullen 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * FUNCTION:	__ns_ldap_freeCred
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  *	Frees all the memory associated with a ns_cred_t structure.
2927c478bd9Sstevel@tonic-gate  *
293b57459abSJulian Pullen  * RETURN VALUES:	NS_LDAP_INVALID_PARAM, NS_LDAP_SUCCESS
2947c478bd9Sstevel@tonic-gate  * INPUT:		ns_cred_t
2957c478bd9Sstevel@tonic-gate  */
2967c478bd9Sstevel@tonic-gate int
__ns_ldap_freeCred(ns_cred_t ** credp)2977c478bd9Sstevel@tonic-gate __ns_ldap_freeCred(ns_cred_t ** credp)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	ns_cred_t *ap;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate #ifdef DEBUG
3027c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__ns_ldap_freeCred START\n");
3037c478bd9Sstevel@tonic-gate #endif
3047c478bd9Sstevel@tonic-gate 	if (credp == NULL || *credp == NULL)
3057c478bd9Sstevel@tonic-gate 		return (NS_LDAP_INVALID_PARAM);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	ap = *credp;
3087c478bd9Sstevel@tonic-gate 	if (ap->hostcertpath) {
3097c478bd9Sstevel@tonic-gate 		(void) memset(ap->hostcertpath, 0,
3104a6b6ac4Schinlong 		    strlen(ap->hostcertpath));
3117c478bd9Sstevel@tonic-gate 		free(ap->hostcertpath);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if (ap->cred.unix_cred.userID) {
3157c478bd9Sstevel@tonic-gate 		(void) memset(ap->cred.unix_cred.userID, 0,
3164a6b6ac4Schinlong 		    strlen(ap->cred.unix_cred.userID));
3177c478bd9Sstevel@tonic-gate 		free(ap->cred.unix_cred.userID);
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (ap->cred.unix_cred.passwd) {
3217c478bd9Sstevel@tonic-gate 		(void) memset(ap->cred.unix_cred.passwd, 0,
3224a6b6ac4Schinlong 		    strlen(ap->cred.unix_cred.passwd));
3237c478bd9Sstevel@tonic-gate 		free(ap->cred.unix_cred.passwd);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (ap->cred.cert_cred.nickname) {
3277c478bd9Sstevel@tonic-gate 		(void) memset(ap->cred.cert_cred.nickname, 0,
3284a6b6ac4Schinlong 		    strlen(ap->cred.cert_cred.nickname));
3297c478bd9Sstevel@tonic-gate 		free(ap->cred.cert_cred.nickname);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	free(ap);
3337c478bd9Sstevel@tonic-gate 	*credp = NULL;
3347c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
337e1dd0a2fSth /*
338e1dd0a2fSth  * FUNCTION:	__s_api_is_auth_matched
339e1dd0a2fSth  *
340e1dd0a2fSth  *	Compare an authentication structure.
341e1dd0a2fSth  *
342e1dd0a2fSth  * RETURN VALUES:	B_TRUE if matched, B_FALSE otherwise.
343e1dd0a2fSth  * INPUT:		auth1, auth2
344e1dd0a2fSth  */
345e1dd0a2fSth boolean_t
__s_api_is_auth_matched(const ns_cred_t * auth1,const ns_cred_t * auth2)346e1dd0a2fSth __s_api_is_auth_matched(const ns_cred_t *auth1,
347e1dd0a2fSth     const ns_cred_t *auth2)
348e1dd0a2fSth {
349e1dd0a2fSth 	if ((auth1->auth.type != auth2->auth.type) ||
350e1dd0a2fSth 	    (auth1->auth.tlstype != auth2->auth.tlstype) ||
351e1dd0a2fSth 	    (auth1->auth.saslmech != auth2->auth.saslmech) ||
352e1dd0a2fSth 	    (auth1->auth.saslopt != auth2->auth.saslopt))
353e1dd0a2fSth 		return (B_FALSE);
354e1dd0a2fSth 
355e1dd0a2fSth 	if ((((auth1->auth.type == NS_LDAP_AUTH_SASL) &&
356e1dd0a2fSth 	    ((auth1->auth.saslmech == NS_LDAP_SASL_CRAM_MD5) ||
357e1dd0a2fSth 	    (auth1->auth.saslmech == NS_LDAP_SASL_DIGEST_MD5))) ||
358e1dd0a2fSth 	    (auth1->auth.type == NS_LDAP_AUTH_SIMPLE)) &&
359e1dd0a2fSth 	    ((auth1->cred.unix_cred.userID == NULL) ||
360e1dd0a2fSth 	    (auth1->cred.unix_cred.passwd == NULL) ||
361e1dd0a2fSth 	    ((strcasecmp(auth1->cred.unix_cred.userID,
362e1dd0a2fSth 	    auth2->cred.unix_cred.userID) != 0)) ||
363e1dd0a2fSth 	    ((strcmp(auth1->cred.unix_cred.passwd,
364e1dd0a2fSth 	    auth2->cred.unix_cred.passwd) != 0))))
365e1dd0a2fSth 		return (B_FALSE);
366e1dd0a2fSth 
367e1dd0a2fSth 	return (B_TRUE);
368e1dd0a2fSth }
369e1dd0a2fSth 
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_getDNs
3727c478bd9Sstevel@tonic-gate  *
3737c478bd9Sstevel@tonic-gate  *	Retrieves the default base dn for the given
3747c478bd9Sstevel@tonic-gate  *	service.
3757c478bd9Sstevel@tonic-gate  *
3767c478bd9Sstevel@tonic-gate  * RETURN VALUES:	NS_LDAP_SUCCESS, NS_LDAP_MEMORY, NS_LDAP_CONFIG
3777c478bd9Sstevel@tonic-gate  * INPUT:		service
3787c478bd9Sstevel@tonic-gate  * OUTPUT:		DN, error
3797c478bd9Sstevel@tonic-gate  */
3804a6b6ac4Schinlong typedef int (*pf)(const char *, char **, ns_ldap_error_t **);
3817c478bd9Sstevel@tonic-gate int
__s_api_getDNs(char *** DN,const char * service,ns_ldap_error_t ** error)3827c478bd9Sstevel@tonic-gate __s_api_getDNs(
3837c478bd9Sstevel@tonic-gate 	char *** DN,
3847c478bd9Sstevel@tonic-gate 	const char *service,
3857c478bd9Sstevel@tonic-gate 	ns_ldap_error_t ** error)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	void	**paramVal = NULL;
3897c478bd9Sstevel@tonic-gate 	char	**dns = NULL;
3907c478bd9Sstevel@tonic-gate 	int	rc = 0;
3917c478bd9Sstevel@tonic-gate 	int	i, len;
3924a6b6ac4Schinlong 	pf	prepend_auto2dn = __s_api_prepend_automountmapname_to_dn;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate #ifdef DEBUG
3957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__s_api_getDNs START\n");
3967c478bd9Sstevel@tonic-gate #endif
3977c478bd9Sstevel@tonic-gate 	if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_BASEDN_P,
3987c478bd9Sstevel@tonic-gate 	    &paramVal, error)) != NS_LDAP_SUCCESS) {
3997c478bd9Sstevel@tonic-gate 		return (rc);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 	if (!paramVal) {
4027c478bd9Sstevel@tonic-gate 		char errmsg[MAXERROR];
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		(void) snprintf(errmsg, sizeof (errmsg),
4054a6b6ac4Schinlong 		    gettext("BaseDN not defined"));
4067c478bd9Sstevel@tonic-gate 		MKERROR(LOG_ERR, *error, NS_CONFIG_FILE, strdup(errmsg),
4077c478bd9Sstevel@tonic-gate 		    NS_LDAP_CONFIG);
4087c478bd9Sstevel@tonic-gate 		return (NS_LDAP_CONFIG);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	dns = (char **)calloc(2, sizeof (char *));
4127c478bd9Sstevel@tonic-gate 	if (dns == NULL) {
4137c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
4147c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (service == NULL) {
4187c478bd9Sstevel@tonic-gate 		dns[0] = strdup((char *)*paramVal);
4197c478bd9Sstevel@tonic-gate 		if (dns[0] == NULL) {
4207c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
4217c478bd9Sstevel@tonic-gate 			free(dns);
4227c478bd9Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 	} else {
4257c478bd9Sstevel@tonic-gate 		for (i = 0; ns_def_map[i].service != NULL; i++) {
4267c478bd9Sstevel@tonic-gate 			if (strcasecmp(service,
4274a6b6ac4Schinlong 			    ns_def_map[i].service) == 0) {
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 				len = strlen((char *)*paramVal) +
4304a6b6ac4Schinlong 				    strlen(ns_def_map[i].rdn) + 1;
4317c478bd9Sstevel@tonic-gate 				dns[0] = (char *)
4324a6b6ac4Schinlong 				    calloc(len, sizeof (char));
4337c478bd9Sstevel@tonic-gate 				if (dns[0] == NULL) {
4347c478bd9Sstevel@tonic-gate 					(void) __ns_ldap_freeParam(
4354a6b6ac4Schinlong 					    &paramVal);
4367c478bd9Sstevel@tonic-gate 					free(dns);
4377c478bd9Sstevel@tonic-gate 					return (NS_LDAP_MEMORY);
4387c478bd9Sstevel@tonic-gate 				}
4397c478bd9Sstevel@tonic-gate 				(void) strcpy(dns[0],
4404a6b6ac4Schinlong 				    ns_def_map[i].rdn);
4417c478bd9Sstevel@tonic-gate 				(void) strcat(dns[0],
4424a6b6ac4Schinlong 				    (char *)*paramVal);
4437c478bd9Sstevel@tonic-gate 				break;
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 		if (ns_def_map[i].service == NULL) {
4477c478bd9Sstevel@tonic-gate 			char *p = (char *)*paramVal;
4487c478bd9Sstevel@tonic-gate 			char *buffer = NULL;
4497c478bd9Sstevel@tonic-gate 			int  buflen = 0;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 			if (strchr(service, '=') == NULL) {
4527c478bd9Sstevel@tonic-gate 			    /* automount entries */
4534a6b6ac4Schinlong 				if (strncasecmp(service, "auto_", 5) == 0) {
4544a6b6ac4Schinlong 					buffer = strdup(p);
4554a6b6ac4Schinlong 					if (!buffer) {
4564a6b6ac4Schinlong 						free(dns);
4574a6b6ac4Schinlong 						(void) __ns_ldap_freeParam(
4584a6b6ac4Schinlong 						    &paramVal);
4594a6b6ac4Schinlong 						return (NS_LDAP_MEMORY);
4604a6b6ac4Schinlong 					}
4614a6b6ac4Schinlong 					/* shorten name to avoid cstyle error */
4624a6b6ac4Schinlong 					rc = prepend_auto2dn(
4634a6b6ac4Schinlong 					    service, &buffer, error);
4644a6b6ac4Schinlong 					if (rc != NS_LDAP_SUCCESS) {
4654a6b6ac4Schinlong 						free(dns);
4664a6b6ac4Schinlong 						free(buffer);
4674a6b6ac4Schinlong 						(void) __ns_ldap_freeParam(
4684a6b6ac4Schinlong 						    &paramVal);
4694a6b6ac4Schinlong 						return (rc);
4704a6b6ac4Schinlong 					}
4714a6b6ac4Schinlong 				} else {
4727c478bd9Sstevel@tonic-gate 				/* strlen("nisMapName")+"="+","+'\0' = 13 */
4734a6b6ac4Schinlong 					buflen = strlen(service) + strlen(p) +
4744a6b6ac4Schinlong 					    13;
4754a6b6ac4Schinlong 					buffer = (char *)malloc(buflen);
4764a6b6ac4Schinlong 					if (buffer == NULL) {
4774a6b6ac4Schinlong 						free(dns);
4784a6b6ac4Schinlong 						(void) __ns_ldap_freeParam(
4794a6b6ac4Schinlong 						    &paramVal);
4804a6b6ac4Schinlong 						return (NS_LDAP_MEMORY);
4814a6b6ac4Schinlong 					}
4824a6b6ac4Schinlong 					(void) snprintf(buffer, buflen,
4834a6b6ac4Schinlong 					    "nisMapName=%s,%s", service, p);
4844a6b6ac4Schinlong 				}
4854a6b6ac4Schinlong 			} else {
4864a6b6ac4Schinlong 				buflen = strlen(service) + strlen(p) + 2;
4877c478bd9Sstevel@tonic-gate 				buffer = (char *)malloc(buflen);
4887c478bd9Sstevel@tonic-gate 				if (buffer == NULL) {
4897c478bd9Sstevel@tonic-gate 					free(dns);
4907c478bd9Sstevel@tonic-gate 					(void) __ns_ldap_freeParam(&paramVal);
4917c478bd9Sstevel@tonic-gate 					return (NS_LDAP_MEMORY);
4927c478bd9Sstevel@tonic-gate 				}
4937c478bd9Sstevel@tonic-gate 				(void) snprintf(buffer, buflen,
4944a6b6ac4Schinlong 				    "%s,%s", service, p);
4957c478bd9Sstevel@tonic-gate 			}
4967c478bd9Sstevel@tonic-gate 			dns[0] = buffer;
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeParam(&paramVal);
5017c478bd9Sstevel@tonic-gate 	*DN = dns;
5027c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_get_search_DNs_v1
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  *	Retrieves the list of search DNS from the V1 profile for the given
5087c478bd9Sstevel@tonic-gate  *	service.
5097c478bd9Sstevel@tonic-gate  *
5107c478bd9Sstevel@tonic-gate  * RETURN VALUES:	NS_LDAP_SUCCESS, NS_LDAP_MEMORY, NS_LDAP_CONFIG
5117c478bd9Sstevel@tonic-gate  * INPUT:		service
5127c478bd9Sstevel@tonic-gate  * OUTPUT:		DN, error
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate int
__s_api_get_search_DNs_v1(char *** DN,const char * service,ns_ldap_error_t ** error)5157c478bd9Sstevel@tonic-gate __s_api_get_search_DNs_v1(
5167c478bd9Sstevel@tonic-gate 	char *** DN,
5177c478bd9Sstevel@tonic-gate 	const char *service,
5187c478bd9Sstevel@tonic-gate 	ns_ldap_error_t ** error)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	void	**paramVal = NULL;
5227c478bd9Sstevel@tonic-gate 	void	**temptr = NULL;
5237c478bd9Sstevel@tonic-gate 	char	**dns = NULL;
5247c478bd9Sstevel@tonic-gate 	int	rc = 0;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_DN_P,
5277c478bd9Sstevel@tonic-gate 	    &paramVal, error)) != NS_LDAP_SUCCESS) {
5287c478bd9Sstevel@tonic-gate 		return (rc);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (service && paramVal) {
5327c478bd9Sstevel@tonic-gate 		for (temptr = paramVal; *temptr != NULL; temptr++) {
5337c478bd9Sstevel@tonic-gate 			dns = parseDN((const char *)(*temptr),
5347c478bd9Sstevel@tonic-gate 			    (const char *)service);
5357c478bd9Sstevel@tonic-gate 			if (dns != NULL)
5367c478bd9Sstevel@tonic-gate 				break;
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeParam(&paramVal);
5417c478bd9Sstevel@tonic-gate 	*DN = dns;
5427c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate  * FUNCTION:	parseDN
5477c478bd9Sstevel@tonic-gate  *
5487c478bd9Sstevel@tonic-gate  *	Parse a special formated list(val) into an array of char *.
5497c478bd9Sstevel@tonic-gate  *
5507c478bd9Sstevel@tonic-gate  * RETURN VALUE:	A char * pointer to the new list of dns.
5517c478bd9Sstevel@tonic-gate  * INPUT:		val, service
5527c478bd9Sstevel@tonic-gate  */
5537c478bd9Sstevel@tonic-gate static char **
parseDN(const char * val,const char * service)5547c478bd9Sstevel@tonic-gate parseDN(
5557c478bd9Sstevel@tonic-gate 	const char *val,
5567c478bd9Sstevel@tonic-gate 	const char *service)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	size_t		len = 0;
5607c478bd9Sstevel@tonic-gate 	size_t		slen = 0;
5617c478bd9Sstevel@tonic-gate 	char		**retVal = NULL;
5627c478bd9Sstevel@tonic-gate 	const char	*temptr;
5637c478bd9Sstevel@tonic-gate 	char		*temptr2;
5647c478bd9Sstevel@tonic-gate 	const char	*valend;
5657c478bd9Sstevel@tonic-gate 	int 		valNo = 0;
5667c478bd9Sstevel@tonic-gate 	int		valSize = 0;
5677c478bd9Sstevel@tonic-gate 	int		i;
5687c478bd9Sstevel@tonic-gate 	char		*SSD_service = NULL;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate #ifdef DEBUG
5717c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "parseDN START\n");
5727c478bd9Sstevel@tonic-gate #endif
5737c478bd9Sstevel@tonic-gate 	if (val == NULL || *val == '\0')
5747c478bd9Sstevel@tonic-gate 		return (NULL);
5757c478bd9Sstevel@tonic-gate 	if (service == NULL || *service == '\0')
5767c478bd9Sstevel@tonic-gate 		return (NULL);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	len = strlen(val);
5797c478bd9Sstevel@tonic-gate 	slen = strlen(service);
5807c478bd9Sstevel@tonic-gate 	if (strncasecmp(val, service, slen) != 0) {
5817c478bd9Sstevel@tonic-gate 		/*
5827c478bd9Sstevel@tonic-gate 		 * This routine is only called
5837c478bd9Sstevel@tonic-gate 		 * to process V1 profile and
5847c478bd9Sstevel@tonic-gate 		 * for V1 profile, map service
5857c478bd9Sstevel@tonic-gate 		 * to the corresponding SSD_service
5867c478bd9Sstevel@tonic-gate 		 * which is associated with a
5877c478bd9Sstevel@tonic-gate 		 * real container in the LDAP directory
5887c478bd9Sstevel@tonic-gate 		 * tree, e.g., map "shadow" to
5897c478bd9Sstevel@tonic-gate 		 * "password". See function
5907c478bd9Sstevel@tonic-gate 		 * __s_api_get_SSD_from_SSDtoUse_service
5917c478bd9Sstevel@tonic-gate 		 * for similar service to SSD_service
5927c478bd9Sstevel@tonic-gate 		 * mapping handling for V2 profile.
5937c478bd9Sstevel@tonic-gate 		 */
5947c478bd9Sstevel@tonic-gate 		for (i = 0; ns_def_map[i].service != NULL; i++) {
5957c478bd9Sstevel@tonic-gate 			if (ns_def_map[i].SSDtoUse_service &&
5964a6b6ac4Schinlong 			    strcasecmp(service,
5974a6b6ac4Schinlong 			    ns_def_map[i].service) == 0) {
5987c478bd9Sstevel@tonic-gate 				SSD_service =
5994a6b6ac4Schinlong 				    ns_def_map[i].SSDtoUse_service;
6007c478bd9Sstevel@tonic-gate 				break;
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		if (SSD_service == NULL)
6057c478bd9Sstevel@tonic-gate 			return (NULL);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		slen = strlen(SSD_service);
6087c478bd9Sstevel@tonic-gate 		if (strncasecmp(val, SSD_service, slen) != 0)
6097c478bd9Sstevel@tonic-gate 			return (NULL);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	temptr = val + slen;
6137c478bd9Sstevel@tonic-gate 	while (*temptr == SPACETOK || *temptr == TABTOK)
6147c478bd9Sstevel@tonic-gate 		temptr++;
6157c478bd9Sstevel@tonic-gate 	if (*temptr != COLONTOK)
6167c478bd9Sstevel@tonic-gate 		return (NULL);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	while (*temptr) {
6197c478bd9Sstevel@tonic-gate 		temptr2 = strchr(temptr, OPARATOK);
6207c478bd9Sstevel@tonic-gate 		if (temptr2 == NULL)
6217c478bd9Sstevel@tonic-gate 			break;
6227c478bd9Sstevel@tonic-gate 		temptr2++;
6237c478bd9Sstevel@tonic-gate 		temptr2 = strchr(temptr2, CPARATOK);
6247c478bd9Sstevel@tonic-gate 		if (temptr2 == NULL)
6257c478bd9Sstevel@tonic-gate 			break;
6267c478bd9Sstevel@tonic-gate 		valNo++;
6277c478bd9Sstevel@tonic-gate 		temptr = temptr2+1;
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	retVal = (char **)calloc(valNo +1, sizeof (char *));
6317c478bd9Sstevel@tonic-gate 	if (retVal == NULL)
6327c478bd9Sstevel@tonic-gate 		return (NULL);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	temptr = val;
6357c478bd9Sstevel@tonic-gate 	valend = val+len;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	for (i = 0; (i < valNo) && (temptr < valend); i++) {
6387c478bd9Sstevel@tonic-gate 		temptr = strchr(temptr, OPARATOK);
6397c478bd9Sstevel@tonic-gate 		if (temptr == NULL) {
6407c478bd9Sstevel@tonic-gate 			__s_api_free2dArray(retVal);
6417c478bd9Sstevel@tonic-gate 			return (NULL);
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 		temptr++;
6447c478bd9Sstevel@tonic-gate 		temptr2 = strchr(temptr, CPARATOK);
6457c478bd9Sstevel@tonic-gate 		if (temptr2 == NULL) {
6467c478bd9Sstevel@tonic-gate 			__s_api_free2dArray(retVal);
6477c478bd9Sstevel@tonic-gate 			return (NULL);
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 		valSize = temptr2 - temptr;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		retVal[i] = (char *)calloc(valSize + 1, sizeof (char));
6527c478bd9Sstevel@tonic-gate 		if (retVal[i] == NULL) {
6537c478bd9Sstevel@tonic-gate 			__s_api_free2dArray(retVal);
6547c478bd9Sstevel@tonic-gate 			return (NULL);
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 		(void) strncpy(retVal[i], temptr, valSize);
6577c478bd9Sstevel@tonic-gate 		retVal[i][valSize] = '\0';
6587c478bd9Sstevel@tonic-gate 		temptr = temptr2 + 1;
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	return (retVal);
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * __s_api_get_local_interfaces
6677c478bd9Sstevel@tonic-gate  *
6687c478bd9Sstevel@tonic-gate  * Returns a pointer to an array of addresses and netmasks of all interfaces
6697c478bd9Sstevel@tonic-gate  * configured on the system.
6707c478bd9Sstevel@tonic-gate  *
6717c478bd9Sstevel@tonic-gate  * NOTE: This function is very IPv4 centric.
6727c478bd9Sstevel@tonic-gate  */
6737c478bd9Sstevel@tonic-gate static struct ifinfo *
__s_api_get_local_interfaces()6747c478bd9Sstevel@tonic-gate __s_api_get_local_interfaces()
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	struct ifconf		ifc;
6777c478bd9Sstevel@tonic-gate 	struct ifreq		ifreq, *ifr;
6787c478bd9Sstevel@tonic-gate 	struct ifinfo		*localinfo;
6797c478bd9Sstevel@tonic-gate 	struct in_addr		netmask;
6807c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin;
6817c478bd9Sstevel@tonic-gate 	void			*buf = NULL;
6827c478bd9Sstevel@tonic-gate 	int			fd = 0;
6837c478bd9Sstevel@tonic-gate 	int			numifs = 0;
6847c478bd9Sstevel@tonic-gate 	int			i, n = 0;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if ((fd = open(UDP, O_RDONLY)) < 0)
6877c478bd9Sstevel@tonic-gate 		return ((struct ifinfo *)NULL);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if (ioctl(fd, SIOCGIFNUM, (char *)&numifs) < 0) {
6907c478bd9Sstevel@tonic-gate 		numifs = MAXIFS;
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	buf = malloc(numifs * sizeof (struct ifreq));
6947c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
6957c478bd9Sstevel@tonic-gate 		(void) close(fd);
6967c478bd9Sstevel@tonic-gate 		return ((struct ifinfo *)NULL);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 	ifc.ifc_len = numifs * (int)sizeof (struct ifreq);
6997c478bd9Sstevel@tonic-gate 	ifc.ifc_buf = buf;
7007c478bd9Sstevel@tonic-gate 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0) {
7017c478bd9Sstevel@tonic-gate 		(void) close(fd);
7027c478bd9Sstevel@tonic-gate 		free(buf);
7037c478bd9Sstevel@tonic-gate 		buf = NULL;
7047c478bd9Sstevel@tonic-gate 		return ((struct ifinfo *)NULL);
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 	ifr = (struct ifreq *)buf;
7077c478bd9Sstevel@tonic-gate 	numifs = ifc.ifc_len/(int)sizeof (struct ifreq);
7087c478bd9Sstevel@tonic-gate 	localinfo = (struct ifinfo *)malloc((numifs + 1) *
7097c478bd9Sstevel@tonic-gate 	    sizeof (struct ifinfo));
7107c478bd9Sstevel@tonic-gate 	if (localinfo == NULL) {
7117c478bd9Sstevel@tonic-gate 		(void) close(fd);
7127c478bd9Sstevel@tonic-gate 		free(buf);
7137c478bd9Sstevel@tonic-gate 		buf = NULL;
7147c478bd9Sstevel@tonic-gate 		return ((struct ifinfo *)NULL);
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	for (i = 0, n = numifs; n > 0; n--, ifr++) {
7187c478bd9Sstevel@tonic-gate 		uint_t ifrflags;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		ifreq = *ifr;
7217c478bd9Sstevel@tonic-gate 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifreq) < 0)
7227c478bd9Sstevel@tonic-gate 			continue;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		ifrflags = ifreq.ifr_flags;
7257c478bd9Sstevel@tonic-gate 		if (((ifrflags & IFF_UP) == 0) ||
7267c478bd9Sstevel@tonic-gate 		    (ifr->ifr_addr.sa_family != AF_INET))
7277c478bd9Sstevel@tonic-gate 			continue;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifreq) < 0)
7307c478bd9Sstevel@tonic-gate 			continue;
7317c478bd9Sstevel@tonic-gate 		netmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		if (ioctl(fd, SIOCGIFADDR, (char *)&ifreq) < 0)
7347c478bd9Sstevel@tonic-gate 			continue;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&ifreq.ifr_addr;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		localinfo[i].addr = sin->sin_addr;
7397c478bd9Sstevel@tonic-gate 		localinfo[i].netmask = netmask;
7407c478bd9Sstevel@tonic-gate 		i++;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	localinfo[i].addr.s_addr = 0;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	free(buf);
7457c478bd9Sstevel@tonic-gate 	buf = NULL;
7467c478bd9Sstevel@tonic-gate 	(void) close(fd);
7477c478bd9Sstevel@tonic-gate 	return (localinfo);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * __s_api_samenet(char *, struct ifinfo *)
7537c478bd9Sstevel@tonic-gate  *
7547c478bd9Sstevel@tonic-gate  * Returns 1 if address is on the same subnet of the array of addresses
7557c478bd9Sstevel@tonic-gate  * passed in.
7567c478bd9Sstevel@tonic-gate  *
7577c478bd9Sstevel@tonic-gate  * NOTE: This function is only valid for IPv4 addresses.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate static int
__s_api_IPv4sameNet(char * addr,struct ifinfo * ifs)7607c478bd9Sstevel@tonic-gate __s_api_IPv4sameNet(char *addr, struct ifinfo *ifs)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	int		answer = 0;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	if (addr && ifs) {
7657c478bd9Sstevel@tonic-gate 		char		*addr_raw;
7667c478bd9Sstevel@tonic-gate 		unsigned long	iaddr;
7677c478bd9Sstevel@tonic-gate 		int		i;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		if ((addr_raw = strdup(addr)) != NULL) {
7707c478bd9Sstevel@tonic-gate 			char	*s;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 			/* Remove port number. */
7737c478bd9Sstevel@tonic-gate 			if ((s = strchr(addr_raw, ':')) != NULL)
7747c478bd9Sstevel@tonic-gate 				*s = '\0';
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 			iaddr = inet_addr(addr_raw);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 			/* Loop through interface list to find match. */
7797c478bd9Sstevel@tonic-gate 			for (i = 0; ifs[i].addr.s_addr != 0; i++) {
7807c478bd9Sstevel@tonic-gate 				if ((iaddr & ifs[i].netmask.s_addr) ==
7817c478bd9Sstevel@tonic-gate 				    (ifs[i].addr.s_addr &
7827c478bd9Sstevel@tonic-gate 				    ifs[i].netmask.s_addr))
7837c478bd9Sstevel@tonic-gate 					answer++;
7847c478bd9Sstevel@tonic-gate 			}
7857c478bd9Sstevel@tonic-gate 			free(addr_raw);
7867c478bd9Sstevel@tonic-gate 		}
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	return (answer);
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate /*
7937c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_getServers
7947c478bd9Sstevel@tonic-gate  *
7957c478bd9Sstevel@tonic-gate  *	Retrieve a list of ldap servers from the config module.
7967c478bd9Sstevel@tonic-gate  *
7977c478bd9Sstevel@tonic-gate  * RETURN VALUE:	NS_LDAP_SUCCESS, NS_LDAP_CONFIG, NS_LDAP_MEMORY
7987c478bd9Sstevel@tonic-gate  * INPUT:		NONE
7997c478bd9Sstevel@tonic-gate  * OUTPUT:		servers, error
8007c478bd9Sstevel@tonic-gate  */
8017c478bd9Sstevel@tonic-gate int
__s_api_getServers(char *** servers,ns_ldap_error_t ** error)8027c478bd9Sstevel@tonic-gate __s_api_getServers(
8037c478bd9Sstevel@tonic-gate 		char *** servers,
8047c478bd9Sstevel@tonic-gate 		ns_ldap_error_t ** error)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	void	**paramVal = NULL;
8077c478bd9Sstevel@tonic-gate 	char	errmsg[MAXERROR];
8087c478bd9Sstevel@tonic-gate 	char	**sortServers = NULL;
8097c478bd9Sstevel@tonic-gate 	char	**netservers = NULL;
8107c478bd9Sstevel@tonic-gate 	int	rc = 0, err = NS_LDAP_CONFIG, version = 1;
8117c478bd9Sstevel@tonic-gate 	const 	char	*str, *str1;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate #ifdef DEBUG
8147c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__s_api_getServers START\n");
8157c478bd9Sstevel@tonic-gate #endif
8167c478bd9Sstevel@tonic-gate 	*servers = NULL;
8177c478bd9Sstevel@tonic-gate 	/* get profile version number */
8187c478bd9Sstevel@tonic-gate 	if ((rc = __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P,
8194a6b6ac4Schinlong 	    &paramVal, error)) != NS_LDAP_SUCCESS)
8207c478bd9Sstevel@tonic-gate 		return (rc);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	if (paramVal == NULL || *paramVal == NULL) {
8237c478bd9Sstevel@tonic-gate 		(void) snprintf(errmsg, sizeof (errmsg),
8244a6b6ac4Schinlong 		    gettext("No file version"));
8257c478bd9Sstevel@tonic-gate 		MKERROR(LOG_INFO, *error, NS_CONFIG_FILE, strdup(errmsg),
8264a6b6ac4Schinlong 		    NS_LDAP_CONFIG);
8277c478bd9Sstevel@tonic-gate 		return (NS_LDAP_CONFIG);
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if (strcasecmp((char *)*paramVal, NS_LDAP_VERSION_1) == 0)
8317c478bd9Sstevel@tonic-gate 		version = 1;
8327c478bd9Sstevel@tonic-gate 	else if (strcasecmp((char *)*paramVal, NS_LDAP_VERSION_2) == 0)
8337c478bd9Sstevel@tonic-gate 		version = 2;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeParam(&paramVal);
8367c478bd9Sstevel@tonic-gate 	paramVal = NULL;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	if ((rc = __ns_ldap_getParam(NS_LDAP_SERVERS_P,
8394a6b6ac4Schinlong 	    &paramVal, error)) != NS_LDAP_SUCCESS)
8407c478bd9Sstevel@tonic-gate 		return (rc);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 * For version 2, default server list could be
8447c478bd9Sstevel@tonic-gate 	 * empty.
8457c478bd9Sstevel@tonic-gate 	 */
8467c478bd9Sstevel@tonic-gate 	if ((paramVal == NULL || (char *)*paramVal == NULL) &&
8474a6b6ac4Schinlong 	    version == 1) {
8487c478bd9Sstevel@tonic-gate 		str = NULL_OR_STR(__s_api_get_configname(NS_LDAP_SERVERS_P));
8497c478bd9Sstevel@tonic-gate 		(void) snprintf(errmsg, sizeof (errmsg),
8504a6b6ac4Schinlong 		    gettext("Unable to retrieve the '%s' list"), str);
8517c478bd9Sstevel@tonic-gate 		MKERROR(LOG_WARNING, *error, NS_CONFIG_FILE, strdup(errmsg),
8524a6b6ac4Schinlong 		    NS_LDAP_CONFIG);
8537c478bd9Sstevel@tonic-gate 		return (NS_LDAP_CONFIG);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/*
8577c478bd9Sstevel@tonic-gate 	 * Get server address(es) and go through them.
8587c478bd9Sstevel@tonic-gate 	 */
8597c478bd9Sstevel@tonic-gate 	*servers = (char **)paramVal;
8607c478bd9Sstevel@tonic-gate 	paramVal = NULL;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/* Sort servers based on network. */
8637c478bd9Sstevel@tonic-gate 	if (*servers) {
8647c478bd9Sstevel@tonic-gate 		netservers = sortServerNet(*servers);
8657c478bd9Sstevel@tonic-gate 		if (netservers) {
8667c478bd9Sstevel@tonic-gate 			free(*servers);
8677c478bd9Sstevel@tonic-gate 			*servers = netservers;
8687c478bd9Sstevel@tonic-gate 		} else {
8697c478bd9Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/* Get preferred server list and sort servers based on that. */
8747c478bd9Sstevel@tonic-gate 	if ((rc = __ns_ldap_getParam(NS_LDAP_SERVER_PREF_P,
8754a6b6ac4Schinlong 	    &paramVal, error)) != NS_LDAP_SUCCESS) {
8767c478bd9Sstevel@tonic-gate 		if (*servers)
8777c478bd9Sstevel@tonic-gate 			__s_api_free2dArray(*servers);
8787c478bd9Sstevel@tonic-gate 		*servers = NULL;
8797c478bd9Sstevel@tonic-gate 		return (rc);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	if (paramVal != NULL) {
8837c478bd9Sstevel@tonic-gate 		char **prefServers;
8847c478bd9Sstevel@tonic-gate 		void **val = NULL;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 		if ((rc =  __ns_ldap_getParam(NS_LDAP_PREF_ONLY_P,
8874a6b6ac4Schinlong 		    &val, error)) != NS_LDAP_SUCCESS) {
8887c478bd9Sstevel@tonic-gate 				if (*servers)
8897c478bd9Sstevel@tonic-gate 					__s_api_free2dArray(*servers);
8907c478bd9Sstevel@tonic-gate 				*servers = NULL;
8917c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
8927c478bd9Sstevel@tonic-gate 			return (rc);
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 		prefServers = (char **)paramVal;
8967c478bd9Sstevel@tonic-gate 		paramVal = NULL;
8977c478bd9Sstevel@tonic-gate 		if (prefServers) {
8987c478bd9Sstevel@tonic-gate 			if (val != NULL && (*val) != NULL &&
8994a6b6ac4Schinlong 			    *(int *)val[0] == 1)
9007c478bd9Sstevel@tonic-gate 				sortServers = sortServerPref(*servers,
9014a6b6ac4Schinlong 				    prefServers, B_FALSE, version,
9024a6b6ac4Schinlong 				    &err);
9037c478bd9Sstevel@tonic-gate 			else
9047c478bd9Sstevel@tonic-gate 				sortServers = sortServerPref(*servers,
9054a6b6ac4Schinlong 				    prefServers, B_TRUE, version,
9064a6b6ac4Schinlong 				    &err);
9077c478bd9Sstevel@tonic-gate 			if (sortServers) {
9087c478bd9Sstevel@tonic-gate 				if (*servers)
9097c478bd9Sstevel@tonic-gate 					free(*servers);
9107c478bd9Sstevel@tonic-gate 				*servers = NULL;
9117c478bd9Sstevel@tonic-gate 				free(prefServers);
9127c478bd9Sstevel@tonic-gate 				prefServers = NULL;
9137c478bd9Sstevel@tonic-gate 				*servers = sortServers;
9147c478bd9Sstevel@tonic-gate 			} else {
9157c478bd9Sstevel@tonic-gate 				if (*servers)
9167c478bd9Sstevel@tonic-gate 					__s_api_free2dArray(*servers);
9177c478bd9Sstevel@tonic-gate 				*servers = NULL;
9187c478bd9Sstevel@tonic-gate 				__s_api_free2dArray(prefServers);
9197c478bd9Sstevel@tonic-gate 				prefServers = NULL;
9207c478bd9Sstevel@tonic-gate 			}
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&val);
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeParam(&paramVal);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	if (*servers == NULL) {
9277c478bd9Sstevel@tonic-gate 		if (err == NS_LDAP_CONFIG) {
9287c478bd9Sstevel@tonic-gate 		str = NULL_OR_STR(__s_api_get_configname(
9294a6b6ac4Schinlong 		    NS_LDAP_SERVERS_P));
9307c478bd9Sstevel@tonic-gate 		str1 = NULL_OR_STR(__s_api_get_configname(
9314a6b6ac4Schinlong 		    NS_LDAP_SERVER_PREF_P));
9327c478bd9Sstevel@tonic-gate 			(void) snprintf(errmsg, sizeof (errmsg),
9334a6b6ac4Schinlong 			    gettext("Unable to generate a new server list "
9344a6b6ac4Schinlong 			    "based on '%s' and/or '%s'"), str, str1);
9357c478bd9Sstevel@tonic-gate 			MKERROR(LOG_WARNING, *error, NS_CONFIG_FILE,
9364a6b6ac4Schinlong 			    strdup(errmsg), err);
9377c478bd9Sstevel@tonic-gate 			return (err);
9387c478bd9Sstevel@tonic-gate 		}
9397c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * FUNCTION:	sortServerNet
9487c478bd9Sstevel@tonic-gate  *	Sort the serverlist based on the distance from client as long
9497c478bd9Sstevel@tonic-gate  *	as the list only contains IPv4 addresses.  Otherwise do nothing.
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate static char **
sortServerNet(char ** srvlist)9527c478bd9Sstevel@tonic-gate sortServerNet(char **srvlist)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	int		count = 0;
9557c478bd9Sstevel@tonic-gate 	int		all = 0;
9567c478bd9Sstevel@tonic-gate 	int		ipv4only = 1;
9577c478bd9Sstevel@tonic-gate 	struct ifinfo	*ifs = __s_api_get_local_interfaces();
9587c478bd9Sstevel@tonic-gate 	char		**tsrvs;
9597c478bd9Sstevel@tonic-gate 	char		**psrvs, **retsrvs;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/* Sanity check. */
9627c478bd9Sstevel@tonic-gate 	if (srvlist == NULL || srvlist[0] == NULL)
9637c478bd9Sstevel@tonic-gate 		return (NULL);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/* Count the number of servers to sort. */
9667c478bd9Sstevel@tonic-gate 	for (count = 0; srvlist[count] != NULL; count++) {
9677c478bd9Sstevel@tonic-gate 		if (!__s_api_isipv4(srvlist[count]))
9687c478bd9Sstevel@tonic-gate 			ipv4only = 0;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 	count++;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	/* Make room for the returned list of servers. */
9737c478bd9Sstevel@tonic-gate 	retsrvs = (char **)calloc(count, sizeof (char *));
9747c478bd9Sstevel@tonic-gate 	if (retsrvs == NULL) {
9757c478bd9Sstevel@tonic-gate 		free(ifs);
9767c478bd9Sstevel@tonic-gate 		ifs = NULL;
9777c478bd9Sstevel@tonic-gate 		return (NULL);
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	retsrvs[count - 1] = NULL;
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/* Make a temporary list of servers. */
9837c478bd9Sstevel@tonic-gate 	psrvs = (char **)calloc(count, sizeof (char *));
9847c478bd9Sstevel@tonic-gate 	if (psrvs == NULL) {
9857c478bd9Sstevel@tonic-gate 		free(ifs);
9867c478bd9Sstevel@tonic-gate 		ifs = NULL;
9877c478bd9Sstevel@tonic-gate 		free(retsrvs);
9887c478bd9Sstevel@tonic-gate 		retsrvs = NULL;
9897c478bd9Sstevel@tonic-gate 		return (NULL);
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	/* Filter servers on the same subnet */
9937c478bd9Sstevel@tonic-gate 	tsrvs = srvlist;
9947c478bd9Sstevel@tonic-gate 	while (*tsrvs) {
9957c478bd9Sstevel@tonic-gate 		if (ipv4only && __s_api_IPv4sameNet(*tsrvs, ifs)) {
9967c478bd9Sstevel@tonic-gate 			psrvs[all] = *tsrvs;
9977c478bd9Sstevel@tonic-gate 			retsrvs[all++] = *(tsrvs);
9987c478bd9Sstevel@tonic-gate 		}
9997c478bd9Sstevel@tonic-gate 		tsrvs++;
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	/* Filter remaining servers. */
10037c478bd9Sstevel@tonic-gate 	tsrvs = srvlist;
10047c478bd9Sstevel@tonic-gate 	while (*tsrvs) {
10057c478bd9Sstevel@tonic-gate 		char	**ttsrvs = psrvs;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 		while (*ttsrvs) {
10087c478bd9Sstevel@tonic-gate 			if (strcmp(*tsrvs, *ttsrvs) == 0)
10097c478bd9Sstevel@tonic-gate 				break;
10107c478bd9Sstevel@tonic-gate 			ttsrvs++;
10117c478bd9Sstevel@tonic-gate 		}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		if (*ttsrvs == NULL)
10147c478bd9Sstevel@tonic-gate 			retsrvs[all++] = *(tsrvs);
10157c478bd9Sstevel@tonic-gate 		tsrvs++;
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	free(ifs);
10197c478bd9Sstevel@tonic-gate 	ifs = NULL;
10207c478bd9Sstevel@tonic-gate 	free(psrvs);
10217c478bd9Sstevel@tonic-gate 	psrvs = NULL;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	return (retsrvs);
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate  * FUNCTION:	sortServerPref
10287c478bd9Sstevel@tonic-gate  *	Sort the serverlist based on the preferred server list.
10297c478bd9Sstevel@tonic-gate  *
10307c478bd9Sstevel@tonic-gate  * The sorting algorithm works as follows:
10317c478bd9Sstevel@tonic-gate  *
10327c478bd9Sstevel@tonic-gate  * If version 1, if flag is TRUE, find all the servers in both preflist
10337c478bd9Sstevel@tonic-gate  * and srvlist, then append other servers in srvlist to this list
10347c478bd9Sstevel@tonic-gate  * and return the list.
10357c478bd9Sstevel@tonic-gate  * If flag is FALSE, just return srvlist.
10367c478bd9Sstevel@tonic-gate  * srvlist can not be empty.
10377c478bd9Sstevel@tonic-gate  *
10387c478bd9Sstevel@tonic-gate  * If version 2, append all the servers in srvlist
10397c478bd9Sstevel@tonic-gate  * but not in preflist to preflist, and return the merged list.
10407c478bd9Sstevel@tonic-gate  * If srvlist is empty, just return preflist.
10417c478bd9Sstevel@tonic-gate  * If preflist is empty, just return srvlist.
10427c478bd9Sstevel@tonic-gate  */
10437c478bd9Sstevel@tonic-gate static char **
sortServerPref(char ** srvlist,char ** preflist,boolean_t flag,int version,int * error)10447c478bd9Sstevel@tonic-gate sortServerPref(char **srvlist, char **preflist,
10457c478bd9Sstevel@tonic-gate 		boolean_t flag, int version, int *error)
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	int		i, scount = 0, pcount = 0;
10487c478bd9Sstevel@tonic-gate 	int		all = 0, dup = 0;
10497c478bd9Sstevel@tonic-gate 	char		**tsrvs;
10507c478bd9Sstevel@tonic-gate 	char		**retsrvs;
10517c478bd9Sstevel@tonic-gate 	char		**dupsrvs;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	/* Count the number of servers to sort. */
10547c478bd9Sstevel@tonic-gate 	if (srvlist && srvlist[0])
10557c478bd9Sstevel@tonic-gate 		for (i = 0; srvlist[i] != NULL; i++)
10567c478bd9Sstevel@tonic-gate 			scount++;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	/* Sanity check. */
10597c478bd9Sstevel@tonic-gate 	if (scount == 0 && version == 1) {
10607c478bd9Sstevel@tonic-gate 		*error = NS_LDAP_CONFIG;
10617c478bd9Sstevel@tonic-gate 		return (NULL);
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/* Count the number of preferred servers */
10657c478bd9Sstevel@tonic-gate 	if (preflist && preflist[0])
10667c478bd9Sstevel@tonic-gate 		for (i = 0; preflist[i] != NULL; i++)
10677c478bd9Sstevel@tonic-gate 			pcount++;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	/* Sanity check. */
10707c478bd9Sstevel@tonic-gate 	if (scount == 0 && pcount == 0) {
10717c478bd9Sstevel@tonic-gate 		*error = NS_LDAP_CONFIG;
10727c478bd9Sstevel@tonic-gate 		return (NULL);
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	/* Make room for the returned list of servers */
10767c478bd9Sstevel@tonic-gate 	retsrvs = (char **)calloc(scount + pcount + 1, sizeof (char *));
10777c478bd9Sstevel@tonic-gate 	if (retsrvs == NULL) {
10787c478bd9Sstevel@tonic-gate 		*error = NS_LDAP_MEMORY;
10797c478bd9Sstevel@tonic-gate 		return (NULL);
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	/*
10837c478bd9Sstevel@tonic-gate 	 * if the preferred server list is empty,
10847c478bd9Sstevel@tonic-gate 	 * just return a copy of the server list
10857c478bd9Sstevel@tonic-gate 	 */
10867c478bd9Sstevel@tonic-gate 	if (pcount == 0) {
10877c478bd9Sstevel@tonic-gate 		tsrvs = srvlist;
10887c478bd9Sstevel@tonic-gate 		while (*tsrvs)
10897c478bd9Sstevel@tonic-gate 			retsrvs[all++] = *(tsrvs++);
10907c478bd9Sstevel@tonic-gate 		return (retsrvs);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 	all = 0;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	/*
10957c478bd9Sstevel@tonic-gate 	 * if the server list is empty,
10967c478bd9Sstevel@tonic-gate 	 * just return a copy of the preferred server list
10977c478bd9Sstevel@tonic-gate 	 */
10987c478bd9Sstevel@tonic-gate 	if (scount == 0) {
10997c478bd9Sstevel@tonic-gate 		tsrvs = preflist;
11007c478bd9Sstevel@tonic-gate 		while (*tsrvs)
11017c478bd9Sstevel@tonic-gate 			retsrvs[all++] = *(tsrvs++);
11027c478bd9Sstevel@tonic-gate 		return (retsrvs);
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 	all = 0;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	/* Make room for the servers whose memory needs to be freed */
11077c478bd9Sstevel@tonic-gate 	dupsrvs = (char **)calloc(scount + pcount + 1, sizeof (char *));
11087c478bd9Sstevel@tonic-gate 	if (dupsrvs == NULL) {
11097c478bd9Sstevel@tonic-gate 		free(retsrvs);
11107c478bd9Sstevel@tonic-gate 		*error = NS_LDAP_MEMORY;
11117c478bd9Sstevel@tonic-gate 		return (NULL);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	/*
11157c478bd9Sstevel@tonic-gate 	 * If version 1,
11167c478bd9Sstevel@tonic-gate 	 * throw out preferred servers not on server list.
11177c478bd9Sstevel@tonic-gate 	 * If version 2, make a copy of the preferred server list.
11187c478bd9Sstevel@tonic-gate 	 */
11197c478bd9Sstevel@tonic-gate 	if (version == 1) {
11207c478bd9Sstevel@tonic-gate 		tsrvs = preflist;
11217c478bd9Sstevel@tonic-gate 		while (*tsrvs) {
11227c478bd9Sstevel@tonic-gate 			char	**ttsrvs = srvlist;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 			while (*ttsrvs) {
11257c478bd9Sstevel@tonic-gate 				if (strcmp(*tsrvs, *(ttsrvs)) == 0)
11267c478bd9Sstevel@tonic-gate 					break;
11277c478bd9Sstevel@tonic-gate 				ttsrvs++;
11287c478bd9Sstevel@tonic-gate 			}
11297c478bd9Sstevel@tonic-gate 			if (*ttsrvs != NULL)
11307c478bd9Sstevel@tonic-gate 				retsrvs[all++] = *tsrvs;
11317c478bd9Sstevel@tonic-gate 			else
11327c478bd9Sstevel@tonic-gate 				dupsrvs[dup++] = *tsrvs;
11337c478bd9Sstevel@tonic-gate 			tsrvs++;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 	} else {
11367c478bd9Sstevel@tonic-gate 		tsrvs = preflist;
11377c478bd9Sstevel@tonic-gate 		while (*tsrvs)
11387c478bd9Sstevel@tonic-gate 			retsrvs[all++] = *(tsrvs++);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 	/*
11417c478bd9Sstevel@tonic-gate 	 * If version 1,
11427c478bd9Sstevel@tonic-gate 	 * if PREF_ONLY is false, we append the non-preferred servers
11437c478bd9Sstevel@tonic-gate 	 * to bottom of list.
11447c478bd9Sstevel@tonic-gate 	 * For version 2, always append.
11457c478bd9Sstevel@tonic-gate 	 */
11467c478bd9Sstevel@tonic-gate 	if (flag == B_TRUE || version != 1) {
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		tsrvs = srvlist;
11497c478bd9Sstevel@tonic-gate 		while (*tsrvs) {
11507c478bd9Sstevel@tonic-gate 			char	**ttsrvs = preflist;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 			while (*ttsrvs) {
11537c478bd9Sstevel@tonic-gate 				if (strcmp(*tsrvs, *ttsrvs) == 0) {
11547c478bd9Sstevel@tonic-gate 					break;
11557c478bd9Sstevel@tonic-gate 				}
11567c478bd9Sstevel@tonic-gate 				ttsrvs++;
11577c478bd9Sstevel@tonic-gate 			}
11587c478bd9Sstevel@tonic-gate 			if (*ttsrvs == NULL)
11597c478bd9Sstevel@tonic-gate 				retsrvs[all++] = *tsrvs;
11607c478bd9Sstevel@tonic-gate 			else
11617c478bd9Sstevel@tonic-gate 				dupsrvs[dup++] = *tsrvs;
11627c478bd9Sstevel@tonic-gate 			tsrvs++;
11637c478bd9Sstevel@tonic-gate 		}
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	/* free memory for duplicate servers */
11677c478bd9Sstevel@tonic-gate 	if (dup) {
11687c478bd9Sstevel@tonic-gate 		for (tsrvs = dupsrvs; *tsrvs; tsrvs++)
11697c478bd9Sstevel@tonic-gate 			free(*tsrvs);
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 	free(dupsrvs);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	return (retsrvs);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
1176689c2bf4Sjanga /*
1177689c2bf4Sjanga  * FUNCTION:	__s_api_removeBadServers
1178689c2bf4Sjanga  *	Contacts the ldap cache manager for marking the
1179689c2bf4Sjanga  *	problem servers as down, so that the server is
1180689c2bf4Sjanga  *	not contacted until the TTL expires.
1181689c2bf4Sjanga  */
1182689c2bf4Sjanga void
__s_api_removeBadServers(char ** Servers)1183689c2bf4Sjanga __s_api_removeBadServers(char ** Servers)
1184689c2bf4Sjanga {
1185689c2bf4Sjanga 
1186689c2bf4Sjanga 	char	**host;
1187689c2bf4Sjanga 
1188689c2bf4Sjanga 	if (Servers == NULL)
1189689c2bf4Sjanga 		return;
1190689c2bf4Sjanga 
1191689c2bf4Sjanga 	for (host = Servers; *host != NULL; host++) {
1192689c2bf4Sjanga 		if (__s_api_removeServer(*host) < 0) {
1193689c2bf4Sjanga 			/*
1194689c2bf4Sjanga 			 * Couldn't remove server from
1195689c2bf4Sjanga 			 * server list. Log a warning.
1196689c2bf4Sjanga 			 */
1197689c2bf4Sjanga 			syslog(LOG_WARNING, "libsldap: could "
11984a6b6ac4Schinlong 			    "not remove %s from servers list", *host);
1199689c2bf4Sjanga 		}
1200689c2bf4Sjanga 	}
1201689c2bf4Sjanga }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate /*
12047c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_free2dArray
12057c478bd9Sstevel@tonic-gate  */
12067c478bd9Sstevel@tonic-gate void
__s_api_free2dArray(char ** inarray)12077c478bd9Sstevel@tonic-gate __s_api_free2dArray(char ** inarray)
12087c478bd9Sstevel@tonic-gate {
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	char	**temptr;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	if (inarray == NULL)
12137c478bd9Sstevel@tonic-gate 		return;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	for (temptr = inarray; *temptr != NULL; temptr++) {
12167c478bd9Sstevel@tonic-gate 		free(*temptr);
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 	free(inarray);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_cp2dArray
12237c478bd9Sstevel@tonic-gate  */
12247c478bd9Sstevel@tonic-gate char **
__s_api_cp2dArray(char ** inarray)12257c478bd9Sstevel@tonic-gate __s_api_cp2dArray(char **inarray)
12267c478bd9Sstevel@tonic-gate {
12277c478bd9Sstevel@tonic-gate 	char	**newarray;
12287c478bd9Sstevel@tonic-gate 	char	 **ttarray, *ret;
12297c478bd9Sstevel@tonic-gate 	int	count;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	if (inarray == NULL)
12327c478bd9Sstevel@tonic-gate 		return (NULL);
12337c478bd9Sstevel@tonic-gate 
12344a6b6ac4Schinlong 	for (count = 0; inarray[count] != NULL; count++)
12354a6b6ac4Schinlong 		;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	newarray = (char **)calloc(count + 1, sizeof (char *));
12387c478bd9Sstevel@tonic-gate 	if (newarray == NULL)
12397c478bd9Sstevel@tonic-gate 		return (NULL);
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	ttarray = newarray;
12427c478bd9Sstevel@tonic-gate 	for (; *inarray; inarray++) {
12437c478bd9Sstevel@tonic-gate 		*(ttarray++) = ret = strdup(*inarray);
12447c478bd9Sstevel@tonic-gate 		if (ret == NULL) {
12457c478bd9Sstevel@tonic-gate 			__s_api_free2dArray(newarray);
12467c478bd9Sstevel@tonic-gate 			return (NULL);
12477c478bd9Sstevel@tonic-gate 		}
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 	return (newarray);
12507c478bd9Sstevel@tonic-gate }
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate /*
12537c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_isCtrlSupported
12547c478bd9Sstevel@tonic-gate  *	Determines if the passed control is supported by the LDAP sever.
12557c478bd9Sstevel@tonic-gate  * RETURNS:	NS_LDAP_SUCCESS if yes, NS_LDAP_OP_FAIL if not.
12567c478bd9Sstevel@tonic-gate  */
12577c478bd9Sstevel@tonic-gate int
__s_api_isCtrlSupported(Connection * con,char * ctrlString)12587c478bd9Sstevel@tonic-gate __s_api_isCtrlSupported(Connection *con, char *ctrlString)
12597c478bd9Sstevel@tonic-gate {
12607c478bd9Sstevel@tonic-gate 	char		**ctrl;
12617c478bd9Sstevel@tonic-gate 	int		len;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	len = strlen(ctrlString);
12647c478bd9Sstevel@tonic-gate 	for (ctrl = con->controls; ctrl && *ctrl; ctrl++) {
12657c478bd9Sstevel@tonic-gate 		if (strncasecmp(*ctrl, ctrlString, len) == 0)
12667c478bd9Sstevel@tonic-gate 			return (NS_LDAP_SUCCESS);
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 	return (NS_LDAP_OP_FAILED);
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate /*
12727c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_toFollowReferrals
12737c478bd9Sstevel@tonic-gate  *	Determines if need to follow referral for an SLDAP API.
12747c478bd9Sstevel@tonic-gate  * RETURN VALUES:	NS_LDAP_SUCCESS, NS_LDAP_INVALID_PARAM, or
12757c478bd9Sstevel@tonic-gate  *			other rc from __ns_ldap_getParam()
12767c478bd9Sstevel@tonic-gate  * INPUT:		flags
12777c478bd9Sstevel@tonic-gate  * OUTPUT:		toFollow, errorp
12787c478bd9Sstevel@tonic-gate  */
12797c478bd9Sstevel@tonic-gate int
__s_api_toFollowReferrals(const int flags,int * toFollow,ns_ldap_error_t ** errorp)12807c478bd9Sstevel@tonic-gate __s_api_toFollowReferrals(const int flags,
12817c478bd9Sstevel@tonic-gate 	int *toFollow,
12827c478bd9Sstevel@tonic-gate 	ns_ldap_error_t **errorp)
12837c478bd9Sstevel@tonic-gate {
12847c478bd9Sstevel@tonic-gate 	void		**paramVal = NULL;
12857c478bd9Sstevel@tonic-gate 	int		rc = 0;
12867c478bd9Sstevel@tonic-gate 	int		iflags = 0;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate #ifdef DEBUG
12897c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__s_api_toFollowReferrals START\n");
12907c478bd9Sstevel@tonic-gate #endif
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	/* Either NS_LDAP_NOREF or NS_LDAP_FOLLOWREF not both */
12937c478bd9Sstevel@tonic-gate 	if ((flags & (NS_LDAP_NOREF | NS_LDAP_FOLLOWREF)) ==
12944a6b6ac4Schinlong 	    (NS_LDAP_NOREF | NS_LDAP_FOLLOWREF)) {
12957c478bd9Sstevel@tonic-gate 		return (NS_LDAP_INVALID_PARAM);
12967c478bd9Sstevel@tonic-gate 	}
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	/*
12997c478bd9Sstevel@tonic-gate 	 * if the NS_LDAP_NOREF or NS_LDAP_FOLLOWREF is set
13007c478bd9Sstevel@tonic-gate 	 * this will take precendence over the values specified
13017c478bd9Sstevel@tonic-gate 	 * in the configuration file
13027c478bd9Sstevel@tonic-gate 	 */
13037c478bd9Sstevel@tonic-gate 	if (flags & (NS_LDAP_NOREF | NS_LDAP_FOLLOWREF)) {
13047c478bd9Sstevel@tonic-gate 			iflags = flags;
13057c478bd9Sstevel@tonic-gate 	} else {
13067c478bd9Sstevel@tonic-gate 		rc = __ns_ldap_getParam(NS_LDAP_SEARCH_REF_P,
13074a6b6ac4Schinlong 		    &paramVal, errorp);
13087c478bd9Sstevel@tonic-gate 		if (rc != NS_LDAP_SUCCESS)
13097c478bd9Sstevel@tonic-gate 			return (rc);
13107c478bd9Sstevel@tonic-gate 		if (paramVal == NULL || *paramVal == NULL) {
13117c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
13127c478bd9Sstevel@tonic-gate 			if (*errorp)
13137c478bd9Sstevel@tonic-gate 				(void) __ns_ldap_freeError(errorp);
13147c478bd9Sstevel@tonic-gate 			*toFollow = TRUE;
13157c478bd9Sstevel@tonic-gate 			return (NS_LDAP_SUCCESS);
13167c478bd9Sstevel@tonic-gate 		}
13177c478bd9Sstevel@tonic-gate 		iflags = (* (int *)(*paramVal));
13187c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	if (iflags & NS_LDAP_NOREF)
13227c478bd9Sstevel@tonic-gate 		*toFollow = FALSE;
13237c478bd9Sstevel@tonic-gate 	else
13247c478bd9Sstevel@tonic-gate 		*toFollow = TRUE;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate /*
13307c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_addRefInfo
13317c478bd9Sstevel@tonic-gate  *	Insert a referral info into a referral info list.
13327c478bd9Sstevel@tonic-gate  * RETURN VALUES:	NS_LDAP_SUCCESS, NS_LDAP_MEMORY, NS_LDAP_OP_FAILED
13337c478bd9Sstevel@tonic-gate  * INPUT:		LDAP URL, pointer to the referral info list,
13347c478bd9Sstevel@tonic-gate  *                      search baseDN, search scope, search filter,
13357c478bd9Sstevel@tonic-gate  *                      previous connection
13367c478bd9Sstevel@tonic-gate  */
13377c478bd9Sstevel@tonic-gate int
__s_api_addRefInfo(ns_referral_info_t ** head,char * url,char * baseDN,int * scope,char * filter,LDAP * ld)13387c478bd9Sstevel@tonic-gate __s_api_addRefInfo(ns_referral_info_t **head, char *url,
13397c478bd9Sstevel@tonic-gate 			char *baseDN, int *scope,
13407c478bd9Sstevel@tonic-gate 			char *filter, LDAP *ld)
13417c478bd9Sstevel@tonic-gate {
13427c478bd9Sstevel@tonic-gate 	char			errmsg[MAXERROR], *tmp;
13437c478bd9Sstevel@tonic-gate 	ns_referral_info_t	*ref, *tmpref;
13447c478bd9Sstevel@tonic-gate 	LDAPURLDesc		*ludp = NULL;
13457c478bd9Sstevel@tonic-gate 	int			hostlen;
13467c478bd9Sstevel@tonic-gate 	char *ld_defhost = NULL;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate #ifdef DEBUG
13497c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__s_api_addRefInfo START\n");
13507c478bd9Sstevel@tonic-gate #endif
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	/* sanity check */
13537c478bd9Sstevel@tonic-gate 	if (head == NULL)
13547c478bd9Sstevel@tonic-gate 		return (NS_LDAP_OP_FAILED);
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	/*
13577c478bd9Sstevel@tonic-gate 	 * log error and return NS_LDAP_SUCCESS
13587c478bd9Sstevel@tonic-gate 	 * if one of the following:
13597c478bd9Sstevel@tonic-gate 	 * 1. non-LDAP URL
13607c478bd9Sstevel@tonic-gate 	 * 2. LDAP URL which can not be parsed
13617c478bd9Sstevel@tonic-gate 	 */
13627c478bd9Sstevel@tonic-gate 	if (!ldap_is_ldap_url(url) ||
13634a6b6ac4Schinlong 	    ldap_url_parse_nodn(url, &ludp) != 0) {
13647c478bd9Sstevel@tonic-gate 		(void) snprintf(errmsg, MAXERROR, "%s: %s",
13654a6b6ac4Schinlong 		    gettext("Invalid or non-LDAP URL when"
13664a6b6ac4Schinlong 		    " processing referrals URL"),
13674a6b6ac4Schinlong 		    url);
13687c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "libsldap: %s", errmsg);
13697c478bd9Sstevel@tonic-gate 		if (ludp)
13707c478bd9Sstevel@tonic-gate 				ldap_free_urldesc(ludp);
13717c478bd9Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	ref = (ns_referral_info_t *)calloc(1,
13754a6b6ac4Schinlong 	    sizeof (ns_referral_info_t));
13767c478bd9Sstevel@tonic-gate 	if (ref == NULL) {
13777c478bd9Sstevel@tonic-gate 		ldap_free_urldesc(ludp);
13787c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/*
13827c478bd9Sstevel@tonic-gate 	 * we do have a valid URL and we were able to parse it
13837c478bd9Sstevel@tonic-gate 	 * however, we still need to find out what hostport to
13847c478bd9Sstevel@tonic-gate 	 * use if none were provided in the LDAP URL
13857c478bd9Sstevel@tonic-gate 	 * (e.g., ldap:///...)
13867c478bd9Sstevel@tonic-gate 	 */
13877c478bd9Sstevel@tonic-gate 	if ((ludp->lud_port == 0) && (ludp->lud_host == NULL)) {
13887c478bd9Sstevel@tonic-gate 		if (ld == NULL) {
13894a6b6ac4Schinlong 			(void) snprintf(errmsg, MAXERROR, "%s: %s",
13904a6b6ac4Schinlong 			    gettext("no LDAP handle when"
13914a6b6ac4Schinlong 			    " processing referrals URL"),
13924a6b6ac4Schinlong 			    url);
13937c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "libsldap: %s", errmsg);
13947c478bd9Sstevel@tonic-gate 			ldap_free_urldesc(ludp);
13957c478bd9Sstevel@tonic-gate 			free(ref);
13967c478bd9Sstevel@tonic-gate 			return (NS_LDAP_SUCCESS);
13977c478bd9Sstevel@tonic-gate 		} else {
13987c478bd9Sstevel@tonic-gate 			(void) ldap_get_option(ld, LDAP_OPT_HOST_NAME,
13994a6b6ac4Schinlong 			    &ld_defhost);
14007c478bd9Sstevel@tonic-gate 			if (ld_defhost == NULL) {
14017c478bd9Sstevel@tonic-gate 				(void) snprintf(errmsg, MAXERROR, "%s: %s",
14024a6b6ac4Schinlong 				    gettext("not able to retrieve default "
14034a6b6ac4Schinlong 				    "host when processing "
14044a6b6ac4Schinlong 				    "referrals URL"),
14054a6b6ac4Schinlong 				    url);
14067c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, "libsldap: %s", errmsg);
14077c478bd9Sstevel@tonic-gate 				ldap_free_urldesc(ludp);
14087c478bd9Sstevel@tonic-gate 				free(ref);
14097c478bd9Sstevel@tonic-gate 				return (NS_LDAP_SUCCESS);
14107c478bd9Sstevel@tonic-gate 			} else {
14117c478bd9Sstevel@tonic-gate 				ref->refHost = strdup(ld_defhost);
14127c478bd9Sstevel@tonic-gate 				if (ref->refHost == NULL) {
14137c478bd9Sstevel@tonic-gate 					ldap_free_urldesc(ludp);
14147c478bd9Sstevel@tonic-gate 					free(ref);
14157c478bd9Sstevel@tonic-gate 					return (NS_LDAP_MEMORY);
14167c478bd9Sstevel@tonic-gate 				}
14177c478bd9Sstevel@tonic-gate 			}
14187c478bd9Sstevel@tonic-gate 		}
14197c478bd9Sstevel@tonic-gate 	} else {
14207c478bd9Sstevel@tonic-gate 		/*
14217c478bd9Sstevel@tonic-gate 		 * add 4 here:
14227c478bd9Sstevel@tonic-gate 		 * 1 for the last '\0'.
14237c478bd9Sstevel@tonic-gate 		 * 1 for host and prot separator ":"
14247c478bd9Sstevel@tonic-gate 		 * and "[" & "]" for possible ipV6 addressing
14257c478bd9Sstevel@tonic-gate 		 */
14267c478bd9Sstevel@tonic-gate 		hostlen = strlen(ludp->lud_host) +
14274a6b6ac4Schinlong 		    sizeof (MAXPORTNUMBER_STR) + 4;
14287c478bd9Sstevel@tonic-gate 		ref->refHost = (char *)malloc(hostlen);
14297c478bd9Sstevel@tonic-gate 		if (ref->refHost == NULL) {
14307c478bd9Sstevel@tonic-gate 			ldap_free_urldesc(ludp);
14317c478bd9Sstevel@tonic-gate 			free(ref);
14327c478bd9Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 		if (ludp->lud_port != 0) {
14367c478bd9Sstevel@tonic-gate 			/*
14377c478bd9Sstevel@tonic-gate 			 * serverAddr = host:port
14387c478bd9Sstevel@tonic-gate 			 * or
14397c478bd9Sstevel@tonic-gate 			 * if host is an IPV6 address
14407c478bd9Sstevel@tonic-gate 			 * [host]:port
14417c478bd9Sstevel@tonic-gate 			 */
14427c478bd9Sstevel@tonic-gate 			tmp = strstr(url, ludp->lud_host);
14437c478bd9Sstevel@tonic-gate 			if (tmp && (tmp > url) && *(tmp - 1) == '[') {
14447c478bd9Sstevel@tonic-gate 				(void) snprintf(ref->refHost, hostlen,
14454a6b6ac4Schinlong 				    "[%s]:%d",
14464a6b6ac4Schinlong 				    ludp->lud_host,
14474a6b6ac4Schinlong 				    ludp->lud_port);
14487c478bd9Sstevel@tonic-gate 			} else {
14497c478bd9Sstevel@tonic-gate 				(void) snprintf(ref->refHost, hostlen,
14504a6b6ac4Schinlong 				    "%s:%d",
14514a6b6ac4Schinlong 				    ludp->lud_host,
14524a6b6ac4Schinlong 				    ludp->lud_port);
14537c478bd9Sstevel@tonic-gate 			}
14547c478bd9Sstevel@tonic-gate 		} else {
14557c478bd9Sstevel@tonic-gate 			/* serverAddr = host */
14567c478bd9Sstevel@tonic-gate 			(void) snprintf(ref->refHost, hostlen, "%s",
14574a6b6ac4Schinlong 			    ludp->lud_host);
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (ludp->lud_dn) {
14627c478bd9Sstevel@tonic-gate 		ref->refDN = strdup(ludp->lud_dn);
14637c478bd9Sstevel@tonic-gate 		if (ref->refDN == NULL) {
14647c478bd9Sstevel@tonic-gate 			ldap_free_urldesc(ludp);
14657c478bd9Sstevel@tonic-gate 			free(ref->refHost);
14667c478bd9Sstevel@tonic-gate 			free(ref);
14677c478bd9Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
14687c478bd9Sstevel@tonic-gate 		}
14697c478bd9Sstevel@tonic-gate 	} else {
14707c478bd9Sstevel@tonic-gate 		if (baseDN) {
14717c478bd9Sstevel@tonic-gate 			ref->refDN = strdup(baseDN);
14727c478bd9Sstevel@tonic-gate 			if (ref->refDN == NULL) {
14737c478bd9Sstevel@tonic-gate 				ldap_free_urldesc(ludp);
14747c478bd9Sstevel@tonic-gate 				free(ref->refHost);
14757c478bd9Sstevel@tonic-gate 				free(ref);
14767c478bd9Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
14777c478bd9Sstevel@tonic-gate 			}
14787c478bd9Sstevel@tonic-gate 		}
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	if (filter)
14827c478bd9Sstevel@tonic-gate 		ref->refFilter = strdup(filter);
14837c478bd9Sstevel@tonic-gate 	else if (ludp->lud_filter)
14847c478bd9Sstevel@tonic-gate 		ref->refFilter = strdup(ludp->lud_filter);
14857c478bd9Sstevel@tonic-gate 	else
14867c478bd9Sstevel@tonic-gate 		ref->refFilter = strdup("");
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	if (ref->refFilter == NULL) {
14897c478bd9Sstevel@tonic-gate 		ldap_free_urldesc(ludp);
14907c478bd9Sstevel@tonic-gate 		free(ref->refHost);
14917c478bd9Sstevel@tonic-gate 		if (ref->refDN)
14927c478bd9Sstevel@tonic-gate 			free(ref->refDN);
14937c478bd9Sstevel@tonic-gate 		free(ref);
14947c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 
1497*6ad9980eSJulian Pullen 	/*
1498*6ad9980eSJulian Pullen 	 * If the scope is specified in the URL use it.
1499*6ad9980eSJulian Pullen 	 * Note if the scope is missing in the URL, ldap_url_parse_nodn()
1500*6ad9980eSJulian Pullen 	 * returns the scope BASE. We need to check that the scope of BASE
1501*6ad9980eSJulian Pullen 	 * is actually present in the URL.
1502*6ad9980eSJulian Pullen 	 * If the scope is missing in the URL then use the passed-in
1503*6ad9980eSJulian Pullen 	 * scope.
1504*6ad9980eSJulian Pullen 	 * If there is no passed-in scope, then use the scope SUBTREE.
1505*6ad9980eSJulian Pullen 	 */
1506*6ad9980eSJulian Pullen 	if (ludp->lud_dn && ludp->lud_scope != LDAP_SCOPE_BASE)
1507*6ad9980eSJulian Pullen 		ref->refScope = ludp->lud_scope;
1508*6ad9980eSJulian Pullen 	else if (ludp->lud_dn && strstr(url, "?base"))
1509*6ad9980eSJulian Pullen 		ref->refScope = LDAP_SCOPE_BASE;
1510*6ad9980eSJulian Pullen 	else if (scope)
15117c478bd9Sstevel@tonic-gate 		ref->refScope = *scope;
1512*6ad9980eSJulian Pullen 	else
1513*6ad9980eSJulian Pullen 		ref->refScope = LDAP_SCOPE_SUBTREE;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	ref->next = NULL;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	ldap_free_urldesc(ludp);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	/* insert the referral info */
15207c478bd9Sstevel@tonic-gate 	if (*head) {
15214a6b6ac4Schinlong 		for (tmpref = *head; tmpref->next; tmpref = tmpref->next)
15224a6b6ac4Schinlong 			;
15237c478bd9Sstevel@tonic-gate 		tmpref->next = ref;
15247c478bd9Sstevel@tonic-gate 	} else
15257c478bd9Sstevel@tonic-gate 		*head = ref;
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate /*
15317c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_deleteRefInfo
15327c478bd9Sstevel@tonic-gate  *	Delete a referral info list.
15337c478bd9Sstevel@tonic-gate  * INPUT:		pointer to the referral info list
15347c478bd9Sstevel@tonic-gate  */
15357c478bd9Sstevel@tonic-gate void
__s_api_deleteRefInfo(ns_referral_info_t * head)15367c478bd9Sstevel@tonic-gate __s_api_deleteRefInfo(ns_referral_info_t *head)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate 	ns_referral_info_t	*ref, *tmp;
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate #ifdef DEBUG
15417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "__s_api_deleteRefInfo START\n");
15427c478bd9Sstevel@tonic-gate #endif
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	for (ref = head; ref; ) {
15457c478bd9Sstevel@tonic-gate 		if (ref->refHost)
15467c478bd9Sstevel@tonic-gate 			free(ref->refHost);
15477c478bd9Sstevel@tonic-gate 		if (ref->refDN)
15487c478bd9Sstevel@tonic-gate 			free(ref->refDN);
15497c478bd9Sstevel@tonic-gate 		if (ref->refFilter)
15507c478bd9Sstevel@tonic-gate 			free(ref->refFilter);
15517c478bd9Sstevel@tonic-gate 		tmp = ref->next;
15527c478bd9Sstevel@tonic-gate 		free(ref);
15537c478bd9Sstevel@tonic-gate 		ref = tmp;
15547c478bd9Sstevel@tonic-gate 	}
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate }
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate /*
15597c478bd9Sstevel@tonic-gate  * FUNCTION:	__s_api_get_SSD_from_SSDtoUse_service
15607c478bd9Sstevel@tonic-gate  *
15617c478bd9Sstevel@tonic-gate  *	Retrieves the Service Search Descriptors which should be used for
15627c478bd9Sstevel@tonic-gate  *	the given service. For example, return all the "passwd" SSDs for
15637c478bd9Sstevel@tonic-gate  *	service "shadow" if no SSD is defined for service "shadow" and
15647c478bd9Sstevel@tonic-gate  *	no filter component is defined in all the "passwd" SSDs. This idea
15657c478bd9Sstevel@tonic-gate  *	of sharing the SSDs defined for some other service is to reduce the
15667c478bd9Sstevel@tonic-gate  *	configuration complexity. For a service, which does not have its own
15677c478bd9Sstevel@tonic-gate  *	entries in the LDAP directory, SSD for it is useless, and should not
15687c478bd9Sstevel@tonic-gate  *	be set. But since this service must share the container with at least
15697c478bd9Sstevel@tonic-gate  *	one other service which does have it own entries, the SSD for
15707c478bd9Sstevel@tonic-gate  *	this other service will be shared by this service.
15717c478bd9Sstevel@tonic-gate  *	This other service is called the SSD-to-use service.
15727c478bd9Sstevel@tonic-gate  *	The static data structure, ns_def_map[], in this file
15737c478bd9Sstevel@tonic-gate  *	defines the SSD-to-use service for all the services supported.
15747c478bd9Sstevel@tonic-gate  *
15757c478bd9Sstevel@tonic-gate  * RETURN VALUES:	NS_LDAP_SUCCESS, NS_LDAP_MEMORY, NS_LDAP_INVALID_PARAM
15767c478bd9Sstevel@tonic-gate  * INPUT:		service
15777c478bd9Sstevel@tonic-gate  * OUTPUT:		*SSDlist, *errorp if error
15787c478bd9Sstevel@tonic-gate  */
15797c478bd9Sstevel@tonic-gate int
__s_api_get_SSD_from_SSDtoUse_service(const char * service,ns_ldap_search_desc_t *** SSDlist,ns_ldap_error_t ** errorp)15807c478bd9Sstevel@tonic-gate __s_api_get_SSD_from_SSDtoUse_service(const char *service,
15817c478bd9Sstevel@tonic-gate 		ns_ldap_search_desc_t ***SSDlist,
15827c478bd9Sstevel@tonic-gate 		ns_ldap_error_t **errorp)
15837c478bd9Sstevel@tonic-gate {
15847c478bd9Sstevel@tonic-gate 	int 			i, rc;
15857c478bd9Sstevel@tonic-gate 	int 			found = FALSE;
15867c478bd9Sstevel@tonic-gate 	int 			filter_found = FALSE;
15877c478bd9Sstevel@tonic-gate 	char			*SSD_service = NULL;
15887c478bd9Sstevel@tonic-gate 	char			errmsg[MAXERROR];
15897c478bd9Sstevel@tonic-gate 	ns_ldap_search_desc_t	**sdlist;
15907c478bd9Sstevel@tonic-gate 	int			auto_service = FALSE;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate #ifdef DEBUG
15937c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
15944a6b6ac4Schinlong 	    "__s_api_get_SSD_from_SSDtoUse_service START\n");
15957c478bd9Sstevel@tonic-gate #endif
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	if (SSDlist == NULL || errorp == NULL)
15987c478bd9Sstevel@tonic-gate 		return (NS_LDAP_INVALID_PARAM);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	*SSDlist = NULL;
16017c478bd9Sstevel@tonic-gate 	*errorp = NULL;
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (service == NULL)
16047c478bd9Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if (strncasecmp(service, "auto_", 5) == 0)
16077c478bd9Sstevel@tonic-gate 		auto_service = TRUE;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	/*
16107c478bd9Sstevel@tonic-gate 	 * First try to return the configured SSDs for the input server
16117c478bd9Sstevel@tonic-gate 	 */
16127c478bd9Sstevel@tonic-gate 	rc = __ns_ldap_getSearchDescriptors(service, SSDlist, errorp);
16137c478bd9Sstevel@tonic-gate 	if (rc != NS_LDAP_SUCCESS)
16147c478bd9Sstevel@tonic-gate 		return (rc);
16157c478bd9Sstevel@tonic-gate 	else {
16167c478bd9Sstevel@tonic-gate 		if (*SSDlist != NULL)
16177c478bd9Sstevel@tonic-gate 			return (NS_LDAP_SUCCESS);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
16217c478bd9Sstevel@tonic-gate 	 * If service == auto_* and SSD is not found,
16227c478bd9Sstevel@tonic-gate 	 * then try automount to see if there is an SSD
16237c478bd9Sstevel@tonic-gate 	 * for automount.
16247c478bd9Sstevel@tonic-gate 	 */
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	if (auto_service) {
16277c478bd9Sstevel@tonic-gate 		rc = __ns_ldap_getSearchDescriptors(
16284a6b6ac4Schinlong 		    "automount", SSDlist, errorp);
16297c478bd9Sstevel@tonic-gate 		if (rc != NS_LDAP_SUCCESS)
16307c478bd9Sstevel@tonic-gate 			return (rc);
16317c478bd9Sstevel@tonic-gate 		else {
16327c478bd9Sstevel@tonic-gate 			if (*SSDlist != NULL) {
16337c478bd9Sstevel@tonic-gate 				/*
16347c478bd9Sstevel@tonic-gate 				 * If SSDlist is found,
16357c478bd9Sstevel@tonic-gate 				 * prepend automountMapName to the basedn
16367c478bd9Sstevel@tonic-gate 				 * in the SSDlist
16377c478bd9Sstevel@tonic-gate 				 *
16387c478bd9Sstevel@tonic-gate 				 */
16397c478bd9Sstevel@tonic-gate 				rc = __s_api_prepend_automountmapname(
16404a6b6ac4Schinlong 				    service,
16414a6b6ac4Schinlong 				    SSDlist,
16424a6b6ac4Schinlong 				    errorp);
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 				if (rc != NS_LDAP_SUCCESS) {
16454a6b6ac4Schinlong 					(void) __ns_ldap_freeSearchDescriptors(
16464a6b6ac4Schinlong 					    SSDlist);
16474a6b6ac4Schinlong 					*SSDlist = NULL;
16487c478bd9Sstevel@tonic-gate 				}
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 				return (rc);
16517c478bd9Sstevel@tonic-gate 			}
16527c478bd9Sstevel@tonic-gate 		}
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	/*
16567c478bd9Sstevel@tonic-gate 	 * Find the SSDtoUse service.
16577c478bd9Sstevel@tonic-gate 	 * If none found, flag "found" remains FALSE.
16587c478bd9Sstevel@tonic-gate 	 */
16597c478bd9Sstevel@tonic-gate 	for (i = 0; ns_def_map[i].service != NULL; i++) {
16607c478bd9Sstevel@tonic-gate 		if (ns_def_map[i].SSDtoUse_service &&
16614a6b6ac4Schinlong 		    strcasecmp(service,
16624a6b6ac4Schinlong 		    ns_def_map[i].service) == 0) {
16637c478bd9Sstevel@tonic-gate 			found = TRUE;
16647c478bd9Sstevel@tonic-gate 			SSD_service = ns_def_map[i].SSDtoUse_service;
16657c478bd9Sstevel@tonic-gate 			break;
16667c478bd9Sstevel@tonic-gate 		}
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	if (!found)
16707c478bd9Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	/*
16737c478bd9Sstevel@tonic-gate 	 * return the SSDs for SSD_service only if no optional filter
16747c478bd9Sstevel@tonic-gate 	 * component is defined in the SSDs
16757c478bd9Sstevel@tonic-gate 	 */
16767c478bd9Sstevel@tonic-gate 	rc = __ns_ldap_getSearchDescriptors(SSD_service,
16774a6b6ac4Schinlong 	    SSDlist, errorp);
16787c478bd9Sstevel@tonic-gate 	if (rc != NS_LDAP_SUCCESS) {
16797c478bd9Sstevel@tonic-gate 		return (rc);
16807c478bd9Sstevel@tonic-gate 	} else {
16817c478bd9Sstevel@tonic-gate 		if (*SSDlist == NULL)
16827c478bd9Sstevel@tonic-gate 			return (NS_LDAP_SUCCESS);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 		/* check to see if filter defined in SSD */
16857c478bd9Sstevel@tonic-gate 		for (sdlist = *SSDlist; *sdlist; sdlist++) {
16867c478bd9Sstevel@tonic-gate 			if ((*sdlist)->filter &&
16874a6b6ac4Schinlong 			    strlen((*sdlist)->filter) > 0) {
16887c478bd9Sstevel@tonic-gate 				filter_found = TRUE;
16897c478bd9Sstevel@tonic-gate 				break;
16907c478bd9Sstevel@tonic-gate 			}
16917c478bd9Sstevel@tonic-gate 		}
16927c478bd9Sstevel@tonic-gate 		if (filter_found) {
16937c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeSearchDescriptors(SSDlist);
16947c478bd9Sstevel@tonic-gate 			*SSDlist = NULL;
16957c478bd9Sstevel@tonic-gate 			(void) snprintf(errmsg, sizeof (errmsg),
16964a6b6ac4Schinlong 			    gettext("Service search descriptor for "
16974a6b6ac4Schinlong 			    "service '%s' contains filter, "
16984a6b6ac4Schinlong 			    "which can not be used for "
16994a6b6ac4Schinlong 			    "service '%s'."),
17004a6b6ac4Schinlong 			    SSD_service, service);
17017c478bd9Sstevel@tonic-gate 			MKERROR(LOG_WARNING, *errorp, NS_CONFIG_FILE,
17024a6b6ac4Schinlong 			    strdup(errmsg), NS_LDAP_CONFIG);
17037c478bd9Sstevel@tonic-gate 			return (NS_LDAP_CONFIG);
17047c478bd9Sstevel@tonic-gate 		}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
17087c478bd9Sstevel@tonic-gate }
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate /*
17127c478bd9Sstevel@tonic-gate  * verify addr is an IPv4 address with the optional [:portno]
17137c478bd9Sstevel@tonic-gate  * RFC2373 & RFC2732 & RFC2396
17147c478bd9Sstevel@tonic-gate  */
17157c478bd9Sstevel@tonic-gate int
__s_api_isipv4(char * addr)17167c478bd9Sstevel@tonic-gate __s_api_isipv4(char *addr)
17177c478bd9Sstevel@tonic-gate {
17187c478bd9Sstevel@tonic-gate 	int i, seg, digit, port;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	if (!addr)
17217c478bd9Sstevel@tonic-gate 		return (0);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	digit = seg = port = 0;
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	for (i = 0; i < strlen(addr); i++) {
17267c478bd9Sstevel@tonic-gate 		if (isdigit(addr[i])) {
17277c478bd9Sstevel@tonic-gate 			digit++;
17287c478bd9Sstevel@tonic-gate 			continue;
17297c478bd9Sstevel@tonic-gate 		}
17307c478bd9Sstevel@tonic-gate 		if (addr[i] == '.') {
17317c478bd9Sstevel@tonic-gate 			if (digit > 3 || digit == 0)
17327c478bd9Sstevel@tonic-gate 				return (0);
17337c478bd9Sstevel@tonic-gate 			digit = 0;
17347c478bd9Sstevel@tonic-gate 			seg++;
17357c478bd9Sstevel@tonic-gate 			continue;
17367c478bd9Sstevel@tonic-gate 		}
17377c478bd9Sstevel@tonic-gate 		if (addr[i] == ':') {
17387c478bd9Sstevel@tonic-gate 			if (digit > 3)
17397c478bd9Sstevel@tonic-gate 				return (0);
17407c478bd9Sstevel@tonic-gate 			port++;
17417c478bd9Sstevel@tonic-gate 			digit = 0;
17427c478bd9Sstevel@tonic-gate 			seg++;
17437c478bd9Sstevel@tonic-gate 			continue;
17447c478bd9Sstevel@tonic-gate 		}
17457c478bd9Sstevel@tonic-gate 		return (0);
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	if ((seg == 3 && port == 0 && digit > 0 && digit < 4) ||
17494a6b6ac4Schinlong 	    (seg == 4 && port == 1 && digit > 0))
17507c478bd9Sstevel@tonic-gate 		return (1);
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	return (0);
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate /*
17577c478bd9Sstevel@tonic-gate  * verify addr is an IPv6 address with the optional [IPv6]:portno
17587c478bd9Sstevel@tonic-gate  * RFC2373 & RFC2732 & RFC2396
17597c478bd9Sstevel@tonic-gate  */
17607c478bd9Sstevel@tonic-gate int
__s_api_isipv6(char * addr)17617c478bd9Sstevel@tonic-gate __s_api_isipv6(char *addr)
17627c478bd9Sstevel@tonic-gate {
17637c478bd9Sstevel@tonic-gate 	int i, col, digit, port, dc, tc;
17647c478bd9Sstevel@tonic-gate 	char *laddr, *c1, *s;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	if (!addr)
17677c478bd9Sstevel@tonic-gate 		return (0);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	s = addr;
17707c478bd9Sstevel@tonic-gate 	laddr = NULL;
17717c478bd9Sstevel@tonic-gate 	digit = col = port = 0;
17727c478bd9Sstevel@tonic-gate 	if (addr[0] == '[') {
17737c478bd9Sstevel@tonic-gate 		laddr = strdup(addr);
17747c478bd9Sstevel@tonic-gate 		if (!laddr)
17757c478bd9Sstevel@tonic-gate 			return (0);
17767c478bd9Sstevel@tonic-gate 		c1 = strchr(laddr, ']');
17777c478bd9Sstevel@tonic-gate 		/* only 1 ']' should be in an addr */
17787c478bd9Sstevel@tonic-gate 		if (!c1 || (strchr(c1+1, ']')))
17797c478bd9Sstevel@tonic-gate 			goto bad;
17807c478bd9Sstevel@tonic-gate 		switch (c1[1]) {
17817c478bd9Sstevel@tonic-gate 			case ':':
17827c478bd9Sstevel@tonic-gate 				port++;
17837c478bd9Sstevel@tonic-gate 				for (i = 2; i < strlen(c1); i++) {
17847c478bd9Sstevel@tonic-gate 					if (!isdigit(c1[i]))
17857c478bd9Sstevel@tonic-gate 						goto bad;
17867c478bd9Sstevel@tonic-gate 					digit++;
17877c478bd9Sstevel@tonic-gate 				}
17887c478bd9Sstevel@tonic-gate 				if (!digit)
17897c478bd9Sstevel@tonic-gate 					goto bad;
17907c478bd9Sstevel@tonic-gate 				c1[0] = '\0';
17917c478bd9Sstevel@tonic-gate 				break;
17927c478bd9Sstevel@tonic-gate 			case '\0':
17937c478bd9Sstevel@tonic-gate 				c1[0] = '\0';
17947c478bd9Sstevel@tonic-gate 				break;
17957c478bd9Sstevel@tonic-gate 			default:
17967c478bd9Sstevel@tonic-gate 				goto bad;
17977c478bd9Sstevel@tonic-gate 		}
17987c478bd9Sstevel@tonic-gate 		s = &laddr[1];
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	digit = dc = tc = 0;
18027c478bd9Sstevel@tonic-gate 	for (i = 0; i < strlen(s); i++) {
18037c478bd9Sstevel@tonic-gate 		if (isxdigit(s[i])) {
18047c478bd9Sstevel@tonic-gate 			if (digit == 0)
18057c478bd9Sstevel@tonic-gate 				dc = i;
18067c478bd9Sstevel@tonic-gate 			digit++;
18077c478bd9Sstevel@tonic-gate 			col = 0;
18087c478bd9Sstevel@tonic-gate 			continue;
18097c478bd9Sstevel@tonic-gate 		}
18107c478bd9Sstevel@tonic-gate 		if (s[i] == ':') {
18117c478bd9Sstevel@tonic-gate 			tc++;
18127c478bd9Sstevel@tonic-gate 			if ((col > 1) || (i && !col && !digit))
18137c478bd9Sstevel@tonic-gate 				goto bad;
18147c478bd9Sstevel@tonic-gate 			digit = 0;
18157c478bd9Sstevel@tonic-gate 			col++;
18167c478bd9Sstevel@tonic-gate 			continue;
18177c478bd9Sstevel@tonic-gate 		}
18187c478bd9Sstevel@tonic-gate 		if (s[i] == '.') {
18197c478bd9Sstevel@tonic-gate 			if (__s_api_isipv4(&s[dc]) && tc)
18207c478bd9Sstevel@tonic-gate 				goto good;
18217c478bd9Sstevel@tonic-gate 			else
18227c478bd9Sstevel@tonic-gate 				goto bad;
18237c478bd9Sstevel@tonic-gate 		}
18247c478bd9Sstevel@tonic-gate 		goto bad;
18257c478bd9Sstevel@tonic-gate 	}
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate good:
18287c478bd9Sstevel@tonic-gate 	free(laddr);
18297c478bd9Sstevel@tonic-gate 	return (1);
18307c478bd9Sstevel@tonic-gate bad:
18317c478bd9Sstevel@tonic-gate 	free(laddr);
18327c478bd9Sstevel@tonic-gate 	return (0);
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate /*
18377c478bd9Sstevel@tonic-gate  * verify addr is a valid hostname with the optional [:portno]
18387c478bd9Sstevel@tonic-gate  * RFC2373 & RFC2732 & RFC2396
18397c478bd9Sstevel@tonic-gate  */
18407c478bd9Sstevel@tonic-gate int
__s_api_ishost(char * addr)18417c478bd9Sstevel@tonic-gate __s_api_ishost(char *addr)
18427c478bd9Sstevel@tonic-gate {
18437c478bd9Sstevel@tonic-gate 	int i, seg, alpha, digit, port;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	if (!addr)
18467c478bd9Sstevel@tonic-gate 		return (0);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	alpha = digit = seg = port = 0;
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/* must start with alpha character */
18517c478bd9Sstevel@tonic-gate 	if (!isalpha(addr[0]))
18527c478bd9Sstevel@tonic-gate 		return (0);
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	for (i = 0; i < strlen(addr); i++) {
18557c478bd9Sstevel@tonic-gate 		if (isalpha(addr[i]) || (i && addr[i] == '-')) {
18567c478bd9Sstevel@tonic-gate 			alpha++;
18577c478bd9Sstevel@tonic-gate 			continue;
18587c478bd9Sstevel@tonic-gate 		}
18597c478bd9Sstevel@tonic-gate 		if (isdigit(addr[i])) {
18607c478bd9Sstevel@tonic-gate 			digit++;
18617c478bd9Sstevel@tonic-gate 			continue;
18627c478bd9Sstevel@tonic-gate 		}
18637c478bd9Sstevel@tonic-gate 		if (addr[i] == '.') {
18647c478bd9Sstevel@tonic-gate 			if (!alpha && !digit)
18657c478bd9Sstevel@tonic-gate 				return (0);
18667c478bd9Sstevel@tonic-gate 			alpha = digit = 0;
18677c478bd9Sstevel@tonic-gate 			seg++;
18687c478bd9Sstevel@tonic-gate 			continue;
18697c478bd9Sstevel@tonic-gate 		}
18707c478bd9Sstevel@tonic-gate 		if (addr[i] == ':') {
18717c478bd9Sstevel@tonic-gate 			if (!alpha && !digit)
18727c478bd9Sstevel@tonic-gate 				return (0);
18737c478bd9Sstevel@tonic-gate 			alpha = digit = 0;
18747c478bd9Sstevel@tonic-gate 			port++;
18757c478bd9Sstevel@tonic-gate 			seg++;
18767c478bd9Sstevel@tonic-gate 			continue;
18777c478bd9Sstevel@tonic-gate 		}
18787c478bd9Sstevel@tonic-gate 		return (0);
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	if ((port == 0 && (seg || alpha || digit)) ||
18824a6b6ac4Schinlong 	    (port == 1 && alpha == 0 && digit))
18837c478bd9Sstevel@tonic-gate 		return (1);
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	return (0);
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate /*
18907c478bd9Sstevel@tonic-gate  * Prepend automountMapName=auto_xxx to the basedn
18917c478bd9Sstevel@tonic-gate  * in the SSDlist
18927c478bd9Sstevel@tonic-gate  */
18937c478bd9Sstevel@tonic-gate 
__s_api_prepend_automountmapname(const char * service,ns_ldap_search_desc_t *** SSDlist,ns_ldap_error_t ** errorp)18947c478bd9Sstevel@tonic-gate int __s_api_prepend_automountmapname(
18957c478bd9Sstevel@tonic-gate 	const char *service,
18967c478bd9Sstevel@tonic-gate 	ns_ldap_search_desc_t ***SSDlist,
18977c478bd9Sstevel@tonic-gate 	ns_ldap_error_t **errorp)
18987c478bd9Sstevel@tonic-gate {
18997c478bd9Sstevel@tonic-gate 	int			i, rc;
19007c478bd9Sstevel@tonic-gate 	ns_ldap_search_desc_t	** ssdlist = NULL;
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	if (service == NULL || SSDlist == NULL || *SSDlist == NULL)
19037c478bd9Sstevel@tonic-gate 		return (NS_LDAP_INVALID_PARAM);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	ssdlist = *SSDlist;
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	for (i = 0; ssdlist[i] != NULL; i++) {
19087c478bd9Sstevel@tonic-gate 		rc = __s_api_prepend_automountmapname_to_dn(
19094a6b6ac4Schinlong 		    service, &ssdlist[i]->basedn, errorp);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 		if (rc != NS_LDAP_SUCCESS)
19127c478bd9Sstevel@tonic-gate 			return (rc);
19137c478bd9Sstevel@tonic-gate 	}
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate /*
19207c478bd9Sstevel@tonic-gate  * Prepend automountMapName=auto_xxx to the DN
19217c478bd9Sstevel@tonic-gate  * Construct a string of
19227c478bd9Sstevel@tonic-gate  * "automountMapName=auto_xxx,dn"
19237c478bd9Sstevel@tonic-gate  *
19247c478bd9Sstevel@tonic-gate  * If automountMapName is mapped to some other attribute,
19257c478bd9Sstevel@tonic-gate  * then use the mapping in the setup.
19267c478bd9Sstevel@tonic-gate  *
19277c478bd9Sstevel@tonic-gate  * If a version 1 profile is in use, use nisMapName for
19287c478bd9Sstevel@tonic-gate  * backward compatibility (i.e. "nisMapName=auto_xxx,dn").
19297c478bd9Sstevel@tonic-gate  */
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate int
__s_api_prepend_automountmapname_to_dn(const char * service,char ** dn,ns_ldap_error_t ** errorp)19327c478bd9Sstevel@tonic-gate __s_api_prepend_automountmapname_to_dn(
19337c478bd9Sstevel@tonic-gate 	const char *service,
19347c478bd9Sstevel@tonic-gate 	char **dn,
19357c478bd9Sstevel@tonic-gate 	ns_ldap_error_t **errorp)
19367c478bd9Sstevel@tonic-gate {
19377c478bd9Sstevel@tonic-gate 	int rc, len_s = 0, len_d = 0, len = 0;
19387c478bd9Sstevel@tonic-gate 	char *buffer = NULL;
19397c478bd9Sstevel@tonic-gate 	char *default_automountmapname = "automountMapName";
19407c478bd9Sstevel@tonic-gate 	char *automountmapname = NULL;
19417c478bd9Sstevel@tonic-gate 	char **mappedattrs = NULL;
19427c478bd9Sstevel@tonic-gate 	char errstr[MAXERROR];
19437c478bd9Sstevel@tonic-gate 	void **paramVal = NULL;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	if (service == NULL || dn == NULL || *dn == NULL)
19467c478bd9Sstevel@tonic-gate 		return (NS_LDAP_INVALID_PARAM);
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	rc = __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P, &paramVal, errorp);
19497c478bd9Sstevel@tonic-gate 	if (rc != NS_LDAP_SUCCESS || !paramVal || !*paramVal) {
19507c478bd9Sstevel@tonic-gate 		if (paramVal)
19517c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
19527c478bd9Sstevel@tonic-gate 		return (rc);
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 	if (strcasecmp(*paramVal, NS_LDAP_VERSION_1) == 0) {
19557c478bd9Sstevel@tonic-gate 		automountmapname = strdup("nisMapName");
19567c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
19577c478bd9Sstevel@tonic-gate 		if (automountmapname == NULL) {
19587c478bd9Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
19597c478bd9Sstevel@tonic-gate 		}
19607c478bd9Sstevel@tonic-gate 	} else {
19617c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 		/* Find mapped attribute name of auto_xxx first */
19647c478bd9Sstevel@tonic-gate 		mappedattrs = __ns_ldap_getMappedAttributes(
19654a6b6ac4Schinlong 		    service, default_automountmapname);
19667c478bd9Sstevel@tonic-gate 		/*
19677c478bd9Sstevel@tonic-gate 		 * if mapped attribute name of auto_xxx is not found,
19687c478bd9Sstevel@tonic-gate 		 * find the mapped attribute name of automount
19697c478bd9Sstevel@tonic-gate 		 */
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 		if (mappedattrs == NULL)
19727c478bd9Sstevel@tonic-gate 			mappedattrs = __ns_ldap_getMappedAttributes(
19737c478bd9Sstevel@tonic-gate 			"automount", default_automountmapname);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 		/*
19767c478bd9Sstevel@tonic-gate 		 * if mapped attr is not found, use the default automountmapname
19777c478bd9Sstevel@tonic-gate 		 */
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 		if (mappedattrs == NULL) {
19807c478bd9Sstevel@tonic-gate 			automountmapname = strdup(default_automountmapname);
19817c478bd9Sstevel@tonic-gate 			if (automountmapname == NULL)
19827c478bd9Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
19837c478bd9Sstevel@tonic-gate 		} else {
19847c478bd9Sstevel@tonic-gate 			if (mappedattrs[0] != NULL) {
19857c478bd9Sstevel@tonic-gate 				/*
19867c478bd9Sstevel@tonic-gate 				 * Copy it from the mapped attr list
19877c478bd9Sstevel@tonic-gate 				 * Assume it's 1 to 1 mapping
19887c478bd9Sstevel@tonic-gate 				 * 1 to n does not make sense
19897c478bd9Sstevel@tonic-gate 				 */
19907c478bd9Sstevel@tonic-gate 				automountmapname = strdup(mappedattrs[0]);
19917c478bd9Sstevel@tonic-gate 				__s_api_free2dArray(mappedattrs);
19927c478bd9Sstevel@tonic-gate 				if (automountmapname == NULL) {
19937c478bd9Sstevel@tonic-gate 					return (NS_LDAP_MEMORY);
19947c478bd9Sstevel@tonic-gate 				}
19957c478bd9Sstevel@tonic-gate 			} else {
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 				/*
19987c478bd9Sstevel@tonic-gate 				 * automountmapname is mapped to an empty string
19997c478bd9Sstevel@tonic-gate 				 */
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 				__s_api_free2dArray(mappedattrs);
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 				(void) sprintf(errstr,
20044a6b6ac4Schinlong 				    gettext(
20054a6b6ac4Schinlong 				    "Attribute automountMapName is "
20064a6b6ac4Schinlong 				    "mapped to an empty string.\n"));
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 				MKERROR(LOG_WARNING, *errorp, NS_CONFIG_SYNTAX,
2009e1dd0a2fSth 				    strdup(errstr), NS_LDAP_MEMORY);
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 				return (NS_LDAP_CONFIG);
20127c478bd9Sstevel@tonic-gate 			}
20137c478bd9Sstevel@tonic-gate 		}
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	len_s = strlen(service);
20177c478bd9Sstevel@tonic-gate 	len_d  = strlen(*dn);
20187c478bd9Sstevel@tonic-gate 	/* automountMapName + "=" + service + "," + dn + '\0' */
20197c478bd9Sstevel@tonic-gate 	len = strlen(automountmapname) + 1 + len_s + 1 + len_d + 1;
20207c478bd9Sstevel@tonic-gate 	buffer = (char *)malloc(len);
20217c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
20227c478bd9Sstevel@tonic-gate 		free(automountmapname);
20237c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	(void) snprintf(buffer, len, "%s=%s,%s",
20274a6b6ac4Schinlong 	    automountmapname, service, *dn);
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	buffer[len-1] = '\0';
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	free(automountmapname);
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	/* free the original dn */
20347c478bd9Sstevel@tonic-gate 	(void) free(*dn);
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	*dn = buffer;
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate /*
20427c478bd9Sstevel@tonic-gate  * Map the LDAP error code and error message from LDAP server
20437c478bd9Sstevel@tonic-gate  * to a password status used for password aging/management.
20447c478bd9Sstevel@tonic-gate  */
20457c478bd9Sstevel@tonic-gate ns_ldap_passwd_status_t
__s_api_set_passwd_status(int errnum,char * errmsg)20467c478bd9Sstevel@tonic-gate __s_api_set_passwd_status(int errnum, char *errmsg)
20477c478bd9Sstevel@tonic-gate {
2048c31b4830SSerge Dussud 	syslog(LOG_DEBUG, "libsldap: got LDAP errnum %d & message: %s ", errnum,
2049c31b4830SSerge Dussud 	    (errmsg != NULL) ? errmsg : "error msg not available");
20507c478bd9Sstevel@tonic-gate 	if (errmsg) {
20517c478bd9Sstevel@tonic-gate 		if (errnum ==
20524a6b6ac4Schinlong 		    LDAP_INVALID_CREDENTIALS) {
20537c478bd9Sstevel@tonic-gate 			/*
20547c478bd9Sstevel@tonic-gate 			 * case 1 (Bind):
20557c478bd9Sstevel@tonic-gate 			 * password expired
20567c478bd9Sstevel@tonic-gate 			 */
20577c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
20584a6b6ac4Schinlong 			    NS_PWDERR_EXPIRED))
20597c478bd9Sstevel@tonic-gate 				return (NS_PASSWD_EXPIRED);
20607c478bd9Sstevel@tonic-gate 		}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 		if (errnum ==
20634a6b6ac4Schinlong 		    LDAP_UNWILLING_TO_PERFORM) {
20647c478bd9Sstevel@tonic-gate 			/*
20657c478bd9Sstevel@tonic-gate 			 * case 1.1 (Bind):
20667c478bd9Sstevel@tonic-gate 			 * password expired
20677c478bd9Sstevel@tonic-gate 			 */
20687c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
20694a6b6ac4Schinlong 			    NS_PWDERR_EXPIRED))
20707c478bd9Sstevel@tonic-gate 				return (NS_PASSWD_EXPIRED);
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 			/*
20737c478bd9Sstevel@tonic-gate 			 * case 2 (Bind):
20747c478bd9Sstevel@tonic-gate 			 * Account inactivated
20757c478bd9Sstevel@tonic-gate 			 */
20767c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
20774a6b6ac4Schinlong 			    NS_PWDERR_ACCT_INACTIVATED))
20787c478bd9Sstevel@tonic-gate 				return (NS_PASSWD_EXPIRED);
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 			/*
20827c478bd9Sstevel@tonic-gate 			 * case 3 (Modify passwd):
20837c478bd9Sstevel@tonic-gate 			 * the user is not allow to change
20847c478bd9Sstevel@tonic-gate 			 * password; only admin can change it
20857c478bd9Sstevel@tonic-gate 			 */
20867c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
20874a6b6ac4Schinlong 			    NS_PWDERR_CHANGE_NOT_ALLOW))
20887c478bd9Sstevel@tonic-gate 				return (NS_PASSWD_CHANGE_NOT_ALLOWED);
20897c478bd9Sstevel@tonic-gate 		}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 		if (errnum ==
20924a6b6ac4Schinlong 		    LDAP_CONSTRAINT_VIOLATION) {
20937c478bd9Sstevel@tonic-gate 			/*
20947c478bd9Sstevel@tonic-gate 			 * case 4 (Bind):
20957c478bd9Sstevel@tonic-gate 			 * the user account is locked due to
20967c478bd9Sstevel@tonic-gate 			 * too many login failures.
20977c478bd9Sstevel@tonic-gate 			 */
20987c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
20994a6b6ac4Schinlong 			    NS_PWDERR_MAXTRIES))
21007c478bd9Sstevel@tonic-gate 				return (NS_PASSWD_RETRY_EXCEEDED);
21017c478bd9Sstevel@tonic-gate 			/*
21027c478bd9Sstevel@tonic-gate 			 * case 5 (Modify passwd):
21037c478bd9Sstevel@tonic-gate 			 * syntax error: the new password
21047c478bd9Sstevel@tonic-gate 			 * has length less than defined
21057c478bd9Sstevel@tonic-gate 			 * minimum
2106c31b4830SSerge Dussud 			 * Not true anymore with strong password
2107c31b4830SSerge Dussud 			 * policies on LDAP server: errmsg that
2108c31b4830SSerge Dussud 			 * contain NS_PWDERR_INVALID_SYNTAX may
2109c31b4830SSerge Dussud 			 * have different meanings.
2110c31b4830SSerge Dussud 			 * To keep compatibility with older password
2111c31b4830SSerge Dussud 			 * policy, check if errmsg is strictly equal
2112c31b4830SSerge Dussud 			 * to NS_PWDERR_INVALID_SYNTAX and if yes only,
2113c31b4830SSerge Dussud 			 * return NS_PASSWD_TOO_SHORT.
21147c478bd9Sstevel@tonic-gate 			 */
2115c31b4830SSerge Dussud 			if (strcmp(errmsg,
2116c31b4830SSerge Dussud 			    NS_PWDERR_INVALID_SYNTAX) == 0)
2117c31b4830SSerge Dussud 				return (NS_PASSWD_TOO_SHORT);
21187c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
21194a6b6ac4Schinlong 			    NS_PWDERR_INVALID_SYNTAX))
2120c31b4830SSerge Dussud 				return (NS_PASSWD_INVALID_SYNTAX);
21217c478bd9Sstevel@tonic-gate 			/*
21227c478bd9Sstevel@tonic-gate 			 * case 6 (Modify passwd):
2123c31b4830SSerge Dussud 			 * trivial password: same value as
21247c478bd9Sstevel@tonic-gate 			 * that of attribute cn, sn, or uid ...
21257c478bd9Sstevel@tonic-gate 			 */
21267c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
21274a6b6ac4Schinlong 			    NS_PWDERR_TRIVIAL_PASSWD))
21287c478bd9Sstevel@tonic-gate 				return (NS_PASSWD_INVALID_SYNTAX);
21297c478bd9Sstevel@tonic-gate 			/*
21307c478bd9Sstevel@tonic-gate 			 * case 7 (Modify passwd):
21317c478bd9Sstevel@tonic-gate 			 * re-use one of the old passwords
21327c478bd9Sstevel@tonic-gate 			 * in history list
21337c478bd9Sstevel@tonic-gate 			 */
21347c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
21354a6b6ac4Schinlong 			    NS_PWDERR_IN_HISTORY))
21367c478bd9Sstevel@tonic-gate 				return (NS_PASSWD_IN_HISTORY);
21377c478bd9Sstevel@tonic-gate 			/*
21387c478bd9Sstevel@tonic-gate 			 * case 8 (Modify passwd):
21397c478bd9Sstevel@tonic-gate 			 * password not allowed to be
21407c478bd9Sstevel@tonic-gate 			 * changed yet; within minimum
21417c478bd9Sstevel@tonic-gate 			 * age
21427c478bd9Sstevel@tonic-gate 			 */
21437c478bd9Sstevel@tonic-gate 			if (strstr(errmsg,
21444a6b6ac4Schinlong 			    NS_PWDERR_WITHIN_MIN_AGE))
21457c478bd9Sstevel@tonic-gate 				return (NS_PASSWD_WITHIN_MIN_AGE);
21467c478bd9Sstevel@tonic-gate 		}
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	return (NS_PASSWD_GOOD);
21517c478bd9Sstevel@tonic-gate }
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate /*
21547c478bd9Sstevel@tonic-gate  * Determine if the input OID list contains
21557c478bd9Sstevel@tonic-gate  * one of the password control OIDs, which are:
21567c478bd9Sstevel@tonic-gate  * LDAP_CONTROL_PWEXPIRED: 2.16.840.1.113730.3.4.4
21577c478bd9Sstevel@tonic-gate  * LDAP_CONTROL_PWEXPIRING: 2.16.840.1.113730.3.4.5.
21587c478bd9Sstevel@tonic-gate  * If yes, return 1, if no, 0.
21597c478bd9Sstevel@tonic-gate  */
21607c478bd9Sstevel@tonic-gate int
__s_api_contain_passwd_control_oid(char ** oids)21617c478bd9Sstevel@tonic-gate __s_api_contain_passwd_control_oid(char **oids)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate 	char **oid;
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	if (oids == NULL)
21667c478bd9Sstevel@tonic-gate 		return (0);
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	for (oid = oids; *oid; oid++) {
21697c478bd9Sstevel@tonic-gate 		if (strcmp(*oid, LDAP_CONTROL_PWEXPIRED) == 0 ||
21704a6b6ac4Schinlong 		    strcmp(*oid, LDAP_CONTROL_PWEXPIRING) == 0) {
21717c478bd9Sstevel@tonic-gate 			return (1);
21727c478bd9Sstevel@tonic-gate 		}
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	return (0);
21767c478bd9Sstevel@tonic-gate }
217747789246Svv 
217847789246Svv /*
217947789246Svv  * Determine if the input OID list contains LDAP V3 password less
218047789246Svv  * account management control OID, which is:
218147789246Svv  * NS_LDAP_ACCOUNT_USABLE_CONTROL:1.3.6.1.4.1.42.2.27.9.5.8
218247789246Svv  * If yes, return 1, if no, 0.
218347789246Svv  */
218447789246Svv int
__s_api_contain_account_usable_control_oid(char ** oids)218547789246Svv __s_api_contain_account_usable_control_oid(char **oids)
218647789246Svv {
218747789246Svv 	char **oid;
218847789246Svv 
218947789246Svv 	if (oids == NULL)
219047789246Svv 		return (0);
219147789246Svv 
219247789246Svv 	for (oid = oids; *oid; oid++) {
219347789246Svv 		if (strcmp(*oid, NS_LDAP_ACCOUNT_USABLE_CONTROL) == 0) {
219447789246Svv 			return (1);
219547789246Svv 		}
219647789246Svv 	}
219747789246Svv 
219847789246Svv 	return (0);
219947789246Svv }
220047789246Svv 
22017c478bd9Sstevel@tonic-gate /*
22027c478bd9Sstevel@tonic-gate  * For some databases in name switch, the name and aliases are saved
22037c478bd9Sstevel@tonic-gate  * as "cn". When the "cn" valuse are retrieved, there is no distinction
22047c478bd9Sstevel@tonic-gate  * which is  the name and which is(are) aliase(s).
22057c478bd9Sstevel@tonic-gate  * This function is to parse RDN and find the value of the "cn" and
22067c478bd9Sstevel@tonic-gate  * then find the matching value in "cn" attribute.
22077c478bd9Sstevel@tonic-gate  * Also see RFC 2307 section 5.6.
22087c478bd9Sstevel@tonic-gate  *
22097c478bd9Sstevel@tonic-gate  * Input -
22107c478bd9Sstevel@tonic-gate  *  entry:	An LDAP entry
22117c478bd9Sstevel@tonic-gate  *  attrptr:	A attribute which value appears in RDN
22127c478bd9Sstevel@tonic-gate  *		This should be "cn" for the name switch for now.
22137c478bd9Sstevel@tonic-gate  *  case_ignore:    0 Case sensitive comparison on the attribute value
22147c478bd9Sstevel@tonic-gate  *		    1 Case insensitive comparison
22157c478bd9Sstevel@tonic-gate  *
22167c478bd9Sstevel@tonic-gate  * Return -
22177c478bd9Sstevel@tonic-gate  *		The value of an attrbute which is used as canonical name
22187c478bd9Sstevel@tonic-gate  *		This is read only and the caller should not try to free it.
22197c478bd9Sstevel@tonic-gate  *		If it's a NULL, it could be either an RDN parsing error
22207c478bd9Sstevel@tonic-gate  *		or RDN value does not match any existing "cn" values.
22217c478bd9Sstevel@tonic-gate  *		e.g.
22227c478bd9Sstevel@tonic-gate  *		dn: cn=xx+ipserviceprotocol=udp,......
22237c478bd9Sstevel@tonic-gate  *		cn: aa
22247c478bd9Sstevel@tonic-gate  *		cn: bb
22257c478bd9Sstevel@tonic-gate  *
22267c478bd9Sstevel@tonic-gate  * Note:
22277c478bd9Sstevel@tonic-gate  *  Although the name switch/ldap's  rdn is in "cn=xx" or "cn=xx+..."
22287c478bd9Sstevel@tonic-gate  * format, this function makes no such assumption. If the DN
22297c478bd9Sstevel@tonic-gate  * is saved as "dn: yy=...+sn=my_canocical_name, ..", then it can still work.
22307c478bd9Sstevel@tonic-gate  * The comments use "cn" as an example only.
22317c478bd9Sstevel@tonic-gate  *
22327c478bd9Sstevel@tonic-gate  */
22337c478bd9Sstevel@tonic-gate typedef int (*cmpfunc)(const char *, const char *);
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate char *
__s_api_get_canonical_name(ns_ldap_entry_t * entry,ns_ldap_attr_t * attrptr,int case_ignore)22367c478bd9Sstevel@tonic-gate __s_api_get_canonical_name(ns_ldap_entry_t *entry, ns_ldap_attr_t *attrptr,
22377c478bd9Sstevel@tonic-gate 			int case_ignore) {
22387c478bd9Sstevel@tonic-gate 	uint_t			i;
22397c478bd9Sstevel@tonic-gate 	char			*token, *lasts, *value = NULL;
22407c478bd9Sstevel@tonic-gate 	char			**rdn = NULL, **attrs = NULL, **values = NULL;
22417c478bd9Sstevel@tonic-gate 	char			*rdn_attr_value = NULL;
22427c478bd9Sstevel@tonic-gate 	cmpfunc			cmp;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 	if (entry == NULL || attrptr == NULL)
22457c478bd9Sstevel@tonic-gate 		return (NULL);
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	/* "values" is read-only */
22487c478bd9Sstevel@tonic-gate 	if ((values = __ns_ldap_getAttr(entry, "dn")) == NULL ||
22494a6b6ac4Schinlong 	    values[0] == NULL)
22507c478bd9Sstevel@tonic-gate 		return (NULL);
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	if ((rdn = ldap_explode_dn(values[0], 0)) == NULL ||
22534a6b6ac4Schinlong 	    rdn[0] == NULL)
22547c478bd9Sstevel@tonic-gate 		return (NULL);
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 	if ((attrs = ldap_explode_rdn(rdn[0], 0)) == NULL) {
22577c478bd9Sstevel@tonic-gate 		ldap_value_free(rdn);
22587c478bd9Sstevel@tonic-gate 		return (NULL);
22597c478bd9Sstevel@tonic-gate 	}
22607c478bd9Sstevel@tonic-gate 	/* Assume the rdn is normalized */
22617c478bd9Sstevel@tonic-gate 	for (i = 0; attrs[i] != NULL; i++) {
22627c478bd9Sstevel@tonic-gate 		/* parse attribute name and value, get attribute name first */
22637c478bd9Sstevel@tonic-gate 		if ((token = strtok_r(attrs[i], "=", &lasts)) == NULL) {
22647c478bd9Sstevel@tonic-gate 			goto cleanup;
22657c478bd9Sstevel@tonic-gate 		}
22667c478bd9Sstevel@tonic-gate 		if (strcasecmp(token, attrptr->attrname) == 0) {
22677c478bd9Sstevel@tonic-gate 			/* get value */
22687c478bd9Sstevel@tonic-gate 			rdn_attr_value = lasts;
22697c478bd9Sstevel@tonic-gate 			break;
22707c478bd9Sstevel@tonic-gate 		}
22717c478bd9Sstevel@tonic-gate 	}
22727c478bd9Sstevel@tonic-gate 	if (rdn_attr_value) {
22737c478bd9Sstevel@tonic-gate 		if (case_ignore)
22747c478bd9Sstevel@tonic-gate 			cmp = strcasecmp;
22757c478bd9Sstevel@tonic-gate 		else
22767c478bd9Sstevel@tonic-gate 			cmp = strcmp;
22777c478bd9Sstevel@tonic-gate 		/*
22787c478bd9Sstevel@tonic-gate 		 * After parsing RDN and find the matching attribute in RDN,
22797c478bd9Sstevel@tonic-gate 		 * match rdn value with values in "cn".
22807c478bd9Sstevel@tonic-gate 		 */
22817c478bd9Sstevel@tonic-gate 		for (i = 0; i < attrptr->value_count; i++) {
22827c478bd9Sstevel@tonic-gate 			if (attrptr->attrvalue[i] &&
22834a6b6ac4Schinlong 			    (*cmp)(rdn_attr_value,
22844a6b6ac4Schinlong 			    attrptr->attrvalue[i]) == 0) {
22857c478bd9Sstevel@tonic-gate 				/* RDN "cn" value matches the "cn" value */
22867c478bd9Sstevel@tonic-gate 				value = attrptr->attrvalue[i];
22877c478bd9Sstevel@tonic-gate 				break;
22887c478bd9Sstevel@tonic-gate 			}
22897c478bd9Sstevel@tonic-gate 		}
22907c478bd9Sstevel@tonic-gate 	}
22917c478bd9Sstevel@tonic-gate cleanup:
22927c478bd9Sstevel@tonic-gate 	ldap_value_free(rdn);
22937c478bd9Sstevel@tonic-gate 	ldap_value_free(attrs);
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	return (value);
22967c478bd9Sstevel@tonic-gate }
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate /*
22997c478bd9Sstevel@tonic-gate  * This function requests a server to be removed from
23007c478bd9Sstevel@tonic-gate  * the cache manager maintained server list. This is
23017c478bd9Sstevel@tonic-gate  * done via the door functionality.
2302e8ac3ceaSsdussud  * Returns 0 if OK, else a negative value.
23037c478bd9Sstevel@tonic-gate  */
23047c478bd9Sstevel@tonic-gate 
2305e8ac3ceaSsdussud int
__s_api_removeServer(const char * server)23067c478bd9Sstevel@tonic-gate __s_api_removeServer(const char *server)
23077c478bd9Sstevel@tonic-gate {
23087c478bd9Sstevel@tonic-gate 	union {
23097c478bd9Sstevel@tonic-gate 		ldap_data_t	s_d;
23107c478bd9Sstevel@tonic-gate 		char		s_b[DOORBUFFERSIZE];
23117c478bd9Sstevel@tonic-gate 	} space;
23127c478bd9Sstevel@tonic-gate 
2313e1dd0a2fSth 	ns_server_info_t		r, *ret = &r;
23147c478bd9Sstevel@tonic-gate 	const char		*ireq;
23157c478bd9Sstevel@tonic-gate 	ldap_data_t		*sptr;
23167c478bd9Sstevel@tonic-gate 	int			ndata;
23177c478bd9Sstevel@tonic-gate 	int			adata;
23187c478bd9Sstevel@tonic-gate 	int			len;
2319e8ac3ceaSsdussud 	int			rc;
2320e1dd0a2fSth 	ns_ldap_error_t		*error = NULL;
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	if (server == NULL)
2323e8ac3ceaSsdussud 		return (-1);
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	ireq = NS_CACHE_NORESP;
23267c478bd9Sstevel@tonic-gate 
2327e1dd0a2fSth 	if (__s_api_isStandalone()) {
2328e1dd0a2fSth 		/*
2329e1dd0a2fSth 		 * Remove 'server' from the standalone server list.
2330e1dd0a2fSth 		 * __s_api_findRootDSE() is the standalone version
2331e1dd0a2fSth 		 * of getldap_get_serverInfo() used in ldap_cachemgr.
2332e1dd0a2fSth 		 * Request NS_CACHE_NORESP indicates 'server' should
2333e1dd0a2fSth 		 * be removed.
2334e1dd0a2fSth 		 */
2335e1dd0a2fSth 		if (__s_api_findRootDSE(ireq,
2336e1dd0a2fSth 		    server,
2337e1dd0a2fSth 		    NS_CACHE_ADDR_IP,
2338e1dd0a2fSth 		    NULL,
2339e1dd0a2fSth 		    &error) != NS_LDAP_SUCCESS) {
2340e1dd0a2fSth 			syslog(LOG_WARNING,
2341e1dd0a2fSth 			    "libsldap (\"standalone\" mode): "
2342e1dd0a2fSth 			    " Unable to remove %s - %s",
2343e1dd0a2fSth 			    server,
2344e1dd0a2fSth 			    error != NULL && error->message != NULL ?
2345e1dd0a2fSth 			    error->message : " no error info");
2346e1dd0a2fSth 			if (error != NULL) {
2347e1dd0a2fSth 				(void) __ns_ldap_freeError(&error);
2348e1dd0a2fSth 			}
2349e1dd0a2fSth 
235029836b19Smichen 			return (NS_CACHE_NOSERVER);
2351e1dd0a2fSth 		}
2352e1dd0a2fSth 
2353e1dd0a2fSth 		return (0);
2354e1dd0a2fSth 	}
2355e1dd0a2fSth 
23567c478bd9Sstevel@tonic-gate 	(void) memset(ret, 0, sizeof (ns_server_info_t));
23577c478bd9Sstevel@tonic-gate 	(void) memset(space.s_b, 0, DOORBUFFERSIZE);
23587c478bd9Sstevel@tonic-gate 
2359cb5caa98Sdjl 	adata = (sizeof (ldap_call_t) + strlen(ireq) +
23604a6b6ac4Schinlong 	    strlen(NS_CACHE_ADDR_IP) + 1);
23617c478bd9Sstevel@tonic-gate 	adata += strlen(DOORLINESEP) + 1;
23627c478bd9Sstevel@tonic-gate 	adata += strlen(server) + 1;
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	ndata = sizeof (space);
23657c478bd9Sstevel@tonic-gate 	space.s_d.ldap_call.ldap_callnumber = GETLDAPSERVER;
23667c478bd9Sstevel@tonic-gate 	len = sizeof (space) - sizeof (space.s_d.ldap_call.ldap_callnumber);
2367cb5caa98Sdjl 	if (strlcpy(space.s_d.ldap_call.ldap_u.domainname, ireq, len) >= len)
2368cb5caa98Sdjl 		return (-1);
2369cb5caa98Sdjl 	if (strlcat(space.s_d.ldap_call.ldap_u.domainname,
23704a6b6ac4Schinlong 	    NS_CACHE_ADDR_IP, len) >= len)
2371cb5caa98Sdjl 		return (-1);
2372cb5caa98Sdjl 	if (strlcat(space.s_d.ldap_call.ldap_u.domainname, DOORLINESEP, len) >=
23734a6b6ac4Schinlong 	    len)
2374cb5caa98Sdjl 		return (-1);
2375cb5caa98Sdjl 	if (strlcat(space.s_d.ldap_call.ldap_u.domainname, server, len) >= len)
2376cb5caa98Sdjl 		return (-1);
23777c478bd9Sstevel@tonic-gate 	sptr = &space.s_d;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	/* try to remove the server via the door interface */
2380e8ac3ceaSsdussud 	rc = __ns_ldap_trydoorcall(&sptr, &ndata, &adata);
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	/* clean up the door call */
23837c478bd9Sstevel@tonic-gate 	if (sptr != &space.s_d) {
23847c478bd9Sstevel@tonic-gate 		(void) munmap((char *)sptr, ndata);
23857c478bd9Sstevel@tonic-gate 	}
2386e8ac3ceaSsdussud 
2387e8ac3ceaSsdussud 	return (rc);
23887c478bd9Sstevel@tonic-gate }
23894a6b6ac4Schinlong 
23904a6b6ac4Schinlong void
__s_api_free_server_info(ns_server_info_t * sinfo)23914a6b6ac4Schinlong __s_api_free_server_info(ns_server_info_t *sinfo) {
23924a6b6ac4Schinlong 	if (sinfo->server) {
23934a6b6ac4Schinlong 		free(sinfo->server);
23944a6b6ac4Schinlong 		sinfo->server = NULL;
23954a6b6ac4Schinlong 	}
23964a6b6ac4Schinlong 	if (sinfo->serverFQDN) {
23974a6b6ac4Schinlong 		free(sinfo->serverFQDN);
23984a6b6ac4Schinlong 		sinfo->serverFQDN = NULL;
23994a6b6ac4Schinlong 	}
24004a6b6ac4Schinlong 	__s_api_free2dArray(sinfo->saslMechanisms);
24014a6b6ac4Schinlong 	sinfo->saslMechanisms = NULL;
24024a6b6ac4Schinlong 	__s_api_free2dArray(sinfo->controls);
24034a6b6ac4Schinlong 	sinfo->controls = NULL;
24044a6b6ac4Schinlong }
2405e1dd0a2fSth 
2406e1dd0a2fSth /*
2407e1dd0a2fSth  * Create an ns_ldap_error structure, set status to 'rc',
2408e1dd0a2fSth  * and copy in the error message 'msg'.
2409e1dd0a2fSth  */
2410e1dd0a2fSth ns_ldap_error_t *
__s_api_make_error(int rc,char * msg)2411e1dd0a2fSth __s_api_make_error(int rc, char *msg) {
2412e1dd0a2fSth 	ns_ldap_error_t *ep;
2413e1dd0a2fSth 
2414e1dd0a2fSth 	ep = (ns_ldap_error_t *)calloc(1, sizeof (*ep));
2415e1dd0a2fSth 	if (ep == NULL)
2416e1dd0a2fSth 		return (NULL);
2417e1dd0a2fSth 
2418e1dd0a2fSth 	ep->status = rc;
2419e1dd0a2fSth 	if (msg != NULL)
2420e1dd0a2fSth 		ep->message =  strdup(msg); /* OK if ep->message is NULL */
2421e1dd0a2fSth 
2422e1dd0a2fSth 	return (ep);
2423e1dd0a2fSth }
2424e1dd0a2fSth 
2425e1dd0a2fSth /*
2426e1dd0a2fSth  * Make a copy of the input ns_ldap_error.
2427e1dd0a2fSth  */
2428e1dd0a2fSth ns_ldap_error_t *
__s_api_copy_error(ns_ldap_error_t * errorp)2429e1dd0a2fSth __s_api_copy_error(ns_ldap_error_t *errorp) {
2430e1dd0a2fSth 	ns_ldap_error_t *ep;
2431e1dd0a2fSth 	char		*msg;
2432e1dd0a2fSth 
2433e1dd0a2fSth 	if (errorp == NULL)
2434e1dd0a2fSth 		return (NULL);
2435e1dd0a2fSth 
2436e1dd0a2fSth 	ep = (ns_ldap_error_t *)malloc(sizeof (*ep));
2437e1dd0a2fSth 	if (ep != NULL) {
2438e1dd0a2fSth 		*ep = *errorp;
2439e1dd0a2fSth 		if (ep->message != NULL) {
2440e1dd0a2fSth 			msg = strdup(ep->message);
2441e1dd0a2fSth 			if (msg == NULL) {
2442e1dd0a2fSth 				free(ep);
2443e1dd0a2fSth 				ep = NULL;
2444e1dd0a2fSth 			} else
2445e1dd0a2fSth 				ep->message = msg;
2446e1dd0a2fSth 		}
2447e1dd0a2fSth 	}
2448e1dd0a2fSth 	return (ep);
2449e1dd0a2fSth }
2450