init.c 9.92 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
25
26
27
28
#define ATTR_NONE	0
#define ATTR_BOOL	1
#define ATTR_INT	2
#define ATTR_KV		3
#define ATTR_STRING	4
29
#define ATTR_TLS	5
30
#define ATTR_URIS	6
31
32

struct ol_keyvalue {
33
	const char *		key;
34
35
36
	int			value;
};

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

45
static const struct ol_attribute {
46
	int			useronly;
47
	int			type;
48
49
	const char *	name;
	const void *	data;
50
51
	size_t		offset;
} attrs[] = {
52
	{0, ATTR_KV,		"DEREF",	deref_kv, /* or &deref_kv[0] */
53
		offsetof(struct ldapoptions, ldo_deref)},
54
	{0, ATTR_INT,		"SIZELIMIT",	NULL,
55
		offsetof(struct ldapoptions, ldo_sizelimit)},
56
	{0, ATTR_INT,		"TIMELIMIT",	NULL,
57
		offsetof(struct ldapoptions, ldo_timelimit)},
58
59
	{1, ATTR_STRING,	"BINDDN",		NULL,
		offsetof(struct ldapoptions, ldo_defbinddn)},
60
	{0, ATTR_STRING,	"BASE",			NULL,
61
		offsetof(struct ldapoptions, ldo_defbase)},
62
	{0, ATTR_INT,		"PORT",			NULL,		/* deprecated */
63
		offsetof(struct ldapoptions, ldo_defport)},
64
65
	{0, ATTR_URIS,		"HOST",			NULL,	1},	/* deprecated */
	{0, ATTR_URIS,		"URI",			NULL,	0}, /* replaces HOST/URI */
66
67
	{0, ATTR_BOOL,		"REFERRALS",	NULL,	LDAP_BOOL_REFERRALS},
	{0, ATTR_BOOL,		"RESTART",		NULL,	LDAP_BOOL_RESTART},
68
69

#ifdef HAVE_TLS
70
71
72
73
74
75
  	{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},
76
77
78
	{0, ATTR_TLS,		"TLS_RANDFILE",	NULL,	LDAP_OPT_X_TLS_RANDOM_FILE},
#endif

79
80
81
82
83
84
#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
85

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
{
	char linebuf[128];
	FILE *fp;
	int i;
	char *cmd, *opt;
	char *start, *end;
100
101
102
103
104
	struct ldapoptions *gopts;

	if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
		return;			/* Could not allocate mem for global options */
	}
105

106
107
108
109
110
	if (file == NULL) {
		/* no file name */
		return;
	}

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

113
114
115
116
117
118
	fp = fopen(file, "r");
	if(fp == NULL) {
		/* could not open file */
		return;
	}

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

121
122
123
124
125
	while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
		/* skip lines starting with '#' */
		if(*start == '#') continue;

		/* trim leading white space */
126
127
		while((*start != '\0') && isspace((unsigned char) *start))
			start++;
128
129
130
131
132
133

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

		/* trim trailing white space */
		end = &start[strlen(start)-1];
134
		while(isspace((unsigned char)*end)) end--;
135
136
137
138
139
140
141
142
		end[1] = '\0';

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

		/* parse the command */
		cmd=start;
143
		while((*start != '\0') && !isspace((unsigned char)*start)) {
144
145
146
147
148
149
150
151
152
			start++;
		}
		if(*start == '\0') {
			/* command has no argument */
			continue;
		} 

		*start++ = '\0';

153
		/* we must have some whitespace to skip */
154
		while(isspace((unsigned char)*start)) start++;
155
156
157
158
159
		opt = start;

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

160
			if( !userconf && attrs[i].useronly ) {
161
162
163
				continue;
			}

164
165
166
167
168
169
170
171
172
173
			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))
				{
174
					LDAP_BOOL_SET(gopts, attrs[i].offset);
175
176

				} else {
177
					LDAP_BOOL_CLR(gopts, attrs[i].offset);
178
179
180
181
182
				}

				break;

			case ATTR_INT:
183
				p = &((char *) gopts)[attrs[i].offset];
184
185
186
187
				* (int*) p = atoi(opt);
				break;

			case ATTR_KV: {
188
					const struct ol_keyvalue *kv;
189

190
					for(kv = attrs[i].data;
191
192
193
194
						kv->key != NULL;
						kv++) {

						if(strcasecmp(opt, kv->key) == 0) {
195
							p = &((char *) gopts)[attrs[i].offset];
196
197
198
199
200
201
202
							* (int*) p = kv->value;
							break;
						}
					}
				} break;

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

	fclose(fp);
225
226
}

227
228
229
230
231
static void openldap_ldap_init_w_sysconf(const char *file)
{
	openldap_ldap_init_w_conf( file, 0 );
}

232
233
static void openldap_ldap_init_w_userconf(const char *file)
{
234
	char *home;
235
	char *path = NULL;
236
237
238
239
240
241
242
243

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

	home = getenv("HOME");

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

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

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
260
261
		/* try ~/.file */
		sprintf(path, "%s/.%s", home, file);
262
		openldap_ldap_init_w_conf(path, 1);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
263
	}
264
265

	if(path != NULL) {
266
		LDAP_FREE(path);
267
	}
268
269

	/* try file */
270
	openldap_ldap_init_w_conf(file, 1);
271
272
}

273
274
275
static void openldap_ldap_init_w_env(
		struct ldapoptions *gopts,
		const char *prefix)
276
277
278
279
280
281
282
283
{
	char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
	int len;
	int i;
	void *p;
	char *value;

	if (prefix == NULL) {
284
		prefix = LDAP_ENV_PREFIX;
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	}

	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))
			{
305
				LDAP_BOOL_SET(gopts, attrs[i].offset);
306
307

			} else {
308
				LDAP_BOOL_CLR(gopts, attrs[i].offset);
309
310
311
312
			}
			break;

		case ATTR_INT:
313
			p = &((char *) gopts)[attrs[i].offset];
314
315
316
317
			* (int*) p = atoi(value);
			break;

		case ATTR_KV: {
318
				const struct ol_keyvalue *kv;
319

320
				for(kv = attrs[i].data;
321
322
323
324
					kv->key != NULL;
					kv++) {

					if(strcasecmp(value, kv->key) == 0) {
325
						p = &((char *) gopts)[attrs[i].offset];
326
327
328
329
330
331
332
						* (int*) p = kv->value;
						break;
					}
				}
			} break;

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

357
358
359
360
/* 
 * Initialize the global options structure with default values.
 */
void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
361
{
362
	if (dbglvl)
363
	    gopts->ldo_debug = *dbglvl;
364
	else
365
		gopts->ldo_debug = 0;
366

367
368
369
370
	gopts->ldo_version   = LDAP_VERSION2;
	gopts->ldo_deref     = LDAP_DEREF_NEVER;
	gopts->ldo_timelimit = LDAP_NO_LIMIT;
	gopts->ldo_sizelimit = LDAP_NO_LIMIT;
371

372
373
	gopts->ldo_tm_api = (struct timeval *)NULL;
	gopts->ldo_tm_net = (struct timeval *)NULL;
374

375
376
377
	/* ldo_defludp is leaked, we should have an at_exit() handler
	 * to free this and whatever else needs to cleaned up. 
	 */
378
379
	ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
	gopts->ldo_defport = LDAP_PORT;
380

381
382
	gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
	gopts->ldo_rebindproc = NULL;
383

384
	LDAP_BOOL_ZERO(gopts);
385

386
	LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
387

388
#ifdef HAVE_TLS
389
   	gopts->ldo_tls_ctx = NULL;
390
#endif
391
#ifdef HAVE_CYRUS_SASL
392
393
	gopts->ldo_sasl_minssf = 0;
	gopts->ldo_sasl_maxssf = INT_MAX;
394
#endif
395

396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
	gopts->ldo_valid = LDAP_INITIALIZED;

   	return;
}

void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
{
	if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
		return;
	}

	ldap_int_utils_init();

#ifdef HAVE_TLS
   	ldap_pvt_tls_init();
#endif

#ifdef HAVE_CYRUS_SASL
	ldap_pvt_sasl_init();
#endif

	if ( ldap_int_tblsize == 0 )
		ldap_int_ip_init();

	ldap_int_initialize_global_options(gopts, NULL);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
421
422
423
424
425

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

426
	openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
427
	openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
428
429

	{
430
		char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
431
432

		if( altfile != NULL ) {
433
434
			Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
			      LDAP_ENV_PREFIX "CONF", altfile, 0);
435
			openldap_ldap_init_w_sysconf( altfile );
436
		}
437
438
439
		else
			Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
			      LDAP_ENV_PREFIX "CONF", 0, 0);
440
441
	}

442
	{
443
		char *altfile = getenv(LDAP_ENV_PREFIX "RC");
444
445

		if( altfile != NULL ) {
446
447
			Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
			      LDAP_ENV_PREFIX "RC", altfile, 0);
448
449
			openldap_ldap_init_w_userconf( altfile );
		}
450
451
452
		else
			Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
			      LDAP_ENV_PREFIX "RC", 0, 0);
453
454
	}

455
	openldap_ldap_init_w_env(gopts, NULL);
456
}