]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
ITS#4799 remove kerberos / kbind references
[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_sec = -1;
263                                 tv.tv_usec = 0;
264                                 tv.tv_sec = strtol( opt, &next, 10 );
265                                 if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
266                                         (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
267                                 }
268                                 } break;
269                         case ATTR_OPT_INT: {
270                                 long l;
271                                 char *next;
272                                 l = strtol( opt, &next, 10 );
273                                 if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
274                                         int v = (int)l;
275                                         (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
276                                 }
277                                 } break;
278                         }
279
280                         break;
281                 }
282         }
283
284         fclose(fp);
285 }
286
287 static void openldap_ldap_init_w_sysconf(const char *file)
288 {
289         openldap_ldap_init_w_conf( file, 0 );
290 }
291
292 static void openldap_ldap_init_w_userconf(const char *file)
293 {
294         char *home;
295         char *path = NULL;
296
297         if (file == NULL) {
298                 /* no file name */
299                 return;
300         }
301
302         home = getenv("HOME");
303
304         if (home != NULL) {
305                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
306                       home, 0, 0);
307                 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
308         } else {
309                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
310                       0, 0, 0);
311         }
312
313         if(home != NULL && path != NULL) {
314                 /* we assume UNIX path syntax is used... */
315
316                 /* try ~/file */
317                 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
318                 openldap_ldap_init_w_conf(path, 1);
319
320                 /* try ~/.file */
321                 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
322                 openldap_ldap_init_w_conf(path, 1);
323         }
324
325         if(path != NULL) {
326                 LDAP_FREE(path);
327         }
328
329         /* try file */
330         openldap_ldap_init_w_conf(file, 1);
331 }
332
333 static void openldap_ldap_init_w_env(
334                 struct ldapoptions *gopts,
335                 const char *prefix)
336 {
337         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
338         int len;
339         int i;
340         void *p;
341         char *value;
342
343         if (prefix == NULL) {
344                 prefix = LDAP_ENV_PREFIX;
345         }
346
347         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
348         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
349         len = strlen(buf);
350
351         for(i=0; attrs[i].type != ATTR_NONE; i++) {
352                 strcpy(&buf[len], attrs[i].name);
353                 value = getenv(buf);
354
355                 if(value == NULL) {
356                         continue;
357                 }
358
359                 switch(attrs[i].type) {
360                 case ATTR_BOOL:
361                         if((strcasecmp(value, "on") == 0) 
362                                 || (strcasecmp(value, "yes") == 0)
363                                 || (strcasecmp(value, "true") == 0))
364                         {
365                                 LDAP_BOOL_SET(gopts, attrs[i].offset);
366
367                         } else {
368                                 LDAP_BOOL_CLR(gopts, attrs[i].offset);
369                         }
370                         break;
371
372                 case ATTR_INT:
373                         p = &((char *) gopts)[attrs[i].offset];
374                         * (int*) p = atoi(value);
375                         break;
376
377                 case ATTR_KV: {
378                                 const struct ol_keyvalue *kv;
379
380                                 for(kv = attrs[i].data;
381                                         kv->key != NULL;
382                                         kv++) {
383
384                                         if(strcasecmp(value, kv->key) == 0) {
385                                                 p = &((char *) gopts)[attrs[i].offset];
386                                                 * (int*) p = kv->value;
387                                                 break;
388                                         }
389                                 }
390                         } break;
391
392                 case ATTR_STRING:
393                         p = &((char *) gopts)[attrs[i].offset];
394                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
395                         if (*value == '\0') {
396                                 * (char**) p = NULL;
397                         } else {
398                                 * (char**) p = LDAP_STRDUP(value);
399                         }
400                         break;
401                 case ATTR_OPTION:
402                         ldap_set_option( NULL, attrs[i].offset, value );
403                         break;
404                 case ATTR_SASL:
405 #ifdef HAVE_CYRUS_SASL
406                         ldap_int_sasl_config( gopts, attrs[i].offset, value );
407 #endif                          
408                         break;
409                 case ATTR_TLS:
410 #ifdef HAVE_TLS
411                         ldap_int_tls_config( NULL, attrs[i].offset, value );
412 #endif                          
413                         break;
414                 }
415         }
416 }
417
418 #if defined(__GNUC__)
419 /* Declare this function as a destructor so that it will automatically be
420  * invoked either at program exit (if libldap is a static library) or
421  * at unload time (if libldap is a dynamic library).
422  *
423  * Sorry, don't know how to handle this for non-GCC environments.
424  */
425 static void ldap_int_destroy_global_options(void)
426         __attribute__ ((destructor));
427 #endif
428
429 static void
430 ldap_int_destroy_global_options(void)
431 {
432         struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
433
434         if ( gopts == NULL )
435                 return;
436
437         gopts->ldo_valid = LDAP_UNINITIALIZED;
438
439         if ( gopts->ldo_defludp ) {
440                 ldap_free_urllist( gopts->ldo_defludp );
441                 gopts->ldo_defludp = NULL;
442         }
443 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
444         WSACleanup( );
445 #endif
446
447 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
448         if ( ldap_int_hostname ) {
449                 LDAP_FREE( ldap_int_hostname );
450                 ldap_int_hostname = NULL;
451         }
452 #endif
453 #ifdef HAVE_CYRUS_SASL
454         if ( gopts->ldo_def_sasl_authcid ) {
455                 LDAP_FREE( gopts->ldo_def_sasl_authcid );
456                 gopts->ldo_def_sasl_authcid = NULL;
457         }
458 #endif
459 #ifdef HAVE_TLS
460         ldap_int_tls_destroy( gopts );
461 #endif
462 }
463
464 /* 
465  * Initialize the global options structure with default values.
466  */
467 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
468 {
469         if (dbglvl)
470             gopts->ldo_debug = *dbglvl;
471         else
472                 gopts->ldo_debug = 0;
473
474         gopts->ldo_version   = LDAP_VERSION2;
475         gopts->ldo_deref     = LDAP_DEREF_NEVER;
476         gopts->ldo_timelimit = LDAP_NO_LIMIT;
477         gopts->ldo_sizelimit = LDAP_NO_LIMIT;
478
479         gopts->ldo_tm_api = (struct timeval *)NULL;
480         gopts->ldo_tm_net = (struct timeval *)NULL;
481
482         /* ldo_defludp will be freed by the termination handler
483          */
484         ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
485         gopts->ldo_defport = LDAP_PORT;
486 #if !defined(__GNUC__) && !defined(PIC)
487         /* Do this only for a static library, and only if we can't
488          * arrange for it to be executed as a library destructor
489          */
490         atexit(ldap_int_destroy_global_options);
491 #endif
492
493         gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
494         gopts->ldo_rebind_proc = NULL;
495         gopts->ldo_rebind_params = NULL;
496
497         LDAP_BOOL_ZERO(gopts);
498
499         LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
500
501 #ifdef LDAP_CONNECTIONLESS
502         gopts->ldo_peer = NULL;
503         gopts->ldo_cldapdn = NULL;
504         gopts->ldo_is_udp = 0;
505 #endif
506
507 #ifdef HAVE_CYRUS_SASL
508         gopts->ldo_def_sasl_mech = NULL;
509         gopts->ldo_def_sasl_realm = NULL;
510         gopts->ldo_def_sasl_authcid = NULL;
511         gopts->ldo_def_sasl_authzid = NULL;
512
513         memset( &gopts->ldo_sasl_secprops,
514                 '\0', sizeof(gopts->ldo_sasl_secprops) );
515
516         gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
517         gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
518         gopts->ldo_sasl_secprops.security_flags =
519                 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
520 #endif
521
522 #ifdef HAVE_TLS
523         gopts->ldo_tls_connect_cb = NULL;
524         gopts->ldo_tls_connect_arg = NULL;
525 #endif
526
527         gopts->ldo_valid = LDAP_INITIALIZED;
528         return;
529 }
530
531 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
532 char * ldap_int_hostname = NULL;
533 #endif
534
535 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
536 {
537         if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
538                 return;
539         }
540
541         ldap_int_error_init();
542
543         ldap_int_utils_init();
544
545 #ifdef HAVE_WINSOCK2
546 {       WORD wVersionRequested;
547         WSADATA wsaData;
548  
549         wVersionRequested = MAKEWORD( 2, 0 );
550         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
551                 /* Tell the user that we couldn't find a usable */
552                 /* WinSock DLL.                                  */
553                 return;
554         }
555  
556         /* Confirm that the WinSock DLL supports 2.0.*/
557         /* Note that if the DLL supports versions greater    */
558         /* than 2.0 in addition to 2.0, it will still return */
559         /* 2.0 in wVersion since that is the version we      */
560         /* requested.                                        */
561  
562         if ( LOBYTE( wsaData.wVersion ) != 2 ||
563                 HIBYTE( wsaData.wVersion ) != 0 )
564         {
565             /* Tell the user that we couldn't find a usable */
566             /* WinSock DLL.                                  */
567             WSACleanup( );
568             return; 
569         }
570 }       /* The WinSock DLL is acceptable. Proceed. */
571 #elif HAVE_WINSOCK
572 {       WSADATA wsaData;
573         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
574             return;
575         }
576 }
577 #endif
578
579 #if 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 }