From 204dd77ad8dbed8a64e3ffbcbadd8e92980078d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Czern=C3=BD?= Date: Tue, 24 Nov 2026 09:41:55 +0100 Subject: [PATCH] B #7377: Match escaped LDAP usernames during authentication (#4719) Apply the LDAP driver's username canonicalization when looking up existing users, and rename the lookup helper to get_auth_user. --- include/UserPool.h | 6 +++ src/rm/api/UserAPI.cc | 4 +- src/um/UserPool.cc | 98 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/include/UserPool.h b/include/UserPool.h index bcf3e072c2..7f06a6db8d 100644 --- a/include/UserPool.h +++ b/include/UserPool.h @@ -146,6 +146,12 @@ public: return u; } + /** + * Gets a locked user for authentication or login token management. + * Prefer an exact name, then try the lowercase name for LDAP accounts. + */ + std::unique_ptr get_auth_user(const std::string& name); + /** * Function to get the token password of an user from the pool * @param uid creator of the object diff --git a/src/rm/api/UserAPI.cc b/src/rm/api/UserAPI.cc index a402bd2fe2..3436b1951f 100644 --- a/src/rm/api/UserAPI.cc +++ b/src/rm/api/UserAPI.cc @@ -250,7 +250,7 @@ Request::ErrorCode UserAPI::login(const std::string& uname, PoolObjectAuth perms; - if (auto user = upool->get_ro(uname)) + if (auto user = upool->get_auth_user(uname)) { user->get_permissions(perms); } @@ -270,7 +270,7 @@ Request::ErrorCode UserAPI::login(const std::string& uname, return Request::AUTHORIZATION; } - auto user = upool->get(uname); + auto user = upool->get(perms.oid); if ( user == nullptr ) { diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index 89738890fd..a95f2eae44 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -25,7 +25,9 @@ #include "NebulaUtil.h" #include "Client.h" +#include #include +#include #include #include #include @@ -1250,6 +1252,100 @@ auth_failure: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +// Keep this in sync with URI_PARSER.unescape and URI_PARSER.escape(...).downcase +// in src/authm_mad/remotes/ldap/authenticate. +static string ldap_canonical_name(const string& name) +{ + auto hex_value = [](unsigned char c) -> int + { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; + }; + + string decoded; + + decoded.reserve(name.size()); + + for (size_t i = 0; i < name.size(); ++i) + { + if (name[i] == '%' && i + 2 < name.size()) + { + int hi = hex_value(static_cast(name[i + 1])); + int lo = hex_value(static_cast(name[i + 2])); + + if (hi >= 0 && lo >= 0) + { + decoded += static_cast((hi << 4) | lo); + i += 2; + + continue; + } + } + + decoded += name[i]; + } + + constexpr char hex[] = "0123456789abcdef"; + constexpr string_view safe = + "!$&'()*+,-./0123456789:;=?@" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz~"; + + string result; + + result.reserve(decoded.size()); + + for (unsigned char c : decoded) + { + if (safe.find(static_cast(c)) != string_view::npos) + { + if (c >= 'A' && c <= 'Z') + { + c += 'a' - 'A'; + } + + result += static_cast(c); + } + else + { + result += '%'; + result += hex[c >> 4]; + result += hex[c & 0x0f]; + } + } + + return result; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +std::unique_ptr UserPool::get_auth_user(const string& name) +{ + if (auto user = get(name)) + { + return user; + } + + string canonical_name = ldap_canonical_name(name); + + if (canonical_name != name) + { + auto ldap_user = get(canonical_name); + + if (ldap_user && ldap_user->get_auth_driver() == "ldap") + { + return ldap_user; + } + } + + return nullptr; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + bool UserPool::authenticate(const string& session, string& password, int& user_id, @@ -1279,7 +1375,7 @@ bool UserPool::authenticate(const string& session, return false; } - if ( auto user = get(username) ) //User known to OpenNebula + if (auto user = get_auth_user(username)) { if (!user->isEnabled()) {