os/security/authorisation/userpromptservice/examples/integration/tmsgserver/tmsgserver.cpp
Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
20 #include "tmsgcommon.h"
21 #include "tmsgserver.h"
25 inline CShutdown::CShutdown() :CTimer(-1) {CActiveScheduler::Add(this);}
26 inline void CShutdown::ConstructL() {CTimer::ConstructL();}
27 inline void CShutdown::Start() {After(KMyShutdownDelay);}
29 void CShutdown::RunL()
31 CActiveScheduler::Stop();
35 // CMsgServer ****************************************************************
36 inline UserPromptService::RUpsSession& CMsgServer::Ups() {return iUps;}
38 const TInt CMsgServer::iRanges[KPolicyRanges] =
40 ESendMsg, // network services requried
41 ECancelSendMsg, // no capability needed to request
45 const TUint8 CMsgServer::iElementsIndex[KPolicyRanges] =
47 0, // capability check for networkservices
48 EAlwaysPass, // no check needed to cancel a request
49 CPolicyServer::ENotSupported
52 static const TInt KMsgServerCustomFailureAction = -1;
54 const CPolicyServer::TPolicyElement CMsgServer::iPolicyElements[KPolicyElements] =
56 {_INIT_SECURITY_POLICY_C1(ECapabilityNetworkServices), KMsgServerCustomFailureAction}
59 const CPolicyServer::TPolicy CMsgServer::iPolicy =
61 CPolicyServer::EAlwaysPass, // so that any client can connect
69 CMsgServer::CMsgServer()
70 : CPolicyServer(EPriorityStandard, iPolicy, ESharableSessions)
74 CMsgServer::~CMsgServer()
79 CMsgServer* CMsgServer::NewLC()
81 CMsgServer* self = new(ELeave) CMsgServer();
82 CleanupStack::PushL(self);
87 void CMsgServer::ConstructL()
89 // Create the RUpsSession
90 // A sub-session is created for each client
91 User::LeaveIfError(iUps.Connect());
93 StartL(KMsgServerName);
94 iShutdown.ConstructL();
98 CPolicyServer::TCustomResult CMsgServer::CustomFailureActionL(const RMessage2& aMessage, TInt /*aAction*/, const TSecurityInfo& /*aMissing*/)
99 /** This function is only invoked for requests that allow UPS authorisation. The
100 * message may be rejected later if the user denies the request.
103 CSession2* s = aMessage.Session();
104 __ASSERT_ALWAYS(s != NULL, User::Invariant());
105 CMsgServerSession* session = static_cast<CMsgServerSession*>(s);
106 session->SetPlatsecResult(EFalse);
107 return CPolicyServer::EPass; // The request may be rejected later by the UPS
110 CSession2* CMsgServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
112 TVersion v(KMsgServerVersionMajor, KMsgServerVersionMinor, KMsgServerVersionBuild);
113 if (!User::QueryVersionSupported(v,aVersion))
115 User::Leave(KErrNotSupported);
117 return new(ELeave) CMsgServerSession();
120 void CMsgServer::AddSession()
126 void CMsgServer::DropSession()
128 if (--iSessionCount==0) iShutdown.Start();
131 void PanicClient(const RMessagePtr2& aMessage,TMsgServerPanic aPanic)
133 aMessage.Panic(KMsgServerName,aPanic);
136 // CMsgServerSession *********************************************************
137 inline CMsgServer& CMsgServerSession::Server()
138 {return *static_cast<CMsgServer*>(const_cast<CServer2*>(CSession2::Server()));}
140 CMsgServerSession::CMsgServerSession()
141 : iPlatsecResult(ETrue)
145 CMsgServerSession::~CMsgServerSession()
149 Server().DropSession();
152 void CMsgServerSession::CreateL()
154 Server().AddSession();
155 iProcessor = CMsgProcessor::NewL(iAuth);
158 void CMsgServerSession::ServiceL(const RMessage2& aMessage)
160 if (! iAuthInitialised)
163 aMessage.ClientL(client);
164 CleanupClosePushL(client);
165 User::LeaveIfError(iAuth.Initialise(Server().Ups(), client));
166 CleanupStack::PopAndDestroy(&client);
167 iAuthInitialised = ETrue;
170 iProcessor->ProcessL(aMessage, iPlatsecResult);
171 // Reset to 'passed' for next message because SetPlatsecResult is only ever called
172 // when a check fails.
173 iPlatsecResult = ETrue;
176 void CMsgServerSession::SetPlatsecResult(TBool aResult)
178 iPlatsecResult = aResult;
181 // CMsgProcessor *********************************************************((((
182 CMsgProcessor* CMsgProcessor::NewL(UserPromptService::RUpsSubsession& aAuth)
184 CMsgProcessor* self = new(ELeave) CMsgProcessor(aAuth);
185 CleanupStack::PushL(self);
187 CleanupStack::Pop(self);
191 CMsgProcessor::CMsgProcessor(UserPromptService::RUpsSubsession& aAuth)
192 : CActive(EPriorityStandard), iState(EIdle), iAuth(aAuth)
194 CActiveScheduler::Add(this);
197 void CMsgProcessor::ConstructL()
199 User::LeaveIfError(iTimer.CreateLocal());
202 CMsgProcessor::~CMsgProcessor()
209 TInt CMsgProcessor::RunError(TInt aError)
211 if (iState != EIdle) iMessage.Complete(aError);
217 void CMsgProcessor::Reset()
219 * Resets the processor for the next message.
227 void CMsgProcessor::RunL()
229 User::LeaveIfError(iStatus.Int());
233 // Check whether the request was authorised before proceeding
234 if (iDecision == EUpsDecSessionNo || iDecision == EUpsDecNo)
235 { // The user or UPS denied the client's request so fail with security error.
236 iMessage.Complete(KErrPermissionDenied);
240 { // Request authorised so carry on as normal
245 // Message send so complete request and return
246 iMessage.Complete(KErrNone);
249 default: // invalid state - should never happen
250 __ASSERT_ALWAYS(EFalse, User::Invariant());
255 void CMsgProcessor::DoCancel()
260 break; // nothing to do
262 iAuth.CancelPrompt();
267 default: // invalid state - should never happen
268 __ASSERT_ALWAYS(EFalse, User::Invariant());
270 if (iState != EIdle) iMessage.Complete(KErrCancel);
274 void CMsgProcessor::GetParamsL(const RMessage2& aMessage)
276 * Reads the parameters for sending a message. This data is also needed for the authorisation prompt
279 TInt l = aMessage.GetDesLength(0);
280 if (l <= 0) User::Leave(KErrArgument);
282 aMessage.ReadL(0, iMsgTo);
283 l = aMessage.GetDesLength(1);
284 if (l <= 0) User::Leave(KErrArgument);
286 aMessage.ReadL(1, iMsgBody);
289 void CMsgProcessor::AuthoriseL(const RMessage2& aMessage, TBool aPlatsecResult)
291 * Implementation of the new authorisation state
292 * @param aMessage The message handle containing the SendMsg parameters
293 * @param aPlatsecResult Whether the client has the correct capabilities etc
296 iState = EAuthorising;
297 iPlatsecResult = aPlatsecResult;
299 GetParamsL(iMessage); // message parameters are used to identify the request
300 iAuth.Authorise(iPlatsecResult, TUid::Uid(KServiceId), iMsgTo, iDecision, iStatus);
304 void CMsgProcessor::SendL()
306 * Simulatate the sending of a message by displaying the body
307 * to the screen and waiting for three seconds.
309 * No changes necessary to support user prompts.
313 HBufC* tmp = HBufC::NewMaxLC(iMsgTo.Length() + iMsgBody.Length() + 3);
314 tmp->Des().Format(_L("%S: %S"), &iMsgTo, &iMsgBody);
315 User::LeaveIfError(User::InfoPrint(*tmp));
316 CleanupStack::PopAndDestroy(tmp);
317 iTimer.After(iStatus, 3 * 1000 * 1000);
321 void CMsgProcessor::ProcessL(const RMessage2& aMessage, TBool aPlatsecResult)
323 switch (aMessage.Function())
327 { // The first state is now authorisation
328 AuthoriseL(aMessage, aPlatsecResult);
331 { // Concurrent requests not supported
332 aMessage.Complete(KErrInUse);
337 aMessage.Complete(KErrNone);
340 User::Leave(KErrNotSupported);
344 static void RunServerL()
346 // normal server startup code
347 User::LeaveIfError(RThread::RenameMe(KMsgServerName));
348 CActiveScheduler* s=new(ELeave) CActiveScheduler;
349 CleanupStack::PushL(s);
350 CActiveScheduler::Install(s);
352 RProcess::Rendezvous(KErrNone);
353 CActiveScheduler::Start();
354 CleanupStack::PopAndDestroy(2, s);
360 CTrapCleanup* cleanup=CTrapCleanup::New();
364 TRAP(r,RunServerL());