10 extern char *strdup (const char *);
13 #include "ldapconfig.h"
15 struct ldapoptions openldap_ldap_global_options;
18 #define gopts openldap_ldap_global_options
20 int openldap_ldap_initialized = 0;
33 static struct ol_keyvalue deref_kv[] = {
34 {"never", LDAP_DEREF_NEVER},
35 {"searching", LDAP_DEREF_SEARCHING},
36 {"finding", LDAP_DEREF_FINDING},
37 {"always", LDAP_DEREF_ALWAYS},
41 static struct ol_attribute {
47 {ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
48 offsetof(struct ldapoptions, ldo_deref)},
49 {ATTR_INT, "SIZELIMIT", NULL,
50 offsetof(struct ldapoptions, ldo_sizelimit)},
51 {ATTR_INT, "TIMELIMIT", NULL,
52 offsetof(struct ldapoptions, ldo_timelimit)},
53 {ATTR_STRING, "BASE", NULL,
54 offsetof(struct ldapoptions, ldo_defbase)},
55 {ATTR_STRING, "HOST", NULL,
56 offsetof(struct ldapoptions, ldo_defhost)},
57 {ATTR_INT, "PORT", NULL,
58 offsetof(struct ldapoptions, ldo_defport)},
59 {ATTR_BOOL, "REFERRALS", (void *) LDAP_BOOL_REFERRALS, 0},
60 {ATTR_BOOL, "RESTART", (void *) LDAP_BOOL_RESTART, 0},
61 {ATTR_BOOL, "DNS", (void *) LDAP_BOOL_DNS, 0},
62 {ATTR_NONE, NULL, NULL, 0}
65 #define MAX_LDAP_ATTR_LEN sizeof("SIZELIMIT")
66 #define MAX_LDAP_ENV_PREFIX_LEN 8
68 static void openldap_ldap_init_w_conf(const char *file)
76 fp = fopen(file, "r");
78 /* could not open file */
82 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
83 /* skip lines starting with '#' */
84 if(*start == '#') continue;
86 /* trim leading white space */
87 while((*start != '\0') && isspace(*start)) start++;
90 if(*start == '\0') continue;
92 /* trim trailing white space */
93 end = &start[strlen(start)-1];
94 while(isspace(*end)) end--;
98 if(*start == '\0') continue;
101 /* parse the command */
103 while((*start != '\0') && !isspace(*start)) {
107 /* command has no argument */
113 /* we must have some non-whitespace to skip */
114 while(isspace(*start)) start++;
117 for(i=0; attrs[i].type != ATTR_NONE; i++) {
120 if(strcasecmp(cmd, attrs[i].name) != 0) {
124 p = &((char *) &gopts)[attrs[i].offset];
126 switch(attrs[i].type) {
128 if((strcasecmp(opt, "on") == 0)
129 || (strcasecmp(opt, "yes") == 0)
130 || (strcasecmp(opt, "true") == 0))
132 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
135 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
141 * (int*) p = atoi(opt);
145 struct ol_keyvalue *kv;
147 for(kv = (struct ol_keyvalue *) attrs[i].data;
151 if(strcasecmp(opt, kv->key) == 0) {
152 * (int*) p = kv->value;
159 if (* (char**) p != NULL) free(* (char**) p);
160 * (char**) p = strdup(opt);
167 static void openldap_ldap_init_w_userconf(const char *file)
169 char *home = getenv("HOME");
173 path = malloc(strlen(home) + strlen(file) + 3);
175 path = malloc(strlen(file) + 3);
179 if(home != NULL && path != NULL) {
181 sprintf(path, "%s/%s", home, file);
182 openldap_ldap_init_w_conf(path);
185 sprintf(path, "%s/.%s", home, file);
186 openldap_ldap_init_w_conf(path);
190 openldap_ldap_init_w_conf(file);
194 sprintf(path, ".%s", file);
195 openldap_ldap_init_w_conf(path);
199 static void openldap_ldap_init_w_env(const char *prefix)
201 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
207 if (prefix == NULL) {
208 prefix = DEFAULT_LDAP_ENV_PREFIX;
211 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
212 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
215 for(i=0; attrs[i].type != ATTR_NONE; i++) {
216 strcpy(&buf[len], attrs[i].name);
223 p = &((char *) &gopts)[attrs[i].offset];
225 switch(attrs[i].type) {
227 if((strcasecmp(value, "on") == 0)
228 || (strcasecmp(value, "yes") == 0)
229 || (strcasecmp(value, "true") == 0))
231 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
234 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
239 * (int*) p = atoi(value);
243 struct ol_keyvalue *kv;
245 for(kv = (struct ol_keyvalue *) attrs[i].data;
249 if(strcasecmp(value, kv->key) == 0) {
250 * (int*) p = kv->value;
257 if (* (char**) p != NULL) free(* (char**) p);
258 if (*value == '\0') {
261 * (char**) p = strdup(value);
268 void openldap_ldap_initialize( void )
270 if ( openldap_ldap_initialized ) {
274 gopts.ldo_version = LDAP_VERSION2;
275 gopts.ldo_deref = LDAP_DEREF_NEVER;
276 gopts.ldo_timelimit = LDAP_NO_LIMIT;
277 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
279 gopts.ldo_defhost = strdup("localhost");
280 gopts.ldo_defport = LDAP_PORT;
282 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
284 LDAP_BOOL_ZERO(&gopts);
286 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
287 LDAP_VERSION_MAX > LDAP_VERSION2
288 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
291 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
292 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
293 openldap_ldap_init_w_env(NULL);
295 openldap_ldap_initialized = 1;