os/persistentdata/persistentstorage/sql/SRC/Security/SqlSecurityImpl.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/SRC/Security/SqlSecurityImpl.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,203 @@
     1.4 +// Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#ifndef __SQLSECURITYIMPL_H__
    1.20 +#define __SQLSECURITYIMPL_H__
    1.21 +
    1.22 +#include <sqldb.h>
    1.23 +#include "SqlBufFlat.h"		//RSqlBufFlat
    1.24 +
    1.25 +//Forward declarations
    1.26 +class TSqlSecurityPolicyIterator;
    1.27 +
    1.28 +/**
    1.29 +CSqlSecurityPolicy class implements RSqlSecurityPolicy.
    1.30 +
    1.31 +Current CSqlSecurityPolicy implementation uses a flat buffer (see RSqlBufFlat class) as a storage for the database 
    1.32 +objects security policies.
    1.33 +By default the flat buffer has threee entries:
    1.34 + - entry 0 is a system entry and keeps the count of the database security entries;
    1.35 + - entry 1 is reserved for the default database security policy. This is a nameless entry;
    1.36 + - entry 2 is reserved for the database security policies: read, write and schema. This is a nameless entry;
    1.37 +The flat buffer may have more entries if database object security policies are added. In this case each flat buffer entry
    1.38 +has a 16-bit encoded database object name, kept at the end of the entry, after the database object security policy data and
    1.39 +database object name length.
    1.40 +Note: Current CSqlSecurityPolicy implementation can have no more than 13 database object security policy entries.
    1.41 +An access to the flat buffer can be obtained via CSqlSecurityPolicy::BufFlat() methods (const and non-const).
    1.42 +There are two ways to externalize (resp. internalize) CSqlSecurityPolicy data:
    1.43 + - (1) using CSqlSecurityPolicy::ExternalizeL()/CSqlSecurityPolicy::InternalizeL();
    1.44 + - (2) using CSqlSecurityPolicy::BufFlat() to get an access to the flat buffer and then externalize/internalize the buffer.
    1.45 +       CSqlSecurityPolicy class has only one data member which is the flat buffer;
    1.46 +
    1.47 +TSqlSecurityPolicyIterator class gives a convinient way to access the security policies kept inside the flat buffer.
    1.48 +The given access is a read-only.
    1.49 +
    1.50 +@see RSqlSecurityPolicy
    1.51 +@see RSqlBufFlat
    1.52 +@see TSqlSecurityPolicyIterator
    1.53 +@see RSqlSecurityPolicy::TPolicyType
    1.54 +@see RSqlSecurityPolicy::TObjectType
    1.55 +@see CSqlSecurityPolicy::NewL()
    1.56 +@see CSqlSecurityPolicy::NewLC()
    1.57 +@see CSqlSecurityPolicy::SetDefaultPolicy()
    1.58 +@see CSqlSecurityPolicy::SetDbPolicy()
    1.59 +@see CSqlSecurityPolicy::SetPolicy()
    1.60 +@see CSqlSecurityPolicy::DefaultPolicy()
    1.61 +@see CSqlSecurityPolicy::DbPolicy()
    1.62 +@see CSqlSecurityPolicy::Policy()
    1.63 +@see CSqlSecurityPolicy::ExternalizeL()
    1.64 +@see CSqlSecurityPolicy::InternalizeL()
    1.65 +@see CSqlSecurityPolicy::BufFlat()
    1.66 +
    1.67 +@internalComponent
    1.68 +*/
    1.69 +NONSHARABLE_CLASS(CSqlSecurityPolicy) : public CBase
    1.70 +	{
    1.71 +	friend class TSqlSecurityPolicyIterator;
    1.72 +	struct TPolicyItem;
    1.73 +
    1.74 +public:
    1.75 +	static CSqlSecurityPolicy* NewL(const TSecurityPolicy& aDefaultPolicy);
    1.76 +	static CSqlSecurityPolicy* NewLC(const TSecurityPolicy& aDefaultPolicy);
    1.77 +	virtual ~CSqlSecurityPolicy();
    1.78 +	void SetDefaultPolicy(const TSecurityPolicy& aPolicy);
    1.79 +	void SetDbPolicy(RSqlSecurityPolicy::TPolicyType aPolicyType, const TSecurityPolicy& aPolicy);
    1.80 +	TInt SetPolicy(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName, RSqlSecurityPolicy::TPolicyType aPolicyType, const TSecurityPolicy& aPolicy);
    1.81 +	TSecurityPolicy DefaultPolicy() const;
    1.82 +	TSecurityPolicy DbPolicy(RSqlSecurityPolicy::TPolicyType aPolicyType) const;
    1.83 +	TSecurityPolicy Policy(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName, RSqlSecurityPolicy::TPolicyType aPolicyType);
    1.84 +	TInt Count() const;
    1.85 +	const RSqlBufFlat&	BufFlat() const;
    1.86 +	RSqlBufFlat& BufFlat();
    1.87 +
    1.88 +private:
    1.89 +	CSqlSecurityPolicy();
    1.90 +	void ConstructL(const TSecurityPolicy& aDefaultPolicy);
    1.91 +	inline static TInt PolicyType2Index(RSqlSecurityPolicy::TPolicyType aPolicyType);
    1.92 +	inline static TPolicyItem* PolicyItemPtr(const RSqlBufFlat::TCell* aBegin, const RSqlBufFlat::TCell* aCurrent);
    1.93 +	const TPolicyItem* FindPolicyItemPtr(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName) const;
    1.94 +	void SetCount(TInt aCount);
    1.95 +	
    1.96 +private:
    1.97 +	enum {
    1.98 +		 EPolicyTypeCount	= 3,					//Schema, Read, Write policy types
    1.99 +		 EMaxCount 			= 16,					//iBufFlat max element count. The database cannot have more than EMaxCount security policies.
   1.100 +		 ECountIdx			= 0,					//The index of the policy counter
   1.101 +		 EDefaultPolicyIdx	= 1,					//The index of the default policy type in iBufFlat
   1.102 +		 EDbPolicyIdx		= 2						//The index of the database policy type in iBufFlat
   1.103 +		 };					
   1.104 +	
   1.105 +	//The database security policies are kept in objects of TPolicyItem type.
   1.106 +	//If particular TPolicyItem object represents the database security policies, then the memory layout is:
   1.107 +	//    [SchemaPolicy]
   1.108 +	//    [ReadPolicy]
   1.109 +	//    [WritePolicy]
   1.110 +	//If particular TPolicyItem object represents table security policies, then the memory layout is:
   1.111 +	//    [SchemaPolicy]
   1.112 +	//    [ReadPolicy]
   1.113 +	//    [WritePolicy]
   1.114 +	//    [The length of the table name - 32-bit integer]
   1.115 +	//    [The table name]
   1.116 +	//As you can see, TPolicyItem does not have "name length" and "name" data members. If that is needed, an appropriate block of memory will be allocated for the TPolicyItem object.
   1.117 +	//There SQL platform security interface currently supports only database security policies, but the implementation is capable of handling also table security policies.
   1.118 +	struct TPolicyItem
   1.119 +		{
   1.120 +		enum {EPolicyTypeCount	= 3};				//Schema, Read, Write policy types. The same as the declaration above.
   1.121 +		TSecurityPolicy iPolicy[EPolicyTypeCount];	//Database object policies
   1.122 +		//Database object name size will be after the end of TPolicyItem object
   1.123 +		inline TInt* NameSizePtr() 
   1.124 +			{
   1.125 +			//See the comments of the TPolicyItem structure above.
   1.126 +			//coverity[overrun-local]
   1.127 +			return reinterpret_cast <TInt*> (reinterpret_cast <TUint8*> (iPolicy) + sizeof(iPolicy));
   1.128 +			}
   1.129 +		inline TInt NameSize() const 
   1.130 +			{
   1.131 +			//See the comments of the TPolicyItem structure above.
   1.132 +			//coverity[overrun-local]
   1.133 +			return *reinterpret_cast <const TInt*> (reinterpret_cast <const TUint8*> (iPolicy) + sizeof(iPolicy));
   1.134 +			}
   1.135 +		//Database object name will be after the end of TPolicyItem object + sizeof(TInt)
   1.136 +		inline const TUint16* NamePtr() const 
   1.137 +			{
   1.138 +			//See the comments of the TPolicyItem structure above.
   1.139 +			//coverity[overrun-local]
   1.140 +			return reinterpret_cast <const TUint16*> 
   1.141 +				(reinterpret_cast <const TUint8*> (iPolicy) + sizeof(iPolicy) + sizeof(TInt));
   1.142 +			}
   1.143 +		inline TUint16* NamePtr() 
   1.144 +			{
   1.145 +			//See the comments of the TPolicyItem structure above.
   1.146 +			//coverity[overrun-local]
   1.147 +			return reinterpret_cast <TUint16*> (reinterpret_cast <TUint8*> (iPolicy) + sizeof(iPolicy) + sizeof(TInt));
   1.148 +			}
   1.149 +		inline static TInt CalcSize(TInt aNameLen) 
   1.150 +			{
   1.151 +			return sizeof(TPolicyItem) + sizeof(TInt) + aNameLen * sizeof(TUint16);
   1.152 +			}
   1.153 +		};	
   1.154 +	RSqlBufFlat			iBufFlat;
   1.155 +	};
   1.156 +
   1.157 +/**
   1.158 +CSqlSecurityPolicy container read-only iterator.
   1.159 +
   1.160 +Usage:
   1.161 +
   1.162 +@code
   1.163 +//Valid CSqlSecurityPolicy instance.
   1.164 +CSqlSecurityPolicy* securityPolicy;
   1.165 +...
   1.166 +TSqlSecurityPolicyIterator it(*securityPolicy);
   1.167 +
   1.168 +//The following variables will be initialized on each iteration
   1.169 +RSqlSecurityPolicy::TObjectType objectType;
   1.170 +TPtrC objectName;
   1.171 +RSqlSecurityPolicy::TPolicyType policyType;
   1.172 +TSecurityPolicy policy;
   1.173 +
   1.174 +while(it.Next(objectType, objectName, policyType, policy))
   1.175 +	{
   1.176 +	//"objectType" is initialized with the object type (see RSqlSecurityPolicy::TObjectType)
   1.177 +	//"objectName" is initialized with the object name (database table name)
   1.178 +	//"policyType" is initialized with the policy type (see RSqlSecurityPolicy::TPolicyType)
   1.179 +	//"policy" is initialized with the security policy data (see TSecurityPolicy)
   1.180 +	<do something with the policy>;
   1.181 +	}
   1.182 +	
   1.183 +@endcode
   1.184 +
   1.185 +@see CSqlSecurityPolicy
   1.186 +@see RSqlSecurityPolicy::TObjectType
   1.187 +@see RSqlSecurityPolicy::TPolicyType
   1.188 +@see TSecurityPolicy
   1.189 +
   1.190 +@internalComponent
   1.191 +*/
   1.192 +NONSHARABLE_CLASS(TSqlSecurityPolicyIterator)
   1.193 +	{
   1.194 +public:
   1.195 +	TSqlSecurityPolicyIterator(const CSqlSecurityPolicy& aSqlSecurityPolicy);
   1.196 +	TBool Next(RSqlSecurityPolicy::TObjectType& aObjectType, TPtrC& aObjectName, RSqlSecurityPolicy::TPolicyType& aPolicyType, TSecurityPolicy& aPolicy);
   1.197 +
   1.198 +private:
   1.199 +	const RSqlBufFlat::TCell* 	iBegin;
   1.200 +	const RSqlBufFlat::TCell* 	iCurrent;
   1.201 +	const RSqlBufFlat::TCell* 	iEnd;
   1.202 +	TInt 						iCurPolicyIdx;
   1.203 +	
   1.204 +	};
   1.205 +
   1.206 +#endif//__SQLSECURITYIMPL_H__