From bdb6fe6168806baf22feb7fce5490dca23b07458 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 14 Dec 2021 12:29:09 +0100 Subject: Support wildcards * and ? in ldap filters Closes #3854 --- src/userldapdata.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/userldapdata.cpp b/src/userldapdata.cpp index 4382800..d4f966f 100644 --- a/src/userldapdata.cpp +++ b/src/userldapdata.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #define INCBREAK if (++i >= len) break @@ -39,6 +40,7 @@ bool init(QString inputFile) keyEnd = i; INCBREAK; if (p[i] == ':') { + // Double ":", means base64 encoded data INCBREAK; b64 = true; } @@ -53,6 +55,7 @@ bool init(QString inputFile) while (dataLen > 0 && p[dataStart + dataLen - 1] == '\n') { dataLen--; } + // Put "lowercase(key):value" into set QString value = QString::fromUtf8(p, keyEnd).toLower() + ":"; if (b64) { value += QString::fromUtf8(QByteArray::fromBase64(QByteArray(p + dataStart, dataLen))); @@ -71,8 +74,18 @@ bool isEmpty() bool isAllowed(const QString& attribute, const QString& value) { - const QString keyLow(attribute.toLower() + ":" + value); - return _entries.contains(keyLow); + if (value.indexOf(QLatin1Char('*')) == -1 && value.indexOf(QLatin1Char('?')) == -1) { + const QString keyLow(attribute.toLower() + QLatin1String(":") + value); + return _entries.contains(keyLow); + } + // Wildcard matching + QRegularExpression re(QLatin1String("^") + QRegularExpression::escape(attribute.toLower()) + QLatin1String(":") + + QRegularExpression::wildcardToRegularExpression(value)); + for (auto str : _entries) { + if (re.match(str).hasMatch()) + return true; + } + return false; } } -- cgit v1.2.3-55-g7522