]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/options.c
further clarify size limit related issues in sync replication (ITS#5243)
[openldap] / libraries / libldap / options.c
index d7db40b27e86fc4f787bb195e2b627b43bdeb92f..69f730211ea00726abc32f2fa0fe6e7d65de40f1 100644 (file)
@@ -1,7 +1,16 @@
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2007 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
 
 #include "ldap-int.h"
 
+#define LDAP_OPT_REBIND_PROC 0x4e814d
+#define LDAP_OPT_REBIND_PARAMS 0x4e814e
+
+#define LDAP_OPT_NEXTREF_PROC 0x4e815d
+#define LDAP_OPT_NEXTREF_PARAMS 0x4e815e
+
 static const LDAPAPIFeatureInfo features[] = {
 #ifdef LDAP_API_FEATURE_X_OPENLDAP
        {       /* OpenLDAP Extensions API Feature */
@@ -61,13 +76,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,
@@ -84,16 +92,19 @@ ldap_get_option(
        int             option,
        void    *outvalue)
 {
-       const struct ldapoptions *lo;
+       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(ld == NULL) {
-               lo = &ldap_int_global_options;
+       if( lo->ldo_valid != LDAP_INITIALIZED ) {
+               ldap_int_initialize(lo, NULL);
+       }
 
-       } else {
+       if(ld != NULL) {
                assert( LDAP_VALID( ld ) );
 
                if( !LDAP_VALID( ld ) ) {
@@ -123,7 +134,6 @@ ldap_get_option(
                                return LDAP_OPT_ERROR;
                        }
 
-                       info->ldapai_api_version = LDAP_API_VERSION;
                        info->ldapai_api_version = LDAP_API_VERSION;
                        info->ldapai_protocol_version = LDAP_VERSION_MAX;
 
@@ -149,26 +159,29 @@ ldap_get_option(
                } break;
 
        case LDAP_OPT_DESC:
-               if(ld == NULL) {
+               if( ld == NULL || ld->ld_sb == NULL ) {
                        /* bad param */
                        break;
                } 
 
-               * (ber_socket_t *) 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_SOCKBUF:
+               if( ld == NULL ) break;
+               *(Sockbuf **)outvalue = ld->ld_sb;
                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 )
-               {
+               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 )
-               {
+               if ( ldap_int_timeval_dup( outvalue, lo->ldo_tm_net ) != 0 ) {
                        return LDAP_OPT_ERROR;
                }
                return LDAP_OPT_SUCCESS;
@@ -193,10 +206,6 @@ ldap_get_option(
                * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART);
                return LDAP_OPT_SUCCESS;
 
-       case LDAP_OPT_DNS:      /* LDAPv2 */
-               * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_DNS);
-               return LDAP_OPT_SUCCESS;
-
        case LDAP_OPT_PROTOCOL_VERSION:
                * (int *) outvalue = lo->ldo_version;
                return LDAP_OPT_SUCCESS;
@@ -252,7 +261,21 @@ ldap_get_option(
                if( ld->ld_matched == NULL ) {
                        * (char **) outvalue = NULL;
                } else {
-                       * (char **) outvalue = LDAP_STRDUP(ld->ld_matched);
+                       * (char **) outvalue = LDAP_STRDUP( ld->ld_matched );
+               }
+
+               return LDAP_OPT_SUCCESS;
+
+       case LDAP_OPT_REFERRAL_URLS:
+               if(ld == NULL) {
+                       /* bad param */
+                       break;
+               } 
+
+               if( ld->ld_referrals == NULL ) {
+                       * (char ***) outvalue = NULL;
+               } else {
+                       * (char ***) outvalue = ldap_value_dup(ld->ld_referrals);
                }
 
                return LDAP_OPT_SUCCESS;
@@ -287,12 +310,14 @@ ldap_get_option(
 
        default:
 #ifdef HAVE_TLS
-               if ( ldap_pvt_tls_get_option((struct ldapoptions *)lo, option, outvalue ) == 0 )
-                       return LDAP_OPT_SUCCESS;
+               if ( ldap_pvt_tls_get_option( ld, option, outvalue ) == 0 ) {
+                       return LDAP_OPT_SUCCESS;
+               }
 #endif
 #ifdef HAVE_CYRUS_SASL
-               if ( ldap_pvt_sasl_get_option(ld, option, outvalue ) == 0 )
-                       return LDAP_OPT_SUCCESS;
+               if ( ldap_int_sasl_get_option( ld, option, outvalue ) == 0 ) {
+                       return LDAP_OPT_SUCCESS;
+               }
 #endif
                /* bad param */
                break;
@@ -308,15 +333,28 @@ 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 ) ) {
@@ -350,7 +388,8 @@ ldap_set_option(
                        LDAPControl *const *controls =
                                (LDAPControl *const *) invalue;
 
-                       ldap_controls_free( lo->ldo_sctrls );
+                       if( lo->ldo_sctrls )
+                               ldap_controls_free( lo->ldo_sctrls );
 
                        if( controls == NULL || *controls == NULL ) {
                                lo->ldo_sctrls = NULL;
@@ -369,7 +408,8 @@ ldap_set_option(
                        LDAPControl *const *controls =
                                (LDAPControl *const *) invalue;
 
-                       ldap_controls_free( lo->ldo_cctrls );
+                       if( lo->ldo_cctrls )
+                               ldap_controls_free( lo->ldo_cctrls );
 
                        if( controls == NULL || *controls == NULL ) {
                                lo->ldo_cctrls = NULL;
@@ -411,42 +451,6 @@ ldap_set_option(
                                return LDAP_OPT_ERROR;
                        }
                } 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;
@@ -454,7 +458,8 @@ ldap_set_option(
                        int rc = LDAP_OPT_SUCCESS;
 
                        if(host != NULL) {
-                               rc = ldap_url_parsehosts(&ludlist, host);
+                               rc = ldap_url_parsehosts( &ludlist, host,
+                                       lo->ldo_defport ? lo->ldo_defport : LDAP_PORT );
 
                        } else if(ld == NULL) {
                                /*
@@ -489,7 +494,6 @@ ldap_set_option(
 
                        if(urls != NULL) {
                                rc = ldap_url_parselist(&ludlist, urls);
-
                        } else if(ld == NULL) {
                                /*
                                 * must want global default returned
@@ -508,6 +512,28 @@ ldap_set_option(
                                        rc = LDAP_NO_MEMORY;
                        }
 
+                       switch (rc) {
+                       case LDAP_URL_SUCCESS:          /* Success */
+                               rc = LDAP_SUCCESS;
+                               break;
+
+                       case LDAP_URL_ERR_MEM:          /* can't allocate memory space */
+                               rc = LDAP_NO_MEMORY;
+                               break;
+
+                       case LDAP_URL_ERR_PARAM:        /* parameter is bad */
+                       case LDAP_URL_ERR_BADSCHEME:    /* URL doesn't begin with "ldap[si]://" */
+                       case LDAP_URL_ERR_BADENCLOSURE: /* URL is missing trailing ">" */
+                       case LDAP_URL_ERR_BADURL:       /* URL is bad */
+                       case LDAP_URL_ERR_BADHOST:      /* host port is bad */
+                       case LDAP_URL_ERR_BADATTRS:     /* bad (or missing) attributes */
+                       case LDAP_URL_ERR_BADSCOPE:     /* scope string is invalid (or missing) */
+                       case LDAP_URL_ERR_BADFILTER:    /* bad or missing filter */
+                       case LDAP_URL_ERR_BADEXTS:      /* bad or missing extensions */
+                               rc = LDAP_PARAM_ERROR;
+                               break;
+                       }
+
                        if (rc == LDAP_OPT_SUCCESS) {
                                if (lo->ldo_defludp != NULL)
                                        ldap_free_urllist(lo->ldo_defludp);
@@ -516,6 +542,57 @@ ldap_set_option(
                        return rc;
                }
 
+       /* 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;
+
+       /* Only accessed from inside this function by ldap_set_nextref_proc() */
+       case LDAP_OPT_NEXTREF_PROC: {
+                       lo->ldo_nextref_proc = (LDAP_NEXTREF_PROC *)invalue;            
+               } return LDAP_OPT_SUCCESS;
+       case LDAP_OPT_NEXTREF_PARAMS: {
+                       lo->ldo_nextref_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_ERROR_NUMBER: {
                        int err = * (const int *) invalue;
 
@@ -537,24 +614,45 @@ ldap_set_option(
 
                        if( ld->ld_error ) {
                                LDAP_FREE(ld->ld_error);
+                               ld->ld_error = NULL;
                        }
 
-                       ld->ld_error = LDAP_STRDUP(err);
+                       if ( err ) {
+                               ld->ld_error = LDAP_STRDUP(err);
+                       }
                } return LDAP_OPT_SUCCESS;
 
        case LDAP_OPT_MATCHED_DN: {
-                       const char *err = (const char *) invalue;
+                       const char *matched = (const char *) invalue;
 
-                       if(ld == NULL) {
+                       if (ld == NULL) {
                                /* need a struct ldap */
                                break;
                        }
 
                        if( ld->ld_matched ) {
                                LDAP_FREE(ld->ld_matched);
+                               ld->ld_matched = NULL;
                        }
 
-                       ld->ld_matched = LDAP_STRDUP(err);
+                       if ( matched ) {
+                               ld->ld_matched = LDAP_STRDUP( matched );
+                       }
+               } return LDAP_OPT_SUCCESS;
+
+       case LDAP_OPT_REFERRAL_URLS: {
+                       char *const *referrals = (char *const *) invalue;
+                       
+                       if(ld == NULL) {
+                               /* need a struct ldap */
+                               break;
+                       }
+
+                       if( ld->ld_referrals ) {
+                               LDAP_VFREE(ld->ld_referrals);
+                       }
+
+                       ld->ld_referrals = ldap_value_dup(referrals);
                } return LDAP_OPT_SUCCESS;
 
        case LDAP_OPT_API_FEATURE_INFO:
@@ -567,11 +665,11 @@ ldap_set_option(
 
        default:
 #ifdef HAVE_TLS
-               if ( ldap_pvt_tls_set_option( lo, option, (void *)invalue ) == 0 )
-               return LDAP_OPT_SUCCESS;
+               if ( ldap_pvt_tls_set_option( ld, option, (void *)invalue ) == 0 )
+                       return LDAP_OPT_SUCCESS;
 #endif
 #ifdef HAVE_CYRUS_SASL
-               if ( ldap_pvt_sasl_set_option( ld, option, (void *)invalue ) == 0 )
+               if ( ldap_int_sasl_set_option( ld, option, (void *)invalue ) == 0 )
                        return LDAP_OPT_SUCCESS;
 #endif
                /* bad param */
@@ -579,3 +677,25 @@ ldap_set_option(
        }
        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;
+}
+
+int
+ldap_set_nextref_proc( LDAP *ld, LDAP_NEXTREF_PROC *proc, void *params )
+{
+       int rc;
+       rc = ldap_set_option( ld, LDAP_OPT_NEXTREF_PROC, (void *)proc );
+       if( rc != LDAP_OPT_SUCCESS ) return rc;
+
+       rc = ldap_set_option( ld, LDAP_OPT_NEXTREF_PARAMS, (void *)params );
+       return rc;
+}