UserAcct.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef __UserAcct_h__
  2. #define __UserAcct_h__
  3. /////////////////////////////////////////////////////////////////////////////
  4. // UserAcct.h : Declaration of the TCUserAccount class.
  5. #include <..\TCLib\NetApi.h>
  6. #include <..\TCLib\AutoHandle.h>
  7. /////////////////////////////////////////////////////////////////////////////
  8. class TCUserAccount
  9. {
  10. // Construction / Destruction
  11. public:
  12. TCUserAccount();
  13. ~TCUserAccount();
  14. HRESULT Init(LPCSTR szUser);
  15. HRESULT Init(LPCWSTR szUser);
  16. // Attributes
  17. public:
  18. // Security ID (SID) Lookup
  19. static HRESULT GetSID(LPCSTR szUserName, PSID* ppSID, LPSTR* ppszDomain = NULL);
  20. static HRESULT GetSID(LPCWSTR szUserName, PSID* ppSID, LPWSTR* ppszDomain = NULL);
  21. // AppID Helpers
  22. static HRESULT ResolveAppID(LPWSTR pszAppID, HKEY* phKey, REGSAM samDesired = KEY_ALL_ACCESS);
  23. static HRESULT SetRunAsInteractiveUser(LPCTSTR szAppID);
  24. static HRESULT SetRunAsUser(LPCTSTR szAppID, LPCTSTR szUserName, LPCTSTR szPassword);
  25. HRESULT SetRunAsUser(LPCTSTR szAppID, LPCTSTR szPassword);
  26. // Property Accessors
  27. ULONG GetUserNameLength() const;
  28. ULONG GetUserName(LPSTR pszUserName, ULONG cbUserName) const;
  29. ULONG GetUserName(LPWSTR pszUserName, ULONG cbUserName) const;
  30. LPCWSTR GetUserNameW() const;
  31. LPCWSTR GetDomainNameW() const;
  32. // Privilege Accessors
  33. HRESULT HasRight (LPTSTR pszPrivilege) const;
  34. HRESULT SetRight (LPTSTR pszPrivilege);
  35. HRESULT RemoveRight(LPTSTR pszPrivilege);
  36. // Data Members
  37. protected:
  38. TCLsaHandle m_hPolicy;
  39. TCCoTaskPtr<WCHAR*> m_spszUserName;
  40. TCCoTaskPtr<WCHAR*> m_spszDomainName;
  41. TCCoTaskPtr<PSID> m_spSIDPrincipal;
  42. };
  43. /////////////////////////////////////////////////////////////////////////////
  44. // Construction / Destruction
  45. inline TCUserAccount::TCUserAccount()
  46. {
  47. }
  48. inline TCUserAccount::~TCUserAccount()
  49. {
  50. }
  51. inline HRESULT TCUserAccount::Init(LPCSTR szUser)
  52. {
  53. USES_CONVERSION;
  54. return Init(A2CW(szUser));
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // Attributes
  58. // Security ID (SID) Lookup
  59. inline HRESULT TCUserAccount::GetSID(LPCSTR szUserName, PSID* ppSID, LPSTR* ppszDomain)
  60. {
  61. USES_CONVERSION;
  62. TCCoTaskPtr<LPWSTR> spszDomain;
  63. LPWSTR* ppszDomainWide = ppszDomain ? &spszDomain : NULL;
  64. RETURN_FAILED(GetSID(A2CW(szUserName), ppSID, ppszDomainWide));
  65. if (ppszDomainWide)
  66. {
  67. UINT cch = (wcslen(*ppszDomainWide) + 1) * sizeof(char);
  68. *ppszDomain = (LPSTR)CoTaskMemAlloc(cch);
  69. if (!*ppszDomain)
  70. return E_OUTOFMEMORY;
  71. strcpy(*ppszDomain, W2CA(*ppszDomainWide));
  72. }
  73. return S_OK;
  74. }
  75. // Property Accessors
  76. inline ULONG TCUserAccount::GetUserNameLength() const
  77. {
  78. return wcslen(m_spszUserName);
  79. }
  80. inline ULONG TCUserAccount::GetUserName(LPSTR pszUserName, ULONG cbUserName) const
  81. {
  82. USES_CONVERSION;
  83. if (pszUserName)
  84. lstrcpynA(pszUserName, W2CA(m_spszUserName), cbUserName);
  85. return GetUserNameLength();
  86. }
  87. inline ULONG TCUserAccount::GetUserName(LPWSTR pszUserName, ULONG cbUserName) const
  88. {
  89. if (pszUserName)
  90. lstrcpynW(pszUserName, m_spszUserName, cbUserName);
  91. return GetUserNameLength();
  92. }
  93. inline LPCWSTR TCUserAccount::GetUserNameW() const
  94. {
  95. return m_spszUserName;
  96. }
  97. inline LPCWSTR TCUserAccount::GetDomainNameW() const
  98. {
  99. return m_spszDomainName;
  100. }
  101. /////////////////////////////////////////////////////////////////////////////
  102. #endif // !__UserAcct_h__