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