os/persistentdata/loggingservices/eventlogger/LogServ/src/logservsecurity.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "logservsecurity.h"
sl@0
    17
#include <e32capability.h>
sl@0
    18
#include "LogServResourceInterpreter.h"
sl@0
    19
#include "LogCliServShared.h"
sl@0
    20
#include "logservpanic.h"
sl@0
    21
sl@0
    22
/**
sl@0
    23
The max number of TCapability(s) that can be used to instantiate a TSecurityPolicy
sl@0
    24
@internalComponent
sl@0
    25
*/
sl@0
    26
const TInt KMaxCapsPerOp = 7;
sl@0
    27
sl@0
    28
///////////////////////////////////////////////////////////////////////////////
sl@0
    29
// TCaps class - declaration and implementation
sl@0
    30
sl@0
    31
/**
sl@0
    32
The class represents a static array of TCapability items (it can't grow or shrink).
sl@0
    33
The class should be used every time when there is a need of a static TCapability array.
sl@0
    34
It offers an overloaded "[]" operator with run-time bounds checks.
sl@0
    35
@internalComponent
sl@0
    36
*/
sl@0
    37
class TCaps
sl@0
    38
	{
sl@0
    39
public:	
sl@0
    40
	TCaps();
sl@0
    41
	inline TInt MaxSize() const
sl@0
    42
		{
sl@0
    43
		return KMaxCapsPerOp;
sl@0
    44
		}
sl@0
    45
	inline TCapability& operator [](TInt aIndex)
sl@0
    46
		{
sl@0
    47
		__ASSERT_DEBUG(aIndex >= 0 && aIndex < MaxSize(), User::Invariant());
sl@0
    48
		return iItems[aIndex];
sl@0
    49
		}
sl@0
    50
	
sl@0
    51
	inline const TCapability& operator [](TInt aIndex) const
sl@0
    52
		{
sl@0
    53
		__ASSERT_DEBUG(aIndex >= 0 && aIndex < MaxSize(), User::Invariant());
sl@0
    54
		return iItems[aIndex];
sl@0
    55
		}
sl@0
    56
		
sl@0
    57
private:
sl@0
    58
	TCapability iItems[KMaxCapsPerOp];
sl@0
    59
	
sl@0
    60
	};
sl@0
    61
sl@0
    62
/**
sl@0
    63
The controlled capabilities are initialized with ECapability_None value.
sl@0
    64
*/
sl@0
    65
TCaps::TCaps()
sl@0
    66
	{
sl@0
    67
	for(TInt i=0;i<MaxSize();++i)
sl@0
    68
		{
sl@0
    69
		iItems[i] = ECapability_None;
sl@0
    70
		}
sl@0
    71
	}
sl@0
    72
	
sl@0
    73
///////////////////////////////////////////////////////////////////////////////
sl@0
    74
// TEventPolicy structure - declaration and implementation
sl@0
    75
sl@0
    76
/**
sl@0
    77
Each LogEngServer event defined in Logwrap.rss has two associated
sl@0
    78
TSecurityPolicy(s) - one each for read/write operations. This structure 
sl@0
    79
contains one read and one write policy per defined LogEngServer event.
sl@0
    80
@internalComponent
sl@0
    81
*/
sl@0
    82
struct TEventPolicy 
sl@0
    83
	{
sl@0
    84
	TEventPolicy(TUid aEventType, const TCaps& aReadCaps, const TCaps& aWriteCaps);
sl@0
    85
				 
sl@0
    86
	TUid 			iEventType;// event type defined in LOGWRAP.RSS
sl@0
    87
	TSecurityPolicy	iReadPolicy;
sl@0
    88
	TSecurityPolicy	iWritePolicy;	
sl@0
    89
	};
sl@0
    90
sl@0
    91
/**
sl@0
    92
@param aEventType Event type. It could be one of the following: KLogCallEventType,
sl@0
    93
					KLogDataEventType, KLogFaxEventType, KLogShortMessageEventType,
sl@0
    94
					KLogMailEventType, KLogTaskSchedulerEventType, KLogPacketDataEventType.
sl@0
    95
					See LOGWRAP.RSS file where these constants are defined.
sl@0
    96
@param aReadCaps Read capablities for aEventType argument. The client, who wants to use
sl@0
    97
			 	 that event type ("read" operations), must satisfy aRead set of capabilities.					
sl@0
    98
@param aWriteCaps Write capablities for aEventType argument. The client, who wants to use
sl@0
    99
			 	 that event type ("write" operations), must satisfy aWrite set of capabilities.					
sl@0
   100
*/	
sl@0
   101
TEventPolicy::TEventPolicy(TUid aEventType, const TCaps& aReadCaps, const TCaps& aWriteCaps) :
sl@0
   102
	iEventType(aEventType),
sl@0
   103
	iReadPolicy(aReadCaps[0],aReadCaps[1],aReadCaps[2],aReadCaps[3],aReadCaps[4],aReadCaps[5],aReadCaps[6]),
sl@0
   104
	iWritePolicy(aWriteCaps[0],aWriteCaps[1],aWriteCaps[2],aWriteCaps[3],aWriteCaps[4],aWriteCaps[5],aWriteCaps[6])
sl@0
   105
	{
sl@0
   106
	}
sl@0
   107
sl@0
   108
///////////////////////////////////////////////////////////////////////////////
sl@0
   109
// TSecurityInfoReader class declaration
sl@0
   110
sl@0
   111
//Forward declaration
sl@0
   112
class CLogServSecurityImpl;
sl@0
   113
	
sl@0
   114
/**
sl@0
   115
The class manages the reading of the Security policy data from Logwrap.rss and storing 
sl@0
   116
it in the supplied as an argument CLogServSecurityImpl object.
sl@0
   117
@internalComponent
sl@0
   118
*/
sl@0
   119
class TSecurityInfoReader
sl@0
   120
	{
sl@0
   121
public:
sl@0
   122
	TSecurityInfoReader(CLogServResourceInterpreter& aResourceInterface, 
sl@0
   123
						CLogServSecurityImpl& aLogServSecurity);
sl@0
   124
	void ReadL();
sl@0
   125
	
sl@0
   126
private:
sl@0
   127
	TInt GetEventTypeCountL();
sl@0
   128
	void GetCapabilities(TResourceReader& aReader, TCaps& aCaps);
sl@0
   129
sl@0
   130
private:
sl@0
   131
	CLogServResourceInterpreter& iResourceInterface;
sl@0
   132
	CLogServSecurityImpl& iLogServSecurity;
sl@0
   133
	
sl@0
   134
	};
sl@0
   135
sl@0
   136
///////////////////////////////////////////////////////////////////////////////
sl@0
   137
// CLogServSecurityImpl class declaration and implementation.
sl@0
   138
sl@0
   139
/**
sl@0
   140
The class implements pure virtual methods in CLogServSecurity class.
sl@0
   141
All functionality, related to processing the data in LogEngServer resource file,
sl@0
   142
is delegated to an instance of TSecurityInfoReader class.
sl@0
   143
@internalComponent
sl@0
   144
*/
sl@0
   145
class CLogServSecurityImpl : public CLogServSecurity
sl@0
   146
	{
sl@0
   147
	friend class TSecurityInfoReader;
sl@0
   148
	
sl@0
   149
public:
sl@0
   150
	static CLogServSecurityImpl* NewL(CLogServResourceInterpreter& aResourceInterface);
sl@0
   151
	virtual ~CLogServSecurityImpl();		
sl@0
   152
	virtual TBool IsAllowed(const RMessage2& aMsg, TUid aEventType, TEventOp aEventOp, const char* aDiagnostic);
sl@0
   153
#ifdef LOGSERV_CAPABILITY_TEST
sl@0
   154
	virtual TSecurityPolicy SecurityPolicy(TUid aEventType, TEventOp aEventOp);
sl@0
   155
#endif //LOGSERV_CAPABILITY_TEST
sl@0
   156
	
sl@0
   157
private:
sl@0
   158
	CLogServSecurityImpl();
sl@0
   159
	void ConstructL(CLogServResourceInterpreter& aResourceInterface);		
sl@0
   160
	const TSecurityPolicy& FindPolicy(TUid aEventType, TEventOp aEventOp) const;
sl@0
   161
	
sl@0
   162
private:
sl@0
   163
	RArray<TEventPolicy> iPolicyCon;
sl@0
   164
	TSecurityPolicy iPassAllPolicy;
sl@0
   165
		
sl@0
   166
	};
sl@0
   167
sl@0
   168
/**
sl@0
   169
Standard, phase-one factory method for creation of objects of CLogServSecurityImpl type.
sl@0
   170
@param aResourceInterface A reference to CLogServResourceInterpreter object used for reading
sl@0
   171
 						  the LogEngServer resource file (logwrap.rss). It is used only durring 
sl@0
   172
 						  the construction phase of CLogServSecurityImpl instance.
sl@0
   173
@return A pointer to the created CLogServSecurityImpl instance.
sl@0
   174
@leave System-wide error codes, including KErrNoMemory and reading file errors.
sl@0
   175
*/	
sl@0
   176
CLogServSecurityImpl* CLogServSecurityImpl::NewL(CLogServResourceInterpreter& aResourceInterface)
sl@0
   177
	{
sl@0
   178
	CLogServSecurityImpl* self = new (ELeave) CLogServSecurityImpl;
sl@0
   179
	CleanupStack::PushL(self);
sl@0
   180
	self->ConstructL(aResourceInterface);
sl@0
   181
	CleanupStack::Pop(self);
sl@0
   182
	return self;
sl@0
   183
	}
sl@0
   184
sl@0
   185
/**
sl@0
   186
*/
sl@0
   187
CLogServSecurityImpl::~CLogServSecurityImpl()
sl@0
   188
	{
sl@0
   189
	iPolicyCon.Close();
sl@0
   190
	}
sl@0
   191
sl@0
   192
/**
sl@0
   193
The method compares the caller's capabilities against the set of capabilities,
sl@0
   194
required for that kind of operation (read or write) and returns ETrue or EFalse.
sl@0
   195
@param aMsg The message, containing the caller capabilities which have to be checked.
sl@0
   196
@param aEventType Event type. For more details see LOGWRAP.RSS file where the  
sl@0
   197
				  UID constants are defined.
sl@0
   198
@param aEventOp The type of the operation which is about to be performed by the 
sl@0
   199
					  caller. It could be EReadOp or EWriteOp.
sl@0
   200
@return ETrue - the caller is allowed to execute the operation, EFalse - the caller's
sl@0
   201
				capabilities do not match the required set of capabilities for that
sl@0
   202
				kind of operation (read or write).	
sl@0
   203
Note: Only built-in types (included in logwrap.rss) are policed.
sl@0
   204
	  So, return ETrue if TUid argument isn't a built-in type.
sl@0
   205
*/
sl@0
   206
TBool CLogServSecurityImpl::IsAllowed(const RMessage2& aMsg, TUid aEventType, TEventOp aEventOp, const char* aDiagnostic)
sl@0
   207
	{
sl@0
   208
	const TSecurityPolicy& policy = FindPolicy(aEventType, aEventOp);
sl@0
   209
	return policy.CheckPolicy(aMsg, aDiagnostic);
sl@0
   210
	}
sl@0
   211
sl@0
   212
#ifdef LOGSERV_CAPABILITY_TEST
sl@0
   213
/**
sl@0
   214
This method is declared and implemented only if "LOGSERV_CAPABILITY_TEST" macro is defined
sl@0
   215
@param aEventType Event type. For more details see LOGWRAP.RSS file where the  
sl@0
   216
				  UID constants are defined.
sl@0
   217
@param aEventOp The type of the event operation: EReadOp or EWriteOp.
sl@0
   218
@return The related with {aEventType, aEventOp} pair TSecurityPOlicy object.
sl@0
   219
*/
sl@0
   220
TSecurityPolicy CLogServSecurityImpl::SecurityPolicy(TUid aEventType, TEventOp aEventOp)
sl@0
   221
	{
sl@0
   222
	const TSecurityPolicy& policy = FindPolicy(aEventType, aEventOp);
sl@0
   223
	return policy;
sl@0
   224
	}
sl@0
   225
#endif //LOGSERV_CAPABILITY_TEST
sl@0
   226
sl@0
   227
/**
sl@0
   228
*/
sl@0
   229
CLogServSecurityImpl::CLogServSecurityImpl() :
sl@0
   230
	iPassAllPolicy(TSecurityPolicy::EAlwaysPass)
sl@0
   231
	{
sl@0
   232
	}
sl@0
   233
sl@0
   234
/**
sl@0
   235
Standard, phase-two construction method for creation of CLogServSecurityImpl objects.
sl@0
   236
@param aResourceInterface A reference to CLogServResourceInterpreter object used for reading
sl@0
   237
 						  the LogEngServer resource file (logwrap.rss). It is used only durring 
sl@0
   238
 						  the construction phase of CLogServSecurityImpl instance.
sl@0
   239
@leave System-wide error codes, including KErrNoMemory and reading file errors.
sl@0
   240
*/	
sl@0
   241
void CLogServSecurityImpl::ConstructL(CLogServResourceInterpreter& aResourceInterface)
sl@0
   242
	{
sl@0
   243
	TSecurityInfoReader reader(aResourceInterface, *this);
sl@0
   244
	reader.ReadL();
sl@0
   245
	}
sl@0
   246
sl@0
   247
/**
sl@0
   248
The method performs a search for the related to {aEventType, aOperationType} pair 
sl@0
   249
TSecurityPolicy object. If there is no registered TSecurityPolicy object for the
sl@0
   250
supplied pair of arguments (which is possible, if aEventType argument is not a
sl@0
   251
built-in type, specified in LOGWRAP.RSS file), then the method returns a reference
sl@0
   252
to pass-all TSecurityPolicy object.
sl@0
   253
@param aEventType Event type. For more details see LOGWRAP.RSS file where the  
sl@0
   254
				  UID constants are defined.
sl@0
   255
@param aAccessType The type of the operation which is about to be performed by the 
sl@0
   256
					  caller. It could be ERead or EWrite.
sl@0
   257
@return A const reference to TSecurityPolicy object, which defines a set of capabilities,
sl@0
   258
		required for that kind of operation (read or write).
sl@0
   259
*/	
sl@0
   260
const TSecurityPolicy& CLogServSecurityImpl::FindPolicy(TUid aEventType, TEventOp aEventOp) const
sl@0
   261
	{
sl@0
   262
	for(TInt i=iPolicyCon.Count()-1;i>=0;--i)
sl@0
   263
		{
sl@0
   264
		const TEventPolicy& eventPolicy = iPolicyCon[i];
sl@0
   265
		if(eventPolicy.iEventType == aEventType)
sl@0
   266
			{
sl@0
   267
			return aEventOp == EWriteOp ? eventPolicy.iWritePolicy : eventPolicy.iReadPolicy;
sl@0
   268
			}
sl@0
   269
		}
sl@0
   270
	// aEventType wasn't found - it doesn't represent a policed event type.		
sl@0
   271
	return iPassAllPolicy;	
sl@0
   272
	}
sl@0
   273
sl@0
   274
///////////////////////////////////////////////////////////////////////////////
sl@0
   275
// CLogServSecurity implementation
sl@0
   276
sl@0
   277
/**
sl@0
   278
Standard, phase-one factory method for creation of objects of CLogServSecurity type.
sl@0
   279
@param aResourceInterface A reference to CLogServResourceInterpreter object used for reading
sl@0
   280
 						  the LogEngServer resource file (logwrap.rss).
sl@0
   281
@return A pointer to the created CLogServSecurity instance.
sl@0
   282
@leave System-wide error codes, including KErrNoMemory and reading file errors.
sl@0
   283
*/	
sl@0
   284
CLogServSecurity* CLogServSecurity::NewL(CLogServResourceInterpreter& aResourceInterface)
sl@0
   285
	{
sl@0
   286
	return CLogServSecurityImpl::NewL(aResourceInterface);
sl@0
   287
	}
sl@0
   288
sl@0
   289
/**
sl@0
   290
*/
sl@0
   291
CLogServSecurity::~CLogServSecurity()
sl@0
   292
	{
sl@0
   293
	}
sl@0
   294
	
sl@0
   295
///////////////////////////////////////////////////////////////////////////////
sl@0
   296
// TSecurityInfoReader class implementation
sl@0
   297
sl@0
   298
/**
sl@0
   299
@param aResourceInterface A reference to CLogServResourceInterpreter object used for reading
sl@0
   300
 						  the LogEngServer resource file (logwrap.rss).
sl@0
   301
@param aLogServSecurity A reference to CLogServSecurityImpl instance, which internal content
sl@0
   302
						will be initialized with the related information from the 
sl@0
   303
						LogEngServer resource file.
sl@0
   304
*/		
sl@0
   305
TSecurityInfoReader::TSecurityInfoReader(CLogServResourceInterpreter& aResourceInterface,
sl@0
   306
										 CLogServSecurityImpl& aLogServSecurity) :
sl@0
   307
	iResourceInterface(aResourceInterface),
sl@0
   308
	iLogServSecurity(aLogServSecurity)
sl@0
   309
	{
sl@0
   310
	}
sl@0
   311
	
sl@0
   312
/**
sl@0
   313
The method reads the LogEngServer events capabilities from the resource file and 
sl@0
   314
initializes with them iLogServSecurity data member;
sl@0
   315
@leave System-wide error codes, including KErrNoMemory and reading file errors.
sl@0
   316
@panic ELogSecurityCapabilitiesUndefined (107) if the total number of event types
sl@0
   317
		don't match the total number of the event capability sets.
sl@0
   318
*/	
sl@0
   319
void TSecurityInfoReader::ReadL()
sl@0
   320
	{
sl@0
   321
	TInt eventTypeCount = GetEventTypeCountL();
sl@0
   322
	
sl@0
   323
	TResourceReader reader;
sl@0
   324
	iResourceInterface.CreateResourceReaderLC(reader, R_LOG_SECURITY);
sl@0
   325
	
sl@0
   326
	TInt securityNodeCount = reader.ReadInt16();
sl@0
   327
sl@0
   328
	// For all built-in event types there _MUST_ be a corresponding set of
sl@0
   329
	// capabilities defined in logwrap.rss.
sl@0
   330
	__ASSERT_ALWAYS(eventTypeCount == securityNodeCount, Panic(ELogSecurityCapabilitiesUndefined));
sl@0
   331
		
sl@0
   332
	iLogServSecurity.iPolicyCon.ReserveL(eventTypeCount);
sl@0
   333
	for(TInt i=0;i<eventTypeCount;++i)
sl@0
   334
		{
sl@0
   335
		TUid eventType = {reader.ReadUint32()};
sl@0
   336
		
sl@0
   337
		TCaps readCaps;
sl@0
   338
		GetCapabilities(reader, readCaps);
sl@0
   339
		
sl@0
   340
		TCaps writeCaps;
sl@0
   341
		GetCapabilities(reader, writeCaps);
sl@0
   342
		
sl@0
   343
	    TInt err = iLogServSecurity.iPolicyCon.Append(TEventPolicy(eventType, readCaps, writeCaps));
sl@0
   344
        __ASSERT_ALWAYS(err == KErrNone, Panic(ELogArrayReserved));
sl@0
   345
		}
sl@0
   346
	
sl@0
   347
	CleanupStack::PopAndDestroy(); // the resource reader
sl@0
   348
	}
sl@0
   349
sl@0
   350
/**
sl@0
   351
The method returns the number of built-in event types defined for the LogEngServer 
sl@0
   352
in logwrap.rss - see section entitled 'r_log_initial_events'.
sl@0
   353
@return An integer number representing the number of the event types found in the 
sl@0
   354
		resource file.
sl@0
   355
@leave System-wide error codes, including KErrNoMemory and reading file errors.
sl@0
   356
*/
sl@0
   357
TInt TSecurityInfoReader::GetEventTypeCountL()
sl@0
   358
	{
sl@0
   359
	TResourceReader reader;
sl@0
   360
	iResourceInterface.CreateResourceReaderLC(reader, R_LOG_INITIAL_EVENTS);
sl@0
   361
	TInt count = reader.ReadInt16();
sl@0
   362
	CleanupStack::PopAndDestroy();
sl@0
   363
	return count;
sl@0
   364
	}
sl@0
   365
sl@0
   366
/**
sl@0
   367
The method reads the capabilities for the currently processed event.
sl@0
   368
@param aReader TResourceReader object used for reading the related resource file entries.
sl@0
   369
@param aCaps An output parameter, reference to the array where the capabilities will be
sl@0
   370
			 stored.
sl@0
   371
@panic ELogTooManyCapabilities (108) if the number of the capabilities in the resource
sl@0
   372
		file exceeds the max allowed number, which is currently set to KMaxCapsPerOp (7).
sl@0
   373
@panic ELogUnknownCapability (109) if the found capability is of unknown type.
sl@0
   374
*/
sl@0
   375
void TSecurityInfoReader::GetCapabilities(TResourceReader& aReader, TCaps& aCaps)
sl@0
   376
	{
sl@0
   377
	TInt capsCount = aReader.ReadInt16();
sl@0
   378
	__ASSERT_ALWAYS((TUint)capsCount <= aCaps.MaxSize(), Panic(ELogTooManyCapabilities));
sl@0
   379
			
sl@0
   380
	for(TInt i=0;i<capsCount;++i)
sl@0
   381
		{
sl@0
   382
		TInt n = aReader.ReadInt32();
sl@0
   383
		__ASSERT_ALWAYS(n >= ECapability_None && n < ECapability_Limit, Panic(ELogUnknownCapability));// its not in e32capability.h !
sl@0
   384
		aCaps[i] = TCapability(n);
sl@0
   385
		}
sl@0
   386
	}