os/security/authorisation/userpromptservice/policies/source/policylist.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "policylist.h"
sl@0
    20
#include "policyreader.h"
sl@0
    21
#include "promptrequest.h"
sl@0
    22
#include <f32file.h>
sl@0
    23
#include <ups/upserr.h>
sl@0
    24
#include "upslog.h"
sl@0
    25
sl@0
    26
using namespace UserPromptService;
sl@0
    27
sl@0
    28
// CPolicyList::TId ----------------------------------------------------------
sl@0
    29
EXPORT_C CPolicyList::TId::TId() 
sl@0
    30
/**
sl@0
    31
Constructor
sl@0
    32
*/	
sl@0
    33
	: iServerSid(), iServiceId(TUid::Null())
sl@0
    34
	{
sl@0
    35
	}
sl@0
    36
sl@0
    37
EXPORT_C CPolicyList::TId::TId(const TSecureId& aServerSid, const TServiceId& aServiceId)
sl@0
    38
/**
sl@0
    39
Constructor
sl@0
    40
@param	aServerSid	The secure id of the system server
sl@0
    41
@param	aServiceId	The UID of the service.
sl@0
    42
*/
sl@0
    43
	: iServerSid(aServerSid), iServiceId(aServiceId)
sl@0
    44
	{	
sl@0
    45
	}
sl@0
    46
sl@0
    47
EXPORT_C TBool CPolicyList::TId::operator==(const CPolicyList::TId& aId) const
sl@0
    48
/**
sl@0
    49
Tests whether this policy list id is euivalent to another policy list id.
sl@0
    50
sl@0
    51
@param	aId	The policy list id object to compare this object against.
sl@0
    52
@return ETrue if the system server secure id and the service UID match;
sl@0
    53
		otherwise, EFalse is returned.
sl@0
    54
*/
sl@0
    55
	{
sl@0
    56
	return (iServerSid == aId.iServerSid &&	iServiceId == aId.iServiceId);
sl@0
    57
	}
sl@0
    58
sl@0
    59
EXPORT_C void CPolicyList::TId::IdL(const TDesC& aPolicyFile, CPolicyList::TId& aId)
sl@0
    60
/**
sl@0
    61
Determines the policy list id from the name of the policy file.
sl@0
    62
@param	aPolicyFile	The filename of the policy file.
sl@0
    63
@param	aId			The id object to populate.
sl@0
    64
sl@0
    65
@leave	KErrUpsInvalidPolicyFileName if the policy file is not in the correct format. 
sl@0
    66
*/
sl@0
    67
	{
sl@0
    68
	TParse* p = new (ELeave) TParse();
sl@0
    69
	CleanupStack::PushL(p);
sl@0
    70
	TInt err(KErrNone);
sl@0
    71
	
sl@0
    72
	if ((err = p->Set(aPolicyFile, NULL, NULL)) == KErrNone)
sl@0
    73
		{
sl@0
    74
		TPtrC n = p->NameAndExt();
sl@0
    75
		_LIT(KPolicyFilePattern, "ups_????????_????????.rsc");
sl@0
    76
		
sl@0
    77
		if (n.MatchF(KPolicyFilePattern) != KErrNotFound)
sl@0
    78
			{
sl@0
    79
			TUint32 serverSid;
sl@0
    80
			TLex l(n.Mid(4,8));
sl@0
    81
			if ((err = l.Val(serverSid, EHex)) == KErrNone)
sl@0
    82
				{
sl@0
    83
				TUint32 serviceId;
sl@0
    84
				l = n.Mid(13,8);
sl@0
    85
				if ((err = l.Val(serviceId, EHex)) == KErrNone)
sl@0
    86
					{
sl@0
    87
					aId.iServerSid = TSecureId(serverSid);
sl@0
    88
					aId.iServiceId = TServiceId::Uid(serviceId);
sl@0
    89
					}
sl@0
    90
				}
sl@0
    91
			}
sl@0
    92
		}
sl@0
    93
		
sl@0
    94
	if (err != KErrNone)
sl@0
    95
		{
sl@0
    96
		User::Leave(KErrUpsInvalidPolicyFileName);
sl@0
    97
		}
sl@0
    98
		
sl@0
    99
	CleanupStack::PopAndDestroy(p);	
sl@0
   100
	}
sl@0
   101
sl@0
   102
EXPORT_C void CPolicyList::TId::AppendNameToPath(TDes& aFileName) const
sl@0
   103
	{
sl@0
   104
	_LIT(KPolicyFileNameFormat, "ups_%08x_%08x.rsc");
sl@0
   105
	aFileName.AppendFormat(KPolicyFileNameFormat, iServerSid.iId, iServiceId.iUid);
sl@0
   106
	}
sl@0
   107
sl@0
   108
// CPolicyList ---------------------------------------------------------------
sl@0
   109
EXPORT_C CPolicyList* CPolicyList::NewL(const CPolicyList::TId& aId, CPolicyReader& aReader)
sl@0
   110
/**
sl@0
   111
Factory method for creating policy list objects.
sl@0
   112
@param	aId			The id (system server SID and service id) of the UPS policy file.
sl@0
   113
@param	aReader		The policy file resource parser.
sl@0
   114
@return A pointer to the new policy list.
sl@0
   115
*/
sl@0
   116
	{
sl@0
   117
	CPolicyList* self = CPolicyList::NewLC(aId, aReader);
sl@0
   118
	CleanupStack::Pop(self);
sl@0
   119
	return self;
sl@0
   120
	}
sl@0
   121
sl@0
   122
EXPORT_C CPolicyList* CPolicyList::NewLC(const CPolicyList::TId& aId, CPolicyReader& aReader)
sl@0
   123
/**
sl@0
   124
Factory method for policy list objects. A pointer to the new policy list object is placed
sl@0
   125
on the cleanup stack.
sl@0
   126
sl@0
   127
@param	aId			The id (system server SID and service id) of the UPS policy file.
sl@0
   128
@param	aReader		The policy file resource parser.
sl@0
   129
@return A pointer to the new policy list.
sl@0
   130
*/
sl@0
   131
	{
sl@0
   132
	CPolicyList* self = new(ELeave) CPolicyList(aId);
sl@0
   133
	CleanupStack::PushL(self);
sl@0
   134
	self->ConstructL(aReader);
sl@0
   135
	return self;
sl@0
   136
	}
sl@0
   137
sl@0
   138
CPolicyList::CPolicyList(const CPolicyList::TId& aId) : iId(aId)
sl@0
   139
/**
sl@0
   140
Constructor
sl@0
   141
@param aId	The ID of the UPS policy list file.
sl@0
   142
*/
sl@0
   143
	{	
sl@0
   144
	}
sl@0
   145
sl@0
   146
CPolicyList::~CPolicyList()
sl@0
   147
/**
sl@0
   148
Destructor
sl@0
   149
*/
sl@0
   150
	{
sl@0
   151
	delete iDefaultPolicy;
sl@0
   152
	iPolicies.ResetAndDestroy();
sl@0
   153
	}
sl@0
   154
sl@0
   155
void CPolicyList::ConstructL(CPolicyReader& aReader)
sl@0
   156
/**
sl@0
   157
Second phase constructor that reads all policies in the policy file.
sl@0
   158
@param aReader	The policy reader instance.
sl@0
   159
*/
sl@0
   160
	{
sl@0
   161
	const TPolicyHeader& hdr = aReader.Header();
sl@0
   162
	iServiceConfig.iServiceId = iId.iServiceId.iUid;
sl@0
   163
	iServiceConfig.iPolicy = hdr.iAuthPolicy;
sl@0
   164
	iServiceConfig.iMajorVersion = hdr.iMajorVersion;
sl@0
   165
	iServiceConfig.iMinorVersion = hdr.iMinorVersion;
sl@0
   166
	
sl@0
   167
	CPolicy* p(0);	
sl@0
   168
	while ((p = aReader.NextPolicyL()) != 0)
sl@0
   169
		{
sl@0
   170
		CleanupStack::PushL(p);
sl@0
   171
		iPolicies.AppendL(p);
sl@0
   172
		CleanupStack::Pop(p);
sl@0
   173
		}
sl@0
   174
	
sl@0
   175
	iDefaultPolicy = aReader.DefaultPolicyL();
sl@0
   176
	}
sl@0
   177
sl@0
   178
EXPORT_C const CPolicy* CPolicyList::Match(const CPromptRequest& aRequest) const
sl@0
   179
/**
sl@0
   180
Tests each policy in the list in turn and returns the first matching policy. If no match
sl@0
   181
is found then a 'default' policy object is returned.
sl@0
   182
sl@0
   183
@param	aRequest	The request to match against the policies.
sl@0
   184
@return				A pointer to the policy object to use for the request.
sl@0
   185
*/
sl@0
   186
	{
sl@0
   187
	TInt n = iPolicies.Count();
sl@0
   188
	const CPolicy* p(0);
sl@0
   189
	for (TInt i = 0; i < n; ++i)
sl@0
   190
		{
sl@0
   191
		if (iPolicies[i]->Matches(aRequest.ClientSid(), aRequest.Destination(), aRequest.SecurityResult())) 
sl@0
   192
			{
sl@0
   193
			p = iPolicies[i];
sl@0
   194
			DEBUG_PRINTF5(_L8("Using policy %d for client sid = 0x%08x, system server sid = 0x%08x, service id = 0x%08x"),
sl@0
   195
				i, aRequest.ClientSid().iId, aRequest.ServerSid().iId, aRequest.ServiceId().iUid);					
sl@0
   196
			break;
sl@0
   197
			}
sl@0
   198
		}
sl@0
   199
sl@0
   200
	if (! p)
sl@0
   201
		{
sl@0
   202
		DEBUG_PRINTF4(_L8("Using default policy for client sid = 0x%08x, system server sid = 0x%08x, service id = 0x%08x"),
sl@0
   203
			aRequest.ClientSid().iId, aRequest.ServerSid().iId, aRequest.ServiceId().iUid);
sl@0
   204
		p = iDefaultPolicy;
sl@0
   205
		}
sl@0
   206
	return p;
sl@0
   207
	}
sl@0
   208
	
sl@0
   209
EXPORT_C const CPolicyList::TId& CPolicyList::Id() const
sl@0
   210
/**
sl@0
   211
Gets the ID that associates a list of policies with a system server SID and service ID.
sl@0
   212
@return The ID of the policy list.
sl@0
   213
*/
sl@0
   214
	{
sl@0
   215
	return iId;
sl@0
   216
	}
sl@0
   217
sl@0
   218
EXPORT_C const TServiceConfig& CPolicyList::ServiceConfig() const
sl@0
   219
/**
sl@0
   220
Gets the service configuration information for this policy file. This is used
sl@0
   221
by the UPS system-server API to determine whether or not to invoke the UPS.
sl@0
   222
@return A const reference to the service configuration information.
sl@0
   223
*/
sl@0
   224
	{
sl@0
   225
	return iServiceConfig;
sl@0
   226
	}