Exemple #1
0
        public static void CheckAndSetLdapError(int error)
        {
            string str;

            if (error == 0)
            {
                return;
            }
            else
            {
                if (!Utility.IsResultCode((ResultCode)error))
                {
                    if (!Utility.IsLdapError((LdapError)error))
                    {
                        throw new LdapException(error);
                    }
                    else
                    {
                        str = LdapErrorMappings.MapResultCode(error);
                        throw new LdapException(error, str);
                    }
                }
                else
                {
                    str = OperationErrorMappings.MapResultCode(error);
                    throw new DirectoryOperationException(null, str);
                }
            }
        }
 internal ConnectionHandle() : base(true)
 {
     base.SetHandle(Wldap32.ldap_init(null, 0x185));
     if (base.handle == IntPtr.Zero)
     {
         int errorCode = Wldap32.LdapGetLastError();
         if (Utility.IsLdapError((LdapError)errorCode))
         {
             string message = LdapErrorMappings.MapResultCode(errorCode);
             throw new LdapException(errorCode, message);
         }
         throw new LdapException(errorCode);
     }
 }
Exemple #3
0
        internal ConnectionHandle() : base(true)
        {
            SetHandle(Wldap32.ldap_init(null, 389));

            if (handle == IntPtr.Zero)
            {
                int error = Wldap32.LdapGetLastError();
                if (Utility.IsLdapError((LdapError)error))
                {
                    string errorMessage = LdapErrorMappings.MapResultCode(error);
                    throw new LdapException(error, errorMessage);
                }
                else
                {
                    throw new LdapException(error);
                }
            }
        }
Exemple #4
0
 public static void CheckAndSetLdapError(int error)
 {
     if (error != (int)ResultCode.Success)
     {
         if (Utility.IsResultCode((ResultCode)error))
         {
             string errorMessage = OperationErrorMappings.MapResultCode(error);
             throw new DirectoryOperationException(null, errorMessage);
         }
         else if (Utility.IsLdapError((LdapError)error))
         {
             string errorMessage = LdapErrorMappings.MapResultCode(error);
             throw new LdapException(error, errorMessage);
         }
         else
         {
             throw new LdapException(error);
         }
     }
 }
Exemple #5
0
 internal ConnectionHandle(IntPtr value, bool disposeHandle) : base(true)
 {
     _needDispose = disposeHandle;
     if (value == IntPtr.Zero)
     {
         int error = Wldap32.LdapGetLastError();
         if (Utility.IsLdapError((LdapError)error))
         {
             string errorMessage = LdapErrorMappings.MapResultCode(error);
             throw new LdapException(error, errorMessage);
         }
         else
         {
             throw new LdapException(error);
         }
     }
     else
     {
         SetHandle(value);
     }
 }
 internal ConnectionHandle() : base(true)
 {
     base.SetHandle(Wldap32.ldap_init(null, 0x185));
     if (this.handle != (IntPtr)0)
     {
         return;
     }
     else
     {
         int num = Wldap32.LdapGetLastError();
         if (!Utility.IsLdapError((LdapError)num))
         {
             throw new LdapException(num);
         }
         else
         {
             string str = LdapErrorMappings.MapResultCode(num);
             throw new LdapException(num, str);
         }
     }
 }
 internal ConnectionHandle(IntPtr value) : base(true)
 {
     if (value != (IntPtr)0)
     {
         base.SetHandle(value);
         return;
     }
     else
     {
         int num = Wldap32.LdapGetLastError();
         if (!Utility.IsLdapError((LdapError)num))
         {
             throw new LdapException(num);
         }
         else
         {
             string str = LdapErrorMappings.MapResultCode(num);
             throw new LdapException(num, str);
         }
     }
 }
        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);
                }
            }
        }
Exemple #9
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);
                }
            }
        }
Exemple #10
0
        private void GetResultsHelper(LdapPartialAsyncResult asyncResult)
        {
            LdapConnection con          = asyncResult.con;
            IntPtr         ldapResult   = (IntPtr)0;
            IntPtr         entryMessage = (IntPtr)0;
            ResultAll      resultType   = ResultAll.LDAP_MSG_RECEIVED;

            if (asyncResult.resultStatus == ResultsStatus.CompleteResult)
            {
                resultType = ResultAll.LDAP_MSG_POLLINGALL;
            }

            try
            {
                SearchResponse response = (SearchResponse)con.ConstructResponse(asyncResult.messageID, LdapOperation.LdapSearch, resultType, asyncResult.requestTimeout, false);
                // this should only happen in the polling thread case
                if (response == null)
                {
                    // only when request time out has not yet expiered
                    if ((asyncResult.startTime.Ticks + asyncResult.requestTimeout.Ticks) > DateTime.Now.Ticks)
                    {
                        // this is expected, just the client does not have the result yet
                        return;
                    }
                    else
                    {
                        // time out, now we need to throw proper exception
                        throw new LdapException((int)LdapError.TimeOut, LdapErrorMappings.MapResultCode((int)LdapError.TimeOut));
                    }
                }

                if (asyncResult.response != null)
                {
                    AddResult(asyncResult.response, response);
                }
                else
                {
                    asyncResult.response = response;
                }

                // if search is done, set the flag
                if (response.searchDone)
                {
                    asyncResult.resultStatus = ResultsStatus.Done;
                }
            }
            catch (Exception e)
            {
                if (e is DirectoryOperationException)
                {
                    SearchResponse response = (SearchResponse)(((DirectoryOperationException)e).Response);

                    if (asyncResult.response != null)
                    {
                        AddResult(asyncResult.response, response);
                    }
                    else
                    {
                        asyncResult.response = response;
                    }

                    // set the response back to the exception so it holds all the results up to now
                    ((DirectoryOperationException)e).Response = asyncResult.response;
                }
                else if (e is LdapException)
                {
                    LdapException ldapE     = (LdapException)e;
                    LdapError     errorCode = (LdapError)ldapE.ErrorCode;

                    if (asyncResult.response != null)
                    {
                        // add previous retrieved entries if available
                        if (asyncResult.response.Entries != null)
                        {
                            for (int i = 0; i < asyncResult.response.Entries.Count; i++)
                            {
                                ldapE.results.Add(asyncResult.response.Entries[i]);
                            }
                        }

                        // add previous retrieved references if available
                        if (asyncResult.response.References != null)
                        {
                            for (int i = 0; i < asyncResult.response.References.Count; i++)
                            {
                                ldapE.results.Add(asyncResult.response.References[i]);
                            }
                        }
                    }
                }

                // exception occurs, this operation is done.
                asyncResult.exception    = e;
                asyncResult.resultStatus = ResultsStatus.Done;

                // need to abandon this request
                Wldap32.ldap_abandon(con.ldapHandle, asyncResult.messageID);
            }
        }
        private void GetResultsHelper(LdapPartialAsyncResult asyncResult)
        {
            LdapConnection con        = asyncResult.con;
            IntPtr         ldapHandle = con.ldapHandle;
            ResultAll      resultType = ResultAll.LDAP_MSG_RECEIVED;

            if (asyncResult.resultStatus == ResultsStatus.CompleteResult)
            {
                resultType = ResultAll.LDAP_MSG_POLLINGALL;
            }
            try
            {
                SearchResponse newResult = (SearchResponse)con.ConstructResponse(asyncResult.messageID, LdapOperation.LdapSearch, resultType, asyncResult.requestTimeout, false);
                if (newResult == null)
                {
                    if ((asyncResult.startTime.Ticks + asyncResult.requestTimeout.Ticks) <= DateTime.Now.Ticks)
                    {
                        throw new LdapException(0x55, LdapErrorMappings.MapResultCode(0x55));
                    }
                }
                else
                {
                    if (asyncResult.response != null)
                    {
                        this.AddResult(asyncResult.response, newResult);
                    }
                    else
                    {
                        asyncResult.response = newResult;
                    }
                    if (newResult.searchDone)
                    {
                        asyncResult.resultStatus = ResultsStatus.Done;
                    }
                }
            }
            catch (Exception exception)
            {
                if (exception is DirectoryOperationException)
                {
                    SearchResponse response = (SearchResponse)((DirectoryOperationException)exception).Response;
                    if (asyncResult.response != null)
                    {
                        this.AddResult(asyncResult.response, response);
                    }
                    else
                    {
                        asyncResult.response = response;
                    }
                    ((DirectoryOperationException)exception).response = asyncResult.response;
                }
                else if (exception is LdapException)
                {
                    LdapException exception2 = (LdapException)exception;
                    int           errorCode  = exception2.ErrorCode;
                    if (asyncResult.response != null)
                    {
                        if (asyncResult.response.Entries != null)
                        {
                            for (int i = 0; i < asyncResult.response.Entries.Count; i++)
                            {
                                exception2.results.Add(asyncResult.response.Entries[i]);
                            }
                        }
                        if (asyncResult.response.References != null)
                        {
                            for (int j = 0; j < asyncResult.response.References.Count; j++)
                            {
                                exception2.results.Add(asyncResult.response.References[j]);
                            }
                        }
                    }
                }
                asyncResult.exception    = exception;
                asyncResult.resultStatus = ResultsStatus.Done;
                Wldap32.ldap_abandon(con.ldapHandle, asyncResult.messageID);
            }
        }
Exemple #12
0
        public override byte[] GetValue()
        {
            IntPtr control    = (IntPtr)0;
            int    structSize = Marshal.SizeOf(typeof(SortKey));
            int    keyCount   = _keys.Length;
            IntPtr memHandle  = Utility.AllocHGlobalIntPtrArray(keyCount + 1);

            try
            {
                IntPtr tempPtr = (IntPtr)0;
                IntPtr sortPtr = (IntPtr)0;
                int    i       = 0;
                for (i = 0; i < keyCount; i++)
                {
                    sortPtr = Marshal.AllocHGlobal(structSize);
                    Marshal.StructureToPtr(_keys[i], sortPtr, false);
                    tempPtr = (IntPtr)((long)memHandle + Marshal.SizeOf(typeof(IntPtr)) * i);
                    Marshal.WriteIntPtr(tempPtr, sortPtr);
                }
                tempPtr = (IntPtr)((long)memHandle + Marshal.SizeOf(typeof(IntPtr)) * i);
                Marshal.WriteIntPtr(tempPtr, (IntPtr)0);

                bool critical = IsCritical;
                int  error    = Wldap32.ldap_create_sort_control(UtilityHandle.GetHandle(), memHandle, critical ? (byte)1 : (byte)0, ref control);

                if (error != 0)
                {
                    if (Utility.IsLdapError((LdapError)error))
                    {
                        string errorMessage = LdapErrorMappings.MapResultCode(error);
                        throw new LdapException(error, errorMessage);
                    }
                    else
                    {
                        throw new LdapException(error);
                    }
                }

                LdapControl managedControl = new LdapControl();
                Marshal.PtrToStructure(control, managedControl);
                berval value = managedControl.ldctl_value;
                // reinitialize the value
                directoryControlValue = null;
                if (value != null)
                {
                    directoryControlValue = new byte[value.bv_len];
                    Marshal.Copy(value.bv_val, directoryControlValue, 0, value.bv_len);
                }
            }
            finally
            {
                if (control != (IntPtr)0)
                {
                    Wldap32.ldap_control_free(control);
                }

                if (memHandle != (IntPtr)0)
                {
                    //release the memory from the heap
                    for (int i = 0; i < keyCount; i++)
                    {
                        IntPtr tempPtr = Marshal.ReadIntPtr(memHandle, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (tempPtr != (IntPtr)0)
                        {
                            // free the marshalled name
                            IntPtr ptr = Marshal.ReadIntPtr(tempPtr);
                            if (ptr != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(ptr);
                            }
                            // free the marshalled rule
                            ptr = Marshal.ReadIntPtr(tempPtr, Marshal.SizeOf(typeof(IntPtr)));
                            if (ptr != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(ptr);
                            }

                            Marshal.FreeHGlobal(tempPtr);
                        }
                    }
                    Marshal.FreeHGlobal(memHandle);
                }
            }

            return(base.GetValue());
        }
Exemple #13
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"));
            }
        }
        public override byte[] GetValue()
        {
            IntPtr intPtr;
            byte   num;
            IntPtr intPtr1 = (IntPtr)0;
            int    num1    = Marshal.SizeOf(typeof(SortKey));
            IntPtr intPtr2 = Utility.AllocHGlobalIntPtrArray((int)this.keys.Length + 1);

            try
            {
                int i = 0;
                for (i = 0; i < (int)this.keys.Length; i++)
                {
                    IntPtr intPtr3 = Marshal.AllocHGlobal(num1);
                    Marshal.StructureToPtr(this.keys[i], intPtr3, false);
                    intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                    Marshal.WriteIntPtr(intPtr, intPtr3);
                }
                intPtr = (IntPtr)((long)intPtr2 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                Marshal.WriteIntPtr(intPtr, (IntPtr)0);
                bool             isCritical = base.IsCritical;
                ConnectionHandle handle     = UtilityHandle.GetHandle();
                IntPtr           intPtr4    = intPtr2;
                if (isCritical)
                {
                    num = 1;
                }
                else
                {
                    num = 0;
                }
                int num2 = Wldap32.ldap_create_sort_control(handle, intPtr4, num, ref intPtr1);
                if (num2 == 0)
                {
                    LdapControl ldapControl = new LdapControl();
                    Marshal.PtrToStructure(intPtr1, ldapControl);
                    berval ldctlValue = ldapControl.ldctl_value;
                    this.directoryControlValue = null;
                    if (ldctlValue != null)
                    {
                        this.directoryControlValue = new byte[ldctlValue.bv_len];
                        Marshal.Copy(ldctlValue.bv_val, this.directoryControlValue, 0, ldctlValue.bv_len);
                    }
                }
                else
                {
                    if (!Utility.IsLdapError((LdapError)num2))
                    {
                        throw new LdapException(num2);
                    }
                    else
                    {
                        string str = LdapErrorMappings.MapResultCode(num2);
                        throw new LdapException(num2, str);
                    }
                }
            }
            finally
            {
                if (intPtr1 != (IntPtr)0)
                {
                    Wldap32.ldap_control_free(intPtr1);
                }
                if (intPtr2 != (IntPtr)0)
                {
                    for (int j = 0; j < (int)this.keys.Length; j++)
                    {
                        IntPtr intPtr5 = Marshal.ReadIntPtr(intPtr2, Marshal.SizeOf(typeof(IntPtr)) * j);
                        if (intPtr5 != (IntPtr)0)
                        {
                            IntPtr intPtr6 = Marshal.ReadIntPtr(intPtr5);
                            if (intPtr6 != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(intPtr6);
                            }
                            intPtr6 = Marshal.ReadIntPtr(intPtr5, Marshal.SizeOf(typeof(IntPtr)));
                            if (intPtr6 != (IntPtr)0)
                            {
                                Marshal.FreeHGlobal(intPtr6);
                            }
                            Marshal.FreeHGlobal(intPtr5);
                        }
                    }
                    Marshal.FreeHGlobal(intPtr2);
                }
            }
            return(base.GetValue());
        }
        public override byte[] GetValue()
        {
            IntPtr zero   = IntPtr.Zero;
            int    cb     = Marshal.SizeOf(typeof(SortKey));
            int    length = this.keys.Length;
            IntPtr keys   = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(IntPtr)) * (length + 1)));

            try
            {
                IntPtr ptr   = IntPtr.Zero;
                IntPtr ptr4  = IntPtr.Zero;
                int    index = 0;
                index = 0;
                while (index < length)
                {
                    ptr4 = Marshal.AllocHGlobal(cb);
                    Marshal.StructureToPtr(this.keys[index], ptr4, false);
                    ptr = (IntPtr)(((long)keys) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                    Marshal.WriteIntPtr(ptr, ptr4);
                    index++;
                }
                ptr = (IntPtr)(((long)keys) + (Marshal.SizeOf(typeof(IntPtr)) * index));
                Marshal.WriteIntPtr(ptr, IntPtr.Zero);
                bool isCritical = base.IsCritical;
                int  errorCode  = Wldap32.ldap_create_sort_control(UtilityHandle.GetHandle(), keys, isCritical ? ((byte)1) : ((byte)0), ref zero);
                if (errorCode != 0)
                {
                    if (Utility.IsLdapError((LdapError)errorCode))
                    {
                        string message = LdapErrorMappings.MapResultCode(errorCode);
                        throw new LdapException(errorCode, message);
                    }
                    throw new LdapException(errorCode);
                }
                LdapControl structure = new LdapControl();
                Marshal.PtrToStructure(zero, structure);
                berval berval = structure.ldctl_value;
                base.directoryControlValue = null;
                if (berval != null)
                {
                    base.directoryControlValue = new byte[berval.bv_len];
                    Marshal.Copy(berval.bv_val, base.directoryControlValue, 0, berval.bv_len);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Wldap32.ldap_control_free(zero);
                }
                if (keys != IntPtr.Zero)
                {
                    for (int i = 0; i < length; i++)
                    {
                        IntPtr ptr5 = Marshal.ReadIntPtr(keys, Marshal.SizeOf(typeof(IntPtr)) * i);
                        if (ptr5 != IntPtr.Zero)
                        {
                            IntPtr hglobal = Marshal.ReadIntPtr(ptr5);
                            if (hglobal != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(hglobal);
                            }
                            hglobal = Marshal.ReadIntPtr(ptr5, Marshal.SizeOf(typeof(IntPtr)));
                            if (hglobal != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(hglobal);
                            }
                            Marshal.FreeHGlobal(ptr5);
                        }
                    }
                    Marshal.FreeHGlobal(keys);
                }
            }
            return(base.GetValue());
        }
Exemple #16
0
        private void GetResultsHelper(LdapPartialAsyncResult asyncResult)
        {
            LdapConnection connection = asyncResult._con;
            ResultAll      resultType = ResultAll.LDAP_MSG_RECEIVED;

            if (asyncResult._resultStatus == ResultsStatus.CompleteResult)
            {
                resultType = ResultAll.LDAP_MSG_POLLINGALL;
            }

            try
            {
                ValueTask <DirectoryResponse> vt = connection.ConstructResponseAsync(asyncResult._messageID, LdapOperation.LdapSearch, resultType, asyncResult._requestTimeout, false, sync: true);
                Debug.Assert(vt.IsCompleted);
                SearchResponse response = (SearchResponse)vt.GetAwaiter().GetResult();

                // This should only happen in the polling thread case.
                if (response == null)
                {
                    // Only when request time out has not yet expiered.
                    if ((asyncResult._startTime.Ticks + asyncResult._requestTimeout.Ticks) > DateTime.Now.Ticks)
                    {
                        // This is expected, just the client does not have the result yet .
                        return;
                    }
                    else
                    {
                        // time out, now we need to throw proper exception
                        throw new LdapException((int)LdapError.TimeOut, LdapErrorMappings.MapResultCode((int)LdapError.TimeOut));
                    }
                }

                if (asyncResult._response != null)
                {
                    AddResult(asyncResult._response, response);
                }
                else
                {
                    asyncResult._response = response;
                }

                // If search is done, set the flag.
                if (response.searchDone)
                {
                    asyncResult._resultStatus = ResultsStatus.Done;
                }
            }
            catch (Exception exception)
            {
                if (exception is DirectoryOperationException directoryOperationException)
                {
                    SearchResponse response = (SearchResponse)directoryOperationException.Response;
                    if (asyncResult._response != null)
                    {
                        AddResult(asyncResult._response, response);
                    }
                    else
                    {
                        asyncResult._response = response;
                    }

                    // Set the response back to the exception so it holds all the results up to now.
                    directoryOperationException.Response = asyncResult._response;
                }
                else if (exception is LdapException ldapException)
                {
                    if (asyncResult._response != null)
                    {
                        // add previous retrieved entries if available
                        if (asyncResult._response.Entries != null)
                        {
                            for (int i = 0; i < asyncResult._response.Entries.Count; i++)
                            {
                                ldapException.PartialResults.Add(asyncResult._response.Entries[i]);
                            }
                        }

                        // add previous retrieved references if available
                        if (asyncResult._response.References != null)
                        {
                            for (int i = 0; i < asyncResult._response.References.Count; i++)
                            {
                                ldapException.PartialResults.Add(asyncResult._response.References[i]);
                            }
                        }
                    }
                }

                // Exception occurs, this operation is done.
                asyncResult._exception    = exception;
                asyncResult._resultStatus = ResultsStatus.Done;

                // Need to abandon this request.
                LdapPal.CancelDirectoryAsyncOperation(connection._ldapHandle, asyncResult._messageID);
            }
        }
 public LdapException(int errorCode)
     : base(SR.Format(SR.DefaultLdapError, LdapErrorMappings.NormalizeResultCode(errorCode)))
 {
     ErrorCode = errorCode;
 }
        private void GetResultsHelper(LdapPartialAsyncResult asyncResult)
        {
            LdapConnection ldapConnection = asyncResult.con;
            ResultAll      resultAll      = ResultAll.LDAP_MSG_RECEIVED;

            if (asyncResult.resultStatus == ResultsStatus.CompleteResult)
            {
                resultAll = ResultAll.LDAP_MSG_POLLINGALL;
            }
            try
            {
                SearchResponse searchResponse = (SearchResponse)ldapConnection.ConstructResponse(asyncResult.messageID, LdapOperation.LdapSearch, resultAll, asyncResult.requestTimeout, false);
                if (searchResponse != null)
                {
                    if (asyncResult.response == null)
                    {
                        asyncResult.response = searchResponse;
                    }
                    else
                    {
                        this.AddResult(asyncResult.response, searchResponse);
                    }
                    if (searchResponse.searchDone)
                    {
                        asyncResult.resultStatus = ResultsStatus.Done;
                    }
                }
                else
                {
                    DateTime now = DateTime.Now;
                    if (asyncResult.startTime.Ticks + asyncResult.requestTimeout.Ticks <= now.Ticks)
                    {
                        throw new LdapException(85, LdapErrorMappings.MapResultCode(85));
                    }
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                if (exception as DirectoryOperationException == null)
                {
                    if (exception as LdapException != null)
                    {
                        LdapException ldapException = (LdapException)exception;
                        //TODO: Review: ldapException.ErrorCode;
                        if (asyncResult.response != null)
                        {
                            if (asyncResult.response.Entries != null)
                            {
                                for (int i = 0; i < asyncResult.response.Entries.Count; i++)
                                {
                                    ldapException.results.Add(asyncResult.response.Entries[i]);
                                }
                            }
                            if (asyncResult.response.References != null)
                            {
                                for (int j = 0; j < asyncResult.response.References.Count; j++)
                                {
                                    ldapException.results.Add(asyncResult.response.References[j]);
                                }
                            }
                        }
                    }
                }
                else
                {
                    SearchResponse response = (SearchResponse)((DirectoryOperationException)exception).Response;
                    if (asyncResult.response == null)
                    {
                        asyncResult.response = response;
                    }
                    else
                    {
                        this.AddResult(asyncResult.response, response);
                    }
                    ((DirectoryOperationException)exception).response = asyncResult.response;
                }
                asyncResult.exception    = exception;
                asyncResult.resultStatus = ResultsStatus.Done;
                Wldap32.ldap_abandon(ldapConnection.ldapHandle, asyncResult.messageID);
            }
        }