Пример #1
0
        private bool ProcessNotifyConnection(IntPtr PrimaryConnection, IntPtr ReferralFromConnection, IntPtr NewDNPtr, string HostName, IntPtr NewConnection, int PortNumber, SEC_WINNT_AUTH_IDENTITY_EX SecAuthIdentity, Luid CurrentUser, int ErrorCodeFromBind)
        {
            string NewDN = null;
            if (NewConnection != (IntPtr)0 && _callbackRoutine.NotifyNewConnection != null)
            {
                if (NewDNPtr != (IntPtr)0)
                    NewDN = Marshal.PtrToStringUni(NewDNPtr);
                StringBuilder target = new StringBuilder();
                target.Append(HostName);
                target.Append(":");
                target.Append(PortNumber);
                LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(target.ToString());
                NetworkCredential cred = ProcessSecAuthIdentity(SecAuthIdentity);
                LdapConnection tempNewConnection = null;
                LdapConnection tempReferralConnection = null;
                WeakReference reference = null;

                lock (LdapConnection.objectLock)
                {
                    // if referrafromconnection handle is valid
                    if (ReferralFromConnection != (IntPtr)0)
                    {
                        //check whether we have save it in the handle table before
                        reference = (WeakReference)(LdapConnection.handleTable[ReferralFromConnection]);
                        if (reference != null && reference.IsAlive && null != ((LdapConnection)reference.Target).ldapHandle)
                        {
                            // save this before and object has not been garbage collected yet.
                            tempReferralConnection = (LdapConnection)reference.Target;
                        }
                        else
                        {
                            // connection has been garbage collected, we need to remove this one
                            if (reference != null)
                                LdapConnection.handleTable.Remove(ReferralFromConnection);

                            // we don't have it yet, construct a new one
                            tempReferralConnection = new LdapConnection(((LdapDirectoryIdentifier)(_connection.Directory)), _connection.GetCredential(), _connection.AuthType, ReferralFromConnection);
                            // save it to the handle table
                            LdapConnection.handleTable.Add(ReferralFromConnection, new WeakReference(tempReferralConnection));
                        }
                    }

                    if (NewConnection != (IntPtr)0)
                    {
                        //check whether we have save it in the handle table before
                        reference = (WeakReference)(LdapConnection.handleTable[NewConnection]);
                        if (reference != null && reference.IsAlive && null != ((LdapConnection)reference.Target).ldapHandle)
                        {
                            // save this before and object has not been garbage collected yet.
                            tempNewConnection = (LdapConnection)reference.Target;
                        }
                        else
                        {
                            // connection has been garbage collected, we need to remove this one
                            if (reference != null)
                                LdapConnection.handleTable.Remove(NewConnection);

                            // we don't have it yet, construct a new one
                            tempNewConnection = new LdapConnection(identifier, cred, _connection.AuthType, NewConnection);
                            // save it to the handle table
                            LdapConnection.handleTable.Add(NewConnection, new WeakReference(tempNewConnection));
                        }
                    }
                }
                long tokenValue = (long)((uint)CurrentUser.LowPart + (((long)CurrentUser.HighPart) << 32));

                bool value = _callbackRoutine.NotifyNewConnection(_connection, tempReferralConnection, NewDN, identifier, tempNewConnection, cred, tokenValue, ErrorCodeFromBind);

                if (value)
                {
                    value = AddLdapHandleRef(tempNewConnection);
                    if (value)
                    {
                        tempNewConnection.NeedDispose = true;
                    }
                }
                return value;
            }
            else
            {
                return false;
            }
        }
Пример #2
0
        private int ProcessQueryConnection(IntPtr PrimaryConnection, IntPtr ReferralFromConnection, IntPtr NewDNPtr, string HostName, int PortNumber, SEC_WINNT_AUTH_IDENTITY_EX SecAuthIdentity, Luid CurrentUserToken, ref IntPtr ConnectionToUse)
        {
            ConnectionToUse = IntPtr.Zero;
            string NewDN = null;

            // user must have registered callback function
            Debug.Assert(_callbackRoutine.QueryForConnection != null);

            // user registers the QUERYFORCONNECTION callback
            if (_callbackRoutine.QueryForConnection != null)
            {
                if (NewDNPtr != (IntPtr)0)
                    NewDN = Marshal.PtrToStringUni(NewDNPtr);
                StringBuilder target = new StringBuilder();
                target.Append(HostName);
                target.Append(":");
                target.Append(PortNumber);
                LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(target.ToString());
                NetworkCredential cred = ProcessSecAuthIdentity(SecAuthIdentity);
                LdapConnection tempReferralConnection = null;
                WeakReference reference = null;

                // if referrafromconnection handle is valid
                if (ReferralFromConnection != (IntPtr)0)
                {
                    lock (LdapConnection.objectLock)
                    {
                        //make sure first whether we have saved it in the handle table before
                        reference = (WeakReference)(LdapConnection.handleTable[ReferralFromConnection]);
                        if (reference != null && reference.IsAlive)
                        {
                            // save this before and object has not been garbage collected yet.
                            tempReferralConnection = (LdapConnection)reference.Target;
                        }
                        else
                        {
                            if (reference != null)
                            {
                                // connection has been garbage collected, we need to remove this one
                                LdapConnection.handleTable.Remove(ReferralFromConnection);
                            }
                            // we don't have it yet, construct a new one
                            tempReferralConnection = new LdapConnection(((LdapDirectoryIdentifier)(_connection.Directory)), _connection.GetCredential(), _connection.AuthType, ReferralFromConnection);

                            // save it to the handle table
                            LdapConnection.handleTable.Add(ReferralFromConnection, new WeakReference(tempReferralConnection));
                        }
                    }
                }

                long tokenValue = (long)((uint)CurrentUserToken.LowPart + (((long)CurrentUserToken.HighPart) << 32));

                LdapConnection con = _callbackRoutine.QueryForConnection(_connection, tempReferralConnection, NewDN, identifier, cred, tokenValue);
                if (null != con && null != con.ldapHandle && !con.ldapHandle.IsInvalid)
                {
                    bool success = AddLdapHandleRef(con);
                    if (success)
                    {
                        ConnectionToUse = con.ldapHandle.DangerousGetHandle();
                    }
                }
                return 0;
            }
            else
            {
                // user does not take ownership of the connection
                return 1;
            }
        }
Пример #3
0
		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;
			}
		}
Пример #4
0
		private bool ProcessNotifyConnection(IntPtr PrimaryConnection, IntPtr ReferralFromConnection, IntPtr NewDNPtr, string HostName, IntPtr NewConnection, int PortNumber, SEC_WINNT_AUTH_IDENTITY_EX SecAuthIdentity, Luid CurrentUser, int ErrorCodeFromBind)
		{
			WeakReference item;
			string stringUni = null;
			if (!(NewConnection != (IntPtr)0) || this.callbackRoutine.NotifyNewConnection == null)
			{
				return false;
			}
			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;
				LdapConnection target = null;
				lock (LdapConnection.objectLock)
				{
					if (ReferralFromConnection != (IntPtr)0)
					{
						item = (WeakReference)LdapConnection.handleTable[(object)ReferralFromConnection];
						if (item == null || !item.IsAlive)
						{
							if (item != null)
							{
								LdapConnection.handleTable.Remove(ReferralFromConnection);
							}
							target = new LdapConnection((LdapDirectoryIdentifier)this.connection.Directory, this.connection.GetCredential(), this.connection.AuthType, ReferralFromConnection);
							LdapConnection.handleTable.Add(ReferralFromConnection, new WeakReference(target));
						}
						else
						{
							target = (LdapConnection)item.Target;
						}
					}
					if (NewConnection != (IntPtr)0)
					{
						item = (WeakReference)LdapConnection.handleTable[(object)NewConnection];
						if (item == null || !item.IsAlive)
						{
							if (item != null)
							{
								LdapConnection.handleTable.Remove(NewConnection);
							}
							ldapConnection = new LdapConnection(ldapDirectoryIdentifier, networkCredential, this.connection.AuthType, NewConnection);
							LdapConnection.handleTable.Add(NewConnection, new WeakReference(ldapConnection));
						}
						else
						{
							ldapConnection = (LdapConnection)item.Target;
						}
					}
				}
				long lowPart = (long)CurrentUser.LowPart + ((long)CurrentUser.HighPart << 32);
				bool errorCodeFromBind = this.callbackRoutine.NotifyNewConnection(this.connection, target, stringUni, ldapDirectoryIdentifier, ldapConnection, networkCredential, lowPart, ErrorCodeFromBind);
				if (errorCodeFromBind)
				{
					ldapConnection.needDispose = true;
				}
				return errorCodeFromBind;
			}
		}
 private int ProcessQueryConnection(IntPtr PrimaryConnection, IntPtr ReferralFromConnection, IntPtr NewDNPtr, string HostName, int PortNumber, SEC_WINNT_AUTH_IDENTITY_EX SecAuthIdentity, Luid CurrentUserToken, ref IntPtr ConnectionToUse)
 {
     ConnectionToUse = IntPtr.Zero;
     string newDistinguishedName = null;
     if (this.callbackRoutine.QueryForConnection == null)
     {
         return 1;
     }
     if (NewDNPtr != IntPtr.Zero)
     {
         newDistinguishedName = Marshal.PtrToStringUni(NewDNPtr);
     }
     StringBuilder builder = new StringBuilder();
     builder.Append(HostName);
     builder.Append(":");
     builder.Append(PortNumber);
     LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(builder.ToString());
     NetworkCredential credential = this.ProcessSecAuthIdentity(SecAuthIdentity);
     LdapConnection target = null;
     WeakReference reference = null;
     if (ReferralFromConnection != IntPtr.Zero)
     {
         lock (LdapConnection.objectLock)
         {
             reference = (WeakReference) LdapConnection.handleTable[ReferralFromConnection];
             if ((reference != null) && reference.IsAlive)
             {
                 target = (LdapConnection) reference.Target;
             }
             else
             {
                 if (reference != null)
                 {
                     LdapConnection.handleTable.Remove(ReferralFromConnection);
                 }
                 target = new LdapConnection((LdapDirectoryIdentifier) this.connection.Directory, this.connection.GetCredential(), this.connection.AuthType, ReferralFromConnection);
                 LdapConnection.handleTable.Add(ReferralFromConnection, new WeakReference(target));
             }
         }
     }
     long currentUserToken = ((long) ((ulong) CurrentUserToken.LowPart)) + (CurrentUserToken.HighPart << 0x20);
     LdapConnection connection2 = this.callbackRoutine.QueryForConnection(this.connection, target, newDistinguishedName, identifier, credential, currentUserToken);
     if (connection2 != null)
     {
         ConnectionToUse = connection2.ldapHandle;
     }
     return 0;
 }
 private bool ProcessNotifyConnection(IntPtr PrimaryConnection, IntPtr ReferralFromConnection, IntPtr NewDNPtr, string HostName, IntPtr NewConnection, int PortNumber, SEC_WINNT_AUTH_IDENTITY_EX SecAuthIdentity, Luid CurrentUser, int ErrorCodeFromBind)
 {
     string newDistinguishedName = null;
     if (!(NewConnection != IntPtr.Zero) || (this.callbackRoutine.NotifyNewConnection == null))
     {
         return false;
     }
     if (NewDNPtr != IntPtr.Zero)
     {
         newDistinguishedName = Marshal.PtrToStringUni(NewDNPtr);
     }
     StringBuilder builder = new StringBuilder();
     builder.Append(HostName);
     builder.Append(":");
     builder.Append(PortNumber);
     LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(builder.ToString());
     NetworkCredential credential = this.ProcessSecAuthIdentity(SecAuthIdentity);
     LdapConnection target = null;
     LdapConnection connection2 = null;
     WeakReference reference = null;
     lock (LdapConnection.objectLock)
     {
         if (ReferralFromConnection != IntPtr.Zero)
         {
             reference = (WeakReference) LdapConnection.handleTable[ReferralFromConnection];
             if ((reference != null) && reference.IsAlive)
             {
                 connection2 = (LdapConnection) reference.Target;
             }
             else
             {
                 if (reference != null)
                 {
                     LdapConnection.handleTable.Remove(ReferralFromConnection);
                 }
                 connection2 = new LdapConnection((LdapDirectoryIdentifier) this.connection.Directory, this.connection.GetCredential(), this.connection.AuthType, ReferralFromConnection);
                 LdapConnection.handleTable.Add(ReferralFromConnection, new WeakReference(connection2));
             }
         }
         if (NewConnection != IntPtr.Zero)
         {
             reference = (WeakReference) LdapConnection.handleTable[NewConnection];
             if ((reference != null) && reference.IsAlive)
             {
                 target = (LdapConnection) reference.Target;
             }
             else
             {
                 if (reference != null)
                 {
                     LdapConnection.handleTable.Remove(NewConnection);
                 }
                 target = new LdapConnection(identifier, credential, this.connection.AuthType, NewConnection);
                 LdapConnection.handleTable.Add(NewConnection, new WeakReference(target));
             }
         }
     }
     long currentUserToken = ((long) ((ulong) CurrentUser.LowPart)) + (CurrentUser.HighPart << 0x20);
     bool flag = this.callbackRoutine.NotifyNewConnection(this.connection, connection2, newDistinguishedName, identifier, target, credential, currentUserToken, ErrorCodeFromBind);
     if (flag)
     {
         target.needDispose = true;
     }
     return flag;
 }