Exemple #1
0
        internal override async Task <IntPtr> BindSimpleAsync(SafeHandle ld, string who, string password)
        {
            LdapConnect(ld);
            return(await Task.Factory.StartNew(() =>
            {
                var berval = new Native.berval
                {
                    bv_len = password.Length,
                    bv_val = Marshal.StringToHGlobalAnsi(password)
                };
                var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(berval));
                Marshal.StructureToPtr(berval, ptr, false);
                var result = IntPtr.Zero;
                var msgidp = NativeMethodsWindows.ldap_simple_bind(ld, who, password);

                if (msgidp == -1)
                {
                    throw new LdapException($"{nameof(BindSimpleAsync)} failed. {nameof(NativeMethodsWindows.ldap_simple_bind)} returns wrong or empty result", nameof(NativeMethodsWindows.ldap_simple_bind), 1);
                }

                var rc = ldap_result(ld, msgidp, 0, IntPtr.Zero, ref result);

                if (rc == Native.LdapResultType.LDAP_ERROR || rc == Native.LdapResultType.LDAP_TIMEOUT)
                {
                    ThrowIfError((int)rc, nameof(NativeMethodsWindows.ldap_simple_bind));
                }

                return result;
            }).ConfigureAwait(false));
        }
 internal override int ldap_rename(SafeHandle ld, string dn, string newrdn, string newparent, int deleteoldrdn, IntPtr serverctrls,
                                   IntPtr clientctrls, ref int msgidp)
 {
     return(NativeMethodsWindows.ldap_rename(ld, dn,
                                             newrdn, newparent, deleteoldrdn,
                                             serverctrls, clientctrls, ref msgidp));
 }
        internal override int BindSasl(SafeHandle ld, Native.LdapAuthType authType, LdapCredential ldapCredential)
        {
            LdapConnect(ld);
            var cred = ToNative(ldapCredential);

            return(NativeMethodsWindows.ldap_bind_s(ld, null, cred, Native.LdapAuthMechanism.ToBindMethod(authType)));
        }
        internal override int ldap_start_tls_s(SafeHandle ld, ref int serverReturnValue, ref IntPtr message,
                                               IntPtr serverctrls, IntPtr clientctrls)
        {
            var rc = NativeMethodsWindows.ldap_start_tls_s(ld, serverReturnValue, message, serverctrls, clientctrls);

            _tlsStarted = rc == (int)Native.ResultCode.Success;
            return(rc);
        }
Exemple #5
0
        private void LdapConnect(SafeHandle ld)
        {
            var timeout = new LDAP_TIMEVAL
            {
                tv_sec = (int)(TimeSpan.FromMinutes(10).Ticks / TimeSpan.TicksPerSecond)
            };

            ThrowIfError(NativeMethodsWindows.ldap_connect(ld, timeout), nameof(NativeMethodsWindows.ldap_connect));
        }
        internal override void LdapConnect(SafeHandle ld, TimeSpan connectionTimeout)
        {
            var timeout = new LDAP_TIMEVAL
            {
                tv_sec = (int)(connectionTimeout.Ticks / TimeSpan.TicksPerSecond)
            };

            ThrowIfError(NativeMethodsWindows.ldap_connect(ld, timeout), nameof(NativeMethodsWindows.ldap_connect));
        }
Exemple #7
0
 internal override int Init(ref IntPtr ld, string hostname, int port)
 {
     ld = NativeMethodsWindows.ldap_init(hostname, port);
     if (ld == IntPtr.Zero)
     {
         return(-1);
     }
     return((int)Native.ResultCode.Success);
 }
        internal override int ldap_get_option(SafeHandle ld, int option, ref string value)
        {
            IntPtr outValue = default;
            var    rc       = NativeMethodsWindows.ldap_get_option(ld, option, ref outValue);

            if (rc == (int)Native.ResultCode.Success && outValue != IntPtr.Zero)
            {
                value = Encoder.Instance.PtrToString(outValue);
            }
            return(rc);
        }
Exemple #9
0
        internal override int BindKerberos(SafeHandle ld)
        {
            LdapConnect(ld);
            var cred = new SEC_WINNT_AUTH_IDENTITY_EX
            {
                version = NativeMethodsWindows.SEC_WINNT_AUTH_IDENTITY_VERSION,
                length  = Marshal.SizeOf(typeof(SEC_WINNT_AUTH_IDENTITY_EX)),
                flags   = NativeMethodsWindows.SEC_WINNT_AUTH_IDENTITY_UNICODE
            };

            return(NativeMethodsWindows.ldap_bind_s(ld, null, cred, BindMethod.LDAP_AUTH_NEGOTIATE));
        }
        internal override async Task <IntPtr> BindSaslAsync(SafeHandle ld, Native.LdapAuthType authType, LdapCredential ldapCredential)
        {
            LdapConnect(ld);
            var cred = ToNative(ldapCredential);

            var task = Task.Factory.StartNew(() =>
            {
                ThrowIfError(NativeMethodsWindows.ldap_bind_s(ld, null, cred, Native.LdapAuthMechanism.ToBindMethod(authType)), nameof(NativeMethodsWindows.ldap_bind_s));

                return(IntPtr.Zero);
            });

            return(await task.ConfigureAwait(false));
        }
Exemple #11
0
        internal override async Task <IntPtr> BindKerberosAsync(SafeHandle ld)
        {
            LdapConnect(ld);
            var cred = new SEC_WINNT_AUTH_IDENTITY_EX
            {
                version = NativeMethodsWindows.SEC_WINNT_AUTH_IDENTITY_VERSION,
                length  = Marshal.SizeOf(typeof(SEC_WINNT_AUTH_IDENTITY_EX)),
                flags   = NativeMethodsWindows.SEC_WINNT_AUTH_IDENTITY_UNICODE
            };

            var task = Task.Factory.StartNew(() =>
            {
                ThrowIfError(NativeMethodsWindows.ldap_bind_s(ld, null, cred, BindMethod.LDAP_AUTH_NEGOTIATE), nameof(NativeMethodsWindows.ldap_bind_s));

                return(IntPtr.Zero);
            });

            return(await task.ConfigureAwait(false));
        }
        internal override async Task <IntPtr> BindSimpleAsync(SafeHandle ld, string who, string password)
        {
            LdapConnect(ld);
            return(await Task.Factory.StartNew(() =>
            {
                var result = IntPtr.Zero;
                var msgidp = NativeMethodsWindows.ldap_bind(ld, who, password, BindMethod.LDAP_AUTH_SIMPLE);

                if (msgidp == -1)
                {
                    throw new LdapException($"{nameof(BindSimpleAsync)} failed. {nameof(NativeMethodsWindows.ldap_bind)} returns wrong or empty result", nameof(NativeMethodsWindows.ldap_bind), 1);
                }

                var rc = ldap_result(ld, msgidp, 0, IntPtr.Zero, ref result);

                if (rc == Native.LdapResultType.LDAP_ERROR || rc == Native.LdapResultType.LDAP_TIMEOUT)
                {
                    ThrowIfError((int)rc, nameof(NativeMethodsWindows.ldap_bind));
                }

                return result;
            }).ConfigureAwait(false));
        }
Exemple #13
0
 internal override string LdapError2String(int error) => NativeMethodsWindows.LdapError2String(error);
Exemple #14
0
 internal override string GetAdditionalErrorInfo(SafeHandle ld) => NativeMethodsWindows.GetAdditionalErrorInfo(ld);
Exemple #15
0
 internal override void ldap_controls_free(IntPtr ctrls) => NativeMethodsWindows.ldap_controls_free(ctrls);
Exemple #16
0
 internal override int ldap_parse_extended_result(SafeHandle ldapHandle, IntPtr result, ref IntPtr oid, ref IntPtr data, byte freeIt) =>
 NativeMethodsWindows.ldap_parse_extended_result(ldapHandle, result, ref oid, ref data, freeIt);
Exemple #17
0
 internal override int ldap_extended_operation(SafeHandle ld, string requestoid, IntPtr requestdata, IntPtr serverctrls,
                                               IntPtr clientctrls, ref int msgidp) =>
 NativeMethodsWindows.ldap_extended_operation(ld, requestoid, requestdata, serverctrls, clientctrls, ref msgidp);
Exemple #18
0
 internal override IntPtr ldap_get_dn(SafeHandle ld, IntPtr message) => NativeMethodsWindows.ldap_get_dn(ld, message);
Exemple #19
0
 internal override int ldap_delete_ext(SafeHandle ld, string dn, IntPtr serverctrls, IntPtr clientctrls, ref int msgidp) => NativeMethodsWindows.ldap_delete_ext(ld, dn, serverctrls, clientctrls, ref msgidp);
Exemple #20
0
 internal override IntPtr ldap_get_values(SafeHandle ld, IntPtr entry, IntPtr pBer) => NativeMethodsWindows.ldap_get_values(ld, entry, pBer);
Exemple #21
0
 internal override void ldap_value_free(IntPtr vals) => NativeMethodsWindows.ldap_value_free(vals);
Exemple #22
0
 internal override int ldap_parse_reference(SafeHandle ld, IntPtr reference, ref string[] referralsp, ref IntPtr serverctrlsp, int freeit) => NativeMethodsWindows.ldap_parse_reference(ld, reference, ref referralsp, ref serverctrlsp, freeit);
Exemple #23
0
 internal override void ldap_msgfree(IntPtr message) => NativeMethodsWindows.ldap_msgfree(message);
Exemple #24
0
 internal override void ldap_memfree(IntPtr ptr) => NativeMethodsWindows.ldap_memfree(ptr);
Exemple #25
0
 internal override int BindSimple(SafeHandle ld, string who, string password)
 {
     LdapConnect(ld);
     return(NativeMethodsWindows.ldap_simple_bind_s(ld, who, password));
 }
Exemple #26
0
 internal override int Compare(SafeHandle ld, string dn, string attr, string value, IntPtr bvalue, IntPtr serverctrls,
                               IntPtr clientctrls,
                               ref int msgidp) =>
 NativeMethodsWindows.ldap_compare_ext(ld, dn, attr, value, bvalue, serverctrls, clientctrls, ref msgidp);
Exemple #27
0
 internal override IntPtr ldap_first_attribute(SafeHandle ld, IntPtr entry, ref IntPtr ppBer) => NativeMethodsWindows.ldap_first_attribute(ld, entry, ref ppBer);
Exemple #28
0
 internal override IntPtr ldap_next_entry(SafeHandle ld, IntPtr message) => NativeMethodsWindows.ldap_next_entry(ld, message);
Exemple #29
0
 internal override int ldap_set_option(SafeHandle ld, int option, ref int invalue)
 => NativeMethodsWindows.ldap_set_option(ld, option, ref invalue);
Exemple #30
0
 internal override IntPtr ldap_next_attribute(SafeHandle ld, IntPtr entry, IntPtr pBer) => NativeMethodsWindows.ldap_next_attribute(ld, entry, pBer);