]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
921062c3a5622b0a74f9fa4252e68616905cee82
[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         {0, ATTR_STRING,        "SASL_MECH",            NULL,
85                 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
86         {0, 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 #ifdef HAVE_CYRUS_SASL
421         if ( gopts->ldo_def_sasl_authcid ) {
422                 LDAP_FREE( gopts->ldo_def_sasl_authcid );
423                 gopts->ldo_def_sasl_authcid = NULL;
424         }
425 #endif
426 #ifdef HAVE_TLS
427         ldap_int_tls_destroy( gopts );
428 #endif
429 }
430
431 /* 
432  * Initialize the global options structure with default values.
433  */
434 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
435 {
436         if (dbglvl)
437             gopts->ldo_debug = *dbglvl;
438         else
439                 gopts->ldo_debug = 0;
440
441         gopts->ldo_version   = LDAP_VERSION2;
442         gopts->ldo_deref     = LDAP_DEREF_NEVER;
443         gopts->ldo_timelimit = LDAP_NO_LIMIT;
444         gopts->ldo_sizelimit = LDAP_NO_LIMIT;
445
446         gopts->ldo_tm_api = (struct timeval *)NULL;
447         gopts->ldo_tm_net = (struct timeval *)NULL;
448
449         /* ldo_defludp will be freed by the termination handler
450          */
451         ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
452         gopts->ldo_defport = LDAP_PORT;
453 #if !defined(__GNUC__) && !defined(PIC)
454         /* Do this only for a static library, and only if we can't
455          * arrange for it to be executed as a library destructor
456          */
457         atexit(ldap_int_destroy_global_options);
458 #endif
459
460         gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
461         gopts->ldo_rebind_proc = NULL;
462         gopts->ldo_rebind_params = NULL;
463
464         LDAP_BOOL_ZERO(gopts);
465
466         LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
467
468 #ifdef LDAP_CONNECTIONLESS
469         gopts->ldo_peer = NULL;
470         gopts->ldo_cldapdn = NULL;
471         gopts->ldo_is_udp = 0;
472 #endif
473
474 #ifdef HAVE_CYRUS_SASL
475         gopts->ldo_def_sasl_mech = NULL;
476         gopts->ldo_def_sasl_realm = NULL;
477         gopts->ldo_def_sasl_authcid = NULL;
478         gopts->ldo_def_sasl_authzid = NULL;
479
480         memset( &gopts->ldo_sasl_secprops,
481                 '\0', sizeof(gopts->ldo_sasl_secprops) );
482
483         gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
484         gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
485         gopts->ldo_sasl_secprops.security_flags =
486                 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
487 #endif
488
489 #ifdef HAVE_TLS
490         gopts->ldo_tls_connect_cb = NULL;
491         gopts->ldo_tls_connect_arg = NULL;
492 #endif
493
494         gopts->ldo_valid = LDAP_INITIALIZED;
495         return;
496 }
497
498 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
499         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
500 char * ldap_int_hostname = NULL;
501 #endif
502
503 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
504 {
505         if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
506                 return;
507         }
508
509         ldap_int_error_init();
510
511         ldap_int_utils_init();
512
513 #ifdef HAVE_WINSOCK2
514 {       WORD wVersionRequested;
515         WSADATA wsaData;
516  
517         wVersionRequested = MAKEWORD( 2, 0 );
518         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
519                 /* Tell the user that we couldn't find a usable */
520                 /* WinSock DLL.                                  */
521                 return;
522         }
523  
524         /* Confirm that the WinSock DLL supports 2.0.*/
525         /* Note that if the DLL supports versions greater    */
526         /* than 2.0 in addition to 2.0, it will still return */
527         /* 2.0 in wVersion since that is the version we      */
528         /* requested.                                        */
529  
530         if ( LOBYTE( wsaData.wVersion ) != 2 ||
531                 HIBYTE( wsaData.wVersion ) != 0 )
532         {
533             /* Tell the user that we couldn't find a usable */
534             /* WinSock DLL.                                  */
535             WSACleanup( );
536             return; 
537         }
538 }       /* The WinSock DLL is acceptable. Proceed. */
539 #elif HAVE_WINSOCK
540 {       WSADATA wsaData;
541         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
542             return;
543         }
544 }
545 #endif
546
547 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
548         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
549         {
550                 char    *name = ldap_int_hostname;
551
552                 ldap_int_hostname = ldap_pvt_get_fqdn( name );
553
554                 if ( name != NULL && name != ldap_int_hostname ) {
555                         LDAP_FREE( name );
556                 }
557         }
558 #endif
559
560 #ifndef HAVE_POLL
561         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
562 #endif
563
564         ldap_int_initialize_global_options(gopts, NULL);
565
566         if( getenv("LDAPNOINIT") != NULL ) {
567                 return;
568         }
569
570 #ifdef HAVE_CYRUS_SASL
571         {
572                 /* set authentication identity to current user name */
573                 char *user = getenv("USER");
574
575                 if( user == NULL ) user = getenv("USERNAME");
576                 if( user == NULL ) user = getenv("LOGNAME");
577
578                 if( user != NULL ) {
579                         gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
580                 }
581     }
582 #endif
583
584         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
585         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
586
587         {
588                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
589
590                 if( altfile != NULL ) {
591                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
592                               LDAP_ENV_PREFIX "CONF", altfile, 0);
593                         openldap_ldap_init_w_sysconf( altfile );
594                 }
595                 else
596                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
597                               LDAP_ENV_PREFIX "CONF", 0, 0);
598         }
599
600         {
601                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
602
603                 if( altfile != NULL ) {
604                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
605                               LDAP_ENV_PREFIX "RC", altfile, 0);
606                         openldap_ldap_init_w_userconf( altfile );
607                 }
608                 else
609                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
610                               LDAP_ENV_PREFIX "RC", 0, 0);
611         }
612
613         openldap_ldap_init_w_env(gopts, NULL);
614 }