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 };
25 #define gopts ldap_int_global_options
40 static const struct ol_keyvalue deref_kv[] = {
41 {"never", LDAP_DEREF_NEVER},
42 {"searching", LDAP_DEREF_SEARCHING},
43 {"finding", LDAP_DEREF_FINDING},
44 {"always", LDAP_DEREF_ALWAYS},
48 static const struct ol_attribute {
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 {1, ATTR_STRING, "BINDDN", NULL,
62 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},
70 {0, ATTR_URIS, "URI", NULL, 0},
71 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
72 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
73 {0, ATTR_BOOL, "DNS", NULL, LDAP_BOOL_DNS},
74 {0, ATTR_TLS, "TLS", NULL, LDAP_OPT_X_TLS},
75 {0, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
76 {0, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
77 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
78 {0, ATTR_TLS, "TLS_CACERTDIR",NULL, LDAP_OPT_X_TLS_CACERTDIR},
79 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
80 #ifdef HAVE_CYRUS_SASL
81 {0, ATTR_INT, "SASL_MINSSF", NULL,
82 offsetof(struct ldapoptions, ldo_sasl_minssf)},
83 {0, ATTR_INT, "SASL_MAXSSF", NULL,
84 offsetof(struct ldapoptions, ldo_sasl_maxssf)},
86 {0, ATTR_NONE, NULL, NULL, 0}
89 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
90 #define MAX_LDAP_ENV_PREFIX_LEN 8
92 static void openldap_ldap_init_w_conf(
93 const char *file, int userconf )
106 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
108 fp = fopen(file, "r");
110 /* could not open file */
114 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
116 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
117 /* skip lines starting with '#' */
118 if(*start == '#') continue;
120 /* trim leading white space */
121 while((*start != '\0') && isspace((unsigned char) *start))
125 if(*start == '\0') continue;
127 /* trim trailing white space */
128 end = &start[strlen(start)-1];
129 while(isspace((unsigned char)*end)) end--;
133 if(*start == '\0') continue;
136 /* parse the command */
138 while((*start != '\0') && !isspace((unsigned char)*start)) {
142 /* command has no argument */
148 /* we must have some whitespace to skip */
149 while(isspace((unsigned char)*start)) start++;
152 for(i=0; attrs[i].type != ATTR_NONE; i++) {
155 if( !userconf && attrs[i].useronly ) {
159 if(strcasecmp(cmd, attrs[i].name) != 0) {
163 switch(attrs[i].type) {
165 if((strcasecmp(opt, "on") == 0)
166 || (strcasecmp(opt, "yes") == 0)
167 || (strcasecmp(opt, "true") == 0))
169 LDAP_BOOL_SET(&gopts, attrs[i].offset);
172 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
178 p = &((char *) &gopts)[attrs[i].offset];
179 * (int*) p = atoi(opt);
183 const struct ol_keyvalue *kv;
185 for(kv = attrs[i].data;
189 if(strcasecmp(opt, kv->key) == 0) {
190 p = &((char *) &gopts)[attrs[i].offset];
191 * (int*) p = kv->value;
198 p = &((char *) &gopts)[attrs[i].offset];
199 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
200 * (char**) p = LDAP_STRDUP(opt);
204 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
208 if (attrs[i].offset == 0) {
209 ldap_set_option( NULL, LDAP_OPT_URI, opt );
211 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
222 static void openldap_ldap_init_w_sysconf(const char *file)
224 openldap_ldap_init_w_conf( file, 0 );
227 static void openldap_ldap_init_w_userconf(const char *file)
237 home = getenv("HOME");
240 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
242 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
244 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
248 if(home != NULL && path != NULL) {
249 /* we assume UNIX path syntax is used... */
252 sprintf(path, "%s/%s", home, file);
253 openldap_ldap_init_w_conf(path, 1);
256 sprintf(path, "%s/.%s", home, file);
257 openldap_ldap_init_w_conf(path, 1);
265 openldap_ldap_init_w_conf(file, 1);
268 static void openldap_ldap_init_w_env(const char *prefix)
270 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
276 if (prefix == NULL) {
277 prefix = LDAP_ENV_PREFIX;
280 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
281 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
284 for(i=0; attrs[i].type != ATTR_NONE; i++) {
285 strcpy(&buf[len], attrs[i].name);
292 switch(attrs[i].type) {
294 if((strcasecmp(value, "on") == 0)
295 || (strcasecmp(value, "yes") == 0)
296 || (strcasecmp(value, "true") == 0))
298 LDAP_BOOL_SET(&gopts, attrs[i].offset);
301 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
306 p = &((char *) &gopts)[attrs[i].offset];
307 * (int*) p = atoi(value);
311 const struct ol_keyvalue *kv;
313 for(kv = attrs[i].data;
317 if(strcasecmp(value, kv->key) == 0) {
318 p = &((char *) &gopts)[attrs[i].offset];
319 * (int*) p = kv->value;
326 p = &((char *) &gopts)[attrs[i].offset];
327 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
328 if (*value == '\0') {
331 * (char**) p = LDAP_STRDUP(value);
336 ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
340 if (attrs[i].offset == 0) {
341 ldap_set_option( NULL, LDAP_OPT_URI, value );
343 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
350 void ldap_int_initialize( int *dbglvl )
352 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
356 ldap_int_utils_init();
362 #ifdef HAVE_CYRUS_SASL
363 ldap_pvt_sasl_init();
366 if ( ldap_int_tblsize == 0 )
370 gopts.ldo_debug = *dbglvl;
374 gopts.ldo_version = LDAP_VERSION2;
375 gopts.ldo_deref = LDAP_DEREF_NEVER;
376 gopts.ldo_timelimit = LDAP_NO_LIMIT;
377 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
379 gopts.ldo_tm_api = (struct timeval *)NULL;
380 gopts.ldo_tm_net = (struct timeval *)NULL;
382 /* ldo_defludp is leaked, we should have an at_exit() handler
383 * to free this and whatever else needs to cleaned up.
385 ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
386 gopts.ldo_defport = LDAP_PORT;
388 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
389 gopts.ldo_rebindproc = NULL;
391 LDAP_BOOL_ZERO(&gopts);
393 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
396 gopts.ldo_tls_ctx = NULL;
398 #ifdef HAVE_CYRUS_SASL
399 gopts.ldo_sasl_minssf = 0;
400 gopts.ldo_sasl_maxssf = INT_MAX;
403 gopts.ldo_valid = LDAP_INITIALIZED;
405 if( getenv("LDAPNOINIT") != NULL ) {
409 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
410 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
413 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
415 if( altfile != NULL ) {
416 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
417 LDAP_ENV_PREFIX "CONF", altfile, 0);
418 openldap_ldap_init_w_sysconf( altfile );
421 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
422 LDAP_ENV_PREFIX "CONF", 0, 0);
426 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
428 if( altfile != NULL ) {
429 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
430 LDAP_ENV_PREFIX "RC", altfile, 0);
431 openldap_ldap_init_w_userconf( altfile );
434 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
435 LDAP_ENV_PREFIX "RC", 0, 0);
438 openldap_ldap_init_w_env(NULL);