sl@0: /* sl@0: * Copyright (c) 2004-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: sl@0: #include "keytool_utils.h" sl@0: #include "keytool_defs.h" sl@0: #include "keytool_controller.h" sl@0: #include "keytool_view_imp.h" sl@0: #include "keytool_commands.h" sl@0: #include "keytoolfileview.h" sl@0: sl@0: sl@0: // Boiler plate sl@0: _LIT(KShortName, "Nokia KeyTool"); sl@0: _LIT(KName, "Nokia KeyStore Manipulation Tool"); sl@0: _LIT(KCopyright, "Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved."); sl@0: sl@0: _LIT(KNewLine, "\n"); sl@0: sl@0: _LIT(KDone, "Press any key to continue... \n"); sl@0: sl@0: // Keytool command line parameters sl@0: _LIT(KList, "-list"); sl@0: _LIT(KListShort, "-l"); sl@0: sl@0: _LIT(KListStores, "-liststores"); sl@0: _LIT(KListStoresShort, "-ls"); sl@0: sl@0: _LIT(KImport, "-import"); sl@0: _LIT(KImportShort, "-i"); sl@0: sl@0: sl@0: _LIT(KRemove, "-remove"); sl@0: _LIT(KRemoveShort, "-r"); sl@0: sl@0: _LIT(KHelp, "-help"); sl@0: _LIT(KHelpShort, "-h"); sl@0: sl@0: _LIT(KSetUser, "-setuser"); sl@0: _LIT(KSetUserShort, "-s"); sl@0: sl@0: _LIT(KSetAllUsers, "-setallusers"); sl@0: _LIT(KSetAllUsersShort, "-a"); sl@0: sl@0: _LIT(KSetManager, "-setmanager"); sl@0: _LIT(KSetManagerShort, "-m"); sl@0: sl@0: _LIT(KRemoveUser, "-removeuser"); sl@0: _LIT(KRemoveUserShort, "-ru"); sl@0: sl@0: // Command parameters sl@0: _LIT(KLabel, "-label"); sl@0: _LIT(KKeyUsage, "-usage"); sl@0: _LIT(KAccess, "-access"); sl@0: sl@0: _LIT(KStore, "-store"); sl@0: sl@0: //_LIT(KExpiry, "-expiry"); // Functionality not present at the moment... sl@0: _LIT(KDetails, "-details"); sl@0: _LIT(KDetailsShort, "-d"); sl@0: sl@0: _LIT(KPageWise, "-page"); sl@0: _LIT(KPageWiseShort, "-p"); sl@0: sl@0: #ifdef KEYTOOL sl@0: _LIT(KMigrateStore, "-migratestore"); sl@0: _LIT(KNewFileName, "-new"); sl@0: _LIT(KAuthExpression, "-expr"); sl@0: _LIT(KFreshness, "-freshness"); sl@0: #endif // KEYTOOL sl@0: sl@0: const TInt KMaxArgs = 10; sl@0: sl@0: /** sl@0: * Displays tool name and copyright informations. sl@0: */ sl@0: LOCAL_D void BoilerPlateL(CConsoleBase* console) sl@0: { sl@0: console->Printf(KNewLine); sl@0: console->Printf(KName); sl@0: console->Printf(KNewLine); sl@0: console->Printf(KCopyright); sl@0: console->Printf(KNewLine); sl@0: console->Printf(KNewLine); sl@0: } sl@0: sl@0: sl@0: LOCAL_D TBool VerifyCommand(const TDesC& aCommand, TInt& aCmdNum, TInt& aCmdCount) sl@0: { sl@0: if ((aCmdNum != -1) && (aCommand[0] == '-')) sl@0: { sl@0: aCmdNum = KeyToolDefController::KUsageCommand; sl@0: aCmdCount = KMaxArgs; sl@0: return 1; sl@0: } sl@0: if (aCommand.CompareF(KList) == 0 || aCommand.Compare(KListShort) == 0) sl@0: { sl@0: aCmdNum = KeyToolDefController::KListCommand; sl@0: } sl@0: else if (aCommand.CompareF(KListStores) == 0 || aCommand.Compare(KListStoresShort) == 0) sl@0: { sl@0: aCmdNum = KeyToolDefController::KListStoresCommand; sl@0: } sl@0: else if (aCommand.CompareF(KImport) == 0 || aCommand.Compare(KImportShort) == 0) sl@0: { sl@0: aCmdNum = KeyToolDefController::KImportCommand; sl@0: } sl@0: else if (aCommand.CompareF(KRemove) == 0 || aCommand.Compare(KRemoveShort) == 0) sl@0: { sl@0: aCmdNum = KeyToolDefController::KRemoveCommand; sl@0: } sl@0: else if (aCommand.CompareF(KSetUser) == 0 || aCommand.Compare(KSetUserShort) == 0) sl@0: { sl@0: aCmdNum = KeyToolDefController::KSetUserCommand; sl@0: } sl@0: else if (aCommand.CompareF(KSetManager) == 0 || aCommand.Compare(KSetManagerShort) == 0) sl@0: { sl@0: aCmdNum = KeyToolDefController::KSetManagerCommand; sl@0: } sl@0: else if (aCommand.CompareF(KRemoveUser) == 0 || aCommand.Compare(KRemoveUserShort) == 0) sl@0: { sl@0: aCmdNum = KeyToolDefController::KRemoveUserCommand; sl@0: } sl@0: else if (aCommand.CompareF(KSetAllUsers) == 0 || aCommand.Compare(KSetAllUsersShort) == 0) sl@0: { sl@0: aCmdNum = KeyToolDefController::KSetAllUsersCommand; sl@0: } sl@0: sl@0: #ifdef KEYTOOL sl@0: else if ( aCommand.CompareF(KMigrateStore) == 0 ) sl@0: { sl@0: aCmdNum = KeyToolDefController::KMigrateStore; sl@0: } sl@0: #endif // KEYTOOL sl@0: sl@0: else sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: /** sl@0: * Parses the command line and given control to the handler to deal with the request. sl@0: */ sl@0: LOCAL_D void DoMainL() sl@0: { sl@0: TBool interactiveMode = ETrue; sl@0: sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: sl@0: CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen)); sl@0: CleanupStack::PushL(console); sl@0: CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC(); sl@0: TInt cmdArgsCount = cmdArgs->Count(); sl@0: sl@0: RFile file; sl@0: // command: keytool inputfile outputfile sl@0: if (cmdArgsCount == 3) sl@0: { sl@0: if (KeyToolUtils::DoesFileExistsL(fs,cmdArgs->Arg(1))) sl@0: { sl@0: interactiveMode = EFalse; sl@0: TInt error = file.Open(fs, cmdArgs->Arg(1), EFileRead|EFileShareAny); sl@0: file.Close(); sl@0: sl@0: TInt error1 = file.Replace(fs, cmdArgs->Arg(2), EFileWrite|EFileShareExclusive); sl@0: CleanupClosePushL(file); sl@0: // If the input file doesn't exist or not able to create outputfile sl@0: // switch to Interactive mode sl@0: if (error != KErrNone || error1 != KErrNone) sl@0: { sl@0: CleanupStack::PopAndDestroy(&file); sl@0: interactiveMode = ETrue; sl@0: } sl@0: } sl@0: } sl@0: sl@0: CKeytoolConsoleView* view = CKeytoolConsoleView::NewLC(*console); sl@0: CKeyToolController* controller = CKeyToolController::NewLC(*view); sl@0: CArrayFixFlat* args = new (ELeave) CArrayFixFlat (10); sl@0: CleanupStack::PushL(args); sl@0: CKeytoolFileView* view1 = NULL; sl@0: sl@0: TInt cmdCount = 0; sl@0: if (interactiveMode) sl@0: { sl@0: KeyToolUtils::SetConsole(console); sl@0: BoilerPlateL(console); sl@0: for (TInt i = 0; i < cmdArgsCount; i++) sl@0: { sl@0: args->AppendL(cmdArgs->Arg(i)); sl@0: } sl@0: // In Interactive mode by default we can execute 1 command only. sl@0: cmdCount = 1; sl@0: } sl@0: else sl@0: { sl@0: CleanupStack::PopAndDestroy(3, view); // args, controller, view sl@0: KeyToolUtils::SetFile(&file); sl@0: view1 = CKeytoolFileView::NewLC(cmdArgs->Arg(1)); sl@0: cmdCount = view1->SplitFileInputToArrayL(); sl@0: } sl@0: sl@0: for (TInt j = 0; j < cmdCount; j++) sl@0: { sl@0: if (!interactiveMode) sl@0: { sl@0: controller = CKeyToolController::NewLC(*view1); sl@0: args = view1->ReadArrayArgumentsLC(j); sl@0: } sl@0: sl@0: CKeyToolParameters* params = CKeyToolParameters::NewLC(); sl@0: sl@0: TInt command = -1; sl@0: TInt i = -1; sl@0: sl@0: TInt argsCount = args->Count(); sl@0: while (i < (argsCount-1)) sl@0: { sl@0: i++; sl@0: sl@0: if ((args->At(i).CompareF(KDetails)==0)|| (args->At(i).Compare(KDetailsShort)==0)) sl@0: { sl@0: params->iIsDetailed = ETrue; sl@0: continue; sl@0: } sl@0: sl@0: if (args->At(i).CompareF(KPageWise)==0 || (args->At(i).Compare(KPageWiseShort)==0)) sl@0: { sl@0: params->iPageWise = ETrue; sl@0: continue; sl@0: } sl@0: sl@0: TDesC& commd = args->At(i); sl@0: if (VerifyCommand(commd, command, i)) sl@0: { sl@0: if (commd.CompareF(KSetAllUsers) == 0 || commd.Compare(KSetAllUsersShort) == 0 || sl@0: commd.CompareF(KRemoveUser) == 0 || commd.Compare(KRemoveUserShort) == 0 || sl@0: commd.CompareF(KSetManager) == 0 || commd.Compare(KSetManagerShort) == 0 ) sl@0: { sl@0: i++; sl@0: if (args->At(i)[0] == '-' || args->At(i).Left(2) == _L("0x")) sl@0: { sl@0: i = argsCount; sl@0: command = KeyToolDefController::KUsageCommand; sl@0: } sl@0: else sl@0: { sl@0: i--; sl@0: } sl@0: } sl@0: if (commd.CompareF(KSetAllUsers) == 0 || commd.Compare(KSetAllUsersShort) == 0) sl@0: { sl@0: params->iPolicy = CKeyToolParameters::ESetAllUsersPolicy; sl@0: params->iIsDetailed = ETrue; sl@0: } sl@0: if (commd.CompareF(KRemoveUser) == 0 || commd.Compare(KRemoveUserShort) == 0) sl@0: { sl@0: params->iPolicy = CKeyToolParameters::ERemoveUserPolicy; sl@0: params->iIsDetailed = ETrue; sl@0: } sl@0: if (commd.CompareF(KSetManager) == 0 || commd.Compare(KSetManagerShort) == 0) sl@0: { sl@0: params->iPolicy = CKeyToolParameters::ESetManagerPolicy; sl@0: params->iIsDetailed = ETrue; sl@0: } sl@0: if (commd.CompareF(KSetUser) == 0 || commd.Compare(KSetUserShort) == 0) sl@0: { sl@0: params->iPolicy = CKeyToolParameters::ESetUserPolicy; sl@0: i++; sl@0: if (i >= argsCount || args->At(i)[0] == '-' || args->At(i).Left(2) != _L("0x")) sl@0: { sl@0: i = argsCount; sl@0: command = KeyToolDefController::KUsageCommand; sl@0: } sl@0: else sl@0: { sl@0: TUid app; sl@0: TLex lex(args->At(i).Mid(2)); sl@0: TUint uid =0; sl@0: TInt err = lex.Val(uid, EHex); sl@0: if (err == KErrNone) sl@0: { sl@0: app = TUid::Uid(uid); sl@0: } sl@0: params->iUIDs.Append(app); sl@0: params->iIsDetailed = ETrue; sl@0: } sl@0: } sl@0: sl@0: #ifdef KEYTOOL sl@0: if(commd.CompareF(KMigrateStore) == 0 ) sl@0: { sl@0: int mandatoryParams = 2; sl@0: if(argsCount-1 >= ++i) sl@0: { sl@0: params->iOldKeyFile = args->At(i).AllocL(); sl@0: } sl@0: else sl@0: { sl@0: controller->DisplayLocalisedMsgL(R_KEYTOOL_USAGE_OLDKEY_ABSENT); sl@0: --mandatoryParams; sl@0: } sl@0: if(argsCount-1 >= ++i) sl@0: { sl@0: params->iPassphrase = args->At(i).AllocL(); sl@0: } sl@0: else sl@0: { sl@0: controller->DisplayLocalisedMsgL(R_KEYTOOL_USAGE_PASSPHRASE_ABSENT); sl@0: --mandatoryParams; sl@0: } sl@0: sl@0: if( mandatoryParams != 2 ) sl@0: { sl@0: command = KeyToolDefController::KUsageCommand; sl@0: } sl@0: else sl@0: { sl@0: TInt count = args->Count(); sl@0: sl@0: while(++i < count ) sl@0: { sl@0: TDesC& commd = args->At(i); sl@0: if(commd.CompareF(KNewFileName) == 0 && argsCount-1 >= ++i) sl@0: { sl@0: delete params->iNewKeyFile; sl@0: params->iNewKeyFile=NULL; sl@0: params->iNewKeyFile = args->At(i++).AllocL(); sl@0: } sl@0: else if(commd.CompareF(KAuthExpression) == 0 && argsCount-1 >= ++i ) sl@0: { sl@0: params->iAuthExpression = args->At(i++).AllocL(); sl@0: } sl@0: else if(commd.CompareF(KFreshness) == 0 && argsCount-1 >= ++i ) sl@0: { sl@0: TLex lex(args->At(i++)); sl@0: TInt err = lex.Val(params->iFreshness); sl@0: } sl@0: } // while sl@0: } // if(mandatoryParams == 0) sl@0: } // if sl@0: sl@0: #endif // KEYTOOL sl@0: continue; sl@0: } sl@0: sl@0: TDesC& cmd = args->At(i); sl@0: if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0 || sl@0: cmd.CompareF(KLabel) == 0 || cmd.CompareF(KKeyUsage) == 0 || sl@0: cmd.CompareF(KStore) == 0 || cmd.CompareF(KAccess) == 0) sl@0: { sl@0: i++; sl@0: if (i >= argsCount || args->At(i)[0] == '-') sl@0: { sl@0: i = argsCount; sl@0: command = KeyToolDefController::KUsageCommand; sl@0: } sl@0: else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0) sl@0: { sl@0: params->iDefault = args->At(i).AllocL(); sl@0: i = argsCount; sl@0: } sl@0: else if (cmd.CompareF(KLabel) == 0) sl@0: { sl@0: params->iLabel = args->At(i).AllocL(); sl@0: } sl@0: else if (cmd.CompareF(KKeyUsage) == 0) sl@0: { sl@0: params->iUsage = KeyToolUtils::ParseKeyUsage(args->At(i)); sl@0: } sl@0: else if (cmd.CompareF(KStore) == 0) sl@0: { sl@0: TLex parser(args->At(i)); sl@0: TInt err = parser.Val(params->iKeystoreIndex); sl@0: params->iIsDetailed = ETrue; sl@0: } sl@0: else if (cmd.CompareF(KAccess) == 0) sl@0: { sl@0: params->iAccess = KeyToolUtils::ParseKeyAccess(args->At(i)); sl@0: } sl@0: else sl@0: { sl@0: // no action required sl@0: } sl@0: continue; sl@0: } sl@0: sl@0: if (i!=0) sl@0: { sl@0: if (args->At(i)[0] == '-') sl@0: { sl@0: i = argsCount; sl@0: command = KeyToolDefController::KUsageCommand; sl@0: continue; sl@0: } sl@0: delete params->iDefault; sl@0: params->iDefault = NULL; sl@0: params->iDefault = args->At(i).AllocL(); sl@0: } sl@0: } sl@0: sl@0: sl@0: if (command != -1) sl@0: { sl@0: TRAP_IGNORE(controller->HandleCommandL(command, params)); sl@0: } sl@0: else sl@0: { sl@0: controller->HandleCommandL(KeyToolDefController::KUsageCommand, params); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(3, controller); // params, args, controller sl@0: } sl@0: if (interactiveMode) sl@0: { sl@0: CleanupStack::PopAndDestroy(view); sl@0: // We are done! sl@0: console->Printf(KNewLine); sl@0: console->Printf(KDone); sl@0: console->Getch(); sl@0: } sl@0: else sl@0: { sl@0: CleanupStack::PopAndDestroy(2, &file); //view1 and file sl@0: } sl@0: CleanupStack::PopAndDestroy(3, &fs); //cmdArgs, console, fs sl@0: } sl@0: sl@0: sl@0: GLDEF_C TInt E32Main() // main function called by E32 sl@0: { sl@0: __UHEAP_MARK; sl@0: CTrapCleanup* cleanup=CTrapCleanup::New(); sl@0: sl@0: TRAP_IGNORE(DoMainL()); sl@0: sl@0: delete cleanup; sl@0: __UHEAP_MARKEND; sl@0: return 0; sl@0: } sl@0: