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