1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/authorisation/userpromptservice/policies/source/policyreader.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,272 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "policyreader.h"
1.23 +#include <ups/ups.hrh>
1.24 +#include <ups/upserr.h>
1.25 +#include "upslog.h"
1.26 +
1.27 +using namespace UserPromptService;
1.28 +
1.29 +/// The policy resource format is versioned.
1.30 +static const TInt KPolicyFormatVersion = 1;
1.31 +
1.32 +TPolicyHeader::TPolicyHeader()
1.33 +/**
1.34 +Constructor
1.35 +*/
1.36 + : iFormatVersion(0), iMajorVersion(0), iMinorVersion(0), iAuthPolicy(EAlwaysCheck),
1.37 + iDefaultPolicyEvaluator(TUid::Null()), iDefaultDialogCreator(TUid::Null())
1.38 + {
1.39 + }
1.40 +
1.41 +void CPolicyReader::ReadPolicyHeaderL()
1.42 +/**
1.43 +Reads the header information from the policy file.
1.44 +*/
1.45 + {
1.46 + TUint16 formatVersion = iReader.ReadUint16L();
1.47 + if (formatVersion != KPolicyFormatVersion)
1.48 + {
1.49 + User::Leave(KErrUpsBadPolicyFile);
1.50 + }
1.51 +
1.52 + (void) iReader.ReadUint32L(); // Skip reserved LLINK
1.53 +
1.54 + iHeader.iFormatVersion = formatVersion;
1.55 + iHeader.iMajorVersion = iReader.ReadUint16L();
1.56 + iHeader.iMinorVersion = iReader.ReadUint16L();
1.57 + iHeader.iAuthPolicy = static_cast<TAuthorisationPolicy>(iReader.ReadInt8L());
1.58 + iHeader.iDefaultPolicyEvaluator = TUid::Uid(iReader.ReadInt32L());
1.59 + iHeader.iDefaultDialogCreator = TUid::Uid(iReader.ReadInt32L());
1.60 + if (iHeader.iDefaultDialogCreator.iUid == 0)
1.61 + {
1.62 + User::Leave(KErrUpsBadPolicyFile);
1.63 + }
1.64 + }
1.65 +
1.66 +EXPORT_C CPolicyReader* CPolicyReader::NewL(RFs& aRFs, const TDesC& aPolicyFileName)
1.67 +/**
1.68 +Creates a policy reader object.
1.69 +@param aRFs the file server session used by the resource parser.
1.70 +@param aPolicyFileName the name of the User Prompt Service policy file to read.
1.71 +
1.72 +@return The new policy reader.
1.73 + */
1.74 + {
1.75 + CPolicyReader* self = CPolicyReader::NewLC(aRFs, aPolicyFileName);
1.76 + CleanupStack::Pop(self);
1.77 + return self;
1.78 + }
1.79 +
1.80 +EXPORT_C CPolicyReader* CPolicyReader::NewLC(RFs& aRFs, const TDesC& aPolicyFileName)
1.81 +/**
1.82 +Creates a policy reader object and places the pointer on the cleanup stack.
1.83 +@param aRFs the file server session used by the resource parser.
1.84 +@param aPolicyFileName the name of the User Prompt Service policy file to read.
1.85 +
1.86 +@return The new policy reader.
1.87 +*/
1.88 + {
1.89 + CPolicyReader* self = new(ELeave) CPolicyReader();
1.90 + CleanupStack::PushL(self);
1.91 + self->ConstructL(aRFs, aPolicyFileName);
1.92 + return self;
1.93 + }
1.94 +
1.95 +void CPolicyReader::ConstructL(RFs& aFs, const TDesC& aResourceFileName)
1.96 +/**
1.97 +Second phase constructor.
1.98 + - Creates the resource reader.
1.99 + - Reads the default policy evaluator and dialog creator UIDs.
1.100 + - Reads the number of policies in the resource file.
1.101 +
1.102 +@param aRFs The file server session used by the resource parser.
1.103 +@param aPolicyFileName The name of the User Prompt Service policy file to read.
1.104 +*/
1.105 + {
1.106 + DEBUG_PRINTF2(_L("Loading UPS policy file %S"), &aResourceFileName);
1.107 +
1.108 + RFile r;
1.109 + User::LeaveIfError(r.Open(aFs, aResourceFileName, EFileRead | EFileShareReadersOnly));
1.110 + CleanupClosePushL(r);
1.111 + TInt size;
1.112 + User::LeaveIfError(r.Size(size));
1.113 + CleanupStack::PopAndDestroy(&r);
1.114 +
1.115 + iResourceFile = CResourceFile::NewL(aFs, aResourceFileName, 0, size);
1.116 + iReader.OpenL(iResourceFile, KPolicyResourceId);
1.117 +
1.118 + TUidType u(iResourceFile->UidType());
1.119 + if (u[1] != TUid::Uid(KUidUpsPolicyResourceFile))
1.120 + {
1.121 + User::Leave(KErrUpsBadPolicyFile);
1.122 + }
1.123 +
1.124 + ReadPolicyHeaderL();
1.125 +
1.126 + iPolicyCount = iReader.ReadUint16L();
1.127 + if (iPolicyCount < 0)
1.128 + {
1.129 + User::Leave(KErrUpsBadPolicyFile);
1.130 + }
1.131 + }
1.132 +
1.133 +EXPORT_C const TPolicyHeader& CPolicyReader::Header() const
1.134 +/**
1.135 +Gets the header information for this policy file.
1.136 +@return A const reference to the policy header information.
1.137 +*/
1.138 + {
1.139 + return iHeader;
1.140 + }
1.141 +
1.142 +CPolicyReader::CPolicyReader()
1.143 +/**
1.144 +Constructor
1.145 +*/
1.146 + {
1.147 + }
1.148 +
1.149 +CPolicyReader::~CPolicyReader()
1.150 +/**
1.151 +Destructor
1.152 +*/
1.153 + {
1.154 + iReader.Close();
1.155 + delete iResourceFile;
1.156 + }
1.157 +
1.158 +EXPORT_C CPolicy* CPolicyReader::DefaultPolicyL()
1.159 +/**
1.160 +Factory method for creating a default policy object from the
1.161 +information in the UPS policy file header.
1.162 +
1.163 +@return A pointer to the default policy object.
1.164 +*/
1.165 + {
1.166 + _LIT(KAllDestinations, "*");
1.167 + TSidClasses allSidClasses(0xFFFF);
1.168 + RArray<TSecureId> sidList;
1.169 + CleanupClosePushL(sidList);
1.170 +
1.171 + CPolicy* p = CPolicy::NewL(
1.172 + allSidClasses, sidList, KAllDestinations,
1.173 + CPolicy::EYes | CPolicy::ENo, iHeader.iDefaultPolicyEvaluator, iHeader.iDefaultDialogCreator,
1.174 + 0, CPolicy::ESystemServerSecurityPassedOrFailed,
1.175 + iHeader.iMajorVersion, iHeader.iMinorVersion, ETrue);
1.176 +
1.177 + CleanupStack::PopAndDestroy(&sidList);
1.178 + return p;
1.179 + }
1.180 +
1.181 +EXPORT_C CPolicy* CPolicyReader::NextPolicyL()
1.182 +/**
1.183 +Gets the next policy from the resource file.
1.184 +
1.185 +@return A pointer to the new policy object or null if the end of
1.186 + the file has been reached.
1.187 +*/
1.188 + {
1.189 + CPolicy* p(0);
1.190 + if (iPolicyNum < iPolicyCount)
1.191 + {
1.192 + p = ReadPolicyL();
1.193 + ++iPolicyNum;
1.194 + }
1.195 + return p;
1.196 + }
1.197 +
1.198 +CPolicy* CPolicyReader::ReadPolicyL()
1.199 +/**
1.200 +Reads the next policy from the resource file.
1.201 +
1.202 +@return A pointer to the new policy object.
1.203 + */
1.204 + {
1.205 + TUint32 classes = iReader.ReadUint32L();
1.206 + TSidClasses sidClasses(classes);
1.207 +
1.208 + TInt numSids = iReader.ReadInt16L();
1.209 + if (numSids < 0)
1.210 + {
1.211 + User::Leave(KErrUpsBadPolicyFile);
1.212 + }
1.213 + RArray<TSecureId> sidList(8);
1.214 + CleanupClosePushL(sidList);
1.215 + for (TInt i = 0; i < numSids; ++i)
1.216 + {
1.217 + sidList.AppendL(TSecureId(iReader.ReadUint32L()));
1.218 + }
1.219 +
1.220 + TUint8 ssSecurity = iReader.ReadUint8L();
1.221 + CPolicy::TSystemServerSecurity systemServerSecurity =
1.222 + CPolicy::ESystemServerSecurityPassedOrFailed;
1.223 + switch (ssSecurity)
1.224 + {
1.225 + case CPolicy::ESystemServerSecurityPassedOrFailed:
1.226 + systemServerSecurity = CPolicy::ESystemServerSecurityPassedOrFailed;
1.227 + break;
1.228 + case CPolicy::ESystemServerSecurityPassed:
1.229 + systemServerSecurity = CPolicy::ESystemServerSecurityPassed;
1.230 + break;
1.231 + case CPolicy::ESystemServerSecurityFailed:
1.232 + systemServerSecurity = CPolicy::ESystemServerSecurityFailed;
1.233 + break;
1.234 + default:
1.235 + User::Leave(KErrUpsBadPolicyFile);
1.236 + break;
1.237 + }
1.238 +
1.239 + HBufC* destination(iReader.ReadHBufCL());
1.240 + if (! destination)
1.241 + {
1.242 + destination = HBufC::NewL(0);
1.243 + }
1.244 + CleanupStack::PushL(destination);
1.245 +
1.246 + TUint options(iReader.ReadUint32L());
1.247 + if ((options &
1.248 + ~(CPolicy::EYes|CPolicy::ENo|CPolicy::ESessionYes|CPolicy::ESessionNo|CPolicy::EAlways|CPolicy::ENever)) != 0)
1.249 + {
1.250 + User::Leave(KErrUpsBadPolicyFile);
1.251 + }
1.252 +
1.253 + TUid policyEvaluator = TUid::Uid(iReader.ReadInt32L());
1.254 + if (policyEvaluator == TUid::Null())
1.255 + {
1.256 + policyEvaluator = iHeader.iDefaultPolicyEvaluator;
1.257 + }
1.258 +
1.259 + TUid dialogCreator = TUid::Uid(iReader.ReadInt32L());
1.260 + if (dialogCreator == TUid::Null())
1.261 + {
1.262 + dialogCreator = iHeader.iDefaultDialogCreator;
1.263 + }
1.264 +
1.265 + TUint16 flags(iReader.ReadUint16L());
1.266 + iReader.ReadUint16L(); // skip over reserved flags
1.267 + iReader.ReadUint32L(); // skip over reserved link
1.268 +
1.269 + CPolicy* pol = CPolicy::NewL(sidClasses, sidList, *destination,
1.270 + options, policyEvaluator, dialogCreator, flags, systemServerSecurity,
1.271 + iHeader.iMajorVersion, iHeader.iMinorVersion);
1.272 +
1.273 + CleanupStack::PopAndDestroy(2, &sidList); // destination, sidList
1.274 + return pol;
1.275 + }