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.

285 lines
8.8 KiB

  1. /*++
  2. Copyright (c) 1999, 2000 Microsoft Corporation
  3. Module Name:
  4. vs_sec.hxx
  5. Abstract:
  6. Declaration of IsAdministrator
  7. Adi Oltean [aoltean] 10/05/1999
  8. Revision History:
  9. Name Date Comments
  10. aoltean 09/27/1999 Created
  11. aoltean 10/05/1999 Moved into security.hxx from admin.hxx
  12. aoltean 12/16/1999 Moved into vs_sec.hxx
  13. brianb 04/27/2000 Added IsRestoreOperator, TurnOnSecurityPrivilegeRestore, TurnOnSecurityPrivilegeBackup
  14. brianb 05/03/2000 Added GetClientTokenOwner method
  15. --*/
  16. #ifndef __VSS_SECURITY_HXX__
  17. #define __VSS_SECURITY_HXX__
  18. #if _MSC_VER > 1000
  19. #pragma once
  20. #endif
  21. ////////////////////////////////////////////////////////////////////////
  22. // Standard foo for file name aliasing. This code block must be after
  23. // all includes of VSS header files.
  24. //
  25. #ifdef VSS_FILE_ALIAS
  26. #undef VSS_FILE_ALIAS
  27. #endif
  28. #define VSS_FILE_ALIAS "INCSECH"
  29. //
  30. ////////////////////////////////////////////////////////////////////////
  31. /////////////////////////////////////////////////////////////////////////////
  32. // global methods
  33. // is caller member of administrators group
  34. bool IsAdministrator() throw (HRESULT);
  35. // is caller member of administrators group or has SE_BACKUP_NAME privilege
  36. // enabled
  37. bool IsBackupOperator() throw(HRESULT);
  38. // is caller member of administrators group or has SE_RESTORE_NAME privilege
  39. // enabled
  40. bool IsRestoreOperator() throw(HRESULT);
  41. // enable SE_BACKUP_NAME privilege
  42. HRESULT TurnOnSecurityPrivilegeBackup();
  43. // enable SE_RESTORE_NAME privilege
  44. HRESULT TurnOnSecurityPrivilegeRestore();
  45. // determine if process has ADMIN privileges
  46. bool IsProcessAdministrator() throw(HRESULT);
  47. // determine if process has backup privilege enabled
  48. bool IsProcessBackupOperator() throw(HRESULT);
  49. // determine if the process has the restore privilege enabeled
  50. bool IsProcessRestoreOperator() throw(HRESULT);
  51. // get SID of calling client process
  52. TOKEN_OWNER *GetClientTokenOwner(BOOL bImpersonate) throw(HRESULT);
  53. // get SID of the user running the client process
  54. TOKEN_USER *GetClientTokenUser(BOOL bImpersonate) throw(HRESULT);
  55. // auto sid class, destroys sid when going out of scope
  56. class CAutoSid : public CVssAuto<SID*, CVssAutoGenericValue_Storage<SID*, NULL, LocalFreeType, ::LocalFree> >
  57. {
  58. typedef CVssAuto<SID*, CVssAutoGenericValue_Storage<SID*, NULL, LocalFreeType, ::LocalFree> > Base;
  59. public:
  60. CAutoSid()
  61. {
  62. }
  63. // create a sid base on a well known sid type
  64. void CreateBasicSid(WELL_KNOWN_SID_TYPE type);
  65. // create a sid from a string
  66. void CreateFromString(LPCWSTR wsz);
  67. };
  68. //////////////////////////////////////////////////////////////////////////////
  69. // CVssSecurityDescriptor
  70. class CVssSecurityDescriptor
  71. {
  72. public:
  73. CVssSecurityDescriptor();
  74. ~CVssSecurityDescriptor();
  75. public:
  76. HRESULT Attach(PSECURITY_DESCRIPTOR pSelfRelativeSD);
  77. HRESULT AttachObject(HANDLE hObject);
  78. HRESULT Initialize();
  79. HRESULT InitializeFromProcessToken(BOOL bDefaulted = FALSE);
  80. HRESULT InitializeFromThreadToken(BOOL bDefaulted = FALSE, BOOL bRevertToProcessToken = TRUE);
  81. HRESULT SetOwner(PSID pOwnerSid, BOOL bDefaulted = FALSE);
  82. HRESULT SetGroup(PSID pGroupSid, BOOL bDefaulted = FALSE);
  83. HRESULT Allow(LPCTSTR pszPrincipal, DWORD dwAccessMask, DWORD dwAceFlags = 0);
  84. HRESULT Deny(LPCTSTR pszPrincipal, DWORD dwAccessMask, DWORD dwAceFlags = 0);
  85. HRESULT Allow(PSID pSid, DWORD dwAccessMask, DWORD dwAceFlags = 0);
  86. HRESULT Deny(PSID pSid, DWORD dwAccessMask, DWORD dwAceFlags = 0);
  87. HRESULT Revoke(LPCTSTR pszPrincipal);
  88. // utility functions
  89. // Any PSID you get from these functions should be free()ed
  90. static HRESULT SetPrivilege(LPCTSTR Privilege, BOOL bEnable = TRUE, HANDLE hToken = NULL);
  91. static HRESULT GetTokenSids(HANDLE hToken, PSID* ppUserSid, PSID* ppGroupSid);
  92. static HRESULT GetProcessSids(PSID* ppUserSid, PSID* ppGroupSid = NULL);
  93. static HRESULT GetThreadSids(PSID* ppUserSid, PSID* ppGroupSid = NULL, BOOL bOpenAsSelf = FALSE);
  94. static HRESULT CopyACL(PACL pDest, PACL pSrc);
  95. static HRESULT GetCurrentUserSID(PSID *ppSid);
  96. static HRESULT GetPrincipalSID(LPCTSTR pszPrincipal, PSID *ppSid);
  97. static HRESULT AddAccessAllowedACEToACL(PACL *Acl, LPCTSTR pszPrincipal, DWORD dwAccessMask, DWORD dwAceFlags);
  98. static HRESULT AddAccessDeniedACEToACL(PACL *Acl, LPCTSTR pszPrincipal, DWORD dwAccessMask, DWORD dwAceFlags);
  99. static HRESULT AddAccessAllowedACEToACL(PACL *Acl, PSID principalSID, DWORD dwAccessMask, DWORD dwAceFlags);
  100. static HRESULT AddAccessDeniedACEToACL(PACL *Acl, PSID principalSID, DWORD dwAccessMask, DWORD dwAceFlags);
  101. static HRESULT RemovePrincipalFromACL(PACL Acl, LPCTSTR pszPrincipal);
  102. operator PSECURITY_DESCRIPTOR()
  103. {
  104. return m_pSD;
  105. }
  106. public:
  107. PSECURITY_DESCRIPTOR m_pSD;
  108. PSID m_pOwner;
  109. PSID m_pGroup;
  110. PACL m_pDACL;
  111. PACL m_pSACL;
  112. };
  113. //////////////////////////////////////////////////////////////////////////////
  114. // Class - CVssSidCollection
  115. //
  116. class CVssSidCollection
  117. {
  118. // Constructors/destructors
  119. private:
  120. CVssSidCollection(const CVssSidCollection&);
  121. CVssSidCollection& operator=(const CVssSidCollection&);
  122. public:
  123. CVssSidCollection();
  124. ~CVssSidCollection();
  125. // Accessors
  126. public:
  127. // Get the total count of stored SIDs
  128. INT GetSidCount();
  129. // Get the SID with the given index (starts with 0)
  130. PSID GetSid(INT nIndex) throw(HRESULT);
  131. // Get the SID use with the given index
  132. SID_NAME_USE GetSidUse(INT nIndex) throw(HRESULT);
  133. // Check if the SID with the given index is allowed
  134. bool IsSidAllowed(INT nIndex) throw(HRESULT);
  135. // Check if the SID with the given index is a local user/group
  136. bool IsLocal(INT nIndex) throw(HRESULT);
  137. // Get the principal for the SID with the given index
  138. LPWSTR GetPrincipal(INT nIndex) throw(HRESULT);
  139. // Get the principal for the SID with the given index
  140. LPWSTR GetName(INT nIndex) throw(HRESULT);
  141. // Get the principal for the SID with the given index
  142. LPWSTR GetDomain(INT nIndex) throw(HRESULT);
  143. // Determine if the current process can be a writer
  144. bool IsProcessValidWriter() throw(HRESULT);
  145. // determine if a SID is allowed to fire
  146. bool IsSidAllowedToFire(PSID psid) throw(HRESULT);
  147. // determine if the sid is a member of a well-known group
  148. bool IsSidRelatedWithLocalSid(
  149. IN PSID pSid,
  150. IN LPWSTR pwszWellKnownPrincipal,
  151. IN PSID pWellKnownSid
  152. ) throw(HRESULT);
  153. PSECURITY_DESCRIPTOR GetSecurityDescriptor() { return m_SD; };
  154. // Operations
  155. public:
  156. // Initialize SID from registry and add the implicit Admin, BO, System SID
  157. void Initialize() throw(HRESULT);
  158. // Implementation
  159. private:
  160. class CVssSidWrapper
  161. {
  162. public:
  163. CVssSidWrapper(bool bAllow,
  164. PSID pSid,
  165. SID_NAME_USE use,
  166. LPWSTR pwszName,
  167. LPWSTR pwszDomain,
  168. bool bIsLocal
  169. ):
  170. m_bAllow(bAllow), m_pSid(pSid),
  171. m_use(use), m_pwszName(pwszName), m_pwszDomain(pwszDomain),
  172. m_bIsLocal(bIsLocal) {};
  173. bool IsSidAllowed() const { return m_bAllow; };
  174. PSID GetSid() const { return m_pSid; };
  175. SID_NAME_USE GetUse() const { return m_use; };
  176. LPWSTR GetName() const { return m_pwszName; };
  177. LPWSTR GetDomain() const { return m_pwszDomain; };
  178. bool IsLocal() const { return m_bIsLocal; };
  179. private:
  180. bool m_bAllow;
  181. PSID m_pSid;
  182. SID_NAME_USE m_use;
  183. LPWSTR m_pwszName;
  184. LPWSTR m_pwszDomain;
  185. bool m_bIsLocal;
  186. };
  187. bool AddUser(
  188. IN LPCWSTR pwszUser,
  189. IN bool bAllow
  190. ) throw(HRESULT);
  191. void AddWellKnownSid(
  192. IN WELL_KNOWN_SID_TYPE type
  193. ) throw(HRESULT);
  194. bool VerifyIsLocal(
  195. IN LPCWSTR pwszDomain,
  196. IN bool bIsAdministratorsAccount
  197. );
  198. // determine if a SID is allowed to fire
  199. bool CheckIfExplicitelySpecified(
  200. IN PSID psid,
  201. IN bool bChechAllowed
  202. ) throw(HRESULT);
  203. // List of sids
  204. CVssSimpleMap<LPWSTR, CVssSidWrapper> m_SidArray;
  205. // Only for assertions
  206. bool m_bInitialized;
  207. // Security descriptor
  208. CVssSecurityDescriptor m_SD;
  209. // Name of the "BUILTIN" domain
  210. //
  211. // This is filled in when the SYSTEM well-known SID is added
  212. // (the SYSTEM account must be added first)
  213. CVssAutoLocalString m_pwszBuiltinDomain;
  214. };
  215. #endif // __VSS_SECURITY_HXX__