123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- #region Apache License
- #endregion
- #if !NETCF
- #if !MONO
- #if !SSCLI
- #if !CLI_1_0
- using System;
- using System.Runtime.InteropServices;
- using System.Security.Principal;
- using System.Security.Permissions;
- using log4net.Core;
- namespace log4net.Util
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class WindowsSecurityContext : SecurityContext, IOptionHandler
- {
-
-
-
-
-
-
-
-
-
- public enum ImpersonationMode
- {
-
-
-
- User,
-
-
-
- Process
- }
- #region Member Variables
- private ImpersonationMode m_impersonationMode = ImpersonationMode.User;
- private string m_userName;
- private string m_domainName = Environment.MachineName;
- private string m_password;
- private WindowsIdentity m_identity;
- #endregion
- #region Constructor
-
-
-
-
-
-
-
-
- public WindowsSecurityContext()
- {
- }
- #endregion
- #region Public Properties
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public ImpersonationMode Credentials
- {
- get { return m_impersonationMode; }
- set { m_impersonationMode = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public string UserName
- {
- get { return m_userName; }
- set { m_userName = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string DomainName
- {
- get { return m_domainName; }
- set { m_domainName = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public string Password
- {
- set { m_password = value; }
- }
- #endregion
- #region IOptionHandler Members
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void ActivateOptions()
- {
- if (m_impersonationMode == ImpersonationMode.User)
- {
- if (m_userName == null) throw new ArgumentNullException("m_userName");
- if (m_domainName == null) throw new ArgumentNullException("m_domainName");
- if (m_password == null) throw new ArgumentNullException("m_password");
- m_identity = LogonUser(m_userName, m_domainName, m_password);
- }
- }
- #endregion
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override IDisposable Impersonate(object state)
- {
- if (m_impersonationMode == ImpersonationMode.User)
- {
- if (m_identity != null)
- {
- return new DisposableImpersonationContext(m_identity.Impersonate());
- }
- }
- else if (m_impersonationMode == ImpersonationMode.Process)
- {
-
- return new DisposableImpersonationContext(WindowsIdentity.Impersonate(IntPtr.Zero));
- }
- return null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if NET_4_0 || MONO_4_0
- [System.Security.SecuritySafeCritical]
- #endif
- [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, UnmanagedCode = true)]
- private static WindowsIdentity LogonUser(string userName, string domainName, string password)
- {
- const int LOGON32_PROVIDER_DEFAULT = 0;
-
- const int LOGON32_LOGON_INTERACTIVE = 2;
-
- IntPtr tokenHandle = IntPtr.Zero;
- if(!LogonUser(userName, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle))
- {
- NativeError error = NativeError.GetLastError();
- throw new Exception("Failed to LogonUser ["+userName+"] in Domain ["+domainName+"]. Error: "+ error.ToString());
- }
- const int SecurityImpersonation = 2;
- IntPtr dupeTokenHandle = IntPtr.Zero;
- if(!DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle))
- {
- NativeError error = NativeError.GetLastError();
- if (tokenHandle != IntPtr.Zero)
- {
- CloseHandle(tokenHandle);
- }
- throw new Exception("Failed to DuplicateToken after LogonUser. Error: " + error.ToString());
- }
- WindowsIdentity identity = new WindowsIdentity(dupeTokenHandle);
-
- if (dupeTokenHandle != IntPtr.Zero)
- {
- CloseHandle(dupeTokenHandle);
- }
- if (tokenHandle != IntPtr.Zero)
- {
- CloseHandle(tokenHandle);
- }
- return identity;
- }
- #region Native Method Stubs
- [DllImport("advapi32.dll", SetLastError=true)]
- private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
- [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
- private extern static bool CloseHandle(IntPtr handle);
- [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
- private extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
- #endregion
- #region DisposableImpersonationContext class
-
-
-
-
-
-
-
-
-
- private sealed class DisposableImpersonationContext : IDisposable
- {
- private readonly WindowsImpersonationContext m_impersonationContext;
-
-
-
-
-
-
-
-
-
- public DisposableImpersonationContext(WindowsImpersonationContext impersonationContext)
- {
- m_impersonationContext = impersonationContext;
- }
-
-
-
-
-
-
-
-
- public void Dispose()
- {
- m_impersonationContext.Undo();
- }
- }
- #endregion
- }
- }
- #endif // !CLI_1_0
- #endif // !SSCLI
- #endif // !MONO
- #endif // !NETCF
|