Exemple #1
0
        internal override int LdapGetLastError(SafeHandle ld)
        {
            int err = -1;

            NativeMethodsLinux.ldap_get_option(ld, (int)Native.LdapOption.LDAP_OPT_RESULT_CODE, ref err);
            return(err);
        }
Exemple #2
0
        internal override async Task <IntPtr> BindSimpleAsync(SafeHandle ld, string userDn, string password, LDAP_TIMEVAL timeout)
        {
            return(await Task.Factory.StartNew(() =>
            {
                var berval = new Native.berval
                {
                    bv_len = password.Length,
                    bv_val = Encoder.Instance.StringToPtr(password)
                };
                var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(berval));
                Marshal.StructureToPtr(berval, ptr, false);
                var msgidp = 0;
                var result = IntPtr.Zero;
                NativeMethodsLinux.ldap_sasl_bind(ld, userDn, null, ptr, IntPtr.Zero, IntPtr.Zero, ref msgidp);
                Marshal.FreeHGlobal(ptr);
                if (msgidp == -1)
                {
                    throw new LdapException(
                        new LdapExceptionData($"{nameof(BindSimpleAsync)} failed. {nameof(NativeMethodsLinux.ldap_sasl_bind)} returns wrong or empty result",
                                              nameof(NativeMethodsLinux.ldap_sasl_bind), 1));
                }

                var rc = ldap_result(ld, msgidp, 0, timeout, ref result);

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

                return result;
            }).ConfigureAwait(false));
        }
Exemple #3
0
        internal override int Search(SafeHandle ld, string @base, int scope, string filter, IntPtr attributes,
                                     int attrsonly, IntPtr serverctrls,
                                     IntPtr clientctrls, int timeout, int sizelimit, ref int msgidp)
        {
            var timePtr = IntPtr.Zero;

            try
            {
                if (timeout > 0)
                {
                    var timeval = new LDAP_TIMEVAL
                    {
                        tv_sec = timeout
                    };
                    timePtr = Marshal.AllocHGlobal(Marshal.SizeOf <LDAP_TIMEVAL>());
                    Marshal.StructureToPtr(timeval, timePtr, true);
                }

                return(NativeMethodsLinux.ldap_search_ext(ld, @base, scope, filter, attributes, attrsonly,
                                                          serverctrls, clientctrls, timePtr, sizelimit, ref msgidp));
            }
            finally
            {
                if (timePtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(timePtr);
                }
            }
        }
Exemple #4
0
        internal override int Compare(SafeHandle ld, string dn, string attr, string value, IntPtr bvalue,
                                      IntPtr serverctrls, IntPtr clientctrls,
                                      ref int msgidp)
        {
            var ptr = bvalue == IntPtr.Zero && value != null?StringToBerVal(value) : bvalue;

            return(NativeMethodsLinux.ldap_compare_ext(ld, dn, attr, ptr, serverctrls, clientctrls, ref msgidp));
        }
Exemple #5
0
 private static void ThrowIfGnuTlsError(int res, string method)
 {
     if (res < 0)
     {
         var errorName    = Encoder.Instance.PtrToString(NativeMethodsLinux.gnutls_strerror_name(res));
         var errorMessage = Encoder.Instance.PtrToString(NativeMethodsLinux.gnutls_strerror(res));
         throw new LdapException(
                   new LdapExceptionData($"GnuTls error name: {errorName}. GnuTls error message: {errorMessage} Result: {res}. Method: {method}"));
     }
 }
Exemple #6
0
        internal override int BindKerberos(SafeHandle ld)
        {
            var saslDefaults = GetSaslDefaults(ld);
            var ptr          = Marshal.AllocHGlobal(Marshal.SizeOf(saslDefaults));

            Marshal.StructureToPtr(saslDefaults, ptr, false);

            return(NativeMethodsLinux.ldap_sasl_interactive_bind_s(ld, null, Native.LdapAuthMechanism.GSSAPI, IntPtr.Zero, IntPtr.Zero,
                                                                   (uint)Native.LdapInteractionFlags.LDAP_SASL_QUIET, (l, flags, d, interact) => (int)Native.ResultCode.Success, ptr));
        }
Exemple #7
0
        internal override int BindSasl(SafeHandle ld, Native.LdapAuthType authType, LdapCredential ldapCredential)
        {
            var mechanism = Native.LdapAuthMechanism.FromAuthType(authType);
            var cred      = ToNative(ld, mechanism, ldapCredential);

            var rc = NativeMethodsLinux.ldap_sasl_interactive_bind_s(ld, null, mechanism, IntPtr.Zero, IntPtr.Zero,
                                                                     (uint)Native.LdapInteractionFlags.LDAP_SASL_QUIET, UnixSaslMethods.SaslInteractionProcedure, cred);

            Marshal.FreeHGlobal(cred);
            return(rc);
        }
Exemple #8
0
        internal override async Task <IntPtr> BindSaslAsync(SafeHandle ld, Native.LdapAuthType authType,
                                                            LdapCredential ldapCredential, LDAP_TIMEVAL timeout)
        {
            var task = Task.Factory.StartNew(() =>
            {
                int rc;
                var msgid        = 0;
                var result       = IntPtr.Zero;
                var rmech        = IntPtr.Zero;
                var mech         = Native.LdapAuthMechanism.FromAuthType(authType);
                var cred         = ToNative(ld, mech, ldapCredential);
                var saslDefaults = Marshal.PtrToStructure <Native.LdapSaslDefaults>(cred);
                do
                {
                    rc = NativeMethodsLinux.ldap_sasl_interactive_bind(ld, null, mech, IntPtr.Zero, IntPtr.Zero,
                                                                       (uint)Native.LdapInteractionFlags.LDAP_SASL_QUIET,
                                                                       UnixSaslMethods.SaslInteractionProcedure, cred, result, ref rmech,
                                                                       ref msgid);
                    if (rc != (int)Native.ResultCode.SaslBindInProgress)
                    {
                        break;
                    }

                    ldap_msgfree(result);

                    if (ldap_result(ld, msgid, 0, timeout, ref result) == Native.LdapResultType.LDAP_ERROR)
                    {
                        ThrowIfError(rc, nameof(NativeMethodsLinux.ldap_sasl_interactive_bind));
                    }

                    if (result == IntPtr.Zero)
                    {
                        throw new LdapException(new LdapExceptionData("Result is not initialized",
                                                                      nameof(NativeMethodsLinux.ldap_sasl_interactive_bind), 1));
                    }
                } while (rc == (int)Native.ResultCode.SaslBindInProgress);

                Marshal.FreeHGlobal(cred);

                ThrowIfError(ld, rc, nameof(NativeMethodsLinux.ldap_sasl_interactive_bind),
                             new Dictionary <string, string>
                {
                    [nameof(saslDefaults)] = saslDefaults.ToString()
                });
                return(result);
            });

            return(await task.ConfigureAwait(false));
        }
Exemple #9
0
        internal override async Task <IntPtr> BindKerberosAsync(SafeHandle ld)
        {
            var task = Task.Factory.StartNew(() =>
            {
                var rc           = 0;
                var msgid        = 0;
                var result       = IntPtr.Zero;
                var rmech        = IntPtr.Zero;
                var saslDefaults = GetSaslDefaults(ld);
                var ptr          = Marshal.AllocHGlobal(Marshal.SizeOf(saslDefaults));
                Marshal.StructureToPtr(saslDefaults, ptr, false);
                do
                {
                    rc = NativeMethodsLinux.ldap_sasl_interactive_bind(ld, null, Native.LdapAuthMechanism.GSSAPI, IntPtr.Zero, IntPtr.Zero,
                                                                       (uint)Native.LdapInteractionFlags.LDAP_SASL_QUIET,
                                                                       SaslInteractProc, ptr, result, ref rmech,
                                                                       ref msgid);
                    if (rc != (int)Native.ResultCode.SaslBindInProgress)
                    {
                        break;
                    }
                    ldap_msgfree(result);

                    if (ldap_result(ld, msgid, 0, IntPtr.Zero, ref result) == Native.LdapResultType.LDAP_ERROR)
                    {
                        ThrowIfError(rc, nameof(NativeMethodsLinux.ldap_sasl_interactive_bind));
                    }

                    if (result == IntPtr.Zero)
                    {
                        throw new LdapException("Result is not initialized", nameof(NativeMethodsLinux.ldap_sasl_interactive_bind), 1);
                    }
                } while (rc == (int)Native.ResultCode.SaslBindInProgress);

                ThrowIfError(ld, rc, nameof(NativeMethodsLinux.ldap_sasl_interactive_bind), new Dictionary <string, string>
                {
                    [nameof(saslDefaults)] = saslDefaults.ToString()
                });
                return(result);
            });

            return(await task.ConfigureAwait(false));
        }
Exemple #10
0
 internal override void ber_memfree(IntPtr value) => NativeMethodsLinux.ber_memfree(value);
Exemple #11
0
 internal override int ber_bvecfree(IntPtr value)
 => NativeMethodsLinux.ber_bvecfree(value);
Exemple #12
0
 internal override int ber_scanf_string(SafeHandle berElement, string format, IntPtr value, ref int length)
 => NativeMethodsLinux.ber_scanf_string(berElement, format, value, ref length);
Exemple #13
0
 internal override int ber_scanf_int(SafeHandle berElement, string format, ref int value)
 => NativeMethodsLinux.ber_scanf_int(berElement, format, ref value);
Exemple #14
0
 internal override IntPtr ber_init(IntPtr value)
 => NativeMethodsLinux.ber_init(value);
Exemple #15
0
 internal override int ber_printf_berarray(SafeHandle berElement, string format, IntPtr value)
 => NativeMethodsLinux.ber_printf_berarray(berElement, format, value);
Exemple #16
0
 internal override int ber_printf_emptyarg(SafeHandle berElement, string format)
 => NativeMethodsLinux.ber_printf_emptyarg(berElement, format);
Exemple #17
0
 internal override int ber_printf_bytearray(SafeHandle berElement, string format, HGlobalMemHandle value, int length)
 => NativeMethodsLinux.ber_printf_bytearray(berElement, format, value, length);
Exemple #18
0
 internal override int ldap_extended_operation(SafeHandle ld, string requestoid, IntPtr requestdata,
                                               IntPtr serverctrls,
                                               IntPtr clientctrls, ref int msgidp) =>
 NativeMethodsLinux.ldap_extended_operation(ld, requestoid, requestdata, serverctrls, clientctrls,
                                            ref msgidp);
Exemple #19
0
 internal override int ber_flatten(SafeHandle berElement, ref IntPtr value)
 => NativeMethodsLinux.ber_flatten(berElement, ref value);
Exemple #20
0
 internal override int ldap_parse_extended_result(SafeHandle ldapHandle, IntPtr result, ref IntPtr oid,
                                                  ref IntPtr data, byte freeIt) =>
 NativeMethodsLinux.ldap_parse_extended_result(ldapHandle, result, ref oid, ref data, freeIt);
Exemple #21
0
 internal override int ber_scanf(SafeHandle berElement, string format)
 => NativeMethodsLinux.ber_scanf(berElement, format);
Exemple #22
0
 internal override int ldap_start_tls_s(SafeHandle ld, ref int serverReturnValue, ref IntPtr message,
                                        IntPtr serverctrls, IntPtr clientctrls)
 {
     return(NativeMethodsLinux.ldap_start_tls_s(ld, serverctrls, clientctrls));
 }
Exemple #23
0
 internal override int ber_scanf_ostring(SafeHandle berElement, string format, IntPtr value) =>
 NativeMethodsLinux.ber_scanf_ostring(berElement, format, value);
Exemple #24
0
 internal override void ldap_controls_free(IntPtr ctrls) => NativeMethodsLinux.ldap_controls_free(ctrls);
Exemple #25
0
 internal override int ber_peek_tag(SafeHandle berElement, ref int length) => NativeMethodsLinux.ber_peek_tag(berElement, ref length);
Exemple #26
0
 internal override int ldap_control_free(IntPtr control) => NativeMethodsLinux.ldap_control_free(control);
Exemple #27
0
 internal override IntPtr ber_free(IntPtr berelem, int option)
 => NativeMethodsLinux.ber_free(berelem, option);
Exemple #28
0
 internal override int ldap_create_sort_control(SafeHandle handle, IntPtr keys, byte critical,
                                                ref IntPtr control)
 => NativeMethodsLinux.ldap_create_sort_control(handle, keys, critical, ref control);
Exemple #29
0
 private bool IsSelfSigned(IntPtr cert) => NativeMethodsLinux.gnutls_x509_crt_check_issuer(cert, cert) != 0;
Exemple #30
0
 internal override IntPtr ber_alloc_t(int option) => NativeMethodsLinux.ber_alloc_t(option);