Configuration.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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 "FileMasks.h"
  14. #include "CopyParam.h"
  15. #include <shlobj.h>
  16. #include <System.IOUtils.hpp>
  17. #include <System.StrUtils.hpp>
  18. //---------------------------------------------------------------------------
  19. #pragma package(smart_init)
  20. //---------------------------------------------------------------------------
  21. const wchar_t * AutoSwitchNames = L"On;Off;Auto";
  22. const wchar_t * NotAutoSwitchNames = L"Off;On;Auto";
  23. //---------------------------------------------------------------------------
  24. // See https://www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xhtml
  25. const UnicodeString Sha1ChecksumAlg(L"sha-1");
  26. const UnicodeString Sha224ChecksumAlg(L"sha-224");
  27. const UnicodeString Sha256ChecksumAlg(L"sha-256");
  28. const UnicodeString Sha384ChecksumAlg(L"sha-384");
  29. const UnicodeString Sha512ChecksumAlg(L"sha-512");
  30. const UnicodeString Md5ChecksumAlg(L"md5");
  31. // Not defined by IANA
  32. const UnicodeString Crc32ChecksumAlg(L"crc32");
  33. //---------------------------------------------------------------------------
  34. const UnicodeString SshFingerprintType(L"ssh");
  35. const UnicodeString TlsFingerprintType(L"tls");
  36. //---------------------------------------------------------------------------
  37. const UnicodeString FtpsCertificateStorageKey(L"FtpsCertificates");
  38. const UnicodeString HttpsCertificateStorageKey(L"HttpsCertificates");
  39. const UnicodeString LastFingerprintsStorageKey(L"LastFingerprints");
  40. const UnicodeString DirectoryStatisticsCacheKey(L"DirectoryStatisticsCache");
  41. const UnicodeString CDCacheKey(L"CDCache");
  42. const UnicodeString BannersKey(L"Banners");
  43. //---------------------------------------------------------------------------
  44. const int BelowNormalLogLevels = 1;
  45. //---------------------------------------------------------------------------
  46. __fastcall TConfiguration::TConfiguration()
  47. {
  48. FCriticalSection = new TCriticalSection();
  49. FUpdating = 0;
  50. FStorage = stDetect;
  51. FDontSave = false;
  52. FForceSave = false;
  53. FApplicationInfo = NULL;
  54. FUsage = new TUsage(this);
  55. FDefaultCollectUsage = false;
  56. FScripting = false;
  57. UnicodeString RandomSeedPath;
  58. if (!GetEnvironmentVariable(L"APPDATA").IsEmpty())
  59. {
  60. RandomSeedPath = L"%APPDATA%";
  61. }
  62. else
  63. {
  64. RandomSeedPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  65. if (RandomSeedPath.IsEmpty())
  66. {
  67. RandomSeedPath = GetShellFolderPath(CSIDL_APPDATA);
  68. }
  69. }
  70. FDefaultRandomSeedFile = IncludeTrailingBackslash(RandomSeedPath) + L"winscp.rnd";
  71. }
  72. //---------------------------------------------------------------------------
  73. void __fastcall TConfiguration::Default()
  74. {
  75. TGuard Guard(FCriticalSection);
  76. FDisablePasswordStoring = false;
  77. FForceBanners = false;
  78. FDisableAcceptingHostKeys = false;
  79. TRegistryStorage * AdminStorage;
  80. AdminStorage = new TRegistryStorage(RegistryStorageKey, HKEY_LOCAL_MACHINE);
  81. try
  82. {
  83. if (AdminStorage->OpenRootKey(false))
  84. {
  85. LoadAdmin(AdminStorage);
  86. AdminStorage->CloseSubKey();
  87. }
  88. }
  89. __finally
  90. {
  91. delete AdminStorage;
  92. }
  93. RandomSeedFile = FDefaultRandomSeedFile;
  94. PuttyRegistryStorageKey = OriginalPuttyRegistryStorageKey;
  95. FConfirmOverwriting = true;
  96. FConfirmResume = true;
  97. FAutoReadDirectoryAfterOp = true;
  98. FSessionReopenAuto = 5000;
  99. FSessionReopenBackground = 2000;
  100. FSessionReopenTimeout = 0;
  101. FSessionReopenAutoStall = 60000;
  102. FTunnelLocalPortNumberLow = 50000;
  103. FTunnelLocalPortNumberHigh = 50099;
  104. FCacheDirectoryChangesMaxSize = 100;
  105. FShowFtpWelcomeMessage = false;
  106. FExternalIpAddress = L"";
  107. FLocalPortNumberMin = 0;
  108. FLocalPortNumberMax = 0;
  109. FTryFtpWhenSshFails = true;
  110. FParallelDurationThreshold = 10;
  111. FMimeTypes = UnicodeString();
  112. FDontReloadMoreThanSessions = 1000;
  113. FScriptProgressFileNameLimit = 25;
  114. CollectUsage = FDefaultCollectUsage;
  115. FLogging = false;
  116. FPermanentLogging = false;
  117. FLogFileName = DefaultLogFileName;
  118. FPermanentLogFileName = FLogFileName;
  119. FLogFileAppend = true;
  120. FLogSensitive = false;
  121. FPermanentLogSensitive = FLogSensitive;
  122. FLogMaxSize = 0;
  123. FPermanentLogMaxSize = FLogMaxSize;
  124. FLogMaxCount = 0;
  125. FPermanentLogMaxCount = FLogMaxCount;
  126. FLogProtocol = 0;
  127. FPermanentLogProtocol = FLogProtocol;
  128. UpdateActualLogProtocol();
  129. FLogActions = false;
  130. FPermanentLogActions = false;
  131. FLogActionsRequired = false;
  132. FActionsLogFileName = L"%TEMP%\\!S.xml";
  133. FPermanentActionsLogFileName = FActionsLogFileName;
  134. FProgramIniPathWrittable = -1;
  135. FCustomIniFileStorageName = LoadCustomIniFileStorageName();
  136. Changed();
  137. }
  138. //---------------------------------------------------------------------------
  139. __fastcall TConfiguration::~TConfiguration()
  140. {
  141. DebugAssert(!FUpdating);
  142. if (FApplicationInfo) FreeFileInfo(FApplicationInfo);
  143. delete FCriticalSection;
  144. delete FUsage;
  145. }
  146. //---------------------------------------------------------------------------
  147. void __fastcall TConfiguration::UpdateStaticUsage()
  148. {
  149. Usage->Set(L"ConfigurationIniFile", (Storage == stIniFile));
  150. Usage->Set(L"ConfigurationIniFileCustom", !CustomIniFileStorageName.IsEmpty());
  151. Usage->Set("Unofficial", IsUnofficial);
  152. // this is called from here, because we are guarded from calling into
  153. // master password handler here, see TWinConfiguration::UpdateStaticUsage
  154. StoredSessions->UpdateStaticUsage();
  155. }
  156. //---------------------------------------------------------------------------
  157. THierarchicalStorage * TConfiguration::CreateConfigStorage()
  158. {
  159. bool SessionList = false;
  160. return CreateScpStorage(SessionList);
  161. }
  162. //---------------------------------------------------------------------------
  163. THierarchicalStorage * TConfiguration::CreateConfigRegistryStorage()
  164. {
  165. return new TRegistryStorage(RegistryStorageKey);
  166. }
  167. //---------------------------------------------------------------------------
  168. THierarchicalStorage * TConfiguration::CreateScpStorage(bool & SessionList)
  169. {
  170. TGuard Guard(FCriticalSection);
  171. THierarchicalStorage * Result;
  172. if (Storage == stRegistry)
  173. {
  174. Result = CreateConfigRegistryStorage();
  175. }
  176. else if (Storage == stNul)
  177. {
  178. Result = TIniFileStorage::CreateFromPath(INI_NUL);
  179. }
  180. else
  181. {
  182. UnicodeString StorageName = IniFileStorageName;
  183. Result = TIniFileStorage::CreateFromPath(StorageName);
  184. }
  185. if ((FOptionsStorage.get() != NULL) && (FOptionsStorage->Count > 0))
  186. {
  187. if (!SessionList)
  188. {
  189. Result = new TOptionsStorage(FOptionsStorage.get(), ConfigurationSubKey, Result);
  190. }
  191. else
  192. {
  193. // cannot reuse session list storage for configuration as for it we need
  194. // the option-override storage above
  195. }
  196. }
  197. else
  198. {
  199. // All the above stores can be reused for configuration,
  200. // if no options-overrides are set
  201. SessionList = false;
  202. }
  203. return Result;
  204. }
  205. //---------------------------------------------------------------------------
  206. UnicodeString __fastcall TConfiguration::PropertyToKey(const UnicodeString & Property)
  207. {
  208. // no longer useful
  209. int P = Property.LastDelimiter(L".>");
  210. UnicodeString Result = Property.SubString(P + 1, Property.Length() - P);
  211. if ((Result[1] == L'F') && ((wchar_t)toupper(Result[2]) == Result[2]))
  212. {
  213. Result.Delete(1, 1);
  214. }
  215. return Result;
  216. }
  217. //---------------------------------------------------------------------------
  218. #define BLOCK(KEY, CANCREATE, BLOCK) \
  219. if (Storage->OpenSubKeyPath(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKeyPath(); }
  220. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  221. #define REGCONFIG(CANCREATE) \
  222. BLOCK(L"Interface", CANCREATE, \
  223. KEY(String, RandomSeedFile); \
  224. KEY(String, PuttyRegistryStorageKey); \
  225. KEY(Bool, ConfirmOverwriting); \
  226. KEY(Bool, ConfirmResume); \
  227. KEY(Bool, AutoReadDirectoryAfterOp); \
  228. KEY(Integer, SessionReopenAuto); \
  229. KEY(Integer, SessionReopenBackground); \
  230. KEY(Integer, SessionReopenTimeout); \
  231. KEY(Integer, SessionReopenAutoStall); \
  232. KEY(Integer, TunnelLocalPortNumberLow); \
  233. KEY(Integer, TunnelLocalPortNumberHigh); \
  234. KEY(Integer, CacheDirectoryChangesMaxSize); \
  235. KEY(Bool, ShowFtpWelcomeMessage); \
  236. KEY(String, ExternalIpAddress); \
  237. KEY(Integer, LocalPortNumberMin); \
  238. KEY(Integer, LocalPortNumberMax); \
  239. KEY(Bool, TryFtpWhenSshFails); \
  240. KEY(Integer, ParallelDurationThreshold); \
  241. KEY(String, MimeTypes); \
  242. KEY(Integer, DontReloadMoreThanSessions); \
  243. KEY(Integer, ScriptProgressFileNameLimit); \
  244. KEY(Bool, CollectUsage); \
  245. ); \
  246. BLOCK(L"Logging", CANCREATE, \
  247. KEYEX(Bool, PermanentLogging, L"Logging"); \
  248. KEYEX(String,PermanentLogFileName, L"LogFileName"); \
  249. KEY(Bool, LogFileAppend); \
  250. KEYEX(Bool, PermanentLogSensitive, L"LogSensitive"); \
  251. KEYEX(Int64, PermanentLogMaxSize, L"LogMaxSize"); \
  252. KEYEX(Integer, PermanentLogMaxCount, L"LogMaxCount"); \
  253. KEYEX(Integer,PermanentLogProtocol, L"LogProtocol"); \
  254. KEYEX(Bool, PermanentLogActions, L"LogActions"); \
  255. KEYEX(String,PermanentActionsLogFileName, L"ActionsLogFileName"); \
  256. );
  257. //---------------------------------------------------------------------------
  258. void __fastcall TConfiguration::SaveData(THierarchicalStorage * Storage, bool /*All*/)
  259. {
  260. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  261. REGCONFIG(true);
  262. #undef KEYEX
  263. if (Storage->OpenSubKey(L"Usage", true))
  264. {
  265. FUsage->Save(Storage);
  266. Storage->CloseSubKey();
  267. }
  268. }
  269. //---------------------------------------------------------------------------
  270. void __fastcall TConfiguration::Save()
  271. {
  272. // only modified, implicit
  273. DoSave(false, false);
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TConfiguration::SaveExplicit()
  277. {
  278. // only modified, explicit
  279. DoSave(false, true);
  280. }
  281. //---------------------------------------------------------------------------
  282. void __fastcall TConfiguration::DoSave(bool All, bool Explicit)
  283. {
  284. if (FDontSave) return;
  285. THierarchicalStorage * AStorage = CreateConfigStorage();
  286. try
  287. {
  288. AStorage->AccessMode = smReadWrite;
  289. AStorage->Explicit = Explicit;
  290. AStorage->ForceSave = FForceSave;
  291. if (AStorage->OpenSubKey(ConfigurationSubKey, true))
  292. {
  293. // if saving to TOptionsStorage, make sure we save everything so that
  294. // all configuration is properly transferred to the master storage
  295. bool ConfigAll = All || AStorage->Temporary;
  296. SaveData(AStorage, ConfigAll);
  297. }
  298. }
  299. __finally
  300. {
  301. delete AStorage;
  302. }
  303. Saved();
  304. if (All)
  305. {
  306. StoredSessions->Save(true, Explicit);
  307. }
  308. // clean up as last, so that if it fails (read only INI), the saving can proceed
  309. if (Storage == stRegistry)
  310. {
  311. CleanupIniFile();
  312. }
  313. SaveCustomIniFileStorageName();
  314. }
  315. //---------------------------------------------------------------------------
  316. void __fastcall TConfiguration::SaveCustomIniFileStorageName()
  317. {
  318. // Particularly, not to create an empty "Override" key, unless the custom INI file is ever set
  319. if (CustomIniFileStorageName != LoadCustomIniFileStorageName())
  320. {
  321. std::unique_ptr<TRegistryStorage> RegistryStorage(new TRegistryStorage(GetRegistryStorageOverrideKey()));
  322. RegistryStorage->AccessMode = smReadWrite;
  323. RegistryStorage->Explicit = true;
  324. if (RegistryStorage->OpenRootKey(true))
  325. {
  326. RegistryStorage->WriteString(L"IniFile", CustomIniFileStorageName);
  327. RegistryStorage->CloseSubKey();
  328. }
  329. }
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TConfiguration::Export(const UnicodeString & FileName)
  333. {
  334. // not to "append" the export to an existing file
  335. if (FileExists(FileName))
  336. {
  337. DeleteFileChecked(FileName);
  338. }
  339. THierarchicalStorage * Storage = NULL;
  340. THierarchicalStorage * ExportStorage = NULL;
  341. try
  342. {
  343. ExportStorage = TIniFileStorage::CreateFromPath(FileName);
  344. ExportStorage->AccessMode = smReadWrite;
  345. ExportStorage->Explicit = true;
  346. Storage = CreateConfigStorage();
  347. Storage->AccessMode = smRead;
  348. CopyData(Storage, ExportStorage);
  349. if (ExportStorage->OpenSubKey(ConfigurationSubKey, true))
  350. {
  351. SaveData(ExportStorage, true);
  352. }
  353. }
  354. __finally
  355. {
  356. delete ExportStorage;
  357. delete Storage;
  358. }
  359. StoredSessions->Export(FileName);
  360. }
  361. //---------------------------------------------------------------------------
  362. void __fastcall TConfiguration::Import(const UnicodeString & FileName)
  363. {
  364. THierarchicalStorage * Storage = NULL;
  365. THierarchicalStorage * ImportStorage = NULL;
  366. try
  367. {
  368. ImportStorage = TIniFileStorage::CreateFromPath(FileName);
  369. ImportStorage->AccessMode = smRead;
  370. Storage = CreateConfigStorage();
  371. Storage->AccessMode = smReadWrite;
  372. Storage->Explicit = true;
  373. CopyData(ImportStorage, Storage);
  374. Default();
  375. LoadFrom(ImportStorage);
  376. if (ImportStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  377. {
  378. StoredSessions->Clear();
  379. StoredSessions->DefaultSettings->Default();
  380. StoredSessions->Load(ImportStorage);
  381. }
  382. }
  383. __finally
  384. {
  385. delete ImportStorage;
  386. delete Storage;
  387. }
  388. // save all and explicit
  389. DoSave(true, true);
  390. }
  391. //---------------------------------------------------------------------------
  392. void __fastcall TConfiguration::LoadData(THierarchicalStorage * Storage)
  393. {
  394. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(NAME, VAR)
  395. #pragma warn -eas
  396. REGCONFIG(false);
  397. #pragma warn +eas
  398. #undef KEYEX
  399. if (Storage->OpenSubKey(L"Usage", false))
  400. {
  401. FUsage->Load(Storage);
  402. Storage->CloseSubKey();
  403. }
  404. if (FPermanentLogActions && FPermanentActionsLogFileName.IsEmpty() &&
  405. FPermanentLogging && !FPermanentLogFileName.IsEmpty())
  406. {
  407. FPermanentActionsLogFileName = FPermanentLogFileName;
  408. FPermanentLogging = false;
  409. FPermanentLogFileName = L"";
  410. }
  411. }
  412. //---------------------------------------------------------------------------
  413. void __fastcall TConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  414. {
  415. FDisablePasswordStoring = Storage->ReadBool(L"DisablePasswordStoring", FDisablePasswordStoring);
  416. FForceBanners = Storage->ReadBool(L"ForceBanners", FForceBanners);
  417. FDisableAcceptingHostKeys = Storage->ReadBool(L"DisableAcceptingHostKeys", FDisableAcceptingHostKeys);
  418. FDefaultCollectUsage = Storage->ReadBool(L"DefaultCollectUsage", FDefaultCollectUsage);
  419. }
  420. //---------------------------------------------------------------------------
  421. void __fastcall TConfiguration::LoadFrom(THierarchicalStorage * Storage)
  422. {
  423. if (Storage->OpenSubKey(ConfigurationSubKey, false))
  424. {
  425. LoadData(Storage);
  426. Storage->CloseSubKey();
  427. }
  428. }
  429. //---------------------------------------------------------------------------
  430. UnicodeString __fastcall TConfiguration::GetRegistryStorageOverrideKey()
  431. {
  432. return GetRegistryStorageKey() + L" Override";
  433. }
  434. //---------------------------------------------------------------------------
  435. UnicodeString __fastcall TConfiguration::LoadCustomIniFileStorageName()
  436. {
  437. UnicodeString Result;
  438. std::unique_ptr<TRegistryStorage> RegistryStorage(new TRegistryStorage(GetRegistryStorageOverrideKey()));
  439. if (RegistryStorage->OpenRootKey(false))
  440. {
  441. Result = RegistryStorage->ReadString(L"IniFile", L"");
  442. RegistryStorage->CloseSubKey();
  443. }
  444. RegistryStorage.reset(NULL);
  445. return Result;
  446. }
  447. //---------------------------------------------------------------------------
  448. void __fastcall TConfiguration::Load(THierarchicalStorage * Storage)
  449. {
  450. TGuard Guard(FCriticalSection);
  451. TStorageAccessMode StorageAccessMode = Storage->AccessMode;
  452. try
  453. {
  454. Storage->AccessMode = smRead;
  455. LoadFrom(Storage);
  456. }
  457. __finally
  458. {
  459. Storage->AccessMode = StorageAccessMode;
  460. }
  461. }
  462. //---------------------------------------------------------------------------
  463. bool __fastcall TConfiguration::CopySubKey(THierarchicalStorage * Source, THierarchicalStorage * Target, const UnicodeString & Name)
  464. {
  465. bool Result = Source->OpenSubKey(Name, false);
  466. if (Result)
  467. {
  468. Result = Target->OpenSubKey(Name, true);
  469. if (!Result)
  470. {
  471. Source->CloseSubKey();
  472. }
  473. }
  474. return Result;
  475. }
  476. //---------------------------------------------------------------------------
  477. void __fastcall TConfiguration::CopyAllStringsInSubKey(
  478. THierarchicalStorage * Source, THierarchicalStorage * Target, const UnicodeString & Name)
  479. {
  480. if (CopySubKey(Source, Target, Name))
  481. {
  482. std::unique_ptr<TStrings> Names(new TStringList());
  483. Source->GetValueNames(Names.get());
  484. for (int Index = 0; Index < Names->Count; Index++)
  485. {
  486. UnicodeString Buf = Source->ReadStringRaw(Names->Strings[Index], UnicodeString());
  487. Target->WriteStringRaw(Names->Strings[Index], Buf);
  488. }
  489. Target->CloseSubKey();
  490. Source->CloseSubKey();
  491. }
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TConfiguration::CopyData(THierarchicalStorage * Source,
  495. THierarchicalStorage * Target)
  496. {
  497. if (CopySubKey(Source, Target, ConfigurationSubKey))
  498. {
  499. if (CopySubKey(Source, Target, CDCacheKey))
  500. {
  501. std::unique_ptr<TStrings> Names(new TStringList());
  502. Source->GetValueNames(Names.get());
  503. for (int Index = 0; Index < Names->Count; Index++)
  504. {
  505. Target->WriteBinaryData(Names->Strings[Index], Source->ReadBinaryData(Names->Strings[Index]));
  506. }
  507. Target->CloseSubKey();
  508. Source->CloseSubKey();
  509. }
  510. CopyAllStringsInSubKey(Source, Target, BannersKey);
  511. CopyAllStringsInSubKey(Source, Target, LastFingerprintsStorageKey);
  512. Target->CloseSubKey();
  513. Source->CloseSubKey();
  514. }
  515. CopyAllStringsInSubKey(Source, Target, SshHostKeysSubKey);
  516. CopyAllStringsInSubKey(Source, Target, FtpsCertificateStorageKey);
  517. CopyAllStringsInSubKey(Source, Target, HttpsCertificateStorageKey);
  518. }
  519. //---------------------------------------------------------------------------
  520. void __fastcall TConfiguration::LoadDirectoryChangesCache(const UnicodeString SessionKey,
  521. TRemoteDirectoryChangesCache * DirectoryChangesCache)
  522. {
  523. THierarchicalStorage * Storage = CreateConfigStorage();
  524. try
  525. {
  526. Storage->AccessMode = smRead;
  527. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  528. Storage->OpenSubKey(CDCacheKey, false) &&
  529. Storage->ValueExists(SessionKey))
  530. {
  531. DirectoryChangesCache->Deserialize(Storage->ReadBinaryData(SessionKey));
  532. }
  533. }
  534. __finally
  535. {
  536. delete Storage;
  537. }
  538. }
  539. //---------------------------------------------------------------------------
  540. void __fastcall TConfiguration::SaveDirectoryChangesCache(const UnicodeString SessionKey,
  541. TRemoteDirectoryChangesCache * DirectoryChangesCache)
  542. {
  543. THierarchicalStorage * Storage = CreateConfigStorage();
  544. try
  545. {
  546. Storage->AccessMode = smReadWrite;
  547. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  548. Storage->OpenSubKey(CDCacheKey, true))
  549. {
  550. UnicodeString Data;
  551. DirectoryChangesCache->Serialize(Data);
  552. Storage->WriteBinaryData(SessionKey, Data);
  553. }
  554. }
  555. __finally
  556. {
  557. delete Storage;
  558. }
  559. }
  560. //---------------------------------------------------------------------------
  561. UnicodeString __fastcall TConfiguration::BannerHash(const UnicodeString & Banner)
  562. {
  563. RawByteString Result;
  564. Result.SetLength(16);
  565. md5checksum(
  566. reinterpret_cast<const char*>(Banner.c_str()), Banner.Length() * sizeof(wchar_t),
  567. (unsigned char*)Result.c_str());
  568. return BytesToHex(Result);
  569. }
  570. //---------------------------------------------------------------------------
  571. void __fastcall TConfiguration::GetBannerData(
  572. const UnicodeString & SessionKey, UnicodeString & BannerHash, unsigned int & Params)
  573. {
  574. BannerHash = UnicodeString();
  575. Params = 0;
  576. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  577. Storage->AccessMode = smRead;
  578. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  579. Storage->OpenSubKey(BannersKey, false))
  580. {
  581. UnicodeString S = Storage->ReadString(SessionKey, L"");
  582. BannerHash = CutToChar(S, L',', true);
  583. Params = StrToIntDef(L"$" + CutToChar(S, L',', true), 0);
  584. }
  585. }
  586. //---------------------------------------------------------------------------
  587. bool __fastcall TConfiguration::ShowBanner(
  588. const UnicodeString & SessionKey, const UnicodeString & Banner, unsigned int & Params)
  589. {
  590. UnicodeString StoredBannerHash;
  591. GetBannerData(SessionKey, StoredBannerHash, Params);
  592. bool Result = (StoredBannerHash != BannerHash(Banner));
  593. return Result;
  594. }
  595. //---------------------------------------------------------------------------
  596. void __fastcall TConfiguration::SetBannerData(
  597. const UnicodeString & SessionKey, const UnicodeString & BannerHash, unsigned int Params)
  598. {
  599. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  600. Storage->AccessMode = smReadWrite;
  601. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  602. Storage->OpenSubKey(BannersKey, true))
  603. {
  604. Storage->WriteString(SessionKey, BannerHash + L"," + UIntToStr(Params));
  605. }
  606. }
  607. //---------------------------------------------------------------------------
  608. void __fastcall TConfiguration::NeverShowBanner(const UnicodeString & SessionKey, const UnicodeString & Banner)
  609. {
  610. UnicodeString DummyBannerHash;
  611. unsigned int Params;
  612. GetBannerData(SessionKey, DummyBannerHash, Params);
  613. SetBannerData(SessionKey, BannerHash(Banner), Params);
  614. }
  615. //---------------------------------------------------------------------------
  616. void __fastcall TConfiguration::SetBannerParams(const UnicodeString & SessionKey, unsigned int Params)
  617. {
  618. UnicodeString BannerHash;
  619. unsigned int DummyParams;
  620. GetBannerData(SessionKey, BannerHash, DummyParams);
  621. SetBannerData(SessionKey, BannerHash, Params);
  622. }
  623. //---------------------------------------------------------------------------
  624. UnicodeString __fastcall TConfiguration::FormatFingerprintKey(const UnicodeString & SiteKey, const UnicodeString & FingerprintType)
  625. {
  626. return FORMAT(L"%s:%s", (SiteKey, FingerprintType));
  627. }
  628. //---------------------------------------------------------------------------
  629. void __fastcall TConfiguration::RememberLastFingerprint(const UnicodeString & SiteKey, const UnicodeString & FingerprintType, const UnicodeString & Fingerprint)
  630. {
  631. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  632. Storage->AccessMode = smReadWrite;
  633. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  634. Storage->OpenSubKey(LastFingerprintsStorageKey, true))
  635. {
  636. UnicodeString FingerprintKey = FormatFingerprintKey(SiteKey, FingerprintType);
  637. Storage->WriteString(FingerprintKey, Fingerprint);
  638. }
  639. }
  640. //---------------------------------------------------------------------------
  641. UnicodeString __fastcall TConfiguration::LastFingerprint(const UnicodeString & SiteKey, const UnicodeString & FingerprintType)
  642. {
  643. UnicodeString Result;
  644. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  645. Storage->AccessMode = smRead;
  646. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  647. Storage->OpenSubKey(LastFingerprintsStorageKey, false))
  648. {
  649. UnicodeString FingerprintKey = FormatFingerprintKey(SiteKey, FingerprintType);
  650. Result = Storage->ReadString(FingerprintKey, L"");
  651. }
  652. return Result;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TConfiguration::Changed()
  656. {
  657. TNotifyEvent AOnChange = NULL;
  658. {
  659. TGuard Guard(FCriticalSection);
  660. if (FUpdating == 0)
  661. {
  662. AOnChange = OnChange;
  663. }
  664. else
  665. {
  666. FChanged = true;
  667. }
  668. }
  669. // No specific reason to call this outside of a guard, just that it is less of a change to a previous unguarded code
  670. if (AOnChange != NULL)
  671. {
  672. AOnChange(this);
  673. }
  674. }
  675. //---------------------------------------------------------------------------
  676. void __fastcall TConfiguration::BeginUpdate()
  677. {
  678. FCriticalSection->Enter();
  679. if (FUpdating == 0)
  680. {
  681. FChanged = false;
  682. }
  683. FUpdating++;
  684. // Greater value would probably indicate some nesting problem in code
  685. DebugAssert(FUpdating < 6);
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TConfiguration::EndUpdate()
  689. {
  690. DebugAssert(FUpdating > 0);
  691. FUpdating--;
  692. if ((FUpdating == 0) && FChanged)
  693. {
  694. FChanged = false;
  695. Changed();
  696. }
  697. FCriticalSection->Leave();
  698. }
  699. //---------------------------------------------------------------------------
  700. void __fastcall TConfiguration::CleanupConfiguration()
  701. {
  702. try
  703. {
  704. CleanupRegistry(ConfigurationSubKey);
  705. if (Storage == stRegistry)
  706. {
  707. FDontSave = true;
  708. }
  709. }
  710. catch (Exception &E)
  711. {
  712. throw ExtException(&E, LoadStr(CLEANUP_CONFIG_ERROR));
  713. }
  714. }
  715. //---------------------------------------------------------------------------
  716. bool TConfiguration::RegistryPathExists(const UnicodeString & RegistryPath)
  717. {
  718. std::unique_ptr<TRegistryStorage> Registry(new TRegistryStorage(RegistryStorageKey));
  719. UnicodeString ParentKey = ExtractFileDir(RegistryPath);
  720. UnicodeString SubKey = ExtractFileName(RegistryPath);
  721. return
  722. Registry->OpenRootKey(false) &&
  723. (ParentKey.IsEmpty() ||
  724. Registry->OpenSubKeyPath(ParentKey, false)) &&
  725. Registry->KeyExists(SubKey);
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TConfiguration::CleanupRegistry(const UnicodeString & RegistryPath)
  729. {
  730. std::unique_ptr<TRegistryStorage> Registry(new TRegistryStorage(RegistryStorageKey));
  731. UnicodeString ParentKey = ExtractFileDir(RegistryPath);
  732. if (ParentKey.IsEmpty() ||
  733. Registry->OpenSubKeyPath(ParentKey, false))
  734. {
  735. UnicodeString SubKey = ExtractFileName(RegistryPath);
  736. Registry->RecursiveDeleteSubKey(SubKey);
  737. }
  738. }
  739. //---------------------------------------------------------------------------
  740. TStrings * TConfiguration::GetCaches()
  741. {
  742. std::unique_ptr<TStrings> Result(new TStringList());
  743. Result->Add(SshHostKeysSubKey);
  744. Result->Add(FtpsCertificateStorageKey);
  745. Result->Add(HttpsCertificateStorageKey);
  746. Result->Add(DirectoryStatisticsCacheKey);
  747. Result->Add(TPath::Combine(ConfigurationSubKey, CDCacheKey));
  748. Result->Add(TPath::Combine(ConfigurationSubKey, BannersKey));
  749. Result->Add(TPath::Combine(ConfigurationSubKey, LastFingerprintsStorageKey));
  750. return Result.release();
  751. }
  752. //---------------------------------------------------------------------------
  753. bool __fastcall TConfiguration::HasAnyCache()
  754. {
  755. bool Result = false;
  756. std::unique_ptr<TStrings> Caches(GetCaches());
  757. for (int Index = 0; Index < Caches->Count; Index++)
  758. {
  759. if (RegistryPathExists(Caches->Strings[Index]))
  760. {
  761. Result = true;
  762. break;
  763. }
  764. }
  765. return Result;
  766. }
  767. //---------------------------------------------------------------------------
  768. void __fastcall TConfiguration::CleanupCaches()
  769. {
  770. try
  771. {
  772. std::unique_ptr<TStrings> Caches(GetCaches());
  773. for (int Index = 0; Index < Caches->Count; Index++)
  774. {
  775. CleanupRegistry(Caches->Strings[Index]);
  776. }
  777. }
  778. catch (Exception & E)
  779. {
  780. throw ExtException(&E, LoadStr(CLEANUP_CACHES_ERROR));
  781. }
  782. }
  783. //---------------------------------------------------------------------------
  784. void __fastcall TConfiguration::CleanupRandomSeedFile()
  785. {
  786. try
  787. {
  788. DontSaveRandomSeed();
  789. if (FileExists(ApiPath(RandomSeedFileName)))
  790. {
  791. DeleteFileChecked(RandomSeedFileName);
  792. }
  793. }
  794. catch (Exception &E)
  795. {
  796. throw ExtException(&E, LoadStr(CLEANUP_SEEDFILE_ERROR));
  797. }
  798. }
  799. //---------------------------------------------------------------------------
  800. void __fastcall TConfiguration::CleanupIniFile()
  801. {
  802. try
  803. {
  804. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  805. {
  806. DeleteFileChecked(IniFileStorageNameForReading);
  807. }
  808. if (Storage == stIniFile)
  809. {
  810. FDontSave = true;
  811. }
  812. }
  813. catch (Exception &E)
  814. {
  815. throw ExtException(&E, LoadStr(CLEANUP_INIFILE_ERROR));
  816. }
  817. }
  818. //---------------------------------------------------------------------------
  819. void __fastcall TConfiguration::DontSave()
  820. {
  821. FDontSave = true;
  822. }
  823. //---------------------------------------------------------------------------
  824. RawByteString __fastcall TConfiguration::EncryptPassword(UnicodeString Password, UnicodeString Key)
  825. {
  826. if (Password.IsEmpty())
  827. {
  828. return RawByteString();
  829. }
  830. else
  831. {
  832. return ::EncryptPassword(Password, Key);
  833. }
  834. }
  835. //---------------------------------------------------------------------------
  836. UnicodeString __fastcall TConfiguration::DecryptPassword(RawByteString Password, UnicodeString Key)
  837. {
  838. if (Password.IsEmpty())
  839. {
  840. return UnicodeString();
  841. }
  842. else
  843. {
  844. return ::DecryptPassword(Password, Key);
  845. }
  846. }
  847. //---------------------------------------------------------------------------
  848. RawByteString __fastcall TConfiguration::StronglyRecryptPassword(RawByteString Password, UnicodeString /*Key*/)
  849. {
  850. return Password;
  851. }
  852. //---------------------------------------------------------------------------
  853. TVSFixedFileInfo *__fastcall TConfiguration::GetFixedApplicationInfo()
  854. {
  855. return GetFixedFileInfo(ApplicationInfo);
  856. }
  857. //---------------------------------------------------------------------------
  858. int __fastcall TConfiguration::GetCompoundVersion()
  859. {
  860. TVSFixedFileInfo * FileInfo = FixedApplicationInfo;
  861. return CalculateCompoundVersion(
  862. HIWORD(FileInfo->dwFileVersionMS), LOWORD(FileInfo->dwFileVersionMS),
  863. HIWORD(FileInfo->dwFileVersionLS));
  864. }
  865. //---------------------------------------------------------------------------
  866. UnicodeString __fastcall TConfiguration::ModuleFileName()
  867. {
  868. return ParamStr(0);
  869. }
  870. //---------------------------------------------------------------------------
  871. void * __fastcall TConfiguration::GetFileApplicationInfo(const UnicodeString FileName)
  872. {
  873. void * Result;
  874. if (FileName.IsEmpty())
  875. {
  876. if (!FApplicationInfo)
  877. {
  878. FApplicationInfo = CreateFileInfo(ModuleFileName());
  879. }
  880. Result = FApplicationInfo;
  881. }
  882. else
  883. {
  884. Result = CreateFileInfo(FileName);
  885. }
  886. return Result;
  887. }
  888. //---------------------------------------------------------------------------
  889. void * __fastcall TConfiguration::GetApplicationInfo()
  890. {
  891. return GetFileApplicationInfo("");
  892. }
  893. //---------------------------------------------------------------------------
  894. UnicodeString __fastcall TConfiguration::GetFileProductName(const UnicodeString FileName)
  895. {
  896. return GetFileFileInfoString(L"ProductName", FileName);
  897. }
  898. //---------------------------------------------------------------------------
  899. UnicodeString __fastcall TConfiguration::GetFileCompanyName(const UnicodeString FileName)
  900. {
  901. // particularly in IDE build, company name is empty
  902. return GetFileFileInfoString(L"CompanyName", FileName, true);
  903. }
  904. //---------------------------------------------------------------------------
  905. UnicodeString __fastcall TConfiguration::GetProductName()
  906. {
  907. return GetFileProductName(L"");
  908. }
  909. //---------------------------------------------------------------------------
  910. UnicodeString __fastcall TConfiguration::GetCompanyName()
  911. {
  912. return GetFileCompanyName(L"");
  913. }
  914. //---------------------------------------------------------------------------
  915. UnicodeString __fastcall TConfiguration::GetFileProductVersion(const UnicodeString FileName)
  916. {
  917. return TrimVersion(GetFileFileInfoString(L"ProductVersion", FileName));
  918. }
  919. //---------------------------------------------------------------------------
  920. UnicodeString __fastcall TConfiguration::GetFileDescription(const UnicodeString & FileName)
  921. {
  922. return GetFileFileInfoString(L"FileDescription", FileName);
  923. }
  924. //---------------------------------------------------------------------------
  925. UnicodeString __fastcall TConfiguration::GetProductVersion()
  926. {
  927. return GetFileProductVersion(L"");
  928. }
  929. //---------------------------------------------------------------------------
  930. UnicodeString __fastcall TConfiguration::GetReleaseType()
  931. {
  932. return GetFileInfoString(L"ReleaseType");
  933. }
  934. //---------------------------------------------------------------------------
  935. bool __fastcall TConfiguration::GetIsUnofficial()
  936. {
  937. #ifdef BUILD_OFFICIAL
  938. return false;
  939. #else
  940. return true;
  941. #endif
  942. }
  943. //---------------------------------------------------------------------------
  944. UnicodeString __fastcall TConfiguration::GetVersionStr()
  945. {
  946. TGuard Guard(FCriticalSection);
  947. try
  948. {
  949. UnicodeString BuildStr;
  950. if (!IsUnofficial)
  951. {
  952. BuildStr = LoadStr(VERSION_BUILD);
  953. }
  954. else
  955. {
  956. #ifdef _DEBUG
  957. BuildStr = LoadStr(VERSION_DEBUG_BUILD);
  958. #else
  959. BuildStr = LoadStr(VERSION_DEV_BUILD);
  960. #endif
  961. }
  962. int Build = LOWORD(FixedApplicationInfo->dwFileVersionLS);
  963. if (Build > 0)
  964. {
  965. BuildStr += L" " + IntToStr(Build);
  966. }
  967. UnicodeString BuildDate = __DATE__;
  968. UnicodeString MonthStr = CutToChar(BuildDate, L' ', true);
  969. int Month = ParseShortEngMonthName(MonthStr);
  970. int Day = StrToInt(CutToChar(BuildDate, L' ', true));
  971. int Year = StrToInt(Trim(BuildDate));
  972. UnicodeString DateStr = FORMAT(L"%d-%2.2d-%2.2d", (Year, Month, Day));
  973. AddToList(BuildStr, DateStr, L" ");
  974. UnicodeString FullVersion = Version;
  975. UnicodeString AReleaseType = GetReleaseType();
  976. if (DebugAlwaysTrue(!AReleaseType.IsEmpty()) &&
  977. !SameText(AReleaseType, L"stable") &&
  978. !SameText(AReleaseType, L"development"))
  979. {
  980. FullVersion += L" " + AReleaseType;
  981. }
  982. UnicodeString Result = FMTLOAD(VERSION2, (FullVersion, BuildStr));
  983. #ifndef BUILD_OFFICIAL
  984. Result += L" " + LoadStr(VERSION_DONT_DISTRIBUTE);
  985. #endif
  986. return Result;
  987. }
  988. catch (Exception &E)
  989. {
  990. throw ExtException(&E, L"Can't get application version");
  991. }
  992. }
  993. //---------------------------------------------------------------------------
  994. UnicodeString __fastcall TConfiguration::GetFileVersion(const UnicodeString & FileName)
  995. {
  996. UnicodeString Result;
  997. void * FileInfo = CreateFileInfo(FileName);
  998. try
  999. {
  1000. Result = GetFileVersion(GetFixedFileInfo(FileInfo));
  1001. }
  1002. __finally
  1003. {
  1004. FreeFileInfo(FileInfo);
  1005. }
  1006. return Result;
  1007. }
  1008. //---------------------------------------------------------------------------
  1009. UnicodeString __fastcall TConfiguration::GetFileVersion(TVSFixedFileInfo * Info)
  1010. {
  1011. TGuard Guard(FCriticalSection);
  1012. try
  1013. {
  1014. UnicodeString Result =
  1015. FormatVersion(
  1016. HIWORD(Info->dwFileVersionMS),
  1017. LOWORD(Info->dwFileVersionMS),
  1018. HIWORD(Info->dwFileVersionLS));
  1019. return Result;
  1020. }
  1021. catch (Exception &E)
  1022. {
  1023. throw ExtException(&E, L"Can't get file version");
  1024. }
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. UnicodeString __fastcall TConfiguration::GetVersion()
  1028. {
  1029. return GetFileVersion(FixedApplicationInfo);
  1030. }
  1031. //---------------------------------------------------------------------------
  1032. UnicodeString __fastcall TConfiguration::GetFileFileInfoString(const UnicodeString Key,
  1033. const UnicodeString FileName, bool AllowEmpty)
  1034. {
  1035. TGuard Guard(FCriticalSection);
  1036. UnicodeString Result;
  1037. void * Info = GetFileApplicationInfo(FileName);
  1038. try
  1039. {
  1040. if ((Info != NULL) && (GetTranslationCount(Info) > 0))
  1041. {
  1042. TTranslation Translation;
  1043. Translation = GetTranslation(Info, 0);
  1044. Result = ::GetFileInfoString(Info, Translation, Key, AllowEmpty);
  1045. }
  1046. else
  1047. {
  1048. DebugAssert(!FileName.IsEmpty());
  1049. }
  1050. }
  1051. __finally
  1052. {
  1053. if (!FileName.IsEmpty())
  1054. {
  1055. FreeFileInfo(Info);
  1056. }
  1057. }
  1058. return Result;
  1059. }
  1060. //---------------------------------------------------------------------------
  1061. UnicodeString __fastcall TConfiguration::GetFileInfoString(const UnicodeString Key)
  1062. {
  1063. return GetFileFileInfoString(Key, L"");
  1064. }
  1065. //---------------------------------------------------------------------------
  1066. UnicodeString __fastcall TConfiguration::GetFileMimeType(const UnicodeString & FileName)
  1067. {
  1068. UnicodeString Result;
  1069. bool Found = false;
  1070. if (!MimeTypes.IsEmpty())
  1071. {
  1072. UnicodeString FileNameOnly = ExtractFileName(FileName);
  1073. UnicodeString AMimeTypes = MimeTypes;
  1074. while (!Found && !AMimeTypes.IsEmpty())
  1075. {
  1076. UnicodeString Token = CutToChar(AMimeTypes, L',', true);
  1077. UnicodeString MaskStr = CutToChar(Token, L'=', true);
  1078. TFileMasks Mask(MaskStr);
  1079. if (Mask.Matches(FileNameOnly))
  1080. {
  1081. Result = Token.Trim();
  1082. Found = true;
  1083. }
  1084. }
  1085. }
  1086. if (!Found) // allow an override to "no" Content-Type
  1087. {
  1088. Result = ::GetFileMimeType(FileName);
  1089. }
  1090. return Result;
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. UnicodeString __fastcall TConfiguration::GetRegistryStorageKey()
  1094. {
  1095. return GetRegistryKey();
  1096. }
  1097. //---------------------------------------------------------------------------
  1098. void __fastcall TConfiguration::SetNulStorage()
  1099. {
  1100. FStorage = stNul;
  1101. }
  1102. //---------------------------------------------------------------------------
  1103. void __fastcall TConfiguration::SetDefaultStorage()
  1104. {
  1105. FStorage = stDetect;
  1106. }
  1107. //---------------------------------------------------------------------------
  1108. void __fastcall TConfiguration::SetExplicitIniFileStorageName(const UnicodeString & FileName)
  1109. {
  1110. FIniFileStorageName = FileName;
  1111. FStorage = stIniFile;
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. UnicodeString __fastcall TConfiguration::GetDefaultIniFileExportPath()
  1115. {
  1116. UnicodeString PersonalDirectory = GetPersonalFolder();
  1117. UnicodeString FileName = IncludeTrailingBackslash(PersonalDirectory) +
  1118. ExtractFileName(ExpandEnvironmentVariables(IniFileStorageName));
  1119. return FileName;
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReading()
  1123. {
  1124. return GetIniFileStorageName(true);
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReadingWriting()
  1128. {
  1129. return GetIniFileStorageName(false);
  1130. }
  1131. //---------------------------------------------------------------------------
  1132. UnicodeString __fastcall TConfiguration::GetAutomaticIniFileStorageName(bool ReadingOnly)
  1133. {
  1134. UnicodeString ProgramPath = ParamStr(0);
  1135. UnicodeString ProgramIniPath = ChangeFileExt(ProgramPath, L".ini");
  1136. UnicodeString IniPath;
  1137. if (FileExists(ApiPath(ProgramIniPath)))
  1138. {
  1139. IniPath = ProgramIniPath;
  1140. }
  1141. else
  1142. {
  1143. UnicodeString AppDataIniPath =
  1144. IncludeTrailingBackslash(GetShellFolderPath(CSIDL_APPDATA)) +
  1145. ExtractFileName(ProgramIniPath);
  1146. if (FileExists(ApiPath(AppDataIniPath)))
  1147. {
  1148. IniPath = AppDataIniPath;
  1149. }
  1150. else
  1151. {
  1152. // avoid expensive test if we are interested in existing files only
  1153. if (!ReadingOnly && (FProgramIniPathWrittable < 0))
  1154. {
  1155. UnicodeString ProgramDir = ExtractFilePath(ProgramPath);
  1156. FProgramIniPathWrittable = IsDirectoryWriteable(ProgramDir) ? 1 : 0;
  1157. }
  1158. // does not really matter what we return when < 0
  1159. IniPath = (FProgramIniPathWrittable == 0) ? AppDataIniPath : ProgramIniPath;
  1160. }
  1161. }
  1162. // BACKWARD COMPATIBILITY with 4.x
  1163. if (FVirtualIniFileStorageName.IsEmpty() &&
  1164. TPath::IsDriveRooted(IniPath))
  1165. {
  1166. UnicodeString LocalAppDataPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  1167. // virtual store for non-system drives have a different virtual store,
  1168. // do not bother about them
  1169. if (TPath::IsDriveRooted(LocalAppDataPath) &&
  1170. SameText(ExtractFileDrive(IniPath), ExtractFileDrive(LocalAppDataPath)))
  1171. {
  1172. FVirtualIniFileStorageName =
  1173. IncludeTrailingBackslash(LocalAppDataPath) +
  1174. L"VirtualStore\\" +
  1175. IniPath.SubString(4, IniPath.Length() - 3);
  1176. }
  1177. }
  1178. if (!FVirtualIniFileStorageName.IsEmpty() &&
  1179. FileExists(ApiPath(FVirtualIniFileStorageName)))
  1180. {
  1181. return FVirtualIniFileStorageName;
  1182. }
  1183. else
  1184. {
  1185. return IniPath;
  1186. }
  1187. }
  1188. //---------------------------------------------------------------------------
  1189. UnicodeString __fastcall TConfiguration::GetIniFileParamValue()
  1190. {
  1191. UnicodeString Result;
  1192. if (Storage == stNul)
  1193. {
  1194. Result = INI_NUL;
  1195. }
  1196. else if ((Storage == stIniFile) && !FIniFileStorageName.IsEmpty())
  1197. {
  1198. Result = FIniFileStorageName;
  1199. }
  1200. return Result;
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. UnicodeString __fastcall TConfiguration::GetIniFileStorageName(bool ReadingOnly)
  1204. {
  1205. UnicodeString Result;
  1206. if (!FIniFileStorageName.IsEmpty())
  1207. {
  1208. Result = FIniFileStorageName;
  1209. }
  1210. else if (!FCustomIniFileStorageName.IsEmpty())
  1211. {
  1212. Result = FCustomIniFileStorageName;
  1213. }
  1214. else
  1215. {
  1216. Result = GetAutomaticIniFileStorageName(ReadingOnly);
  1217. }
  1218. return Result;
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. void __fastcall TConfiguration::SetOptionsStorage(TStrings * value)
  1222. {
  1223. TGuard Guard(FCriticalSection);
  1224. if (FOptionsStorage.get() == NULL)
  1225. {
  1226. FOptionsStorage.reset(new TStringList());
  1227. }
  1228. FOptionsStorage->AddStrings(value);
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. TStrings * __fastcall TConfiguration::GetOptionsStorage()
  1232. {
  1233. return FOptionsStorage.get();
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. UnicodeString __fastcall TConfiguration::GetPuttySessionsSubKey()
  1237. {
  1238. return StoredSessionsSubKey;
  1239. }
  1240. //---------------------------------------------------------------------------
  1241. UnicodeString __fastcall TConfiguration::GetPuttySessionsKey()
  1242. {
  1243. return PuttyRegistryStorageKey + L"\\" + PuttySessionsSubKey;
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. UnicodeString __fastcall TConfiguration::GetStoredSessionsSubKey()
  1247. {
  1248. return L"Sessions";
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. UnicodeString __fastcall TConfiguration::GetSshHostKeysSubKey()
  1252. {
  1253. return L"SshHostKeys";
  1254. }
  1255. //---------------------------------------------------------------------------
  1256. UnicodeString __fastcall TConfiguration::GetConfigurationSubKey()
  1257. {
  1258. return L"Configuration";
  1259. }
  1260. //---------------------------------------------------------------------------
  1261. UnicodeString __fastcall TConfiguration::GetRootKeyStr()
  1262. {
  1263. return RootKeyToStr(HKEY_CURRENT_USER);
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TConfiguration::MoveStorage(TStorage AStorage, const UnicodeString & ACustomIniFileStorageName)
  1267. {
  1268. if ((FStorage != AStorage) ||
  1269. ((FStorage == stIniFile) && !FIniFileStorageName.IsEmpty()) ||
  1270. !IsPathToSameFile(FCustomIniFileStorageName, ACustomIniFileStorageName))
  1271. {
  1272. TStorage StorageBak = FStorage;
  1273. UnicodeString CustomIniFileStorageNameBak = FCustomIniFileStorageName;
  1274. UnicodeString IniFileStorageNameBak = FIniFileStorageName;
  1275. try
  1276. {
  1277. THierarchicalStorage * SourceStorage = NULL;
  1278. THierarchicalStorage * TargetStorage = NULL;
  1279. try
  1280. {
  1281. SourceStorage = CreateConfigStorage();
  1282. SourceStorage->AccessMode = smRead;
  1283. FStorage = AStorage;
  1284. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1285. FIniFileStorageName = UnicodeString();
  1286. TargetStorage = CreateConfigStorage();
  1287. TargetStorage->AccessMode = smReadWrite;
  1288. TargetStorage->Explicit = true;
  1289. // copy before save as it removes the ini file,
  1290. // when switching from ini to registry
  1291. CopyData(SourceStorage, TargetStorage);
  1292. }
  1293. __finally
  1294. {
  1295. delete SourceStorage;
  1296. delete TargetStorage;
  1297. }
  1298. // save all and explicit,
  1299. // this also removes an INI file, when switching to registry storage
  1300. DoSave(true, true);
  1301. }
  1302. catch (...)
  1303. {
  1304. // If this fails, do not pretend that storage was switched.
  1305. // For instance:
  1306. // - When writing to an INI file fails (unlikely, as we fallback to user profile)
  1307. // - When removing INI file fails, when switching to registry
  1308. // (possible, when the INI file is in Program Files folder)
  1309. FStorage = StorageBak;
  1310. FCustomIniFileStorageName = CustomIniFileStorageNameBak;
  1311. FIniFileStorageName = IniFileStorageNameBak;
  1312. throw;
  1313. }
  1314. }
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. void __fastcall TConfiguration::ScheduleCustomIniFileStorageUse(const UnicodeString & ACustomIniFileStorageName)
  1318. {
  1319. FStorage = stIniFile;
  1320. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1321. SaveCustomIniFileStorageName();
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TConfiguration::Saved()
  1325. {
  1326. // nothing
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. TStorage __fastcall TConfiguration::GetStorage()
  1330. {
  1331. TGuard Guard(FCriticalSection);
  1332. if (FStorage == stDetect)
  1333. {
  1334. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  1335. {
  1336. FStorage = stIniFile;
  1337. }
  1338. else
  1339. {
  1340. FStorage = stRegistry;
  1341. }
  1342. }
  1343. return FStorage;
  1344. }
  1345. //---------------------------------------------------------------------
  1346. TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport(
  1347. TStoredSessionList * Sessions, UnicodeString & Error)
  1348. {
  1349. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1350. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1351. UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
  1352. UnicodeString FilezillaSiteManagerFile =
  1353. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\sitemanager.xml";
  1354. UnicodeString FilezillaConfigurationFile =
  1355. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\filezilla.xml";
  1356. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1357. {
  1358. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile, FilezillaConfigurationFile);
  1359. if (ImportSessionList->Count > 0)
  1360. {
  1361. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1362. }
  1363. else
  1364. {
  1365. Error = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1366. }
  1367. }
  1368. else
  1369. {
  1370. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1371. }
  1372. return ImportSessionList.release();
  1373. }
  1374. //---------------------------------------------------------------------
  1375. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1376. {
  1377. try
  1378. {
  1379. UnicodeString Error;
  1380. std::unique_ptr<TStoredSessionList> Sesssions(SelectFilezillaSessionsForImport(Sessions, Error));
  1381. return (Sesssions->Count > 0);
  1382. }
  1383. catch (...)
  1384. {
  1385. return false;
  1386. }
  1387. }
  1388. //---------------------------------------------------------------------
  1389. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1390. TStoredSessionList * Sessions, UnicodeString & Error)
  1391. {
  1392. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1393. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1394. UnicodeString ProfilePath = GetShellFolderPath(CSIDL_PROFILE);
  1395. UnicodeString KnownHostsFile = IncludeTrailingBackslash(ProfilePath) + L".ssh\\known_hosts";
  1396. try
  1397. {
  1398. if (FileExists(ApiPath(KnownHostsFile)))
  1399. {
  1400. std::unique_ptr<TStrings> Lines(new TStringList());
  1401. LoadScriptFromFile(KnownHostsFile, Lines.get(), true);
  1402. ImportSessionList->ImportFromKnownHosts(Lines.get());
  1403. }
  1404. else
  1405. {
  1406. throw Exception(LoadStr(KNOWN_HOSTS_NOT_FOUND));
  1407. }
  1408. }
  1409. catch (Exception & E)
  1410. {
  1411. Error = FORMAT(L"%s\n(%s)", (E.Message, KnownHostsFile));
  1412. }
  1413. return ImportSessionList.release();
  1414. }
  1415. //---------------------------------------------------------------------
  1416. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1417. TStrings * Lines, TStoredSessionList * Sessions, UnicodeString & Error)
  1418. {
  1419. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1420. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1421. try
  1422. {
  1423. ImportSessionList->ImportFromKnownHosts(Lines);
  1424. }
  1425. catch (Exception & E)
  1426. {
  1427. Error = E.Message;
  1428. }
  1429. return ImportSessionList.release();
  1430. }
  1431. //---------------------------------------------------------------------------
  1432. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1433. {
  1434. if (RandomSeedFile != value)
  1435. {
  1436. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1437. FRandomSeedFile = value;
  1438. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1439. if (RandomSeedFileName.IsEmpty())
  1440. {
  1441. FRandomSeedFile = FDefaultRandomSeedFile;
  1442. }
  1443. if (!PrevRandomSeedFileName.IsEmpty() &&
  1444. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1445. FileExists(ApiPath(PrevRandomSeedFileName)))
  1446. {
  1447. // ignore any error
  1448. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1449. }
  1450. }
  1451. }
  1452. //---------------------------------------------------------------------------
  1453. UnicodeString __fastcall TConfiguration::GetDirectoryStatisticsCacheKey(
  1454. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1455. {
  1456. std::unique_ptr<TStringList> RawOptions(new TStringList());
  1457. RawOptions->Add(SessionKey);
  1458. RawOptions->Add(UnixExcludeTrailingBackslash(Path));
  1459. TCopyParamType Defaults;
  1460. TCopyParamType FilterCopyParam;
  1461. FilterCopyParam.IncludeFileMask = CopyParam.IncludeFileMask;
  1462. FilterCopyParam.ExcludeHiddenFiles = CopyParam.ExcludeHiddenFiles;
  1463. FilterCopyParam.ExcludeEmptyDirectories = CopyParam.ExcludeEmptyDirectories;
  1464. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawOptions.get(), true));
  1465. FilterCopyParam.Save(OptionsStorage.get(), &Defaults);
  1466. UTF8String RawOptionsBuf(RawOptions->CommaText.LowerCase());
  1467. UnicodeString Result = Sha256(RawOptionsBuf.c_str(), RawOptionsBuf.Length());
  1468. return Result;
  1469. }
  1470. //---------------------------------------------------------------------------
  1471. THierarchicalStorage * TConfiguration::OpenDirectoryStatisticsCache(bool CanCreate)
  1472. {
  1473. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateConfigStorage());
  1474. Storage->AccessMode = CanCreate ? smReadWrite : smRead;
  1475. if (!Storage->OpenSubKey(DirectoryStatisticsCacheKey, CanCreate))
  1476. {
  1477. Storage.reset(NULL);
  1478. }
  1479. return Storage.release();
  1480. }
  1481. //---------------------------------------------------------------------------
  1482. TStrings * __fastcall TConfiguration::LoadDirectoryStatisticsCache(
  1483. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1484. {
  1485. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(false));
  1486. std::unique_ptr<TStringList> Result(new TStringList());
  1487. if (Storage.get() != NULL)
  1488. {
  1489. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1490. UnicodeString Buf = Storage->ReadString(Key, UnicodeString());
  1491. Result->CommaText = Buf;
  1492. }
  1493. return Result.release();
  1494. }
  1495. //---------------------------------------------------------------------------
  1496. void __fastcall TConfiguration::SaveDirectoryStatisticsCache(
  1497. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam, TStrings * DataList)
  1498. {
  1499. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(true));
  1500. if (Storage.get() != NULL)
  1501. {
  1502. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1503. UnicodeString Buf = DataList->CommaText;
  1504. Storage->WriteString(Key, Buf);
  1505. }
  1506. }
  1507. //---------------------------------------------------------------------
  1508. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1509. {
  1510. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1511. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1512. }
  1513. //---------------------------------------------------------------------
  1514. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1515. {
  1516. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1517. }
  1518. //---------------------------------------------------------------------
  1519. bool TConfiguration::HasLocalPortNumberLimits()
  1520. {
  1521. return (LocalPortNumberMin > 0) && (LocalPortNumberMax >= LocalPortNumberMin);
  1522. }
  1523. //---------------------------------------------------------------------
  1524. void TConfiguration::SetLocalPortNumberMin(int value)
  1525. {
  1526. SET_CONFIG_PROPERTY(LocalPortNumberMin);
  1527. }
  1528. //---------------------------------------------------------------------
  1529. void TConfiguration::SetLocalPortNumberMax(int value)
  1530. {
  1531. SET_CONFIG_PROPERTY(LocalPortNumberMax);
  1532. }
  1533. //---------------------------------------------------------------------
  1534. void __fastcall TConfiguration::SetMimeTypes(UnicodeString value)
  1535. {
  1536. SET_CONFIG_PROPERTY(MimeTypes);
  1537. }
  1538. //---------------------------------------------------------------------
  1539. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1540. {
  1541. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1542. }
  1543. //---------------------------------------------------------------------
  1544. void __fastcall TConfiguration::SetParallelDurationThreshold(int value)
  1545. {
  1546. SET_CONFIG_PROPERTY(ParallelDurationThreshold);
  1547. }
  1548. //---------------------------------------------------------------------
  1549. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1550. {
  1551. SET_CONFIG_PROPERTY(PuttyRegistryStorageKey);
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1555. {
  1556. return eolCRLF;
  1557. }
  1558. //---------------------------------------------------------------------
  1559. bool __fastcall TConfiguration::GetCollectUsage()
  1560. {
  1561. return FUsage->Collect;
  1562. }
  1563. //---------------------------------------------------------------------
  1564. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1565. {
  1566. FUsage->Collect = value;
  1567. }
  1568. //---------------------------------------------------------------------
  1569. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1570. {
  1571. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1572. {
  1573. TemporaryActionsLogging(ALogFileName);
  1574. }
  1575. else
  1576. {
  1577. FLogging = true;
  1578. FLogFileName = ALogFileName;
  1579. UpdateActualLogProtocol();
  1580. }
  1581. }
  1582. //---------------------------------------------------------------------
  1583. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1584. {
  1585. FLogActions = true;
  1586. FActionsLogFileName = ALogFileName;
  1587. }
  1588. //---------------------------------------------------------------------
  1589. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1590. {
  1591. FLogProtocol = ALogProtocol;
  1592. UpdateActualLogProtocol();
  1593. }
  1594. //---------------------------------------------------------------------
  1595. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1596. {
  1597. FLogSensitive = ALogSensitive;
  1598. }
  1599. //---------------------------------------------------------------------
  1600. void __fastcall TConfiguration::TemporaryLogMaxSize(__int64 ALogMaxSize)
  1601. {
  1602. FLogMaxSize = ALogMaxSize;
  1603. }
  1604. //---------------------------------------------------------------------
  1605. void __fastcall TConfiguration::TemporaryLogMaxCount(int ALogMaxCount)
  1606. {
  1607. FLogMaxCount = ALogMaxCount;
  1608. }
  1609. //---------------------------------------------------------------------
  1610. void __fastcall TConfiguration::SetLogging(bool value)
  1611. {
  1612. TGuard Guard(FCriticalSection);
  1613. if (Logging != value)
  1614. {
  1615. FPermanentLogging = value;
  1616. FLogging = value;
  1617. UpdateActualLogProtocol();
  1618. Changed();
  1619. }
  1620. }
  1621. //---------------------------------------------------------------------
  1622. bool __fastcall TConfiguration::GetLogging()
  1623. {
  1624. TGuard Guard(FCriticalSection);
  1625. return FPermanentLogging;
  1626. }
  1627. //---------------------------------------------------------------------
  1628. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  1629. {
  1630. TGuard Guard(FCriticalSection);
  1631. if (LogFileName != value)
  1632. {
  1633. FPermanentLogFileName = value;
  1634. FLogFileName = value;
  1635. Changed();
  1636. }
  1637. }
  1638. //---------------------------------------------------------------------
  1639. UnicodeString __fastcall TConfiguration::GetLogFileName()
  1640. {
  1641. TGuard Guard(FCriticalSection);
  1642. return FPermanentLogFileName;
  1643. }
  1644. //---------------------------------------------------------------------
  1645. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  1646. {
  1647. TGuard Guard(FCriticalSection);
  1648. if (ActionsLogFileName != value)
  1649. {
  1650. FPermanentActionsLogFileName = value;
  1651. FActionsLogFileName = value;
  1652. Changed();
  1653. }
  1654. }
  1655. //---------------------------------------------------------------------
  1656. UnicodeString __fastcall TConfiguration::GetPermanentActionsLogFileName()
  1657. {
  1658. TGuard Guard(FCriticalSection);
  1659. return FPermanentActionsLogFileName;
  1660. }
  1661. //---------------------------------------------------------------------
  1662. UnicodeString __fastcall TConfiguration::GetActionsLogFileName()
  1663. {
  1664. TGuard Guard(FCriticalSection);
  1665. return FActionsLogFileName;
  1666. }
  1667. //---------------------------------------------------------------------
  1668. bool __fastcall TConfiguration::GetLogToFile()
  1669. {
  1670. // guarded within GetLogFileName
  1671. return !LogFileName.IsEmpty();
  1672. }
  1673. //---------------------------------------------------------------------
  1674. void __fastcall TConfiguration::UpdateActualLogProtocol()
  1675. {
  1676. FActualLogProtocol = FLogging ? FLogProtocol : (-BelowNormalLogLevels - 1);
  1677. }
  1678. //---------------------------------------------------------------------
  1679. void __fastcall TConfiguration::SetLogProtocol(int value)
  1680. {
  1681. TGuard Guard(FCriticalSection);
  1682. if (LogProtocol != value)
  1683. {
  1684. FPermanentLogProtocol = value;
  1685. FLogProtocol = value;
  1686. Changed();
  1687. UpdateActualLogProtocol();
  1688. }
  1689. }
  1690. //---------------------------------------------------------------------
  1691. void __fastcall TConfiguration::SetLogActions(bool value)
  1692. {
  1693. TGuard Guard(FCriticalSection);
  1694. if (LogActions != value)
  1695. {
  1696. FPermanentLogActions = value;
  1697. FLogActions = value;
  1698. Changed();
  1699. }
  1700. }
  1701. //---------------------------------------------------------------------
  1702. bool __fastcall TConfiguration::GetLogActions()
  1703. {
  1704. TGuard Guard(FCriticalSection);
  1705. return FPermanentLogActions;
  1706. }
  1707. //---------------------------------------------------------------------
  1708. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  1709. {
  1710. SET_CONFIG_PROPERTY(LogFileAppend);
  1711. }
  1712. //---------------------------------------------------------------------
  1713. void __fastcall TConfiguration::SetLogSensitive(bool value)
  1714. {
  1715. if (LogSensitive != value)
  1716. {
  1717. FPermanentLogSensitive = value;
  1718. FLogSensitive = value;
  1719. Changed();
  1720. }
  1721. }
  1722. //---------------------------------------------------------------------
  1723. void __fastcall TConfiguration::SetLogMaxSize(__int64 value)
  1724. {
  1725. TGuard Guard(FCriticalSection);
  1726. if (LogMaxSize != value)
  1727. {
  1728. FPermanentLogMaxSize = value;
  1729. FLogMaxSize = value;
  1730. Changed();
  1731. }
  1732. }
  1733. //---------------------------------------------------------------------
  1734. __int64 __fastcall TConfiguration::GetLogMaxSize()
  1735. {
  1736. TGuard Guard(FCriticalSection);
  1737. return FPermanentLogMaxSize;
  1738. }
  1739. //---------------------------------------------------------------------
  1740. void __fastcall TConfiguration::SetLogMaxCount(int value)
  1741. {
  1742. if (LogMaxCount != value)
  1743. {
  1744. FPermanentLogMaxCount = value;
  1745. FLogMaxCount = value;
  1746. Changed();
  1747. }
  1748. }
  1749. //---------------------------------------------------------------------
  1750. int __fastcall TConfiguration::GetLogMaxCount()
  1751. {
  1752. TGuard Guard(FCriticalSection);
  1753. return FPermanentLogMaxCount;
  1754. }
  1755. //---------------------------------------------------------------------
  1756. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  1757. {
  1758. return L"%TEMP%\\!S.log";
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  1762. {
  1763. TGuard Guard(FCriticalSection);
  1764. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  1765. }
  1766. //---------------------------------------------------------------------------
  1767. bool __fastcall TConfiguration::GetConfirmOverwriting()
  1768. {
  1769. TGuard Guard(FCriticalSection);
  1770. return FConfirmOverwriting;
  1771. }
  1772. //---------------------------------------------------------------------------
  1773. void __fastcall TConfiguration::SetConfirmResume(bool value)
  1774. {
  1775. TGuard Guard(FCriticalSection);
  1776. SET_CONFIG_PROPERTY(ConfirmResume);
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. bool __fastcall TConfiguration::GetConfirmResume()
  1780. {
  1781. TGuard Guard(FCriticalSection);
  1782. return FConfirmResume;
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  1786. {
  1787. TGuard Guard(FCriticalSection);
  1788. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  1789. }
  1790. //---------------------------------------------------------------------------
  1791. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  1792. {
  1793. TGuard Guard(FCriticalSection);
  1794. return FAutoReadDirectoryAfterOp;
  1795. }
  1796. //---------------------------------------------------------------------------
  1797. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  1798. {
  1799. return L"h:nn:ss";
  1800. }
  1801. //---------------------------------------------------------------------------
  1802. UnicodeString __fastcall TConfiguration::GetPartialExt() const
  1803. {
  1804. return PARTIAL_EXT;
  1805. }
  1806. //---------------------------------------------------------------------------
  1807. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  1808. {
  1809. return L"";
  1810. }
  1811. //---------------------------------------------------------------------------
  1812. bool __fastcall TConfiguration::GetRememberPassword()
  1813. {
  1814. return false;
  1815. }
  1816. //---------------------------------------------------------------------------
  1817. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  1818. {
  1819. SET_CONFIG_PROPERTY(SessionReopenAuto);
  1820. }
  1821. //---------------------------------------------------------------------------
  1822. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  1823. {
  1824. SET_CONFIG_PROPERTY(SessionReopenBackground);
  1825. }
  1826. //---------------------------------------------------------------------------
  1827. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  1828. {
  1829. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  1830. }
  1831. //---------------------------------------------------------------------------
  1832. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  1833. {
  1834. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  1835. }
  1836. //---------------------------------------------------------------------------
  1837. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  1838. {
  1839. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  1840. }
  1841. //---------------------------------------------------------------------------
  1842. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  1843. {
  1844. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  1845. }
  1846. //---------------------------------------------------------------------------
  1847. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  1848. {
  1849. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  1850. }
  1851. //---------------------------------------------------------------------------
  1852. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  1853. {
  1854. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  1855. }
  1856. //---------------------------------------------------------------------------
  1857. bool __fastcall TConfiguration::GetPersistent()
  1858. {
  1859. return (Storage != stNul) && !FDontSave;
  1860. }
  1861. //---------------------------------------------------------------------------
  1862. //---------------------------------------------------------------------------
  1863. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  1864. {
  1865. FShortCuts.insert(ShortCut);
  1866. }
  1867. //---------------------------------------------------------------------------
  1868. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  1869. {
  1870. return (FShortCuts.count(ShortCut) != 0);
  1871. }