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