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