Example #1
0
 internal static extern int LsaCallAuthenticationPackage(
     [In] LsaLogonProcessSafeHandle lsaHandle,
     [In] int authenticationPackage,
     [In] NegotiateCallerNameRequest protocolSubmitBuffer,
     [In] int submitBufferLength,
     [Out] out IntPtr protocolReturnBuffer,
     [Out] out int returnBufferLength,
     [Out] out int protocolStatus);
Example #2
0
        internal static string GetLoggedOnDomain()
        {
            string domainName = null;

            NegotiateCallerNameRequest requestBuffer = new NegotiateCallerNameRequest();
            int requestBufferLength = (int)Marshal.SizeOf(requestBuffer);

            IntPtr pResponseBuffer = IntPtr.Zero;
            NegotiateCallerNameResponse responseBuffer = new NegotiateCallerNameResponse();
            int responseBufferLength;
            int protocolStatus;
            int result;

            LsaLogonProcessSafeHandle lsaHandle;

            //
            // since we are using safe handles, we don't need to explicitly call NativeMethods.LsaDeregisterLogonProcess(lsaHandle)
            //
            result = NativeMethods.LsaConnectUntrusted(out lsaHandle);

            if (result == 0)
            {
                //
                // initialize the request buffer
                //
                requestBuffer.messageType = NativeMethods.NegGetCallerName;

                result = NativeMethods.LsaCallAuthenticationPackage(lsaHandle, 0, requestBuffer, requestBufferLength, out pResponseBuffer, out responseBufferLength, out protocolStatus);

                try
                {
                    if (result == 0 && protocolStatus == 0)
                    {
                        Marshal.PtrToStructure(pResponseBuffer, responseBuffer);

                        //
                        // callerName is of the form domain\username
                        //
                        Debug.Assert((responseBuffer.callerName != null), "NativeMethods.LsaCallAuthenticationPackage returned null callerName.");
                        int index = responseBuffer.callerName.IndexOf('\\');
                        Debug.Assert((index != -1), "NativeMethods.LsaCallAuthenticationPackage returned callerName not in domain\\username format.");
                        domainName = responseBuffer.callerName.Substring(0, index);
                    }
                    else
                    {
                        if (result == NativeMethods.STATUS_QUOTA_EXCEEDED)
                        {
                            throw new OutOfMemoryException();
                        }
                        else if ((result == 0) && (UnsafeNativeMethods.LsaNtStatusToWinError(protocolStatus) == NativeMethods.ERROR_NO_SUCH_LOGON_SESSION))
                        {
                            // If this is a directory user, extract domain info from username
                            if (!Utils.IsSamUser())
                            {
                                WindowsIdentity identity = WindowsIdentity.GetCurrent();

                                int index = identity.Name.IndexOf('\\');
                                Debug.Assert(index != -1);
                                domainName = identity.Name.Substring(0, index);
                            }
                        }
                        else
                        {
                            throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError((result != 0) ? result : protocolStatus));
                        }
                    }
                }
                finally
                {
                    if (pResponseBuffer != IntPtr.Zero)
                    {
                        NativeMethods.LsaFreeReturnBuffer(pResponseBuffer);
                    }
                }
            }
            else if (result == NativeMethods.STATUS_QUOTA_EXCEEDED)
            {
                throw new OutOfMemoryException();
            }
            else
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(result));
            }

            // If we're running as a local user (i.e. NT AUTHORITY\LOCAL SYSTEM, IIS APPPOOL\APPPoolIdentity, etc.),
            // domainName will be null and we fall back to the machine's domain
            domainName = GetDnsDomainName(domainName);

            if (domainName == null)
            {
                //
                // we should never get to this point here since we should have already verified that the context is valid 
                // by the time we get to this point
                //
                throw new ActiveDirectoryOperationException(Res.GetString(Res.ContextNotAssociatedWithDomain));
            }

            return domainName;
        }
        internal static string GetLoggedOnDomain()
        {
            string domainName = null;

            NegotiateCallerNameRequest requestBuffer = new NegotiateCallerNameRequest();
            int requestBufferLength = (int)Marshal.SizeOf(requestBuffer);

            IntPtr pResponseBuffer = IntPtr.Zero;
            NegotiateCallerNameResponse responseBuffer = new NegotiateCallerNameResponse();
            int responseBufferLength;
            int protocolStatus;
            int result;

            LsaLogonProcessSafeHandle lsaHandle;

            //
            // since we are using safe handles, we don't need to explicitly call NativeMethods.LsaDeregisterLogonProcess(lsaHandle)
            //
            result = NativeMethods.LsaConnectUntrusted(out lsaHandle);

            if (result == 0)
            {
                //
                // initialize the request buffer
                //
                requestBuffer.messageType = NativeMethods.NegGetCallerName;

                result = NativeMethods.LsaCallAuthenticationPackage(lsaHandle, 0, requestBuffer, requestBufferLength, out pResponseBuffer, out responseBufferLength, out protocolStatus);

                try
                {
                    if (result == 0 && protocolStatus == 0)
                    {
                        Marshal.PtrToStructure(pResponseBuffer, responseBuffer);

                        //
                        // callerName is of the form domain\username
                        //
                        Debug.Assert((responseBuffer.callerName != null), "NativeMethods.LsaCallAuthenticationPackage returned null callerName.");
                        int index = responseBuffer.callerName.IndexOf('\\');
                        Debug.Assert((index != -1), "NativeMethods.LsaCallAuthenticationPackage returned callerName not in domain\\username format.");
                        domainName = responseBuffer.callerName.Substring(0, index);
                    }
                    else
                    {
                        if (result == NativeMethods.STATUS_QUOTA_EXCEEDED)
                        {
                            throw new OutOfMemoryException();
                        }
                        else if ((result == 0) && (UnsafeNativeMethods.LsaNtStatusToWinError(protocolStatus) == NativeMethods.ERROR_NO_SUCH_LOGON_SESSION))
                        {
                            // If this is a directory user, extract domain info from username
                            if (!Utils.IsSamUser())
                            {
                                WindowsIdentity identity = WindowsIdentity.GetCurrent();

                                int index = identity.Name.IndexOf('\\');
                                Debug.Assert(index != -1);
                                domainName = identity.Name.Substring(0, index);
                            }
                        }
                        else
                        {
                            throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError((result != 0) ? result : protocolStatus));
                        }
                    }
                }
                finally
                {
                    if (pResponseBuffer != IntPtr.Zero)
                    {
                        NativeMethods.LsaFreeReturnBuffer(pResponseBuffer);
                    }
                }
            }
            else if (result == NativeMethods.STATUS_QUOTA_EXCEEDED)
            {
                throw new OutOfMemoryException();
            }
            else
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(result));
            }

            // If we're running as a local user (i.e. NT AUTHORITY\LOCAL SYSTEM, IIS APPPOOL\APPPoolIdentity, etc.),
            // domainName will be null and we fall back to the machine's domain
            domainName = GetDnsDomainName(domainName);

            if (domainName == null)
            {
                //
                // we should never get to this point here since we should have already verified that the context is valid
                // by the time we get to this point
                //
                throw new ActiveDirectoryOperationException(SR.ContextNotAssociatedWithDomain);
            }

            return(domainName);
        }
Example #4
0
        internal static string GetLoggedOnDomain()
        {
            int num  = 0;
            int num1 = 0;
            LsaLogonProcessSafeHandle lsaLogonProcessSafeHandle = null;
            int    num2;
            string dnsDomainName = null;
            NegotiateCallerNameRequest negotiateCallerNameRequest = new NegotiateCallerNameRequest();
            int    num3 = Marshal.SizeOf(negotiateCallerNameRequest);
            IntPtr zero = IntPtr.Zero;
            NegotiateCallerNameResponse negotiateCallerNameResponse = new NegotiateCallerNameResponse();
            int num4 = NativeMethods.LsaConnectUntrusted(out lsaLogonProcessSafeHandle);

            if (num4 != 0)
            {
                if (num4 != -1073741756)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(num4));
                }
                else
                {
                    throw new OutOfMemoryException();
                }
            }
            else
            {
                negotiateCallerNameRequest.messageType = 1;
                num4 = NativeMethods.LsaCallAuthenticationPackage(lsaLogonProcessSafeHandle, 0, negotiateCallerNameRequest, num3, out zero, out num, out num1);
                try
                {
                    if (num4 != 0 || num1 != 0)
                    {
                        if (num4 != -1073741756)
                        {
                            if (num4 != 0 || UnsafeNativeMethods.LsaNtStatusToWinError(num1) != 0x520)
                            {
                                if (num4 != 0)
                                {
                                    num2 = num4;
                                }
                                else
                                {
                                    num2 = num1;
                                }
                                throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(num2));
                            }
                            else
                            {
                                if (!Utils.IsSamUser())
                                {
                                    WindowsIdentity current = WindowsIdentity.GetCurrent();
                                    int             num5    = current.Name.IndexOf('\\');
                                    dnsDomainName = current.Name.Substring(0, num5);
                                }
                            }
                        }
                        else
                        {
                            throw new OutOfMemoryException();
                        }
                    }
                    else
                    {
                        Marshal.PtrToStructure(zero, negotiateCallerNameResponse);
                        int num6 = negotiateCallerNameResponse.callerName.IndexOf('\\');
                        dnsDomainName = negotiateCallerNameResponse.callerName.Substring(0, num6);
                    }
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        NativeMethods.LsaFreeReturnBuffer(zero);
                    }
                }
                dnsDomainName = DirectoryContext.GetDnsDomainName(dnsDomainName);
                if (dnsDomainName != null)
                {
                    return(dnsDomainName);
                }
                else
                {
                    throw new ActiveDirectoryOperationException(Res.GetString("ContextNotAssociatedWithDomain"));
                }
            }
        }
        internal static string GetLoggedOnDomain()
        {
            string dnsDomainName = null;
            LsaLogonProcessSafeHandle  handle;
            NegotiateCallerNameRequest structure = new NegotiateCallerNameRequest();
            int    submitBufferLength            = Marshal.SizeOf(structure);
            IntPtr zero = IntPtr.Zero;
            NegotiateCallerNameResponse response = new NegotiateCallerNameResponse();
            int status = System.DirectoryServices.ActiveDirectory.NativeMethods.LsaConnectUntrusted(out handle);

            switch (status)
            {
            case 0:
                int num2;
                int num3;
                structure.messageType = 1;
                status = System.DirectoryServices.ActiveDirectory.NativeMethods.LsaCallAuthenticationPackage(handle, 0, structure, submitBufferLength, out zero, out num2, out num3);
                try
                {
                    if ((status != 0) || (num3 != 0))
                    {
                        if (status == -1073741756)
                        {
                            throw new OutOfMemoryException();
                        }
                        if ((status != 0) || (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LsaNtStatusToWinError(num3) != 0x520))
                        {
                            throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LsaNtStatusToWinError((status != 0) ? status : num3));
                        }
                        WindowsIdentity current = WindowsIdentity.GetCurrent();
                        int             index   = current.Name.IndexOf('\\');
                        dnsDomainName = current.Name.Substring(0, index);
                    }
                    else
                    {
                        Marshal.PtrToStructure(zero, response);
                        int length = response.callerName.IndexOf('\\');
                        dnsDomainName = response.callerName.Substring(0, length);
                    }
                    if ((dnsDomainName != null) && (Utils.Compare(dnsDomainName, Utils.GetNtAuthorityString()) == 0))
                    {
                        dnsDomainName = GetDnsDomainName(null);
                    }
                    else
                    {
                        dnsDomainName = GetDnsDomainName(dnsDomainName);
                    }
                    if (dnsDomainName == null)
                    {
                        throw new ActiveDirectoryOperationException(Res.GetString("ContextNotAssociatedWithDomain"));
                    }
                    return(dnsDomainName);
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        System.DirectoryServices.ActiveDirectory.NativeMethods.LsaFreeReturnBuffer(zero);
                    }
                }
                break;

            case -1073741756:
                throw new OutOfMemoryException();
            }
            throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LsaNtStatusToWinError(status));
        }
Example #6
0
		internal static string GetLoggedOnDomain()
		{
			int num = 0;
			int num1 = 0;
			LsaLogonProcessSafeHandle lsaLogonProcessSafeHandle = null;
			int num2;
			string dnsDomainName = null;
			NegotiateCallerNameRequest negotiateCallerNameRequest = new NegotiateCallerNameRequest();
			int num3 = Marshal.SizeOf(negotiateCallerNameRequest);
			IntPtr zero = IntPtr.Zero;
			NegotiateCallerNameResponse negotiateCallerNameResponse = new NegotiateCallerNameResponse();
			int num4 = NativeMethods.LsaConnectUntrusted(out lsaLogonProcessSafeHandle);
			if (num4 != 0)
			{
				if (num4 != -1073741756)
				{
					throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(num4));
				}
				else
				{
					throw new OutOfMemoryException();
				}
			}
			else
			{
				negotiateCallerNameRequest.messageType = 1;
				num4 = NativeMethods.LsaCallAuthenticationPackage(lsaLogonProcessSafeHandle, 0, negotiateCallerNameRequest, num3, out zero, out num, out num1);
				try
				{
					if (num4 != 0 || num1 != 0)
					{
						if (num4 != -1073741756)
						{
							if (num4 != 0 || UnsafeNativeMethods.LsaNtStatusToWinError(num1) != 0x520)
							{
								if (num4 != 0)
								{
									num2 = num4;
								}
								else
								{
									num2 = num1;
								}
								throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(num2));
							}
							else
							{
								if (!Utils.IsSamUser())
								{
									WindowsIdentity current = WindowsIdentity.GetCurrent();
									int num5 = current.Name.IndexOf('\\');
									dnsDomainName = current.Name.Substring(0, num5);
								}
							}
						}
						else
						{
							throw new OutOfMemoryException();
						}
					}
					else
					{
						Marshal.PtrToStructure(zero, negotiateCallerNameResponse);
						int num6 = negotiateCallerNameResponse.callerName.IndexOf('\\');
						dnsDomainName = negotiateCallerNameResponse.callerName.Substring(0, num6);
					}
				}
				finally
				{
					if (zero != IntPtr.Zero)
					{
						NativeMethods.LsaFreeReturnBuffer(zero);
					}
				}
				dnsDomainName = DirectoryContext.GetDnsDomainName(dnsDomainName);
				if (dnsDomainName != null)
				{
					return dnsDomainName;
				}
				else
				{
					throw new ActiveDirectoryOperationException(Res.GetString("ContextNotAssociatedWithDomain"));
				}
			}
		}