Configuration.cpp 49 KB

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