12 #include "ldapconfig.h"
14 struct ldapoptions openldap_ldap_global_options;
17 #define gopts openldap_ldap_global_options
19 int openldap_ldap_initialized = 0;
32 static struct ol_keyvalue deref_kv[] = {
33 {"never", LDAP_DEREF_NEVER},
34 {"searching", LDAP_DEREF_SEARCHING},
35 {"finding", LDAP_DEREF_FINDING},
36 {"always", LDAP_DEREF_ALWAYS},
40 static struct ol_attribute {
46 {ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
47 offsetof(struct ldapoptions, ldo_deref)},
48 {ATTR_INT, "SIZELIMIT", NULL,
49 offsetof(struct ldapoptions, ldo_sizelimit)},
50 {ATTR_INT, "TIMELIMIT", NULL,
51 offsetof(struct ldapoptions, ldo_timelimit)},
52 {ATTR_STRING, "BASE", NULL,
53 offsetof(struct ldapoptions, ldo_defbase)},
54 {ATTR_STRING, "HOST", NULL,
55 offsetof(struct ldapoptions, ldo_defhost)},
56 {ATTR_INT, "PORT", NULL,
57 offsetof(struct ldapoptions, ldo_defport)},
58 {ATTR_BOOL, "REFERRALS", (void *) LDAP_BOOL_REFERRALS, 0},
59 {ATTR_BOOL, "RESTART", (void *) LDAP_BOOL_RESTART, 0},
60 {ATTR_BOOL, "DNS", (void *) LDAP_BOOL_DNS, 0},
61 {ATTR_NONE, NULL, NULL, 0}
64 #define MAX_LDAP_ATTR_LEN sizeof("SIZELIMIT")
65 #define MAX_LDAP_ENV_PREFIX_LEN 8
67 static void openldap_ldap_init_w_conf(const char *file)
80 fp = fopen(file, "r");
82 /* could not open file */
86 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
87 /* skip lines starting with '#' */
88 if(*start == '#') continue;
90 /* trim leading white space */
91 while((*start != '\0') && isspace(*start)) start++;
94 if(*start == '\0') continue;
96 /* trim trailing white space */
97 end = &start[strlen(start)-1];
98 while(isspace(*end)) end--;
102 if(*start == '\0') continue;
105 /* parse the command */
107 while((*start != '\0') && !isspace(*start)) {
111 /* command has no argument */
117 /* we must have some non-whitespace to skip */
118 while(isspace(*start)) start++;
121 for(i=0; attrs[i].type != ATTR_NONE; i++) {
124 if(strcasecmp(cmd, attrs[i].name) != 0) {
128 p = &((char *) &gopts)[attrs[i].offset];
130 switch(attrs[i].type) {
132 if((strcasecmp(opt, "on") == 0)
133 || (strcasecmp(opt, "yes") == 0)
134 || (strcasecmp(opt, "true") == 0))
136 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
139 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
145 * (int*) p = atoi(opt);
149 struct ol_keyvalue *kv;
151 for(kv = (struct ol_keyvalue *) attrs[i].data;
155 if(strcasecmp(opt, kv->key) == 0) {
156 * (int*) p = kv->value;
163 if (* (char**) p != NULL) free(* (char**) p);
164 * (char**) p = ldap_strdup(opt);
171 static void openldap_ldap_init_w_userconf(const char *file)
181 home = getenv("HOME");
184 path = malloc(strlen(home) + strlen(file) + 3);
186 path = malloc(strlen(file) + 3);
189 if(home != NULL && path != NULL) {
190 /* we assume UNIX path syntax is used... */
193 sprintf(path, "%s/%s", home, file);
194 openldap_ldap_init_w_conf(path);
197 sprintf(path, "%s/.%s", home, file);
198 openldap_ldap_init_w_conf(path);
206 openldap_ldap_init_w_conf(file);
209 static void openldap_ldap_init_w_env(const char *prefix)
211 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
217 if (prefix == NULL) {
218 prefix = DEFAULT_LDAP_ENV_PREFIX;
221 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
222 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
225 for(i=0; attrs[i].type != ATTR_NONE; i++) {
226 strcpy(&buf[len], attrs[i].name);
233 p = &((char *) &gopts)[attrs[i].offset];
235 switch(attrs[i].type) {
237 if((strcasecmp(value, "on") == 0)
238 || (strcasecmp(value, "yes") == 0)
239 || (strcasecmp(value, "true") == 0))
241 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
244 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
249 * (int*) p = atoi(value);
253 struct ol_keyvalue *kv;
255 for(kv = (struct ol_keyvalue *) attrs[i].data;
259 if(strcasecmp(value, kv->key) == 0) {
260 * (int*) p = kv->value;
267 if (* (char**) p != NULL) free(* (char**) p);
268 if (*value == '\0') {
271 * (char**) p = ldap_strdup(value);
278 void openldap_ldap_initialize( void )
280 if ( openldap_ldap_initialized ) {
284 gopts.ldo_version = LDAP_VERSION2;
285 gopts.ldo_deref = LDAP_DEREF_NEVER;
286 gopts.ldo_timelimit = LDAP_NO_LIMIT;
287 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
291 gopts.ldo_defhost = ldap_strdup("localhost");
292 gopts.ldo_defport = LDAP_PORT;
294 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
296 LDAP_BOOL_ZERO(&gopts);
298 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
299 LDAP_VERSION_MAX > LDAP_VERSION2
300 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
303 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
304 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
307 char *altfile = getenv("LDAPRC");
309 if( altfile != NULL ) {
310 openldap_ldap_init_w_conf( altfile );
314 openldap_ldap_init_w_env(NULL);
316 openldap_ldap_initialized = 1;