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 ( ldap_int_timeval_dup( outvalue, lo->ldo_tm_api ) != 0 ) {
181 return LDAP_OPT_ERROR;
183 return LDAP_OPT_SUCCESS;
185 case LDAP_OPT_NETWORK_TIMEOUT:
186 /* the caller has to free outvalue ! */
187 if ( ldap_int_timeval_dup( outvalue, lo->ldo_tm_net ) != 0 ) {
188 return LDAP_OPT_ERROR;
190 return LDAP_OPT_SUCCESS;
193 * (int *) outvalue = lo->ldo_deref;
194 return LDAP_OPT_SUCCESS;
196 case LDAP_OPT_SIZELIMIT:
197 * (int *) outvalue = lo->ldo_sizelimit;
198 return LDAP_OPT_SUCCESS;
200 case LDAP_OPT_TIMELIMIT:
201 * (int *) outvalue = lo->ldo_timelimit;
202 return LDAP_OPT_SUCCESS;
204 case LDAP_OPT_REFERRALS:
205 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS);
206 return LDAP_OPT_SUCCESS;
208 case LDAP_OPT_RESTART:
209 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART);
210 return LDAP_OPT_SUCCESS;
212 case LDAP_OPT_PROTOCOL_VERSION:
213 * (int *) outvalue = lo->ldo_version;
214 return LDAP_OPT_SUCCESS;
216 case LDAP_OPT_SERVER_CONTROLS:
217 * (LDAPControl ***) outvalue =
218 ldap_controls_dup( lo->ldo_sctrls );
220 return LDAP_OPT_SUCCESS;
222 case LDAP_OPT_CLIENT_CONTROLS:
223 * (LDAPControl ***) outvalue =
224 ldap_controls_dup( lo->ldo_cctrls );
226 return LDAP_OPT_SUCCESS;
228 case LDAP_OPT_HOST_NAME:
229 * (char **) outvalue = ldap_url_list2hosts(lo->ldo_defludp);
230 return LDAP_OPT_SUCCESS;
233 * (char **) outvalue = ldap_url_list2urls(lo->ldo_defludp);
234 return LDAP_OPT_SUCCESS;
236 case LDAP_OPT_DEFBASE:
237 if( lo->ldo_defbase == NULL ) {
238 * (char **) outvalue = NULL;
240 * (char **) outvalue = LDAP_STRDUP(lo->ldo_defbase);
243 return LDAP_OPT_SUCCESS;
245 case LDAP_OPT_CONNECT_ASYNC:
246 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_CONNECT_ASYNC);
247 return LDAP_OPT_SUCCESS;
249 case LDAP_OPT_RESULT_CODE:
254 * (int *) outvalue = ld->ld_errno;
255 return LDAP_OPT_SUCCESS;
257 case LDAP_OPT_DIAGNOSTIC_MESSAGE:
263 if( ld->ld_error == NULL ) {
264 * (char **) outvalue = NULL;
266 * (char **) outvalue = LDAP_STRDUP(ld->ld_error);
269 return LDAP_OPT_SUCCESS;
271 case LDAP_OPT_MATCHED_DN:
277 if( ld->ld_matched == NULL ) {
278 * (char **) outvalue = NULL;
280 * (char **) outvalue = LDAP_STRDUP( ld->ld_matched );
283 return LDAP_OPT_SUCCESS;
285 case LDAP_OPT_REFERRAL_URLS:
291 if( ld->ld_referrals == NULL ) {
292 * (char ***) outvalue = NULL;
294 * (char ***) outvalue = ldap_value_dup(ld->ld_referrals);
297 return LDAP_OPT_SUCCESS;
299 case LDAP_OPT_API_FEATURE_INFO: {
300 LDAPAPIFeatureInfo *info = (LDAPAPIFeatureInfo *) outvalue;
303 if(info == NULL) return LDAP_OPT_ERROR;
305 if(info->ldapaif_info_version != LDAP_FEATURE_INFO_VERSION) {
306 /* api info version mismatch */
307 info->ldapaif_info_version = LDAP_FEATURE_INFO_VERSION;
308 return LDAP_OPT_ERROR;
311 if(info->ldapaif_name == NULL) return LDAP_OPT_ERROR;
313 for(i=0; features[i].ldapaif_name != NULL; i++) {
314 if(!strcmp(info->ldapaif_name, features[i].ldapaif_name)) {
315 info->ldapaif_version =
316 features[i].ldapaif_version;
317 return LDAP_OPT_SUCCESS;
323 case LDAP_OPT_DEBUG_LEVEL:
324 * (int *) outvalue = lo->ldo_debug;
325 return LDAP_OPT_SUCCESS;
329 if ( ldap_pvt_tls_get_option( ld, option, outvalue ) == 0 ) {
330 return LDAP_OPT_SUCCESS;
333 #ifdef HAVE_CYRUS_SASL
334 if ( ldap_int_sasl_get_option( ld, option, outvalue ) == 0 ) {
335 return LDAP_OPT_SUCCESS;
342 return LDAP_OPT_ERROR;
349 LDAP_CONST void *invalue)
351 struct ldapoptions *lo;
354 /* Get pointer to global option structure */
355 lo = LDAP_INT_GLOBAL_OPT();
357 return LDAP_NO_MEMORY;
361 * The architecture to turn on debugging has a chicken and egg
362 * problem. Thus, we introduce a fix here.
365 if (option == LDAP_OPT_DEBUG_LEVEL) {
366 dbglvl = (int *) invalue;
369 if( lo->ldo_valid != LDAP_INITIALIZED ) {
370 ldap_int_initialize(lo, dbglvl);
374 assert( LDAP_VALID( ld ) );
376 if( !LDAP_VALID( ld ) ) {
377 return LDAP_OPT_ERROR;
380 lo = &ld->ld_options;
384 case LDAP_OPT_REFERRALS:
385 if(invalue == LDAP_OPT_OFF) {
386 LDAP_BOOL_CLR(lo, LDAP_BOOL_REFERRALS);
388 LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS);
390 return LDAP_OPT_SUCCESS;
392 case LDAP_OPT_RESTART:
393 if(invalue == LDAP_OPT_OFF) {
394 LDAP_BOOL_CLR(lo, LDAP_BOOL_RESTART);
396 LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART);
398 return LDAP_OPT_SUCCESS;
400 case LDAP_OPT_CONNECT_ASYNC:
401 if(invalue == LDAP_OPT_OFF) {
402 LDAP_BOOL_CLR(lo, LDAP_BOOL_CONNECT_ASYNC);
404 LDAP_BOOL_SET(lo, LDAP_BOOL_CONNECT_ASYNC);
406 return LDAP_OPT_SUCCESS;
409 /* options which can withstand invalue == NULL */
411 case LDAP_OPT_SERVER_CONTROLS: {
412 LDAPControl *const *controls =
413 (LDAPControl *const *) invalue;
416 ldap_controls_free( lo->ldo_sctrls );
418 if( controls == NULL || *controls == NULL ) {
419 lo->ldo_sctrls = NULL;
420 return LDAP_OPT_SUCCESS;
423 lo->ldo_sctrls = ldap_controls_dup( controls );
425 if(lo->ldo_sctrls == NULL) {
426 /* memory allocation error ? */
429 } return LDAP_OPT_SUCCESS;
431 case LDAP_OPT_CLIENT_CONTROLS: {
432 LDAPControl *const *controls =
433 (LDAPControl *const *) invalue;
436 ldap_controls_free( lo->ldo_cctrls );
438 if( controls == NULL || *controls == NULL ) {
439 lo->ldo_cctrls = NULL;
440 return LDAP_OPT_SUCCESS;
443 lo->ldo_cctrls = ldap_controls_dup( controls );
445 if(lo->ldo_cctrls == NULL) {
446 /* memory allocation error ? */
449 } return LDAP_OPT_SUCCESS;
451 case LDAP_OPT_TIMEOUT: {
452 const struct timeval *tv =
453 (const struct timeval *) invalue;
455 if ( lo->ldo_tm_api != NULL ) {
456 LDAP_FREE( lo->ldo_tm_api );
457 lo->ldo_tm_api = NULL;
460 if ( ldap_int_timeval_dup( &lo->ldo_tm_api, tv ) != 0 ) {
461 return LDAP_OPT_ERROR;
463 } return LDAP_OPT_SUCCESS;
465 case LDAP_OPT_NETWORK_TIMEOUT: {
466 const struct timeval *tv =
467 (const struct timeval *) invalue;
469 if ( lo->ldo_tm_net != NULL ) {
470 LDAP_FREE( lo->ldo_tm_net );
471 lo->ldo_tm_net = NULL;
474 if ( ldap_int_timeval_dup( &lo->ldo_tm_net, tv ) != 0 ) {
475 return LDAP_OPT_ERROR;
477 } return LDAP_OPT_SUCCESS;
479 case LDAP_OPT_HOST_NAME: {
480 const char *host = (const char *) invalue;
481 LDAPURLDesc *ludlist = NULL;
482 int rc = LDAP_OPT_SUCCESS;
485 rc = ldap_url_parsehosts( &ludlist, host,
486 lo->ldo_defport ? lo->ldo_defport : LDAP_PORT );
488 } else if(ld == NULL) {
490 * must want global default returned
491 * to initial condition.
493 rc = ldap_url_parselist_ext(&ludlist, "ldap://localhost/", NULL,
494 LDAP_PVT_URL_PARSE_NOEMPTY_HOST
495 | LDAP_PVT_URL_PARSE_DEF_PORT );
499 * must want the session default
500 * updated to the current global default
502 ludlist = ldap_url_duplist(
503 ldap_int_global_options.ldo_defludp);
508 if (rc == LDAP_OPT_SUCCESS) {
509 if (lo->ldo_defludp != NULL)
510 ldap_free_urllist(lo->ldo_defludp);
511 lo->ldo_defludp = ludlist;
517 const char *urls = (const char *) invalue;
518 LDAPURLDesc *ludlist = NULL;
519 int rc = LDAP_OPT_SUCCESS;
522 rc = ldap_url_parselist_ext(&ludlist, urls, NULL,
523 LDAP_PVT_URL_PARSE_NOEMPTY_HOST
524 | LDAP_PVT_URL_PARSE_DEF_PORT );
525 } else if(ld == NULL) {
527 * must want global default returned
528 * to initial condition.
530 rc = ldap_url_parselist_ext(&ludlist, "ldap://localhost/", NULL,
531 LDAP_PVT_URL_PARSE_NOEMPTY_HOST
532 | LDAP_PVT_URL_PARSE_DEF_PORT );
536 * must want the session default
537 * updated to the current global default
539 ludlist = ldap_url_duplist(
540 ldap_int_global_options.ldo_defludp);
542 rc = LDAP_URL_ERR_MEM;
546 case LDAP_URL_SUCCESS: /* Success */
550 case LDAP_URL_ERR_MEM: /* can't allocate memory space */
554 case LDAP_URL_ERR_PARAM: /* parameter is bad */
555 case LDAP_URL_ERR_BADSCHEME: /* URL doesn't begin with "ldap[si]://" */
556 case LDAP_URL_ERR_BADENCLOSURE: /* URL is missing trailing ">" */
557 case LDAP_URL_ERR_BADURL: /* URL is bad */
558 case LDAP_URL_ERR_BADHOST: /* host port is bad */
559 case LDAP_URL_ERR_BADATTRS: /* bad (or missing) attributes */
560 case LDAP_URL_ERR_BADSCOPE: /* scope string is invalid (or missing) */
561 case LDAP_URL_ERR_BADFILTER: /* bad or missing filter */
562 case LDAP_URL_ERR_BADEXTS: /* bad or missing extensions */
563 rc = LDAP_PARAM_ERROR;
567 if (rc == LDAP_SUCCESS) {
568 if (lo->ldo_defludp != NULL)
569 ldap_free_urllist(lo->ldo_defludp);
570 lo->ldo_defludp = ludlist;
575 case LDAP_OPT_DEFBASE: {
576 const char *newbase = (const char *) invalue;
577 char *defbase = NULL;
579 if ( newbase != NULL ) {
580 defbase = LDAP_STRDUP( newbase );
581 if ( defbase == NULL ) return LDAP_NO_MEMORY;
583 } else if ( ld != NULL ) {
584 defbase = LDAP_STRDUP( ldap_int_global_options.ldo_defbase );
585 if ( defbase == NULL ) return LDAP_NO_MEMORY;
588 if ( lo->ldo_defbase != NULL )
589 LDAP_FREE( lo->ldo_defbase );
590 lo->ldo_defbase = defbase;
591 } return LDAP_OPT_SUCCESS;
593 case LDAP_OPT_DIAGNOSTIC_MESSAGE: {
594 const char *err = (const char *) invalue;
597 /* need a struct ldap */
598 return LDAP_OPT_ERROR;
602 LDAP_FREE(ld->ld_error);
607 ld->ld_error = LDAP_STRDUP(err);
609 } return LDAP_OPT_SUCCESS;
611 case LDAP_OPT_MATCHED_DN: {
612 const char *matched = (const char *) invalue;
615 /* need a struct ldap */
616 return LDAP_OPT_ERROR;
619 if( ld->ld_matched ) {
620 LDAP_FREE(ld->ld_matched);
621 ld->ld_matched = NULL;
625 ld->ld_matched = LDAP_STRDUP( matched );
627 } return LDAP_OPT_SUCCESS;
629 case LDAP_OPT_REFERRAL_URLS: {
630 char *const *referrals = (char *const *) invalue;
633 /* need a struct ldap */
634 return LDAP_OPT_ERROR;
637 if( ld->ld_referrals ) {
638 LDAP_VFREE(ld->ld_referrals);
642 ld->ld_referrals = ldap_value_dup(referrals);
644 } return LDAP_OPT_SUCCESS;
646 /* Only accessed from inside this function by ldap_set_rebind_proc() */
647 case LDAP_OPT_REBIND_PROC: {
648 lo->ldo_rebind_proc = (LDAP_REBIND_PROC *)invalue;
649 } return LDAP_OPT_SUCCESS;
650 case LDAP_OPT_REBIND_PARAMS: {
651 lo->ldo_rebind_params = (void *)invalue;
652 } return LDAP_OPT_SUCCESS;
654 /* Only accessed from inside this function by ldap_set_nextref_proc() */
655 case LDAP_OPT_NEXTREF_PROC: {
656 lo->ldo_nextref_proc = (LDAP_NEXTREF_PROC *)invalue;
657 } return LDAP_OPT_SUCCESS;
658 case LDAP_OPT_NEXTREF_PARAMS: {
659 lo->ldo_nextref_params = (void *)invalue;
660 } return LDAP_OPT_SUCCESS;
662 /* Only accessed from inside this function by ldap_set_urllist_proc() */
663 case LDAP_OPT_URLLIST_PROC: {
664 lo->ldo_urllist_proc = (LDAP_URLLIST_PROC *)invalue;
665 } return LDAP_OPT_SUCCESS;
666 case LDAP_OPT_URLLIST_PARAMS: {
667 lo->ldo_urllist_params = (void *)invalue;
668 } return LDAP_OPT_SUCCESS;
670 /* read-only options */
671 case LDAP_OPT_API_INFO:
673 case LDAP_OPT_SOCKBUF:
674 case LDAP_OPT_API_FEATURE_INFO:
675 return LDAP_OPT_ERROR;
677 /* options which cannot withstand invalue == NULL */
679 case LDAP_OPT_SIZELIMIT:
680 case LDAP_OPT_TIMELIMIT:
681 case LDAP_OPT_PROTOCOL_VERSION:
682 case LDAP_OPT_RESULT_CODE:
683 case LDAP_OPT_DEBUG_LEVEL:
684 if(invalue == NULL) {
685 /* no place to set from */
686 return LDAP_OPT_ERROR;
692 if ( ldap_pvt_tls_set_option( ld, option, (void *)invalue ) == 0 )
693 return LDAP_OPT_SUCCESS;
695 #ifdef HAVE_CYRUS_SASL
696 if ( ldap_int_sasl_set_option( ld, option, (void *)invalue ) == 0 )
697 return LDAP_OPT_SUCCESS;
700 return LDAP_OPT_ERROR;
703 /* options which cannot withstand invalue == NULL */
707 /* FIXME: check value for protocol compliance? */
708 lo->ldo_deref = * (const int *) invalue;
709 return LDAP_OPT_SUCCESS;
711 case LDAP_OPT_SIZELIMIT:
712 /* FIXME: check value for protocol compliance? */
713 lo->ldo_sizelimit = * (const int *) invalue;
714 return LDAP_OPT_SUCCESS;
716 case LDAP_OPT_TIMELIMIT:
717 /* FIXME: check value for protocol compliance? */
718 lo->ldo_timelimit = * (const int *) invalue;
719 return LDAP_OPT_SUCCESS;
721 case LDAP_OPT_PROTOCOL_VERSION: {
722 int vers = * (const int *) invalue;
723 if (vers < LDAP_VERSION_MIN || vers > LDAP_VERSION_MAX) {
727 lo->ldo_version = vers;
728 } return LDAP_OPT_SUCCESS;
730 case LDAP_OPT_RESULT_CODE: {
731 int err = * (const int *) invalue;
734 /* need a struct ldap */
739 } return LDAP_OPT_SUCCESS;
741 case LDAP_OPT_DEBUG_LEVEL:
742 lo->ldo_debug = * (const int *) invalue;
743 return LDAP_OPT_SUCCESS;
745 return LDAP_OPT_ERROR;
749 ldap_set_rebind_proc( LDAP *ld, LDAP_REBIND_PROC *proc, void *params )
752 rc = ldap_set_option( ld, LDAP_OPT_REBIND_PROC, (void *)proc );
753 if( rc != LDAP_OPT_SUCCESS ) return rc;
755 rc = ldap_set_option( ld, LDAP_OPT_REBIND_PARAMS, (void *)params );
760 ldap_set_nextref_proc( LDAP *ld, LDAP_NEXTREF_PROC *proc, void *params )
763 rc = ldap_set_option( ld, LDAP_OPT_NEXTREF_PROC, (void *)proc );
764 if( rc != LDAP_OPT_SUCCESS ) return rc;
766 rc = ldap_set_option( ld, LDAP_OPT_NEXTREF_PARAMS, (void *)params );
771 ldap_set_urllist_proc( LDAP *ld, LDAP_URLLIST_PROC *proc, void *params )
774 rc = ldap_set_option( ld, LDAP_OPT_URLLIST_PROC, (void *)proc );
775 if( rc != LDAP_OPT_SUCCESS ) return rc;
777 rc = ldap_set_option( ld, LDAP_OPT_URLLIST_PARAMS, (void *)params );