CoreMain.cpp 7.4 KB

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