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 {0, ATTR_NONE, NULL, NULL, 0}
81 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
82 #define MAX_LDAP_ENV_PREFIX_LEN 8
84 static void openldap_ldap_init_w_conf(
85 const char *file, int userconf )
98 fp = fopen(file, "r");
100 /* could not open file */
104 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
105 /* skip lines starting with '#' */
106 if(*start == '#') continue;
108 /* trim leading white space */
109 while((*start != '\0') && isspace((unsigned char) *start))
113 if(*start == '\0') continue;
115 /* trim trailing white space */
116 end = &start[strlen(start)-1];
117 while(isspace((unsigned char)*end)) end--;
121 if(*start == '\0') continue;
124 /* parse the command */
126 while((*start != '\0') && !isspace((unsigned char)*start)) {
130 /* command has no argument */
136 /* we must have some whitespace to skip */
137 while(isspace((unsigned char)*start)) start++;
140 for(i=0; attrs[i].type != ATTR_NONE; i++) {
143 if( !userconf && !attrs[i].useronly ) {
147 if(strcasecmp(cmd, attrs[i].name) != 0) {
151 switch(attrs[i].type) {
153 if((strcasecmp(opt, "on") == 0)
154 || (strcasecmp(opt, "yes") == 0)
155 || (strcasecmp(opt, "true") == 0))
157 LDAP_BOOL_SET(&gopts, attrs[i].offset);
160 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
166 p = &((char *) &gopts)[attrs[i].offset];
167 * (int*) p = atoi(opt);
171 const struct ol_keyvalue *kv;
173 for(kv = attrs[i].data;
177 if(strcasecmp(opt, kv->key) == 0) {
178 p = &((char *) &gopts)[attrs[i].offset];
179 * (int*) p = kv->value;
186 p = &((char *) &gopts)[attrs[i].offset];
187 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
188 * (char**) p = LDAP_STRDUP(opt);
192 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
196 if (attrs[i].offset == 0) {
197 ldap_set_option( NULL, LDAP_OPT_URI, opt );
199 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
209 static void openldap_ldap_init_w_sysconf(const char *file)
211 openldap_ldap_init_w_conf( file, 0 );
214 static void openldap_ldap_init_w_userconf(const char *file)
224 home = getenv("HOME");
227 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
229 path = LDAP_MALLOC(strlen(file) + 3);
232 if(home != NULL && path != NULL) {
233 /* we assume UNIX path syntax is used... */
236 sprintf(path, "%s/%s", home, file);
237 openldap_ldap_init_w_conf(path, 1);
240 sprintf(path, "%s/.%s", home, file);
241 openldap_ldap_init_w_conf(path, 1);
249 openldap_ldap_init_w_conf(file, 1);
252 static void openldap_ldap_init_w_env(const char *prefix)
254 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
260 if (prefix == NULL) {
261 prefix = LDAP_ENV_PREFIX;
264 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
265 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
268 for(i=0; attrs[i].type != ATTR_NONE; i++) {
269 strcpy(&buf[len], attrs[i].name);
276 switch(attrs[i].type) {
278 if((strcasecmp(value, "on") == 0)
279 || (strcasecmp(value, "yes") == 0)
280 || (strcasecmp(value, "true") == 0))
282 LDAP_BOOL_SET(&gopts, attrs[i].offset);
285 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
290 p = &((char *) &gopts)[attrs[i].offset];
291 * (int*) p = atoi(value);
295 const struct ol_keyvalue *kv;
297 for(kv = attrs[i].data;
301 if(strcasecmp(value, kv->key) == 0) {
302 p = &((char *) &gopts)[attrs[i].offset];
303 * (int*) p = kv->value;
310 p = &((char *) &gopts)[attrs[i].offset];
311 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
312 if (*value == '\0') {
315 * (char**) p = LDAP_STRDUP(value);
320 ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
324 if (attrs[i].offset == 0) {
325 ldap_set_option( NULL, LDAP_OPT_URI, value );
327 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
334 void ldap_int_initialize( void )
336 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
340 ldap_int_utils_init();
346 #ifdef HAVE_CYRUS_SASL
347 ldap_pvt_sasl_init();
350 if ( ldap_int_tblsize == 0 )
355 gopts.ldo_version = LDAP_VERSION2;
356 gopts.ldo_deref = LDAP_DEREF_NEVER;
357 gopts.ldo_timelimit = LDAP_NO_LIMIT;
358 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
360 gopts.ldo_tm_api = (struct timeval *)NULL;
361 gopts.ldo_tm_net = (struct timeval *)NULL;
363 ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
364 gopts.ldo_defport = LDAP_PORT;
366 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
368 LDAP_BOOL_ZERO(&gopts);
370 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
373 gopts.ldo_tls_ctx = NULL;
376 gopts.ldo_valid = LDAP_INITIALIZED;
378 if( getenv("LDAPNOINIT") != NULL ) {
382 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
383 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
386 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
388 if( altfile != NULL ) {
389 openldap_ldap_init_w_sysconf( altfile );
394 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
396 if( altfile != NULL ) {
397 openldap_ldap_init_w_userconf( altfile );
401 openldap_ldap_init_w_env(NULL);