Configuration.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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. #ifndef BUILD_OFFICIAL
  968. UnicodeString BuildDate = __DATE__;
  969. UnicodeString MonthStr = CutToChar(BuildDate, L' ', true);
  970. int Month = ParseShortEngMonthName(MonthStr);
  971. int Day = StrToInt(CutToChar(BuildDate, L' ', true));
  972. int Year = StrToInt(Trim(BuildDate));
  973. UnicodeString DateStr = FORMAT(L"%d-%2.2d-%2.2d", (Year, Month, Day));
  974. AddToList(BuildStr, DateStr, L" ");
  975. #endif
  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. TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport(
  1349. TStoredSessionList * Sessions, UnicodeString & Error)
  1350. {
  1351. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1352. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1353. UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
  1354. UnicodeString FilezillaSiteManagerFile =
  1355. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\sitemanager.xml";
  1356. UnicodeString FilezillaConfigurationFile =
  1357. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\filezilla.xml";
  1358. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1359. {
  1360. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile, FilezillaConfigurationFile);
  1361. if (ImportSessionList->Count > 0)
  1362. {
  1363. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1364. }
  1365. else
  1366. {
  1367. Error = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1368. }
  1369. }
  1370. else
  1371. {
  1372. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1373. }
  1374. return ImportSessionList.release();
  1375. }
  1376. //---------------------------------------------------------------------
  1377. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1378. {
  1379. try
  1380. {
  1381. UnicodeString Error;
  1382. std::unique_ptr<TStoredSessionList> Sesssions(SelectFilezillaSessionsForImport(Sessions, Error));
  1383. return (Sesssions->Count > 0);
  1384. }
  1385. catch (...)
  1386. {
  1387. return false;
  1388. }
  1389. }
  1390. //---------------------------------------------------------------------
  1391. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1392. TStoredSessionList * Sessions, UnicodeString & Error)
  1393. {
  1394. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1395. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1396. UnicodeString ProfilePath = GetShellFolderPath(CSIDL_PROFILE);
  1397. UnicodeString KnownHostsFile = IncludeTrailingBackslash(ProfilePath) + L".ssh\\known_hosts";
  1398. try
  1399. {
  1400. if (FileExists(ApiPath(KnownHostsFile)))
  1401. {
  1402. std::unique_ptr<TStrings> Lines(new TStringList());
  1403. LoadScriptFromFile(KnownHostsFile, Lines.get(), true);
  1404. ImportSessionList->ImportFromKnownHosts(Lines.get());
  1405. }
  1406. else
  1407. {
  1408. throw Exception(LoadStr(KNOWN_HOSTS_NOT_FOUND));
  1409. }
  1410. }
  1411. catch (Exception & E)
  1412. {
  1413. Error = FORMAT(L"%s\n(%s)", (E.Message, KnownHostsFile));
  1414. }
  1415. return ImportSessionList.release();
  1416. }
  1417. //---------------------------------------------------------------------
  1418. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1419. TStrings * Lines, TStoredSessionList * Sessions, UnicodeString & Error)
  1420. {
  1421. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1422. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1423. try
  1424. {
  1425. ImportSessionList->ImportFromKnownHosts(Lines);
  1426. }
  1427. catch (Exception & E)
  1428. {
  1429. Error = E.Message;
  1430. }
  1431. return ImportSessionList.release();
  1432. }
  1433. //---------------------------------------------------------------------------
  1434. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1435. {
  1436. if (RandomSeedFile != value)
  1437. {
  1438. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1439. FRandomSeedFile = value;
  1440. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1441. if (RandomSeedFileName.IsEmpty())
  1442. {
  1443. FRandomSeedFile = FDefaultRandomSeedFile;
  1444. }
  1445. if (!PrevRandomSeedFileName.IsEmpty() &&
  1446. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1447. FileExists(ApiPath(PrevRandomSeedFileName)))
  1448. {
  1449. // ignore any error
  1450. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1451. }
  1452. }
  1453. }
  1454. //---------------------------------------------------------------------------
  1455. UnicodeString __fastcall TConfiguration::GetDirectoryStatisticsCacheKey(
  1456. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1457. {
  1458. std::unique_ptr<TStringList> RawOptions(new TStringList());
  1459. RawOptions->Add(SessionKey);
  1460. RawOptions->Add(UnixExcludeTrailingBackslash(Path));
  1461. TCopyParamType Defaults;
  1462. TCopyParamType FilterCopyParam;
  1463. FilterCopyParam.IncludeFileMask = CopyParam.IncludeFileMask;
  1464. FilterCopyParam.ExcludeHiddenFiles = CopyParam.ExcludeHiddenFiles;
  1465. FilterCopyParam.ExcludeEmptyDirectories = CopyParam.ExcludeEmptyDirectories;
  1466. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawOptions.get(), true));
  1467. FilterCopyParam.Save(OptionsStorage.get(), &Defaults);
  1468. UTF8String RawOptionsBuf(RawOptions->CommaText.LowerCase());
  1469. UnicodeString Result = Sha256(RawOptionsBuf.c_str(), RawOptionsBuf.Length());
  1470. return Result;
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. THierarchicalStorage * TConfiguration::OpenDirectoryStatisticsCache(bool CanCreate)
  1474. {
  1475. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateConfigStorage());
  1476. Storage->AccessMode = CanCreate ? smReadWrite : smRead;
  1477. if (!Storage->OpenSubKey(DirectoryStatisticsCacheKey, CanCreate))
  1478. {
  1479. Storage.reset(NULL);
  1480. }
  1481. return Storage.release();
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. TStrings * __fastcall TConfiguration::LoadDirectoryStatisticsCache(
  1485. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1486. {
  1487. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(false));
  1488. std::unique_ptr<TStringList> Result(new TStringList());
  1489. if (Storage.get() != NULL)
  1490. {
  1491. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1492. UnicodeString Buf = Storage->ReadString(Key, UnicodeString());
  1493. Result->CommaText = Buf;
  1494. }
  1495. return Result.release();
  1496. }
  1497. //---------------------------------------------------------------------------
  1498. void __fastcall TConfiguration::SaveDirectoryStatisticsCache(
  1499. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam, TStrings * DataList)
  1500. {
  1501. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(true));
  1502. if (Storage.get() != NULL)
  1503. {
  1504. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1505. UnicodeString Buf = DataList->CommaText;
  1506. Storage->WriteString(Key, Buf);
  1507. }
  1508. }
  1509. //---------------------------------------------------------------------
  1510. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1511. {
  1512. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1513. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1514. }
  1515. //---------------------------------------------------------------------
  1516. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1517. {
  1518. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1519. }
  1520. //---------------------------------------------------------------------
  1521. bool TConfiguration::HasLocalPortNumberLimits()
  1522. {
  1523. return (LocalPortNumberMin > 0) && (LocalPortNumberMax >= LocalPortNumberMin);
  1524. }
  1525. //---------------------------------------------------------------------
  1526. void TConfiguration::SetLocalPortNumberMin(int value)
  1527. {
  1528. SET_CONFIG_PROPERTY(LocalPortNumberMin);
  1529. }
  1530. //---------------------------------------------------------------------
  1531. void TConfiguration::SetLocalPortNumberMax(int value)
  1532. {
  1533. SET_CONFIG_PROPERTY(LocalPortNumberMax);
  1534. }
  1535. //---------------------------------------------------------------------
  1536. void __fastcall TConfiguration::SetMimeTypes(UnicodeString value)
  1537. {
  1538. SET_CONFIG_PROPERTY(MimeTypes);
  1539. }
  1540. //---------------------------------------------------------------------
  1541. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1542. {
  1543. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1544. }
  1545. //---------------------------------------------------------------------
  1546. void __fastcall TConfiguration::SetParallelDurationThreshold(int value)
  1547. {
  1548. SET_CONFIG_PROPERTY(ParallelDurationThreshold);
  1549. }
  1550. //---------------------------------------------------------------------
  1551. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1552. {
  1553. SET_CONFIG_PROPERTY(PuttyRegistryStorageKey);
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1557. {
  1558. return eolCRLF;
  1559. }
  1560. //---------------------------------------------------------------------
  1561. bool __fastcall TConfiguration::GetCollectUsage()
  1562. {
  1563. return FUsage->Collect;
  1564. }
  1565. //---------------------------------------------------------------------
  1566. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1567. {
  1568. FUsage->Collect = value;
  1569. }
  1570. //---------------------------------------------------------------------
  1571. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1572. {
  1573. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1574. {
  1575. TemporaryActionsLogging(ALogFileName);
  1576. }
  1577. else
  1578. {
  1579. FLogging = true;
  1580. FLogFileName = ALogFileName;
  1581. UpdateActualLogProtocol();
  1582. }
  1583. }
  1584. //---------------------------------------------------------------------
  1585. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1586. {
  1587. FLogActions = true;
  1588. FActionsLogFileName = ALogFileName;
  1589. }
  1590. //---------------------------------------------------------------------
  1591. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1592. {
  1593. FLogProtocol = ALogProtocol;
  1594. UpdateActualLogProtocol();
  1595. }
  1596. //---------------------------------------------------------------------
  1597. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1598. {
  1599. FLogSensitive = ALogSensitive;
  1600. }
  1601. //---------------------------------------------------------------------
  1602. void __fastcall TConfiguration::TemporaryLogMaxSize(__int64 ALogMaxSize)
  1603. {
  1604. FLogMaxSize = ALogMaxSize;
  1605. }
  1606. //---------------------------------------------------------------------
  1607. void __fastcall TConfiguration::TemporaryLogMaxCount(int ALogMaxCount)
  1608. {
  1609. FLogMaxCount = ALogMaxCount;
  1610. }
  1611. //---------------------------------------------------------------------
  1612. void __fastcall TConfiguration::SetLogging(bool value)
  1613. {
  1614. TGuard Guard(FCriticalSection);
  1615. if (Logging != value)
  1616. {
  1617. FPermanentLogging = value;
  1618. FLogging = value;
  1619. UpdateActualLogProtocol();
  1620. Changed();
  1621. }
  1622. }
  1623. //---------------------------------------------------------------------
  1624. bool __fastcall TConfiguration::GetLogging()
  1625. {
  1626. TGuard Guard(FCriticalSection);
  1627. return FPermanentLogging;
  1628. }
  1629. //---------------------------------------------------------------------
  1630. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  1631. {
  1632. TGuard Guard(FCriticalSection);
  1633. if (LogFileName != value)
  1634. {
  1635. FPermanentLogFileName = value;
  1636. FLogFileName = value;
  1637. Changed();
  1638. }
  1639. }
  1640. //---------------------------------------------------------------------
  1641. UnicodeString __fastcall TConfiguration::GetLogFileName()
  1642. {
  1643. TGuard Guard(FCriticalSection);
  1644. return FPermanentLogFileName;
  1645. }
  1646. //---------------------------------------------------------------------
  1647. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  1648. {
  1649. TGuard Guard(FCriticalSection);
  1650. if (ActionsLogFileName != value)
  1651. {
  1652. FPermanentActionsLogFileName = value;
  1653. FActionsLogFileName = value;
  1654. Changed();
  1655. }
  1656. }
  1657. //---------------------------------------------------------------------
  1658. UnicodeString __fastcall TConfiguration::GetPermanentActionsLogFileName()
  1659. {
  1660. TGuard Guard(FCriticalSection);
  1661. return FPermanentActionsLogFileName;
  1662. }
  1663. //---------------------------------------------------------------------
  1664. UnicodeString __fastcall TConfiguration::GetActionsLogFileName()
  1665. {
  1666. TGuard Guard(FCriticalSection);
  1667. return FActionsLogFileName;
  1668. }
  1669. //---------------------------------------------------------------------
  1670. bool __fastcall TConfiguration::GetLogToFile()
  1671. {
  1672. // guarded within GetLogFileName
  1673. return !LogFileName.IsEmpty();
  1674. }
  1675. //---------------------------------------------------------------------
  1676. void __fastcall TConfiguration::UpdateActualLogProtocol()
  1677. {
  1678. FActualLogProtocol = FLogging ? FLogProtocol : (-BelowNormalLogLevels - 1);
  1679. }
  1680. //---------------------------------------------------------------------
  1681. void __fastcall TConfiguration::SetLogProtocol(int value)
  1682. {
  1683. TGuard Guard(FCriticalSection);
  1684. if (LogProtocol != value)
  1685. {
  1686. FPermanentLogProtocol = value;
  1687. FLogProtocol = value;
  1688. Changed();
  1689. UpdateActualLogProtocol();
  1690. }
  1691. }
  1692. //---------------------------------------------------------------------
  1693. void __fastcall TConfiguration::SetLogActions(bool value)
  1694. {
  1695. TGuard Guard(FCriticalSection);
  1696. if (LogActions != value)
  1697. {
  1698. FPermanentLogActions = value;
  1699. FLogActions = value;
  1700. Changed();
  1701. }
  1702. }
  1703. //---------------------------------------------------------------------
  1704. bool __fastcall TConfiguration::GetLogActions()
  1705. {
  1706. TGuard Guard(FCriticalSection);
  1707. return FPermanentLogActions;
  1708. }
  1709. //---------------------------------------------------------------------
  1710. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  1711. {
  1712. SET_CONFIG_PROPERTY(LogFileAppend);
  1713. }
  1714. //---------------------------------------------------------------------
  1715. void __fastcall TConfiguration::SetLogSensitive(bool value)
  1716. {
  1717. if (LogSensitive != value)
  1718. {
  1719. FPermanentLogSensitive = value;
  1720. FLogSensitive = value;
  1721. Changed();
  1722. }
  1723. }
  1724. //---------------------------------------------------------------------
  1725. void __fastcall TConfiguration::SetLogMaxSize(__int64 value)
  1726. {
  1727. TGuard Guard(FCriticalSection);
  1728. if (LogMaxSize != value)
  1729. {
  1730. FPermanentLogMaxSize = value;
  1731. FLogMaxSize = value;
  1732. Changed();
  1733. }
  1734. }
  1735. //---------------------------------------------------------------------
  1736. __int64 __fastcall TConfiguration::GetLogMaxSize()
  1737. {
  1738. TGuard Guard(FCriticalSection);
  1739. return FPermanentLogMaxSize;
  1740. }
  1741. //---------------------------------------------------------------------
  1742. void __fastcall TConfiguration::SetLogMaxCount(int value)
  1743. {
  1744. if (LogMaxCount != value)
  1745. {
  1746. FPermanentLogMaxCount = value;
  1747. FLogMaxCount = value;
  1748. Changed();
  1749. }
  1750. }
  1751. //---------------------------------------------------------------------
  1752. int __fastcall TConfiguration::GetLogMaxCount()
  1753. {
  1754. TGuard Guard(FCriticalSection);
  1755. return FPermanentLogMaxCount;
  1756. }
  1757. //---------------------------------------------------------------------
  1758. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  1759. {
  1760. return L"%TEMP%\\!S.log";
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  1764. {
  1765. TGuard Guard(FCriticalSection);
  1766. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  1767. }
  1768. //---------------------------------------------------------------------------
  1769. bool __fastcall TConfiguration::GetConfirmOverwriting()
  1770. {
  1771. TGuard Guard(FCriticalSection);
  1772. return FConfirmOverwriting;
  1773. }
  1774. //---------------------------------------------------------------------------
  1775. void __fastcall TConfiguration::SetConfirmResume(bool value)
  1776. {
  1777. TGuard Guard(FCriticalSection);
  1778. SET_CONFIG_PROPERTY(ConfirmResume);
  1779. }
  1780. //---------------------------------------------------------------------------
  1781. bool __fastcall TConfiguration::GetConfirmResume()
  1782. {
  1783. TGuard Guard(FCriticalSection);
  1784. return FConfirmResume;
  1785. }
  1786. //---------------------------------------------------------------------------
  1787. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  1788. {
  1789. TGuard Guard(FCriticalSection);
  1790. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  1791. }
  1792. //---------------------------------------------------------------------------
  1793. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  1794. {
  1795. TGuard Guard(FCriticalSection);
  1796. return FAutoReadDirectoryAfterOp;
  1797. }
  1798. //---------------------------------------------------------------------------
  1799. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  1800. {
  1801. return L"h:nn:ss";
  1802. }
  1803. //---------------------------------------------------------------------------
  1804. UnicodeString __fastcall TConfiguration::GetPartialExt() const
  1805. {
  1806. return PARTIAL_EXT;
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  1810. {
  1811. return L"";
  1812. }
  1813. //---------------------------------------------------------------------------
  1814. bool __fastcall TConfiguration::GetRememberPassword()
  1815. {
  1816. return false;
  1817. }
  1818. //---------------------------------------------------------------------------
  1819. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  1820. {
  1821. SET_CONFIG_PROPERTY(SessionReopenAuto);
  1822. }
  1823. //---------------------------------------------------------------------------
  1824. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  1825. {
  1826. SET_CONFIG_PROPERTY(SessionReopenBackground);
  1827. }
  1828. //---------------------------------------------------------------------------
  1829. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  1830. {
  1831. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  1832. }
  1833. //---------------------------------------------------------------------------
  1834. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  1835. {
  1836. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  1837. }
  1838. //---------------------------------------------------------------------------
  1839. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  1840. {
  1841. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  1842. }
  1843. //---------------------------------------------------------------------------
  1844. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  1845. {
  1846. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  1847. }
  1848. //---------------------------------------------------------------------------
  1849. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  1850. {
  1851. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  1852. }
  1853. //---------------------------------------------------------------------------
  1854. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  1855. {
  1856. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  1857. }
  1858. //---------------------------------------------------------------------------
  1859. bool __fastcall TConfiguration::GetPersistent()
  1860. {
  1861. return (Storage != stNul) && !FDontSave;
  1862. }
  1863. //---------------------------------------------------------------------------
  1864. //---------------------------------------------------------------------------
  1865. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  1866. {
  1867. FShortCuts.insert(ShortCut);
  1868. }
  1869. //---------------------------------------------------------------------------
  1870. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  1871. {
  1872. return (FShortCuts.count(ShortCut) != 0);
  1873. }