1
0
mirror of https://github.com/anope/anope.git synced 2026-06-12 15:44:46 +02:00

Add EscapeDN and EscapeSF to the LDAP API.

This commit is contained in:
Sadie Powell
2026-05-26 09:29:57 +01:00
parent 01fc7421b6
commit 449cfa6503
2 changed files with 84 additions and 0 deletions
+10
View File
@@ -166,6 +166,16 @@ class LDAPProvider : public Service
* @param attributes The attributes to modify
*/
virtual void Modify(LDAPInterface *i, const Anope::string &base, LDAPMods &attributes) = 0;
/** Escapes a LDAP string for use in a DN.
* @param str The string to escape.
*/
virtual Anope::string EscapeDN(const Anope::string &str) const = 0;
/** Escapes a LDAP string for use in a search filter.
* @param str The string to escape.
*/
virtual Anope::string EscapeSF(const Anope::string &str) const = 0;
};
#endif // ANOPE_LDAP_H
+74
View File
@@ -388,6 +388,80 @@ class LDAPService : public LDAPProvider, public Thread, public Condition
QueueRequest(mod);
}
Anope::string EscapeDN(const Anope::string &str) const anope_override
{
if (str.empty())
return str;
Anope::string newstr;
newstr.str().reserve(str.length());
for (size_t idx = 0; idx < str.length(); ++idx)
{
const char chr = str[idx];
if (chr == '\0')
{
newstr.append("\\00");
}
else if (chr == '"' || chr == '+' || chr == ',' || chr == ';' ||
chr == '<' || chr == '=' || chr == '>' || chr == '\\')
{
newstr.push_back('\\');
newstr.push_back(chr);
}
else if (idx == 0 && (chr == '#' || chr == ' '))
{
newstr.push_back('\\');
newstr.push_back(chr);
}
else if (idx == str.length() - 1 && chr == ' ')
{
newstr.push_back('\\');
newstr.push_back(chr);
}
else
{
newstr.push_back(chr);
}
}
return newstr;
}
Anope::string EscapeSF(const Anope::string &str) const anope_override
{
if (str.empty())
return str;
Anope::string newstr;
newstr.str().reserve(str.length());
for (size_t idx = 0; idx < str.length(); ++idx)
{
const char chr = str[idx];
switch (chr)
{
case '\0':
newstr.append("\\00");
break;
case '(':
newstr.append("\\28");
break;
case ')':
newstr.append("\\29");
break;
case '*':
newstr.append("\\2A");
break;
case '\\':
newstr.append("\\5C");
break;
default:
newstr.push_back(chr);
break;
}
}
return newstr;
}
private:
void BuildReply(int res, LDAPRequest *req)
{