persistentstorage/sql/SRC/Security/SqlSecurity.cpp
changeset 0 08ec8eefde2f
child 15 fcc16690f446
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2005-2009 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 #include "SqlSecurityImpl.h"
       
    17 #include "SqlPanic.h"
       
    18 #include "UTraceSql.h"
       
    19 
       
    20 const TInt32 KEndOfSPStream = -1;//If found in the stream, given as an argument to RSqlSecurityPolicy::InternalizeL(),
       
    21 							     //then there are no more policies in the stream.
       
    22 
       
    23 /**
       
    24 Initializes RSqlSecurityPolicy instance data members with their default values.
       
    25 
       
    26 @capability None
       
    27 */
       
    28 EXPORT_C RSqlSecurityPolicy::RSqlSecurityPolicy() :
       
    29 	iImpl(NULL)
       
    30 	{
       
    31 	}
       
    32 
       
    33 /**
       
    34 Initializes RSqlSecurityPolicy instance.
       
    35 
       
    36 @param aDefaultPolicy Default security policy which will be used for the database and all database objects.
       
    37 
       
    38 @return KErrNone, the operation has completed successfully;
       
    39 		KErrNoMemory, an out of memory condition has occured.
       
    40                       Note that database specific errors categorised as ESqlDbError, and
       
    41                       other system-wide error codes may also be returned.
       
    42 
       
    43 @see TSecurityPolicy
       
    44 
       
    45 @capability None
       
    46 */
       
    47 EXPORT_C TInt RSqlSecurityPolicy::Create(const TSecurityPolicy& aDefaultPolicy)
       
    48 	{
       
    49 	SQLUTRACE_PROFILER(this);
       
    50 	TRAPD(err, CreateL(aDefaultPolicy));
       
    51 	return err;
       
    52 	}
       
    53 
       
    54 /**
       
    55 Initializes RSqlSecurityPolicy instance.
       
    56 
       
    57 @param aDefaultPolicy Default security policy which will be used for the database and all database objects.
       
    58 
       
    59 @leave  KErrNoMemory, an out of memory condition has occured.
       
    60                       Note that database specific errors categorised as ESqlDbError, and
       
    61                       other system-wide error codes may also be returned.
       
    62 
       
    63 @see TSecurityPolicy
       
    64 
       
    65 @capability None
       
    66 */
       
    67 EXPORT_C void RSqlSecurityPolicy::CreateL(const TSecurityPolicy& aDefaultPolicy)
       
    68 	{
       
    69 	SQLUTRACE_PROFILER(this);
       
    70 	iImpl = CSqlSecurityPolicy::NewL(aDefaultPolicy);
       
    71 	}
       
    72 
       
    73 /**
       
    74 Frees the allocated by RSqlSecurityPolicy instance memory and other resources.
       
    75 
       
    76 @capability None
       
    77 */
       
    78 EXPORT_C void RSqlSecurityPolicy::Close()
       
    79 	{
       
    80 	SQLUTRACE_PROFILER(this);
       
    81 	delete iImpl;
       
    82 	iImpl = NULL;
       
    83 	}
       
    84 
       
    85 
       
    86 /**
       
    87 Sets a database security policy of a specific type.
       
    88 
       
    89 Sets database security policy (aPolicy argument) of aPolicyType type.
       
    90 If the aPolicyType database security policy has already been set then it will be replaced with the supplied policy.
       
    91 
       
    92 @param aPolicyType Database security policy type: RSqlSecurityPolicy::ESchema, RSqlSecurityPolicy::ERead, RSqlSecurityPolicy::EWrite.
       
    93 @param aPolicy The database security policy.
       
    94 
       
    95 @panic SqlDb 4 Invalid aPolicyType value.
       
    96 
       
    97 @return KErrNone
       
    98 
       
    99 @see RSqlSecurityPolicy::TPolicyType
       
   100 @see TSecurityPolicy
       
   101 
       
   102 @capability None
       
   103 */
       
   104 EXPORT_C TInt RSqlSecurityPolicy::SetDbPolicy(TPolicyType aPolicyType, const TSecurityPolicy& aPolicy)
       
   105 	{
       
   106 	SQLUTRACE_PROFILER(this);
       
   107 	__SQLASSERT_ALWAYS(aPolicyType >= ESchemaPolicy && aPolicyType <= EWritePolicy, ESqlPanicBadArgument);
       
   108 	return Impl().SetDbPolicy(aPolicyType, aPolicy);
       
   109 	}
       
   110 	
       
   111 /**
       
   112 Sets a database object security policy of a specific type.
       
   113 
       
   114 If there is no entry in the security policy container for the object with aObjectName name, then a new entry for this 
       
   115 object will be created and all object security policies will be initialized with the default security policy. 
       
   116 The specific database object policy, refered by aPolicyType parameter, will be set after that.
       
   117 
       
   118 If an entry for aObjectName object already exists, its security policy of "aPolicyType" type will be 
       
   119 reinitialized with the data of aPolicy parameter.
       
   120 
       
   121 @param aObjectType Database object type. At the moment there is only one database object type - RSqlSecurityPolicy::ETable.
       
   122 @param aObjectName Database object name. It cannot be a null descriptor.
       
   123 @param aPolicyType Database object security policy type: RSqlSecurityPolicy::EReadPolicy, RSqlSecurityPolicy::EWritePolicy.
       
   124 @param aPolicy Database security policy.
       
   125 
       
   126 @return KErrNone, the operation has completed successfully;
       
   127 		KErrNoMemory, an out of memory condition has occured.
       
   128 
       
   129 @panic SqlDb 4 Invalid aPolicyType value.
       
   130 @panic SqlDb 4 Invalid aObjectType value (It has to be RSqlSecurityPolicy::ETable).
       
   131 @panic SqlDb 4 Invalid aObjectName value (Null descriptor).
       
   132 
       
   133 @see RSqlSecurityPolicy::TObjectType
       
   134 @see RSqlSecurityPolicy::TPolicyType
       
   135 @see TSecurityPolicy
       
   136 
       
   137 @capability None
       
   138 */
       
   139 EXPORT_C TInt RSqlSecurityPolicy::SetPolicy(TObjectType aObjectType, const TDesC& aObjectName, 
       
   140 									  TPolicyType aPolicyType, const TSecurityPolicy& aPolicy)
       
   141 	{
       
   142 	SQLUTRACE_PROFILER(this);
       
   143 	__SQLASSERT_ALWAYS(aObjectType == ETable, ESqlPanicBadArgument);
       
   144 	__SQLASSERT_ALWAYS(aObjectName.Length() > 0, ESqlPanicBadArgument);
       
   145 	__SQLASSERT_ALWAYS(aPolicyType >= EReadPolicy && aPolicyType <= EWritePolicy, ESqlPanicBadArgument);
       
   146 	return Impl().SetPolicy(aObjectType, aObjectName, aPolicyType, aPolicy);
       
   147 	}
       
   148 
       
   149 /**
       
   150 Gets the default database security policy.
       
   151 
       
   152 @return The default security policy.
       
   153 				   
       
   154 @see TSecurityPolicy
       
   155 
       
   156 @capability None
       
   157 */	
       
   158 EXPORT_C TSecurityPolicy RSqlSecurityPolicy::DefaultPolicy() const
       
   159 	{
       
   160 	SQLUTRACE_PROFILER(this);
       
   161 	return Impl().DefaultPolicy();
       
   162 	}
       
   163 
       
   164 /**
       
   165 Gets a database security policy of the specified type.
       
   166 
       
   167 @param aPolicyType Database security policy type: RSqlSecurityPolicy::ESchemaPolicy, RSqlSecurityPolicy::EReadPolicy, 
       
   168 				   RSqlSecurityPolicy::EWritePolicy.
       
   169 
       
   170 @return The requested database security policy.
       
   171 				   
       
   172 @panic SqlDb 4 Invalid aPolicyType value.
       
   173 
       
   174 @see RSqlSecurityPolicy::TPolicyType
       
   175 @see TSecurityPolicy
       
   176 
       
   177 @capability None
       
   178 */	
       
   179 EXPORT_C TSecurityPolicy RSqlSecurityPolicy::DbPolicy(TPolicyType aPolicyType) const
       
   180 	{
       
   181 	SQLUTRACE_PROFILER(this);
       
   182 	__SQLASSERT_ALWAYS(aPolicyType >= ESchemaPolicy && aPolicyType <= EWritePolicy, ESqlPanicBadArgument);
       
   183 	return Impl().DbPolicy(aPolicyType);
       
   184 	}
       
   185 	
       
   186 /**
       
   187 Gets a database object security policy of the specified type.
       
   188 
       
   189 If no security policy of the specified type exists for that database object - the default security policy
       
   190 will be returned.
       
   191 
       
   192 @param aObjectType Database object type. At the moment there is only one database object type - RSqlSecurityPolicy::ETable.
       
   193 @param aObjectName Database object name. It cannot be a null descriptor.
       
   194 @param aPolicyType Database object security policy type: RSqlSecurityPolicy::EReadPolicy, RSqlSecurityPolicy::EWritePolicy.
       
   195 
       
   196 @return The requested security policy.
       
   197 
       
   198 @panic SqlDb 4 Invalid aPolicyType value.
       
   199 @panic SqlDb 4 Invalid aObjectType value (It has to be RSqlSecurityPolicy::ETable).
       
   200 @panic SqlDb 4 Invalid aObjectName value (Null descriptor).
       
   201 
       
   202 @see RSqlSecurityPolicy::TObjectType
       
   203 @see RSqlSecurityPolicy::TPolicyType
       
   204 @see TSecurityPolicy
       
   205 
       
   206 @capability None
       
   207 */
       
   208 EXPORT_C TSecurityPolicy RSqlSecurityPolicy::Policy(TObjectType aObjectType, const TDesC& aObjectName, 
       
   209 												 TPolicyType aPolicyType) const
       
   210 	{
       
   211 	SQLUTRACE_PROFILER(this);
       
   212 	__SQLASSERT_ALWAYS(aObjectType == ETable, ESqlPanicBadArgument);
       
   213 	__SQLASSERT_ALWAYS(aObjectName.Length() > 0, ESqlPanicBadArgument);
       
   214 	__SQLASSERT_ALWAYS(aPolicyType >= EReadPolicy && aPolicyType <= EWritePolicy, ESqlPanicBadArgument);
       
   215 	return Impl().Policy(aObjectType, aObjectName, aPolicyType);
       
   216 	}
       
   217 
       
   218 /**
       
   219 Externalizes RSqlSecurityPolicy instance to a write stream.
       
   220 
       
   221 @param aStream Stream to which RSqlSecurityPolicy instance should be externalised.
       
   222 
       
   223 @leave KErrNoMemory, an out of memory condition has occured.
       
   224 
       
   225 @capability None
       
   226 */
       
   227 EXPORT_C void RSqlSecurityPolicy::ExternalizeL(RWriteStream& aStream) const
       
   228 	{
       
   229 	SQLUTRACE_PROFILER(this);
       
   230 	RSqlSecurityPolicy::TObjectType objectType;
       
   231 	TPtrC objectName;
       
   232 	RSqlSecurityPolicy::TPolicyType policyType;
       
   233 	TSecurityPolicy policy;
       
   234 	//Default policy
       
   235 	policy = Impl().DefaultPolicy();
       
   236 	aStream << policy.Package();
       
   237 	//Database policies
       
   238 	policy = Impl().DbPolicy(RSqlSecurityPolicy::ESchemaPolicy);
       
   239 	aStream << policy.Package();
       
   240 	policy = Impl().DbPolicy(RSqlSecurityPolicy::EReadPolicy);
       
   241 	aStream << policy.Package();
       
   242 	policy = Impl().DbPolicy(RSqlSecurityPolicy::EWritePolicy);
       
   243 	aStream << policy.Package();
       
   244 	//Database object policies
       
   245 	TSqlSecurityPolicyIterator it(Impl());
       
   246 	while(it.Next(objectType, objectName, policyType, policy))
       
   247 		{
       
   248 		aStream << static_cast <TInt32> (objectType);
       
   249 		aStream << objectName;
       
   250 		aStream << static_cast <TInt32> (policyType);
       
   251 		aStream << policy.Package();
       
   252 		}
       
   253 	//Object policy stream - end
       
   254 	aStream << KEndOfSPStream;
       
   255 	}
       
   256 	
       
   257 /**
       
   258 Initializes RSqlSecurityPolicy instance from a stream.
       
   259 In case of an error the original security policy data is preserved.
       
   260 
       
   261 @param aStream A read stream containing the data with which the RSqlSecurityPolicy instance will be initialized.
       
   262 
       
   263 @leave KErrNoMemory, an out of memory condition has occured.
       
   264                      Note that the function may leave with other system-wide error codes.
       
   265 
       
   266 @capability None
       
   267 */
       
   268 EXPORT_C void RSqlSecurityPolicy::InternalizeL(RReadStream& aStream)
       
   269 	{
       
   270 	SQLUTRACE_PROFILER(this);
       
   271 	TSecurityPolicy policy;
       
   272 	TBuf8<sizeof(TSecurityPolicy)> policyBuf;
       
   273 	//Default policy
       
   274 	aStream >> policyBuf;
       
   275 	policy.Set(policyBuf);
       
   276 	//Create new sql security policy object	and initialize it with the policies read from the input stream
       
   277 	RSqlSecurityPolicy newPolicy;
       
   278 	newPolicy.CreateL(policy);
       
   279 	CleanupClosePushL(newPolicy);
       
   280 	//Database policies
       
   281 	aStream >> policyBuf;
       
   282 	policy.Set(policyBuf);
       
   283 	__SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, policy));
       
   284 	aStream >> policyBuf;
       
   285 	policy.Set(policyBuf);
       
   286 	__SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, policy));
       
   287 	aStream >> policyBuf;
       
   288 	policy.Set(policyBuf);
       
   289 	__SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, policy));
       
   290 	//Database object policies
       
   291 	for(;;)
       
   292 		{
       
   293 		TInt32 objectType;
       
   294 		aStream >> objectType;
       
   295 		if(objectType == KEndOfSPStream)
       
   296 			{
       
   297 			break;	
       
   298 			}
       
   299 		TBuf<KMaxFileName> objectName;
       
   300 		aStream >> objectName;
       
   301 		TInt32 policyType;
       
   302 		aStream >> policyType;
       
   303 		aStream >> policyBuf;
       
   304 		policy.Set(policyBuf);
       
   305 		__SQLLEAVE_IF_ERROR(newPolicy.SetPolicy(static_cast <RSqlSecurityPolicy::TObjectType> (objectType), objectName, static_cast <RSqlSecurityPolicy::TPolicyType> (policyType), policy));
       
   306 		}
       
   307 	//Swap the original sql security policy with the new sql security policy
       
   308 	CSqlSecurityPolicy* temp = newPolicy.iImpl;
       
   309 	newPolicy.iImpl = iImpl;
       
   310 	iImpl = temp;
       
   311 	//Destroy the old policy (which was swapped)
       
   312 	CleanupStack::PopAndDestroy(&newPolicy);
       
   313 	}
       
   314 
       
   315 /**
       
   316 Destroys the existing iImpl object and replaces it with aImpl parameter.
       
   317 
       
   318 @internalComponent
       
   319 */
       
   320 void RSqlSecurityPolicy::Set(CSqlSecurityPolicy& aImpl)
       
   321 	{
       
   322 	delete iImpl;
       
   323 	iImpl = &aImpl;
       
   324 	}
       
   325 
       
   326 /**
       
   327 @return A reference to the implementation object.
       
   328 
       
   329 @panic SqlDb 2 Create() has not previously been called on  this RSqlSecurityPolicy object.
       
   330 
       
   331 @internalComponent
       
   332 */
       
   333 CSqlSecurityPolicy& RSqlSecurityPolicy::Impl() const
       
   334 	{
       
   335 	__SQLASSERT_ALWAYS(iImpl != NULL, ESqlPanicInvalidObj);
       
   336 	return *iImpl;	
       
   337 	}