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