os/security/authorisation/userpromptservice/policies/test/dumppolicy/source/dumppolicy.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/authorisation/userpromptservice/policies/test/dumppolicy/source/dumppolicy.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,362 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <bacline.h>
1.23 +#include <e32cons.h>
1.24 +#include <e32debug.h>
1.25 +#include <e32std.h>
1.26 +#include <f32file.h>
1.27 +#include <utf.h>
1.28 +
1.29 +#include <ups/policy.h>
1.30 +#include "../../../source/policyreader.h"
1.31 +
1.32 +_LIT(KAppName, "dumppolicy");
1.33 +
1.34 +using namespace UserPromptService;
1.35 +
1.36 +/**
1.37 + Class that prints text to the console and optionally a log file.
1.38 + */
1.39 +class CPrinter : public CBase
1.40 + {
1.41 +public:
1.42 + static CPrinter* NewLC(CConsoleBase* aConsole);
1.43 +
1.44 + static CPrinter* NewLC(CConsoleBase* aConsole, RFile& aFile);
1.45 + void PrintL(TRefByValue<const TDesC16> aFormat, ...);
1.46 + ~CPrinter();
1.47 +
1.48 +private:
1.49 + CPrinter(CConsoleBase* aConsole);
1.50 +
1.51 + /** Console object to print text to */
1.52 + CConsoleBase* iCon;
1.53 +
1.54 + /* Optional file handle to write text to */
1.55 + RFile iFile;
1.56 +
1.57 + /* Whether to log the output to the file */
1.58 + TBool iLogToFile;
1.59 +
1.60 + /** Temporary buffer */
1.61 + TBuf<256> iBuffer;
1.62 + };
1.63 +
1.64 +
1.65 +CPrinter* CPrinter::NewLC(CConsoleBase* aConsole, RFile& aFile)
1.66 +/**
1.67 +Creates a new printer object and places the pointer on the cleanup stack.
1.68 +@param aConsole The console object to print text to.
1.69 +@param aFile A handle to a file to write the text to. The handle is duplicated internally.
1.70 +@return A pointer to the new printer object.
1.71 +*/
1.72 + {
1.73 + CPrinter* self = CPrinter::NewLC(aConsole);
1.74 + User::LeaveIfError(self->iFile.Duplicate(aFile));
1.75 + self->iLogToFile = ETrue;
1.76 + return self;
1.77 + }
1.78 +
1.79 +CPrinter* CPrinter::NewLC(CConsoleBase* aConsole)
1.80 +/**
1.81 +Creates a new printer object and places the pointer on the cleanup stack.
1.82 +@param aConsole The console object to print text to.
1.83 +@return A pointer to the new printer object.
1.84 +*/
1.85 + {
1.86 + CPrinter* self = new(ELeave) CPrinter(aConsole);
1.87 + CleanupStack::PushL(self);
1.88 + return self;
1.89 + }
1.90 +
1.91 +void CPrinter::PrintL(TRefByValue<const TDesC16> aFormat, ...)
1.92 + {
1.93 + VA_LIST list;
1.94 + VA_START(list, aFormat);
1.95 +
1.96 + iBuffer.Zero();
1.97 + iBuffer.AppendFormatList(aFormat, list);
1.98 +
1.99 + iCon->Printf(iBuffer);
1.100 + if (iLogToFile)
1.101 + {
1.102 + HBufC8* utf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(iBuffer);
1.103 + CleanupStack::PushL(utf8);
1.104 + User::LeaveIfError(iFile.Write(*utf8));
1.105 + CleanupStack::PopAndDestroy(utf8);
1.106 + }
1.107 +
1.108 + VA_END(list);
1.109 + }
1.110 +
1.111 +CPrinter::CPrinter(CConsoleBase* aConsole) : iCon(aConsole)
1.112 +/**
1.113 + Constructor
1.114 +*/
1.115 + {
1.116 + }
1.117 +
1.118 +CPrinter::~CPrinter()
1.119 +/**
1.120 + Destructor
1.121 +*/
1.122 + {
1.123 + iFile.Close();
1.124 + }
1.125 +
1.126 +void PrintSystemServerSecurity(CPrinter* aPrinter, CPolicy *aPolicy)
1.127 +/**
1.128 +Prints the system server security configuration.
1.129 +*/
1.130 + {
1.131 +
1.132 + TPtrC p;
1.133 + switch (aPolicy->SystemServerSecurity())
1.134 + {
1.135 + case CPolicy::ESystemServerSecurityPassedOrFailed:
1.136 + p.Set(_L("ESystemServerSecurityPassedOrFailed"));
1.137 + break;
1.138 + case CPolicy::ESystemServerSecurityPassed:
1.139 + p.Set(_L("ESystemServerSecurityPassed"));
1.140 + break;
1.141 + case CPolicy::ESystemServerSecurityFailed:
1.142 + p.Set(_L("ESystemServerSecurityFailed"));
1.143 + break;
1.144 + default:
1.145 + p.Set(_L("*** UNKNOWN ***"));
1.146 + break;
1.147 + }
1.148 + TBuf<80> buf;
1.149 + buf.AppendFormat(_L(" System Server Security: %S\n"), &p);
1.150 + aPrinter->PrintL(buf);
1.151 + }
1.152 +
1.153 +/**
1.154 + Prints a object to the supplied printer object.
1.155 + @param aPrinter The printer.
1.156 + @param aPolicy The policy to print.
1.157 + */
1.158 +void PrintPolicy(CPrinter* aPrinter, CPolicy *aPolicy)
1.159 + {
1.160 + _LIT16(KYes, "EYes");
1.161 + _LIT16(KNo, "ENo");
1.162 + _LIT16(KSessionYes, "ESessionYes");
1.163 + _LIT16(KSessionNo, "ESessionNo");
1.164 + _LIT16(KAlways, "EAlways");
1.165 + _LIT16(KNever, "ENever");
1.166 + _LIT16(KSpace, " ");
1.167 +
1.168 + TBuf<256> tmp;
1.169 + TInt sidClasses = aPolicy->SidClasses().iSidClasses;
1.170 + aPrinter->PrintL(_L(" SID Classes: 0x%04x\n"), sidClasses);
1.171 + tmp.Zero();
1.172 +
1.173 + const RArray<TSecureId>& sidList = aPolicy->SidList();
1.174 + aPrinter->PrintL(_L(" SID List:"));
1.175 +
1.176 + TInt sidCount = sidList.Count();
1.177 + for (TInt i = 0; i < sidCount; ++i)
1.178 + {
1.179 + aPrinter->PrintL(_L(" 0x%08x"), sidList[i].iId);
1.180 + }
1.181 + aPrinter->PrintL(_L("\n"));
1.182 +
1.183 + PrintSystemServerSecurity(aPrinter, aPolicy);
1.184 +
1.185 + tmp.Copy(aPolicy->Destination());
1.186 + aPrinter->PrintL(_L(" Destination: %S\n"), &tmp);
1.187 +
1.188 + TInt options = aPolicy->Options();
1.189 + tmp.Zero();
1.190 + if (options & CPolicy::EYes)
1.191 + {
1.192 + tmp.Append(KYes);
1.193 + tmp.Append(KSpace);
1.194 + }
1.195 + if (options & CPolicy::ENo)
1.196 + {
1.197 + tmp.Append(KNo);
1.198 + tmp.Append(KSpace);
1.199 + }
1.200 + if (options & CPolicy::ESessionYes)
1.201 + {
1.202 + tmp.Append(KSessionYes);
1.203 + tmp.Append(KSpace);
1.204 + }
1.205 + if (options & CPolicy::EAlways)
1.206 + {
1.207 + tmp.Append(KAlways);
1.208 + tmp.Append(KSpace);
1.209 + }
1.210 + if (options & CPolicy::ENever)
1.211 + {
1.212 + tmp.Append(KNever);
1.213 + tmp.Append(KSpace);
1.214 + }
1.215 + if (options & CPolicy::ESessionNo)
1.216 + {
1.217 + tmp.Append(KSessionNo);
1.218 + tmp.Append(KSpace);
1.219 + }
1.220 +
1.221 + aPrinter->PrintL(_L(" Options: %S\n"), &tmp);
1.222 + aPrinter->PrintL(_L(" Policy Evaluator: 0x%08x\n"), aPolicy->PolicyEvaluator());
1.223 + aPrinter->PrintL(_L(" Dialog Creator: 0x%08x\n"), aPolicy->DialogCreator());
1.224 + aPrinter->PrintL(_L("\n"));
1.225 + }
1.226 +
1.227 +static void PrintAuthPolicyL(CPrinter* aPrinter, TAuthorisationPolicy aAuthPolicy)
1.228 +/**
1.229 + Prints the authorisation policy.
1.230 + @param aPrinter The printer object.
1.231 + @param aAuthPolicy The authorisation policy.
1.232 + */
1.233 + {
1.234 + TBuf<80> buf;
1.235 + TPtrC authPol;
1.236 + switch (aAuthPolicy)
1.237 + {
1.238 + case EAlwaysCheck:
1.239 + authPol.Set(_L("EAlwaysCheck"));
1.240 + break;
1.241 + case ECheckPostManufacture:
1.242 + authPol.Set(_L("ECheckPostManufacture"));
1.243 + break;
1.244 + case ECheckUnprotectedSids:
1.245 + authPol.Set(_L("ECheckUnprotectedSids"));
1.246 + break;
1.247 + case ECheckIfFailed:
1.248 + authPol.Set(_L("ECheckIfFailed"));
1.249 + break;
1.250 + case ENeverCheck:
1.251 + authPol.Set(_L("ENeverCheck"));
1.252 + break;
1.253 + default:
1.254 + authPol.Set(_L("*** UNKNOWN ***"));
1.255 + break;
1.256 + }
1.257 + buf.AppendFormat(_L(" Authorisation Policy: %S\n"), &authPol);
1.258 + aPrinter->PrintL(buf);
1.259 + }
1.260 +
1.261 +static void PrintPoliciesL(CPrinter* aPrinter, CPolicyReader* aReader)
1.262 +/**
1.263 + Prints all of the policies returned by a CPolicyReader object.
1.264 + @param aPrinter The printer object.
1.265 + @param aReader The policy reader.
1.266 + */
1.267 + {
1.268 + TPolicyHeader hdr = aReader->Header();
1.269 + aPrinter->PrintL(_L("*** Policy Header ***\n"));
1.270 + aPrinter->PrintL(_L(" Policy Format: %d\n"), hdr.iFormatVersion);
1.271 + aPrinter->PrintL(_L(" Major Version: %d\n"), hdr.iMajorVersion);
1.272 + aPrinter->PrintL(_L(" Minor Version: %d\n"), hdr.iMajorVersion);
1.273 + aPrinter->PrintL(_L(" Default Policy Evaluator: 0x%08x\n"), hdr.iDefaultPolicyEvaluator);
1.274 + aPrinter->PrintL(_L(" Default Dialog Creator: 0x%08x\n"), hdr.iDefaultDialogCreator);
1.275 + PrintAuthPolicyL(aPrinter, hdr.iAuthPolicy);
1.276 + aPrinter->PrintL(_L("\n"));
1.277 +
1.278 + CPolicy* p;
1.279 + TInt i = 0;
1.280 + while ((p = aReader->NextPolicyL()) != 0)
1.281 + {
1.282 + TBuf<32> buf;
1.283 + buf.AppendFormat(_L("*** Policy %d ***\n"), i);
1.284 + aPrinter->PrintL(buf);
1.285 +
1.286 + PrintPolicy(aPrinter, p);
1.287 + delete p;
1.288 + ++i;
1.289 + }
1.290 + }
1.291 +
1.292 +static void MainL(void)
1.293 +/**
1.294 +Takes a User Prompt Service policy resource file and dumps it as human readable text to the
1.295 +console. The user may also specify the name of an output file on the command line. If so, text
1.296 +is also written to this file.
1.297 +*/
1.298 + {
1.299 + RFs fs;
1.300 + User::LeaveIfError(fs.Connect());
1.301 + CleanupClosePushL(fs);
1.302 +
1.303 + CConsoleBase* console = Console::NewL(KAppName, TSize(KDefaultConsWidth, KDefaultConsHeight));
1.304 + CleanupStack::PushL(console);
1.305 +
1.306 + CCommandLineArguments* args = CCommandLineArguments::NewLC();
1.307 +
1.308 + if (args->Count() > 1)
1.309 + {
1.310 + CPolicyReader* reader = CPolicyReader::NewLC(fs, args->Arg(1));
1.311 + CPrinter* printer(0);
1.312 + if (args->Count() > 2)
1.313 + {
1.314 + RFile outFile;
1.315 + User::LeaveIfError(outFile.Replace(fs, args->Arg(2), EFileShareExclusive | EFileWrite));
1.316 + CleanupClosePushL(outFile);
1.317 + printer = CPrinter::NewLC(console, outFile);
1.318 +
1.319 + CleanupStack::Pop(printer);
1.320 + CleanupStack::PopAndDestroy(&outFile);
1.321 + CleanupStack::PushL(printer);
1.322 + }
1.323 + else
1.324 + {
1.325 + printer = CPrinter::NewLC(console);
1.326 + }
1.327 + __UHEAP_MARK;
1.328 + PrintPoliciesL(printer, reader);
1.329 + __UHEAP_MARKEND;
1.330 +
1.331 + if (args->Count() < 3)
1.332 + {
1.333 + // If no output file is specified then pause after finishing
1.334 + // because the console will vanish when it is closed.
1.335 + console->Printf(_L("Press any key to continue\r\n"));
1.336 + console->Getch();
1.337 + }
1.338 + CleanupStack::PopAndDestroy(2, reader); // printer, reader
1.339 + }
1.340 + else
1.341 + {
1.342 + console->Printf(_L("Usage: dumppolicy.exe policy.rsc <output.txt>\r\n"));
1.343 + console->Printf(_L("Press any key to continue\r\n"));
1.344 + console->Getch();
1.345 + }
1.346 +
1.347 + CleanupStack::PopAndDestroy(3, &fs); // args, console, fs
1.348 + }
1.349 +
1.350 +GLDEF_C TInt E32Main()
1.351 +/**
1.352 +Creats clean up stack and invokes real main function.
1.353 +*/
1.354 + {
1.355 + CTrapCleanup* cleanup = CTrapCleanup::New();
1.356 + if(cleanup == NULL)
1.357 + {
1.358 + return KErrNoMemory;
1.359 + }
1.360 + __UHEAP_MARK;
1.361 + TRAPD(err, MainL());
1.362 + __UHEAP_MARKEND;
1.363 + delete cleanup;
1.364 + return err;
1.365 + }