xref: /titanic_52/usr/src/cmd/sendmail/libsm/ldap.c (revision 445f2479fe3d7435daab18bf2cdc310b86cd6738)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2001-2005 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *      All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include <sm/gen.h>
13*445f2479Sjbeck SM_RCSID("@(#)$Id: ldap.c,v 1.67 2005/12/14 00:08:03 ca Exp $")
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #if LDAPMAP
167c478bd9Sstevel@tonic-gate # include <sys/types.h>
177c478bd9Sstevel@tonic-gate # include <errno.h>
187c478bd9Sstevel@tonic-gate # include <setjmp.h>
197c478bd9Sstevel@tonic-gate # include <stdlib.h>
207c478bd9Sstevel@tonic-gate # include <unistd.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate # include <sm/bitops.h>
237c478bd9Sstevel@tonic-gate # include <sm/clock.h>
247c478bd9Sstevel@tonic-gate # include <sm/conf.h>
257c478bd9Sstevel@tonic-gate # include <sm/debug.h>
267c478bd9Sstevel@tonic-gate # include <sm/errstring.h>
277c478bd9Sstevel@tonic-gate # include <sm/ldap.h>
287c478bd9Sstevel@tonic-gate # include <sm/string.h>
297c478bd9Sstevel@tonic-gate #  ifdef EX_OK
307c478bd9Sstevel@tonic-gate #   undef EX_OK			/* for SVr4.2 SMP */
317c478bd9Sstevel@tonic-gate #  endif /* EX_OK */
327c478bd9Sstevel@tonic-gate # include <sm/sysexits.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate SM_DEBUG_T SmLDAPTrace = SM_DEBUG_INITIALIZER("sm_trace_ldap",
357c478bd9Sstevel@tonic-gate 	"@(#)$Debug: sm_trace_ldap - trace LDAP operations $");
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static void	ldaptimeout __P((int));
387c478bd9Sstevel@tonic-gate static bool	sm_ldap_has_objectclass __P((SM_LDAP_STRUCT *, LDAPMessage *, char *));
397c478bd9Sstevel@tonic-gate static SM_LDAP_RECURSE_ENTRY *sm_ldap_add_recurse __P((SM_LDAP_RECURSE_LIST **, char *, int, SM_RPOOL_T *));
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate **  SM_LDAP_CLEAR -- set default values for SM_LDAP_STRUCT
437c478bd9Sstevel@tonic-gate **
447c478bd9Sstevel@tonic-gate **	Parameters:
457c478bd9Sstevel@tonic-gate **		lmap -- pointer to SM_LDAP_STRUCT to clear
467c478bd9Sstevel@tonic-gate **
477c478bd9Sstevel@tonic-gate **	Returns:
487c478bd9Sstevel@tonic-gate **		None.
497c478bd9Sstevel@tonic-gate **
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate 
52*445f2479Sjbeck #if _FFR_LDAP_VERSION
53*445f2479Sjbeck # if defined(LDAP_VERSION_MAX) && _FFR_LDAP_VERSION > LDAP_VERSION_MAX
54*445f2479Sjbeck     ERROR FFR_LDAP_VERSION > _LDAP_VERSION_MAX
55*445f2479Sjbeck # endif /* defined(LDAP_VERSION_MAX) && _FFR_LDAP_VERSION > LDAP_VERSION_MAX */
56*445f2479Sjbeck # if defined(LDAP_VERSION_MIN) && _FFR_LDAP_VERSION < LDAP_VERSION_MIN
57*445f2479Sjbeck     ERROR FFR_LDAP_VERSION < _LDAP_VERSION_MIN
58*445f2479Sjbeck # endif /* defined(LDAP_VERSION_MIN) && _FFR_LDAP_VERSION < LDAP_VERSION_MIN */
59*445f2479Sjbeck # define SM_LDAP_VERSION_DEFAULT	_FFR_LDAP_VERSION
60*445f2479Sjbeck #else /* _FFR_LDAP_VERSION */
61*445f2479Sjbeck # define SM_LDAP_VERSION_DEFAULT	0
62*445f2479Sjbeck #endif /* _FFR_LDAP_VERSION */
63*445f2479Sjbeck 
647c478bd9Sstevel@tonic-gate void
657c478bd9Sstevel@tonic-gate sm_ldap_clear(lmap)
667c478bd9Sstevel@tonic-gate 	SM_LDAP_STRUCT *lmap;
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	if (lmap == NULL)
697c478bd9Sstevel@tonic-gate 		return;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	lmap->ldap_host = NULL;
727c478bd9Sstevel@tonic-gate 	lmap->ldap_port = LDAP_PORT;
737c478bd9Sstevel@tonic-gate 	lmap->ldap_uri = NULL;
74*445f2479Sjbeck 	lmap->ldap_version = SM_LDAP_VERSION_DEFAULT;
757c478bd9Sstevel@tonic-gate 	lmap->ldap_deref = LDAP_DEREF_NEVER;
767c478bd9Sstevel@tonic-gate 	lmap->ldap_timelimit = LDAP_NO_LIMIT;
777c478bd9Sstevel@tonic-gate 	lmap->ldap_sizelimit = LDAP_NO_LIMIT;
787c478bd9Sstevel@tonic-gate # ifdef LDAP_REFERRALS
797c478bd9Sstevel@tonic-gate 	lmap->ldap_options = LDAP_OPT_REFERRALS;
807c478bd9Sstevel@tonic-gate # else /* LDAP_REFERRALS */
817c478bd9Sstevel@tonic-gate 	lmap->ldap_options = 0;
827c478bd9Sstevel@tonic-gate # endif /* LDAP_REFERRALS */
837c478bd9Sstevel@tonic-gate 	lmap->ldap_attrsep = '\0';
847c478bd9Sstevel@tonic-gate 	lmap->ldap_binddn = NULL;
857c478bd9Sstevel@tonic-gate 	lmap->ldap_secret = NULL;
867c478bd9Sstevel@tonic-gate 	lmap->ldap_method = LDAP_AUTH_SIMPLE;
877c478bd9Sstevel@tonic-gate 	lmap->ldap_base = NULL;
887c478bd9Sstevel@tonic-gate 	lmap->ldap_scope = LDAP_SCOPE_SUBTREE;
897c478bd9Sstevel@tonic-gate 	lmap->ldap_attrsonly = LDAPMAP_FALSE;
907c478bd9Sstevel@tonic-gate 	lmap->ldap_timeout.tv_sec = 0;
917c478bd9Sstevel@tonic-gate 	lmap->ldap_timeout.tv_usec = 0;
927c478bd9Sstevel@tonic-gate 	lmap->ldap_ld = NULL;
937c478bd9Sstevel@tonic-gate 	lmap->ldap_filter = NULL;
947c478bd9Sstevel@tonic-gate 	lmap->ldap_attr[0] = NULL;
957c478bd9Sstevel@tonic-gate 	lmap->ldap_attr_type[0] = SM_LDAP_ATTR_NONE;
967c478bd9Sstevel@tonic-gate 	lmap->ldap_attr_needobjclass[0] = NULL;
977c478bd9Sstevel@tonic-gate 	lmap->ldap_res = NULL;
987c478bd9Sstevel@tonic-gate 	lmap->ldap_next = NULL;
997c478bd9Sstevel@tonic-gate 	lmap->ldap_pid = 0;
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate **  SM_LDAP_START -- actually connect to an LDAP server
1047c478bd9Sstevel@tonic-gate **
1057c478bd9Sstevel@tonic-gate **	Parameters:
1067c478bd9Sstevel@tonic-gate **		name -- name of map for debug output.
1077c478bd9Sstevel@tonic-gate **		lmap -- the LDAP map being opened.
1087c478bd9Sstevel@tonic-gate **
1097c478bd9Sstevel@tonic-gate **	Returns:
1107c478bd9Sstevel@tonic-gate **		true if connection is successful, false otherwise.
1117c478bd9Sstevel@tonic-gate **
1127c478bd9Sstevel@tonic-gate **	Side Effects:
1137c478bd9Sstevel@tonic-gate **		Populates lmap->ldap_ld.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static jmp_buf	LDAPTimeout;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #define SM_LDAP_SETTIMEOUT(to)						\
1197c478bd9Sstevel@tonic-gate do									\
1207c478bd9Sstevel@tonic-gate {									\
1217c478bd9Sstevel@tonic-gate 	if (to != 0)							\
1227c478bd9Sstevel@tonic-gate 	{								\
1237c478bd9Sstevel@tonic-gate 		if (setjmp(LDAPTimeout) != 0)				\
1247c478bd9Sstevel@tonic-gate 		{							\
1257c478bd9Sstevel@tonic-gate 			errno = ETIMEDOUT;				\
1267c478bd9Sstevel@tonic-gate 			return false;					\
1277c478bd9Sstevel@tonic-gate 		}							\
1287c478bd9Sstevel@tonic-gate 		ev = sm_setevent(to, ldaptimeout, 0);			\
1297c478bd9Sstevel@tonic-gate 	}								\
1307c478bd9Sstevel@tonic-gate } while (0)
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define SM_LDAP_CLEARTIMEOUT()						\
1337c478bd9Sstevel@tonic-gate do									\
1347c478bd9Sstevel@tonic-gate {									\
1357c478bd9Sstevel@tonic-gate 	if (ev != NULL)							\
1367c478bd9Sstevel@tonic-gate 		sm_clrevent(ev);					\
1377c478bd9Sstevel@tonic-gate } while (0)
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate bool
1407c478bd9Sstevel@tonic-gate sm_ldap_start(name, lmap)
1417c478bd9Sstevel@tonic-gate 	char *name;
1427c478bd9Sstevel@tonic-gate 	SM_LDAP_STRUCT *lmap;
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	int bind_result;
1457c478bd9Sstevel@tonic-gate 	int save_errno = 0;
1467c478bd9Sstevel@tonic-gate 	char *id;
1477c478bd9Sstevel@tonic-gate 	SM_EVENT *ev = NULL;
1487c478bd9Sstevel@tonic-gate 	LDAP *ld = NULL;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (sm_debug_active(&SmLDAPTrace, 2))
1517c478bd9Sstevel@tonic-gate 		sm_dprintf("ldapmap_start(%s)\n", name == NULL ? "" : name);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (lmap->ldap_host != NULL)
1547c478bd9Sstevel@tonic-gate 		id = lmap->ldap_host;
1557c478bd9Sstevel@tonic-gate 	else if (lmap->ldap_uri != NULL)
1567c478bd9Sstevel@tonic-gate 		id = lmap->ldap_uri;
1577c478bd9Sstevel@tonic-gate 	else
1587c478bd9Sstevel@tonic-gate 		id = "localhost";
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (sm_debug_active(&SmLDAPTrace, 9))
1617c478bd9Sstevel@tonic-gate 	{
1627c478bd9Sstevel@tonic-gate 		/* Don't print a port number for LDAP URIs */
1637c478bd9Sstevel@tonic-gate 		if (lmap->ldap_uri != NULL)
1647c478bd9Sstevel@tonic-gate 			sm_dprintf("ldapmap_start(%s)\n", id);
1657c478bd9Sstevel@tonic-gate 		else
1667c478bd9Sstevel@tonic-gate 			sm_dprintf("ldapmap_start(%s, %d)\n", id,
1677c478bd9Sstevel@tonic-gate 				   lmap->ldap_port);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (lmap->ldap_uri != NULL)
1717c478bd9Sstevel@tonic-gate 	{
1727c478bd9Sstevel@tonic-gate #if SM_CONF_LDAP_INITIALIZE
1737c478bd9Sstevel@tonic-gate 		/* LDAP server supports URIs so use them directly */
1747c478bd9Sstevel@tonic-gate 		save_errno = ldap_initialize(&ld, lmap->ldap_uri);
1757c478bd9Sstevel@tonic-gate #else /* SM_CONF_LDAP_INITIALIZE */
1767c478bd9Sstevel@tonic-gate 		int err;
1777c478bd9Sstevel@tonic-gate 		LDAPURLDesc *ludp = NULL;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		/* Blast apart URL and use the ldap_init/ldap_open below */
1807c478bd9Sstevel@tonic-gate 		err = ldap_url_parse(lmap->ldap_uri, &ludp);
1817c478bd9Sstevel@tonic-gate 		if (err != 0)
1827c478bd9Sstevel@tonic-gate 		{
1837c478bd9Sstevel@tonic-gate 			errno = err + E_LDAPURLBASE;
1847c478bd9Sstevel@tonic-gate 			return false;
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		lmap->ldap_host = sm_strdup_x(ludp->lud_host);
1877c478bd9Sstevel@tonic-gate 		if (lmap->ldap_host == NULL)
1887c478bd9Sstevel@tonic-gate 		{
1897c478bd9Sstevel@tonic-gate 			save_errno = errno;
1907c478bd9Sstevel@tonic-gate 			ldap_free_urldesc(ludp);
1917c478bd9Sstevel@tonic-gate 			errno = save_errno;
1927c478bd9Sstevel@tonic-gate 			return false;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		lmap->ldap_port = ludp->lud_port;
1957c478bd9Sstevel@tonic-gate 		ldap_free_urldesc(ludp);
1967c478bd9Sstevel@tonic-gate #endif /* SM_CONF_LDAP_INITIALIZE */
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (ld == NULL)
2007c478bd9Sstevel@tonic-gate 	{
2017c478bd9Sstevel@tonic-gate # if USE_LDAP_INIT
2027c478bd9Sstevel@tonic-gate 		ld = ldap_init(lmap->ldap_host, lmap->ldap_port);
2037c478bd9Sstevel@tonic-gate 		save_errno = errno;
2047c478bd9Sstevel@tonic-gate # else /* USE_LDAP_INIT */
2057c478bd9Sstevel@tonic-gate 		/*
2067c478bd9Sstevel@tonic-gate 		**  If using ldap_open(), the actual connection to the server
2077c478bd9Sstevel@tonic-gate 		**  happens now so we need the timeout here.  For ldap_init(),
2087c478bd9Sstevel@tonic-gate 		**  the connection happens at bind time.
2097c478bd9Sstevel@tonic-gate 		*/
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		SM_LDAP_SETTIMEOUT(lmap->ldap_timeout.tv_sec);
2127c478bd9Sstevel@tonic-gate 		ld = ldap_open(lmap->ldap_host, lmap->ldap_port);
2137c478bd9Sstevel@tonic-gate 		save_errno = errno;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		/* clear the event if it has not sprung */
2167c478bd9Sstevel@tonic-gate 		SM_LDAP_CLEARTIMEOUT();
2177c478bd9Sstevel@tonic-gate # endif /* USE_LDAP_INIT */
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	errno = save_errno;
2217c478bd9Sstevel@tonic-gate 	if (ld == NULL)
2227c478bd9Sstevel@tonic-gate 		return false;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	sm_ldap_setopts(ld, lmap);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate # if USE_LDAP_INIT
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	**  If using ldap_init(), the actual connection to the server
2297c478bd9Sstevel@tonic-gate 	**  happens at ldap_bind_s() so we need the timeout here.
2307c478bd9Sstevel@tonic-gate 	*/
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	SM_LDAP_SETTIMEOUT(lmap->ldap_timeout.tv_sec);
2337c478bd9Sstevel@tonic-gate # endif /* USE_LDAP_INIT */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate # ifdef LDAP_AUTH_KRBV4
2367c478bd9Sstevel@tonic-gate 	if (lmap->ldap_method == LDAP_AUTH_KRBV4 &&
2377c478bd9Sstevel@tonic-gate 	    lmap->ldap_secret != NULL)
2387c478bd9Sstevel@tonic-gate 	{
2397c478bd9Sstevel@tonic-gate 		/*
2407c478bd9Sstevel@tonic-gate 		**  Need to put ticket in environment here instead of
2417c478bd9Sstevel@tonic-gate 		**  during parseargs as there may be different tickets
2427c478bd9Sstevel@tonic-gate 		**  for different LDAP connections.
2437c478bd9Sstevel@tonic-gate 		*/
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		(void) putenv(lmap->ldap_secret);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate # endif /* LDAP_AUTH_KRBV4 */
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	bind_result = ldap_bind_s(ld, lmap->ldap_binddn,
2507c478bd9Sstevel@tonic-gate 				  lmap->ldap_secret, lmap->ldap_method);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate # if USE_LDAP_INIT
2537c478bd9Sstevel@tonic-gate 	/* clear the event if it has not sprung */
2547c478bd9Sstevel@tonic-gate 	SM_LDAP_CLEARTIMEOUT();
2557c478bd9Sstevel@tonic-gate # endif /* USE_LDAP_INIT */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (bind_result != LDAP_SUCCESS)
2587c478bd9Sstevel@tonic-gate 	{
2597c478bd9Sstevel@tonic-gate 		errno = bind_result + E_LDAPBASE;
2607c478bd9Sstevel@tonic-gate 		return false;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/* Save PID to make sure only this PID closes the LDAP connection */
2647c478bd9Sstevel@tonic-gate 	lmap->ldap_pid = getpid();
2657c478bd9Sstevel@tonic-gate 	lmap->ldap_ld = ld;
2667c478bd9Sstevel@tonic-gate 	return true;
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /* ARGSUSED */
2707c478bd9Sstevel@tonic-gate static void
2717c478bd9Sstevel@tonic-gate ldaptimeout(unused)
2727c478bd9Sstevel@tonic-gate 	int unused;
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	**  NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
2767c478bd9Sstevel@tonic-gate 	**	ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2777c478bd9Sstevel@tonic-gate 	**	DOING.
2787c478bd9Sstevel@tonic-gate 	*/
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	errno = ETIMEDOUT;
2817c478bd9Sstevel@tonic-gate 	longjmp(LDAPTimeout, 1);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate **  SM_LDAP_SEARCH -- initiate LDAP search
2867c478bd9Sstevel@tonic-gate **
2877c478bd9Sstevel@tonic-gate **	Initiate an LDAP search, return the msgid.
2887c478bd9Sstevel@tonic-gate **	The calling function must collect the results.
2897c478bd9Sstevel@tonic-gate **
2907c478bd9Sstevel@tonic-gate **	Parameters:
2917c478bd9Sstevel@tonic-gate **		lmap -- LDAP map information
2927c478bd9Sstevel@tonic-gate **		key -- key to substitute in LDAP filter
2937c478bd9Sstevel@tonic-gate **
2947c478bd9Sstevel@tonic-gate **	Returns:
2957c478bd9Sstevel@tonic-gate **		-1 on failure, msgid on success
2967c478bd9Sstevel@tonic-gate **
2977c478bd9Sstevel@tonic-gate */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate int
3007c478bd9Sstevel@tonic-gate sm_ldap_search(lmap, key)
3017c478bd9Sstevel@tonic-gate 	SM_LDAP_STRUCT *lmap;
3027c478bd9Sstevel@tonic-gate 	char *key;
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	int msgid;
3057c478bd9Sstevel@tonic-gate 	char *fp, *p, *q;
3067c478bd9Sstevel@tonic-gate 	char filter[LDAPMAP_MAX_FILTER + 1];
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/* substitute key into filter, perhaps multiple times */
3097c478bd9Sstevel@tonic-gate 	memset(filter, '\0', sizeof filter);
3107c478bd9Sstevel@tonic-gate 	fp = filter;
3117c478bd9Sstevel@tonic-gate 	p = lmap->ldap_filter;
3127c478bd9Sstevel@tonic-gate 	while ((q = strchr(p, '%')) != NULL)
3137c478bd9Sstevel@tonic-gate 	{
3147c478bd9Sstevel@tonic-gate 		if (q[1] == 's')
3157c478bd9Sstevel@tonic-gate 		{
3167c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(fp, SPACELEFT(filter, fp),
3177c478bd9Sstevel@tonic-gate 					   "%.*s%s", (int) (q - p), p, key);
3187c478bd9Sstevel@tonic-gate 			fp += strlen(fp);
3197c478bd9Sstevel@tonic-gate 			p = q + 2;
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 		else if (q[1] == '0')
3227c478bd9Sstevel@tonic-gate 		{
3237c478bd9Sstevel@tonic-gate 			char *k = key;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(fp, SPACELEFT(filter, fp),
3267c478bd9Sstevel@tonic-gate 					   "%.*s", (int) (q - p), p);
3277c478bd9Sstevel@tonic-gate 			fp += strlen(fp);
3287c478bd9Sstevel@tonic-gate 			p = q + 2;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 			/* Properly escape LDAP special characters */
3317c478bd9Sstevel@tonic-gate 			while (SPACELEFT(filter, fp) > 0 &&
3327c478bd9Sstevel@tonic-gate 			       *k != '\0')
3337c478bd9Sstevel@tonic-gate 			{
3347c478bd9Sstevel@tonic-gate 				if (*k == '*' || *k == '(' ||
3357c478bd9Sstevel@tonic-gate 				    *k == ')' || *k == '\\')
3367c478bd9Sstevel@tonic-gate 				{
3377c478bd9Sstevel@tonic-gate 					(void) sm_strlcat(fp,
3387c478bd9Sstevel@tonic-gate 						       (*k == '*' ? "\\2A" :
3397c478bd9Sstevel@tonic-gate 							(*k == '(' ? "\\28" :
3407c478bd9Sstevel@tonic-gate 							 (*k == ')' ? "\\29" :
3417c478bd9Sstevel@tonic-gate 							  (*k == '\\' ? "\\5C" :
3427c478bd9Sstevel@tonic-gate 							   "\00")))),
3437c478bd9Sstevel@tonic-gate 						SPACELEFT(filter, fp));
3447c478bd9Sstevel@tonic-gate 					fp += strlen(fp);
3457c478bd9Sstevel@tonic-gate 					k++;
3467c478bd9Sstevel@tonic-gate 				}
3477c478bd9Sstevel@tonic-gate 				else
3487c478bd9Sstevel@tonic-gate 					*fp++ = *k++;
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 		else
3527c478bd9Sstevel@tonic-gate 		{
3537c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(fp, SPACELEFT(filter, fp),
3547c478bd9Sstevel@tonic-gate 				"%.*s", (int) (q - p + 1), p);
3557c478bd9Sstevel@tonic-gate 			p = q + (q[1] == '%' ? 2 : 1);
3567c478bd9Sstevel@tonic-gate 			fp += strlen(fp);
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(fp, p, SPACELEFT(filter, fp));
3607c478bd9Sstevel@tonic-gate 	if (sm_debug_active(&SmLDAPTrace, 20))
3617c478bd9Sstevel@tonic-gate 		sm_dprintf("ldap search filter=%s\n", filter);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	lmap->ldap_res = NULL;
3647c478bd9Sstevel@tonic-gate 	msgid = ldap_search(lmap->ldap_ld, lmap->ldap_base,
3657c478bd9Sstevel@tonic-gate 			    lmap->ldap_scope, filter,
3667c478bd9Sstevel@tonic-gate 			    (lmap->ldap_attr[0] == NULL ? NULL :
3677c478bd9Sstevel@tonic-gate 			     lmap->ldap_attr),
3687c478bd9Sstevel@tonic-gate 			    lmap->ldap_attrsonly);
3697c478bd9Sstevel@tonic-gate 	return msgid;
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate **  SM_LDAP_HAS_OBJECTCLASS -- determine if an LDAP entry is part of a
3747c478bd9Sstevel@tonic-gate **			       particular objectClass
3757c478bd9Sstevel@tonic-gate **
3767c478bd9Sstevel@tonic-gate **	Parameters:
3777c478bd9Sstevel@tonic-gate **		lmap -- pointer to SM_LDAP_STRUCT in use
3787c478bd9Sstevel@tonic-gate **		entry -- current LDAP entry struct
3797c478bd9Sstevel@tonic-gate **		ocvalue -- particular objectclass in question.
3807c478bd9Sstevel@tonic-gate **			   may be of form (fee|foo|fum) meaning
3817c478bd9Sstevel@tonic-gate **			   any entry can be part of either fee,
3827c478bd9Sstevel@tonic-gate **			   foo or fum objectclass
3837c478bd9Sstevel@tonic-gate **
3847c478bd9Sstevel@tonic-gate **	Returns:
3857c478bd9Sstevel@tonic-gate **		true if item has that objectClass
3867c478bd9Sstevel@tonic-gate */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate static bool
3897c478bd9Sstevel@tonic-gate sm_ldap_has_objectclass(lmap, entry, ocvalue)
3907c478bd9Sstevel@tonic-gate 	SM_LDAP_STRUCT *lmap;
3917c478bd9Sstevel@tonic-gate 	LDAPMessage *entry;
3927c478bd9Sstevel@tonic-gate 	char *ocvalue;
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	char **vals = NULL;
3957c478bd9Sstevel@tonic-gate 	int i;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (ocvalue == NULL)
3987c478bd9Sstevel@tonic-gate 		return false;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	vals = ldap_get_values(lmap->ldap_ld, entry, "objectClass");
4017c478bd9Sstevel@tonic-gate 	if (vals == NULL)
4027c478bd9Sstevel@tonic-gate 		return false;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	for (i = 0; vals[i] != NULL; i++)
4057c478bd9Sstevel@tonic-gate 	{
4067c478bd9Sstevel@tonic-gate 		char *p;
4077c478bd9Sstevel@tonic-gate 		char *q;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		p = q = ocvalue;
4107c478bd9Sstevel@tonic-gate 		while (*p != '\0')
4117c478bd9Sstevel@tonic-gate 		{
4127c478bd9Sstevel@tonic-gate 			while (*p != '\0' && *p != '|')
4137c478bd9Sstevel@tonic-gate 				p++;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 			if ((p - q) == strlen(vals[i]) &&
4167c478bd9Sstevel@tonic-gate 			    sm_strncasecmp(vals[i], q, p - q) == 0)
4177c478bd9Sstevel@tonic-gate 			{
4187c478bd9Sstevel@tonic-gate 				ldap_value_free(vals);
4197c478bd9Sstevel@tonic-gate 				return true;
4207c478bd9Sstevel@tonic-gate 			}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 			while (*p == '|')
4237c478bd9Sstevel@tonic-gate 				p++;
4247c478bd9Sstevel@tonic-gate 			q = p;
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	ldap_value_free(vals);
4297c478bd9Sstevel@tonic-gate 	return false;
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate **  SM_LDAP_RESULTS -- return results from an LDAP lookup in result
4347c478bd9Sstevel@tonic-gate **
4357c478bd9Sstevel@tonic-gate **	Parameters:
4367c478bd9Sstevel@tonic-gate **		lmap -- pointer to SM_LDAP_STRUCT in use
4377c478bd9Sstevel@tonic-gate **		msgid -- msgid returned by sm_ldap_search()
4387c478bd9Sstevel@tonic-gate **		flags -- flags for the lookup
4397c478bd9Sstevel@tonic-gate **		delim -- delimiter for result concatenation
4407c478bd9Sstevel@tonic-gate **		rpool -- memory pool for storage
4417c478bd9Sstevel@tonic-gate **		result -- return string
4427c478bd9Sstevel@tonic-gate **		recurse -- recursion list
4437c478bd9Sstevel@tonic-gate **
4447c478bd9Sstevel@tonic-gate **	Returns:
4457c478bd9Sstevel@tonic-gate **		status (sysexit)
4467c478bd9Sstevel@tonic-gate */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate # define SM_LDAP_ERROR_CLEANUP()				\
4497c478bd9Sstevel@tonic-gate {								\
4507c478bd9Sstevel@tonic-gate 	if (lmap->ldap_res != NULL)				\
4517c478bd9Sstevel@tonic-gate 	{							\
4527c478bd9Sstevel@tonic-gate 		ldap_msgfree(lmap->ldap_res);			\
4537c478bd9Sstevel@tonic-gate 		lmap->ldap_res = NULL;				\
4547c478bd9Sstevel@tonic-gate 	}							\
4557c478bd9Sstevel@tonic-gate 	(void) ldap_abandon(lmap->ldap_ld, msgid);		\
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate static SM_LDAP_RECURSE_ENTRY *
4597c478bd9Sstevel@tonic-gate sm_ldap_add_recurse(top, item, type, rpool)
4607c478bd9Sstevel@tonic-gate 	SM_LDAP_RECURSE_LIST **top;
4617c478bd9Sstevel@tonic-gate 	char *item;
4627c478bd9Sstevel@tonic-gate 	int type;
4637c478bd9Sstevel@tonic-gate 	SM_RPOOL_T *rpool;
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	int n;
4667c478bd9Sstevel@tonic-gate 	int m;
4677c478bd9Sstevel@tonic-gate 	int p;
4687c478bd9Sstevel@tonic-gate 	int insertat;
4697c478bd9Sstevel@tonic-gate 	int moveb;
4707c478bd9Sstevel@tonic-gate 	int oldsizeb;
4717c478bd9Sstevel@tonic-gate 	int rc;
4727c478bd9Sstevel@tonic-gate 	SM_LDAP_RECURSE_ENTRY *newe;
4737c478bd9Sstevel@tonic-gate 	SM_LDAP_RECURSE_ENTRY **olddata;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	**  This code will maintain a list of
4777c478bd9Sstevel@tonic-gate 	**  SM_LDAP_RECURSE_ENTRY structures
4787c478bd9Sstevel@tonic-gate 	**  in ascending order.
4797c478bd9Sstevel@tonic-gate 	*/
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (*top == NULL)
4827c478bd9Sstevel@tonic-gate 	{
4837c478bd9Sstevel@tonic-gate 		/* Allocate an initial SM_LDAP_RECURSE_LIST struct */
4847c478bd9Sstevel@tonic-gate 		*top = sm_rpool_malloc_x(rpool, sizeof **top);
4857c478bd9Sstevel@tonic-gate 		(*top)->lr_cnt = 0;
4867c478bd9Sstevel@tonic-gate 		(*top)->lr_size = 0;
4877c478bd9Sstevel@tonic-gate 		(*top)->lr_data = NULL;
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if ((*top)->lr_cnt >= (*top)->lr_size)
4917c478bd9Sstevel@tonic-gate 	{
4927c478bd9Sstevel@tonic-gate 		/* Grow the list of SM_LDAP_RECURSE_ENTRY ptrs */
4937c478bd9Sstevel@tonic-gate 		olddata = (*top)->lr_data;
4947c478bd9Sstevel@tonic-gate 		if ((*top)->lr_size == 0)
4957c478bd9Sstevel@tonic-gate 		{
4967c478bd9Sstevel@tonic-gate 			oldsizeb = 0;
4977c478bd9Sstevel@tonic-gate 			(*top)->lr_size = 256;
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		else
5007c478bd9Sstevel@tonic-gate 		{
5017c478bd9Sstevel@tonic-gate 			oldsizeb = (*top)->lr_size * sizeof *((*top)->lr_data);
5027c478bd9Sstevel@tonic-gate 			(*top)->lr_size *= 2;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 		(*top)->lr_data = sm_rpool_malloc_x(rpool,
5057c478bd9Sstevel@tonic-gate 						    (*top)->lr_size * sizeof *((*top)->lr_data));
5067c478bd9Sstevel@tonic-gate 		if (oldsizeb > 0)
5077c478bd9Sstevel@tonic-gate 			memcpy((*top)->lr_data, olddata, oldsizeb);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	**  Binary search/insert item:type into list.
5127c478bd9Sstevel@tonic-gate 	**  Return current entry pointer if already exists.
5137c478bd9Sstevel@tonic-gate 	*/
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	n = 0;
5167c478bd9Sstevel@tonic-gate 	m = (*top)->lr_cnt - 1;
5177c478bd9Sstevel@tonic-gate 	if (m < 0)
5187c478bd9Sstevel@tonic-gate 		insertat = 0;
5197c478bd9Sstevel@tonic-gate 	else
5207c478bd9Sstevel@tonic-gate 		insertat = -1;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	while (insertat == -1)
5237c478bd9Sstevel@tonic-gate 	{
5247c478bd9Sstevel@tonic-gate 		p = (m + n) / 2;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		rc = sm_strcasecmp(item, (*top)->lr_data[p]->lr_search);
5277c478bd9Sstevel@tonic-gate 		if (rc == 0)
5287c478bd9Sstevel@tonic-gate 			rc = type - (*top)->lr_data[p]->lr_type;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		if (rc < 0)
5317c478bd9Sstevel@tonic-gate 			m = p - 1;
5327c478bd9Sstevel@tonic-gate 		else if (rc > 0)
5337c478bd9Sstevel@tonic-gate 			n = p + 1;
5347c478bd9Sstevel@tonic-gate 		else
5357c478bd9Sstevel@tonic-gate 			return (*top)->lr_data[p];
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		if (m == -1)
5387c478bd9Sstevel@tonic-gate 			insertat = 0;
5397c478bd9Sstevel@tonic-gate 		else if (n >= (*top)->lr_cnt)
5407c478bd9Sstevel@tonic-gate 			insertat = (*top)->lr_cnt;
5417c478bd9Sstevel@tonic-gate 		else if (m < n)
5427c478bd9Sstevel@tonic-gate 			insertat = m + 1;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	** Not found in list, make room
5477c478bd9Sstevel@tonic-gate 	** at insert point and add it.
5487c478bd9Sstevel@tonic-gate 	*/
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	newe = sm_rpool_malloc_x(rpool, sizeof *newe);
5517c478bd9Sstevel@tonic-gate 	if (newe != NULL)
5527c478bd9Sstevel@tonic-gate 	{
5537c478bd9Sstevel@tonic-gate 		moveb = ((*top)->lr_cnt - insertat) * sizeof *((*top)->lr_data);
5547c478bd9Sstevel@tonic-gate 		if (moveb > 0)
5557c478bd9Sstevel@tonic-gate 			memmove(&((*top)->lr_data[insertat + 1]),
5567c478bd9Sstevel@tonic-gate 				&((*top)->lr_data[insertat]),
5577c478bd9Sstevel@tonic-gate 				moveb);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		newe->lr_search = sm_rpool_strdup_x(rpool, item);
5607c478bd9Sstevel@tonic-gate 		newe->lr_type = type;
5617c478bd9Sstevel@tonic-gate 		newe->lr_ludp = NULL;
5627c478bd9Sstevel@tonic-gate 		newe->lr_attrs = NULL;
5637c478bd9Sstevel@tonic-gate 		newe->lr_done = false;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		((*top)->lr_data)[insertat] = newe;
5667c478bd9Sstevel@tonic-gate 		(*top)->lr_cnt++;
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	return newe;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate int
5727c478bd9Sstevel@tonic-gate sm_ldap_results(lmap, msgid, flags, delim, rpool, result,
5737c478bd9Sstevel@tonic-gate 		resultln, resultsz, recurse)
5747c478bd9Sstevel@tonic-gate 	SM_LDAP_STRUCT *lmap;
5757c478bd9Sstevel@tonic-gate 	int msgid;
5767c478bd9Sstevel@tonic-gate 	int flags;
5777c478bd9Sstevel@tonic-gate 	int delim;
5787c478bd9Sstevel@tonic-gate 	SM_RPOOL_T *rpool;
5797c478bd9Sstevel@tonic-gate 	char **result;
5807c478bd9Sstevel@tonic-gate 	int *resultln;
5817c478bd9Sstevel@tonic-gate 	int *resultsz;
5827c478bd9Sstevel@tonic-gate 	SM_LDAP_RECURSE_LIST *recurse;
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	bool toplevel;
5857c478bd9Sstevel@tonic-gate 	int i;
5867c478bd9Sstevel@tonic-gate 	int statp;
5877c478bd9Sstevel@tonic-gate 	int vsize;
5887c478bd9Sstevel@tonic-gate 	int ret;
5897c478bd9Sstevel@tonic-gate 	int save_errno;
5907c478bd9Sstevel@tonic-gate 	char *p;
5917c478bd9Sstevel@tonic-gate 	SM_LDAP_RECURSE_ENTRY *rl;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/* Are we the top top level of the search? */
5947c478bd9Sstevel@tonic-gate 	toplevel = (recurse == NULL);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	/* Get results */
5977c478bd9Sstevel@tonic-gate 	statp = EX_NOTFOUND;
5987c478bd9Sstevel@tonic-gate 	while ((ret = ldap_result(lmap->ldap_ld, msgid, 0,
5997c478bd9Sstevel@tonic-gate 				  (lmap->ldap_timeout.tv_sec == 0 ? NULL :
6007c478bd9Sstevel@tonic-gate 				   &(lmap->ldap_timeout)),
6017c478bd9Sstevel@tonic-gate 				  &(lmap->ldap_res))) == LDAP_RES_SEARCH_ENTRY)
6027c478bd9Sstevel@tonic-gate 	{
6037c478bd9Sstevel@tonic-gate 		LDAPMessage *entry;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		/* If we don't want multiple values and we have one, break */
6067c478bd9Sstevel@tonic-gate 		if ((char) delim == '\0' &&
6077c478bd9Sstevel@tonic-gate 		    !bitset(SM_LDAP_SINGLEMATCH, flags) &&
6087c478bd9Sstevel@tonic-gate 		    *result != NULL)
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		/* Cycle through all entries */
6127c478bd9Sstevel@tonic-gate 		for (entry = ldap_first_entry(lmap->ldap_ld, lmap->ldap_res);
6137c478bd9Sstevel@tonic-gate 		     entry != NULL;
6147c478bd9Sstevel@tonic-gate 		     entry = ldap_next_entry(lmap->ldap_ld, lmap->ldap_res))
6157c478bd9Sstevel@tonic-gate 		{
6167c478bd9Sstevel@tonic-gate 			BerElement *ber;
6177c478bd9Sstevel@tonic-gate 			char *attr;
6187c478bd9Sstevel@tonic-gate 			char **vals = NULL;
6197c478bd9Sstevel@tonic-gate 			char *dn;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 			/*
6227c478bd9Sstevel@tonic-gate 			**  If matching only and found an entry,
6237c478bd9Sstevel@tonic-gate 			**  no need to spin through attributes
6247c478bd9Sstevel@tonic-gate 			*/
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 			if (bitset(SM_LDAP_MATCHONLY, flags))
6277c478bd9Sstevel@tonic-gate 			{
6287c478bd9Sstevel@tonic-gate 				statp = EX_OK;
6297c478bd9Sstevel@tonic-gate 				continue;
6307c478bd9Sstevel@tonic-gate 			}
6317c478bd9Sstevel@tonic-gate 
63249218d4fSjbeck #if _FFR_LDAP_SINGLEDN
63349218d4fSjbeck 			if (bitset(SM_LDAP_SINGLEDN, flags) && *result != NULL)
63449218d4fSjbeck 			{
63549218d4fSjbeck 				/* only wanted one match */
63649218d4fSjbeck 				SM_LDAP_ERROR_CLEANUP();
63749218d4fSjbeck 				errno = ENOENT;
63849218d4fSjbeck 				return EX_NOTFOUND;
63949218d4fSjbeck 			}
64049218d4fSjbeck #endif /* _FFR_LDAP_SINGLEDN */
64149218d4fSjbeck 
6427c478bd9Sstevel@tonic-gate 			/* record completed DN's to prevent loops */
6437c478bd9Sstevel@tonic-gate 			dn = ldap_get_dn(lmap->ldap_ld, entry);
6447c478bd9Sstevel@tonic-gate 			if (dn == NULL)
6457c478bd9Sstevel@tonic-gate 			{
6467c478bd9Sstevel@tonic-gate 				save_errno = sm_ldap_geterrno(lmap->ldap_ld);
6477c478bd9Sstevel@tonic-gate 				save_errno += E_LDAPBASE;
6487c478bd9Sstevel@tonic-gate 				SM_LDAP_ERROR_CLEANUP();
6497c478bd9Sstevel@tonic-gate 				errno = save_errno;
6507c478bd9Sstevel@tonic-gate 				return EX_TEMPFAIL;
6517c478bd9Sstevel@tonic-gate 			}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 			rl = sm_ldap_add_recurse(&recurse, dn,
6547c478bd9Sstevel@tonic-gate 						 SM_LDAP_ATTR_DN,
6557c478bd9Sstevel@tonic-gate 						 rpool);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 			if (rl == NULL)
6587c478bd9Sstevel@tonic-gate 			{
6597c478bd9Sstevel@tonic-gate 				ldap_memfree(dn);
6607c478bd9Sstevel@tonic-gate 				SM_LDAP_ERROR_CLEANUP();
6617c478bd9Sstevel@tonic-gate 				errno = ENOMEM;
6627c478bd9Sstevel@tonic-gate 				return EX_OSERR;
6637c478bd9Sstevel@tonic-gate 			}
6647c478bd9Sstevel@tonic-gate 			else if (rl->lr_done)
6657c478bd9Sstevel@tonic-gate 			{
6667c478bd9Sstevel@tonic-gate 				/* already on list, skip it */
6677c478bd9Sstevel@tonic-gate 				ldap_memfree(dn);
6687c478bd9Sstevel@tonic-gate 				continue;
6697c478bd9Sstevel@tonic-gate 			}
6707c478bd9Sstevel@tonic-gate 			ldap_memfree(dn);
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate # if !defined(LDAP_VERSION_MAX) && !defined(LDAP_OPT_SIZELIMIT)
6737c478bd9Sstevel@tonic-gate 			/*
6747c478bd9Sstevel@tonic-gate 			**  Reset value to prevent lingering
6757c478bd9Sstevel@tonic-gate 			**  LDAP_DECODING_ERROR due to
6767c478bd9Sstevel@tonic-gate 			**  OpenLDAP 1.X's hack (see below)
6777c478bd9Sstevel@tonic-gate 			*/
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 			lmap->ldap_ld->ld_errno = LDAP_SUCCESS;
6807c478bd9Sstevel@tonic-gate # endif /* !defined(LDAP_VERSION_MAX) !defined(LDAP_OPT_SIZELIMIT) */
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			for (attr = ldap_first_attribute(lmap->ldap_ld, entry,
6837c478bd9Sstevel@tonic-gate 							 &ber);
6847c478bd9Sstevel@tonic-gate 			     attr != NULL;
6857c478bd9Sstevel@tonic-gate 			     attr = ldap_next_attribute(lmap->ldap_ld, entry,
6867c478bd9Sstevel@tonic-gate 							ber))
6877c478bd9Sstevel@tonic-gate 			{
6887c478bd9Sstevel@tonic-gate 				char *tmp, *vp_tmp;
6897c478bd9Sstevel@tonic-gate 				int type;
6907c478bd9Sstevel@tonic-gate 				char *needobjclass = NULL;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 				type = SM_LDAP_ATTR_NONE;
6937c478bd9Sstevel@tonic-gate 				for (i = 0; lmap->ldap_attr[i] != NULL; i++)
6947c478bd9Sstevel@tonic-gate 				{
6957c478bd9Sstevel@tonic-gate 					if (sm_strcasecmp(lmap->ldap_attr[i],
6967c478bd9Sstevel@tonic-gate 							  attr) == 0)
6977c478bd9Sstevel@tonic-gate 					{
6987c478bd9Sstevel@tonic-gate 						type = lmap->ldap_attr_type[i];
6997c478bd9Sstevel@tonic-gate 						needobjclass = lmap->ldap_attr_needobjclass[i];
7007c478bd9Sstevel@tonic-gate 						break;
7017c478bd9Sstevel@tonic-gate 					}
7027c478bd9Sstevel@tonic-gate 				}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 				if (bitset(SM_LDAP_USE_ALLATTR, flags) &&
7057c478bd9Sstevel@tonic-gate 				    type == SM_LDAP_ATTR_NONE)
7067c478bd9Sstevel@tonic-gate 				{
7077c478bd9Sstevel@tonic-gate 					/* URL lookups specify attrs to use */
7087c478bd9Sstevel@tonic-gate 					type = SM_LDAP_ATTR_NORMAL;
7097c478bd9Sstevel@tonic-gate 					needobjclass = NULL;
7107c478bd9Sstevel@tonic-gate 				}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 				if (type == SM_LDAP_ATTR_NONE)
7137c478bd9Sstevel@tonic-gate 				{
7147c478bd9Sstevel@tonic-gate 					/* attribute not requested */
7157c478bd9Sstevel@tonic-gate 					ldap_memfree(attr);
7167c478bd9Sstevel@tonic-gate 					SM_LDAP_ERROR_CLEANUP();
7177c478bd9Sstevel@tonic-gate 					errno = EFAULT;
7187c478bd9Sstevel@tonic-gate 					return EX_SOFTWARE;
7197c478bd9Sstevel@tonic-gate 				}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 				/*
7227c478bd9Sstevel@tonic-gate 				**  For recursion on a particular attribute,
7237c478bd9Sstevel@tonic-gate 				**  we may need to see if this entry is
7247c478bd9Sstevel@tonic-gate 				**  part of a particular objectclass.
7257c478bd9Sstevel@tonic-gate 				**  Also, ignore objectClass attribute.
7267c478bd9Sstevel@tonic-gate 				**  Otherwise we just ignore this attribute.
7277c478bd9Sstevel@tonic-gate 				*/
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 				if (type == SM_LDAP_ATTR_OBJCLASS ||
7307c478bd9Sstevel@tonic-gate 				    (needobjclass != NULL &&
7317c478bd9Sstevel@tonic-gate 				     !sm_ldap_has_objectclass(lmap, entry,
7327c478bd9Sstevel@tonic-gate 							      needobjclass)))
7337c478bd9Sstevel@tonic-gate 				{
7347c478bd9Sstevel@tonic-gate 					ldap_memfree(attr);
7357c478bd9Sstevel@tonic-gate 					continue;
7367c478bd9Sstevel@tonic-gate 				}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 				if (lmap->ldap_attrsonly == LDAPMAP_FALSE)
7397c478bd9Sstevel@tonic-gate 				{
7407c478bd9Sstevel@tonic-gate 					vals = ldap_get_values(lmap->ldap_ld,
7417c478bd9Sstevel@tonic-gate 							       entry,
7427c478bd9Sstevel@tonic-gate 							       attr);
7437c478bd9Sstevel@tonic-gate 					if (vals == NULL)
7447c478bd9Sstevel@tonic-gate 					{
7457c478bd9Sstevel@tonic-gate 						save_errno = sm_ldap_geterrno(lmap->ldap_ld);
7467c478bd9Sstevel@tonic-gate 						if (save_errno == LDAP_SUCCESS)
7477c478bd9Sstevel@tonic-gate 						{
7487c478bd9Sstevel@tonic-gate 							ldap_memfree(attr);
7497c478bd9Sstevel@tonic-gate 							continue;
7507c478bd9Sstevel@tonic-gate 						}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 						/* Must be an error */
7537c478bd9Sstevel@tonic-gate 						save_errno += E_LDAPBASE;
7547c478bd9Sstevel@tonic-gate 						ldap_memfree(attr);
7557c478bd9Sstevel@tonic-gate 						SM_LDAP_ERROR_CLEANUP();
7567c478bd9Sstevel@tonic-gate 						errno = save_errno;
7577c478bd9Sstevel@tonic-gate 						return EX_TEMPFAIL;
7587c478bd9Sstevel@tonic-gate 					}
7597c478bd9Sstevel@tonic-gate 				}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 				statp = EX_OK;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate # if !defined(LDAP_VERSION_MAX) && !defined(LDAP_OPT_SIZELIMIT)
7647c478bd9Sstevel@tonic-gate 				/*
7657c478bd9Sstevel@tonic-gate 				**  Reset value to prevent lingering
7667c478bd9Sstevel@tonic-gate 				**  LDAP_DECODING_ERROR due to
7677c478bd9Sstevel@tonic-gate 				**  OpenLDAP 1.X's hack (see below)
7687c478bd9Sstevel@tonic-gate 				*/
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 				lmap->ldap_ld->ld_errno = LDAP_SUCCESS;
7717c478bd9Sstevel@tonic-gate # endif /* !defined(LDAP_VERSION_MAX) !defined(LDAP_OPT_SIZELIMIT) */
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 				/*
7747c478bd9Sstevel@tonic-gate 				**  If matching only,
7757c478bd9Sstevel@tonic-gate 				**  no need to spin through entries
7767c478bd9Sstevel@tonic-gate 				*/
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 				if (bitset(SM_LDAP_MATCHONLY, flags))
7797c478bd9Sstevel@tonic-gate 				{
7807c478bd9Sstevel@tonic-gate 					if (lmap->ldap_attrsonly == LDAPMAP_FALSE)
7817c478bd9Sstevel@tonic-gate 						ldap_value_free(vals);
7827c478bd9Sstevel@tonic-gate 					ldap_memfree(attr);
7837c478bd9Sstevel@tonic-gate 					continue;
7847c478bd9Sstevel@tonic-gate 				}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 				/*
7877c478bd9Sstevel@tonic-gate 				**  If we don't want multiple values,
7887c478bd9Sstevel@tonic-gate 				**  return first found.
7897c478bd9Sstevel@tonic-gate 				*/
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 				if ((char) delim == '\0')
7927c478bd9Sstevel@tonic-gate 				{
7937c478bd9Sstevel@tonic-gate 					if (*result != NULL)
7947c478bd9Sstevel@tonic-gate 					{
7957c478bd9Sstevel@tonic-gate 						/* already have a value */
7967c478bd9Sstevel@tonic-gate 						if (bitset(SM_LDAP_SINGLEMATCH,
7977c478bd9Sstevel@tonic-gate 							   flags))
7987c478bd9Sstevel@tonic-gate 						{
7997c478bd9Sstevel@tonic-gate 							/* only wanted one match */
8007c478bd9Sstevel@tonic-gate 							SM_LDAP_ERROR_CLEANUP();
8017c478bd9Sstevel@tonic-gate 							errno = ENOENT;
8027c478bd9Sstevel@tonic-gate 							return EX_NOTFOUND;
8037c478bd9Sstevel@tonic-gate 						}
8047c478bd9Sstevel@tonic-gate 						break;
8057c478bd9Sstevel@tonic-gate 					}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 					if (lmap->ldap_attrsonly == LDAPMAP_TRUE)
8087c478bd9Sstevel@tonic-gate 					{
8097c478bd9Sstevel@tonic-gate 						*result = sm_rpool_strdup_x(rpool,
8107c478bd9Sstevel@tonic-gate 									    attr);
8117c478bd9Sstevel@tonic-gate 						ldap_memfree(attr);
8127c478bd9Sstevel@tonic-gate 						break;
8137c478bd9Sstevel@tonic-gate 					}
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 					if (vals[0] == NULL)
8167c478bd9Sstevel@tonic-gate 					{
8177c478bd9Sstevel@tonic-gate 						ldap_value_free(vals);
8187c478bd9Sstevel@tonic-gate 						ldap_memfree(attr);
8197c478bd9Sstevel@tonic-gate 						continue;
8207c478bd9Sstevel@tonic-gate 					}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 					vsize = strlen(vals[0]) + 1;
8237c478bd9Sstevel@tonic-gate 					if (lmap->ldap_attrsep != '\0')
8247c478bd9Sstevel@tonic-gate 						vsize += strlen(attr) + 1;
8257c478bd9Sstevel@tonic-gate 					*result = sm_rpool_malloc_x(rpool,
8267c478bd9Sstevel@tonic-gate 								    vsize);
8277c478bd9Sstevel@tonic-gate 					if (lmap->ldap_attrsep != '\0')
8287c478bd9Sstevel@tonic-gate 						sm_snprintf(*result, vsize,
8297c478bd9Sstevel@tonic-gate 							    "%s%c%s",
8307c478bd9Sstevel@tonic-gate 							    attr,
8317c478bd9Sstevel@tonic-gate 							    lmap->ldap_attrsep,
8327c478bd9Sstevel@tonic-gate 							    vals[0]);
8337c478bd9Sstevel@tonic-gate 					else
8347c478bd9Sstevel@tonic-gate 						sm_strlcpy(*result, vals[0],
8357c478bd9Sstevel@tonic-gate 							   vsize);
8367c478bd9Sstevel@tonic-gate 					ldap_value_free(vals);
8377c478bd9Sstevel@tonic-gate 					ldap_memfree(attr);
8387c478bd9Sstevel@tonic-gate 					break;
8397c478bd9Sstevel@tonic-gate 				}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 				/* attributes only */
8427c478bd9Sstevel@tonic-gate 				if (lmap->ldap_attrsonly == LDAPMAP_TRUE)
8437c478bd9Sstevel@tonic-gate 				{
8447c478bd9Sstevel@tonic-gate 					if (*result == NULL)
8457c478bd9Sstevel@tonic-gate 						*result = sm_rpool_strdup_x(rpool,
8467c478bd9Sstevel@tonic-gate 									    attr);
8477c478bd9Sstevel@tonic-gate 					else
8487c478bd9Sstevel@tonic-gate 					{
8497c478bd9Sstevel@tonic-gate 						if (bitset(SM_LDAP_SINGLEMATCH,
8507c478bd9Sstevel@tonic-gate 							   flags) &&
8517c478bd9Sstevel@tonic-gate 						    *result != NULL)
8527c478bd9Sstevel@tonic-gate 						{
8537c478bd9Sstevel@tonic-gate 							/* only wanted one match */
8547c478bd9Sstevel@tonic-gate 							SM_LDAP_ERROR_CLEANUP();
8557c478bd9Sstevel@tonic-gate 							errno = ENOENT;
8567c478bd9Sstevel@tonic-gate 							return EX_NOTFOUND;
8577c478bd9Sstevel@tonic-gate 						}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 						vsize = strlen(*result) +
8607c478bd9Sstevel@tonic-gate 							strlen(attr) + 2;
8617c478bd9Sstevel@tonic-gate 						tmp = sm_rpool_malloc_x(rpool,
8627c478bd9Sstevel@tonic-gate 									vsize);
8637c478bd9Sstevel@tonic-gate 						(void) sm_snprintf(tmp,
8647c478bd9Sstevel@tonic-gate 							vsize, "%s%c%s",
8657c478bd9Sstevel@tonic-gate 							*result, (char) delim,
8667c478bd9Sstevel@tonic-gate 							attr);
8677c478bd9Sstevel@tonic-gate 						*result = tmp;
8687c478bd9Sstevel@tonic-gate 					}
8697c478bd9Sstevel@tonic-gate 					ldap_memfree(attr);
8707c478bd9Sstevel@tonic-gate 					continue;
8717c478bd9Sstevel@tonic-gate 				}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 				/*
8747c478bd9Sstevel@tonic-gate 				**  If there is more than one, munge then
8757c478bd9Sstevel@tonic-gate 				**  into a map_coldelim separated string.
8767c478bd9Sstevel@tonic-gate 				**  If we are recursing we may have an entry
8777c478bd9Sstevel@tonic-gate 				**  with no 'normal' values to put in the
8787c478bd9Sstevel@tonic-gate 				**  string.
8797c478bd9Sstevel@tonic-gate 				**  This is not an error.
8807c478bd9Sstevel@tonic-gate 				*/
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 				if (type == SM_LDAP_ATTR_NORMAL &&
8837c478bd9Sstevel@tonic-gate 				    bitset(SM_LDAP_SINGLEMATCH, flags) &&
8847c478bd9Sstevel@tonic-gate 				    *result != NULL)
8857c478bd9Sstevel@tonic-gate 				{
8867c478bd9Sstevel@tonic-gate 					/* only wanted one match */
8877c478bd9Sstevel@tonic-gate 					SM_LDAP_ERROR_CLEANUP();
8887c478bd9Sstevel@tonic-gate 					errno = ENOENT;
8897c478bd9Sstevel@tonic-gate 					return EX_NOTFOUND;
8907c478bd9Sstevel@tonic-gate 				}
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 				vsize = 0;
8937c478bd9Sstevel@tonic-gate 				for (i = 0; vals[i] != NULL; i++)
8947c478bd9Sstevel@tonic-gate 				{
8957c478bd9Sstevel@tonic-gate 					if (type == SM_LDAP_ATTR_DN ||
8967c478bd9Sstevel@tonic-gate 					    type == SM_LDAP_ATTR_FILTER ||
8977c478bd9Sstevel@tonic-gate 					    type == SM_LDAP_ATTR_URL)
8987c478bd9Sstevel@tonic-gate 					{
8997c478bd9Sstevel@tonic-gate 						/* add to recursion */
9007c478bd9Sstevel@tonic-gate 						if (sm_ldap_add_recurse(&recurse,
9017c478bd9Sstevel@tonic-gate 									vals[i],
9027c478bd9Sstevel@tonic-gate 									type,
9037c478bd9Sstevel@tonic-gate 									rpool) == NULL)
9047c478bd9Sstevel@tonic-gate 						{
9057c478bd9Sstevel@tonic-gate 							SM_LDAP_ERROR_CLEANUP();
9067c478bd9Sstevel@tonic-gate 							errno = ENOMEM;
9077c478bd9Sstevel@tonic-gate 							return EX_OSERR;
9087c478bd9Sstevel@tonic-gate 						}
9097c478bd9Sstevel@tonic-gate 						continue;
9107c478bd9Sstevel@tonic-gate 					}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 					vsize += strlen(vals[i]) + 1;
9137c478bd9Sstevel@tonic-gate 					if (lmap->ldap_attrsep != '\0')
9147c478bd9Sstevel@tonic-gate 						vsize += strlen(attr) + 1;
9157c478bd9Sstevel@tonic-gate 				}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 				/*
9187c478bd9Sstevel@tonic-gate 				**  Create/Append to string any normal
9197c478bd9Sstevel@tonic-gate 				**  attribute values.  Otherwise, just free
9207c478bd9Sstevel@tonic-gate 				**  memory and move on to the next
9217c478bd9Sstevel@tonic-gate 				**  attribute in this entry.
9227c478bd9Sstevel@tonic-gate 				*/
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 				if (type == SM_LDAP_ATTR_NORMAL && vsize > 0)
9257c478bd9Sstevel@tonic-gate 				{
9267c478bd9Sstevel@tonic-gate 					char *pe;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 					/* Grow result string if needed */
9297c478bd9Sstevel@tonic-gate 					if ((*resultln + vsize) >= *resultsz)
9307c478bd9Sstevel@tonic-gate 					{
9317c478bd9Sstevel@tonic-gate 						while ((*resultln + vsize) >= *resultsz)
9327c478bd9Sstevel@tonic-gate 						{
9337c478bd9Sstevel@tonic-gate 							if (*resultsz == 0)
9347c478bd9Sstevel@tonic-gate 								*resultsz = 1024;
9357c478bd9Sstevel@tonic-gate 							else
9367c478bd9Sstevel@tonic-gate 								*resultsz *= 2;
9377c478bd9Sstevel@tonic-gate 						}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 						vp_tmp = sm_rpool_malloc_x(rpool, *resultsz);
9407c478bd9Sstevel@tonic-gate 						*vp_tmp = '\0';
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 						if (*result != NULL)
9437c478bd9Sstevel@tonic-gate 							sm_strlcpy(vp_tmp,
9447c478bd9Sstevel@tonic-gate 								   *result,
9457c478bd9Sstevel@tonic-gate 								   *resultsz);
9467c478bd9Sstevel@tonic-gate 						*result = vp_tmp;
9477c478bd9Sstevel@tonic-gate 					}
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 					p = *result + *resultln;
9507c478bd9Sstevel@tonic-gate 					pe = *result + *resultsz;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 					for (i = 0; vals[i] != NULL; i++)
9537c478bd9Sstevel@tonic-gate 					{
9547c478bd9Sstevel@tonic-gate 						if (*resultln > 0 &&
9557c478bd9Sstevel@tonic-gate 						    p < pe)
9567c478bd9Sstevel@tonic-gate 							*p++ = (char) delim;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 						if (lmap->ldap_attrsep != '\0')
9597c478bd9Sstevel@tonic-gate 						{
9607c478bd9Sstevel@tonic-gate 							p += sm_strlcpy(p, attr,
9617c478bd9Sstevel@tonic-gate 									pe - p);
9627c478bd9Sstevel@tonic-gate 							if (p < pe)
9637c478bd9Sstevel@tonic-gate 								*p++ = lmap->ldap_attrsep;
9647c478bd9Sstevel@tonic-gate 						}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 						p += sm_strlcpy(p, vals[i],
9677c478bd9Sstevel@tonic-gate 								pe - p);
9687c478bd9Sstevel@tonic-gate 						*resultln = p - (*result);
9697c478bd9Sstevel@tonic-gate 						if (p >= pe)
9707c478bd9Sstevel@tonic-gate 						{
9717c478bd9Sstevel@tonic-gate 							/* Internal error: buffer too small for LDAP values */
9727c478bd9Sstevel@tonic-gate 							SM_LDAP_ERROR_CLEANUP();
9737c478bd9Sstevel@tonic-gate 							errno = ENOMEM;
9747c478bd9Sstevel@tonic-gate 							return EX_OSERR;
9757c478bd9Sstevel@tonic-gate 						}
9767c478bd9Sstevel@tonic-gate 					}
9777c478bd9Sstevel@tonic-gate 				}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 				ldap_value_free(vals);
9807c478bd9Sstevel@tonic-gate 				ldap_memfree(attr);
9817c478bd9Sstevel@tonic-gate 			}
9827c478bd9Sstevel@tonic-gate 			save_errno = sm_ldap_geterrno(lmap->ldap_ld);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 			/*
9857c478bd9Sstevel@tonic-gate 			**  We check save_errno != LDAP_DECODING_ERROR since
9867c478bd9Sstevel@tonic-gate 			**  OpenLDAP 1.X has a very ugly *undocumented*
9877c478bd9Sstevel@tonic-gate 			**  hack of returning this error code from
9887c478bd9Sstevel@tonic-gate 			**  ldap_next_attribute() if the library freed the
9897c478bd9Sstevel@tonic-gate 			**  ber attribute.  See:
9907c478bd9Sstevel@tonic-gate 			**  http://www.openldap.org/lists/openldap-devel/9901/msg00064.html
9917c478bd9Sstevel@tonic-gate 			*/
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 			if (save_errno != LDAP_SUCCESS &&
9947c478bd9Sstevel@tonic-gate 			    save_errno != LDAP_DECODING_ERROR)
9957c478bd9Sstevel@tonic-gate 			{
9967c478bd9Sstevel@tonic-gate 				/* Must be an error */
9977c478bd9Sstevel@tonic-gate 				save_errno += E_LDAPBASE;
9987c478bd9Sstevel@tonic-gate 				SM_LDAP_ERROR_CLEANUP();
9997c478bd9Sstevel@tonic-gate 				errno = save_errno;
10007c478bd9Sstevel@tonic-gate 				return EX_TEMPFAIL;
10017c478bd9Sstevel@tonic-gate 			}
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 			/* mark this DN as done */
10047c478bd9Sstevel@tonic-gate 			rl->lr_done = true;
10057c478bd9Sstevel@tonic-gate 			if (rl->lr_ludp != NULL)
10067c478bd9Sstevel@tonic-gate 			{
10077c478bd9Sstevel@tonic-gate 				ldap_free_urldesc(rl->lr_ludp);
10087c478bd9Sstevel@tonic-gate 				rl->lr_ludp = NULL;
10097c478bd9Sstevel@tonic-gate 			}
10107c478bd9Sstevel@tonic-gate 			if (rl->lr_attrs != NULL)
10117c478bd9Sstevel@tonic-gate 			{
10127c478bd9Sstevel@tonic-gate 				free(rl->lr_attrs);
10137c478bd9Sstevel@tonic-gate 				rl->lr_attrs = NULL;
10147c478bd9Sstevel@tonic-gate 			}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 			/* We don't want multiple values and we have one */
10177c478bd9Sstevel@tonic-gate 			if ((char) delim == '\0' &&
10187c478bd9Sstevel@tonic-gate 			    !bitset(SM_LDAP_SINGLEMATCH, flags) &&
10197c478bd9Sstevel@tonic-gate 			    *result != NULL)
10207c478bd9Sstevel@tonic-gate 				break;
10217c478bd9Sstevel@tonic-gate 		}
10227c478bd9Sstevel@tonic-gate 		save_errno = sm_ldap_geterrno(lmap->ldap_ld);
10237c478bd9Sstevel@tonic-gate 		if (save_errno != LDAP_SUCCESS &&
10247c478bd9Sstevel@tonic-gate 		    save_errno != LDAP_DECODING_ERROR)
10257c478bd9Sstevel@tonic-gate 		{
10267c478bd9Sstevel@tonic-gate 			/* Must be an error */
10277c478bd9Sstevel@tonic-gate 			save_errno += E_LDAPBASE;
10287c478bd9Sstevel@tonic-gate 			SM_LDAP_ERROR_CLEANUP();
10297c478bd9Sstevel@tonic-gate 			errno = save_errno;
10307c478bd9Sstevel@tonic-gate 			return EX_TEMPFAIL;
10317c478bd9Sstevel@tonic-gate 		}
10327c478bd9Sstevel@tonic-gate 		ldap_msgfree(lmap->ldap_res);
10337c478bd9Sstevel@tonic-gate 		lmap->ldap_res = NULL;
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if (ret == 0)
10377c478bd9Sstevel@tonic-gate 		save_errno = ETIMEDOUT;
10387c478bd9Sstevel@tonic-gate 	else
10397c478bd9Sstevel@tonic-gate 		save_errno = sm_ldap_geterrno(lmap->ldap_ld);
10407c478bd9Sstevel@tonic-gate 	if (save_errno != LDAP_SUCCESS)
10417c478bd9Sstevel@tonic-gate 	{
10427c478bd9Sstevel@tonic-gate 		statp = EX_TEMPFAIL;
10437c478bd9Sstevel@tonic-gate 		if (ret != 0)
10447c478bd9Sstevel@tonic-gate 		{
10457c478bd9Sstevel@tonic-gate 			switch (save_errno)
10467c478bd9Sstevel@tonic-gate 			{
10477c478bd9Sstevel@tonic-gate #ifdef LDAP_SERVER_DOWN
10487c478bd9Sstevel@tonic-gate 			  case LDAP_SERVER_DOWN:
10497c478bd9Sstevel@tonic-gate #endif /* LDAP_SERVER_DOWN */
10507c478bd9Sstevel@tonic-gate 			  case LDAP_TIMEOUT:
10517c478bd9Sstevel@tonic-gate 			  case LDAP_UNAVAILABLE:
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 				/*
10547c478bd9Sstevel@tonic-gate 				**  server disappeared,
10557c478bd9Sstevel@tonic-gate 				**  try reopen on next search
10567c478bd9Sstevel@tonic-gate 				*/
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 				statp = EX_RESTART;
10597c478bd9Sstevel@tonic-gate 				break;
10607c478bd9Sstevel@tonic-gate 			}
10617c478bd9Sstevel@tonic-gate 			save_errno += E_LDAPBASE;
10627c478bd9Sstevel@tonic-gate 		}
10637c478bd9Sstevel@tonic-gate 		SM_LDAP_ERROR_CLEANUP();
10647c478bd9Sstevel@tonic-gate 		errno = save_errno;
10657c478bd9Sstevel@tonic-gate 		return statp;
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	if (lmap->ldap_res != NULL)
10697c478bd9Sstevel@tonic-gate 	{
10707c478bd9Sstevel@tonic-gate 		ldap_msgfree(lmap->ldap_res);
10717c478bd9Sstevel@tonic-gate 		lmap->ldap_res = NULL;
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (toplevel)
10757c478bd9Sstevel@tonic-gate 	{
10767c478bd9Sstevel@tonic-gate 		int rlidx;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		/*
10797c478bd9Sstevel@tonic-gate 		**  Spin through the built-up recurse list at the top
10807c478bd9Sstevel@tonic-gate 		**  of the recursion.  Since new items are added at the
10817c478bd9Sstevel@tonic-gate 		**  end of the shared list, we actually only ever get
10827c478bd9Sstevel@tonic-gate 		**  one level of recursion before things pop back to the
10837c478bd9Sstevel@tonic-gate 		**  top.  Any items added to the list during that recursion
10847c478bd9Sstevel@tonic-gate 		**  will be expanded by the top level.
10857c478bd9Sstevel@tonic-gate 		*/
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 		for (rlidx = 0; recurse != NULL && rlidx < recurse->lr_cnt; rlidx++)
10887c478bd9Sstevel@tonic-gate 		{
10897c478bd9Sstevel@tonic-gate 			int newflags;
10907c478bd9Sstevel@tonic-gate 			int sid;
10917c478bd9Sstevel@tonic-gate 			int status;
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 			rl = recurse->lr_data[rlidx];
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 			newflags = flags;
10967c478bd9Sstevel@tonic-gate 			if (rl->lr_done)
10977c478bd9Sstevel@tonic-gate 			{
10987c478bd9Sstevel@tonic-gate 				/* already expanded */
10997c478bd9Sstevel@tonic-gate 				continue;
11007c478bd9Sstevel@tonic-gate 			}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 			if (rl->lr_type == SM_LDAP_ATTR_DN)
11037c478bd9Sstevel@tonic-gate 			{
11047c478bd9Sstevel@tonic-gate 				/* do DN search */
11057c478bd9Sstevel@tonic-gate 				sid = ldap_search(lmap->ldap_ld,
11067c478bd9Sstevel@tonic-gate 						  rl->lr_search,
11077c478bd9Sstevel@tonic-gate 						  lmap->ldap_scope,
11087c478bd9Sstevel@tonic-gate 						  "(objectClass=*)",
11097c478bd9Sstevel@tonic-gate 						  (lmap->ldap_attr[0] == NULL ?
11107c478bd9Sstevel@tonic-gate 						   NULL : lmap->ldap_attr),
11117c478bd9Sstevel@tonic-gate 						  lmap->ldap_attrsonly);
11127c478bd9Sstevel@tonic-gate 			}
11137c478bd9Sstevel@tonic-gate 			else if (rl->lr_type == SM_LDAP_ATTR_FILTER)
11147c478bd9Sstevel@tonic-gate 			{
11157c478bd9Sstevel@tonic-gate 				/* do new search */
11167c478bd9Sstevel@tonic-gate 				sid = ldap_search(lmap->ldap_ld,
11177c478bd9Sstevel@tonic-gate 						  lmap->ldap_base,
11187c478bd9Sstevel@tonic-gate 						  lmap->ldap_scope,
11197c478bd9Sstevel@tonic-gate 						  rl->lr_search,
11207c478bd9Sstevel@tonic-gate 						  (lmap->ldap_attr[0] == NULL ?
11217c478bd9Sstevel@tonic-gate 						   NULL : lmap->ldap_attr),
11227c478bd9Sstevel@tonic-gate 						  lmap->ldap_attrsonly);
11237c478bd9Sstevel@tonic-gate 			}
11247c478bd9Sstevel@tonic-gate 			else if (rl->lr_type == SM_LDAP_ATTR_URL)
11257c478bd9Sstevel@tonic-gate 			{
11267c478bd9Sstevel@tonic-gate 				/* Parse URL */
11277c478bd9Sstevel@tonic-gate 				sid = ldap_url_parse(rl->lr_search,
11287c478bd9Sstevel@tonic-gate 						     &rl->lr_ludp);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 				if (sid != 0)
11317c478bd9Sstevel@tonic-gate 				{
11327c478bd9Sstevel@tonic-gate 					errno = sid + E_LDAPURLBASE;
11337c478bd9Sstevel@tonic-gate 					return EX_TEMPFAIL;
11347c478bd9Sstevel@tonic-gate 				}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 				/* We need to add objectClass */
11377c478bd9Sstevel@tonic-gate 				if (rl->lr_ludp->lud_attrs != NULL)
11387c478bd9Sstevel@tonic-gate 				{
11397c478bd9Sstevel@tonic-gate 					int attrnum = 0;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 					while (rl->lr_ludp->lud_attrs[attrnum] != NULL)
11427c478bd9Sstevel@tonic-gate 					{
11437c478bd9Sstevel@tonic-gate 						if (strcasecmp(rl->lr_ludp->lud_attrs[attrnum],
11447c478bd9Sstevel@tonic-gate 							       "objectClass") == 0)
11457c478bd9Sstevel@tonic-gate 						{
11467c478bd9Sstevel@tonic-gate 							/* already requested */
11477c478bd9Sstevel@tonic-gate 							attrnum = -1;
11487c478bd9Sstevel@tonic-gate 							break;
11497c478bd9Sstevel@tonic-gate 						}
11507c478bd9Sstevel@tonic-gate 						attrnum++;
11517c478bd9Sstevel@tonic-gate 					}
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 					if (attrnum >= 0)
11547c478bd9Sstevel@tonic-gate 					{
11557c478bd9Sstevel@tonic-gate 						int i;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 						rl->lr_attrs = (char **)malloc(sizeof(char *) * (attrnum + 2));
11587c478bd9Sstevel@tonic-gate 						if (rl->lr_attrs == NULL)
11597c478bd9Sstevel@tonic-gate 						{
11607c478bd9Sstevel@tonic-gate 							save_errno = errno;
11617c478bd9Sstevel@tonic-gate 							ldap_free_urldesc(rl->lr_ludp);
11627c478bd9Sstevel@tonic-gate 							errno = save_errno;
11637c478bd9Sstevel@tonic-gate 							return EX_TEMPFAIL;
11647c478bd9Sstevel@tonic-gate 						}
11657c478bd9Sstevel@tonic-gate 						for (i = 0 ; i < attrnum; i++)
11667c478bd9Sstevel@tonic-gate 						{
11677c478bd9Sstevel@tonic-gate 							rl->lr_attrs[i] = rl->lr_ludp->lud_attrs[i];
11687c478bd9Sstevel@tonic-gate 						}
11697c478bd9Sstevel@tonic-gate 						rl->lr_attrs[i++] = "objectClass";
11707c478bd9Sstevel@tonic-gate 						rl->lr_attrs[i++] = NULL;
11717c478bd9Sstevel@tonic-gate 					}
11727c478bd9Sstevel@tonic-gate 				}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 				/*
11757c478bd9Sstevel@tonic-gate 				**  Use the existing connection
11767c478bd9Sstevel@tonic-gate 				**  for this search.  It really
11777c478bd9Sstevel@tonic-gate 				**  should use lud_scheme://lud_host:lud_port/
11787c478bd9Sstevel@tonic-gate 				**  instead but that would require
11797c478bd9Sstevel@tonic-gate 				**  opening a new connection.
11807c478bd9Sstevel@tonic-gate 				**  This should be fixed ASAP.
11817c478bd9Sstevel@tonic-gate 				*/
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 				sid = ldap_search(lmap->ldap_ld,
11847c478bd9Sstevel@tonic-gate 						  rl->lr_ludp->lud_dn,
11857c478bd9Sstevel@tonic-gate 						  rl->lr_ludp->lud_scope,
11867c478bd9Sstevel@tonic-gate 						  rl->lr_ludp->lud_filter,
11877c478bd9Sstevel@tonic-gate 						  rl->lr_attrs,
11887c478bd9Sstevel@tonic-gate 						  lmap->ldap_attrsonly);
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 				/* Use the attributes specified by URL */
11917c478bd9Sstevel@tonic-gate 				newflags |= SM_LDAP_USE_ALLATTR;
11927c478bd9Sstevel@tonic-gate 			}
11937c478bd9Sstevel@tonic-gate 			else
11947c478bd9Sstevel@tonic-gate 			{
11957c478bd9Sstevel@tonic-gate 				/* unknown or illegal attribute type */
11967c478bd9Sstevel@tonic-gate 				errno = EFAULT;
11977c478bd9Sstevel@tonic-gate 				return EX_SOFTWARE;
11987c478bd9Sstevel@tonic-gate 			}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 			/* Collect results */
12017c478bd9Sstevel@tonic-gate 			if (sid == -1)
12027c478bd9Sstevel@tonic-gate 			{
12037c478bd9Sstevel@tonic-gate 				save_errno = sm_ldap_geterrno(lmap->ldap_ld);
12047c478bd9Sstevel@tonic-gate 				statp = EX_TEMPFAIL;
12057c478bd9Sstevel@tonic-gate 				switch (save_errno)
12067c478bd9Sstevel@tonic-gate 				{
12077c478bd9Sstevel@tonic-gate #ifdef LDAP_SERVER_DOWN
12087c478bd9Sstevel@tonic-gate 				  case LDAP_SERVER_DOWN:
12097c478bd9Sstevel@tonic-gate #endif /* LDAP_SERVER_DOWN */
12107c478bd9Sstevel@tonic-gate 				  case LDAP_TIMEOUT:
12117c478bd9Sstevel@tonic-gate 				  case LDAP_UNAVAILABLE:
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 					/*
12147c478bd9Sstevel@tonic-gate 					**  server disappeared,
12157c478bd9Sstevel@tonic-gate 					**  try reopen on next search
12167c478bd9Sstevel@tonic-gate 					*/
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 					statp = EX_RESTART;
12197c478bd9Sstevel@tonic-gate 					break;
12207c478bd9Sstevel@tonic-gate 				}
12217c478bd9Sstevel@tonic-gate 				errno = save_errno + E_LDAPBASE;
12227c478bd9Sstevel@tonic-gate 				return statp;
12237c478bd9Sstevel@tonic-gate 			}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 			status = sm_ldap_results(lmap, sid, newflags, delim,
12267c478bd9Sstevel@tonic-gate 						 rpool, result, resultln,
12277c478bd9Sstevel@tonic-gate 						 resultsz, recurse);
12287c478bd9Sstevel@tonic-gate 			save_errno = errno;
12297c478bd9Sstevel@tonic-gate 			if (status != EX_OK && status != EX_NOTFOUND)
12307c478bd9Sstevel@tonic-gate 			{
12317c478bd9Sstevel@tonic-gate 				errno = save_errno;
12327c478bd9Sstevel@tonic-gate 				return status;
12337c478bd9Sstevel@tonic-gate 			}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 			/* Mark as done */
12367c478bd9Sstevel@tonic-gate 			rl->lr_done = true;
12377c478bd9Sstevel@tonic-gate 			if (rl->lr_ludp != NULL)
12387c478bd9Sstevel@tonic-gate 			{
12397c478bd9Sstevel@tonic-gate 				ldap_free_urldesc(rl->lr_ludp);
12407c478bd9Sstevel@tonic-gate 				rl->lr_ludp = NULL;
12417c478bd9Sstevel@tonic-gate 			}
12427c478bd9Sstevel@tonic-gate 			if (rl->lr_attrs != NULL)
12437c478bd9Sstevel@tonic-gate 			{
12447c478bd9Sstevel@tonic-gate 				free(rl->lr_attrs);
12457c478bd9Sstevel@tonic-gate 				rl->lr_attrs = NULL;
12467c478bd9Sstevel@tonic-gate 			}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 			/* Reset rlidx as new items may have been added */
12497c478bd9Sstevel@tonic-gate 			rlidx = -1;
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 	return statp;
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate /*
12567c478bd9Sstevel@tonic-gate **  SM_LDAP_CLOSE -- close LDAP connection
12577c478bd9Sstevel@tonic-gate **
12587c478bd9Sstevel@tonic-gate **	Parameters:
12597c478bd9Sstevel@tonic-gate **		lmap -- LDAP map information
12607c478bd9Sstevel@tonic-gate **
12617c478bd9Sstevel@tonic-gate **	Returns:
12627c478bd9Sstevel@tonic-gate **		None.
12637c478bd9Sstevel@tonic-gate **
12647c478bd9Sstevel@tonic-gate */
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate void
12677c478bd9Sstevel@tonic-gate sm_ldap_close(lmap)
12687c478bd9Sstevel@tonic-gate 	SM_LDAP_STRUCT *lmap;
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	if (lmap->ldap_ld == NULL)
12717c478bd9Sstevel@tonic-gate 		return;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	if (lmap->ldap_pid == getpid())
12747c478bd9Sstevel@tonic-gate 		ldap_unbind(lmap->ldap_ld);
12757c478bd9Sstevel@tonic-gate 	lmap->ldap_ld = NULL;
12767c478bd9Sstevel@tonic-gate 	lmap->ldap_pid = 0;
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate /*
12807c478bd9Sstevel@tonic-gate **  SM_LDAP_SETOPTS -- set LDAP options
12817c478bd9Sstevel@tonic-gate **
12827c478bd9Sstevel@tonic-gate **	Parameters:
12837c478bd9Sstevel@tonic-gate **		ld -- LDAP session handle
12847c478bd9Sstevel@tonic-gate **		lmap -- LDAP map information
12857c478bd9Sstevel@tonic-gate **
12867c478bd9Sstevel@tonic-gate **	Returns:
12877c478bd9Sstevel@tonic-gate **		None.
12887c478bd9Sstevel@tonic-gate **
12897c478bd9Sstevel@tonic-gate */
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate void
12927c478bd9Sstevel@tonic-gate sm_ldap_setopts(ld, lmap)
12937c478bd9Sstevel@tonic-gate 	LDAP *ld;
12947c478bd9Sstevel@tonic-gate 	SM_LDAP_STRUCT *lmap;
12957c478bd9Sstevel@tonic-gate {
12967c478bd9Sstevel@tonic-gate # if USE_LDAP_SET_OPTION
12977c478bd9Sstevel@tonic-gate 	if (lmap->ldap_version != 0)
12987c478bd9Sstevel@tonic-gate 	{
12997c478bd9Sstevel@tonic-gate 		ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION,
13007c478bd9Sstevel@tonic-gate 				&lmap->ldap_version);
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 	ldap_set_option(ld, LDAP_OPT_DEREF, &lmap->ldap_deref);
13037c478bd9Sstevel@tonic-gate 	if (bitset(LDAP_OPT_REFERRALS, lmap->ldap_options))
13047c478bd9Sstevel@tonic-gate 		ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
13057c478bd9Sstevel@tonic-gate 	else
13067c478bd9Sstevel@tonic-gate 		ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
13077c478bd9Sstevel@tonic-gate 	ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &lmap->ldap_sizelimit);
13087c478bd9Sstevel@tonic-gate 	ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &lmap->ldap_timelimit);
13097c478bd9Sstevel@tonic-gate #  ifdef LDAP_OPT_RESTART
13107c478bd9Sstevel@tonic-gate 	ldap_set_option(ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
13117c478bd9Sstevel@tonic-gate #  endif /* LDAP_OPT_RESTART */
13127c478bd9Sstevel@tonic-gate # else /* USE_LDAP_SET_OPTION */
13137c478bd9Sstevel@tonic-gate 	/* From here on in we can use ldap internal timelimits */
13147c478bd9Sstevel@tonic-gate 	ld->ld_deref = lmap->ldap_deref;
13157c478bd9Sstevel@tonic-gate 	ld->ld_options = lmap->ldap_options;
13167c478bd9Sstevel@tonic-gate 	ld->ld_sizelimit = lmap->ldap_sizelimit;
13177c478bd9Sstevel@tonic-gate 	ld->ld_timelimit = lmap->ldap_timelimit;
13187c478bd9Sstevel@tonic-gate # endif /* USE_LDAP_SET_OPTION */
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate **  SM_LDAP_GETERRNO -- get ldap errno value
13237c478bd9Sstevel@tonic-gate **
13247c478bd9Sstevel@tonic-gate **	Parameters:
13257c478bd9Sstevel@tonic-gate **		ld -- LDAP session handle
13267c478bd9Sstevel@tonic-gate **
13277c478bd9Sstevel@tonic-gate **	Returns:
13287c478bd9Sstevel@tonic-gate **		LDAP errno.
13297c478bd9Sstevel@tonic-gate **
13307c478bd9Sstevel@tonic-gate */
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate int
13337c478bd9Sstevel@tonic-gate sm_ldap_geterrno(ld)
13347c478bd9Sstevel@tonic-gate 	LDAP *ld;
13357c478bd9Sstevel@tonic-gate {
13367c478bd9Sstevel@tonic-gate 	int err = LDAP_SUCCESS;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate # if defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3
13397c478bd9Sstevel@tonic-gate 	(void) ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &err);
13407c478bd9Sstevel@tonic-gate # else /* defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3 */
13417c478bd9Sstevel@tonic-gate #  ifdef LDAP_OPT_SIZELIMIT
13427c478bd9Sstevel@tonic-gate 	err = ldap_get_lderrno(ld, NULL, NULL);
13437c478bd9Sstevel@tonic-gate #  else /* LDAP_OPT_SIZELIMIT */
13447c478bd9Sstevel@tonic-gate 	err = ld->ld_errno;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	/*
13477c478bd9Sstevel@tonic-gate 	**  Reset value to prevent lingering LDAP_DECODING_ERROR due to
13487c478bd9Sstevel@tonic-gate 	**  OpenLDAP 1.X's hack (see above)
13497c478bd9Sstevel@tonic-gate 	*/
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	ld->ld_errno = LDAP_SUCCESS;
13527c478bd9Sstevel@tonic-gate #  endif /* LDAP_OPT_SIZELIMIT */
13537c478bd9Sstevel@tonic-gate # endif /* defined(LDAP_VERSION_MAX) && LDAP_VERSION_MAX >= 3 */
13547c478bd9Sstevel@tonic-gate 	return err;
13557c478bd9Sstevel@tonic-gate }
13567c478bd9Sstevel@tonic-gate # endif /* LDAPMAP */
1357