os/persistentdata/persistentstorage/sql/SRC/Security/SqlSecurity.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/SRC/Security/SqlSecurity.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,342 @@
     1.4 +// Copyright (c) 2005-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 +#include "SqlSecurityImpl.h"
    1.20 +#include "SqlAssert.h"
    1.21 +#include "OstTraceDefinitions.h"
    1.22 +#ifdef OST_TRACE_COMPILER_IN_USE
    1.23 +#include "SqlSecurityTraces.h"
    1.24 +#endif
    1.25 +#include "SqlTraceDef.h"
    1.26 +
    1.27 +const TInt32 KEndOfSPStream = -1;//If found in the stream, given as an argument to RSqlSecurityPolicy::InternalizeL(),
    1.28 +							     //then there are no more policies in the stream.
    1.29 +
    1.30 +/**
    1.31 +Initializes RSqlSecurityPolicy instance data members with their default values.
    1.32 +
    1.33 +@capability None
    1.34 +*/
    1.35 +EXPORT_C RSqlSecurityPolicy::RSqlSecurityPolicy() :
    1.36 +	iImpl(NULL)
    1.37 +	{
    1.38 +	}
    1.39 +
    1.40 +/**
    1.41 +Initializes RSqlSecurityPolicy instance.
    1.42 +
    1.43 +@param aDefaultPolicy Default security policy which will be used for the database and all database objects.
    1.44 +
    1.45 +@return KErrNone, the operation has completed successfully;
    1.46 +		KErrNoMemory, an out of memory condition has occured.
    1.47 +                      Note that database specific errors categorised as ESqlDbError, and
    1.48 +                      other system-wide error codes may also be returned.
    1.49 +
    1.50 +@see TSecurityPolicy
    1.51 +
    1.52 +@capability None
    1.53 +*/
    1.54 +EXPORT_C TInt RSqlSecurityPolicy::Create(const TSecurityPolicy& aDefaultPolicy)
    1.55 +	{
    1.56 +	SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATE_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::Create;aDefaultPolicy=0x%X", (TUint)this, (TUint)&aDefaultPolicy));
    1.57 +	TRAPD(err, CreateL(aDefaultPolicy));
    1.58 +    SQL_TRACE_BORDER(OstTraceExt3(TRACE_BORDER, RSQLSECURITYPOLICY_CREATE_EXIT, "Exit;0x%X;RSqlSecurityPolicy::Create;iImpl=0x%X;err=%d", (TUint)this, (TUint)iImpl, err));
    1.59 +	return err;
    1.60 +	}
    1.61 +
    1.62 +/**
    1.63 +Initializes RSqlSecurityPolicy instance.
    1.64 +
    1.65 +@param aDefaultPolicy Default security policy which will be used for the database and all database objects.
    1.66 +
    1.67 +@leave  KErrNoMemory, an out of memory condition has occured.
    1.68 +                      Note that database specific errors categorised as ESqlDbError, and
    1.69 +                      other system-wide error codes may also be returned.
    1.70 +
    1.71 +@see TSecurityPolicy
    1.72 +
    1.73 +@capability None
    1.74 +*/
    1.75 +EXPORT_C void RSqlSecurityPolicy::CreateL(const TSecurityPolicy& aDefaultPolicy)
    1.76 +	{
    1.77 +    SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATEL_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::CreateL;aDefaultPolicy=0x%X", (TUint)this, (TUint)&aDefaultPolicy));
    1.78 +	iImpl = CSqlSecurityPolicy::NewL(aDefaultPolicy);
    1.79 +    SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::CreateL;iImpl=0x%X", (TUint)this, (TUint)iImpl));
    1.80 +	}
    1.81 +
    1.82 +/**
    1.83 +Frees the allocated by RSqlSecurityPolicy instance memory and other resources.
    1.84 +
    1.85 +@capability None
    1.86 +*/
    1.87 +EXPORT_C void RSqlSecurityPolicy::Close()
    1.88 +	{
    1.89 +    SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_CLOSE_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::Close", (TUint)this));
    1.90 +	delete iImpl;
    1.91 +    SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CLOSE_EXIT, "Exit;0x%X;RSqlSecurityPolicy::Close;iImpl=0x%X", (TUint)this, (TUint)iImpl));
    1.92 +	iImpl = NULL;
    1.93 +	}
    1.94 +
    1.95 +
    1.96 +/**
    1.97 +Sets a database security policy of a specific type.
    1.98 +
    1.99 +Sets database security policy (aPolicy argument) of aPolicyType type.
   1.100 +If the aPolicyType database security policy has already been set then it will be replaced with the supplied policy.
   1.101 +
   1.102 +@param aPolicyType Database security policy type: RSqlSecurityPolicy::ESchema, RSqlSecurityPolicy::ERead, RSqlSecurityPolicy::EWrite.
   1.103 +@param aPolicy The database security policy.
   1.104 +
   1.105 +@panic SqlDb 4 Invalid aPolicyType value.
   1.106 +
   1.107 +@return KErrNone
   1.108 +
   1.109 +@see RSqlSecurityPolicy::TPolicyType
   1.110 +@see TSecurityPolicy
   1.111 +
   1.112 +@capability None
   1.113 +*/
   1.114 +EXPORT_C TInt RSqlSecurityPolicy::SetDbPolicy(TPolicyType aPolicyType, const TSecurityPolicy& aPolicy)
   1.115 +	{
   1.116 +	__ASSERT_ALWAYS(aPolicyType >= ESchemaPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument));
   1.117 +	Impl().SetDbPolicy(aPolicyType, aPolicy);
   1.118 +	return KErrNone;
   1.119 +	}
   1.120 +	
   1.121 +/**
   1.122 +Sets a database object security policy of a specific type.
   1.123 +
   1.124 +If there is no entry in the security policy container for the object with aObjectName name, then a new entry for this 
   1.125 +object will be created and all object security policies will be initialized with the default security policy. 
   1.126 +The specific database object policy, refered by aPolicyType parameter, will be set after that.
   1.127 +
   1.128 +If an entry for aObjectName object already exists, its security policy of "aPolicyType" type will be 
   1.129 +reinitialized with the data of aPolicy parameter.
   1.130 +
   1.131 +@param aObjectType Database object type. At the moment there is only one database object type - RSqlSecurityPolicy::ETable.
   1.132 +@param aObjectName Database object name. It cannot be a null descriptor.
   1.133 +@param aPolicyType Database object security policy type: RSqlSecurityPolicy::EReadPolicy, RSqlSecurityPolicy::EWritePolicy.
   1.134 +@param aPolicy Database security policy.
   1.135 +
   1.136 +@return KErrNone, the operation has completed successfully;
   1.137 +		KErrNoMemory, an out of memory condition has occured.
   1.138 +
   1.139 +@panic SqlDb 4 Invalid aPolicyType value.
   1.140 +@panic SqlDb 4 Invalid aObjectType value (It has to be RSqlSecurityPolicy::ETable).
   1.141 +@panic SqlDb 4 Invalid aObjectName value (Null descriptor).
   1.142 +
   1.143 +@see RSqlSecurityPolicy::TObjectType
   1.144 +@see RSqlSecurityPolicy::TPolicyType
   1.145 +@see TSecurityPolicy
   1.146 +
   1.147 +@capability None
   1.148 +*/
   1.149 +EXPORT_C TInt RSqlSecurityPolicy::SetPolicy(TObjectType aObjectType, const TDesC& aObjectName, 
   1.150 +									  TPolicyType aPolicyType, const TSecurityPolicy& aPolicy)
   1.151 +	{
   1.152 +	__ASSERT_ALWAYS(aObjectType == ETable, __SQLPANIC(ESqlPanicBadArgument));
   1.153 +	__ASSERT_ALWAYS(aObjectName.Length() > 0, __SQLPANIC(ESqlPanicBadArgument));
   1.154 +	__ASSERT_ALWAYS(aPolicyType >= EReadPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument));
   1.155 +	return Impl().SetPolicy(aObjectType, aObjectName, aPolicyType, aPolicy);
   1.156 +	}
   1.157 +
   1.158 +/**
   1.159 +Gets the default database security policy.
   1.160 +
   1.161 +@return The default security policy.
   1.162 +				   
   1.163 +@see TSecurityPolicy
   1.164 +
   1.165 +@capability None
   1.166 +*/	
   1.167 +EXPORT_C TSecurityPolicy RSqlSecurityPolicy::DefaultPolicy() const
   1.168 +	{
   1.169 +	return Impl().DefaultPolicy();
   1.170 +	}
   1.171 +
   1.172 +/**
   1.173 +Gets a database security policy of the specified type.
   1.174 +
   1.175 +@param aPolicyType Database security policy type: RSqlSecurityPolicy::ESchemaPolicy, RSqlSecurityPolicy::EReadPolicy, 
   1.176 +				   RSqlSecurityPolicy::EWritePolicy.
   1.177 +
   1.178 +@return The requested database security policy.
   1.179 +				   
   1.180 +@panic SqlDb 4 Invalid aPolicyType value.
   1.181 +
   1.182 +@see RSqlSecurityPolicy::TPolicyType
   1.183 +@see TSecurityPolicy
   1.184 +
   1.185 +@capability None
   1.186 +*/	
   1.187 +EXPORT_C TSecurityPolicy RSqlSecurityPolicy::DbPolicy(TPolicyType aPolicyType) const
   1.188 +	{
   1.189 +	__ASSERT_ALWAYS(aPolicyType >= ESchemaPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument));
   1.190 +	return Impl().DbPolicy(aPolicyType);
   1.191 +	}
   1.192 +	
   1.193 +/**
   1.194 +Gets a database object security policy of the specified type.
   1.195 +
   1.196 +If no security policy of the specified type exists for that database object - the default security policy
   1.197 +will be returned.
   1.198 +
   1.199 +@param aObjectType Database object type. At the moment there is only one database object type - RSqlSecurityPolicy::ETable.
   1.200 +@param aObjectName Database object name. It cannot be a null descriptor.
   1.201 +@param aPolicyType Database object security policy type: RSqlSecurityPolicy::EReadPolicy, RSqlSecurityPolicy::EWritePolicy.
   1.202 +
   1.203 +@return The requested security policy.
   1.204 +
   1.205 +@panic SqlDb 4 Invalid aPolicyType value.
   1.206 +@panic SqlDb 4 Invalid aObjectType value (It has to be RSqlSecurityPolicy::ETable).
   1.207 +@panic SqlDb 4 Invalid aObjectName value (Null descriptor).
   1.208 +
   1.209 +@see RSqlSecurityPolicy::TObjectType
   1.210 +@see RSqlSecurityPolicy::TPolicyType
   1.211 +@see TSecurityPolicy
   1.212 +
   1.213 +@capability None
   1.214 +*/
   1.215 +EXPORT_C TSecurityPolicy RSqlSecurityPolicy::Policy(TObjectType aObjectType, const TDesC& aObjectName, 
   1.216 +												 TPolicyType aPolicyType) const
   1.217 +	{
   1.218 +	__ASSERT_ALWAYS(aObjectType == ETable, __SQLPANIC(ESqlPanicBadArgument));
   1.219 +	__ASSERT_ALWAYS(aObjectName.Length() > 0, __SQLPANIC(ESqlPanicBadArgument));
   1.220 +	__ASSERT_ALWAYS(aPolicyType >= EReadPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument));
   1.221 +	return Impl().Policy(aObjectType, aObjectName, aPolicyType);
   1.222 +	}
   1.223 +
   1.224 +/**
   1.225 +Externalizes RSqlSecurityPolicy instance to a write stream.
   1.226 +
   1.227 +@param aStream Stream to which RSqlSecurityPolicy instance should be externalised.
   1.228 +
   1.229 +@leave KErrNoMemory, an out of memory condition has occured.
   1.230 +
   1.231 +@capability None
   1.232 +*/
   1.233 +EXPORT_C void RSqlSecurityPolicy::ExternalizeL(RWriteStream& aStream) const
   1.234 +	{
   1.235 +    SQL_TRACE_BORDER(OstTraceExt3(TRACE_BORDER, RSQLSECURITYPOLICY_EXTERNALIZEL_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::ExternalizeL;aStream=0x%X;aStream.Sink()=0x%X", (TUint)this, (TUint)&aStream, (TUint)aStream.Sink()));
   1.236 +	RSqlSecurityPolicy::TObjectType objectType;
   1.237 +	TPtrC objectName;
   1.238 +	RSqlSecurityPolicy::TPolicyType policyType;
   1.239 +	TSecurityPolicy policy;
   1.240 +	//Default policy
   1.241 +	policy = Impl().DefaultPolicy();
   1.242 +	aStream << policy.Package();
   1.243 +	//Database policies
   1.244 +	policy = Impl().DbPolicy(RSqlSecurityPolicy::ESchemaPolicy);
   1.245 +	aStream << policy.Package();
   1.246 +	policy = Impl().DbPolicy(RSqlSecurityPolicy::EReadPolicy);
   1.247 +	aStream << policy.Package();
   1.248 +	policy = Impl().DbPolicy(RSqlSecurityPolicy::EWritePolicy);
   1.249 +	aStream << policy.Package();
   1.250 +	//Database object policies
   1.251 +	TSqlSecurityPolicyIterator it(Impl());
   1.252 +	while(it.Next(objectType, objectName, policyType, policy))
   1.253 +		{
   1.254 +		aStream << static_cast <TInt32> (objectType);
   1.255 +		aStream << objectName;
   1.256 +		aStream << static_cast <TInt32> (policyType);
   1.257 +		aStream << policy.Package();
   1.258 +		}
   1.259 +	//Object policy stream - end
   1.260 +	aStream << KEndOfSPStream;
   1.261 +    SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_EXTERNALIZEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::ExternalizeL", (TUint)this));
   1.262 +	}
   1.263 +	
   1.264 +/**
   1.265 +Initializes RSqlSecurityPolicy instance from a stream.
   1.266 +In case of an error the original security policy data is preserved.
   1.267 +
   1.268 +@param aStream A read stream containing the data with which the RSqlSecurityPolicy instance will be initialized.
   1.269 +
   1.270 +@leave KErrNoMemory, an out of memory condition has occured.
   1.271 +                     Note that the function may leave with other system-wide error codes.
   1.272 +
   1.273 +@capability None
   1.274 +*/
   1.275 +EXPORT_C void RSqlSecurityPolicy::InternalizeL(RReadStream& aStream)
   1.276 +	{
   1.277 +    SQL_TRACE_BORDER(OstTraceExt3(TRACE_BORDER, RSQLSECURITYPOLICY_INTERNALIZEL_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::InternalizeL;aStream=0x%X;aStream.Source()=0x%X", (TUint)this, (TUint)&aStream, (TUint)aStream.Source()));
   1.278 +	TSecurityPolicy policy;
   1.279 +	TBuf8<sizeof(TSecurityPolicy)> policyBuf;
   1.280 +	//Default policy
   1.281 +	aStream >> policyBuf;
   1.282 +	policy.Set(policyBuf);
   1.283 +	//Create new sql security policy object	and initialize it with the policies read from the input stream
   1.284 +	RSqlSecurityPolicy newPolicy;
   1.285 +	newPolicy.CreateL(policy);
   1.286 +	CleanupClosePushL(newPolicy);
   1.287 +	//Database policies
   1.288 +	aStream >> policyBuf;
   1.289 +	policy.Set(policyBuf);
   1.290 +	__SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, policy));
   1.291 +	aStream >> policyBuf;
   1.292 +	policy.Set(policyBuf);
   1.293 +	__SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, policy));
   1.294 +	aStream >> policyBuf;
   1.295 +	policy.Set(policyBuf);
   1.296 +	__SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, policy));
   1.297 +	//Database object policies
   1.298 +	for(;;)
   1.299 +		{
   1.300 +		TInt32 objectType;
   1.301 +		aStream >> objectType;
   1.302 +		if(objectType == KEndOfSPStream)
   1.303 +			{
   1.304 +			break;	
   1.305 +			}
   1.306 +		TBuf<KMaxFileName> objectName;
   1.307 +		aStream >> objectName;
   1.308 +		TInt32 policyType;
   1.309 +		aStream >> policyType;
   1.310 +		aStream >> policyBuf;
   1.311 +		policy.Set(policyBuf);
   1.312 +		__SQLLEAVE_IF_ERROR(newPolicy.SetPolicy(static_cast <RSqlSecurityPolicy::TObjectType> (objectType), objectName, static_cast <RSqlSecurityPolicy::TPolicyType> (policyType), policy));
   1.313 +		}
   1.314 +	//Swap the original sql security policy with the new sql security policy
   1.315 +	CSqlSecurityPolicy* temp = newPolicy.iImpl;
   1.316 +	newPolicy.iImpl = iImpl;
   1.317 +	iImpl = temp;
   1.318 +	//Destroy the old policy (which was swapped)
   1.319 +	CleanupStack::PopAndDestroy(&newPolicy);
   1.320 +    SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_INTERNALIZEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::InternalizeL", (TUint)this));
   1.321 +	}
   1.322 +
   1.323 +/**
   1.324 +Destroys the existing iImpl object and replaces it with aImpl parameter.
   1.325 +
   1.326 +@internalComponent
   1.327 +*/
   1.328 +void RSqlSecurityPolicy::Set(CSqlSecurityPolicy& aImpl)
   1.329 +	{
   1.330 +	delete iImpl;
   1.331 +	iImpl = &aImpl;
   1.332 +	}
   1.333 +
   1.334 +/**
   1.335 +@return A reference to the implementation object.
   1.336 +
   1.337 +@panic SqlDb 2 Create() has not previously been called on  this RSqlSecurityPolicy object.
   1.338 +
   1.339 +@internalComponent
   1.340 +*/
   1.341 +CSqlSecurityPolicy& RSqlSecurityPolicy::Impl() const
   1.342 +	{
   1.343 +	__ASSERT_ALWAYS(iImpl != NULL, __SQLPANIC(ESqlPanicInvalidObj));
   1.344 +	return *iImpl;	
   1.345 +	}