1N/A/*
1N/A * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
1N/A */
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A/*
1N/A * setoption.c - ldap_set_option implementation
1N/A */
1N/A
1N/A#include "ldap-int.h"
1N/A#ifdef _SOLARIS_SDK
1N/A#include "solaris-priv.h"
1N/A#endif
1N/A
1N/Aextern int nsldapi_sasl_secprops(const char *in,
1N/A sasl_security_properties_t *secprops);
1N/A
1N/A#define LDAP_SETCLR_BITOPT(ld, bit, optdata) \
1N/A if (optdata != NULL) { \
1N/A (ld)->ld_options |= bit; \
1N/A } else { \
1N/A (ld)->ld_options &= ~bit; \
1N/A }
1N/A
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_set_option(LDAP *ld, int option, const void *optdata)
1N/A{
1N/A int rc, i;
1N/A char *matched, *errstr;
1N/A
1N/A if (!nsldapi_initialized) {
1N/A nsldapi_initialize_defaults();
1N/A }
1N/A
1N/A /*
1N/A * process global options (not associated with an LDAP session handle)
1N/A */
1N/A if (option == LDAP_OPT_MEMALLOC_FN_PTRS) {
1N/A struct lber_memalloc_fns memalloc_fns;
1N/A
1N/A /* set libldap ones via a struct copy */
1N/A nsldapi_memalloc_fns = *((struct ldap_memalloc_fns *)optdata);
1N/A
1N/A /* also set liblber memory allocation callbacks */
1N/A memalloc_fns.lbermem_malloc =
1N/A nsldapi_memalloc_fns.ldapmem_malloc;
1N/A memalloc_fns.lbermem_calloc =
1N/A nsldapi_memalloc_fns.ldapmem_calloc;
1N/A memalloc_fns.lbermem_realloc =
1N/A nsldapi_memalloc_fns.ldapmem_realloc;
1N/A memalloc_fns.lbermem_free =
1N/A nsldapi_memalloc_fns.ldapmem_free;
1N/A if (ber_set_option(NULL, LBER_OPT_MEMALLOC_FN_PTRS,
1N/A &memalloc_fns) != 0) {
1N/A return (-1);
1N/A }
1N/A
1N/A return (0);
1N/A }
1N/A /*
1N/A * LDAP_OPT_DEBUG_LEVEL is global
1N/A */
1N/A if (LDAP_OPT_DEBUG_LEVEL == option) {
1N/A#ifdef LDAP_DEBUG
1N/A ldap_debug = *((int *)optdata);
1N/A#endif
1N/A return (0);
1N/A }
1N/A
1N/A /*
1N/A * if ld is NULL, arrange to modify our default settings
1N/A */
1N/A if (ld == NULL) {
1N/A ld = &nsldapi_ld_defaults;
1N/A#ifdef LDAP_DEBUG
1N/A ldap_debug = 0;
1N/A#endif
1N/A
1N/A }
1N/A
1N/A /*
1N/A * process options that are associated with an LDAP session handle
1N/A */
1N/A if (!NSLDAPI_VALID_LDAP_POINTER(ld)) {
1N/A return (-1); /* punt */
1N/A }
1N/A
1N/A rc = 0;
1N/A if (ld != &nsldapi_ld_defaults &&
1N/A option != LDAP_OPT_EXTRA_THREAD_FN_PTRS &&
1N/A option != LDAP_OPT_THREAD_FN_PTRS) {
1N/A LDAP_MUTEX_LOCK(ld, LDAP_OPTION_LOCK);
1N/A }
1N/A switch (option) {
1N/A /* options that can be turned on and off */
1N/A#ifdef LDAP_DNS
1N/A case LDAP_OPT_DNS:
1N/A LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_DNS, optdata);
1N/A break;
1N/A#endif
1N/A
1N/A case LDAP_OPT_REFERRALS:
1N/A LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_REFERRALS, optdata);
1N/A break;
1N/A
1N/A#ifdef LDAP_SSLIO_HOOKS
1N/A case LDAP_OPT_SSL:
1N/A LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_SSL, optdata);
1N/A break;
1N/A#endif
1N/A
1N/A case LDAP_OPT_RESTART:
1N/A LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_RESTART, optdata);
1N/A break;
1N/A
1N/A case LDAP_OPT_RECONNECT:
1N/A LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_RECONNECT, optdata);
1N/A break;
1N/A
1N/A#ifdef LDAP_ASYNC_IO
1N/A case LDAP_OPT_ASYNC_CONNECT:
1N/A LDAP_SETCLR_BITOPT(ld, LDAP_BITOPT_ASYNC, optdata);
1N/A break;
1N/A#endif /* LDAP_ASYNC_IO */
1N/A
1N/A /* fields in the LDAP structure */
1N/A case LDAP_OPT_DEREF:
1N/A ld->ld_deref = *((int *)optdata);
1N/A break;
1N/A case LDAP_OPT_SIZELIMIT:
1N/A ld->ld_sizelimit = *((int *)optdata);
1N/A break;
1N/A case LDAP_OPT_TIMELIMIT:
1N/A ld->ld_timelimit = *((int *)optdata);
1N/A break;
1N/A case LDAP_OPT_REFERRAL_HOP_LIMIT:
1N/A ld->ld_refhoplimit = *((int *)optdata);
1N/A break;
1N/A case LDAP_OPT_PROTOCOL_VERSION:
1N/A ld->ld_version = *((int *)optdata);
1N/A if (ld->ld_defconn != NULL) { /* also set in default conn. */
1N/A ld->ld_defconn->lconn_version = ld->ld_version;
1N/A }
1N/A break;
1N/A case LDAP_OPT_SERVER_CONTROLS:
1N/A /* nsldapi_dup_controls returns -1 and sets lderrno on error */
1N/A rc = nsldapi_dup_controls(ld, &ld->ld_servercontrols,
1N/A (LDAPControl **)optdata);
1N/A break;
1N/A case LDAP_OPT_CLIENT_CONTROLS:
1N/A /* nsldapi_dup_controls returns -1 and sets lderrno on error */
1N/A rc = nsldapi_dup_controls(ld, &ld->ld_clientcontrols,
1N/A (LDAPControl **)optdata);
1N/A break;
1N/A
1N/A /* rebind proc */
1N/A case LDAP_OPT_REBIND_FN:
1N/A ld->ld_rebind_fn = (LDAP_REBINDPROC_CALLBACK *) optdata;
1N/A break;
1N/A case LDAP_OPT_REBIND_ARG:
1N/A ld->ld_rebind_arg = (void *) optdata;
1N/A break;
1N/A
1N/A#ifdef LDAP_SSLIO_HOOKS
1N/A /* i/o function pointers */
1N/A case LDAP_OPT_IO_FN_PTRS:
1N/A if ((rc = nsldapi_install_compat_io_fns(ld,
1N/A (struct ldap_io_fns *)optdata)) != LDAP_SUCCESS) {
1N/A LDAP_SET_LDERRNO(ld, rc, NULL, NULL);
1N/A rc = -1;
1N/A }
1N/A break;
1N/A
1N/A /* extended i/o function pointers */
1N/A case LDAP_X_OPT_EXTIO_FN_PTRS:
1N/A /* denotes use of old iofns struct (no writev) */
1N/A if (((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_size ==
1N/A LDAP_X_EXTIO_FNS_SIZE_REV0) {
1N/A ld->ld_extio_size = LDAP_X_EXTIO_FNS_SIZE;
1N/A ld->ld_extclose_fn =
1N/A ((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_close;
1N/A ld->ld_extconnect_fn =
1N/A ((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_connect;
1N/A ld->ld_extread_fn =
1N/A ((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_read;
1N/A ld->ld_extwrite_fn =
1N/A ((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_write;
1N/A ld->ld_extpoll_fn =
1N/A ((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_poll;
1N/A ld->ld_extnewhandle_fn =
1N/A ((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_newhandle;
1N/A ld->ld_extdisposehandle_fn =
1N/A ((struct ldap_x_ext_io_fns_rev0 *)optdata)->
1N/A lextiof_disposehandle;
1N/A ld->ld_ext_session_arg =
1N/A ((struct ldap_x_ext_io_fns_rev0 *)optdata)->lextiof_session_arg;
1N/A ld->ld_extwritev_fn = NULL;
1N/A if (ber_sockbuf_set_option(ld->ld_sbp, LBER_SOCKBUF_OPT_EXT_IO_FNS,
1N/A &(ld->ld_ext_io_fns)) != 0) {
1N/A return (LDAP_LOCAL_ERROR);
1N/A }
1N/A } else {
1N/A /* struct copy */
1N/A ld->ld_ext_io_fns = *((struct ldap_x_ext_io_fns *)optdata);
1N/A }
1N/A if ((rc = nsldapi_install_lber_extiofns(ld, ld->ld_sbp))
1N/A != LDAP_SUCCESS) {
1N/A LDAP_SET_LDERRNO(ld, rc, NULL, NULL);
1N/A rc = -1;
1N/A }
1N/A break;
1N/A#endif
1N/A
1N/A /* thread function pointers */
1N/A case LDAP_OPT_THREAD_FN_PTRS:
1N/A /*
1N/A * It is only safe to set the thread function pointers
1N/A * when one thread is using the LDAP session handle.
1N/A */
1N/A /* free existing mutexes (some are allocated by ldap_init()) */
1N/A nsldapi_mutex_free_all(ld);
1N/A
1N/A /* struct copy */
1N/A ld->ld_thread = *((struct ldap_thread_fns *)optdata);
1N/A
1N/A /* allocate new mutexes */
1N/A rc = nsldapi_mutex_alloc_all(ld);
1N/A
1N/A /* LDAP_OPTION_LOCK was never locked... so just return */
1N/A return (rc);
1N/A
1N/A /* extra thread function pointers */
1N/A case LDAP_OPT_EXTRA_THREAD_FN_PTRS:
1N/A /* The extra thread funcs will only pick up the threadid */
1N/A ld->ld_thread2 = *((struct ldap_extra_thread_fns *)optdata);
1N/A
1N/A /* Reset the rest of the structure preserving the threadid fn */
1N/A ld->ld_mutex_trylock_fn = (LDAP_TF_MUTEX_TRYLOCK_CALLBACK *)NULL;
1N/A ld->ld_sema_alloc_fn = (LDAP_TF_SEMA_ALLOC_CALLBACK *) NULL;
1N/A ld->ld_sema_free_fn = (LDAP_TF_SEMA_FREE_CALLBACK *) NULL;
1N/A ld->ld_sema_wait_fn = (LDAP_TF_SEMA_WAIT_CALLBACK *) NULL;
1N/A ld->ld_sema_post_fn = (LDAP_TF_SEMA_POST_CALLBACK *) NULL;
1N/A
1N/A /* We assume that only one thread is active when replacing */
1N/A /* the threadid function. We will now proceed and reset all */
1N/A /* of the threadid/refcounts */
1N/A for (i = 0; i < LDAP_MAX_LOCK; i++) {
1N/A ld->ld_mutex_threadid[i] = (void *) -1;
1N/A ld->ld_mutex_refcnt[i] = 0;
1N/A }
1N/A
1N/A return (rc);
1N/A
1N/A /* DNS function pointers */
1N/A case LDAP_OPT_DNS_FN_PTRS:
1N/A /* struct copy */
1N/A ld->ld_dnsfn = *((struct ldap_dns_fns *)optdata);
1N/A break;
1N/A
1N/A /* cache function pointers */
1N/A case LDAP_OPT_CACHE_FN_PTRS:
1N/A /* struct copy */
1N/A ld->ld_cache = *((struct ldap_cache_fns *)optdata);
1N/A break;
1N/A case LDAP_OPT_CACHE_STRATEGY:
1N/A ld->ld_cache_strategy = *((int *)optdata);
1N/A break;
1N/A case LDAP_OPT_CACHE_ENABLE:
1N/A ld->ld_cache_on = *((int *)optdata);
1N/A break;
1N/A
1N/A case LDAP_OPT_ERROR_NUMBER:
1N/A LDAP_GET_LDERRNO(ld, &matched, &errstr);
1N/A matched = nsldapi_strdup(matched);
1N/A errstr = nsldapi_strdup(errstr);
1N/A LDAP_SET_LDERRNO(ld, *((int *)optdata), matched, errstr);
1N/A break;
1N/A
1N/A case LDAP_OPT_ERROR_STRING:
1N/A rc = LDAP_GET_LDERRNO(ld, &matched, NULL);
1N/A matched = nsldapi_strdup(matched);
1N/A LDAP_SET_LDERRNO(ld, rc, matched,
1N/A nsldapi_strdup((char *)optdata));
1N/A rc = LDAP_SUCCESS;
1N/A break;
1N/A
1N/A case LDAP_OPT_MATCHED_DN:
1N/A rc = LDAP_GET_LDERRNO(ld, NULL, &errstr);
1N/A errstr = nsldapi_strdup(errstr);
1N/A LDAP_SET_LDERRNO(ld, rc,
1N/A nsldapi_strdup((char *)optdata), errstr);
1N/A rc = LDAP_SUCCESS;
1N/A break;
1N/A
1N/A case LDAP_OPT_PREFERRED_LANGUAGE:
1N/A if (NULL != ld->ld_preferred_language) {
1N/A NSLDAPI_FREE(ld->ld_preferred_language);
1N/A }
1N/A ld->ld_preferred_language = nsldapi_strdup((char *)optdata);
1N/A break;
1N/A
1N/A case LDAP_OPT_HOST_NAME:
1N/A if (NULL != ld->ld_defhost) {
1N/A NSLDAPI_FREE(ld->ld_defhost);
1N/A }
1N/A ld->ld_defhost = nsldapi_strdup((char *)optdata);
1N/A break;
1N/A
1N/A case LDAP_X_OPT_CONNECT_TIMEOUT:
1N/A ld->ld_connect_timeout = *((int *)optdata);
1N/A break;
1N/A
1N/A#ifdef _SOLARIS_SDK
1N/A /* recursion prevention dns functions */
1N/A case LDAP_X_OPT_DNS_SKIPDB:
1N/A rc = prldap_x_install_dns_skipdb(ld, (const char *)optdata);
1N/A break;
1N/A#endif
1N/A#ifdef LDAP_SASLIO_HOOKS
1N/A /* SASL options */
1N/A case LDAP_OPT_X_SASL_MECH:
1N/A if (NULL != ld->ld_def_sasl_mech) {
1N/A
1N/A NSLDAPI_FREE(ld->ld_def_sasl_mech);
1N/A }
1N/A ld->ld_def_sasl_mech = nsldapi_strdup((char *)optdata);
1N/A break;
1N/A case LDAP_OPT_X_SASL_REALM:
1N/A if (NULL != ld->ld_def_sasl_realm) {
1N/A NSLDAPI_FREE(ld->ld_def_sasl_realm);
1N/A }
1N/A ld->ld_def_sasl_realm = nsldapi_strdup((char *)optdata);
1N/A break;
1N/A case LDAP_OPT_X_SASL_AUTHCID:
1N/A if (NULL != ld->ld_def_sasl_authcid) {
1N/A NSLDAPI_FREE(ld->ld_def_sasl_authcid);
1N/A }
1N/A ld->ld_def_sasl_authcid = nsldapi_strdup((char *)optdata);
1N/A break;
1N/A case LDAP_OPT_X_SASL_AUTHZID:
1N/A if (NULL != ld->ld_def_sasl_authzid) {
1N/A NSLDAPI_FREE(ld->ld_def_sasl_authzid);
1N/A }
1N/A ld->ld_def_sasl_authzid = nsldapi_strdup((char *)optdata);
1N/A break;
1N/A case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1N/A {
1N/A int sc;
1N/A sasl_ssf_t extprops;
1N/A sasl_conn_t *ctx;
1N/A if (ld->ld_defconn == NULL ||
1N/A ld->ld_defconn->lconn_sb == NULL) {
1N/A return (-1);
1N/A }
1N/A ctx = (sasl_conn_t *)
1N/A (ld->ld_defconn->lconn_sb->sb_sasl_ctx);
1N/A if (ctx == NULL) {
1N/A return (-1);
1N/A }
1N/A memset(&extprops, 0L, sizeof (extprops));
1N/A extprops = * ((sasl_ssf_t *)optdata);
1N/A sc = sasl_setprop(ctx, SASL_SSF_EXTERNAL,
1N/A (void *) &extprops);
1N/A if (sc != SASL_OK) {
1N/A return (-1);
1N/A }
1N/A }
1N/A break;
1N/A case LDAP_OPT_X_SASL_SECPROPS:
1N/A {
1N/A int sc;
1N/A sc = nsldapi_sasl_secprops((char *)optdata,
1N/A &ld->ld_sasl_secprops);
1N/A return (sc == LDAP_SUCCESS ? 0 : -1);
1N/A }
1N/A case LDAP_OPT_X_SASL_SSF_MIN:
1N/A ld->ld_sasl_secprops.min_ssf = *((sasl_ssf_t *)optdata);
1N/A break;
1N/A case LDAP_OPT_X_SASL_SSF_MAX:
1N/A ld->ld_sasl_secprops.max_ssf = *((sasl_ssf_t *)optdata);
1N/A break;
1N/A case LDAP_OPT_X_SASL_MAXBUFSIZE:
1N/A ld->ld_sasl_secprops.maxbufsize = *((sasl_ssf_t *)optdata);
1N/A break;
1N/A case LDAP_OPT_X_SASL_SSF: /* read only */
1N/A LDAP_SET_LDERRNO(ld, LDAP_PARAM_ERROR, NULL, NULL);
1N/A rc = -1;
1N/A break;
1N/A#endif
1N/A
1N/A default:
1N/A LDAP_SET_LDERRNO(ld, LDAP_PARAM_ERROR, NULL, NULL);
1N/A rc = -1;
1N/A }
1N/A
1N/A if (ld != &nsldapi_ld_defaults) {
1N/A LDAP_MUTEX_UNLOCK(ld, LDAP_OPTION_LOCK);
1N/A }
1N/A return (rc);
1N/A}