sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include "../../../source/policyreader.h" sl@0: sl@0: _LIT(KAppName, "dumppolicy"); sl@0: sl@0: using namespace UserPromptService; sl@0: sl@0: /** sl@0: Class that prints text to the console and optionally a log file. sl@0: */ sl@0: class CPrinter : public CBase sl@0: { sl@0: public: sl@0: static CPrinter* NewLC(CConsoleBase* aConsole); sl@0: sl@0: static CPrinter* NewLC(CConsoleBase* aConsole, RFile& aFile); sl@0: void PrintL(TRefByValue aFormat, ...); sl@0: ~CPrinter(); sl@0: sl@0: private: sl@0: CPrinter(CConsoleBase* aConsole); sl@0: sl@0: /** Console object to print text to */ sl@0: CConsoleBase* iCon; sl@0: sl@0: /* Optional file handle to write text to */ sl@0: RFile iFile; sl@0: sl@0: /* Whether to log the output to the file */ sl@0: TBool iLogToFile; sl@0: sl@0: /** Temporary buffer */ sl@0: TBuf<256> iBuffer; sl@0: }; sl@0: sl@0: sl@0: CPrinter* CPrinter::NewLC(CConsoleBase* aConsole, RFile& aFile) sl@0: /** sl@0: Creates a new printer object and places the pointer on the cleanup stack. sl@0: @param aConsole The console object to print text to. sl@0: @param aFile A handle to a file to write the text to. The handle is duplicated internally. sl@0: @return A pointer to the new printer object. sl@0: */ sl@0: { sl@0: CPrinter* self = CPrinter::NewLC(aConsole); sl@0: User::LeaveIfError(self->iFile.Duplicate(aFile)); sl@0: self->iLogToFile = ETrue; sl@0: return self; sl@0: } sl@0: sl@0: CPrinter* CPrinter::NewLC(CConsoleBase* aConsole) sl@0: /** sl@0: Creates a new printer object and places the pointer on the cleanup stack. sl@0: @param aConsole The console object to print text to. sl@0: @return A pointer to the new printer object. sl@0: */ sl@0: { sl@0: CPrinter* self = new(ELeave) CPrinter(aConsole); sl@0: CleanupStack::PushL(self); sl@0: return self; sl@0: } sl@0: sl@0: void CPrinter::PrintL(TRefByValue aFormat, ...) sl@0: { sl@0: VA_LIST list; sl@0: VA_START(list, aFormat); sl@0: sl@0: iBuffer.Zero(); sl@0: iBuffer.AppendFormatList(aFormat, list); sl@0: sl@0: iCon->Printf(iBuffer); sl@0: if (iLogToFile) sl@0: { sl@0: HBufC8* utf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(iBuffer); sl@0: CleanupStack::PushL(utf8); sl@0: User::LeaveIfError(iFile.Write(*utf8)); sl@0: CleanupStack::PopAndDestroy(utf8); sl@0: } sl@0: sl@0: VA_END(list); sl@0: } sl@0: sl@0: CPrinter::CPrinter(CConsoleBase* aConsole) : iCon(aConsole) sl@0: /** sl@0: Constructor sl@0: */ sl@0: { sl@0: } sl@0: sl@0: CPrinter::~CPrinter() sl@0: /** sl@0: Destructor sl@0: */ sl@0: { sl@0: iFile.Close(); sl@0: } sl@0: sl@0: void PrintSystemServerSecurity(CPrinter* aPrinter, CPolicy *aPolicy) sl@0: /** sl@0: Prints the system server security configuration. sl@0: */ sl@0: { sl@0: sl@0: TPtrC p; sl@0: switch (aPolicy->SystemServerSecurity()) sl@0: { sl@0: case CPolicy::ESystemServerSecurityPassedOrFailed: sl@0: p.Set(_L("ESystemServerSecurityPassedOrFailed")); sl@0: break; sl@0: case CPolicy::ESystemServerSecurityPassed: sl@0: p.Set(_L("ESystemServerSecurityPassed")); sl@0: break; sl@0: case CPolicy::ESystemServerSecurityFailed: sl@0: p.Set(_L("ESystemServerSecurityFailed")); sl@0: break; sl@0: default: sl@0: p.Set(_L("*** UNKNOWN ***")); sl@0: break; sl@0: } sl@0: TBuf<80> buf; sl@0: buf.AppendFormat(_L(" System Server Security: %S\n"), &p); sl@0: aPrinter->PrintL(buf); sl@0: } sl@0: sl@0: /** sl@0: Prints a object to the supplied printer object. sl@0: @param aPrinter The printer. sl@0: @param aPolicy The policy to print. sl@0: */ sl@0: void PrintPolicy(CPrinter* aPrinter, CPolicy *aPolicy) sl@0: { sl@0: _LIT16(KYes, "EYes"); sl@0: _LIT16(KNo, "ENo"); sl@0: _LIT16(KSessionYes, "ESessionYes"); sl@0: _LIT16(KSessionNo, "ESessionNo"); sl@0: _LIT16(KAlways, "EAlways"); sl@0: _LIT16(KNever, "ENever"); sl@0: _LIT16(KSpace, " "); sl@0: sl@0: TBuf<256> tmp; sl@0: TInt sidClasses = aPolicy->SidClasses().iSidClasses; sl@0: aPrinter->PrintL(_L(" SID Classes: 0x%04x\n"), sidClasses); sl@0: tmp.Zero(); sl@0: sl@0: const RArray& sidList = aPolicy->SidList(); sl@0: aPrinter->PrintL(_L(" SID List:")); sl@0: sl@0: TInt sidCount = sidList.Count(); sl@0: for (TInt i = 0; i < sidCount; ++i) sl@0: { sl@0: aPrinter->PrintL(_L(" 0x%08x"), sidList[i].iId); sl@0: } sl@0: aPrinter->PrintL(_L("\n")); sl@0: sl@0: PrintSystemServerSecurity(aPrinter, aPolicy); sl@0: sl@0: tmp.Copy(aPolicy->Destination()); sl@0: aPrinter->PrintL(_L(" Destination: %S\n"), &tmp); sl@0: sl@0: TInt options = aPolicy->Options(); sl@0: tmp.Zero(); sl@0: if (options & CPolicy::EYes) sl@0: { sl@0: tmp.Append(KYes); sl@0: tmp.Append(KSpace); sl@0: } sl@0: if (options & CPolicy::ENo) sl@0: { sl@0: tmp.Append(KNo); sl@0: tmp.Append(KSpace); sl@0: } sl@0: if (options & CPolicy::ESessionYes) sl@0: { sl@0: tmp.Append(KSessionYes); sl@0: tmp.Append(KSpace); sl@0: } sl@0: if (options & CPolicy::EAlways) sl@0: { sl@0: tmp.Append(KAlways); sl@0: tmp.Append(KSpace); sl@0: } sl@0: if (options & CPolicy::ENever) sl@0: { sl@0: tmp.Append(KNever); sl@0: tmp.Append(KSpace); sl@0: } sl@0: if (options & CPolicy::ESessionNo) sl@0: { sl@0: tmp.Append(KSessionNo); sl@0: tmp.Append(KSpace); sl@0: } sl@0: sl@0: aPrinter->PrintL(_L(" Options: %S\n"), &tmp); sl@0: aPrinter->PrintL(_L(" Policy Evaluator: 0x%08x\n"), aPolicy->PolicyEvaluator()); sl@0: aPrinter->PrintL(_L(" Dialog Creator: 0x%08x\n"), aPolicy->DialogCreator()); sl@0: aPrinter->PrintL(_L("\n")); sl@0: } sl@0: sl@0: static void PrintAuthPolicyL(CPrinter* aPrinter, TAuthorisationPolicy aAuthPolicy) sl@0: /** sl@0: Prints the authorisation policy. sl@0: @param aPrinter The printer object. sl@0: @param aAuthPolicy The authorisation policy. sl@0: */ sl@0: { sl@0: TBuf<80> buf; sl@0: TPtrC authPol; sl@0: switch (aAuthPolicy) sl@0: { sl@0: case EAlwaysCheck: sl@0: authPol.Set(_L("EAlwaysCheck")); sl@0: break; sl@0: case ECheckPostManufacture: sl@0: authPol.Set(_L("ECheckPostManufacture")); sl@0: break; sl@0: case ECheckUnprotectedSids: sl@0: authPol.Set(_L("ECheckUnprotectedSids")); sl@0: break; sl@0: case ECheckIfFailed: sl@0: authPol.Set(_L("ECheckIfFailed")); sl@0: break; sl@0: case ENeverCheck: sl@0: authPol.Set(_L("ENeverCheck")); sl@0: break; sl@0: default: sl@0: authPol.Set(_L("*** UNKNOWN ***")); sl@0: break; sl@0: } sl@0: buf.AppendFormat(_L(" Authorisation Policy: %S\n"), &authPol); sl@0: aPrinter->PrintL(buf); sl@0: } sl@0: sl@0: static void PrintPoliciesL(CPrinter* aPrinter, CPolicyReader* aReader) sl@0: /** sl@0: Prints all of the policies returned by a CPolicyReader object. sl@0: @param aPrinter The printer object. sl@0: @param aReader The policy reader. sl@0: */ sl@0: { sl@0: TPolicyHeader hdr = aReader->Header(); sl@0: aPrinter->PrintL(_L("*** Policy Header ***\n")); sl@0: aPrinter->PrintL(_L(" Policy Format: %d\n"), hdr.iFormatVersion); sl@0: aPrinter->PrintL(_L(" Major Version: %d\n"), hdr.iMajorVersion); sl@0: aPrinter->PrintL(_L(" Minor Version: %d\n"), hdr.iMajorVersion); sl@0: aPrinter->PrintL(_L(" Default Policy Evaluator: 0x%08x\n"), hdr.iDefaultPolicyEvaluator); sl@0: aPrinter->PrintL(_L(" Default Dialog Creator: 0x%08x\n"), hdr.iDefaultDialogCreator); sl@0: PrintAuthPolicyL(aPrinter, hdr.iAuthPolicy); sl@0: aPrinter->PrintL(_L("\n")); sl@0: sl@0: CPolicy* p; sl@0: TInt i = 0; sl@0: while ((p = aReader->NextPolicyL()) != 0) sl@0: { sl@0: TBuf<32> buf; sl@0: buf.AppendFormat(_L("*** Policy %d ***\n"), i); sl@0: aPrinter->PrintL(buf); sl@0: sl@0: PrintPolicy(aPrinter, p); sl@0: delete p; sl@0: ++i; sl@0: } sl@0: } sl@0: sl@0: static void MainL(void) sl@0: /** sl@0: Takes a User Prompt Service policy resource file and dumps it as human readable text to the sl@0: console. The user may also specify the name of an output file on the command line. If so, text sl@0: is also written to this file. sl@0: */ sl@0: { sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: sl@0: CConsoleBase* console = Console::NewL(KAppName, TSize(KDefaultConsWidth, KDefaultConsHeight)); sl@0: CleanupStack::PushL(console); sl@0: sl@0: CCommandLineArguments* args = CCommandLineArguments::NewLC(); sl@0: sl@0: if (args->Count() > 1) sl@0: { sl@0: CPolicyReader* reader = CPolicyReader::NewLC(fs, args->Arg(1)); sl@0: CPrinter* printer(0); sl@0: if (args->Count() > 2) sl@0: { sl@0: RFile outFile; sl@0: User::LeaveIfError(outFile.Replace(fs, args->Arg(2), EFileShareExclusive | EFileWrite)); sl@0: CleanupClosePushL(outFile); sl@0: printer = CPrinter::NewLC(console, outFile); sl@0: sl@0: CleanupStack::Pop(printer); sl@0: CleanupStack::PopAndDestroy(&outFile); sl@0: CleanupStack::PushL(printer); sl@0: } sl@0: else sl@0: { sl@0: printer = CPrinter::NewLC(console); sl@0: } sl@0: __UHEAP_MARK; sl@0: PrintPoliciesL(printer, reader); sl@0: __UHEAP_MARKEND; sl@0: sl@0: if (args->Count() < 3) sl@0: { sl@0: // If no output file is specified then pause after finishing sl@0: // because the console will vanish when it is closed. sl@0: console->Printf(_L("Press any key to continue\r\n")); sl@0: console->Getch(); sl@0: } sl@0: CleanupStack::PopAndDestroy(2, reader); // printer, reader sl@0: } sl@0: else sl@0: { sl@0: console->Printf(_L("Usage: dumppolicy.exe policy.rsc \r\n")); sl@0: console->Printf(_L("Press any key to continue\r\n")); sl@0: console->Getch(); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(3, &fs); // args, console, fs sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: /** sl@0: Creats clean up stack and invokes real main function. sl@0: */ sl@0: { sl@0: CTrapCleanup* cleanup = CTrapCleanup::New(); sl@0: if(cleanup == NULL) sl@0: { sl@0: return KErrNoMemory; sl@0: } sl@0: __UHEAP_MARK; sl@0: TRAPD(err, MainL()); sl@0: __UHEAP_MARKEND; sl@0: delete cleanup; sl@0: return err; sl@0: }