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