Hi Gary,
The LDAP_SERVER_SORT_OID is not being used in any of the queries.
The code below is performing a number of different queries to the server, using both the ANSI and Unicode version of the LDAP APIs. I've tried this MultiByteToWideChar\WideCharToMultiByte with UTF-8 encoding and codepages, it's does make much difference, the output is not the same as LDP, which shows the DN as CN=Gačy Reynolds,OU=test1,DC=w2k12,DC=local and works, where my code doesn't.
String Temp;
int c;
LDAPMessage *pMes;
DWORD ret;
LDAP *hLDAP;
DWORD SrcLen=0, DstLen=0;
wchar_t wsrc[] = L"CN=Ga?y Reynolds,OU=test1,DC=w2k12,DC=local";
wsrc[5]=0x10d;
char csrc[] = "CN=Ga??y Reynolds,OU=test1,DC=w2k12,DC=local";
csrc[5]=0xc4;
csrc[6]=0x8d;
wchar_t Dst[1024];
char Dst1[1024];
SrcLen = strlen(csrc);
DstLen=1024;
ret = MultiByteToWideChar(CP_UTF8,0,csrc,-1,Dst,DstLen);
Temp = "";
for(c=0;c<ret;c++){
Temp += IntToHex(Dst[c],4) + " ";
}
memLDPResults->Lines->Add("MultiByteToWideChar = " + Temp);
memLDPResults->Lines->Add("MultiByteToWideChar = " + String(Dst));
SrcLen = wcslen(wsrc);
ret = WideCharToMultiByte(CP_UTF8,0,wsrc,-1,Dst1,DstLen,NULL,NULL);
Temp = "";
for(c=0;c<ret;c++){
Temp += IntToHex((BYTE)Dst1[c],2) + " ";
}
memLDPResults->Lines->Add("WideCharToMultiByte = " + Temp);
memLDPResults->Lines->Add("WideCharToMultiByte = " + String(Dst1));
//---------------------------------------------------------------------------
// Search with Unicode string, Unicode API
//---------------------------------------------------------------------------
memLDPResults->Lines->Add("Search with Unicode string, Unicode API");
hLDAP = ldap_initW(L"192.168.1.245",LDAP_PORT);
if (!hLDAP){
memLDPResults->Lines->Add("Failed to ldap_initW to server, ");
return;
}
if (ldap_bind_sW(hLDAP,L"CN=Administrator,CN=Users,DC=w2k12,DC=local",L"Pass",LDAP_AUTH_SIMPLE)){
memLDPResults->Lines->Add("Failed to ldap_bindW to server, " );
return;
}
struct l_timeval tm;
tm.tv_sec = 60;
tm.tv_usec = 0;
try {
ret = ldap_search_ext_sW(hLDAP,
wsrc,
LDAP_SCOPE_BASE,
L"(objectclass=*)",
NULL,
0,
NULL,
NULL,
&tm,
0,
&pMes);
}
catch(...){
ret = -1;
}
if (ret != LDAP_SUCCESS && ret !=9){ // enable partial results to be returned
memLDPResults->Lines->Add("Failed to ldap_search_ext_sW to server, " + IntToStr(LdapGetLastError()) );
} else {
memLDPResults->Lines->Add("ldap_search_ext_sW Found " );
ldap_msgfree(pMes);
}
ldap_unbind_s(hLDAP);
//---------------------------------------------------------------------------
// Search with MultiByteToWideChar char -> unicode, Unicode API
//---------------------------------------------------------------------------
memLDPResults->Lines->Add("Search with MultiByteToWideChar char -> unicode, Unicode API");
hLDAP = ldap_initW(L"192.168.1.245",LDAP_PORT);
if (!hLDAP){
memLDPResults->Lines->Add("Failed to ldap_initW to server, ");
return;
}
if (ldap_bind_sW(hLDAP,L"CN=Administrator,CN=Users,DC=w2k12,DC=local",L"Pass",LDAP_AUTH_SIMPLE)){
memLDPResults->Lines->Add("Failed to ldap_bindW to server, " );
return;
}
tm.tv_sec = 60;
tm.tv_usec = 0;
try {
ret = ldap_search_ext_sW(hLDAP,
Dst,
LDAP_SCOPE_BASE,
L"(objectclass=*)",
NULL,
0,
NULL,
NULL,
&tm,
0,
&pMes);
}
catch(...){
ret = -1;
}
if (ret != LDAP_SUCCESS && ret !=9){ // enable partial results to be returned
memLDPResults->Lines->Add("Failed to ldap_search_ext_sW to server, " + IntToStr(LdapGetLastError()) );
} else {
memLDPResults->Lines->Add("ldap_search_ext_sW Found " );
ldap_msgfree(pMes);
}
ldap_unbind_s(hLDAP);
//---------------------------------------------------------------------------
// Search with char, ANSI API
//---------------------------------------------------------------------------
memLDPResults->Lines->Add("Search with char, ANSI API");
hLDAP = ldap_initA("192.168.1.245",LDAP_PORT);
if (!hLDAP){
memLDPResults->Lines->Add("Failed to ldap_init to server, ");
return;
}
if (ldap_bind_sA(hLDAP,"CN=Administrator,CN=Users,DC=w2k12,DC=local","Pass",LDAP_AUTH_SIMPLE)){
memLDPResults->Lines->Add("Failed to ldap_bind to server, " );
return;
}
try {
ret = ldap_search_ext_sA(hLDAP,
csrc,
LDAP_SCOPE_BASE,
"(objectclass=*)",
NULL,
0,
NULL,
NULL,
&tm,
0,
&pMes);
}
catch(...){
ret = -1;
}
if (ret != LDAP_SUCCESS && ret !=9){ // enable partial results to be returned
memLDPResults->Lines->Add("Failed to ldap_search_ext_s to server, " + IntToStr(LdapGetLastError()) );
} else {
memLDPResults->Lines->Add("ldap_search_ext_s Found " );
ldap_msgfree(pMes);
}
ldap_unbind_s(hLDAP);
//---------------------------------------------------------------------------
// Search with WideCharToMultiByte unicode -> char, ANSI API
//---------------------------------------------------------------------------
memLDPResults->Lines->Add("Search with WideCharToMultiByte unicode -> char, ANSI API");
hLDAP = ldap_initA("192.168.1.245",LDAP_PORT);
if (!hLDAP){
memLDPResults->Lines->Add("Failed to ldap_init to server, ");
return;
}
if (ldap_bind_sA(hLDAP,"CN=Administrator,CN=Users,DC=w2k12,DC=local","Pass",LDAP_AUTH_SIMPLE)){
memLDPResults->Lines->Add("Failed to ldap_bind to server, " );
return;
}
try {
ret = ldap_search_ext_sA(hLDAP,
Dst1,
LDAP_SCOPE_BASE,
"(objectclass=*)",
NULL,
0,
NULL,
NULL,
&tm,
0,
&pMes);
}
catch(...){
ret = -1;
}
if (ret != LDAP_SUCCESS && ret !=9){ // enable partial results to be returned
memLDPResults->Lines->Add("Failed to ldap_search_ext_s to server, " + IntToStr(LdapGetLastError()) );
} else {
memLDPResults->Lines->Add("ldap_search_ext_s Found " );
ldap_msgfree(pMes);
}
ldap_unbind_s(hLDAP);
This is the output of the code
MultiByteToWideChar = 0043 004E 003D 0047 0061 010D 0079 0020 0052 0065 0079 006E 006F 006C 0064 0073 002C 004F 0055 003D 0074 0065 0073 0074 0031 002C 0044 0043 003D 0077 0032 006B 0031 0032 002C 0044 0043 003D 006C 006F 0063 0061 006C 0000
MultiByteToWideChar = CN=Gacy Reynolds,OU=test1,DC=w2k12,DC=local
WideCharToMultiByte = 43 4E 3D 47 61 C4 8D 79 20 52 65 79 6E 6F 6C 64 73 2C 4F 55 3D 74 65 73 74 31 2C 44 43 3D 77 32 6B 31 32 2C 44 43 3D 6C 6F 63 61 6C 00
WideCharToMultiByte = CN=GaÄ y Reynolds,OU=test1,DC=w2k12,DC=local
Search with Unicode string, Unicode API
ldap_search_ext_sW Found
Search with MultiByteToWideChar char -> unicode, Unicode API
ldap_search_ext_sW Found
Search with char, ANSI API
Failed to ldap_search_ext_s to server, 32
Search with WideCharToMultiByte unicode -> char, ANSI API
Failed to ldap_search_ext_s to server, 32
The network trace for the four binds, The first two are showing that the unicode character has been simplified based on the current code page. The last two are showing the unicode format, but both fail, even though they are the same as the LDP search.
The network trace for LDP
Also attached is the network traces for the fourbinds and LDP 211209-ldp.txt 211248-foursearches.txt, just rename them to pcapng files