1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/loggingservices/eventlogger/LogServ/src/logservsecurity.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,386 @@
1.4 +// Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "logservsecurity.h"
1.20 +#include <e32capability.h>
1.21 +#include "LogServResourceInterpreter.h"
1.22 +#include "LogCliServShared.h"
1.23 +#include "logservpanic.h"
1.24 +
1.25 +/**
1.26 +The max number of TCapability(s) that can be used to instantiate a TSecurityPolicy
1.27 +@internalComponent
1.28 +*/
1.29 +const TInt KMaxCapsPerOp = 7;
1.30 +
1.31 +///////////////////////////////////////////////////////////////////////////////
1.32 +// TCaps class - declaration and implementation
1.33 +
1.34 +/**
1.35 +The class represents a static array of TCapability items (it can't grow or shrink).
1.36 +The class should be used every time when there is a need of a static TCapability array.
1.37 +It offers an overloaded "[]" operator with run-time bounds checks.
1.38 +@internalComponent
1.39 +*/
1.40 +class TCaps
1.41 + {
1.42 +public:
1.43 + TCaps();
1.44 + inline TInt MaxSize() const
1.45 + {
1.46 + return KMaxCapsPerOp;
1.47 + }
1.48 + inline TCapability& operator [](TInt aIndex)
1.49 + {
1.50 + __ASSERT_DEBUG(aIndex >= 0 && aIndex < MaxSize(), User::Invariant());
1.51 + return iItems[aIndex];
1.52 + }
1.53 +
1.54 + inline const TCapability& operator [](TInt aIndex) const
1.55 + {
1.56 + __ASSERT_DEBUG(aIndex >= 0 && aIndex < MaxSize(), User::Invariant());
1.57 + return iItems[aIndex];
1.58 + }
1.59 +
1.60 +private:
1.61 + TCapability iItems[KMaxCapsPerOp];
1.62 +
1.63 + };
1.64 +
1.65 +/**
1.66 +The controlled capabilities are initialized with ECapability_None value.
1.67 +*/
1.68 +TCaps::TCaps()
1.69 + {
1.70 + for(TInt i=0;i<MaxSize();++i)
1.71 + {
1.72 + iItems[i] = ECapability_None;
1.73 + }
1.74 + }
1.75 +
1.76 +///////////////////////////////////////////////////////////////////////////////
1.77 +// TEventPolicy structure - declaration and implementation
1.78 +
1.79 +/**
1.80 +Each LogEngServer event defined in Logwrap.rss has two associated
1.81 +TSecurityPolicy(s) - one each for read/write operations. This structure
1.82 +contains one read and one write policy per defined LogEngServer event.
1.83 +@internalComponent
1.84 +*/
1.85 +struct TEventPolicy
1.86 + {
1.87 + TEventPolicy(TUid aEventType, const TCaps& aReadCaps, const TCaps& aWriteCaps);
1.88 +
1.89 + TUid iEventType;// event type defined in LOGWRAP.RSS
1.90 + TSecurityPolicy iReadPolicy;
1.91 + TSecurityPolicy iWritePolicy;
1.92 + };
1.93 +
1.94 +/**
1.95 +@param aEventType Event type. It could be one of the following: KLogCallEventType,
1.96 + KLogDataEventType, KLogFaxEventType, KLogShortMessageEventType,
1.97 + KLogMailEventType, KLogTaskSchedulerEventType, KLogPacketDataEventType.
1.98 + See LOGWRAP.RSS file where these constants are defined.
1.99 +@param aReadCaps Read capablities for aEventType argument. The client, who wants to use
1.100 + that event type ("read" operations), must satisfy aRead set of capabilities.
1.101 +@param aWriteCaps Write capablities for aEventType argument. The client, who wants to use
1.102 + that event type ("write" operations), must satisfy aWrite set of capabilities.
1.103 +*/
1.104 +TEventPolicy::TEventPolicy(TUid aEventType, const TCaps& aReadCaps, const TCaps& aWriteCaps) :
1.105 + iEventType(aEventType),
1.106 + iReadPolicy(aReadCaps[0],aReadCaps[1],aReadCaps[2],aReadCaps[3],aReadCaps[4],aReadCaps[5],aReadCaps[6]),
1.107 + iWritePolicy(aWriteCaps[0],aWriteCaps[1],aWriteCaps[2],aWriteCaps[3],aWriteCaps[4],aWriteCaps[5],aWriteCaps[6])
1.108 + {
1.109 + }
1.110 +
1.111 +///////////////////////////////////////////////////////////////////////////////
1.112 +// TSecurityInfoReader class declaration
1.113 +
1.114 +//Forward declaration
1.115 +class CLogServSecurityImpl;
1.116 +
1.117 +/**
1.118 +The class manages the reading of the Security policy data from Logwrap.rss and storing
1.119 +it in the supplied as an argument CLogServSecurityImpl object.
1.120 +@internalComponent
1.121 +*/
1.122 +class TSecurityInfoReader
1.123 + {
1.124 +public:
1.125 + TSecurityInfoReader(CLogServResourceInterpreter& aResourceInterface,
1.126 + CLogServSecurityImpl& aLogServSecurity);
1.127 + void ReadL();
1.128 +
1.129 +private:
1.130 + TInt GetEventTypeCountL();
1.131 + void GetCapabilities(TResourceReader& aReader, TCaps& aCaps);
1.132 +
1.133 +private:
1.134 + CLogServResourceInterpreter& iResourceInterface;
1.135 + CLogServSecurityImpl& iLogServSecurity;
1.136 +
1.137 + };
1.138 +
1.139 +///////////////////////////////////////////////////////////////////////////////
1.140 +// CLogServSecurityImpl class declaration and implementation.
1.141 +
1.142 +/**
1.143 +The class implements pure virtual methods in CLogServSecurity class.
1.144 +All functionality, related to processing the data in LogEngServer resource file,
1.145 +is delegated to an instance of TSecurityInfoReader class.
1.146 +@internalComponent
1.147 +*/
1.148 +class CLogServSecurityImpl : public CLogServSecurity
1.149 + {
1.150 + friend class TSecurityInfoReader;
1.151 +
1.152 +public:
1.153 + static CLogServSecurityImpl* NewL(CLogServResourceInterpreter& aResourceInterface);
1.154 + virtual ~CLogServSecurityImpl();
1.155 + virtual TBool IsAllowed(const RMessage2& aMsg, TUid aEventType, TEventOp aEventOp, const char* aDiagnostic);
1.156 +#ifdef LOGSERV_CAPABILITY_TEST
1.157 + virtual TSecurityPolicy SecurityPolicy(TUid aEventType, TEventOp aEventOp);
1.158 +#endif //LOGSERV_CAPABILITY_TEST
1.159 +
1.160 +private:
1.161 + CLogServSecurityImpl();
1.162 + void ConstructL(CLogServResourceInterpreter& aResourceInterface);
1.163 + const TSecurityPolicy& FindPolicy(TUid aEventType, TEventOp aEventOp) const;
1.164 +
1.165 +private:
1.166 + RArray<TEventPolicy> iPolicyCon;
1.167 + TSecurityPolicy iPassAllPolicy;
1.168 +
1.169 + };
1.170 +
1.171 +/**
1.172 +Standard, phase-one factory method for creation of objects of CLogServSecurityImpl type.
1.173 +@param aResourceInterface A reference to CLogServResourceInterpreter object used for reading
1.174 + the LogEngServer resource file (logwrap.rss). It is used only durring
1.175 + the construction phase of CLogServSecurityImpl instance.
1.176 +@return A pointer to the created CLogServSecurityImpl instance.
1.177 +@leave System-wide error codes, including KErrNoMemory and reading file errors.
1.178 +*/
1.179 +CLogServSecurityImpl* CLogServSecurityImpl::NewL(CLogServResourceInterpreter& aResourceInterface)
1.180 + {
1.181 + CLogServSecurityImpl* self = new (ELeave) CLogServSecurityImpl;
1.182 + CleanupStack::PushL(self);
1.183 + self->ConstructL(aResourceInterface);
1.184 + CleanupStack::Pop(self);
1.185 + return self;
1.186 + }
1.187 +
1.188 +/**
1.189 +*/
1.190 +CLogServSecurityImpl::~CLogServSecurityImpl()
1.191 + {
1.192 + iPolicyCon.Close();
1.193 + }
1.194 +
1.195 +/**
1.196 +The method compares the caller's capabilities against the set of capabilities,
1.197 +required for that kind of operation (read or write) and returns ETrue or EFalse.
1.198 +@param aMsg The message, containing the caller capabilities which have to be checked.
1.199 +@param aEventType Event type. For more details see LOGWRAP.RSS file where the
1.200 + UID constants are defined.
1.201 +@param aEventOp The type of the operation which is about to be performed by the
1.202 + caller. It could be EReadOp or EWriteOp.
1.203 +@return ETrue - the caller is allowed to execute the operation, EFalse - the caller's
1.204 + capabilities do not match the required set of capabilities for that
1.205 + kind of operation (read or write).
1.206 +Note: Only built-in types (included in logwrap.rss) are policed.
1.207 + So, return ETrue if TUid argument isn't a built-in type.
1.208 +*/
1.209 +TBool CLogServSecurityImpl::IsAllowed(const RMessage2& aMsg, TUid aEventType, TEventOp aEventOp, const char* aDiagnostic)
1.210 + {
1.211 + const TSecurityPolicy& policy = FindPolicy(aEventType, aEventOp);
1.212 + return policy.CheckPolicy(aMsg, aDiagnostic);
1.213 + }
1.214 +
1.215 +#ifdef LOGSERV_CAPABILITY_TEST
1.216 +/**
1.217 +This method is declared and implemented only if "LOGSERV_CAPABILITY_TEST" macro is defined
1.218 +@param aEventType Event type. For more details see LOGWRAP.RSS file where the
1.219 + UID constants are defined.
1.220 +@param aEventOp The type of the event operation: EReadOp or EWriteOp.
1.221 +@return The related with {aEventType, aEventOp} pair TSecurityPOlicy object.
1.222 +*/
1.223 +TSecurityPolicy CLogServSecurityImpl::SecurityPolicy(TUid aEventType, TEventOp aEventOp)
1.224 + {
1.225 + const TSecurityPolicy& policy = FindPolicy(aEventType, aEventOp);
1.226 + return policy;
1.227 + }
1.228 +#endif //LOGSERV_CAPABILITY_TEST
1.229 +
1.230 +/**
1.231 +*/
1.232 +CLogServSecurityImpl::CLogServSecurityImpl() :
1.233 + iPassAllPolicy(TSecurityPolicy::EAlwaysPass)
1.234 + {
1.235 + }
1.236 +
1.237 +/**
1.238 +Standard, phase-two construction method for creation of CLogServSecurityImpl objects.
1.239 +@param aResourceInterface A reference to CLogServResourceInterpreter object used for reading
1.240 + the LogEngServer resource file (logwrap.rss). It is used only durring
1.241 + the construction phase of CLogServSecurityImpl instance.
1.242 +@leave System-wide error codes, including KErrNoMemory and reading file errors.
1.243 +*/
1.244 +void CLogServSecurityImpl::ConstructL(CLogServResourceInterpreter& aResourceInterface)
1.245 + {
1.246 + TSecurityInfoReader reader(aResourceInterface, *this);
1.247 + reader.ReadL();
1.248 + }
1.249 +
1.250 +/**
1.251 +The method performs a search for the related to {aEventType, aOperationType} pair
1.252 +TSecurityPolicy object. If there is no registered TSecurityPolicy object for the
1.253 +supplied pair of arguments (which is possible, if aEventType argument is not a
1.254 +built-in type, specified in LOGWRAP.RSS file), then the method returns a reference
1.255 +to pass-all TSecurityPolicy object.
1.256 +@param aEventType Event type. For more details see LOGWRAP.RSS file where the
1.257 + UID constants are defined.
1.258 +@param aAccessType The type of the operation which is about to be performed by the
1.259 + caller. It could be ERead or EWrite.
1.260 +@return A const reference to TSecurityPolicy object, which defines a set of capabilities,
1.261 + required for that kind of operation (read or write).
1.262 +*/
1.263 +const TSecurityPolicy& CLogServSecurityImpl::FindPolicy(TUid aEventType, TEventOp aEventOp) const
1.264 + {
1.265 + for(TInt i=iPolicyCon.Count()-1;i>=0;--i)
1.266 + {
1.267 + const TEventPolicy& eventPolicy = iPolicyCon[i];
1.268 + if(eventPolicy.iEventType == aEventType)
1.269 + {
1.270 + return aEventOp == EWriteOp ? eventPolicy.iWritePolicy : eventPolicy.iReadPolicy;
1.271 + }
1.272 + }
1.273 + // aEventType wasn't found - it doesn't represent a policed event type.
1.274 + return iPassAllPolicy;
1.275 + }
1.276 +
1.277 +///////////////////////////////////////////////////////////////////////////////
1.278 +// CLogServSecurity implementation
1.279 +
1.280 +/**
1.281 +Standard, phase-one factory method for creation of objects of CLogServSecurity type.
1.282 +@param aResourceInterface A reference to CLogServResourceInterpreter object used for reading
1.283 + the LogEngServer resource file (logwrap.rss).
1.284 +@return A pointer to the created CLogServSecurity instance.
1.285 +@leave System-wide error codes, including KErrNoMemory and reading file errors.
1.286 +*/
1.287 +CLogServSecurity* CLogServSecurity::NewL(CLogServResourceInterpreter& aResourceInterface)
1.288 + {
1.289 + return CLogServSecurityImpl::NewL(aResourceInterface);
1.290 + }
1.291 +
1.292 +/**
1.293 +*/
1.294 +CLogServSecurity::~CLogServSecurity()
1.295 + {
1.296 + }
1.297 +
1.298 +///////////////////////////////////////////////////////////////////////////////
1.299 +// TSecurityInfoReader class implementation
1.300 +
1.301 +/**
1.302 +@param aResourceInterface A reference to CLogServResourceInterpreter object used for reading
1.303 + the LogEngServer resource file (logwrap.rss).
1.304 +@param aLogServSecurity A reference to CLogServSecurityImpl instance, which internal content
1.305 + will be initialized with the related information from the
1.306 + LogEngServer resource file.
1.307 +*/
1.308 +TSecurityInfoReader::TSecurityInfoReader(CLogServResourceInterpreter& aResourceInterface,
1.309 + CLogServSecurityImpl& aLogServSecurity) :
1.310 + iResourceInterface(aResourceInterface),
1.311 + iLogServSecurity(aLogServSecurity)
1.312 + {
1.313 + }
1.314 +
1.315 +/**
1.316 +The method reads the LogEngServer events capabilities from the resource file and
1.317 +initializes with them iLogServSecurity data member;
1.318 +@leave System-wide error codes, including KErrNoMemory and reading file errors.
1.319 +@panic ELogSecurityCapabilitiesUndefined (107) if the total number of event types
1.320 + don't match the total number of the event capability sets.
1.321 +*/
1.322 +void TSecurityInfoReader::ReadL()
1.323 + {
1.324 + TInt eventTypeCount = GetEventTypeCountL();
1.325 +
1.326 + TResourceReader reader;
1.327 + iResourceInterface.CreateResourceReaderLC(reader, R_LOG_SECURITY);
1.328 +
1.329 + TInt securityNodeCount = reader.ReadInt16();
1.330 +
1.331 + // For all built-in event types there _MUST_ be a corresponding set of
1.332 + // capabilities defined in logwrap.rss.
1.333 + __ASSERT_ALWAYS(eventTypeCount == securityNodeCount, Panic(ELogSecurityCapabilitiesUndefined));
1.334 +
1.335 + iLogServSecurity.iPolicyCon.ReserveL(eventTypeCount);
1.336 + for(TInt i=0;i<eventTypeCount;++i)
1.337 + {
1.338 + TUid eventType = {reader.ReadUint32()};
1.339 +
1.340 + TCaps readCaps;
1.341 + GetCapabilities(reader, readCaps);
1.342 +
1.343 + TCaps writeCaps;
1.344 + GetCapabilities(reader, writeCaps);
1.345 +
1.346 + TInt err = iLogServSecurity.iPolicyCon.Append(TEventPolicy(eventType, readCaps, writeCaps));
1.347 + __ASSERT_ALWAYS(err == KErrNone, Panic(ELogArrayReserved));
1.348 + }
1.349 +
1.350 + CleanupStack::PopAndDestroy(); // the resource reader
1.351 + }
1.352 +
1.353 +/**
1.354 +The method returns the number of built-in event types defined for the LogEngServer
1.355 +in logwrap.rss - see section entitled 'r_log_initial_events'.
1.356 +@return An integer number representing the number of the event types found in the
1.357 + resource file.
1.358 +@leave System-wide error codes, including KErrNoMemory and reading file errors.
1.359 +*/
1.360 +TInt TSecurityInfoReader::GetEventTypeCountL()
1.361 + {
1.362 + TResourceReader reader;
1.363 + iResourceInterface.CreateResourceReaderLC(reader, R_LOG_INITIAL_EVENTS);
1.364 + TInt count = reader.ReadInt16();
1.365 + CleanupStack::PopAndDestroy();
1.366 + return count;
1.367 + }
1.368 +
1.369 +/**
1.370 +The method reads the capabilities for the currently processed event.
1.371 +@param aReader TResourceReader object used for reading the related resource file entries.
1.372 +@param aCaps An output parameter, reference to the array where the capabilities will be
1.373 + stored.
1.374 +@panic ELogTooManyCapabilities (108) if the number of the capabilities in the resource
1.375 + file exceeds the max allowed number, which is currently set to KMaxCapsPerOp (7).
1.376 +@panic ELogUnknownCapability (109) if the found capability is of unknown type.
1.377 +*/
1.378 +void TSecurityInfoReader::GetCapabilities(TResourceReader& aReader, TCaps& aCaps)
1.379 + {
1.380 + TInt capsCount = aReader.ReadInt16();
1.381 + __ASSERT_ALWAYS((TUint)capsCount <= aCaps.MaxSize(), Panic(ELogTooManyCapabilities));
1.382 +
1.383 + for(TInt i=0;i<capsCount;++i)
1.384 + {
1.385 + TInt n = aReader.ReadInt32();
1.386 + __ASSERT_ALWAYS(n >= ECapability_None && n < ECapability_Limit, Panic(ELogUnknownCapability));// its not in e32capability.h !
1.387 + aCaps[i] = TCapability(n);
1.388 + }
1.389 + }