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