1
0

CoreMain.cpp 6.2 KB

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