Configuration.cpp 47 KB

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