]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
4e45c7554ad7bbce9311d9ab2f3eb29ac6f78807
[openldap] / libraries / libldap / init.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <stdio.h>
8 #include <ac/stdlib.h>
9
10 #include <ac/socket.h>
11 #include <ac/string.h>
12 #include <ac/ctype.h>
13 #include <ac/time.h>
14
15 #include "ldap-int.h"
16 #include "ldap_defaults.h"
17
18 struct ldapoptions ldap_int_global_options =
19         { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
20
21 #undef gopts
22 #define gopts ldap_int_global_options
23
24 #define ATTR_NONE       0
25 #define ATTR_BOOL       1
26 #define ATTR_INT        2
27 #define ATTR_KV         3
28 #define ATTR_STRING     4
29
30 struct ol_keyvalue {
31         const char *            key;
32         int                     value;
33 };
34
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},
40         {NULL, 0}
41 };
42
43 static const struct ol_attribute {
44         int                     type;
45         const char *    name;
46         const void *    data;
47         size_t          offset;
48 } attrs[] = {
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}
65 };
66
67 #define MAX_LDAP_ATTR_LEN  sizeof("SIZELIMIT")
68 #define MAX_LDAP_ENV_PREFIX_LEN 8
69
70 static void openldap_ldap_init_w_conf(const char *file)
71 {
72         char linebuf[128];
73         FILE *fp;
74         int i;
75         char *cmd, *opt;
76         char *start, *end;
77
78         if (file == NULL) {
79                 /* no file name */
80                 return;
81         }
82
83         fp = fopen(file, "r");
84         if(fp == NULL) {
85                 /* could not open file */
86                 return;
87         }
88
89         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
90                 /* skip lines starting with '#' */
91                 if(*start == '#') continue;
92
93                 /* trim leading white space */
94                 while((*start != '\0') && isspace((unsigned char) *start))
95                         start++;
96
97                 /* anything left? */
98                 if(*start == '\0') continue;
99
100                 /* trim trailing white space */
101                 end = &start[strlen(start)-1];
102                 while(isspace((unsigned char)*end)) end--;
103                 end[1] = '\0';
104
105                 /* anything left? */
106                 if(*start == '\0') continue;
107                 
108
109                 /* parse the command */
110                 cmd=start;
111                 while((*start != '\0') && !isspace((unsigned char)*start)) {
112                         start++;
113                 }
114                 if(*start == '\0') {
115                         /* command has no argument */
116                         continue;
117                 } 
118
119                 *start++ = '\0';
120
121                 /* we must have some non-whitespace to skip */
122                 while(isspace((unsigned char)*start)) start++;
123                 opt = start;
124
125                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
126                         void *p;
127
128                         if(strcasecmp(cmd, attrs[i].name) != 0) {
129                                 continue;
130                         }
131
132                         switch(attrs[i].type) {
133                         case ATTR_BOOL:
134                                 if((strcasecmp(opt, "on") == 0) 
135                                         || (strcasecmp(opt, "yes") == 0)
136                                         || (strcasecmp(opt, "true") == 0))
137                                 {
138                                         LDAP_BOOL_SET(&gopts, attrs[i].offset);
139
140                                 } else {
141                                         LDAP_BOOL_CLR(&gopts, attrs[i].offset);
142                                 }
143
144                                 break;
145
146                         case ATTR_INT:
147                                 p = &((char *) &gopts)[attrs[i].offset];
148                                 * (int*) p = atoi(opt);
149                                 break;
150
151                         case ATTR_KV: {
152                                         const struct ol_keyvalue *kv;
153
154                                         for(kv = attrs[i].data;
155                                                 kv->key != NULL;
156                                                 kv++) {
157
158                                                 if(strcasecmp(opt, kv->key) == 0) {
159                                                         p = &((char *) &gopts)[attrs[i].offset];
160                                                         * (int*) p = kv->value;
161                                                         break;
162                                                 }
163                                         }
164                                 } break;
165
166                         case ATTR_STRING:
167                                 p = &((char *) &gopts)[attrs[i].offset];
168                                 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
169                                 * (char**) p = LDAP_STRDUP(opt);
170                                 break;
171                         }
172                 }
173         }
174
175         fclose(fp);
176 }
177
178 static void openldap_ldap_init_w_userconf(const char *file)
179 {
180         char *home;
181         char *path;
182
183         if (file == NULL) {
184                 /* no file name */
185                 return;
186         }
187
188         home = getenv("HOME");
189
190         if (home != NULL) {
191                 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
192         } else {
193                 path = LDAP_MALLOC(strlen(file) + 3);
194         }
195
196         if(home != NULL && path != NULL) {
197                 /* we assume UNIX path syntax is used... */
198
199                 /* try ~/file */
200                 sprintf(path, "%s/%s", home, file);
201                 openldap_ldap_init_w_conf(path);
202
203                 /* try ~/.file */
204                 sprintf(path, "%s/.%s", home, file);
205                 openldap_ldap_init_w_conf(path);
206         }
207
208         if(path != NULL) {
209                 LDAP_FREE(path);
210         }
211
212         /* try file */
213         openldap_ldap_init_w_conf(file);
214 }
215
216 static void openldap_ldap_init_w_env(const char *prefix)
217 {
218         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
219         int len;
220         int i;
221         void *p;
222         char *value;
223
224         if (prefix == NULL) {
225                 prefix = LDAP_ENV_PREFIX;
226         }
227
228         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
229         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
230         len = strlen(buf);
231
232         for(i=0; attrs[i].type != ATTR_NONE; i++) {
233                 strcpy(&buf[len], attrs[i].name);
234                 value = getenv(buf);
235
236                 if(value == NULL) {
237                         continue;
238                 }
239
240                 switch(attrs[i].type) {
241                 case ATTR_BOOL:
242                         if((strcasecmp(value, "on") == 0) 
243                                 || (strcasecmp(value, "yes") == 0)
244                                 || (strcasecmp(value, "true") == 0))
245                         {
246                                 LDAP_BOOL_SET(&gopts, attrs[i].offset);
247
248                         } else {
249                                 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
250                         }
251                         break;
252
253                 case ATTR_INT:
254                         p = &((char *) &gopts)[attrs[i].offset];
255                         * (int*) p = atoi(value);
256                         break;
257
258                 case ATTR_KV: {
259                                 const struct ol_keyvalue *kv;
260
261                                 for(kv = attrs[i].data;
262                                         kv->key != NULL;
263                                         kv++) {
264
265                                         if(strcasecmp(value, kv->key) == 0) {
266                                                 p = &((char *) &gopts)[attrs[i].offset];
267                                                 * (int*) p = kv->value;
268                                                 break;
269                                         }
270                                 }
271                         } break;
272
273                 case ATTR_STRING:
274                         p = &((char *) &gopts)[attrs[i].offset];
275                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
276                         if (*value == '\0') {
277                                 * (char**) p = NULL;
278                         } else {
279                                 * (char**) p = LDAP_STRDUP(value);
280                         }
281                         break;
282                 }
283         }
284 }
285
286 void ldap_int_initialize( void )
287 {
288         if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
289                 return;
290         }
291
292         ldap_int_utils_init();
293         if ( ldap_int_tblsize == 0 )
294                 ldap_int_ip_init();
295
296         gopts.ldo_version =     LDAP_VERSION2;
297         gopts.ldo_deref =       LDAP_DEREF_NEVER;
298         gopts.ldo_timelimit = LDAP_NO_LIMIT;
299         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
300
301         gopts.ldo_debug = 0;
302
303         gopts.ldo_defhost = LDAP_STRDUP("localhost");
304         gopts.ldo_defport = LDAP_PORT;
305
306         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
307
308         LDAP_BOOL_ZERO(&gopts);
309
310         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
311
312         gopts.ldo_valid = LDAP_INITIALIZED;
313
314         if( getenv("LDAPNOINIT") != NULL ) {
315                 return;
316         }
317
318         openldap_ldap_init_w_conf(LDAP_CONF_FILE);
319         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
320
321         {
322                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
323
324                 if( altfile != NULL ) {
325                         openldap_ldap_init_w_conf( altfile );
326                 }
327         }
328
329         {
330                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
331
332                 if( altfile != NULL ) {
333                         openldap_ldap_init_w_userconf( altfile );
334                 }
335         }
336
337         openldap_ldap_init_w_env(NULL);
338 }