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