- // bind anonymously
- //if ((res = ldap_simple_bind_s(conn, "", "")) != LDAP_SUCCESS)
- struct berval cred; cred.bv_val = ""; cred.bv_len = 0;
- if ((res = ldap_sasl_bind_s(conn, "", LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
- {
- if (verbose)
- ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP bind anonymously failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
- ldap_unbind_ext(conn, NULL, NULL);
- conn = NULL;
- return false;
+ char* authpass = strdup(password.c_str());
+ // bind anonymously if no bind DN and authentication are given in the config
+ struct berval cred;
+ cred.bv_val = authpass;
+ cred.bv_len = password.length();
+
+ if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
+ {
+ 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!%s@%s (LDAP bind failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
+ free(authpass);
+ ldap_unbind_ext(conn, NULL, NULL);
+ conn = NULL;
+ return false;
+ }