Ejemplo n.º 1
0
        private DirectoryResponse ConstructElement(XmlElement node)
        {
            DirectoryResponse el = null;

            Debug.Assert(node != null);

            switch (node.LocalName)
            {
            case DsmlConstants.DsmlErrorResponse:
                el = new DsmlErrorResponse(node);
                break;

            case DsmlConstants.DsmlSearchResponse:
                el = new SearchResponse(node);
                break;

            case DsmlConstants.DsmlModifyResponse:
                el = new ModifyResponse(node);
                break;

            case DsmlConstants.DsmlAddResponse:
                el = new AddResponse(node);
                break;

            case DsmlConstants.DsmlDelResponse:
                el = new DeleteResponse(node);
                break;

            case DsmlConstants.DsmlModDNResponse:
                el = new ModifyDNResponse(node);
                break;

            case DsmlConstants.DsmlCompareResponse:
                el = new CompareResponse(node);
                break;

            case DsmlConstants.DsmlExtendedResponse:
                el = new ExtendedResponse(node);
                break;

            case DsmlConstants.DsmlAuthResponse:
                el = new DsmlAuthResponse(node);
                break;

            default:
                throw new DsmlInvalidDocumentException(Res.GetString(Res.UnknownResponseElement));
            }

            Debug.Assert(el != null);

            return(el);
        }
Ejemplo n.º 2
0
        private DirectoryResponse ConstructElement(XmlElement node)
        {
            DirectoryResponse dsmlErrorResponse = null;
            string            localName         = node.LocalName;
            string            str = localName;

            if (localName != null)
            {
                if (str == "errorResponse")
                {
                    dsmlErrorResponse = new DsmlErrorResponse(node);
                }
                else if (str == "searchResponse")
                {
                    dsmlErrorResponse = new SearchResponse(node);
                }
                else if (str == "modifyResponse")
                {
                    dsmlErrorResponse = new ModifyResponse(node);
                }
                else if (str == "addResponse")
                {
                    dsmlErrorResponse = new AddResponse(node);
                }
                else if (str == "delResponse")
                {
                    dsmlErrorResponse = new DeleteResponse(node);
                }
                else if (str == "modDNResponse")
                {
                    dsmlErrorResponse = new ModifyDNResponse(node);
                }
                else if (str == "compareResponse")
                {
                    dsmlErrorResponse = new CompareResponse(node);
                }
                else if (str == "extendedResponse")
                {
                    dsmlErrorResponse = new ExtendedResponse(node);
                }
                else if (str == "authResponse")
                {
                    dsmlErrorResponse = new DsmlAuthResponse(node);
                }
                else
                {
                    throw new DsmlInvalidDocumentException(Res.GetString("UnknownResponseElement"));
                }
                return(dsmlErrorResponse);
            }
            throw new DsmlInvalidDocumentException(Res.GetString("UnknownResponseElement"));
        }
Ejemplo n.º 3
0
        private DirectoryException ConstructException(int error, LdapOperation operation)
        {
            DirectoryResponse response = null;

            if (Utility.IsResultCode((ResultCode)error))
            {
                if (operation == LdapOperation.LdapAdd)
                    response = new AddResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
                else if (operation == LdapOperation.LdapModify)
                    response = new ModifyResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
                else if (operation == LdapOperation.LdapDelete)
                    response = new DeleteResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
                else if (operation == LdapOperation.LdapModifyDn)
                    response = new ModifyDNResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
                else if (operation == LdapOperation.LdapCompare)
                    response = new CompareResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
                else if (operation == LdapOperation.LdapSearch)
                    response = new SearchResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
                else if (operation == LdapOperation.LdapExtendedRequest)
                    response = new ExtendedResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);

                string errorMessage = OperationErrorMappings.MapResultCode(error);
                return new DirectoryOperationException(response, errorMessage);
            }
            else
            {
                if (Utility.IsLdapError((LdapError)error))
                {
                    string errorMessage = LdapErrorMappings.MapResultCode(error);
                    string serverErrorMessage = _options.ServerErrorMessage;
                    if ((serverErrorMessage != null) && (serverErrorMessage.Length > 0))
                    {
                        throw new LdapException(error, errorMessage, serverErrorMessage);
                    }
                    else
                    {
                        return new LdapException(error, errorMessage);
                    }
                }
                else
                    return new LdapException(error);
            }
        }
Ejemplo n.º 4
0
        internal DirectoryResponse ConstructResponse(int messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, bool exceptionOnTimeOut)
        {
            int error;
            LDAP_TIMEVAL timeout = new LDAP_TIMEVAL();
            timeout.tv_sec = (int)(requestTimeOut.Ticks / TimeSpan.TicksPerSecond);
            IntPtr ldapResult = (IntPtr)0;
            DirectoryResponse response = null;

            IntPtr requestName = (IntPtr)0;
            IntPtr requestValue = (IntPtr)0;

            IntPtr entryMessage = (IntPtr)0;

            bool needAbandon = true;

            // processing for the partial results retrieval
            if (resultType != ResultAll.LDAP_MSG_ALL)
            {
                // we need to have 0 timeout as we are polling for the results and don't want to wait
                timeout.tv_sec = 0;
                timeout.tv_usec = 0;

                if (resultType == ResultAll.LDAP_MSG_POLLINGALL)
                    resultType = ResultAll.LDAP_MSG_ALL;

                // when doing partial results retrieving, if ldap_result failed, we don't do ldap_abandon here.
                needAbandon = false;
            }

            error = Wldap32.ldap_result(ldapHandle, messageId, (int)resultType, timeout, ref ldapResult);
            if (error != -1 && error != 0)
            {
                // parsing the result
                int serverError = 0;
                try
                {
                    int resulterror = 0;
                    string responseDn = null;
                    string responseMessage = null;
                    Uri[] responseReferral = null;
                    DirectoryControl[] responseControl = null;

                    // ldap_parse_result skips over messages of type LDAP_RES_SEARCH_ENTRY and LDAP_RES_SEARCH_REFERRAL
                    if (error != (int)LdapResult.LDAP_RES_SEARCH_ENTRY && error != (int)LdapResult.LDAP_RES_REFERRAL)
                        resulterror = ConstructParsedResult(ldapResult, ref serverError, ref responseDn, ref responseMessage, ref responseReferral, ref responseControl);

                    if (resulterror == 0)
                    {
                        resulterror = serverError;

                        if (error == (int)LdapResult.LDAP_RES_ADD)
                            response = new AddResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_MODIFY)
                            response = new ModifyResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_DELETE)
                            response = new DeleteResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_MODRDN)
                            response = new ModifyDNResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_COMPARE)
                            response = new CompareResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                        else if (error == (int)LdapResult.LDAP_RES_EXTENDED)
                        {
                            response = new ExtendedResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);
                            if (resulterror == (int)ResultCode.Success)
                            {
                                resulterror = Wldap32.ldap_parse_extended_result(ldapHandle, ldapResult, ref requestName, ref requestValue, 0 /*not free it*/);
                                if (resulterror == 0)
                                {
                                    string name = null;
                                    if (requestName != (IntPtr)0)
                                    {
                                        name = Marshal.PtrToStringUni(requestName);
                                    }

                                    berval val = null;
                                    byte[] requestValueArray = null;
                                    if (requestValue != (IntPtr)0)
                                    {
                                        val = new berval();
                                        Marshal.PtrToStructure(requestValue, val);
                                        if (val.bv_len != 0 && val.bv_val != (IntPtr)0)
                                        {
                                            requestValueArray = new byte[val.bv_len];
                                            Marshal.Copy(val.bv_val, requestValueArray, 0, val.bv_len);
                                        }
                                    }

                                    ((ExtendedResponse)response).name = name;
                                    ((ExtendedResponse)response).value = requestValueArray;
                                }
                            }
                        }
                        else if (error == (int)LdapResult.LDAP_RES_SEARCH_RESULT ||
                               error == (int)LdapResult.LDAP_RES_SEARCH_ENTRY ||
                               error == (int)LdapResult.LDAP_RES_REFERRAL)
                        {
                            response = new SearchResponse(responseDn, responseControl, (ResultCode)resulterror, responseMessage, responseReferral);

                            //set the flag here so our partial result processor knows whether the search is done or not
                            if (error == (int)LdapResult.LDAP_RES_SEARCH_RESULT)
                            {
                                ((SearchResponse)response).searchDone = true;
                            }

                            SearchResultEntryCollection searchResultEntries = new SearchResultEntryCollection();
                            SearchResultReferenceCollection searchResultReferences = new SearchResultReferenceCollection();

                            // parsing the resultentry
                            entryMessage = Wldap32.ldap_first_entry(ldapHandle, ldapResult);

                            int entrycount = 0;
                            while (entryMessage != (IntPtr)0)
                            {
                                SearchResultEntry entry = ConstructEntry(entryMessage);
                                if (entry != null)
                                    searchResultEntries.Add(entry);

                                entrycount++;
                                entryMessage = Wldap32.ldap_next_entry(ldapHandle, entryMessage);
                            }

                            // parsing the reference
                            IntPtr referenceMessage = Wldap32.ldap_first_reference(ldapHandle, ldapResult);

                            while (referenceMessage != (IntPtr)0)
                            {
                                SearchResultReference reference = ConstructReference(referenceMessage);
                                if (reference != null)
                                    searchResultReferences.Add(reference);

                                referenceMessage = Wldap32.ldap_next_reference(ldapHandle, referenceMessage);
                            }

                            ((SearchResponse)response).SetEntries(searchResultEntries);
                            ((SearchResponse)response).SetReferences(searchResultReferences);
                        }

                        if (resulterror != (int)ResultCode.Success && resulterror != (int)ResultCode.CompareFalse && resulterror != (int)ResultCode.CompareTrue && resulterror != (int)ResultCode.Referral && resulterror != (int)ResultCode.ReferralV2)
                        {
                            // throw operation exception                   
                            if (Utility.IsResultCode((ResultCode)resulterror))
                            {
                                throw new DirectoryOperationException(response, OperationErrorMappings.MapResultCode(resulterror));
                            }
                            else
                                // should not occur
                                throw new DirectoryOperationException(response);
                        }

                        return response;
                    }
                    else
                    {
                        // fall over, throw the exception beow
                        error = resulterror;
                    }
                }
                finally
                {
                    if (requestName != (IntPtr)0)
                        Wldap32.ldap_memfree(requestName);

                    if (requestValue != (IntPtr)0)
                        Wldap32.ldap_memfree(requestValue);

                    if (ldapResult != (IntPtr)0)
                    {
                        Wldap32.ldap_msgfree(ldapResult);
                    }
                }
            }
            else
            {
                // ldap_result failed
                if (error == 0)
                {
                    if (exceptionOnTimeOut)
                    {
                        // client side timeout                        
                        error = (int)LdapError.TimeOut;
                    }
                    else
                    {
                        // if we don't throw exception on time out (notification search for example), we just return empty resposne
                        return null;
                    }
                }
                else
                {
                    error = Wldap32.LdapGetLastError();
                }

                // abandon the request
                if (needAbandon)
                    Wldap32.ldap_abandon(ldapHandle, messageId);
            }

            // throw proper exception here            
            throw ConstructException(error, operation);
        }
        public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls)
        {
            IntPtr zero = IntPtr.Zero;

            LdapControl[] controlArray   = null;
            IntPtr        clientControls = IntPtr.Zero;

            LdapControl[] controlArray2     = null;
            IntPtr        message           = IntPtr.Zero;
            IntPtr        referral          = IntPtr.Zero;
            int           serverReturnValue = 0;

            Uri[] uriArray = null;
            if (Utility.IsWin2kOS)
            {
                throw new PlatformNotSupportedException(Res.GetString("TLSNotSupported"));
            }
            if (this.connection.disposed)
            {
                throw new ObjectDisposedException(base.GetType().Name);
            }
            try
            {
                IntPtr ptr  = IntPtr.Zero;
                IntPtr ptr6 = IntPtr.Zero;
                controlArray = this.connection.BuildControlArray(controls, true);
                int cb = Marshal.SizeOf(typeof(LdapControl));
                if (controlArray != null)
                {
                    zero = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(IntPtr)) * (controlArray.Length + 1)));
                    for (int i = 0; i < controlArray.Length; i++)
                    {
                        ptr = Marshal.AllocHGlobal(cb);
                        Marshal.StructureToPtr(controlArray[i], ptr, false);
                        ptr6 = (IntPtr)(((long)zero) + (Marshal.SizeOf(typeof(IntPtr)) * i));
                        Marshal.WriteIntPtr(ptr6, ptr);
                    }
                    ptr6 = (IntPtr)(((long)zero) + (Marshal.SizeOf(typeof(IntPtr)) * controlArray.Length));
                    Marshal.WriteIntPtr(ptr6, IntPtr.Zero);
                }
                controlArray2 = this.connection.BuildControlArray(controls, false);
                if (controlArray2 != null)
                {
                    clientControls = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(IntPtr)) * (controlArray2.Length + 1)));
                    for (int j = 0; j < controlArray2.Length; j++)
                    {
                        ptr = Marshal.AllocHGlobal(cb);
                        Marshal.StructureToPtr(controlArray2[j], ptr, false);
                        ptr6 = (IntPtr)(((long)clientControls) + (Marshal.SizeOf(typeof(IntPtr)) * j));
                        Marshal.WriteIntPtr(ptr6, ptr);
                    }
                    ptr6 = (IntPtr)(((long)clientControls) + (Marshal.SizeOf(typeof(IntPtr)) * controlArray2.Length));
                    Marshal.WriteIntPtr(ptr6, IntPtr.Zero);
                }
                int errorCode = Wldap32.ldap_start_tls(this.connection.ldapHandle, ref serverReturnValue, ref message, zero, clientControls);
                if (((message != IntPtr.Zero) && (Wldap32.ldap_parse_result_referral(this.connection.ldapHandle, message, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref referral, IntPtr.Zero, 0) == 0)) && (referral != IntPtr.Zero))
                {
                    char **   chPtr  = (char **)referral;
                    char *    chPtr2 = chPtr[0];
                    int       index  = 0;
                    ArrayList list   = new ArrayList();
                    while (chPtr2 != null)
                    {
                        string str = Marshal.PtrToStringUni((IntPtr)chPtr2);
                        list.Add(str);
                        index++;
                        chPtr2 = chPtr[index];
                    }
                    if (referral != IntPtr.Zero)
                    {
                        Wldap32.ldap_value_free(referral);
                        referral = IntPtr.Zero;
                    }
                    if (list.Count > 0)
                    {
                        uriArray = new Uri[list.Count];
                        for (int k = 0; k < list.Count; k++)
                        {
                            uriArray[k] = new Uri((string)list[k]);
                        }
                    }
                }
                if (errorCode != 0)
                {
                    string str2 = Res.GetString("DefaultLdapError");
                    if (Utility.IsResultCode((ResultCode)errorCode))
                    {
                        if (errorCode == 80)
                        {
                            errorCode = serverReturnValue;
                        }
                        str2 = OperationErrorMappings.MapResultCode(errorCode);
                        ExtendedResponse response = new ExtendedResponse(null, null, (ResultCode)errorCode, str2, uriArray)
                        {
                            name = "1.3.6.1.4.1.1466.20037"
                        };
                        throw new TlsOperationException(response);
                    }
                    if (Utility.IsLdapError((LdapError)errorCode))
                    {
                        str2 = LdapErrorMappings.MapResultCode(errorCode);
                        throw new LdapException(errorCode, str2);
                    }
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    for (int m = 0; m < controlArray.Length; m++)
                    {
                        IntPtr hglobal = Marshal.ReadIntPtr(zero, Marshal.SizeOf(typeof(IntPtr)) * m);
                        if (hglobal != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(hglobal);
                        }
                    }
                    Marshal.FreeHGlobal(zero);
                }
                if (controlArray != null)
                {
                    for (int n = 0; n < controlArray.Length; n++)
                    {
                        if (controlArray[n].ldctl_oid != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(controlArray[n].ldctl_oid);
                        }
                        if ((controlArray[n].ldctl_value != null) && (controlArray[n].ldctl_value.bv_val != IntPtr.Zero))
                        {
                            Marshal.FreeHGlobal(controlArray[n].ldctl_value.bv_val);
                        }
                    }
                }
                if (clientControls != IntPtr.Zero)
                {
                    for (int num11 = 0; num11 < controlArray2.Length; num11++)
                    {
                        IntPtr ptr8 = Marshal.ReadIntPtr(clientControls, Marshal.SizeOf(typeof(IntPtr)) * num11);
                        if (ptr8 != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(ptr8);
                        }
                    }
                    Marshal.FreeHGlobal(clientControls);
                }
                if (controlArray2 != null)
                {
                    for (int num12 = 0; num12 < controlArray2.Length; num12++)
                    {
                        if (controlArray2[num12].ldctl_oid != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(controlArray2[num12].ldctl_oid);
                        }
                        if ((controlArray2[num12].ldctl_value != null) && (controlArray2[num12].ldctl_value.bv_val != IntPtr.Zero))
                        {
                            Marshal.FreeHGlobal(controlArray2[num12].ldctl_value.bv_val);
                        }
                    }
                }
                if (referral != IntPtr.Zero)
                {
                    Wldap32.ldap_value_free(referral);
                }
            }
        }
Ejemplo n.º 6
0
        public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls)
        {
            IntPtr serverControlArray = (IntPtr)0;

            LdapControl[] managedServerControls = null;
            IntPtr        clientControlArray    = (IntPtr)0;

            LdapControl[] managedClientControls = null;
            IntPtr        ldapResult            = (IntPtr)0;
            IntPtr        referral = (IntPtr)0;

            int serverError = 0;

            Uri[] responseReferral = null;

            if (_connection.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            try
            {
                IntPtr controlPtr = (IntPtr)0;
                IntPtr tempPtr    = (IntPtr)0;

                // build server control
                managedServerControls = _connection.BuildControlArray(controls, true);
                int structSize = Marshal.SizeOf(typeof(LdapControl));
                if (managedServerControls != null)
                {
                    serverControlArray = Utility.AllocHGlobalIntPtrArray(managedServerControls.Length + 1);
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        controlPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedServerControls[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)serverControlArray + IntPtr.Size * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }

                    tempPtr = (IntPtr)((long)serverControlArray + IntPtr.Size * managedServerControls.Length);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                }

                // build client control
                managedClientControls = _connection.BuildControlArray(controls, false);
                if (managedClientControls != null)
                {
                    clientControlArray = Utility.AllocHGlobalIntPtrArray(managedClientControls.Length + 1);
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        controlPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedClientControls[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)clientControlArray + IntPtr.Size * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }
                    tempPtr = (IntPtr)((long)clientControlArray + IntPtr.Size * managedClientControls.Length);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                }

                int error = Wldap32.ldap_start_tls(_connection.ldapHandle, ref serverError, ref ldapResult, serverControlArray, clientControlArray);
                if (ldapResult != (IntPtr)0)
                {
                    // parsing the referral
                    int resulterror = Wldap32.ldap_parse_result_referral(_connection.ldapHandle, ldapResult, (IntPtr)0, (IntPtr)0, (IntPtr)0, ref referral, (IntPtr)0, 0 /* not free it */);
                    if (resulterror == 0)
                    {
                        // parsing referral
                        if (referral != (IntPtr)0)
                        {
                            char **   referralPtr    = (char **)referral;
                            char *    singleReferral = referralPtr[0];
                            int       i            = 0;
                            ArrayList referralList = new ArrayList();
                            while (singleReferral != null)
                            {
                                string s = Marshal.PtrToStringUni((IntPtr)singleReferral);
                                referralList.Add(s);

                                i++;
                                singleReferral = referralPtr[i];
                            }

                            // free heap memory
                            if (referral != (IntPtr)0)
                            {
                                Wldap32.ldap_value_free(referral);
                                referral = (IntPtr)0;
                            }

                            if (referralList.Count > 0)
                            {
                                responseReferral = new Uri[referralList.Count];
                                for (int j = 0; j < referralList.Count; j++)
                                {
                                    responseReferral[j] = new Uri((string)referralList[j]);
                                }
                            }
                        }
                    }
                }

                if (error != (int)ResultCode.Success)
                {
                    string errorMessage = String.Format(CultureInfo.CurrentCulture, SR.DefaultLdapError);
                    if (Utility.IsResultCode((ResultCode)error))
                    {
                        //If the server failed request for whatever reason, the ldap_start_tls returns LDAP_OTHER
                        // and the ServerReturnValue will contain the error code from the server.
                        if (error == (int)ResultCode.Other)
                        {
                            error = serverError;
                        }

                        errorMessage = OperationErrorMappings.MapResultCode(error);
                        ExtendedResponse response = new ExtendedResponse(null, null, (ResultCode)error, errorMessage, responseReferral);
                        response.ResponseName = "1.3.6.1.4.1.1466.20037";
                        throw new TlsOperationException(response);
                    }
                    else if (Utility.IsLdapError((LdapError)error))
                    {
                        errorMessage = LdapErrorMappings.MapResultCode(error);
                        throw new LdapException(error, errorMessage);
                    }
                }
            }
            finally
            {
                if (serverControlArray != (IntPtr)0)
                {
                    //release the memory from the heap
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(serverControlArray, IntPtr.Size * i);
                        if (tempPtr != (IntPtr)0)
                        {
                            Marshal.FreeHGlobal(tempPtr);
                        }
                    }
                    Marshal.FreeHGlobal(serverControlArray);
                }

                if (managedServerControls != null)
                {
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        if (managedServerControls[i].ldctl_oid != (IntPtr)0)
                        {
                            Marshal.FreeHGlobal(managedServerControls[i].ldctl_oid);
                        }

                        if (managedServerControls[i].ldctl_value != null)
                        {
                            if (managedServerControls[i].ldctl_value.bv_val != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(managedServerControls[i].ldctl_value.bv_val);
                            }
                        }
                    }
                }

                if (clientControlArray != (IntPtr)0)
                {
                    // release the memor from the heap
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(clientControlArray, IntPtr.Size * i);
                        if (tempPtr != (IntPtr)0)
                        {
                            Marshal.FreeHGlobal(tempPtr);
                        }
                    }
                    Marshal.FreeHGlobal(clientControlArray);
                }

                if (managedClientControls != null)
                {
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        if (managedClientControls[i].ldctl_oid != (IntPtr)0)
                        {
                            Marshal.FreeHGlobal(managedClientControls[i].ldctl_oid);
                        }

                        if (managedClientControls[i].ldctl_value != null)
                        {
                            if (managedClientControls[i].ldctl_value.bv_val != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(managedClientControls[i].ldctl_value.bv_val);
                            }
                        }
                    }
                }

                if (referral != (IntPtr)0)
                {
                    Wldap32.ldap_value_free(referral);
                }
            }
        }
Ejemplo n.º 7
0
        public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls)
        {
            IntPtr serverControlArray = (IntPtr)0;
            LdapControl[] managedServerControls = null;
            IntPtr clientControlArray = (IntPtr)0;
            LdapControl[] managedClientControls = null;
            IntPtr ldapResult = (IntPtr)0;
            IntPtr referral = (IntPtr)0;

            int serverError = 0;
            Uri[] responseReferral = null;

            if (Utility.IsWin2kOS)
                throw new PlatformNotSupportedException(Res.GetString(Res.TLSNotSupported));

            if (_connection.disposed)
                throw new ObjectDisposedException(GetType().Name);

            try
            {
                IntPtr controlPtr = (IntPtr)0;
                IntPtr tempPtr = (IntPtr)0;

                // build server control
                managedServerControls = _connection.BuildControlArray(controls, true);
                int structSize = Marshal.SizeOf(typeof(LdapControl));
                if (managedServerControls != null)
                {
                    serverControlArray = Utility.AllocHGlobalIntPtrArray(managedServerControls.Length + 1);
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        controlPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedServerControls[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)serverControlArray + Marshal.SizeOf(typeof(IntPtr)) * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }

                    tempPtr = (IntPtr)((long)serverControlArray + Marshal.SizeOf(typeof(IntPtr)) * managedServerControls.Length);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                }

                // build client control
                managedClientControls = _connection.BuildControlArray(controls, false);
                if (managedClientControls != null)
                {
                    clientControlArray = Utility.AllocHGlobalIntPtrArray(managedClientControls.Length + 1);
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        controlPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedClientControls[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)clientControlArray + Marshal.SizeOf(typeof(IntPtr)) * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }
                    tempPtr = (IntPtr)((long)clientControlArray + Marshal.SizeOf(typeof(IntPtr)) * managedClientControls.Length);
                    Marshal.WriteIntPtr(tempPtr, (IntPtr)0);
                }

                int error = Wldap32.ldap_start_tls(_connection.ldapHandle, ref serverError, ref ldapResult, serverControlArray, clientControlArray);
                if (ldapResult != (IntPtr)0)
                {
                    // parsing the referral                          
                    int resulterror = Wldap32.ldap_parse_result_referral(_connection.ldapHandle, ldapResult, (IntPtr)0, (IntPtr)0, (IntPtr)0, ref referral, (IntPtr)0, 0 /* not free it */);
                    if (resulterror == 0)
                    {
                        // parsing referral                        
                        if (referral != (IntPtr)0)
                        {
                            char** referralPtr = (char**)referral;
                            char* singleReferral = referralPtr[0];
                            int i = 0;
                            ArrayList referralList = new ArrayList();
                            while (singleReferral != null)
                            {
                                string s = Marshal.PtrToStringUni((IntPtr)singleReferral);
                                referralList.Add(s);

                                i++;
                                singleReferral = referralPtr[i];
                            }

                            // free heap memory
                            if (referral != (IntPtr)0)
                            {
                                Wldap32.ldap_value_free(referral);
                                referral = (IntPtr)0;
                            }

                            if (referralList.Count > 0)
                            {
                                responseReferral = new Uri[referralList.Count];
                                for (int j = 0; j < referralList.Count; j++)
                                    responseReferral[j] = new Uri((string)referralList[j]);
                            }
                        }
                    }
                }

                if (error != (int)ResultCode.Success)
                {
                    string errorMessage = Res.GetString(Res.DefaultLdapError);
                    if (Utility.IsResultCode((ResultCode)error))
                    {
                        //If the server failed request for whatever reason, the ldap_start_tls returns LDAP_OTHER
                        // and the ServerReturnValue will contain the error code from the server.   
                        if (error == (int)ResultCode.Other)
                            error = serverError;

                        errorMessage = OperationErrorMappings.MapResultCode(error);
                        ExtendedResponse response = new ExtendedResponse(null, null, (ResultCode)error, errorMessage, responseReferral);
                        response.name = "1.3.6.1.4.1.1466.20037";
                        throw new TlsOperationException(response);
                    }
                    else if (Utility.IsLdapError((LdapError)error))
                    {
                        errorMessage = LdapErrorMappings.MapResultCode(error);
                        throw new LdapException(error, errorMessage);
                    }
                }
            }
            finally
            {
                if (serverControlArray != (IntPtr)0)
                {
                    //release the memory from the heap
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(serverControlArray, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                            Marshal.FreeHGlobal(tempPtr);
                    }
                    Marshal.FreeHGlobal(serverControlArray);
                }

                if (managedServerControls != null)
                {
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        if (managedServerControls[i].ldctl_oid != (IntPtr)0)
                            Marshal.FreeHGlobal(managedServerControls[i].ldctl_oid);

                        if (managedServerControls[i].ldctl_value != null)
                        {
                            if (managedServerControls[i].ldctl_value.bv_val != (IntPtr)0)
                                Marshal.FreeHGlobal(managedServerControls[i].ldctl_value.bv_val);
                        }
                    }
                }

                if (clientControlArray != (IntPtr)0)
                {
                    // release the memor from the heap
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(clientControlArray, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                            Marshal.FreeHGlobal(tempPtr);
                    }
                    Marshal.FreeHGlobal(clientControlArray);
                }

                if (managedClientControls != null)
                {
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        if (managedClientControls[i].ldctl_oid != (IntPtr)0)
                            Marshal.FreeHGlobal(managedClientControls[i].ldctl_oid);

                        if (managedClientControls[i].ldctl_value != null)
                        {
                            if (managedClientControls[i].ldctl_value.bv_val != (IntPtr)0)
                                Marshal.FreeHGlobal(managedClientControls[i].ldctl_value.bv_val);
                        }
                    }
                }

                if (referral != (IntPtr)0)
                    Wldap32.ldap_value_free(referral);
            }
        }
Ejemplo n.º 8
0
		public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls)
		{
			IntPtr intPtr;
			IntPtr intPtr1;
			IntPtr intPtr2 = (IntPtr)0;
			LdapControl[] ldapControlArray = null;
			IntPtr intPtr3 = (IntPtr)0;
			LdapControl[] ldapControlArray1 = null;
			IntPtr intPtr4 = (IntPtr)0;
			IntPtr intPtr5 = (IntPtr)0;
			int num = 0;
			Uri[] uri = null;
			if (!Utility.IsWin2kOS)
			{
				if (!this.connection.disposed)
				{
					try
					{
						ldapControlArray = this.connection.BuildControlArray(controls, true);
						int num1 = Marshal.SizeOf(typeof(LdapControl));
						if (ldapControlArray != null)
						{
							intPtr2 = Utility.AllocHGlobalIntPtrArray((int)ldapControlArray.Length + 1);
							for (int i = 0; i < (int)ldapControlArray.Length; i++)
							{
								intPtr = Marshal.AllocHGlobal(num1);
								Marshal.StructureToPtr(ldapControlArray[i], intPtr, false);
								intPtr1 = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
								Marshal.WriteIntPtr(intPtr1, intPtr);
							}
							intPtr1 = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * (int)ldapControlArray.Length));
							Marshal.WriteIntPtr(intPtr1, (IntPtr)0);
						}
						ldapControlArray1 = this.connection.BuildControlArray(controls, false);
						if (ldapControlArray1 != null)
						{
							intPtr3 = Utility.AllocHGlobalIntPtrArray((int)ldapControlArray1.Length + 1);
							for (int j = 0; j < (int)ldapControlArray1.Length; j++)
							{
								intPtr = Marshal.AllocHGlobal(num1);
								Marshal.StructureToPtr(ldapControlArray1[j], intPtr, false);
								intPtr1 = (IntPtr)((long)intPtr3 + (long)(Marshal.SizeOf(typeof(IntPtr)) * j));
								Marshal.WriteIntPtr(intPtr1, intPtr);
							}
							intPtr1 = (IntPtr)((long)intPtr3 + (long)(Marshal.SizeOf(typeof(IntPtr)) * (int)ldapControlArray1.Length));
							Marshal.WriteIntPtr(intPtr1, (IntPtr)0);
						}
						int num2 = Wldap32.ldap_start_tls(this.connection.ldapHandle, ref num, ref intPtr4, intPtr2, intPtr3);
						if (intPtr4 != (IntPtr)0)
						{
							int num3 = Wldap32.ldap_parse_result_referral(this.connection.ldapHandle, intPtr4, (IntPtr)0, (IntPtr)0, (IntPtr)0, ref intPtr5, (IntPtr)0, 0);
							if (num3 == 0 && intPtr5 != (IntPtr)0)
							{
								char** chrPointer = (char**)((void*)intPtr5);
								char* chrPointer1 = (char*)((void*)(*(chrPointer)));
								int num4 = 0;
								ArrayList arrayLists = new ArrayList();
								while (chrPointer1 != null)
								{
									string stringUni = Marshal.PtrToStringUni((IntPtr)chrPointer1);
									arrayLists.Add(stringUni);
									num4++;
									chrPointer1 = (char*)((void*)(*(chrPointer + num4 * sizeof(char*))));
								}
								if (intPtr5 != (IntPtr)0)
								{
									Wldap32.ldap_value_free(intPtr5);
									intPtr5 = (IntPtr)0;
								}
								if (arrayLists.Count > 0)
								{
									uri = new Uri[arrayLists.Count];
									for (int k = 0; k < arrayLists.Count; k++)
									{
										uri[k] = new Uri((string)arrayLists[k]);
									}
								}
							}
						}
						if (num2 != 0)
						{
							string str = Res.GetString("DefaultLdapError");
							if (!Utility.IsResultCode((ResultCode)num2))
							{
								if (Utility.IsLdapError((LdapError)num2))
								{
									str = LdapErrorMappings.MapResultCode(num2);
									throw new LdapException(num2, str);
								}
							}
							else
							{
								if (num2 == 80)
								{
									num2 = num;
								}
								str = OperationErrorMappings.MapResultCode(num2);
								ExtendedResponse extendedResponse = new ExtendedResponse(null, null, (ResultCode)num2, str, uri);
								extendedResponse.name = "1.3.6.1.4.1.1466.20037";
								throw new TlsOperationException(extendedResponse);
							}
						}
					}
					finally
					{
						if (intPtr2 != (IntPtr)0)
						{
							for (int l = 0; l < (int)ldapControlArray.Length; l++)
							{
								IntPtr intPtr6 = Marshal.ReadIntPtr(intPtr2, Marshal.SizeOf(typeof(IntPtr)) * l);
								if (intPtr6 != (IntPtr)0)
								{
									Marshal.FreeHGlobal(intPtr6);
								}
							}
							Marshal.FreeHGlobal(intPtr2);
						}
						if (ldapControlArray != null)
						{
							for (int m = 0; m < (int)ldapControlArray.Length; m++)
							{
								if (ldapControlArray[m].ldctl_oid != (IntPtr)0)
								{
									Marshal.FreeHGlobal(ldapControlArray[m].ldctl_oid);
								}
								if (ldapControlArray[m].ldctl_value != null && ldapControlArray[m].ldctl_value.bv_val != (IntPtr)0)
								{
									Marshal.FreeHGlobal(ldapControlArray[m].ldctl_value.bv_val);
								}
							}
						}
						if (intPtr3 != (IntPtr)0)
						{
							for (int n = 0; n < (int)ldapControlArray1.Length; n++)
							{
								IntPtr intPtr7 = Marshal.ReadIntPtr(intPtr3, Marshal.SizeOf(typeof(IntPtr)) * n);
								if (intPtr7 != (IntPtr)0)
								{
									Marshal.FreeHGlobal(intPtr7);
								}
							}
							Marshal.FreeHGlobal(intPtr3);
						}
						if (ldapControlArray1 != null)
						{
							for (int o = 0; o < (int)ldapControlArray1.Length; o++)
							{
								if (ldapControlArray1[o].ldctl_oid != (IntPtr)0)
								{
									Marshal.FreeHGlobal(ldapControlArray1[o].ldctl_oid);
								}
								if (ldapControlArray1[o].ldctl_value != null && ldapControlArray1[o].ldctl_value.bv_val != (IntPtr)0)
								{
									Marshal.FreeHGlobal(ldapControlArray1[o].ldctl_value.bv_val);
								}
							}
						}
						if (intPtr5 != (IntPtr)0)
						{
							Wldap32.ldap_value_free(intPtr5);
						}
					}
					return;
				}
				else
				{
					throw new ObjectDisposedException(this.GetType().Name);
				}
			}
			else
			{
				throw new PlatformNotSupportedException(Res.GetString("TLSNotSupported"));
			}
		}
Ejemplo n.º 9
0
        public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls)
        {
            IntPtr intPtr;
            IntPtr intPtr1;
            IntPtr intPtr2 = (IntPtr)0;

            LdapControl[] ldapControlArray = null;
            IntPtr        intPtr3          = (IntPtr)0;

            LdapControl[] ldapControlArray1 = null;
            IntPtr        intPtr4           = (IntPtr)0;
            IntPtr        intPtr5           = (IntPtr)0;
            int           num = 0;

            Uri[] uri = null;
            if (!Utility.IsWin2kOS)
            {
                if (!this.connection.disposed)
                {
                    try
                    {
                        ldapControlArray = this.connection.BuildControlArray(controls, true);
                        int num1 = Marshal.SizeOf(typeof(LdapControl));
                        if (ldapControlArray != null)
                        {
                            intPtr2 = Utility.AllocHGlobalIntPtrArray((int)ldapControlArray.Length + 1);
                            for (int i = 0; i < (int)ldapControlArray.Length; i++)
                            {
                                intPtr = Marshal.AllocHGlobal(num1);
                                Marshal.StructureToPtr(ldapControlArray[i], intPtr, false);
                                intPtr1 = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                                Marshal.WriteIntPtr(intPtr1, intPtr);
                            }
                            intPtr1 = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * (int)ldapControlArray.Length));
                            Marshal.WriteIntPtr(intPtr1, (IntPtr)0);
                        }
                        ldapControlArray1 = this.connection.BuildControlArray(controls, false);
                        if (ldapControlArray1 != null)
                        {
                            intPtr3 = Utility.AllocHGlobalIntPtrArray((int)ldapControlArray1.Length + 1);
                            for (int j = 0; j < (int)ldapControlArray1.Length; j++)
                            {
                                intPtr = Marshal.AllocHGlobal(num1);
                                Marshal.StructureToPtr(ldapControlArray1[j], intPtr, false);
                                intPtr1 = (IntPtr)((long)intPtr3 + (long)(Marshal.SizeOf(typeof(IntPtr)) * j));
                                Marshal.WriteIntPtr(intPtr1, intPtr);
                            }
                            intPtr1 = (IntPtr)((long)intPtr3 + (long)(Marshal.SizeOf(typeof(IntPtr)) * (int)ldapControlArray1.Length));
                            Marshal.WriteIntPtr(intPtr1, (IntPtr)0);
                        }
                        int num2 = Wldap32.ldap_start_tls(this.connection.ldapHandle, ref num, ref intPtr4, intPtr2, intPtr3);
                        if (intPtr4 != (IntPtr)0)
                        {
                            int num3 = Wldap32.ldap_parse_result_referral(this.connection.ldapHandle, intPtr4, (IntPtr)0, (IntPtr)0, (IntPtr)0, ref intPtr5, (IntPtr)0, 0);
                            if (num3 == 0 && intPtr5 != (IntPtr)0)
                            {
                                char **   chrPointer  = (char **)((void *)intPtr5);
                                char *    chrPointer1 = (char *)((void *)(*(chrPointer)));
                                int       num4        = 0;
                                ArrayList arrayLists  = new ArrayList();
                                while (chrPointer1 != null)
                                {
                                    string stringUni = Marshal.PtrToStringUni((IntPtr)chrPointer1);
                                    arrayLists.Add(stringUni);
                                    num4++;
                                    chrPointer1 = (char *)((void *)(*(chrPointer + num4 * sizeof(char *))));
                                }
                                if (intPtr5 != (IntPtr)0)
                                {
                                    Wldap32.ldap_value_free(intPtr5);
                                    intPtr5 = (IntPtr)0;
                                }
                                if (arrayLists.Count > 0)
                                {
                                    uri = new Uri[arrayLists.Count];
                                    for (int k = 0; k < arrayLists.Count; k++)
                                    {
                                        uri[k] = new Uri((string)arrayLists[k]);
                                    }
                                }
                            }
                        }
                        if (num2 != 0)
                        {
                            string str = Res.GetString("DefaultLdapError");
                            if (!Utility.IsResultCode((ResultCode)num2))
                            {
                                if (Utility.IsLdapError((LdapError)num2))
                                {
                                    str = LdapErrorMappings.MapResultCode(num2);
                                    throw new LdapException(num2, str);
                                }
                            }
                            else
                            {
                                if (num2 == 80)
                                {
                                    num2 = num;
                                }
                                str = OperationErrorMappings.MapResultCode(num2);
                                ExtendedResponse extendedResponse = new ExtendedResponse(null, null, (ResultCode)num2, str, uri);
                                extendedResponse.name = "1.3.6.1.4.1.1466.20037";
                                throw new TlsOperationException(extendedResponse);
                            }
                        }
                    }
                    finally
                    {
                        if (intPtr2 != (IntPtr)0)
                        {
                            for (int l = 0; l < (int)ldapControlArray.Length; l++)
                            {
                                IntPtr intPtr6 = Marshal.ReadIntPtr(intPtr2, Marshal.SizeOf(typeof(IntPtr)) * l);
                                if (intPtr6 != (IntPtr)0)
                                {
                                    Marshal.FreeHGlobal(intPtr6);
                                }
                            }
                            Marshal.FreeHGlobal(intPtr2);
                        }
                        if (ldapControlArray != null)
                        {
                            for (int m = 0; m < (int)ldapControlArray.Length; m++)
                            {
                                if (ldapControlArray[m].ldctl_oid != (IntPtr)0)
                                {
                                    Marshal.FreeHGlobal(ldapControlArray[m].ldctl_oid);
                                }
                                if (ldapControlArray[m].ldctl_value != null && ldapControlArray[m].ldctl_value.bv_val != (IntPtr)0)
                                {
                                    Marshal.FreeHGlobal(ldapControlArray[m].ldctl_value.bv_val);
                                }
                            }
                        }
                        if (intPtr3 != (IntPtr)0)
                        {
                            for (int n = 0; n < (int)ldapControlArray1.Length; n++)
                            {
                                IntPtr intPtr7 = Marshal.ReadIntPtr(intPtr3, Marshal.SizeOf(typeof(IntPtr)) * n);
                                if (intPtr7 != (IntPtr)0)
                                {
                                    Marshal.FreeHGlobal(intPtr7);
                                }
                            }
                            Marshal.FreeHGlobal(intPtr3);
                        }
                        if (ldapControlArray1 != null)
                        {
                            for (int o = 0; o < (int)ldapControlArray1.Length; o++)
                            {
                                if (ldapControlArray1[o].ldctl_oid != (IntPtr)0)
                                {
                                    Marshal.FreeHGlobal(ldapControlArray1[o].ldctl_oid);
                                }
                                if (ldapControlArray1[o].ldctl_value != null && ldapControlArray1[o].ldctl_value.bv_val != (IntPtr)0)
                                {
                                    Marshal.FreeHGlobal(ldapControlArray1[o].ldctl_value.bv_val);
                                }
                            }
                        }
                        if (intPtr5 != (IntPtr)0)
                        {
                            Wldap32.ldap_value_free(intPtr5);
                        }
                    }
                    return;
                }
                else
                {
                    throw new ObjectDisposedException(this.GetType().Name);
                }
            }
            else
            {
                throw new PlatformNotSupportedException(Res.GetString("TLSNotSupported"));
            }
        }
Ejemplo n.º 10
0
        private DirectoryResponse ConstructElement(XmlElement node)
        {
            DirectoryResponse el = null;

            Debug.Assert(node != null);

            switch (node.LocalName)
            {
                case DsmlConstants.DsmlErrorResponse:
                    el = new DsmlErrorResponse(node);
                    break;

                case DsmlConstants.DsmlSearchResponse:
                    el = new SearchResponse(node);
                    break;

                case DsmlConstants.DsmlModifyResponse:
                    el = new ModifyResponse(node);
                    break;

                case DsmlConstants.DsmlAddResponse:
                    el = new AddResponse(node);
                    break;

                case DsmlConstants.DsmlDelResponse:
                    el = new DeleteResponse(node);
                    break;

                case DsmlConstants.DsmlModDNResponse:
                    el = new ModifyDNResponse(node);
                    break;

                case DsmlConstants.DsmlCompareResponse:
                    el = new CompareResponse(node);
                    break;

                case DsmlConstants.DsmlExtendedResponse:
                    el = new ExtendedResponse(node);
                    break;

                case DsmlConstants.DsmlAuthResponse:
                    el = new DsmlAuthResponse(node);
                    break;
                default:
                    throw new DsmlInvalidDocumentException(Res.GetString(Res.UnknownResponseElement));
            }

            Debug.Assert(el != null);

            return el;
        }
Ejemplo n.º 11
0
		private DirectoryException ConstructException(int error, LdapOperation operation)
		{
			DirectoryResponse extendedResponse = null;
			if (!Utility.IsResultCode((ResultCode)error))
			{
				if (!Utility.IsLdapError((LdapError)error))
				{
					return new LdapException(error);
				}
				else
				{
					string str = LdapErrorMappings.MapResultCode(error);
					string serverErrorMessage = this.options.ServerErrorMessage;
					if (serverErrorMessage == null || serverErrorMessage.Length <= 0)
					{
						return new LdapException(error, str);
					}
					else
					{
						throw new LdapException(error, str, serverErrorMessage);
					}
				}
			}
			else
			{
				if (operation != LdapOperation.LdapAdd)
				{
					if (operation != LdapOperation.LdapModify)
					{
						if (operation != LdapOperation.LdapDelete)
						{
							if (operation != LdapOperation.LdapModifyDn)
							{
								if (operation != LdapOperation.LdapCompare)
								{
									if (operation != LdapOperation.LdapSearch)
									{
										if (operation == LdapOperation.LdapExtendedRequest)
										{
											extendedResponse = new ExtendedResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
										}
									}
									else
									{
										extendedResponse = new SearchResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
									}
								}
								else
								{
									extendedResponse = new CompareResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
								}
							}
							else
							{
								extendedResponse = new ModifyDNResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
							}
						}
						else
						{
							extendedResponse = new DeleteResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
						}
					}
					else
					{
						extendedResponse = new ModifyResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
					}
				}
				else
				{
					extendedResponse = new AddResponse(null, null, (ResultCode)error, OperationErrorMappings.MapResultCode(error), null);
				}
				string str1 = OperationErrorMappings.MapResultCode(error);
				return new DirectoryOperationException(extendedResponse, str1);
			}
		}
Ejemplo n.º 12
0
		internal DirectoryResponse ConstructResponse(int messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, bool exceptionOnTimeOut)
		{
			DirectoryResponse directoryResponse;
			LDAP_TIMEVAL lDAPTIMEVAL = new LDAP_TIMEVAL();
			lDAPTIMEVAL.tv_sec = (int)(requestTimeOut.Ticks / (long)0x989680);
			IntPtr intPtr = (IntPtr)0;
			DirectoryResponse searchResponse = null;
			IntPtr intPtr1 = (IntPtr)0;
			IntPtr intPtr2 = (IntPtr)0;
			bool flag = true;
			if (resultType != ResultAll.LDAP_MSG_ALL)
			{
				lDAPTIMEVAL.tv_sec = 0;
				lDAPTIMEVAL.tv_usec = 0;
				if (resultType == ResultAll.LDAP_MSG_POLLINGALL)
				{
					resultType = ResultAll.LDAP_MSG_ALL;
				}
				flag = false;
			}
			int num = Wldap32.ldap_result(this.ldapHandle, messageId, (int)resultType, lDAPTIMEVAL, ref intPtr);
			if (num == -1 || num == 0)
			{
				if (num != 0)
				{
					num = Wldap32.LdapGetLastError();
				}
				else
				{
					if (!exceptionOnTimeOut)
					{
						return null;
					}
					else
					{
						num = 85;
					}
				}
				if (flag)
				{
					Wldap32.ldap_abandon(this.ldapHandle, messageId);
				}
			}
			else
			{
				int num1 = 0;
				try
				{
					int num2 = 0;
					string str = null;
					string str1 = null;
					Uri[] uriArray = null;
					DirectoryControl[] directoryControlArray = null;
					if (num != 100 && num != 115)
					{
						num2 = this.ConstructParsedResult(intPtr, ref num1, ref str, ref str1, ref uriArray, ref directoryControlArray);
					}
					if (num2 != 0)
					{
						num = num2;
						throw this.ConstructException(num, operation);
					}
					else
					{
						num2 = num1;
						if (num != 105)
						{
							if (num != 103)
							{
								if (num != 107)
								{
									if (num != 109)
									{
										if (num != 111)
										{
											if (num != 120)
											{
												if (num == 101 || num == 100 || num == 115)
												{
													searchResponse = new SearchResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
													if (num == 101)
													{
														((SearchResponse)searchResponse).searchDone = true;
													}
													SearchResultEntryCollection searchResultEntryCollection = new SearchResultEntryCollection();
													SearchResultReferenceCollection searchResultReferenceCollection = new SearchResultReferenceCollection();
													IntPtr intPtr3 = Wldap32.ldap_first_entry(this.ldapHandle, intPtr);
													int num3 = 0;
													while (intPtr3 != (IntPtr)0)
													{
														SearchResultEntry searchResultEntry = this.ConstructEntry(intPtr3);
														if (searchResultEntry != null)
														{
															searchResultEntryCollection.Add(searchResultEntry);
														}
														num3++;
														intPtr3 = Wldap32.ldap_next_entry(this.ldapHandle, intPtr3);
													}
													IntPtr intPtr4 = Wldap32.ldap_first_reference(this.ldapHandle, intPtr);
													while (intPtr4 != (IntPtr)0)
													{
														SearchResultReference searchResultReference = this.ConstructReference(intPtr4);
														if (searchResultReference != null)
														{
															searchResultReferenceCollection.Add(searchResultReference);
														}
														intPtr4 = Wldap32.ldap_next_reference(this.ldapHandle, intPtr4);
													}
													((SearchResponse)searchResponse).SetEntries(searchResultEntryCollection);
													((SearchResponse)searchResponse).SetReferences(searchResultReferenceCollection);
												}
											}
											else
											{
												searchResponse = new ExtendedResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
												if (num2 == 0)
												{
													num2 = Wldap32.ldap_parse_extended_result(this.ldapHandle, intPtr, ref intPtr1, ref intPtr2, 0);
													if (num2 == 0)
													{
														string stringUni = null;
														if (intPtr1 != (IntPtr)0)
														{
															stringUni = Marshal.PtrToStringUni(intPtr1);
														}
														byte[] numArray = null;
														if (intPtr2 != (IntPtr)0)
														{
															berval _berval = new berval();
															Marshal.PtrToStructure(intPtr2, _berval);
															if (_berval.bv_len != 0 && _berval.bv_val != (IntPtr)0)
															{
																numArray = new byte[_berval.bv_len];
																Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
															}
														}
														((ExtendedResponse)searchResponse).name = stringUni;
														((ExtendedResponse)searchResponse).@value = numArray;
													}
												}
											}
										}
										else
										{
											searchResponse = new CompareResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
										}
									}
									else
									{
										searchResponse = new ModifyDNResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
									}
								}
								else
								{
									searchResponse = new DeleteResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
								}
							}
							else
							{
								searchResponse = new ModifyResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
							}
						}
						else
						{
							searchResponse = new AddResponse(str, directoryControlArray, (ResultCode)num2, str1, uriArray);
						}
						if (num2 == 0 || num2 == 5 || num2 == 6 || num2 == 10 || num2 == 9)
						{
							directoryResponse = searchResponse;
						}
						else
						{
							if (!Utility.IsResultCode((ResultCode)num2))
							{
								throw new DirectoryOperationException(searchResponse);
							}
							else
							{
								throw new DirectoryOperationException(searchResponse, OperationErrorMappings.MapResultCode(num2));
							}
						}
					}
				}
				finally
				{
					if (intPtr1 != (IntPtr)0)
					{
						Wldap32.ldap_memfree(intPtr1);
					}
					if (intPtr2 != (IntPtr)0)
					{
						Wldap32.ldap_memfree(intPtr2);
					}
					if (intPtr != (IntPtr)0)
					{
						Wldap32.ldap_msgfree(intPtr);
					}
				}
				return directoryResponse;
			}
			throw this.ConstructException(num, operation);
		}
Ejemplo n.º 13
0
		private DirectoryResponse ConstructElement(XmlElement node)
		{
			DirectoryResponse dsmlErrorResponse = null;
			string localName = node.LocalName;
			string str = localName;
			if (localName != null)
			{
				if (str == "errorResponse")
				{
					dsmlErrorResponse = new DsmlErrorResponse(node);
				}
				else if (str == "searchResponse")
				{
					dsmlErrorResponse = new SearchResponse(node);
				}
				else if (str == "modifyResponse")
				{
					dsmlErrorResponse = new ModifyResponse(node);
				}
				else if (str == "addResponse")
				{
					dsmlErrorResponse = new AddResponse(node);
				}
				else if (str == "delResponse")
				{
					dsmlErrorResponse = new DeleteResponse(node);
				}
				else if (str == "modDNResponse")
				{
					dsmlErrorResponse = new ModifyDNResponse(node);
				}
				else if (str == "compareResponse")
				{
					dsmlErrorResponse = new CompareResponse(node);
				}
				else if (str == "extendedResponse")
				{
					dsmlErrorResponse = new ExtendedResponse(node);
				}
				else if (str == "authResponse")
				{
					dsmlErrorResponse = new DsmlAuthResponse(node);
				}
				else
				{
					throw new DsmlInvalidDocumentException(Res.GetString("UnknownResponseElement"));
				}
				return dsmlErrorResponse;
			}
			throw new DsmlInvalidDocumentException(Res.GetString("UnknownResponseElement"));
		}
        public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls)
        {
            IntPtr serverControlArray = IntPtr.Zero;

            LdapControl[] managedServerControls = null;
            IntPtr        clientControlArray    = IntPtr.Zero;

            LdapControl[] managedClientControls = null;
            //IntPtr ldapResult = IntPtr.Zero;
            //IntPtr referral = IntPtr.Zero;

            //int serverError = 0;
            //Uri[] responseReferral = null;

            if (_connection._disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            try
            {
                IntPtr tempPtr = IntPtr.Zero;

                // build server control
                managedServerControls = _connection.BuildControlArray(controls, true);
                int structSize = Marshal.SizeOf(typeof(LdapControl));
                if (managedServerControls != null)
                {
                    serverControlArray = Utility.AllocHGlobalIntPtrArray(managedServerControls.Length + 1);
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        IntPtr controlPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedServerControls[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)serverControlArray + IntPtr.Size * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }

                    tempPtr = (IntPtr)((long)serverControlArray + IntPtr.Size * managedServerControls.Length);
                    Marshal.WriteIntPtr(tempPtr, IntPtr.Zero);
                }

                // Build client control.
                managedClientControls = _connection.BuildControlArray(controls, false);
                if (managedClientControls != null)
                {
                    clientControlArray = Utility.AllocHGlobalIntPtrArray(managedClientControls.Length + 1);
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        IntPtr controlPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedClientControls[i], controlPtr, false);
                        tempPtr = (IntPtr)((long)clientControlArray + IntPtr.Size * i);
                        Marshal.WriteIntPtr(tempPtr, controlPtr);
                    }

                    tempPtr = (IntPtr)((long)clientControlArray + IntPtr.Size * managedClientControls.Length);
                    Marshal.WriteIntPtr(tempPtr, IntPtr.Zero);
                }

                // requested certificate.
                //  - no certificate = no problem.
                //  - bad certificate = no problem
                var require_cert = (int)LDAP_OPT_X_TLS.ALLOW;
                Wldap32.ldap_set_option_int(_connection._ldapHandle, LdapOption.LDAP_OPT_X_TLS_REQUIRE_CERT, ref require_cert);
                //new tls client context
                var off = 0;
                Wldap32.ldap_set_option_int(_connection._ldapHandle, LdapOption.LDAP_OPT_X_TLS_NEWCTX, ref off);

                //var test tls connect callback
                var result = Wldap32.ldap_set_option_TLS_CONNECT_CB(_connection._ldapHandle, LdapOption.LDAP_OPT_X_TLS_CONNECT_CB, _routine);



                int error = Wldap32.ldap_start_tls(_connection._ldapHandle, serverControlArray, clientControlArray);


                //IntPtr ret1 = IntPtr.Zero;
                //Wldap32.ldap_get_option_ptr(_connection._ldapHandle, LdapOption.LDAP_OPT_X_TLS_CACERTFILE, ref ret1);
                //Console.WriteLine(Encoding.PtrToStringUTF8(ret1));

                //typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl, void *ctx, void *arg ));


                //TODO: check referrals equivalent with openldap

                /*                if (ldapResult != IntPtr.Zero)
                 *              {
                 *                  // Parse the referral.
                 *                  int resultError = Wldap32.ldap_parse_result_referral(_connection._ldapHandle, ldapResult, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref referral, IntPtr.Zero, 0  );
                 *                  if (resultError == 0 && referral != IntPtr.Zero)
                 *                  {
                 *                      char** referralPtr = (char**)referral;
                 *                      char* singleReferral = referralPtr[0];
                 *                      int i = 0;
                 *                      ArrayList referralList = new ArrayList();
                 *                      while (singleReferral != null)
                 *                      {
                 *                          string s = Encoding.PtrToString((IntPtr)singleReferral);
                 *                          referralList.Add(s);
                 *
                 *                          i++;
                 *                          singleReferral = referralPtr[i];
                 *                      }
                 *
                 *                      // Free heap memory.
                 *                      if (referral != IntPtr.Zero)
                 *                      {
                 *                          Wldap32.ldap_value_free(referral);
                 *                          referral = IntPtr.Zero;
                 *                      }
                 *
                 *                      if (referralList.Count > 0)
                 *                      {
                 *                          responseReferral = new Uri[referralList.Count];
                 *                          for (int j = 0; j < referralList.Count; j++)
                 *                          {
                 *                              responseReferral[j] = new Uri((string)referralList[j]);
                 *                          }
                 *                      }
                 *                  }
                 *              }
                 */
                if (error != (int)ResultCode.Success)
                {
                    string errorText = Encoding.PtrToString(Wldap32.ldap_err2string(error));

                    string errorAdditional = GetStringValueHelper(LdapOption.LDAP_OPT_DIAGNOSTIC_MESSAGE, true);

                    if (error > 0)
                    {
                        //positive, indicating an LDAP resultCode other than 'success'

                        ExtendedResponse response = new ExtendedResponse(null, null, (ResultCode)error, errorText, null);
                        response.ResponseName = "1.3.6.1.4.1.1466.20037";
                        throw new TlsOperationException(response);
                    }
                    else if (error < 0)
                    {
                        //negative, indicating an API error code;
                        error = Math.Abs(error) + 80; //convert negative number for exception for compatibilty with original S.DS.P
                        throw new LdapException(error, errorText, errorAdditional);
                    }
                }
            }
            finally
            {
                if (serverControlArray != IntPtr.Zero)
                {
                    // Release the memory from the heap.
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(serverControlArray, IntPtr.Size * i);
                        if (tempPtr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(tempPtr);
                        }
                    }
                    Marshal.FreeHGlobal(serverControlArray);
                }

                if (managedServerControls != null)
                {
                    for (int i = 0; i < managedServerControls.Length; i++)
                    {
                        if (managedServerControls[i].ldctl_oid != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(managedServerControls[i].ldctl_oid);
                        }

                        if (managedServerControls[i].ldctl_value != null)
                        {
                            if (managedServerControls[i].ldctl_value.bv_val != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(managedServerControls[i].ldctl_value.bv_val);
                            }
                        }
                    }
                }

                if (clientControlArray != IntPtr.Zero)
                {
                    // Release the memor from the heap.
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(clientControlArray, IntPtr.Size * i);
                        if (tempPtr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(tempPtr);
                        }
                    }

                    Marshal.FreeHGlobal(clientControlArray);
                }

                if (managedClientControls != null)
                {
                    for (int i = 0; i < managedClientControls.Length; i++)
                    {
                        if (managedClientControls[i].ldctl_oid != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(managedClientControls[i].ldctl_oid);
                        }

                        if (managedClientControls[i].ldctl_value != null)
                        {
                            if (managedClientControls[i].ldctl_value.bv_val != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(managedClientControls[i].ldctl_value.bv_val);
                            }
                        }
                    }
                }

                /*if (referral != IntPtr.Zero)
                 * {
                 *  Wldap32.ldap_value_free(referral);
                 * }*/
            }
        }
        internal DirectoryResponse ConstructResponse(int messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, bool exceptionOnTimeOut)
        {
            LDAP_TIMEVAL timeout = new LDAP_TIMEVAL {
                tv_sec = (int) (requestTimeOut.Ticks / 0x989680L)
            };
            IntPtr zero = IntPtr.Zero;
            DirectoryResponse response = null;
            IntPtr oid = IntPtr.Zero;
            IntPtr data = IntPtr.Zero;
            IntPtr entryMessage = IntPtr.Zero;
            bool flag = true;
            if (resultType != ResultAll.LDAP_MSG_ALL)
            {
                timeout.tv_sec = 0;
                timeout.tv_usec = 0;
                if (resultType == ResultAll.LDAP_MSG_POLLINGALL)
                {
                    resultType = ResultAll.LDAP_MSG_ALL;
                }
                flag = false;
            }
            int error = Wldap32.ldap_result(this.ldapHandle, messageId, (int) resultType, timeout, ref zero);
            switch (error)
            {
                case -1:
                case 0:
                    break;

                default:
                {
                    int serverError = 0;
                    try
                    {
                        int errorCode = 0;
                        string responseDn = null;
                        string responseMessage = null;
                        Uri[] responseReferral = null;
                        DirectoryControl[] responseControl = null;
                        if ((error != 100) && (error != 0x73))
                        {
                            errorCode = this.ConstructParsedResult(zero, ref serverError, ref responseDn, ref responseMessage, ref responseReferral, ref responseControl);
                        }
                        if (errorCode == 0)
                        {
                            errorCode = serverError;
                            switch (error)
                            {
                                case 0x69:
                                    response = new AddResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 0x67:
                                    response = new ModifyResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 0x6b:
                                    response = new DeleteResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 0x6d:
                                    response = new ModifyDNResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 0x6f:
                                    response = new CompareResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    break;

                                case 120:
                                    response = new ExtendedResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    if (errorCode == 0)
                                    {
                                        errorCode = Wldap32.ldap_parse_extended_result(this.ldapHandle, zero, ref oid, ref data, 0);
                                        if (errorCode == 0)
                                        {
                                            string str3 = null;
                                            if (oid != IntPtr.Zero)
                                            {
                                                str3 = Marshal.PtrToStringUni(oid);
                                            }
                                            berval structure = null;
                                            byte[] destination = null;
                                            if (data != IntPtr.Zero)
                                            {
                                                structure = new berval();
                                                Marshal.PtrToStructure(data, structure);
                                                if ((structure.bv_len != 0) && (structure.bv_val != IntPtr.Zero))
                                                {
                                                    destination = new byte[structure.bv_len];
                                                    Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
                                                }
                                            }
                                            ((ExtendedResponse) response).name = str3;
                                            ((ExtendedResponse) response).value = destination;
                                        }
                                    }
                                    break;

                                case 0x65:
                                case 100:
                                case 0x73:
                                {
                                    response = new SearchResponse(responseDn, responseControl, (ResultCode) errorCode, responseMessage, responseReferral);
                                    if (error == 0x65)
                                    {
                                        ((SearchResponse) response).searchDone = true;
                                    }
                                    SearchResultEntryCollection col = new SearchResultEntryCollection();
                                    SearchResultReferenceCollection references = new SearchResultReferenceCollection();
                                    entryMessage = Wldap32.ldap_first_entry(this.ldapHandle, zero);
                                    int num4 = 0;
                                    while (entryMessage != IntPtr.Zero)
                                    {
                                        SearchResultEntry entry = this.ConstructEntry(entryMessage);
                                        if (entry != null)
                                        {
                                            col.Add(entry);
                                        }
                                        num4++;
                                        entryMessage = Wldap32.ldap_next_entry(this.ldapHandle, entryMessage);
                                    }
                                    for (IntPtr ptr5 = Wldap32.ldap_first_reference(this.ldapHandle, zero); ptr5 != IntPtr.Zero; ptr5 = Wldap32.ldap_next_reference(this.ldapHandle, ptr5))
                                    {
                                        SearchResultReference reference = this.ConstructReference(ptr5);
                                        if (reference != null)
                                        {
                                            references.Add(reference);
                                        }
                                    }
                                    ((SearchResponse) response).SetEntries(col);
                                    ((SearchResponse) response).SetReferences(references);
                                    break;
                                }
                            }
                            switch (errorCode)
                            {
                                case 0:
                                case 5:
                                case 6:
                                case 10:
                                case 9:
                                    return response;

                                default:
                                    if (Utility.IsResultCode((ResultCode) errorCode))
                                    {
                                        throw new DirectoryOperationException(response, OperationErrorMappings.MapResultCode(errorCode));
                                    }
                                    throw new DirectoryOperationException(response);
                            }
                        }
                        error = errorCode;
                        goto Label_03A7;
                    }
                    finally
                    {
                        if (oid != IntPtr.Zero)
                        {
                            Wldap32.ldap_memfree(oid);
                        }
                        if (data != IntPtr.Zero)
                        {
                            Wldap32.ldap_memfree(data);
                        }
                        if (zero != IntPtr.Zero)
                        {
                            Wldap32.ldap_msgfree(zero);
                        }
                    }
                    break;
                }
            }
            if (error == 0)
            {
                if (!exceptionOnTimeOut)
                {
                    return null;
                }
                error = 0x55;
            }
            else
            {
                error = Wldap32.LdapGetLastError();
            }
            if (flag)
            {
                Wldap32.ldap_abandon(this.ldapHandle, messageId);
            }
        Label_03A7:
            throw this.ConstructException(error, operation);
        }
 public unsafe void StartTransportLayerSecurity(DirectoryControlCollection controls)
 {
     IntPtr zero = IntPtr.Zero;
     LdapControl[] controlArray = null;
     IntPtr clientControls = IntPtr.Zero;
     LdapControl[] controlArray2 = null;
     IntPtr message = IntPtr.Zero;
     IntPtr referral = IntPtr.Zero;
     int serverReturnValue = 0;
     Uri[] uriArray = null;
     if (Utility.IsWin2kOS)
     {
         throw new PlatformNotSupportedException(Res.GetString("TLSNotSupported"));
     }
     if (this.connection.disposed)
     {
         throw new ObjectDisposedException(base.GetType().Name);
     }
     try
     {
         IntPtr ptr = IntPtr.Zero;
         IntPtr ptr6 = IntPtr.Zero;
         controlArray = this.connection.BuildControlArray(controls, true);
         int cb = Marshal.SizeOf(typeof(LdapControl));
         if (controlArray != null)
         {
             zero = Marshal.AllocHGlobal((int) (Marshal.SizeOf(typeof(IntPtr)) * (controlArray.Length + 1)));
             for (int i = 0; i < controlArray.Length; i++)
             {
                 ptr = Marshal.AllocHGlobal(cb);
                 Marshal.StructureToPtr(controlArray[i], ptr, false);
                 ptr6 = (IntPtr) (((long) zero) + (Marshal.SizeOf(typeof(IntPtr)) * i));
                 Marshal.WriteIntPtr(ptr6, ptr);
             }
             ptr6 = (IntPtr) (((long) zero) + (Marshal.SizeOf(typeof(IntPtr)) * controlArray.Length));
             Marshal.WriteIntPtr(ptr6, IntPtr.Zero);
         }
         controlArray2 = this.connection.BuildControlArray(controls, false);
         if (controlArray2 != null)
         {
             clientControls = Marshal.AllocHGlobal((int) (Marshal.SizeOf(typeof(IntPtr)) * (controlArray2.Length + 1)));
             for (int j = 0; j < controlArray2.Length; j++)
             {
                 ptr = Marshal.AllocHGlobal(cb);
                 Marshal.StructureToPtr(controlArray2[j], ptr, false);
                 ptr6 = (IntPtr) (((long) clientControls) + (Marshal.SizeOf(typeof(IntPtr)) * j));
                 Marshal.WriteIntPtr(ptr6, ptr);
             }
             ptr6 = (IntPtr) (((long) clientControls) + (Marshal.SizeOf(typeof(IntPtr)) * controlArray2.Length));
             Marshal.WriteIntPtr(ptr6, IntPtr.Zero);
         }
         int errorCode = Wldap32.ldap_start_tls(this.connection.ldapHandle, ref serverReturnValue, ref message, zero, clientControls);
         if (((message != IntPtr.Zero) && (Wldap32.ldap_parse_result_referral(this.connection.ldapHandle, message, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref referral, IntPtr.Zero, 0) == 0)) && (referral != IntPtr.Zero))
         {
             char** chPtr = (char**) referral;
             char* chPtr2 = chPtr[0];
             int index = 0;
             ArrayList list = new ArrayList();
             while (chPtr2 != null)
             {
                 string str = Marshal.PtrToStringUni((IntPtr) chPtr2);
                 list.Add(str);
                 index++;
                 chPtr2 = chPtr[index];
             }
             if (referral != IntPtr.Zero)
             {
                 Wldap32.ldap_value_free(referral);
                 referral = IntPtr.Zero;
             }
             if (list.Count > 0)
             {
                 uriArray = new Uri[list.Count];
                 for (int k = 0; k < list.Count; k++)
                 {
                     uriArray[k] = new Uri((string) list[k]);
                 }
             }
         }
         if (errorCode != 0)
         {
             string str2 = Res.GetString("DefaultLdapError");
             if (Utility.IsResultCode((ResultCode) errorCode))
             {
                 if (errorCode == 80)
                 {
                     errorCode = serverReturnValue;
                 }
                 str2 = OperationErrorMappings.MapResultCode(errorCode);
                 ExtendedResponse response = new ExtendedResponse(null, null, (ResultCode) errorCode, str2, uriArray) {
                     name = "1.3.6.1.4.1.1466.20037"
                 };
                 throw new TlsOperationException(response);
             }
             if (Utility.IsLdapError((LdapError) errorCode))
             {
                 str2 = LdapErrorMappings.MapResultCode(errorCode);
                 throw new LdapException(errorCode, str2);
             }
         }
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             for (int m = 0; m < controlArray.Length; m++)
             {
                 IntPtr hglobal = Marshal.ReadIntPtr(zero, Marshal.SizeOf(typeof(IntPtr)) * m);
                 if (hglobal != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(hglobal);
                 }
             }
             Marshal.FreeHGlobal(zero);
         }
         if (controlArray != null)
         {
             for (int n = 0; n < controlArray.Length; n++)
             {
                 if (controlArray[n].ldctl_oid != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(controlArray[n].ldctl_oid);
                 }
                 if ((controlArray[n].ldctl_value != null) && (controlArray[n].ldctl_value.bv_val != IntPtr.Zero))
                 {
                     Marshal.FreeHGlobal(controlArray[n].ldctl_value.bv_val);
                 }
             }
         }
         if (clientControls != IntPtr.Zero)
         {
             for (int num11 = 0; num11 < controlArray2.Length; num11++)
             {
                 IntPtr ptr8 = Marshal.ReadIntPtr(clientControls, Marshal.SizeOf(typeof(IntPtr)) * num11);
                 if (ptr8 != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(ptr8);
                 }
             }
             Marshal.FreeHGlobal(clientControls);
         }
         if (controlArray2 != null)
         {
             for (int num12 = 0; num12 < controlArray2.Length; num12++)
             {
                 if (controlArray2[num12].ldctl_oid != IntPtr.Zero)
                 {
                     Marshal.FreeHGlobal(controlArray2[num12].ldctl_oid);
                 }
                 if ((controlArray2[num12].ldctl_value != null) && (controlArray2[num12].ldctl_value.bv_val != IntPtr.Zero))
                 {
                     Marshal.FreeHGlobal(controlArray2[num12].ldctl_value.bv_val);
                 }
             }
         }
         if (referral != IntPtr.Zero)
         {
             Wldap32.ldap_value_free(referral);
         }
     }
 }