3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
12 #include <ac/string.h>
17 #include "ldap_defaults.h"
19 struct ldapoptions ldap_int_global_options =
20 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };
23 #define gopts ldap_int_global_options
38 static const struct ol_keyvalue deref_kv[] = {
39 {"never", LDAP_DEREF_NEVER},
40 {"searching", LDAP_DEREF_SEARCHING},
41 {"finding", LDAP_DEREF_FINDING},
42 {"always", LDAP_DEREF_ALWAYS},
46 static const struct ol_attribute {
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 {1, ATTR_STRING, "BINDDN", NULL,
60 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},
68 {0, ATTR_URIS, "URI", NULL, 0},
69 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
70 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
71 {0, ATTR_BOOL, "DNS", NULL, LDAP_BOOL_DNS},
72 {0, ATTR_TLS, "TLS", NULL, LDAP_OPT_X_TLS},
73 {0, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
74 {0, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
75 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
76 {0, ATTR_TLS, "TLS_CACERTDIR",NULL, LDAP_OPT_X_TLS_CACERTDIR},
77 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
78 #ifdef HAVE_CYRUS_SASL
79 {0, ATTR_INT, "SASL_MINSSF", NULL,
80 offsetof(struct ldapoptions, ldo_sasl_minssf)},
81 {0, ATTR_INT, "SASL_MAXSSF", NULL,
82 offsetof(struct ldapoptions, ldo_sasl_maxssf)},
84 {0, ATTR_NONE, NULL, NULL, 0}
87 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
88 #define MAX_LDAP_ENV_PREFIX_LEN 8
90 static void openldap_ldap_init_w_conf(
91 const char *file, int userconf )
104 fp = fopen(file, "r");
106 /* could not open file */
110 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
111 /* skip lines starting with '#' */
112 if(*start == '#') continue;
114 /* trim leading white space */
115 while((*start != '\0') && isspace((unsigned char) *start))
119 if(*start == '\0') continue;
121 /* trim trailing white space */
122 end = &start[strlen(start)-1];
123 while(isspace((unsigned char)*end)) end--;
127 if(*start == '\0') continue;
130 /* parse the command */
132 while((*start != '\0') && !isspace((unsigned char)*start)) {
136 /* command has no argument */
142 /* we must have some whitespace to skip */
143 while(isspace((unsigned char)*start)) start++;
146 for(i=0; attrs[i].type != ATTR_NONE; i++) {
149 if( !userconf && attrs[i].useronly ) {
153 if(strcasecmp(cmd, attrs[i].name) != 0) {
157 switch(attrs[i].type) {
159 if((strcasecmp(opt, "on") == 0)
160 || (strcasecmp(opt, "yes") == 0)
161 || (strcasecmp(opt, "true") == 0))
163 LDAP_BOOL_SET(&gopts, attrs[i].offset);
166 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
172 p = &((char *) &gopts)[attrs[i].offset];
173 * (int*) p = atoi(opt);
177 const struct ol_keyvalue *kv;
179 for(kv = attrs[i].data;
183 if(strcasecmp(opt, kv->key) == 0) {
184 p = &((char *) &gopts)[attrs[i].offset];
185 * (int*) p = kv->value;
192 p = &((char *) &gopts)[attrs[i].offset];
193 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
194 * (char**) p = LDAP_STRDUP(opt);
198 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
202 if (attrs[i].offset == 0) {
203 ldap_set_option( NULL, LDAP_OPT_URI, opt );
205 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
215 static void openldap_ldap_init_w_sysconf(const char *file)
217 openldap_ldap_init_w_conf( file, 0 );
220 static void openldap_ldap_init_w_userconf(const char *file)
230 home = getenv("HOME");
233 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
235 path = LDAP_MALLOC(strlen(file) + 3);
238 if(home != NULL && path != NULL) {
239 /* we assume UNIX path syntax is used... */
242 sprintf(path, "%s/%s", home, file);
243 openldap_ldap_init_w_conf(path, 1);
246 sprintf(path, "%s/.%s", home, file);
247 openldap_ldap_init_w_conf(path, 1);
255 openldap_ldap_init_w_conf(file, 1);
258 static void openldap_ldap_init_w_env(const char *prefix)
260 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
266 if (prefix == NULL) {
267 prefix = LDAP_ENV_PREFIX;
270 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
271 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
274 for(i=0; attrs[i].type != ATTR_NONE; i++) {
275 strcpy(&buf[len], attrs[i].name);
282 switch(attrs[i].type) {
284 if((strcasecmp(value, "on") == 0)
285 || (strcasecmp(value, "yes") == 0)
286 || (strcasecmp(value, "true") == 0))
288 LDAP_BOOL_SET(&gopts, attrs[i].offset);
291 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
296 p = &((char *) &gopts)[attrs[i].offset];
297 * (int*) p = atoi(value);
301 const struct ol_keyvalue *kv;
303 for(kv = attrs[i].data;
307 if(strcasecmp(value, kv->key) == 0) {
308 p = &((char *) &gopts)[attrs[i].offset];
309 * (int*) p = kv->value;
316 p = &((char *) &gopts)[attrs[i].offset];
317 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
318 if (*value == '\0') {
321 * (char**) p = LDAP_STRDUP(value);
326 ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
330 if (attrs[i].offset == 0) {
331 ldap_set_option( NULL, LDAP_OPT_URI, value );
333 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
340 void ldap_int_initialize( void )
342 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
346 ldap_int_utils_init();
352 #ifdef HAVE_CYRUS_SASL
353 ldap_pvt_sasl_init();
356 if ( ldap_int_tblsize == 0 )
361 gopts.ldo_version = LDAP_VERSION2;
362 gopts.ldo_deref = LDAP_DEREF_NEVER;
363 gopts.ldo_timelimit = LDAP_NO_LIMIT;
364 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
366 gopts.ldo_tm_api = (struct timeval *)NULL;
367 gopts.ldo_tm_net = (struct timeval *)NULL;
369 /* ldo_defludp is leaked, we should have an at_exit() handler
370 * to free this and whatever else needs to cleaned up.
372 ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
373 gopts.ldo_defport = LDAP_PORT;
375 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
377 LDAP_BOOL_ZERO(&gopts);
379 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
382 gopts.ldo_tls_ctx = NULL;
384 #ifdef HAVE_CYRUS_SASL
385 gopts.ldo_sasl_minssf = 0;
386 gopts.ldo_sasl_maxssf = INT_MAX;
389 gopts.ldo_valid = LDAP_INITIALIZED;
391 if( getenv("LDAPNOINIT") != NULL ) {
395 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
396 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
399 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
401 if( altfile != NULL ) {
402 openldap_ldap_init_w_sysconf( altfile );
407 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
409 if( altfile != NULL ) {
410 openldap_ldap_init_w_userconf( altfile );
414 openldap_ldap_init_w_env(NULL);