diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f872a7ed34..9fdf30b520f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https: - cats: postgresql introduce pl/sql lstat_decode() function [PR #1466] - bsmtp: make mailhost and port message info a debug message [PR #1507] - dird: cats: abort purge when there are no eligible jobids [PR #1512] +- dird: show current and allowed console connections [PR #1487] ### Removed - remove no longer used pkglists [PR #1335] @@ -202,6 +203,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https: [PR #1477]: https://github.com/bareos/bareos/pull/1477 [PR #1479]: https://github.com/bareos/bareos/pull/1479 [PR #1484]: https://github.com/bareos/bareos/pull/1484 +[PR #1487]: https://github.com/bareos/bareos/pull/1487 [PR #1488]: https://github.com/bareos/bareos/pull/1488 [PR #1490]: https://github.com/bareos/bareos/pull/1490 [PR #1493]: https://github.com/bareos/bareos/pull/1493 diff --git a/core/src/dird/CMakeLists.txt b/core/src/dird/CMakeLists.txt index eb9a04e27e3..8deeec2c187 100644 --- a/core/src/dird/CMakeLists.txt +++ b/core/src/dird/CMakeLists.txt @@ -37,6 +37,7 @@ set(DIRD_OBJECTS_SRCS catreq.cc check_catalog.cc consolidate.cc + console_connection_lease.cc dbcheck_utils.cc dird_globals.cc dir_plugins.cc diff --git a/core/src/dird/authenticate_console.cc b/core/src/dird/authenticate_console.cc index a48c06d6214..3e8fca54417 100644 --- a/core/src/dird/authenticate_console.cc +++ b/core/src/dird/authenticate_console.cc @@ -37,6 +37,7 @@ #include "lib/util.h" #include "lib/version.h" #include "include/version_numbers.h" +#include "console_connection_lease.h" namespace directordaemon { @@ -354,19 +355,6 @@ static void LogErrorMessage(std::string console_name, UaContext* ua) ua->UA_sock->port()); } -static bool NumberOfConsoleConnectionsExceeded() -{ - JobControlRecord* jcr; - unsigned int cnt = 0; - - foreach_jcr (jcr) { - if (jcr->is_JobType(JT_CONSOLE)) { cnt++; } - } - endeach_jcr(jcr); - - return (cnt >= me->MaxConsoleConnections) ? true : false; -} - static bool GetConsoleNameAndVersion(BareosSocket* ua_sock, std::string& name_out, BareosVersionNumber& version_out) @@ -407,13 +395,7 @@ static ConsoleAuthenticator* CreateConsoleAuthenticator(UaContext* ua) bool AuthenticateConsole(UaContext* ua) { - if (NumberOfConsoleConnectionsExceeded()) { - Emsg0(M_ERROR, 0, - _("Number of console connections exceeded " - "MaximumConsoleConnections\n")); - return false; - } - + auto num_leases = ConsoleConnectionLease::get_num_leases(); std::unique_ptr console_authenticator( CreateConsoleAuthenticator(ua)); if (!console_authenticator) { return false; } @@ -424,7 +406,21 @@ bool AuthenticateConsole(UaContext* ua) LogErrorMessage(console_authenticator->console_name_, ua); return false; } + if (num_leases > me->MaxConsoleConnections) { + Emsg0(M_INFO, 0, + _("Number of console connections exceeded\n" + "Maximum: %u, Current: %zu\n"), + me->MaxConsoleConnections, num_leases); + } } else { + if (num_leases > me->MaxConsoleConnections) { + Emsg0(M_ERROR, 0, + _("Number of console connections exceeded " + "Maximum :%u, Current: %zu\n"), + me->MaxConsoleConnections, num_leases); + return false; + } + console_authenticator->AuthenticateNamedConsole(); if (!console_authenticator->auth_success_) { LogErrorMessage(console_authenticator->console_name_, ua); diff --git a/core/src/dird/console_connection_lease.cc b/core/src/dird/console_connection_lease.cc new file mode 100644 index 00000000000..04899d2141a --- /dev/null +++ b/core/src/dird/console_connection_lease.cc @@ -0,0 +1,23 @@ +/* + BAREOSĀ® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2023-2023 Bareos GmbH & Co. KG + + This program is Free Software; you can redistribute it and/or + modify it under the terms of version three of the GNU Affero General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#include "console_connection_lease.h" + +std::atomic ConsoleConnectionLease::num_leases{0}; diff --git a/core/src/dird/console_connection_lease.h b/core/src/dird/console_connection_lease.h new file mode 100644 index 00000000000..2a9f56406d6 --- /dev/null +++ b/core/src/dird/console_connection_lease.h @@ -0,0 +1,35 @@ +/* + BAREOSĀ® - Backup Archiving REcovery Open Sourced + + Copyright (C) 2023-2023 Bareos GmbH & Co. KG + + This program is Free Software; you can redistribute it and/or + modify it under the terms of version three of the GNU Affero General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef BAREOS_DIRD_CONSOLE_CONNECTION_LEASE_H_ +#define BAREOS_DIRD_CONSOLE_CONNECTION_LEASE_H_ + +#include + +class ConsoleConnectionLease { + static std::atomic num_leases; + + public: + ConsoleConnectionLease() { num_leases++; } + ~ConsoleConnectionLease() { num_leases--; } + static std::size_t get_num_leases() { return num_leases; } +}; +#endif // BAREOS_DIRD_CONSOLE_CONNECTION_LEASE_H_ diff --git a/core/src/dird/ua_server.cc b/core/src/dird/ua_server.cc index fa1e69fee56..ce03ff3119c 100644 --- a/core/src/dird/ua_server.cc +++ b/core/src/dird/ua_server.cc @@ -3,7 +3,7 @@ Copyright (C) 2000-2007 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2022 Bareos GmbH & Co. KG + Copyright (C) 2013-2023 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -43,7 +43,7 @@ #include "lib/parse_conf.h" #include "lib/thread_specific_data.h" #include "dird/jcr_util.h" - +#include "console_connection_lease.h" namespace directordaemon { @@ -91,6 +91,7 @@ void* HandleUserAgentClientRequest(BareosSocket* user_agent_socket) ua->UA_sock = user_agent_socket; SetJcrInThreadSpecificData(nullptr); + ConsoleConnectionLease lease; // obtain lease to count connections bool success = AuthenticateConsole(ua); if (!success) { ua->quit = true; }