sl@0: // Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "SqlSecurityImpl.h" sl@0: #include "SqlAssert.h" sl@0: #include "OstTraceDefinitions.h" sl@0: #ifdef OST_TRACE_COMPILER_IN_USE sl@0: #include "SqlSecurityTraces.h" sl@0: #endif sl@0: #include "SqlTraceDef.h" sl@0: sl@0: const TInt32 KEndOfSPStream = -1;//If found in the stream, given as an argument to RSqlSecurityPolicy::InternalizeL(), sl@0: //then there are no more policies in the stream. sl@0: sl@0: /** sl@0: Initializes RSqlSecurityPolicy instance data members with their default values. sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C RSqlSecurityPolicy::RSqlSecurityPolicy() : sl@0: iImpl(NULL) sl@0: { sl@0: } sl@0: sl@0: /** sl@0: Initializes RSqlSecurityPolicy instance. sl@0: sl@0: @param aDefaultPolicy Default security policy which will be used for the database and all database objects. sl@0: sl@0: @return KErrNone, the operation has completed successfully; sl@0: KErrNoMemory, an out of memory condition has occured. sl@0: Note that database specific errors categorised as ESqlDbError, and sl@0: other system-wide error codes may also be returned. sl@0: sl@0: @see TSecurityPolicy sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C TInt RSqlSecurityPolicy::Create(const TSecurityPolicy& aDefaultPolicy) sl@0: { sl@0: SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATE_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::Create;aDefaultPolicy=0x%X", (TUint)this, (TUint)&aDefaultPolicy)); sl@0: TRAPD(err, CreateL(aDefaultPolicy)); sl@0: SQL_TRACE_BORDER(OstTraceExt3(TRACE_BORDER, RSQLSECURITYPOLICY_CREATE_EXIT, "Exit;0x%X;RSqlSecurityPolicy::Create;iImpl=0x%X;err=%d", (TUint)this, (TUint)iImpl, err)); sl@0: return err; sl@0: } sl@0: sl@0: /** sl@0: Initializes RSqlSecurityPolicy instance. sl@0: sl@0: @param aDefaultPolicy Default security policy which will be used for the database and all database objects. sl@0: sl@0: @leave KErrNoMemory, an out of memory condition has occured. sl@0: Note that database specific errors categorised as ESqlDbError, and sl@0: other system-wide error codes may also be returned. sl@0: sl@0: @see TSecurityPolicy sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C void RSqlSecurityPolicy::CreateL(const TSecurityPolicy& aDefaultPolicy) sl@0: { sl@0: SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATEL_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::CreateL;aDefaultPolicy=0x%X", (TUint)this, (TUint)&aDefaultPolicy)); sl@0: iImpl = CSqlSecurityPolicy::NewL(aDefaultPolicy); sl@0: SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::CreateL;iImpl=0x%X", (TUint)this, (TUint)iImpl)); sl@0: } sl@0: sl@0: /** sl@0: Frees the allocated by RSqlSecurityPolicy instance memory and other resources. sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C void RSqlSecurityPolicy::Close() sl@0: { sl@0: SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_CLOSE_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::Close", (TUint)this)); sl@0: delete iImpl; sl@0: SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CLOSE_EXIT, "Exit;0x%X;RSqlSecurityPolicy::Close;iImpl=0x%X", (TUint)this, (TUint)iImpl)); sl@0: iImpl = NULL; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Sets a database security policy of a specific type. sl@0: sl@0: Sets database security policy (aPolicy argument) of aPolicyType type. sl@0: If the aPolicyType database security policy has already been set then it will be replaced with the supplied policy. sl@0: sl@0: @param aPolicyType Database security policy type: RSqlSecurityPolicy::ESchema, RSqlSecurityPolicy::ERead, RSqlSecurityPolicy::EWrite. sl@0: @param aPolicy The database security policy. sl@0: sl@0: @panic SqlDb 4 Invalid aPolicyType value. sl@0: sl@0: @return KErrNone sl@0: sl@0: @see RSqlSecurityPolicy::TPolicyType sl@0: @see TSecurityPolicy sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C TInt RSqlSecurityPolicy::SetDbPolicy(TPolicyType aPolicyType, const TSecurityPolicy& aPolicy) sl@0: { sl@0: __ASSERT_ALWAYS(aPolicyType >= ESchemaPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument)); sl@0: Impl().SetDbPolicy(aPolicyType, aPolicy); sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** sl@0: Sets a database object security policy of a specific type. sl@0: sl@0: If there is no entry in the security policy container for the object with aObjectName name, then a new entry for this sl@0: object will be created and all object security policies will be initialized with the default security policy. sl@0: The specific database object policy, refered by aPolicyType parameter, will be set after that. sl@0: sl@0: If an entry for aObjectName object already exists, its security policy of "aPolicyType" type will be sl@0: reinitialized with the data of aPolicy parameter. sl@0: sl@0: @param aObjectType Database object type. At the moment there is only one database object type - RSqlSecurityPolicy::ETable. sl@0: @param aObjectName Database object name. It cannot be a null descriptor. sl@0: @param aPolicyType Database object security policy type: RSqlSecurityPolicy::EReadPolicy, RSqlSecurityPolicy::EWritePolicy. sl@0: @param aPolicy Database security policy. sl@0: sl@0: @return KErrNone, the operation has completed successfully; sl@0: KErrNoMemory, an out of memory condition has occured. sl@0: sl@0: @panic SqlDb 4 Invalid aPolicyType value. sl@0: @panic SqlDb 4 Invalid aObjectType value (It has to be RSqlSecurityPolicy::ETable). sl@0: @panic SqlDb 4 Invalid aObjectName value (Null descriptor). sl@0: sl@0: @see RSqlSecurityPolicy::TObjectType sl@0: @see RSqlSecurityPolicy::TPolicyType sl@0: @see TSecurityPolicy sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C TInt RSqlSecurityPolicy::SetPolicy(TObjectType aObjectType, const TDesC& aObjectName, sl@0: TPolicyType aPolicyType, const TSecurityPolicy& aPolicy) sl@0: { sl@0: __ASSERT_ALWAYS(aObjectType == ETable, __SQLPANIC(ESqlPanicBadArgument)); sl@0: __ASSERT_ALWAYS(aObjectName.Length() > 0, __SQLPANIC(ESqlPanicBadArgument)); sl@0: __ASSERT_ALWAYS(aPolicyType >= EReadPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument)); sl@0: return Impl().SetPolicy(aObjectType, aObjectName, aPolicyType, aPolicy); sl@0: } sl@0: sl@0: /** sl@0: Gets the default database security policy. sl@0: sl@0: @return The default security policy. sl@0: sl@0: @see TSecurityPolicy sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C TSecurityPolicy RSqlSecurityPolicy::DefaultPolicy() const sl@0: { sl@0: return Impl().DefaultPolicy(); sl@0: } sl@0: sl@0: /** sl@0: Gets a database security policy of the specified type. sl@0: sl@0: @param aPolicyType Database security policy type: RSqlSecurityPolicy::ESchemaPolicy, RSqlSecurityPolicy::EReadPolicy, sl@0: RSqlSecurityPolicy::EWritePolicy. sl@0: sl@0: @return The requested database security policy. sl@0: sl@0: @panic SqlDb 4 Invalid aPolicyType value. sl@0: sl@0: @see RSqlSecurityPolicy::TPolicyType sl@0: @see TSecurityPolicy sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C TSecurityPolicy RSqlSecurityPolicy::DbPolicy(TPolicyType aPolicyType) const sl@0: { sl@0: __ASSERT_ALWAYS(aPolicyType >= ESchemaPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument)); sl@0: return Impl().DbPolicy(aPolicyType); sl@0: } sl@0: sl@0: /** sl@0: Gets a database object security policy of the specified type. sl@0: sl@0: If no security policy of the specified type exists for that database object - the default security policy sl@0: will be returned. sl@0: sl@0: @param aObjectType Database object type. At the moment there is only one database object type - RSqlSecurityPolicy::ETable. sl@0: @param aObjectName Database object name. It cannot be a null descriptor. sl@0: @param aPolicyType Database object security policy type: RSqlSecurityPolicy::EReadPolicy, RSqlSecurityPolicy::EWritePolicy. sl@0: sl@0: @return The requested security policy. sl@0: sl@0: @panic SqlDb 4 Invalid aPolicyType value. sl@0: @panic SqlDb 4 Invalid aObjectType value (It has to be RSqlSecurityPolicy::ETable). sl@0: @panic SqlDb 4 Invalid aObjectName value (Null descriptor). sl@0: sl@0: @see RSqlSecurityPolicy::TObjectType sl@0: @see RSqlSecurityPolicy::TPolicyType sl@0: @see TSecurityPolicy sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C TSecurityPolicy RSqlSecurityPolicy::Policy(TObjectType aObjectType, const TDesC& aObjectName, sl@0: TPolicyType aPolicyType) const sl@0: { sl@0: __ASSERT_ALWAYS(aObjectType == ETable, __SQLPANIC(ESqlPanicBadArgument)); sl@0: __ASSERT_ALWAYS(aObjectName.Length() > 0, __SQLPANIC(ESqlPanicBadArgument)); sl@0: __ASSERT_ALWAYS(aPolicyType >= EReadPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument)); sl@0: return Impl().Policy(aObjectType, aObjectName, aPolicyType); sl@0: } sl@0: sl@0: /** sl@0: Externalizes RSqlSecurityPolicy instance to a write stream. sl@0: sl@0: @param aStream Stream to which RSqlSecurityPolicy instance should be externalised. sl@0: sl@0: @leave KErrNoMemory, an out of memory condition has occured. sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C void RSqlSecurityPolicy::ExternalizeL(RWriteStream& aStream) const sl@0: { sl@0: 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())); sl@0: RSqlSecurityPolicy::TObjectType objectType; sl@0: TPtrC objectName; sl@0: RSqlSecurityPolicy::TPolicyType policyType; sl@0: TSecurityPolicy policy; sl@0: //Default policy sl@0: policy = Impl().DefaultPolicy(); sl@0: aStream << policy.Package(); sl@0: //Database policies sl@0: policy = Impl().DbPolicy(RSqlSecurityPolicy::ESchemaPolicy); sl@0: aStream << policy.Package(); sl@0: policy = Impl().DbPolicy(RSqlSecurityPolicy::EReadPolicy); sl@0: aStream << policy.Package(); sl@0: policy = Impl().DbPolicy(RSqlSecurityPolicy::EWritePolicy); sl@0: aStream << policy.Package(); sl@0: //Database object policies sl@0: TSqlSecurityPolicyIterator it(Impl()); sl@0: while(it.Next(objectType, objectName, policyType, policy)) sl@0: { sl@0: aStream << static_cast (objectType); sl@0: aStream << objectName; sl@0: aStream << static_cast (policyType); sl@0: aStream << policy.Package(); sl@0: } sl@0: //Object policy stream - end sl@0: aStream << KEndOfSPStream; sl@0: SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_EXTERNALIZEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::ExternalizeL", (TUint)this)); sl@0: } sl@0: sl@0: /** sl@0: Initializes RSqlSecurityPolicy instance from a stream. sl@0: In case of an error the original security policy data is preserved. sl@0: sl@0: @param aStream A read stream containing the data with which the RSqlSecurityPolicy instance will be initialized. sl@0: sl@0: @leave KErrNoMemory, an out of memory condition has occured. sl@0: Note that the function may leave with other system-wide error codes. sl@0: sl@0: @capability None sl@0: */ sl@0: EXPORT_C void RSqlSecurityPolicy::InternalizeL(RReadStream& aStream) sl@0: { sl@0: 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())); sl@0: TSecurityPolicy policy; sl@0: TBuf8 policyBuf; sl@0: //Default policy sl@0: aStream >> policyBuf; sl@0: policy.Set(policyBuf); sl@0: //Create new sql security policy object and initialize it with the policies read from the input stream sl@0: RSqlSecurityPolicy newPolicy; sl@0: newPolicy.CreateL(policy); sl@0: CleanupClosePushL(newPolicy); sl@0: //Database policies sl@0: aStream >> policyBuf; sl@0: policy.Set(policyBuf); sl@0: __SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, policy)); sl@0: aStream >> policyBuf; sl@0: policy.Set(policyBuf); sl@0: __SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, policy)); sl@0: aStream >> policyBuf; sl@0: policy.Set(policyBuf); sl@0: __SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, policy)); sl@0: //Database object policies sl@0: for(;;) sl@0: { sl@0: TInt32 objectType; sl@0: aStream >> objectType; sl@0: if(objectType == KEndOfSPStream) sl@0: { sl@0: break; sl@0: } sl@0: TBuf objectName; sl@0: aStream >> objectName; sl@0: TInt32 policyType; sl@0: aStream >> policyType; sl@0: aStream >> policyBuf; sl@0: policy.Set(policyBuf); sl@0: __SQLLEAVE_IF_ERROR(newPolicy.SetPolicy(static_cast (objectType), objectName, static_cast (policyType), policy)); sl@0: } sl@0: //Swap the original sql security policy with the new sql security policy sl@0: CSqlSecurityPolicy* temp = newPolicy.iImpl; sl@0: newPolicy.iImpl = iImpl; sl@0: iImpl = temp; sl@0: //Destroy the old policy (which was swapped) sl@0: CleanupStack::PopAndDestroy(&newPolicy); sl@0: SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_INTERNALIZEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::InternalizeL", (TUint)this)); sl@0: } sl@0: sl@0: /** sl@0: Destroys the existing iImpl object and replaces it with aImpl parameter. sl@0: sl@0: @internalComponent sl@0: */ sl@0: void RSqlSecurityPolicy::Set(CSqlSecurityPolicy& aImpl) sl@0: { sl@0: delete iImpl; sl@0: iImpl = &aImpl; sl@0: } sl@0: sl@0: /** sl@0: @return A reference to the implementation object. sl@0: sl@0: @panic SqlDb 2 Create() has not previously been called on this RSqlSecurityPolicy object. sl@0: sl@0: @internalComponent sl@0: */ sl@0: CSqlSecurityPolicy& RSqlSecurityPolicy::Impl() const sl@0: { sl@0: __ASSERT_ALWAYS(iImpl != NULL, __SQLPANIC(ESqlPanicInvalidObj)); sl@0: return *iImpl; sl@0: }