2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 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>
22 #include <ac/unistd.h>
25 #include <ac/socket.h>
26 #include <ac/string.h>
35 #include "ldap_defaults.h"
38 struct ldapoptions ldap_int_global_options =
39 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE
41 LDAP_LDO_CONNECTIONLESS_NULLARG
44 LDAP_LDO_GSSAPI_NULLARG
45 LDAP_LDO_MUTEX_NULLARG };
58 #define ATTR_OPT_INT 9
60 #define ATTR_GSSAPI 10
67 static const struct ol_keyvalue deref_kv[] = {
68 {"never", LDAP_DEREF_NEVER},
69 {"searching", LDAP_DEREF_SEARCHING},
70 {"finding", LDAP_DEREF_FINDING},
71 {"always", LDAP_DEREF_ALWAYS},
75 static const struct ol_attribute {
82 {0, ATTR_OPT_TV, "TIMEOUT", NULL, LDAP_OPT_TIMEOUT},
83 {0, ATTR_OPT_TV, "NETWORK_TIMEOUT", NULL, LDAP_OPT_NETWORK_TIMEOUT},
84 {0, ATTR_OPT_INT, "VERSION", NULL, LDAP_OPT_PROTOCOL_VERSION},
85 {0, ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
86 offsetof(struct ldapoptions, ldo_deref)},
87 {0, ATTR_INT, "SIZELIMIT", NULL,
88 offsetof(struct ldapoptions, ldo_sizelimit)},
89 {0, ATTR_INT, "TIMELIMIT", NULL,
90 offsetof(struct ldapoptions, ldo_timelimit)},
91 {1, ATTR_STRING, "BINDDN", NULL,
92 offsetof(struct ldapoptions, ldo_defbinddn)},
93 {0, ATTR_STRING, "BASE", NULL,
94 offsetof(struct ldapoptions, ldo_defbase)},
95 {0, ATTR_INT, "PORT", NULL, /* deprecated */
96 offsetof(struct ldapoptions, ldo_defport)},
97 {0, ATTR_OPTION, "HOST", NULL, LDAP_OPT_HOST_NAME}, /* deprecated */
98 {0, ATTR_OPTION, "URI", NULL, LDAP_OPT_URI}, /* replaces HOST/PORT */
99 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
101 /* This should only be allowed via ldap_set_option(3) */
102 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
105 #ifdef HAVE_CYRUS_SASL
106 {0, ATTR_STRING, "SASL_MECH", NULL,
107 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
108 {0, ATTR_STRING, "SASL_REALM", NULL,
109 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
110 {1, ATTR_STRING, "SASL_AUTHCID", NULL,
111 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
112 {1, ATTR_STRING, "SASL_AUTHZID", NULL,
113 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
114 {0, ATTR_SASL, "SASL_SECPROPS", NULL, LDAP_OPT_X_SASL_SECPROPS},
115 {0, ATTR_BOOL, "SASL_NOCANON", NULL, LDAP_BOOL_SASL_NOCANON},
119 {0, ATTR_GSSAPI,"GSSAPI_SIGN", NULL, LDAP_OPT_SIGN},
120 {0, ATTR_GSSAPI,"GSSAPI_ENCRYPT", NULL, LDAP_OPT_ENCRYPT},
121 {0, ATTR_GSSAPI,"GSSAPI_ALLOW_REMOTE_PRINCIPAL",NULL, LDAP_OPT_X_GSSAPI_ALLOW_REMOTE_PRINCIPAL},
125 {1, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
126 {1, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
127 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
128 {0, ATTR_TLS, "TLS_CACERTDIR", NULL, LDAP_OPT_X_TLS_CACERTDIR},
129 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
130 {0, ATTR_TLS, "TLS_RANDFILE", NULL, LDAP_OPT_X_TLS_RANDOM_FILE},
131 {0, ATTR_TLS, "TLS_CIPHER_SUITE", NULL, LDAP_OPT_X_TLS_CIPHER_SUITE},
132 {0, ATTR_TLS, "TLS_PROTOCOL_MIN", NULL, LDAP_OPT_X_TLS_PROTOCOL_MIN},
134 #ifdef HAVE_OPENSSL_CRL
135 {0, ATTR_TLS, "TLS_CRLCHECK", NULL, LDAP_OPT_X_TLS_CRLCHECK},
138 {0, ATTR_TLS, "TLS_CRLFILE", NULL, LDAP_OPT_X_TLS_CRLFILE},
143 {0, ATTR_NONE, NULL, NULL, 0}
146 #define MAX_LDAP_ATTR_LEN sizeof("GSSAPI_ALLOW_REMOTE_PRINCIPAL")
147 #define MAX_LDAP_ENV_PREFIX_LEN 8
149 static void openldap_ldap_init_w_conf(
150 const char *file, int userconf )
152 char linebuf[ AC_LINE_MAX ];
157 struct ldapoptions *gopts;
159 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
160 return; /* Could not allocate mem for global options */
168 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
170 fp = fopen(file, "r");
172 /* could not open file */
176 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
178 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
179 /* skip lines starting with '#' */
180 if(*start == '#') continue;
182 /* trim leading white space */
183 while((*start != '\0') && isspace((unsigned char) *start))
187 if(*start == '\0') continue;
189 /* trim trailing white space */
190 end = &start[strlen(start)-1];
191 while(isspace((unsigned char)*end)) end--;
195 if(*start == '\0') continue;
198 /* parse the command */
200 while((*start != '\0') && !isspace((unsigned char)*start)) {
204 /* command has no argument */
210 /* we must have some whitespace to skip */
211 while(isspace((unsigned char)*start)) start++;
214 for(i=0; attrs[i].type != ATTR_NONE; i++) {
217 if( !userconf && attrs[i].useronly ) {
221 if(strcasecmp(cmd, attrs[i].name) != 0) {
225 switch(attrs[i].type) {
227 if((strcasecmp(opt, "on") == 0)
228 || (strcasecmp(opt, "yes") == 0)
229 || (strcasecmp(opt, "true") == 0))
231 LDAP_BOOL_SET(gopts, attrs[i].offset);
234 LDAP_BOOL_CLR(gopts, attrs[i].offset);
242 p = &((char *) gopts)[attrs[i].offset];
243 l = strtol( opt, &next, 10 );
244 if ( next != opt && next[ 0 ] == '\0' ) {
250 const struct ol_keyvalue *kv;
252 for(kv = attrs[i].data;
256 if(strcasecmp(opt, kv->key) == 0) {
257 p = &((char *) gopts)[attrs[i].offset];
258 * (int*) p = kv->value;
265 p = &((char *) gopts)[attrs[i].offset];
266 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
267 * (char**) p = LDAP_STRDUP(opt);
270 ldap_set_option( NULL, attrs[i].offset, opt );
273 #ifdef HAVE_CYRUS_SASL
274 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
279 ldap_int_gssapi_config( gopts, attrs[i].offset, opt );
284 ldap_pvt_tls_config( NULL, attrs[i].offset, opt );
291 tv.tv_sec = strtol( opt, &next, 10 );
292 if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
293 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
299 l = strtol( opt, &next, 10 );
300 if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
302 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
314 static void openldap_ldap_init_w_sysconf(const char *file)
316 openldap_ldap_init_w_conf( file, 0 );
319 static void openldap_ldap_init_w_userconf(const char *file)
329 home = getenv("HOME");
332 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
334 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
336 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
340 if(home != NULL && path != NULL) {
341 /* we assume UNIX path syntax is used... */
344 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
345 openldap_ldap_init_w_conf(path, 1);
348 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
349 openldap_ldap_init_w_conf(path, 1);
357 openldap_ldap_init_w_conf(file, 1);
360 static void openldap_ldap_init_w_env(
361 struct ldapoptions *gopts,
364 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
370 if (prefix == NULL) {
371 prefix = LDAP_ENV_PREFIX;
374 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
375 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
378 for(i=0; attrs[i].type != ATTR_NONE; i++) {
379 strcpy(&buf[len], attrs[i].name);
386 switch(attrs[i].type) {
388 if((strcasecmp(value, "on") == 0)
389 || (strcasecmp(value, "yes") == 0)
390 || (strcasecmp(value, "true") == 0))
392 LDAP_BOOL_SET(gopts, attrs[i].offset);
395 LDAP_BOOL_CLR(gopts, attrs[i].offset);
400 p = &((char *) gopts)[attrs[i].offset];
401 * (int*) p = atoi(value);
405 const struct ol_keyvalue *kv;
407 for(kv = attrs[i].data;
411 if(strcasecmp(value, kv->key) == 0) {
412 p = &((char *) gopts)[attrs[i].offset];
413 * (int*) p = kv->value;
420 p = &((char *) gopts)[attrs[i].offset];
421 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
422 if (*value == '\0') {
425 * (char**) p = LDAP_STRDUP(value);
429 ldap_set_option( NULL, attrs[i].offset, value );
432 #ifdef HAVE_CYRUS_SASL
433 ldap_int_sasl_config( gopts, attrs[i].offset, value );
438 ldap_int_gssapi_config( gopts, attrs[i].offset, value );
443 ldap_pvt_tls_config( NULL, attrs[i].offset, value );
450 tv.tv_sec = strtol( value, &next, 10 );
451 if ( next != value && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
452 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
458 l = strtol( value, &next, 10 );
459 if ( next != value && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
461 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
468 #if defined(__GNUC__)
469 /* Declare this function as a destructor so that it will automatically be
470 * invoked either at program exit (if libldap is a static library) or
471 * at unload time (if libldap is a dynamic library).
473 * Sorry, don't know how to handle this for non-GCC environments.
475 static void ldap_int_destroy_global_options(void)
476 __attribute__ ((destructor));
480 ldap_int_destroy_global_options(void)
482 struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
487 gopts->ldo_valid = LDAP_UNINITIALIZED;
489 if ( gopts->ldo_defludp ) {
490 ldap_free_urllist( gopts->ldo_defludp );
491 gopts->ldo_defludp = NULL;
493 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
497 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
498 if ( ldap_int_hostname ) {
499 LDAP_FREE( ldap_int_hostname );
500 ldap_int_hostname = NULL;
503 #ifdef HAVE_CYRUS_SASL
504 if ( gopts->ldo_def_sasl_authcid ) {
505 LDAP_FREE( gopts->ldo_def_sasl_authcid );
506 gopts->ldo_def_sasl_authcid = NULL;
510 ldap_int_tls_destroy( gopts );
515 * Initialize the global options structure with default values.
517 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
519 #ifdef LDAP_R_COMPILE
520 LDAP_PVT_MUTEX_FIRSTCREATE(gopts->ldo_mutex);
522 LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
523 if (gopts->ldo_valid == LDAP_INITIALIZED) {
524 /* someone else got here first */
525 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
529 gopts->ldo_debug = *dbglvl;
531 gopts->ldo_debug = 0;
533 gopts->ldo_version = LDAP_VERSION2;
534 gopts->ldo_deref = LDAP_DEREF_NEVER;
535 gopts->ldo_timelimit = LDAP_NO_LIMIT;
536 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
538 gopts->ldo_tm_api.tv_sec = -1;
539 gopts->ldo_tm_net.tv_sec = -1;
541 /* ldo_defludp will be freed by the termination handler
543 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
544 gopts->ldo_defport = LDAP_PORT;
545 #if !defined(__GNUC__) && !defined(PIC)
546 /* Do this only for a static library, and only if we can't
547 * arrange for it to be executed as a library destructor
549 atexit(ldap_int_destroy_global_options);
552 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
553 gopts->ldo_rebind_proc = NULL;
554 gopts->ldo_rebind_params = NULL;
556 LDAP_BOOL_ZERO(gopts);
558 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
560 #ifdef LDAP_CONNECTIONLESS
561 gopts->ldo_peer = NULL;
562 gopts->ldo_cldapdn = NULL;
563 gopts->ldo_is_udp = 0;
566 #ifdef HAVE_CYRUS_SASL
567 gopts->ldo_def_sasl_mech = NULL;
568 gopts->ldo_def_sasl_realm = NULL;
569 gopts->ldo_def_sasl_authcid = NULL;
570 gopts->ldo_def_sasl_authzid = NULL;
572 memset( &gopts->ldo_sasl_secprops,
573 '\0', sizeof(gopts->ldo_sasl_secprops) );
575 gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
576 gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
577 gopts->ldo_sasl_secprops.security_flags =
578 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
582 gopts->ldo_tls_connect_cb = NULL;
583 gopts->ldo_tls_connect_arg = NULL;
584 gopts->ldo_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
586 gopts->ldo_keepalive_probes = 0;
587 gopts->ldo_keepalive_interval = 0;
588 gopts->ldo_keepalive_idle = 0;
590 gopts->ldo_valid = LDAP_INITIALIZED;
591 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
595 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
596 char * ldap_int_hostname = NULL;
599 #ifdef LDAP_R_COMPILE
600 int ldap_int_stackguard;
603 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
605 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
609 ldap_int_error_init();
611 ldap_int_utils_init();
614 { WORD wVersionRequested;
617 wVersionRequested = MAKEWORD( 2, 0 );
618 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
619 /* Tell the user that we couldn't find a usable */
624 /* Confirm that the WinSock DLL supports 2.0.*/
625 /* Note that if the DLL supports versions greater */
626 /* than 2.0 in addition to 2.0, it will still return */
627 /* 2.0 in wVersion since that is the version we */
630 if ( LOBYTE( wsaData.wVersion ) != 2 ||
631 HIBYTE( wsaData.wVersion ) != 0 )
633 /* Tell the user that we couldn't find a usable */
638 } /* The WinSock DLL is acceptable. Proceed. */
639 #elif defined(HAVE_WINSOCK)
641 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
647 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
648 LDAP_MUTEX_LOCK( &ldap_int_hostname_mutex );
650 char *name = ldap_int_hostname;
652 ldap_int_hostname = ldap_pvt_get_fqdn( name );
654 if ( name != NULL && name != ldap_int_hostname ) {
658 LDAP_MUTEX_UNLOCK( &ldap_int_hostname_mutex );
662 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
665 ldap_int_initialize_global_options(gopts, dbglvl);
667 if( getenv("LDAPNOINIT") != NULL ) {
671 #ifdef LDAP_R_COMPILE
672 if( getenv("LDAPSTACKGUARD") != NULL ) {
673 ldap_int_stackguard = 1;
677 #ifdef HAVE_CYRUS_SASL
679 /* set authentication identity to current user name */
680 char *user = getenv("USER");
682 if( user == NULL ) user = getenv("USERNAME");
683 if( user == NULL ) user = getenv("LOGNAME");
686 gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
691 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
694 if ( geteuid() != getuid() )
698 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
701 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
703 if( altfile != NULL ) {
704 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
705 LDAP_ENV_PREFIX "CONF", altfile, 0);
706 openldap_ldap_init_w_sysconf( altfile );
709 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
710 LDAP_ENV_PREFIX "CONF", 0, 0);
714 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
716 if( altfile != NULL ) {
717 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
718 LDAP_ENV_PREFIX "RC", altfile, 0);
719 openldap_ldap_init_w_userconf( altfile );
722 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
723 LDAP_ENV_PREFIX "RC", 0, 0);
726 openldap_ldap_init_w_env(gopts, NULL);