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, /* deprecated */
63 offsetof(struct ldapoptions, ldo_defport)},
64 {0, ATTR_URIS, "HOST", NULL, 1}, /* deprecated */
65 {0, ATTR_URIS, "URI", NULL, 0}, /* replaces HOST/URI */
66 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
67 {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 {0, ATTR_TLS, "TLS_RANDFILE", NULL, LDAP_OPT_X_TLS_RANDOM_FILE},
79 #ifdef HAVE_CYRUS_SASL
80 {0, ATTR_INT, "SASL_MINSSF", NULL,
81 offsetof(struct ldapoptions, ldo_sasl_minssf)},
82 {0, ATTR_INT, "SASL_MAXSSF", NULL,
83 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 )
100 struct ldapoptions *gopts;
102 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
103 return; /* Could not allocate mem for global options */
111 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
113 fp = fopen(file, "r");
115 /* could not open file */
119 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
121 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
122 /* skip lines starting with '#' */
123 if(*start == '#') continue;
125 /* trim leading white space */
126 while((*start != '\0') && isspace((unsigned char) *start))
130 if(*start == '\0') continue;
132 /* trim trailing white space */
133 end = &start[strlen(start)-1];
134 while(isspace((unsigned char)*end)) end--;
138 if(*start == '\0') continue;
141 /* parse the command */
143 while((*start != '\0') && !isspace((unsigned char)*start)) {
147 /* command has no argument */
153 /* we must have some whitespace to skip */
154 while(isspace((unsigned char)*start)) start++;
157 for(i=0; attrs[i].type != ATTR_NONE; i++) {
160 if( !userconf && attrs[i].useronly ) {
164 if(strcasecmp(cmd, attrs[i].name) != 0) {
168 switch(attrs[i].type) {
170 if((strcasecmp(opt, "on") == 0)
171 || (strcasecmp(opt, "yes") == 0)
172 || (strcasecmp(opt, "true") == 0))
174 LDAP_BOOL_SET(gopts, attrs[i].offset);
177 LDAP_BOOL_CLR(gopts, attrs[i].offset);
183 p = &((char *) gopts)[attrs[i].offset];
184 * (int*) p = atoi(opt);
188 const struct ol_keyvalue *kv;
190 for(kv = attrs[i].data;
194 if(strcasecmp(opt, kv->key) == 0) {
195 p = &((char *) gopts)[attrs[i].offset];
196 * (int*) p = kv->value;
203 p = &((char *) gopts)[attrs[i].offset];
204 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
205 * (char**) p = LDAP_STRDUP(opt);
209 ldap_pvt_tls_config( gopts, attrs[i].offset, opt );
213 if (attrs[i].offset == 0) {
214 ldap_set_option( NULL, LDAP_OPT_URI, opt );
216 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
227 static void openldap_ldap_init_w_sysconf(const char *file)
229 openldap_ldap_init_w_conf( file, 0 );
232 static void openldap_ldap_init_w_userconf(const char *file)
242 home = getenv("HOME");
245 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
247 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
249 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
253 if(home != NULL && path != NULL) {
254 /* we assume UNIX path syntax is used... */
257 sprintf(path, "%s/%s", home, file);
258 openldap_ldap_init_w_conf(path, 1);
261 sprintf(path, "%s/.%s", home, file);
262 openldap_ldap_init_w_conf(path, 1);
270 openldap_ldap_init_w_conf(file, 1);
273 static void openldap_ldap_init_w_env(
274 struct ldapoptions *gopts,
277 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
283 if (prefix == NULL) {
284 prefix = LDAP_ENV_PREFIX;
287 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
288 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
291 for(i=0; attrs[i].type != ATTR_NONE; i++) {
292 strcpy(&buf[len], attrs[i].name);
299 switch(attrs[i].type) {
301 if((strcasecmp(value, "on") == 0)
302 || (strcasecmp(value, "yes") == 0)
303 || (strcasecmp(value, "true") == 0))
305 LDAP_BOOL_SET(gopts, attrs[i].offset);
308 LDAP_BOOL_CLR(gopts, attrs[i].offset);
313 p = &((char *) gopts)[attrs[i].offset];
314 * (int*) p = atoi(value);
318 const struct ol_keyvalue *kv;
320 for(kv = attrs[i].data;
324 if(strcasecmp(value, kv->key) == 0) {
325 p = &((char *) gopts)[attrs[i].offset];
326 * (int*) p = kv->value;
333 p = &((char *) gopts)[attrs[i].offset];
334 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
335 if (*value == '\0') {
338 * (char**) p = LDAP_STRDUP(value);
343 ldap_pvt_tls_config( gopts, attrs[i].offset, value );
347 if (attrs[i].offset == 0) {
348 ldap_set_option( NULL, LDAP_OPT_URI, value );
350 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
358 * Initialize the global options structure with default values.
360 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
363 gopts->ldo_debug = *dbglvl;
365 gopts->ldo_debug = 0;
367 gopts->ldo_version = LDAP_VERSION2;
368 gopts->ldo_deref = LDAP_DEREF_NEVER;
369 gopts->ldo_timelimit = LDAP_NO_LIMIT;
370 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
372 gopts->ldo_tm_api = (struct timeval *)NULL;
373 gopts->ldo_tm_net = (struct timeval *)NULL;
375 /* ldo_defludp is leaked, we should have an at_exit() handler
376 * to free this and whatever else needs to cleaned up.
378 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
379 gopts->ldo_defport = LDAP_PORT;
381 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
382 gopts->ldo_rebindproc = NULL;
384 LDAP_BOOL_ZERO(gopts);
386 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
389 gopts->ldo_tls_ctx = NULL;
391 #ifdef HAVE_CYRUS_SASL
392 gopts->ldo_sasl_minssf = 0;
393 gopts->ldo_sasl_maxssf = INT_MAX;
396 gopts->ldo_valid = LDAP_INITIALIZED;
401 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
403 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
407 ldap_int_utils_init();
413 #ifdef HAVE_CYRUS_SASL
414 ldap_pvt_sasl_init();
417 if ( ldap_int_tblsize == 0 )
420 ldap_int_initialize_global_options(gopts, NULL);
422 if( getenv("LDAPNOINIT") != NULL ) {
426 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
427 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
430 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
432 if( altfile != NULL ) {
433 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
434 LDAP_ENV_PREFIX "CONF", altfile, 0);
435 openldap_ldap_init_w_sysconf( altfile );
438 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
439 LDAP_ENV_PREFIX "CONF", 0, 0);
443 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
445 if( altfile != NULL ) {
446 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
447 LDAP_ENV_PREFIX "RC", altfile, 0);
448 openldap_ldap_init_w_userconf( altfile );
451 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
452 LDAP_ENV_PREFIX "RC", 0, 0);
455 openldap_ldap_init_w_env(gopts, NULL);