2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/socket.h>
11 #include <ac/string.h>
16 #include "ldap_defaults.h"
18 struct ldapoptions ldap_int_global_options =
19 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };
22 #define gopts ldap_int_global_options
36 static const struct ol_keyvalue deref_kv[] = {
37 {"never", LDAP_DEREF_NEVER},
38 {"searching", LDAP_DEREF_SEARCHING},
39 {"finding", LDAP_DEREF_FINDING},
40 {"always", LDAP_DEREF_ALWAYS},
44 static const struct ol_attribute {
50 {ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
51 offsetof(struct ldapoptions, ldo_deref)},
52 {ATTR_INT, "SIZELIMIT", NULL,
53 offsetof(struct ldapoptions, ldo_sizelimit)},
54 {ATTR_INT, "TIMELIMIT", NULL,
55 offsetof(struct ldapoptions, ldo_timelimit)},
56 {ATTR_STRING, "BASE", NULL,
57 offsetof(struct ldapoptions, ldo_defbase)},
58 {ATTR_STRING, "HOST", NULL,
59 offsetof(struct ldapoptions, ldo_defhost)},
60 {ATTR_INT, "PORT", NULL,
61 offsetof(struct ldapoptions, ldo_defport)},
62 {ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
63 {ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
64 {ATTR_BOOL, "DNS", NULL, LDAP_BOOL_DNS},
65 {ATTR_BOOL, "TLS", NULL, LDAP_OPT_X_TLS},
66 {ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
67 {ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
68 {ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
69 {ATTR_TLS, "TLS_CACERTDIR",NULL, LDAP_OPT_X_TLS_CACERTDIR},
70 {ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
71 {ATTR_NONE, NULL, NULL, 0}
74 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
75 #define MAX_LDAP_ENV_PREFIX_LEN 8
77 static void openldap_ldap_init_w_conf(const char *file)
90 fp = fopen(file, "r");
92 /* could not open file */
96 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
97 /* skip lines starting with '#' */
98 if(*start == '#') continue;
100 /* trim leading white space */
101 while((*start != '\0') && isspace((unsigned char) *start))
105 if(*start == '\0') continue;
107 /* trim trailing white space */
108 end = &start[strlen(start)-1];
109 while(isspace((unsigned char)*end)) end--;
113 if(*start == '\0') continue;
116 /* parse the command */
118 while((*start != '\0') && !isspace((unsigned char)*start)) {
122 /* command has no argument */
128 /* we must have some non-whitespace to skip */
129 while(isspace((unsigned char)*start)) start++;
132 for(i=0; attrs[i].type != ATTR_NONE; i++) {
135 if(strcasecmp(cmd, attrs[i].name) != 0) {
139 switch(attrs[i].type) {
141 if((strcasecmp(opt, "on") == 0)
142 || (strcasecmp(opt, "yes") == 0)
143 || (strcasecmp(opt, "true") == 0))
145 LDAP_BOOL_SET(&gopts, attrs[i].offset);
148 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
154 p = &((char *) &gopts)[attrs[i].offset];
155 * (int*) p = atoi(opt);
159 const struct ol_keyvalue *kv;
161 for(kv = attrs[i].data;
165 if(strcasecmp(opt, kv->key) == 0) {
166 p = &((char *) &gopts)[attrs[i].offset];
167 * (int*) p = kv->value;
174 p = &((char *) &gopts)[attrs[i].offset];
175 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
176 * (char**) p = LDAP_STRDUP(opt);
180 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
190 static void openldap_ldap_init_w_userconf(const char *file)
200 home = getenv("HOME");
203 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
205 path = LDAP_MALLOC(strlen(file) + 3);
208 if(home != NULL && path != NULL) {
209 /* we assume UNIX path syntax is used... */
212 sprintf(path, "%s/%s", home, file);
213 openldap_ldap_init_w_conf(path);
216 sprintf(path, "%s/.%s", home, file);
217 openldap_ldap_init_w_conf(path);
225 openldap_ldap_init_w_conf(file);
228 static void openldap_ldap_init_w_env(const char *prefix)
230 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
236 if (prefix == NULL) {
237 prefix = LDAP_ENV_PREFIX;
240 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
241 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
244 for(i=0; attrs[i].type != ATTR_NONE; i++) {
245 strcpy(&buf[len], attrs[i].name);
252 switch(attrs[i].type) {
254 if((strcasecmp(value, "on") == 0)
255 || (strcasecmp(value, "yes") == 0)
256 || (strcasecmp(value, "true") == 0))
258 LDAP_BOOL_SET(&gopts, attrs[i].offset);
261 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
266 p = &((char *) &gopts)[attrs[i].offset];
267 * (int*) p = atoi(value);
271 const struct ol_keyvalue *kv;
273 for(kv = attrs[i].data;
277 if(strcasecmp(value, kv->key) == 0) {
278 p = &((char *) &gopts)[attrs[i].offset];
279 * (int*) p = kv->value;
286 p = &((char *) &gopts)[attrs[i].offset];
287 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
288 if (*value == '\0') {
291 * (char**) p = LDAP_STRDUP(value);
296 ldap_pvt_tls_config( attrs[i].offset, value );
303 void ldap_int_initialize( void )
305 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
309 ldap_int_utils_init();
315 if ( ldap_int_tblsize == 0 )
320 gopts.ldo_version = LDAP_VERSION2;
321 gopts.ldo_deref = LDAP_DEREF_NEVER;
322 gopts.ldo_timelimit = LDAP_NO_LIMIT;
323 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
325 gopts.ldo_tm_api = (struct timeval *)NULL;
326 gopts.ldo_tm_net = (struct timeval *)NULL;
328 gopts.ldo_defhost = LDAP_STRDUP("localhost");
329 gopts.ldo_defport = LDAP_PORT;
331 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
333 LDAP_BOOL_ZERO(&gopts);
335 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
338 gopts.ldo_tls_ctx = NULL;
341 gopts.ldo_valid = LDAP_INITIALIZED;
343 if( getenv("LDAPNOINIT") != NULL ) {
347 openldap_ldap_init_w_conf(LDAP_CONF_FILE);
348 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
351 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
353 if( altfile != NULL ) {
354 openldap_ldap_init_w_conf( altfile );
359 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
361 if( altfile != NULL ) {
362 openldap_ldap_init_w_userconf( altfile );
366 openldap_ldap_init_w_env(NULL);