17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate /*
87c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
97c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
107c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
117c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
147c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
157c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
167c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
197c478bd9Sstevel@tonic-gate  * March 31, 1998.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
227c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
237c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
247c478bd9Sstevel@tonic-gate  * Rights Reserved.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * Contributor(s):
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  *  Copyright (c) 1995 Regents of the University of Michigan.
307c478bd9Sstevel@tonic-gate  *  All rights reserved.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *  open.c
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if 0
37*c94f4b03SToomas Soome #ifndef lint
387c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "ldap-int.h"
437c478bd9Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
447c478bd9Sstevel@tonic-gate /* Valid for any ANSI C compiler */
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define VI_PRODUCTVERSION 3
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifndef INADDR_LOOPBACK
517c478bd9Sstevel@tonic-gate #define INADDR_LOOPBACK	((unsigned long) 0x7f000001)
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN
557c478bd9Sstevel@tonic-gate #define MAXHOSTNAMELEN  64
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
597c478bd9Sstevel@tonic-gate int	ldap_debug;
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * global defaults for callbacks are stored here.  callers of the API set
657c478bd9Sstevel@tonic-gate  *    these by passing a NULL "ld" to ldap_set_option().  Everything in
667c478bd9Sstevel@tonic-gate  *    nsldapi_ld_defaults can be overridden on a per-ld basis as well (the
677c478bd9Sstevel@tonic-gate  *    memory allocation functions are global to all ld's).
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate struct ldap                     nsldapi_ld_defaults;
707c478bd9Sstevel@tonic-gate struct ldap_memalloc_fns        nsldapi_memalloc_fns = { 0, 0, 0, 0 };
717c478bd9Sstevel@tonic-gate int				nsldapi_initialized = 0;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #ifndef _WINDOWS
747c478bd9Sstevel@tonic-gate #include <pthread.h>
757c478bd9Sstevel@tonic-gate static pthread_key_t		nsldapi_key;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate struct nsldapi_ldap_error {
787c478bd9Sstevel@tonic-gate         int     le_errno;
797c478bd9Sstevel@tonic-gate         char    *le_matched;
807c478bd9Sstevel@tonic-gate         char    *le_errmsg;
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate #else
837c478bd9Sstevel@tonic-gate __declspec ( thread ) int	nsldapi_gldaperrno;
847c478bd9Sstevel@tonic-gate __declspec ( thread ) char	*nsldapi_gmatched = NULL;
85*c94f4b03SToomas Soome __declspec ( thread ) char	*nsldapi_gldaperror = NULL;
867c478bd9Sstevel@tonic-gate #endif /* _WINDOWS */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef _WINDOWS
897c478bd9Sstevel@tonic-gate #define	LDAP_MUTEX_T	HANDLE
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int
pthread_mutex_init(LDAP_MUTEX_T * mp,void * attr)927c478bd9Sstevel@tonic-gate pthread_mutex_init( LDAP_MUTEX_T *mp, void *attr)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate         if ( (*mp = CreateMutex(NULL, FALSE, NULL)) == NULL )
957c478bd9Sstevel@tonic-gate                 return( 1 );
967c478bd9Sstevel@tonic-gate         else
977c478bd9Sstevel@tonic-gate                 return( 0 );
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static void *
pthread_mutex_alloc(void)1017c478bd9Sstevel@tonic-gate pthread_mutex_alloc( void )
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate         LDAP_MUTEX_T *mutexp;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate         if ( (mutexp = malloc( sizeof(LDAP_MUTEX_T) )) != NULL ) {
1067c478bd9Sstevel@tonic-gate                 pthread_mutex_init( mutexp, NULL );
1077c478bd9Sstevel@tonic-gate         }
1087c478bd9Sstevel@tonic-gate         return( mutexp );
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate int
pthread_mutex_destroy(LDAP_MUTEX_T * mp)1127c478bd9Sstevel@tonic-gate pthread_mutex_destroy( LDAP_MUTEX_T *mp )
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate         if ( !(CloseHandle(*mp)) )
1157c478bd9Sstevel@tonic-gate                 return( 1 );
1167c478bd9Sstevel@tonic-gate         else
1177c478bd9Sstevel@tonic-gate                 return( 0 );
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static void
pthread_mutex_free(void * mutexp)1217c478bd9Sstevel@tonic-gate pthread_mutex_free( void *mutexp )
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate         pthread_mutex_destroy( (LDAP_MUTEX_T *) mutexp );
1247c478bd9Sstevel@tonic-gate         free( mutexp );
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate int
pthread_mutex_lock(LDAP_MUTEX_T * mp)1287c478bd9Sstevel@tonic-gate pthread_mutex_lock( LDAP_MUTEX_T *mp )
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate         if ( (WaitForSingleObject(*mp, INFINITE) != WAIT_OBJECT_0) )
1317c478bd9Sstevel@tonic-gate                 return( 1 );
1327c478bd9Sstevel@tonic-gate         else
1337c478bd9Sstevel@tonic-gate                 return( 0 );
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate int
pthread_mutex_unlock(LDAP_MUTEX_T * mp)1377c478bd9Sstevel@tonic-gate pthread_mutex_unlock( LDAP_MUTEX_T *mp )
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate         if ( !(ReleaseMutex(*mp)) )
1407c478bd9Sstevel@tonic-gate                 return( 1 );
1417c478bd9Sstevel@tonic-gate         else
1427c478bd9Sstevel@tonic-gate                 return( 0 );
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static int
get_errno(void)1467c478bd9Sstevel@tonic-gate get_errno( void )
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	return errno;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static void
set_errno(int Errno)1527c478bd9Sstevel@tonic-gate set_errno( int Errno )
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	errno = Errno;
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static int
get_ld_error(char ** LDMatched,char ** LDError,void * Args)1587c478bd9Sstevel@tonic-gate get_ld_error( char **LDMatched, char **LDError, void * Args )
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	if ( LDMatched != NULL )
1617c478bd9Sstevel@tonic-gate 	{
1627c478bd9Sstevel@tonic-gate 		*LDMatched = nsldapi_gmatched;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	if ( LDError != NULL )
1657c478bd9Sstevel@tonic-gate 	{
1667c478bd9Sstevel@tonic-gate 		*LDError = nsldapi_gldaperror;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	return nsldapi_gldaperrno;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static void
set_ld_error(int LDErrno,char * LDMatched,char * LDError,void * Args)1727c478bd9Sstevel@tonic-gate set_ld_error( int LDErrno, char *  LDMatched, char *  LDError,
1737c478bd9Sstevel@tonic-gate 	void *  Args )
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	/* Clean up any previous string storage. */
1767c478bd9Sstevel@tonic-gate 	if ( nsldapi_gmatched != NULL )
1777c478bd9Sstevel@tonic-gate 	{
1787c478bd9Sstevel@tonic-gate 		ldap_memfree( nsldapi_gmatched );
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	if ( nsldapi_gldaperror != NULL )
1817c478bd9Sstevel@tonic-gate 	{
1827c478bd9Sstevel@tonic-gate 		ldap_memfree( nsldapi_gldaperror );
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	nsldapi_gldaperrno  = LDErrno;
1867c478bd9Sstevel@tonic-gate 	nsldapi_gmatched    = LDMatched;
1877c478bd9Sstevel@tonic-gate 	nsldapi_gldaperror  = LDError;
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate #else
1907c478bd9Sstevel@tonic-gate static void *
pthread_mutex_alloc(void)1917c478bd9Sstevel@tonic-gate pthread_mutex_alloc( void )
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	pthread_mutex_t *mutexp;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if ( (mutexp = malloc( sizeof(pthread_mutex_t) )) != NULL ) {
1967c478bd9Sstevel@tonic-gate 		pthread_mutex_init( mutexp, NULL );
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	return( mutexp );
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static void
pthread_mutex_free(void * mutexp)2027c478bd9Sstevel@tonic-gate pthread_mutex_free( void *mutexp )
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	pthread_mutex_destroy( (pthread_mutex_t *) mutexp );
2057c478bd9Sstevel@tonic-gate 	free( mutexp );
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static void
set_ld_error(int err,char * matched,char * errmsg,void * dummy)2097c478bd9Sstevel@tonic-gate set_ld_error( int err, char *matched, char *errmsg, void *dummy )
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate         struct nsldapi_ldap_error *le;
2127c478bd9Sstevel@tonic-gate         void *tsd;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate         le = pthread_getspecific( nsldapi_key );
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate         if (le == NULL) {
2177c478bd9Sstevel@tonic-gate                 tsd = (void *)calloc(1, sizeof(struct nsldapi_ldap_error));
2187c478bd9Sstevel@tonic-gate                 pthread_setspecific( nsldapi_key, tsd );
2197c478bd9Sstevel@tonic-gate         }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate         le = pthread_getspecific( nsldapi_key );
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate         if (le == NULL) {
2247c478bd9Sstevel@tonic-gate 		free(tsd);
2257c478bd9Sstevel@tonic-gate                 return;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate         le->le_errno = err;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate         if ( le->le_matched != NULL ) {
2317c478bd9Sstevel@tonic-gate                 ldap_memfree( le->le_matched );
2327c478bd9Sstevel@tonic-gate         }
2337c478bd9Sstevel@tonic-gate         le->le_matched = matched;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate         if ( le->le_errmsg != NULL ) {
2367c478bd9Sstevel@tonic-gate                 ldap_memfree( le->le_errmsg );
2377c478bd9Sstevel@tonic-gate         }
2387c478bd9Sstevel@tonic-gate         le->le_errmsg = errmsg;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static int
get_ld_error(char ** matched,char ** errmsg,void * dummy)2427c478bd9Sstevel@tonic-gate get_ld_error( char **matched, char **errmsg, void *dummy )
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate         struct nsldapi_ldap_error *le;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate         le = pthread_getspecific( nsldapi_key );
2477c478bd9Sstevel@tonic-gate 	if (le != NULL) {
248*c94f4b03SToomas Soome 		if ( matched != NULL ) {
249*c94f4b03SToomas Soome 			*matched = le->le_matched;
250*c94f4b03SToomas Soome 		}
251*c94f4b03SToomas Soome 		if ( errmsg != NULL ) {
252*c94f4b03SToomas Soome 			*errmsg = le->le_errmsg;
253*c94f4b03SToomas Soome 		}
254*c94f4b03SToomas Soome 		return( le->le_errno );
2557c478bd9Sstevel@tonic-gate 	} else {
256*c94f4b03SToomas Soome 		if ( matched != NULL )
257*c94f4b03SToomas Soome 			*matched = NULL;
258*c94f4b03SToomas Soome 		if ( errmsg != NULL )
259*c94f4b03SToomas Soome 			*errmsg = NULL;
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	return (LDAP_SUCCESS);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static void
set_errno(int err)2657c478bd9Sstevel@tonic-gate set_errno( int err )
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate         errno = err;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static int
get_errno(void)2717c478bd9Sstevel@tonic-gate get_errno( void )
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate         return( errno );
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate #endif /* _WINDOWS */
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static struct ldap_thread_fns
2787c478bd9Sstevel@tonic-gate 	nsldapi_default_thread_fns = {
2797c478bd9Sstevel@tonic-gate 		(void *(*)(void))pthread_mutex_alloc,
2807c478bd9Sstevel@tonic-gate 		(void (*)(void *))pthread_mutex_free,
2817c478bd9Sstevel@tonic-gate 		(int (*)(void *))pthread_mutex_lock,
2827c478bd9Sstevel@tonic-gate 		(int (*)(void *))pthread_mutex_unlock,
2837c478bd9Sstevel@tonic-gate                 (int (*)(void))get_errno,
2847c478bd9Sstevel@tonic-gate                 (void (*)(int))set_errno,
2857c478bd9Sstevel@tonic-gate                 (int (*)(char **, char **, void *))get_ld_error,
2867c478bd9Sstevel@tonic-gate                 (void (*)(int, char *, char *, void *))set_ld_error,
2877c478bd9Sstevel@tonic-gate 		0 };
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate static struct ldap_extra_thread_fns
2907c478bd9Sstevel@tonic-gate 	nsldapi_default_extra_thread_fns = {
2917c478bd9Sstevel@tonic-gate 		0, 0, 0, 0, 0,
2927c478bd9Sstevel@tonic-gate #ifdef _WINDOWS
2937c478bd9Sstevel@tonic-gate 		0
2947c478bd9Sstevel@tonic-gate #else
295*c94f4b03SToomas Soome 		(void *(*)(void))(uintptr_t)pthread_self
2967c478bd9Sstevel@tonic-gate #endif /* _WINDOWS */
2977c478bd9Sstevel@tonic-gate 		};
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate void
nsldapi_initialize_defaults(void)3007c478bd9Sstevel@tonic-gate nsldapi_initialize_defaults( void )
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if ( nsldapi_initialized ) {
3047c478bd9Sstevel@tonic-gate 		return;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate #ifdef	_SOLARIS_SDK
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * This has to be called before nsldapi_initialized is set to 1
3097c478bd9Sstevel@tonic-gate 	 * because nsldapi_initialized does not have mutex protection
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	prldap_nspr_init();
3127c478bd9Sstevel@tonic-gate #endif
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate #ifndef _WINDOWS
3157c478bd9Sstevel@tonic-gate         if ( pthread_key_create(&nsldapi_key, free ) != 0) {
3167c478bd9Sstevel@tonic-gate                 perror("pthread_key_create");
3177c478bd9Sstevel@tonic-gate         }
3187c478bd9Sstevel@tonic-gate #endif /* _WINDOWS */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	nsldapi_initialized = 1;
3217c478bd9Sstevel@tonic-gate 	memset( &nsldapi_memalloc_fns, 0, sizeof( nsldapi_memalloc_fns ));
3227c478bd9Sstevel@tonic-gate 	memset( &nsldapi_ld_defaults, 0, sizeof( nsldapi_ld_defaults ));
3237c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_options = LDAP_BITOPT_REFERRALS;
3247c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_version = LDAP_VERSION2;
3257c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_lberoptions = LBER_OPT_USE_DER;
3267c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
3297c478bd9Sstevel@tonic-gate 	/* SASL default option settings */
3307c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_def_sasl_mech = NULL;
3317c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_def_sasl_realm = NULL;
3327c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_def_sasl_authcid = NULL;
3337c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_def_sasl_authzid = NULL;
3347c478bd9Sstevel@tonic-gate 	/* SASL Security properties */
3357c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_sasl_secprops.max_ssf = UINT_MAX;
3367c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
3377c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_sasl_secprops.security_flags =
3387c478bd9Sstevel@tonic-gate 		SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
3397c478bd9Sstevel@tonic-gate #endif
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
3427c478bd9Sstevel@tonic-gate 	nsldapi_ld_defaults.ld_lberoptions |= LBER_OPT_TRANSLATE_STRINGS;
3437c478bd9Sstevel@tonic-gate #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
3447c478bd9Sstevel@tonic-gate 	ldap_set_string_translators( &nsldapi_ld_defaults, ldap_8859_to_t61,
3457c478bd9Sstevel@tonic-gate 	    ldap_t61_to_8859 );
3467c478bd9Sstevel@tonic-gate #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
3477c478bd9Sstevel@tonic-gate #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate         /* set default connect timeout (in milliseconds) */
3507c478bd9Sstevel@tonic-gate         /* this was picked as it is the standard tcp timeout as well */
3517c478bd9Sstevel@tonic-gate         nsldapi_ld_defaults.ld_connect_timeout = LDAP_X_IO_TIMEOUT_NO_TIMEOUT;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate         /* load up default platform specific locking routines */
3547c478bd9Sstevel@tonic-gate         if (ldap_set_option( NULL, LDAP_OPT_THREAD_FN_PTRS,
3557c478bd9Sstevel@tonic-gate                 (void *)&nsldapi_default_thread_fns) != LDAP_SUCCESS) {
3567c478bd9Sstevel@tonic-gate                 return;
3577c478bd9Sstevel@tonic-gate         }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate #ifndef _WINDOWS
3607c478bd9Sstevel@tonic-gate         /* load up default threadid function */
3617c478bd9Sstevel@tonic-gate         if (ldap_set_option( NULL, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
3627c478bd9Sstevel@tonic-gate                 (void *)&nsldapi_default_extra_thread_fns) != LDAP_SUCCESS) {
3637c478bd9Sstevel@tonic-gate                 return;
3647c478bd9Sstevel@tonic-gate         }
3657c478bd9Sstevel@tonic-gate #endif /* _WINDOWS */
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * ldap_version - report version levels for important properties
3717c478bd9Sstevel@tonic-gate  * This function is deprecated.  Use ldap_get_option( ..., LDAP_OPT_API_INFO,
3727c478bd9Sstevel@tonic-gate  *	... ) instead.
3737c478bd9Sstevel@tonic-gate  *
3747c478bd9Sstevel@tonic-gate  * Example:
3757c478bd9Sstevel@tonic-gate  *	LDAPVersion ver;
3767c478bd9Sstevel@tonic-gate  *	ldap_version( &ver );
3777c478bd9Sstevel@tonic-gate  *  if ( (ver.sdk_version < 100) || (ver.SSL_version < 300) )
3787c478bd9Sstevel@tonic-gate  *      fprintf( stderr, "LDAP SDK level insufficient\n" );
3797c478bd9Sstevel@tonic-gate  *
3807c478bd9Sstevel@tonic-gate  * or:
3817c478bd9Sstevel@tonic-gate  *  if ( ldap_version(NULL) < 100 )
3827c478bd9Sstevel@tonic-gate  *      fprintf( stderr, "LDAP SDK level insufficient\n" );
3837c478bd9Sstevel@tonic-gate  *
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate int
3877c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_version(LDAPVersion * ver)3887c478bd9Sstevel@tonic-gate ldap_version( LDAPVersion *ver )
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	if ( NULL != ver )
3917c478bd9Sstevel@tonic-gate 	{
3927c478bd9Sstevel@tonic-gate 		memset( ver, 0, sizeof(*ver) );
3937c478bd9Sstevel@tonic-gate 		ver->sdk_version = (int)(VI_PRODUCTVERSION * 100);
3947c478bd9Sstevel@tonic-gate 		ver->protocol_version = LDAP_VERSION_MAX * 100;
3957c478bd9Sstevel@tonic-gate 		ver->SSL_version = SSL_VERSION * 100;
396*c94f4b03SToomas Soome 		/*
397*c94f4b03SToomas Soome 		 * set security to none by default
3987c478bd9Sstevel@tonic-gate 		 */
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		ver->security_level = LDAP_SECURITY_NONE;
4017c478bd9Sstevel@tonic-gate #if defined(LINK_SSL)
4027c478bd9Sstevel@tonic-gate 		ver->security_level = 128;
4037c478bd9Sstevel@tonic-gate #endif
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 	return (int)(VI_PRODUCTVERSION * 100);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
4117c478bd9Sstevel@tonic-gate  * be used for future communication is returned on success, NULL on failure.
4127c478bd9Sstevel@tonic-gate  * "host" may be a space-separated list of hosts or IP addresses
4137c478bd9Sstevel@tonic-gate  *
4147c478bd9Sstevel@tonic-gate  * Example:
4157c478bd9Sstevel@tonic-gate  *	LDAP	*ld;
4167c478bd9Sstevel@tonic-gate  *	ld = ldap_open( hostname, port );
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate LDAP *
4207c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_open(const char * host,int port)4217c478bd9Sstevel@tonic-gate ldap_open( const char *host, int port )
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	LDAP	*ld;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (( ld = ldap_init( host, port )) == NULL ) {
4287c478bd9Sstevel@tonic-gate 		return( NULL );
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
4327c478bd9Sstevel@tonic-gate 	if ( nsldapi_open_ldap_defconn( ld ) < 0 ) {
4337c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
4347c478bd9Sstevel@tonic-gate 		ldap_ld_free( ld, NULL, NULL, 0 );
4357c478bd9Sstevel@tonic-gate 		return( NULL );
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
4397c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
4407c478bd9Sstevel@tonic-gate 		( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	return( ld );
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
4487c478bd9Sstevel@tonic-gate  * future communication is returned on success, NULL on failure.
4497c478bd9Sstevel@tonic-gate  * "defhost" may be a space-separated list of hosts or IP addresses
4507c478bd9Sstevel@tonic-gate  *
4517c478bd9Sstevel@tonic-gate  * Example:
4527c478bd9Sstevel@tonic-gate  *	LDAP	*ld;
4537c478bd9Sstevel@tonic-gate  *	ld = ldap_init( default_hostname, default_port );
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate LDAP *
4567c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_init(const char * defhost,int defport)4577c478bd9Sstevel@tonic-gate ldap_init( const char *defhost, int defport )
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	LDAP	*ld;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if ( !nsldapi_initialized ) {
4627c478bd9Sstevel@tonic-gate 		nsldapi_initialize_defaults();
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if ( defport < 0 || defport > LDAP_PORT_MAX ) {
4667c478bd9Sstevel@tonic-gate 	    LDAPDebug( LDAP_DEBUG_ANY,
4677c478bd9Sstevel@tonic-gate 		    "ldap_init: port %d is invalid (port numbers must range from 1 to %d)\n",
4687c478bd9Sstevel@tonic-gate 		    defport, LDAP_PORT_MAX, 0 );
4697c478bd9Sstevel@tonic-gate #if !defined( macintosh ) && !defined( DOS )
4707c478bd9Sstevel@tonic-gate 	    errno = EINVAL;
4717c478bd9Sstevel@tonic-gate #endif
4727c478bd9Sstevel@tonic-gate 	    return( NULL );
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if ( (ld = (LDAP*)NSLDAPI_MALLOC( sizeof(struct ldap) )) == NULL ) {
4787c478bd9Sstevel@tonic-gate 		return( NULL );
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/* copy defaults */
4827c478bd9Sstevel@tonic-gate 	SAFEMEMCPY( ld, &nsldapi_ld_defaults, sizeof( struct ldap ));
4837c478bd9Sstevel@tonic-gate 	if ( nsldapi_ld_defaults.ld_io_fns_ptr != NULL ) {
4847c478bd9Sstevel@tonic-gate 		if (( ld->ld_io_fns_ptr = (struct ldap_io_fns *)NSLDAPI_MALLOC(
4857c478bd9Sstevel@tonic-gate 		    sizeof( struct ldap_io_fns ))) == NULL ) {
4867c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( (char *)ld );
4877c478bd9Sstevel@tonic-gate 			return( NULL );
4887c478bd9Sstevel@tonic-gate 		}
4897c478bd9Sstevel@tonic-gate 		/* struct copy */
4907c478bd9Sstevel@tonic-gate 		*(ld->ld_io_fns_ptr) = *(nsldapi_ld_defaults.ld_io_fns_ptr);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	/* call the new handle I/O callback if one is defined */
4947c478bd9Sstevel@tonic-gate 	if ( ld->ld_extnewhandle_fn != NULL ) {
4957c478bd9Sstevel@tonic-gate 		/*
4967c478bd9Sstevel@tonic-gate 		 * We always pass the session extended I/O argument to
4977c478bd9Sstevel@tonic-gate 		 * the new handle callback.
4987c478bd9Sstevel@tonic-gate 		 */
4997c478bd9Sstevel@tonic-gate 		if ( ld->ld_extnewhandle_fn( ld, ld->ld_ext_session_arg )
5007c478bd9Sstevel@tonic-gate 		    != LDAP_SUCCESS ) {
5017c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( (char*)ld );
5027c478bd9Sstevel@tonic-gate 			return( NULL );
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/* allocate session-specific resources */
5077c478bd9Sstevel@tonic-gate 	if (( ld->ld_sbp = ber_sockbuf_alloc()) == NULL ||
5087c478bd9Sstevel@tonic-gate 	    ( defhost != NULL &&
5097c478bd9Sstevel@tonic-gate 	    ( ld->ld_defhost = nsldapi_strdup( defhost )) == NULL ) ||
5107c478bd9Sstevel@tonic-gate 	    ((ld->ld_mutex = (void **) NSLDAPI_CALLOC( LDAP_MAX_LOCK, sizeof(void *))) == NULL )) {
5117c478bd9Sstevel@tonic-gate 		if ( ld->ld_sbp != NULL ) {
5127c478bd9Sstevel@tonic-gate 			ber_sockbuf_free( ld->ld_sbp );
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 		if( ld->ld_mutex != NULL ) {
5157c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( ld->ld_mutex );
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( (char*)ld );
5187c478bd9Sstevel@tonic-gate 		return( NULL );
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* install Sockbuf I/O functions if set in LDAP * */
5227c478bd9Sstevel@tonic-gate 	if ( ld->ld_extread_fn != NULL || ld->ld_extwrite_fn != NULL ) {
5237c478bd9Sstevel@tonic-gate 		struct lber_x_ext_io_fns lberiofns;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		memset( &lberiofns, 0, sizeof( lberiofns ));
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		lberiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
5287c478bd9Sstevel@tonic-gate 		lberiofns.lbextiofn_read = ld->ld_extread_fn;
5297c478bd9Sstevel@tonic-gate 		lberiofns.lbextiofn_write = ld->ld_extwrite_fn;
5307c478bd9Sstevel@tonic-gate 		lberiofns.lbextiofn_writev = ld->ld_extwritev_fn;
5317c478bd9Sstevel@tonic-gate 		lberiofns.lbextiofn_socket_arg = NULL;
5327c478bd9Sstevel@tonic-gate 		ber_sockbuf_set_option( ld->ld_sbp, LBER_SOCKBUF_OPT_EXT_IO_FNS,
5337c478bd9Sstevel@tonic-gate 			(void *)&lberiofns );
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
5377c478bd9Sstevel@tonic-gate 	/* Install the functions for IPv6 support	*/
5387c478bd9Sstevel@tonic-gate 	/* code sequencing is critical from here to nsldapi_mutex_alloc_all */
5397c478bd9Sstevel@tonic-gate         if ( prldap_install_thread_functions( ld, 1 ) != 0 ||
5407c478bd9Sstevel@tonic-gate              prldap_install_io_functions( ld, 1 ) != 0 ||
5417c478bd9Sstevel@tonic-gate              prldap_install_dns_functions( ld ) != 0 ) {
5427c478bd9Sstevel@tonic-gate 		/* go through ld and free resources */
5437c478bd9Sstevel@tonic-gate 		ldap_unbind( ld );
5447c478bd9Sstevel@tonic-gate 		ld = NULL;
5457c478bd9Sstevel@tonic-gate 		return( NULL );
5467c478bd9Sstevel@tonic-gate         }
5477c478bd9Sstevel@tonic-gate #else
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/* allocate mutexes */
5507c478bd9Sstevel@tonic-gate 	nsldapi_mutex_alloc_all( ld );
5517c478bd9Sstevel@tonic-gate #endif
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/* set default port */
5547c478bd9Sstevel@tonic-gate 	ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	return( ld );
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate void
nsldapi_mutex_alloc_all(LDAP * ld)5607c478bd9Sstevel@tonic-gate nsldapi_mutex_alloc_all( LDAP *ld )
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate         int     i;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate         if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
5657c478bd9Sstevel@tonic-gate                 for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
5667c478bd9Sstevel@tonic-gate                         ld->ld_mutex[i] = LDAP_MUTEX_ALLOC( ld );
5677c478bd9Sstevel@tonic-gate                         ld->ld_mutex_threadid[i] = (void *) -1;
5687c478bd9Sstevel@tonic-gate                         ld->ld_mutex_refcnt[i] = 0;
5697c478bd9Sstevel@tonic-gate                 }
5707c478bd9Sstevel@tonic-gate         }
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate void
nsldapi_mutex_free_all(LDAP * ld)5757c478bd9Sstevel@tonic-gate nsldapi_mutex_free_all( LDAP *ld )
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate         int     i;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate         if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
5807c478bd9Sstevel@tonic-gate                 for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
5817c478bd9Sstevel@tonic-gate                         LDAP_MUTEX_FREE( ld, ld->ld_mutex[i] );
5827c478bd9Sstevel@tonic-gate                 }
5837c478bd9Sstevel@tonic-gate         }
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /* returns 0 if connection opened and -1 if an error occurs */
5877c478bd9Sstevel@tonic-gate int
nsldapi_open_ldap_defconn(LDAP * ld)5887c478bd9Sstevel@tonic-gate nsldapi_open_ldap_defconn( LDAP *ld )
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	LDAPServer	*srv;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer ))) ==
5937c478bd9Sstevel@tonic-gate 	    NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
5947c478bd9Sstevel@tonic-gate 	    nsldapi_strdup( ld->ld_defhost )) == NULL )) {
5957c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
5967c478bd9Sstevel@tonic-gate 		return( -1 );
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 	srv->lsrv_port = ld->ld_defport;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate #ifdef LDAP_SSLIO_HOOKS
6017c478bd9Sstevel@tonic-gate 	if (( ld->ld_options & LDAP_BITOPT_SSL ) != 0 ) {
6027c478bd9Sstevel@tonic-gate 		srv->lsrv_options |= LDAP_SRV_OPT_SECURE;
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate #endif
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (( ld->ld_defconn = nsldapi_new_connection( ld, &srv, 1, 1, 0 ))
6077c478bd9Sstevel@tonic-gate 	    == NULL ) {
6087c478bd9Sstevel@tonic-gate 		if ( ld->ld_defhost != NULL ) {
6097c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( srv->lsrv_host );
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( (char *)srv );
6127c478bd9Sstevel@tonic-gate 		return( -1 );
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 	++ld->ld_defconn->lconn_refcnt;	/* so it never gets closed/freed */
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	return( 0 );
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate struct ldap_x_hostlist_status {
6217c478bd9Sstevel@tonic-gate 	char	*lhs_hostlist;
6227c478bd9Sstevel@tonic-gate 	char	*lhs_nexthost;
6237c478bd9Sstevel@tonic-gate 	int	lhs_defport;
6247c478bd9Sstevel@tonic-gate };
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * Return the first host and port in hostlist (setting *hostp and *portp).
6287c478bd9Sstevel@tonic-gate  * Return value is an LDAP API error code (LDAP_SUCCESS if all goes well).
6297c478bd9Sstevel@tonic-gate  * Note that a NULL or zero-length hostlist causes the host "127.0.0.1" to
6307c478bd9Sstevel@tonic-gate  * be returned.
6317c478bd9Sstevel@tonic-gate  */
6327c478bd9Sstevel@tonic-gate int LDAP_CALL
ldap_x_hostlist_first(const char * hostlist,int defport,char ** hostp,int * portp,struct ldap_x_hostlist_status ** statusp)6337c478bd9Sstevel@tonic-gate ldap_x_hostlist_first( const char *hostlist, int defport, char **hostp,
6347c478bd9Sstevel@tonic-gate     int *portp, struct ldap_x_hostlist_status **statusp )
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if ( NULL == hostp || NULL == portp || NULL == statusp ) {
6387c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if ( NULL == hostlist || *hostlist == '\0' ) {
6427c478bd9Sstevel@tonic-gate 		*hostp = nsldapi_strdup( "127.0.0.1" );
6437c478bd9Sstevel@tonic-gate 		if ( NULL == *hostp ) {
6447c478bd9Sstevel@tonic-gate 			return( LDAP_NO_MEMORY );
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 		*portp = defport;
6477c478bd9Sstevel@tonic-gate 		*statusp = NULL;
6487c478bd9Sstevel@tonic-gate 		return( LDAP_SUCCESS );
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	*statusp = NSLDAPI_CALLOC( 1, sizeof( struct ldap_x_hostlist_status ));
6527c478bd9Sstevel@tonic-gate 	if ( NULL == *statusp ) {
6537c478bd9Sstevel@tonic-gate 		return( LDAP_NO_MEMORY );
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	(*statusp)->lhs_hostlist = nsldapi_strdup( hostlist );
6567c478bd9Sstevel@tonic-gate 	if ( NULL == (*statusp)->lhs_hostlist ) {
6577c478bd9Sstevel@tonic-gate 		return( LDAP_NO_MEMORY );
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 	(*statusp)->lhs_nexthost = (*statusp)->lhs_hostlist;
6607c478bd9Sstevel@tonic-gate 	(*statusp)->lhs_defport = defport;
6617c478bd9Sstevel@tonic-gate 	return( ldap_x_hostlist_next( hostp, portp, *statusp ));
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * Return the next host and port in hostlist (setting *hostp and *portp).
6667c478bd9Sstevel@tonic-gate  * Return value is an LDAP API error code (LDAP_SUCCESS if all goes well).
6677c478bd9Sstevel@tonic-gate  * If no more hosts are available, LDAP_SUCCESS is returned but *hostp is set
6687c478bd9Sstevel@tonic-gate  * to NULL.
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate int LDAP_CALL
ldap_x_hostlist_next(char ** hostp,int * portp,struct ldap_x_hostlist_status * status)6717c478bd9Sstevel@tonic-gate ldap_x_hostlist_next( char **hostp, int *portp,
6727c478bd9Sstevel@tonic-gate 	struct ldap_x_hostlist_status *status )
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	char	*q;
6757c478bd9Sstevel@tonic-gate 	int		squarebrackets = 0;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if ( NULL == hostp || NULL == portp ) {
6787c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if ( NULL == status || NULL == status->lhs_nexthost ) {
6827c478bd9Sstevel@tonic-gate 		*hostp = NULL;
6837c478bd9Sstevel@tonic-gate 		return( LDAP_SUCCESS );
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * skip past leading '[' if present (IPv6 addresses may be surrounded
6887c478bd9Sstevel@tonic-gate 	 * with square brackets, e.g., [fe80::a00:20ff:fee5:c0b4]:389
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	if ( status->lhs_nexthost[0] == '[' ) {
6917c478bd9Sstevel@tonic-gate 		++status->lhs_nexthost;
6927c478bd9Sstevel@tonic-gate 		squarebrackets = 1;
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/* copy host into *hostp */
6967c478bd9Sstevel@tonic-gate 	if ( NULL != ( q = strchr( status->lhs_nexthost, ' ' ))) {
6977c478bd9Sstevel@tonic-gate 		size_t	len = q - status->lhs_nexthost;
6987c478bd9Sstevel@tonic-gate 		*hostp = NSLDAPI_MALLOC( len + 1 );
6997c478bd9Sstevel@tonic-gate 		if ( NULL == *hostp ) {
7007c478bd9Sstevel@tonic-gate 			return( LDAP_NO_MEMORY );
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 		strncpy( *hostp, status->lhs_nexthost, len );
7037c478bd9Sstevel@tonic-gate 		(*hostp)[len] = '\0';
7047c478bd9Sstevel@tonic-gate 		status->lhs_nexthost += ( len + 1 );
7057c478bd9Sstevel@tonic-gate 	} else {	/* last host */
7067c478bd9Sstevel@tonic-gate 		*hostp = nsldapi_strdup( status->lhs_nexthost );
7077c478bd9Sstevel@tonic-gate 		if ( NULL == *hostp ) {
7087c478bd9Sstevel@tonic-gate 			return( LDAP_NO_MEMORY );
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 		status->lhs_nexthost = NULL;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
713*c94f4b03SToomas Soome 	/*
7147c478bd9Sstevel@tonic-gate 	 * Look for closing ']' and skip past it before looking for port.
7157c478bd9Sstevel@tonic-gate 	 */
7167c478bd9Sstevel@tonic-gate 	if ( squarebrackets && NULL != ( q = strchr( *hostp, ']' ))) {
7177c478bd9Sstevel@tonic-gate 		*q++ = '\0';
7187c478bd9Sstevel@tonic-gate 	} else {
7197c478bd9Sstevel@tonic-gate 		q = *hostp;
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/* determine and set port */
7237c478bd9Sstevel@tonic-gate 	if ( NULL != ( q = strchr( q, ':' ))) {
7247c478bd9Sstevel@tonic-gate 		*q++ = '\0';
7257c478bd9Sstevel@tonic-gate 		*portp = atoi( q );
7267c478bd9Sstevel@tonic-gate 	} else {
7277c478bd9Sstevel@tonic-gate 		*portp = status->lhs_defport;
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	return( LDAP_SUCCESS );
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate void LDAP_CALL
ldap_x_hostlist_statusfree(struct ldap_x_hostlist_status * status)7357c478bd9Sstevel@tonic-gate ldap_x_hostlist_statusfree( struct ldap_x_hostlist_status *status )
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	if ( NULL != status ) {
7387c478bd9Sstevel@tonic-gate 		if ( NULL != status->lhs_hostlist ) {
7397c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE( status->lhs_hostlist );
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( status );
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * memory allocation functions.  we include these in open.c since every
7497c478bd9Sstevel@tonic-gate  *    LDAP application is likely to pull the rest of the code in this file
7507c478bd9Sstevel@tonic-gate  *    in anyways.
7517c478bd9Sstevel@tonic-gate  */
7527c478bd9Sstevel@tonic-gate void *
ldap_x_malloc(size_t size)7537c478bd9Sstevel@tonic-gate ldap_x_malloc( size_t size )
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate 	return( nsldapi_memalloc_fns.ldapmem_malloc == NULL ?
7567c478bd9Sstevel@tonic-gate 	    malloc( size ) :
7577c478bd9Sstevel@tonic-gate 	    nsldapi_memalloc_fns.ldapmem_malloc( size ));
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate void *
ldap_x_calloc(size_t nelem,size_t elsize)7627c478bd9Sstevel@tonic-gate ldap_x_calloc( size_t nelem, size_t elsize )
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate 	return( nsldapi_memalloc_fns.ldapmem_calloc == NULL ?
7657c478bd9Sstevel@tonic-gate 	    calloc(  nelem, elsize ) :
7667c478bd9Sstevel@tonic-gate 	    nsldapi_memalloc_fns.ldapmem_calloc( nelem, elsize ));
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate void *
ldap_x_realloc(void * ptr,size_t size)7717c478bd9Sstevel@tonic-gate ldap_x_realloc( void *ptr, size_t size )
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	return( nsldapi_memalloc_fns.ldapmem_realloc == NULL ?
7747c478bd9Sstevel@tonic-gate 	    realloc( ptr, size ) :
7757c478bd9Sstevel@tonic-gate 	    nsldapi_memalloc_fns.ldapmem_realloc( ptr, size ));
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate void
ldap_x_free(void * ptr)7807c478bd9Sstevel@tonic-gate ldap_x_free( void *ptr )
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	if ( nsldapi_memalloc_fns.ldapmem_free == NULL ) {
7837c478bd9Sstevel@tonic-gate 		free( ptr );
7847c478bd9Sstevel@tonic-gate 	} else {
7857c478bd9Sstevel@tonic-gate 		nsldapi_memalloc_fns.ldapmem_free( ptr );
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /* if s is NULL, returns NULL */
7917c478bd9Sstevel@tonic-gate char *
nsldapi_strdup(const char * s)7927c478bd9Sstevel@tonic-gate nsldapi_strdup( const char *s )
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	char	*p;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if ( s == NULL ||
7977c478bd9Sstevel@tonic-gate 	    (p = (char *)NSLDAPI_MALLOC( strlen( s ) + 1 )) == NULL )
7987c478bd9Sstevel@tonic-gate 		return( NULL );
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	strcpy( p, s );
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	return( p );
8037c478bd9Sstevel@tonic-gate }
804