vforteli / Flexinets.Ldap.Server

Basic LDAP server for .Net. Parse and assemble LDAP packets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Search Success response and pack response in one shoot

aria321 opened this issue · comments

commented

Thank you for your project it is useful for us to have custom-ed LDAP server but we have some problems there as below descriptions:
I am using https://github.com/Sammuel-Miranda/LdapServerLib but I think there is a problem in HandleRequest
because I have tested with WireShark in a real LDAP system and your LDAP server code
When a Search request came from client the real LDAP server passed out Success and Search result in one stream as below:
true

but with your LDAP server the result for search request is something like below:

wrong
thanks in advance.

I resolved this as below code:
Success and returning Pack in one shoot:

private void ReturnSingleUser(SysSock.NetworkStream stream, int MessageID, string UserName)
{
    if (!string.IsNullOrEmpty(UserName))
    {
        UserName = UserName.ToLower();
        foreach (LDap.IUserData user in this._validator.ListUsers())
        {
            if (user.UserName == UserName)
            {
                using (LCore.LdapPacket pkO = this.RespondUserData(user, this.UserPack(user), MessageID))
                {
                    //Returning success and user pack together
                    LCore.LdapPacket response = new LCore.LdapPacket(MessageID);
                    var searchAttribSuccess = new LCore.LdapResultAttribute(LCore.LdapOperation.SearchResultDone, LCore.LdapResult.success); 
                    response.ChildAttributes.Add(searchAttribSuccess); 
                    var userLdapPacket = pkO.GetBytes();
                    var mainLdapPacketRes = response.GetBytes();
                    //Merging LdapPacks
                    byte[] allPacks = new byte[userLdapPacket.Length + mainLdapPacketRes.Length];
                    Buffer.BlockCopy(userLdapPacket, 0, allPacks, 0, userLdapPacket.Length);
                    Buffer.BlockCopy(mainLdapPacketRes, 0, allPacks, userLdapPacket.Length, mainLdapPacketRes.Length);
                    stream.Write(allPacks, 0, allPacks.Length);
                    Logging.Log(LoggingMode.Prompt, $"User found by username:{UserName}");
                }
                break;
            }
        }
    }
}

but still there is a problem that I don't know what is that if you take a deep look at response of real LDAP server there is a sentence said 2 results in picture while in your LDAP response that sentence in 1 result, why?

Can you please suggest and guide a little ?

Hello,

From the capture it seems like it did work?

This shouldnt be an issue though. LDAP packets dont necessarily translate to TCP segments, so it could be that depending on packet size, MTU and what not, the LDAP packets are sent or not sent in the same TCP segment. I guess it may depend on if you flush the stream as well.

commented

Hello,

From the capture it seems like it did work?

This shouldnt be an issue though. LDAP packets dont necessarily translate to TCP segments, so it could be that depending on packet size, MTU and what not, the LDAP packets are sent or not sent in the same TCP segment. I guess it may depend on if you flush the stream as well.

So this is not an issue, I was confused because in Search response of real LDAP there are two LightWeight Directory Access Protocol in one response as you can see in above screenshots but in yours there are two separate response one of them is success message and another one is response packet, anyway I changed the code to merge them and write them to NetworkStream as one response as:

LCore.LdapPacket response = new LCore.LdapPacket(MessageID);
var searchAttribSuccess = new LCore.LdapResultAttribute(LCore.LdapOperation.SearchResultDone, LCore.LdapResult.success); 
response.ChildAttributes.Add(searchAttribSuccess); 
var userLdapPacket = pkO.GetBytes();
var mainLdapPacketRes = response.GetBytes();
//Merging LdapPacks
byte[] allPacks = new byte[userLdapPacket.Length + mainLdapPacketRes.Length];
Buffer.BlockCopy(userLdapPacket, 0, allPacks, 0, userLdapPacket.Length);
Buffer.BlockCopy(mainLdapPacketRes, 0, allPacks, userLdapPacket.Length, mainLdapPacketRes.Length);
 stream.Write(allPacks, 0, allPacks.Length);

I don't know it make side effect or not in future.

Anyway thank you for your attention.