authorisation/userpromptservice/policies/test/dumppolicy/source/dumppolicy.cpp
changeset 8 35751d3474b7
child 108 ca9a0fc2f082
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bacline.h>
       
    20 #include <e32cons.h>
       
    21 #include <e32debug.h>
       
    22 #include <e32std.h>
       
    23 #include <f32file.h>
       
    24 #include <utf.h>
       
    25 
       
    26 #include <ups/policy.h>
       
    27 #include "../../../source/policyreader.h" 
       
    28 
       
    29 _LIT(KAppName, "dumppolicy");
       
    30 
       
    31 using namespace UserPromptService;
       
    32 
       
    33 /**
       
    34  Class that prints text to the console and optionally a log file.
       
    35  */
       
    36 class CPrinter : public CBase
       
    37 	{
       
    38 public:
       
    39 	static CPrinter* NewLC(CConsoleBase* aConsole);
       
    40 	
       
    41 	static CPrinter* NewLC(CConsoleBase* aConsole, RFile& aFile);
       
    42 	void PrintL(TRefByValue<const TDesC16> aFormat, ...);
       
    43 	~CPrinter();
       
    44 	
       
    45 private:
       
    46 	CPrinter(CConsoleBase* aConsole);
       
    47 	
       
    48 	/** Console object to print text to */
       
    49 	CConsoleBase* iCon;
       
    50 	
       
    51 	/* Optional file handle to write text to */
       
    52 	RFile iFile;
       
    53 	
       
    54 	/* Whether to log the output to the file */
       
    55 	TBool iLogToFile;
       
    56 	
       
    57 	/** Temporary buffer */
       
    58 	TBuf<256> iBuffer;
       
    59 	};
       
    60 
       
    61 	
       
    62 CPrinter* CPrinter::NewLC(CConsoleBase* aConsole, RFile& aFile) 
       
    63 /**
       
    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.
       
    68 */
       
    69 	{
       
    70 	CPrinter* self = CPrinter::NewLC(aConsole);
       
    71 	User::LeaveIfError(self->iFile.Duplicate(aFile));
       
    72 	self->iLogToFile = ETrue;
       
    73 	return self;
       
    74 	}
       
    75 
       
    76 CPrinter* CPrinter::NewLC(CConsoleBase* aConsole) 
       
    77 /**
       
    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.
       
    81 */
       
    82 	{
       
    83 	CPrinter* self = new(ELeave) CPrinter(aConsole);
       
    84 	CleanupStack::PushL(self);
       
    85 	return self;
       
    86 	}
       
    87 
       
    88 void CPrinter::PrintL(TRefByValue<const TDesC16> aFormat, ...)
       
    89 	{
       
    90 	VA_LIST list;
       
    91 	VA_START(list, aFormat);
       
    92 		
       
    93 	iBuffer.Zero();
       
    94 	iBuffer.AppendFormatList(aFormat, list);
       
    95 		
       
    96 	iCon->Printf(iBuffer);	
       
    97 	if (iLogToFile)
       
    98 		{
       
    99 		HBufC8* utf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(iBuffer);
       
   100 		CleanupStack::PushL(utf8);
       
   101 		User::LeaveIfError(iFile.Write(*utf8));
       
   102 		CleanupStack::PopAndDestroy(utf8);
       
   103 		}	
       
   104 	
       
   105 	VA_END(list);
       
   106 	}
       
   107 
       
   108 CPrinter::CPrinter(CConsoleBase* aConsole) : iCon(aConsole)
       
   109 /**
       
   110  Constructor
       
   111 */
       
   112 	{
       
   113 	}
       
   114 
       
   115 CPrinter::~CPrinter() 
       
   116 /**
       
   117  Destructor
       
   118 */
       
   119 	{
       
   120 	iFile.Close();
       
   121 	}
       
   122 
       
   123 void PrintSystemServerSecurity(CPrinter* aPrinter, CPolicy *aPolicy)
       
   124 /**
       
   125 Prints the system server security configuration.
       
   126 */
       
   127 	{
       
   128 
       
   129 	TPtrC p;
       
   130 	switch (aPolicy->SystemServerSecurity())
       
   131 		{
       
   132 	case CPolicy::ESystemServerSecurityPassedOrFailed:
       
   133 		p.Set(_L("ESystemServerSecurityPassedOrFailed"));
       
   134 		break;
       
   135 	case CPolicy::ESystemServerSecurityPassed:
       
   136 		p.Set(_L("ESystemServerSecurityPassed"));	
       
   137 		break;
       
   138 	case CPolicy::ESystemServerSecurityFailed:
       
   139 		p.Set(_L("ESystemServerSecurityFailed"));
       
   140 		break;
       
   141 	default:
       
   142 		p.Set(_L("*** UNKNOWN ***"));
       
   143 		break;				
       
   144 		}
       
   145 	TBuf<80> buf;
       
   146 	buf.AppendFormat(_L(" System Server Security: %S\n"), &p);
       
   147 	aPrinter->PrintL(buf);
       
   148 	}
       
   149 
       
   150 /**
       
   151  Prints a object to the supplied printer object.
       
   152  @param aPrinter	The printer.
       
   153  @param aPolicy		The policy to print.
       
   154  */
       
   155 void PrintPolicy(CPrinter* aPrinter, CPolicy *aPolicy)
       
   156 	{
       
   157 	_LIT16(KYes, "EYes");
       
   158 	_LIT16(KNo, "ENo");
       
   159 	_LIT16(KSessionYes, "ESessionYes");	
       
   160 	_LIT16(KSessionNo, "ESessionNo");	
       
   161 	_LIT16(KAlways, "EAlways");
       
   162 	_LIT16(KNever, "ENever");
       
   163 	_LIT16(KSpace, " ");
       
   164 		
       
   165 	TBuf<256> tmp;			
       
   166 	TInt sidClasses = aPolicy->SidClasses().iSidClasses;
       
   167 	aPrinter->PrintL(_L("            SID Classes: 0x%04x\n"), sidClasses);
       
   168 	tmp.Zero();
       
   169 	
       
   170 	const RArray<TSecureId>& sidList = aPolicy->SidList();	
       
   171 	aPrinter->PrintL(_L("               SID List:"));	
       
   172 	
       
   173 	TInt sidCount = sidList.Count();	
       
   174 	for (TInt i = 0; i < sidCount; ++i)
       
   175 		{		
       
   176 		aPrinter->PrintL(_L(" 0x%08x"), sidList[i].iId);
       
   177 		}
       
   178 	aPrinter->PrintL(_L("\n"));
       
   179 	
       
   180 	PrintSystemServerSecurity(aPrinter, aPolicy);
       
   181 	
       
   182 	tmp.Copy(aPolicy->Destination());
       
   183 	aPrinter->PrintL(_L("            Destination: %S\n"), &tmp);	
       
   184 	
       
   185 	TInt options = aPolicy->Options();
       
   186 	tmp.Zero();
       
   187 	if (options & CPolicy::EYes)
       
   188 		{
       
   189 		tmp.Append(KYes);
       
   190 		tmp.Append(KSpace);
       
   191 		}
       
   192 	if (options & CPolicy::ENo)
       
   193 		{
       
   194 		tmp.Append(KNo);
       
   195 		tmp.Append(KSpace);
       
   196 		}
       
   197 	if (options & CPolicy::ESessionYes)
       
   198 		{
       
   199 		tmp.Append(KSessionYes);
       
   200 		tmp.Append(KSpace);
       
   201 		}
       
   202 	if (options & CPolicy::EAlways)
       
   203 		{
       
   204 		tmp.Append(KAlways);
       
   205 		tmp.Append(KSpace);
       
   206 		}
       
   207 	if (options & CPolicy::ENever)
       
   208 		{
       
   209 		tmp.Append(KNever);
       
   210 		tmp.Append(KSpace);
       
   211 		}		
       
   212 	if (options & CPolicy::ESessionNo)
       
   213 		{
       
   214 		tmp.Append(KSessionNo);
       
   215 		tmp.Append(KSpace);
       
   216 		}
       
   217 	
       
   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"));
       
   222 	}
       
   223 
       
   224 static void PrintAuthPolicyL(CPrinter* aPrinter, TAuthorisationPolicy aAuthPolicy)
       
   225 /**
       
   226  Prints the authorisation policy.
       
   227  @param aPrinter	The printer object.
       
   228  @param aAuthPolicy	The authorisation policy.
       
   229  */
       
   230 	{
       
   231 	TBuf<80> buf;
       
   232 	TPtrC authPol;
       
   233 	switch (aAuthPolicy)
       
   234 		{
       
   235 		case EAlwaysCheck:
       
   236 			authPol.Set(_L("EAlwaysCheck"));
       
   237 			break;
       
   238 		case ECheckPostManufacture:
       
   239 			authPol.Set(_L("ECheckPostManufacture"));
       
   240 			break;
       
   241 		case ECheckUnprotectedSids:
       
   242 			authPol.Set(_L("ECheckUnprotectedSids"));
       
   243 			break;
       
   244 		case ECheckIfFailed:
       
   245 			authPol.Set(_L("ECheckIfFailed"));
       
   246 			break;
       
   247 		case ENeverCheck:
       
   248 			authPol.Set(_L("ENeverCheck"));
       
   249 			break;
       
   250 		default:
       
   251 			authPol.Set(_L("*** UNKNOWN ***"));
       
   252 			break;
       
   253 		}
       
   254 	buf.AppendFormat(_L("     Authorisation Policy: %S\n"), &authPol);
       
   255 	aPrinter->PrintL(buf);
       
   256 	}
       
   257 
       
   258 static void PrintPoliciesL(CPrinter* aPrinter, CPolicyReader* aReader)
       
   259 /**
       
   260  Prints all of the policies returned by a CPolicyReader object.
       
   261  @param aPrinter The printer object.
       
   262  @param aReader	 The policy reader.
       
   263  */
       
   264 	{
       
   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"));
       
   274 	
       
   275 	CPolicy* p;
       
   276 	TInt i = 0;
       
   277 	while ((p = aReader->NextPolicyL()) != 0)
       
   278 		{
       
   279 		TBuf<32> buf;
       
   280 		buf.AppendFormat(_L("*** Policy %d ***\n"), i);
       
   281 		aPrinter->PrintL(buf);
       
   282 		
       
   283 		PrintPolicy(aPrinter, p);
       
   284 		delete p;
       
   285 		++i;
       
   286 		}	
       
   287 	}
       
   288 
       
   289 static void MainL(void)
       
   290 /**
       
   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.
       
   294 */
       
   295 	{
       
   296 	RFs fs;
       
   297 	User::LeaveIfError(fs.Connect());
       
   298 	CleanupClosePushL(fs);
       
   299 		
       
   300 	CConsoleBase* console = Console::NewL(KAppName, TSize(KDefaultConsWidth, KDefaultConsHeight));
       
   301 	CleanupStack::PushL(console);
       
   302 	
       
   303 	CCommandLineArguments* args = CCommandLineArguments::NewLC();
       
   304 	
       
   305 	if (args->Count() > 1)
       
   306 		{		
       
   307 		CPolicyReader* reader = CPolicyReader::NewLC(fs, args->Arg(1));		
       
   308 		CPrinter* printer(0);
       
   309 		if (args->Count() > 2)
       
   310 			{
       
   311 			RFile outFile;
       
   312 			User::LeaveIfError(outFile.Replace(fs, args->Arg(2), EFileShareExclusive | EFileWrite));
       
   313 			CleanupClosePushL(outFile);
       
   314 			printer = CPrinter::NewLC(console, outFile);		
       
   315 			
       
   316 			CleanupStack::Pop(printer); 
       
   317 			CleanupStack::PopAndDestroy(&outFile);
       
   318 			CleanupStack::PushL(printer);
       
   319 			}
       
   320 		else
       
   321 			{
       
   322 			printer = CPrinter::NewLC(console);	
       
   323 			}
       
   324 		__UHEAP_MARK;
       
   325 		PrintPoliciesL(printer, reader);
       
   326 		__UHEAP_MARKEND;
       
   327 		
       
   328 		if (args->Count() < 3)
       
   329 			{
       
   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"));
       
   333 			console->Getch();
       
   334 			}		
       
   335 		CleanupStack::PopAndDestroy(2, reader); // printer, reader		
       
   336 		}
       
   337 	else 
       
   338 		{
       
   339 		console->Printf(_L("Usage: dumppolicy.exe policy.rsc <output.txt>\r\n"));
       
   340 		console->Printf(_L("Press any key to continue\r\n"));
       
   341 		console->Getch();
       
   342 		}
       
   343 	
       
   344 	CleanupStack::PopAndDestroy(3, &fs); // args, console, fs
       
   345 	}
       
   346 
       
   347 GLDEF_C TInt E32Main()
       
   348 /**
       
   349 Creats clean up stack and invokes real main function.
       
   350 */
       
   351 	{
       
   352 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   353 	if(cleanup == NULL)
       
   354 		{
       
   355 		return KErrNoMemory;
       
   356 		}
       
   357 	__UHEAP_MARK;
       
   358 	TRAPD(err, MainL());
       
   359 	__UHEAP_MARKEND;
       
   360 	delete cleanup;
       
   361 	return err;
       
   362 	}