]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
dc65b464765e2ae07ede48c288e6292845cdbc93
[openldap] / libraries / libldap / init.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <ac/stdlib.h>
20
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/ctype.h>
24 #include <ac/time.h>
25
26 #ifdef HAVE_LIMITS_H
27 #include <limits.h>
28 #endif
29
30 #include "ldap-int.h"
31 #include "ldap_defaults.h"
32
33 struct ldapoptions ldap_int_global_options =
34         { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
35
36 #define ATTR_NONE       0
37 #define ATTR_BOOL       1
38 #define ATTR_INT        2
39 #define ATTR_KV         3
40 #define ATTR_STRING     4
41 #define ATTR_OPTION     5
42
43 #define ATTR_SASL       6
44 #define ATTR_TLS        7
45
46 struct ol_keyvalue {
47         const char *            key;
48         int                     value;
49 };
50
51 static const struct ol_keyvalue deref_kv[] = {
52         {"never", LDAP_DEREF_NEVER},
53         {"searching", LDAP_DEREF_SEARCHING},
54         {"finding", LDAP_DEREF_FINDING},
55         {"always", LDAP_DEREF_ALWAYS},
56         {NULL, 0}
57 };
58
59 static const struct ol_attribute {
60         int                     useronly;
61         int                     type;
62         const char *    name;
63         const void *    data;
64         size_t          offset;
65 } attrs[] = {
66         {0, ATTR_KV,            "DEREF",        deref_kv, /* or &deref_kv[0] */
67                 offsetof(struct ldapoptions, ldo_deref)},
68         {0, ATTR_INT,           "SIZELIMIT",    NULL,
69                 offsetof(struct ldapoptions, ldo_sizelimit)},
70         {0, ATTR_INT,           "TIMELIMIT",    NULL,
71                 offsetof(struct ldapoptions, ldo_timelimit)},
72         {1, ATTR_STRING,        "BINDDN",               NULL,
73                 offsetof(struct ldapoptions, ldo_defbinddn)},
74         {0, ATTR_STRING,        "BASE",                 NULL,
75                 offsetof(struct ldapoptions, ldo_defbase)},
76         {0, ATTR_INT,           "PORT",                 NULL,           /* deprecated */
77                 offsetof(struct ldapoptions, ldo_defport)},
78         {0, ATTR_OPTION,        "HOST",                 NULL,   LDAP_OPT_HOST_NAME}, /* deprecated */
79         {0, ATTR_OPTION,        "URI",                  NULL,   LDAP_OPT_URI}, /* replaces HOST/PORT */
80         {0, ATTR_BOOL,          "REFERRALS",    NULL,   LDAP_BOOL_REFERRALS},
81         {0, ATTR_BOOL,          "RESTART",              NULL,   LDAP_BOOL_RESTART},
82
83 #ifdef HAVE_CYRUS_SASL
84         {1, ATTR_STRING,        "SASL_MECH",            NULL,
85                 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
86         {1, ATTR_STRING,        "SASL_REALM",           NULL,
87                 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
88         {1, ATTR_STRING,        "SASL_AUTHCID",         NULL,
89                 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
90         {1, ATTR_STRING,        "SASL_AUTHZID",         NULL,
91                 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
92         {0, ATTR_SASL,          "SASL_SECPROPS",        NULL,   LDAP_OPT_X_SASL_SECPROPS},
93 #endif
94
95 #ifdef HAVE_TLS
96         {1, ATTR_TLS,   "TLS_CERT",                     NULL,   LDAP_OPT_X_TLS_CERTFILE},
97         {1, ATTR_TLS,   "TLS_KEY",                      NULL,   LDAP_OPT_X_TLS_KEYFILE},
98         {0, ATTR_TLS,   "TLS_CACERT",           NULL,   LDAP_OPT_X_TLS_CACERTFILE},
99         {0, ATTR_TLS,   "TLS_CACERTDIR",        NULL,   LDAP_OPT_X_TLS_CACERTDIR},
100         {0, ATTR_TLS,   "TLS_REQCERT",          NULL,   LDAP_OPT_X_TLS_REQUIRE_CERT},
101         {0, ATTR_TLS,   "TLS_RANDFILE",         NULL,   LDAP_OPT_X_TLS_RANDOM_FILE},
102         {0, ATTR_TLS,   "TLS_CIPHER_SUITE",     NULL,   LDAP_OPT_X_TLS_CIPHER_SUITE},
103
104 #ifdef HAVE_OPENSSL_CRL
105         {0, ATTR_TLS,   "TLS_CRLCHECK",         NULL,   LDAP_OPT_X_TLS_CRLCHECK},
106 #endif
107         
108 #endif
109
110         {0, ATTR_NONE,          NULL,           NULL,   0}
111 };
112
113 #define MAX_LDAP_ATTR_LEN  sizeof("TLS_CIPHER_SUITE")
114 #define MAX_LDAP_ENV_PREFIX_LEN 8
115
116 static void openldap_ldap_init_w_conf(
117         const char *file, int userconf )
118 {
119         char linebuf[128];
120         FILE *fp;
121         int i;
122         char *cmd, *opt;
123         char *start, *end;
124         struct ldapoptions *gopts;
125
126         if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
127                 return;                 /* Could not allocate mem for global options */
128         }
129
130         if (file == NULL) {
131                 /* no file name */
132                 return;
133         }
134
135         Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
136
137         fp = fopen(file, "r");
138         if(fp == NULL) {
139                 /* could not open file */
140                 return;
141         }
142
143         Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
144
145         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
146                 /* skip lines starting with '#' */
147                 if(*start == '#') continue;
148
149                 /* trim leading white space */
150                 while((*start != '\0') && isspace((unsigned char) *start))
151                         start++;
152
153                 /* anything left? */
154                 if(*start == '\0') continue;
155
156                 /* trim trailing white space */
157                 end = &start[strlen(start)-1];
158                 while(isspace((unsigned char)*end)) end--;
159                 end[1] = '\0';
160
161                 /* anything left? */
162                 if(*start == '\0') continue;
163                 
164
165                 /* parse the command */
166                 cmd=start;
167                 while((*start != '\0') && !isspace((unsigned char)*start)) {
168                         start++;
169                 }
170                 if(*start == '\0') {
171                         /* command has no argument */
172                         continue;
173                 } 
174
175                 *start++ = '\0';
176
177                 /* we must have some whitespace to skip */
178                 while(isspace((unsigned char)*start)) start++;
179                 opt = start;
180
181                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
182                         void *p;
183
184                         if( !userconf && attrs[i].useronly ) {
185                                 continue;
186                         }
187
188                         if(strcasecmp(cmd, attrs[i].name) != 0) {
189                                 continue;
190                         }
191
192                         switch(attrs[i].type) {
193                         case ATTR_BOOL:
194                                 if((strcasecmp(opt, "on") == 0) 
195                                         || (strcasecmp(opt, "yes") == 0)
196                                         || (strcasecmp(opt, "true") == 0))
197                                 {
198                                         LDAP_BOOL_SET(gopts, attrs[i].offset);
199
200                                 } else {
201                                         LDAP_BOOL_CLR(gopts, attrs[i].offset);
202                                 }
203
204                                 break;
205
206                         case ATTR_INT:
207                                 p = &((char *) gopts)[attrs[i].offset];
208                                 * (int*) p = atoi(opt);
209                                 break;
210
211                         case ATTR_KV: {
212                                         const struct ol_keyvalue *kv;
213
214                                         for(kv = attrs[i].data;
215                                                 kv->key != NULL;
216                                                 kv++) {
217
218                                                 if(strcasecmp(opt, kv->key) == 0) {
219                                                         p = &((char *) gopts)[attrs[i].offset];
220                                                         * (int*) p = kv->value;
221                                                         break;
222                                                 }
223                                         }
224                                 } break;
225
226                         case ATTR_STRING:
227                                 p = &((char *) gopts)[attrs[i].offset];
228                                 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
229                                 * (char**) p = LDAP_STRDUP(opt);
230                                 break;
231                         case ATTR_OPTION:
232                                 ldap_set_option( NULL, attrs[i].offset, opt );
233                                 break;
234                         case ATTR_SASL:
235 #ifdef HAVE_CYRUS_SASL
236                                 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
237 #endif
238                                 break;
239                         case ATTR_TLS:
240 #ifdef HAVE_TLS
241                                 ldap_int_tls_config( NULL, attrs[i].offset, opt );
242 #endif
243                                 break;
244                         }
245
246                         break;
247                 }
248         }
249
250         fclose(fp);
251 }
252
253 static void openldap_ldap_init_w_sysconf(const char *file)
254 {
255         openldap_ldap_init_w_conf( file, 0 );
256 }
257
258 static void openldap_ldap_init_w_userconf(const char *file)
259 {
260         char *home;
261         char *path = NULL;
262
263         if (file == NULL) {
264                 /* no file name */
265                 return;
266         }
267
268         home = getenv("HOME");
269
270         if (home != NULL) {
271                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
272                       home, 0, 0);
273                 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
274         } else {
275                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
276                       0, 0, 0);
277         }
278
279         if(home != NULL && path != NULL) {
280                 /* we assume UNIX path syntax is used... */
281
282                 /* try ~/file */
283                 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
284                 openldap_ldap_init_w_conf(path, 1);
285
286                 /* try ~/.file */
287                 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
288                 openldap_ldap_init_w_conf(path, 1);
289         }
290
291         if(path != NULL) {
292                 LDAP_FREE(path);
293         }
294
295         /* try file */
296         openldap_ldap_init_w_conf(file, 1);
297 }
298
299 static void openldap_ldap_init_w_env(
300                 struct ldapoptions *gopts,
301                 const char *prefix)
302 {
303         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
304         int len;
305         int i;
306         void *p;
307         char *value;
308
309         if (prefix == NULL) {
310                 prefix = LDAP_ENV_PREFIX;
311         }
312
313         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
314         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
315         len = strlen(buf);
316
317         for(i=0; attrs[i].type != ATTR_NONE; i++) {
318                 strcpy(&buf[len], attrs[i].name);
319                 value = getenv(buf);
320
321                 if(value == NULL) {
322                         continue;
323                 }
324
325                 switch(attrs[i].type) {
326                 case ATTR_BOOL:
327                         if((strcasecmp(value, "on") == 0) 
328                                 || (strcasecmp(value, "yes") == 0)
329                                 || (strcasecmp(value, "true") == 0))
330                         {
331                                 LDAP_BOOL_SET(gopts, attrs[i].offset);
332
333                         } else {
334                                 LDAP_BOOL_CLR(gopts, attrs[i].offset);
335                         }
336                         break;
337
338                 case ATTR_INT:
339                         p = &((char *) gopts)[attrs[i].offset];
340                         * (int*) p = atoi(value);
341                         break;
342
343                 case ATTR_KV: {
344                                 const struct ol_keyvalue *kv;
345
346                                 for(kv = attrs[i].data;
347                                         kv->key != NULL;
348                                         kv++) {
349
350                                         if(strcasecmp(value, kv->key) == 0) {
351                                                 p = &((char *) gopts)[attrs[i].offset];
352                                                 * (int*) p = kv->value;
353                                                 break;
354                                         }
355                                 }
356                         } break;
357
358                 case ATTR_STRING:
359                         p = &((char *) gopts)[attrs[i].offset];
360                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
361                         if (*value == '\0') {
362                                 * (char**) p = NULL;
363                         } else {
364                                 * (char**) p = LDAP_STRDUP(value);
365                         }
366                         break;
367                 case ATTR_OPTION:
368                         ldap_set_option( NULL, attrs[i].offset, value );
369                         break;
370                 case ATTR_SASL:
371 #ifdef HAVE_CYRUS_SASL
372                         ldap_int_sasl_config( gopts, attrs[i].offset, value );
373 #endif                          
374                         break;
375                 case ATTR_TLS:
376 #ifdef HAVE_TLS
377                         ldap_int_tls_config( NULL, attrs[i].offset, value );
378 #endif                          
379                         break;
380                 }
381         }
382 }
383
384 #if defined(__GNUC__)
385 /* Declare this function as a destructor so that it will automatically be
386  * invoked either at program exit (if libldap is a static library) or
387  * at unload time (if libldap is a dynamic library).
388  *
389  * Sorry, don't know how to handle this for non-GCC environments.
390  */
391 static void ldap_int_destroy_global_options(void)
392         __attribute__ ((destructor));
393 #endif
394
395 static void
396 ldap_int_destroy_global_options(void)
397 {
398         struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
399
400         if ( gopts == NULL )
401                 return;
402
403         gopts->ldo_valid = LDAP_UNINITIALIZED;
404
405         if ( gopts->ldo_defludp ) {
406                 ldap_free_urllist( gopts->ldo_defludp );
407                 gopts->ldo_defludp = NULL;
408         }
409 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
410         WSACleanup( );
411 #endif
412
413 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
414         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
415         if ( ldap_int_hostname ) {
416                 LDAP_FREE( ldap_int_hostname );
417                 ldap_int_hostname = NULL;
418         }
419 #endif
420 }
421
422 /* 
423  * Initialize the global options structure with default values.
424  */
425 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
426 {
427         if (dbglvl)
428             gopts->ldo_debug = *dbglvl;
429         else
430                 gopts->ldo_debug = 0;
431
432         gopts->ldo_version   = LDAP_VERSION2;
433         gopts->ldo_deref     = LDAP_DEREF_NEVER;
434         gopts->ldo_timelimit = LDAP_NO_LIMIT;
435         gopts->ldo_sizelimit = LDAP_NO_LIMIT;
436
437         gopts->ldo_tm_api = (struct timeval *)NULL;
438         gopts->ldo_tm_net = (struct timeval *)NULL;
439
440         /* ldo_defludp will be freed by the termination handler
441          */
442         ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
443         gopts->ldo_defport = LDAP_PORT;
444 #if !defined(__GNUC__) && !defined(PIC)
445         /* Do this only for a static library, and only if we can't
446          * arrange for it to be executed as a library destructor
447          */
448         atexit(ldap_int_destroy_global_options);
449 #endif
450
451         gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
452         gopts->ldo_rebind_proc = NULL;
453         gopts->ldo_rebind_params = NULL;
454
455         LDAP_BOOL_ZERO(gopts);
456
457         LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
458
459 #ifdef LDAP_CONNECTIONLESS
460         gopts->ldo_peer = NULL;
461         gopts->ldo_cldapdn = NULL;
462         gopts->ldo_is_udp = 0;
463 #endif
464
465 #ifdef HAVE_CYRUS_SASL
466         gopts->ldo_def_sasl_mech = NULL;
467         gopts->ldo_def_sasl_realm = NULL;
468         gopts->ldo_def_sasl_authcid = NULL;
469         gopts->ldo_def_sasl_authzid = NULL;
470
471         memset( &gopts->ldo_sasl_secprops,
472                 '\0', sizeof(gopts->ldo_sasl_secprops) );
473
474         gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
475         gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
476         gopts->ldo_sasl_secprops.security_flags =
477                 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
478 #endif
479
480 #ifdef HAVE_TLS
481         gopts->ldo_tls_connect_cb = NULL;
482         gopts->ldo_tls_connect_arg = NULL;
483 #endif
484
485         gopts->ldo_valid = LDAP_INITIALIZED;
486         return;
487 }
488
489 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
490         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
491 char * ldap_int_hostname = NULL;
492 #endif
493
494 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
495 {
496         if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
497                 return;
498         }
499
500         ldap_int_error_init();
501
502         ldap_int_utils_init();
503
504 #ifdef HAVE_WINSOCK2
505 {       WORD wVersionRequested;
506         WSADATA wsaData;
507  
508         wVersionRequested = MAKEWORD( 2, 0 );
509         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
510                 /* Tell the user that we couldn't find a usable */
511                 /* WinSock DLL.                                  */
512                 return;
513         }
514  
515         /* Confirm that the WinSock DLL supports 2.0.*/
516         /* Note that if the DLL supports versions greater    */
517         /* than 2.0 in addition to 2.0, it will still return */
518         /* 2.0 in wVersion since that is the version we      */
519         /* requested.                                        */
520  
521         if ( LOBYTE( wsaData.wVersion ) != 2 ||
522                 HIBYTE( wsaData.wVersion ) != 0 )
523         {
524             /* Tell the user that we couldn't find a usable */
525             /* WinSock DLL.                                  */
526             WSACleanup( );
527             return; 
528         }
529 }       /* The WinSock DLL is acceptable. Proceed. */
530 #elif HAVE_WINSOCK
531 {       WSADATA wsaData;
532         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
533             return;
534         }
535 }
536 #endif
537
538 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
539         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
540         {
541                 char    *name = ldap_int_hostname;
542
543                 ldap_int_hostname = ldap_pvt_get_fqdn( name );
544
545                 if ( name != NULL && name != ldap_int_hostname ) {
546                         LDAP_FREE( name );
547                 }
548         }
549 #endif
550
551 #ifndef HAVE_POLL
552         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
553 #endif
554
555         ldap_int_initialize_global_options(gopts, NULL);
556
557         if( getenv("LDAPNOINIT") != NULL ) {
558                 return;
559         }
560
561 #ifdef HAVE_CYRUS_SASL
562         {
563                 /* set authentication identity to current user name */
564                 char *user = getenv("USER");
565
566                 if( user == NULL ) user = getenv("USERNAME");
567                 if( user == NULL ) user = getenv("LOGNAME");
568
569                 if( user != NULL ) {
570                         gopts->ldo_def_sasl_authcid = user;
571                 }
572     }
573 #endif
574
575         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
576         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
577
578         {
579                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
580
581                 if( altfile != NULL ) {
582                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
583                               LDAP_ENV_PREFIX "CONF", altfile, 0);
584                         openldap_ldap_init_w_sysconf( altfile );
585                 }
586                 else
587                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
588                               LDAP_ENV_PREFIX "CONF", 0, 0);
589         }
590
591         {
592                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
593
594                 if( altfile != NULL ) {
595                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
596                               LDAP_ENV_PREFIX "RC", altfile, 0);
597                         openldap_ldap_init_w_userconf( altfile );
598                 }
599                 else
600                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
601                               LDAP_ENV_PREFIX "RC", 0, 0);
602         }
603
604         openldap_ldap_init_w_env(gopts, NULL);
605 }