2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/stdlib.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
28 #define LDAP_OPT_REBIND_PROC 0x4e814d
29 #define LDAP_OPT_REBIND_PARAMS 0x4e814e
31 #define LDAP_OPT_NEXTREF_PROC 0x4e815d
32 #define LDAP_OPT_NEXTREF_PARAMS 0x4e815e
34 #define LDAP_OPT_URLLIST_PROC 0x4e816d
35 #define LDAP_OPT_URLLIST_PARAMS 0x4e816e
37 static const LDAPAPIFeatureInfo features[] = {
38 #ifdef LDAP_API_FEATURE_X_OPENLDAP
39 { /* OpenLDAP Extensions API Feature */
40 LDAP_FEATURE_INFO_VERSION,
42 LDAP_API_FEATURE_X_OPENLDAP
46 #ifdef LDAP_API_FEATURE_THREAD_SAFE
47 { /* Basic Thread Safe */
48 LDAP_FEATURE_INFO_VERSION,
50 LDAP_API_FEATURE_THREAD_SAFE
53 #ifdef LDAP_API_FEATURE_SESSION_THREAD_SAFE
54 { /* Session Thread Safe */
55 LDAP_FEATURE_INFO_VERSION,
56 "SESSION_THREAD_SAFE",
57 LDAP_API_FEATURE_SESSION_THREAD_SAFE
60 #ifdef LDAP_API_FEATURE_OPERATION_THREAD_SAFE
61 { /* Operation Thread Safe */
62 LDAP_FEATURE_INFO_VERSION,
63 "OPERATION_THREAD_SAFE",
64 LDAP_API_FEATURE_OPERATION_THREAD_SAFE
67 #ifdef LDAP_API_FEATURE_X_OPENLDAP_REENTRANT
68 { /* OpenLDAP Reentrant */
69 LDAP_FEATURE_INFO_VERSION,
70 "X_OPENLDAP_REENTRANT",
71 LDAP_API_FEATURE_X_OPENLDAP_REENTRANT
74 #if defined( LDAP_API_FEATURE_X_OPENLDAP_THREAD_SAFE ) && \
75 defined( LDAP_THREAD_SAFE )
76 { /* OpenLDAP Thread Safe */
77 LDAP_FEATURE_INFO_VERSION,
78 "X_OPENLDAP_THREAD_SAFE",
79 LDAP_API_FEATURE_X_OPENLDAP_THREAD_SAFE
82 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
84 LDAP_FEATURE_INFO_VERSION,
85 "X_OPENLDAP_V2_REFERRALS",
86 LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
98 struct ldapoptions *lo;
100 /* Get pointer to global option structure */
101 lo = LDAP_INT_GLOBAL_OPT();
103 return LDAP_NO_MEMORY;
106 if( lo->ldo_valid != LDAP_INITIALIZED ) {
107 ldap_int_initialize(lo, NULL);
111 assert( LDAP_VALID( ld ) );
113 if( !LDAP_VALID( ld ) ) {
114 return LDAP_OPT_ERROR;
117 lo = &ld->ld_options;
120 if(outvalue == NULL) {
121 /* no place to get to */
122 return LDAP_OPT_ERROR;
126 case LDAP_OPT_API_INFO: {
127 struct ldapapiinfo *info = (struct ldapapiinfo *) outvalue;
130 /* outvalue must point to an apiinfo structure */
131 return LDAP_OPT_ERROR;
134 if(info->ldapai_info_version != LDAP_API_INFO_VERSION) {
135 /* api info version mismatch */
136 info->ldapai_info_version = LDAP_API_INFO_VERSION;
137 return LDAP_OPT_ERROR;
140 info->ldapai_api_version = LDAP_API_VERSION;
141 info->ldapai_protocol_version = LDAP_VERSION_MAX;
143 if(features[0].ldapaif_name == NULL) {
144 info->ldapai_extensions = NULL;
147 info->ldapai_extensions = LDAP_MALLOC(sizeof(char *) *
148 sizeof(features)/sizeof(LDAPAPIFeatureInfo));
150 for(i=0; features[i].ldapaif_name != NULL; i++) {
151 info->ldapai_extensions[i] =
152 LDAP_STRDUP(features[i].ldapaif_name);
155 info->ldapai_extensions[i] = NULL;
158 info->ldapai_vendor_name = LDAP_STRDUP(LDAP_VENDOR_NAME);
159 info->ldapai_vendor_version = LDAP_VENDOR_VERSION;
161 return LDAP_OPT_SUCCESS;
165 if( ld == NULL || ld->ld_sb == NULL ) {
170 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, outvalue );
171 return LDAP_OPT_SUCCESS;
173 case LDAP_OPT_SOCKBUF:
174 if( ld == NULL ) break;
175 *(Sockbuf **)outvalue = ld->ld_sb;
176 return LDAP_OPT_SUCCESS;
178 case LDAP_OPT_TIMEOUT:
179 /* the caller has to free outvalue ! */
180 if ( lo->ldo_tm_api.tv_sec < 0 ) {
181 *(void **)outvalue = NULL;
182 } else if ( ldap_int_timeval_dup( outvalue, &lo->ldo_tm_api ) != 0 ) {
183 return LDAP_OPT_ERROR;
185 return LDAP_OPT_SUCCESS;
187 case LDAP_OPT_NETWORK_TIMEOUT:
188 /* the caller has to free outvalue ! */
189 if ( lo->ldo_tm_net.tv_sec < 0 ) {
190 *(void **)outvalue = NULL;
191 } else if ( ldap_int_timeval_dup( outvalue, &lo->ldo_tm_net ) != 0 ) {
192 return LDAP_OPT_ERROR;
194 return LDAP_OPT_SUCCESS;
197 * (int *) outvalue = lo->ldo_deref;
198 return LDAP_OPT_SUCCESS;
200 case LDAP_OPT_SIZELIMIT:
201 * (int *) outvalue = lo->ldo_sizelimit;
202 return LDAP_OPT_SUCCESS;
204 case LDAP_OPT_TIMELIMIT:
205 * (int *) outvalue = lo->ldo_timelimit;
206 return LDAP_OPT_SUCCESS;
208 case LDAP_OPT_REFERRALS:
209 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS);
210 return LDAP_OPT_SUCCESS;
212 case LDAP_OPT_RESTART:
213 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART);
214 return LDAP_OPT_SUCCESS;
216 case LDAP_OPT_PROTOCOL_VERSION:
217 * (int *) outvalue = lo->ldo_version;
218 return LDAP_OPT_SUCCESS;
220 case LDAP_OPT_SERVER_CONTROLS:
221 * (LDAPControl ***) outvalue =
222 ldap_controls_dup( lo->ldo_sctrls );
224 return LDAP_OPT_SUCCESS;
226 case LDAP_OPT_CLIENT_CONTROLS:
227 * (LDAPControl ***) outvalue =
228 ldap_controls_dup( lo->ldo_cctrls );
230 return LDAP_OPT_SUCCESS;
232 case LDAP_OPT_HOST_NAME:
233 * (char **) outvalue = ldap_url_list2hosts(lo->ldo_defludp);
234 return LDAP_OPT_SUCCESS;
237 * (char **) outvalue = ldap_url_list2urls(lo->ldo_defludp);
238 return LDAP_OPT_SUCCESS;
240 case LDAP_OPT_DEFBASE:
241 if( lo->ldo_defbase == NULL ) {
242 * (char **) outvalue = NULL;
244 * (char **) outvalue = LDAP_STRDUP(lo->ldo_defbase);
247 return LDAP_OPT_SUCCESS;
249 case LDAP_OPT_CONNECT_ASYNC:
250 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_CONNECT_ASYNC);
251 return LDAP_OPT_SUCCESS;
253 case LDAP_OPT_RESULT_CODE:
258 * (int *) outvalue = ld->ld_errno;
259 return LDAP_OPT_SUCCESS;
261 case LDAP_OPT_DIAGNOSTIC_MESSAGE:
267 if( ld->ld_error == NULL ) {
268 * (char **) outvalue = NULL;
270 * (char **) outvalue = LDAP_STRDUP(ld->ld_error);
273 return LDAP_OPT_SUCCESS;
275 case LDAP_OPT_MATCHED_DN:
281 if( ld->ld_matched == NULL ) {
282 * (char **) outvalue = NULL;
284 * (char **) outvalue = LDAP_STRDUP( ld->ld_matched );
287 return LDAP_OPT_SUCCESS;
289 case LDAP_OPT_REFERRAL_URLS:
295 if( ld->ld_referrals == NULL ) {
296 * (char ***) outvalue = NULL;
298 * (char ***) outvalue = ldap_value_dup(ld->ld_referrals);
301 return LDAP_OPT_SUCCESS;
303 case LDAP_OPT_API_FEATURE_INFO: {
304 LDAPAPIFeatureInfo *info = (LDAPAPIFeatureInfo *) outvalue;
307 if(info == NULL) return LDAP_OPT_ERROR;
309 if(info->ldapaif_info_version != LDAP_FEATURE_INFO_VERSION) {
310 /* api info version mismatch */
311 info->ldapaif_info_version = LDAP_FEATURE_INFO_VERSION;
312 return LDAP_OPT_ERROR;
315 if(info->ldapaif_name == NULL) return LDAP_OPT_ERROR;
317 for(i=0; features[i].ldapaif_name != NULL; i++) {
318 if(!strcmp(info->ldapaif_name, features[i].ldapaif_name)) {
319 info->ldapaif_version =
320 features[i].ldapaif_version;
321 return LDAP_OPT_SUCCESS;
327 case LDAP_OPT_DEBUG_LEVEL:
328 * (int *) outvalue = lo->ldo_debug;
329 return LDAP_OPT_SUCCESS;
333 if ( ldap_pvt_tls_get_option( ld, option, outvalue ) == 0 ) {
334 return LDAP_OPT_SUCCESS;
337 #ifdef HAVE_CYRUS_SASL
338 if ( ldap_int_sasl_get_option( ld, option, outvalue ) == 0 ) {
339 return LDAP_OPT_SUCCESS;
346 return LDAP_OPT_ERROR;
353 LDAP_CONST void *invalue)
355 struct ldapoptions *lo;
358 /* Get pointer to global option structure */
359 lo = LDAP_INT_GLOBAL_OPT();
361 return LDAP_NO_MEMORY;
365 * The architecture to turn on debugging has a chicken and egg
366 * problem. Thus, we introduce a fix here.
369 if (option == LDAP_OPT_DEBUG_LEVEL) {
370 dbglvl = (int *) invalue;
373 if( lo->ldo_valid != LDAP_INITIALIZED ) {
374 ldap_int_initialize(lo, dbglvl);
378 assert( LDAP_VALID( ld ) );
380 if( !LDAP_VALID( ld ) ) {
381 return LDAP_OPT_ERROR;
384 lo = &ld->ld_options;
388 case LDAP_OPT_REFERRALS:
389 if(invalue == LDAP_OPT_OFF) {
390 LDAP_BOOL_CLR(lo, LDAP_BOOL_REFERRALS);
392 LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS);
394 return LDAP_OPT_SUCCESS;
396 case LDAP_OPT_RESTART:
397 if(invalue == LDAP_OPT_OFF) {
398 LDAP_BOOL_CLR(lo, LDAP_BOOL_RESTART);
400 LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART);
402 return LDAP_OPT_SUCCESS;
404 case LDAP_OPT_CONNECT_ASYNC:
405 if(invalue == LDAP_OPT_OFF) {
406 LDAP_BOOL_CLR(lo, LDAP_BOOL_CONNECT_ASYNC);
408 LDAP_BOOL_SET(lo, LDAP_BOOL_CONNECT_ASYNC);
410 return LDAP_OPT_SUCCESS;
413 /* options which can withstand invalue == NULL */
415 case LDAP_OPT_SERVER_CONTROLS: {
416 LDAPControl *const *controls =
417 (LDAPControl *const *) invalue;
420 ldap_controls_free( lo->ldo_sctrls );
422 if( controls == NULL || *controls == NULL ) {
423 lo->ldo_sctrls = NULL;
424 return LDAP_OPT_SUCCESS;
427 lo->ldo_sctrls = ldap_controls_dup( controls );
429 if(lo->ldo_sctrls == NULL) {
430 /* memory allocation error ? */
433 } return LDAP_OPT_SUCCESS;
435 case LDAP_OPT_CLIENT_CONTROLS: {
436 LDAPControl *const *controls =
437 (LDAPControl *const *) invalue;
440 ldap_controls_free( lo->ldo_cctrls );
442 if( controls == NULL || *controls == NULL ) {
443 lo->ldo_cctrls = NULL;
444 return LDAP_OPT_SUCCESS;
447 lo->ldo_cctrls = ldap_controls_dup( controls );
449 if(lo->ldo_cctrls == NULL) {
450 /* memory allocation error ? */
453 } return LDAP_OPT_SUCCESS;
456 case LDAP_OPT_HOST_NAME: {
457 const char *host = (const char *) invalue;
458 LDAPURLDesc *ludlist = NULL;
459 int rc = LDAP_OPT_SUCCESS;
462 rc = ldap_url_parsehosts( &ludlist, host,
463 lo->ldo_defport ? lo->ldo_defport : LDAP_PORT );
465 } else if(ld == NULL) {
467 * must want global default returned
468 * to initial condition.
470 rc = ldap_url_parselist_ext(&ludlist, "ldap://localhost/", NULL,
471 LDAP_PVT_URL_PARSE_NOEMPTY_HOST
472 | LDAP_PVT_URL_PARSE_DEF_PORT );
476 * must want the session default
477 * updated to the current global default
479 ludlist = ldap_url_duplist(
480 ldap_int_global_options.ldo_defludp);
485 if (rc == LDAP_OPT_SUCCESS) {
486 if (lo->ldo_defludp != NULL)
487 ldap_free_urllist(lo->ldo_defludp);
488 lo->ldo_defludp = ludlist;
494 const char *urls = (const char *) invalue;
495 LDAPURLDesc *ludlist = NULL;
496 int rc = LDAP_OPT_SUCCESS;
499 rc = ldap_url_parselist_ext(&ludlist, urls, NULL,
500 LDAP_PVT_URL_PARSE_NOEMPTY_HOST
501 | LDAP_PVT_URL_PARSE_DEF_PORT );
502 } else if(ld == NULL) {
504 * must want global default returned
505 * to initial condition.
507 rc = ldap_url_parselist_ext(&ludlist, "ldap://localhost/", NULL,
508 LDAP_PVT_URL_PARSE_NOEMPTY_HOST
509 | LDAP_PVT_URL_PARSE_DEF_PORT );
513 * must want the session default
514 * updated to the current global default
516 ludlist = ldap_url_duplist(
517 ldap_int_global_options.ldo_defludp);
519 rc = LDAP_URL_ERR_MEM;
523 case LDAP_URL_SUCCESS: /* Success */
527 case LDAP_URL_ERR_MEM: /* can't allocate memory space */
531 case LDAP_URL_ERR_PARAM: /* parameter is bad */
532 case LDAP_URL_ERR_BADSCHEME: /* URL doesn't begin with "ldap[si]://" */
533 case LDAP_URL_ERR_BADENCLOSURE: /* URL is missing trailing ">" */
534 case LDAP_URL_ERR_BADURL: /* URL is bad */
535 case LDAP_URL_ERR_BADHOST: /* host port is bad */
536 case LDAP_URL_ERR_BADATTRS: /* bad (or missing) attributes */
537 case LDAP_URL_ERR_BADSCOPE: /* scope string is invalid (or missing) */
538 case LDAP_URL_ERR_BADFILTER: /* bad or missing filter */
539 case LDAP_URL_ERR_BADEXTS: /* bad or missing extensions */
540 rc = LDAP_PARAM_ERROR;
544 if (rc == LDAP_SUCCESS) {
545 if (lo->ldo_defludp != NULL)
546 ldap_free_urllist(lo->ldo_defludp);
547 lo->ldo_defludp = ludlist;
552 case LDAP_OPT_DEFBASE: {
553 const char *newbase = (const char *) invalue;
554 char *defbase = NULL;
556 if ( newbase != NULL ) {
557 defbase = LDAP_STRDUP( newbase );
558 if ( defbase == NULL ) return LDAP_NO_MEMORY;
560 } else if ( ld != NULL ) {
561 defbase = LDAP_STRDUP( ldap_int_global_options.ldo_defbase );
562 if ( defbase == NULL ) return LDAP_NO_MEMORY;
565 if ( lo->ldo_defbase != NULL )
566 LDAP_FREE( lo->ldo_defbase );
567 lo->ldo_defbase = defbase;
568 } return LDAP_OPT_SUCCESS;
570 case LDAP_OPT_DIAGNOSTIC_MESSAGE: {
571 const char *err = (const char *) invalue;
574 /* need a struct ldap */
575 return LDAP_OPT_ERROR;
579 LDAP_FREE(ld->ld_error);
584 ld->ld_error = LDAP_STRDUP(err);
586 } return LDAP_OPT_SUCCESS;
588 case LDAP_OPT_MATCHED_DN: {
589 const char *matched = (const char *) invalue;
592 /* need a struct ldap */
593 return LDAP_OPT_ERROR;
596 if( ld->ld_matched ) {
597 LDAP_FREE(ld->ld_matched);
598 ld->ld_matched = NULL;
602 ld->ld_matched = LDAP_STRDUP( matched );
604 } return LDAP_OPT_SUCCESS;
606 case LDAP_OPT_REFERRAL_URLS: {
607 char *const *referrals = (char *const *) invalue;
610 /* need a struct ldap */
611 return LDAP_OPT_ERROR;
614 if( ld->ld_referrals ) {
615 LDAP_VFREE(ld->ld_referrals);
619 ld->ld_referrals = ldap_value_dup(referrals);
621 } return LDAP_OPT_SUCCESS;
623 /* Only accessed from inside this function by ldap_set_rebind_proc() */
624 case LDAP_OPT_REBIND_PROC: {
625 lo->ldo_rebind_proc = (LDAP_REBIND_PROC *)invalue;
626 } return LDAP_OPT_SUCCESS;
627 case LDAP_OPT_REBIND_PARAMS: {
628 lo->ldo_rebind_params = (void *)invalue;
629 } return LDAP_OPT_SUCCESS;
631 /* Only accessed from inside this function by ldap_set_nextref_proc() */
632 case LDAP_OPT_NEXTREF_PROC: {
633 lo->ldo_nextref_proc = (LDAP_NEXTREF_PROC *)invalue;
634 } return LDAP_OPT_SUCCESS;
635 case LDAP_OPT_NEXTREF_PARAMS: {
636 lo->ldo_nextref_params = (void *)invalue;
637 } return LDAP_OPT_SUCCESS;
639 /* Only accessed from inside this function by ldap_set_urllist_proc() */
640 case LDAP_OPT_URLLIST_PROC: {
641 lo->ldo_urllist_proc = (LDAP_URLLIST_PROC *)invalue;
642 } return LDAP_OPT_SUCCESS;
643 case LDAP_OPT_URLLIST_PARAMS: {
644 lo->ldo_urllist_params = (void *)invalue;
645 } return LDAP_OPT_SUCCESS;
647 /* read-only options */
648 case LDAP_OPT_API_INFO:
650 case LDAP_OPT_SOCKBUF:
651 case LDAP_OPT_API_FEATURE_INFO:
652 return LDAP_OPT_ERROR;
654 /* options which cannot withstand invalue == NULL */
656 case LDAP_OPT_SIZELIMIT:
657 case LDAP_OPT_TIMELIMIT:
658 case LDAP_OPT_PROTOCOL_VERSION:
659 case LDAP_OPT_RESULT_CODE:
660 case LDAP_OPT_DEBUG_LEVEL:
661 case LDAP_OPT_TIMEOUT:
662 case LDAP_OPT_NETWORK_TIMEOUT:
663 if(invalue == NULL) {
664 /* no place to set from */
665 return LDAP_OPT_ERROR;
671 if ( ldap_pvt_tls_set_option( ld, option, (void *)invalue ) == 0 )
672 return LDAP_OPT_SUCCESS;
674 #ifdef HAVE_CYRUS_SASL
675 if ( ldap_int_sasl_set_option( ld, option, (void *)invalue ) == 0 )
676 return LDAP_OPT_SUCCESS;
679 return LDAP_OPT_ERROR;
682 /* options which cannot withstand invalue == NULL */
686 /* FIXME: check value for protocol compliance? */
687 lo->ldo_deref = * (const int *) invalue;
688 return LDAP_OPT_SUCCESS;
690 case LDAP_OPT_SIZELIMIT:
691 /* FIXME: check value for protocol compliance? */
692 lo->ldo_sizelimit = * (const int *) invalue;
693 return LDAP_OPT_SUCCESS;
695 case LDAP_OPT_TIMELIMIT:
696 /* FIXME: check value for protocol compliance? */
697 lo->ldo_timelimit = * (const int *) invalue;
698 return LDAP_OPT_SUCCESS;
700 case LDAP_OPT_TIMEOUT: {
701 const struct timeval *tv =
702 (const struct timeval *) invalue;
704 lo->ldo_tm_api = *tv;
705 } return LDAP_OPT_SUCCESS;
707 case LDAP_OPT_NETWORK_TIMEOUT: {
708 const struct timeval *tv =
709 (const struct timeval *) invalue;
711 lo->ldo_tm_net = *tv;
712 } return LDAP_OPT_SUCCESS;
714 case LDAP_OPT_PROTOCOL_VERSION: {
715 int vers = * (const int *) invalue;
716 if (vers < LDAP_VERSION_MIN || vers > LDAP_VERSION_MAX) {
720 lo->ldo_version = vers;
721 } return LDAP_OPT_SUCCESS;
723 case LDAP_OPT_RESULT_CODE: {
724 int err = * (const int *) invalue;
727 /* need a struct ldap */
732 } return LDAP_OPT_SUCCESS;
734 case LDAP_OPT_DEBUG_LEVEL:
735 lo->ldo_debug = * (const int *) invalue;
736 return LDAP_OPT_SUCCESS;
738 return LDAP_OPT_ERROR;
742 ldap_set_rebind_proc( LDAP *ld, LDAP_REBIND_PROC *proc, void *params )
745 rc = ldap_set_option( ld, LDAP_OPT_REBIND_PROC, (void *)proc );
746 if( rc != LDAP_OPT_SUCCESS ) return rc;
748 rc = ldap_set_option( ld, LDAP_OPT_REBIND_PARAMS, (void *)params );
753 ldap_set_nextref_proc( LDAP *ld, LDAP_NEXTREF_PROC *proc, void *params )
756 rc = ldap_set_option( ld, LDAP_OPT_NEXTREF_PROC, (void *)proc );
757 if( rc != LDAP_OPT_SUCCESS ) return rc;
759 rc = ldap_set_option( ld, LDAP_OPT_NEXTREF_PARAMS, (void *)params );
764 ldap_set_urllist_proc( LDAP *ld, LDAP_URLLIST_PROC *proc, void *params )
767 rc = ldap_set_option( ld, LDAP_OPT_URLLIST_PROC, (void *)proc );
768 if( rc != LDAP_OPT_SUCCESS ) return rc;
770 rc = ldap_set_option( ld, LDAP_OPT_URLLIST_PARAMS, (void *)params );