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 const 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 const 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 const struct ol_keyvalue *kv;
155 for(kv = 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);
179 static void openldap_ldap_init_w_userconf(const char *file)
189 home = getenv("HOME");
192 path = malloc(strlen(home) + strlen(file) + 3);
194 path = malloc(strlen(file) + 3);
197 if(home != NULL && path != NULL) {
198 /* we assume UNIX path syntax is used... */
201 sprintf(path, "%s/%s", home, file);
202 openldap_ldap_init_w_conf(path);
205 sprintf(path, "%s/.%s", home, file);
206 openldap_ldap_init_w_conf(path);
214 openldap_ldap_init_w_conf(file);
217 static void openldap_ldap_init_w_env(const char *prefix)
219 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
225 if (prefix == NULL) {
226 prefix = DEFAULT_LDAP_ENV_PREFIX;
229 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
230 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
233 for(i=0; attrs[i].type != ATTR_NONE; i++) {
234 strcpy(&buf[len], attrs[i].name);
241 switch(attrs[i].type) {
243 if((strcasecmp(value, "on") == 0)
244 || (strcasecmp(value, "yes") == 0)
245 || (strcasecmp(value, "true") == 0))
247 LDAP_BOOL_SET(&gopts, attrs[i].offset);
250 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
255 p = &((char *) &gopts)[attrs[i].offset];
256 * (int*) p = atoi(value);
260 const struct ol_keyvalue *kv;
262 for(kv = attrs[i].data;
266 if(strcasecmp(value, kv->key) == 0) {
267 p = &((char *) &gopts)[attrs[i].offset];
268 * (int*) p = kv->value;
275 p = &((char *) &gopts)[attrs[i].offset];
276 if (* (char**) p != NULL) free(* (char**) p);
277 if (*value == '\0') {
280 * (char**) p = strdup(value);
287 void openldap_ldap_initialize( void )
289 if ( openldap_ldap_initialized ) {
293 ldap_int_utils_init();
295 gopts.ldo_version = LDAP_VERSION2;
296 gopts.ldo_deref = LDAP_DEREF_NEVER;
297 gopts.ldo_timelimit = LDAP_NO_LIMIT;
298 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
302 gopts.ldo_defhost = strdup("localhost");
303 gopts.ldo_defport = LDAP_PORT;
305 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
307 LDAP_BOOL_ZERO(&gopts);
309 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
311 openldap_ldap_initialized = 1;
313 if( getenv("LDAPNOINIT") != NULL ) {
317 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
318 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
321 char *altfile = getenv("LDAPCONF");
323 if( altfile != NULL ) {
324 openldap_ldap_init_w_conf( altfile );
329 char *altfile = getenv("LDAPRC");
331 if( altfile != NULL ) {
332 openldap_ldap_init_w_userconf( altfile );
336 openldap_ldap_init_w_env(NULL);