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