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