+ if (res == LDAP_SERVER_DOWN)
+ {
+ // Attempt to reconnect if the connection dropped
+ if (verbose)
+ ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
+ Connect();
+ res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
+ }
+
+ if (res != LDAP_SUCCESS)
+ {
+ if (verbose)
+ ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s (LDAP bind failed: %s)", user->GetFullRealHost().c_str(), ldap_err2string(res));
+ ldap_unbind_ext(conn, NULL, NULL);
+ conn = NULL;
+ return false;
+ }
+ }
+
+ RAIILDAPMessage msg;
+ std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
+ if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
+ {
+ // Do a second search, based on password, if it contains a :
+ // That is, PASS <user>:<password> will work.
+ size_t pos = user->password.find(":");
+ if (pos != std::string::npos)
+ {
+ // manpage says we must deallocate regardless of success or failure
+ // since we're about to do another query (and reset msg), first
+ // free the old one.
+ msg.dealloc();
+
+ std::string cutpassword = user->password.substr(0, pos);
+ res = ldap_search_ext_s(conn, base.c_str(), searchscope, cutpassword.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg);
+
+ if (res == LDAP_SUCCESS)
+ {
+ // Trim the user: prefix, leaving just 'pass' for later password check
+ user->password = user->password.substr(pos + 1);
+ }
+ }
+
+ // It may have found based on user:pass check above.
+ if (res != LDAP_SUCCESS)
+ {
+ if (verbose)
+ ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s (LDAP search failed: %s)", user->GetFullRealHost().c_str(), ldap_err2string(res));
+ return false;
+ }