2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
19 #include <ac/stdlib.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
31 #include "ldap_defaults.h"
34 struct ldapoptions ldap_int_global_options =
35 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };
48 #define ATTR_OPT_INT 9
55 static const struct ol_keyvalue deref_kv[] = {
56 {"never", LDAP_DEREF_NEVER},
57 {"searching", LDAP_DEREF_SEARCHING},
58 {"finding", LDAP_DEREF_FINDING},
59 {"always", LDAP_DEREF_ALWAYS},
63 static const struct ol_attribute {
70 {0, ATTR_OPT_TV, "TIMEOUT", NULL, LDAP_OPT_TIMEOUT},
71 {0, ATTR_OPT_TV, "NETWORK_TIMEOUT", NULL, LDAP_OPT_NETWORK_TIMEOUT},
72 {0, ATTR_OPT_INT, "VERSION", NULL, LDAP_OPT_PROTOCOL_VERSION},
73 {0, ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
74 offsetof(struct ldapoptions, ldo_deref)},
75 {0, ATTR_INT, "SIZELIMIT", NULL,
76 offsetof(struct ldapoptions, ldo_sizelimit)},
77 {0, ATTR_INT, "TIMELIMIT", NULL,
78 offsetof(struct ldapoptions, ldo_timelimit)},
79 {1, ATTR_STRING, "BINDDN", NULL,
80 offsetof(struct ldapoptions, ldo_defbinddn)},
81 {0, ATTR_STRING, "BASE", NULL,
82 offsetof(struct ldapoptions, ldo_defbase)},
83 {0, ATTR_INT, "PORT", NULL, /* deprecated */
84 offsetof(struct ldapoptions, ldo_defport)},
85 {0, ATTR_OPTION, "HOST", NULL, LDAP_OPT_HOST_NAME}, /* deprecated */
86 {0, ATTR_OPTION, "URI", NULL, LDAP_OPT_URI}, /* replaces HOST/PORT */
87 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
89 /* This should only be allowed via ldap_set_option(3) */
90 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
93 #ifdef HAVE_CYRUS_SASL
94 {0, ATTR_STRING, "SASL_MECH", NULL,
95 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
96 {0, ATTR_STRING, "SASL_REALM", NULL,
97 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
98 {1, ATTR_STRING, "SASL_AUTHCID", NULL,
99 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
100 {1, ATTR_STRING, "SASL_AUTHZID", NULL,
101 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
102 {0, ATTR_SASL, "SASL_SECPROPS", NULL, LDAP_OPT_X_SASL_SECPROPS},
106 {1, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
107 {1, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
108 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
109 {0, ATTR_TLS, "TLS_CACERTDIR", NULL, LDAP_OPT_X_TLS_CACERTDIR},
110 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
111 {0, ATTR_TLS, "TLS_RANDFILE", NULL, LDAP_OPT_X_TLS_RANDOM_FILE},
112 {0, ATTR_TLS, "TLS_CIPHER_SUITE", NULL, LDAP_OPT_X_TLS_CIPHER_SUITE},
114 #ifdef HAVE_OPENSSL_CRL
115 {0, ATTR_TLS, "TLS_CRLCHECK", NULL, LDAP_OPT_X_TLS_CRLCHECK},
118 {0, ATTR_TLS, "TLS_CRL", NULL, LDAP_OPT_X_TLS_CRLFILE},
123 {0, ATTR_NONE, NULL, NULL, 0}
126 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CIPHER_SUITE")
127 #define MAX_LDAP_ENV_PREFIX_LEN 8
129 static void openldap_ldap_init_w_conf(
130 const char *file, int userconf )
132 char linebuf[ AC_LINE_MAX ];
137 struct ldapoptions *gopts;
139 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
140 return; /* Could not allocate mem for global options */
148 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
150 fp = fopen(file, "r");
152 /* could not open file */
156 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
158 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
159 /* skip lines starting with '#' */
160 if(*start == '#') continue;
162 /* trim leading white space */
163 while((*start != '\0') && isspace((unsigned char) *start))
167 if(*start == '\0') continue;
169 /* trim trailing white space */
170 end = &start[strlen(start)-1];
171 while(isspace((unsigned char)*end)) end--;
175 if(*start == '\0') continue;
178 /* parse the command */
180 while((*start != '\0') && !isspace((unsigned char)*start)) {
184 /* command has no argument */
190 /* we must have some whitespace to skip */
191 while(isspace((unsigned char)*start)) start++;
194 for(i=0; attrs[i].type != ATTR_NONE; i++) {
197 if( !userconf && attrs[i].useronly ) {
201 if(strcasecmp(cmd, attrs[i].name) != 0) {
205 switch(attrs[i].type) {
207 if((strcasecmp(opt, "on") == 0)
208 || (strcasecmp(opt, "yes") == 0)
209 || (strcasecmp(opt, "true") == 0))
211 LDAP_BOOL_SET(gopts, attrs[i].offset);
214 LDAP_BOOL_CLR(gopts, attrs[i].offset);
222 p = &((char *) gopts)[attrs[i].offset];
223 l = strtol( opt, &next, 10 );
224 if ( next != opt && next[ 0 ] == '\0' ) {
230 const struct ol_keyvalue *kv;
232 for(kv = attrs[i].data;
236 if(strcasecmp(opt, kv->key) == 0) {
237 p = &((char *) gopts)[attrs[i].offset];
238 * (int*) p = kv->value;
245 p = &((char *) gopts)[attrs[i].offset];
246 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
247 * (char**) p = LDAP_STRDUP(opt);
250 ldap_set_option( NULL, attrs[i].offset, opt );
253 #ifdef HAVE_CYRUS_SASL
254 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
259 ldap_int_tls_config( NULL, attrs[i].offset, opt );
266 tv.tv_sec = strtol( opt, &next, 10 );
267 if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
268 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
274 l = strtol( opt, &next, 10 );
275 if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
277 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
289 static void openldap_ldap_init_w_sysconf(const char *file)
291 openldap_ldap_init_w_conf( file, 0 );
294 static void openldap_ldap_init_w_userconf(const char *file)
304 home = getenv("HOME");
307 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
309 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
311 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
315 if(home != NULL && path != NULL) {
316 /* we assume UNIX path syntax is used... */
319 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
320 openldap_ldap_init_w_conf(path, 1);
323 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
324 openldap_ldap_init_w_conf(path, 1);
332 openldap_ldap_init_w_conf(file, 1);
335 static void openldap_ldap_init_w_env(
336 struct ldapoptions *gopts,
339 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
345 if (prefix == NULL) {
346 prefix = LDAP_ENV_PREFIX;
349 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
350 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
353 for(i=0; attrs[i].type != ATTR_NONE; i++) {
354 strcpy(&buf[len], attrs[i].name);
361 switch(attrs[i].type) {
363 if((strcasecmp(value, "on") == 0)
364 || (strcasecmp(value, "yes") == 0)
365 || (strcasecmp(value, "true") == 0))
367 LDAP_BOOL_SET(gopts, attrs[i].offset);
370 LDAP_BOOL_CLR(gopts, attrs[i].offset);
375 p = &((char *) gopts)[attrs[i].offset];
376 * (int*) p = atoi(value);
380 const struct ol_keyvalue *kv;
382 for(kv = attrs[i].data;
386 if(strcasecmp(value, kv->key) == 0) {
387 p = &((char *) gopts)[attrs[i].offset];
388 * (int*) p = kv->value;
395 p = &((char *) gopts)[attrs[i].offset];
396 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
397 if (*value == '\0') {
400 * (char**) p = LDAP_STRDUP(value);
404 ldap_set_option( NULL, attrs[i].offset, value );
407 #ifdef HAVE_CYRUS_SASL
408 ldap_int_sasl_config( gopts, attrs[i].offset, value );
413 ldap_int_tls_config( NULL, attrs[i].offset, value );
420 #if defined(__GNUC__)
421 /* Declare this function as a destructor so that it will automatically be
422 * invoked either at program exit (if libldap is a static library) or
423 * at unload time (if libldap is a dynamic library).
425 * Sorry, don't know how to handle this for non-GCC environments.
427 static void ldap_int_destroy_global_options(void)
428 __attribute__ ((destructor));
432 ldap_int_destroy_global_options(void)
434 struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
439 gopts->ldo_valid = LDAP_UNINITIALIZED;
441 if ( gopts->ldo_defludp ) {
442 ldap_free_urllist( gopts->ldo_defludp );
443 gopts->ldo_defludp = NULL;
445 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
449 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
450 if ( ldap_int_hostname ) {
451 LDAP_FREE( ldap_int_hostname );
452 ldap_int_hostname = NULL;
455 #ifdef HAVE_CYRUS_SASL
456 if ( gopts->ldo_def_sasl_authcid ) {
457 LDAP_FREE( gopts->ldo_def_sasl_authcid );
458 gopts->ldo_def_sasl_authcid = NULL;
462 ldap_int_tls_destroy( gopts );
467 * Initialize the global options structure with default values.
469 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
472 gopts->ldo_debug = *dbglvl;
474 gopts->ldo_debug = 0;
476 gopts->ldo_version = LDAP_VERSION2;
477 gopts->ldo_deref = LDAP_DEREF_NEVER;
478 gopts->ldo_timelimit = LDAP_NO_LIMIT;
479 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
481 gopts->ldo_tm_api.tv_sec = -1;
482 gopts->ldo_tm_net.tv_sec = -1;
484 /* ldo_defludp will be freed by the termination handler
486 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
487 gopts->ldo_defport = LDAP_PORT;
488 #if !defined(__GNUC__) && !defined(PIC)
489 /* Do this only for a static library, and only if we can't
490 * arrange for it to be executed as a library destructor
492 atexit(ldap_int_destroy_global_options);
495 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
496 gopts->ldo_rebind_proc = NULL;
497 gopts->ldo_rebind_params = NULL;
499 LDAP_BOOL_ZERO(gopts);
501 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
503 #ifdef LDAP_CONNECTIONLESS
504 gopts->ldo_peer = NULL;
505 gopts->ldo_cldapdn = NULL;
506 gopts->ldo_is_udp = 0;
509 #ifdef HAVE_CYRUS_SASL
510 gopts->ldo_def_sasl_mech = NULL;
511 gopts->ldo_def_sasl_realm = NULL;
512 gopts->ldo_def_sasl_authcid = NULL;
513 gopts->ldo_def_sasl_authzid = NULL;
515 memset( &gopts->ldo_sasl_secprops,
516 '\0', sizeof(gopts->ldo_sasl_secprops) );
518 gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
519 gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
520 gopts->ldo_sasl_secprops.security_flags =
521 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
525 gopts->ldo_tls_connect_cb = NULL;
526 gopts->ldo_tls_connect_arg = NULL;
527 gopts->ldo_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
530 gopts->ldo_valid = LDAP_INITIALIZED;
534 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
535 char * ldap_int_hostname = NULL;
538 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
540 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
544 ldap_int_error_init();
546 ldap_int_utils_init();
549 { WORD wVersionRequested;
552 wVersionRequested = MAKEWORD( 2, 0 );
553 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
554 /* Tell the user that we couldn't find a usable */
559 /* Confirm that the WinSock DLL supports 2.0.*/
560 /* Note that if the DLL supports versions greater */
561 /* than 2.0 in addition to 2.0, it will still return */
562 /* 2.0 in wVersion since that is the version we */
565 if ( LOBYTE( wsaData.wVersion ) != 2 ||
566 HIBYTE( wsaData.wVersion ) != 0 )
568 /* Tell the user that we couldn't find a usable */
573 } /* The WinSock DLL is acceptable. Proceed. */
576 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
582 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
584 char *name = ldap_int_hostname;
586 ldap_int_hostname = ldap_pvt_get_fqdn( name );
588 if ( name != NULL && name != ldap_int_hostname ) {
595 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
598 ldap_int_initialize_global_options(gopts, NULL);
600 if( getenv("LDAPNOINIT") != NULL ) {
604 #ifdef HAVE_CYRUS_SASL
606 /* set authentication identity to current user name */
607 char *user = getenv("USER");
609 if( user == NULL ) user = getenv("USERNAME");
610 if( user == NULL ) user = getenv("LOGNAME");
613 gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
618 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
619 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
622 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
624 if( altfile != NULL ) {
625 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
626 LDAP_ENV_PREFIX "CONF", altfile, 0);
627 openldap_ldap_init_w_sysconf( altfile );
630 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
631 LDAP_ENV_PREFIX "CONF", 0, 0);
635 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
637 if( altfile != NULL ) {
638 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
639 LDAP_ENV_PREFIX "RC", altfile, 0);
640 openldap_ldap_init_w_userconf( altfile );
643 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
644 LDAP_ENV_PREFIX "RC", 0, 0);
647 openldap_ldap_init_w_env(gopts, NULL);