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