text: //WinQLSecurityDescriptor.h
  
  // Copyright Querysoft Limited 2013, 2017
  //
  // Permission is hereby granted, free of charge, to any person or organization
  // obtaining a copy of the software and accompanying documentation covered by
  // this license (the "Software") to use, reproduce, display, distribute,
  // execute, and transmit the Software, and to prepare derivative works of the
  // Software, and to permit third-parties to whom the Software is furnished to
  // do so, all subject to the following:
  // 
  // The copyright notices in the Software and this entire statement, including
  // the above license grant, this restriction and the following disclaimer,
  // must be included in all copies of the Software, in whole or in part, and
  // all derivative works of the Software, unless such copies or derivative
  // works are solely in the form of machine-executable object code generated by
  // a source language processor.
  // 
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  // DEALINGS IN THE SOFTWARE.
  
  //Win32 Security Descriptor
  
  #ifndef WINQL_SECURITY_DESCRIPTOR_H_3
  #define WINQL_SECURITY_DESCRIPTOR_H_3
  
  #include "CompilerQOR.h"
  
  #ifdef	__QCMP_OPTIMIZEINCLUDE
  #pragma	__QCMP_OPTIMIZEINCLUDE
  #endif//__QCMP_OPTIMIZEINCLUDE
  
  #include "WinQL/Definitions/Security.h"
  #include "WinQL/System/Security/WinQLTrustee.h"
  #include "WinQL/System/Security/WinQLExplicitAccess.h"
  
  //--------------------------------------------------------------------------------
  namespace nsWin32
  {
  	//--------------------------------------------------------------------------------
  	struct GenericMapping 
  	{
  		unsigned long GenericRead;
  		unsigned long GenericWrite;
  		unsigned long GenericExecute;
  		unsigned long GenericAll;
  	};
  
  	//--------------------------------------------------------------------------------
  	struct ObjectTypeList
  	{
  		unsigned short Level;
  		unsigned short Sbz;
  		nsWin32::GUID* ObjectType;
  	};
  
  	//--------------------------------------------------------------------------------
  	enum AuditEventType
  	{
  		AuditEventObjectAccess,
  		AuditEventDirectoryServiceAccess
  	};// AUDIT_EVENT_TYPE, *PAUDIT_EVENT_TYPE;
  
  
  	//--------------------------------------------------------------------------------
  	class __QOR_INTERFACE( __WINQL ) CSecurityDescriptor
  	{
  	public:
  
  		__QOR_DECLARE_OCLASS_ID( CSecurityDescriptor );
  
  		CSecurityDescriptor();
  		CSecurityDescriptor( const CSecurityDescriptor& src );
  		CSecurityDescriptor( void* pDescriptor, bool bTemp = true );
  		CSecurityDescriptor( void* ParentDescriptor, void* CreatorDescriptor, bool IsDirectoryObject, void* Token, nsWin32::GenericMapping* GenericMapping );
  		CSecurityDescriptor( void* ParentDescriptor, void* CreatorDescriptor, nsWin32::GUID* ObjectType, bool IsContainerObject, unsigned long AutoInheritFlags, void* Token, nsWin32::GenericMapping* GenericMapping );
  		CSecurityDescriptor( void* ParentDescriptor, void* CreatorDescriptor, nsWin32::GUID** ObjectTypes, unsigned long GuidCount, bool IsContainerObject, unsigned long AutoInheritFlags, void* Token, nsWin32::GenericMapping* GenericMapping );
  		CSecurityDescriptor( nsWin32::Trustee* pOwner, nsWin32::Trustee* pGroup, unsigned long cCountOfAccessEntries, nsWin32::ExplicitAccess* pListOfAccessEntries, unsigned long cCountOfAuditEntries, nsWin32::ExplicitAccess* pListOfAuditEntries, void* pOldSD, unsigned long* pSizeNewSD );
  		CSecurityDescriptor( void* handle, nsWin32::SeObjectType ObjectType, unsigned long SecurityInfo, void** ppsidOwner, void** ppsidGroup, nsWin32::AccessControlList** ppDacl, nsWin32::AccessControlList** ppSacl );
  		CSecurityDescriptor( const TCHAR* pObjectName, nsWin32::SeObjectType ObjectType, unsigned long SecurityInfo, void** ppsidOwner, void** ppsidGroup, nsWin32::AccessControlList** ppDacl, nsWin32::AccessControlList** ppSacl );
  		/*
  		CSecurityDescriptor( LPCTSTR lpObjectName, SeObjectType objectType, SECURITY_INFORMATION securityInfo,
  						LPCTSTR lpProvider, LPCTSTR lpProperty, PACTRL_ACCESS* ppAccessList, 
  						PACTRL_AUDIT* ppAuditList, LPTSTR* lppOwner, LPTSTR* lppGroup )
  		*/
  
  		CSecurityDescriptor( const TCHAR* StringSecurityDescriptor, unsigned long StringSDRevision );
  		~CSecurityDescriptor();
  		TCHAR* ToString( unsigned long SecurityInformation );
  		bool AccessCheck( void* ClientToken, unsigned long DesiredAccess, nsWin32::GenericMapping* GenericMapping, nsWin32::PPRIVILEGE_SET PrivilegeSet, unsigned long* PrivilegeSetLength, unsigned long* GrantedAccess, int* AccessStatus );
  		bool AccessCheckAndAuditAlarmT( const TCHAR* SubsystemName, void* HandleId, TCHAR* ObjectTypeName, TCHAR* ObjectName, unsigned long DesiredAccess, nsWin32::GenericMapping* GenericMapping, bool ObjectCreation, unsigned long* GrantedAccess, int* AccessStatus, int* pfGenerateOnClose );
  		bool AccessCheckByType( void* PrincipalSelfSid, void* ClientToken, unsigned long DesiredAccess, nsWin32::ObjectTypeList* ObjectTypeList, unsigned long ObjectTypeListLength, nsWin32::GenericMapping* GenericMapping, nsWin32::PPRIVILEGE_SET PrivilegeSet, unsigned long* PrivilegeSetLength, unsigned long* GrantedAccess, int* AccessStatus );
  		bool AccessCheckByTypeAndAuditAlarmT( const TCHAR* SubsystemName, void* HandleId, const TCHAR* ObjectTypeName, const TCHAR* ObjectName, void* PrincipalSelfSid, unsigned long DesiredAccess, nsWin32::AuditEventType AuditType, unsigned long Flags, nsWin32::ObjectTypeList* ObjectTypeList, unsigned long ObjectTypeListLength, nsWin32::GenericMapping* GenericMapping, bool ObjectCreation, unsigned long* GrantedAccess, int* AccessStatus, int* pfGenerateOnClose );
  		bool AccessCheckByTypeResultList( void* PrincipalSelfSid, void* ClientToken, unsigned long DesiredAccess, nsWin32::ObjectTypeList* ObjectTypeList, unsigned long ObjectTypeListLength, nsWin32::GenericMapping* GenericMapping, nsWin32::PPRIVILEGE_SET PrivilegeSet, unsigned long* PrivilegeSetLength, unsigned long* GrantedAccessList, unsigned long* AccessStatusList );
  		bool AccessCheckByTypeResultListAndAuditAlarmT( const TCHAR* SubsystemName, void* HandleId, const TCHAR* ObjectTypeName, const TCHAR* ObjectName, void* PrincipalSelfSid, unsigned long DesiredAccess, nsWin32::AuditEventType AuditType, unsigned long Flags, nsWin32::ObjectTypeList* ObjectTypeList, unsigned long ObjectTypeListLength, nsWin32::GenericMapping* GenericMapping, bool ObjectCreation, unsigned long* GrantedAccess, unsigned long* AccessStatusList, int* pfGenerateOnClose );
  		bool AccessCheckByTypeResultListAndAuditAlarmByHandleT( const TCHAR* SubsystemName, void* HandleId, void* ClientToken, const TCHAR* ObjectTypeName, const TCHAR* ObjectName, void* PrincipalSelfSid, unsigned long DesiredAccess, nsWin32::AuditEventType AuditType, unsigned long Flags, nsWin32::ObjectTypeList* ObjectTypeList, unsigned long ObjectTypeListLength, nsWin32::GenericMapping* GenericMapping, bool ObjectCreation, unsigned long* GrantedAccess, unsigned long* AccessStatusList, int* pfGenerateOnClose );
  		unsigned long Build( nsWin32::Trustee* pOwner, nsWin32::Trustee* pGroup, unsigned long cCountOfAccessEntries, nsWin32::ExplicitAccess* pListOfAccessEntries, unsigned long cCountOfAuditEntries, nsWin32::ExplicitAccess* pListOfAuditEntries, unsigned long* pSizeNewSD );
  		bool ConvertToAutoInheritPrivateObjectSecurity( void* ParentDescriptor, nsWin32::GUID* ObjectType, unsigned char IsDirectoryObject, nsWin32::GenericMapping* GenericMapping );
  		bool GetControl( unsigned short* pControl, unsigned long* lpdwRevision );
  		bool GetDacl( int* lpbDaclPresent, nsWin32::AccessControlList** pDacl, int* lpbDaclDefaulted );
  		bool GetGroup( void** pGroup, int* lpbGroupDefaulted );
  		unsigned long GetLength( void );
  		bool GetOwner( void** pOwner, int* lpbOwnerDefaulted );
  		unsigned long GetRMControl( unsigned char* RMControl );
  		bool GetSacl( int* lpbSaclPresent, nsWin32::AccessControlList** pSacl, int* lpbSaclDefaulted );
  		bool GetPrivateObjectInformation( unsigned long SecurityInformation, void* ResultantDescriptor, unsigned long DescriptorLength, unsigned long* ReturnLength );
  		bool SetPrivateObjectInformation( unsigned long SecurityInformation, void* ModificationDescriptor, unsigned long AutoInheritFlags, nsWin32::GenericMapping* GenericMapping, void* Token );
  		bool Initialize( unsigned long dwRevision );
  		bool IsValid( void );
  		unsigned long LookupParts( nsWin32::Trustee** pOwner, nsWin32::Trustee** pGroup, unsigned long* cCountOfAccessEntries, nsWin32::ExplicitAccess** pListOfAccessEntries, unsigned long* cCountOfAuditEntries, nsWin32::ExplicitAccess** pListOfAuditEntries );
  		bool SetDacl( bool bDaclPresent, nsWin32::AccessControlList* pDacl, bool bDaclDefaulted );
  		bool SetGroup( void* pGroup, bool bGroupDefaulted );
  		bool SetOwner( void* pOwner, bool bOwnerDefaulted );
  		unsigned long SetRMControl( unsigned char* RMControl );
  		bool SetSacl( bool bSaclPresent, nsWin32::AccessControlList* pSacl, bool bSaclDefaulted );
  		void* GetDescriptor( void );
  
  	private:
  
  		void* m_pDescriptor;
  		nsWinQAPI::CAdvAPI32& m_AdvAPI32Library;
  		bool m_bLocalFree;
  		bool m_bPrivateFree;
  
  		__QCS_DECLARE_NONASSIGNABLE( CSecurityDescriptor );
  
  	};
  
  }//nsWin32
  
  #endif//WINQL_SECURITY_DESCRIPTOR_H_3
  