1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
8*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
9*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
10*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
13*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
15*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
18*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
21*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
22*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
23*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  * Contributor(s):
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * setoption.c - ldap_set_option implementation
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
32*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
33*7c478bd9Sstevel@tonic-gate #include "solaris-priv.h"
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate extern int nsldapi_sasl_secprops(const char *in,
37*7c478bd9Sstevel@tonic-gate 	sasl_security_properties_t *secprops);
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #define	LDAP_SETCLR_BITOPT(ld, bit, optdata) \
40*7c478bd9Sstevel@tonic-gate 	if (optdata != NULL) {		\
41*7c478bd9Sstevel@tonic-gate 		(ld)->ld_options |= bit;	\
42*7c478bd9Sstevel@tonic-gate 	} else {				\
43*7c478bd9Sstevel@tonic-gate 		(ld)->ld_options &= ~bit;	\
44*7c478bd9Sstevel@tonic-gate 	}
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate int
48*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_set_option(LDAP * ld,int option,const void * optdata)49*7c478bd9Sstevel@tonic-gate ldap_set_option(LDAP *ld, int option, const void *optdata)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	int		rc, i;
52*7c478bd9Sstevel@tonic-gate 	char		*matched, *errstr;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if (!nsldapi_initialized) {
55*7c478bd9Sstevel@tonic-gate 		nsldapi_initialize_defaults();
56*7c478bd9Sstevel@tonic-gate 	}
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	/*
59*7c478bd9Sstevel@tonic-gate 	 * process global options (not associated with an LDAP session handle)
60*7c478bd9Sstevel@tonic-gate 	 */
61*7c478bd9Sstevel@tonic-gate 	if (option == LDAP_OPT_MEMALLOC_FN_PTRS) {
62*7c478bd9Sstevel@tonic-gate 		struct lber_memalloc_fns	memalloc_fns;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 		/* set libldap ones via a struct copy */
65*7c478bd9Sstevel@tonic-gate 		nsldapi_memalloc_fns = *((struct ldap_memalloc_fns *)optdata);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 		/* also set liblber memory allocation callbacks */
68*7c478bd9Sstevel@tonic-gate 		memalloc_fns.lbermem_malloc =
69*7c478bd9Sstevel@tonic-gate 		    nsldapi_memalloc_fns.ldapmem_malloc;
70*7c478bd9Sstevel@tonic-gate 		memalloc_fns.lbermem_calloc =
71*7c478bd9Sstevel@tonic-gate 		    nsldapi_memalloc_fns.ldapmem_calloc;
72*7c478bd9Sstevel@tonic-gate 		memalloc_fns.lbermem_realloc =
73*7c478bd9Sstevel@tonic-gate 		    nsldapi_memalloc_fns.ldapmem_realloc;
74*7c478bd9Sstevel@tonic-gate 		memalloc_fns.lbermem_free =
75*7c478bd9Sstevel@tonic-gate 		    nsldapi_memalloc_fns.ldapmem_free;
76*7c478bd9Sstevel@tonic-gate 		if (ber_set_option(NULL, LBER_OPT_MEMALLOC_FN_PTRS,
77*7c478bd9Sstevel@tonic-gate 		    &memalloc_fns) != 0) {
78*7c478bd9Sstevel@tonic-gate 			return (-1);
79*7c478bd9Sstevel@tonic-gate 		}
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 		return (0);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	/*
84*7c478bd9Sstevel@tonic-gate 	 * LDAP_OPT_DEBUG_LEVEL is global
85*7c478bd9Sstevel@tonic-gate 	 */
86*7c478bd9Sstevel@tonic-gate 	if (LDAP_OPT_DEBUG_LEVEL == option) {
87*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
88*7c478bd9Sstevel@tonic-gate 		ldap_debug = *((int *)optdata);
89*7c478bd9Sstevel@tonic-gate #endif
90*7c478bd9Sstevel@tonic-gate 		return (0);
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	/*
94*7c478bd9Sstevel@tonic-gate 	 * if ld is NULL, arrange to modify our default settings
95*7c478bd9Sstevel@tonic-gate 	 */
96*7c478bd9Sstevel@tonic-gate 	if (ld == NULL) {
97*7c478bd9Sstevel@tonic-gate 		ld = &nsldapi_ld_defaults;
98*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
99*7c478bd9Sstevel@tonic-gate 		ldap_debug = 0;
100*7c478bd9Sstevel@tonic-gate #endif
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	/*
105*7c478bd9Sstevel@tonic-gate 	 * process options that are associated with an LDAP session handle
106*7c478bd9Sstevel@tonic-gate 	 */
107*7c478bd9Sstevel@tonic-gate 	if (!NSLDAPI_VALID_LDAP_POINTER(ld)) {
108*7c478bd9Sstevel@tonic-gate 		return (-1);	/* punt */
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	rc = 0;
112*7c478bd9Sstevel@tonic-gate 	if (ld != &nsldapi_ld_defaults &&
113*7c478bd9Sstevel@tonic-gate 		option != LDAP_OPT_EXTRA_THREAD_FN_PTRS &&
114*7c478bd9Sstevel@tonic-gate 		option != LDAP_OPT_THREAD_FN_PTRS) {
115*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK(ld, LDAP_OPTION_LOCK);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 	switch (option) {
118*7c478bd9Sstevel@tonic-gate 	/* options that can be turned on and off */
119*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DNS
120*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_DNS:
121*7c478bd9Sstevel@tonic-gate 		LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_DNS, optdata);
122*7c478bd9Sstevel@tonic-gate 		break;
123*7c478bd9Sstevel@tonic-gate #endif
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_REFERRALS:
126*7c478bd9Sstevel@tonic-gate 		LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_REFERRALS, optdata);
127*7c478bd9Sstevel@tonic-gate 		break;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate #ifdef LDAP_SSLIO_HOOKS
130*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_SSL:
131*7c478bd9Sstevel@tonic-gate 		LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_SSL, optdata);
132*7c478bd9Sstevel@tonic-gate 		break;
133*7c478bd9Sstevel@tonic-gate #endif
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_RESTART:
136*7c478bd9Sstevel@tonic-gate 		LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_RESTART, optdata);
137*7c478bd9Sstevel@tonic-gate 		break;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_RECONNECT:
140*7c478bd9Sstevel@tonic-gate 		LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_RECONNECT, optdata);
141*7c478bd9Sstevel@tonic-gate 		break;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate #ifdef LDAP_ASYNC_IO
144*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_ASYNC_CONNECT:
145*7c478bd9Sstevel@tonic-gate 		LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_ASYNC, optdata);
146*7c478bd9Sstevel@tonic-gate 		break;
147*7c478bd9Sstevel@tonic-gate #endif /* LDAP_ASYNC_IO */
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	/* fields in the LDAP structure */
150*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_DEREF:
151*7c478bd9Sstevel@tonic-gate 		ld->ld_deref = *((int *)optdata);
152*7c478bd9Sstevel@tonic-gate 		break;
153*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_SIZELIMIT:
154*7c478bd9Sstevel@tonic-gate 		ld->ld_sizelimit = *((int *)optdata);
155*7c478bd9Sstevel@tonic-gate 		break;
156*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_TIMELIMIT:
157*7c478bd9Sstevel@tonic-gate 		ld->ld_timelimit = *((int *)optdata);
158*7c478bd9Sstevel@tonic-gate 		break;
159*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_REFERRAL_HOP_LIMIT:
160*7c478bd9Sstevel@tonic-gate 		ld->ld_refhoplimit = *((int *)optdata);
161*7c478bd9Sstevel@tonic-gate 		break;
162*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_PROTOCOL_VERSION:
163*7c478bd9Sstevel@tonic-gate 		ld->ld_version = *((int *)optdata);
164*7c478bd9Sstevel@tonic-gate 		if (ld->ld_defconn != NULL) {	/* also set in default conn. */
165*7c478bd9Sstevel@tonic-gate 			ld->ld_defconn->lconn_version = ld->ld_version;
166*7c478bd9Sstevel@tonic-gate 		}
167*7c478bd9Sstevel@tonic-gate 		break;
168*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_SERVER_CONTROLS:
169*7c478bd9Sstevel@tonic-gate 		/* nsldapi_dup_controls returns -1 and sets lderrno on error */
170*7c478bd9Sstevel@tonic-gate 		rc = nsldapi_dup_controls(ld, &ld->ld_servercontrols,
171*7c478bd9Sstevel@tonic-gate 		    (LDAPControl **)optdata);
172*7c478bd9Sstevel@tonic-gate 		break;
173*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_CLIENT_CONTROLS:
174*7c478bd9Sstevel@tonic-gate 		/* nsldapi_dup_controls returns -1 and sets lderrno on error */
175*7c478bd9Sstevel@tonic-gate 		rc = nsldapi_dup_controls(ld, &ld->ld_clientcontrols,
176*7c478bd9Sstevel@tonic-gate 		    (LDAPControl **)optdata);
177*7c478bd9Sstevel@tonic-gate 		break;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	/* rebind proc */
180*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_REBIND_FN:
181*7c478bd9Sstevel@tonic-gate 		ld->ld_rebind_fn = (LDAP_REBINDPROC_CALLBACK *) optdata;
182*7c478bd9Sstevel@tonic-gate 		break;
183*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_REBIND_ARG:
184*7c478bd9Sstevel@tonic-gate 		ld->ld_rebind_arg = (void *) optdata;
185*7c478bd9Sstevel@tonic-gate 		break;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate #ifdef LDAP_SSLIO_HOOKS
188*7c478bd9Sstevel@tonic-gate 	/* i/o function pointers */
189*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_IO_FN_PTRS:
190*7c478bd9Sstevel@tonic-gate 		if ((rc = nsldapi_install_compat_io_fns(ld,
191*7c478bd9Sstevel@tonic-gate 		    (struct ldap_io_fns *)optdata)) != LDAP_SUCCESS) {
192*7c478bd9Sstevel@tonic-gate 			LDAP_SET_LDERRNO(ld, rc, NULL, NULL);
193*7c478bd9Sstevel@tonic-gate 			rc = -1;
194*7c478bd9Sstevel@tonic-gate 		}
195*7c478bd9Sstevel@tonic-gate 		break;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	/* extended i/o function pointers */
198*7c478bd9Sstevel@tonic-gate 	case LDAP_X_OPT_EXTIO_FN_PTRS:
199*7c478bd9Sstevel@tonic-gate 	    /* denotes use of old iofns struct (no writev) */
200*7c478bd9Sstevel@tonic-gate 	    if (((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_size ==
201*7c478bd9Sstevel@tonic-gate 			LDAP_X_EXTIO_FNS_SIZE_REV0) {
202*7c478bd9Sstevel@tonic-gate 		ld->ld_extio_size = LDAP_X_EXTIO_FNS_SIZE;
203*7c478bd9Sstevel@tonic-gate 	    ld->ld_extclose_fn =
204*7c478bd9Sstevel@tonic-gate 		((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_close;
205*7c478bd9Sstevel@tonic-gate 	    ld->ld_extconnect_fn =
206*7c478bd9Sstevel@tonic-gate 		((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_connect;
207*7c478bd9Sstevel@tonic-gate 	    ld->ld_extread_fn =
208*7c478bd9Sstevel@tonic-gate 		((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_read;
209*7c478bd9Sstevel@tonic-gate 	    ld->ld_extwrite_fn =
210*7c478bd9Sstevel@tonic-gate 		((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_write;
211*7c478bd9Sstevel@tonic-gate 	    ld->ld_extpoll_fn =
212*7c478bd9Sstevel@tonic-gate 		((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_poll;
213*7c478bd9Sstevel@tonic-gate 	    ld->ld_extnewhandle_fn =
214*7c478bd9Sstevel@tonic-gate 		((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_newhandle;
215*7c478bd9Sstevel@tonic-gate 	    ld->ld_extdisposehandle_fn =
216*7c478bd9Sstevel@tonic-gate 		((struct ldap_x_ext_io_fns_rev0 *)optdata)->
217*7c478bd9Sstevel@tonic-gate 		lextiof_disposehandle;
218*7c478bd9Sstevel@tonic-gate 	    ld->ld_ext_session_arg =
219*7c478bd9Sstevel@tonic-gate 		((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_session_arg;
220*7c478bd9Sstevel@tonic-gate 	    ld->ld_extwritev_fn = NULL;
221*7c478bd9Sstevel@tonic-gate 	    if (ber_sockbuf_set_option(ld->ld_sbp, LBER_SOCKBUF_OPT_EXT_IO_FNS,
222*7c478bd9Sstevel@tonic-gate 				&(ld->ld_ext_io_fns)) != 0) {
223*7c478bd9Sstevel@tonic-gate 			return (LDAP_LOCAL_ERROR);
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 	    } else {
226*7c478bd9Sstevel@tonic-gate 	    /* struct copy */
227*7c478bd9Sstevel@tonic-gate 		ld->ld_ext_io_fns = *((struct ldap_x_ext_io_fns *)optdata);
228*7c478bd9Sstevel@tonic-gate 	    }
229*7c478bd9Sstevel@tonic-gate 	    if ((rc = nsldapi_install_lber_extiofns(ld, ld->ld_sbp))
230*7c478bd9Sstevel@tonic-gate 						!= LDAP_SUCCESS) {
231*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO(ld, rc, NULL, NULL);
232*7c478bd9Sstevel@tonic-gate 		rc = -1;
233*7c478bd9Sstevel@tonic-gate 	    }
234*7c478bd9Sstevel@tonic-gate 		break;
235*7c478bd9Sstevel@tonic-gate #endif
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	/* thread function pointers */
238*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_THREAD_FN_PTRS:
239*7c478bd9Sstevel@tonic-gate 		/*
240*7c478bd9Sstevel@tonic-gate 		 * It is only safe to set the thread function pointers
241*7c478bd9Sstevel@tonic-gate 		 * when one thread is using the LDAP session handle.
242*7c478bd9Sstevel@tonic-gate 		 */
243*7c478bd9Sstevel@tonic-gate 		/* free existing mutexes (some are allocated by ldap_init()) */
244*7c478bd9Sstevel@tonic-gate 		nsldapi_mutex_free_all(ld);
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 		/* struct copy */
247*7c478bd9Sstevel@tonic-gate 		ld->ld_thread = *((struct ldap_thread_fns *)optdata);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		/* allocate new mutexes */
250*7c478bd9Sstevel@tonic-gate 		nsldapi_mutex_alloc_all(ld);
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 		/* LDAP_OPTION_LOCK was never locked... so just return */
253*7c478bd9Sstevel@tonic-gate 		return (rc);
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	/* extra thread function pointers */
256*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_EXTRA_THREAD_FN_PTRS:
257*7c478bd9Sstevel@tonic-gate 	/* The extra thread funcs will only pick up the threadid */
258*7c478bd9Sstevel@tonic-gate 	    ld->ld_thread2  = *((struct ldap_extra_thread_fns *)optdata);
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	/* Reset the rest of the structure preserving the threadid fn */
261*7c478bd9Sstevel@tonic-gate 	    ld->ld_mutex_trylock_fn =  (LDAP_TF_MUTEX_TRYLOCK_CALLBACK *)NULL;
262*7c478bd9Sstevel@tonic-gate 	    ld->ld_sema_alloc_fn = (LDAP_TF_SEMA_ALLOC_CALLBACK *) NULL;
263*7c478bd9Sstevel@tonic-gate 	    ld->ld_sema_free_fn = (LDAP_TF_SEMA_FREE_CALLBACK *) NULL;
264*7c478bd9Sstevel@tonic-gate 	    ld->ld_sema_wait_fn = (LDAP_TF_SEMA_WAIT_CALLBACK *) NULL;
265*7c478bd9Sstevel@tonic-gate 	    ld->ld_sema_post_fn = (LDAP_TF_SEMA_POST_CALLBACK *) NULL;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	/* We assume that only one thread is active when replacing */
268*7c478bd9Sstevel@tonic-gate 	/* the threadid function.  We will now proceed and reset all */
269*7c478bd9Sstevel@tonic-gate 	/* of the threadid/refcounts */
270*7c478bd9Sstevel@tonic-gate 	    for (i = 0; i < LDAP_MAX_LOCK; i++) {
271*7c478bd9Sstevel@tonic-gate 		ld->ld_mutex_threadid[i] = (void *) -1;
272*7c478bd9Sstevel@tonic-gate 		ld->ld_mutex_refcnt[i] = 0;
273*7c478bd9Sstevel@tonic-gate 	    }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	    return (rc);
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	/* DNS function pointers */
278*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_DNS_FN_PTRS:
279*7c478bd9Sstevel@tonic-gate 		/* struct copy */
280*7c478bd9Sstevel@tonic-gate 		ld->ld_dnsfn = *((struct ldap_dns_fns *)optdata);
281*7c478bd9Sstevel@tonic-gate 		break;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	/* cache function pointers */
284*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_CACHE_FN_PTRS:
285*7c478bd9Sstevel@tonic-gate 		/* struct copy */
286*7c478bd9Sstevel@tonic-gate 		ld->ld_cache = *((struct ldap_cache_fns *)optdata);
287*7c478bd9Sstevel@tonic-gate 		break;
288*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_CACHE_STRATEGY:
289*7c478bd9Sstevel@tonic-gate 		ld->ld_cache_strategy = *((int *)optdata);
290*7c478bd9Sstevel@tonic-gate 		break;
291*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_CACHE_ENABLE:
292*7c478bd9Sstevel@tonic-gate 		ld->ld_cache_on = *((int *)optdata);
293*7c478bd9Sstevel@tonic-gate 		break;
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_ERROR_NUMBER:
296*7c478bd9Sstevel@tonic-gate 		LDAP_GET_LDERRNO(ld, &matched, &errstr);
297*7c478bd9Sstevel@tonic-gate 		matched = nsldapi_strdup(matched);
298*7c478bd9Sstevel@tonic-gate 		errstr = nsldapi_strdup(errstr);
299*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO(ld, *((int *)optdata), matched, errstr);
300*7c478bd9Sstevel@tonic-gate 		break;
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_ERROR_STRING:
303*7c478bd9Sstevel@tonic-gate 		rc = LDAP_GET_LDERRNO(ld, &matched, NULL);
304*7c478bd9Sstevel@tonic-gate 		matched = nsldapi_strdup(matched);
305*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO(ld, rc, matched,
306*7c478bd9Sstevel@tonic-gate 		    nsldapi_strdup((char *)optdata));
307*7c478bd9Sstevel@tonic-gate 		rc = LDAP_SUCCESS;
308*7c478bd9Sstevel@tonic-gate 		break;
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_MATCHED_DN:
311*7c478bd9Sstevel@tonic-gate 		rc = LDAP_GET_LDERRNO(ld, NULL, &errstr);
312*7c478bd9Sstevel@tonic-gate 		errstr = nsldapi_strdup(errstr);
313*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO(ld, rc,
314*7c478bd9Sstevel@tonic-gate 		    nsldapi_strdup((char *)optdata), errstr);
315*7c478bd9Sstevel@tonic-gate 		rc = LDAP_SUCCESS;
316*7c478bd9Sstevel@tonic-gate 		break;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_PREFERRED_LANGUAGE:
319*7c478bd9Sstevel@tonic-gate 		if (NULL != ld->ld_preferred_language) {
320*7c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE(ld->ld_preferred_language);
321*7c478bd9Sstevel@tonic-gate 		}
322*7c478bd9Sstevel@tonic-gate 		ld->ld_preferred_language = nsldapi_strdup((char *)optdata);
323*7c478bd9Sstevel@tonic-gate 		break;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_HOST_NAME:
326*7c478bd9Sstevel@tonic-gate 		if (NULL != ld->ld_defhost) {
327*7c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE(ld->ld_defhost);
328*7c478bd9Sstevel@tonic-gate 		}
329*7c478bd9Sstevel@tonic-gate 		ld->ld_defhost = nsldapi_strdup((char *)optdata);
330*7c478bd9Sstevel@tonic-gate 		break;
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	case LDAP_X_OPT_CONNECT_TIMEOUT:
333*7c478bd9Sstevel@tonic-gate 		ld->ld_connect_timeout = *((int *)optdata);
334*7c478bd9Sstevel@tonic-gate 		break;
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
337*7c478bd9Sstevel@tonic-gate 	/* recursion prevention dns functions */
338*7c478bd9Sstevel@tonic-gate 	case LDAP_X_OPT_DNS_SKIPDB:
339*7c478bd9Sstevel@tonic-gate 		rc = prldap_x_install_dns_skipdb(ld, (const char *)optdata);
340*7c478bd9Sstevel@tonic-gate 		break;
341*7c478bd9Sstevel@tonic-gate #endif
342*7c478bd9Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
343*7c478bd9Sstevel@tonic-gate 	/* SASL options */
344*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_MECH:
345*7c478bd9Sstevel@tonic-gate 		if (NULL != ld->ld_def_sasl_mech) {
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE(ld->ld_def_sasl_mech);
348*7c478bd9Sstevel@tonic-gate 		}
349*7c478bd9Sstevel@tonic-gate 		ld->ld_def_sasl_mech = nsldapi_strdup((char *)optdata);
350*7c478bd9Sstevel@tonic-gate 		break;
351*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_REALM:
352*7c478bd9Sstevel@tonic-gate 		if (NULL != ld->ld_def_sasl_realm) {
353*7c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE(ld->ld_def_sasl_realm);
354*7c478bd9Sstevel@tonic-gate 		}
355*7c478bd9Sstevel@tonic-gate 		ld->ld_def_sasl_realm = nsldapi_strdup((char *)optdata);
356*7c478bd9Sstevel@tonic-gate 		break;
357*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_AUTHCID:
358*7c478bd9Sstevel@tonic-gate 		if (NULL != ld->ld_def_sasl_authcid) {
359*7c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE(ld->ld_def_sasl_authcid);
360*7c478bd9Sstevel@tonic-gate 		}
361*7c478bd9Sstevel@tonic-gate 		ld->ld_def_sasl_authcid = nsldapi_strdup((char *)optdata);
362*7c478bd9Sstevel@tonic-gate 		break;
363*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_AUTHZID:
364*7c478bd9Sstevel@tonic-gate 		if (NULL != ld->ld_def_sasl_authzid) {
365*7c478bd9Sstevel@tonic-gate 			NSLDAPI_FREE(ld->ld_def_sasl_authzid);
366*7c478bd9Sstevel@tonic-gate 		}
367*7c478bd9Sstevel@tonic-gate 		ld->ld_def_sasl_authzid = nsldapi_strdup((char *)optdata);
368*7c478bd9Sstevel@tonic-gate 		break;
369*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_SSF_EXTERNAL:
370*7c478bd9Sstevel@tonic-gate 		{
371*7c478bd9Sstevel@tonic-gate 		    int sc;
372*7c478bd9Sstevel@tonic-gate 		    sasl_ssf_t extprops;
373*7c478bd9Sstevel@tonic-gate 		    sasl_conn_t *ctx;
374*7c478bd9Sstevel@tonic-gate 		    if (ld->ld_defconn == NULL ||
375*7c478bd9Sstevel@tonic-gate 			ld->ld_defconn->lconn_sb == NULL) {
376*7c478bd9Sstevel@tonic-gate 			return (-1);
377*7c478bd9Sstevel@tonic-gate 		    }
378*7c478bd9Sstevel@tonic-gate 		    ctx = (sasl_conn_t *)
379*7c478bd9Sstevel@tonic-gate 				(ld->ld_defconn->lconn_sb->sb_sasl_ctx);
380*7c478bd9Sstevel@tonic-gate 		    if (ctx == NULL) {
381*7c478bd9Sstevel@tonic-gate 			return (-1);
382*7c478bd9Sstevel@tonic-gate 		    }
383*7c478bd9Sstevel@tonic-gate 		    memset(&extprops, 0L, sizeof (extprops));
384*7c478bd9Sstevel@tonic-gate 		    extprops = * ((sasl_ssf_t *)optdata);
385*7c478bd9Sstevel@tonic-gate 		    sc = sasl_setprop(ctx, SASL_SSF_EXTERNAL,
386*7c478bd9Sstevel@tonic-gate 				(void *) &extprops);
387*7c478bd9Sstevel@tonic-gate 		    if (sc != SASL_OK) {
388*7c478bd9Sstevel@tonic-gate 			return (-1);
389*7c478bd9Sstevel@tonic-gate 		    }
390*7c478bd9Sstevel@tonic-gate 		}
391*7c478bd9Sstevel@tonic-gate 		break;
392*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_SECPROPS:
393*7c478bd9Sstevel@tonic-gate 		{
394*7c478bd9Sstevel@tonic-gate 			int sc;
395*7c478bd9Sstevel@tonic-gate 			sc = nsldapi_sasl_secprops((char *)optdata,
396*7c478bd9Sstevel@tonic-gate 				&ld->ld_sasl_secprops);
397*7c478bd9Sstevel@tonic-gate 			return (sc == LDAP_SUCCESS ? 0 : -1);
398*7c478bd9Sstevel@tonic-gate 		}
399*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_SSF_MIN:
400*7c478bd9Sstevel@tonic-gate 		ld->ld_sasl_secprops.min_ssf = *((sasl_ssf_t *)optdata);
401*7c478bd9Sstevel@tonic-gate 		break;
402*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_SSF_MAX:
403*7c478bd9Sstevel@tonic-gate 		ld->ld_sasl_secprops.max_ssf = *((sasl_ssf_t *)optdata);
404*7c478bd9Sstevel@tonic-gate 		break;
405*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_MAXBUFSIZE:
406*7c478bd9Sstevel@tonic-gate 		ld->ld_sasl_secprops.maxbufsize = *((sasl_ssf_t *)optdata);
407*7c478bd9Sstevel@tonic-gate 		break;
408*7c478bd9Sstevel@tonic-gate 	case LDAP_OPT_X_SASL_SSF:	/* read only */
409*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO(ld, LDAP_PARAM_ERROR, NULL, NULL);
410*7c478bd9Sstevel@tonic-gate 		rc = -1;
411*7c478bd9Sstevel@tonic-gate 		break;
412*7c478bd9Sstevel@tonic-gate #endif
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	default:
415*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO(ld, LDAP_PARAM_ERROR, NULL, NULL);
416*7c478bd9Sstevel@tonic-gate 		rc = -1;
417*7c478bd9Sstevel@tonic-gate 	}
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	if (ld != &nsldapi_ld_defaults) {
420*7c478bd9Sstevel@tonic-gate 	    LDAP_MUTEX_UNLOCK(ld, LDAP_OPTION_LOCK);
421*7c478bd9Sstevel@tonic-gate 	}
422*7c478bd9Sstevel@tonic-gate 	return (rc);
423*7c478bd9Sstevel@tonic-gate }
424