Source code of Windows XP (NT5)
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.

466 lines
11 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. WINNTSEC.H
  5. Abstract:
  6. Generic wrapper classes for NT security objects.
  7. Documention on class members is in WINNTSEC.CPP. Inline members
  8. are commented in this file.
  9. History:
  10. raymcc 08-Jul-97 Created.
  11. --*/
  12. #ifndef _WINNTSEC_H_
  13. #define _WINNTSEC_H_
  14. class POLARITY CNtSecurity;
  15. // All ACE types are currently have the same binary layout. Rather
  16. // than doing a lot of useless casts, we produce a general-purpose
  17. // typedef to hold all ACEs.
  18. // ================================================================
  19. typedef ACCESS_ALLOWED_ACE GENERIC_ACE;
  20. typedef GENERIC_ACE *PGENERIC_ACE;
  21. #define FULL_CONTROL \
  22. (DELETE | \
  23. READ_CONTROL | \
  24. WRITE_DAC | \
  25. WRITE_OWNER | \
  26. SYNCHRONIZE | GENERIC_ALL)
  27. //***************************************************************************
  28. //
  29. // CNtSid
  30. //
  31. // Models SIDs (users/groups).
  32. //
  33. //***************************************************************************
  34. class POLARITY CNtSid
  35. {
  36. PSID m_pSid;
  37. LPWSTR m_pMachine;
  38. LPWSTR m_pDomain;
  39. DWORD m_dwStatus;
  40. SID_NAME_USE m_snu;
  41. public:
  42. enum { NoError, Failed, NullSid, InvalidSid, InternalError, AccessDenied = 0x5 };
  43. enum SidType {CURRENT_USER, CURRENT_THREAD};
  44. CNtSid(SidType st);
  45. CNtSid() { m_pSid = 0; m_pMachine = 0; m_dwStatus = NullSid; }
  46. bool IsUser(){return m_snu == SidTypeUser;};
  47. CNtSid(PSID pSrc);
  48. // Construct based on another SID.
  49. CNtSid(LPWSTR pUser, LPWSTR pMachine = 0);
  50. // Construct based on a user (machine name is optional).
  51. ~CNtSid();
  52. CNtSid(CNtSid &Src);
  53. CNtSid &operator =(CNtSid &Src);
  54. int operator ==(CNtSid &Comparand);
  55. DWORD GetStatus() { return m_dwStatus; }
  56. // Returns one of the enumerated types.
  57. PSID GetPtr() { return m_pSid; }
  58. // Returns the internal SID ptr to interface with NT APIs
  59. DWORD GetSize();
  60. BOOL CopyTo(PSID pDestination);
  61. BOOL IsValid() { return (m_pSid && IsValidSid(m_pSid)); }
  62. // Checks the validity of the internal SID.
  63. void Dump();
  64. // Dumps SID info to console for debugging.
  65. int GetInfo(
  66. LPWSTR *pRetAccount, // Account, use operator delete
  67. LPWSTR *pRetDomain, // Domain, use operator delete
  68. DWORD *pdwUse // See SID_NAME_USE for values
  69. );
  70. BOOL GetTextSid(LPTSTR pszSidText, LPDWORD dwBufferLen);
  71. };
  72. //***************************************************************************
  73. //
  74. // CBaseAce
  75. //
  76. // Base class for aces.
  77. //
  78. //***************************************************************************
  79. class POLARITY CBaseAce
  80. {
  81. public:
  82. CBaseAce(){};
  83. virtual ~CBaseAce(){};
  84. virtual int GetType() = 0;
  85. virtual int GetFlags() = 0; // inheritance etc.
  86. virtual ACCESS_MASK GetAccessMask() = 0;
  87. virtual HRESULT GetFullUserName(WCHAR * pBuff, DWORD dwSize) = 0;
  88. virtual HRESULT GetFullUserName2(WCHAR ** pBuff) = 0; // call must free
  89. virtual DWORD GetStatus() = 0;
  90. virtual void SetFlags(long lFlags) =0;
  91. virtual DWORD GetSerializedSize() = 0;
  92. virtual bool Serialize(BYTE * pData) = 0;
  93. virtual bool Deserialize(BYTE * pData) = 0;
  94. };
  95. //***************************************************************************
  96. //
  97. // CNtAce
  98. //
  99. // Models NT ACEs.
  100. //
  101. //***************************************************************************
  102. class POLARITY CNtAce : public CBaseAce
  103. {
  104. PGENERIC_ACE m_pAce;
  105. DWORD m_dwStatus;
  106. public:
  107. enum { NoError, InvalidAce, NullAce, InternalError };
  108. CNtAce() { m_pAce = 0; m_dwStatus = NullAce; }
  109. CNtAce(PGENERIC_ACE pAceSrc);
  110. CNtAce(CNtAce &Src);
  111. CNtAce & operator =(CNtAce &Src);
  112. ~CNtAce();
  113. CNtAce(
  114. ACCESS_MASK Mask,
  115. DWORD AceType,
  116. DWORD dwAceFlags,
  117. LPWSTR pUser,
  118. LPWSTR pMachine = 0 // Defaults to local machine
  119. );
  120. CNtAce(
  121. ACCESS_MASK Mask,
  122. DWORD AceType,
  123. DWORD dwAceFlags,
  124. CNtSid & Sid
  125. );
  126. int GetType();
  127. int GetFlags(); // inheritance etc.
  128. void SetFlags(long lFlags){m_pAce->Header.AceFlags = (unsigned char)lFlags;};
  129. DWORD GetStatus() { return m_dwStatus; }
  130. // Returns one of the enumerated types.
  131. int GetSubject(
  132. LPWSTR *pSubject
  133. );
  134. ACCESS_MASK GetAccessMask();
  135. CNtSid *GetSid();
  136. BOOL GetSid(CNtSid &Dest);
  137. PGENERIC_ACE GetPtr() { return m_pAce; }
  138. DWORD GetSize() { return m_pAce ? m_pAce->Header.AceSize : 0; }
  139. HRESULT GetFullUserName(WCHAR * pBuff, DWORD dwSize);
  140. HRESULT GetFullUserName2(WCHAR ** pBuff); // call must free
  141. DWORD GetSerializedSize();
  142. bool Serialize(BYTE * pData);
  143. bool Deserialize(BYTE * pData);
  144. void Dump(int iAceNum = -1);
  145. void DumpAccessMask();
  146. };
  147. //***************************************************************************
  148. //
  149. // C9XAce
  150. //
  151. // Simulates NT ACEs for 9X boxs.
  152. //
  153. //***************************************************************************
  154. class POLARITY C9XAce : public CBaseAce
  155. {
  156. LPWSTR m_wszFullName;
  157. DWORD m_dwAccess;
  158. int m_iFlags;
  159. int m_iType;
  160. public:
  161. C9XAce(){m_wszFullName = 0;};
  162. C9XAce(DWORD Mask,
  163. DWORD AceType,
  164. DWORD dwAceFlags,
  165. LPWSTR pUser);
  166. ~C9XAce();
  167. int GetType(){return m_iType;};
  168. int GetFlags(){return m_iFlags;}; // inheritance etc.
  169. ACCESS_MASK GetAccessMask(){return m_dwAccess;};
  170. HRESULT GetFullUserName(WCHAR * pBuff, DWORD dwSize);
  171. HRESULT GetFullUserName2(WCHAR ** pBuff); // call must free
  172. DWORD GetStatus(){ return CNtAce::NoError; };
  173. void SetFlags(long lFlags){m_iFlags = (unsigned char)lFlags;};
  174. DWORD GetSerializedSize();
  175. bool Serialize(BYTE * pData);
  176. bool Deserialize(BYTE * pData);
  177. };
  178. //***************************************************************************
  179. //
  180. // CNtAcl
  181. //
  182. // Models an NT ACL.
  183. //
  184. //***************************************************************************
  185. class POLARITY CNtAcl
  186. {
  187. PACL m_pAcl;
  188. DWORD m_dwStatus;
  189. public:
  190. enum { NoError, InternalError, NullAcl, InvalidAcl };
  191. enum { MinimumSize = 1 };
  192. CNtAcl(DWORD dwInitialSize = 128);
  193. CNtAcl(CNtAcl &Src);
  194. CNtAcl & operator = (CNtAcl &Src);
  195. CNtAcl(PACL pAcl); // Makes a copy
  196. ~CNtAcl();
  197. int GetNumAces();
  198. DWORD GetStatus() { return m_dwStatus; }
  199. // Returns one of the enumerated types.
  200. BOOL ContainsSid ( CNtSid& sid, BYTE& flags ) ;
  201. CNtAce *GetAce(int nIndex);
  202. BOOL GetAce(int nIndex, CNtAce &Dest);
  203. BOOL DeleteAce(int nIndex);
  204. BOOL AddAce(CNtAce *pAce);
  205. BOOL IsValid() { return(m_pAcl && IsValidAcl(m_pAcl)); }
  206. // Checks the validity of the embedded ACL.
  207. BOOL Resize(DWORD dwNewSize);
  208. // Or use CNtAcl::MinimumSize to trim the ACL to min size.
  209. // Fails if an illegal size is specified.
  210. DWORD GetSize();
  211. PACL GetPtr() { return m_pAcl; }
  212. // Returns the internal pointer for interface with NT APIs.
  213. BOOL GetAclSizeInfo(
  214. PDWORD pdwBytesInUse,
  215. PDWORD pdwBytesFree
  216. );
  217. void Dump();
  218. };
  219. //***************************************************************************
  220. //
  221. // SNtAbsoluteSD
  222. //
  223. // Helper for converting between absolute and relative SDs.
  224. //
  225. //***************************************************************************
  226. struct SNtAbsoluteSD
  227. {
  228. PSECURITY_DESCRIPTOR m_pSD;
  229. PACL m_pDacl;
  230. PACL m_pSacl;
  231. PSID m_pOwner;
  232. PSID m_pPrimaryGroup;
  233. SNtAbsoluteSD();
  234. ~SNtAbsoluteSD();
  235. };
  236. //***************************************************************************
  237. //
  238. // CNtSecurityDescriptor
  239. //
  240. // Models an NT Security Descriptor. Note that in order to use this for an
  241. // AccessCheck, the DACL, owner sid, and group sid must be set!
  242. //
  243. //***************************************************************************
  244. class POLARITY CNtSecurityDescriptor
  245. {
  246. PSECURITY_DESCRIPTOR m_pSD;
  247. int m_dwStatus;
  248. public:
  249. enum { NoError, NullSD, Failed, InvalidSD, SDOwned, SDNotOwned };
  250. CNtSecurityDescriptor();
  251. CNtSecurityDescriptor(
  252. PSECURITY_DESCRIPTOR pSD,
  253. BOOL bAcquire = FALSE
  254. );
  255. CNtSecurityDescriptor(CNtSecurityDescriptor &Src);
  256. CNtSecurityDescriptor & operator=(CNtSecurityDescriptor &Src);
  257. ~CNtSecurityDescriptor();
  258. SNtAbsoluteSD* CNtSecurityDescriptor::GetAbsoluteCopy();
  259. BOOL SetFromAbsoluteCopy(SNtAbsoluteSD *pSrc);
  260. int HasOwner();
  261. BOOL IsValid() { return(m_pSD && IsValidSecurityDescriptor(m_pSD)); }
  262. // Checks the validity of the embedded security descriptor&
  263. DWORD GetStatus() { return m_dwStatus; }
  264. // Returns one of the enumerated types.
  265. CNtAcl *GetDacl();
  266. // Deallocate with operator delete
  267. BOOL GetDacl(CNtAcl &DestAcl);
  268. // Retrieve into an existing object
  269. BOOL SetDacl(CNtAcl *pSrc);
  270. CNtAcl *GetSacl();
  271. // Deallocate with operator delete
  272. BOOL SetSacl(CNtAcl *pSrc);
  273. CNtSid *GetOwner();
  274. BOOL SetOwner(CNtSid *pSid);
  275. CNtSid *GetGroup();
  276. BOOL SetGroup(CNtSid *pSid);
  277. PSECURITY_DESCRIPTOR GetPtr() { return m_pSD; }
  278. // Returns the internal pointer for interface with NT APIs
  279. DWORD GetSize();
  280. void Dump();
  281. };
  282. //***************************************************************************
  283. //
  284. // CNtSecurity
  285. //
  286. // General-purpose NT security helpers.
  287. //
  288. //***************************************************************************
  289. class POLARITY CNtSecurity
  290. {
  291. public:
  292. enum { NoError, InternalFailure, NotFound, InvalidName, AccessDenied = 5, NoSecurity,
  293. Failed };
  294. static BOOL DumpPrivileges();
  295. static BOOL SetPrivilege(
  296. IN TCHAR *pszPrivilegeName, // An SE_ value.
  297. IN BOOL bEnable // TRUE=enable, FALSE=disable
  298. );
  299. static BOOL GetFileSD(
  300. IN TCHAR *pszFile,
  301. IN SECURITY_INFORMATION SecInfo,
  302. OUT CNtSecurityDescriptor **pSD
  303. );
  304. static BOOL SetFileSD(
  305. IN TCHAR *pszFile,
  306. IN SECURITY_INFORMATION SecInfo,
  307. IN CNtSecurityDescriptor *pSD
  308. );
  309. static int GetRegSD(
  310. IN HKEY hRoot,
  311. IN TCHAR *pszSubKey,
  312. IN SECURITY_INFORMATION SecInfo,
  313. OUT CNtSecurityDescriptor **pSD
  314. );
  315. static int SetRegSD(
  316. IN HKEY hRoot,
  317. IN TCHAR *pszSubKey,
  318. IN SECURITY_INFORMATION SecInfo,
  319. IN CNtSecurityDescriptor *pSD
  320. );
  321. /* static int GetDCName(
  322. IN LPWSTR pszDomain,
  323. OUT LPWSTR *pszDC,
  324. IN LPWSTR pszServer
  325. );*/
  326. static BOOL IsUserInGroup(
  327. HANDLE hClientToken,
  328. CNtSid & Sid
  329. );
  330. static DWORD AccessCheck(
  331. HANDLE hAccessToken,
  332. ACCESS_MASK RequiredAccess,
  333. CNtSecurityDescriptor *pSD
  334. ); // TBD
  335. static CNtSid *GetCurrentThreadSid(); // TBD
  336. static bool DoesLocalGroupExist(LPWSTR pwszGroup, LPWSTR pwszMachine);
  337. static bool AddLocalGroup(LPWSTR pwszGroupName, LPWSTR pwszGroupDescription);
  338. };
  339. BOOL FIsRunningAsService(VOID);
  340. POLARITY BOOL SetObjectAccess2(HANDLE hObj);
  341. POLARITY BOOL IsAdmin(HANDLE hAccess);
  342. POLARITY BOOL IsNetworkService(HANDLE hAccess);
  343. POLARITY BOOL IsLocalService(HANDLE hAccess);
  344. POLARITY HRESULT GetAccessToken(HANDLE &hAccessToken);
  345. POLARITY BOOL IsInAdminGroup();
  346. #endif