Permalink
Browse files

ldapauth: fix memory leak

ldap_get_dn() results need to be freed. Use a RAII wrapper to do this.

Original code by Attila Molnar
  • Loading branch information...
1 parent e031d19 commit 1813369adecc1efc9812e90c40c21dc32e4965c9 @rburchell rburchell committed Nov 28, 2012
Showing with 29 additions and 3 deletions.
  1. +29 −3 src/modules/extra/m_ldapauth.cpp
View
32 src/modules/extra/m_ldapauth.cpp
@@ -38,6 +38,31 @@
/* $ModDesc: Allow/Deny connections based upon answer from LDAP server */
/* $LinkerFlags: -lldap */
+struct LDAPString
+{
+ char *str;
+
+ LDAPString(char *Str)
+ : str(Str)
+ {
+ }
+
+ ~LDAPString()
+ {
+ ldap_memfree(str);
+ }
+
+ operator char*()
+ {
+ return str;
+ }
+
+ operator std::string()
+ {
+ return str;
+ }
+};
+
class ModuleLDAPAuth : public Module
{
LocalIntExt ldapAuthed;
@@ -296,7 +321,8 @@ class ModuleLDAPAuth : public Module
}
cred.bv_val = (char*)user->password.data();
cred.bv_len = user->password.length();
- if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
+ LDAPString DN(ldap_get_dn(conn, entry));
+ if ((res = ldap_sasl_bind_s(conn, DN, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
{
if (verbose)
ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s (%s)", user->GetFullRealHost().c_str(), ldap_err2string(res));
@@ -319,7 +345,7 @@ class ModuleLDAPAuth : public Module
ServerInstance->Logs->Log("m_ldapauth", DEBUG, "LDAP compare: %s=%s", attr.c_str(), val.c_str());
- authed = (ldap_compare_ext_s(conn, ldap_get_dn(conn, entry), attr.c_str(), &attr_value, NULL, NULL) == LDAP_COMPARE_TRUE);
+ authed = (ldap_compare_ext_s(conn, DN, attr.c_str(), &attr_value, NULL, NULL) == LDAP_COMPARE_TRUE);
if (authed)
break;
@@ -336,7 +362,7 @@ class ModuleLDAPAuth : public Module
if (!vhost.empty())
{
- irc::commasepstream stream(ldap_get_dn(conn, entry));
+ irc::commasepstream stream(DN);
// mashed map of key:value parts of the DN
std::map<std::string, std::string> dnParts;

0 comments on commit 1813369

Please sign in to comment.