| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #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;
- struct berval bindPw;
- char passwordCstr[password.length()];
- strcpy(passwordCstr, password.c_str());
- bindPw.bv_len = password.length();
- bindPw.bv_val = passwordCstr;
- int err = ldap_sasl_bind_s(ldapHandler, bindDn.str().c_str(), LDAP_SASL_SIMPLE, &bindPw, nullptr, nullptr, nullptr);
- 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;
- }
|