]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
Happy new year! (belated)
[openldap] / libraries / libldap / init.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2008 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <ac/stdlib.h>
20
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/ctype.h>
24 #include <ac/time.h>
25
26 #ifdef HAVE_LIMITS_H
27 #include <limits.h>
28 #endif
29
30 #include "ldap-int.h"
31 #include "ldap_defaults.h"
32
33 struct ldapoptions ldap_int_global_options =
34         { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
35
36 #define ATTR_NONE       0
37 #define ATTR_BOOL       1
38 #define ATTR_INT        2
39 #define ATTR_KV         3
40 #define ATTR_STRING     4
41 #define ATTR_OPTION     5
42
43 #define ATTR_SASL       6
44 #define ATTR_TLS        7
45
46 struct ol_keyvalue {
47         const char *            key;
48         int                     value;
49 };
50
51 static const struct ol_keyvalue deref_kv[] = {
52         {"never", LDAP_DEREF_NEVER},
53         {"searching", LDAP_DEREF_SEARCHING},
54         {"finding", LDAP_DEREF_FINDING},
55         {"always", LDAP_DEREF_ALWAYS},
56         {NULL, 0}
57 };
58
59 static const struct ol_attribute {
60         int                     useronly;
61         int                     type;
62         const char *    name;
63         const void *    data;
64         size_t          offset;
65 } attrs[] = {
66         {0, ATTR_KV,            "DEREF",        deref_kv, /* or &deref_kv[0] */
67                 offsetof(struct ldapoptions, ldo_deref)},
68         {0, ATTR_INT,           "SIZELIMIT",    NULL,
69                 offsetof(struct ldapoptions, ldo_sizelimit)},
70         {0, ATTR_INT,           "TIMELIMIT",    NULL,
71                 offsetof(struct ldapoptions, ldo_timelimit)},
72         {1, ATTR_STRING,        "BINDDN",               NULL,
73                 offsetof(struct ldapoptions, ldo_defbinddn)},
74         {0, ATTR_STRING,        "BASE",                 NULL,
75                 offsetof(struct ldapoptions, ldo_defbase)},
76         {0, ATTR_INT,           "PORT",                 NULL,           /* deprecated */
77                 offsetof(struct ldapoptions, ldo_defport)},
78         {0, ATTR_OPTION,        "HOST",                 NULL,   LDAP_OPT_HOST_NAME}, /* deprecated */
79         {0, ATTR_OPTION,        "URI",                  NULL,   LDAP_OPT_URI}, /* replaces HOST/PORT */
80         {0, ATTR_BOOL,          "REFERRALS",    NULL,   LDAP_BOOL_REFERRALS},
81         {0, ATTR_BOOL,          "RESTART",              NULL,   LDAP_BOOL_RESTART},
82
83 #ifdef HAVE_CYRUS_SASL
84         {1, ATTR_STRING,        "SASL_MECH",            NULL,
85                 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
86         {1, ATTR_STRING,        "SASL_REALM",           NULL,
87                 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
88         {1, ATTR_STRING,        "SASL_AUTHCID",         NULL,
89                 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
90         {1, ATTR_STRING,        "SASL_AUTHZID",         NULL,
91                 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
92         {0, ATTR_SASL,          "SASL_SECPROPS",        NULL,   LDAP_OPT_X_SASL_SECPROPS},
93 #endif
94
95 #ifdef HAVE_TLS
96         {1, ATTR_TLS,   "TLS_CERT",                     NULL,   LDAP_OPT_X_TLS_CERTFILE},
97         {1, ATTR_TLS,   "TLS_KEY",                      NULL,   LDAP_OPT_X_TLS_KEYFILE},
98         {0, ATTR_TLS,   "TLS_CACERT",           NULL,   LDAP_OPT_X_TLS_CACERTFILE},
99         {0, ATTR_TLS,   "TLS_CACERTDIR",        NULL,   LDAP_OPT_X_TLS_CACERTDIR},
100         {0, ATTR_TLS,   "TLS_REQCERT",          NULL,   LDAP_OPT_X_TLS_REQUIRE_CERT},
101         {0, ATTR_TLS,   "TLS_RANDFILE",         NULL,   LDAP_OPT_X_TLS_RANDOM_FILE},
102         {0, ATTR_TLS,   "TLS_CIPHER_SUITE",     NULL,   LDAP_OPT_X_TLS_CIPHER_SUITE},
103
104 #ifdef HAVE_OPENSSL_CRL
105         {0, ATTR_TLS,   "TLS_CRLCHECK",         NULL,   LDAP_OPT_X_TLS_CRLCHECK},
106 #endif
107         
108 #endif
109
110         {0, ATTR_NONE,          NULL,           NULL,   0}
111 };
112
113 #define MAX_LDAP_ATTR_LEN  sizeof("TLS_CIPHER_SUITE")
114 #define MAX_LDAP_ENV_PREFIX_LEN 8
115
116 static void openldap_ldap_init_w_conf(
117         const char *file, int userconf )
118 {
119         char linebuf[ AC_LINE_MAX ];
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 }
427
428 /* 
429  * Initialize the global options structure with default values.
430  */
431 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
432 {
433         if (dbglvl)
434             gopts->ldo_debug = *dbglvl;
435         else
436                 gopts->ldo_debug = 0;
437
438         gopts->ldo_version   = LDAP_VERSION2;
439         gopts->ldo_deref     = LDAP_DEREF_NEVER;
440         gopts->ldo_timelimit = LDAP_NO_LIMIT;
441         gopts->ldo_sizelimit = LDAP_NO_LIMIT;
442
443         gopts->ldo_tm_api = (struct timeval *)NULL;
444         gopts->ldo_tm_net = (struct timeval *)NULL;
445
446         /* ldo_defludp will be freed by the termination handler
447          */
448         ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
449         gopts->ldo_defport = LDAP_PORT;
450 #if !defined(__GNUC__) && !defined(PIC)
451         /* Do this only for a static library, and only if we can't
452          * arrange for it to be executed as a library destructor
453          */
454         atexit(ldap_int_destroy_global_options);
455 #endif
456
457         gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
458         gopts->ldo_rebind_proc = NULL;
459         gopts->ldo_rebind_params = NULL;
460
461         LDAP_BOOL_ZERO(gopts);
462
463         LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
464
465 #ifdef LDAP_CONNECTIONLESS
466         gopts->ldo_peer = NULL;
467         gopts->ldo_cldapdn = NULL;
468         gopts->ldo_is_udp = 0;
469 #endif
470
471 #ifdef HAVE_CYRUS_SASL
472         gopts->ldo_def_sasl_mech = NULL;
473         gopts->ldo_def_sasl_realm = NULL;
474         gopts->ldo_def_sasl_authcid = NULL;
475         gopts->ldo_def_sasl_authzid = NULL;
476
477         memset( &gopts->ldo_sasl_secprops,
478                 '\0', sizeof(gopts->ldo_sasl_secprops) );
479
480         gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
481         gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
482         gopts->ldo_sasl_secprops.security_flags =
483                 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
484 #endif
485
486 #ifdef HAVE_TLS
487         gopts->ldo_tls_connect_cb = NULL;
488         gopts->ldo_tls_connect_arg = NULL;
489 #endif
490
491         gopts->ldo_valid = LDAP_INITIALIZED;
492         return;
493 }
494
495 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
496         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
497 char * ldap_int_hostname = NULL;
498 #endif
499
500 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
501 {
502         if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
503                 return;
504         }
505
506         ldap_int_error_init();
507
508         ldap_int_utils_init();
509
510 #ifdef HAVE_WINSOCK2
511 {       WORD wVersionRequested;
512         WSADATA wsaData;
513  
514         wVersionRequested = MAKEWORD( 2, 0 );
515         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
516                 /* Tell the user that we couldn't find a usable */
517                 /* WinSock DLL.                                  */
518                 return;
519         }
520  
521         /* Confirm that the WinSock DLL supports 2.0.*/
522         /* Note that if the DLL supports versions greater    */
523         /* than 2.0 in addition to 2.0, it will still return */
524         /* 2.0 in wVersion since that is the version we      */
525         /* requested.                                        */
526  
527         if ( LOBYTE( wsaData.wVersion ) != 2 ||
528                 HIBYTE( wsaData.wVersion ) != 0 )
529         {
530             /* Tell the user that we couldn't find a usable */
531             /* WinSock DLL.                                  */
532             WSACleanup( );
533             return; 
534         }
535 }       /* The WinSock DLL is acceptable. Proceed. */
536 #elif HAVE_WINSOCK
537 {       WSADATA wsaData;
538         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
539             return;
540         }
541 }
542 #endif
543
544 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
545         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
546         {
547                 char    *name = ldap_int_hostname;
548
549                 ldap_int_hostname = ldap_pvt_get_fqdn( name );
550
551                 if ( name != NULL && name != ldap_int_hostname ) {
552                         LDAP_FREE( name );
553                 }
554         }
555 #endif
556
557 #ifndef HAVE_POLL
558         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
559 #endif
560
561         ldap_int_initialize_global_options(gopts, NULL);
562
563         if( getenv("LDAPNOINIT") != NULL ) {
564                 return;
565         }
566
567 #ifdef HAVE_CYRUS_SASL
568         {
569                 /* set authentication identity to current user name */
570                 char *user = getenv("USER");
571
572                 if( user == NULL ) user = getenv("USERNAME");
573                 if( user == NULL ) user = getenv("LOGNAME");
574
575                 if( user != NULL ) {
576                         gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
577                 }
578     }
579 #endif
580
581         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
582         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
583
584         {
585                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
586
587                 if( altfile != NULL ) {
588                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
589                               LDAP_ENV_PREFIX "CONF", altfile, 0);
590                         openldap_ldap_init_w_sysconf( altfile );
591                 }
592                 else
593                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
594                               LDAP_ENV_PREFIX "CONF", 0, 0);
595         }
596
597         {
598                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
599
600                 if( altfile != NULL ) {
601                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
602                               LDAP_ENV_PREFIX "RC", altfile, 0);
603                         openldap_ldap_init_w_userconf( altfile );
604                 }
605                 else
606                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
607                               LDAP_ENV_PREFIX "RC", 0, 0);
608         }
609
610         openldap_ldap_init_w_env(gopts, NULL);
611 }