11 #include "ldapconfig.h"
13 struct ldapoptions openldap_ldap_global_options;
16 #define gopts openldap_ldap_global_options
18 int openldap_ldap_initialized = 0;
31 struct ol_keyvalue deref_kv[] = {
32 {"never", LDAP_DEREF_NEVER},
33 {"searching", LDAP_DEREF_SEARCHING},
34 {"finding", LDAP_DEREF_FINDING},
35 {"always", LDAP_DEREF_ALWAYS},
45 {ATTR_KV, "DEREF", &deref_kv,
46 offsetof(struct ldapoptions, ldo_deref)},
47 {ATTR_INT, "SIZELIMIT", NULL,
48 offsetof(struct ldapoptions, ldo_sizelimit)},
49 {ATTR_INT, "TIMELIMIT", NULL,
50 offsetof(struct ldapoptions, ldo_timelimit)},
51 {ATTR_STRING, "BASE", NULL,
52 offsetof(struct ldapoptions, ldo_defbase)},
53 {ATTR_STRING, "HOST", NULL,
54 offsetof(struct ldapoptions, ldo_defhost)},
55 {ATTR_INT, "PORT", NULL,
56 offsetof(struct ldapoptions, ldo_defport)},
57 {ATTR_BOOL, "REFERRALS", (void *) LDAP_BOOL_REFERRALS, 0},
58 {ATTR_BOOL, "RESTART", (void *) LDAP_BOOL_RESTART, 0},
59 {ATTR_BOOL, "DNS", (void *) LDAP_BOOL_DNS, 0},
63 #define MAX_LDAP_ATTR_LEN sizeof("SIZELIMIT")
64 #define MAX_LDAP_ENV_PREFIX_LEN 8
66 static void openldap_ldap_init_w_conf(const char *file)
74 fp = fopen(file, "r");
76 /* could not open file */
80 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
81 /* skip lines starting with '#' */
82 if(*start == '#') continue;
84 /* trim leading white space */
85 while((*start != '\0') && isspace(*start)) start++;
88 if(*start == '\0') continue;
90 /* trim trailing white space */
91 end = &start[strlen(start)-1];
92 while(isspace(*end)) end--;
96 if(*start == '\0') continue;
99 /* parse the command */
101 while((*start != '\0') && !isspace(*start)) {
105 /* command has no argument */
111 /* we must have some non-whitespace to skip */
112 while(isspace(*start)) start++;
115 for(i=0; attrs[i].type != ATTR_NONE; i++) {
118 if(strcasecmp(cmd, attrs[i].name) != 0) {
122 p = &((char *) &gopts)[attrs[i].offset];
124 switch(attrs[i].type) {
126 if((strcasecmp(opt, "on") == 0)
127 || (strcasecmp(opt, "yes") == 0)
128 || (strcasecmp(opt, "true") == 0))
130 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
133 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
139 * (int*) p = atoi(opt);
143 struct ol_keyvalue *kv;
145 for(kv = (struct ol_keyvalue *) attrs[i].data;
149 if(strcasecmp(opt, kv->key) == 0) {
150 * (int*) p = kv->value;
157 if (* (char**) p != NULL) free(* (char**) p);
158 * (char**) p = strdup(opt);
165 static void openldap_ldap_init_w_userconf(const char *file)
167 char *home = getenv("HOME");
171 path = malloc(strlen(home) + strlen(file) + 3);
173 path = malloc(strlen(file) + 3);
177 if(home != NULL && path != NULL) {
179 sprintf(path, "%s/%s", home, file);
180 openldap_ldap_init_w_conf(path);
183 sprintf(path, "%s/.%s", home, file);
184 openldap_ldap_init_w_conf(path);
188 openldap_ldap_init_w_conf(file);
192 sprintf(path, ".%s", file);
193 openldap_ldap_init_w_conf(path);
197 static void openldap_ldap_init_w_env(const char *prefix)
199 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
205 if (prefix == NULL) {
206 prefix = DEFAULT_LDAP_ENV_PREFIX;
209 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
210 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
213 for(i=0; attrs[i].type != ATTR_NONE; i++) {
214 strcpy(&buf[len], attrs[i].name);
221 p = &((char *) &gopts)[attrs[i].offset];
223 switch(attrs[i].type) {
225 if((strcasecmp(value, "on") == 0)
226 || (strcasecmp(value, "yes") == 0)
227 || (strcasecmp(value, "true") == 0))
229 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
232 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
237 * (int*) p = atoi(value);
241 struct ol_keyvalue *kv;
243 for(kv = (struct ol_keyvalue *) attrs[i].data;
247 if(strcasecmp(value, kv->key) == 0) {
248 * (int*) p = kv->value;
255 if (* (char**) p != NULL) free(* (char**) p);
256 if (*value == '\0') {
259 * (char**) p = strdup(value);
266 void openldap_ldap_initialize( void )
268 if ( openldap_ldap_initialized ) {
272 gopts.ldo_version = LDAP_VERSION2;
273 gopts.ldo_deref = LDAP_DEREF_NEVER;
274 gopts.ldo_timelimit = LDAP_NO_LIMIT;
275 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
277 gopts.ldo_defhost = strdup("localhost");
278 gopts.ldo_defport = LDAP_PORT;
280 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
282 LDAP_BOOL_ZERO(&gopts);
284 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
285 LDAP_VERSION_MAX > LDAP_VERSION2
286 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
289 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
290 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
291 openldap_ldap_init_w_env(NULL);
293 openldap_ldap_initialized = 1;