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