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