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.

408 lines
10 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. DWORD m_dwStatus;
  39. SID_NAME_USE m_snu;
  40. public:
  41. enum { NoError, Failed, NullSid, InvalidSid, InternalError, AccessDenied = 0x5 };
  42. enum SidType {CURRENT_USER, CURRENT_THREAD};
  43. CNtSid(SidType st);
  44. CNtSid() { m_pSid = 0; m_pMachine = 0; m_dwStatus = NullSid; }
  45. bool IsUser(){return m_snu == SidTypeUser;};
  46. CNtSid(PSID pSrc);
  47. // Construct based on another SID.
  48. CNtSid(LPWSTR pUser, LPWSTR pMachine = 0);
  49. // Construct based on a user (machine name is optional).
  50. ~CNtSid();
  51. explicit CNtSid( const CNtSid &Src);
  52. CNtSid &operator =( const CNtSid &Src);
  53. int operator ==(CNtSid &Comparand);
  54. DWORD GetStatus() { return m_dwStatus; }
  55. // Returns one of the enumerated types.
  56. PSID GetPtr() { return m_pSid; }
  57. // Returns the internal SID ptr to interface with NT APIs
  58. DWORD GetSize();
  59. BOOL CopyTo(PSID pDestination);
  60. BOOL IsValid() { return (m_pSid && IsValidSid(m_pSid)); }
  61. // Checks the validity of the internal SID.
  62. int GetInfo(
  63. LPWSTR *pRetAccount, // Account, use operator delete
  64. LPWSTR *pRetDomain, // Domain, use operator delete
  65. DWORD *pdwUse // See SID_NAME_USE for values
  66. );
  67. BOOL GetTextSid(LPTSTR pszSidText, LPDWORD dwBufferLen);
  68. };
  69. //***************************************************************************
  70. //
  71. // CBaseAce
  72. //
  73. // Base class for aces.
  74. //
  75. //***************************************************************************
  76. class POLARITY CBaseAce
  77. {
  78. public:
  79. CBaseAce(){};
  80. virtual ~CBaseAce(){};
  81. virtual int GetType() = 0;
  82. virtual int GetFlags() = 0; // inheritance etc.
  83. virtual ACCESS_MASK GetAccessMask() = 0;
  84. virtual HRESULT GetFullUserName2(WCHAR ** pBuff) = 0; // call must free
  85. virtual DWORD GetStatus() = 0;
  86. virtual void SetFlags(long lFlags) =0;
  87. virtual DWORD GetSerializedSize() = 0;
  88. virtual bool Serialize(BYTE * pData, size_t buffersize)=0;
  89. virtual bool Deserialize(BYTE * pData) = 0;
  90. };
  91. //***************************************************************************
  92. //
  93. // CNtAce
  94. //
  95. // Models NT ACEs.
  96. //
  97. //***************************************************************************
  98. class POLARITY CNtAce : public CBaseAce
  99. {
  100. PGENERIC_ACE m_pAce;
  101. DWORD m_dwStatus;
  102. public:
  103. enum { NoError, InvalidAce, NullAce, InternalError };
  104. CNtAce() { m_pAce = 0; m_dwStatus = NullAce; }
  105. CNtAce(PGENERIC_ACE pAceSrc);
  106. CNtAce(const CNtAce &Src);
  107. CNtAce & operator =(const CNtAce &Src);
  108. ~CNtAce();
  109. CNtAce(
  110. ACCESS_MASK Mask,
  111. DWORD AceType,
  112. DWORD dwAceFlags,
  113. LPWSTR pUser,
  114. LPWSTR pMachine = 0 // Defaults to local machine
  115. );
  116. CNtAce(
  117. ACCESS_MASK Mask,
  118. DWORD AceType,
  119. DWORD dwAceFlags,
  120. CNtSid & Sid
  121. );
  122. int GetType();
  123. int GetFlags(); // inheritance etc.
  124. void SetFlags(long lFlags){m_pAce->Header.AceFlags = (unsigned char)lFlags;};
  125. DWORD GetStatus() { return m_dwStatus; }
  126. // Returns one of the enumerated types.
  127. int GetSubject(
  128. LPWSTR *pSubject
  129. );
  130. ACCESS_MASK GetAccessMask();
  131. CNtSid *GetSid();
  132. BOOL GetSid(CNtSid &Dest);
  133. PGENERIC_ACE GetPtr() { return m_pAce; }
  134. DWORD GetSize() { return m_pAce ? m_pAce->Header.AceSize : 0; }
  135. HRESULT GetFullUserName2(WCHAR ** pBuff); // call must free
  136. DWORD GetSerializedSize();
  137. bool Serialize(BYTE * pData, size_t bufferSize);
  138. bool Deserialize(BYTE * pData);
  139. };
  140. //***************************************************************************
  141. //
  142. // C9XAce
  143. //
  144. // Simulates NT ACEs for 9X boxs.
  145. //
  146. //***************************************************************************
  147. class POLARITY C9XAce : public CBaseAce
  148. {
  149. LPWSTR m_wszFullName;
  150. DWORD m_dwAccess;
  151. int m_iFlags;
  152. int m_iType;
  153. public:
  154. C9XAce(){m_wszFullName = 0;};
  155. C9XAce(DWORD Mask,
  156. DWORD AceType,
  157. DWORD dwAceFlags,
  158. LPWSTR pUser);
  159. ~C9XAce();
  160. int GetType(){return m_iType;};
  161. int GetFlags(){return m_iFlags;}; // inheritance etc.
  162. ACCESS_MASK GetAccessMask(){return m_dwAccess;};
  163. HRESULT GetFullUserName2(WCHAR ** pBuff); // call must free
  164. DWORD GetStatus(){ return CNtAce::NoError; };
  165. void SetFlags(long lFlags){m_iFlags = (unsigned char)lFlags;};
  166. DWORD GetSerializedSize();
  167. bool Serialize(BYTE * pData, size_t buferSize);
  168. bool Deserialize(BYTE * pData);
  169. };
  170. //***************************************************************************
  171. //
  172. // CNtAcl
  173. //
  174. // Models an NT ACL.
  175. //
  176. //***************************************************************************
  177. class POLARITY CNtAcl
  178. {
  179. PACL m_pAcl;
  180. DWORD m_dwStatus;
  181. public:
  182. enum { NoError, InternalError, NullAcl, InvalidAcl };
  183. enum { MinimumSize = 1 };
  184. CNtAcl(DWORD dwInitialSize = 128);
  185. CNtAcl(const CNtAcl &Src);
  186. CNtAcl & operator = (const CNtAcl &Src);
  187. CNtAcl(PACL pAcl); // Makes a copy
  188. ~CNtAcl();
  189. int GetNumAces();
  190. DWORD GetStatus() { return m_dwStatus; }
  191. // Returns one of the enumerated types.
  192. BOOL ContainsSid ( CNtSid& sid, BYTE& flags ) ;
  193. CNtAce *GetAce(int nIndex);
  194. BOOL GetAce(int nIndex, CNtAce &Dest);
  195. BOOL DeleteAce(int nIndex);
  196. BOOL AddAce(CNtAce *pAce);
  197. CNtAcl* OrderAces ( ) ;
  198. BOOL IsValid() { return(m_pAcl && IsValidAcl(m_pAcl)); }
  199. // Checks the validity of the embedded ACL.
  200. BOOL Resize(DWORD dwNewSize);
  201. // Or use CNtAcl::MinimumSize to trim the ACL to min size.
  202. // Fails if an illegal size is specified.
  203. DWORD GetSize();
  204. PACL GetPtr() { return m_pAcl; }
  205. // Returns the internal pointer for interface with NT APIs.
  206. BOOL GetAclSizeInfo(
  207. PDWORD pdwBytesInUse,
  208. PDWORD pdwBytesFree
  209. );
  210. };
  211. //***************************************************************************
  212. //
  213. // SNtAbsoluteSD
  214. //
  215. // Helper for converting between absolute and relative SDs.
  216. //
  217. //***************************************************************************
  218. struct SNtAbsoluteSD
  219. {
  220. PSECURITY_DESCRIPTOR m_pSD;
  221. PACL m_pDacl;
  222. PACL m_pSacl;
  223. PSID m_pOwner;
  224. PSID m_pPrimaryGroup;
  225. SNtAbsoluteSD();
  226. ~SNtAbsoluteSD();
  227. };
  228. //***************************************************************************
  229. //
  230. // CNtSecurityDescriptor
  231. //
  232. // Models an NT Security Descriptor. Note that in order to use this for an
  233. // AccessCheck, the DACL, owner sid, and group sid must be set!
  234. //
  235. //***************************************************************************
  236. class POLARITY CNtSecurityDescriptor
  237. {
  238. PSECURITY_DESCRIPTOR m_pSD;
  239. int m_dwStatus;
  240. public:
  241. enum { NoError, NullSD, Failed, InvalidSD, SDOwned, SDNotOwned };
  242. CNtSecurityDescriptor();
  243. CNtSecurityDescriptor(
  244. PSECURITY_DESCRIPTOR pSD,
  245. BOOL bAcquire = FALSE
  246. );
  247. CNtSecurityDescriptor(CNtSecurityDescriptor &Src);
  248. CNtSecurityDescriptor & operator=(CNtSecurityDescriptor &Src);
  249. ~CNtSecurityDescriptor();
  250. SNtAbsoluteSD* CNtSecurityDescriptor::GetAbsoluteCopy();
  251. BOOL SetFromAbsoluteCopy(SNtAbsoluteSD *pSrc);
  252. int HasOwner();
  253. BOOL IsValid() { return(m_pSD && IsValidSecurityDescriptor(m_pSD)); }
  254. // Checks the validity of the embedded security descriptor&
  255. DWORD GetStatus() { return m_dwStatus; }
  256. // Returns one of the enumerated types.
  257. CNtAcl *GetDacl();
  258. // Deallocate with operator delete
  259. BOOL GetDacl(CNtAcl &DestAcl);
  260. // Retrieve into an existing object
  261. BOOL SetDacl(CNtAcl *pSrc);
  262. CNtAcl *GetSacl();
  263. // Deallocate with operator delete
  264. BOOL SetSacl(CNtAcl *pSrc);
  265. CNtSid *GetOwner();
  266. BOOL SetOwner(CNtSid *pSid);
  267. CNtSid *GetGroup();
  268. BOOL SetGroup(CNtSid *pSid);
  269. PSECURITY_DESCRIPTOR GetPtr() { return m_pSD; }
  270. // Returns the internal pointer for interface with NT APIs
  271. DWORD GetSize();
  272. };
  273. //***************************************************************************
  274. //
  275. // CNtSecurity
  276. //
  277. // General-purpose NT security helpers.
  278. //
  279. //***************************************************************************
  280. class POLARITY CNtSecurity
  281. {
  282. public:
  283. enum { NoError, InternalFailure, NotFound, InvalidName, AccessDenied = 5, NoSecurity,
  284. Failed };
  285. static BOOL IsUserInGroup(
  286. HANDLE hClientToken,
  287. CNtSid & Sid
  288. );
  289. };
  290. BOOL FIsRunningAsService(VOID);
  291. POLARITY BOOL SetObjectAccess2(HANDLE hObj);
  292. POLARITY BOOL IsAdmin(HANDLE hAccess);
  293. POLARITY BOOL IsNetworkService(HANDLE hAccess);
  294. POLARITY BOOL IsLocalService(HANDLE hAccess);
  295. POLARITY HRESULT GetAccessToken(HANDLE &hAccessToken);
  296. POLARITY BOOL IsInAdminGroup();
  297. #endif