/// <summary>
        /// Creates a ModifyRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="objectDn">The DN of object to be modified.</param>
        /// <param name="modificationList">Modification list of attributes.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsModifyRequestPacket CreateModifyRequest(
            AdtsLdapContext context,
            string objectDn,
            params MsLdap.DirectoryAttributeModification[] modificationList)
        {
            int length = (modificationList != null) ? modificationList.Length : 0;

            ModifyRequest_modifications_element[] modifyRequestArray = new ModifyRequest_modifications_element[length];
            for (int i = 0; i < length; i++)
            {
                modifyRequestArray[i] = new ModifyRequest_modifications_element(
                    new ModifyRequest_modifications_element_operation((long)modificationList[i].Operation),
                    new ModifyRequest_modifications_element_modification(
                        new AttributeType(modificationList[i].Name),
                        CreateAttributeValueSet((string[])modificationList[i].GetValues(typeof(string)))));
            }
            Asn1SequenceOf <ModifyRequest_modifications_element> modificationSequence = new Asn1SequenceOf <ModifyRequest_modifications_element>(modifyRequestArray);

            ModifyRequest modifyRequest = new ModifyRequest(
                new LDAPDN(objectDn ?? string.Empty),
                modificationSequence);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.modifyRequest, modifyRequest);

            LDAPMessage             message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsModifyRequestPacket packet  = new AdtsModifyRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates a ModifyDNRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="oldDn">The original DN to be modified.</param>
        /// <param name="newRdn">The new relative DN.</param>
        /// <param name="newParentDn">
        /// The new parent DN. For LDAP v3 only. Ignored when creating LDAP v2 requests.
        /// </param>
        /// <param name="delOldRdn">
        /// Whether to delete old RDN. For LDAP v3 only. Ignored when creating LDAP v2 requests.
        /// </param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsModifyDnRequestPacket CreateModifyDnRequest(
            AdtsLdapContext context,
            string oldDn,
            string newRdn,
            string newParentDn,
            bool delOldRdn)
        {
            ModifyDNRequest modifyDnRequest = new ModifyDNRequest(
                new LDAPDN(oldDn ?? string.Empty),
                new RelativeLDAPDN(newRdn ?? string.Empty),
                new Asn1Boolean(delOldRdn),
                new LDAPDN(newParentDn));

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.modifyDNRequest, modifyDnRequest);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsModifyDnRequestPacket packet = new AdtsModifyDnRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates an AddRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="objectDn">The DN of the object to be added.</param>
        /// <param name="attributes">Attributes to be set.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsAddRequestPacket CreateAddRequest(
            AdtsLdapContext context,
            string objectDn,
            params KeyValuePair <string, string[]>[] attributes)
        {
            int length = (attributes != null) ? attributes.Length : 0;

            AddRequest_attrs_element[] addrequestAttrsArray = new AddRequest_attrs_element[length];
            for (int i = 0; i < length; i++)
            {
                addrequestAttrsArray[i] = new AddRequest_attrs_element(
                    new AttributeType(attributes[i].Key),
                    CreateAttributeValueSet(attributes[i].Value));
            }
            Asn1SequenceOf <AddRequest_attrs_element> attributeList = new Asn1SequenceOf <AddRequest_attrs_element>(addrequestAttrsArray);

            AddRequest addRequest = new AddRequest(
                new LDAPDN(objectDn ?? string.Empty),
                attributeList);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.addRequest, addRequest);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation);

            AdtsAddRequestPacket packet = new AdtsAddRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates an AddRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="objectDn">The DN of the object to be added.</param>
        /// <param name="attributes">Attributes to be set.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsAddRequestPacket CreateAddRequest(
            AdtsLdapContext context,
            string objectDn,
            params KeyValuePair <string, string[]>[] attributes)
        {
            int length = (attributes != null) ? attributes.Length : 0;

            AttributeList_element[] attributeArray = new AttributeList_element[length];
            for (int i = 0; i < length; i++)
            {
                AttributeValue[] attributeValues = new AttributeValue[attributes[i].Value.Length];
                for (int j = 0; j < attributes[i].Value.Length; i++)
                {
                    attributeValues[j] = new AttributeValue(attributes[i].Value[j]);
                }
                attributeArray[i] = new AttributeList_element(new AttributeDescription(attributes[i].Key), new Asn1SetOf <AttributeValue>(attributeValues));
            }

            AddRequest addRequest = new AddRequest(
                new LDAPDN(objectDn ?? string.Empty),
                new AttributeList(attributeArray));

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.extendedReq, addRequest);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation, null);

            AdtsAddRequestPacket packet = new AdtsAddRequestPacket();

            packet.ldapMessagev3 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a SearchResultReference. For LDAP v3 only.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="referenceUrls">The referenced URL.</param>
        /// <returns>The packet that contains the response.</returns>
        internal override AdtsSearchResultReferencePacket CreateSearchResultReference(
            AdtsLdapContext context,
            string[] referenceUrls)
        {
            int length = (referenceUrls != null) ? referenceUrls.Length : 0;

            LDAPURL[] ldapUrlArray = new LDAPURL[length];
            for (int i = 0; i < length; i++)
            {
                ldapUrlArray[i] = new LDAPURL(referenceUrls[i]);
            }
            SearchResultReference reference = new SearchResultReference(ldapUrlArray);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.searchResRef, reference);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation, null);
            AdtsSearchResultReferencePacket packet = new AdtsSearchResultReferencePacket();

            packet.ldapMessagev3 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a SearchResultDone packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="resultCode">Result code of previous request, as specified in RFC 2251.</param>
        /// <param name="matchedDn">Matched DN.</param>
        /// <param name="errorMessage">Error message for result code. Required.</param>
        /// <param name="referral">Referral. Optional.</param>
        /// <returns>The packet that contains the response.</returns>
        internal override AdtsSearchResultDonePacket CreateSearchResultDone(
            AdtsLdapContext context,
            MsLdap.ResultCode resultCode,
            string matchedDn,
            string errorMessage,
            string[] referral)
        {
            SearchResultDone searchResultDone = new SearchResultDone(
                new LDAPResult_resultCode((long)resultCode),
                new LDAPDN(matchedDn ?? string.Empty),
                new LDAPString(errorMessage ?? string.Empty),
                CreateReferral(referral));

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.searchResDone, searchResultDone);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation, null);
            AdtsSearchResultDonePacket packet = new AdtsSearchResultDonePacket();

            packet.ldapMessagev3 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates a BindResponse for normal bindings, SASL bindings and sicily bindings.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="resultCode">Result code of previous request, as specified in RFC 2251.</param>
        /// <param name="matchedDn">Matched DN. Required, but can be an empty string.</param>
        /// <param name="errorMessage">Error message for result code. Required.</param>
        /// <param name="referral">Referral. Optional and for LDAP v3 only.</param>
        /// <param name="serverCredentials">Server credentials, optional for normal bind.</param>
        /// <returns>The packet that contains the response.</returns>
        internal override AdtsBindResponsePacket CreateBindResponse(
            AdtsLdapContext context,
            MsLdap.ResultCode resultCode,
            string matchedDn,
            string errorMessage,
            string[] referral,
            byte[] serverCredentials)
        {
            BindResponse bindResponse = new BindResponse(
                new LDAPResult_resultCode((long)resultCode),
                new LDAPDN(matchedDn ?? string.Empty),
                new LDAPString(errorMessage ?? string.Empty));

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.bindResponse, bindResponse);

            LDAPMessage            message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsBindResponsePacket packet  = new AdtsBindResponsePacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates a SearchResultEntry packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="matchedDn">Matched DN.</param>
        /// <param name="attributes">The attributes and values that are contained in the entry.</param>
        /// <returns>The packet that contains the response.</returns>
        internal override AdtsSearchResultEntryPacket CreateSearchedResultEntry(
            AdtsLdapContext context,
            string matchedDn,
            params KeyValuePair <string, string[]>[] attributes)
        {
            int length = (attributes != null) ? attributes.Length : 0;

            PartialAttributeList_element[] partialAttributeElementArray = new PartialAttributeList_element[length];
            for (int i = 0; i < length; i++)
            {
                partialAttributeElementArray[i] = new PartialAttributeList_element(
                    new AttributeDescription(attributes[i].Key),
                    CreateAttributeValueSet(attributes[i].Value));
            }
            PartialAttributeList attributeList = new PartialAttributeList(partialAttributeElementArray);

            SearchResultEntry entry = new SearchResultEntry(
                new LDAPDN(matchedDn ?? string.Empty),
                attributeList);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.searchResEntry, entry);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation, null);
            AdtsSearchResultEntryPacket packet = new AdtsSearchResultEntryPacket();

            packet.ldapMessagev3 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Adds extended controls to a packet.
        /// </summary>
        /// <param name="packet">The packet to which the controls are added.</param>
        /// <param name="controls">The controls.</param>
        internal void AddDirectoryControls(AdtsLdapPacket packet, params MsLdap.DirectoryControl[] controls)
        {
            LDAPMessage message = packet.ldapMessagev3;

            int existingControlCount = (message.controls != null) ? message.controls.Elements.Length : 0;

            Control[] controlArray = new Control[existingControlCount + controls.Length];

            // Add original existing controls
            for (int i = 0; i < existingControlCount; i++)
            {
                controlArray[i] = message.controls.Elements[i];
            }

            // Add newly added controls
            for (int i = 0; i < controls.Length; i++)
            {
                controlArray[existingControlCount + i] = new Control(
                    new LDAPOID(controls[i].Type),
                    new Asn1Boolean(controls[i].IsCritical),
                    new Asn1OctetString(controls[i].GetValue()));
            }

            Controls allControls = new Controls(controlArray);

            message.controls = allControls;
        }
        /// <summary>
        /// Creates a SearchResultEntry packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="matchedDn">Matched DN.</param>
        /// <param name="attributes">The attributes and values that are contained in the entry.</param>
        /// <returns>The packet that contains the response.</returns>
        internal override AdtsSearchResultEntryPacket CreateSearchedResultEntry(
            AdtsLdapContext context,
            string matchedDn,
            params KeyValuePair <string, string[]>[] attributes)
        {
            int length = (attributes != null) ? attributes.Length : 0;

            SearchResponse_entry_attributes_element[] attributesElementArray = new SearchResponse_entry_attributes_element[length];
            for (int i = 0; i < length; i++)
            {
                attributesElementArray[i] = new SearchResponse_entry_attributes_element(
                    new AttributeType(attributes[i].Key),
                    CreateAttributeValueSet(attributes[i].Value));
            }
            Asn1SequenceOf <SearchResponse_entry_attributes_element> attributesElements = new Asn1SequenceOf <SearchResponse_entry_attributes_element>(attributesElementArray);

            SearchResponse_entry entry = new SearchResponse_entry(
                new LDAPDN(matchedDn ?? string.Empty),
                attributesElements);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.searchResponse, new SearchResponse(SearchResponse.entry, entry));

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsSearchResultEntryPacket packet = new AdtsSearchResultEntryPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Creates a DelRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="objectDn">The DN of the object to be deleted.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsDelRequestPacket CreateDelRequest(AdtsLdapContext context, string objectDn)
        {
            DelRequest delRequest = new DelRequest(objectDn ?? string.Empty);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.delRequest, delRequest);

            LDAPMessage          message = new LDAPMessage(new MessageID(context.MessageId), operation, null);
            AdtsDelRequestPacket packet  = new AdtsDelRequestPacket();

            packet.ldapMessagev3 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Decodes an LDAP v3 packet.
        /// </summary>
        /// <param name="messageBytes">The message bytes that contains the packet data.</param>
        /// <param name="context">The context that contains decode-related information.</param>
        /// <returns>Decoded LDAP v3 packet.</returns>
        internal override AdtsLdapPacket ParseAdtsLdapPacket(byte[] messageBytes, AdtsLdapContext context)
        {
            LDAPMessage        message      = new LDAPMessage();
            Asn1DecodingBuffer decodeBuffer = new Asn1DecodingBuffer(messageBytes);

            message.BerDecode(decodeBuffer);

            Type           innerMessageType = message.protocolOp.GetData().GetType();
            AdtsLdapPacket packet           = CreatePacketFromType(innerMessageType);

            context.MessageId    = (long)message.messageID.Value;
            packet.messageId     = (long)message.messageID.Value;
            packet.ldapMessagev3 = message;

            return(packet);
        }
        /// <summary>
        /// Creates an UnbindRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsUnbindRequestPacket CreateUnbindRequest(AdtsLdapContext context)
        {
            UnbindRequest unbindRequest = new UnbindRequest();

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.unbindRequest, unbindRequest);

            LDAPMessage             message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsUnbindRequestPacket packet  = new AdtsUnbindRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates an AbandonRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="messageId">The ID of message to be abandoned.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsAbandonRequestPacket CreateAbandonRequest(AdtsLdapContext context, long messageId)
        {
            AbandonRequest abandonRequest = new AbandonRequest(messageId);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.abandonRequest, abandonRequest);

            LDAPMessage message             = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsAbandonRequestPacket packet = new AdtsAbandonRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates a BindRequestPacket with context and BindRequest.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="bindRequest">The BindRequest message.</param>
        /// <returns>The BindRequestPacket.</returns>
        private AdtsBindRequestPacket CreateBindRequestPacket(
            AdtsLdapContext context,
            BindRequest bindRequest)
        {
            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.bindRequest, bindRequest);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation);

            AdtsBindRequestPacket packet = new AdtsBindRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates a SearchRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="dn">The DN to be searched.</param>
        /// <param name="sizeLimit">Size limit.</param>
        /// <param name="timeLimit">Time limit, in seconds.</param>
        /// <param name="scope">Search scope. Base, single level, or subtree.</param>
        /// <param name="dereferenceAliases">Dereference aliase options.</param>
        /// <param name="filter">Search filter.</param>
        /// <param name="typesOnly">
        /// Specifies whether the search returns only the attribute names without the attribute values.
        /// </param>
        /// <param name="attributes">The attributes to be retrieved.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsSearchRequestPacket CreateSearchRequest(
            AdtsLdapContext context,
            string dn,
            long sizeLimit,
            long timeLimit,
            MsLdap.SearchScope scope,
            MsLdap.DereferenceAlias dereferenceAliases,
            Asn1Choice filter,
            bool typesOnly,
            params string[] attributes)
        {
            int length = (attributes != null) ? attributes.Length : 0;

            AttributeType[] attributeTypeArray = new AttributeType[length];
            for (int i = 0; i < length; i++)
            {
                attributeTypeArray[i] = new AttributeType(attributes[i]);
            }
            Asn1SequenceOf <AttributeType> attributeList = new Asn1SequenceOf <AttributeType>(attributeTypeArray);

            SearchRequest searchRequest = new SearchRequest(
                new LDAPDN(dn ?? string.Empty),
                new SearchRequest_scope((long)scope),
                new SearchRequest_derefAliases((long)dereferenceAliases),
                new Asn1Integer(sizeLimit),
                new Asn1Integer(timeLimit),
                new Asn1Boolean(typesOnly),
                (Filter)filter,
                attributeList);

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.searchRequest, searchRequest);

            LDAPMessage             message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsSearchRequestPacket packet  = new AdtsSearchRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Creates an ExtendedRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="requestName">The request name of the extended operation.</param>
        /// <param name="requestValue">The request value of the extended operation.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsExtendedRequestPacket CreateExtendedRequest(
            AdtsLdapContext context,
            string requestName,
            byte[] requestValue)
        {
            ExtendedRequest extendedRequest = new ExtendedRequest(
                new LDAPOID(requestName ?? string.Empty),
                new Asn1OctetString(requestValue));

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.extendedReq, extendedRequest);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation, null);
            AdtsExtendedRequestPacket packet = new AdtsExtendedRequestPacket();

            packet.ldapMessagev3 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates a SicilyBindResponse packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="resultCode">Result code of previous request, as specified in RFC 2251.</param>
        /// <param name="serverCredentials">Server credentials, optional for normal and sicily bind.</param>
        /// <param name="errorMessage">Error message for result code. Required.</param>
        /// <returns>The packet that contains the response.</returns>
        internal override AdtsSicilyBindResponsePacket CreateSicilyBindResponse(
            AdtsLdapContext context,
            MsLdap.ResultCode resultCode,
            byte[] serverCredentials,
            string errorMessage)
        {
            SicilyBindResponse sicilyResponse = new SicilyBindResponse(
                new SicilyBindResponse_resultCode((long)resultCode),
                new Asn1OctetString(serverCredentials ?? (new byte[0])),
                new LDAPString(errorMessage ?? string.Empty));

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.sicilyResponse, sicilyResponse);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsSicilyBindResponsePacket packet = new AdtsSicilyBindResponsePacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates a CompareResponse packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="resultCode">Result code of previous request, as specified in RFC 2251.</param>
        /// <param name="matchedDn">Matched DN.</param>
        /// <param name="errorMessage">Error message for result code. Required.</param>
        /// <param name="referral">Referral. Optional. Used for LDAP v3 only.</param>
        /// <returns>The packet that contains the response.</returns>
        internal override AdtsCompareResponsePacket CreateCompareResponse(
            AdtsLdapContext context,
            MsLdap.ResultCode resultCode,
            string matchedDn,
            string errorMessage,
            string[] referral)
        {
            CompareResponse compareResponse = new CompareResponse(
                new LDAPResult_resultCode((long)resultCode),
                new LDAPDN(matchedDn ?? string.Empty),
                new LDAPString(errorMessage ?? string.Empty));

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.compareDNResponse, compareResponse);

            LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsCompareResponsePacket packet = new AdtsCompareResponsePacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
        /// <summary>
        /// Creates a CompareRequest packet.
        /// </summary>
        /// <param name="context">The user context which contains message ID.</param>
        /// <param name="objectDn">The DN of the object to be compared.</param>
        /// <param name="attributeName">The name of the attribute.</param>
        /// <param name="attributeValue">The value of the attribute.</param>
        /// <returns>The packet that contains the request.</returns>
        internal override AdtsCompareRequestPacket CreateCompareRequest(
            AdtsLdapContext context,
            string objectDn,
            string attributeName,
            string attributeValue)
        {
            CompareRequest compareRequest = new CompareRequest(
                new LDAPDN(objectDn ?? string.Empty),
                new AttributeValueAssertion(
                    new AttributeType(attributeName ?? string.Empty),
                    new AttributeValue(attributeValue ?? string.Empty)));

            LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp();

            operation.SetData(LDAPMessage_protocolOp.compareDNRequest, compareRequest);

            LDAPMessage message             = new LDAPMessage(new MessageID(context.MessageId), operation);
            AdtsCompareRequestPacket packet = new AdtsCompareRequestPacket();

            packet.ldapMessagev2 = message;
            packet.messageId     = context.MessageId;

            return(packet);
        }
Ejemplo n.º 21
0
        internal void ReceiveClient(object parameters)
        {
            object[]      parameterArray = parameters as object[];
            TcpClient     tcpClient      = (TcpClient)parameterArray[0];
            int           port           = (int)parameterArray[1];
            NetworkStream tcpStream      = tcpClient.GetStream();
            string        ntlmChallenge  = "";
            string        clientIP       = ((IPEndPoint)(tcpClient.Client.RemoteEndPoint)).Address.ToString();
            string        clientPort     = ((IPEndPoint)(tcpClient.Client.RemoteEndPoint)).Port.ToString();
            string        listenerPort   = ((IPEndPoint)(tcpClient.Client.LocalEndPoint)).Port.ToString();

            try
            {
                while (tcpClient.Connected && isRunning)
                {
                    byte[] requestData = new byte[4096];

                    do
                    {
                        Thread.Sleep(100);
                    }while (!tcpStream.DataAvailable && tcpClient.Connected);

                    while (tcpStream.DataAvailable)
                    {
                        tcpStream.Read(requestData, 0, requestData.Length);
                    }

                    LDAPMessage message = new LDAPMessage();
                    message.Decode(requestData);
                    LDAPMessage message2 = new LDAPMessage();
                    message2.MessageID = message.MessageID;
                    byte[] buffer = new byte[0];
                    OutputConnection(listenerPort, clientIP, clientPort, message.Tag);

                    if (message.Tag == 3)
                    {
                        LDAPMessage message3 = new LDAPMessage();
                        message3.MessageID = message.MessageID;
                        LDAPSearchRequest searchRequest = new LDAPSearchRequest();
                        searchRequest.ReadBytes((byte[][])message.ProtocolOp);

                        LDAPSearchResDone resdone = new LDAPSearchResDone();
                        resdone.ResultCode = 0;
                        LDAPSearchResEntry search = new LDAPSearchResEntry();

                        if (String.Equals(searchRequest.Attributes[0], "supportedCapabilities"))
                        {
                            LDAPSupportedCapabilities cap = new LDAPSupportedCapabilities();
                            search.Attributes = cap.Encode();
                        }
                        else if (String.Equals(searchRequest.Attributes[0], "supportedSASLMechanisms"))
                        {
                            LDAPSupportedSASLMechanisms mech = new LDAPSupportedSASLMechanisms();
                            search.Attributes = mech.Encode();
                        }

                        message2.ProtocolOp = search;
                        message3.ProtocolOp = resdone;
                        buffer = Utilities.BlockCopy(message2.Encode(4), message3.Encode(5));
                    }
                    else if (message.Tag == 0)
                    {
                        LDAPBindRequest bind = new LDAPBindRequest();
                        bind.ReadBytes((byte[][])message.ProtocolOp);
                        LDAPSaslCredentials sasl = new LDAPSaslCredentials();
                        sasl.ReadBytes(bind.Authentication);
                        NTLMNegotiate ntlm = new NTLMNegotiate();
                        ntlm.ReadBytes(sasl.Credentials, 0);

                        if (ntlm.MessageType == 1)
                        {
                            NTLMChallenge challenge     = new NTLMChallenge(Challenge, NetbiosDomain, ComputerName, DNSDomain, ComputerName, DNSDomain);
                            byte[]        challengeData = challenge.GetBytes(ComputerName);
                            ntlmChallenge = BitConverter.ToString(challenge.ServerChallenge).Replace("-", "");

                            LDAPBindResponse bindResponse = new LDAPBindResponse
                            {
                                ServerSaslCreds = challengeData
                            };

                            LDAPMessage bindMessage = new LDAPMessage
                            {
                                MessageID  = message.MessageID,
                                ProtocolOp = bindResponse
                            };

                            buffer = bindMessage.Encode(3);
                            OutputChallenge(listenerPort, clientIP, clientPort, ntlmChallenge);
                        }
                        else if (ntlm.MessageType == 3)
                        {
                            NTLMResponse ntlmResponse = new NTLMResponse(sasl.Credentials, false);
                            string       domain       = Encoding.Unicode.GetString(ntlmResponse.DomainName);
                            string       user         = Encoding.Unicode.GetString(ntlmResponse.UserName);
                            string       host         = Encoding.Unicode.GetString(ntlmResponse.Workstation);
                            string       response2    = BitConverter.ToString(ntlmResponse.NtChallengeResponse).Replace("-", "");
                            string       lmResponse   = BitConverter.ToString(ntlmResponse.LmChallengeResponse).Replace("-", "");
                            OutputNTLM("LDAP", listenerPort, clientIP, clientPort, user, domain, host, ntlmChallenge, response2, lmResponse);
                        }
                    }

                    tcpStream.Write(buffer, 0, buffer.Length);
                    tcpStream.Flush();
                }
            }
            catch (Exception ex)
            {
                OutputError(ex, port);
            }
        }