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", NULL, LDAP_BOOL_REFERRALS},
63 {ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
64 {ATTR_BOOL, "DNS", NULL, LDAP_BOOL_DNS},
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((unsigned char) *start))
99 if(*start == '\0') continue;
101 /* trim trailing white space */
102 end = &start[strlen(start)-1];
103 while(isspace((unsigned char)*end)) end--;
107 if(*start == '\0') continue;
110 /* parse the command */
112 while((*start != '\0') && !isspace((unsigned char)*start)) {
116 /* command has no argument */
122 /* we must have some non-whitespace to skip */
123 while(isspace((unsigned char)*start)) start++;
126 for(i=0; attrs[i].type != ATTR_NONE; i++) {
129 if(strcasecmp(cmd, attrs[i].name) != 0) {
133 switch(attrs[i].type) {
135 if((strcasecmp(opt, "on") == 0)
136 || (strcasecmp(opt, "yes") == 0)
137 || (strcasecmp(opt, "true") == 0))
139 LDAP_BOOL_SET(&gopts, attrs[i].offset);
142 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
148 p = &((char *) &gopts)[attrs[i].offset];
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 p = &((char *) &gopts)[attrs[i].offset];
161 * (int*) p = kv->value;
168 p = &((char *) &gopts)[attrs[i].offset];
169 if (* (char**) p != NULL) free(* (char**) p);
170 * (char**) p = strdup(opt);
177 static void openldap_ldap_init_w_userconf(const char *file)
187 home = getenv("HOME");
190 path = malloc(strlen(home) + strlen(file) + 3);
192 path = malloc(strlen(file) + 3);
195 if(home != NULL && path != NULL) {
196 /* we assume UNIX path syntax is used... */
199 sprintf(path, "%s/%s", home, file);
200 openldap_ldap_init_w_conf(path);
203 sprintf(path, "%s/.%s", home, file);
204 openldap_ldap_init_w_conf(path);
212 openldap_ldap_init_w_conf(file);
215 static void openldap_ldap_init_w_env(const char *prefix)
217 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
223 if (prefix == NULL) {
224 prefix = DEFAULT_LDAP_ENV_PREFIX;
227 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
228 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
231 for(i=0; attrs[i].type != ATTR_NONE; i++) {
232 strcpy(&buf[len], attrs[i].name);
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, attrs[i].offset);
248 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
253 p = &((char *) &gopts)[attrs[i].offset];
254 * (int*) p = atoi(value);
258 struct ol_keyvalue *kv;
260 for(kv = (struct ol_keyvalue *) attrs[i].data;
264 if(strcasecmp(value, kv->key) == 0) {
265 p = &((char *) &gopts)[attrs[i].offset];
266 * (int*) p = kv->value;
273 p = &((char *) &gopts)[attrs[i].offset];
274 if (* (char**) p != NULL) free(* (char**) p);
275 if (*value == '\0') {
278 * (char**) p = strdup(value);
285 void openldap_ldap_initialize( void )
287 if ( openldap_ldap_initialized ) {
291 ldap_pvt_init_utils();
293 gopts.ldo_version = LDAP_VERSION2;
294 gopts.ldo_deref = LDAP_DEREF_NEVER;
295 gopts.ldo_timelimit = LDAP_NO_LIMIT;
296 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
300 gopts.ldo_defhost = strdup("localhost");
301 gopts.ldo_defport = LDAP_PORT;
303 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
305 LDAP_BOOL_ZERO(&gopts);
307 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
308 LDAP_VERSION_MAX > LDAP_VERSION2
309 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
312 openldap_ldap_initialized = 1;
314 if( getenv("LDAPNOINIT") != NULL ) {
318 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
319 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
322 char *altfile = getenv("LDAPCONF");
324 if( altfile != NULL ) {
325 openldap_ldap_init_w_conf( altfile );
330 char *altfile = getenv("LDAPRC");
332 if( altfile != NULL ) {
333 openldap_ldap_init_w_userconf( altfile );
337 openldap_ldap_init_w_env(NULL);