Configuration.cpp 65 KB

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