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((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 p = &((char *) &gopts)[attrs[i].offset];
135 switch(attrs[i].type) {
137 if((strcasecmp(opt, "on") == 0)
138 || (strcasecmp(opt, "yes") == 0)
139 || (strcasecmp(opt, "true") == 0))
141 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
144 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
150 * (int*) p = atoi(opt);
154 struct ol_keyvalue *kv;
156 for(kv = (struct ol_keyvalue *) attrs[i].data;
160 if(strcasecmp(opt, kv->key) == 0) {
161 * (int*) p = kv->value;
168 if (* (char**) p != NULL) free(* (char**) p);
169 * (char**) p = strdup(opt);
176 static void openldap_ldap_init_w_userconf(const char *file)
186 home = getenv("HOME");
189 path = malloc(strlen(home) + strlen(file) + 3);
191 path = malloc(strlen(file) + 3);
194 if(home != NULL && path != NULL) {
195 /* we assume UNIX path syntax is used... */
198 sprintf(path, "%s/%s", home, file);
199 openldap_ldap_init_w_conf(path);
202 sprintf(path, "%s/.%s", home, file);
203 openldap_ldap_init_w_conf(path);
211 openldap_ldap_init_w_conf(file);
214 static void openldap_ldap_init_w_env(const char *prefix)
216 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
222 if (prefix == NULL) {
223 prefix = DEFAULT_LDAP_ENV_PREFIX;
226 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
227 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
230 for(i=0; attrs[i].type != ATTR_NONE; i++) {
231 strcpy(&buf[len], attrs[i].name);
238 p = &((char *) &gopts)[attrs[i].offset];
240 switch(attrs[i].type) {
242 if((strcasecmp(value, "on") == 0)
243 || (strcasecmp(value, "yes") == 0)
244 || (strcasecmp(value, "true") == 0))
246 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
249 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
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 * (int*) p = kv->value;
272 if (* (char**) p != NULL) free(* (char**) p);
273 if (*value == '\0') {
276 * (char**) p = strdup(value);
283 void openldap_ldap_initialize( void )
285 if ( openldap_ldap_initialized ) {
289 ldap_pvt_init_utils();
291 gopts.ldo_version = LDAP_VERSION2;
292 gopts.ldo_deref = LDAP_DEREF_NEVER;
293 gopts.ldo_timelimit = LDAP_NO_LIMIT;
294 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
298 gopts.ldo_defhost = strdup("localhost");
299 gopts.ldo_defport = LDAP_PORT;
301 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
303 LDAP_BOOL_ZERO(&gopts);
305 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
306 LDAP_VERSION_MAX > LDAP_VERSION2
307 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
310 openldap_ldap_initialized = 1;
312 if( getenv("LDAPNOINIT") != NULL ) {
316 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
317 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
320 char *altfile = getenv("LDAPCONF");
322 if( altfile != NULL ) {
323 openldap_ldap_init_w_conf( altfile );
328 char *altfile = getenv("LDAPRC");
330 if( altfile != NULL ) {
331 openldap_ldap_init_w_userconf( altfile );
335 openldap_ldap_init_w_env(NULL);