]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
d507e8cb4d82fdb3caa384f9c425019a488b5277
[openldap] / libraries / libldap / init.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 <limits.h>
17
18 #include "ldap-int.h"
19 #include "ldap_defaults.h"
20
21 struct ldapoptions ldap_int_global_options =
22         { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
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 #define ATTR_TLS        5
30 #define ATTR_URIS       6
31
32 struct ol_keyvalue {
33         const char *            key;
34         int                     value;
35 };
36
37 static const struct ol_keyvalue deref_kv[] = {
38         {"never", LDAP_DEREF_NEVER},
39         {"searching", LDAP_DEREF_SEARCHING},
40         {"finding", LDAP_DEREF_FINDING},
41         {"always", LDAP_DEREF_ALWAYS},
42         {NULL, 0}
43 };
44
45 static const struct ol_attribute {
46         int                     useronly;
47         int                     type;
48         const char *    name;
49         const void *    data;
50         size_t          offset;
51 } attrs[] = {
52         {0, ATTR_KV,            "DEREF",        deref_kv, /* or &deref_kv[0] */
53                 offsetof(struct ldapoptions, ldo_deref)},
54         {0, ATTR_INT,           "SIZELIMIT",    NULL,
55                 offsetof(struct ldapoptions, ldo_sizelimit)},
56         {0, ATTR_INT,           "TIMELIMIT",    NULL,
57                 offsetof(struct ldapoptions, ldo_timelimit)},
58         {1, ATTR_STRING,        "BINDDN",               NULL,
59                 offsetof(struct ldapoptions, ldo_defbinddn)},
60         {0, ATTR_STRING,        "BASE",                 NULL,
61                 offsetof(struct ldapoptions, ldo_defbase)},
62         {0, ATTR_INT,           "PORT",                 NULL,           /* deprecated */
63                 offsetof(struct ldapoptions, ldo_defport)},
64         {0, ATTR_URIS,          "HOST",                 NULL,   1},     /* deprecated */
65         {0, ATTR_URIS,          "URI",                  NULL,   0}, /* replaces HOST/URI */
66         {0, ATTR_BOOL,          "REFERRALS",    NULL,   LDAP_BOOL_REFERRALS},
67         {0, ATTR_BOOL,          "RESTART",              NULL,   LDAP_BOOL_RESTART},
68
69 #ifdef HAVE_TLS
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_TLS,           "TLS_RANDFILE", NULL,   LDAP_OPT_X_TLS_RANDOM_FILE},
77 #endif
78
79 #ifdef HAVE_CYRUS_SASL
80         {0, ATTR_INT,           "SASL_MINSSF",  NULL,
81                 offsetof(struct ldapoptions, ldo_sasl_minssf)},
82         {0, ATTR_INT,           "SASL_MAXSSF",  NULL,
83                 offsetof(struct ldapoptions, ldo_sasl_maxssf)},
84 #endif
85
86         {0, ATTR_NONE,          NULL,           NULL,   0}
87 };
88
89 #define MAX_LDAP_ATTR_LEN  sizeof("TLS_CACERTDIR")
90 #define MAX_LDAP_ENV_PREFIX_LEN 8
91
92 static void openldap_ldap_init_w_conf(
93         const char *file, int userconf )
94 {
95         char linebuf[128];
96         FILE *fp;
97         int i;
98         char *cmd, *opt;
99         char *start, *end;
100         struct ldapoptions *gopts;
101
102         if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
103                 return;                 /* Could not allocate mem for global options */
104         }
105
106         if (file == NULL) {
107                 /* no file name */
108                 return;
109         }
110
111         Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
112
113         fp = fopen(file, "r");
114         if(fp == NULL) {
115                 /* could not open file */
116                 return;
117         }
118
119         Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
120
121         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
122                 /* skip lines starting with '#' */
123                 if(*start == '#') continue;
124
125                 /* trim leading white space */
126                 while((*start != '\0') && isspace((unsigned char) *start))
127                         start++;
128
129                 /* anything left? */
130                 if(*start == '\0') continue;
131
132                 /* trim trailing white space */
133                 end = &start[strlen(start)-1];
134                 while(isspace((unsigned char)*end)) end--;
135                 end[1] = '\0';
136
137                 /* anything left? */
138                 if(*start == '\0') continue;
139                 
140
141                 /* parse the command */
142                 cmd=start;
143                 while((*start != '\0') && !isspace((unsigned char)*start)) {
144                         start++;
145                 }
146                 if(*start == '\0') {
147                         /* command has no argument */
148                         continue;
149                 } 
150
151                 *start++ = '\0';
152
153                 /* we must have some whitespace to skip */
154                 while(isspace((unsigned char)*start)) start++;
155                 opt = start;
156
157                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
158                         void *p;
159
160                         if( !userconf && attrs[i].useronly ) {
161                                 continue;
162                         }
163
164                         if(strcasecmp(cmd, attrs[i].name) != 0) {
165                                 continue;
166                         }
167
168                         switch(attrs[i].type) {
169                         case ATTR_BOOL:
170                                 if((strcasecmp(opt, "on") == 0) 
171                                         || (strcasecmp(opt, "yes") == 0)
172                                         || (strcasecmp(opt, "true") == 0))
173                                 {
174                                         LDAP_BOOL_SET(gopts, attrs[i].offset);
175
176                                 } else {
177                                         LDAP_BOOL_CLR(gopts, attrs[i].offset);
178                                 }
179
180                                 break;
181
182                         case ATTR_INT:
183                                 p = &((char *) gopts)[attrs[i].offset];
184                                 * (int*) p = atoi(opt);
185                                 break;
186
187                         case ATTR_KV: {
188                                         const struct ol_keyvalue *kv;
189
190                                         for(kv = attrs[i].data;
191                                                 kv->key != NULL;
192                                                 kv++) {
193
194                                                 if(strcasecmp(opt, kv->key) == 0) {
195                                                         p = &((char *) gopts)[attrs[i].offset];
196                                                         * (int*) p = kv->value;
197                                                         break;
198                                                 }
199                                         }
200                                 } break;
201
202                         case ATTR_STRING:
203                                 p = &((char *) gopts)[attrs[i].offset];
204                                 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
205                                 * (char**) p = LDAP_STRDUP(opt);
206                                 break;
207                         case ATTR_TLS:
208 #ifdef HAVE_TLS
209                                 ldap_pvt_tls_config( gopts, attrs[i].offset, opt );
210 #endif
211                                 break;
212                         case ATTR_URIS:
213                                 if (attrs[i].offset == 0) {
214                                         ldap_set_option( NULL, LDAP_OPT_URI, opt );
215                                 } else {
216                                         ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
217                                 }
218                                 break;
219                         }
220                         break;
221                 }
222         }
223
224         fclose(fp);
225 }
226
227 static void openldap_ldap_init_w_sysconf(const char *file)
228 {
229         openldap_ldap_init_w_conf( file, 0 );
230 }
231
232 static void openldap_ldap_init_w_userconf(const char *file)
233 {
234         char *home;
235         char *path = NULL;
236
237         if (file == NULL) {
238                 /* no file name */
239                 return;
240         }
241
242         home = getenv("HOME");
243
244         if (home != NULL) {
245                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
246                       home, 0, 0);
247                 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
248         } else {
249                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
250                       0, 0, 0);
251         }
252
253         if(home != NULL && path != NULL) {
254                 /* we assume UNIX path syntax is used... */
255
256                 /* try ~/file */
257                 sprintf(path, "%s/%s", home, file);
258                 openldap_ldap_init_w_conf(path, 1);
259
260                 /* try ~/.file */
261                 sprintf(path, "%s/.%s", home, file);
262                 openldap_ldap_init_w_conf(path, 1);
263         }
264
265         if(path != NULL) {
266                 LDAP_FREE(path);
267         }
268
269         /* try file */
270         openldap_ldap_init_w_conf(file, 1);
271 }
272
273 static void openldap_ldap_init_w_env(
274                 struct ldapoptions *gopts,
275                 const char *prefix)
276 {
277         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
278         int len;
279         int i;
280         void *p;
281         char *value;
282
283         if (prefix == NULL) {
284                 prefix = LDAP_ENV_PREFIX;
285         }
286
287         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
288         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
289         len = strlen(buf);
290
291         for(i=0; attrs[i].type != ATTR_NONE; i++) {
292                 strcpy(&buf[len], attrs[i].name);
293                 value = getenv(buf);
294
295                 if(value == NULL) {
296                         continue;
297                 }
298
299                 switch(attrs[i].type) {
300                 case ATTR_BOOL:
301                         if((strcasecmp(value, "on") == 0) 
302                                 || (strcasecmp(value, "yes") == 0)
303                                 || (strcasecmp(value, "true") == 0))
304                         {
305                                 LDAP_BOOL_SET(gopts, attrs[i].offset);
306
307                         } else {
308                                 LDAP_BOOL_CLR(gopts, attrs[i].offset);
309                         }
310                         break;
311
312                 case ATTR_INT:
313                         p = &((char *) gopts)[attrs[i].offset];
314                         * (int*) p = atoi(value);
315                         break;
316
317                 case ATTR_KV: {
318                                 const struct ol_keyvalue *kv;
319
320                                 for(kv = attrs[i].data;
321                                         kv->key != NULL;
322                                         kv++) {
323
324                                         if(strcasecmp(value, kv->key) == 0) {
325                                                 p = &((char *) gopts)[attrs[i].offset];
326                                                 * (int*) p = kv->value;
327                                                 break;
328                                         }
329                                 }
330                         } break;
331
332                 case ATTR_STRING:
333                         p = &((char *) gopts)[attrs[i].offset];
334                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
335                         if (*value == '\0') {
336                                 * (char**) p = NULL;
337                         } else {
338                                 * (char**) p = LDAP_STRDUP(value);
339                         }
340                         break;
341                 case ATTR_TLS:
342 #ifdef HAVE_TLS
343                         ldap_pvt_tls_config( gopts, attrs[i].offset, value );
344 #endif                          
345                         break;
346                 case ATTR_URIS:
347                         if (attrs[i].offset == 0) {
348                                 ldap_set_option( NULL, LDAP_OPT_URI, value );
349                         } else {
350                                 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
351                         }
352                         break;
353                 }
354         }
355 }
356
357 /* 
358  * Initialize the global options structure with default values.
359  */
360 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
361 {
362         if (dbglvl)
363             gopts->ldo_debug = *dbglvl;
364         else
365                 gopts->ldo_debug = 0;
366
367         gopts->ldo_version   = LDAP_VERSION2;
368         gopts->ldo_deref     = LDAP_DEREF_NEVER;
369         gopts->ldo_timelimit = LDAP_NO_LIMIT;
370         gopts->ldo_sizelimit = LDAP_NO_LIMIT;
371
372         gopts->ldo_tm_api = (struct timeval *)NULL;
373         gopts->ldo_tm_net = (struct timeval *)NULL;
374
375         /* ldo_defludp is leaked, we should have an at_exit() handler
376          * to free this and whatever else needs to cleaned up. 
377          */
378         ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
379         gopts->ldo_defport = LDAP_PORT;
380
381         gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
382         gopts->ldo_rebindproc = NULL;
383
384         LDAP_BOOL_ZERO(gopts);
385
386         LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
387
388 #ifdef HAVE_TLS
389         gopts->ldo_tls_ctx = NULL;
390 #endif
391 #ifdef HAVE_CYRUS_SASL
392         gopts->ldo_sasl_minssf = 0;
393         gopts->ldo_sasl_maxssf = INT_MAX;
394 #endif
395
396         gopts->ldo_valid = LDAP_INITIALIZED;
397
398         return;
399 }
400
401 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
402 {
403         if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
404                 return;
405         }
406
407         ldap_int_utils_init();
408
409 #ifdef HAVE_TLS
410         ldap_pvt_tls_init();
411 #endif
412
413 #ifdef HAVE_CYRUS_SASL
414         ldap_pvt_sasl_init();
415 #endif
416
417         if ( ldap_int_tblsize == 0 )
418                 ldap_int_ip_init();
419
420         ldap_int_initialize_global_options(gopts, NULL);
421
422         if( getenv("LDAPNOINIT") != NULL ) {
423                 return;
424         }
425
426         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
427         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
428
429         {
430                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
431
432                 if( altfile != NULL ) {
433                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
434                               LDAP_ENV_PREFIX "CONF", altfile, 0);
435                         openldap_ldap_init_w_sysconf( altfile );
436                 }
437                 else
438                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
439                               LDAP_ENV_PREFIX "CONF", 0, 0);
440         }
441
442         {
443                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
444
445                 if( altfile != NULL ) {
446                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
447                               LDAP_ENV_PREFIX "RC", altfile, 0);
448                         openldap_ldap_init_w_userconf( altfile );
449                 }
450                 else
451                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
452                               LDAP_ENV_PREFIX "RC", 0, 0);
453         }
454
455         openldap_ldap_init_w_env(gopts, NULL);
456 }