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