2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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},
133 {0, ATTR_TLS, "TLS_PEERKEY_HASH", NULL, LDAP_OPT_X_TLS_PEERKEY_HASH},
135 #ifdef HAVE_OPENSSL_CRL
136 {0, ATTR_TLS, "TLS_CRLCHECK", NULL, LDAP_OPT_X_TLS_CRLCHECK},
139 {0, ATTR_TLS, "TLS_CRLFILE", NULL, LDAP_OPT_X_TLS_CRLFILE},
144 {0, ATTR_NONE, NULL, NULL, 0}
147 #define MAX_LDAP_ATTR_LEN sizeof("GSSAPI_ALLOW_REMOTE_PRINCIPAL")
148 #define MAX_LDAP_ENV_PREFIX_LEN 8
150 static void openldap_ldap_init_w_conf(
151 const char *file, int userconf )
153 char linebuf[ AC_LINE_MAX ];
158 struct ldapoptions *gopts;
160 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
161 return; /* Could not allocate mem for global options */
169 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
171 fp = fopen(file, "r");
173 /* could not open file */
177 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
179 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
180 /* skip lines starting with '#' */
181 if(*start == '#') continue;
183 /* trim leading white space */
184 while((*start != '\0') && isspace((unsigned char) *start))
188 if(*start == '\0') continue;
190 /* trim trailing white space */
191 end = &start[strlen(start)-1];
192 while(isspace((unsigned char)*end)) end--;
196 if(*start == '\0') continue;
199 /* parse the command */
201 while((*start != '\0') && !isspace((unsigned char)*start)) {
205 /* command has no argument */
211 /* we must have some whitespace to skip */
212 while(isspace((unsigned char)*start)) start++;
215 for(i=0; attrs[i].type != ATTR_NONE; i++) {
218 if( !userconf && attrs[i].useronly ) {
222 if(strcasecmp(cmd, attrs[i].name) != 0) {
226 switch(attrs[i].type) {
228 if((strcasecmp(opt, "on") == 0)
229 || (strcasecmp(opt, "yes") == 0)
230 || (strcasecmp(opt, "true") == 0))
232 LDAP_BOOL_SET(gopts, attrs[i].offset);
235 LDAP_BOOL_CLR(gopts, attrs[i].offset);
243 p = &((char *) gopts)[attrs[i].offset];
244 l = strtol( opt, &next, 10 );
245 if ( next != opt && next[ 0 ] == '\0' ) {
251 const struct ol_keyvalue *kv;
253 for(kv = attrs[i].data;
257 if(strcasecmp(opt, kv->key) == 0) {
258 p = &((char *) gopts)[attrs[i].offset];
259 * (int*) p = kv->value;
266 p = &((char *) gopts)[attrs[i].offset];
267 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
268 * (char**) p = LDAP_STRDUP(opt);
271 ldap_set_option( NULL, attrs[i].offset, opt );
274 #ifdef HAVE_CYRUS_SASL
275 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
280 ldap_int_gssapi_config( gopts, attrs[i].offset, opt );
285 ldap_pvt_tls_config( NULL, attrs[i].offset, opt );
292 tv.tv_sec = strtol( opt, &next, 10 );
293 if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
294 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
300 l = strtol( opt, &next, 10 );
301 if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
303 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
315 static void openldap_ldap_init_w_sysconf(const char *file)
317 openldap_ldap_init_w_conf( file, 0 );
320 static void openldap_ldap_init_w_userconf(const char *file)
330 home = getenv("HOME");
333 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
335 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
337 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
341 if(home != NULL && path != NULL) {
342 /* we assume UNIX path syntax is used... */
345 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
346 openldap_ldap_init_w_conf(path, 1);
349 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
350 openldap_ldap_init_w_conf(path, 1);
358 openldap_ldap_init_w_conf(file, 1);
361 static void openldap_ldap_init_w_env(
362 struct ldapoptions *gopts,
365 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
371 if (prefix == NULL) {
372 prefix = LDAP_ENV_PREFIX;
375 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
376 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
379 for(i=0; attrs[i].type != ATTR_NONE; i++) {
380 strcpy(&buf[len], attrs[i].name);
387 switch(attrs[i].type) {
389 if((strcasecmp(value, "on") == 0)
390 || (strcasecmp(value, "yes") == 0)
391 || (strcasecmp(value, "true") == 0))
393 LDAP_BOOL_SET(gopts, attrs[i].offset);
396 LDAP_BOOL_CLR(gopts, attrs[i].offset);
401 p = &((char *) gopts)[attrs[i].offset];
402 * (int*) p = atoi(value);
406 const struct ol_keyvalue *kv;
408 for(kv = attrs[i].data;
412 if(strcasecmp(value, kv->key) == 0) {
413 p = &((char *) gopts)[attrs[i].offset];
414 * (int*) p = kv->value;
421 p = &((char *) gopts)[attrs[i].offset];
422 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
423 if (*value == '\0') {
426 * (char**) p = LDAP_STRDUP(value);
430 ldap_set_option( NULL, attrs[i].offset, value );
433 #ifdef HAVE_CYRUS_SASL
434 ldap_int_sasl_config( gopts, attrs[i].offset, value );
439 ldap_int_gssapi_config( gopts, attrs[i].offset, value );
444 ldap_pvt_tls_config( NULL, attrs[i].offset, value );
451 tv.tv_sec = strtol( value, &next, 10 );
452 if ( next != value && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
453 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
459 l = strtol( value, &next, 10 );
460 if ( next != value && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
462 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
469 #if defined(__GNUC__)
470 /* Declare this function as a destructor so that it will automatically be
471 * invoked either at program exit (if libldap is a static library) or
472 * at unload time (if libldap is a dynamic library).
474 * Sorry, don't know how to handle this for non-GCC environments.
476 static void ldap_int_destroy_global_options(void)
477 __attribute__ ((destructor));
481 ldap_int_destroy_global_options(void)
483 struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
488 gopts->ldo_valid = LDAP_UNINITIALIZED;
490 if ( gopts->ldo_defludp ) {
491 ldap_free_urllist( gopts->ldo_defludp );
492 gopts->ldo_defludp = NULL;
494 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
498 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
499 if ( ldap_int_hostname ) {
500 LDAP_FREE( ldap_int_hostname );
501 ldap_int_hostname = NULL;
504 #ifdef HAVE_CYRUS_SASL
505 if ( gopts->ldo_def_sasl_authcid ) {
506 LDAP_FREE( gopts->ldo_def_sasl_authcid );
507 gopts->ldo_def_sasl_authcid = NULL;
511 ldap_int_tls_destroy( gopts );
516 * Initialize the global options structure with default values.
518 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
520 #ifdef LDAP_R_COMPILE
521 LDAP_PVT_MUTEX_FIRSTCREATE(gopts->ldo_mutex);
523 LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
524 if (gopts->ldo_valid == LDAP_INITIALIZED) {
525 /* someone else got here first */
526 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
530 gopts->ldo_debug = *dbglvl;
532 gopts->ldo_debug = 0;
534 gopts->ldo_version = LDAP_VERSION2;
535 gopts->ldo_deref = LDAP_DEREF_NEVER;
536 gopts->ldo_timelimit = LDAP_NO_LIMIT;
537 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
539 gopts->ldo_tm_api.tv_sec = -1;
540 gopts->ldo_tm_net.tv_sec = -1;
542 /* ldo_defludp will be freed by the termination handler
544 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
545 gopts->ldo_defport = LDAP_PORT;
546 #if !defined(__GNUC__) && !defined(PIC)
547 /* Do this only for a static library, and only if we can't
548 * arrange for it to be executed as a library destructor
550 atexit(ldap_int_destroy_global_options);
553 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
554 gopts->ldo_rebind_proc = NULL;
555 gopts->ldo_rebind_params = NULL;
557 LDAP_BOOL_ZERO(gopts);
559 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
561 #ifdef LDAP_CONNECTIONLESS
562 gopts->ldo_peer = NULL;
563 gopts->ldo_cldapdn = NULL;
564 gopts->ldo_is_udp = 0;
567 #ifdef HAVE_CYRUS_SASL
568 gopts->ldo_def_sasl_mech = NULL;
569 gopts->ldo_def_sasl_realm = NULL;
570 gopts->ldo_def_sasl_authcid = NULL;
571 gopts->ldo_def_sasl_authzid = NULL;
573 memset( &gopts->ldo_sasl_secprops,
574 '\0', sizeof(gopts->ldo_sasl_secprops) );
576 gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
577 gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
578 gopts->ldo_sasl_secprops.security_flags =
579 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
583 gopts->ldo_tls_connect_cb = NULL;
584 gopts->ldo_tls_connect_arg = NULL;
585 gopts->ldo_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
587 gopts->ldo_keepalive_probes = 0;
588 gopts->ldo_keepalive_interval = 0;
589 gopts->ldo_keepalive_idle = 0;
591 gopts->ldo_valid = LDAP_INITIALIZED;
592 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
596 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
597 char * ldap_int_hostname = NULL;
600 #ifdef LDAP_R_COMPILE
601 int ldap_int_stackguard;
604 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
606 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
610 ldap_int_error_init();
612 ldap_int_utils_init();
615 { WORD wVersionRequested;
618 wVersionRequested = MAKEWORD( 2, 0 );
619 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
620 /* Tell the user that we couldn't find a usable */
625 /* Confirm that the WinSock DLL supports 2.0.*/
626 /* Note that if the DLL supports versions greater */
627 /* than 2.0 in addition to 2.0, it will still return */
628 /* 2.0 in wVersion since that is the version we */
631 if ( LOBYTE( wsaData.wVersion ) != 2 ||
632 HIBYTE( wsaData.wVersion ) != 0 )
634 /* Tell the user that we couldn't find a usable */
639 } /* The WinSock DLL is acceptable. Proceed. */
640 #elif defined(HAVE_WINSOCK)
642 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
648 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
649 LDAP_MUTEX_LOCK( &ldap_int_hostname_mutex );
651 char *name = ldap_int_hostname;
653 ldap_int_hostname = ldap_pvt_get_fqdn( name );
655 if ( name != NULL && name != ldap_int_hostname ) {
659 LDAP_MUTEX_UNLOCK( &ldap_int_hostname_mutex );
663 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
666 #ifdef HAVE_CYRUS_SASL
667 if ( ldap_int_sasl_init() != 0 ) {
672 ldap_int_initialize_global_options(gopts, dbglvl);
674 if( getenv("LDAPNOINIT") != NULL ) {
678 #ifdef LDAP_R_COMPILE
679 if( getenv("LDAPSTACKGUARD") != NULL ) {
680 ldap_int_stackguard = 1;
684 #ifdef HAVE_CYRUS_SASL
686 /* set authentication identity to current user name */
687 char *user = getenv("USER");
689 if( user == NULL ) user = getenv("USERNAME");
690 if( user == NULL ) user = getenv("LOGNAME");
693 gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
698 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
701 if ( geteuid() != getuid() )
705 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
708 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
710 if( altfile != NULL ) {
711 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
712 LDAP_ENV_PREFIX "CONF", altfile, 0);
713 openldap_ldap_init_w_sysconf( altfile );
716 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
717 LDAP_ENV_PREFIX "CONF", 0, 0);
721 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
723 if( altfile != NULL ) {
724 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
725 LDAP_ENV_PREFIX "RC", altfile, 0);
726 openldap_ldap_init_w_userconf( altfile );
729 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
730 LDAP_ENV_PREFIX "RC", 0, 0);
733 openldap_ldap_init_w_env(gopts, NULL);