os/persistentdata/persistentstorage/sql/SRC/Security/SqlSecurityImpl.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #ifndef __SQLSECURITYIMPL_H__
    17 #define __SQLSECURITYIMPL_H__
    18 
    19 #include <sqldb.h>
    20 #include "SqlBufFlat.h"		//RSqlBufFlat
    21 
    22 //Forward declarations
    23 class TSqlSecurityPolicyIterator;
    24 
    25 /**
    26 CSqlSecurityPolicy class implements RSqlSecurityPolicy.
    27 
    28 Current CSqlSecurityPolicy implementation uses a flat buffer (see RSqlBufFlat class) as a storage for the database 
    29 objects security policies.
    30 By default the flat buffer has threee entries:
    31  - entry 0 is a system entry and keeps the count of the database security entries;
    32  - entry 1 is reserved for the default database security policy. This is a nameless entry;
    33  - entry 2 is reserved for the database security policies: read, write and schema. This is a nameless entry;
    34 The flat buffer may have more entries if database object security policies are added. In this case each flat buffer entry
    35 has a 16-bit encoded database object name, kept at the end of the entry, after the database object security policy data and
    36 database object name length.
    37 Note: Current CSqlSecurityPolicy implementation can have no more than 13 database object security policy entries.
    38 An access to the flat buffer can be obtained via CSqlSecurityPolicy::BufFlat() methods (const and non-const).
    39 There are two ways to externalize (resp. internalize) CSqlSecurityPolicy data:
    40  - (1) using CSqlSecurityPolicy::ExternalizeL()/CSqlSecurityPolicy::InternalizeL();
    41  - (2) using CSqlSecurityPolicy::BufFlat() to get an access to the flat buffer and then externalize/internalize the buffer.
    42        CSqlSecurityPolicy class has only one data member which is the flat buffer;
    43 
    44 TSqlSecurityPolicyIterator class gives a convinient way to access the security policies kept inside the flat buffer.
    45 The given access is a read-only.
    46 
    47 @see RSqlSecurityPolicy
    48 @see RSqlBufFlat
    49 @see TSqlSecurityPolicyIterator
    50 @see RSqlSecurityPolicy::TPolicyType
    51 @see RSqlSecurityPolicy::TObjectType
    52 @see CSqlSecurityPolicy::NewL()
    53 @see CSqlSecurityPolicy::NewLC()
    54 @see CSqlSecurityPolicy::SetDefaultPolicy()
    55 @see CSqlSecurityPolicy::SetDbPolicy()
    56 @see CSqlSecurityPolicy::SetPolicy()
    57 @see CSqlSecurityPolicy::DefaultPolicy()
    58 @see CSqlSecurityPolicy::DbPolicy()
    59 @see CSqlSecurityPolicy::Policy()
    60 @see CSqlSecurityPolicy::ExternalizeL()
    61 @see CSqlSecurityPolicy::InternalizeL()
    62 @see CSqlSecurityPolicy::BufFlat()
    63 
    64 @internalComponent
    65 */
    66 NONSHARABLE_CLASS(CSqlSecurityPolicy) : public CBase
    67 	{
    68 	friend class TSqlSecurityPolicyIterator;
    69 	struct TPolicyItem;
    70 
    71 public:
    72 	static CSqlSecurityPolicy* NewL(const TSecurityPolicy& aDefaultPolicy);
    73 	static CSqlSecurityPolicy* NewLC(const TSecurityPolicy& aDefaultPolicy);
    74 	virtual ~CSqlSecurityPolicy();
    75 	void SetDefaultPolicy(const TSecurityPolicy& aPolicy);
    76 	void SetDbPolicy(RSqlSecurityPolicy::TPolicyType aPolicyType, const TSecurityPolicy& aPolicy);
    77 	TInt SetPolicy(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName, RSqlSecurityPolicy::TPolicyType aPolicyType, const TSecurityPolicy& aPolicy);
    78 	TSecurityPolicy DefaultPolicy() const;
    79 	TSecurityPolicy DbPolicy(RSqlSecurityPolicy::TPolicyType aPolicyType) const;
    80 	TSecurityPolicy Policy(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName, RSqlSecurityPolicy::TPolicyType aPolicyType);
    81 	TInt Count() const;
    82 	const RSqlBufFlat&	BufFlat() const;
    83 	RSqlBufFlat& BufFlat();
    84 
    85 private:
    86 	CSqlSecurityPolicy();
    87 	void ConstructL(const TSecurityPolicy& aDefaultPolicy);
    88 	inline static TInt PolicyType2Index(RSqlSecurityPolicy::TPolicyType aPolicyType);
    89 	inline static TPolicyItem* PolicyItemPtr(const RSqlBufFlat::TCell* aBegin, const RSqlBufFlat::TCell* aCurrent);
    90 	const TPolicyItem* FindPolicyItemPtr(RSqlSecurityPolicy::TObjectType aObjectType, const TDesC& aObjectName) const;
    91 	void SetCount(TInt aCount);
    92 	
    93 private:
    94 	enum {
    95 		 EPolicyTypeCount	= 3,					//Schema, Read, Write policy types
    96 		 EMaxCount 			= 16,					//iBufFlat max element count. The database cannot have more than EMaxCount security policies.
    97 		 ECountIdx			= 0,					//The index of the policy counter
    98 		 EDefaultPolicyIdx	= 1,					//The index of the default policy type in iBufFlat
    99 		 EDbPolicyIdx		= 2						//The index of the database policy type in iBufFlat
   100 		 };					
   101 	
   102 	//The database security policies are kept in objects of TPolicyItem type.
   103 	//If particular TPolicyItem object represents the database security policies, then the memory layout is:
   104 	//    [SchemaPolicy]
   105 	//    [ReadPolicy]
   106 	//    [WritePolicy]
   107 	//If particular TPolicyItem object represents table security policies, then the memory layout is:
   108 	//    [SchemaPolicy]
   109 	//    [ReadPolicy]
   110 	//    [WritePolicy]
   111 	//    [The length of the table name - 32-bit integer]
   112 	//    [The table name]
   113 	//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.
   114 	//There SQL platform security interface currently supports only database security policies, but the implementation is capable of handling also table security policies.
   115 	struct TPolicyItem
   116 		{
   117 		enum {EPolicyTypeCount	= 3};				//Schema, Read, Write policy types. The same as the declaration above.
   118 		TSecurityPolicy iPolicy[EPolicyTypeCount];	//Database object policies
   119 		//Database object name size will be after the end of TPolicyItem object
   120 		inline TInt* NameSizePtr() 
   121 			{
   122 			//See the comments of the TPolicyItem structure above.
   123 			//coverity[overrun-local]
   124 			return reinterpret_cast <TInt*> (reinterpret_cast <TUint8*> (iPolicy) + sizeof(iPolicy));
   125 			}
   126 		inline TInt NameSize() const 
   127 			{
   128 			//See the comments of the TPolicyItem structure above.
   129 			//coverity[overrun-local]
   130 			return *reinterpret_cast <const TInt*> (reinterpret_cast <const TUint8*> (iPolicy) + sizeof(iPolicy));
   131 			}
   132 		//Database object name will be after the end of TPolicyItem object + sizeof(TInt)
   133 		inline const TUint16* NamePtr() const 
   134 			{
   135 			//See the comments of the TPolicyItem structure above.
   136 			//coverity[overrun-local]
   137 			return reinterpret_cast <const TUint16*> 
   138 				(reinterpret_cast <const TUint8*> (iPolicy) + sizeof(iPolicy) + sizeof(TInt));
   139 			}
   140 		inline TUint16* NamePtr() 
   141 			{
   142 			//See the comments of the TPolicyItem structure above.
   143 			//coverity[overrun-local]
   144 			return reinterpret_cast <TUint16*> (reinterpret_cast <TUint8*> (iPolicy) + sizeof(iPolicy) + sizeof(TInt));
   145 			}
   146 		inline static TInt CalcSize(TInt aNameLen) 
   147 			{
   148 			return sizeof(TPolicyItem) + sizeof(TInt) + aNameLen * sizeof(TUint16);
   149 			}
   150 		};	
   151 	RSqlBufFlat			iBufFlat;
   152 	};
   153 
   154 /**
   155 CSqlSecurityPolicy container read-only iterator.
   156 
   157 Usage:
   158 
   159 @code
   160 //Valid CSqlSecurityPolicy instance.
   161 CSqlSecurityPolicy* securityPolicy;
   162 ...
   163 TSqlSecurityPolicyIterator it(*securityPolicy);
   164 
   165 //The following variables will be initialized on each iteration
   166 RSqlSecurityPolicy::TObjectType objectType;
   167 TPtrC objectName;
   168 RSqlSecurityPolicy::TPolicyType policyType;
   169 TSecurityPolicy policy;
   170 
   171 while(it.Next(objectType, objectName, policyType, policy))
   172 	{
   173 	//"objectType" is initialized with the object type (see RSqlSecurityPolicy::TObjectType)
   174 	//"objectName" is initialized with the object name (database table name)
   175 	//"policyType" is initialized with the policy type (see RSqlSecurityPolicy::TPolicyType)
   176 	//"policy" is initialized with the security policy data (see TSecurityPolicy)
   177 	<do something with the policy>;
   178 	}
   179 	
   180 @endcode
   181 
   182 @see CSqlSecurityPolicy
   183 @see RSqlSecurityPolicy::TObjectType
   184 @see RSqlSecurityPolicy::TPolicyType
   185 @see TSecurityPolicy
   186 
   187 @internalComponent
   188 */
   189 NONSHARABLE_CLASS(TSqlSecurityPolicyIterator)
   190 	{
   191 public:
   192 	TSqlSecurityPolicyIterator(const CSqlSecurityPolicy& aSqlSecurityPolicy);
   193 	TBool Next(RSqlSecurityPolicy::TObjectType& aObjectType, TPtrC& aObjectName, RSqlSecurityPolicy::TPolicyType& aPolicyType, TSecurityPolicy& aPolicy);
   194 
   195 private:
   196 	const RSqlBufFlat::TCell* 	iBegin;
   197 	const RSqlBufFlat::TCell* 	iCurrent;
   198 	const RSqlBufFlat::TCell* 	iEnd;
   199 	TInt 						iCurPolicyIdx;
   200 	
   201 	};
   202 
   203 #endif//__SQLSECURITYIMPL_H__