|
|
@@ -0,0 +1,51 @@
|
|
|
+#define LDAP_DEPRECATED // FIXME
|
|
|
+
|
|
|
+#include <iostream>
|
|
|
+#include <sstream>
|
|
|
+#include <ldap.h>
|
|
|
+#include "ldapReader.h"
|
|
|
+
|
|
|
+LDAPReader::LDAPReader(const std::string& _uri, const std::string& _attribute, const std::string& _base):
|
|
|
+ uri(_uri), attribute(_attribute), baseDn(_base)
|
|
|
+{}
|
|
|
+
|
|
|
+LDAPReader::~LDAPReader()
|
|
|
+{}
|
|
|
+
|
|
|
+void* LDAPReader::InitConnection()
|
|
|
+{
|
|
|
+ LDAP* ldapHandler;
|
|
|
+ int protoVersion = LDAP_VERSION3;
|
|
|
+
|
|
|
+ int err = ldap_initialize(&ldapHandler, uri.c_str());
|
|
|
+ if (err != LDAP_SUCCESS)
|
|
|
+ {
|
|
|
+ std::cerr << "LDAP Initialize error: (" << err << ") " << ldap_err2string(err) << std::endl;
|
|
|
+ return nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ err = ldap_set_option(ldapHandler, LDAP_OPT_PROTOCOL_VERSION, &protoVersion);
|
|
|
+ if (err != LDAP_SUCCESS)
|
|
|
+ {
|
|
|
+ std::cerr << "LDAP Protocol error: (" << err << ") " << ldap_err2string(err) << std::endl;
|
|
|
+ return nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ldapHandler;
|
|
|
+}
|
|
|
+
|
|
|
+bool LDAPReader::Authenticate(const std::string& username, const std::string& password)
|
|
|
+{
|
|
|
+ LDAP* ldapHandler = reinterpret_cast<LDAP*>(InitConnection());
|
|
|
+ if (!ldapHandler)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ std::stringstream bindDn;
|
|
|
+ bindDn << attribute << "=" << username << "," << baseDn;
|
|
|
+ int err = ldap_simple_bind_s(ldapHandler, bindDn.str().c_str(), password.c_str());
|
|
|
+ if (err != LDAP_SUCCESS)
|
|
|
+ std::cerr << "LDAP Bind Error: (" << err << ") " << ldap_err2string(err) << std::endl;
|
|
|
+ ldap_unbind_ext(ldapHandler, nullptr, nullptr);
|
|
|
+ return err == LDAP_SUCCESS;
|
|
|
+}
|
|
|
+
|