123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "CoreMain.h"
- #include "Common.h"
- #include "Interface.h"
- #include "Configuration.h"
- #include "PuttyIntf.h"
- #include "Cryptography.h"
- #include <DateUtils.hpp>
- #include "FileZillaIntf.h"
- #include "NeonIntf.h"
- #include "TextsCore.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- TConfiguration * Configuration = NULL;
- TStoredSessionList * StoredSessions = NULL;
- TApplicationLog * ApplicationLog = NULL;
- bool AnySession = false;
- //---------------------------------------------------------------------------
- TQueryButtonAlias::TQueryButtonAlias()
- {
- OnSubmit = NULL;
- GroupWith = -1;
- ElevationRequired = false;
- MenuButton = false;
- }
- //---------------------------------------------------------------------------
- TQueryButtonAlias TQueryButtonAlias::CreateYesToAllGrouppedWithYes()
- {
- TQueryButtonAlias Result;
- Result.Button = qaYesToAll;
- Result.GroupWith = qaYes;
- Result.GrouppedShiftState = TShiftState() << ssShift;
- return Result;
- }
- //---------------------------------------------------------------------------
- TQueryButtonAlias TQueryButtonAlias::CreateNoToAllGrouppedWithNo()
- {
- TQueryButtonAlias Result;
- Result.Button = qaNoToAll;
- Result.GroupWith = qaNo;
- Result.GrouppedShiftState = TShiftState() << ssShift;
- return Result;
- }
- //---------------------------------------------------------------------------
- TQueryButtonAlias TQueryButtonAlias::CreateAllAsYesToNewerGrouppedWithYes()
- {
- TQueryButtonAlias Result;
- Result.Button = qaAll;
- Result.Alias = LoadStr(YES_TO_NEWER_BUTTON);
- Result.GroupWith = qaYes;
- Result.GrouppedShiftState = TShiftState() << ssCtrl;
- return Result;
- }
- //---------------------------------------------------------------------------
- TQueryButtonAlias TQueryButtonAlias::CreateIgnoreAsRenameGrouppedWithNo()
- {
- TQueryButtonAlias Result;
- Result.Button = qaIgnore;
- Result.Alias = LoadStr(RENAME_BUTTON);
- Result.GroupWith = qaNo;
- Result.GrouppedShiftState = TShiftState() << ssCtrl;
- return Result;
- }
- //---------------------------------------------------------------------------
- TQueryParams::TQueryParams(unsigned int AParams, UnicodeString AHelpKeyword)
- {
- Params = AParams;
- Aliases = NULL;
- AliasesCount = 0;
- Timer = 0;
- TimerEvent = NULL;
- TimerMessage = L"";
- TimerAnswers = 0;
- TimerQueryType = static_cast<TQueryType>(-1);
- Timeout = 0;
- TimeoutAnswer = 0;
- TimeoutResponse = 0;
- NoBatchAnswers = 0;
- HelpKeyword = AHelpKeyword;
- }
- //---------------------------------------------------------------------------
- TQueryParams::TQueryParams(const TQueryParams & Source)
- {
- Assign(Source);
- }
- //---------------------------------------------------------------------------
- void TQueryParams::Assign(const TQueryParams & Source)
- {
- *this = Source;
- }
- //---------------------------------------------------------------------------
- bool __fastcall IsAuthenticationPrompt(TPromptKind Kind)
- {
- return
- (Kind == pkUserName) || (Kind == pkPassphrase) || (Kind == pkTIS) ||
- (Kind == pkCryptoCard) || (Kind == pkKeybInteractive) ||
- (Kind == pkPassword) || (Kind == pkNewPassword);
- }
- //---------------------------------------------------------------------------
- bool __fastcall IsPasswordOrPassphrasePrompt(TPromptKind Kind, TStrings * Prompts)
- {
- return
- (Prompts->Count == 1) && FLAGCLEAR(int(Prompts->Objects[0]), pupEcho) &&
- ((Kind == pkPassword) || (Kind == pkPassphrase) || (Kind == pkKeybInteractive) ||
- (Kind == pkTIS) || (Kind == pkCryptoCard));
- }
- //---------------------------------------------------------------------------
- bool __fastcall IsPasswordPrompt(TPromptKind Kind, TStrings * Prompts)
- {
- return
- IsPasswordOrPassphrasePrompt(Kind, Prompts) &&
- (Kind != pkPassphrase);
- }
- //---------------------------------------------------------------------------
- void CoreLoad()
- {
- bool SessionList = true;
- std::unique_ptr<THierarchicalStorage> SessionsStorage(Configuration->CreateScpStorage(SessionList));
- THierarchicalStorage * ConfigStorage;
- std::unique_ptr<THierarchicalStorage> ConfigStorageAuto;
- if (!SessionList)
- {
- // can reuse this for configuration
- ConfigStorage = SessionsStorage.get();
- }
- else
- {
- ConfigStorageAuto.reset(Configuration->CreateConfigStorage());
- ConfigStorage = ConfigStorageAuto.get();
- }
- try
- {
- Configuration->Load(ConfigStorage);
- }
- catch (Exception & E)
- {
- ShowExtendedException(&E);
- }
- // should be noop, unless exception occured above
- ConfigStorage->CloseAll();
- StoredSessions = new TStoredSessionList();
- try
- {
- if (SessionsStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
- {
- StoredSessions->Load(SessionsStorage.get());
- }
- }
- catch (Exception & E)
- {
- ShowExtendedException(&E);
- }
- }
- //---------------------------------------------------------------------------
- void CoreInitialize()
- {
- Randomize();
- CryptographyInitialize();
- // we do not expect configuration re-creation
- DebugAssert(Configuration == NULL);
- // configuration needs to be created and loaded before putty is initialized,
- // so that random seed path is known
- Configuration = CreateConfiguration();
- PuttyInitialize();
- TFileZillaIntf::Initialize();
- // TlsCipherList() also relies on this to be called
- NeonInitialize();
- CoreLoad();
- }
- //---------------------------------------------------------------------------
- void CoreFinalize()
- {
- try
- {
- Configuration->Save();
- }
- catch(Exception & E)
- {
- ShowExtendedException(&E);
- }
- NeonFinalize();
- TFileZillaIntf::Finalize();
- PuttyFinalize();
- delete StoredSessions;
- StoredSessions = NULL;
- delete Configuration;
- Configuration = NULL;
- CryptographyFinalize();
- }
- //---------------------------------------------------------------------------
- void CoreSetResourceModule(void * ResourceHandle)
- {
- TFileZillaIntf::SetResourceModule(ResourceHandle);
- }
- //---------------------------------------------------------------------------
- void CoreMaintenanceTask()
- {
- DontSaveRandomSeed();
- }
- //---------------------------------------------------------------------------
- void CoreUpdateFinalStaticUsage()
- {
- if (!AnySession)
- {
- Configuration->Usage->Inc(L"RunsWithoutSession");
- }
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TOperationVisualizer::TOperationVisualizer(bool UseBusyCursor) :
- FUseBusyCursor(UseBusyCursor)
- {
- if (FUseBusyCursor)
- {
- FToken = BusyStart();
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TOperationVisualizer::~TOperationVisualizer()
- {
- if (FUseBusyCursor)
- {
- BusyEnd(FToken);
- }
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TInstantOperationVisualizer::TInstantOperationVisualizer() :
- FStart(Now())
- {
- }
- //---------------------------------------------------------------------------
- __fastcall TInstantOperationVisualizer::~TInstantOperationVisualizer()
- {
- TDateTime Time = Now();
- __int64 Duration = MilliSecondsBetween(Time, FStart);
- const __int64 MinDuration = 250;
- if (Duration < MinDuration)
- {
- Sleep(static_cast<unsigned int>(MinDuration - Duration));
- }
- }
- //---------------------------------------------------------------------------
|