From aa334ea60ad7a1b4f8816c95a0164fe798974cb0 Mon Sep 17 00:00:00 2001 From: Bartosz Wieczorek Date: Tue, 3 Jun 2025 13:02:57 +0200 Subject: [PATCH] fix formatting --- PAM/ssh/include/rublon/configuration.hpp | 34 ++++++++++-------- PAM/ssh/include/rublon/curl.hpp | 36 ++++++++++--------- .../include/rublon/method/method_select.hpp | 8 +++-- PAM/ssh/include/rublon/pam_action.hpp | 8 +++-- 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/PAM/ssh/include/rublon/configuration.hpp b/PAM/ssh/include/rublon/configuration.hpp index 1001bb1..9e7abd0 100644 --- a/PAM/ssh/include/rublon/configuration.hpp +++ b/PAM/ssh/include/rublon/configuration.hpp @@ -1,15 +1,20 @@ #pragma once -#include -#include #include +#include #include #include +#include #include #include #include +#include +#include +#include +#include + namespace rublon { class ConfigurationFactory; @@ -20,7 +25,7 @@ class Configuration { std::pmr::memory_resource * memoryResource; public: - Configuration() : memoryResource{memory::default_resource()} {} + Configuration(std::pmr::memory_resource * mr = memory::default_resource()) : memoryResource{mr} {} // change to StaticString std::pmr::string systemToken{memoryResource}; @@ -43,16 +48,15 @@ class Configuration { bool proxyEnabled{}; // defaulted }; - class ConfigurationReader { public: ConfigurationReader(std::pmr::memory_resource * memResource = memory::default_resource()) : memoryResource(memResource) {} // Load config from file path - bool loadFromFile(const std::string & filepath) { + bool loadFromFile(std::string_view filepath) { using namespace memory::literals; memory::MonotonicStackResource< 8_kB > stackResource; - std::ifstream file(filepath); + std::ifstream file(filepath.data()); if(not file.good()) return false; @@ -73,7 +77,7 @@ class ConfigurationReader { auto posEqual = line.find('='); key = line.substr(0, posEqual); value = line.substr(posEqual + 1); - + keyValues[std::move(key)] = std::move(value); } @@ -84,7 +88,7 @@ class ConfigurationReader { tl::expected< bool, ConfigurationError > applyTo(Configuration & config) { // Helper lambdas for conversion using string = std::pmr::string; - + auto getStringOpt = [&](const string & key) -> std::optional< std::pmr::string > { auto it = keyValues.find(key); if(it == keyValues.end()) { @@ -112,12 +116,12 @@ class ConfigurationReader { auto it = keyValues.find(key); if(it == keyValues.end()) return std::nullopt; - - if (it->second.size() > 5 ){ + + if(it->second.size() > 5) { log(LogLevel::Warning, "Configuration value %s is too long, please check", key.c_str()); return std::nullopt; } - + std::pmr::string val{&memoryResource}; val = it->second; std::transform(val.begin(), val.end(), val.begin(), [](unsigned char c) { return static_cast< char >(std::tolower(c)); }); @@ -200,7 +204,7 @@ class ConfigurationReader { private: std::pmr::memory_resource * memoryResource; - std::pmr::map< std::pmr::string, std::pmr::string > keyValues{memoryResource}; + std::pmr::unordered_map< std::pmr::string, std::pmr::string > keyValues{memoryResource}; }; class ConfigurationFactory { @@ -208,10 +212,10 @@ class ConfigurationFactory { ConfigurationFactory() = default; std::optional< Configuration > systemConfig() { - std::optional< Configuration > conf{Configuration{}}; - ConfigurationReader reader; + std::optional< Configuration > conf{}; + ConfigurationReader reader{}; reader.loadFromFile("/etc/rublon.config"); - if(auto ok = reader.applyTo(conf.value()); not ok.has_value()){ + if(auto ok = reader.applyTo(conf.value()); not ok.has_value()) { return std::nullopt; } return conf.value(); diff --git a/PAM/ssh/include/rublon/curl.hpp b/PAM/ssh/include/rublon/curl.hpp index ea70027..0771af1 100644 --- a/PAM/ssh/include/rublon/curl.hpp +++ b/PAM/ssh/include/rublon/curl.hpp @@ -2,9 +2,9 @@ #include "rublon/memory.hpp" #include +#include #include #include -#include #include @@ -12,7 +12,6 @@ namespace rublon { - namespace { size_t WriteMemoryCallback(void * contents, size_t size, size_t nmemb, void * userp) { const size_t realsize = size * nmemb; @@ -55,9 +54,11 @@ struct Response { class CURL { std::unique_ptr< ::CURL, void (*)(::CURL *) > curl; - const Configuration &_config; + const Configuration & _config; + public: - CURL(const Configuration &config) : curl{std::unique_ptr< ::CURL, void (*)(::CURL *) >(curl_easy_init(), curl_easy_cleanup)}, _config{config} {} + CURL(const Configuration & config) + : curl{std::unique_ptr< ::CURL, void (*)(::CURL *) >(curl_easy_init(), curl_easy_cleanup)}, _config{config} {} tl::expected< std::reference_wrapper< Response >, ConnectionError > request(std::string_view uri, const Request & request, Response & response) const { @@ -72,45 +73,46 @@ class CURL { log(LogLevel::Debug, "%s header: %s: %s", "CURL", header.first.c_str(), header.second.c_str()); curl_headers.reset(curl_slist_append(curl_headers.release(), (header.first + ": " + header.second).c_str())); }); - + // Optional: Build full proxy URL if proxy is enabled - if (_config.proxyEnabled) { + if(_config.proxyEnabled) { // configuration reader check if proxy has needed fields assert(_config.proxyType.has_value()); assert(_config.proxyServer.has_value()); - + std::pmr::string proxyUrl{&memoryResource}; proxyUrl.reserve(conservative_estimate(_config.proxyType, _config.proxyServer, _config.proxyPort) + 10); - if (_config.proxyType == "http" || _config.proxyType == "https" || _config.proxyType == "socks4" || _config.proxyType == "socks5") { + if(_config.proxyType == "http" || _config.proxyType == "https" || _config.proxyType == "socks4" || + _config.proxyType == "socks5") { proxyUrl = *_config.proxyType; proxyUrl += "://"; proxyUrl += *_config.proxyServer; - if (_config.proxyPort > 0) { + if(_config.proxyPort > 0) { proxyUrl += ":"; proxyUrl += std::to_string(*_config.proxyPort); } - + curl_easy_setopt(curl.get(), CURLOPT_PROXY, proxyUrl.c_str()); - - if (_config.proxyType == "socks4") { + + if(_config.proxyType == "socks4") { curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4); - } else if (_config.proxyType == "socks5") { + } else if(_config.proxyType == "socks5") { curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); } else { curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_HTTP); } - - if (_config.proxyAuthRequired) { + + if(_config.proxyAuthRequired) { assert(_config.proxyUsername.has_value()); assert(_config.proxyPass.has_value()); std::pmr::string proxyAuth{&memoryResource}; - proxyAuth.reserve(conservative_estimate(_config.proxyUsername->size() + _config.proxyPass->size())); + proxyAuth.reserve(conservative_estimate(_config.proxyUsername, _config.proxyPass)); proxyAuth += *_config.proxyUsername; if(_config.proxyPass->size()) { - // can proxy have name but no pass? + // can proxy have name but no pass? proxyAuth += ":"; proxyAuth += *_config.proxyPass; } diff --git a/PAM/ssh/include/rublon/method/method_select.hpp b/PAM/ssh/include/rublon/method/method_select.hpp index 616f821..0117f33 100644 --- a/PAM/ssh/include/rublon/method/method_select.hpp +++ b/PAM/ssh/include/rublon/method/method_select.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -129,12 +130,15 @@ class MethodSelect { int _prompts; bool _autopushPrompt; - std::vector< std::string > _methodsAvailable; // TODO pmr + std::pmr::memory_resource * _mr; + + // method name is really short, there is almost no chance that thos strings will allocate + std::pmr::vector< std::pmr::string > _methodsAvailable; public: template < typename Array_t > MethodSelect(Session & session, const Array_t & methodsEnabledInCore, int prompts, bool autopushPrompt) - : _session{session}, _prompts{prompts}, _autopushPrompt{autopushPrompt} { + : _session{session}, _prompts{prompts}, _autopushPrompt{autopushPrompt}, _mr{memory::default_resource()}, _methodsAvailable{_mr} { rublon::log(LogLevel::Debug, "Checking what methods from core are supported"); using namespace std::string_view_literals; _methodsAvailable.reserve(std::size(methodsEnabledInCore)); diff --git a/PAM/ssh/include/rublon/pam_action.hpp b/PAM/ssh/include/rublon/pam_action.hpp index 44e341f..c7aa97f 100644 --- a/PAM/ssh/include/rublon/pam_action.hpp +++ b/PAM/ssh/include/rublon/pam_action.hpp @@ -8,11 +8,11 @@ namespace rublon { class AuthenticationStatus { public: - using tokenT = std::optional< StaticString< 64 > >; + using Token_t = std::optional< StaticString< 64 > >; enum class Action { Denied, Confirmed, Bypass }; AuthenticationStatus(Action action, const char * token = nullptr) - : _action{action}, _authenticationToken{token == nullptr ? tokenT{std::nullopt} : tokenT{token}} {} + : _action{action}, _authenticationToken{token == nullptr ? Token_t{std::nullopt} : Token_t{token}} {} constexpr bool userAuthorized() const { return _action == Action::Confirmed; @@ -23,12 +23,14 @@ class AuthenticationStatus { } std::string_view accessToken() const { + if(not _authenticationToken) + return ""; return {_authenticationToken->c_str(), _authenticationToken->size()}; } private: Action _action; - tokenT _authenticationToken; /// TODO dynamic mem + Token_t _authenticationToken; }; } // namespace rublon