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