WebFreak001 / ldap

D LDAP client library using winldap on windows and openldap on other platforms
6 stars 2 forks source link

Not working on Linux x64 #4

Open TransientResponse opened 5 years ago

TransientResponse commented 5 years ago

Doesn't appear to be working on Linux x64, while it does on Windows 10 x64.

The following code (edited to remove secrets) works correctly on Windows (with winldap, built with LDC 1.13.0):

import std.stdio;
import ldap;

void main()
{
    int proto_version;

    auto ldap = LDAPConnection("192.168.100.10:389"); // normal ldap connection
    auto auther = LDAPAuthenticationEngine("192.168.100.10:389"); // ldap connection with fast binding and no encryption support on windows (used for password authentication)
    ldap.getOption(LDAP_OPT_PROTOCOL_VERSION, &proto_version);
    if (proto_version == 2)
    {
        proto_version = 3;
        ldap.setOption(LDAP_OPT_PROTOCOL_VERSION, &proto_version);
        writeln("Switched to protocol version 3");
    }

    ldap.bind("<snip>", "<snip>");

    auto arr = ldap.search("dc=XX,dc=XXX,dc=XX",
            LDAPSearchScope.subTree, `(CN=R*)`, ["samaccountname"]); // find all users & contacts

    writefln("Found %s results", arr.length);
    foreach (r; arr)
    {
        writeln(r.distinguishedName); // print path of contact
        foreach (k, v; r.attributes)
        {
            writef("%s = %s", k, v); // prints location of contacts (because of ["l"] argument above)
        }
    }
    writeln("Done");

    assert(!auther.check("non valid user", "non valid password"));
    assert(auther.check("<snip>", "<snip>"));
}

On Windows I get correct results, but on Linux (both OpenSuSE Leap 15 x64 with DMD 2.084.1 and LDC 1.14.0, and Ubuntu 18.04 (WSL) with LDC 1.8.0) I get the same error:

Performing "debug" build using ldc2 for x86_64.
ldap 0.2.0: building configuration "library"...
ldap_test ~master: building configuration "application"...
Running ./ldap_test 
Switched to protocol version 3
ldap.LDAPException@/home/rraab/.dub/packages/ldap-0.2.0/ldap/source/ldap.d(335): LDAP Error 'Operations error' in search (Error code 1)
----------------
??:? [0x457f30]
??:? [0x460efa]
??:? [0x4493fd]
ldap.d:219 [0x41bc57]
app.d:20 [0x404e9f]
??:? [0x44907f]
??:? [0x448f77]
__entrypoint.d:8 [0x41a8b4]
??:? __libc_start_main [0x7f9b2fe79f49]
start.S:120 [0x404c59]
Program exited with code 1
TransientResponse commented 5 years ago

Additional info: ldapsearch with the same host, bind, etc and same filter as in the D sample works correctly

WebFreak001 commented 5 years ago

do you have openldap installed? I made this package to work on both windows and linux and it works on our linux machines. Note that you might want to try some different bind values. In your bind are you specifying "username@domain" or "domain\username"? You have to specify a username with domain here for it to work properly

TransientResponse commented 5 years ago

I've tried both forms with domain and get the same error. It's erroring at app.d line 20, which is the ldap.search line. I get "invalid credentials" on line 18 (bind) if I purposely use a bad username or domain.

Furthermore, it's connecting and switching to protocol version 3, and binding too.

On Ubuntu 18.04, I have libldap and libldap-dev which claim to be OpenLDAP. My OpenSuSE box has openldap2 (and -devel) 2.4.46 installed.

I'll try to ascertain which version of MS Active Directory is running here, as that might have an impact.

TransientResponse commented 5 years ago

So I've whittled down some Wireshark captures between Windows and WSL to see what went wrong and why. As it turns out the actual "operative" filter succeeds in Linux, but some later filters fail and that propagates out to erroring out the whole thing.

A good result from Windows here: image

A bad result from within WSL here: image

It looks like the same queries but in reverse order. Interestingly, the first one from Windows fails but the rest succeed, while in Linux they all fail despite being the same filters (and previously successfully bound). Could the order the queries are made matter?

WebFreak001 commented 5 years ago

Uh I'm not really an expert in LDAP, I just made this library for cross-platform login and basic data fetching in our AD.

I think encryption wasn't properly supported or something, maybe your server has some forced settings here or something?