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 {
52 {ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
53 offsetof(struct ldapoptions, ldo_deref)},
54 {ATTR_INT, "SIZELIMIT", NULL,
55 offsetof(struct ldapoptions, ldo_sizelimit)},
56 {ATTR_INT, "TIMELIMIT", NULL,
57 offsetof(struct ldapoptions, ldo_timelimit)},
58 {ATTR_STRING, "BASE", NULL,
59 offsetof(struct ldapoptions, ldo_defbase)},
60 {ATTR_INT, "PORT", NULL,
61 offsetof(struct ldapoptions, ldo_defport)},
62 /* **** keep this around for backward compatibility */
63 {ATTR_URLS, "HOST", NULL, 1},
65 {ATTR_URLS, "URL", NULL, 0},
66 {ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
67 {ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
68 {ATTR_BOOL, "DNS", NULL, LDAP_BOOL_DNS},
69 {ATTR_TLS, "TLS", NULL, LDAP_OPT_X_TLS},
70 {ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
71 {ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
72 {ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
73 {ATTR_TLS, "TLS_CACERTDIR",NULL, LDAP_OPT_X_TLS_CACERTDIR},
74 {ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
75 {ATTR_NONE, NULL, NULL, 0}
78 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
79 #define MAX_LDAP_ENV_PREFIX_LEN 8
81 static void openldap_ldap_init_w_conf(const char *file)
94 fp = fopen(file, "r");
96 /* could not open file */
100 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
101 /* skip lines starting with '#' */
102 if(*start == '#') continue;
104 /* trim leading white space */
105 while((*start != '\0') && isspace((unsigned char) *start))
109 if(*start == '\0') continue;
111 /* trim trailing white space */
112 end = &start[strlen(start)-1];
113 while(isspace((unsigned char)*end)) end--;
117 if(*start == '\0') continue;
120 /* parse the command */
122 while((*start != '\0') && !isspace((unsigned char)*start)) {
126 /* command has no argument */
132 /* we must have some non-whitespace to skip */
133 while(isspace((unsigned char)*start)) start++;
136 for(i=0; attrs[i].type != ATTR_NONE; i++) {
139 if(strcasecmp(cmd, attrs[i].name) != 0) {
143 switch(attrs[i].type) {
145 if((strcasecmp(opt, "on") == 0)
146 || (strcasecmp(opt, "yes") == 0)
147 || (strcasecmp(opt, "true") == 0))
149 LDAP_BOOL_SET(&gopts, attrs[i].offset);
152 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
158 p = &((char *) &gopts)[attrs[i].offset];
159 * (int*) p = atoi(opt);
163 const struct ol_keyvalue *kv;
165 for(kv = attrs[i].data;
169 if(strcasecmp(opt, kv->key) == 0) {
170 p = &((char *) &gopts)[attrs[i].offset];
171 * (int*) p = kv->value;
178 p = &((char *) &gopts)[attrs[i].offset];
179 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
180 * (char**) p = LDAP_STRDUP(opt);
184 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
188 if (attrs[i].offset == 0) {
189 ldap_set_option( NULL, LDAP_OPT_URI, opt );
191 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
201 static void openldap_ldap_init_w_userconf(const char *file)
211 home = getenv("HOME");
214 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
216 path = LDAP_MALLOC(strlen(file) + 3);
219 if(home != NULL && path != NULL) {
220 /* we assume UNIX path syntax is used... */
223 sprintf(path, "%s/%s", home, file);
224 openldap_ldap_init_w_conf(path);
227 sprintf(path, "%s/.%s", home, file);
228 openldap_ldap_init_w_conf(path);
236 openldap_ldap_init_w_conf(file);
239 static void openldap_ldap_init_w_env(const char *prefix)
241 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
247 if (prefix == NULL) {
248 prefix = LDAP_ENV_PREFIX;
251 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
252 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
255 for(i=0; attrs[i].type != ATTR_NONE; i++) {
256 strcpy(&buf[len], attrs[i].name);
263 switch(attrs[i].type) {
265 if((strcasecmp(value, "on") == 0)
266 || (strcasecmp(value, "yes") == 0)
267 || (strcasecmp(value, "true") == 0))
269 LDAP_BOOL_SET(&gopts, attrs[i].offset);
272 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
277 p = &((char *) &gopts)[attrs[i].offset];
278 * (int*) p = atoi(value);
282 const struct ol_keyvalue *kv;
284 for(kv = attrs[i].data;
288 if(strcasecmp(value, kv->key) == 0) {
289 p = &((char *) &gopts)[attrs[i].offset];
290 * (int*) p = kv->value;
297 p = &((char *) &gopts)[attrs[i].offset];
298 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
299 if (*value == '\0') {
302 * (char**) p = LDAP_STRDUP(value);
307 ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
311 if (attrs[i].offset == 0) {
312 ldap_set_option( NULL, LDAP_OPT_URI, value );
314 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
321 void ldap_int_initialize( void )
323 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
327 ldap_int_utils_init();
333 if ( ldap_int_tblsize == 0 )
338 gopts.ldo_version = LDAP_VERSION2;
339 gopts.ldo_deref = LDAP_DEREF_NEVER;
340 gopts.ldo_timelimit = LDAP_NO_LIMIT;
341 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
343 gopts.ldo_tm_api = (struct timeval *)NULL;
344 gopts.ldo_tm_net = (struct timeval *)NULL;
346 ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
347 gopts.ldo_defport = LDAP_PORT;
349 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
351 LDAP_BOOL_ZERO(&gopts);
353 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
356 gopts.ldo_tls_ctx = NULL;
359 gopts.ldo_valid = LDAP_INITIALIZED;
361 if( getenv("LDAPNOINIT") != NULL ) {
365 openldap_ldap_init_w_conf(LDAP_CONF_FILE);
366 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
369 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
371 if( altfile != NULL ) {
372 openldap_ldap_init_w_conf( altfile );
377 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
379 if( altfile != NULL ) {
380 openldap_ldap_init_w_userconf( altfile );
384 openldap_ldap_init_w_env(NULL);