CoreMain.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. TimeoutResponse = 0;
  79. NoBatchAnswers = 0;
  80. HelpKeyword = AHelpKeyword;
  81. }
  82. //---------------------------------------------------------------------------
  83. TQueryParams::TQueryParams(const TQueryParams & Source)
  84. {
  85. Assign(Source);
  86. }
  87. //---------------------------------------------------------------------------
  88. void TQueryParams::Assign(const TQueryParams & Source)
  89. {
  90. *this = Source;
  91. }
  92. //---------------------------------------------------------------------------
  93. bool __fastcall IsAuthenticationPrompt(TPromptKind Kind)
  94. {
  95. return
  96. (Kind == pkUserName) || (Kind == pkPassphrase) || (Kind == pkTIS) ||
  97. (Kind == pkCryptoCard) || (Kind == pkKeybInteractive) ||
  98. (Kind == pkPassword) || (Kind == pkNewPassword);
  99. }
  100. //---------------------------------------------------------------------------
  101. bool __fastcall IsPasswordOrPassphrasePrompt(TPromptKind Kind, TStrings * Prompts)
  102. {
  103. return
  104. (Prompts->Count == 1) && FLAGCLEAR(int(Prompts->Objects[0]), pupEcho) &&
  105. ((Kind == pkPassword) || (Kind == pkPassphrase) || (Kind == pkKeybInteractive) ||
  106. (Kind == pkTIS) || (Kind == pkCryptoCard));
  107. }
  108. //---------------------------------------------------------------------------
  109. bool __fastcall IsPasswordPrompt(TPromptKind Kind, TStrings * Prompts)
  110. {
  111. return
  112. IsPasswordOrPassphrasePrompt(Kind, Prompts) &&
  113. (Kind != pkPassphrase);
  114. }
  115. //---------------------------------------------------------------------------
  116. void CoreLoad()
  117. {
  118. bool SessionList = true;
  119. std::unique_ptr<THierarchicalStorage> SessionsStorage(Configuration->CreateScpStorage(SessionList));
  120. THierarchicalStorage * ConfigStorage;
  121. std::unique_ptr<THierarchicalStorage> ConfigStorageAuto;
  122. if (!SessionList)
  123. {
  124. // can reuse this for configuration
  125. ConfigStorage = SessionsStorage.get();
  126. }
  127. else
  128. {
  129. ConfigStorageAuto.reset(Configuration->CreateConfigStorage());
  130. ConfigStorage = ConfigStorageAuto.get();
  131. }
  132. try
  133. {
  134. Configuration->Load(ConfigStorage);
  135. }
  136. catch (Exception & E)
  137. {
  138. ShowExtendedException(&E);
  139. }
  140. // should be noop, unless exception occured above
  141. ConfigStorage->CloseAll();
  142. StoredSessions = new TStoredSessionList();
  143. try
  144. {
  145. if (SessionsStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  146. {
  147. StoredSessions->Load(SessionsStorage.get());
  148. }
  149. }
  150. catch (Exception & E)
  151. {
  152. ShowExtendedException(&E);
  153. }
  154. }
  155. //---------------------------------------------------------------------------
  156. void CoreInitialize()
  157. {
  158. Randomize();
  159. CryptographyInitialize();
  160. // we do not expect configuration re-creation
  161. DebugAssert(Configuration == NULL);
  162. // configuration needs to be created and loaded before putty is initialized,
  163. // so that random seed path is known
  164. Configuration = CreateConfiguration();
  165. PuttyInitialize();
  166. TFileZillaIntf::Initialize();
  167. // TlsCipherList() also relies on this to be called
  168. NeonInitialize();
  169. CoreLoad();
  170. }
  171. //---------------------------------------------------------------------------
  172. void CoreFinalize()
  173. {
  174. try
  175. {
  176. Configuration->Save();
  177. }
  178. catch(Exception & E)
  179. {
  180. ShowExtendedException(&E);
  181. }
  182. NeonFinalize();
  183. TFileZillaIntf::Finalize();
  184. PuttyFinalize();
  185. delete StoredSessions;
  186. StoredSessions = NULL;
  187. delete Configuration;
  188. Configuration = NULL;
  189. CryptographyFinalize();
  190. }
  191. //---------------------------------------------------------------------------
  192. void CoreSetResourceModule(void * ResourceHandle)
  193. {
  194. TFileZillaIntf::SetResourceModule(ResourceHandle);
  195. }
  196. //---------------------------------------------------------------------------
  197. void CoreMaintenanceTask()
  198. {
  199. DontSaveRandomSeed();
  200. }
  201. //---------------------------------------------------------------------------
  202. //---------------------------------------------------------------------------
  203. __fastcall TOperationVisualizer::TOperationVisualizer(bool UseBusyCursor) :
  204. FUseBusyCursor(UseBusyCursor)
  205. {
  206. if (FUseBusyCursor)
  207. {
  208. FToken = BusyStart();
  209. }
  210. }
  211. //---------------------------------------------------------------------------
  212. __fastcall TOperationVisualizer::~TOperationVisualizer()
  213. {
  214. if (FUseBusyCursor)
  215. {
  216. BusyEnd(FToken);
  217. }
  218. }
  219. //---------------------------------------------------------------------------
  220. //---------------------------------------------------------------------------
  221. __fastcall TInstantOperationVisualizer::TInstantOperationVisualizer() :
  222. FStart(Now())
  223. {
  224. }
  225. //---------------------------------------------------------------------------
  226. __fastcall TInstantOperationVisualizer::~TInstantOperationVisualizer()
  227. {
  228. TDateTime Time = Now();
  229. __int64 Duration = MilliSecondsBetween(Time, FStart);
  230. const __int64 MinDuration = 250;
  231. if (Duration < MinDuration)
  232. {
  233. Sleep(static_cast<unsigned int>(MinDuration - Duration));
  234. }
  235. }
  236. //---------------------------------------------------------------------------