Configuration.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <FileInfo.h>
  5. #include "Common.h"
  6. #include "Exceptions.h"
  7. #include "Configuration.h"
  8. #include "PuttyIntf.h"
  9. #include "TextsCore.h"
  10. #include "Interface.h"
  11. #include "CoreMain.h"
  12. #include "Security.h"
  13. #include <shlobj.h>
  14. #include <System.IOUtils.hpp>
  15. #include <System.StrUtils.hpp>
  16. //---------------------------------------------------------------------------
  17. #pragma package(smart_init)
  18. //---------------------------------------------------------------------------
  19. const wchar_t * AutoSwitchNames = L"On;Off;Auto";
  20. const wchar_t * NotAutoSwitchNames = L"Off;On;Auto";
  21. //---------------------------------------------------------------------------
  22. // See http://www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xhtml
  23. const UnicodeString Sha1ChecksumAlg(L"sha-1");
  24. const UnicodeString Sha224ChecksumAlg(L"sha-224");
  25. const UnicodeString Sha256ChecksumAlg(L"sha-256");
  26. const UnicodeString Sha384ChecksumAlg(L"sha-384");
  27. const UnicodeString Sha512ChecksumAlg(L"sha-512");
  28. const UnicodeString Md5ChecksumAlg(L"md5");
  29. // Not defined by IANA
  30. const UnicodeString Crc32ChecksumAlg(L"crc32");
  31. //---------------------------------------------------------------------------
  32. const UnicodeString SshFingerprintType(L"ssh");
  33. const UnicodeString TlsFingerprintType(L"tls");
  34. //---------------------------------------------------------------------------
  35. __fastcall TConfiguration::TConfiguration()
  36. {
  37. FCriticalSection = new TCriticalSection();
  38. FUpdating = 0;
  39. FStorage = stDetect;
  40. FDontSave = false;
  41. FApplicationInfo = NULL;
  42. FUsage = new TUsage(this);
  43. FDefaultCollectUsage = false;
  44. FScripting = false;
  45. UnicodeString RandomSeedPath;
  46. if (!GetEnvironmentVariable(L"APPDATA").IsEmpty())
  47. {
  48. RandomSeedPath = L"%APPDATA%";
  49. }
  50. else
  51. {
  52. RandomSeedPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  53. if (RandomSeedPath.IsEmpty())
  54. {
  55. RandomSeedPath = GetShellFolderPath(CSIDL_APPDATA);
  56. }
  57. }
  58. FDefaultRandomSeedFile = IncludeTrailingBackslash(RandomSeedPath) + L"winscp.rnd";
  59. }
  60. //---------------------------------------------------------------------------
  61. void __fastcall TConfiguration::Default()
  62. {
  63. TGuard Guard(FCriticalSection);
  64. FDisablePasswordStoring = false;
  65. FForceBanners = false;
  66. FDisableAcceptingHostKeys = false;
  67. TRegistryStorage * AdminStorage;
  68. AdminStorage = new TRegistryStorage(RegistryStorageKey, HKEY_LOCAL_MACHINE);
  69. try
  70. {
  71. if (AdminStorage->OpenRootKey(false))
  72. {
  73. LoadAdmin(AdminStorage);
  74. AdminStorage->CloseSubKey();
  75. }
  76. }
  77. __finally
  78. {
  79. delete AdminStorage;
  80. }
  81. RandomSeedFile = FDefaultRandomSeedFile;
  82. PuttyRegistryStorageKey = OriginalPuttyRegistryStorageKey;
  83. FConfirmOverwriting = true;
  84. FConfirmResume = true;
  85. FAutoReadDirectoryAfterOp = true;
  86. FSessionReopenAuto = 5000;
  87. FSessionReopenBackground = 2000;
  88. FSessionReopenTimeout = 0;
  89. FSessionReopenAutoStall = 60000;
  90. FTunnelLocalPortNumberLow = 50000;
  91. FTunnelLocalPortNumberHigh = 50099;
  92. FCacheDirectoryChangesMaxSize = 100;
  93. FShowFtpWelcomeMessage = false;
  94. FExternalIpAddress = L"";
  95. FTryFtpWhenSshFails = true;
  96. CollectUsage = FDefaultCollectUsage;
  97. FLogging = false;
  98. FPermanentLogging = false;
  99. FLogFileName = DefaultLogFileName;
  100. FPermanentLogFileName = FLogFileName;
  101. FLogFileAppend = true;
  102. FLogSensitive = false;
  103. FPermanentLogSensitive = FLogSensitive;
  104. FLogWindowLines = 100;
  105. FLogProtocol = 0;
  106. FPermanentLogProtocol = FLogProtocol;
  107. UpdateActualLogProtocol();
  108. FLogActions = false;
  109. FPermanentLogActions = false;
  110. FActionsLogFileName = L"%TEMP%\\!S.xml";
  111. FPermanentActionsLogFileName = FActionsLogFileName;
  112. FProgramIniPathWrittable = -1;
  113. Changed();
  114. }
  115. //---------------------------------------------------------------------------
  116. __fastcall TConfiguration::~TConfiguration()
  117. {
  118. DebugAssert(!FUpdating);
  119. if (FApplicationInfo) FreeFileInfo(FApplicationInfo);
  120. delete FCriticalSection;
  121. delete FUsage;
  122. }
  123. //---------------------------------------------------------------------------
  124. void __fastcall TConfiguration::UpdateStaticUsage()
  125. {
  126. Usage->Set(L"ConfigurationIniFile", (Storage == stIniFile));
  127. Usage->Set("Unofficial", IsUnofficial);
  128. // this is called from here, because we are guarded from calling into
  129. // master password handler here, see TWinConfiguration::UpdateStaticUsage
  130. StoredSessions->UpdateStaticUsage();
  131. }
  132. //---------------------------------------------------------------------------
  133. THierarchicalStorage * TConfiguration::CreateConfigStorage()
  134. {
  135. bool SessionList = false;
  136. return CreateScpStorage(SessionList);
  137. }
  138. //---------------------------------------------------------------------------
  139. THierarchicalStorage * TConfiguration::CreateScpStorage(bool & SessionList)
  140. {
  141. THierarchicalStorage * Result;
  142. if (Storage == stRegistry)
  143. {
  144. Result = new TRegistryStorage(RegistryStorageKey);
  145. }
  146. else if (Storage == stNul)
  147. {
  148. Result = new TIniFileStorage(INI_NUL);
  149. }
  150. else
  151. {
  152. Result = new TIniFileStorage(IniFileStorageName);
  153. }
  154. if ((FOptionsStorage.get() != NULL) && (FOptionsStorage->Count > 0))
  155. {
  156. if (!SessionList)
  157. {
  158. Result = new TOptionsStorage(FOptionsStorage.get(), ConfigurationSubKey, Result);
  159. }
  160. else
  161. {
  162. // cannot reuse session list storage for configuration as for it we need
  163. // the option-override storage above
  164. }
  165. }
  166. else
  167. {
  168. // All the above stores can be reused for configuration,
  169. // if no options-overrides are set
  170. SessionList = false;
  171. }
  172. return Result;
  173. }
  174. //---------------------------------------------------------------------------
  175. UnicodeString __fastcall TConfiguration::PropertyToKey(const UnicodeString & Property)
  176. {
  177. // no longer useful
  178. int P = Property.LastDelimiter(L".>");
  179. return Property.SubString(P + 1, Property.Length() - P);
  180. }
  181. //---------------------------------------------------------------------------
  182. #define BLOCK(KEY, CANCREATE, BLOCK) \
  183. if (Storage->OpenSubKey(KEY, CANCREATE, true)) try { BLOCK } __finally { Storage->CloseSubKey(); }
  184. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  185. #define REGCONFIG(CANCREATE) \
  186. BLOCK(L"Interface", CANCREATE, \
  187. KEY(String, RandomSeedFile); \
  188. KEY(String, PuttyRegistryStorageKey); \
  189. KEY(Bool, ConfirmOverwriting); \
  190. KEY(Bool, ConfirmResume); \
  191. KEY(Bool, AutoReadDirectoryAfterOp); \
  192. KEY(Integer, SessionReopenAuto); \
  193. KEY(Integer, SessionReopenBackground); \
  194. KEY(Integer, SessionReopenTimeout); \
  195. KEY(Integer, SessionReopenAutoStall); \
  196. KEY(Integer, TunnelLocalPortNumberLow); \
  197. KEY(Integer, TunnelLocalPortNumberHigh); \
  198. KEY(Integer, CacheDirectoryChangesMaxSize); \
  199. KEY(Bool, ShowFtpWelcomeMessage); \
  200. KEY(String, ExternalIpAddress); \
  201. KEY(Bool, TryFtpWhenSshFails); \
  202. KEY(Bool, CollectUsage); \
  203. ); \
  204. BLOCK(L"Logging", CANCREATE, \
  205. KEYEX(Bool, PermanentLogging, L"Logging"); \
  206. KEYEX(String,PermanentLogFileName, L"LogFileName"); \
  207. KEY(Bool, LogFileAppend); \
  208. KEYEX(Bool, PermanentLogSensitive, L"LogSensitive"); \
  209. KEY(Integer, LogWindowLines); \
  210. KEYEX(Integer,PermanentLogProtocol, L"LogProtocol"); \
  211. KEYEX(Bool, PermanentLogActions, L"LogActions"); \
  212. KEYEX(String,PermanentActionsLogFileName, L"ActionsLogFileName"); \
  213. );
  214. //---------------------------------------------------------------------------
  215. void __fastcall TConfiguration::SaveData(THierarchicalStorage * Storage, bool /*All*/)
  216. {
  217. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  218. REGCONFIG(true);
  219. #undef KEYEX
  220. if (Storage->OpenSubKey(L"Usage", true))
  221. {
  222. FUsage->Save(Storage);
  223. Storage->CloseSubKey();
  224. }
  225. }
  226. //---------------------------------------------------------------------------
  227. void __fastcall TConfiguration::Save()
  228. {
  229. // only modified, implicit
  230. DoSave(false, false);
  231. }
  232. //---------------------------------------------------------------------------
  233. void __fastcall TConfiguration::SaveExplicit()
  234. {
  235. // only modified, explicit
  236. DoSave(false, true);
  237. }
  238. //---------------------------------------------------------------------------
  239. void __fastcall TConfiguration::DoSave(bool All, bool Explicit)
  240. {
  241. if (FDontSave) return;
  242. THierarchicalStorage * AStorage = CreateConfigStorage();
  243. try
  244. {
  245. AStorage->AccessMode = smReadWrite;
  246. AStorage->Explicit = Explicit;
  247. if (AStorage->OpenSubKey(ConfigurationSubKey, true))
  248. {
  249. // if saving to TOptionsStorage, make sure we save everything so that
  250. // all configuration is properly transferred to the master storage
  251. bool ConfigAll = All || AStorage->Temporary;
  252. SaveData(AStorage, ConfigAll);
  253. }
  254. }
  255. __finally
  256. {
  257. delete AStorage;
  258. }
  259. Saved();
  260. if (All)
  261. {
  262. StoredSessions->Save(true, Explicit);
  263. }
  264. // clean up as last, so that if it fails (read only INI), the saving can proceed
  265. if (Storage == stRegistry)
  266. {
  267. CleanupIniFile();
  268. }
  269. }
  270. //---------------------------------------------------------------------------
  271. void __fastcall TConfiguration::Export(const UnicodeString & FileName)
  272. {
  273. // not to "append" the export to an existing file
  274. if (FileExists(FileName))
  275. {
  276. DeleteFileChecked(FileName);
  277. }
  278. THierarchicalStorage * Storage = NULL;
  279. THierarchicalStorage * ExportStorage = NULL;
  280. try
  281. {
  282. ExportStorage = new TIniFileStorage(FileName);
  283. ExportStorage->AccessMode = smReadWrite;
  284. ExportStorage->Explicit = true;
  285. Storage = CreateConfigStorage();
  286. Storage->AccessMode = smRead;
  287. CopyData(Storage, ExportStorage);
  288. if (ExportStorage->OpenSubKey(ConfigurationSubKey, true))
  289. {
  290. SaveData(ExportStorage, true);
  291. }
  292. }
  293. __finally
  294. {
  295. delete ExportStorage;
  296. delete Storage;
  297. }
  298. StoredSessions->Export(FileName);
  299. }
  300. //---------------------------------------------------------------------------
  301. void __fastcall TConfiguration::Import(const UnicodeString & FileName)
  302. {
  303. THierarchicalStorage * Storage = NULL;
  304. THierarchicalStorage * ImportStorage = NULL;
  305. try
  306. {
  307. ImportStorage = new TIniFileStorage(FileName);
  308. ImportStorage->AccessMode = smRead;
  309. Storage = CreateConfigStorage();
  310. Storage->AccessMode = smReadWrite;
  311. Storage->Explicit = true;
  312. CopyData(ImportStorage, Storage);
  313. Default();
  314. LoadFrom(ImportStorage);
  315. if (ImportStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  316. {
  317. StoredSessions->Clear();
  318. StoredSessions->DefaultSettings->Default();
  319. StoredSessions->Load(ImportStorage);
  320. }
  321. }
  322. __finally
  323. {
  324. delete ImportStorage;
  325. delete Storage;
  326. }
  327. // save all and explicit
  328. DoSave(true, true);
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TConfiguration::LoadData(THierarchicalStorage * Storage)
  332. {
  333. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(NAME, VAR)
  334. #pragma warn -eas
  335. REGCONFIG(false);
  336. #pragma warn +eas
  337. #undef KEYEX
  338. if (Storage->OpenSubKey(L"Usage", false))
  339. {
  340. FUsage->Load(Storage);
  341. Storage->CloseSubKey();
  342. }
  343. if (FPermanentLogActions && FPermanentActionsLogFileName.IsEmpty() &&
  344. FPermanentLogging && !FPermanentLogFileName.IsEmpty())
  345. {
  346. FPermanentActionsLogFileName = FPermanentLogFileName;
  347. FPermanentLogging = false;
  348. FPermanentLogFileName = L"";
  349. }
  350. }
  351. //---------------------------------------------------------------------------
  352. void __fastcall TConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  353. {
  354. FDisablePasswordStoring = Storage->ReadBool(L"DisablePasswordStoring", FDisablePasswordStoring);
  355. FForceBanners = Storage->ReadBool(L"ForceBanners", FForceBanners);
  356. FDisableAcceptingHostKeys = Storage->ReadBool(L"DisableAcceptingHostKeys", FDisableAcceptingHostKeys);
  357. FDefaultCollectUsage = Storage->ReadBool(L"DefaultCollectUsage", FDefaultCollectUsage);
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TConfiguration::LoadFrom(THierarchicalStorage * Storage)
  361. {
  362. if (Storage->OpenSubKey(ConfigurationSubKey, false))
  363. {
  364. LoadData(Storage);
  365. Storage->CloseSubKey();
  366. }
  367. }
  368. //---------------------------------------------------------------------------
  369. void __fastcall TConfiguration::Load(THierarchicalStorage * Storage)
  370. {
  371. TGuard Guard(FCriticalSection);
  372. TStorageAccessMode StorageAccessMode = Storage->AccessMode;
  373. try
  374. {
  375. Storage->AccessMode = smRead;
  376. LoadFrom(Storage);
  377. }
  378. __finally
  379. {
  380. Storage->AccessMode = StorageAccessMode;
  381. }
  382. }
  383. //---------------------------------------------------------------------------
  384. void __fastcall TConfiguration::CopyData(THierarchicalStorage * Source,
  385. THierarchicalStorage * Target)
  386. {
  387. TStrings * Names = new TStringList();
  388. try
  389. {
  390. if (Source->OpenSubKey(ConfigurationSubKey, false))
  391. {
  392. if (Target->OpenSubKey(ConfigurationSubKey, true))
  393. {
  394. if (Source->OpenSubKey(L"CDCache", false))
  395. {
  396. if (Target->OpenSubKey(L"CDCache", true))
  397. {
  398. Names->Clear();
  399. Source->GetValueNames(Names);
  400. for (int Index = 0; Index < Names->Count; Index++)
  401. {
  402. Target->WriteBinaryData(Names->Strings[Index],
  403. Source->ReadBinaryData(Names->Strings[Index]));
  404. }
  405. Target->CloseSubKey();
  406. }
  407. Source->CloseSubKey();
  408. }
  409. if (Source->OpenSubKey(L"Banners", false))
  410. {
  411. if (Target->OpenSubKey(L"Banners", true))
  412. {
  413. Names->Clear();
  414. Source->GetValueNames(Names);
  415. for (int Index = 0; Index < Names->Count; Index++)
  416. {
  417. Target->WriteString(Names->Strings[Index],
  418. Source->ReadString(Names->Strings[Index], L""));
  419. }
  420. Target->CloseSubKey();
  421. }
  422. Source->CloseSubKey();
  423. }
  424. Target->CloseSubKey();
  425. }
  426. Source->CloseSubKey();
  427. }
  428. if (Source->OpenSubKey(SshHostKeysSubKey, false))
  429. {
  430. if (Target->OpenSubKey(SshHostKeysSubKey, true))
  431. {
  432. Names->Clear();
  433. Source->GetValueNames(Names);
  434. for (int Index = 0; Index < Names->Count; Index++)
  435. {
  436. Target->WriteStringRaw(Names->Strings[Index],
  437. Source->ReadStringRaw(Names->Strings[Index], L""));
  438. }
  439. Target->CloseSubKey();
  440. }
  441. Source->CloseSubKey();
  442. }
  443. }
  444. __finally
  445. {
  446. delete Names;
  447. }
  448. }
  449. //---------------------------------------------------------------------------
  450. void __fastcall TConfiguration::LoadDirectoryChangesCache(const UnicodeString SessionKey,
  451. TRemoteDirectoryChangesCache * DirectoryChangesCache)
  452. {
  453. THierarchicalStorage * Storage = CreateConfigStorage();
  454. try
  455. {
  456. Storage->AccessMode = smRead;
  457. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  458. Storage->OpenSubKey(L"CDCache", false) &&
  459. Storage->ValueExists(SessionKey))
  460. {
  461. DirectoryChangesCache->Deserialize(Storage->ReadBinaryData(SessionKey));
  462. }
  463. }
  464. __finally
  465. {
  466. delete Storage;
  467. }
  468. }
  469. //---------------------------------------------------------------------------
  470. void __fastcall TConfiguration::SaveDirectoryChangesCache(const UnicodeString SessionKey,
  471. TRemoteDirectoryChangesCache * DirectoryChangesCache)
  472. {
  473. THierarchicalStorage * Storage = CreateConfigStorage();
  474. try
  475. {
  476. Storage->AccessMode = smReadWrite;
  477. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  478. Storage->OpenSubKey(L"CDCache", true))
  479. {
  480. UnicodeString Data;
  481. DirectoryChangesCache->Serialize(Data);
  482. Storage->WriteBinaryData(SessionKey, Data);
  483. }
  484. }
  485. __finally
  486. {
  487. delete Storage;
  488. }
  489. }
  490. //---------------------------------------------------------------------------
  491. UnicodeString __fastcall TConfiguration::BannerHash(const UnicodeString & Banner)
  492. {
  493. RawByteString Result;
  494. Result.SetLength(16);
  495. md5checksum(
  496. reinterpret_cast<const char*>(Banner.c_str()), Banner.Length() * sizeof(wchar_t),
  497. (unsigned char*)Result.c_str());
  498. return BytesToHex(Result);
  499. }
  500. //---------------------------------------------------------------------------
  501. bool __fastcall TConfiguration::ShowBanner(const UnicodeString SessionKey,
  502. const UnicodeString & Banner)
  503. {
  504. bool Result;
  505. THierarchicalStorage * Storage = CreateConfigStorage();
  506. try
  507. {
  508. Storage->AccessMode = smRead;
  509. Result =
  510. !Storage->OpenSubKey(ConfigurationSubKey, false) ||
  511. !Storage->OpenSubKey(L"Banners", false) ||
  512. !Storage->ValueExists(SessionKey) ||
  513. (Storage->ReadString(SessionKey, L"") != BannerHash(Banner));
  514. }
  515. __finally
  516. {
  517. delete Storage;
  518. }
  519. return Result;
  520. }
  521. //---------------------------------------------------------------------------
  522. void __fastcall TConfiguration::NeverShowBanner(const UnicodeString SessionKey,
  523. const UnicodeString & Banner)
  524. {
  525. THierarchicalStorage * Storage = CreateConfigStorage();
  526. try
  527. {
  528. Storage->AccessMode = smReadWrite;
  529. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  530. Storage->OpenSubKey(L"Banners", true))
  531. {
  532. Storage->WriteString(SessionKey, BannerHash(Banner));
  533. }
  534. }
  535. __finally
  536. {
  537. delete Storage;
  538. }
  539. }
  540. //---------------------------------------------------------------------------
  541. UnicodeString __fastcall TConfiguration::FormatFingerprintKey(const UnicodeString & SiteKey, const UnicodeString & FingerprintType)
  542. {
  543. return FORMAT(L"%s:%s", (SiteKey, FingerprintType));
  544. }
  545. //---------------------------------------------------------------------------
  546. void __fastcall TConfiguration::RememberLastFingerprint(const UnicodeString & SiteKey, const UnicodeString & FingerprintType, const UnicodeString & Fingerprint)
  547. {
  548. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  549. Storage->AccessMode = smReadWrite;
  550. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  551. Storage->OpenSubKey(L"LastFingerprints", true))
  552. {
  553. UnicodeString FingerprintKey = FormatFingerprintKey(SiteKey, FingerprintType);
  554. Storage->WriteString(FingerprintKey, Fingerprint);
  555. }
  556. }
  557. //---------------------------------------------------------------------------
  558. UnicodeString __fastcall TConfiguration::LastFingerprint(const UnicodeString & SiteKey, const UnicodeString & FingerprintType)
  559. {
  560. UnicodeString Result;
  561. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  562. Storage->AccessMode = smRead;
  563. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  564. Storage->OpenSubKey(L"LastFingerprints", false))
  565. {
  566. UnicodeString FingerprintKey = FormatFingerprintKey(SiteKey, FingerprintType);
  567. Result = Storage->ReadString(FingerprintKey, L"");
  568. }
  569. return Result;
  570. }
  571. //---------------------------------------------------------------------------
  572. void __fastcall TConfiguration::Changed()
  573. {
  574. if (FUpdating == 0)
  575. {
  576. if (OnChange)
  577. {
  578. OnChange(this);
  579. }
  580. }
  581. else
  582. {
  583. FChanged = true;
  584. }
  585. }
  586. //---------------------------------------------------------------------------
  587. void __fastcall TConfiguration::BeginUpdate()
  588. {
  589. if (FUpdating == 0)
  590. {
  591. FChanged = false;
  592. }
  593. FUpdating++;
  594. // Greater value would probably indicate some nesting problem in code
  595. DebugAssert(FUpdating < 6);
  596. }
  597. //---------------------------------------------------------------------------
  598. void __fastcall TConfiguration::EndUpdate()
  599. {
  600. DebugAssert(FUpdating > 0);
  601. FUpdating--;
  602. if ((FUpdating == 0) && FChanged)
  603. {
  604. FChanged = false;
  605. Changed();
  606. }
  607. }
  608. //---------------------------------------------------------------------------
  609. void __fastcall TConfiguration::CleanupConfiguration()
  610. {
  611. try
  612. {
  613. CleanupRegistry(ConfigurationSubKey);
  614. if (Storage == stRegistry)
  615. {
  616. FDontSave = true;
  617. }
  618. }
  619. catch (Exception &E)
  620. {
  621. throw ExtException(&E, LoadStr(CLEANUP_CONFIG_ERROR));
  622. }
  623. }
  624. //---------------------------------------------------------------------------
  625. void __fastcall TConfiguration::CleanupRegistry(UnicodeString CleanupSubKey)
  626. {
  627. TRegistryStorage *Registry = new TRegistryStorage(RegistryStorageKey);
  628. try
  629. {
  630. Registry->RecursiveDeleteSubKey(CleanupSubKey);
  631. }
  632. __finally
  633. {
  634. delete Registry;
  635. }
  636. }
  637. //---------------------------------------------------------------------------
  638. void __fastcall TConfiguration::CleanupHostKeys()
  639. {
  640. try
  641. {
  642. CleanupRegistry(SshHostKeysSubKey);
  643. }
  644. catch (Exception &E)
  645. {
  646. throw ExtException(&E, LoadStr(CLEANUP_HOSTKEYS_ERROR));
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall TConfiguration::CleanupRandomSeedFile()
  651. {
  652. try
  653. {
  654. DontSaveRandomSeed();
  655. if (FileExists(ApiPath(RandomSeedFileName)))
  656. {
  657. DeleteFileChecked(RandomSeedFileName);
  658. }
  659. }
  660. catch (Exception &E)
  661. {
  662. throw ExtException(&E, LoadStr(CLEANUP_SEEDFILE_ERROR));
  663. }
  664. }
  665. //---------------------------------------------------------------------------
  666. void __fastcall TConfiguration::CleanupIniFile()
  667. {
  668. try
  669. {
  670. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  671. {
  672. DeleteFileChecked(IniFileStorageNameForReading);
  673. }
  674. if (Storage == stIniFile)
  675. {
  676. FDontSave = true;
  677. }
  678. }
  679. catch (Exception &E)
  680. {
  681. throw ExtException(&E, LoadStr(CLEANUP_INIFILE_ERROR));
  682. }
  683. }
  684. //---------------------------------------------------------------------------
  685. void __fastcall TConfiguration::DontSave()
  686. {
  687. FDontSave = true;
  688. }
  689. //---------------------------------------------------------------------------
  690. RawByteString __fastcall TConfiguration::EncryptPassword(UnicodeString Password, UnicodeString Key)
  691. {
  692. if (Password.IsEmpty())
  693. {
  694. return RawByteString();
  695. }
  696. else
  697. {
  698. return ::EncryptPassword(Password, Key);
  699. }
  700. }
  701. //---------------------------------------------------------------------------
  702. UnicodeString __fastcall TConfiguration::DecryptPassword(RawByteString Password, UnicodeString Key)
  703. {
  704. if (Password.IsEmpty())
  705. {
  706. return UnicodeString();
  707. }
  708. else
  709. {
  710. return ::DecryptPassword(Password, Key);
  711. }
  712. }
  713. //---------------------------------------------------------------------------
  714. RawByteString __fastcall TConfiguration::StronglyRecryptPassword(RawByteString Password, UnicodeString /*Key*/)
  715. {
  716. return Password;
  717. }
  718. //---------------------------------------------------------------------------
  719. TVSFixedFileInfo *__fastcall TConfiguration::GetFixedApplicationInfo()
  720. {
  721. return GetFixedFileInfo(ApplicationInfo);
  722. }
  723. //---------------------------------------------------------------------------
  724. int __fastcall TConfiguration::GetCompoundVersion()
  725. {
  726. TVSFixedFileInfo * FileInfo = FixedApplicationInfo;
  727. return CalculateCompoundVersion(
  728. HIWORD(FileInfo->dwFileVersionMS), LOWORD(FileInfo->dwFileVersionMS),
  729. HIWORD(FileInfo->dwFileVersionLS), LOWORD(FileInfo->dwFileVersionLS));
  730. }
  731. //---------------------------------------------------------------------------
  732. UnicodeString __fastcall TConfiguration::ModuleFileName()
  733. {
  734. return ParamStr(0);
  735. }
  736. //---------------------------------------------------------------------------
  737. void * __fastcall TConfiguration::GetFileApplicationInfo(const UnicodeString FileName)
  738. {
  739. void * Result;
  740. if (FileName.IsEmpty())
  741. {
  742. if (!FApplicationInfo)
  743. {
  744. FApplicationInfo = CreateFileInfo(ModuleFileName());
  745. }
  746. Result = FApplicationInfo;
  747. }
  748. else
  749. {
  750. Result = CreateFileInfo(FileName);
  751. }
  752. return Result;
  753. }
  754. //---------------------------------------------------------------------------
  755. void * __fastcall TConfiguration::GetApplicationInfo()
  756. {
  757. return GetFileApplicationInfo("");
  758. }
  759. //---------------------------------------------------------------------------
  760. UnicodeString __fastcall TConfiguration::GetFileProductName(const UnicodeString FileName)
  761. {
  762. return GetFileFileInfoString(L"ProductName", FileName);
  763. }
  764. //---------------------------------------------------------------------------
  765. UnicodeString __fastcall TConfiguration::GetFileCompanyName(const UnicodeString FileName)
  766. {
  767. // particularly in IDE build, company name is empty
  768. return GetFileFileInfoString(L"CompanyName", FileName, true);
  769. }
  770. //---------------------------------------------------------------------------
  771. UnicodeString __fastcall TConfiguration::GetProductName()
  772. {
  773. return GetFileProductName(L"");
  774. }
  775. //---------------------------------------------------------------------------
  776. UnicodeString __fastcall TConfiguration::GetCompanyName()
  777. {
  778. return GetFileCompanyName(L"");
  779. }
  780. //---------------------------------------------------------------------------
  781. UnicodeString __fastcall TConfiguration::GetFileProductVersion(const UnicodeString FileName)
  782. {
  783. return TrimVersion(GetFileFileInfoString(L"ProductVersion", FileName));
  784. }
  785. //---------------------------------------------------------------------------
  786. UnicodeString __fastcall TConfiguration::GetFileDescription(const UnicodeString & FileName)
  787. {
  788. return GetFileFileInfoString(L"FileDescription", FileName);
  789. }
  790. //---------------------------------------------------------------------------
  791. UnicodeString __fastcall TConfiguration::GetProductVersion()
  792. {
  793. return GetFileProductVersion(L"");
  794. }
  795. //---------------------------------------------------------------------------
  796. UnicodeString __fastcall TConfiguration::GetReleaseType()
  797. {
  798. return GetFileInfoString(L"ReleaseType");
  799. }
  800. //---------------------------------------------------------------------------
  801. bool __fastcall TConfiguration::GetIsUnofficial()
  802. {
  803. #ifdef BUILD_OFFICIAL
  804. return false;
  805. #else
  806. return true;
  807. #endif
  808. }
  809. //---------------------------------------------------------------------------
  810. UnicodeString __fastcall TConfiguration::GetVersionStr()
  811. {
  812. TGuard Guard(FCriticalSection);
  813. try
  814. {
  815. UnicodeString BuildStr;
  816. if (!IsUnofficial)
  817. {
  818. BuildStr = LoadStr(VERSION_BUILD);
  819. }
  820. else
  821. {
  822. #ifdef _DEBUG
  823. BuildStr = LoadStr(VERSION_DEBUG_BUILD);
  824. #else
  825. BuildStr = LoadStr(VERSION_DEV_BUILD);
  826. #endif
  827. }
  828. int Build = LOWORD(FixedApplicationInfo->dwFileVersionLS);
  829. if (Build > 0)
  830. {
  831. BuildStr += L" " + IntToStr(Build);
  832. }
  833. #ifndef BUILD_OFFICIAL
  834. UnicodeString BuildDate = __DATE__;
  835. UnicodeString MonthStr = CutToChar(BuildDate, L' ', true);
  836. int Month = ParseShortEngMonthName(MonthStr);
  837. int Day = StrToInt(CutToChar(BuildDate, L' ', true));
  838. int Year = StrToInt(Trim(BuildDate));
  839. UnicodeString DateStr = FORMAT(L"%d-%2.2d-%2.2d", (Year, Month, Day));
  840. AddToList(BuildStr, DateStr, L" ");
  841. #endif
  842. UnicodeString FullVersion = Version;
  843. UnicodeString AReleaseType = GetReleaseType();
  844. if (DebugAlwaysTrue(!AReleaseType.IsEmpty()) &&
  845. !SameText(AReleaseType, L"stable") &&
  846. !SameText(AReleaseType, L"development"))
  847. {
  848. FullVersion += L" " + AReleaseType;
  849. }
  850. UnicodeString Result = FMTLOAD(VERSION2, (FullVersion, BuildStr));
  851. #ifndef BUILD_OFFICIAL
  852. Result += L" " + LoadStr(VERSION_DONT_DISTRIBUTE);
  853. #endif
  854. return Result;
  855. }
  856. catch (Exception &E)
  857. {
  858. throw ExtException(&E, L"Can't get application version");
  859. }
  860. }
  861. //---------------------------------------------------------------------------
  862. UnicodeString __fastcall TConfiguration::GetFileVersion(const UnicodeString & FileName)
  863. {
  864. UnicodeString Result;
  865. void * FileInfo = CreateFileInfo(FileName);
  866. try
  867. {
  868. Result = GetFileVersion(GetFixedFileInfo(FileInfo));
  869. }
  870. __finally
  871. {
  872. FreeFileInfo(FileInfo);
  873. }
  874. return Result;
  875. }
  876. //---------------------------------------------------------------------------
  877. UnicodeString __fastcall TConfiguration::GetFileVersion(TVSFixedFileInfo * Info)
  878. {
  879. TGuard Guard(FCriticalSection);
  880. try
  881. {
  882. UnicodeString Result =
  883. FormatVersion(
  884. HIWORD(Info->dwFileVersionMS),
  885. LOWORD(Info->dwFileVersionMS),
  886. HIWORD(Info->dwFileVersionLS));
  887. return Result;
  888. }
  889. catch (Exception &E)
  890. {
  891. throw ExtException(&E, L"Can't get file version");
  892. }
  893. }
  894. //---------------------------------------------------------------------------
  895. UnicodeString __fastcall TConfiguration::GetVersion()
  896. {
  897. return GetFileVersion(FixedApplicationInfo);
  898. }
  899. //---------------------------------------------------------------------------
  900. UnicodeString __fastcall TConfiguration::GetFileFileInfoString(const UnicodeString Key,
  901. const UnicodeString FileName, bool AllowEmpty)
  902. {
  903. TGuard Guard(FCriticalSection);
  904. UnicodeString Result;
  905. void * Info = GetFileApplicationInfo(FileName);
  906. try
  907. {
  908. if ((Info != NULL) && (GetTranslationCount(Info) > 0))
  909. {
  910. TTranslation Translation;
  911. Translation = GetTranslation(Info, 0);
  912. Result = ::GetFileInfoString(Info, Translation, Key, AllowEmpty);
  913. }
  914. else
  915. {
  916. DebugAssert(!FileName.IsEmpty());
  917. }
  918. }
  919. __finally
  920. {
  921. if (!FileName.IsEmpty())
  922. {
  923. FreeFileInfo(Info);
  924. }
  925. }
  926. return Result;
  927. }
  928. //---------------------------------------------------------------------------
  929. UnicodeString __fastcall TConfiguration::GetFileInfoString(const UnicodeString Key)
  930. {
  931. return GetFileFileInfoString(Key, L"");
  932. }
  933. //---------------------------------------------------------------------------
  934. UnicodeString __fastcall TConfiguration::GetRegistryStorageKey()
  935. {
  936. return GetRegistryKey();
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TConfiguration::SetNulStorage()
  940. {
  941. FStorage = stNul;
  942. }
  943. //---------------------------------------------------------------------------
  944. void __fastcall TConfiguration::SetDefaultStorage()
  945. {
  946. FStorage = stDetect;
  947. }
  948. //---------------------------------------------------------------------------
  949. void __fastcall TConfiguration::SetIniFileStorageName(UnicodeString value)
  950. {
  951. FIniFileStorageName = value;
  952. FStorage = stIniFile;
  953. }
  954. //---------------------------------------------------------------------------
  955. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReading()
  956. {
  957. return GetIniFileStorageName(true);
  958. }
  959. //---------------------------------------------------------------------------
  960. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReadingWriting()
  961. {
  962. return GetIniFileStorageName(false);
  963. }
  964. //---------------------------------------------------------------------------
  965. UnicodeString __fastcall TConfiguration::GetIniFileStorageName(bool ReadingOnly)
  966. {
  967. if (FIniFileStorageName.IsEmpty())
  968. {
  969. UnicodeString ProgramPath = ParamStr(0);
  970. UnicodeString ProgramIniPath = ChangeFileExt(ProgramPath, L".ini");
  971. UnicodeString IniPath;
  972. if (FileExists(ApiPath(ProgramIniPath)))
  973. {
  974. IniPath = ProgramIniPath;
  975. }
  976. else
  977. {
  978. UnicodeString AppDataIniPath =
  979. IncludeTrailingBackslash(GetShellFolderPath(CSIDL_APPDATA)) +
  980. ExtractFileName(ProgramIniPath);
  981. if (FileExists(ApiPath(AppDataIniPath)))
  982. {
  983. IniPath = AppDataIniPath;
  984. }
  985. else
  986. {
  987. // avoid expensive test if we are interested in existing files only
  988. if (!ReadingOnly && (FProgramIniPathWrittable < 0))
  989. {
  990. UnicodeString ProgramDir = ExtractFilePath(ProgramPath);
  991. FProgramIniPathWrittable = IsDirectoryWriteable(ProgramDir) ? 1 : 0;
  992. }
  993. // does not really matter what we return when < 0
  994. IniPath = (FProgramIniPathWrittable == 0) ? AppDataIniPath : ProgramIniPath;
  995. }
  996. }
  997. // BACKWARD COMPATIBILITY with 4.x
  998. if (FVirtualIniFileStorageName.IsEmpty() &&
  999. TPath::IsDriveRooted(IniPath))
  1000. {
  1001. UnicodeString LocalAppDataPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  1002. // virtual store for non-system drives have a different virtual store,
  1003. // do not bother about them
  1004. if (TPath::IsDriveRooted(LocalAppDataPath) &&
  1005. SameText(ExtractFileDrive(IniPath), ExtractFileDrive(LocalAppDataPath)))
  1006. {
  1007. FVirtualIniFileStorageName =
  1008. IncludeTrailingBackslash(LocalAppDataPath) +
  1009. L"VirtualStore\\" +
  1010. IniPath.SubString(4, IniPath.Length() - 3);
  1011. }
  1012. }
  1013. if (!FVirtualIniFileStorageName.IsEmpty() &&
  1014. FileExists(ApiPath(FVirtualIniFileStorageName)))
  1015. {
  1016. return FVirtualIniFileStorageName;
  1017. }
  1018. else
  1019. {
  1020. return IniPath;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. return FIniFileStorageName;
  1026. }
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TConfiguration::SetOptionsStorage(TStrings * value)
  1030. {
  1031. if (FOptionsStorage.get() == NULL)
  1032. {
  1033. FOptionsStorage.reset(new TStringList());
  1034. }
  1035. FOptionsStorage->AddStrings(value);
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. TStrings * __fastcall TConfiguration::GetOptionsStorage()
  1039. {
  1040. return FOptionsStorage.get();
  1041. }
  1042. //---------------------------------------------------------------------------
  1043. UnicodeString __fastcall TConfiguration::GetPuttySessionsKey()
  1044. {
  1045. return PuttyRegistryStorageKey + L"\\Sessions";
  1046. }
  1047. //---------------------------------------------------------------------------
  1048. UnicodeString __fastcall TConfiguration::GetStoredSessionsSubKey()
  1049. {
  1050. return L"Sessions";
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. UnicodeString __fastcall TConfiguration::GetSshHostKeysSubKey()
  1054. {
  1055. return L"SshHostKeys";
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. UnicodeString __fastcall TConfiguration::GetConfigurationSubKey()
  1059. {
  1060. return L"Configuration";
  1061. }
  1062. //---------------------------------------------------------------------------
  1063. UnicodeString __fastcall TConfiguration::GetRootKeyStr()
  1064. {
  1065. return RootKeyToStr(HKEY_CURRENT_USER);
  1066. }
  1067. //---------------------------------------------------------------------------
  1068. void __fastcall TConfiguration::SetStorage(TStorage value)
  1069. {
  1070. if (FStorage != value)
  1071. {
  1072. TStorage StorageBak = FStorage;
  1073. try
  1074. {
  1075. THierarchicalStorage * SourceStorage = NULL;
  1076. THierarchicalStorage * TargetStorage = NULL;
  1077. try
  1078. {
  1079. SourceStorage = CreateConfigStorage();
  1080. SourceStorage->AccessMode = smRead;
  1081. FStorage = value;
  1082. TargetStorage = CreateConfigStorage();
  1083. TargetStorage->AccessMode = smReadWrite;
  1084. TargetStorage->Explicit = true;
  1085. // copy before save as it removes the ini file,
  1086. // when switching from ini to registry
  1087. CopyData(SourceStorage, TargetStorage);
  1088. }
  1089. __finally
  1090. {
  1091. delete SourceStorage;
  1092. delete TargetStorage;
  1093. }
  1094. // save all and explicit,
  1095. // this also removes an INI file, when switching to registry storage
  1096. DoSave(true, true);
  1097. }
  1098. catch (...)
  1099. {
  1100. // If this fails, do not pretend that storage was switched.
  1101. // For instance:
  1102. // - When writing to an INI file fails (unlikely, as we fallback to user profile)
  1103. // - When removing INI file fails, when switching to registry
  1104. // (possible, when the INI file is in Program Files folder)
  1105. FStorage = StorageBak;
  1106. throw;
  1107. }
  1108. }
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. void __fastcall TConfiguration::Saved()
  1112. {
  1113. // nothing
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. TStorage __fastcall TConfiguration::GetStorage()
  1117. {
  1118. if (FStorage == stDetect)
  1119. {
  1120. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  1121. {
  1122. FStorage = stIniFile;
  1123. }
  1124. else
  1125. {
  1126. FStorage = stRegistry;
  1127. }
  1128. }
  1129. return FStorage;
  1130. }
  1131. //---------------------------------------------------------------------
  1132. TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport(
  1133. TStoredSessionList * Sessions, UnicodeString & Error)
  1134. {
  1135. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1136. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1137. UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
  1138. UnicodeString FilezillaSiteManagerFile =
  1139. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\sitemanager.xml";
  1140. UnicodeString FilezillaConfigurationFile =
  1141. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\filezilla.xml";
  1142. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1143. {
  1144. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile, FilezillaConfigurationFile);
  1145. if (ImportSessionList->Count > 0)
  1146. {
  1147. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1148. }
  1149. else
  1150. {
  1151. Error = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1152. }
  1153. }
  1154. else
  1155. {
  1156. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1157. }
  1158. return ImportSessionList.release();
  1159. }
  1160. //---------------------------------------------------------------------
  1161. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1162. {
  1163. try
  1164. {
  1165. UnicodeString Error;
  1166. std::unique_ptr<TStoredSessionList> Sesssions(SelectFilezillaSessionsForImport(Sessions, Error));
  1167. return (Sesssions->Count > 0);
  1168. }
  1169. catch (...)
  1170. {
  1171. return false;
  1172. }
  1173. }
  1174. //---------------------------------------------------------------------------
  1175. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1176. {
  1177. if (RandomSeedFile != value)
  1178. {
  1179. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1180. FRandomSeedFile = value;
  1181. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1182. if (RandomSeedFileName.IsEmpty())
  1183. {
  1184. FRandomSeedFile = FDefaultRandomSeedFile;
  1185. }
  1186. if (!PrevRandomSeedFileName.IsEmpty() &&
  1187. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1188. FileExists(ApiPath(PrevRandomSeedFileName)))
  1189. {
  1190. // ignore any error
  1191. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1192. }
  1193. }
  1194. }
  1195. //---------------------------------------------------------------------
  1196. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1197. {
  1198. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1199. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1200. }
  1201. //---------------------------------------------------------------------
  1202. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1203. {
  1204. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1205. }
  1206. //---------------------------------------------------------------------
  1207. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1208. {
  1209. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1210. }
  1211. //---------------------------------------------------------------------
  1212. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1213. {
  1214. SET_CONFIG_PROPERTY(PuttyRegistryStorageKey);
  1215. }
  1216. //---------------------------------------------------------------------------
  1217. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1218. {
  1219. return eolCRLF;
  1220. }
  1221. //---------------------------------------------------------------------
  1222. bool __fastcall TConfiguration::GetCollectUsage()
  1223. {
  1224. return FUsage->Collect;
  1225. }
  1226. //---------------------------------------------------------------------
  1227. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1228. {
  1229. FUsage->Collect = value;
  1230. }
  1231. //---------------------------------------------------------------------
  1232. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1233. {
  1234. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1235. {
  1236. TemporaryActionsLogging(ALogFileName);
  1237. }
  1238. else
  1239. {
  1240. FLogging = true;
  1241. FLogFileName = ALogFileName;
  1242. UpdateActualLogProtocol();
  1243. }
  1244. }
  1245. //---------------------------------------------------------------------
  1246. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1247. {
  1248. FLogActions = true;
  1249. FActionsLogFileName = ALogFileName;
  1250. }
  1251. //---------------------------------------------------------------------
  1252. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1253. {
  1254. FLogProtocol = ALogProtocol;
  1255. UpdateActualLogProtocol();
  1256. }
  1257. //---------------------------------------------------------------------
  1258. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1259. {
  1260. FLogSensitive = ALogSensitive;
  1261. }
  1262. //---------------------------------------------------------------------
  1263. void __fastcall TConfiguration::SetLogging(bool value)
  1264. {
  1265. if (Logging != value)
  1266. {
  1267. FPermanentLogging = value;
  1268. FLogging = value;
  1269. UpdateActualLogProtocol();
  1270. Changed();
  1271. }
  1272. }
  1273. //---------------------------------------------------------------------
  1274. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  1275. {
  1276. if (LogFileName != value)
  1277. {
  1278. FPermanentLogFileName = value;
  1279. FLogFileName = value;
  1280. Changed();
  1281. }
  1282. }
  1283. //---------------------------------------------------------------------
  1284. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  1285. {
  1286. if (ActionsLogFileName != value)
  1287. {
  1288. FPermanentActionsLogFileName = value;
  1289. FActionsLogFileName = value;
  1290. Changed();
  1291. }
  1292. }
  1293. //---------------------------------------------------------------------
  1294. bool __fastcall TConfiguration::GetLogToFile()
  1295. {
  1296. return !LogFileName.IsEmpty();
  1297. }
  1298. //---------------------------------------------------------------------
  1299. void __fastcall TConfiguration::UpdateActualLogProtocol()
  1300. {
  1301. FActualLogProtocol = FLogging ? FLogProtocol : 0;
  1302. }
  1303. //---------------------------------------------------------------------
  1304. void __fastcall TConfiguration::SetLogProtocol(int value)
  1305. {
  1306. if (LogProtocol != value)
  1307. {
  1308. FPermanentLogProtocol = value;
  1309. FLogProtocol = value;
  1310. Changed();
  1311. UpdateActualLogProtocol();
  1312. }
  1313. }
  1314. //---------------------------------------------------------------------
  1315. void __fastcall TConfiguration::SetLogActions(bool value)
  1316. {
  1317. if (LogActions != value)
  1318. {
  1319. FPermanentLogActions = value;
  1320. FLogActions = value;
  1321. Changed();
  1322. }
  1323. }
  1324. //---------------------------------------------------------------------
  1325. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  1326. {
  1327. SET_CONFIG_PROPERTY(LogFileAppend);
  1328. }
  1329. //---------------------------------------------------------------------
  1330. void __fastcall TConfiguration::SetLogSensitive(bool value)
  1331. {
  1332. if (LogSensitive != value)
  1333. {
  1334. FPermanentLogSensitive = value;
  1335. FLogSensitive = value;
  1336. Changed();
  1337. }
  1338. }
  1339. //---------------------------------------------------------------------
  1340. void __fastcall TConfiguration::SetLogWindowLines(int value)
  1341. {
  1342. SET_CONFIG_PROPERTY(LogWindowLines);
  1343. }
  1344. //---------------------------------------------------------------------
  1345. void __fastcall TConfiguration::SetLogWindowComplete(bool value)
  1346. {
  1347. if (value != LogWindowComplete)
  1348. {
  1349. LogWindowLines = value ? 0 : 50;
  1350. Changed();
  1351. }
  1352. }
  1353. //---------------------------------------------------------------------
  1354. bool __fastcall TConfiguration::GetLogWindowComplete()
  1355. {
  1356. return (bool)(LogWindowLines == 0);
  1357. }
  1358. //---------------------------------------------------------------------
  1359. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  1360. {
  1361. return L"%TEMP%\\!S.log";
  1362. }
  1363. //---------------------------------------------------------------------------
  1364. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  1365. {
  1366. TGuard Guard(FCriticalSection);
  1367. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  1368. }
  1369. //---------------------------------------------------------------------------
  1370. bool __fastcall TConfiguration::GetConfirmOverwriting()
  1371. {
  1372. TGuard Guard(FCriticalSection);
  1373. return FConfirmOverwriting;
  1374. }
  1375. //---------------------------------------------------------------------------
  1376. void __fastcall TConfiguration::SetConfirmResume(bool value)
  1377. {
  1378. TGuard Guard(FCriticalSection);
  1379. SET_CONFIG_PROPERTY(ConfirmResume);
  1380. }
  1381. //---------------------------------------------------------------------------
  1382. bool __fastcall TConfiguration::GetConfirmResume()
  1383. {
  1384. TGuard Guard(FCriticalSection);
  1385. return FConfirmResume;
  1386. }
  1387. //---------------------------------------------------------------------------
  1388. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  1389. {
  1390. TGuard Guard(FCriticalSection);
  1391. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  1392. }
  1393. //---------------------------------------------------------------------------
  1394. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  1395. {
  1396. TGuard Guard(FCriticalSection);
  1397. return FAutoReadDirectoryAfterOp;
  1398. }
  1399. //---------------------------------------------------------------------------
  1400. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  1401. {
  1402. return L"h:nn:ss";
  1403. }
  1404. //---------------------------------------------------------------------------
  1405. UnicodeString __fastcall TConfiguration::GetPartialExt() const
  1406. {
  1407. return PARTIAL_EXT;
  1408. }
  1409. //---------------------------------------------------------------------------
  1410. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  1411. {
  1412. return L"";
  1413. }
  1414. //---------------------------------------------------------------------------
  1415. bool __fastcall TConfiguration::GetRememberPassword()
  1416. {
  1417. return false;
  1418. }
  1419. //---------------------------------------------------------------------------
  1420. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  1421. {
  1422. SET_CONFIG_PROPERTY(SessionReopenAuto);
  1423. }
  1424. //---------------------------------------------------------------------------
  1425. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  1426. {
  1427. SET_CONFIG_PROPERTY(SessionReopenBackground);
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  1431. {
  1432. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  1436. {
  1437. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  1438. }
  1439. //---------------------------------------------------------------------------
  1440. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  1441. {
  1442. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  1443. }
  1444. //---------------------------------------------------------------------------
  1445. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  1446. {
  1447. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  1448. }
  1449. //---------------------------------------------------------------------------
  1450. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  1451. {
  1452. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  1453. }
  1454. //---------------------------------------------------------------------------
  1455. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  1456. {
  1457. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  1458. }
  1459. //---------------------------------------------------------------------------
  1460. bool __fastcall TConfiguration::GetPersistent()
  1461. {
  1462. return (Storage != stNul);
  1463. }
  1464. //---------------------------------------------------------------------------
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  1467. {
  1468. FShortCuts.insert(ShortCut);
  1469. }
  1470. //---------------------------------------------------------------------------
  1471. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  1472. {
  1473. return (FShortCuts.count(ShortCut) != 0);
  1474. }