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