]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/options.c
Braced ldap_connect_to_path() in #ifdef LDAP_PF_LOCAL so as to compile
[openldap] / libraries / libldap / options.c
index ef19dfda61f273e2ea341c128f99b7863324a252..62af0ec4f96bf17a9a2e7fb87fbefe071a9df064 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenLDAP$ */
 /*
  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
@@ -11,6 +12,7 @@
 
 #include <ac/socket.h>
 #include <ac/string.h>
+#include <ac/time.h>
 
 #include "ldap-int.h"
 
@@ -78,11 +80,11 @@ static const LDAPAPIFeatureInfo features[] = {
 
 int
 ldap_get_option(
-       LDAP_CONST LDAP *ld,
+       LDAP    *ld,
        int             option,
        void    *outvalue)
 {
-       LDAP_CONST struct ldapoptions *lo;
+       const struct ldapoptions *lo;
 
        if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
                ldap_int_initialize();
@@ -155,6 +157,22 @@ ldap_get_option(
                * (ber_socket_t *) outvalue = ber_pvt_sb_get_desc( &(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 )
+               {
+                       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:
                * (int *) outvalue = lo->ldo_deref;
                return LDAP_OPT_SUCCESS;
@@ -200,7 +218,11 @@ ldap_get_option(
                return LDAP_OPT_SUCCESS;
 
        case LDAP_OPT_HOST_NAME:
-               * (char **) outvalue = LDAP_STRDUP(lo->ldo_defhost);
+               * (char **) outvalue = ldap_url_list2hosts(lo->ldo_defludp);
+               return LDAP_OPT_SUCCESS;
+
+       case LDAP_OPT_URI:
+               * (char **) outvalue = ldap_url_list2urls(lo->ldo_defludp);
                return LDAP_OPT_SUCCESS;
 
        case LDAP_OPT_ERROR_NUMBER:
@@ -268,6 +290,10 @@ ldap_get_option(
                return LDAP_OPT_SUCCESS;
 
        default:
+#ifdef HAVE_TLS
+               if ( ldap_pvt_tls_get_option((struct ldapoptions *)lo, option, outvalue ) == 0 )
+                       return LDAP_OPT_SUCCESS;
+#endif
                /* bad param */
                break;
        }
@@ -318,40 +344,11 @@ ldap_set_option(
                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 );
 
@@ -360,8 +357,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 ? */
@@ -370,7 +366,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 );
 
@@ -379,8 +376,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 ? */
@@ -388,38 +384,140 @@ 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 ( 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;
+       }
+
+       if(invalue == NULL) {
+               /* no place to set from */
+               return LDAP_OPT_ERROR;
+       }
+
+       /* options which cannot withstand invalue == NULL */
 
-                       if(lo->ldo_defhost != NULL) {
-                               LDAP_FREE(lo->ldo_defhost);
-                               lo->ldo_defhost = 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;
                        }
+                       ld->ld_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 = LDAP_STRDUP(host);
-                               return LDAP_OPT_SUCCESS;
+                               rc = ldap_url_parsehosts(&ludlist, host);
+
+                       } 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 = LDAP_STRDUP("localhost");
+                               rc = ldap_url_parselist(&ludlist, "ldap://localhost/");
 
                        } else {
                                /*
                                 * must want the session default
                                 *   updated to the current global default
                                 */
-                               lo->ldo_defhost = LDAP_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 */
@@ -430,7 +528,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 */
@@ -445,7 +543,7 @@ ldap_set_option(
                } return LDAP_OPT_SUCCESS;
 
        case LDAP_OPT_MATCHED_DN: {
-                       char* err = (char *) invalue;
+                       const char *err = (const char *) invalue;
 
                        if(ld == NULL) {
                                /* need a struct ldap */
@@ -464,10 +562,14 @@ 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( lo, option, (void *)invalue ) == 0 )
+                       return LDAP_OPT_SUCCESS;
+#endif
                /* bad param */
                break;
        }