Exemplo n.º 1
0
        public void TestPacketPartialAttribute()
        {
            var expected            = "3084000000800204000000016478042d636e3d62696e64557365722c636e3d55736572732c64633d6465762c64633d636f6d70616e792c64633d636f6d3047301804037569643111040f75736572756964676f657368657265302b040b6f626a656374436c617373311c040c616161616161616161616161040c626262626262626262626262";
            var responseEntryPacket = new LdapPacket(1);
            var searchResultEntry   = new LdapAttribute(LdapOperation.SearchResultEntry);

            searchResultEntry.ChildAttributes.Add(new LdapAttribute(UniversalDataType.OctetString, "cn=bindUser,cn=Users,dc=dev,dc=company,dc=com"));   //  objectName

            var partialAttributeList = new LdapAttribute(UniversalDataType.Sequence);



            partialAttributeList.ChildAttributes.Add(new LdapPartialAttribute("uid", "useruidgoeshere"));
            partialAttributeList.ChildAttributes.Add(new LdapPartialAttribute("objectClass", new List <String> {
                "aaaaaaaaaaaa", "bbbbbbbbbbbb"
            }));

            searchResultEntry.ChildAttributes.Add(partialAttributeList);
            responseEntryPacket.ChildAttributes.Add(searchResultEntry);
            var responsEntryBytes = responseEntryPacket.GetBytes();

            Console.WriteLine(Utils.ByteArrayToString(responsEntryBytes));


            var packet = LdapPacket.ParsePacket(responsEntryBytes);

            RecurseAttributes(packet);
            Assert.AreEqual(expected, Utils.ByteArrayToString(packet.GetBytes()));
        }
Exemplo n.º 2
0
        public void TestLdapAttributeSequenceGetBytesShortcut()
        {
            var packet       = new LdapPacket(1);
            var bindresponse = new LdapResultAttribute(LdapOperation.BindResponse, LdapResult.success);

            packet.ChildAttributes.Add(bindresponse);

            var expected = "300f02040000000161070a010004000400"; // "300c02010161070a010004000400";

            Assert.AreEqual(expected, Utils.ByteArrayToString(packet.GetBytes()));
        }
Exemplo n.º 3
0
        public void TestLdapAttributeSequenceGetBytesString()
        {
            var packet = new LdapPacket(1);

            var bindrequest = new LdapAttribute(LdapOperation.BindRequest);

            bindrequest.ChildAttributes.Add(new LdapAttribute(UniversalDataType.Integer, (Byte)3));
            bindrequest.ChildAttributes.Add(new LdapAttribute(UniversalDataType.OctetString, "cn=bindUser,cn=Users,dc=dev,dc=company,dc=com"));
            bindrequest.ChildAttributes.Add(new LdapAttribute((byte)0, "bindUserPassword"));

            packet.ChildAttributes.Add(bindrequest);

            var expected = "304c0204000000016044020103042d636e3d62696e64557365722c636e3d55736572732c64633d6465762c64633d636f6d70616e792c64633d636f6d801062696e645573657250617373776f7264"; // "30490201016044020103042d636e3d62696e64557365722c636e3d55736572732c64633d6465762c64633d636f6d70616e792c64633d636f6d801062696e645573657250617373776f7264";

            Assert.AreEqual(expected, Utils.ByteArrayToString(packet.GetBytes()));
        }
Exemplo n.º 4
0
        public void TestLdapAttributeSequenceGetBytes2()
        {
            var packet = new LdapPacket(1);

            var bindresponse = new LdapAttribute(LdapOperation.BindResponse);

            var resultCode = new LdapAttribute(UniversalDataType.Enumerated, (Byte)LdapResult.success);

            bindresponse.ChildAttributes.Add(resultCode);

            var matchedDn         = new LdapAttribute(UniversalDataType.OctetString);
            var diagnosticMessage = new LdapAttribute(UniversalDataType.OctetString);

            bindresponse.ChildAttributes.Add(matchedDn);
            bindresponse.ChildAttributes.Add(diagnosticMessage);

            packet.ChildAttributes.Add(bindresponse);

            var expected = "300f02040000000161070a010004000400"; // "300c02010161070a010004000400";

            Assert.AreEqual(expected, Utils.ByteArrayToString(packet.GetBytes()));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Handle bindrequests
        /// </summary>
        /// <param name="bindrequest"></param>
        private Boolean HandleBindRequest(Stream stream, LdapPacket requestPacket)
        {
            var bindrequest = requestPacket.ChildAttributes.SingleOrDefault(o => o.LdapOperation == LdapOperation.BindRequest);
            var username    = bindrequest.ChildAttributes[1].GetValue <String>();
            var password    = bindrequest.ChildAttributes[2].GetValue <String>();

            var response = LdapResult.invalidCredentials;

            if (username == "cn=bindUser,cn=Users,dc=dev,dc=company,dc=com" && password == "bindUserPassword" ||
                username == "cn=user,dc=example,dc=com" && password == "123")
            {
                response = LdapResult.success;
            }

            var responsePacket = new LdapPacket(requestPacket.MessageId);

            responsePacket.ChildAttributes.Add(new LdapResultAttribute(LdapOperation.BindResponse, response));
            var responseBytes = responsePacket.GetBytes();

            stream.Write(responseBytes, 0, responseBytes.Length);
            return(response == LdapResult.success);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Handle search requests
        /// </summary>
        /// <param name="searchRequest"></param>
        /// <returns></returns>
        private void HandleSearchRequest(NetworkStream stream, LdapPacket requestPacket)
        {
            var searchRequest = requestPacket.ChildAttributes.SingleOrDefault(o => o.LdapOperation == LdapOperation.SearchRequest);
            var filter        = searchRequest.ChildAttributes[6];

            if ((LdapFilterChoice)filter.ContextType == LdapFilterChoice.equalityMatch && filter.ChildAttributes[0].GetValue <String>() == "sAMAccountName" && filter.ChildAttributes[1].GetValue <String>() == "testuser") // equalityMatch
            {
                var responseEntryPacket = new LdapPacket(requestPacket.MessageId);
                var searchResultEntry   = new LdapAttribute(LdapOperation.SearchResultEntry);
                searchResultEntry.ChildAttributes.Add(new LdapAttribute(UniversalDataType.OctetString, "cn=testuser,cn=Users,dc=dev,dc=company,dc=com"));
                searchResultEntry.ChildAttributes.Add(new LdapAttribute(UniversalDataType.Sequence));
                responseEntryPacket.ChildAttributes.Add(searchResultEntry);
                var responsEntryBytes = responseEntryPacket.GetBytes();
                stream.Write(responsEntryBytes, 0, responsEntryBytes.Length);
            }

            var responseDonePacket = new LdapPacket(requestPacket.MessageId);

            responseDonePacket.ChildAttributes.Add(new LdapResultAttribute(LdapOperation.SearchResultDone, LdapResult.success));
            var responseDoneBytes = responseDonePacket.GetBytes();

            stream.Write(responseDoneBytes, 0, responseDoneBytes.Length);
        }