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