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