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.

315 lines
16 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. MPC_security.h
  5. Abstract:
  6. This file contains the declaration of various security functions/classes.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 04/26/2000
  9. created
  10. ******************************************************************************/
  11. #if !defined(__INCLUDED___MPC___SECURITY_H___)
  12. #define __INCLUDED___MPC___SECURITY_H___
  13. #include <MPC_main.h>
  14. #include <MPC_utils.h>
  15. #include <Ntsecapi.h>
  16. //
  17. // From #include <Ntstatus.h> (including the file generates a lot of redefinition error with WINNT.H)
  18. //
  19. #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) // ntsubauth
  20. #define STATUS_OBJECT_NAME_NOT_FOUND ((NTSTATUS)0xC0000034L)
  21. #include <Lmaccess.h>
  22. #include <Lmerr.h>
  23. #include <Sddl.h>
  24. #include <sspi.h>
  25. #include <secext.h>
  26. namespace MPC
  27. {
  28. struct SID2
  29. {
  30. SID m_main;
  31. DWORD m_SubAuthority2;
  32. operator SID&() { return m_main; }
  33. };
  34. class SecurityDescriptor
  35. {
  36. protected: // To enable other classes to extend the functionality.
  37. PSECURITY_DESCRIPTOR m_pSD;
  38. PSID m_pOwner;
  39. BOOL m_bOwnerDefaulted;
  40. PSID m_pGroup;
  41. BOOL m_bGroupDefaulted;
  42. PACL m_pDACL;
  43. BOOL m_bDaclDefaulted;
  44. PACL m_pSACL;
  45. BOOL m_bSaclDefaulted;
  46. ////////////////////////////////////////////////////////////////////////////////
  47. public:
  48. static const SID s_EveryoneSid;
  49. static const SID s_SystemSid;
  50. static const SID2 s_AdminSid;
  51. static const SID2 s_Alias_AdminsSid;
  52. static const SID2 s_Alias_PowerUsersSid;
  53. static const SID2 s_Alias_UsersSid;
  54. static const SID2 s_Alias_GuestsSid;
  55. static const SECURITY_INFORMATION s_SecInfo_ALL = OWNER_SECURITY_INFORMATION |
  56. GROUP_SECURITY_INFORMATION |
  57. DACL_SECURITY_INFORMATION |
  58. SACL_SECURITY_INFORMATION ;
  59. static const SECURITY_INFORMATION s_SecInfo_MOST = OWNER_SECURITY_INFORMATION |
  60. GROUP_SECURITY_INFORMATION |
  61. DACL_SECURITY_INFORMATION ;
  62. static const SECURITY_DESCRIPTOR_CONTROL s_sdcMask = SE_DACL_AUTO_INHERIT_REQ |
  63. SE_SACL_AUTO_INHERIT_REQ |
  64. SE_DACL_AUTO_INHERITED |
  65. SE_SACL_AUTO_INHERITED |
  66. SE_DACL_PROTECTED |
  67. SE_SACL_PROTECTED ;
  68. //
  69. // Any memory returned by this class should be release with ReleaseMemory( (void*&)<var> ).
  70. //
  71. static HRESULT AllocateMemory( /*[in/out]*/ LPVOID& ptr, /*[in]*/ size_t iLen );
  72. static void ReleaseMemory ( /*[in/out]*/ LPVOID& ptr );
  73. static void InitLsaString( /*[in/out]*/ LSA_UNICODE_STRING& lsaString, /*[in]*/ LPCWSTR szText );
  74. //
  75. // Utility functions.
  76. //
  77. static HRESULT SetPrivilege( /*[in]*/ LPCWSTR Privilege, /*[in]*/ BOOL bEnable = TRUE, /*[in]*/ HANDLE hToken = NULL );
  78. static HRESULT AddPrivilege ( /*[in]*/ LPCWSTR szPrincipal, /*[in]*/ LPCWSTR szPrivilege );
  79. static HRESULT RemovePrivilege( /*[in]*/ LPCWSTR szPrincipal, /*[in]*/ LPCWSTR szPrivilege );
  80. ////////////////////
  81. static HRESULT GetTokenSids ( /*[in]*/ HANDLE hToken, /*[out]*/ PSID *ppUserSid, /*[out]*/ PSID *ppGroupSid );
  82. static HRESULT GetProcessSids( /*[out]*/ PSID *ppUserSid, /*[out]*/ PSID *ppGroupSid = NULL );
  83. static HRESULT GetThreadSids ( /*[out]*/ PSID *ppUserSid, /*[out]*/ PSID *ppGroupSid = NULL, /*[in]*/ BOOL bOpenAsSelf = FALSE );
  84. ////////////////////
  85. static HRESULT VerifyPrincipal ( /*[in ]*/ LPCWSTR szPrincipal );
  86. static HRESULT ConvertPrincipalToSID( /*[in ]*/ LPCWSTR szPrincipal, /*[out]*/ PSID& pSid, /*[out]*/ LPCWSTR *pszDomain = NULL );
  87. static HRESULT ConvertSIDToPrincipal( /*[in]*/ PSID pSid, /*[out]*/ LPCWSTR *pszPrincipal , /*[out]*/ LPCWSTR *pszDomain = NULL );
  88. static HRESULT ConvertSIDToPrincipal( /*[in]*/ PSID pSid, /*[out]*/ MPC::wstring& strPrincipal );
  89. static HRESULT NormalizePrincipalToStringSID( /*[in]*/ LPCWSTR szPrincipal, /*[in]*/ LPCWSTR szDomain, /*[out]*/ MPC::wstring& strSID );
  90. ////////////////////
  91. static HRESULT GetAccountName ( /*[in]*/ LPCWSTR szPrincipal, /*[out]*/ MPC::wstring& strName );
  92. static HRESULT GetAccountDomain ( /*[in]*/ LPCWSTR szPrincipal, /*[out]*/ MPC::wstring& strDomain );
  93. static HRESULT GetAccountDisplayName( /*[in]*/ LPCWSTR szPrincipal, /*[out]*/ MPC::wstring& strDisplayName );
  94. ////////////////////
  95. static HRESULT CloneACL( /*[in/out]*/ PACL& pDest, /*[in]*/ PACL pSrc );
  96. static HRESULT RemovePrincipalFromACL( /*[in ]*/ PACL pACL, /*[in]*/ PSID pPrincipalSID, /*[in]*/ int pos = -1 );
  97. static HRESULT AddACEToACL ( /*[in/out]*/ PACL& pACL, /*[in]*/ PSID pPrincipalSID,
  98. /*[in ]*/ DWORD dwAceType ,
  99. /*[in ]*/ DWORD dwAceFlags ,
  100. /*[in ]*/ DWORD dwAccessMask ,
  101. /*[in ]*/ GUID* guidObjectType = NULL ,
  102. /*[in ]*/ GUID* guidInheritedObjectType = NULL );
  103. ////////////////////////////////////////////////////////////////////////////////
  104. private:
  105. static HRESULT CopyACL ( /*[in ]*/ PACL pDest, /*[in]*/ PACL pSrc );
  106. static HRESULT EnsureACLSize( /*[in/out]*/ PACL& pACL , /*[in]*/ DWORD dwExpand );
  107. ////////////////////////////////////////////////////////////////////////////////
  108. public:
  109. SecurityDescriptor();
  110. virtual ~SecurityDescriptor();
  111. void CleanUp ( );
  112. HRESULT Initialize ( );
  113. HRESULT InitializeFromProcessToken( /*[in]*/ BOOL bDefaulted = FALSE );
  114. HRESULT InitializeFromThreadToken ( /*[in]*/ BOOL bDefaulted = FALSE, /*[in]*/ BOOL bRevertToProcessToken = TRUE );
  115. HRESULT ConvertFromString( /*[in ]*/ LPCWSTR szSD );
  116. HRESULT ConvertToString ( /*[out]*/ BSTR *pbstrSD );
  117. ////////////////////
  118. HRESULT Attach ( /*[in]*/ PSECURITY_DESCRIPTOR pSelfRelativeSD );
  119. HRESULT AttachObject( /*[in]*/ HANDLE hObject , /*[in]*/ SECURITY_INFORMATION secInfo = s_SecInfo_MOST );
  120. ////////////////////
  121. HRESULT GetControl( /*[out]*/ SECURITY_DESCRIPTOR_CONTROL& sdc );
  122. HRESULT SetControl( /*[in ]*/ SECURITY_DESCRIPTOR_CONTROL sdc );
  123. HRESULT SetOwner( /*[in]*/ PSID pOwnerSid , /*[in]*/ BOOL bDefaulted = FALSE );
  124. HRESULT SetOwner( /*[in]*/ LPCWSTR szOwnerName, /*[in]*/ BOOL bDefaulted = FALSE );
  125. HRESULT SetGroup( /*[in]*/ PSID pGroupSid , /*[in]*/ BOOL bDefaulted = FALSE );
  126. HRESULT SetGroup( /*[in]*/ LPCWSTR szGroupName, /*[in]*/ BOOL bDefaulted = FALSE );
  127. ////////////////////
  128. HRESULT Remove( /*[in]*/ PSID pPrincipalSid, /*[in]*/ int pos = -1 );
  129. HRESULT Remove( /*[in]*/ LPCWSTR szPrincipal , /*[in]*/ int pos = -1 );
  130. HRESULT Add( /*[in]*/ PSID pPrincipalSid ,
  131. /*[in]*/ DWORD dwAceType ,
  132. /*[in]*/ DWORD dwAceFlags ,
  133. /*[in]*/ DWORD dwAccessMask ,
  134. /*[in]*/ GUID* guidObjectType = NULL ,
  135. /*[in]*/ GUID* guidInheritedObjectType = NULL );
  136. HRESULT Add( /*[in]*/ LPCWSTR szPrincipal ,
  137. /*[in]*/ DWORD dwAceType ,
  138. /*[in]*/ DWORD dwAceFlags ,
  139. /*[in]*/ DWORD dwAccessMask ,
  140. /*[in]*/ GUID* guidObjectType = NULL ,
  141. /*[in]*/ GUID* guidInheritedObjectType = NULL );
  142. PSECURITY_DESCRIPTOR& GetSD () { return m_pSD ; }
  143. PSID& GetOwner() { return m_pOwner; }
  144. PSID& GetGroup() { return m_pGroup; }
  145. PACL& GetDACL () { return m_pDACL ; }
  146. PACL& GetSACL () { return m_pSACL ; }
  147. ////////////////////////////////////////
  148. HRESULT GetForFile ( /*[in]*/ LPCWSTR szFilename, /*[in]*/ SECURITY_INFORMATION secInfo );
  149. HRESULT SetForFile ( /*[in]*/ LPCWSTR szFilename, /*[in]*/ SECURITY_INFORMATION secInfo );
  150. HRESULT GetForRegistry( /*[in]*/ LPCWSTR szKey , /*[in]*/ SECURITY_INFORMATION secInfo, /*[in]*/ HKEY hKeyRoot = NULL );
  151. HRESULT SetForRegistry( /*[in]*/ LPCWSTR szKey , /*[in]*/ SECURITY_INFORMATION secInfo, /*[in]*/ HKEY hKeyRoot = NULL );
  152. };
  153. ////////////////////////////////////////////////////////////////////////////////
  154. class Impersonation
  155. {
  156. HANDLE m_hToken;
  157. bool m_fImpersonating;
  158. void Release();
  159. public:
  160. Impersonation();
  161. Impersonation( /*[in]*/ const Impersonation& imp );
  162. virtual ~Impersonation();
  163. Impersonation& operator=( /*[in]*/ const Impersonation& imp );
  164. HRESULT Initialize( /*[in]*/ DWORD dwDesiredAccess = TOKEN_QUERY | TOKEN_IMPERSONATE );
  165. void Attach ( /*[in]*/ HANDLE hToken );
  166. HANDLE Detach ( );
  167. HRESULT Impersonate ();
  168. HRESULT RevertToSelf();
  169. operator HANDLE() { return m_hToken; }
  170. };
  171. ////////////////////////////////////////////////////////////////////////////////
  172. class AccessCheck
  173. {
  174. HANDLE m_hToken;
  175. void Release();
  176. public:
  177. AccessCheck();
  178. virtual ~AccessCheck();
  179. HRESULT GetTokenFromImpersonation( );
  180. void Attach ( /*[in]*/ HANDLE hToken );
  181. HANDLE Detach ( );
  182. HRESULT Verify( /*[in]*/ DWORD dwDesired, /*[out]*/ BOOL& fGranted, /*[out]*/ DWORD& dwGranted, /*[in]*/ PSECURITY_DESCRIPTOR sd );
  183. HRESULT Verify( /*[in]*/ DWORD dwDesired, /*[out]*/ BOOL& fGranted, /*[out]*/ DWORD& dwGranted, /*[in]*/ MPC::SecurityDescriptor& sd );
  184. HRESULT Verify( /*[in]*/ DWORD dwDesired, /*[out]*/ BOOL& fGranted, /*[out]*/ DWORD& dwGranted, /*[in]*/ LPCWSTR sd );
  185. };
  186. ////////////////////////////////////////////////////////////////////////////////
  187. HRESULT ChangeSD( /*[in]*/ MPC::SecurityDescriptor& sdd ,
  188. /*[in]*/ MPC::FileSystemObject& fso ,
  189. /*[in]*/ SECURITY_INFORMATION secInfo = GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION ,
  190. /*[in]*/ bool fDeep = true ,
  191. /*[in]*/ bool fApplyToDirs = true ,
  192. /*[in]*/ bool fApplyToFiles = true );
  193. HRESULT ChangeSD( /*[in]*/ MPC::SecurityDescriptor& sdd ,
  194. /*[in]*/ LPCWSTR szRoot ,
  195. /*[in]*/ SECURITY_INFORMATION secInfo = GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION ,
  196. /*[in]*/ bool fDeep = true ,
  197. /*[in]*/ bool fApplyToDirs = true ,
  198. /*[in]*/ bool fApplyToFiles = true );
  199. ////////////////////////////////////////////////////////////////////////////////
  200. static const DWORD IDENTITY_SYSTEM = 0x00000001;
  201. static const DWORD IDENTITY_ADMIN = 0x00000002;
  202. static const DWORD IDENTITY_ADMINS = 0x00000004;
  203. static const DWORD IDENTITY_POWERUSERS = 0x00000008;
  204. static const DWORD IDENTITY_USERS = 0x00000010;
  205. static const DWORD IDENTITY_GUESTS = 0x00000020;
  206. HRESULT GetCallerPrincipal ( /*[in]*/ bool fImpersonate, /*[out]*/ CComBSTR& bstrUser, /*[out]*/ DWORD *pdwAllowedIdentity = NULL );
  207. HRESULT CheckCallerAgainstPrincipal( /*[in]*/ bool fImpersonate, /*[out]*/ BSTR bstrUser, /*[in ]*/ DWORD dwAllowedIdentity = 0 );
  208. ////////////////////////////////////////////////////////////////////////////////
  209. HRESULT GetInterfaceSecurity( /*[in ]*/ IUnknown* pUnk ,
  210. /*[out]*/ DWORD *pAuthnSvc ,
  211. /*[out]*/ DWORD *pAuthzSvc ,
  212. /*[out]*/ OLECHAR* *pServerPrincName ,
  213. /*[out]*/ DWORD *pAuthnLevel ,
  214. /*[out]*/ DWORD *pImpLevel ,
  215. /*[out]*/ RPC_AUTH_IDENTITY_HANDLE *pAuthInfo ,
  216. /*[out]*/ DWORD *pCapabilities );
  217. HRESULT SetInterfaceSecurity( /*[in]*/ IUnknown* pUnk ,
  218. /*[in]*/ DWORD *pAuthnSvc ,
  219. /*[in]*/ DWORD *pAuthzSvc ,
  220. /*[in]*/ OLECHAR* pServerPrincName ,
  221. /*[in]*/ DWORD *pAuthnLevel ,
  222. /*[in]*/ DWORD *pImpLevel ,
  223. /*[in]*/ RPC_AUTH_IDENTITY_HANDLE *pAuthInfo ,
  224. /*[in]*/ DWORD *pCapabilities );
  225. HRESULT SetInterfaceSecurity_ImpLevel( /*[in]*/ IUnknown* pUnk ,
  226. /*[in]*/ DWORD ImpLevel );
  227. };
  228. ////////////////////////////////////////////////////////////////////////////////
  229. #endif // !defined(__INCLUDED___MPC___SECURITY_H___)