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 "ldapconfig.h"
18 struct ldapoptions openldap_ldap_global_options = { LDAP_DEBUG_NONE };
21 #define gopts openldap_ldap_global_options
23 int openldap_ldap_initialized = 0;
36 static 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 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", (void *) LDAP_BOOL_REFERRALS, 0},
63 {ATTR_BOOL, "RESTART", (void *) LDAP_BOOL_RESTART, 0},
64 {ATTR_BOOL, "DNS", (void *) LDAP_BOOL_DNS, 0},
65 {ATTR_NONE, NULL, NULL, 0}
68 #define MAX_LDAP_ATTR_LEN sizeof("SIZELIMIT")
69 #define MAX_LDAP_ENV_PREFIX_LEN 8
71 static void openldap_ldap_init_w_conf(const char *file)
84 fp = fopen(file, "r");
86 /* could not open file */
90 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
91 /* skip lines starting with '#' */
92 if(*start == '#') continue;
94 /* trim leading white space */
95 while((*start != '\0') && isspace(*start)) start++;
98 if(*start == '\0') continue;
100 /* trim trailing white space */
101 end = &start[strlen(start)-1];
102 while(isspace(*end)) end--;
106 if(*start == '\0') continue;
109 /* parse the command */
111 while((*start != '\0') && !isspace(*start)) {
115 /* command has no argument */
121 /* we must have some non-whitespace to skip */
122 while(isspace(*start)) start++;
125 for(i=0; attrs[i].type != ATTR_NONE; i++) {
128 if(strcasecmp(cmd, attrs[i].name) != 0) {
132 p = &((char *) &gopts)[attrs[i].offset];
134 switch(attrs[i].type) {
136 if((strcasecmp(opt, "on") == 0)
137 || (strcasecmp(opt, "yes") == 0)
138 || (strcasecmp(opt, "true") == 0))
140 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
143 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
149 * (int*) p = atoi(opt);
153 struct ol_keyvalue *kv;
155 for(kv = (struct ol_keyvalue *) attrs[i].data;
159 if(strcasecmp(opt, kv->key) == 0) {
160 * (int*) p = kv->value;
167 if (* (char**) p != NULL) free(* (char**) p);
168 * (char**) p = strdup(opt);
175 static void openldap_ldap_init_w_userconf(const char *file)
185 home = getenv("HOME");
188 path = malloc(strlen(home) + strlen(file) + 3);
190 path = malloc(strlen(file) + 3);
193 if(home != NULL && path != NULL) {
194 /* we assume UNIX path syntax is used... */
197 sprintf(path, "%s/%s", home, file);
198 openldap_ldap_init_w_conf(path);
201 sprintf(path, "%s/.%s", home, file);
202 openldap_ldap_init_w_conf(path);
210 openldap_ldap_init_w_conf(file);
213 static void openldap_ldap_init_w_env(const char *prefix)
215 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
221 if (prefix == NULL) {
222 prefix = DEFAULT_LDAP_ENV_PREFIX;
225 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
226 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
229 for(i=0; attrs[i].type != ATTR_NONE; i++) {
230 strcpy(&buf[len], attrs[i].name);
237 p = &((char *) &gopts)[attrs[i].offset];
239 switch(attrs[i].type) {
241 if((strcasecmp(value, "on") == 0)
242 || (strcasecmp(value, "yes") == 0)
243 || (strcasecmp(value, "true") == 0))
245 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
248 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
253 * (int*) p = atoi(value);
257 struct ol_keyvalue *kv;
259 for(kv = (struct ol_keyvalue *) attrs[i].data;
263 if(strcasecmp(value, kv->key) == 0) {
264 * (int*) p = kv->value;
271 if (* (char**) p != NULL) free(* (char**) p);
272 if (*value == '\0') {
275 * (char**) p = strdup(value);
282 void openldap_ldap_initialize( void )
284 if ( openldap_ldap_initialized ) {
288 ldap_pvt_init_utils();
290 gopts.ldo_version = LDAP_VERSION2;
291 gopts.ldo_deref = LDAP_DEREF_NEVER;
292 gopts.ldo_timelimit = LDAP_NO_LIMIT;
293 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
297 gopts.ldo_defhost = strdup("localhost");
298 gopts.ldo_defport = LDAP_PORT;
300 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
302 LDAP_BOOL_ZERO(&gopts);
304 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
305 LDAP_VERSION_MAX > LDAP_VERSION2
306 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
309 openldap_ldap_initialized = 1;
311 if( getenv("LDAPNOINIT") != NULL ) {
315 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
316 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
319 char *altfile = getenv("LDAPCONF");
321 if( altfile != NULL ) {
322 openldap_ldap_init_w_conf( altfile );
327 char *altfile = getenv("LDAPRC");
329 if( altfile != NULL ) {
330 openldap_ldap_init_w_userconf( altfile );
334 openldap_ldap_init_w_env(NULL);