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