1
0

CoreMain.cpp 7.5 KB

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