]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
ITS#399: timelimit/timeout handling
[openldap] / libraries / libldap / init.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9 #include <ac/stdlib.h>
10
11 #include <ac/socket.h>
12 #include <ac/string.h>
13 #include <ac/ctype.h>
14 #include <ac/time.h>
15
16 #include "ldap-int.h"
17 #include "ldap_defaults.h"
18
19 struct ldapoptions ldap_int_global_options =
20         { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
21
22 #undef gopts
23 #define gopts ldap_int_global_options
24
25 #define ATTR_NONE       0
26 #define ATTR_BOOL       1
27 #define ATTR_INT        2
28 #define ATTR_KV         3
29 #define ATTR_STRING     4
30 #define ATTR_TLS        5
31 #define ATTR_URIS       6
32
33 struct ol_keyvalue {
34         const char *            key;
35         int                     value;
36 };
37
38 static const struct ol_keyvalue deref_kv[] = {
39         {"never", LDAP_DEREF_NEVER},
40         {"searching", LDAP_DEREF_SEARCHING},
41         {"finding", LDAP_DEREF_FINDING},
42         {"always", LDAP_DEREF_ALWAYS},
43         {NULL, 0}
44 };
45
46 static const struct ol_attribute {
47         int                     useronly;
48         int                     type;
49         const char *    name;
50         const void *    data;
51         size_t          offset;
52 } attrs[] = {
53         {0, ATTR_KV,            "DEREF",        deref_kv, /* or &deref_kv[0] */
54                 offsetof(struct ldapoptions, ldo_deref)},
55         {0, ATTR_INT,           "SIZELIMIT",    NULL,
56                 offsetof(struct ldapoptions, ldo_sizelimit)},
57         {0, ATTR_INT,           "TIMELIMIT",    NULL,
58                 offsetof(struct ldapoptions, ldo_timelimit)},
59         {0, ATTR_STRING,        "BASE",                 NULL,
60                 offsetof(struct ldapoptions, ldo_defbase)},
61         {0, ATTR_INT,           "PORT",                 NULL,
62                 offsetof(struct ldapoptions, ldo_defport)},
63         /* **** keep this around for backward compatibility */
64         {0, ATTR_URIS,          "HOST",                 NULL,   1},
65         /* **** */
66         {0, ATTR_URIS,          "URI",                  NULL,   0},
67         {0, ATTR_BOOL,          "REFERRALS",    NULL,   LDAP_BOOL_REFERRALS},
68         {0, ATTR_BOOL,          "RESTART",              NULL,   LDAP_BOOL_RESTART},
69         {0, ATTR_BOOL,          "DNS",                  NULL,   LDAP_BOOL_DNS},
70         {0, ATTR_TLS,           "TLS",                  NULL,   LDAP_OPT_X_TLS},
71         {0, ATTR_TLS,           "TLS_CERT",             NULL,   LDAP_OPT_X_TLS_CERTFILE},
72         {0, ATTR_TLS,           "TLS_KEY",              NULL,   LDAP_OPT_X_TLS_KEYFILE},
73         {0, ATTR_TLS,           "TLS_CACERT",   NULL,   LDAP_OPT_X_TLS_CACERTFILE},
74         {0, ATTR_TLS,           "TLS_CACERTDIR",NULL,   LDAP_OPT_X_TLS_CACERTDIR},
75         {0, ATTR_TLS,           "TLS_REQCERT",  NULL,   LDAP_OPT_X_TLS_REQUIRE_CERT},
76         {0, ATTR_NONE,          NULL,           NULL,   0}
77 };
78
79 #define MAX_LDAP_ATTR_LEN  sizeof("TLS_CACERTDIR")
80 #define MAX_LDAP_ENV_PREFIX_LEN 8
81
82 static void openldap_ldap_init_w_conf(
83         const char *file, int userconf )
84 {
85         char linebuf[128];
86         FILE *fp;
87         int i;
88         char *cmd, *opt;
89         char *start, *end;
90
91         if (file == NULL) {
92                 /* no file name */
93                 return;
94         }
95
96         fp = fopen(file, "r");
97         if(fp == NULL) {
98                 /* could not open file */
99                 return;
100         }
101
102         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
103                 /* skip lines starting with '#' */
104                 if(*start == '#') continue;
105
106                 /* trim leading white space */
107                 while((*start != '\0') && isspace((unsigned char) *start))
108                         start++;
109
110                 /* anything left? */
111                 if(*start == '\0') continue;
112
113                 /* trim trailing white space */
114                 end = &start[strlen(start)-1];
115                 while(isspace((unsigned char)*end)) end--;
116                 end[1] = '\0';
117
118                 /* anything left? */
119                 if(*start == '\0') continue;
120                 
121
122                 /* parse the command */
123                 cmd=start;
124                 while((*start != '\0') && !isspace((unsigned char)*start)) {
125                         start++;
126                 }
127                 if(*start == '\0') {
128                         /* command has no argument */
129                         continue;
130                 } 
131
132                 *start++ = '\0';
133
134                 /* we must have some non-whitespace to skip */
135                 while(isspace((unsigned char)*start)) start++;
136                 opt = start;
137
138                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
139                         void *p;
140
141                         if( !userconf && !attrs[i].useronly ) {
142                                 continue;
143                         }
144
145                         if(strcasecmp(cmd, attrs[i].name) != 0) {
146                                 continue;
147                         }
148
149                         switch(attrs[i].type) {
150                         case ATTR_BOOL:
151                                 if((strcasecmp(opt, "on") == 0) 
152                                         || (strcasecmp(opt, "yes") == 0)
153                                         || (strcasecmp(opt, "true") == 0))
154                                 {
155                                         LDAP_BOOL_SET(&gopts, attrs[i].offset);
156
157                                 } else {
158                                         LDAP_BOOL_CLR(&gopts, attrs[i].offset);
159                                 }
160
161                                 break;
162
163                         case ATTR_INT:
164                                 p = &((char *) &gopts)[attrs[i].offset];
165                                 * (int*) p = atoi(opt);
166                                 break;
167
168                         case ATTR_KV: {
169                                         const struct ol_keyvalue *kv;
170
171                                         for(kv = attrs[i].data;
172                                                 kv->key != NULL;
173                                                 kv++) {
174
175                                                 if(strcasecmp(opt, kv->key) == 0) {
176                                                         p = &((char *) &gopts)[attrs[i].offset];
177                                                         * (int*) p = kv->value;
178                                                         break;
179                                                 }
180                                         }
181                                 } break;
182
183                         case ATTR_STRING:
184                                 p = &((char *) &gopts)[attrs[i].offset];
185                                 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
186                                 * (char**) p = LDAP_STRDUP(opt);
187                                 break;
188                         case ATTR_TLS:
189 #ifdef HAVE_TLS
190                                 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
191 #endif
192                                 break;
193                         case ATTR_URIS:
194                                 if (attrs[i].offset == 0) {
195                                         ldap_set_option( NULL, LDAP_OPT_URI, opt );
196                                 } else {
197                                         ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
198                                 }
199                                 break;
200                         }
201                 }
202         }
203
204         fclose(fp);
205 }
206
207 static void openldap_ldap_init_w_sysconf(const char *file)
208 {
209         openldap_ldap_init_w_conf( file, 0 );
210 }
211
212 static void openldap_ldap_init_w_userconf(const char *file)
213 {
214         char *home;
215         char *path;
216
217         if (file == NULL) {
218                 /* no file name */
219                 return;
220         }
221
222         home = getenv("HOME");
223
224         if (home != NULL) {
225                 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
226         } else {
227                 path = LDAP_MALLOC(strlen(file) + 3);
228         }
229
230         if(home != NULL && path != NULL) {
231                 /* we assume UNIX path syntax is used... */
232
233                 /* try ~/file */
234                 sprintf(path, "%s/%s", home, file);
235                 openldap_ldap_init_w_conf(path, 1);
236
237                 /* try ~/.file */
238                 sprintf(path, "%s/.%s", home, file);
239                 openldap_ldap_init_w_conf(path, 1);
240         }
241
242         if(path != NULL) {
243                 LDAP_FREE(path);
244         }
245
246         /* try file */
247         openldap_ldap_init_w_conf(file, 1);
248 }
249
250 static void openldap_ldap_init_w_env(const char *prefix)
251 {
252         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
253         int len;
254         int i;
255         void *p;
256         char *value;
257
258         if (prefix == NULL) {
259                 prefix = LDAP_ENV_PREFIX;
260         }
261
262         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
263         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
264         len = strlen(buf);
265
266         for(i=0; attrs[i].type != ATTR_NONE; i++) {
267                 strcpy(&buf[len], attrs[i].name);
268                 value = getenv(buf);
269
270                 if(value == NULL) {
271                         continue;
272                 }
273
274                 switch(attrs[i].type) {
275                 case ATTR_BOOL:
276                         if((strcasecmp(value, "on") == 0) 
277                                 || (strcasecmp(value, "yes") == 0)
278                                 || (strcasecmp(value, "true") == 0))
279                         {
280                                 LDAP_BOOL_SET(&gopts, attrs[i].offset);
281
282                         } else {
283                                 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
284                         }
285                         break;
286
287                 case ATTR_INT:
288                         p = &((char *) &gopts)[attrs[i].offset];
289                         * (int*) p = atoi(value);
290                         break;
291
292                 case ATTR_KV: {
293                                 const struct ol_keyvalue *kv;
294
295                                 for(kv = attrs[i].data;
296                                         kv->key != NULL;
297                                         kv++) {
298
299                                         if(strcasecmp(value, kv->key) == 0) {
300                                                 p = &((char *) &gopts)[attrs[i].offset];
301                                                 * (int*) p = kv->value;
302                                                 break;
303                                         }
304                                 }
305                         } break;
306
307                 case ATTR_STRING:
308                         p = &((char *) &gopts)[attrs[i].offset];
309                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
310                         if (*value == '\0') {
311                                 * (char**) p = NULL;
312                         } else {
313                                 * (char**) p = LDAP_STRDUP(value);
314                         }
315                         break;
316                 case ATTR_TLS:
317 #ifdef HAVE_TLS
318                         ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
319 #endif                          
320                         break;
321                 case ATTR_URIS:
322                         if (attrs[i].offset == 0) {
323                                 ldap_set_option( NULL, LDAP_OPT_URI, value );
324                         } else {
325                                 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
326                         }
327                         break;
328                 }
329         }
330 }
331
332 void ldap_int_initialize( void )
333 {
334         if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
335                 return;
336         }
337
338         ldap_int_utils_init();
339
340 #ifdef HAVE_TLS
341         ldap_pvt_tls_init();
342 #endif
343
344         if ( ldap_int_tblsize == 0 )
345                 ldap_int_ip_init();
346
347         gopts.ldo_debug = 0;
348
349         gopts.ldo_version =     LDAP_VERSION2;
350         gopts.ldo_deref =       LDAP_DEREF_NEVER;
351         gopts.ldo_timelimit = LDAP_NO_LIMIT;
352         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
353
354         gopts.ldo_tm_api = (struct timeval *)NULL;
355         gopts.ldo_tm_net = (struct timeval *)NULL;
356
357         ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
358         gopts.ldo_defport = LDAP_PORT;
359
360         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
361
362         LDAP_BOOL_ZERO(&gopts);
363
364         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
365
366 #ifdef HAVE_TLS
367         gopts.ldo_tls_ctx = NULL;
368 #endif
369
370         gopts.ldo_valid = LDAP_INITIALIZED;
371
372         if( getenv("LDAPNOINIT") != NULL ) {
373                 return;
374         }
375
376         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
377         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
378
379         {
380                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
381
382                 if( altfile != NULL ) {
383                         openldap_ldap_init_w_sysconf( altfile );
384                 }
385         }
386
387         {
388                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
389
390                 if( altfile != NULL ) {
391                         openldap_ldap_init_w_userconf( altfile );
392                 }
393         }
394
395         openldap_ldap_init_w_env(NULL);
396 }