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.

155 lines
5.7 KiB

  1. /*---------------------------------------------------------------------------
  2. File: SD.hpp
  3. Comments: A generic class for managing security descriptors.
  4. The constructor takes a security descriptor in self-relative format.
  5. (c) Copyright 1995-1998, Mission Critical Software, Inc., All Rights Reserved
  6. Proprietary and confidential to Mission Critical Software, Inc.
  7. REVISION LOG ENTRY
  8. Revision By: Christy Boles
  9. Revised on 01-Oct-98 12:30:26
  10. ---------------------------------------------------------------------------
  11. */
  12. #include <stdlib.h>
  13. #include <malloc.h>
  14. #define SD_DEFAULT_STRUCT_SIZE (sizeof (SECURITY_DESCRIPTOR) )
  15. #define SD_DEFAULT_ACL_SIZE 787
  16. #define SD_DEFAULT_SID_SIZE 30
  17. #define SD_DEFAULT_SIZE 400
  18. #define DACL_FULLCONTROL_MASK (FILE_GENERIC_READ | FILE_ALL_ACCESS)
  19. #define DACL_CHANGE_MASK (FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE)
  20. #define DACL_READ_MASK ( FILE_GENERIC_READ | FILE_GENERIC_EXECUTE )
  21. #define DACL_NO_MASK 0
  22. #define SACL_READ_MASK (ACCESS_SYSTEM_SECURITY | FILE_GENERIC_READ)
  23. #define SACL_WRITE_MASK (ACCESS_SYSTEM_SECURITY | FILE_GENERIC_WRITE)
  24. #define SACL_EXECUTE_MASK ( SYNCHRONIZE | FILE_GENERIC_EXECUTE )
  25. #define SACL_DELETE_MASK (DELETE)
  26. #define SACL_CHANGEPERMS_MASK (WRITE_DAC)
  27. #define SACL_CHANGEOWNER_MASK (WRITE_OWNER)
  28. typedef enum { McsUnknownSD=0, McsFileSD, McsDirectorySD, McsShareSD, McsMailboxSD, McsExchangeSD, McsRegistrySD, McsPrinterSD } SecuredObjectType;
  29. class TSecurableObject;
  30. class TACE
  31. {
  32. ACCESS_ALLOWED_ACE * m_pAce;
  33. BOOL m_bNeedToFree;
  34. public:
  35. TACE(BYTE type,BYTE flags,DWORD mask, PSID sid); // allocates and initializes a new ace
  36. TACE(void * pAce) { m_pAce = (ACCESS_ALLOWED_ACE *)pAce; m_bNeedToFree = FALSE; } // manages an existing ace
  37. ~TACE() { if ( m_bNeedToFree ) free(m_pAce); }
  38. void * GetBuffer() { return m_pAce; }
  39. void SetBuffer(void * pAce, BOOL bNeedToFree = FALSE) { m_pAce = (ACCESS_ALLOWED_ACE *)pAce; m_bNeedToFree = bNeedToFree;}
  40. BYTE GetType();
  41. BYTE GetFlags();
  42. DWORD GetMask();
  43. PSID GetSid();
  44. WORD GetSize();
  45. BOOL SetType(BYTE newType);
  46. BOOL SetFlags(BYTE newFlags);
  47. BOOL SetMask(DWORD newMask);
  48. BOOL SetSid(PSID sid);
  49. BOOL IsAccessAllowedAce();
  50. };
  51. class TSD
  52. {
  53. friend class TSecurableObject;
  54. protected:
  55. SECURITY_DESCRIPTOR * m_absSD; // SD in absolute format
  56. BOOL m_bOwnerChanged;
  57. BOOL m_bGroupChanged;
  58. BOOL m_bDACLChanged;
  59. BOOL m_bSACLChanged;
  60. BOOL m_bNeedToFreeSD;
  61. BOOL m_bNeedToFreeOwner;
  62. BOOL m_bNeedToFreeGroup;
  63. BOOL m_bNeedToFreeDacl;
  64. BOOL m_bNeedToFreeSacl;
  65. SecuredObjectType m_ObjectType;
  66. public:
  67. TSD(SECURITY_DESCRIPTOR * pSD, SecuredObjectType objectType, BOOL bResponsibleForDelete);
  68. TSD(TSD * pTSD);
  69. ~TSD();
  70. BOOL operator == (TSD & otherSD);
  71. SECURITY_DESCRIPTOR const * GetSD() const { return m_absSD; } // returns a pointer to the absolute-format SD
  72. SECURITY_DESCRIPTOR * MakeAbsSD() const; // returns a copy of the SD in absolute format
  73. SECURITY_DESCRIPTOR * MakeRelSD() const; // returns a copy of the SD in self-relative format
  74. // type of secured object
  75. SecuredObjectType GetType() const { return m_ObjectType; }
  76. void SetType(SecuredObjectType newType) { m_ObjectType = newType;}
  77. // Security Descriptor parts
  78. PSID const GetOwner() const;
  79. void SetOwner(PSID pNewOwner);
  80. PSID const GetGroup() const;
  81. void SetGroup(PSID const pNewGroup);
  82. PACL const GetDacl() const;
  83. // SetDacl will free the buffer pNewAcl.
  84. void SetDacl(PACL pNewAcl,BOOL present = TRUE);
  85. PACL const GetSacl() const;
  86. // SetSacl will free the buffer pNewAcl.
  87. void SetSacl(PACL pNewAcl, BOOL present = TRUE);
  88. // Security Descriptor flags
  89. BOOL IsOwnerDefaulted() const;
  90. BOOL IsGroupDefaulted() const;
  91. BOOL IsDaclDefaulted() const;
  92. BOOL IsDaclPresent() const;
  93. BOOL IsSaclDefaulted() const;
  94. BOOL IsSaclPresent() const;
  95. // Change tracking functions
  96. BOOL IsOwnerChanged() const { return m_bOwnerChanged; }
  97. BOOL IsGroupChanged() const { return m_bGroupChanged; }
  98. BOOL IsDACLChanged() const { return m_bDACLChanged; }
  99. BOOL IsSACLChanged() const { return m_bSACLChanged; }
  100. BOOL IsChanged() const { return ( m_bOwnerChanged || m_bGroupChanged || m_bDACLChanged || m_bSACLChanged ); }
  101. void MarkAllChanged(BOOL bChanged) { m_bOwnerChanged=bChanged; m_bGroupChanged=bChanged; m_bDACLChanged=bChanged; m_bSACLChanged=bChanged; }
  102. // Functions to manage ACLs
  103. int GetNumDaclAces() { return ACLGetNumAces(GetDacl()); }
  104. void AddDaclAce(TACE * pAce);
  105. void RemoveDaclAce(int ndx);
  106. void * GetDaclAce(int ndx) { return ACLGetAce(GetDacl(),ndx); }
  107. int GetNumSaclAces() { return ACLGetNumAces(GetSacl()); }
  108. void AddSaclAce(TACE * pAce);
  109. void RemoveSaclAce(int ndx);
  110. void * GetSaclAce(int ndx) { return ACLGetAce(GetSacl(),ndx); }
  111. BOOL IsValid() { return (m_absSD && IsValidSecurityDescriptor(m_absSD)); }
  112. void FreeAbsSD(SECURITY_DESCRIPTOR * pSD, BOOL bAll = TRUE);
  113. void ACLAddAce(PACL * ppAcl, TACE * pAce, int pos);
  114. void * ACLGetAce(PACL acl, int ndx);
  115. protected:
  116. // Implementation - helper functions
  117. // Comparison functions
  118. BOOL EqualSD(TSD * otherSD);
  119. BOOL ACLCompare(PACL acl1,BOOL present1,PACL acl2, BOOL present2);
  120. // ACL manipulation functions
  121. int ACLGetNumAces(PACL acl);
  122. DWORD ACLGetFreeBytes(PACL acl);
  123. DWORD ACLGetBytesInUse(PACL acl);
  124. void ACLDeleteAce(PACL acl, int ndx);
  125. SECURITY_DESCRIPTOR * MakeAbsSD(SECURITY_DESCRIPTOR * pSD) const;
  126. };