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