internal static int GetSecInfoOption(ConnectionHandle ldapHandle, LdapOption option, SecurityPackageContextConnectionInformation outValue) => Interop.Ldap.ldap_get_option_secInfo(ldapHandle, option, outValue);
internal LdapConnection(LdapDirectoryIdentifier identifier, NetworkCredential credential, AuthType authType, IntPtr handle) { this.connectionAuthType = AuthType.Negotiate; this.automaticBind = true; this.needDispose = true; this.directoryIdentifier = identifier; this.ldapHandle = new ConnectionHandle(handle); this.directoryCredential = credential; this.connectionAuthType = authType; this.options = new LdapSessionOptions(this); this.needDispose = false; this.clientCertificateRoutine = new QUERYCLIENTCERT(this.ProcessClientCertificate); }
internal void Init() { string hostname = null; string[] servers = (directoryIdentifier == null ? null : ((LdapDirectoryIdentifier)directoryIdentifier).Servers); if (servers != null && servers.Length != 0) { StringBuilder temp = new StringBuilder(200); for (int i = 0; i < servers.Length; i++) { if (servers[i] != null) { temp.Append(servers[i]); if (i < servers.Length - 1) temp.Append(" "); } } if (temp.Length != 0) hostname = temp.ToString(); } // user wants to setup a connectionless session with server if (((LdapDirectoryIdentifier)directoryIdentifier).Connectionless == true) { ldapHandle = new ConnectionHandle(Wldap32.cldap_open(hostname, ((LdapDirectoryIdentifier)directoryIdentifier).PortNumber), needDispose); } else { ldapHandle = new ConnectionHandle(Wldap32.ldap_init(hostname, ((LdapDirectoryIdentifier)directoryIdentifier).PortNumber), needDispose); } // create a WeakReference object with the target of ldapHandle and put it into our handle table. lock (objectLock) { if (handleTable[ldapHandle.DangerousGetHandle()] != null) handleTable.Remove(ldapHandle.DangerousGetHandle()); handleTable.Add(ldapHandle.DangerousGetHandle(), new WeakReference(this)); } }
public static extern int ldap_bind_s(ConnectionHandle ldapHandle, string dn, SEC_WINNT_AUTH_IDENTITY_EX credentials, BindMethod method);
public static extern int ldap_extended_operation(ConnectionHandle ldapHandle, string oid, berval data, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber);
public static extern int ldap_search(ConnectionHandle ldapHandle, string dn, int scope, string filter, IntPtr attributes, bool attributeOnly, IntPtr servercontrol, IntPtr clientcontrol, int timelimit, int sizelimit, ref int messageNumber);
public static extern int ldap_set_option_referral(ConnectionHandle ldapHandle, LdapOption option, ref LdapReferralCallback outValue);
internal static int ParseExtendedResult(ConnectionHandle ldapHandle, IntPtr result, ref IntPtr oid, ref IntPtr data, byte freeIt) => Interop.Ldap.ldap_parse_extended_result(ldapHandle, result, ref oid, ref data, freeIt);
internal static int ParseReference(ConnectionHandle ldapHandle, IntPtr result, ref IntPtr referrals) => Interop.Ldap.ldap_parse_reference(ldapHandle, result, ref referrals);
internal static IntPtr GetNextEntryFromResult(ConnectionHandle ldapHandle, IntPtr result) => Interop.Ldap.ldap_next_entry(ldapHandle, result);
internal static IntPtr GetNextReferenceFromResult(ConnectionHandle ldapHandle, IntPtr result) => Interop.Ldap.ldap_next_reference(ldapHandle, result);
internal static IntPtr GetNextAttributeFromResult(ConnectionHandle ldapHandle, IntPtr result, IntPtr address) => Interop.Ldap.ldap_next_attribute(ldapHandle, result, address);
internal static int ModifyDirectoryEntry(ConnectionHandle ldapHandle, string dn, IntPtr attrs, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber) => Interop.Ldap.ldap_modify(ldapHandle, dn, attrs, servercontrol, clientcontrol, ref messageNumber);
internal static IntPtr GetValuesFromAttribute(ConnectionHandle ldapHandle, IntPtr result, string name) => Interop.Ldap.ldap_get_values_len(ldapHandle, result, name);
public static extern int ldap_parse_result_referral(ConnectionHandle ldapHandle, IntPtr result, IntPtr serverError, IntPtr dn, IntPtr message, ref IntPtr referral, IntPtr control, byte freeIt);
internal static int ParseResult(ConnectionHandle ldapHandle, IntPtr result, ref int serverError, ref IntPtr dn, ref IntPtr message, ref IntPtr referral, ref IntPtr control, byte freeIt) => Interop.Ldap.ldap_parse_result(ldapHandle, result, ref serverError, ref dn, ref message, ref referral, ref control, freeIt);
public static extern int ldap_result(ConnectionHandle ldapHandle, int messageId, int all, LDAP_TIMEVAL timeout, ref IntPtr Mesage);
internal static int ParseResultReferral(ConnectionHandle ldapHandle, IntPtr result, IntPtr serverError, IntPtr dn, IntPtr message, ref IntPtr referral, IntPtr control, byte freeIt) => Interop.Ldap.ldap_parse_result_referral(ldapHandle, result, serverError, dn, message, ref referral, control, freeIt);
public static extern int ldap_set_option_int(ConnectionHandle ldapHandle, LdapOption option, ref int inValue);
internal static int RenameDirectoryEntry(ConnectionHandle ldapHandle, string dn, string newRdn, string newParentDn, int deleteOldRdn, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber) => Interop.Ldap.ldap_rename(ldapHandle, dn, newRdn, newParentDn, deleteOldRdn, servercontrol, clientcontrol, ref messageNumber);
public static extern int ldap_simple_bind_s(ConnectionHandle ldapHandle, string distinguishedName, string password);
internal static int GetResultFromAsyncOperation(ConnectionHandle ldapHandle, int messageId, int all, LDAP_TIMEVAL timeout, ref IntPtr Message) => Interop.Ldap.ldap_result(ldapHandle, messageId, all, timeout, ref Message);
public static extern int ldap_connect(ConnectionHandle ldapHandle, LDAP_TIMEVAL timeout);
internal static int ResultToErrorCode(ConnectionHandle ldapHandle, IntPtr result, int freeIt) => Interop.Ldap.ldap_result2error(ldapHandle, result, freeIt);
protected virtual void Dispose(bool disposing) { if (disposing) { lock (LdapConnection.objectLock) { LdapConnection.handleTable.Remove(this.ldapHandle); } } if (this.needDispose && this.ldapHandle != null && !this.ldapHandle.IsInvalid) { this.ldapHandle.Dispose(); } this.ldapHandle = null; this.disposed = true; }
internal static int SearchDirectory(ConnectionHandle ldapHandle, string dn, int scope, string filter, IntPtr attributes, bool attributeOnly, IntPtr servercontrol, IntPtr clientcontrol, int timelimit, int sizelimit, ref int messageNumber) => Interop.Ldap.ldap_search(ldapHandle, dn, scope, filter, attributes, attributeOnly, servercontrol, clientcontrol, timelimit, sizelimit, ref messageNumber);
protected virtual void Dispose(bool disposing) { if (disposing) { // free other state (managed objects) // we need to remove the handle from the handle table lock (objectLock) { if (null != ldapHandle) { handleTable.Remove(ldapHandle.DangerousGetHandle()); } } } // free your own state (unmanaged objects) // close the ldap connection if (needDispose && ldapHandle != null && !ldapHandle.IsInvalid) ldapHandle.Dispose(); ldapHandle = null; disposed = true; Debug.WriteLine("Connection object is disposed\n"); }
internal static int SetClientCertOption(ConnectionHandle ldapHandle, LdapOption option, QUERYCLIENTCERT outValue) => Interop.Ldap.ldap_set_option_clientcert(ldapHandle, option, outValue);
public static extern byte ldap_stop_tls(ConnectionHandle ldapHandle);
internal static int SetIntOption(ConnectionHandle ld, LdapOption option, ref int inValue) => Interop.Ldap.ldap_set_option_int(ld, option, ref inValue);
public static extern int ldap_parse_reference(ConnectionHandle ldapHandle, IntPtr result, ref IntPtr referrals);
internal static int SetPtrOption(ConnectionHandle ldapHandle, LdapOption option, ref IntPtr inValue) => Interop.Ldap.ldap_set_option_ptr(ldapHandle, option, ref inValue);
public static extern int ldap_rename(ConnectionHandle ldapHandle, string dn, string newRdn, string newParentDn, int deleteOldRdn, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber);
internal static int SetReferralOption(ConnectionHandle ldapHandle, LdapOption option, ref LdapReferralCallback outValue) => Interop.Ldap.ldap_set_option_referral(ldapHandle, option, ref outValue);
public static extern int ldap_result2error(ConnectionHandle ldapHandle, IntPtr result, int freeIt);
internal static int SetServerCertOption(ConnectionHandle ldapHandle, LdapOption option, VERIFYSERVERCERT outValue) => Interop.Ldap.ldap_set_option_servercert(ldapHandle, option, outValue);
public static extern int ldap_set_option_clientcert(ConnectionHandle ldapHandle, LdapOption option, QUERYCLIENTCERT outValue);
internal static void CancelDirectoryAsyncOperation(ConnectionHandle ldapHandle, int messagId) => Interop.Ldap.ldap_abandon(ldapHandle, messagId);
public static extern int ldap_set_option_ptr(ConnectionHandle ldapHandle, LdapOption option, ref IntPtr inValue);
internal static int BindToDirectory(ConnectionHandle ld, string who, string passwd) => Interop.Ldap.ldap_simple_bind_s(ld, who, passwd);
public static extern int ldap_set_option_servercert(ConnectionHandle ldapHandle, LdapOption option, VERIFYSERVERCERT outValue);
internal static int StartTls(ConnectionHandle ldapHandle, ref int ServerReturnValue, ref IntPtr Message, IntPtr ServerControls, IntPtr ClientControls) => Interop.Ldap.ldap_start_tls(ldapHandle, ref ServerReturnValue, ref Message, ServerControls, ClientControls);
public static extern int ldap_abandon(ConnectionHandle ldapHandle, int messagId);
internal static byte StopTls(ConnectionHandle ldapHandle) => Interop.Ldap.ldap_stop_tls(ldapHandle);
public static extern int ldap_compare(ConnectionHandle ldapHandle, string dn, string attributeName, string strValue, berval binaryValue, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber);
public static extern int ldap_bind_s([In] ConnectionHandle ldapHandle, string dn, SEC_WINNT_AUTH_IDENTITY_EX credentials, BindMethod method);
public static extern int ldap_delete_ext(ConnectionHandle ldapHandle, string dn, IntPtr servercontrol, IntPtr clientcontrol, ref int messageNumber);
public static extern int ldap_connect([In] ConnectionHandle ldapHandle, LDAP_TIMEVAL timeout);
public static extern IntPtr ldap_first_attribute(ConnectionHandle ldapHandle, IntPtr result, ref IntPtr address);
public static extern int ldap_set_option_int([In] ConnectionHandle ldapHandle, [In] LdapOption option, ref int inValue);
internal void Init() { string[] servers; string str = null; if (this.directoryIdentifier == null) { servers = null; } else { servers = ((LdapDirectoryIdentifier)this.directoryIdentifier).Servers; } string[] strArrays = servers; if (strArrays != null && (int)strArrays.Length != 0) { StringBuilder stringBuilder = new StringBuilder(200); for (int i = 0; i < (int)strArrays.Length; i++) { if (strArrays[i] != null) { stringBuilder.Append(strArrays[i]); if (i < (int)strArrays.Length - 1) { stringBuilder.Append(" "); } } } if (stringBuilder.Length != 0) { str = stringBuilder.ToString(); } } if (!((LdapDirectoryIdentifier)this.directoryIdentifier).Connectionless) { this.ldapHandle = new ConnectionHandle(Wldap32.ldap_init(str, ((LdapDirectoryIdentifier)this.directoryIdentifier).PortNumber)); } else { this.ldapHandle = new ConnectionHandle(Wldap32.cldap_open(str, ((LdapDirectoryIdentifier)this.directoryIdentifier).PortNumber)); } lock (LdapConnection.objectLock) { if (LdapConnection.handleTable[this.ldapHandle] != null) { LdapConnection.handleTable.Remove(this.ldapHandle); } LdapConnection.handleTable.Add(this.ldapHandle, new WeakReference(this)); } }
public static extern int ldap_set_option_ptr([In] ConnectionHandle ldapHandle, [In] LdapOption option, ref IntPtr inValue);
private int ProcessQueryConnection(IntPtr PrimaryConnection, IntPtr ReferralFromConnection, IntPtr NewDNPtr, string HostName, int PortNumber, SEC_WINNT_AUTH_IDENTITY_EX SecAuthIdentity, Luid CurrentUserToken, ref ConnectionHandle ConnectionToUse) { ConnectionToUse = null; string stringUni = null; if (this.callbackRoutine.QueryForConnection == null) { return 1; } else { if (NewDNPtr != (IntPtr)0) { stringUni = Marshal.PtrToStringUni(NewDNPtr); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(HostName); stringBuilder.Append(":"); stringBuilder.Append(PortNumber); LdapDirectoryIdentifier ldapDirectoryIdentifier = new LdapDirectoryIdentifier(stringBuilder.ToString()); NetworkCredential networkCredential = this.ProcessSecAuthIdentity(SecAuthIdentity); LdapConnection ldapConnection = null; if (ReferralFromConnection != (IntPtr)0) { lock (LdapConnection.objectLock) { WeakReference item = (WeakReference)LdapConnection.handleTable[(object)ReferralFromConnection]; if (item == null || !item.IsAlive) { if (item != null) { LdapConnection.handleTable.Remove(ReferralFromConnection); } ldapConnection = new LdapConnection((LdapDirectoryIdentifier)this.connection.Directory, this.connection.GetCredential(), this.connection.AuthType, ReferralFromConnection); LdapConnection.handleTable.Add(ReferralFromConnection, new WeakReference(ldapConnection)); } else { ldapConnection = (LdapConnection)item.Target; } } } long lowPart = (long)CurrentUserToken.LowPart + ((long)CurrentUserToken.HighPart << 32); LdapConnection queryForConnection = this.callbackRoutine.QueryForConnection(this.connection, ldapConnection, stringUni, ldapDirectoryIdentifier, networkCredential, lowPart); if (queryForConnection != null) { ConnectionToUse = queryForConnection.ldapHandle; } return 0; } }
public static extern int ldap_get_option_sechandle([In] ConnectionHandle ldapHandle, [In] LdapOption option, ref SecurityHandle outValue);
internal LdapConnection(LdapDirectoryIdentifier identifier, NetworkCredential credential, AuthType authType, IntPtr handle) { directoryIdentifier = identifier; needDispose = false; ldapHandle = new ConnectionHandle(handle, needDispose); directoryCredential = credential; _connectionAuthType = authType; _options = new LdapSessionOptions(this); clientCertificateRoutine = new QUERYCLIENTCERT(ProcessClientCertificate); }
public static extern int ldap_get_option_secInfo([In] ConnectionHandle ldapHandle, [In] LdapOption option, [In, Out] SecurityPackageContextConnectionInformation outValue);
public static extern int ldap_start_tls(ConnectionHandle ldapHandle, ref int ServerReturnValue, ref IntPtr Message, IntPtr ServerControls, IntPtr ClientControls);
public static extern int ldap_parse_extended_result(ConnectionHandle ldapHandle, IntPtr result, ref IntPtr oid, ref IntPtr data, byte freeIt);
public static extern int ldap_create_sort_control(ConnectionHandle handle, IntPtr keys, byte critical, ref IntPtr control);
internal static int GetSecurityHandleOption(ConnectionHandle ldapHandle, LdapOption option, ref SecurityHandle outValue) => Interop.Ldap.ldap_get_option_sechandle(ldapHandle, option, ref outValue);