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