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 };
39 static const struct ol_keyvalue deref_kv[] = {
40 {"never", LDAP_DEREF_NEVER},
41 {"searching", LDAP_DEREF_SEARCHING},
42 {"finding", LDAP_DEREF_FINDING},
43 {"always", LDAP_DEREF_ALWAYS},
47 static const struct ol_attribute {
54 {0, ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
55 offsetof(struct ldapoptions, ldo_deref)},
56 {0, ATTR_INT, "SIZELIMIT", NULL,
57 offsetof(struct ldapoptions, ldo_sizelimit)},
58 {0, ATTR_INT, "TIMELIMIT", NULL,
59 offsetof(struct ldapoptions, ldo_timelimit)},
60 {1, ATTR_STRING, "BINDDN", NULL,
61 offsetof(struct ldapoptions, ldo_defbinddn)},
62 {0, ATTR_STRING, "BASE", NULL,
63 offsetof(struct ldapoptions, ldo_defbase)},
64 {0, ATTR_INT, "PORT", NULL, /* deprecated */
65 offsetof(struct ldapoptions, ldo_defport)},
66 {0, ATTR_URIS, "HOST", NULL, 1}, /* deprecated */
67 {0, ATTR_URIS, "URI", NULL, 0}, /* replaces HOST/URI */
68 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
69 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
71 #ifdef HAVE_CYRUS_SASL
72 {1, ATTR_STRING, "SASL_MECH", NULL,
73 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
74 {1, ATTR_STRING, "SASL_REALM", NULL,
75 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
76 {1, ATTR_STRING, "SASL_AUTHCID", NULL,
77 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
78 {1, ATTR_STRING, "SASL_AUTHZID", NULL,
79 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
80 {0, ATTR_SASL, "SASL_SECPROPS", NULL, LDAP_OPT_X_SASL_SECPROPS},
84 {0, ATTR_TLS, "TLS", NULL, LDAP_OPT_X_TLS},
85 {1, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
86 {1, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
87 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
88 {0, ATTR_TLS, "TLS_CACERTDIR",NULL, LDAP_OPT_X_TLS_CACERTDIR},
89 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
90 {0, ATTR_TLS, "TLS_RANDFILE", NULL, LDAP_OPT_X_TLS_RANDOM_FILE},
93 {0, ATTR_NONE, NULL, NULL, 0}
96 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
97 #define MAX_LDAP_ENV_PREFIX_LEN 8
99 static void openldap_ldap_init_w_conf(
100 const char *file, int userconf )
107 struct ldapoptions *gopts;
109 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
110 return; /* Could not allocate mem for global options */
118 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
120 fp = fopen(file, "r");
122 /* could not open file */
126 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
128 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
129 /* skip lines starting with '#' */
130 if(*start == '#') continue;
132 /* trim leading white space */
133 while((*start != '\0') && isspace((unsigned char) *start))
137 if(*start == '\0') continue;
139 /* trim trailing white space */
140 end = &start[strlen(start)-1];
141 while(isspace((unsigned char)*end)) end--;
145 if(*start == '\0') continue;
148 /* parse the command */
150 while((*start != '\0') && !isspace((unsigned char)*start)) {
154 /* command has no argument */
160 /* we must have some whitespace to skip */
161 while(isspace((unsigned char)*start)) start++;
164 for(i=0; attrs[i].type != ATTR_NONE; i++) {
167 if( !userconf && attrs[i].useronly ) {
171 if(strcasecmp(cmd, attrs[i].name) != 0) {
175 switch(attrs[i].type) {
177 if((strcasecmp(opt, "on") == 0)
178 || (strcasecmp(opt, "yes") == 0)
179 || (strcasecmp(opt, "true") == 0))
181 LDAP_BOOL_SET(gopts, attrs[i].offset);
184 LDAP_BOOL_CLR(gopts, attrs[i].offset);
190 p = &((char *) gopts)[attrs[i].offset];
191 * (int*) p = atoi(opt);
195 const struct ol_keyvalue *kv;
197 for(kv = attrs[i].data;
201 if(strcasecmp(opt, kv->key) == 0) {
202 p = &((char *) gopts)[attrs[i].offset];
203 * (int*) p = kv->value;
210 p = &((char *) gopts)[attrs[i].offset];
211 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
212 * (char**) p = LDAP_STRDUP(opt);
215 if (attrs[i].offset == 0) {
216 ldap_set_option( NULL, LDAP_OPT_URI, opt );
218 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
222 #ifdef HAVE_CYRUS_SASL
223 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
228 ldap_int_tls_config( gopts, attrs[i].offset, opt );
240 static void openldap_ldap_init_w_sysconf(const char *file)
242 openldap_ldap_init_w_conf( file, 0 );
245 static void openldap_ldap_init_w_userconf(const char *file)
255 home = getenv("HOME");
258 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
260 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
262 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
266 if(home != NULL && path != NULL) {
267 /* we assume UNIX path syntax is used... */
270 sprintf(path, "%s/%s", home, file);
271 openldap_ldap_init_w_conf(path, 1);
274 sprintf(path, "%s/.%s", home, file);
275 openldap_ldap_init_w_conf(path, 1);
283 openldap_ldap_init_w_conf(file, 1);
286 static void openldap_ldap_init_w_env(
287 struct ldapoptions *gopts,
290 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
296 if (prefix == NULL) {
297 prefix = LDAP_ENV_PREFIX;
300 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
301 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
304 for(i=0; attrs[i].type != ATTR_NONE; i++) {
305 strcpy(&buf[len], attrs[i].name);
312 switch(attrs[i].type) {
314 if((strcasecmp(value, "on") == 0)
315 || (strcasecmp(value, "yes") == 0)
316 || (strcasecmp(value, "true") == 0))
318 LDAP_BOOL_SET(gopts, attrs[i].offset);
321 LDAP_BOOL_CLR(gopts, attrs[i].offset);
326 p = &((char *) gopts)[attrs[i].offset];
327 * (int*) p = atoi(value);
331 const struct ol_keyvalue *kv;
333 for(kv = attrs[i].data;
337 if(strcasecmp(value, kv->key) == 0) {
338 p = &((char *) gopts)[attrs[i].offset];
339 * (int*) p = kv->value;
346 p = &((char *) gopts)[attrs[i].offset];
347 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
348 if (*value == '\0') {
351 * (char**) p = LDAP_STRDUP(value);
355 if (attrs[i].offset == 0) {
356 ldap_set_option( NULL, LDAP_OPT_URI, value );
358 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
362 #ifdef HAVE_CYRUS_SASL
363 ldap_int_sasl_config( gopts, attrs[i].offset, value );
368 ldap_int_tls_config( gopts, attrs[i].offset, value );
376 * Initialize the global options structure with default values.
378 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
381 gopts->ldo_debug = *dbglvl;
383 gopts->ldo_debug = 0;
385 gopts->ldo_version = LDAP_VERSION2;
386 gopts->ldo_deref = LDAP_DEREF_NEVER;
387 gopts->ldo_timelimit = LDAP_NO_LIMIT;
388 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
390 gopts->ldo_tm_api = (struct timeval *)NULL;
391 gopts->ldo_tm_net = (struct timeval *)NULL;
393 /* ldo_defludp is leaked, we should have an at_exit() handler
394 * to free this and whatever else needs to cleaned up.
396 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
397 gopts->ldo_defport = LDAP_PORT;
399 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
400 gopts->ldo_rebindproc = NULL;
402 LDAP_BOOL_ZERO(gopts);
404 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
406 #ifdef HAVE_CYRUS_SASL
407 gopts->ldo_def_sasl_mech = NULL;
408 gopts->ldo_def_sasl_realm = NULL;
409 gopts->ldo_def_sasl_authcid = NULL;
410 gopts->ldo_def_sasl_authzid = NULL;
412 memset( &gopts->ldo_sasl_secprops,
413 '\0', sizeof(gopts->ldo_sasl_secprops) );
415 gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
416 gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
417 gopts->ldo_sasl_secprops.security_flags =
418 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
422 gopts->ldo_tls_ctx = NULL;
425 gopts->ldo_valid = LDAP_INITIALIZED;
430 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
431 || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
432 char * ldap_int_hostname = NULL;
435 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
437 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
441 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
442 || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
443 ldap_int_hostname = ldap_pvt_get_fqdn( ldap_int_hostname );
446 ldap_int_utils_init();
448 if ( ldap_int_tblsize == 0 )
451 ldap_int_initialize_global_options(gopts, NULL);
453 if( getenv("LDAPNOINIT") != NULL ) {
457 #ifdef HAVE_CYRUS_SASL
459 /* set authentication identity to current user name */
460 char *user = getenv("USER");
462 if( user == NULL ) user = getenv("USERNAME");
463 if( user == NULL ) user = getenv("LOGNAME");
466 /* this value is leaked, need at_exit() handler */
467 gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
472 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
473 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
476 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
478 if( altfile != NULL ) {
479 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
480 LDAP_ENV_PREFIX "CONF", altfile, 0);
481 openldap_ldap_init_w_sysconf( altfile );
484 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
485 LDAP_ENV_PREFIX "CONF", 0, 0);
489 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
491 if( altfile != NULL ) {
492 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
493 LDAP_ENV_PREFIX "RC", altfile, 0);
494 openldap_ldap_init_w_userconf( altfile );
497 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
498 LDAP_ENV_PREFIX "RC", 0, 0);
501 openldap_ldap_init_w_env(gopts, NULL);
503 ldap_int_sasl_init();