os/security/authorisation/userpromptservice/policies/test/dumppolicy/source/dumppolicy.cpp
First public contribution.
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.
26 #include <ups/policy.h>
27 #include "../../../source/policyreader.h"
29 _LIT(KAppName, "dumppolicy");
31 using namespace UserPromptService;
34 Class that prints text to the console and optionally a log file.
36 class CPrinter : public CBase
39 static CPrinter* NewLC(CConsoleBase* aConsole);
41 static CPrinter* NewLC(CConsoleBase* aConsole, RFile& aFile);
42 void PrintL(TRefByValue<const TDesC16> aFormat, ...);
46 CPrinter(CConsoleBase* aConsole);
48 /** Console object to print text to */
51 /* Optional file handle to write text to */
54 /* Whether to log the output to the file */
57 /** Temporary buffer */
62 CPrinter* CPrinter::NewLC(CConsoleBase* aConsole, RFile& aFile)
64 Creates a new printer object and places the pointer on the cleanup stack.
65 @param aConsole The console object to print text to.
66 @param aFile A handle to a file to write the text to. The handle is duplicated internally.
67 @return A pointer to the new printer object.
70 CPrinter* self = CPrinter::NewLC(aConsole);
71 User::LeaveIfError(self->iFile.Duplicate(aFile));
72 self->iLogToFile = ETrue;
76 CPrinter* CPrinter::NewLC(CConsoleBase* aConsole)
78 Creates a new printer object and places the pointer on the cleanup stack.
79 @param aConsole The console object to print text to.
80 @return A pointer to the new printer object.
83 CPrinter* self = new(ELeave) CPrinter(aConsole);
84 CleanupStack::PushL(self);
88 void CPrinter::PrintL(TRefByValue<const TDesC16> aFormat, ...)
91 VA_START(list, aFormat);
94 iBuffer.AppendFormatList(aFormat, list);
96 iCon->Printf(iBuffer);
99 HBufC8* utf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(iBuffer);
100 CleanupStack::PushL(utf8);
101 User::LeaveIfError(iFile.Write(*utf8));
102 CleanupStack::PopAndDestroy(utf8);
108 CPrinter::CPrinter(CConsoleBase* aConsole) : iCon(aConsole)
115 CPrinter::~CPrinter()
123 void PrintSystemServerSecurity(CPrinter* aPrinter, CPolicy *aPolicy)
125 Prints the system server security configuration.
130 switch (aPolicy->SystemServerSecurity())
132 case CPolicy::ESystemServerSecurityPassedOrFailed:
133 p.Set(_L("ESystemServerSecurityPassedOrFailed"));
135 case CPolicy::ESystemServerSecurityPassed:
136 p.Set(_L("ESystemServerSecurityPassed"));
138 case CPolicy::ESystemServerSecurityFailed:
139 p.Set(_L("ESystemServerSecurityFailed"));
142 p.Set(_L("*** UNKNOWN ***"));
146 buf.AppendFormat(_L(" System Server Security: %S\n"), &p);
147 aPrinter->PrintL(buf);
151 Prints a object to the supplied printer object.
152 @param aPrinter The printer.
153 @param aPolicy The policy to print.
155 void PrintPolicy(CPrinter* aPrinter, CPolicy *aPolicy)
157 _LIT16(KYes, "EYes");
159 _LIT16(KSessionYes, "ESessionYes");
160 _LIT16(KSessionNo, "ESessionNo");
161 _LIT16(KAlways, "EAlways");
162 _LIT16(KNever, "ENever");
166 TInt sidClasses = aPolicy->SidClasses().iSidClasses;
167 aPrinter->PrintL(_L(" SID Classes: 0x%04x\n"), sidClasses);
170 const RArray<TSecureId>& sidList = aPolicy->SidList();
171 aPrinter->PrintL(_L(" SID List:"));
173 TInt sidCount = sidList.Count();
174 for (TInt i = 0; i < sidCount; ++i)
176 aPrinter->PrintL(_L(" 0x%08x"), sidList[i].iId);
178 aPrinter->PrintL(_L("\n"));
180 PrintSystemServerSecurity(aPrinter, aPolicy);
182 tmp.Copy(aPolicy->Destination());
183 aPrinter->PrintL(_L(" Destination: %S\n"), &tmp);
185 TInt options = aPolicy->Options();
187 if (options & CPolicy::EYes)
192 if (options & CPolicy::ENo)
197 if (options & CPolicy::ESessionYes)
199 tmp.Append(KSessionYes);
202 if (options & CPolicy::EAlways)
207 if (options & CPolicy::ENever)
212 if (options & CPolicy::ESessionNo)
214 tmp.Append(KSessionNo);
218 aPrinter->PrintL(_L(" Options: %S\n"), &tmp);
219 aPrinter->PrintL(_L(" Policy Evaluator: 0x%08x\n"), aPolicy->PolicyEvaluator());
220 aPrinter->PrintL(_L(" Dialog Creator: 0x%08x\n"), aPolicy->DialogCreator());
221 aPrinter->PrintL(_L("\n"));
224 static void PrintAuthPolicyL(CPrinter* aPrinter, TAuthorisationPolicy aAuthPolicy)
226 Prints the authorisation policy.
227 @param aPrinter The printer object.
228 @param aAuthPolicy The authorisation policy.
236 authPol.Set(_L("EAlwaysCheck"));
238 case ECheckPostManufacture:
239 authPol.Set(_L("ECheckPostManufacture"));
241 case ECheckUnprotectedSids:
242 authPol.Set(_L("ECheckUnprotectedSids"));
245 authPol.Set(_L("ECheckIfFailed"));
248 authPol.Set(_L("ENeverCheck"));
251 authPol.Set(_L("*** UNKNOWN ***"));
254 buf.AppendFormat(_L(" Authorisation Policy: %S\n"), &authPol);
255 aPrinter->PrintL(buf);
258 static void PrintPoliciesL(CPrinter* aPrinter, CPolicyReader* aReader)
260 Prints all of the policies returned by a CPolicyReader object.
261 @param aPrinter The printer object.
262 @param aReader The policy reader.
265 TPolicyHeader hdr = aReader->Header();
266 aPrinter->PrintL(_L("*** Policy Header ***\n"));
267 aPrinter->PrintL(_L(" Policy Format: %d\n"), hdr.iFormatVersion);
268 aPrinter->PrintL(_L(" Major Version: %d\n"), hdr.iMajorVersion);
269 aPrinter->PrintL(_L(" Minor Version: %d\n"), hdr.iMajorVersion);
270 aPrinter->PrintL(_L(" Default Policy Evaluator: 0x%08x\n"), hdr.iDefaultPolicyEvaluator);
271 aPrinter->PrintL(_L(" Default Dialog Creator: 0x%08x\n"), hdr.iDefaultDialogCreator);
272 PrintAuthPolicyL(aPrinter, hdr.iAuthPolicy);
273 aPrinter->PrintL(_L("\n"));
277 while ((p = aReader->NextPolicyL()) != 0)
280 buf.AppendFormat(_L("*** Policy %d ***\n"), i);
281 aPrinter->PrintL(buf);
283 PrintPolicy(aPrinter, p);
289 static void MainL(void)
291 Takes a User Prompt Service policy resource file and dumps it as human readable text to the
292 console. The user may also specify the name of an output file on the command line. If so, text
293 is also written to this file.
297 User::LeaveIfError(fs.Connect());
298 CleanupClosePushL(fs);
300 CConsoleBase* console = Console::NewL(KAppName, TSize(KDefaultConsWidth, KDefaultConsHeight));
301 CleanupStack::PushL(console);
303 CCommandLineArguments* args = CCommandLineArguments::NewLC();
305 if (args->Count() > 1)
307 CPolicyReader* reader = CPolicyReader::NewLC(fs, args->Arg(1));
308 CPrinter* printer(0);
309 if (args->Count() > 2)
312 User::LeaveIfError(outFile.Replace(fs, args->Arg(2), EFileShareExclusive | EFileWrite));
313 CleanupClosePushL(outFile);
314 printer = CPrinter::NewLC(console, outFile);
316 CleanupStack::Pop(printer);
317 CleanupStack::PopAndDestroy(&outFile);
318 CleanupStack::PushL(printer);
322 printer = CPrinter::NewLC(console);
325 PrintPoliciesL(printer, reader);
328 if (args->Count() < 3)
330 // If no output file is specified then pause after finishing
331 // because the console will vanish when it is closed.
332 console->Printf(_L("Press any key to continue\r\n"));
335 CleanupStack::PopAndDestroy(2, reader); // printer, reader
339 console->Printf(_L("Usage: dumppolicy.exe policy.rsc <output.txt>\r\n"));
340 console->Printf(_L("Press any key to continue\r\n"));
344 CleanupStack::PopAndDestroy(3, &fs); // args, console, fs
347 GLDEF_C TInt E32Main()
349 Creats clean up stack and invokes real main function.
352 CTrapCleanup* cleanup = CTrapCleanup::New();