Back to index

openldap  2.4.31
init.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 #include "portable.h"
00017 
00018 #include <stdio.h>
00019 #include <ac/stdlib.h>
00020 
00021 #ifdef HAVE_GETEUID
00022 #include <ac/unistd.h>
00023 #endif
00024 
00025 #include <ac/socket.h>
00026 #include <ac/string.h>
00027 #include <ac/ctype.h>
00028 #include <ac/time.h>
00029 
00030 #ifdef HAVE_LIMITS_H
00031 #include <limits.h>
00032 #endif
00033 
00034 #include "ldap-int.h"
00035 #include "ldap_defaults.h"
00036 #include "lutil.h"
00037 
00038 struct ldapoptions ldap_int_global_options =
00039        { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE LDAP_LDO_MUTEX_NULLARG };  
00040 
00041 #define ATTR_NONE    0
00042 #define ATTR_BOOL    1
00043 #define ATTR_INT     2
00044 #define ATTR_KV             3
00045 #define ATTR_STRING  4
00046 #define ATTR_OPTION  5
00047 
00048 #define ATTR_SASL    6
00049 #define ATTR_TLS     7
00050 
00051 #define ATTR_OPT_TV  8
00052 #define ATTR_OPT_INT 9
00053 
00054 #define ATTR_GSSAPI  10
00055 
00056 struct ol_keyvalue {
00057        const char *         key;
00058        int                  value;
00059 };
00060 
00061 static const struct ol_keyvalue deref_kv[] = {
00062        {"never", LDAP_DEREF_NEVER},
00063        {"searching", LDAP_DEREF_SEARCHING},
00064        {"finding", LDAP_DEREF_FINDING},
00065        {"always", LDAP_DEREF_ALWAYS},
00066        {NULL, 0}
00067 };
00068 
00069 static const struct ol_attribute {
00070        int                  useronly;
00071        int                  type;
00072        const char *  name;
00073        const void *  data;
00074        size_t        offset;
00075 } attrs[] = {
00076        {0, ATTR_OPT_TV,     "TIMEOUT",           NULL,  LDAP_OPT_TIMEOUT},
00077        {0, ATTR_OPT_TV,     "NETWORK_TIMEOUT",   NULL,  LDAP_OPT_NETWORK_TIMEOUT},
00078        {0, ATTR_OPT_INT,    "VERSION",           NULL,  LDAP_OPT_PROTOCOL_VERSION},
00079        {0, ATTR_KV,         "DEREF",      deref_kv, /* or &deref_kv[0] */
00080               offsetof(struct ldapoptions, ldo_deref)},
00081        {0, ATTR_INT,        "SIZELIMIT",  NULL,
00082               offsetof(struct ldapoptions, ldo_sizelimit)},
00083        {0, ATTR_INT,        "TIMELIMIT",  NULL,
00084               offsetof(struct ldapoptions, ldo_timelimit)},
00085        {1, ATTR_STRING,     "BINDDN",            NULL,
00086               offsetof(struct ldapoptions, ldo_defbinddn)},
00087        {0, ATTR_STRING,     "BASE",                     NULL,
00088               offsetof(struct ldapoptions, ldo_defbase)},
00089        {0, ATTR_INT,        "PORT",                     NULL,         /* deprecated */
00090               offsetof(struct ldapoptions, ldo_defport)},
00091        {0, ATTR_OPTION,     "HOST",                     NULL,  LDAP_OPT_HOST_NAME}, /* deprecated */
00092        {0, ATTR_OPTION,     "URI",               NULL,  LDAP_OPT_URI}, /* replaces HOST/PORT */
00093        {0, ATTR_BOOL,              "REFERRALS",  NULL,  LDAP_BOOL_REFERRALS},
00094 #if 0
00095        /* This should only be allowed via ldap_set_option(3) */
00096        {0, ATTR_BOOL,              "RESTART",           NULL,  LDAP_BOOL_RESTART},
00097 #endif
00098 
00099 #ifdef HAVE_CYRUS_SASL
00100        {0, ATTR_STRING,     "SASL_MECH",         NULL,
00101               offsetof(struct ldapoptions, ldo_def_sasl_mech)},
00102        {0, ATTR_STRING,     "SASL_REALM",        NULL,
00103               offsetof(struct ldapoptions, ldo_def_sasl_realm)},
00104        {1, ATTR_STRING,     "SASL_AUTHCID",             NULL,
00105               offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
00106        {1, ATTR_STRING,     "SASL_AUTHZID",             NULL,
00107               offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
00108        {0, ATTR_SASL,              "SASL_SECPROPS",     NULL,  LDAP_OPT_X_SASL_SECPROPS},
00109        {0, ATTR_BOOL,              "SASL_NOCANON",      NULL,  LDAP_BOOL_SASL_NOCANON},
00110 #endif
00111 
00112 #ifdef HAVE_GSSAPI
00113        {0, ATTR_GSSAPI,"GSSAPI_SIGN",                   NULL,  LDAP_OPT_SIGN},
00114        {0, ATTR_GSSAPI,"GSSAPI_ENCRYPT",         NULL,  LDAP_OPT_ENCRYPT},
00115        {0, ATTR_GSSAPI,"GSSAPI_ALLOW_REMOTE_PRINCIPAL",NULL,   LDAP_OPT_X_GSSAPI_ALLOW_REMOTE_PRINCIPAL},
00116 #endif
00117 
00118 #ifdef HAVE_TLS
00119        {1, ATTR_TLS, "TLS_CERT",                 NULL,  LDAP_OPT_X_TLS_CERTFILE},
00120        {1, ATTR_TLS, "TLS_KEY",                  NULL,  LDAP_OPT_X_TLS_KEYFILE},
00121        {0, ATTR_TLS, "TLS_CACERT",        NULL,  LDAP_OPT_X_TLS_CACERTFILE},
00122        {0, ATTR_TLS, "TLS_CACERTDIR",     NULL,  LDAP_OPT_X_TLS_CACERTDIR},
00123        {0, ATTR_TLS, "TLS_REQCERT",              NULL,  LDAP_OPT_X_TLS_REQUIRE_CERT},
00124        {0, ATTR_TLS, "TLS_RANDFILE",             NULL,  LDAP_OPT_X_TLS_RANDOM_FILE},
00125        {0, ATTR_TLS, "TLS_CIPHER_SUITE",  NULL,  LDAP_OPT_X_TLS_CIPHER_SUITE},
00126        {0, ATTR_TLS, "TLS_PROTOCOL_MIN",  NULL,  LDAP_OPT_X_TLS_PROTOCOL_MIN},
00127 
00128 #ifdef HAVE_OPENSSL_CRL
00129        {0, ATTR_TLS, "TLS_CRLCHECK",             NULL,  LDAP_OPT_X_TLS_CRLCHECK},
00130 #endif
00131 #ifdef HAVE_GNUTLS
00132        {0, ATTR_TLS, "TLS_CRLFILE",                     NULL,  LDAP_OPT_X_TLS_CRLFILE},
00133 #endif
00134         
00135 #endif
00136 
00137        {0, ATTR_NONE,              NULL,         NULL,  0}
00138 };
00139 
00140 #define MAX_LDAP_ATTR_LEN  sizeof("GSSAPI_ALLOW_REMOTE_PRINCIPAL")
00141 #define MAX_LDAP_ENV_PREFIX_LEN 8
00142 
00143 static void openldap_ldap_init_w_conf(
00144        const char *file, int userconf )
00145 {
00146        char linebuf[ AC_LINE_MAX ];
00147        FILE *fp;
00148        int i;
00149        char *cmd, *opt;
00150        char *start, *end;
00151        struct ldapoptions *gopts;
00152 
00153        if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
00154               return;                     /* Could not allocate mem for global options */
00155        }
00156 
00157        if (file == NULL) {
00158               /* no file name */
00159               return;
00160        }
00161 
00162        Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
00163 
00164        fp = fopen(file, "r");
00165        if(fp == NULL) {
00166               /* could not open file */
00167               return;
00168        }
00169 
00170        Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
00171 
00172        while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
00173               /* skip lines starting with '#' */
00174               if(*start == '#') continue;
00175 
00176               /* trim leading white space */
00177               while((*start != '\0') && isspace((unsigned char) *start))
00178                      start++;
00179 
00180               /* anything left? */
00181               if(*start == '\0') continue;
00182 
00183               /* trim trailing white space */
00184               end = &start[strlen(start)-1];
00185               while(isspace((unsigned char)*end)) end--;
00186               end[1] = '\0';
00187 
00188               /* anything left? */
00189               if(*start == '\0') continue;
00190               
00191 
00192               /* parse the command */
00193               cmd=start;
00194               while((*start != '\0') && !isspace((unsigned char)*start)) {
00195                      start++;
00196               }
00197               if(*start == '\0') {
00198                      /* command has no argument */
00199                      continue;
00200               } 
00201 
00202               *start++ = '\0';
00203 
00204               /* we must have some whitespace to skip */
00205               while(isspace((unsigned char)*start)) start++;
00206               opt = start;
00207 
00208               for(i=0; attrs[i].type != ATTR_NONE; i++) {
00209                      void *p;
00210 
00211                      if( !userconf && attrs[i].useronly ) {
00212                             continue;
00213                      }
00214 
00215                      if(strcasecmp(cmd, attrs[i].name) != 0) {
00216                             continue;
00217                      }
00218 
00219                      switch(attrs[i].type) {
00220                      case ATTR_BOOL:
00221                             if((strcasecmp(opt, "on") == 0) 
00222                                    || (strcasecmp(opt, "yes") == 0)
00223                                    || (strcasecmp(opt, "true") == 0))
00224                             {
00225                                    LDAP_BOOL_SET(gopts, attrs[i].offset);
00226 
00227                             } else {
00228                                    LDAP_BOOL_CLR(gopts, attrs[i].offset);
00229                             }
00230 
00231                             break;
00232 
00233                      case ATTR_INT: {
00234                             char *next;
00235                             long l;
00236                             p = &((char *) gopts)[attrs[i].offset];
00237                             l = strtol( opt, &next, 10 );
00238                             if ( next != opt && next[ 0 ] == '\0' ) {
00239                                    * (int*) p = l;
00240                             }
00241                             } break;
00242 
00243                      case ATTR_KV: {
00244                                    const struct ol_keyvalue *kv;
00245 
00246                                    for(kv = attrs[i].data;
00247                                           kv->key != NULL;
00248                                           kv++) {
00249 
00250                                           if(strcasecmp(opt, kv->key) == 0) {
00251                                                  p = &((char *) gopts)[attrs[i].offset];
00252                                                  * (int*) p = kv->value;
00253                                                  break;
00254                                           }
00255                                    }
00256                             } break;
00257 
00258                      case ATTR_STRING:
00259                             p = &((char *) gopts)[attrs[i].offset];
00260                             if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
00261                             * (char**) p = LDAP_STRDUP(opt);
00262                             break;
00263                      case ATTR_OPTION:
00264                             ldap_set_option( NULL, attrs[i].offset, opt );
00265                             break;
00266                      case ATTR_SASL:
00267 #ifdef HAVE_CYRUS_SASL
00268                             ldap_int_sasl_config( gopts, attrs[i].offset, opt );
00269 #endif
00270                             break;
00271                      case ATTR_GSSAPI:
00272 #ifdef HAVE_GSSAPI
00273                             ldap_int_gssapi_config( gopts, attrs[i].offset, opt );
00274 #endif
00275                             break;
00276                      case ATTR_TLS:
00277 #ifdef HAVE_TLS
00278                             ldap_int_tls_config( NULL, attrs[i].offset, opt );
00279 #endif
00280                             break;
00281                      case ATTR_OPT_TV: {
00282                             struct timeval tv;
00283                             char *next;
00284                             tv.tv_usec = 0;
00285                             tv.tv_sec = strtol( opt, &next, 10 );
00286                             if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
00287                                    (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
00288                             }
00289                             } break;
00290                      case ATTR_OPT_INT: {
00291                             long l;
00292                             char *next;
00293                             l = strtol( opt, &next, 10 );
00294                             if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
00295                                    int v = (int)l;
00296                                    (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
00297                             }
00298                             } break;
00299                      }
00300 
00301                      break;
00302               }
00303        }
00304 
00305        fclose(fp);
00306 }
00307 
00308 static void openldap_ldap_init_w_sysconf(const char *file)
00309 {
00310        openldap_ldap_init_w_conf( file, 0 );
00311 }
00312 
00313 static void openldap_ldap_init_w_userconf(const char *file)
00314 {
00315        char *home;
00316        char *path = NULL;
00317 
00318        if (file == NULL) {
00319               /* no file name */
00320               return;
00321        }
00322 
00323        home = getenv("HOME");
00324 
00325        if (home != NULL) {
00326               Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
00327                     home, 0, 0);
00328               path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
00329        } else {
00330               Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
00331                     0, 0, 0);
00332        }
00333 
00334        if(home != NULL && path != NULL) {
00335               /* we assume UNIX path syntax is used... */
00336 
00337               /* try ~/file */
00338               sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
00339               openldap_ldap_init_w_conf(path, 1);
00340 
00341               /* try ~/.file */
00342               sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
00343               openldap_ldap_init_w_conf(path, 1);
00344        }
00345 
00346        if(path != NULL) {
00347               LDAP_FREE(path);
00348        }
00349 
00350        /* try file */
00351        openldap_ldap_init_w_conf(file, 1);
00352 }
00353 
00354 static void openldap_ldap_init_w_env(
00355               struct ldapoptions *gopts,
00356               const char *prefix)
00357 {
00358        char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
00359        int len;
00360        int i;
00361        void *p;
00362        char *value;
00363 
00364        if (prefix == NULL) {
00365               prefix = LDAP_ENV_PREFIX;
00366        }
00367 
00368        strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
00369        buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
00370        len = strlen(buf);
00371 
00372        for(i=0; attrs[i].type != ATTR_NONE; i++) {
00373               strcpy(&buf[len], attrs[i].name);
00374               value = getenv(buf);
00375 
00376               if(value == NULL) {
00377                      continue;
00378               }
00379 
00380               switch(attrs[i].type) {
00381               case ATTR_BOOL:
00382                      if((strcasecmp(value, "on") == 0) 
00383                             || (strcasecmp(value, "yes") == 0)
00384                             || (strcasecmp(value, "true") == 0))
00385                      {
00386                             LDAP_BOOL_SET(gopts, attrs[i].offset);
00387 
00388                      } else {
00389                             LDAP_BOOL_CLR(gopts, attrs[i].offset);
00390                      }
00391                      break;
00392 
00393               case ATTR_INT:
00394                      p = &((char *) gopts)[attrs[i].offset];
00395                      * (int*) p = atoi(value);
00396                      break;
00397 
00398               case ATTR_KV: {
00399                             const struct ol_keyvalue *kv;
00400 
00401                             for(kv = attrs[i].data;
00402                                    kv->key != NULL;
00403                                    kv++) {
00404 
00405                                    if(strcasecmp(value, kv->key) == 0) {
00406                                           p = &((char *) gopts)[attrs[i].offset];
00407                                           * (int*) p = kv->value;
00408                                           break;
00409                                    }
00410                             }
00411                      } break;
00412 
00413               case ATTR_STRING:
00414                      p = &((char *) gopts)[attrs[i].offset];
00415                      if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
00416                      if (*value == '\0') {
00417                             * (char**) p = NULL;
00418                      } else {
00419                             * (char**) p = LDAP_STRDUP(value);
00420                      }
00421                      break;
00422               case ATTR_OPTION:
00423                      ldap_set_option( NULL, attrs[i].offset, value );
00424                      break;
00425               case ATTR_SASL:
00426 #ifdef HAVE_CYRUS_SASL
00427                      ldap_int_sasl_config( gopts, attrs[i].offset, value );
00428 #endif                      
00429                      break;
00430               case ATTR_GSSAPI:
00431 #ifdef HAVE_GSSAPI
00432                      ldap_int_gssapi_config( gopts, attrs[i].offset, value );
00433 #endif
00434                      break;
00435               case ATTR_TLS:
00436 #ifdef HAVE_TLS
00437                      ldap_int_tls_config( NULL, attrs[i].offset, value );
00438 #endif                      
00439                      break;
00440               case ATTR_OPT_TV: {
00441                      struct timeval tv;
00442                      char *next;
00443                      tv.tv_usec = 0;
00444                      tv.tv_sec = strtol( value, &next, 10 );
00445                      if ( next != value && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
00446                             (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
00447                      }
00448                      } break;
00449               case ATTR_OPT_INT: {
00450                      long l;
00451                      char *next;
00452                      l = strtol( value, &next, 10 );
00453                      if ( next != value && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
00454                             int v = (int)l;
00455                             (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
00456                      }
00457                      } break;
00458               }
00459        }
00460 }
00461 
00462 #if defined(__GNUC__)
00463 /* Declare this function as a destructor so that it will automatically be
00464  * invoked either at program exit (if libldap is a static library) or
00465  * at unload time (if libldap is a dynamic library).
00466  *
00467  * Sorry, don't know how to handle this for non-GCC environments.
00468  */
00469 static void ldap_int_destroy_global_options(void)
00470        __attribute__ ((destructor));
00471 #endif
00472 
00473 static void
00474 ldap_int_destroy_global_options(void)
00475 {
00476        struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
00477 
00478        if ( gopts == NULL )
00479               return;
00480 
00481        gopts->ldo_valid = LDAP_UNINITIALIZED;
00482 
00483        if ( gopts->ldo_defludp ) {
00484               ldap_free_urllist( gopts->ldo_defludp );
00485               gopts->ldo_defludp = NULL;
00486        }
00487 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
00488        WSACleanup( );
00489 #endif
00490 
00491 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
00492        if ( ldap_int_hostname ) {
00493               LDAP_FREE( ldap_int_hostname );
00494               ldap_int_hostname = NULL;
00495        }
00496 #endif
00497 #ifdef HAVE_CYRUS_SASL
00498        if ( gopts->ldo_def_sasl_authcid ) {
00499               LDAP_FREE( gopts->ldo_def_sasl_authcid );
00500               gopts->ldo_def_sasl_authcid = NULL;
00501        }
00502 #endif
00503 #ifdef HAVE_TLS
00504        ldap_int_tls_destroy( gopts );
00505 #endif
00506 }
00507 
00508 /* 
00509  * Initialize the global options structure with default values.
00510  */
00511 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
00512 {
00513 #ifdef LDAP_R_COMPILE
00514        LDAP_PVT_MUTEX_FIRSTCREATE(gopts->ldo_mutex);
00515 #endif
00516        LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
00517        if (gopts->ldo_valid == LDAP_INITIALIZED) {
00518               /* someone else got here first */
00519               LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
00520               return;
00521        }
00522        if (dbglvl)
00523            gopts->ldo_debug = *dbglvl;
00524        else
00525               gopts->ldo_debug = 0;
00526 
00527        gopts->ldo_version   = LDAP_VERSION2;
00528        gopts->ldo_deref     = LDAP_DEREF_NEVER;
00529        gopts->ldo_timelimit = LDAP_NO_LIMIT;
00530        gopts->ldo_sizelimit = LDAP_NO_LIMIT;
00531 
00532        gopts->ldo_tm_api.tv_sec = -1;
00533        gopts->ldo_tm_net.tv_sec = -1;
00534 
00535        /* ldo_defludp will be freed by the termination handler
00536         */
00537        ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
00538        gopts->ldo_defport = LDAP_PORT;
00539 #if !defined(__GNUC__) && !defined(PIC)
00540        /* Do this only for a static library, and only if we can't
00541         * arrange for it to be executed as a library destructor
00542         */
00543        atexit(ldap_int_destroy_global_options);
00544 #endif
00545 
00546        gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
00547        gopts->ldo_rebind_proc = NULL;
00548        gopts->ldo_rebind_params = NULL;
00549 
00550        LDAP_BOOL_ZERO(gopts);
00551 
00552        LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
00553 
00554 #ifdef LDAP_CONNECTIONLESS
00555        gopts->ldo_peer = NULL;
00556        gopts->ldo_cldapdn = NULL;
00557        gopts->ldo_is_udp = 0;
00558 #endif
00559 
00560 #ifdef HAVE_CYRUS_SASL
00561        gopts->ldo_def_sasl_mech = NULL;
00562        gopts->ldo_def_sasl_realm = NULL;
00563        gopts->ldo_def_sasl_authcid = NULL;
00564        gopts->ldo_def_sasl_authzid = NULL;
00565 
00566        memset( &gopts->ldo_sasl_secprops,
00567               '\0', sizeof(gopts->ldo_sasl_secprops) );
00568 
00569        gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
00570        gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
00571        gopts->ldo_sasl_secprops.security_flags =
00572               SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
00573 #endif
00574 
00575 #ifdef HAVE_TLS
00576        gopts->ldo_tls_connect_cb = NULL;
00577        gopts->ldo_tls_connect_arg = NULL;
00578        gopts->ldo_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
00579 #endif
00580        gopts->ldo_keepalive_probes = 0;
00581        gopts->ldo_keepalive_interval = 0;
00582        gopts->ldo_keepalive_idle = 0;
00583 
00584        gopts->ldo_valid = LDAP_INITIALIZED;
00585        LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
00586        return;
00587 }
00588 
00589 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
00590 char * ldap_int_hostname = NULL;
00591 #endif
00592 
00593 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
00594 {
00595        if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
00596               return;
00597        }
00598 
00599        ldap_int_error_init();
00600 
00601        ldap_int_utils_init();
00602 
00603 #ifdef HAVE_WINSOCK2
00604 {      WORD wVersionRequested;
00605        WSADATA wsaData;
00606  
00607        wVersionRequested = MAKEWORD( 2, 0 );
00608        if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
00609               /* Tell the user that we couldn't find a usable */
00610               /* WinSock DLL.                                  */
00611               return;
00612        }
00613  
00614        /* Confirm that the WinSock DLL supports 2.0.*/
00615        /* Note that if the DLL supports versions greater    */
00616        /* than 2.0 in addition to 2.0, it will still return */
00617        /* 2.0 in wVersion since that is the version we      */
00618        /* requested.                                        */
00619  
00620        if ( LOBYTE( wsaData.wVersion ) != 2 ||
00621               HIBYTE( wsaData.wVersion ) != 0 )
00622        {
00623            /* Tell the user that we couldn't find a usable */
00624            /* WinSock DLL.                                  */
00625            WSACleanup( );
00626            return; 
00627        }
00628 }      /* The WinSock DLL is acceptable. Proceed. */
00629 #elif defined(HAVE_WINSOCK)
00630 {      WSADATA wsaData;
00631        if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
00632            return;
00633        }
00634 }
00635 #endif
00636 
00637 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
00638        {
00639               char   *name = ldap_int_hostname;
00640 
00641               ldap_int_hostname = ldap_pvt_get_fqdn( name );
00642 
00643               if ( name != NULL && name != ldap_int_hostname ) {
00644                      LDAP_FREE( name );
00645               }
00646        }
00647 #endif
00648 
00649 #ifndef HAVE_POLL
00650        if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
00651 #endif
00652 
00653        ldap_int_initialize_global_options(gopts, NULL);
00654 
00655        if( getenv("LDAPNOINIT") != NULL ) {
00656               return;
00657        }
00658 
00659 #ifdef HAVE_CYRUS_SASL
00660        {
00661               /* set authentication identity to current user name */
00662               char *user = getenv("USER");
00663 
00664               if( user == NULL ) user = getenv("USERNAME");
00665               if( user == NULL ) user = getenv("LOGNAME");
00666 
00667               if( user != NULL ) {
00668                      gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
00669               }
00670     }
00671 #endif
00672 
00673        openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
00674 
00675 #ifdef HAVE_GETEUID
00676        if ( geteuid() != getuid() )
00677               return;
00678 #endif
00679 
00680        openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
00681 
00682        {
00683               char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
00684 
00685               if( altfile != NULL ) {
00686                      Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
00687                            LDAP_ENV_PREFIX "CONF", altfile, 0);
00688                      openldap_ldap_init_w_sysconf( altfile );
00689               }
00690               else
00691                      Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
00692                            LDAP_ENV_PREFIX "CONF", 0, 0);
00693        }
00694 
00695        {
00696               char *altfile = getenv(LDAP_ENV_PREFIX "RC");
00697 
00698               if( altfile != NULL ) {
00699                      Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
00700                            LDAP_ENV_PREFIX "RC", altfile, 0);
00701                      openldap_ldap_init_w_userconf( altfile );
00702               }
00703               else
00704                      Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
00705                            LDAP_ENV_PREFIX "RC", 0, 0);
00706        }
00707 
00708        openldap_ldap_init_w_env(gopts, NULL);
00709 }