2019-03-29 02:11:00 +00:00
|
|
|
/* Benjamin DELPY `gentilkiwi`
|
2020-09-17 01:17:11 +00:00
|
|
|
https://blog.gentilkiwi.com
|
2019-03-29 02:11:00 +00:00
|
|
|
benjamin@gentilkiwi.com
|
|
|
|
Licence : https://creativecommons.org/licenses/by/4.0/
|
|
|
|
*/
|
|
|
|
#include "kull_m_ldap.h"
|
|
|
|
|
2021-09-06 22:19:15 +00:00
|
|
|
BOOL kull_m_ldap_getLdapAndRootDN(PCWCHAR system, PCWCHAR nc, PLDAP *ld, PWCHAR *rootDn, PSEC_WINNT_AUTH_IDENTITY pIdentity)
|
2019-03-29 02:11:00 +00:00
|
|
|
{
|
|
|
|
BOOL status = FALSE;
|
|
|
|
DWORD dwErr;
|
|
|
|
|
|
|
|
if(*ld = ldap_init((PWCHAR) system, LDAP_PORT))
|
|
|
|
{
|
2019-04-11 23:25:20 +00:00
|
|
|
if(*rootDn = kull_m_ldap_getRootDomainNamingContext(nc, *ld))
|
2019-03-29 02:11:00 +00:00
|
|
|
{
|
2021-09-06 22:19:15 +00:00
|
|
|
dwErr = ldap_bind_s(*ld, NULL, (PWCHAR) pIdentity, LDAP_AUTH_NEGOTIATE);
|
2019-03-29 02:11:00 +00:00
|
|
|
status = (dwErr == LDAP_SUCCESS);
|
|
|
|
if(!status)
|
|
|
|
{
|
|
|
|
PRINT_ERROR(L"ldap_bind_s 0x%x (%u)\n", dwErr, dwErr);
|
|
|
|
*rootDn = (PWCHAR) LocalFree(*rootDn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!status)
|
|
|
|
ldap_unbind(*ld);
|
|
|
|
}
|
|
|
|
else PRINT_ERROR(L"ldap_init\n");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-04-11 23:25:20 +00:00
|
|
|
PWCHAR kull_m_ldap_getRootDomainNamingContext(PCWCHAR nc, LDAP *ld)
|
2019-03-29 02:11:00 +00:00
|
|
|
{
|
|
|
|
DWORD dwErr;
|
2019-04-11 23:25:20 +00:00
|
|
|
PWCHAR rootAttr[] = {nc ? (PWCHAR) nc : L"rootDomainNamingContext", NULL}, ret = NULL;
|
2019-03-29 02:11:00 +00:00
|
|
|
PLDAPMessage pMessage = NULL;
|
|
|
|
PBERVAL *pBerVal;
|
|
|
|
|
|
|
|
dwErr = ldap_search_s(ld, NULL, LDAP_SCOPE_BASE, L"(dn=RootDSE)", rootAttr, FALSE, &pMessage);
|
|
|
|
if(dwErr == LDAP_SUCCESS)
|
|
|
|
{
|
|
|
|
if(ldap_count_entries(ld, pMessage) == 1)
|
|
|
|
{
|
|
|
|
if(pBerVal = ldap_get_values_len(ld, pMessage, rootAttr[0]))
|
|
|
|
{
|
|
|
|
if(ldap_count_values_len(pBerVal) == 1)
|
|
|
|
ret = kull_m_string_qad_ansi_c_to_unicode(pBerVal[0]->bv_val, pBerVal[0]->bv_len);
|
|
|
|
else PRINT_ERROR(L"ldap_get_values_len is NOT 1\n");
|
|
|
|
ldap_value_free_len(pBerVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else PRINT_ERROR(L"ldap_count_entries is NOT 1\n");
|
|
|
|
}
|
|
|
|
else PRINT_ERROR(L"ldap_search_s 0x%x (%u)\n", dwErr, dwErr);
|
|
|
|
if(pMessage)
|
|
|
|
ldap_msgfree(pMessage);
|
|
|
|
return ret;
|
|
|
|
}
|