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
37 static const struct ol_keyvalue deref_kv[] = {
38 {"never", LDAP_DEREF_NEVER},
39 {"searching", LDAP_DEREF_SEARCHING},
40 {"finding", LDAP_DEREF_FINDING},
41 {"always", LDAP_DEREF_ALWAYS},
45 static const struct ol_attribute {
51 {ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
52 offsetof(struct ldapoptions, ldo_deref)},
53 {ATTR_INT, "SIZELIMIT", NULL,
54 offsetof(struct ldapoptions, ldo_sizelimit)},
55 {ATTR_INT, "TIMELIMIT", NULL,
56 offsetof(struct ldapoptions, ldo_timelimit)},
57 {ATTR_STRING, "BASE", NULL,
58 offsetof(struct ldapoptions, ldo_defbase)},
59 {ATTR_STRING, "HOST", NULL,
60 offsetof(struct ldapoptions, ldo_defhost)},
61 {ATTR_INT, "PORT", NULL,
62 offsetof(struct ldapoptions, ldo_defport)},
63 {ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
64 {ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
65 {ATTR_BOOL, "DNS", NULL, LDAP_BOOL_DNS},
66 {ATTR_TLS, "TLS", NULL, LDAP_OPT_X_TLS},
67 {ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
68 {ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
69 {ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
70 {ATTR_TLS, "TLS_CACERTDIR",NULL, LDAP_OPT_X_TLS_CACERTDIR},
71 {ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
72 {ATTR_NONE, NULL, NULL, 0}
75 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CACERTDIR")
76 #define MAX_LDAP_ENV_PREFIX_LEN 8
78 static void openldap_ldap_init_w_conf(const char *file)
91 fp = fopen(file, "r");
93 /* could not open file */
97 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
98 /* skip lines starting with '#' */
99 if(*start == '#') continue;
101 /* trim leading white space */
102 while((*start != '\0') && isspace((unsigned char) *start))
106 if(*start == '\0') continue;
108 /* trim trailing white space */
109 end = &start[strlen(start)-1];
110 while(isspace((unsigned char)*end)) end--;
114 if(*start == '\0') continue;
117 /* parse the command */
119 while((*start != '\0') && !isspace((unsigned char)*start)) {
123 /* command has no argument */
129 /* we must have some non-whitespace to skip */
130 while(isspace((unsigned char)*start)) start++;
133 for(i=0; attrs[i].type != ATTR_NONE; i++) {
136 if(strcasecmp(cmd, attrs[i].name) != 0) {
140 switch(attrs[i].type) {
142 if((strcasecmp(opt, "on") == 0)
143 || (strcasecmp(opt, "yes") == 0)
144 || (strcasecmp(opt, "true") == 0))
146 LDAP_BOOL_SET(&gopts, attrs[i].offset);
149 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
155 p = &((char *) &gopts)[attrs[i].offset];
156 * (int*) p = atoi(opt);
160 const struct ol_keyvalue *kv;
162 for(kv = attrs[i].data;
166 if(strcasecmp(opt, kv->key) == 0) {
167 p = &((char *) &gopts)[attrs[i].offset];
168 * (int*) p = kv->value;
175 p = &((char *) &gopts)[attrs[i].offset];
176 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
177 * (char**) p = LDAP_STRDUP(opt);
181 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
191 static void openldap_ldap_init_w_userconf(const char *file)
201 home = getenv("HOME");
204 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
206 path = LDAP_MALLOC(strlen(file) + 3);
209 if(home != NULL && path != NULL) {
210 /* we assume UNIX path syntax is used... */
213 sprintf(path, "%s/%s", home, file);
214 openldap_ldap_init_w_conf(path);
217 sprintf(path, "%s/.%s", home, file);
218 openldap_ldap_init_w_conf(path);
226 openldap_ldap_init_w_conf(file);
229 static void openldap_ldap_init_w_env(const char *prefix)
231 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
237 if (prefix == NULL) {
238 prefix = LDAP_ENV_PREFIX;
241 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
242 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
245 for(i=0; attrs[i].type != ATTR_NONE; i++) {
246 strcpy(&buf[len], attrs[i].name);
253 switch(attrs[i].type) {
255 if((strcasecmp(value, "on") == 0)
256 || (strcasecmp(value, "yes") == 0)
257 || (strcasecmp(value, "true") == 0))
259 LDAP_BOOL_SET(&gopts, attrs[i].offset);
262 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
267 p = &((char *) &gopts)[attrs[i].offset];
268 * (int*) p = atoi(value);
272 const struct ol_keyvalue *kv;
274 for(kv = attrs[i].data;
278 if(strcasecmp(value, kv->key) == 0) {
279 p = &((char *) &gopts)[attrs[i].offset];
280 * (int*) p = kv->value;
287 p = &((char *) &gopts)[attrs[i].offset];
288 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
289 if (*value == '\0') {
292 * (char**) p = LDAP_STRDUP(value);
297 ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
304 void ldap_int_initialize( void )
306 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
310 ldap_int_utils_init();
316 if ( ldap_int_tblsize == 0 )
321 gopts.ldo_version = LDAP_VERSION2;
322 gopts.ldo_deref = LDAP_DEREF_NEVER;
323 gopts.ldo_timelimit = LDAP_NO_LIMIT;
324 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
326 gopts.ldo_tm_api = (struct timeval *)NULL;
327 gopts.ldo_tm_net = (struct timeval *)NULL;
329 gopts.ldo_defhost = LDAP_STRDUP("localhost");
330 gopts.ldo_defport = LDAP_PORT;
332 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
334 LDAP_BOOL_ZERO(&gopts);
336 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
339 gopts.ldo_tls_ctx = NULL;
342 gopts.ldo_valid = LDAP_INITIALIZED;
344 if( getenv("LDAPNOINIT") != NULL ) {
348 openldap_ldap_init_w_conf(LDAP_CONF_FILE);
349 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
352 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
354 if( altfile != NULL ) {
355 openldap_ldap_init_w_conf( altfile );
360 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
362 if( altfile != NULL ) {
363 openldap_ldap_init_w_userconf( altfile );
367 openldap_ldap_init_w_env(NULL);