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. bool AnySession = false;
  20. //---------------------------------------------------------------------------
  21. TQueryButtonAlias::TQueryButtonAlias()
  22. {
  23. OnSubmit = NULL;
  24. GroupWith = -1;
  25. ElevationRequired = false;
  26. MenuButton = false;
  27. }
  28. //---------------------------------------------------------------------------
  29. TQueryButtonAlias TQueryButtonAlias::CreateYesToAllGrouppedWithYes()
  30. {
  31. TQueryButtonAlias Result;
  32. Result.Button = qaYesToAll;
  33. Result.GroupWith = qaYes;
  34. Result.GrouppedShiftState = TShiftState() << ssShift;
  35. return Result;
  36. }
  37. //---------------------------------------------------------------------------
  38. TQueryButtonAlias TQueryButtonAlias::CreateNoToAllGrouppedWithNo()
  39. {
  40. TQueryButtonAlias Result;
  41. Result.Button = qaNoToAll;
  42. Result.GroupWith = qaNo;
  43. Result.GrouppedShiftState = TShiftState() << ssShift;
  44. return Result;
  45. }
  46. //---------------------------------------------------------------------------
  47. TQueryButtonAlias TQueryButtonAlias::CreateAllAsYesToNewerGrouppedWithYes()
  48. {
  49. TQueryButtonAlias Result;
  50. Result.Button = qaAll;
  51. Result.Alias = LoadStr(YES_TO_NEWER_BUTTON);
  52. Result.GroupWith = qaYes;
  53. Result.GrouppedShiftState = TShiftState() << ssCtrl;
  54. return Result;
  55. }
  56. //---------------------------------------------------------------------------
  57. TQueryButtonAlias TQueryButtonAlias::CreateIgnoreAsRenameGrouppedWithNo()
  58. {
  59. TQueryButtonAlias Result;
  60. Result.Button = qaIgnore;
  61. Result.Alias = LoadStr(RENAME_BUTTON);
  62. Result.GroupWith = qaNo;
  63. Result.GrouppedShiftState = TShiftState() << ssCtrl;
  64. return Result;
  65. }
  66. //---------------------------------------------------------------------------
  67. TQueryParams::TQueryParams(unsigned int AParams, UnicodeString AHelpKeyword)
  68. {
  69. Params = AParams;
  70. Aliases = NULL;
  71. AliasesCount = 0;
  72. Timer = 0;
  73. TimerEvent = NULL;
  74. TimerMessage = L"";
  75. TimerAnswers = 0;
  76. TimerQueryType = static_cast<TQueryType>(-1);
  77. Timeout = 0;
  78. TimeoutAnswer = 0;
  79. TimeoutResponse = 0;
  80. NoBatchAnswers = 0;
  81. HelpKeyword = AHelpKeyword;
  82. }
  83. //---------------------------------------------------------------------------
  84. TQueryParams::TQueryParams(const TQueryParams & Source)
  85. {
  86. Assign(Source);
  87. }
  88. //---------------------------------------------------------------------------
  89. void TQueryParams::Assign(const TQueryParams & Source)
  90. {
  91. *this = Source;
  92. }
  93. //---------------------------------------------------------------------------
  94. bool __fastcall IsAuthenticationPrompt(TPromptKind Kind)
  95. {
  96. return
  97. (Kind == pkUserName) || (Kind == pkPassphrase) || (Kind == pkTIS) ||
  98. (Kind == pkCryptoCard) || (Kind == pkKeybInteractive) ||
  99. (Kind == pkPassword) || (Kind == pkNewPassword);
  100. }
  101. //---------------------------------------------------------------------------
  102. bool __fastcall IsPasswordOrPassphrasePrompt(TPromptKind Kind, TStrings * Prompts)
  103. {
  104. return
  105. (Prompts->Count == 1) && FLAGCLEAR(int(Prompts->Objects[0]), pupEcho) &&
  106. ((Kind == pkPassword) || (Kind == pkPassphrase) || (Kind == pkKeybInteractive) ||
  107. (Kind == pkTIS) || (Kind == pkCryptoCard));
  108. }
  109. //---------------------------------------------------------------------------
  110. bool __fastcall IsPasswordPrompt(TPromptKind Kind, TStrings * Prompts)
  111. {
  112. return
  113. IsPasswordOrPassphrasePrompt(Kind, Prompts) &&
  114. (Kind != pkPassphrase);
  115. }
  116. //---------------------------------------------------------------------------
  117. void CoreLoad()
  118. {
  119. bool SessionList = true;
  120. std::unique_ptr<THierarchicalStorage> SessionsStorage(Configuration->CreateScpStorage(SessionList));
  121. THierarchicalStorage * ConfigStorage;
  122. std::unique_ptr<THierarchicalStorage> ConfigStorageAuto;
  123. if (!SessionList)
  124. {
  125. // can reuse this for configuration
  126. ConfigStorage = SessionsStorage.get();
  127. }
  128. else
  129. {
  130. ConfigStorageAuto.reset(Configuration->CreateConfigStorage());
  131. ConfigStorage = ConfigStorageAuto.get();
  132. }
  133. try
  134. {
  135. Configuration->Load(ConfigStorage);
  136. }
  137. catch (Exception & E)
  138. {
  139. ShowExtendedException(&E);
  140. }
  141. // should be noop, unless exception occured above
  142. ConfigStorage->CloseAll();
  143. StoredSessions = new TStoredSessionList();
  144. try
  145. {
  146. if (SessionsStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  147. {
  148. StoredSessions->Load(SessionsStorage.get());
  149. }
  150. }
  151. catch (Exception & E)
  152. {
  153. ShowExtendedException(&E);
  154. }
  155. }
  156. //---------------------------------------------------------------------------
  157. void CoreInitialize()
  158. {
  159. Randomize();
  160. CryptographyInitialize();
  161. // we do not expect configuration re-creation
  162. DebugAssert(Configuration == NULL);
  163. // configuration needs to be created and loaded before putty is initialized,
  164. // so that random seed path is known
  165. Configuration = CreateConfiguration();
  166. PuttyInitialize();
  167. TFileZillaIntf::Initialize();
  168. // TlsCipherList() also relies on this to be called
  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. //---------------------------------------------------------------------------