Example #1
0
        private string GetStringValueHelper(LdapOption option, bool releasePtr)
        {
            if (_connection.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            IntPtr outValue = new IntPtr(0);
            int    error    = Wldap32.ldap_get_option_ptr(_connection.ldapHandle, option, ref outValue);

            ErrorChecking.CheckAndSetLdapError(error);

            string s = null;

            if (outValue != (IntPtr)0)
            {
                s = Marshal.PtrToStringUni(outValue);
            }

            if (releasePtr)
            {
                Wldap32.ldap_memfree(outValue);
            }

            return(s);
        }
Example #2
0
 public void StopTransportLayerSecurity()
 {
     if (!Utility.IsWin2kOS)
     {
         if (!this.connection.disposed)
         {
             byte num = Wldap32.ldap_stop_tls(this.connection.ldapHandle);
             if (num != 0)
             {
                 return;
             }
             else
             {
                 throw new TlsOperationException(null, Res.GetString("TLSStopFailure"));
             }
         }
         else
         {
             throw new ObjectDisposedException(this.GetType().Name);
         }
     }
     else
     {
         throw new PlatformNotSupportedException(Res.GetString("TLSNotSupported"));
     }
 }
        private static byte[] DecodingByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            IntPtr zero      = IntPtr.Zero;
            berval structure = new berval();

            byte[] destination = null;
            error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref zero);
            try
            {
                if ((error == 0) && (zero != IntPtr.Zero))
                {
                    Marshal.PtrToStructure(zero, structure);
                    destination = new byte[structure.bv_len];
                    Marshal.Copy(structure.bv_val, destination, 0, structure.bv_len);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Wldap32.ber_bvfree(zero);
                }
            }
            return(destination);
        }
Example #4
0
        private void SetStringValueHelper(LdapOption option, string value)
        {
            if (_connection.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            IntPtr inValue = new IntPtr(0);

            if (value != null)
            {
                inValue = Marshal.StringToHGlobalUni(value);
            }

            try
            {
                int error = Wldap32.ldap_set_option_ptr(_connection.ldapHandle, option, ref inValue);
                ErrorChecking.CheckAndSetLdapError(error);
            }
            finally
            {
                if (inValue != (IntPtr)0)
                {
                    Marshal.FreeHGlobal(inValue);
                }
            }
        }
Example #5
0
 private void SetStringValueHelper(LdapOption option, string value)
 {
     if (!this.connection.disposed)
     {
         IntPtr intPtr = new IntPtr(0);
         if (value != null)
         {
             intPtr = Marshal.StringToHGlobalUni(value);
         }
         try
         {
             int num = Wldap32.ldap_set_option_ptr(this.connection.ldapHandle, option, ref intPtr);
             ErrorChecking.CheckAndSetLdapError(num);
         }
         finally
         {
             if (intPtr != (IntPtr)0)
             {
                 Marshal.FreeHGlobal(intPtr);
             }
         }
         return;
     }
     else
     {
         throw new ObjectDisposedException(this.GetType().Name);
     }
 }
Example #6
0
        private static byte[] DecodingByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            IntPtr intPtr  = (IntPtr)0;
            berval _berval = new berval();

            byte[] numArray = null;
            error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref intPtr);
            try
            {
                if (error == 0 && intPtr != (IntPtr)0)
                {
                    Marshal.PtrToStructure(intPtr, _berval);
                    numArray = new byte[_berval.bv_len];
                    Marshal.Copy(_berval.bv_val, numArray, 0, _berval.bv_len);
                }
            }
            finally
            {
                if (intPtr != (IntPtr)0)
                {
                    Wldap32.ber_bvfree(intPtr);
                }
            }
            return(numArray);
        }
Example #7
0
        private static int EncodingMultiByteArrayHelper(BerSafeHandle berElement, byte[][] tempValue, char fmt)
        {
            IntPtr intPtr;
            IntPtr intPtr1 = (IntPtr)0;

            SafeBerval[] safeBerval = null;
            int          num        = 0;

            try
            {
                if (tempValue != null)
                {
                    int i = 0;
                    intPtr1 = Utility.AllocHGlobalIntPtrArray((int)tempValue.Length + 1);
                    int num1 = Marshal.SizeOf(typeof(SafeBerval));
                    safeBerval = new SafeBerval[(int)tempValue.Length];
                    for (i = 0; i < (int)tempValue.Length; i++)
                    {
                        byte[] numArray = tempValue[i];
                        safeBerval[i] = new SafeBerval();
                        if (numArray != null)
                        {
                            safeBerval[i].bv_len = (int)numArray.Length;
                            safeBerval[i].bv_val = Marshal.AllocHGlobal((int)numArray.Length);
                            Marshal.Copy(numArray, 0, safeBerval[i].bv_val, (int)numArray.Length);
                        }
                        else
                        {
                            safeBerval[i].bv_len = 0;
                            safeBerval[i].bv_val = (IntPtr)0;
                        }
                        IntPtr intPtr2 = Marshal.AllocHGlobal(num1);
                        Marshal.StructureToPtr(safeBerval[i], intPtr2, false);
                        intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                        Marshal.WriteIntPtr(intPtr, intPtr2);
                    }
                    intPtr = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(IntPtr)) * i));
                    Marshal.WriteIntPtr(intPtr, (IntPtr)0);
                }
                num = Wldap32.ber_printf_berarray(berElement, new string(fmt, 1), intPtr1);
                GC.KeepAlive(safeBerval);
            }
            finally
            {
                if (intPtr1 != (IntPtr)0)
                {
                    for (int j = 0; j < (int)tempValue.Length; j++)
                    {
                        IntPtr intPtr3 = Marshal.ReadIntPtr(intPtr1, Marshal.SizeOf(typeof(IntPtr)) * j);
                        if (intPtr3 != (IntPtr)0)
                        {
                            Marshal.FreeHGlobal(intPtr3);
                        }
                    }
                    Marshal.FreeHGlobal(intPtr1);
                }
            }
            return(num);
        }
Example #8
0
 internal BerSafeHandle() : base(true)
 {
     SetHandle(Wldap32.ber_alloc(1));
     if (handle == IntPtr.Zero)
     {
         throw new OutOfMemoryException();
     }
 }
Example #9
0
 internal BerSafeHandle(berval value) : base(true)
 {
     SetHandle(Wldap32.ber_init(value));
     if (handle == IntPtr.Zero)
     {
         throw new BerConversionException();
     }
 }
Example #10
0
 protected override bool ReleaseHandle()
 {
     if (this.handle != (IntPtr)0)
     {
         Wldap32.ldap_unbind(this.handle);
         this.handle = (IntPtr)0;
     }
     return(true);
 }
Example #11
0
        private static byte[][] DecodingMultiByteArrayHelper(BerSafeHandle berElement, char fmt, ref int error)
        {
            error = 0;
            // several berval
            IntPtr    ptrResult  = IntPtr.Zero;
            int       i          = 0;
            ArrayList binaryList = new ArrayList();
            IntPtr    tempPtr    = IntPtr.Zero;

            byte[][] result = null;

            try
            {
                error = Wldap32.ber_scanf_ptr(berElement, new string(fmt, 1), ref ptrResult);

                if (error == 0)
                {
                    if (ptrResult != IntPtr.Zero)
                    {
                        tempPtr = Marshal.ReadIntPtr(ptrResult);
                        while (tempPtr != IntPtr.Zero)
                        {
                            berval ber = new berval();
                            Marshal.PtrToStructure(tempPtr, ber);

                            byte[] berArray = new byte[ber.bv_len];
                            Marshal.Copy(ber.bv_val, berArray, 0, ber.bv_len);

                            binaryList.Add(berArray);

                            i++;
                            tempPtr = Marshal.ReadIntPtr(ptrResult, i * IntPtr.Size);
                        }

                        result = new byte[binaryList.Count][];
                        for (int j = 0; j < binaryList.Count; j++)
                        {
                            result[j] = (byte[])binaryList[j];
                        }
                    }
                }
                else
                {
                    Debug.WriteLine("ber_scanf for format character 'V' failed");
                }
            }
            finally
            {
                if (ptrResult != IntPtr.Zero)
                {
                    Wldap32.ber_bvecfree(ptrResult);
                }
            }

            return(result);
        }
        private void SetIntValueHelper(LdapOption option, int value)
        {
            if (this.connection.disposed)
            {
                throw new ObjectDisposedException(base.GetType().Name);
            }
            int inValue = value;

            ErrorChecking.CheckAndSetLdapError(Wldap32.ldap_set_option_int(this.connection.ldapHandle, option, ref inValue));
        }
        private int GetIntValueHelper(LdapOption option)
        {
            if (this.connection.disposed)
            {
                throw new ObjectDisposedException(base.GetType().Name);
            }
            int outValue = 0;

            ErrorChecking.CheckAndSetLdapError(Wldap32.ldap_get_option_int(this.connection.ldapHandle, option, ref outValue));
            return(outValue);
        }
 private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
 {
     if (tempValue != null)
     {
         IntPtr destination = Marshal.AllocHGlobal(tempValue.Length);
         Marshal.Copy(tempValue, 0, destination, tempValue.Length);
         HGlobalMemHandle handle = new HGlobalMemHandle(destination);
         return(Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), handle, tempValue.Length));
     }
     return(Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle(IntPtr.Zero), 0));
 }
        private void ProcessCallBackRoutine(System.DirectoryServices.Protocols.ReferralCallback tempCallback)
        {
            LdapReferralCallback outValue = new LdapReferralCallback {
                sizeofcallback = Marshal.SizeOf(typeof(LdapReferralCallback)),
                query          = (tempCallback.QueryForConnection == null) ? null : this.queryDelegate,
                notify         = (tempCallback.NotifyNewConnection == null) ? null : this.notifiyDelegate,
                dereference    = (tempCallback.DereferenceConnection == null) ? null : this.dereferenceDelegate
            };

            ErrorChecking.CheckAndSetLdapError(Wldap32.ldap_set_option_referral(this.connection.ldapHandle, LdapOption.LDAP_OPT_REFERRAL_CALLBACK, ref outValue));
        }
Example #16
0
 override protected bool ReleaseHandle()
 {
     if (handle != (IntPtr)0)
     {
         if (needDispose)
         {
             Wldap32.ldap_unbind(handle);
         }
         handle = (IntPtr)0;
     }
     return(true);
 }
Example #17
0
        private void SetIntValueHelper(LdapOption option, int value)
        {
            if (_connection.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            int temp  = value;
            int error = Wldap32.ldap_set_option_int(_connection.ldapHandle, option, ref temp);

            ErrorChecking.CheckAndSetLdapError(error);
        }
Example #18
0
 internal BerSafeHandle() : base(true)
 {
     base.SetHandle(Wldap32.ber_alloc(1));
     if (this.handle != (IntPtr)0)
     {
         return;
     }
     else
     {
         throw new OutOfMemoryException();
     }
 }
Example #19
0
 internal BerSafeHandle(berval value) : base(true)
 {
     base.SetHandle(Wldap32.ber_init(value));
     if (this.handle != (IntPtr)0)
     {
         return;
     }
     else
     {
         throw new BerConversionException();
     }
 }
Example #20
0
        private void ProcessCallBackRoutine(ReferralCallback tempCallback)
        {
            LdapReferralCallback value = new LdapReferralCallback();

            value.sizeofcallback = Marshal.SizeOf(typeof(LdapReferralCallback));
            value.query          = tempCallback.QueryForConnection == null ? null : _queryDelegate;
            value.notify         = tempCallback.NotifyNewConnection == null ? null : _notifiyDelegate;
            value.dereference    = tempCallback.DereferenceConnection == null ? null : _dereferenceDelegate;

            int error = Wldap32.ldap_set_option_referral(_connection.ldapHandle, LdapOption.LDAP_OPT_REFERRAL_CALLBACK, ref value);

            ErrorChecking.CheckAndSetLdapError(error);
        }
Example #21
0
        override protected bool ReleaseHandle()
        {
            if (handle != IntPtr.Zero)
            {
                if (_needDispose)
                {
                    Wldap32.ldap_unbind(handle);
                }

                handle = IntPtr.Zero;
            }
            return(true);
        }
Example #22
0
 private void SetIntValueHelper(LdapOption option, int value)
 {
     if (!this.connection.disposed)
     {
         int num  = value;
         int num1 = Wldap32.ldap_set_option_int(this.connection.ldapHandle, option, ref num);
         ErrorChecking.CheckAndSetLdapError(num1);
         return;
     }
     else
     {
         throw new ObjectDisposedException(this.GetType().Name);
     }
 }
 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);
     }
 }
Example #24
0
        public void StopTransportLayerSecurity()
        {
            if (_connection.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            byte result = Wldap32.ldap_stop_tls(_connection.ldapHandle);

            if (result == 0)
            {
                // caller needs to close this ldap connection
                throw new TlsOperationException(null, String.Format(CultureInfo.CurrentCulture, SR.TLSStopFailure));
            }
        }
        public void FastConcurrentBind()
        {
            if (this.connection.disposed)
            {
                throw new ObjectDisposedException(base.GetType().Name);
            }
            int inValue = 1;

            this.ProtocolVersion = 3;
            int error = Wldap32.ldap_set_option_int(this.connection.ldapHandle, LdapOption.LDAP_OPT_FAST_CONCURRENT_BIND, ref inValue);

            if ((error == 0x59) && !Utility.IsWin2k3AboveOS)
            {
                throw new PlatformNotSupportedException(Res.GetString("ConcurrentBindNotSupport"));
            }
            ErrorChecking.CheckAndSetLdapError(error);
        }
Example #26
0
        private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
        {
            int num;

            if (tempValue == null)
            {
                num = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle((IntPtr)0), 0);
            }
            else
            {
                IntPtr intPtr = Marshal.AllocHGlobal((int)tempValue.Length);
                Marshal.Copy(tempValue, 0, intPtr, (int)tempValue.Length);
                HGlobalMemHandle hGlobalMemHandle = new HGlobalMemHandle(intPtr);
                num = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), hGlobalMemHandle, (int)tempValue.Length);
            }
            return(num);
        }
Example #27
0
        public void FastConcurrentBind()
        {
            if (_connection.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            int inValue = 1;

            // bump up the protocol version
            ProtocolVersion = 3;
            // do the fast concurrent bind
            int error = Wldap32.ldap_set_option_int(_connection.ldapHandle, LdapOption.LDAP_OPT_FAST_CONCURRENT_BIND, ref inValue);

            //we only throw PlatformNotSupportedException when we get parameter error and os is win2k3 below which does not support fast concurrent bind
            ErrorChecking.CheckAndSetLdapError(error);
        }
Example #28
0
        private void ProcessCallBackRoutine(ReferralCallback tempCallback)
        {
            QUERYFORCONNECTIONInternal    qUERYFORCONNECTIONInternal;
            NOTIFYOFNEWCONNECTIONInternal nOTIFYOFNEWCONNECTIONInternal;
            DEREFERENCECONNECTIONInternal dEREFERENCECONNECTIONInternal;
            LdapReferralCallback          ldapReferralCallback = new LdapReferralCallback();

            ldapReferralCallback.sizeofcallback = Marshal.SizeOf(typeof(LdapReferralCallback));
            LdapReferralCallback ldapReferralCallbackPointer = ldapReferralCallback;

            if (tempCallback.QueryForConnection == null)
            {
                qUERYFORCONNECTIONInternal = null;
            }
            else
            {
                qUERYFORCONNECTIONInternal = this.queryDelegate;
            }
            ldapReferralCallbackPointer.query = qUERYFORCONNECTIONInternal;
            LdapReferralCallback ldapReferralCallbackPointer1 = ldapReferralCallback;

            if (tempCallback.NotifyNewConnection == null)
            {
                nOTIFYOFNEWCONNECTIONInternal = null;
            }
            else
            {
                nOTIFYOFNEWCONNECTIONInternal = this.notifiyDelegate;
            }
            ldapReferralCallbackPointer1.notify = nOTIFYOFNEWCONNECTIONInternal;
            LdapReferralCallback ldapReferralCallbackPointer2 = ldapReferralCallback;

            if (tempCallback.DereferenceConnection == null)
            {
                dEREFERENCECONNECTIONInternal = null;
            }
            else
            {
                dEREFERENCECONNECTIONInternal = this.dereferenceDelegate;
            }
            ldapReferralCallbackPointer2.dereference = dEREFERENCECONNECTIONInternal;
            int num = Wldap32.ldap_set_option_referral(this.connection.ldapHandle, LdapOption.LDAP_OPT_REFERRAL_CALLBACK, ref ldapReferralCallback);

            ErrorChecking.CheckAndSetLdapError(num);
        }
Example #29
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);
                }
            }
        }
Example #30
0
        private static int EncodingByteArrayHelper(BerSafeHandle berElement, byte[] tempValue, char fmt)
        {
            int error = 0;

            // one byte array, one int arguments
            if (tempValue != null)
            {
                IntPtr tmp = Marshal.AllocHGlobal(tempValue.Length);
                Marshal.Copy(tempValue, 0, tmp, tempValue.Length);
                HGlobalMemHandle memHandle = new HGlobalMemHandle(tmp);

                error = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), memHandle, tempValue.Length);
            }
            else
            {
                error = Wldap32.ber_printf_bytearray(berElement, new string(fmt, 1), new HGlobalMemHandle(IntPtr.Zero), 0);
            }

            return(error);
        }