3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
12 #include <ac/string.h>
19 #include "ldap_defaults.h"
21 struct ldapoptions ldap_int_global_options =
22 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };
37 static const struct ol_keyvalue deref_kv[] = {
38 {"never", LDAP_DEREF_NEVER},
39 {"searching", LDAP_DEREF_SEARCHING},
40 {"finding", LDAP_DEREF_FINDING},
41 {"always", LDAP_DEREF_ALWAYS},
45 static const struct ol_attribute {
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 {1, ATTR_STRING, "BINDDN", NULL,
59 offsetof(struct ldapoptions, ldo_defbinddn)},
60 {0, ATTR_STRING, "BASE", NULL,
61 offsetof(struct ldapoptions, ldo_defbase)},
62 {0, ATTR_INT, "PORT", NULL,
63 offsetof(struct ldapoptions, ldo_defport)},
64 /* **** keep this around for backward compatibility */
65 {0, ATTR_URIS, "HOST", NULL, 1},
67 {0, ATTR_URIS, "URI", NULL, 0},
68 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
69 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
70 {0, ATTR_TLS, "TLS", NULL, LDAP_OPT_X_TLS},
71 {0, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
72 {0, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
73 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
74 {0, ATTR_TLS, "TLS_CACERTDIR",NULL, LDAP_OPT_X_TLS_CACERTDIR},
75 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
76 #ifdef HAVE_CYRUS_SASL
77 {0, ATTR_INT, "SASL_MINSSF", NULL,
78 offsetof(struct ldapoptions, ldo_sasl_minssf)},
79 {0, ATTR_INT, "SASL_MAXSSF", NULL,
80 offsetof(struct ldapoptions, ldo_sasl_maxssf)},
82 {0, ATTR_NONE, NULL, NULL, 0}
85 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
86 #define MAX_LDAP_ENV_PREFIX_LEN 8
88 static void openldap_ldap_init_w_conf(
89 const char *file, int userconf )
96 struct ldapoptions *gopts;
98 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
99 return; /* Could not allocate mem for global options */
107 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
109 fp = fopen(file, "r");
111 /* could not open file */
115 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
117 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
118 /* skip lines starting with '#' */
119 if(*start == '#') continue;
121 /* trim leading white space */
122 while((*start != '\0') && isspace((unsigned char) *start))
126 if(*start == '\0') continue;
128 /* trim trailing white space */
129 end = &start[strlen(start)-1];
130 while(isspace((unsigned char)*end)) end--;
134 if(*start == '\0') continue;
137 /* parse the command */
139 while((*start != '\0') && !isspace((unsigned char)*start)) {
143 /* command has no argument */
149 /* we must have some whitespace to skip */
150 while(isspace((unsigned char)*start)) start++;
153 for(i=0; attrs[i].type != ATTR_NONE; i++) {
156 if( !userconf && attrs[i].useronly ) {
160 if(strcasecmp(cmd, attrs[i].name) != 0) {
164 switch(attrs[i].type) {
166 if((strcasecmp(opt, "on") == 0)
167 || (strcasecmp(opt, "yes") == 0)
168 || (strcasecmp(opt, "true") == 0))
170 LDAP_BOOL_SET(gopts, attrs[i].offset);
173 LDAP_BOOL_CLR(gopts, attrs[i].offset);
179 p = &((char *) gopts)[attrs[i].offset];
180 * (int*) p = atoi(opt);
184 const struct ol_keyvalue *kv;
186 for(kv = attrs[i].data;
190 if(strcasecmp(opt, kv->key) == 0) {
191 p = &((char *) gopts)[attrs[i].offset];
192 * (int*) p = kv->value;
199 p = &((char *) gopts)[attrs[i].offset];
200 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
201 * (char**) p = LDAP_STRDUP(opt);
205 ldap_pvt_tls_config( gopts, attrs[i].offset, opt );
209 if (attrs[i].offset == 0) {
210 ldap_set_option( NULL, LDAP_OPT_URI, opt );
212 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
223 static void openldap_ldap_init_w_sysconf(const char *file)
225 openldap_ldap_init_w_conf( file, 0 );
228 static void openldap_ldap_init_w_userconf(const char *file)
238 home = getenv("HOME");
241 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
243 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
245 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
249 if(home != NULL && path != NULL) {
250 /* we assume UNIX path syntax is used... */
253 sprintf(path, "%s/%s", home, file);
254 openldap_ldap_init_w_conf(path, 1);
257 sprintf(path, "%s/.%s", home, file);
258 openldap_ldap_init_w_conf(path, 1);
266 openldap_ldap_init_w_conf(file, 1);
269 static void openldap_ldap_init_w_env(
270 struct ldapoptions *gopts,
273 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
279 if (prefix == NULL) {
280 prefix = LDAP_ENV_PREFIX;
283 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
284 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
287 for(i=0; attrs[i].type != ATTR_NONE; i++) {
288 strcpy(&buf[len], attrs[i].name);
295 switch(attrs[i].type) {
297 if((strcasecmp(value, "on") == 0)
298 || (strcasecmp(value, "yes") == 0)
299 || (strcasecmp(value, "true") == 0))
301 LDAP_BOOL_SET(gopts, attrs[i].offset);
304 LDAP_BOOL_CLR(gopts, attrs[i].offset);
309 p = &((char *) gopts)[attrs[i].offset];
310 * (int*) p = atoi(value);
314 const struct ol_keyvalue *kv;
316 for(kv = attrs[i].data;
320 if(strcasecmp(value, kv->key) == 0) {
321 p = &((char *) gopts)[attrs[i].offset];
322 * (int*) p = kv->value;
329 p = &((char *) gopts)[attrs[i].offset];
330 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
331 if (*value == '\0') {
334 * (char**) p = LDAP_STRDUP(value);
339 ldap_pvt_tls_config( gopts, attrs[i].offset, value );
343 if (attrs[i].offset == 0) {
344 ldap_set_option( NULL, LDAP_OPT_URI, value );
346 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
354 * Initialize the global options structure with default values.
356 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
359 gopts->ldo_debug = *dbglvl;
361 gopts->ldo_debug = 0;
363 gopts->ldo_version = LDAP_VERSION2;
364 gopts->ldo_deref = LDAP_DEREF_NEVER;
365 gopts->ldo_timelimit = LDAP_NO_LIMIT;
366 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
368 gopts->ldo_tm_api = (struct timeval *)NULL;
369 gopts->ldo_tm_net = (struct timeval *)NULL;
371 /* ldo_defludp is leaked, we should have an at_exit() handler
372 * to free this and whatever else needs to cleaned up.
374 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
375 gopts->ldo_defport = LDAP_PORT;
377 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
378 gopts->ldo_rebindproc = NULL;
380 LDAP_BOOL_ZERO(gopts);
382 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
385 gopts->ldo_tls_ctx = NULL;
387 #ifdef HAVE_CYRUS_SASL
388 gopts->ldo_sasl_minssf = 0;
389 gopts->ldo_sasl_maxssf = INT_MAX;
392 gopts->ldo_valid = LDAP_INITIALIZED;
397 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
399 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
403 ldap_int_utils_init();
409 #ifdef HAVE_CYRUS_SASL
410 ldap_pvt_sasl_init();
413 if ( ldap_int_tblsize == 0 )
416 ldap_int_initialize_global_options(gopts, NULL);
418 if( getenv("LDAPNOINIT") != NULL ) {
422 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
423 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
426 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
428 if( altfile != NULL ) {
429 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
430 LDAP_ENV_PREFIX "CONF", altfile, 0);
431 openldap_ldap_init_w_sysconf( altfile );
434 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
435 LDAP_ENV_PREFIX "CONF", 0, 0);
439 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
441 if( altfile != NULL ) {
442 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
443 LDAP_ENV_PREFIX "RC", altfile, 0);
444 openldap_ldap_init_w_userconf( altfile );
447 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
448 LDAP_ENV_PREFIX "RC", 0, 0);
451 openldap_ldap_init_w_env(gopts, NULL);