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 {0, ATTR_STRING, "BASE", NULL,
60 offsetof(struct ldapoptions, ldo_defbase)},
61 {0, ATTR_INT, "PORT", NULL,
62 offsetof(struct ldapoptions, ldo_defport)},
63 /* **** keep this around for backward compatibility */
64 {0, ATTR_URIS, "HOST", NULL, 1},
66 {0, ATTR_URIS, "URI", NULL, 0},
67 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
68 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
69 {0, ATTR_BOOL, "DNS", NULL, LDAP_BOOL_DNS},
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_NONE, NULL, NULL, 0}
79 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
80 #define MAX_LDAP_ENV_PREFIX_LEN 8
82 static void openldap_ldap_init_w_conf(
83 const char *file, int userconf )
96 fp = fopen(file, "r");
98 /* could not open file */
102 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
103 /* skip lines starting with '#' */
104 if(*start == '#') continue;
106 /* trim leading white space */
107 while((*start != '\0') && isspace((unsigned char) *start))
111 if(*start == '\0') continue;
113 /* trim trailing white space */
114 end = &start[strlen(start)-1];
115 while(isspace((unsigned char)*end)) end--;
119 if(*start == '\0') continue;
122 /* parse the command */
124 while((*start != '\0') && !isspace((unsigned char)*start)) {
128 /* command has no argument */
134 /* we must have some non-whitespace to skip */
135 while(isspace((unsigned char)*start)) start++;
138 for(i=0; attrs[i].type != ATTR_NONE; i++) {
141 if( !userconf && !attrs[i].useronly ) {
145 if(strcasecmp(cmd, attrs[i].name) != 0) {
149 switch(attrs[i].type) {
151 if((strcasecmp(opt, "on") == 0)
152 || (strcasecmp(opt, "yes") == 0)
153 || (strcasecmp(opt, "true") == 0))
155 LDAP_BOOL_SET(&gopts, attrs[i].offset);
158 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
164 p = &((char *) &gopts)[attrs[i].offset];
165 * (int*) p = atoi(opt);
169 const struct ol_keyvalue *kv;
171 for(kv = attrs[i].data;
175 if(strcasecmp(opt, kv->key) == 0) {
176 p = &((char *) &gopts)[attrs[i].offset];
177 * (int*) p = kv->value;
184 p = &((char *) &gopts)[attrs[i].offset];
185 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
186 * (char**) p = LDAP_STRDUP(opt);
190 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
194 if (attrs[i].offset == 0) {
195 ldap_set_option( NULL, LDAP_OPT_URI, opt );
197 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
207 static void openldap_ldap_init_w_sysconf(const char *file)
209 openldap_ldap_init_w_conf( file, 0 );
212 static void openldap_ldap_init_w_userconf(const char *file)
222 home = getenv("HOME");
225 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
227 path = LDAP_MALLOC(strlen(file) + 3);
230 if(home != NULL && path != NULL) {
231 /* we assume UNIX path syntax is used... */
234 sprintf(path, "%s/%s", home, file);
235 openldap_ldap_init_w_conf(path, 1);
238 sprintf(path, "%s/.%s", home, file);
239 openldap_ldap_init_w_conf(path, 1);
247 openldap_ldap_init_w_conf(file, 1);
250 static void openldap_ldap_init_w_env(const char *prefix)
252 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
258 if (prefix == NULL) {
259 prefix = LDAP_ENV_PREFIX;
262 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
263 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
266 for(i=0; attrs[i].type != ATTR_NONE; i++) {
267 strcpy(&buf[len], attrs[i].name);
274 switch(attrs[i].type) {
276 if((strcasecmp(value, "on") == 0)
277 || (strcasecmp(value, "yes") == 0)
278 || (strcasecmp(value, "true") == 0))
280 LDAP_BOOL_SET(&gopts, attrs[i].offset);
283 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
288 p = &((char *) &gopts)[attrs[i].offset];
289 * (int*) p = atoi(value);
293 const struct ol_keyvalue *kv;
295 for(kv = attrs[i].data;
299 if(strcasecmp(value, kv->key) == 0) {
300 p = &((char *) &gopts)[attrs[i].offset];
301 * (int*) p = kv->value;
308 p = &((char *) &gopts)[attrs[i].offset];
309 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
310 if (*value == '\0') {
313 * (char**) p = LDAP_STRDUP(value);
318 ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
322 if (attrs[i].offset == 0) {
323 ldap_set_option( NULL, LDAP_OPT_URI, value );
325 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
332 void ldap_int_initialize( void )
334 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
338 ldap_int_utils_init();
344 if ( ldap_int_tblsize == 0 )
349 gopts.ldo_version = LDAP_VERSION2;
350 gopts.ldo_deref = LDAP_DEREF_NEVER;
351 gopts.ldo_timelimit = LDAP_NO_LIMIT;
352 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
354 gopts.ldo_tm_api = (struct timeval *)NULL;
355 gopts.ldo_tm_net = (struct timeval *)NULL;
357 ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
358 gopts.ldo_defport = LDAP_PORT;
360 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
362 LDAP_BOOL_ZERO(&gopts);
364 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
367 gopts.ldo_tls_ctx = NULL;
370 gopts.ldo_valid = LDAP_INITIALIZED;
372 if( getenv("LDAPNOINIT") != NULL ) {
376 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
377 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
380 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
382 if( altfile != NULL ) {
383 openldap_ldap_init_w_sysconf( altfile );
388 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
390 if( altfile != NULL ) {
391 openldap_ldap_init_w_userconf( altfile );
395 openldap_ldap_init_w_env(NULL);