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 ldap_int_global_options =
19 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };
22 #define gopts ldap_int_global_options
35 static const struct ol_keyvalue deref_kv[] = {
36 {"never", LDAP_DEREF_NEVER},
37 {"searching", LDAP_DEREF_SEARCHING},
38 {"finding", LDAP_DEREF_FINDING},
39 {"always", LDAP_DEREF_ALWAYS},
43 static const struct ol_attribute {
49 {ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
50 offsetof(struct ldapoptions, ldo_deref)},
51 {ATTR_INT, "SIZELIMIT", NULL,
52 offsetof(struct ldapoptions, ldo_sizelimit)},
53 {ATTR_INT, "TIMELIMIT", NULL,
54 offsetof(struct ldapoptions, ldo_timelimit)},
55 {ATTR_STRING, "BASE", NULL,
56 offsetof(struct ldapoptions, ldo_defbase)},
57 {ATTR_STRING, "HOST", NULL,
58 offsetof(struct ldapoptions, ldo_defhost)},
59 {ATTR_INT, "PORT", NULL,
60 offsetof(struct ldapoptions, ldo_defport)},
61 {ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
62 {ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
63 {ATTR_BOOL, "DNS", NULL, LDAP_BOOL_DNS},
64 {ATTR_NONE, NULL, NULL, 0}
67 #define MAX_LDAP_ATTR_LEN sizeof("SIZELIMIT")
68 #define MAX_LDAP_ENV_PREFIX_LEN 8
70 static void openldap_ldap_init_w_conf(const char *file)
83 fp = fopen(file, "r");
85 /* could not open file */
89 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
90 /* skip lines starting with '#' */
91 if(*start == '#') continue;
93 /* trim leading white space */
94 while((*start != '\0') && isspace((unsigned char) *start))
98 if(*start == '\0') continue;
100 /* trim trailing white space */
101 end = &start[strlen(start)-1];
102 while(isspace((unsigned char)*end)) end--;
106 if(*start == '\0') continue;
109 /* parse the command */
111 while((*start != '\0') && !isspace((unsigned char)*start)) {
115 /* command has no argument */
121 /* we must have some non-whitespace to skip */
122 while(isspace((unsigned char)*start)) start++;
125 for(i=0; attrs[i].type != ATTR_NONE; i++) {
128 if(strcasecmp(cmd, attrs[i].name) != 0) {
132 switch(attrs[i].type) {
134 if((strcasecmp(opt, "on") == 0)
135 || (strcasecmp(opt, "yes") == 0)
136 || (strcasecmp(opt, "true") == 0))
138 LDAP_BOOL_SET(&gopts, attrs[i].offset);
141 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
147 p = &((char *) &gopts)[attrs[i].offset];
148 * (int*) p = atoi(opt);
152 const struct ol_keyvalue *kv;
154 for(kv = attrs[i].data;
158 if(strcasecmp(opt, kv->key) == 0) {
159 p = &((char *) &gopts)[attrs[i].offset];
160 * (int*) p = kv->value;
167 p = &((char *) &gopts)[attrs[i].offset];
168 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
169 * (char**) p = LDAP_STRDUP(opt);
178 static void openldap_ldap_init_w_userconf(const char *file)
188 home = getenv("HOME");
191 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
193 path = LDAP_MALLOC(strlen(file) + 3);
196 if(home != NULL && path != NULL) {
197 /* we assume UNIX path syntax is used... */
200 sprintf(path, "%s/%s", home, file);
201 openldap_ldap_init_w_conf(path);
204 sprintf(path, "%s/.%s", home, file);
205 openldap_ldap_init_w_conf(path);
213 openldap_ldap_init_w_conf(file);
216 static void openldap_ldap_init_w_env(const char *prefix)
218 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
224 if (prefix == NULL) {
225 prefix = DEFAULT_LDAP_ENV_PREFIX;
228 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
229 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
232 for(i=0; attrs[i].type != ATTR_NONE; i++) {
233 strcpy(&buf[len], attrs[i].name);
240 switch(attrs[i].type) {
242 if((strcasecmp(value, "on") == 0)
243 || (strcasecmp(value, "yes") == 0)
244 || (strcasecmp(value, "true") == 0))
246 LDAP_BOOL_SET(&gopts, attrs[i].offset);
249 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
254 p = &((char *) &gopts)[attrs[i].offset];
255 * (int*) p = atoi(value);
259 const struct ol_keyvalue *kv;
261 for(kv = attrs[i].data;
265 if(strcasecmp(value, kv->key) == 0) {
266 p = &((char *) &gopts)[attrs[i].offset];
267 * (int*) p = kv->value;
274 p = &((char *) &gopts)[attrs[i].offset];
275 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
276 if (*value == '\0') {
279 * (char**) p = LDAP_STRDUP(value);
286 void ldap_int_initialize( void )
288 if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
292 ldap_int_utils_init();
294 gopts.ldo_version = LDAP_VERSION2;
295 gopts.ldo_deref = LDAP_DEREF_NEVER;
296 gopts.ldo_timelimit = LDAP_NO_LIMIT;
297 gopts.ldo_sizelimit = LDAP_NO_LIMIT;
301 gopts.ldo_defhost = LDAP_STRDUP("localhost");
302 gopts.ldo_defport = LDAP_PORT;
304 gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
306 LDAP_BOOL_ZERO(&gopts);
308 LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
310 gopts.ldo_valid = LDAP_INITIALIZED;
312 if( getenv("LDAPNOINIT") != NULL ) {
316 openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
317 openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
320 char *altfile = getenv("LDAPCONF");
322 if( altfile != NULL ) {
323 openldap_ldap_init_w_conf( altfile );
328 char *altfile = getenv("LDAPRC");
330 if( altfile != NULL ) {
331 openldap_ldap_init_w_userconf( altfile );
335 openldap_ldap_init_w_env(NULL);