CoreMain.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "CoreMain.h"
  5. #include "Common.h"
  6. #include "Interface.h"
  7. #include "Configuration.h"
  8. #include "PuttyIntf.h"
  9. #include "Cryptography.h"
  10. #include <DateUtils.hpp>
  11. #include "FileZillaIntf.h"
  12. #include "NeonIntf.h"
  13. #include "TextsCore.h"
  14. //---------------------------------------------------------------------------
  15. #pragma package(smart_init)
  16. //---------------------------------------------------------------------------
  17. TConfiguration * Configuration = NULL;
  18. TStoredSessionList * StoredSessions = NULL;
  19. TApplicationLog * ApplicationLog = NULL;
  20. bool AnySession = false;
  21. //---------------------------------------------------------------------------
  22. TQueryButtonAlias::TQueryButtonAlias()
  23. {
  24. OnSubmit = NULL;
  25. GroupWith = -1;
  26. ElevationRequired = false;
  27. MenuButton = false;
  28. }
  29. //---------------------------------------------------------------------------
  30. TQueryButtonAlias TQueryButtonAlias::CreateYesToAllGrouppedWithYes()
  31. {
  32. TQueryButtonAlias Result;
  33. Result.Button = qaYesToAll;
  34. Result.GroupWith = qaYes;
  35. Result.GrouppedShiftState = TShiftState() << ssShift;
  36. return Result;
  37. }
  38. //---------------------------------------------------------------------------
  39. TQueryButtonAlias TQueryButtonAlias::CreateNoToAllGrouppedWithNo()
  40. {
  41. TQueryButtonAlias Result;
  42. Result.Button = qaNoToAll;
  43. Result.GroupWith = qaNo;
  44. Result.GrouppedShiftState = TShiftState() << ssShift;
  45. return Result;
  46. }
  47. //---------------------------------------------------------------------------
  48. TQueryButtonAlias TQueryButtonAlias::CreateAllAsYesToNewerGrouppedWithYes()
  49. {
  50. TQueryButtonAlias Result;
  51. Result.Button = qaAll;
  52. Result.Alias = LoadStr(YES_TO_NEWER_BUTTON);
  53. Result.GroupWith = qaYes;
  54. Result.GrouppedShiftState = TShiftState() << ssCtrl;
  55. return Result;
  56. }
  57. //---------------------------------------------------------------------------
  58. TQueryButtonAlias TQueryButtonAlias::CreateIgnoreAsRenameGrouppedWithNo()
  59. {
  60. TQueryButtonAlias Result;
  61. Result.Button = qaIgnore;
  62. Result.Alias = LoadStr(RENAME_BUTTON);
  63. Result.GroupWith = qaNo;
  64. Result.GrouppedShiftState = TShiftState() << ssCtrl;
  65. return Result;
  66. }
  67. //---------------------------------------------------------------------------
  68. TQueryParams::TQueryParams(unsigned int AParams, UnicodeString AHelpKeyword)
  69. {
  70. Params = AParams;
  71. Aliases = NULL;
  72. AliasesCount = 0;
  73. Timer = 0;
  74. TimerEvent = NULL;
  75. TimerMessage = L"";
  76. TimerAnswers = 0;
  77. TimerQueryType = static_cast<TQueryType>(-1);
  78. Timeout = 0;
  79. TimeoutAnswer = 0;
  80. TimeoutResponse = 0;
  81. NoBatchAnswers = 0;
  82. HelpKeyword = AHelpKeyword;
  83. }
  84. //---------------------------------------------------------------------------
  85. TQueryParams::TQueryParams(const TQueryParams & Source)
  86. {
  87. Assign(Source);
  88. }
  89. //---------------------------------------------------------------------------
  90. void TQueryParams::Assign(const TQueryParams & Source)
  91. {
  92. *this = Source;
  93. }
  94. //---------------------------------------------------------------------------
  95. bool __fastcall IsAuthenticationPrompt(TPromptKind Kind)
  96. {
  97. return
  98. (Kind == pkUserName) || (Kind == pkPassphrase) || (Kind == pkTIS) ||
  99. (Kind == pkCryptoCard) || (Kind == pkKeybInteractive) ||
  100. (Kind == pkPassword) || (Kind == pkNewPassword);
  101. }
  102. //---------------------------------------------------------------------------
  103. bool __fastcall IsPasswordOrPassphrasePrompt(TPromptKind Kind, TStrings * Prompts)
  104. {
  105. return
  106. (Prompts->Count == 1) && FLAGCLEAR(int(Prompts->Objects[0]), pupEcho) &&
  107. ((Kind == pkPassword) || (Kind == pkPassphrase) || (Kind == pkKeybInteractive) ||
  108. (Kind == pkTIS) || (Kind == pkCryptoCard));
  109. }
  110. //---------------------------------------------------------------------------
  111. bool __fastcall IsPasswordPrompt(TPromptKind Kind, TStrings * Prompts)
  112. {
  113. return
  114. IsPasswordOrPassphrasePrompt(Kind, Prompts) &&
  115. (Kind != pkPassphrase);
  116. }
  117. //---------------------------------------------------------------------------
  118. void CoreLoad()
  119. {
  120. bool SessionList = true;
  121. std::unique_ptr<THierarchicalStorage> SessionsStorage(Configuration->CreateScpStorage(SessionList));
  122. THierarchicalStorage * ConfigStorage;
  123. std::unique_ptr<THierarchicalStorage> ConfigStorageAuto;
  124. if (!SessionList)
  125. {
  126. // can reuse this for configuration
  127. ConfigStorage = SessionsStorage.get();
  128. }
  129. else
  130. {
  131. ConfigStorageAuto.reset(Configuration->CreateConfigStorage());
  132. ConfigStorage = ConfigStorageAuto.get();
  133. }
  134. try
  135. {
  136. Configuration->Load(ConfigStorage);
  137. }
  138. catch (Exception & E)
  139. {
  140. ShowExtendedException(&E);
  141. }
  142. // should be noop, unless exception occurred above
  143. ConfigStorage->CloseAll();
  144. StoredSessions = new TStoredSessionList();
  145. try
  146. {
  147. if (SessionsStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  148. {
  149. StoredSessions->Load(SessionsStorage.get());
  150. }
  151. }
  152. catch (Exception & E)
  153. {
  154. ShowExtendedException(&E);
  155. }
  156. }
  157. //---------------------------------------------------------------------------
  158. void CoreInitialize()
  159. {
  160. Randomize();
  161. CryptographyInitialize();
  162. // we do not expect configuration re-creation
  163. DebugAssert(Configuration == NULL);
  164. // configuration needs to be created and loaded before putty is initialized,
  165. // so that random seed path is known
  166. Configuration = CreateConfiguration();
  167. PuttyInitialize();
  168. TFileZillaIntf::Initialize();
  169. NeonInitialize();
  170. CoreLoad();
  171. }
  172. //---------------------------------------------------------------------------
  173. void CoreFinalize()
  174. {
  175. try
  176. {
  177. Configuration->Save();
  178. }
  179. catch(Exception & E)
  180. {
  181. ShowExtendedException(&E);
  182. }
  183. NeonFinalize();
  184. TFileZillaIntf::Finalize();
  185. PuttyFinalize();
  186. delete StoredSessions;
  187. StoredSessions = NULL;
  188. delete Configuration;
  189. Configuration = NULL;
  190. CryptographyFinalize();
  191. }
  192. //---------------------------------------------------------------------------
  193. void CoreSetResourceModule(void * ResourceHandle)
  194. {
  195. TFileZillaIntf::SetResourceModule(ResourceHandle);
  196. }
  197. //---------------------------------------------------------------------------
  198. void CoreMaintenanceTask()
  199. {
  200. DontSaveRandomSeed();
  201. }
  202. //---------------------------------------------------------------------------
  203. void CoreUpdateFinalStaticUsage()
  204. {
  205. if (!AnySession)
  206. {
  207. Configuration->Usage->Inc(L"RunsWithoutSession");
  208. }
  209. }
  210. //---------------------------------------------------------------------------
  211. //---------------------------------------------------------------------------
  212. __fastcall TOperationVisualizer::TOperationVisualizer(bool UseBusyCursor) :
  213. FUseBusyCursor(UseBusyCursor)
  214. {
  215. if (FUseBusyCursor)
  216. {
  217. FToken = BusyStart();
  218. }
  219. }
  220. //---------------------------------------------------------------------------
  221. __fastcall TOperationVisualizer::~TOperationVisualizer()
  222. {
  223. if (FUseBusyCursor)
  224. {
  225. BusyEnd(FToken);
  226. }
  227. }
  228. //---------------------------------------------------------------------------
  229. //---------------------------------------------------------------------------
  230. __fastcall TInstantOperationVisualizer::TInstantOperationVisualizer() :
  231. FStart(Now())
  232. {
  233. }
  234. //---------------------------------------------------------------------------
  235. __fastcall TInstantOperationVisualizer::~TInstantOperationVisualizer()
  236. {
  237. TDateTime Time = Now();
  238. __int64 Duration = MilliSecondsBetween(Time, FStart);
  239. const __int64 MinDuration = 250;
  240. if (Duration < MinDuration)
  241. {
  242. Sleep(static_cast<unsigned int>(MinDuration - Duration));
  243. }
  244. }
  245. //---------------------------------------------------------------------------