X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Foptions.c;h=7d099089cb714f32dd12feef2bea816d2c38daff;hb=2b0819c4a9209784f762ec154ce4721038522a8a;hp=149c83cc30dbc0f6b2b9efd3418f06b27732446e;hpb=1bcec8bf6a17a65396b2c947faed846d20428db9;p=openldap diff --git a/libraries/libldap/options.c b/libraries/libldap/options.c index 149c83cc30..7d099089cb 100644 --- a/libraries/libldap/options.c +++ b/libraries/libldap/options.c @@ -1,18 +1,24 @@ +/* $OpenLDAP$ */ /* - * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. + * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved. * COPYING RESTRICTIONS APPLY, see COPYRIGHT file */ #include "portable.h" #include -#include + +#include #include #include +#include #include "ldap-int.h" +#define LDAP_OPT_REBIND_PROC 0x4e814d +#define LDAP_OPT_REBIND_PARAMS 0x4e814e + static const LDAPAPIFeatureInfo features[] = { #ifdef LDAP_API_FEATURE_X_OPENLDAP { /* OpenLDAP Extensions API Feature */ @@ -58,13 +64,6 @@ static const LDAPAPIFeatureInfo features[] = { LDAP_API_FEATURE_X_OPENLDAP_THREAD_SAFE }, #endif -#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS - { /* DNS */ - LDAP_FEATURE_INFO_VERSION, - "X_OPENLDAP_V2_DNS", - LDAP_API_FEATURE_X_OPENLDAP_V2_DNS - }, -#endif #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS { /* V2 Referrals */ LDAP_FEATURE_INFO_VERSION, @@ -83,19 +82,17 @@ ldap_get_option( { struct ldapoptions *lo; - if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) { - ldap_int_initialize(); + /* Get pointer to global option structure */ + lo = LDAP_INT_GLOBAL_OPT(); + if (NULL == lo) { + return LDAP_NO_MEMORY; } - if(outvalue == NULL) { - /* no place to get to */ - return LDAP_OPT_ERROR; + if( lo->ldo_valid != LDAP_INITIALIZED ) { + ldap_int_initialize(lo, NULL); } - if(ld == NULL) { - lo = &ldap_int_global_options; - - } else { + if(ld != NULL) { assert( LDAP_VALID( ld ) ); if( !LDAP_VALID( ld ) ) { @@ -105,6 +102,11 @@ ldap_get_option( lo = &ld->ld_options; } + if(outvalue == NULL) { + /* no place to get to */ + return LDAP_OPT_ERROR; + } + switch(option) { case LDAP_OPT_API_INFO: { struct ldapapiinfo *info = (struct ldapapiinfo *) outvalue; @@ -133,25 +135,41 @@ ldap_get_option( for(i=0; features[i].ldapaif_name != NULL; i++) { info->ldapai_extensions[i] = - strdup(features[i].ldapaif_name); + LDAP_STRDUP(features[i].ldapaif_name); } info->ldapai_extensions[i] = NULL; } - info->ldapai_vendor_name = strdup(LDAP_VENDOR_NAME); + info->ldapai_vendor_name = LDAP_STRDUP(LDAP_VENDOR_NAME); info->ldapai_vendor_version = LDAP_VENDOR_VERSION; return LDAP_OPT_SUCCESS; } break; case LDAP_OPT_DESC: - if(ld == NULL) { + if( ld == NULL || ld->ld_sb == NULL ) { /* bad param */ break; } - * (int *) outvalue = ber_pvt_sb_get_desc( &(ld->ld_sb) ); + ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, outvalue ); + return LDAP_OPT_SUCCESS; + + case LDAP_OPT_TIMEOUT: + /* the caller has to free outvalue ! */ + if ( ldap_int_timeval_dup( outvalue, lo->ldo_tm_api) != 0 ) + { + return LDAP_OPT_ERROR; + } + return LDAP_OPT_SUCCESS; + + case LDAP_OPT_NETWORK_TIMEOUT: + /* the caller has to free outvalue ! */ + if ( ldap_int_timeval_dup( outvalue, lo->ldo_tm_net ) != 0 ) + { + return LDAP_OPT_ERROR; + } return LDAP_OPT_SUCCESS; case LDAP_OPT_DEREF: @@ -167,26 +185,15 @@ ldap_get_option( return LDAP_OPT_SUCCESS; case LDAP_OPT_REFERRALS: - * (int *) outvalue = (LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS) == - LDAP_OPT_ON); + * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS); return LDAP_OPT_SUCCESS; case LDAP_OPT_RESTART: - * (int *) outvalue = (LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART) == - LDAP_OPT_ON); - return LDAP_OPT_SUCCESS; - - case LDAP_OPT_DNS: /* LDAPv2 */ - * (int *) outvalue = (LDAP_BOOL_GET(lo, LDAP_BOOL_DNS) == - LDAP_OPT_ON); + * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART); return LDAP_OPT_SUCCESS; case LDAP_OPT_PROTOCOL_VERSION: - if ((ld != NULL) && ld->ld_version) { - * (int *) outvalue = ld->ld_version; - } else { - * (int *) outvalue = lo->ldo_version; - } + * (int *) outvalue = lo->ldo_version; return LDAP_OPT_SUCCESS; case LDAP_OPT_SERVER_CONTROLS: @@ -202,13 +209,11 @@ ldap_get_option( return LDAP_OPT_SUCCESS; case LDAP_OPT_HOST_NAME: - /* - * draft-ietf-ldapext-ldap-c-api-01 doesn't state - * whether caller has to free host names or not, - * we do. - */ + * (char **) outvalue = ldap_url_list2hosts(lo->ldo_defludp); + return LDAP_OPT_SUCCESS; - * (char **) outvalue = strdup(lo->ldo_defhost); + case LDAP_OPT_URI: + * (char **) outvalue = ldap_url_list2urls(lo->ldo_defludp); return LDAP_OPT_SUCCESS; case LDAP_OPT_ERROR_NUMBER: @@ -225,37 +230,27 @@ ldap_get_option( break; } - /* - * draft-ietf-ldapext-ldap-c-api-01 doesn't require - * the client to have to free error strings, we do - */ - if( ld->ld_error == NULL ) { * (char **) outvalue = NULL; } else { - * (char **) outvalue = strdup(ld->ld_error); + * (char **) outvalue = LDAP_STRDUP(ld->ld_error); } return LDAP_OPT_SUCCESS; - case LDAP_OPT_MATCH_STRING: + case LDAP_OPT_MATCHED_DN: if(ld == NULL) { /* bad param */ break; } - /* - * draft-ietf-ldapext-ldap-c-api-01 doesn't require - * the client to have to free error strings, we do - */ - if( ld->ld_matched == NULL ) { * (char **) outvalue = NULL; } else { - * (char **) outvalue = strdup(ld->ld_matched); + * (char **) outvalue = LDAP_STRDUP(ld->ld_matched); } - return 0; + return LDAP_OPT_SUCCESS; case LDAP_OPT_API_FEATURE_INFO: { LDAPAPIFeatureInfo *info = (LDAPAPIFeatureInfo *) outvalue; @@ -286,6 +281,16 @@ ldap_get_option( return LDAP_OPT_SUCCESS; default: +#ifdef HAVE_TLS + if ( ldap_pvt_tls_get_option( ld, option, outvalue ) == 0 ) { + return LDAP_OPT_SUCCESS; + } +#endif +#ifdef HAVE_CYRUS_SASL + if ( ldap_int_sasl_get_option( ld, option, outvalue ) == 0 ) { + return LDAP_OPT_SUCCESS; + } +#endif /* bad param */ break; } @@ -300,15 +305,27 @@ ldap_set_option( LDAP_CONST void *invalue) { struct ldapoptions *lo; + int *dbglvl = NULL; - if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) { - ldap_int_initialize(); + /* Get pointer to global option structure */ + lo = LDAP_INT_GLOBAL_OPT(); + if (lo == NULL) { + return LDAP_NO_MEMORY; } - if(ld == NULL) { - lo = &ldap_int_global_options; + /* + * The architecture to turn on debugging has a chicken and egg + * problem. Thus, we introduce a fix here. + */ - } else { + if (option == LDAP_OPT_DEBUG_LEVEL) + dbglvl = (int *) invalue; + + if( lo->ldo_valid != LDAP_INITIALIZED ) { + ldap_int_initialize(lo, dbglvl); + } + + if(ld != NULL) { assert( LDAP_VALID( ld ) ); if( !LDAP_VALID( ld ) ) { @@ -320,56 +337,27 @@ ldap_set_option( switch(option) { case LDAP_OPT_REFERRALS: - if(invalue == LDAP_OPT_ON) { - LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS); - } else { + if(invalue == LDAP_OPT_OFF) { LDAP_BOOL_CLR(lo, LDAP_BOOL_REFERRALS); + } else { + LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS); } return LDAP_OPT_SUCCESS; case LDAP_OPT_RESTART: - if(invalue == LDAP_OPT_ON) { - LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART); - } else { + if(invalue == LDAP_OPT_OFF) { LDAP_BOOL_CLR(lo, LDAP_BOOL_RESTART); + } else { + LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART); } return LDAP_OPT_SUCCESS; } - if(invalue == NULL) { - /* no place to set from */ - return LDAP_OPT_ERROR; - } - - switch(option) { - case LDAP_OPT_API_INFO: - case LDAP_OPT_DESC: - /* READ ONLY */ - break; - - case LDAP_OPT_DEREF: - lo->ldo_deref = * (int *) invalue; - return LDAP_OPT_SUCCESS; - - case LDAP_OPT_SIZELIMIT: - lo->ldo_sizelimit = * (int *) invalue; - return LDAP_OPT_SUCCESS; - - case LDAP_OPT_TIMELIMIT: - lo->ldo_timelimit = * (int *) invalue; - return LDAP_OPT_SUCCESS; - - case LDAP_OPT_PROTOCOL_VERSION: { - int vers = * (int *) invalue; - if (vers < LDAP_VERSION_MIN || vers > LDAP_VERSION_MAX) { - /* not supported */ - break; - } - ld->ld_version = vers; - } return LDAP_OPT_SUCCESS; - + /* options which can withstand invalue == NULL */ + switch ( option ) { case LDAP_OPT_SERVER_CONTROLS: { - LDAPControl **controls = (LDAPControl **) invalue; + LDAPControl *const *controls = + (LDAPControl *const *) invalue; ldap_controls_free( lo->ldo_sctrls ); @@ -378,8 +366,7 @@ ldap_set_option( return LDAP_OPT_SUCCESS; } - lo->ldo_sctrls = - ldap_controls_dup( (LDAPControl **) invalue ); + lo->ldo_sctrls = ldap_controls_dup( controls ); if(lo->ldo_sctrls == NULL) { /* memory allocation error ? */ @@ -388,7 +375,8 @@ ldap_set_option( } return LDAP_OPT_SUCCESS; case LDAP_OPT_CLIENT_CONTROLS: { - LDAPControl **controls = (LDAPControl **) invalue; + LDAPControl *const *controls = + (LDAPControl *const *) invalue; ldap_controls_free( lo->ldo_cctrls ); @@ -397,8 +385,7 @@ ldap_set_option( return LDAP_OPT_SUCCESS; } - lo->ldo_cctrls = - ldap_controls_dup( (LDAPControl **) invalue ); + lo->ldo_cctrls = ldap_controls_dup( controls ); if(lo->ldo_cctrls == NULL) { /* memory allocation error ? */ @@ -406,38 +393,149 @@ ldap_set_option( } } return LDAP_OPT_SUCCESS; - case LDAP_OPT_HOST_NAME: { - char* host = (char *) invalue; + case LDAP_OPT_TIMEOUT: { + const struct timeval *tv = + (const struct timeval *) invalue; + + if ( lo->ldo_tm_api != NULL ) { + LDAP_FREE( lo->ldo_tm_api ); + lo->ldo_tm_api = NULL; + } - if(lo->ldo_defhost != NULL) { - LDAP_FREE(lo->ldo_defhost); - lo->ldo_defhost = NULL; + if ( ldap_int_timeval_dup( &lo->ldo_tm_api, tv ) != 0 ) { + return LDAP_OPT_ERROR; } + } return LDAP_OPT_SUCCESS; + + case LDAP_OPT_NETWORK_TIMEOUT: { + const struct timeval *tv = + (const struct timeval *) invalue; + + if ( lo->ldo_tm_net != NULL ) { + LDAP_FREE( lo->ldo_tm_net ); + lo->ldo_tm_net = NULL; + } + + if ( ldap_int_timeval_dup( &lo->ldo_tm_net, tv ) != 0 ) { + return LDAP_OPT_ERROR; + } + } return LDAP_OPT_SUCCESS; + + /* Only accessed from inside this function by ldap_set_rebind_proc() */ + case LDAP_OPT_REBIND_PROC: { + lo->ldo_rebind_proc = (LDAP_REBIND_PROC *)invalue; + } return LDAP_OPT_SUCCESS; + case LDAP_OPT_REBIND_PARAMS: { + lo->ldo_rebind_params = (void *)invalue; + } return LDAP_OPT_SUCCESS; + } + + if(invalue == NULL) { + /* no place to set from */ + return LDAP_OPT_ERROR; + } + + /* options which cannot withstand invalue == NULL */ + + switch(option) { + case LDAP_OPT_API_INFO: + case LDAP_OPT_DESC: + /* READ ONLY */ + break; + + case LDAP_OPT_DEREF: + lo->ldo_deref = * (const int *) invalue; + return LDAP_OPT_SUCCESS; + + case LDAP_OPT_SIZELIMIT: + lo->ldo_sizelimit = * (const int *) invalue; + return LDAP_OPT_SUCCESS; + + case LDAP_OPT_TIMELIMIT: + lo->ldo_timelimit = * (const int *) invalue; + return LDAP_OPT_SUCCESS; + + case LDAP_OPT_PROTOCOL_VERSION: { + int vers = * (const int *) invalue; + if (vers < LDAP_VERSION_MIN || vers > LDAP_VERSION_MAX) { + /* not supported */ + break; + } + lo->ldo_version = vers; + } return LDAP_OPT_SUCCESS; + + + case LDAP_OPT_HOST_NAME: { + const char *host = (const char *) invalue; + LDAPURLDesc *ludlist = NULL; + int rc = LDAP_OPT_SUCCESS; if(host != NULL) { - lo->ldo_defhost = strdup(host); - return LDAP_OPT_SUCCESS; + rc = ldap_url_parsehosts( &ludlist, host, + lo->ldo_defport ? lo->ldo_defport : LDAP_PORT ); + + } else if(ld == NULL) { + /* + * must want global default returned + * to initial condition. + */ + rc = ldap_url_parselist(&ludlist, "ldap://localhost/"); + + } else { + /* + * must want the session default + * updated to the current global default + */ + ludlist = ldap_url_duplist( + ldap_int_global_options.ldo_defludp); + if (ludlist == NULL) + rc = LDAP_NO_MEMORY; } - if(ld == NULL) { + if (rc == LDAP_OPT_SUCCESS) { + if (lo->ldo_defludp != NULL) + ldap_free_urllist(lo->ldo_defludp); + lo->ldo_defludp = ludlist; + } + return rc; + } + + case LDAP_OPT_URI: { + const char *urls = (const char *) invalue; + LDAPURLDesc *ludlist = NULL; + int rc = LDAP_OPT_SUCCESS; + + if(urls != NULL) { + rc = ldap_url_parselist(&ludlist, urls); + + } else if(ld == NULL) { /* * must want global default returned * to initial condition. */ - lo->ldo_defhost = strdup("localhost"); + rc = ldap_url_parselist(&ludlist, "ldap://localhost/"); } else { /* * must want the session default * updated to the current global default */ - lo->ldo_defhost = strdup( - ldap_int_global_options.ldo_defhost); + ludlist = ldap_url_duplist( + ldap_int_global_options.ldo_defludp); + if (ludlist == NULL) + rc = LDAP_NO_MEMORY; } - } return LDAP_OPT_SUCCESS; + + if (rc == LDAP_OPT_SUCCESS) { + if (lo->ldo_defludp != NULL) + ldap_free_urllist(lo->ldo_defludp); + lo->ldo_defludp = ludlist; + } + return rc; + } case LDAP_OPT_ERROR_NUMBER: { - int err = * (int *) invalue; + int err = * (const int *) invalue; if(ld == NULL) { /* need a struct ldap */ @@ -448,7 +546,7 @@ ldap_set_option( } return LDAP_OPT_SUCCESS; case LDAP_OPT_ERROR_STRING: { - char* err = (char *) invalue; + const char *err = (const char *) invalue; if(ld == NULL) { /* need a struct ldap */ @@ -459,7 +557,22 @@ ldap_set_option( LDAP_FREE(ld->ld_error); } - ld->ld_error = strdup(err); + ld->ld_error = LDAP_STRDUP(err); + } return LDAP_OPT_SUCCESS; + + case LDAP_OPT_MATCHED_DN: { + const char *err = (const char *) invalue; + + if(ld == NULL) { + /* need a struct ldap */ + break; + } + + if( ld->ld_matched ) { + LDAP_FREE(ld->ld_matched); + } + + ld->ld_matched = LDAP_STRDUP(err); } return LDAP_OPT_SUCCESS; case LDAP_OPT_API_FEATURE_INFO: @@ -467,12 +580,31 @@ ldap_set_option( break; case LDAP_OPT_DEBUG_LEVEL: - lo->ldo_debug = * (int *) invalue; + lo->ldo_debug = * (const int *) invalue; return LDAP_OPT_SUCCESS; default: +#ifdef HAVE_TLS + if ( ldap_pvt_tls_set_option( ld, option, (void *)invalue ) == 0 ) + return LDAP_OPT_SUCCESS; +#endif +#ifdef HAVE_CYRUS_SASL + if ( ldap_int_sasl_set_option( ld, option, (void *)invalue ) == 0 ) + return LDAP_OPT_SUCCESS; +#endif /* bad param */ break; } return LDAP_OPT_ERROR; } + +int +ldap_set_rebind_proc( LDAP *ld, LDAP_REBIND_PROC *proc, void *params ) +{ + int rc; + rc = ldap_set_option( ld, LDAP_OPT_REBIND_PROC, (void *)proc ); + if( rc != LDAP_OPT_SUCCESS ) return rc; + + rc = ldap_set_option( ld, LDAP_OPT_REBIND_PARAMS, (void *)params ); + return rc; +}