Configuration.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1141. {
  1142. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile);
  1143. if (ImportSessionList->Count > 0)
  1144. {
  1145. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1146. }
  1147. else
  1148. {
  1149. Error = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1150. }
  1151. }
  1152. else
  1153. {
  1154. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1155. }
  1156. return ImportSessionList.release();
  1157. }
  1158. //---------------------------------------------------------------------
  1159. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1160. {
  1161. try
  1162. {
  1163. UnicodeString Error;
  1164. std::unique_ptr<TStoredSessionList> Sesssions(SelectFilezillaSessionsForImport(Sessions, Error));
  1165. return (Sesssions->Count > 0);
  1166. }
  1167. catch (...)
  1168. {
  1169. return false;
  1170. }
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1174. {
  1175. if (RandomSeedFile != value)
  1176. {
  1177. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1178. FRandomSeedFile = value;
  1179. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1180. if (RandomSeedFileName.IsEmpty())
  1181. {
  1182. FRandomSeedFile = FDefaultRandomSeedFile;
  1183. }
  1184. if (!PrevRandomSeedFileName.IsEmpty() &&
  1185. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1186. FileExists(ApiPath(PrevRandomSeedFileName)))
  1187. {
  1188. // ignore any error
  1189. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1190. }
  1191. }
  1192. }
  1193. //---------------------------------------------------------------------
  1194. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1195. {
  1196. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1197. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1198. }
  1199. //---------------------------------------------------------------------
  1200. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1201. {
  1202. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1203. }
  1204. //---------------------------------------------------------------------
  1205. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1206. {
  1207. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1208. }
  1209. //---------------------------------------------------------------------
  1210. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1211. {
  1212. SET_CONFIG_PROPERTY(PuttyRegistryStorageKey);
  1213. }
  1214. //---------------------------------------------------------------------------
  1215. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1216. {
  1217. return eolCRLF;
  1218. }
  1219. //---------------------------------------------------------------------
  1220. bool __fastcall TConfiguration::GetCollectUsage()
  1221. {
  1222. return FUsage->Collect;
  1223. }
  1224. //---------------------------------------------------------------------
  1225. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1226. {
  1227. FUsage->Collect = value;
  1228. }
  1229. //---------------------------------------------------------------------
  1230. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1231. {
  1232. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1233. {
  1234. TemporaryActionsLogging(ALogFileName);
  1235. }
  1236. else
  1237. {
  1238. FLogging = true;
  1239. FLogFileName = ALogFileName;
  1240. UpdateActualLogProtocol();
  1241. }
  1242. }
  1243. //---------------------------------------------------------------------
  1244. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1245. {
  1246. FLogActions = true;
  1247. FActionsLogFileName = ALogFileName;
  1248. }
  1249. //---------------------------------------------------------------------
  1250. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1251. {
  1252. FLogProtocol = ALogProtocol;
  1253. UpdateActualLogProtocol();
  1254. }
  1255. //---------------------------------------------------------------------
  1256. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1257. {
  1258. FLogSensitive = ALogSensitive;
  1259. }
  1260. //---------------------------------------------------------------------
  1261. void __fastcall TConfiguration::SetLogging(bool value)
  1262. {
  1263. if (Logging != value)
  1264. {
  1265. FPermanentLogging = value;
  1266. FLogging = value;
  1267. UpdateActualLogProtocol();
  1268. Changed();
  1269. }
  1270. }
  1271. //---------------------------------------------------------------------
  1272. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  1273. {
  1274. if (LogFileName != value)
  1275. {
  1276. FPermanentLogFileName = value;
  1277. FLogFileName = value;
  1278. Changed();
  1279. }
  1280. }
  1281. //---------------------------------------------------------------------
  1282. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  1283. {
  1284. if (ActionsLogFileName != value)
  1285. {
  1286. FPermanentActionsLogFileName = value;
  1287. FActionsLogFileName = value;
  1288. Changed();
  1289. }
  1290. }
  1291. //---------------------------------------------------------------------
  1292. bool __fastcall TConfiguration::GetLogToFile()
  1293. {
  1294. return !LogFileName.IsEmpty();
  1295. }
  1296. //---------------------------------------------------------------------
  1297. void __fastcall TConfiguration::UpdateActualLogProtocol()
  1298. {
  1299. FActualLogProtocol = FLogging ? FLogProtocol : 0;
  1300. }
  1301. //---------------------------------------------------------------------
  1302. void __fastcall TConfiguration::SetLogProtocol(int value)
  1303. {
  1304. if (LogProtocol != value)
  1305. {
  1306. FPermanentLogProtocol = value;
  1307. FLogProtocol = value;
  1308. Changed();
  1309. UpdateActualLogProtocol();
  1310. }
  1311. }
  1312. //---------------------------------------------------------------------
  1313. void __fastcall TConfiguration::SetLogActions(bool value)
  1314. {
  1315. if (LogActions != value)
  1316. {
  1317. FPermanentLogActions = value;
  1318. FLogActions = value;
  1319. Changed();
  1320. }
  1321. }
  1322. //---------------------------------------------------------------------
  1323. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  1324. {
  1325. SET_CONFIG_PROPERTY(LogFileAppend);
  1326. }
  1327. //---------------------------------------------------------------------
  1328. void __fastcall TConfiguration::SetLogSensitive(bool value)
  1329. {
  1330. if (LogSensitive != value)
  1331. {
  1332. FPermanentLogSensitive = value;
  1333. FLogSensitive = value;
  1334. Changed();
  1335. }
  1336. }
  1337. //---------------------------------------------------------------------
  1338. void __fastcall TConfiguration::SetLogWindowLines(int value)
  1339. {
  1340. SET_CONFIG_PROPERTY(LogWindowLines);
  1341. }
  1342. //---------------------------------------------------------------------
  1343. void __fastcall TConfiguration::SetLogWindowComplete(bool value)
  1344. {
  1345. if (value != LogWindowComplete)
  1346. {
  1347. LogWindowLines = value ? 0 : 50;
  1348. Changed();
  1349. }
  1350. }
  1351. //---------------------------------------------------------------------
  1352. bool __fastcall TConfiguration::GetLogWindowComplete()
  1353. {
  1354. return (bool)(LogWindowLines == 0);
  1355. }
  1356. //---------------------------------------------------------------------
  1357. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  1358. {
  1359. return L"%TEMP%\\!S.log";
  1360. }
  1361. //---------------------------------------------------------------------------
  1362. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  1363. {
  1364. TGuard Guard(FCriticalSection);
  1365. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. bool __fastcall TConfiguration::GetConfirmOverwriting()
  1369. {
  1370. TGuard Guard(FCriticalSection);
  1371. return FConfirmOverwriting;
  1372. }
  1373. //---------------------------------------------------------------------------
  1374. void __fastcall TConfiguration::SetConfirmResume(bool value)
  1375. {
  1376. TGuard Guard(FCriticalSection);
  1377. SET_CONFIG_PROPERTY(ConfirmResume);
  1378. }
  1379. //---------------------------------------------------------------------------
  1380. bool __fastcall TConfiguration::GetConfirmResume()
  1381. {
  1382. TGuard Guard(FCriticalSection);
  1383. return FConfirmResume;
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  1387. {
  1388. TGuard Guard(FCriticalSection);
  1389. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  1390. }
  1391. //---------------------------------------------------------------------------
  1392. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  1393. {
  1394. TGuard Guard(FCriticalSection);
  1395. return FAutoReadDirectoryAfterOp;
  1396. }
  1397. //---------------------------------------------------------------------------
  1398. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  1399. {
  1400. return L"h:nn:ss";
  1401. }
  1402. //---------------------------------------------------------------------------
  1403. UnicodeString __fastcall TConfiguration::GetPartialExt() const
  1404. {
  1405. return PARTIAL_EXT;
  1406. }
  1407. //---------------------------------------------------------------------------
  1408. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  1409. {
  1410. return L"";
  1411. }
  1412. //---------------------------------------------------------------------------
  1413. bool __fastcall TConfiguration::GetRememberPassword()
  1414. {
  1415. return false;
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  1419. {
  1420. SET_CONFIG_PROPERTY(SessionReopenAuto);
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  1424. {
  1425. SET_CONFIG_PROPERTY(SessionReopenBackground);
  1426. }
  1427. //---------------------------------------------------------------------------
  1428. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  1429. {
  1430. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  1434. {
  1435. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  1439. {
  1440. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  1441. }
  1442. //---------------------------------------------------------------------------
  1443. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  1444. {
  1445. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  1449. {
  1450. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  1451. }
  1452. //---------------------------------------------------------------------------
  1453. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  1454. {
  1455. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  1456. }
  1457. //---------------------------------------------------------------------------
  1458. bool __fastcall TConfiguration::GetPersistent()
  1459. {
  1460. return (Storage != stNul);
  1461. }
  1462. //---------------------------------------------------------------------------
  1463. //---------------------------------------------------------------------------
  1464. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  1465. {
  1466. FShortCuts.insert(ShortCut);
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  1470. {
  1471. return (FShortCuts.count(ShortCut) != 0);
  1472. }