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.

156 lines
5.9 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. #define SET_SID_FAILED -1
  29. #define SET_SID_NOTLARGEENOUGH 0
  30. #define SET_SID_SUCCEEDED 1
  31. typedef enum { McsUnknownSD=0, McsFileSD, McsDirectorySD, McsShareSD, McsMailboxSD, McsExchangeSD, McsRegistrySD, McsPrinterSD } SecuredObjectType;
  32. class TSecurableObject;
  33. class TACE
  34. {
  35. ACCESS_ALLOWED_ACE * m_pAce;
  36. BOOL m_bNeedToFree;
  37. public:
  38. TACE(BYTE type,BYTE flags,DWORD mask, PSID sid); // allocates and initializes a new ace
  39. TACE(void * pAce) { m_pAce = (ACCESS_ALLOWED_ACE *)pAce; m_bNeedToFree = FALSE; } // manages an existing ace
  40. ~TACE() { if ( m_bNeedToFree ) free(m_pAce); }
  41. void * GetBuffer() { return m_pAce; }
  42. void SetBuffer(void * pAce, BOOL bNeedToFree = FALSE) { m_pAce = (ACCESS_ALLOWED_ACE *)pAce; m_bNeedToFree = bNeedToFree;}
  43. BYTE GetType();
  44. BYTE GetFlags();
  45. DWORD GetMask();
  46. PSID GetSid();
  47. WORD GetSize();
  48. BOOL SetType(BYTE newType);
  49. BOOL SetFlags(BYTE newFlags);
  50. BOOL SetMask(DWORD newMask);
  51. DWORD SetSid(PSID sid);
  52. BOOL IsAccessAllowedAce();
  53. };
  54. class TSD
  55. {
  56. friend class TSecurableObject;
  57. protected:
  58. SECURITY_DESCRIPTOR * m_absSD; // SD in absolute format
  59. BOOL m_bOwnerChanged;
  60. BOOL m_bGroupChanged;
  61. BOOL m_bDACLChanged;
  62. BOOL m_bSACLChanged;
  63. BOOL m_bNeedToFreeSD;
  64. BOOL m_bNeedToFreeOwner;
  65. BOOL m_bNeedToFreeGroup;
  66. BOOL m_bNeedToFreeDacl;
  67. BOOL m_bNeedToFreeSacl;
  68. SecuredObjectType m_ObjectType;
  69. public:
  70. TSD(SECURITY_DESCRIPTOR * pSD, SecuredObjectType objectType, BOOL bResponsibleForDelete);
  71. TSD(TSD * pTSD);
  72. TSD(SecuredObjectType objectType);
  73. ~TSD();
  74. SECURITY_DESCRIPTOR const * GetSD() const { return m_absSD; } // returns a pointer to the absolute-format SD
  75. SECURITY_DESCRIPTOR * MakeAbsSD() const; // returns a copy of the SD in absolute format
  76. SECURITY_DESCRIPTOR * MakeRelSD() const; // returns a copy of the SD in self-relative format
  77. // type of secured object
  78. SecuredObjectType GetType() const { return m_ObjectType; }
  79. void SetType(SecuredObjectType newType) { m_ObjectType = newType;}
  80. // Security Descriptor parts
  81. PSID const GetOwner() const;
  82. void SetOwner(PSID pNewOwner);
  83. PSID const GetGroup() const;
  84. void SetGroup(PSID const pNewGroup);
  85. PACL const GetDacl() const;
  86. // SetDacl will free the buffer pNewAcl.
  87. BOOL SetDacl(PACL pNewAcl,BOOL present = TRUE);
  88. PACL const GetSacl() const;
  89. // SetSacl will free the buffer pNewAcl.
  90. void SetSacl(PACL pNewAcl, BOOL present = TRUE);
  91. // Security Descriptor flags
  92. BOOL IsOwnerDefaulted() const;
  93. BOOL IsGroupDefaulted() const;
  94. BOOL IsDaclDefaulted() const;
  95. BOOL IsDaclPresent() const;
  96. BOOL IsSaclDefaulted() const;
  97. BOOL IsSaclPresent() const;
  98. // Change tracking functions
  99. BOOL IsOwnerChanged() const { return m_bOwnerChanged; }
  100. BOOL IsGroupChanged() const { return m_bGroupChanged; }
  101. BOOL IsDACLChanged() const { return m_bDACLChanged; }
  102. BOOL IsSACLChanged() const { return m_bSACLChanged; }
  103. BOOL IsChanged() const { return ( m_bOwnerChanged || m_bGroupChanged || m_bDACLChanged || m_bSACLChanged ); }
  104. void MarkAllChanged(BOOL bChanged) { m_bOwnerChanged=bChanged; m_bGroupChanged=bChanged; m_bDACLChanged=bChanged; m_bSACLChanged=bChanged; }
  105. // Functions to manage ACLs
  106. int GetNumDaclAces() { return ACLGetNumAces(GetDacl()); }
  107. void AddDaclAce(TACE * pAce);
  108. void RemoveDaclAce(int ndx);
  109. void * GetDaclAce(int ndx) { return ACLGetAce(GetDacl(),ndx); }
  110. int GetNumSaclAces() { return ACLGetNumAces(GetSacl()); }
  111. void AddSaclAce(TACE * pAce);
  112. void RemoveSaclAce(int ndx);
  113. void * GetSaclAce(int ndx) { return ACLGetAce(GetSacl(),ndx); }
  114. BOOL IsValid() { return (m_absSD && IsValidSecurityDescriptor(m_absSD)); }
  115. void FreeAbsSD(SECURITY_DESCRIPTOR * pSD, BOOL bAll = TRUE);
  116. void ACLAddAce(PACL * ppAcl, TACE * pAce, int pos);
  117. void * ACLGetAce(PACL acl, int ndx);
  118. protected:
  119. // Implementation - helper functions
  120. // Comparison functions
  121. // ACL manipulation functions
  122. int ACLGetNumAces(PACL acl);
  123. DWORD ACLGetFreeBytes(PACL acl);
  124. DWORD ACLGetBytesInUse(PACL acl);
  125. void ACLDeleteAce(PACL acl, int ndx);
  126. SECURITY_DESCRIPTOR * MakeAbsSD(SECURITY_DESCRIPTOR * pSD) const;
  127. };