init.c 8.46 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
4
5
/*
 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
 * 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
16
#include <ac/time.h>

#include "ldap-int.h"
17
#include "ldap_defaults.h"
18

19
20
struct ldapoptions ldap_int_global_options =
	{ LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
21
22

#undef gopts
23
#define gopts ldap_int_global_options
24

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

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

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

46
static const struct ol_attribute {
47
	int			useronly;
48
	int			type;
49
50
	const char *	name;
	const void *	data;
51
52
	size_t		offset;
} attrs[] = {
53
	{0, ATTR_KV,		"DEREF",	deref_kv, /* or &deref_kv[0] */
54
		offsetof(struct ldapoptions, ldo_deref)},
55
	{0, ATTR_INT,		"SIZELIMIT",	NULL,
56
		offsetof(struct ldapoptions, ldo_sizelimit)},
57
	{0, ATTR_INT,		"TIMELIMIT",	NULL,
58
		offsetof(struct ldapoptions, ldo_timelimit)},
59
60
	{1, ATTR_STRING,	"BINDDN",		NULL,
		offsetof(struct ldapoptions, ldo_defbinddn)},
61
	{0, ATTR_STRING,	"BASE",			NULL,
62
		offsetof(struct ldapoptions, ldo_defbase)},
63
	{0, ATTR_INT,		"PORT",			NULL,
64
		offsetof(struct ldapoptions, ldo_defport)},
65
	/* **** keep this around for backward compatibility */
66
	{0, ATTR_URIS,		"HOST",			NULL,	1},
67
	/* **** */
68
69
70
71
72
73
74
75
76
77
78
	{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},
	{0, ATTR_NONE,		NULL,		NULL,	0}
79
80
};

81
#define MAX_LDAP_ATTR_LEN  sizeof("TLS_CACERTDIR")
82
83
#define MAX_LDAP_ENV_PREFIX_LEN 8

84
85
static void openldap_ldap_init_w_conf(
	const char *file, int userconf )
86
87
88
89
90
91
92
{
	char linebuf[128];
	FILE *fp;
	int i;
	char *cmd, *opt;
	char *start, *end;

93
94
95
96
97
	if (file == NULL) {
		/* no file name */
		return;
	}

98
99
100
101
102
103
104
105
106
107
108
	fp = fopen(file, "r");
	if(fp == NULL) {
		/* could not open file */
		return;
	}

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

		/* trim leading white space */
109
110
		while((*start != '\0') && isspace((unsigned char) *start))
			start++;
111
112
113
114
115
116

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

		/* trim trailing white space */
		end = &start[strlen(start)-1];
117
		while(isspace((unsigned char)*end)) end--;
118
119
120
121
122
123
124
125
		end[1] = '\0';

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

		/* parse the command */
		cmd=start;
126
		while((*start != '\0') && !isspace((unsigned char)*start)) {
127
128
129
130
131
132
133
134
135
			start++;
		}
		if(*start == '\0') {
			/* command has no argument */
			continue;
		} 

		*start++ = '\0';

136
		/* we must have some whitespace to skip */
137
		while(isspace((unsigned char)*start)) start++;
138
139
140
141
142
		opt = start;

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

143
144
145
146
			if( !userconf && !attrs[i].useronly ) {
				continue;
			}

147
148
149
150
151
152
153
154
155
156
			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))
				{
157
					LDAP_BOOL_SET(&gopts, attrs[i].offset);
158
159

				} else {
160
					LDAP_BOOL_CLR(&gopts, attrs[i].offset);
161
162
163
164
165
				}

				break;

			case ATTR_INT:
166
				p = &((char *) &gopts)[attrs[i].offset];
167
168
169
170
				* (int*) p = atoi(opt);
				break;

			case ATTR_KV: {
171
					const struct ol_keyvalue *kv;
172

173
					for(kv = attrs[i].data;
174
175
176
177
						kv->key != NULL;
						kv++) {

						if(strcasecmp(opt, kv->key) == 0) {
178
							p = &((char *) &gopts)[attrs[i].offset];
179
180
181
182
183
184
185
							* (int*) p = kv->value;
							break;
						}
					}
				} break;

			case ATTR_STRING:
186
				p = &((char *) &gopts)[attrs[i].offset];
187
				if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
188
				* (char**) p = LDAP_STRDUP(opt);
189
				break;
190
191
192
193
194
			case ATTR_TLS:
#ifdef HAVE_TLS
			   	ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
#endif
				break;
195
			case ATTR_URIS:
196
197
198
199
200
201
				if (attrs[i].offset == 0) {
					ldap_set_option( NULL, LDAP_OPT_URI, opt );
				} else {
					ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
				}
				break;
202
203
204
			}
		}
	}
205
206

	fclose(fp);
207
208
}

209
210
211
212
213
static void openldap_ldap_init_w_sysconf(const char *file)
{
	openldap_ldap_init_w_conf( file, 0 );
}

214
215
static void openldap_ldap_init_w_userconf(const char *file)
{
216
	char *home;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
217
	char *path;
218
219
220
221
222
223
224
225

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

	home = getenv("HOME");

Kurt Zeilenga's avatar
Kurt Zeilenga committed
226
	if (home != NULL) {
227
		path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
228
	} else {
229
		path = LDAP_MALLOC(strlen(file) + 3);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
230
231
232
	}

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

Kurt Zeilenga's avatar
Kurt Zeilenga committed
235
236
		/* try ~/file */
		sprintf(path, "%s/%s", home, file);
237
		openldap_ldap_init_w_conf(path, 1);
238

Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
240
		/* try ~/.file */
		sprintf(path, "%s/.%s", home, file);
241
		openldap_ldap_init_w_conf(path, 1);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
242
	}
243
244

	if(path != NULL) {
245
		LDAP_FREE(path);
246
	}
247
248

	/* try file */
249
	openldap_ldap_init_w_conf(file, 1);
250
251
252
253
254
255
256
257
258
259
260
}

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) {
261
		prefix = LDAP_ENV_PREFIX;
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
	}

	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))
			{
282
				LDAP_BOOL_SET(&gopts, attrs[i].offset);
283
284

			} else {
285
				LDAP_BOOL_CLR(&gopts, attrs[i].offset);
286
287
288
289
			}
			break;

		case ATTR_INT:
290
			p = &((char *) &gopts)[attrs[i].offset];
291
292
293
294
			* (int*) p = atoi(value);
			break;

		case ATTR_KV: {
295
				const struct ol_keyvalue *kv;
296

297
				for(kv = attrs[i].data;
298
299
300
301
					kv->key != NULL;
					kv++) {

					if(strcasecmp(value, kv->key) == 0) {
302
						p = &((char *) &gopts)[attrs[i].offset];
303
304
305
306
307
308
309
						* (int*) p = kv->value;
						break;
					}
				}
			} break;

		case ATTR_STRING:
310
			p = &((char *) &gopts)[attrs[i].offset];
311
			if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
312
313
314
			if (*value == '\0') {
				* (char**) p = NULL;
			} else {
315
				* (char**) p = LDAP_STRDUP(value);
316
317
			}
			break;
318
319
		case ATTR_TLS:
#ifdef HAVE_TLS
320
		   	ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
321
322
#endif			 	
		   	break;
323
		case ATTR_URIS:
324
325
326
327
328
329
			if (attrs[i].offset == 0) {
				ldap_set_option( NULL, LDAP_OPT_URI, value );
			} else {
				ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
			}
			break;
330
331
332
333
		}
	}
}

334
void ldap_int_initialize( void )
335
{
336
	if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
337
338
		return;
	}
339

340
	ldap_int_utils_init();
341
342
343
344
345

#ifdef HAVE_TLS
   	ldap_pvt_tls_init();
#endif

346
347
348
349
#ifdef HAVE_CYRUS_SASL
	ldap_pvt_sasl_init();
#endif

Hallvard Furuseth's avatar
Hallvard Furuseth committed
350
351
	if ( ldap_int_tblsize == 0 )
		ldap_int_ip_init();
352

353
354
	gopts.ldo_debug = 0;

355
356
357
358
359
	gopts.ldo_version =	LDAP_VERSION2;
	gopts.ldo_deref =	LDAP_DEREF_NEVER;
	gopts.ldo_timelimit = LDAP_NO_LIMIT;
	gopts.ldo_sizelimit = LDAP_NO_LIMIT;

360
361
	gopts.ldo_tm_api = (struct timeval *)NULL;
	gopts.ldo_tm_net = (struct timeval *)NULL;
362

363
364
365
	/* ldo_defludp is leaked, we should have an at_exit() handler
	 * to free this and whatever else needs to cleaned up. 
	 */
366
	ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
367
368
	gopts.ldo_defport = LDAP_PORT;

369
370
	gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;

371
372
373
374
	LDAP_BOOL_ZERO(&gopts);

	LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);

375
376
377
378
#ifdef HAVE_TLS
   	gopts.ldo_tls_ctx = NULL;
#endif

379
	gopts.ldo_valid = LDAP_INITIALIZED;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
380
381
382
383
384

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

385
	openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
386
	openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
387
388

	{
389
		char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
390
391

		if( altfile != NULL ) {
392
			openldap_ldap_init_w_sysconf( altfile );
393
394
395
		}
	}

396
	{
397
		char *altfile = getenv(LDAP_ENV_PREFIX "RC");
398
399
400
401
402
403

		if( altfile != NULL ) {
			openldap_ldap_init_w_userconf( altfile );
		}
	}

404
	openldap_ldap_init_w_env(NULL);
405
}