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)
75 fp = fopen(file, "r");
77 /* could not open file */
81 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
82 /* skip lines starting with '#' */
83 if(*start == '#') continue;
85 /* trim leading white space */
86 while((*start != '\0') && isspace(*start)) start++;
89 if(*start == '\0') continue;
91 /* trim trailing white space */
92 end = &start[strlen(start)-1];
93 while(isspace(*end)) end--;
97 if(*start == '\0') continue;
100 /* parse the command */
102 while((*start != '\0') && !isspace(*start)) {
106 /* command has no argument */
112 /* we must have some non-whitespace to skip */
113 while(isspace(*start)) start++;
116 for(i=0; attrs[i].type != ATTR_NONE; i++) {
119 if(strcasecmp(cmd, attrs[i].name) != 0) {
123 p = &((char *) &gopts)[attrs[i].offset];
125 switch(attrs[i].type) {
127 if((strcasecmp(opt, "on") == 0)
128 || (strcasecmp(opt, "yes") == 0)
129 || (strcasecmp(opt, "true") == 0))
131 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
134 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
140 * (int*) p = atoi(opt);
144 struct ol_keyvalue *kv;
146 for(kv = (struct ol_keyvalue *) attrs[i].data;
150 if(strcasecmp(opt, kv->key) == 0) {
151 * (int*) p = kv->value;
158 if (* (char**) p != NULL) free(* (char**) p);
159 * (char**) p = ldap_strdup(opt);
166 static void openldap_ldap_init_w_userconf(const char *file)
168 char *home = getenv("HOME");
172 path = malloc(strlen(home) + strlen(file) + 3);
174 path = malloc(strlen(file) + 3);
178 if(home != NULL && path != NULL) {
180 sprintf(path, "%s/%s", home, file);
181 openldap_ldap_init_w_conf(path);
184 sprintf(path, "%s/.%s", home, file);
185 openldap_ldap_init_w_conf(path);
189 openldap_ldap_init_w_conf(file);
192 static void openldap_ldap_init_w_env(const char *prefix)
194 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
200 if (prefix == NULL) {
201 prefix = DEFAULT_LDAP_ENV_PREFIX;
204 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
205 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
208 for(i=0; attrs[i].type != ATTR_NONE; i++) {
209 strcpy(&buf[len], attrs[i].name);
216 p = &((char *) &gopts)[attrs[i].offset];
218 switch(attrs[i].type) {
220 if((strcasecmp(value, "on") == 0)
221 || (strcasecmp(value, "yes") == 0)
222 || (strcasecmp(value, "true") == 0))
224 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
227 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
232 * (int*) p = atoi(value);
236 struct ol_keyvalue *kv;
238 for(kv = (struct ol_keyvalue *) attrs[i].data;
242 if(strcasecmp(value, kv->key) == 0) {
243 * (int*) p = kv->value;
250 if (* (char**) p != NULL) free(* (char**) p);
251 if (*value == '\0') {
254 * (char**) p = ldap_strdup(value);
261 void openldap_ldap_initialize( void )
263 if ( openldap_ldap_initialized ) {
267 gopts.ldo_version = LDAP_VERSION2;
268 gopts.ldo_deref = LDAP_DEREF_NEVER;
269 gopts.ldo_timelimit = LDAP_NO_LIMIT;
270 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
272 gopts.ldo_defhost = ldap_strdup("localhost");
273 gopts.ldo_defport = LDAP_PORT;
275 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
277 LDAP_BOOL_ZERO(&gopts);
279 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
280 LDAP_VERSION_MAX > LDAP_VERSION2
281 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
284 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
285 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
288 char *altfile = getenv("LDAPRC");
290 if( altfile != NULL ) {
291 openldap_ldap_init_w_conf( altfile );
295 openldap_ldap_init_w_env(NULL);
297 openldap_ldap_initialized = 1;