Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.4 KiB

  1. //=================================================================
  2. //
  3. // ImpLogonUser.H -- Class to perform impersonation of logged on user.
  4. //
  5. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. // Revisions: 09/09/97 a-sanjes Created
  8. //
  9. //=================================================================
  10. #ifndef __IMPLOGONUSER_H__
  11. #define __IMPLOGONUSER_H__
  12. //////////////////////////////////////////////////////////////////////////////
  13. //
  14. // ImpLogonUser.H - Class definition of CImpersonateLoggedOnUser.
  15. //
  16. // This class is intended to provide a way for a process to identify the shell
  17. // process on a Windows NT system, and using the access token of that process
  18. // to attempt to impersonate the user logged onto the Interactive Desktop of
  19. // a workstation.
  20. //
  21. // To use this class, simply construct it, and call the Begin() function. If
  22. // it succeeds, you may then access information that would otherwise not be
  23. // available to your process (such as network connection info). When you are
  24. // finished, call End() to clear out the class.
  25. //
  26. // Caveats:
  27. // 1> This class is NOT thread safe, so don't share it across multiple
  28. // threads! Besides, ImpersonateLoggedOnUser() is only good for the thread
  29. // it was called on.
  30. // 2> If multiple instances of the Shell process are running, this method
  31. // may or may not be accurate. It will probably work in a large percentage
  32. // of cases however.
  33. // 3> Multiple logged on users will cause problems for this code (see #2).
  34. // 4> This class may need to be optimized for speed, as it currently makes no
  35. // use of caches and "redicovers" the shell process each time an instance
  36. // is implemented.
  37. // 5> PSAPI.DLL must be available.
  38. //
  39. //////////////////////////////////////////////////////////////////////////////
  40. #ifdef NTONLY
  41. #include "wbempsapi.h"
  42. // String Constants
  43. // Resides under HKEY_LOCAL_MACHINE
  44. #define WINNT_WINLOGON_KEY _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon")
  45. #define WINNT_SHELL_VALUE _T("Shell")
  46. // Memory allocation definitions
  47. #define PROCESSID_ARRAY_BLOCKSIZE 1024
  48. #define HMODULE_ARRAY_BLOCKSIZE 1024
  49. class CImpersonateLoggedOnUser
  50. {
  51. public:
  52. CImpersonateLoggedOnUser();
  53. ~CImpersonateLoggedOnUser();
  54. // User Interface
  55. BOOL Begin( void );
  56. BOOL End( void );
  57. // inlines
  58. BOOL IsImpersonatingUser( void );
  59. protected:
  60. private:
  61. // Helpers for identifying the shell process and locating it
  62. BOOL LoadShellName( LPTSTR pszShellName, DWORD cbShellNameBuffer );
  63. BOOL FindShellProcess( LPCTSTR pszShellProcessName );
  64. BOOL FindShellModuleInProcess( LPCTSTR pszShellName, HANDLE hProcess, HMODULE*& phModules, DWORD& dwModuleArraySize, CPSAPI *a_psapi );
  65. bool GetCurrentProcessSid(CSid& csidCurrentProcess);
  66. DWORD AdjustSecurityDescriptorOfImpersonatedToken(CSid& csidSidOfCurrentProcess);
  67. // Perform actual impersonation and revert
  68. BOOL ImpersonateUser( void );
  69. BOOL Revert( void );
  70. // Memory Allocation Helpers
  71. BOOL ReallocProcessIdArray( PDWORD& pdwProcessIds, DWORD& dwArraySize );
  72. BOOL ReallocModuleHandleArray( HMODULE*& phModules, DWORD& dwArraySize );
  73. // Data for impersonating data
  74. BOOL m_fImpersonatingUser;
  75. HANDLE m_hShellProcess, m_hThreadToken ,
  76. m_hUserToken;
  77. };
  78. inline BOOL CImpersonateLoggedOnUser::IsImpersonatingUser( void )
  79. {
  80. return m_fImpersonatingUser;
  81. }
  82. #endif
  83. #endif