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