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