]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
Merge remote-tracking branch 'origin/mdb.RE/0.9' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / libldap / init.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2017 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <ac/stdlib.h>
20
21 #ifdef HAVE_GETEUID
22 #include <ac/unistd.h>
23 #endif
24
25 #include <ac/socket.h>
26 #include <ac/string.h>
27 #include <ac/ctype.h>
28 #include <ac/time.h>
29
30 #ifdef HAVE_LIMITS_H
31 #include <limits.h>
32 #endif
33
34 #include "ldap-int.h"
35 #include "ldap_defaults.h"
36 #include "lutil.h"
37
38 struct ldapoptions ldap_int_global_options =
39         { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE
40                 LDAP_LDO_NULLARG
41                 LDAP_LDO_CONNECTIONLESS_NULLARG
42                 LDAP_LDO_TLS_NULLARG
43                 LDAP_LDO_SASL_NULLARG
44                 LDAP_LDO_GSSAPI_NULLARG
45                 LDAP_LDO_MUTEX_NULLARG };
46
47 #define ATTR_NONE       0
48 #define ATTR_BOOL       1
49 #define ATTR_INT        2
50 #define ATTR_KV         3
51 #define ATTR_STRING     4
52 #define ATTR_OPTION     5
53
54 #define ATTR_SASL       6
55 #define ATTR_TLS        7
56
57 #define ATTR_OPT_TV     8
58 #define ATTR_OPT_INT    9
59
60 #define ATTR_GSSAPI     10
61
62 struct ol_keyvalue {
63         const char *            key;
64         int                     value;
65 };
66
67 static const struct ol_keyvalue deref_kv[] = {
68         {"never", LDAP_DEREF_NEVER},
69         {"searching", LDAP_DEREF_SEARCHING},
70         {"finding", LDAP_DEREF_FINDING},
71         {"always", LDAP_DEREF_ALWAYS},
72         {NULL, 0}
73 };
74
75 static const struct ol_attribute {
76         int                     useronly;
77         int                     type;
78         const char *    name;
79         const void *    data;
80         size_t          offset;
81 } attrs[] = {
82         {0, ATTR_OPT_TV,        "TIMEOUT",              NULL,   LDAP_OPT_TIMEOUT},
83         {0, ATTR_OPT_TV,        "NETWORK_TIMEOUT",      NULL,   LDAP_OPT_NETWORK_TIMEOUT},
84         {0, ATTR_OPT_INT,       "VERSION",              NULL,   LDAP_OPT_PROTOCOL_VERSION},
85         {0, ATTR_KV,            "DEREF",        deref_kv, /* or &deref_kv[0] */
86                 offsetof(struct ldapoptions, ldo_deref)},
87         {0, ATTR_INT,           "SIZELIMIT",    NULL,
88                 offsetof(struct ldapoptions, ldo_sizelimit)},
89         {0, ATTR_INT,           "TIMELIMIT",    NULL,
90                 offsetof(struct ldapoptions, ldo_timelimit)},
91         {1, ATTR_STRING,        "BINDDN",               NULL,
92                 offsetof(struct ldapoptions, ldo_defbinddn)},
93         {0, ATTR_STRING,        "BASE",                 NULL,
94                 offsetof(struct ldapoptions, ldo_defbase)},
95         {0, ATTR_INT,           "PORT",                 NULL,           /* deprecated */
96                 offsetof(struct ldapoptions, ldo_defport)},
97         {0, ATTR_OPTION,        "HOST",                 NULL,   LDAP_OPT_HOST_NAME}, /* deprecated */
98         {0, ATTR_OPTION,        "URI",                  NULL,   LDAP_OPT_URI}, /* replaces HOST/PORT */
99         {0, ATTR_BOOL,          "REFERRALS",    NULL,   LDAP_BOOL_REFERRALS},
100 #if 0
101         /* This should only be allowed via ldap_set_option(3) */
102         {0, ATTR_BOOL,          "RESTART",              NULL,   LDAP_BOOL_RESTART},
103 #endif
104
105 #ifdef HAVE_CYRUS_SASL
106         {0, ATTR_STRING,        "SASL_MECH",            NULL,
107                 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
108         {0, ATTR_STRING,        "SASL_REALM",           NULL,
109                 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
110         {1, ATTR_STRING,        "SASL_AUTHCID",         NULL,
111                 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
112         {1, ATTR_STRING,        "SASL_AUTHZID",         NULL,
113                 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
114         {0, ATTR_SASL,          "SASL_SECPROPS",        NULL,   LDAP_OPT_X_SASL_SECPROPS},
115         {0, ATTR_BOOL,          "SASL_NOCANON", NULL,   LDAP_BOOL_SASL_NOCANON},
116 #endif
117
118 #ifdef HAVE_GSSAPI
119         {0, ATTR_GSSAPI,"GSSAPI_SIGN",                  NULL,   LDAP_OPT_SIGN},
120         {0, ATTR_GSSAPI,"GSSAPI_ENCRYPT",               NULL,   LDAP_OPT_ENCRYPT},
121         {0, ATTR_GSSAPI,"GSSAPI_ALLOW_REMOTE_PRINCIPAL",NULL,   LDAP_OPT_X_GSSAPI_ALLOW_REMOTE_PRINCIPAL},
122 #endif
123
124 #ifdef HAVE_TLS
125         {1, ATTR_TLS,   "TLS_CERT",                     NULL,   LDAP_OPT_X_TLS_CERTFILE},
126         {1, ATTR_TLS,   "TLS_KEY",                      NULL,   LDAP_OPT_X_TLS_KEYFILE},
127         {0, ATTR_TLS,   "TLS_CACERT",           NULL,   LDAP_OPT_X_TLS_CACERTFILE},
128         {0, ATTR_TLS,   "TLS_CACERTDIR",        NULL,   LDAP_OPT_X_TLS_CACERTDIR},
129         {0, ATTR_TLS,   "TLS_REQCERT",          NULL,   LDAP_OPT_X_TLS_REQUIRE_CERT},
130         {0, ATTR_TLS,   "TLS_RANDFILE",         NULL,   LDAP_OPT_X_TLS_RANDOM_FILE},
131         {0, ATTR_TLS,   "TLS_CIPHER_SUITE",     NULL,   LDAP_OPT_X_TLS_CIPHER_SUITE},
132         {0, ATTR_TLS,   "TLS_PROTOCOL_MIN",     NULL,   LDAP_OPT_X_TLS_PROTOCOL_MIN},
133
134 #ifdef HAVE_OPENSSL_CRL
135         {0, ATTR_TLS,   "TLS_CRLCHECK",         NULL,   LDAP_OPT_X_TLS_CRLCHECK},
136 #endif
137 #ifdef HAVE_GNUTLS
138         {0, ATTR_TLS,   "TLS_CRLFILE",                  NULL,   LDAP_OPT_X_TLS_CRLFILE},
139 #endif
140         
141 #endif
142
143         {0, ATTR_NONE,          NULL,           NULL,   0}
144 };
145
146 #define MAX_LDAP_ATTR_LEN  sizeof("GSSAPI_ALLOW_REMOTE_PRINCIPAL")
147 #define MAX_LDAP_ENV_PREFIX_LEN 8
148
149 static void openldap_ldap_init_w_conf(
150         const char *file, int userconf )
151 {
152         char linebuf[ AC_LINE_MAX ];
153         FILE *fp;
154         int i;
155         char *cmd, *opt;
156         char *start, *end;
157         struct ldapoptions *gopts;
158
159         if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
160                 return;                 /* Could not allocate mem for global options */
161         }
162
163         if (file == NULL) {
164                 /* no file name */
165                 return;
166         }
167
168         Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
169
170         fp = fopen(file, "r");
171         if(fp == NULL) {
172                 /* could not open file */
173                 return;
174         }
175
176         Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
177
178         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
179                 /* skip lines starting with '#' */
180                 if(*start == '#') continue;
181
182                 /* trim leading white space */
183                 while((*start != '\0') && isspace((unsigned char) *start))
184                         start++;
185
186                 /* anything left? */
187                 if(*start == '\0') continue;
188
189                 /* trim trailing white space */
190                 end = &start[strlen(start)-1];
191                 while(isspace((unsigned char)*end)) end--;
192                 end[1] = '\0';
193
194                 /* anything left? */
195                 if(*start == '\0') continue;
196                 
197
198                 /* parse the command */
199                 cmd=start;
200                 while((*start != '\0') && !isspace((unsigned char)*start)) {
201                         start++;
202                 }
203                 if(*start == '\0') {
204                         /* command has no argument */
205                         continue;
206                 } 
207
208                 *start++ = '\0';
209
210                 /* we must have some whitespace to skip */
211                 while(isspace((unsigned char)*start)) start++;
212                 opt = start;
213
214                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
215                         void *p;
216
217                         if( !userconf && attrs[i].useronly ) {
218                                 continue;
219                         }
220
221                         if(strcasecmp(cmd, attrs[i].name) != 0) {
222                                 continue;
223                         }
224
225                         switch(attrs[i].type) {
226                         case ATTR_BOOL:
227                                 if((strcasecmp(opt, "on") == 0) 
228                                         || (strcasecmp(opt, "yes") == 0)
229                                         || (strcasecmp(opt, "true") == 0))
230                                 {
231                                         LDAP_BOOL_SET(gopts, attrs[i].offset);
232
233                                 } else {
234                                         LDAP_BOOL_CLR(gopts, attrs[i].offset);
235                                 }
236
237                                 break;
238
239                         case ATTR_INT: {
240                                 char *next;
241                                 long l;
242                                 p = &((char *) gopts)[attrs[i].offset];
243                                 l = strtol( opt, &next, 10 );
244                                 if ( next != opt && next[ 0 ] == '\0' ) {
245                                         * (int*) p = l;
246                                 }
247                                 } break;
248
249                         case ATTR_KV: {
250                                         const struct ol_keyvalue *kv;
251
252                                         for(kv = attrs[i].data;
253                                                 kv->key != NULL;
254                                                 kv++) {
255
256                                                 if(strcasecmp(opt, kv->key) == 0) {
257                                                         p = &((char *) gopts)[attrs[i].offset];
258                                                         * (int*) p = kv->value;
259                                                         break;
260                                                 }
261                                         }
262                                 } break;
263
264                         case ATTR_STRING:
265                                 p = &((char *) gopts)[attrs[i].offset];
266                                 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
267                                 * (char**) p = LDAP_STRDUP(opt);
268                                 break;
269                         case ATTR_OPTION:
270                                 ldap_set_option( NULL, attrs[i].offset, opt );
271                                 break;
272                         case ATTR_SASL:
273 #ifdef HAVE_CYRUS_SASL
274                                 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
275 #endif
276                                 break;
277                         case ATTR_GSSAPI:
278 #ifdef HAVE_GSSAPI
279                                 ldap_int_gssapi_config( gopts, attrs[i].offset, opt );
280 #endif
281                                 break;
282                         case ATTR_TLS:
283 #ifdef HAVE_TLS
284                                 ldap_int_tls_config( NULL, attrs[i].offset, opt );
285 #endif
286                                 break;
287                         case ATTR_OPT_TV: {
288                                 struct timeval tv;
289                                 char *next;
290                                 tv.tv_usec = 0;
291                                 tv.tv_sec = strtol( opt, &next, 10 );
292                                 if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
293                                         (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
294                                 }
295                                 } break;
296                         case ATTR_OPT_INT: {
297                                 long l;
298                                 char *next;
299                                 l = strtol( opt, &next, 10 );
300                                 if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
301                                         int v = (int)l;
302                                         (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
303                                 }
304                                 } break;
305                         }
306
307                         break;
308                 }
309         }
310
311         fclose(fp);
312 }
313
314 static void openldap_ldap_init_w_sysconf(const char *file)
315 {
316         openldap_ldap_init_w_conf( file, 0 );
317 }
318
319 static void openldap_ldap_init_w_userconf(const char *file)
320 {
321         char *home;
322         char *path = NULL;
323
324         if (file == NULL) {
325                 /* no file name */
326                 return;
327         }
328
329         home = getenv("HOME");
330
331         if (home != NULL) {
332                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
333                       home, 0, 0);
334                 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
335         } else {
336                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
337                       0, 0, 0);
338         }
339
340         if(home != NULL && path != NULL) {
341                 /* we assume UNIX path syntax is used... */
342
343                 /* try ~/file */
344                 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
345                 openldap_ldap_init_w_conf(path, 1);
346
347                 /* try ~/.file */
348                 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
349                 openldap_ldap_init_w_conf(path, 1);
350         }
351
352         if(path != NULL) {
353                 LDAP_FREE(path);
354         }
355
356         /* try file */
357         openldap_ldap_init_w_conf(file, 1);
358 }
359
360 static void openldap_ldap_init_w_env(
361                 struct ldapoptions *gopts,
362                 const char *prefix)
363 {
364         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
365         int len;
366         int i;
367         void *p;
368         char *value;
369
370         if (prefix == NULL) {
371                 prefix = LDAP_ENV_PREFIX;
372         }
373
374         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
375         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
376         len = strlen(buf);
377
378         for(i=0; attrs[i].type != ATTR_NONE; i++) {
379                 strcpy(&buf[len], attrs[i].name);
380                 value = getenv(buf);
381
382                 if(value == NULL) {
383                         continue;
384                 }
385
386                 switch(attrs[i].type) {
387                 case ATTR_BOOL:
388                         if((strcasecmp(value, "on") == 0) 
389                                 || (strcasecmp(value, "yes") == 0)
390                                 || (strcasecmp(value, "true") == 0))
391                         {
392                                 LDAP_BOOL_SET(gopts, attrs[i].offset);
393
394                         } else {
395                                 LDAP_BOOL_CLR(gopts, attrs[i].offset);
396                         }
397                         break;
398
399                 case ATTR_INT:
400                         p = &((char *) gopts)[attrs[i].offset];
401                         * (int*) p = atoi(value);
402                         break;
403
404                 case ATTR_KV: {
405                                 const struct ol_keyvalue *kv;
406
407                                 for(kv = attrs[i].data;
408                                         kv->key != NULL;
409                                         kv++) {
410
411                                         if(strcasecmp(value, kv->key) == 0) {
412                                                 p = &((char *) gopts)[attrs[i].offset];
413                                                 * (int*) p = kv->value;
414                                                 break;
415                                         }
416                                 }
417                         } break;
418
419                 case ATTR_STRING:
420                         p = &((char *) gopts)[attrs[i].offset];
421                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
422                         if (*value == '\0') {
423                                 * (char**) p = NULL;
424                         } else {
425                                 * (char**) p = LDAP_STRDUP(value);
426                         }
427                         break;
428                 case ATTR_OPTION:
429                         ldap_set_option( NULL, attrs[i].offset, value );
430                         break;
431                 case ATTR_SASL:
432 #ifdef HAVE_CYRUS_SASL
433                         ldap_int_sasl_config( gopts, attrs[i].offset, value );
434 #endif                          
435                         break;
436                 case ATTR_GSSAPI:
437 #ifdef HAVE_GSSAPI
438                         ldap_int_gssapi_config( gopts, attrs[i].offset, value );
439 #endif
440                         break;
441                 case ATTR_TLS:
442 #ifdef HAVE_TLS
443                         ldap_int_tls_config( NULL, attrs[i].offset, value );
444 #endif                          
445                         break;
446                 case ATTR_OPT_TV: {
447                         struct timeval tv;
448                         char *next;
449                         tv.tv_usec = 0;
450                         tv.tv_sec = strtol( value, &next, 10 );
451                         if ( next != value && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
452                                 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
453                         }
454                         } break;
455                 case ATTR_OPT_INT: {
456                         long l;
457                         char *next;
458                         l = strtol( value, &next, 10 );
459                         if ( next != value && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
460                                 int v = (int)l;
461                                 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
462                         }
463                         } break;
464                 }
465         }
466 }
467
468 #if defined(__GNUC__)
469 /* Declare this function as a destructor so that it will automatically be
470  * invoked either at program exit (if libldap is a static library) or
471  * at unload time (if libldap is a dynamic library).
472  *
473  * Sorry, don't know how to handle this for non-GCC environments.
474  */
475 static void ldap_int_destroy_global_options(void)
476         __attribute__ ((destructor));
477 #endif
478
479 static void
480 ldap_int_destroy_global_options(void)
481 {
482         struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
483
484         if ( gopts == NULL )
485                 return;
486
487         gopts->ldo_valid = LDAP_UNINITIALIZED;
488
489         if ( gopts->ldo_defludp ) {
490                 ldap_free_urllist( gopts->ldo_defludp );
491                 gopts->ldo_defludp = NULL;
492         }
493 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
494         WSACleanup( );
495 #endif
496
497 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
498         if ( ldap_int_hostname ) {
499                 LDAP_FREE( ldap_int_hostname );
500                 ldap_int_hostname = NULL;
501         }
502 #endif
503 #ifdef HAVE_CYRUS_SASL
504         if ( gopts->ldo_def_sasl_authcid ) {
505                 LDAP_FREE( gopts->ldo_def_sasl_authcid );
506                 gopts->ldo_def_sasl_authcid = NULL;
507         }
508 #endif
509 #ifdef HAVE_TLS
510         ldap_int_tls_destroy( gopts );
511 #endif
512 }
513
514 /* 
515  * Initialize the global options structure with default values.
516  */
517 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
518 {
519 #ifdef LDAP_R_COMPILE
520         LDAP_PVT_MUTEX_FIRSTCREATE(gopts->ldo_mutex);
521 #endif
522         LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
523         if (gopts->ldo_valid == LDAP_INITIALIZED) {
524                 /* someone else got here first */
525                 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
526                 return;
527         }
528         if (dbglvl)
529             gopts->ldo_debug = *dbglvl;
530         else
531                 gopts->ldo_debug = 0;
532
533         gopts->ldo_version   = LDAP_VERSION2;
534         gopts->ldo_deref     = LDAP_DEREF_NEVER;
535         gopts->ldo_timelimit = LDAP_NO_LIMIT;
536         gopts->ldo_sizelimit = LDAP_NO_LIMIT;
537
538         gopts->ldo_tm_api.tv_sec = -1;
539         gopts->ldo_tm_net.tv_sec = -1;
540
541         /* ldo_defludp will be freed by the termination handler
542          */
543         ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
544         gopts->ldo_defport = LDAP_PORT;
545 #if !defined(__GNUC__) && !defined(PIC)
546         /* Do this only for a static library, and only if we can't
547          * arrange for it to be executed as a library destructor
548          */
549         atexit(ldap_int_destroy_global_options);
550 #endif
551
552         gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
553         gopts->ldo_rebind_proc = NULL;
554         gopts->ldo_rebind_params = NULL;
555
556         LDAP_BOOL_ZERO(gopts);
557
558         LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
559
560 #ifdef LDAP_CONNECTIONLESS
561         gopts->ldo_peer = NULL;
562         gopts->ldo_cldapdn = NULL;
563         gopts->ldo_is_udp = 0;
564 #endif
565
566 #ifdef HAVE_CYRUS_SASL
567         gopts->ldo_def_sasl_mech = NULL;
568         gopts->ldo_def_sasl_realm = NULL;
569         gopts->ldo_def_sasl_authcid = NULL;
570         gopts->ldo_def_sasl_authzid = NULL;
571
572         memset( &gopts->ldo_sasl_secprops,
573                 '\0', sizeof(gopts->ldo_sasl_secprops) );
574
575         gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
576         gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
577         gopts->ldo_sasl_secprops.security_flags =
578                 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
579 #endif
580
581 #ifdef HAVE_TLS
582         gopts->ldo_tls_connect_cb = NULL;
583         gopts->ldo_tls_connect_arg = NULL;
584         gopts->ldo_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
585 #endif
586         gopts->ldo_keepalive_probes = 0;
587         gopts->ldo_keepalive_interval = 0;
588         gopts->ldo_keepalive_idle = 0;
589
590         gopts->ldo_valid = LDAP_INITIALIZED;
591         LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
592         return;
593 }
594
595 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
596 char * ldap_int_hostname = NULL;
597 #endif
598
599 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
600 {
601         if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
602                 return;
603         }
604
605         ldap_int_error_init();
606
607         ldap_int_utils_init();
608
609 #ifdef HAVE_WINSOCK2
610 {       WORD wVersionRequested;
611         WSADATA wsaData;
612  
613         wVersionRequested = MAKEWORD( 2, 0 );
614         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
615                 /* Tell the user that we couldn't find a usable */
616                 /* WinSock DLL.                                  */
617                 return;
618         }
619  
620         /* Confirm that the WinSock DLL supports 2.0.*/
621         /* Note that if the DLL supports versions greater    */
622         /* than 2.0 in addition to 2.0, it will still return */
623         /* 2.0 in wVersion since that is the version we      */
624         /* requested.                                        */
625  
626         if ( LOBYTE( wsaData.wVersion ) != 2 ||
627                 HIBYTE( wsaData.wVersion ) != 0 )
628         {
629             /* Tell the user that we couldn't find a usable */
630             /* WinSock DLL.                                  */
631             WSACleanup( );
632             return; 
633         }
634 }       /* The WinSock DLL is acceptable. Proceed. */
635 #elif defined(HAVE_WINSOCK)
636 {       WSADATA wsaData;
637         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
638             return;
639         }
640 }
641 #endif
642
643 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
644         LDAP_MUTEX_LOCK( &ldap_int_hostname_mutex );
645         {
646                 char    *name = ldap_int_hostname;
647
648                 ldap_int_hostname = ldap_pvt_get_fqdn( name );
649
650                 if ( name != NULL && name != ldap_int_hostname ) {
651                         LDAP_FREE( name );
652                 }
653         }
654         LDAP_MUTEX_UNLOCK( &ldap_int_hostname_mutex );
655 #endif
656
657 #ifndef HAVE_POLL
658         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
659 #endif
660
661 #ifdef HAVE_CYRUS_SASL
662         if ( ldap_int_sasl_init() != 0 ) {
663                 return;
664         }
665 #endif
666
667         ldap_int_initialize_global_options(gopts, dbglvl);
668
669         if( getenv("LDAPNOINIT") != NULL ) {
670                 return;
671         }
672
673 #ifdef HAVE_CYRUS_SASL
674         {
675                 /* set authentication identity to current user name */
676                 char *user = getenv("USER");
677
678                 if( user == NULL ) user = getenv("USERNAME");
679                 if( user == NULL ) user = getenv("LOGNAME");
680
681                 if( user != NULL ) {
682                         gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
683                 }
684     }
685 #endif
686
687         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
688
689 #ifdef HAVE_GETEUID
690         if ( geteuid() != getuid() )
691                 return;
692 #endif
693
694         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
695
696         {
697                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
698
699                 if( altfile != NULL ) {
700                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
701                               LDAP_ENV_PREFIX "CONF", altfile, 0);
702                         openldap_ldap_init_w_sysconf( altfile );
703                 }
704                 else
705                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
706                               LDAP_ENV_PREFIX "CONF", 0, 0);
707         }
708
709         {
710                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
711
712                 if( altfile != NULL ) {
713                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
714                               LDAP_ENV_PREFIX "RC", altfile, 0);
715                         openldap_ldap_init_w_userconf( altfile );
716                 }
717                 else
718                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
719                               LDAP_ENV_PREFIX "RC", 0, 0);
720         }
721
722         openldap_ldap_init_w_env(gopts, NULL);
723 }