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 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
106 fp = fopen(file, "r");
108 /* could not open file */
112 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
114 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
115 /* skip lines starting with '#' */
116 if(*start == '#') continue;
118 /* trim leading white space */
119 while((*start != '\0') && isspace((unsigned char) *start))
123 if(*start == '\0') continue;
125 /* trim trailing white space */
126 end = &start[strlen(start)-1];
127 while(isspace((unsigned char)*end)) end--;
131 if(*start == '\0') continue;
134 /* parse the command */
136 while((*start != '\0') && !isspace((unsigned char)*start)) {
140 /* command has no argument */
146 /* we must have some whitespace to skip */
147 while(isspace((unsigned char)*start)) start++;
150 for(i=0; attrs[i].type != ATTR_NONE; i++) {
153 if( !userconf && attrs[i].useronly ) {
157 if(strcasecmp(cmd, attrs[i].name) != 0) {
161 switch(attrs[i].type) {
163 if((strcasecmp(opt, "on") == 0)
164 || (strcasecmp(opt, "yes") == 0)
165 || (strcasecmp(opt, "true") == 0))
167 LDAP_BOOL_SET(&gopts, attrs[i].offset);
170 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
176 p = &((char *) &gopts)[attrs[i].offset];
177 * (int*) p = atoi(opt);
181 const struct ol_keyvalue *kv;
183 for(kv = attrs[i].data;
187 if(strcasecmp(opt, kv->key) == 0) {
188 p = &((char *) &gopts)[attrs[i].offset];
189 * (int*) p = kv->value;
196 p = &((char *) &gopts)[attrs[i].offset];
197 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
198 * (char**) p = LDAP_STRDUP(opt);
202 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
206 if (attrs[i].offset == 0) {
207 ldap_set_option( NULL, LDAP_OPT_URI, opt );
209 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
220 static void openldap_ldap_init_w_sysconf(const char *file)
222 openldap_ldap_init_w_conf( file, 0 );
225 static void openldap_ldap_init_w_userconf(const char *file)
235 home = getenv("HOME");
238 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
240 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
242 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
246 if(home != NULL && path != NULL) {
247 /* we assume UNIX path syntax is used... */
250 sprintf(path, "%s/%s", home, file);
251 openldap_ldap_init_w_conf(path, 1);
254 sprintf(path, "%s/.%s", home, file);
255 openldap_ldap_init_w_conf(path, 1);
263 openldap_ldap_init_w_conf(file, 1);
266 static void openldap_ldap_init_w_env(const char *prefix)
268 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
274 if (prefix == NULL) {
275 prefix = LDAP_ENV_PREFIX;
278 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
279 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
282 for(i=0; attrs[i].type != ATTR_NONE; i++) {
283 strcpy(&buf[len], attrs[i].name);
290 switch(attrs[i].type) {
292 if((strcasecmp(value, "on") == 0)
293 || (strcasecmp(value, "yes") == 0)
294 || (strcasecmp(value, "true") == 0))
296 LDAP_BOOL_SET(&gopts, attrs[i].offset);
299 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
304 p = &((char *) &gopts)[attrs[i].offset];
305 * (int*) p = atoi(value);
309 const struct ol_keyvalue *kv;
311 for(kv = attrs[i].data;
315 if(strcasecmp(value, kv->key) == 0) {
316 p = &((char *) &gopts)[attrs[i].offset];
317 * (int*) p = kv->value;
324 p = &((char *) &gopts)[attrs[i].offset];
325 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
326 if (*value == '\0') {
329 * (char**) p = LDAP_STRDUP(value);
334 ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
338 if (attrs[i].offset == 0) {
339 ldap_set_option( NULL, LDAP_OPT_URI, value );
341 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
348 void ldap_int_initialize( int *dbglvl )
350 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
354 ldap_int_utils_init();
360 #ifdef HAVE_CYRUS_SASL
361 ldap_pvt_sasl_init();
364 if ( ldap_int_tblsize == 0 )
368 gopts.ldo_debug = *dbglvl;
372 gopts.ldo_version = LDAP_VERSION2;
373 gopts.ldo_deref = LDAP_DEREF_NEVER;
374 gopts.ldo_timelimit = LDAP_NO_LIMIT;
375 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
377 gopts.ldo_tm_api = (struct timeval *)NULL;
378 gopts.ldo_tm_net = (struct timeval *)NULL;
380 /* ldo_defludp is leaked, we should have an at_exit() handler
381 * to free this and whatever else needs to cleaned up.
383 ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
384 gopts.ldo_defport = LDAP_PORT;
386 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
388 LDAP_BOOL_ZERO(&gopts);
390 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
393 gopts.ldo_tls_ctx = NULL;
395 #ifdef HAVE_CYRUS_SASL
396 gopts.ldo_sasl_minssf = 0;
397 gopts.ldo_sasl_maxssf = INT_MAX;
400 gopts.ldo_valid = LDAP_INITIALIZED;
402 if( getenv("LDAPNOINIT") != NULL ) {
406 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
407 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
410 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
412 if( altfile != NULL ) {
413 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
414 LDAP_ENV_PREFIX "CONF", altfile, 0);
415 openldap_ldap_init_w_sysconf( altfile );
418 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
419 LDAP_ENV_PREFIX "CONF", 0, 0);
423 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
425 if( altfile != NULL ) {
426 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
427 LDAP_ENV_PREFIX "RC", altfile, 0);
428 openldap_ldap_init_w_userconf( altfile );
431 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
432 LDAP_ENV_PREFIX "RC", 0, 0);
435 openldap_ldap_init_w_env(NULL);