-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Adding the support of reusing machine-wide credentials in the case where the machine is already domain-joined to the LDAP Server. #36405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| using System.Diagnostics; | ||
| using System.Net; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace System.DirectoryServices.Protocols | ||
| { | ||
|
|
@@ -24,9 +25,9 @@ private int InternalConnectToServer() | |
| private int InternalBind(NetworkCredential tempCredential, SEC_WINNT_AUTH_IDENTITY_EX cred, BindMethod method) | ||
| { | ||
| int error; | ||
| if (tempCredential == null && AuthType == AuthType.External) | ||
| if (tempCredential == null && (AuthType == AuthType.External || AuthType == AuthType.Kerberos)) | ||
| { | ||
| error = Interop.ldap_simple_bind(_ldapHandle, null, null); | ||
| error = BindSasl(); | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -35,5 +36,43 @@ private int InternalBind(NetworkCredential tempCredential, SEC_WINNT_AUTH_IDENTI | |
|
|
||
| return error; | ||
| } | ||
|
|
||
| private int BindSasl() | ||
| { | ||
| SaslDefaultCredentials defaults = GetSaslDefaults(); | ||
| IntPtr ptrToDefaults = Marshal.AllocHGlobal(Marshal.SizeOf(defaults)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how big usually the size of defaults? I am just wondering if we need to use the stackalloc for small sizes to avoid the allocations?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not big at all, it is just one of these [StructLayout(LayoutKind.Sequential)]
internal struct SaslDefaultCredentials
{
public string mech;
public string realm;
public string authcid;
public string passwd;
public string authzid;
}which will usually only have mech and authcid set, with all the other strings set to NULL. |
||
| Marshal.StructureToPtr(defaults, ptrToDefaults, false); | ||
| try | ||
| { | ||
| return Interop.ldap_sasl_interactive_bind(_ldapHandle, null, Interop.KerberosDefaultMechanism, IntPtr.Zero, IntPtr.Zero, Interop.LDAP_SASL_QUIET, LdapPal.SaslInteractionProcedure, ptrToDefaults); | ||
| } | ||
| finally | ||
| { | ||
| GC.KeepAlive(defaults); //Making sure we keep it in scope as we will still use ptrToDefaults | ||
| Marshal.FreeHGlobal(ptrToDefaults); | ||
| } | ||
| } | ||
|
|
||
| private SaslDefaultCredentials GetSaslDefaults() | ||
| { | ||
| var defaults = new SaslDefaultCredentials { mech = Interop.KerberosDefaultMechanism }; | ||
| IntPtr outValue = IntPtr.Zero; | ||
| int error = Interop.ldap_get_option_ptr(_ldapHandle, LdapOption.LDAP_OPT_X_SASL_REALM, ref outValue); | ||
| if (error == 0 && outValue != IntPtr.Zero) | ||
| { | ||
| defaults.realm = Marshal.PtrToStringAnsi(outValue); | ||
| } | ||
| error = Interop.ldap_get_option_ptr(_ldapHandle, LdapOption.LDAP_OPT_X_SASL_AUTHCID, ref outValue); | ||
| if (error == 0 && outValue != IntPtr.Zero) | ||
| { | ||
| defaults.authcid = Marshal.PtrToStringAnsi(outValue); | ||
| } | ||
| error = Interop.ldap_get_option_ptr(_ldapHandle, LdapOption.LDAP_OPT_X_SASL_AUTHZID, ref outValue); | ||
| if (error == 0 && outValue != IntPtr.Zero) | ||
| { | ||
| defaults.authzid = Marshal.PtrToStringAnsi(outValue); | ||
| } | ||
| return defaults; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need to copy this to a local variable? can't you use interactPtr directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interact structure actually contains several interactchallenges which we loop in the while loop bellow. I guess we could just reuse interctPtr and reassign it but I opted to keep a current in case we ever needed to diagnose some issue with this and needed to know what was the original interactPtr that got passed in