1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3*0Sstevel@tonic-gate * Use is subject to license terms.
4*0Sstevel@tonic-gate */
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
11*0Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
12*0Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
13*0Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
14*0Sstevel@tonic-gate *
15*0Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
16*0Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17*0Sstevel@tonic-gate * implied. See the License for the specific language governing
18*0Sstevel@tonic-gate * rights and limitations under the License.
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
21*0Sstevel@tonic-gate * March 31, 1998.
22*0Sstevel@tonic-gate *
23*0Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
24*0Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
25*0Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
26*0Sstevel@tonic-gate * Rights Reserved.
27*0Sstevel@tonic-gate *
28*0Sstevel@tonic-gate * Contributor(s):
29*0Sstevel@tonic-gate */
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate * Copyright (c) 1995 Regents of the University of Michigan.
32*0Sstevel@tonic-gate * All rights reserved.
33*0Sstevel@tonic-gate */
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate * open.c
36*0Sstevel@tonic-gate */
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate #if 0
39*0Sstevel@tonic-gate #ifndef lint
40*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
41*0Sstevel@tonic-gate #endif
42*0Sstevel@tonic-gate #endif
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate #include "ldap-int.h"
45*0Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
46*0Sstevel@tonic-gate /* Valid for any ANSI C compiler */
47*0Sstevel@tonic-gate #include <limits.h>
48*0Sstevel@tonic-gate #endif
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate #define VI_PRODUCTVERSION 3
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate #ifndef INADDR_LOOPBACK
53*0Sstevel@tonic-gate #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
54*0Sstevel@tonic-gate #endif
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN
57*0Sstevel@tonic-gate #define MAXHOSTNAMELEN 64
58*0Sstevel@tonic-gate #endif
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
61*0Sstevel@tonic-gate int ldap_debug;
62*0Sstevel@tonic-gate #endif
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate * global defaults for callbacks are stored here. callers of the API set
67*0Sstevel@tonic-gate * these by passing a NULL "ld" to ldap_set_option(). Everything in
68*0Sstevel@tonic-gate * nsldapi_ld_defaults can be overridden on a per-ld basis as well (the
69*0Sstevel@tonic-gate * memory allocation functions are global to all ld's).
70*0Sstevel@tonic-gate */
71*0Sstevel@tonic-gate struct ldap nsldapi_ld_defaults;
72*0Sstevel@tonic-gate struct ldap_memalloc_fns nsldapi_memalloc_fns = { 0, 0, 0, 0 };
73*0Sstevel@tonic-gate int nsldapi_initialized = 0;
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate #ifndef _WINDOWS
76*0Sstevel@tonic-gate #include <pthread.h>
77*0Sstevel@tonic-gate static pthread_key_t nsldapi_key;
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate struct nsldapi_ldap_error {
80*0Sstevel@tonic-gate int le_errno;
81*0Sstevel@tonic-gate char *le_matched;
82*0Sstevel@tonic-gate char *le_errmsg;
83*0Sstevel@tonic-gate };
84*0Sstevel@tonic-gate #else
85*0Sstevel@tonic-gate __declspec ( thread ) int nsldapi_gldaperrno;
86*0Sstevel@tonic-gate __declspec ( thread ) char *nsldapi_gmatched = NULL;
87*0Sstevel@tonic-gate __declspec ( thread ) char *nsldapi_gldaperror = NULL;
88*0Sstevel@tonic-gate #endif /* _WINDOWS */
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate #ifdef _WINDOWS
91*0Sstevel@tonic-gate #define LDAP_MUTEX_T HANDLE
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate int
pthread_mutex_init(LDAP_MUTEX_T * mp,void * attr)94*0Sstevel@tonic-gate pthread_mutex_init( LDAP_MUTEX_T *mp, void *attr)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate if ( (*mp = CreateMutex(NULL, FALSE, NULL)) == NULL )
97*0Sstevel@tonic-gate return( 1 );
98*0Sstevel@tonic-gate else
99*0Sstevel@tonic-gate return( 0 );
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate static void *
pthread_mutex_alloc(void)103*0Sstevel@tonic-gate pthread_mutex_alloc( void )
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate LDAP_MUTEX_T *mutexp;
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate if ( (mutexp = malloc( sizeof(LDAP_MUTEX_T) )) != NULL ) {
108*0Sstevel@tonic-gate pthread_mutex_init( mutexp, NULL );
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate return( mutexp );
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate int
pthread_mutex_destroy(LDAP_MUTEX_T * mp)114*0Sstevel@tonic-gate pthread_mutex_destroy( LDAP_MUTEX_T *mp )
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate if ( !(CloseHandle(*mp)) )
117*0Sstevel@tonic-gate return( 1 );
118*0Sstevel@tonic-gate else
119*0Sstevel@tonic-gate return( 0 );
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate static void
pthread_mutex_free(void * mutexp)123*0Sstevel@tonic-gate pthread_mutex_free( void *mutexp )
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate pthread_mutex_destroy( (LDAP_MUTEX_T *) mutexp );
126*0Sstevel@tonic-gate free( mutexp );
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate int
pthread_mutex_lock(LDAP_MUTEX_T * mp)130*0Sstevel@tonic-gate pthread_mutex_lock( LDAP_MUTEX_T *mp )
131*0Sstevel@tonic-gate {
132*0Sstevel@tonic-gate if ( (WaitForSingleObject(*mp, INFINITE) != WAIT_OBJECT_0) )
133*0Sstevel@tonic-gate return( 1 );
134*0Sstevel@tonic-gate else
135*0Sstevel@tonic-gate return( 0 );
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate int
pthread_mutex_unlock(LDAP_MUTEX_T * mp)139*0Sstevel@tonic-gate pthread_mutex_unlock( LDAP_MUTEX_T *mp )
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate if ( !(ReleaseMutex(*mp)) )
142*0Sstevel@tonic-gate return( 1 );
143*0Sstevel@tonic-gate else
144*0Sstevel@tonic-gate return( 0 );
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate static int
get_errno(void)148*0Sstevel@tonic-gate get_errno( void )
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate return errno;
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate static void
set_errno(int Errno)154*0Sstevel@tonic-gate set_errno( int Errno )
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate errno = Errno;
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate static int
get_ld_error(char ** LDMatched,char ** LDError,void * Args)160*0Sstevel@tonic-gate get_ld_error( char **LDMatched, char **LDError, void * Args )
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate if ( LDMatched != NULL )
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate *LDMatched = nsldapi_gmatched;
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate if ( LDError != NULL )
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate *LDError = nsldapi_gldaperror;
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate return nsldapi_gldaperrno;
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gate static void
set_ld_error(int LDErrno,char * LDMatched,char * LDError,void * Args)174*0Sstevel@tonic-gate set_ld_error( int LDErrno, char * LDMatched, char * LDError,
175*0Sstevel@tonic-gate void * Args )
176*0Sstevel@tonic-gate {
177*0Sstevel@tonic-gate /* Clean up any previous string storage. */
178*0Sstevel@tonic-gate if ( nsldapi_gmatched != NULL )
179*0Sstevel@tonic-gate {
180*0Sstevel@tonic-gate ldap_memfree( nsldapi_gmatched );
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate if ( nsldapi_gldaperror != NULL )
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate ldap_memfree( nsldapi_gldaperror );
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate nsldapi_gldaperrno = LDErrno;
188*0Sstevel@tonic-gate nsldapi_gmatched = LDMatched;
189*0Sstevel@tonic-gate nsldapi_gldaperror = LDError;
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate #else
192*0Sstevel@tonic-gate static void *
pthread_mutex_alloc(void)193*0Sstevel@tonic-gate pthread_mutex_alloc( void )
194*0Sstevel@tonic-gate {
195*0Sstevel@tonic-gate pthread_mutex_t *mutexp;
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate if ( (mutexp = malloc( sizeof(pthread_mutex_t) )) != NULL ) {
198*0Sstevel@tonic-gate pthread_mutex_init( mutexp, NULL );
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate return( mutexp );
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate static void
pthread_mutex_free(void * mutexp)204*0Sstevel@tonic-gate pthread_mutex_free( void *mutexp )
205*0Sstevel@tonic-gate {
206*0Sstevel@tonic-gate pthread_mutex_destroy( (pthread_mutex_t *) mutexp );
207*0Sstevel@tonic-gate free( mutexp );
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate static void
set_ld_error(int err,char * matched,char * errmsg,void * dummy)211*0Sstevel@tonic-gate set_ld_error( int err, char *matched, char *errmsg, void *dummy )
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate struct nsldapi_ldap_error *le;
214*0Sstevel@tonic-gate void *tsd;
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate le = pthread_getspecific( nsldapi_key );
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate if (le == NULL) {
219*0Sstevel@tonic-gate tsd = (void *)calloc(1, sizeof(struct nsldapi_ldap_error));
220*0Sstevel@tonic-gate pthread_setspecific( nsldapi_key, tsd );
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate le = pthread_getspecific( nsldapi_key );
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate if (le == NULL) {
226*0Sstevel@tonic-gate free(tsd);
227*0Sstevel@tonic-gate return;
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate le->le_errno = err;
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate if ( le->le_matched != NULL ) {
233*0Sstevel@tonic-gate ldap_memfree( le->le_matched );
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate le->le_matched = matched;
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate if ( le->le_errmsg != NULL ) {
238*0Sstevel@tonic-gate ldap_memfree( le->le_errmsg );
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate le->le_errmsg = errmsg;
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate static int
get_ld_error(char ** matched,char ** errmsg,void * dummy)244*0Sstevel@tonic-gate get_ld_error( char **matched, char **errmsg, void *dummy )
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate struct nsldapi_ldap_error *le;
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate le = pthread_getspecific( nsldapi_key );
249*0Sstevel@tonic-gate if (le != NULL) {
250*0Sstevel@tonic-gate if ( matched != NULL ) {
251*0Sstevel@tonic-gate *matched = le->le_matched;
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate if ( errmsg != NULL ) {
254*0Sstevel@tonic-gate *errmsg = le->le_errmsg;
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate return( le->le_errno );
257*0Sstevel@tonic-gate } else {
258*0Sstevel@tonic-gate if ( matched != NULL )
259*0Sstevel@tonic-gate *matched = NULL;
260*0Sstevel@tonic-gate if ( errmsg != NULL )
261*0Sstevel@tonic-gate *errmsg = NULL;
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate return (LDAP_SUCCESS);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate static void
set_errno(int err)267*0Sstevel@tonic-gate set_errno( int err )
268*0Sstevel@tonic-gate {
269*0Sstevel@tonic-gate errno = err;
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate
272*0Sstevel@tonic-gate static int
get_errno(void)273*0Sstevel@tonic-gate get_errno( void )
274*0Sstevel@tonic-gate {
275*0Sstevel@tonic-gate return( errno );
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate #endif /* _WINDOWS */
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate static struct ldap_thread_fns
280*0Sstevel@tonic-gate nsldapi_default_thread_fns = {
281*0Sstevel@tonic-gate (void *(*)(void))pthread_mutex_alloc,
282*0Sstevel@tonic-gate (void (*)(void *))pthread_mutex_free,
283*0Sstevel@tonic-gate (int (*)(void *))pthread_mutex_lock,
284*0Sstevel@tonic-gate (int (*)(void *))pthread_mutex_unlock,
285*0Sstevel@tonic-gate (int (*)(void))get_errno,
286*0Sstevel@tonic-gate (void (*)(int))set_errno,
287*0Sstevel@tonic-gate (int (*)(char **, char **, void *))get_ld_error,
288*0Sstevel@tonic-gate (void (*)(int, char *, char *, void *))set_ld_error,
289*0Sstevel@tonic-gate 0 };
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate static struct ldap_extra_thread_fns
292*0Sstevel@tonic-gate nsldapi_default_extra_thread_fns = {
293*0Sstevel@tonic-gate 0, 0, 0, 0, 0,
294*0Sstevel@tonic-gate #ifdef _WINDOWS
295*0Sstevel@tonic-gate 0
296*0Sstevel@tonic-gate #else
297*0Sstevel@tonic-gate (void *(*)(void))pthread_self
298*0Sstevel@tonic-gate #endif /* _WINDOWS */
299*0Sstevel@tonic-gate };
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate void
nsldapi_initialize_defaults(void)302*0Sstevel@tonic-gate nsldapi_initialize_defaults( void )
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gate if ( nsldapi_initialized ) {
306*0Sstevel@tonic-gate return;
307*0Sstevel@tonic-gate }
308*0Sstevel@tonic-gate #ifdef _SOLARIS_SDK
309*0Sstevel@tonic-gate /*
310*0Sstevel@tonic-gate * This has to be called before nsldapi_initialized is set to 1
311*0Sstevel@tonic-gate * because nsldapi_initialized does not have mutex protection
312*0Sstevel@tonic-gate */
313*0Sstevel@tonic-gate prldap_nspr_init();
314*0Sstevel@tonic-gate #endif
315*0Sstevel@tonic-gate
316*0Sstevel@tonic-gate #ifndef _WINDOWS
317*0Sstevel@tonic-gate if ( pthread_key_create(&nsldapi_key, free ) != 0) {
318*0Sstevel@tonic-gate perror("pthread_key_create");
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate #endif /* _WINDOWS */
321*0Sstevel@tonic-gate
322*0Sstevel@tonic-gate nsldapi_initialized = 1;
323*0Sstevel@tonic-gate memset( &nsldapi_memalloc_fns, 0, sizeof( nsldapi_memalloc_fns ));
324*0Sstevel@tonic-gate memset( &nsldapi_ld_defaults, 0, sizeof( nsldapi_ld_defaults ));
325*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_options = LDAP_BITOPT_REFERRALS;
326*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_version = LDAP_VERSION2;
327*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_lberoptions = LBER_OPT_USE_DER;
328*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
331*0Sstevel@tonic-gate /* SASL default option settings */
332*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_def_sasl_mech = NULL;
333*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_def_sasl_realm = NULL;
334*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_def_sasl_authcid = NULL;
335*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_def_sasl_authzid = NULL;
336*0Sstevel@tonic-gate /* SASL Security properties */
337*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_sasl_secprops.max_ssf = UINT_MAX;
338*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
339*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_sasl_secprops.security_flags =
340*0Sstevel@tonic-gate SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
341*0Sstevel@tonic-gate #endif
342*0Sstevel@tonic-gate
343*0Sstevel@tonic-gate #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
344*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_lberoptions |= LBER_OPT_TRANSLATE_STRINGS;
345*0Sstevel@tonic-gate #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
346*0Sstevel@tonic-gate ldap_set_string_translators( &nsldapi_ld_defaults, ldap_8859_to_t61,
347*0Sstevel@tonic-gate ldap_t61_to_8859 );
348*0Sstevel@tonic-gate #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
349*0Sstevel@tonic-gate #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
350*0Sstevel@tonic-gate
351*0Sstevel@tonic-gate /* set default connect timeout (in milliseconds) */
352*0Sstevel@tonic-gate /* this was picked as it is the standard tcp timeout as well */
353*0Sstevel@tonic-gate nsldapi_ld_defaults.ld_connect_timeout = LDAP_X_IO_TIMEOUT_NO_TIMEOUT;
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gate /* load up default platform specific locking routines */
356*0Sstevel@tonic-gate if (ldap_set_option( NULL, LDAP_OPT_THREAD_FN_PTRS,
357*0Sstevel@tonic-gate (void *)&nsldapi_default_thread_fns) != LDAP_SUCCESS) {
358*0Sstevel@tonic-gate return;
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate #ifndef _WINDOWS
362*0Sstevel@tonic-gate /* load up default threadid function */
363*0Sstevel@tonic-gate if (ldap_set_option( NULL, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
364*0Sstevel@tonic-gate (void *)&nsldapi_default_extra_thread_fns) != LDAP_SUCCESS) {
365*0Sstevel@tonic-gate return;
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate #endif /* _WINDOWS */
368*0Sstevel@tonic-gate }
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate /*
372*0Sstevel@tonic-gate * ldap_version - report version levels for important properties
373*0Sstevel@tonic-gate * This function is deprecated. Use ldap_get_option( ..., LDAP_OPT_API_INFO,
374*0Sstevel@tonic-gate * ... ) instead.
375*0Sstevel@tonic-gate *
376*0Sstevel@tonic-gate * Example:
377*0Sstevel@tonic-gate * LDAPVersion ver;
378*0Sstevel@tonic-gate * ldap_version( &ver );
379*0Sstevel@tonic-gate * if ( (ver.sdk_version < 100) || (ver.SSL_version < 300) )
380*0Sstevel@tonic-gate * fprintf( stderr, "LDAP SDK level insufficient\n" );
381*0Sstevel@tonic-gate *
382*0Sstevel@tonic-gate * or:
383*0Sstevel@tonic-gate * if ( ldap_version(NULL) < 100 )
384*0Sstevel@tonic-gate * fprintf( stderr, "LDAP SDK level insufficient\n" );
385*0Sstevel@tonic-gate *
386*0Sstevel@tonic-gate */
387*0Sstevel@tonic-gate
388*0Sstevel@tonic-gate int
389*0Sstevel@tonic-gate LDAP_CALL
ldap_version(LDAPVersion * ver)390*0Sstevel@tonic-gate ldap_version( LDAPVersion *ver )
391*0Sstevel@tonic-gate {
392*0Sstevel@tonic-gate if ( NULL != ver )
393*0Sstevel@tonic-gate {
394*0Sstevel@tonic-gate memset( ver, 0, sizeof(*ver) );
395*0Sstevel@tonic-gate ver->sdk_version = (int)(VI_PRODUCTVERSION * 100);
396*0Sstevel@tonic-gate ver->protocol_version = LDAP_VERSION_MAX * 100;
397*0Sstevel@tonic-gate ver->SSL_version = SSL_VERSION * 100;
398*0Sstevel@tonic-gate /*
399*0Sstevel@tonic-gate * set security to none by default
400*0Sstevel@tonic-gate */
401*0Sstevel@tonic-gate
402*0Sstevel@tonic-gate ver->security_level = LDAP_SECURITY_NONE;
403*0Sstevel@tonic-gate #if defined(LINK_SSL)
404*0Sstevel@tonic-gate #if defined(NS_DOMESTIC)
405*0Sstevel@tonic-gate ver->security_level = 128;
406*0Sstevel@tonic-gate #elif defined(NSS_EXPORT)
407*0Sstevel@tonic-gate ver->security_level = 40;
408*0Sstevel@tonic-gate #endif
409*0Sstevel@tonic-gate #endif
410*0Sstevel@tonic-gate
411*0Sstevel@tonic-gate }
412*0Sstevel@tonic-gate return (int)(VI_PRODUCTVERSION * 100);
413*0Sstevel@tonic-gate }
414*0Sstevel@tonic-gate
415*0Sstevel@tonic-gate /*
416*0Sstevel@tonic-gate * ldap_open - initialize and connect to an ldap server. A magic cookie to
417*0Sstevel@tonic-gate * be used for future communication is returned on success, NULL on failure.
418*0Sstevel@tonic-gate * "host" may be a space-separated list of hosts or IP addresses
419*0Sstevel@tonic-gate *
420*0Sstevel@tonic-gate * Example:
421*0Sstevel@tonic-gate * LDAP *ld;
422*0Sstevel@tonic-gate * ld = ldap_open( hostname, port );
423*0Sstevel@tonic-gate */
424*0Sstevel@tonic-gate
425*0Sstevel@tonic-gate LDAP *
426*0Sstevel@tonic-gate LDAP_CALL
ldap_open(const char * host,int port)427*0Sstevel@tonic-gate ldap_open( const char *host, int port )
428*0Sstevel@tonic-gate {
429*0Sstevel@tonic-gate LDAP *ld;
430*0Sstevel@tonic-gate
431*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
432*0Sstevel@tonic-gate
433*0Sstevel@tonic-gate if (( ld = ldap_init( host, port )) == NULL ) {
434*0Sstevel@tonic-gate return( NULL );
435*0Sstevel@tonic-gate }
436*0Sstevel@tonic-gate
437*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
438*0Sstevel@tonic-gate if ( nsldapi_open_ldap_defconn( ld ) < 0 ) {
439*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
440*0Sstevel@tonic-gate ldap_ld_free( ld, NULL, NULL, 0 );
441*0Sstevel@tonic-gate return( NULL );
442*0Sstevel@tonic-gate }
443*0Sstevel@tonic-gate
444*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
445*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
446*0Sstevel@tonic-gate ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
447*0Sstevel@tonic-gate
448*0Sstevel@tonic-gate return( ld );
449*0Sstevel@tonic-gate }
450*0Sstevel@tonic-gate
451*0Sstevel@tonic-gate
452*0Sstevel@tonic-gate /*
453*0Sstevel@tonic-gate * ldap_init - initialize the LDAP library. A magic cookie to be used for
454*0Sstevel@tonic-gate * future communication is returned on success, NULL on failure.
455*0Sstevel@tonic-gate * "defhost" may be a space-separated list of hosts or IP addresses
456*0Sstevel@tonic-gate *
457*0Sstevel@tonic-gate * Example:
458*0Sstevel@tonic-gate * LDAP *ld;
459*0Sstevel@tonic-gate * ld = ldap_init( default_hostname, default_port );
460*0Sstevel@tonic-gate */
461*0Sstevel@tonic-gate LDAP *
462*0Sstevel@tonic-gate LDAP_CALL
ldap_init(const char * defhost,int defport)463*0Sstevel@tonic-gate ldap_init( const char *defhost, int defport )
464*0Sstevel@tonic-gate {
465*0Sstevel@tonic-gate LDAP *ld;
466*0Sstevel@tonic-gate
467*0Sstevel@tonic-gate if ( !nsldapi_initialized ) {
468*0Sstevel@tonic-gate nsldapi_initialize_defaults();
469*0Sstevel@tonic-gate }
470*0Sstevel@tonic-gate
471*0Sstevel@tonic-gate if ( defport < 0 || defport > LDAP_PORT_MAX ) {
472*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_ANY,
473*0Sstevel@tonic-gate "ldap_init: port %d is invalid (port numbers must range from 1 to %d)\n",
474*0Sstevel@tonic-gate defport, LDAP_PORT_MAX, 0 );
475*0Sstevel@tonic-gate #if !defined( macintosh ) && !defined( DOS )
476*0Sstevel@tonic-gate errno = EINVAL;
477*0Sstevel@tonic-gate #endif
478*0Sstevel@tonic-gate return( NULL );
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate
481*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
482*0Sstevel@tonic-gate
483*0Sstevel@tonic-gate if ( (ld = (LDAP*)NSLDAPI_MALLOC( sizeof(struct ldap) )) == NULL ) {
484*0Sstevel@tonic-gate return( NULL );
485*0Sstevel@tonic-gate }
486*0Sstevel@tonic-gate
487*0Sstevel@tonic-gate /* copy defaults */
488*0Sstevel@tonic-gate SAFEMEMCPY( ld, &nsldapi_ld_defaults, sizeof( struct ldap ));
489*0Sstevel@tonic-gate if ( nsldapi_ld_defaults.ld_io_fns_ptr != NULL ) {
490*0Sstevel@tonic-gate if (( ld->ld_io_fns_ptr = (struct ldap_io_fns *)NSLDAPI_MALLOC(
491*0Sstevel@tonic-gate sizeof( struct ldap_io_fns ))) == NULL ) {
492*0Sstevel@tonic-gate NSLDAPI_FREE( (char *)ld );
493*0Sstevel@tonic-gate return( NULL );
494*0Sstevel@tonic-gate }
495*0Sstevel@tonic-gate /* struct copy */
496*0Sstevel@tonic-gate *(ld->ld_io_fns_ptr) = *(nsldapi_ld_defaults.ld_io_fns_ptr);
497*0Sstevel@tonic-gate }
498*0Sstevel@tonic-gate
499*0Sstevel@tonic-gate /* call the new handle I/O callback if one is defined */
500*0Sstevel@tonic-gate if ( ld->ld_extnewhandle_fn != NULL ) {
501*0Sstevel@tonic-gate /*
502*0Sstevel@tonic-gate * We always pass the session extended I/O argument to
503*0Sstevel@tonic-gate * the new handle callback.
504*0Sstevel@tonic-gate */
505*0Sstevel@tonic-gate if ( ld->ld_extnewhandle_fn( ld, ld->ld_ext_session_arg )
506*0Sstevel@tonic-gate != LDAP_SUCCESS ) {
507*0Sstevel@tonic-gate NSLDAPI_FREE( (char*)ld );
508*0Sstevel@tonic-gate return( NULL );
509*0Sstevel@tonic-gate }
510*0Sstevel@tonic-gate }
511*0Sstevel@tonic-gate
512*0Sstevel@tonic-gate /* allocate session-specific resources */
513*0Sstevel@tonic-gate if (( ld->ld_sbp = ber_sockbuf_alloc()) == NULL ||
514*0Sstevel@tonic-gate ( defhost != NULL &&
515*0Sstevel@tonic-gate ( ld->ld_defhost = nsldapi_strdup( defhost )) == NULL ) ||
516*0Sstevel@tonic-gate ((ld->ld_mutex = (void **) NSLDAPI_CALLOC( LDAP_MAX_LOCK, sizeof(void *))) == NULL )) {
517*0Sstevel@tonic-gate if ( ld->ld_sbp != NULL ) {
518*0Sstevel@tonic-gate ber_sockbuf_free( ld->ld_sbp );
519*0Sstevel@tonic-gate }
520*0Sstevel@tonic-gate if( ld->ld_mutex != NULL ) {
521*0Sstevel@tonic-gate NSLDAPI_FREE( ld->ld_mutex );
522*0Sstevel@tonic-gate }
523*0Sstevel@tonic-gate NSLDAPI_FREE( (char*)ld );
524*0Sstevel@tonic-gate return( NULL );
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate
527*0Sstevel@tonic-gate /* install Sockbuf I/O functions if set in LDAP * */
528*0Sstevel@tonic-gate if ( ld->ld_extread_fn != NULL || ld->ld_extwrite_fn != NULL ) {
529*0Sstevel@tonic-gate struct lber_x_ext_io_fns lberiofns;
530*0Sstevel@tonic-gate
531*0Sstevel@tonic-gate memset( &lberiofns, 0, sizeof( lberiofns ));
532*0Sstevel@tonic-gate
533*0Sstevel@tonic-gate lberiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
534*0Sstevel@tonic-gate lberiofns.lbextiofn_read = ld->ld_extread_fn;
535*0Sstevel@tonic-gate lberiofns.lbextiofn_write = ld->ld_extwrite_fn;
536*0Sstevel@tonic-gate lberiofns.lbextiofn_writev = ld->ld_extwritev_fn;
537*0Sstevel@tonic-gate lberiofns.lbextiofn_socket_arg = NULL;
538*0Sstevel@tonic-gate ber_sockbuf_set_option( ld->ld_sbp, LBER_SOCKBUF_OPT_EXT_IO_FNS,
539*0Sstevel@tonic-gate (void *)&lberiofns );
540*0Sstevel@tonic-gate }
541*0Sstevel@tonic-gate
542*0Sstevel@tonic-gate #ifdef _SOLARIS_SDK
543*0Sstevel@tonic-gate /* Install the functions for IPv6 support */
544*0Sstevel@tonic-gate /* code sequencing is critical from here to nsldapi_mutex_alloc_all */
545*0Sstevel@tonic-gate if ( prldap_install_thread_functions( ld, 1 ) != 0 ||
546*0Sstevel@tonic-gate prldap_install_io_functions( ld, 1 ) != 0 ||
547*0Sstevel@tonic-gate prldap_install_dns_functions( ld ) != 0 ) {
548*0Sstevel@tonic-gate /* go through ld and free resources */
549*0Sstevel@tonic-gate ldap_unbind( ld );
550*0Sstevel@tonic-gate ld = NULL;
551*0Sstevel@tonic-gate return( NULL );
552*0Sstevel@tonic-gate }
553*0Sstevel@tonic-gate #else
554*0Sstevel@tonic-gate
555*0Sstevel@tonic-gate /* allocate mutexes */
556*0Sstevel@tonic-gate nsldapi_mutex_alloc_all( ld );
557*0Sstevel@tonic-gate #endif
558*0Sstevel@tonic-gate
559*0Sstevel@tonic-gate /* set default port */
560*0Sstevel@tonic-gate ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
561*0Sstevel@tonic-gate
562*0Sstevel@tonic-gate return( ld );
563*0Sstevel@tonic-gate }
564*0Sstevel@tonic-gate
565*0Sstevel@tonic-gate void
nsldapi_mutex_alloc_all(LDAP * ld)566*0Sstevel@tonic-gate nsldapi_mutex_alloc_all( LDAP *ld )
567*0Sstevel@tonic-gate {
568*0Sstevel@tonic-gate int i;
569*0Sstevel@tonic-gate
570*0Sstevel@tonic-gate if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
571*0Sstevel@tonic-gate for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
572*0Sstevel@tonic-gate ld->ld_mutex[i] = LDAP_MUTEX_ALLOC( ld );
573*0Sstevel@tonic-gate ld->ld_mutex_threadid[i] = (void *) -1;
574*0Sstevel@tonic-gate ld->ld_mutex_refcnt[i] = 0;
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate }
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate
579*0Sstevel@tonic-gate
580*0Sstevel@tonic-gate void
nsldapi_mutex_free_all(LDAP * ld)581*0Sstevel@tonic-gate nsldapi_mutex_free_all( LDAP *ld )
582*0Sstevel@tonic-gate {
583*0Sstevel@tonic-gate int i;
584*0Sstevel@tonic-gate
585*0Sstevel@tonic-gate if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
586*0Sstevel@tonic-gate for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
587*0Sstevel@tonic-gate LDAP_MUTEX_FREE( ld, ld->ld_mutex[i] );
588*0Sstevel@tonic-gate }
589*0Sstevel@tonic-gate }
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate
592*0Sstevel@tonic-gate /* returns 0 if connection opened and -1 if an error occurs */
593*0Sstevel@tonic-gate int
nsldapi_open_ldap_defconn(LDAP * ld)594*0Sstevel@tonic-gate nsldapi_open_ldap_defconn( LDAP *ld )
595*0Sstevel@tonic-gate {
596*0Sstevel@tonic-gate LDAPServer *srv;
597*0Sstevel@tonic-gate
598*0Sstevel@tonic-gate if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer ))) ==
599*0Sstevel@tonic-gate NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
600*0Sstevel@tonic-gate nsldapi_strdup( ld->ld_defhost )) == NULL )) {
601*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
602*0Sstevel@tonic-gate return( -1 );
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate srv->lsrv_port = ld->ld_defport;
605*0Sstevel@tonic-gate
606*0Sstevel@tonic-gate #ifdef LDAP_SSLIO_HOOKS
607*0Sstevel@tonic-gate if (( ld->ld_options & LDAP_BITOPT_SSL ) != 0 ) {
608*0Sstevel@tonic-gate srv->lsrv_options |= LDAP_SRV_OPT_SECURE;
609*0Sstevel@tonic-gate }
610*0Sstevel@tonic-gate #endif
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate if (( ld->ld_defconn = nsldapi_new_connection( ld, &srv, 1, 1, 0 ))
613*0Sstevel@tonic-gate == NULL ) {
614*0Sstevel@tonic-gate if ( ld->ld_defhost != NULL ) {
615*0Sstevel@tonic-gate NSLDAPI_FREE( srv->lsrv_host );
616*0Sstevel@tonic-gate }
617*0Sstevel@tonic-gate NSLDAPI_FREE( (char *)srv );
618*0Sstevel@tonic-gate return( -1 );
619*0Sstevel@tonic-gate }
620*0Sstevel@tonic-gate ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
621*0Sstevel@tonic-gate
622*0Sstevel@tonic-gate return( 0 );
623*0Sstevel@tonic-gate }
624*0Sstevel@tonic-gate
625*0Sstevel@tonic-gate
626*0Sstevel@tonic-gate struct ldap_x_hostlist_status {
627*0Sstevel@tonic-gate char *lhs_hostlist;
628*0Sstevel@tonic-gate char *lhs_nexthost;
629*0Sstevel@tonic-gate int lhs_defport;
630*0Sstevel@tonic-gate };
631*0Sstevel@tonic-gate
632*0Sstevel@tonic-gate /*
633*0Sstevel@tonic-gate * Return the first host and port in hostlist (setting *hostp and *portp).
634*0Sstevel@tonic-gate * Return value is an LDAP API error code (LDAP_SUCCESS if all goes well).
635*0Sstevel@tonic-gate * Note that a NULL or zero-length hostlist causes the host "127.0.0.1" to
636*0Sstevel@tonic-gate * be returned.
637*0Sstevel@tonic-gate */
638*0Sstevel@tonic-gate int LDAP_CALL
ldap_x_hostlist_first(const char * hostlist,int defport,char ** hostp,int * portp,struct ldap_x_hostlist_status ** statusp)639*0Sstevel@tonic-gate ldap_x_hostlist_first( const char *hostlist, int defport, char **hostp,
640*0Sstevel@tonic-gate int *portp, struct ldap_x_hostlist_status **statusp )
641*0Sstevel@tonic-gate {
642*0Sstevel@tonic-gate
643*0Sstevel@tonic-gate if ( NULL == hostp || NULL == portp || NULL == statusp ) {
644*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
645*0Sstevel@tonic-gate }
646*0Sstevel@tonic-gate
647*0Sstevel@tonic-gate if ( NULL == hostlist || *hostlist == '\0' ) {
648*0Sstevel@tonic-gate *hostp = nsldapi_strdup( "127.0.0.1" );
649*0Sstevel@tonic-gate if ( NULL == *hostp ) {
650*0Sstevel@tonic-gate return( LDAP_NO_MEMORY );
651*0Sstevel@tonic-gate }
652*0Sstevel@tonic-gate *portp = defport;
653*0Sstevel@tonic-gate *statusp = NULL;
654*0Sstevel@tonic-gate return( LDAP_SUCCESS );
655*0Sstevel@tonic-gate }
656*0Sstevel@tonic-gate
657*0Sstevel@tonic-gate *statusp = NSLDAPI_CALLOC( 1, sizeof( struct ldap_x_hostlist_status ));
658*0Sstevel@tonic-gate if ( NULL == *statusp ) {
659*0Sstevel@tonic-gate return( LDAP_NO_MEMORY );
660*0Sstevel@tonic-gate }
661*0Sstevel@tonic-gate (*statusp)->lhs_hostlist = nsldapi_strdup( hostlist );
662*0Sstevel@tonic-gate if ( NULL == (*statusp)->lhs_hostlist ) {
663*0Sstevel@tonic-gate return( LDAP_NO_MEMORY );
664*0Sstevel@tonic-gate }
665*0Sstevel@tonic-gate (*statusp)->lhs_nexthost = (*statusp)->lhs_hostlist;
666*0Sstevel@tonic-gate (*statusp)->lhs_defport = defport;
667*0Sstevel@tonic-gate return( ldap_x_hostlist_next( hostp, portp, *statusp ));
668*0Sstevel@tonic-gate }
669*0Sstevel@tonic-gate
670*0Sstevel@tonic-gate /*
671*0Sstevel@tonic-gate * Return the next host and port in hostlist (setting *hostp and *portp).
672*0Sstevel@tonic-gate * Return value is an LDAP API error code (LDAP_SUCCESS if all goes well).
673*0Sstevel@tonic-gate * If no more hosts are available, LDAP_SUCCESS is returned but *hostp is set
674*0Sstevel@tonic-gate * to NULL.
675*0Sstevel@tonic-gate */
676*0Sstevel@tonic-gate int LDAP_CALL
ldap_x_hostlist_next(char ** hostp,int * portp,struct ldap_x_hostlist_status * status)677*0Sstevel@tonic-gate ldap_x_hostlist_next( char **hostp, int *portp,
678*0Sstevel@tonic-gate struct ldap_x_hostlist_status *status )
679*0Sstevel@tonic-gate {
680*0Sstevel@tonic-gate char *q;
681*0Sstevel@tonic-gate int squarebrackets = 0;
682*0Sstevel@tonic-gate
683*0Sstevel@tonic-gate if ( NULL == hostp || NULL == portp ) {
684*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
685*0Sstevel@tonic-gate }
686*0Sstevel@tonic-gate
687*0Sstevel@tonic-gate if ( NULL == status || NULL == status->lhs_nexthost ) {
688*0Sstevel@tonic-gate *hostp = NULL;
689*0Sstevel@tonic-gate return( LDAP_SUCCESS );
690*0Sstevel@tonic-gate }
691*0Sstevel@tonic-gate
692*0Sstevel@tonic-gate /*
693*0Sstevel@tonic-gate * skip past leading '[' if present (IPv6 addresses may be surrounded
694*0Sstevel@tonic-gate * with square brackets, e.g., [fe80::a00:20ff:fee5:c0b4]:389
695*0Sstevel@tonic-gate */
696*0Sstevel@tonic-gate if ( status->lhs_nexthost[0] == '[' ) {
697*0Sstevel@tonic-gate ++status->lhs_nexthost;
698*0Sstevel@tonic-gate squarebrackets = 1;
699*0Sstevel@tonic-gate }
700*0Sstevel@tonic-gate
701*0Sstevel@tonic-gate /* copy host into *hostp */
702*0Sstevel@tonic-gate if ( NULL != ( q = strchr( status->lhs_nexthost, ' ' ))) {
703*0Sstevel@tonic-gate size_t len = q - status->lhs_nexthost;
704*0Sstevel@tonic-gate *hostp = NSLDAPI_MALLOC( len + 1 );
705*0Sstevel@tonic-gate if ( NULL == *hostp ) {
706*0Sstevel@tonic-gate return( LDAP_NO_MEMORY );
707*0Sstevel@tonic-gate }
708*0Sstevel@tonic-gate strncpy( *hostp, status->lhs_nexthost, len );
709*0Sstevel@tonic-gate (*hostp)[len] = '\0';
710*0Sstevel@tonic-gate status->lhs_nexthost += ( len + 1 );
711*0Sstevel@tonic-gate } else { /* last host */
712*0Sstevel@tonic-gate *hostp = nsldapi_strdup( status->lhs_nexthost );
713*0Sstevel@tonic-gate if ( NULL == *hostp ) {
714*0Sstevel@tonic-gate return( LDAP_NO_MEMORY );
715*0Sstevel@tonic-gate }
716*0Sstevel@tonic-gate status->lhs_nexthost = NULL;
717*0Sstevel@tonic-gate }
718*0Sstevel@tonic-gate
719*0Sstevel@tonic-gate /*
720*0Sstevel@tonic-gate * Look for closing ']' and skip past it before looking for port.
721*0Sstevel@tonic-gate */
722*0Sstevel@tonic-gate if ( squarebrackets && NULL != ( q = strchr( *hostp, ']' ))) {
723*0Sstevel@tonic-gate *q++ = '\0';
724*0Sstevel@tonic-gate } else {
725*0Sstevel@tonic-gate q = *hostp;
726*0Sstevel@tonic-gate }
727*0Sstevel@tonic-gate
728*0Sstevel@tonic-gate /* determine and set port */
729*0Sstevel@tonic-gate if ( NULL != ( q = strchr( q, ':' ))) {
730*0Sstevel@tonic-gate *q++ = '\0';
731*0Sstevel@tonic-gate *portp = atoi( q );
732*0Sstevel@tonic-gate } else {
733*0Sstevel@tonic-gate *portp = status->lhs_defport;
734*0Sstevel@tonic-gate }
735*0Sstevel@tonic-gate
736*0Sstevel@tonic-gate return( LDAP_SUCCESS );
737*0Sstevel@tonic-gate }
738*0Sstevel@tonic-gate
739*0Sstevel@tonic-gate
740*0Sstevel@tonic-gate void LDAP_CALL
ldap_x_hostlist_statusfree(struct ldap_x_hostlist_status * status)741*0Sstevel@tonic-gate ldap_x_hostlist_statusfree( struct ldap_x_hostlist_status *status )
742*0Sstevel@tonic-gate {
743*0Sstevel@tonic-gate if ( NULL != status ) {
744*0Sstevel@tonic-gate if ( NULL != status->lhs_hostlist ) {
745*0Sstevel@tonic-gate NSLDAPI_FREE( status->lhs_hostlist );
746*0Sstevel@tonic-gate }
747*0Sstevel@tonic-gate NSLDAPI_FREE( status );
748*0Sstevel@tonic-gate }
749*0Sstevel@tonic-gate }
750*0Sstevel@tonic-gate
751*0Sstevel@tonic-gate
752*0Sstevel@tonic-gate
753*0Sstevel@tonic-gate /*
754*0Sstevel@tonic-gate * memory allocation functions. we include these in open.c since every
755*0Sstevel@tonic-gate * LDAP application is likely to pull the rest of the code in this file
756*0Sstevel@tonic-gate * in anyways.
757*0Sstevel@tonic-gate */
758*0Sstevel@tonic-gate void *
ldap_x_malloc(size_t size)759*0Sstevel@tonic-gate ldap_x_malloc( size_t size )
760*0Sstevel@tonic-gate {
761*0Sstevel@tonic-gate return( nsldapi_memalloc_fns.ldapmem_malloc == NULL ?
762*0Sstevel@tonic-gate malloc( size ) :
763*0Sstevel@tonic-gate nsldapi_memalloc_fns.ldapmem_malloc( size ));
764*0Sstevel@tonic-gate }
765*0Sstevel@tonic-gate
766*0Sstevel@tonic-gate
767*0Sstevel@tonic-gate void *
ldap_x_calloc(size_t nelem,size_t elsize)768*0Sstevel@tonic-gate ldap_x_calloc( size_t nelem, size_t elsize )
769*0Sstevel@tonic-gate {
770*0Sstevel@tonic-gate return( nsldapi_memalloc_fns.ldapmem_calloc == NULL ?
771*0Sstevel@tonic-gate calloc( nelem, elsize ) :
772*0Sstevel@tonic-gate nsldapi_memalloc_fns.ldapmem_calloc( nelem, elsize ));
773*0Sstevel@tonic-gate }
774*0Sstevel@tonic-gate
775*0Sstevel@tonic-gate
776*0Sstevel@tonic-gate void *
ldap_x_realloc(void * ptr,size_t size)777*0Sstevel@tonic-gate ldap_x_realloc( void *ptr, size_t size )
778*0Sstevel@tonic-gate {
779*0Sstevel@tonic-gate return( nsldapi_memalloc_fns.ldapmem_realloc == NULL ?
780*0Sstevel@tonic-gate realloc( ptr, size ) :
781*0Sstevel@tonic-gate nsldapi_memalloc_fns.ldapmem_realloc( ptr, size ));
782*0Sstevel@tonic-gate }
783*0Sstevel@tonic-gate
784*0Sstevel@tonic-gate
785*0Sstevel@tonic-gate void
ldap_x_free(void * ptr)786*0Sstevel@tonic-gate ldap_x_free( void *ptr )
787*0Sstevel@tonic-gate {
788*0Sstevel@tonic-gate if ( nsldapi_memalloc_fns.ldapmem_free == NULL ) {
789*0Sstevel@tonic-gate free( ptr );
790*0Sstevel@tonic-gate } else {
791*0Sstevel@tonic-gate nsldapi_memalloc_fns.ldapmem_free( ptr );
792*0Sstevel@tonic-gate }
793*0Sstevel@tonic-gate }
794*0Sstevel@tonic-gate
795*0Sstevel@tonic-gate
796*0Sstevel@tonic-gate /* if s is NULL, returns NULL */
797*0Sstevel@tonic-gate char *
nsldapi_strdup(const char * s)798*0Sstevel@tonic-gate nsldapi_strdup( const char *s )
799*0Sstevel@tonic-gate {
800*0Sstevel@tonic-gate char *p;
801*0Sstevel@tonic-gate
802*0Sstevel@tonic-gate if ( s == NULL ||
803*0Sstevel@tonic-gate (p = (char *)NSLDAPI_MALLOC( strlen( s ) + 1 )) == NULL )
804*0Sstevel@tonic-gate return( NULL );
805*0Sstevel@tonic-gate
806*0Sstevel@tonic-gate strcpy( p, s );
807*0Sstevel@tonic-gate
808*0Sstevel@tonic-gate return( p );
809*0Sstevel@tonic-gate }
810