First public contribution.
1 // Copyright (c) 2005-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "SqlSecurityImpl.h"
17 #include "SqlAssert.h"
18 #include "OstTraceDefinitions.h"
19 #ifdef OST_TRACE_COMPILER_IN_USE
20 #include "SqlSecurityTraces.h"
22 #include "SqlTraceDef.h"
24 const TInt32 KEndOfSPStream = -1;//If found in the stream, given as an argument to RSqlSecurityPolicy::InternalizeL(),
25 //then there are no more policies in the stream.
28 Initializes RSqlSecurityPolicy instance data members with their default values.
32 EXPORT_C RSqlSecurityPolicy::RSqlSecurityPolicy() :
38 Initializes RSqlSecurityPolicy instance.
40 @param aDefaultPolicy Default security policy which will be used for the database and all database objects.
42 @return KErrNone, the operation has completed successfully;
43 KErrNoMemory, an out of memory condition has occured.
44 Note that database specific errors categorised as ESqlDbError, and
45 other system-wide error codes may also be returned.
51 EXPORT_C TInt RSqlSecurityPolicy::Create(const TSecurityPolicy& aDefaultPolicy)
53 SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATE_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::Create;aDefaultPolicy=0x%X", (TUint)this, (TUint)&aDefaultPolicy));
54 TRAPD(err, CreateL(aDefaultPolicy));
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));
60 Initializes RSqlSecurityPolicy instance.
62 @param aDefaultPolicy Default security policy which will be used for the database and all database objects.
64 @leave KErrNoMemory, an out of memory condition has occured.
65 Note that database specific errors categorised as ESqlDbError, and
66 other system-wide error codes may also be returned.
72 EXPORT_C void RSqlSecurityPolicy::CreateL(const TSecurityPolicy& aDefaultPolicy)
74 SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATEL_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::CreateL;aDefaultPolicy=0x%X", (TUint)this, (TUint)&aDefaultPolicy));
75 iImpl = CSqlSecurityPolicy::NewL(aDefaultPolicy);
76 SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CREATEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::CreateL;iImpl=0x%X", (TUint)this, (TUint)iImpl));
80 Frees the allocated by RSqlSecurityPolicy instance memory and other resources.
84 EXPORT_C void RSqlSecurityPolicy::Close()
86 SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_CLOSE_ENTRY , "Entry;0x%X;RSqlSecurityPolicy::Close", (TUint)this));
88 SQL_TRACE_BORDER(OstTraceExt2(TRACE_BORDER, RSQLSECURITYPOLICY_CLOSE_EXIT, "Exit;0x%X;RSqlSecurityPolicy::Close;iImpl=0x%X", (TUint)this, (TUint)iImpl));
94 Sets a database security policy of a specific type.
96 Sets database security policy (aPolicy argument) of aPolicyType type.
97 If the aPolicyType database security policy has already been set then it will be replaced with the supplied policy.
99 @param aPolicyType Database security policy type: RSqlSecurityPolicy::ESchema, RSqlSecurityPolicy::ERead, RSqlSecurityPolicy::EWrite.
100 @param aPolicy The database security policy.
102 @panic SqlDb 4 Invalid aPolicyType value.
106 @see RSqlSecurityPolicy::TPolicyType
111 EXPORT_C TInt RSqlSecurityPolicy::SetDbPolicy(TPolicyType aPolicyType, const TSecurityPolicy& aPolicy)
113 __ASSERT_ALWAYS(aPolicyType >= ESchemaPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument));
114 Impl().SetDbPolicy(aPolicyType, aPolicy);
119 Sets a database object security policy of a specific type.
121 If there is no entry in the security policy container for the object with aObjectName name, then a new entry for this
122 object will be created and all object security policies will be initialized with the default security policy.
123 The specific database object policy, refered by aPolicyType parameter, will be set after that.
125 If an entry for aObjectName object already exists, its security policy of "aPolicyType" type will be
126 reinitialized with the data of aPolicy parameter.
128 @param aObjectType Database object type. At the moment there is only one database object type - RSqlSecurityPolicy::ETable.
129 @param aObjectName Database object name. It cannot be a null descriptor.
130 @param aPolicyType Database object security policy type: RSqlSecurityPolicy::EReadPolicy, RSqlSecurityPolicy::EWritePolicy.
131 @param aPolicy Database security policy.
133 @return KErrNone, the operation has completed successfully;
134 KErrNoMemory, an out of memory condition has occured.
136 @panic SqlDb 4 Invalid aPolicyType value.
137 @panic SqlDb 4 Invalid aObjectType value (It has to be RSqlSecurityPolicy::ETable).
138 @panic SqlDb 4 Invalid aObjectName value (Null descriptor).
140 @see RSqlSecurityPolicy::TObjectType
141 @see RSqlSecurityPolicy::TPolicyType
146 EXPORT_C TInt RSqlSecurityPolicy::SetPolicy(TObjectType aObjectType, const TDesC& aObjectName,
147 TPolicyType aPolicyType, const TSecurityPolicy& aPolicy)
149 __ASSERT_ALWAYS(aObjectType == ETable, __SQLPANIC(ESqlPanicBadArgument));
150 __ASSERT_ALWAYS(aObjectName.Length() > 0, __SQLPANIC(ESqlPanicBadArgument));
151 __ASSERT_ALWAYS(aPolicyType >= EReadPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument));
152 return Impl().SetPolicy(aObjectType, aObjectName, aPolicyType, aPolicy);
156 Gets the default database security policy.
158 @return The default security policy.
164 EXPORT_C TSecurityPolicy RSqlSecurityPolicy::DefaultPolicy() const
166 return Impl().DefaultPolicy();
170 Gets a database security policy of the specified type.
172 @param aPolicyType Database security policy type: RSqlSecurityPolicy::ESchemaPolicy, RSqlSecurityPolicy::EReadPolicy,
173 RSqlSecurityPolicy::EWritePolicy.
175 @return The requested database security policy.
177 @panic SqlDb 4 Invalid aPolicyType value.
179 @see RSqlSecurityPolicy::TPolicyType
184 EXPORT_C TSecurityPolicy RSqlSecurityPolicy::DbPolicy(TPolicyType aPolicyType) const
186 __ASSERT_ALWAYS(aPolicyType >= ESchemaPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument));
187 return Impl().DbPolicy(aPolicyType);
191 Gets a database object security policy of the specified type.
193 If no security policy of the specified type exists for that database object - the default security policy
196 @param aObjectType Database object type. At the moment there is only one database object type - RSqlSecurityPolicy::ETable.
197 @param aObjectName Database object name. It cannot be a null descriptor.
198 @param aPolicyType Database object security policy type: RSqlSecurityPolicy::EReadPolicy, RSqlSecurityPolicy::EWritePolicy.
200 @return The requested security policy.
202 @panic SqlDb 4 Invalid aPolicyType value.
203 @panic SqlDb 4 Invalid aObjectType value (It has to be RSqlSecurityPolicy::ETable).
204 @panic SqlDb 4 Invalid aObjectName value (Null descriptor).
206 @see RSqlSecurityPolicy::TObjectType
207 @see RSqlSecurityPolicy::TPolicyType
212 EXPORT_C TSecurityPolicy RSqlSecurityPolicy::Policy(TObjectType aObjectType, const TDesC& aObjectName,
213 TPolicyType aPolicyType) const
215 __ASSERT_ALWAYS(aObjectType == ETable, __SQLPANIC(ESqlPanicBadArgument));
216 __ASSERT_ALWAYS(aObjectName.Length() > 0, __SQLPANIC(ESqlPanicBadArgument));
217 __ASSERT_ALWAYS(aPolicyType >= EReadPolicy && aPolicyType <= EWritePolicy, __SQLPANIC(ESqlPanicBadArgument));
218 return Impl().Policy(aObjectType, aObjectName, aPolicyType);
222 Externalizes RSqlSecurityPolicy instance to a write stream.
224 @param aStream Stream to which RSqlSecurityPolicy instance should be externalised.
226 @leave KErrNoMemory, an out of memory condition has occured.
230 EXPORT_C void RSqlSecurityPolicy::ExternalizeL(RWriteStream& aStream) const
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()));
233 RSqlSecurityPolicy::TObjectType objectType;
235 RSqlSecurityPolicy::TPolicyType policyType;
236 TSecurityPolicy policy;
238 policy = Impl().DefaultPolicy();
239 aStream << policy.Package();
241 policy = Impl().DbPolicy(RSqlSecurityPolicy::ESchemaPolicy);
242 aStream << policy.Package();
243 policy = Impl().DbPolicy(RSqlSecurityPolicy::EReadPolicy);
244 aStream << policy.Package();
245 policy = Impl().DbPolicy(RSqlSecurityPolicy::EWritePolicy);
246 aStream << policy.Package();
247 //Database object policies
248 TSqlSecurityPolicyIterator it(Impl());
249 while(it.Next(objectType, objectName, policyType, policy))
251 aStream << static_cast <TInt32> (objectType);
252 aStream << objectName;
253 aStream << static_cast <TInt32> (policyType);
254 aStream << policy.Package();
256 //Object policy stream - end
257 aStream << KEndOfSPStream;
258 SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_EXTERNALIZEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::ExternalizeL", (TUint)this));
262 Initializes RSqlSecurityPolicy instance from a stream.
263 In case of an error the original security policy data is preserved.
265 @param aStream A read stream containing the data with which the RSqlSecurityPolicy instance will be initialized.
267 @leave KErrNoMemory, an out of memory condition has occured.
268 Note that the function may leave with other system-wide error codes.
272 EXPORT_C void RSqlSecurityPolicy::InternalizeL(RReadStream& aStream)
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()));
275 TSecurityPolicy policy;
276 TBuf8<sizeof(TSecurityPolicy)> policyBuf;
278 aStream >> policyBuf;
279 policy.Set(policyBuf);
280 //Create new sql security policy object and initialize it with the policies read from the input stream
281 RSqlSecurityPolicy newPolicy;
282 newPolicy.CreateL(policy);
283 CleanupClosePushL(newPolicy);
285 aStream >> policyBuf;
286 policy.Set(policyBuf);
287 __SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, policy));
288 aStream >> policyBuf;
289 policy.Set(policyBuf);
290 __SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::EReadPolicy, policy));
291 aStream >> policyBuf;
292 policy.Set(policyBuf);
293 __SQLLEAVE_IF_ERROR(newPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, policy));
294 //Database object policies
298 aStream >> objectType;
299 if(objectType == KEndOfSPStream)
303 TBuf<KMaxFileName> objectName;
304 aStream >> objectName;
306 aStream >> policyType;
307 aStream >> policyBuf;
308 policy.Set(policyBuf);
309 __SQLLEAVE_IF_ERROR(newPolicy.SetPolicy(static_cast <RSqlSecurityPolicy::TObjectType> (objectType), objectName, static_cast <RSqlSecurityPolicy::TPolicyType> (policyType), policy));
311 //Swap the original sql security policy with the new sql security policy
312 CSqlSecurityPolicy* temp = newPolicy.iImpl;
313 newPolicy.iImpl = iImpl;
315 //Destroy the old policy (which was swapped)
316 CleanupStack::PopAndDestroy(&newPolicy);
317 SQL_TRACE_BORDER(OstTrace1(TRACE_BORDER, RSQLSECURITYPOLICY_INTERNALIZEL_EXIT, "Exit;0x%X;RSqlSecurityPolicy::InternalizeL", (TUint)this));
321 Destroys the existing iImpl object and replaces it with aImpl parameter.
325 void RSqlSecurityPolicy::Set(CSqlSecurityPolicy& aImpl)
332 @return A reference to the implementation object.
334 @panic SqlDb 2 Create() has not previously been called on this RSqlSecurityPolicy object.
338 CSqlSecurityPolicy& RSqlSecurityPolicy::Impl() const
340 __ASSERT_ALWAYS(iImpl != NULL, __SQLPANIC(ESqlPanicInvalidObj));