First public contribution.
1 // Copyright (c) 2004-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // CPolicyBase, CDbPolicy, CTblPolicy, CPolicyDomain classes
18 #include "SC_Policy.h"
23 ///////////////////////////////////////////////////////////////////////////////////////////
28 CPolicyBase::~CPolicyBase()
30 iPolicyCollection.Close();//Collection of R/W/S security policies
35 Dumps the content of a CPolicyBase instance to a text file.
36 @param aFile A reference to RFile object, which has to be used for the output.
38 void CPolicyBase::Dump(RFile& aFile) const
42 _LIT8(KClassName, "Class: CPolicyBase. this=%X");
43 _LIT8(KCount, "Security Policy, Count=%d");
45 _LIT8(KPolicyType, "Policy type: ");
46 _LIT8(KRead, "Read, ");
47 _LIT8(KWrite, "Write, ");
48 _LIT8(KSchema, "Schema, ");
49 _LIT8(KPolicyData, "Policy data: ");
53 buf.Format(KClassName, this);
54 (void)aFile.Write(buf);
55 (void)aFile.Write(KCrLf);
57 TInt cnt = iPolicyCollection.Count();
58 buf.Format(KCount, TInt32(cnt));
59 (void)aFile.Write(buf);
60 (void)aFile.Write(KCrLf);
62 for(TInt i=0;i<cnt;++i)
64 const TPolicy& policy = iPolicyCollection[i];
65 (void)aFile.Write(KPolicyType);
69 (void)aFile.Write(KRead);
72 (void)aFile.Write(KWrite);
75 (void)aFile.Write(KSchema);
81 (void)aFile.Write(KPolicyData);
82 TPtrC8 packet = policy.iData.Package();
83 TInt len = packet.Length();
84 for(TInt j=0;j<len;++j)
86 buf.Format(KFmt, packet[j]);
87 (void)aFile.Write(buf);
89 (void)aFile.Write(KCrLf);
95 Standard phase-two construction method for CPolicyBase instance.
96 @param aPolicyCollection A const reference to a collection of R/W/S policies, which has to
97 be used to control the access to a database object, controlled by CPolicyBase
100 void CPolicyBase::ConstructL(const CPolicyBase::RPolicyCollection& aPolicyCollection)
102 iPolicyCollection.Reset();
103 TInt cnt = aPolicyCollection.Count();
104 for(TInt i=0;i<cnt;++i)
106 __LEAVE_IF_ERROR(iPolicyCollection.Append(aPolicyCollection[i]));
112 It is used in the production code.
113 If the object data is not in a consistent state, the method will leave
114 with KErrGeneral error.
115 @leave KErrGeneral, if the object data is not in a consistent state
117 void CPolicyBase::InvariantL() const
120 for(TInt i=(iPolicyCollection.Count()-1);i>-1;--i)
122 TPolicy& policy = const_cast <TPolicy&> (iPolicyCollection[i]);
123 if(policy.iType == EPTNone)
125 __LEAVE(KErrGeneral);
127 if(mask & policy.iType) //This security policy is duplicated
129 __LEAVE(KErrGeneral);
131 TPtrC8 packet = policy.iData.Package();
132 if(policy.iData.Set(packet) != KErrNone)
134 __LEAVE(KErrGeneral);
136 mask |= policy.iType;
141 This method implements pure virtual MPolicy::Get().
142 It searches object's policy collection for a policy of type aPolicyType
143 and initializes aPolicy parameter with the found policy.
144 @param aPolicyType Type of the requested security policy: read/write/schema
145 @param aPolicy Outout parameter, which will be initialized with the found security policy data.
146 @return System-wide error code, including KErrNotFound if the requested policy was not found.
148 TInt CPolicyBase::Get(TPolicyType aPolicyType, TSecurityPolicy& aPolicy) const
151 TInt err = KErrNotFound;
152 const TSecurityPolicy* securityPolicy = Policy(aPolicyType);
155 err = aPolicy.Set(securityPolicy->Package());
160 #ifdef __DBINVARIANT__
162 Asserts the internal state of CPolicyBase instance.
163 It can be used for pre- or post- condition checks in CPolicyBase methods implementations.
165 void CPolicyBase::Invariant() const
167 TRAPD(err, InvariantL());
168 DB_INVARIANT_ASSERT(err == KErrNone);
170 #endif//__DBINVARIANT__
173 The method traverses the policies collection and searches for a policy of aPolicyType type.
174 If such a policy exists, a const pointer to it will be returned, otherwise - NULL.
175 @param aPolicyType Policy type - R/W/S
176 @return A const pointer to the found policy or NULL if not found.
178 const TSecurityPolicy* CPolicyBase::Policy(TPolicyType aPolicyType) const
180 __ASSERT(aPolicyType != EPTNone);
181 const TSecurityPolicy* policy = NULL;
182 for(TInt i=(iPolicyCollection.Count()-1);i>-1;--i)
184 if(iPolicyCollection[i].iType == aPolicyType)
186 policy = &iPolicyCollection[i].iData;
194 Asserts caller capabilities/SID/VID.
195 @param aMessage An object whith caller capabilities/SID/VID, which has to be checked.
196 @param aPolicyType Policy type - R/W/S.
197 @return EPCNotFound - the policy cannot be found
198 EPCPassed - policy check passed
199 EPCNotPassed - policy check not passed
201 CPolicyBase::TPolicyCheckResult CPolicyBase::DoCheck(const RMessage2& aMessage, TPolicyType aPolicyType) const
203 const TSecurityPolicy* securityPolicy = Policy(aPolicyType);
210 return securityPolicy->CheckPolicy(aMessage) ? EPCPassed : EPCNotPassed;
213 ///////////////////////////////////////////////////////////////////////////////////////////
218 CDbPolicy::~CDbPolicy()
223 Asserts caller capabilities/SID/VID.
224 @param aMessage An object whith caller capabilities/SID/VID, which has to be checked.
225 @param aPolicyType Policy type - R/W/S.
226 @return ETrue The caller capabilities/SID/VID satisfy the specified security policy.
227 EFalse The check not passed.
228 @panic EDBSCPolicyNotFound, if there is no such policy
230 TBool CDbPolicy::Check(const RMessage2& aMessage, TPolicyType aPolicyType) const
232 __ASSERT(aPolicyType != EPTNone);
234 TPolicyCheckResult res = DoCheck(aMessage, aPolicyType);
235 __ASSERT(res != EPCNotFound);
236 return res == EPCPassed ? ETrue : EFalse;
240 Standard phase-one factory method for CDbPolicy instance.
241 @param aPolicyCollection A const reference to a collection of R/W/S policies, which has to
242 be used to control the access to the database, controlled by CDbPolicy instance.
243 @return A pointer to just created CDbPolicy instance.
244 @leave System-wide error codes, including KErrNoMemory.
246 CDbPolicy* CDbPolicy::NewLC(const CPolicyBase::RPolicyCollection& aPolicyCollection)
248 CDbPolicy* self = new (ELeave) CDbPolicy;
249 CleanupStack::PushL(self);
250 self->ConstructL(aPolicyCollection);
256 Dumps the content of a CDbPolicy instance to a text file.
257 @param aFile A reference to RFile object, which has to be used for the output.
259 void CDbPolicy::Dump(RFile& aFile) const
263 _LIT8(KClassName, "Class: CDbPolicy. this=%X");
264 _LIT8(KCrLf, "\r\n");
265 _LIT8(KObjType, "Object: Database");
266 _LIT8(KEnd, "==========================");
269 buf.Format(KClassName, this);
270 (void)aFile.Write(buf);
271 (void)aFile.Write(KCrLf);
272 (void)aFile.Write(KObjType);
273 (void)aFile.Write(KCrLf);
274 CPolicyBase::Dump(aFile);
275 (void)aFile.Write(KEnd);
276 (void)aFile.Write(KCrLf);
281 It is used in the production code.
282 If the object data is not in a consistent state, the method will leave
283 with KErrGeneral error.
284 @leave KErrGeneral, if the object data is not in a consistent state
286 void CDbPolicy::InvariantL() const
288 for(TInt c=0;c<KPolicyTypesCount;++c)
290 TPolicyType t = static_cast <TPolicyType> (1 << c);
291 if(Policy(t) == NULL)
293 __LEAVE(KErrGeneral);
296 CPolicyBase::InvariantL();
299 ///////////////////////////////////////////////////////////////////////////////////////////
304 CTblPolicy::~CTblPolicy()
310 Asserts caller capabilities/SID/VID.
311 @param aMessage An object whith caller capabilities/SID/VID, which has to be checked.
312 @param aPolicyType Policy type - R/W/S.
313 @return ETrue The caller capabilities/SID/VID satisfy the specified security policy.
314 EFalse The check not passed.
315 @panic EDBSCPolicyNotFound, if there is no such policy
317 TBool CTblPolicy::Check(const RMessage2& aMessage, TPolicyType aPolicyType) const
319 __ASSERT(aPolicyType != EPTNone);
322 TPolicyCheckResult res = EPCNotPassed;
323 //1. Check database security policy
324 if(iDbPolicy->Check(aMessage, aPolicyType))
326 //2. Check table security policy
327 res = DoCheck(aMessage, aPolicyType);
329 //If there is no table security policy of the requested type - no problem, the database
330 //security policy of that type has been checked already and the check passed.
331 return res == EPCNotPassed ? EFalse : ETrue;
335 This method implements pure virtual MPolicy::Get().
336 It searches object's policy collection for a policy of type aPolicyType
337 and initializes aPolicy parameter with the found policy.
338 @param aPolicyType Type of the requested security policy: read/write
339 @param aPolicy Outout parameter, which will be initialized with the found security policy data.
340 @return System-wide error codes, including KErrNotSupported, if the request is for a schema policy.
342 TInt CTblPolicy::Get(TPolicyType aPolicyType, TSecurityPolicy& aPolicy) const
344 if(aPolicyType == EPTSchema)
346 return KErrNotSupported;
349 TInt err = CPolicyBase::Get(aPolicyType, aPolicy);
350 if(err == KErrNotFound)
352 err = iDbPolicy->Get(aPolicyType, aPolicy);
354 __ASSERT(err != KErrNotFound);
359 Standard phase-one factory method for CTblPolicy instance.
360 @param aTblName Name of the controlled by this instance database table.
361 @param aPolicyCollection A const reference to a collection of R/W/S policies, which has to
362 be used to control the access to the table, controlled by CTblPolicy instance.
363 @param aDbPolicy The related for the table database policy.
364 CTblPolicy instance does not take the ownership on aDbPolicy pointer!
365 @return A pointer to just created CTblPolicy instance.
366 @leave System-wide error codes, including KErrNoMemory.
368 CTblPolicy* CTblPolicy::NewLC(const TDesC& aTblName,
369 const CPolicyBase::RPolicyCollection& aPolicyCollection,
370 const CDbPolicy* aDbPolicy)
372 CTblPolicy* self = new (ELeave) CTblPolicy(aDbPolicy);
373 CleanupStack::PushL(self);
374 self->ConstructL(aTblName, aPolicyCollection);
380 Dumps the content of a CTblPolicy instance to a text file.
381 @param aFile A reference to RFile object, which has to be used for the output.
383 void CTblPolicy::Dump(RFile& aFile) const
387 _LIT8(KClassName, "Class: CTblPolicy. this=%X");
388 _LIT8(KDbPolicyPtr, "Db policy ptr=%X");
389 _LIT8(KCrLf, "\r\n");
390 _LIT8(KName, "Table name: ");
391 _LIT8(KObjType, "Object: Table");
392 _LIT8(KEnd, "==========================");
395 buf.Format(KClassName, this);
396 (void)aFile.Write(buf);
397 (void)aFile.Write(KCrLf);
398 (void)aFile.Write(KObjType);
399 (void)aFile.Write(KCrLf);
400 buf.Format(KDbPolicyPtr, iDbPolicy);
401 (void)aFile.Write(buf);
402 (void)aFile.Write(KCrLf);
404 buf.Append(*iTblName);
405 (void)aFile.Write(buf);
406 (void)aFile.Write(KCrLf);
407 CPolicyBase::Dump(aFile);
408 (void)aFile.Write(KEnd);
409 (void)aFile.Write(KCrLf);
414 It is used in the production code.
415 If the object data is not in a consistent state, the method will leave
416 with KErrGeneral error.
417 @leave KErrGeneral, if the object data is not in a consistent state
419 void CTblPolicy::InvariantL() const
421 if(iDbPolicy == NULL)
423 __LEAVE(KErrGeneral);
425 if(iTblName == NULL || iTblName->Length() == 0)
427 __LEAVE(KErrGeneral);
429 if(Policy(EPTSchema) != NULL)
431 __LEAVE(KErrGeneral);
433 CPolicyBase::InvariantL();
437 Standard phase-two construction method for CTblPolicy instance.
438 @param aTblName Name of the controlled by this instance database table.
439 @param aPolicyCollection A const reference to a collection of R/W/S policies, which has to
440 be used to control the access to the table object, controlled by CTblPolicy
443 void CTblPolicy::ConstructL(const TDesC& aTblName, const CPolicyBase::RPolicyCollection& aPolicyCollection)
445 iTblName = HBufC::NewL(aTblName.Length());
446 *iTblName = aTblName;
447 CPolicyBase::ConstructL(aPolicyCollection);
451 ///////////////////////////////////////////////////////////////////////////////////////////
452 //CPolicyDomain class
455 Standard phase-one factory method for CPolicyDomain instance.
456 @param aUid UID of the controlled by this instance security policy domain.
457 @param aPDLoader A reference to an implementation of MPolicyDomainLoader interface,
458 which is used to load and add security policies to the controlled collection.
459 @return A pointer to just created CPolicyDomain instance.
460 @leave System-wide error codes, including KErrNoMemory.
462 CPolicyDomain* CPolicyDomain::NewLC(TUid aUid, MPolicyDomainLoader& aPDLoader)
464 CPolicyDomain* self = new (ELeave) CPolicyDomain(aUid);
465 CleanupStack::PushL(self);
466 self->InternalizeL(aPDLoader);
472 CPolicyDomain::~CPolicyDomain()
478 The method returns the database policy interface.
479 @return A const pointer to the database policy interface in CPolicyDomain.
481 const MPolicy* CPolicyDomain::DbPolicy() const
488 The method returns a table policy interface, identified by aTblName parameter.
489 @param aTblName Name of the table, which policy interface has to be retrieved.
490 @return A const pointer to the table policy interface, which is identified by aTblName parameter.
492 const MPolicy* CPolicyDomain::TblPolicy(const TDesC& aTblName) const
494 __ASSERT(aTblName.Length() > 0);
496 const MPolicy* policy = NULL;
497 TInt cnt = iTPCollection.Count();
498 for(TInt i=0;i<cnt;++i)
500 CTblPolicy* tblPolicy = iTPCollection[i];
502 if(aTblName.CompareF(tblPolicy->TableName()) == 0)
517 Externalizes the security policy collection using MPolicyDomainPersister interface as an
519 @param aPDPersister A reference to an MPolicyDomainPersister implementation, which will
520 persist the controlled collection of security policies.
522 void CPolicyDomain::ExternalizeL(MPolicyDomainPersister& aPDPersister) const
525 TPolicyDomainReader reader(*this);
526 aPDPersister.RunL(reader);
531 Dumps the content of a CPolicyDomain instance to a text file.
532 @param aFile A reference to RFile object, which has to be used for the output.
534 void CPolicyDomain::Dump(RFile& aFile) const
538 _LIT8(KClassName, "Class: CPolicyDomain. this=%X");
539 _LIT8(KUidFmt, "UID=%X");
540 _LIT8(KCrLf, "\r\n");
541 _LIT8(KEnd, "==========================");
542 _LIT8(KBackupSIDFmt, "BackupSID=%X");
545 buf.Format(KClassName, this);
546 (void)aFile.Write(buf);
547 (void)aFile.Write(KCrLf);
548 buf.Format(KUidFmt, iUid.iUid);
549 (void)aFile.Write(buf);
550 (void)aFile.Write(KCrLf);
551 (void)aFile.Write(KEnd);
552 (void)aFile.Write(KCrLf);
553 iDbPolicy->Dump(aFile);
554 TInt cnt = iTPCollection.Count();
555 for(TInt i=0;i<cnt;++i)
557 __ASSERT(iTPCollection[i]);
558 iTPCollection[i]->Dump(aFile);
560 (void)aFile.Write(KEnd);
561 buf.Format(KBackupSIDFmt, iBackupSID.iUid);
562 (void)aFile.Write(buf);
563 (void)aFile.Write(KCrLf);
568 It is used in the production code.
569 If the object data is not in a consistent state, the method will leave
570 with KErrGeneral error.
571 @leave KErrGeneral, if the object data is not in a consistent state
573 void CPolicyDomain::InvariantL() const
577 __LEAVE(KErrGeneral);
579 if(iDbPolicy == NULL)
581 __LEAVE(KErrGeneral);
583 iDbPolicy->InvariantL();
585 TInt cnt = iTPCollection.Count();
589 if(iTPCollection[i] == NULL)
591 __LEAVE(KErrGeneral);
593 iTPCollection[i]->InvariantL();
595 //Check that each represented table has unique name
596 for(i=0;i<(cnt-1);++i)
598 for(TInt j=(i+1);j<cnt;++j)
600 if(iTPCollection[i]->TableName() == iTPCollection[j]->TableName())
602 __LEAVE(KErrGeneral);
608 #ifdef __DBINVARIANT__
610 Asserts the internal state of CPolicyDomain instance.
611 It can be used for pre- or post- condition checks in CPolicyDomain methods implementations.
613 void CPolicyDomain::Invariant() const
615 TRAPD(err, InvariantL());
616 DB_INVARIANT_ASSERT(err == KErrNone);
618 #endif//__DBINVARIANT__
621 Creates the collection of security policies using MPolicyDomainLoader interface as a security
623 @param aPDLoader A reference to MPolicyDomainLoader implementation, which is used to load
624 and add security policies to the controlled collection.
625 @leave System-wide error code including KErrGeneral if the data is not consistent
627 void CPolicyDomain::InternalizeL(MPolicyDomainLoader& aPDLoader)
629 TPolicyDomainBuilder builder(*this);
630 aPDLoader.RunL(builder);
631 #ifdef __DBINVARIANT__
639 The method destroys the controlled by CPolicyDomain collection of security policies.
641 void CPolicyDomain::Destroy()
643 TInt cnt = iTPCollection.Count();
644 for(TInt i=0;i<cnt;++i)
646 __ASSERT(iTPCollection[i]);
647 delete iTPCollection[i];
649 iTPCollection.Close();
654 } //end of - namespace DBSC