os/security/authorisation/userpromptservice/policies/source/policylist.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/authorisation/userpromptservice/policies/source/policylist.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,226 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 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 "policylist.h"
    1.23 +#include "policyreader.h"
    1.24 +#include "promptrequest.h"
    1.25 +#include <f32file.h>
    1.26 +#include <ups/upserr.h>
    1.27 +#include "upslog.h"
    1.28 +
    1.29 +using namespace UserPromptService;
    1.30 +
    1.31 +// CPolicyList::TId ----------------------------------------------------------
    1.32 +EXPORT_C CPolicyList::TId::TId() 
    1.33 +/**
    1.34 +Constructor
    1.35 +*/	
    1.36 +	: iServerSid(), iServiceId(TUid::Null())
    1.37 +	{
    1.38 +	}
    1.39 +
    1.40 +EXPORT_C CPolicyList::TId::TId(const TSecureId& aServerSid, const TServiceId& aServiceId)
    1.41 +/**
    1.42 +Constructor
    1.43 +@param	aServerSid	The secure id of the system server
    1.44 +@param	aServiceId	The UID of the service.
    1.45 +*/
    1.46 +	: iServerSid(aServerSid), iServiceId(aServiceId)
    1.47 +	{	
    1.48 +	}
    1.49 +
    1.50 +EXPORT_C TBool CPolicyList::TId::operator==(const CPolicyList::TId& aId) const
    1.51 +/**
    1.52 +Tests whether this policy list id is euivalent to another policy list id.
    1.53 +
    1.54 +@param	aId	The policy list id object to compare this object against.
    1.55 +@return ETrue if the system server secure id and the service UID match;
    1.56 +		otherwise, EFalse is returned.
    1.57 +*/
    1.58 +	{
    1.59 +	return (iServerSid == aId.iServerSid &&	iServiceId == aId.iServiceId);
    1.60 +	}
    1.61 +
    1.62 +EXPORT_C void CPolicyList::TId::IdL(const TDesC& aPolicyFile, CPolicyList::TId& aId)
    1.63 +/**
    1.64 +Determines the policy list id from the name of the policy file.
    1.65 +@param	aPolicyFile	The filename of the policy file.
    1.66 +@param	aId			The id object to populate.
    1.67 +
    1.68 +@leave	KErrUpsInvalidPolicyFileName if the policy file is not in the correct format. 
    1.69 +*/
    1.70 +	{
    1.71 +	TParse* p = new (ELeave) TParse();
    1.72 +	CleanupStack::PushL(p);
    1.73 +	TInt err(KErrNone);
    1.74 +	
    1.75 +	if ((err = p->Set(aPolicyFile, NULL, NULL)) == KErrNone)
    1.76 +		{
    1.77 +		TPtrC n = p->NameAndExt();
    1.78 +		_LIT(KPolicyFilePattern, "ups_????????_????????.rsc");
    1.79 +		
    1.80 +		if (n.MatchF(KPolicyFilePattern) != KErrNotFound)
    1.81 +			{
    1.82 +			TUint32 serverSid;
    1.83 +			TLex l(n.Mid(4,8));
    1.84 +			if ((err = l.Val(serverSid, EHex)) == KErrNone)
    1.85 +				{
    1.86 +				TUint32 serviceId;
    1.87 +				l = n.Mid(13,8);
    1.88 +				if ((err = l.Val(serviceId, EHex)) == KErrNone)
    1.89 +					{
    1.90 +					aId.iServerSid = TSecureId(serverSid);
    1.91 +					aId.iServiceId = TServiceId::Uid(serviceId);
    1.92 +					}
    1.93 +				}
    1.94 +			}
    1.95 +		}
    1.96 +		
    1.97 +	if (err != KErrNone)
    1.98 +		{
    1.99 +		User::Leave(KErrUpsInvalidPolicyFileName);
   1.100 +		}
   1.101 +		
   1.102 +	CleanupStack::PopAndDestroy(p);	
   1.103 +	}
   1.104 +
   1.105 +EXPORT_C void CPolicyList::TId::AppendNameToPath(TDes& aFileName) const
   1.106 +	{
   1.107 +	_LIT(KPolicyFileNameFormat, "ups_%08x_%08x.rsc");
   1.108 +	aFileName.AppendFormat(KPolicyFileNameFormat, iServerSid.iId, iServiceId.iUid);
   1.109 +	}
   1.110 +
   1.111 +// CPolicyList ---------------------------------------------------------------
   1.112 +EXPORT_C CPolicyList* CPolicyList::NewL(const CPolicyList::TId& aId, CPolicyReader& aReader)
   1.113 +/**
   1.114 +Factory method for creating policy list objects.
   1.115 +@param	aId			The id (system server SID and service id) of the UPS policy file.
   1.116 +@param	aReader		The policy file resource parser.
   1.117 +@return A pointer to the new policy list.
   1.118 +*/
   1.119 +	{
   1.120 +	CPolicyList* self = CPolicyList::NewLC(aId, aReader);
   1.121 +	CleanupStack::Pop(self);
   1.122 +	return self;
   1.123 +	}
   1.124 +
   1.125 +EXPORT_C CPolicyList* CPolicyList::NewLC(const CPolicyList::TId& aId, CPolicyReader& aReader)
   1.126 +/**
   1.127 +Factory method for policy list objects. A pointer to the new policy list object is placed
   1.128 +on the cleanup stack.
   1.129 +
   1.130 +@param	aId			The id (system server SID and service id) of the UPS policy file.
   1.131 +@param	aReader		The policy file resource parser.
   1.132 +@return A pointer to the new policy list.
   1.133 +*/
   1.134 +	{
   1.135 +	CPolicyList* self = new(ELeave) CPolicyList(aId);
   1.136 +	CleanupStack::PushL(self);
   1.137 +	self->ConstructL(aReader);
   1.138 +	return self;
   1.139 +	}
   1.140 +
   1.141 +CPolicyList::CPolicyList(const CPolicyList::TId& aId) : iId(aId)
   1.142 +/**
   1.143 +Constructor
   1.144 +@param aId	The ID of the UPS policy list file.
   1.145 +*/
   1.146 +	{	
   1.147 +	}
   1.148 +
   1.149 +CPolicyList::~CPolicyList()
   1.150 +/**
   1.151 +Destructor
   1.152 +*/
   1.153 +	{
   1.154 +	delete iDefaultPolicy;
   1.155 +	iPolicies.ResetAndDestroy();
   1.156 +	}
   1.157 +
   1.158 +void CPolicyList::ConstructL(CPolicyReader& aReader)
   1.159 +/**
   1.160 +Second phase constructor that reads all policies in the policy file.
   1.161 +@param aReader	The policy reader instance.
   1.162 +*/
   1.163 +	{
   1.164 +	const TPolicyHeader& hdr = aReader.Header();
   1.165 +	iServiceConfig.iServiceId = iId.iServiceId.iUid;
   1.166 +	iServiceConfig.iPolicy = hdr.iAuthPolicy;
   1.167 +	iServiceConfig.iMajorVersion = hdr.iMajorVersion;
   1.168 +	iServiceConfig.iMinorVersion = hdr.iMinorVersion;
   1.169 +	
   1.170 +	CPolicy* p(0);	
   1.171 +	while ((p = aReader.NextPolicyL()) != 0)
   1.172 +		{
   1.173 +		CleanupStack::PushL(p);
   1.174 +		iPolicies.AppendL(p);
   1.175 +		CleanupStack::Pop(p);
   1.176 +		}
   1.177 +	
   1.178 +	iDefaultPolicy = aReader.DefaultPolicyL();
   1.179 +	}
   1.180 +
   1.181 +EXPORT_C const CPolicy* CPolicyList::Match(const CPromptRequest& aRequest) const
   1.182 +/**
   1.183 +Tests each policy in the list in turn and returns the first matching policy. If no match
   1.184 +is found then a 'default' policy object is returned.
   1.185 +
   1.186 +@param	aRequest	The request to match against the policies.
   1.187 +@return				A pointer to the policy object to use for the request.
   1.188 +*/
   1.189 +	{
   1.190 +	TInt n = iPolicies.Count();
   1.191 +	const CPolicy* p(0);
   1.192 +	for (TInt i = 0; i < n; ++i)
   1.193 +		{
   1.194 +		if (iPolicies[i]->Matches(aRequest.ClientSid(), aRequest.Destination(), aRequest.SecurityResult())) 
   1.195 +			{
   1.196 +			p = iPolicies[i];
   1.197 +			DEBUG_PRINTF5(_L8("Using policy %d for client sid = 0x%08x, system server sid = 0x%08x, service id = 0x%08x"),
   1.198 +				i, aRequest.ClientSid().iId, aRequest.ServerSid().iId, aRequest.ServiceId().iUid);					
   1.199 +			break;
   1.200 +			}
   1.201 +		}
   1.202 +
   1.203 +	if (! p)
   1.204 +		{
   1.205 +		DEBUG_PRINTF4(_L8("Using default policy for client sid = 0x%08x, system server sid = 0x%08x, service id = 0x%08x"),
   1.206 +			aRequest.ClientSid().iId, aRequest.ServerSid().iId, aRequest.ServiceId().iUid);
   1.207 +		p = iDefaultPolicy;
   1.208 +		}
   1.209 +	return p;
   1.210 +	}
   1.211 +	
   1.212 +EXPORT_C const CPolicyList::TId& CPolicyList::Id() const
   1.213 +/**
   1.214 +Gets the ID that associates a list of policies with a system server SID and service ID.
   1.215 +@return The ID of the policy list.
   1.216 +*/
   1.217 +	{
   1.218 +	return iId;
   1.219 +	}
   1.220 +
   1.221 +EXPORT_C const TServiceConfig& CPolicyList::ServiceConfig() const
   1.222 +/**
   1.223 +Gets the service configuration information for this policy file. This is used
   1.224 +by the UPS system-server API to determine whether or not to invoke the UPS.
   1.225 +@return A const reference to the service configuration information.
   1.226 +*/
   1.227 +	{
   1.228 +	return iServiceConfig;
   1.229 +	}