]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
81938ec9ad12784931fd6f66fc3c9d869204bab8
[openldap] / libraries / libldap / init.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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         Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
129
130         fp = fopen(file, "r");
131         if(fp == NULL) {
132                 /* could not open file */
133                 return;
134         }
135
136         Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
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                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
265                       home, 0, 0);
266                 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
267         } else {
268                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
269                       0, 0, 0);
270         }
271
272         if(home != NULL && path != NULL) {
273                 /* we assume UNIX path syntax is used... */
274
275                 /* try ~/file */
276                 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
277                 openldap_ldap_init_w_conf(path, 1);
278
279                 /* try ~/.file */
280                 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
281                 openldap_ldap_init_w_conf(path, 1);
282         }
283
284         if(path != NULL) {
285                 LDAP_FREE(path);
286         }
287
288         /* try file */
289         openldap_ldap_init_w_conf(file, 1);
290 }
291
292 static void openldap_ldap_init_w_env(
293                 struct ldapoptions *gopts,
294                 const char *prefix)
295 {
296         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
297         int len;
298         int i;
299         void *p;
300         char *value;
301
302         if (prefix == NULL) {
303                 prefix = LDAP_ENV_PREFIX;
304         }
305
306         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
307         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
308         len = strlen(buf);
309
310         for(i=0; attrs[i].type != ATTR_NONE; i++) {
311                 strcpy(&buf[len], attrs[i].name);
312                 value = getenv(buf);
313
314                 if(value == NULL) {
315                         continue;
316                 }
317
318                 switch(attrs[i].type) {
319                 case ATTR_BOOL:
320                         if((strcasecmp(value, "on") == 0) 
321                                 || (strcasecmp(value, "yes") == 0)
322                                 || (strcasecmp(value, "true") == 0))
323                         {
324                                 LDAP_BOOL_SET(gopts, attrs[i].offset);
325
326                         } else {
327                                 LDAP_BOOL_CLR(gopts, attrs[i].offset);
328                         }
329                         break;
330
331                 case ATTR_INT:
332                         p = &((char *) gopts)[attrs[i].offset];
333                         * (int*) p = atoi(value);
334                         break;
335
336                 case ATTR_KV: {
337                                 const struct ol_keyvalue *kv;
338
339                                 for(kv = attrs[i].data;
340                                         kv->key != NULL;
341                                         kv++) {
342
343                                         if(strcasecmp(value, kv->key) == 0) {
344                                                 p = &((char *) gopts)[attrs[i].offset];
345                                                 * (int*) p = kv->value;
346                                                 break;
347                                         }
348                                 }
349                         } break;
350
351                 case ATTR_STRING:
352                         p = &((char *) gopts)[attrs[i].offset];
353                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
354                         if (*value == '\0') {
355                                 * (char**) p = NULL;
356                         } else {
357                                 * (char**) p = LDAP_STRDUP(value);
358                         }
359                         break;
360                 case ATTR_OPTION:
361                         ldap_set_option( NULL, attrs[i].offset, value );
362                         break;
363                 case ATTR_SASL:
364 #ifdef HAVE_CYRUS_SASL
365                         ldap_int_sasl_config( gopts, attrs[i].offset, value );
366 #endif                          
367                         break;
368                 case ATTR_TLS:
369 #ifdef HAVE_TLS
370                         ldap_int_tls_config( NULL, attrs[i].offset, value );
371 #endif                          
372                         break;
373                 }
374         }
375 }
376
377 #if defined(__GNUC__)
378 /* Declare this function as a destructor so that it will automatically be
379  * invoked either at program exit (if libldap is a static library) or
380  * at unload time (if libldap is a dynamic library).
381  *
382  * Sorry, don't know how to handle this for non-GCC environments.
383  */
384 static void ldap_int_destroy_global_options(void)
385         __attribute__ ((destructor));
386 #endif
387
388 static void
389 ldap_int_destroy_global_options(void)
390 {
391         struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
392
393         gopts->ldo_valid = LDAP_UNINITIALIZED;
394
395         if ( gopts->ldo_defludp ) {
396                 ldap_free_urllist( gopts->ldo_defludp );
397                 gopts->ldo_defludp = NULL;
398         }
399 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
400         WSACleanup( );
401 #endif
402 }
403
404 /* 
405  * Initialize the global options structure with default values.
406  */
407 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
408 {
409         if (dbglvl)
410             gopts->ldo_debug = *dbglvl;
411         else
412                 gopts->ldo_debug = 0;
413
414         gopts->ldo_version   = LDAP_VERSION2;
415         gopts->ldo_deref     = LDAP_DEREF_NEVER;
416         gopts->ldo_timelimit = LDAP_NO_LIMIT;
417         gopts->ldo_sizelimit = LDAP_NO_LIMIT;
418
419         gopts->ldo_tm_api = (struct timeval *)NULL;
420         gopts->ldo_tm_net = (struct timeval *)NULL;
421
422         /* ldo_defludp will be freed by the termination handler
423          */
424         ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
425         gopts->ldo_defport = LDAP_PORT;
426 #if !defined(__GNUC__) && !defined(PIC)
427         /* Do this only for a static library, and only if we can't
428          * arrange for it to be executed as a library destructor
429          */
430         atexit(ldap_int_destroy_global_options);
431 #endif
432
433         gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
434         gopts->ldo_rebind_proc = NULL;
435         gopts->ldo_rebind_params = NULL;
436
437         LDAP_BOOL_ZERO(gopts);
438
439         LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
440
441 #ifdef LDAP_CONNECTIONLESS
442         gopts->ldo_peer = NULL;
443         gopts->ldo_cldapdn = NULL;
444         gopts->ldo_is_udp = 0;
445 #endif
446
447 #ifdef HAVE_CYRUS_SASL
448         gopts->ldo_def_sasl_mech = NULL;
449         gopts->ldo_def_sasl_realm = NULL;
450         gopts->ldo_def_sasl_authcid = NULL;
451         gopts->ldo_def_sasl_authzid = NULL;
452
453         memset( &gopts->ldo_sasl_secprops,
454                 '\0', sizeof(gopts->ldo_sasl_secprops) );
455
456         gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
457         gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
458         gopts->ldo_sasl_secprops.security_flags =
459                 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
460 #endif
461
462         gopts->ldo_valid = LDAP_INITIALIZED;
463         return;
464 }
465
466 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
467         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
468 char * ldap_int_hostname = NULL;
469 #endif
470
471 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
472 {
473         if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
474                 return;
475         }
476
477         ldap_int_error_init();
478
479         ldap_int_utils_init();
480
481 #ifdef HAVE_WINSOCK2
482 {       WORD wVersionRequested;
483         WSADATA wsaData;
484  
485         wVersionRequested = MAKEWORD( 2, 0 );
486         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
487                 /* Tell the user that we couldn't find a usable */
488                 /* WinSock DLL.                                  */
489                 return;
490         }
491  
492         /* Confirm that the WinSock DLL supports 2.0.*/
493         /* Note that if the DLL supports versions greater    */
494         /* than 2.0 in addition to 2.0, it will still return */
495         /* 2.0 in wVersion since that is the version we      */
496         /* requested.                                        */
497  
498         if ( LOBYTE( wsaData.wVersion ) != 2 ||
499                 HIBYTE( wsaData.wVersion ) != 0 )
500         {
501             /* Tell the user that we couldn't find a usable */
502             /* WinSock DLL.                                  */
503             WSACleanup( );
504             return; 
505         }
506 }       /* The WinSock DLL is acceptable. Proceed. */
507 #elif HAVE_WINSOCK
508 {       WSADATA wsaData;
509         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
510             return;
511         }
512 }
513 #endif
514
515 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
516         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
517         ldap_int_hostname = ldap_pvt_get_fqdn( ldap_int_hostname );
518 #endif
519
520 #ifndef HAVE_POLL
521         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
522 #endif
523
524         ldap_int_initialize_global_options(gopts, NULL);
525
526         if( getenv("LDAPNOINIT") != NULL ) {
527                 return;
528         }
529
530 #ifdef HAVE_CYRUS_SASL
531         {
532                 /* set authentication identity to current user name */
533                 char *user = getenv("USER");
534
535                 if( user == NULL ) user = getenv("USERNAME");
536                 if( user == NULL ) user = getenv("LOGNAME");
537
538                 if( user != NULL ) {
539                         gopts->ldo_def_sasl_authcid = user;
540                 }
541     }
542 #endif
543
544         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
545         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
546
547         {
548                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
549
550                 if( altfile != NULL ) {
551                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
552                               LDAP_ENV_PREFIX "CONF", altfile, 0);
553                         openldap_ldap_init_w_sysconf( altfile );
554                 }
555                 else
556                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
557                               LDAP_ENV_PREFIX "CONF", 0, 0);
558         }
559
560         {
561                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
562
563                 if( altfile != NULL ) {
564                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
565                               LDAP_ENV_PREFIX "RC", altfile, 0);
566                         openldap_ldap_init_w_userconf( altfile );
567                 }
568                 else
569                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
570                               LDAP_ENV_PREFIX "RC", 0, 0);
571         }
572
573         openldap_ldap_init_w_env(gopts, NULL);
574 }