init.c 9.48 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
5
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
6
7
8
#include "portable.h"

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
9
#include <ac/stdlib.h>
10
11
12

#include <ac/socket.h>
#include <ac/string.h>
13
#include <ac/ctype.h>
14
15
#include <ac/time.h>

16
17
#include <limits.h>

18
#include "ldap-int.h"
19
#include "ldap_defaults.h"
20

21
22
struct ldapoptions ldap_int_global_options =
	{ LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
23
24

#undef gopts
25
#define gopts ldap_int_global_options
26

27
28
29
30
31
#define ATTR_NONE	0
#define ATTR_BOOL	1
#define ATTR_INT	2
#define ATTR_KV		3
#define ATTR_STRING	4
32
#define ATTR_TLS	5
33
#define ATTR_URIS	6
34
35

struct ol_keyvalue {
36
	const char *		key;
37
38
39
	int			value;
};

40
static const struct ol_keyvalue deref_kv[] = {
41
42
43
44
45
46
47
	{"never", LDAP_DEREF_NEVER},
	{"searching", LDAP_DEREF_SEARCHING},
	{"finding", LDAP_DEREF_FINDING},
	{"always", LDAP_DEREF_ALWAYS},
	{NULL, 0}
};

48
static const struct ol_attribute {
49
	int			useronly;
50
	int			type;
51
52
	const char *	name;
	const void *	data;
53
54
	size_t		offset;
} attrs[] = {
55
	{0, ATTR_KV,		"DEREF",	deref_kv, /* or &deref_kv[0] */
56
		offsetof(struct ldapoptions, ldo_deref)},
57
	{0, ATTR_INT,		"SIZELIMIT",	NULL,
58
		offsetof(struct ldapoptions, ldo_sizelimit)},
59
	{0, ATTR_INT,		"TIMELIMIT",	NULL,
60
		offsetof(struct ldapoptions, ldo_timelimit)},
61
62
	{1, ATTR_STRING,	"BINDDN",		NULL,
		offsetof(struct ldapoptions, ldo_defbinddn)},
63
	{0, ATTR_STRING,	"BASE",			NULL,
64
		offsetof(struct ldapoptions, ldo_defbase)},
65
	{0, ATTR_INT,		"PORT",			NULL,
66
		offsetof(struct ldapoptions, ldo_defport)},
67
	/* **** keep this around for backward compatibility */
68
	{0, ATTR_URIS,		"HOST",			NULL,	1},
69
	/* **** */
70
71
72
73
74
75
76
77
78
79
	{0, ATTR_URIS,		"URI",			NULL,	0},
	{0, ATTR_BOOL,		"REFERRALS",	NULL,	LDAP_BOOL_REFERRALS},
	{0, ATTR_BOOL,		"RESTART",		NULL,	LDAP_BOOL_RESTART},
	{0, ATTR_BOOL,		"DNS",			NULL,	LDAP_BOOL_DNS},
  	{0, ATTR_TLS,		"TLS",			NULL,	LDAP_OPT_X_TLS},
	{0, ATTR_TLS,		"TLS_CERT",		NULL,	LDAP_OPT_X_TLS_CERTFILE},
	{0, ATTR_TLS,		"TLS_KEY",		NULL,	LDAP_OPT_X_TLS_KEYFILE},
  	{0, ATTR_TLS,		"TLS_CACERT",	NULL,	LDAP_OPT_X_TLS_CACERTFILE},
  	{0, ATTR_TLS,		"TLS_CACERTDIR",NULL,	LDAP_OPT_X_TLS_CACERTDIR},
  	{0, ATTR_TLS,		"TLS_REQCERT",	NULL,	LDAP_OPT_X_TLS_REQUIRE_CERT},
80
81
82
83
84
85
#ifdef HAVE_CYRUS_SASL
	{0, ATTR_INT,		"SASL_MINSSF",	NULL,
		offsetof(struct ldapoptions, ldo_sasl_minssf)},
	{0, ATTR_INT,		"SASL_MAXSSF",	NULL,
		offsetof(struct ldapoptions, ldo_sasl_maxssf)},
#endif
86
	{0, ATTR_NONE,		NULL,		NULL,	0}
87
88
};

89
#define MAX_LDAP_ATTR_LEN  sizeof("TLS_CACERTDIR")
90
91
#define MAX_LDAP_ENV_PREFIX_LEN 8

92
93
static void openldap_ldap_init_w_conf(
	const char *file, int userconf )
94
95
96
97
98
99
100
{
	char linebuf[128];
	FILE *fp;
	int i;
	char *cmd, *opt;
	char *start, *end;

101
102
103
104
105
	if (file == NULL) {
		/* no file name */
		return;
	}

106
107
	Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);

108
109
110
111
112
113
	fp = fopen(file, "r");
	if(fp == NULL) {
		/* could not open file */
		return;
	}

114
115
	Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);

116
117
118
119
120
	while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
		/* skip lines starting with '#' */
		if(*start == '#') continue;

		/* trim leading white space */
121
122
		while((*start != '\0') && isspace((unsigned char) *start))
			start++;
123
124
125
126
127
128

		/* anything left? */
		if(*start == '\0') continue;

		/* trim trailing white space */
		end = &start[strlen(start)-1];
129
		while(isspace((unsigned char)*end)) end--;
130
131
132
133
134
135
136
137
		end[1] = '\0';

		/* anything left? */
		if(*start == '\0') continue;
		

		/* parse the command */
		cmd=start;
138
		while((*start != '\0') && !isspace((unsigned char)*start)) {
139
140
141
142
143
144
145
146
147
			start++;
		}
		if(*start == '\0') {
			/* command has no argument */
			continue;
		} 

		*start++ = '\0';

148
		/* we must have some whitespace to skip */
149
		while(isspace((unsigned char)*start)) start++;
150
151
152
153
154
		opt = start;

		for(i=0; attrs[i].type != ATTR_NONE; i++) {
			void *p;

155
			if( !userconf && attrs[i].useronly ) {
156
157
158
				continue;
			}

159
160
161
162
163
164
165
166
167
168
			if(strcasecmp(cmd, attrs[i].name) != 0) {
				continue;
			}

			switch(attrs[i].type) {
			case ATTR_BOOL:
				if((strcasecmp(opt, "on") == 0) 
					|| (strcasecmp(opt, "yes") == 0)
					|| (strcasecmp(opt, "true") == 0))
				{
169
					LDAP_BOOL_SET(&gopts, attrs[i].offset);
170
171

				} else {
172
					LDAP_BOOL_CLR(&gopts, attrs[i].offset);
173
174
175
176
177
				}

				break;

			case ATTR_INT:
178
				p = &((char *) &gopts)[attrs[i].offset];
179
180
181
182
				* (int*) p = atoi(opt);
				break;

			case ATTR_KV: {
183
					const struct ol_keyvalue *kv;
184

185
					for(kv = attrs[i].data;
186
187
188
189
						kv->key != NULL;
						kv++) {

						if(strcasecmp(opt, kv->key) == 0) {
190
							p = &((char *) &gopts)[attrs[i].offset];
191
192
193
194
195
196
197
							* (int*) p = kv->value;
							break;
						}
					}
				} break;

			case ATTR_STRING:
198
				p = &((char *) &gopts)[attrs[i].offset];
199
				if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
200
				* (char**) p = LDAP_STRDUP(opt);
201
				break;
202
203
204
205
206
			case ATTR_TLS:
#ifdef HAVE_TLS
			   	ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
#endif
				break;
207
			case ATTR_URIS:
208
209
210
211
212
213
				if (attrs[i].offset == 0) {
					ldap_set_option( NULL, LDAP_OPT_URI, opt );
				} else {
					ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
				}
				break;
214
			}
215
			break;
216
217
		}
	}
218
219

	fclose(fp);
220
221
}

222
223
224
225
226
static void openldap_ldap_init_w_sysconf(const char *file)
{
	openldap_ldap_init_w_conf( file, 0 );
}

227
228
static void openldap_ldap_init_w_userconf(const char *file)
{
229
	char *home;
230
	char *path = NULL;
231
232
233
234
235
236
237
238

	if (file == NULL) {
		/* no file name */
		return;
	}

	home = getenv("HOME");

Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
	if (home != NULL) {
240
241
		Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
		      home, 0, 0);
242
		path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
243
	} else {
244
245
		Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
		      0, 0, 0);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
246
247
248
	}

	if(home != NULL && path != NULL) {
249
250
		/* we assume UNIX path syntax is used... */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
251
252
		/* try ~/file */
		sprintf(path, "%s/%s", home, file);
253
		openldap_ldap_init_w_conf(path, 1);
254

Kurt Zeilenga's avatar
Kurt Zeilenga committed
255
256
		/* try ~/.file */
		sprintf(path, "%s/.%s", home, file);
257
		openldap_ldap_init_w_conf(path, 1);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
258
	}
259
260

	if(path != NULL) {
261
		LDAP_FREE(path);
262
	}
263
264

	/* try file */
265
	openldap_ldap_init_w_conf(file, 1);
266
267
268
269
270
271
272
273
274
275
276
}

static void openldap_ldap_init_w_env(const char *prefix)
{
	char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
	int len;
	int i;
	void *p;
	char *value;

	if (prefix == NULL) {
277
		prefix = LDAP_ENV_PREFIX;
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
	}

	strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
	buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
	len = strlen(buf);

	for(i=0; attrs[i].type != ATTR_NONE; i++) {
		strcpy(&buf[len], attrs[i].name);
		value = getenv(buf);

		if(value == NULL) {
			continue;
		}

		switch(attrs[i].type) {
		case ATTR_BOOL:
			if((strcasecmp(value, "on") == 0) 
				|| (strcasecmp(value, "yes") == 0)
				|| (strcasecmp(value, "true") == 0))
			{
298
				LDAP_BOOL_SET(&gopts, attrs[i].offset);
299
300

			} else {
301
				LDAP_BOOL_CLR(&gopts, attrs[i].offset);
302
303
304
305
			}
			break;

		case ATTR_INT:
306
			p = &((char *) &gopts)[attrs[i].offset];
307
308
309
310
			* (int*) p = atoi(value);
			break;

		case ATTR_KV: {
311
				const struct ol_keyvalue *kv;
312

313
				for(kv = attrs[i].data;
314
315
316
317
					kv->key != NULL;
					kv++) {

					if(strcasecmp(value, kv->key) == 0) {
318
						p = &((char *) &gopts)[attrs[i].offset];
319
320
321
322
323
324
325
						* (int*) p = kv->value;
						break;
					}
				}
			} break;

		case ATTR_STRING:
326
			p = &((char *) &gopts)[attrs[i].offset];
327
			if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
328
329
330
			if (*value == '\0') {
				* (char**) p = NULL;
			} else {
331
				* (char**) p = LDAP_STRDUP(value);
332
333
			}
			break;
334
335
		case ATTR_TLS:
#ifdef HAVE_TLS
336
		   	ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
337
338
#endif			 	
		   	break;
339
		case ATTR_URIS:
340
341
342
343
344
345
			if (attrs[i].offset == 0) {
				ldap_set_option( NULL, LDAP_OPT_URI, value );
			} else {
				ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
			}
			break;
346
347
348
349
		}
	}
}

350
void ldap_int_initialize( int *dbglvl )
351
{
352
	if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
353
354
		return;
	}
355

356
	ldap_int_utils_init();
357
358
359
360
361

#ifdef HAVE_TLS
   	ldap_pvt_tls_init();
#endif

362
363
364
365
#ifdef HAVE_CYRUS_SASL
	ldap_pvt_sasl_init();
#endif

Hallvard Furuseth's avatar
Hallvard Furuseth committed
366
367
	if ( ldap_int_tblsize == 0 )
		ldap_int_ip_init();
368

369
370
371
	if (dbglvl)
	    gopts.ldo_debug = *dbglvl;
	else
372
373
	gopts.ldo_debug = 0;

374
375
376
377
378
	gopts.ldo_version =	LDAP_VERSION2;
	gopts.ldo_deref =	LDAP_DEREF_NEVER;
	gopts.ldo_timelimit = LDAP_NO_LIMIT;
	gopts.ldo_sizelimit = LDAP_NO_LIMIT;

379
380
	gopts.ldo_tm_api = (struct timeval *)NULL;
	gopts.ldo_tm_net = (struct timeval *)NULL;
381

382
383
384
	/* ldo_defludp is leaked, we should have an at_exit() handler
	 * to free this and whatever else needs to cleaned up. 
	 */
385
	ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
386
387
	gopts.ldo_defport = LDAP_PORT;

388
389
	gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;

390
391
392
393
	LDAP_BOOL_ZERO(&gopts);

	LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);

394
395
396
#ifdef HAVE_TLS
   	gopts.ldo_tls_ctx = NULL;
#endif
397
398
399
400
#ifdef HAVE_CYRUS_SASL
	gopts.ldo_sasl_minssf = 0;
	gopts.ldo_sasl_maxssf = INT_MAX;
#endif
401

402
	gopts.ldo_valid = LDAP_INITIALIZED;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
403
404
405
406
407

	if( getenv("LDAPNOINIT") != NULL ) {
		return;
	}

408
	openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
409
	openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
410
411

	{
412
		char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
413
414

		if( altfile != NULL ) {
415
416
			Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
			      LDAP_ENV_PREFIX "CONF", altfile, 0);
417
			openldap_ldap_init_w_sysconf( altfile );
418
		}
419
420
421
		else
			Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
			      LDAP_ENV_PREFIX "CONF", 0, 0);
422
423
	}

424
	{
425
		char *altfile = getenv(LDAP_ENV_PREFIX "RC");
426
427

		if( altfile != NULL ) {
428
429
			Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
			      LDAP_ENV_PREFIX "RC", altfile, 0);
430
431
			openldap_ldap_init_w_userconf( altfile );
		}
432
433
434
		else
			Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
			      LDAP_ENV_PREFIX "RC", 0, 0);
435
436
	}

437
	openldap_ldap_init_w_env(NULL);
438
}