Configuration.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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. #ifndef BUILD_OFFICIAL
  970. UnicodeString BuildDate = __DATE__;
  971. UnicodeString MonthStr = CutToChar(BuildDate, L' ', true);
  972. int Month = ParseShortEngMonthName(MonthStr);
  973. int Day = StrToInt(CutToChar(BuildDate, L' ', true));
  974. int Year = StrToInt(Trim(BuildDate));
  975. UnicodeString DateStr = FORMAT(L"%d-%2.2d-%2.2d", (Year, Month, Day));
  976. AddToList(BuildStr, DateStr, L" ");
  977. #endif
  978. UnicodeString FullVersion = Version;
  979. UnicodeString AReleaseType = GetReleaseType();
  980. if (DebugAlwaysTrue(!AReleaseType.IsEmpty()) &&
  981. !SameText(AReleaseType, L"stable") &&
  982. !SameText(AReleaseType, L"development"))
  983. {
  984. FullVersion += L" " + AReleaseType;
  985. }
  986. UnicodeString Result = FMTLOAD(VERSION2, (FullVersion, BuildStr));
  987. #ifndef BUILD_OFFICIAL
  988. Result += L" " + LoadStr(VERSION_DONT_DISTRIBUTE);
  989. #endif
  990. return Result;
  991. }
  992. catch (Exception &E)
  993. {
  994. throw ExtException(&E, L"Can't get application version");
  995. }
  996. }
  997. //---------------------------------------------------------------------------
  998. UnicodeString __fastcall TConfiguration::GetFileVersion(const UnicodeString & FileName)
  999. {
  1000. UnicodeString Result;
  1001. void * FileInfo = CreateFileInfo(FileName);
  1002. try
  1003. {
  1004. Result = GetFileVersion(GetFixedFileInfo(FileInfo));
  1005. }
  1006. __finally
  1007. {
  1008. FreeFileInfo(FileInfo);
  1009. }
  1010. return Result;
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. UnicodeString __fastcall TConfiguration::GetFileVersion(TVSFixedFileInfo * Info)
  1014. {
  1015. TGuard Guard(FCriticalSection);
  1016. try
  1017. {
  1018. UnicodeString Result =
  1019. FormatVersion(
  1020. HIWORD(Info->dwFileVersionMS),
  1021. LOWORD(Info->dwFileVersionMS),
  1022. HIWORD(Info->dwFileVersionLS));
  1023. return Result;
  1024. }
  1025. catch (Exception &E)
  1026. {
  1027. throw ExtException(&E, L"Can't get file version");
  1028. }
  1029. }
  1030. //---------------------------------------------------------------------------
  1031. UnicodeString __fastcall TConfiguration::GetVersion()
  1032. {
  1033. return GetFileVersion(FixedApplicationInfo);
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. UnicodeString __fastcall TConfiguration::GetFileFileInfoString(const UnicodeString Key,
  1037. const UnicodeString FileName, bool AllowEmpty)
  1038. {
  1039. TGuard Guard(FCriticalSection);
  1040. UnicodeString Result;
  1041. void * Info = GetFileApplicationInfo(FileName);
  1042. try
  1043. {
  1044. if ((Info != NULL) && (GetTranslationCount(Info) > 0))
  1045. {
  1046. TTranslation Translation;
  1047. Translation = GetTranslation(Info, 0);
  1048. Result = ::GetFileInfoString(Info, Translation, Key, AllowEmpty);
  1049. }
  1050. else
  1051. {
  1052. DebugAssert(!FileName.IsEmpty());
  1053. }
  1054. }
  1055. __finally
  1056. {
  1057. if (!FileName.IsEmpty())
  1058. {
  1059. FreeFileInfo(Info);
  1060. }
  1061. }
  1062. return Result;
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. UnicodeString __fastcall TConfiguration::GetFileInfoString(const UnicodeString Key)
  1066. {
  1067. return GetFileFileInfoString(Key, L"");
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. UnicodeString __fastcall TConfiguration::GetFileMimeType(const UnicodeString & FileName)
  1071. {
  1072. UnicodeString Result;
  1073. bool Found = false;
  1074. if (!MimeTypes.IsEmpty())
  1075. {
  1076. UnicodeString FileNameOnly = ExtractFileName(FileName);
  1077. UnicodeString AMimeTypes = MimeTypes;
  1078. while (!Found && !AMimeTypes.IsEmpty())
  1079. {
  1080. UnicodeString Token = CutToChar(AMimeTypes, L',', true);
  1081. UnicodeString MaskStr = CutToChar(Token, L'=', true);
  1082. TFileMasks Mask(MaskStr);
  1083. if (Mask.Matches(FileNameOnly))
  1084. {
  1085. Result = Token.Trim();
  1086. Found = true;
  1087. }
  1088. }
  1089. }
  1090. if (!Found) // allow an override to "no" Content-Type
  1091. {
  1092. Result = ::GetFileMimeType(FileName);
  1093. }
  1094. return Result;
  1095. }
  1096. //---------------------------------------------------------------------------
  1097. UnicodeString __fastcall TConfiguration::GetRegistryStorageKey()
  1098. {
  1099. return GetRegistryKey();
  1100. }
  1101. //---------------------------------------------------------------------------
  1102. void __fastcall TConfiguration::SetNulStorage()
  1103. {
  1104. FStorage = stNul;
  1105. }
  1106. //---------------------------------------------------------------------------
  1107. void __fastcall TConfiguration::SetDefaultStorage()
  1108. {
  1109. FStorage = stDetect;
  1110. }
  1111. //---------------------------------------------------------------------------
  1112. void __fastcall TConfiguration::SetExplicitIniFileStorageName(const UnicodeString & FileName)
  1113. {
  1114. FIniFileStorageName = FileName;
  1115. FStorage = stIniFile;
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. UnicodeString __fastcall TConfiguration::GetDefaultIniFileExportPath()
  1119. {
  1120. UnicodeString PersonalDirectory = GetPersonalFolder();
  1121. UnicodeString FileName = IncludeTrailingBackslash(PersonalDirectory) +
  1122. ExtractFileName(ExpandEnvironmentVariables(IniFileStorageName));
  1123. return FileName;
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReading()
  1127. {
  1128. return GetIniFileStorageName(true);
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReadingWriting()
  1132. {
  1133. return GetIniFileStorageName(false);
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. UnicodeString __fastcall TConfiguration::GetAutomaticIniFileStorageName(bool ReadingOnly)
  1137. {
  1138. UnicodeString ProgramPath = ParamStr(0);
  1139. UnicodeString ProgramIniPath = ChangeFileExt(ProgramPath, L".ini");
  1140. UnicodeString IniPath;
  1141. if (FileExists(ApiPath(ProgramIniPath)))
  1142. {
  1143. IniPath = ProgramIniPath;
  1144. }
  1145. else
  1146. {
  1147. UnicodeString AppDataIniPath =
  1148. IncludeTrailingBackslash(GetShellFolderPath(CSIDL_APPDATA)) +
  1149. ExtractFileName(ProgramIniPath);
  1150. if (FileExists(ApiPath(AppDataIniPath)))
  1151. {
  1152. IniPath = AppDataIniPath;
  1153. }
  1154. else
  1155. {
  1156. // avoid expensive test if we are interested in existing files only
  1157. if (!ReadingOnly && (FProgramIniPathWrittable < 0))
  1158. {
  1159. UnicodeString ProgramDir = ExtractFilePath(ProgramPath);
  1160. FProgramIniPathWrittable = IsDirectoryWriteable(ProgramDir) ? 1 : 0;
  1161. }
  1162. // does not really matter what we return when < 0
  1163. IniPath = (FProgramIniPathWrittable == 0) ? AppDataIniPath : ProgramIniPath;
  1164. }
  1165. }
  1166. // BACKWARD COMPATIBILITY with 4.x
  1167. if (FVirtualIniFileStorageName.IsEmpty() &&
  1168. TPath::IsDriveRooted(IniPath))
  1169. {
  1170. UnicodeString LocalAppDataPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  1171. // virtual store for non-system drives have a different virtual store,
  1172. // do not bother about them
  1173. if (TPath::IsDriveRooted(LocalAppDataPath) &&
  1174. SameText(ExtractFileDrive(IniPath), ExtractFileDrive(LocalAppDataPath)))
  1175. {
  1176. FVirtualIniFileStorageName =
  1177. IncludeTrailingBackslash(LocalAppDataPath) +
  1178. L"VirtualStore\\" +
  1179. IniPath.SubString(4, IniPath.Length() - 3);
  1180. }
  1181. }
  1182. if (!FVirtualIniFileStorageName.IsEmpty() &&
  1183. FileExists(ApiPath(FVirtualIniFileStorageName)))
  1184. {
  1185. return FVirtualIniFileStorageName;
  1186. }
  1187. else
  1188. {
  1189. return IniPath;
  1190. }
  1191. }
  1192. //---------------------------------------------------------------------------
  1193. UnicodeString __fastcall TConfiguration::GetIniFileParamValue()
  1194. {
  1195. UnicodeString Result;
  1196. if (Storage == stNul)
  1197. {
  1198. Result = INI_NUL;
  1199. }
  1200. else if ((Storage == stIniFile) && !FIniFileStorageName.IsEmpty())
  1201. {
  1202. Result = FIniFileStorageName;
  1203. }
  1204. return Result;
  1205. }
  1206. //---------------------------------------------------------------------------
  1207. UnicodeString __fastcall TConfiguration::GetIniFileStorageName(bool ReadingOnly)
  1208. {
  1209. UnicodeString Result;
  1210. if (!FIniFileStorageName.IsEmpty())
  1211. {
  1212. Result = FIniFileStorageName;
  1213. }
  1214. else if (!FCustomIniFileStorageName.IsEmpty())
  1215. {
  1216. Result = FCustomIniFileStorageName;
  1217. }
  1218. else
  1219. {
  1220. Result = GetAutomaticIniFileStorageName(ReadingOnly);
  1221. }
  1222. return Result;
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. void __fastcall TConfiguration::SetOptionsStorage(TStrings * value)
  1226. {
  1227. TGuard Guard(FCriticalSection);
  1228. if (FOptionsStorage.get() == NULL)
  1229. {
  1230. FOptionsStorage.reset(new TStringList());
  1231. }
  1232. FOptionsStorage->AddStrings(value);
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. TStrings * __fastcall TConfiguration::GetOptionsStorage()
  1236. {
  1237. return FOptionsStorage.get();
  1238. }
  1239. //---------------------------------------------------------------------------
  1240. UnicodeString __fastcall TConfiguration::GetPuttySessionsSubKey()
  1241. {
  1242. return StoredSessionsSubKey;
  1243. }
  1244. //---------------------------------------------------------------------------
  1245. UnicodeString __fastcall TConfiguration::GetPuttySessionsKey()
  1246. {
  1247. return PuttyRegistryStorageKey + L"\\" + PuttySessionsSubKey;
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. UnicodeString __fastcall TConfiguration::GetStoredSessionsSubKey()
  1251. {
  1252. return L"Sessions";
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. UnicodeString __fastcall TConfiguration::GetSshHostKeysSubKey()
  1256. {
  1257. return L"SshHostKeys";
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. UnicodeString __fastcall TConfiguration::GetConfigurationSubKey()
  1261. {
  1262. return L"Configuration";
  1263. }
  1264. //---------------------------------------------------------------------------
  1265. UnicodeString __fastcall TConfiguration::GetRootKeyStr()
  1266. {
  1267. return RootKeyToStr(HKEY_CURRENT_USER);
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. void __fastcall TConfiguration::MoveStorage(TStorage AStorage, const UnicodeString & ACustomIniFileStorageName)
  1271. {
  1272. if ((FStorage != AStorage) ||
  1273. ((FStorage == stIniFile) && !FIniFileStorageName.IsEmpty()) ||
  1274. !IsPathToSameFile(FCustomIniFileStorageName, ACustomIniFileStorageName))
  1275. {
  1276. TStorage StorageBak = FStorage;
  1277. UnicodeString CustomIniFileStorageNameBak = FCustomIniFileStorageName;
  1278. UnicodeString IniFileStorageNameBak = FIniFileStorageName;
  1279. try
  1280. {
  1281. THierarchicalStorage * SourceStorage = NULL;
  1282. THierarchicalStorage * TargetStorage = NULL;
  1283. try
  1284. {
  1285. SourceStorage = CreateConfigStorage();
  1286. SourceStorage->AccessMode = smRead;
  1287. FStorage = AStorage;
  1288. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1289. FIniFileStorageName = UnicodeString();
  1290. TargetStorage = CreateConfigStorage();
  1291. TargetStorage->AccessMode = smReadWrite;
  1292. TargetStorage->Explicit = true;
  1293. // copy before save as it removes the ini file,
  1294. // when switching from ini to registry
  1295. CopyData(SourceStorage, TargetStorage);
  1296. }
  1297. __finally
  1298. {
  1299. delete SourceStorage;
  1300. delete TargetStorage;
  1301. }
  1302. // save all and explicit,
  1303. // this also removes an INI file, when switching to registry storage
  1304. DoSave(true, true);
  1305. }
  1306. catch (...)
  1307. {
  1308. // If this fails, do not pretend that storage was switched.
  1309. // For instance:
  1310. // - When writing to an INI file fails (unlikely, as we fallback to user profile)
  1311. // - When removing INI file fails, when switching to registry
  1312. // (possible, when the INI file is in Program Files folder)
  1313. FStorage = StorageBak;
  1314. FCustomIniFileStorageName = CustomIniFileStorageNameBak;
  1315. FIniFileStorageName = IniFileStorageNameBak;
  1316. throw;
  1317. }
  1318. }
  1319. }
  1320. //---------------------------------------------------------------------------
  1321. void __fastcall TConfiguration::ScheduleCustomIniFileStorageUse(const UnicodeString & ACustomIniFileStorageName)
  1322. {
  1323. FStorage = stIniFile;
  1324. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1325. SaveCustomIniFileStorageName();
  1326. }
  1327. //---------------------------------------------------------------------------
  1328. void __fastcall TConfiguration::Saved()
  1329. {
  1330. // nothing
  1331. }
  1332. //---------------------------------------------------------------------------
  1333. TStorage __fastcall TConfiguration::GetStorage()
  1334. {
  1335. TGuard Guard(FCriticalSection);
  1336. if (FStorage == stDetect)
  1337. {
  1338. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  1339. {
  1340. FStorage = stIniFile;
  1341. }
  1342. else
  1343. {
  1344. FStorage = stRegistry;
  1345. }
  1346. }
  1347. return FStorage;
  1348. }
  1349. //---------------------------------------------------------------------
  1350. TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport(
  1351. TStoredSessionList * Sessions, UnicodeString & Error)
  1352. {
  1353. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1354. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1355. UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
  1356. UnicodeString FilezillaSiteManagerFile =
  1357. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\sitemanager.xml";
  1358. UnicodeString FilezillaConfigurationFile =
  1359. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\filezilla.xml";
  1360. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1361. {
  1362. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile, FilezillaConfigurationFile);
  1363. if (ImportSessionList->Count > 0)
  1364. {
  1365. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1366. }
  1367. else
  1368. {
  1369. Error = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1370. }
  1371. }
  1372. else
  1373. {
  1374. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1375. }
  1376. return ImportSessionList.release();
  1377. }
  1378. //---------------------------------------------------------------------
  1379. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1380. {
  1381. try
  1382. {
  1383. UnicodeString Error;
  1384. std::unique_ptr<TStoredSessionList> Sesssions(SelectFilezillaSessionsForImport(Sessions, Error));
  1385. return (Sesssions->Count > 0);
  1386. }
  1387. catch (...)
  1388. {
  1389. return false;
  1390. }
  1391. }
  1392. //---------------------------------------------------------------------
  1393. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1394. TStoredSessionList * Sessions, UnicodeString & Error)
  1395. {
  1396. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1397. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1398. UnicodeString ProfilePath = GetShellFolderPath(CSIDL_PROFILE);
  1399. UnicodeString KnownHostsFile = IncludeTrailingBackslash(ProfilePath) + L".ssh\\known_hosts";
  1400. try
  1401. {
  1402. if (FileExists(ApiPath(KnownHostsFile)))
  1403. {
  1404. std::unique_ptr<TStrings> Lines(new TStringList());
  1405. LoadScriptFromFile(KnownHostsFile, Lines.get(), true);
  1406. ImportSessionList->ImportFromKnownHosts(Lines.get());
  1407. }
  1408. else
  1409. {
  1410. throw Exception(LoadStr(KNOWN_HOSTS_NOT_FOUND));
  1411. }
  1412. }
  1413. catch (Exception & E)
  1414. {
  1415. Error = FORMAT(L"%s\n(%s)", (E.Message, KnownHostsFile));
  1416. }
  1417. return ImportSessionList.release();
  1418. }
  1419. //---------------------------------------------------------------------
  1420. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1421. TStrings * Lines, TStoredSessionList * Sessions, UnicodeString & Error)
  1422. {
  1423. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1424. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1425. try
  1426. {
  1427. ImportSessionList->ImportFromKnownHosts(Lines);
  1428. }
  1429. catch (Exception & E)
  1430. {
  1431. Error = E.Message;
  1432. }
  1433. return ImportSessionList.release();
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1437. {
  1438. if (RandomSeedFile != value)
  1439. {
  1440. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1441. FRandomSeedFile = value;
  1442. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1443. if (RandomSeedFileName.IsEmpty())
  1444. {
  1445. FRandomSeedFile = FDefaultRandomSeedFile;
  1446. }
  1447. if (!PrevRandomSeedFileName.IsEmpty() &&
  1448. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1449. FileExists(ApiPath(PrevRandomSeedFileName)))
  1450. {
  1451. // ignore any error
  1452. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1453. }
  1454. }
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. UnicodeString __fastcall TConfiguration::GetDirectoryStatisticsCacheKey(
  1458. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1459. {
  1460. std::unique_ptr<TStringList> RawOptions(new TStringList());
  1461. RawOptions->Add(SessionKey);
  1462. RawOptions->Add(UnixExcludeTrailingBackslash(Path));
  1463. TCopyParamType Defaults;
  1464. TCopyParamType FilterCopyParam;
  1465. FilterCopyParam.IncludeFileMask = CopyParam.IncludeFileMask;
  1466. FilterCopyParam.ExcludeHiddenFiles = CopyParam.ExcludeHiddenFiles;
  1467. FilterCopyParam.ExcludeEmptyDirectories = CopyParam.ExcludeEmptyDirectories;
  1468. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawOptions.get(), true));
  1469. FilterCopyParam.Save(OptionsStorage.get(), &Defaults);
  1470. UTF8String RawOptionsBuf(RawOptions->CommaText.LowerCase());
  1471. UnicodeString Result = Sha256(RawOptionsBuf.c_str(), RawOptionsBuf.Length());
  1472. return Result;
  1473. }
  1474. //---------------------------------------------------------------------------
  1475. THierarchicalStorage * TConfiguration::OpenDirectoryStatisticsCache(bool CanCreate)
  1476. {
  1477. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateConfigStorage());
  1478. Storage->AccessMode = CanCreate ? smReadWrite : smRead;
  1479. if (!Storage->OpenSubKey(DirectoryStatisticsCacheKey, CanCreate))
  1480. {
  1481. Storage.reset(NULL);
  1482. }
  1483. return Storage.release();
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. TStrings * __fastcall TConfiguration::LoadDirectoryStatisticsCache(
  1487. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1488. {
  1489. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(false));
  1490. std::unique_ptr<TStringList> Result(new TStringList());
  1491. if (Storage.get() != NULL)
  1492. {
  1493. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1494. UnicodeString Buf = Storage->ReadString(Key, UnicodeString());
  1495. Result->CommaText = Buf;
  1496. }
  1497. return Result.release();
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. void __fastcall TConfiguration::SaveDirectoryStatisticsCache(
  1501. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam, TStrings * DataList)
  1502. {
  1503. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(true));
  1504. if (Storage.get() != NULL)
  1505. {
  1506. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1507. UnicodeString Buf = DataList->CommaText;
  1508. Storage->WriteString(Key, Buf);
  1509. }
  1510. }
  1511. //---------------------------------------------------------------------
  1512. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1513. {
  1514. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1515. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1516. }
  1517. //---------------------------------------------------------------------
  1518. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1519. {
  1520. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1521. }
  1522. //---------------------------------------------------------------------
  1523. bool TConfiguration::HasLocalPortNumberLimits()
  1524. {
  1525. return (LocalPortNumberMin > 0) && (LocalPortNumberMax >= LocalPortNumberMin);
  1526. }
  1527. //---------------------------------------------------------------------
  1528. void TConfiguration::SetLocalPortNumberMin(int value)
  1529. {
  1530. SET_CONFIG_PROPERTY(LocalPortNumberMin);
  1531. }
  1532. //---------------------------------------------------------------------
  1533. void TConfiguration::SetLocalPortNumberMax(int value)
  1534. {
  1535. SET_CONFIG_PROPERTY(LocalPortNumberMax);
  1536. }
  1537. //---------------------------------------------------------------------
  1538. void __fastcall TConfiguration::SetMimeTypes(UnicodeString value)
  1539. {
  1540. SET_CONFIG_PROPERTY(MimeTypes);
  1541. }
  1542. //---------------------------------------------------------------------
  1543. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1544. {
  1545. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1546. }
  1547. //---------------------------------------------------------------------
  1548. void __fastcall TConfiguration::SetParallelDurationThreshold(int value)
  1549. {
  1550. SET_CONFIG_PROPERTY(ParallelDurationThreshold);
  1551. }
  1552. //---------------------------------------------------------------------
  1553. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1554. {
  1555. SET_CONFIG_PROPERTY(PuttyRegistryStorageKey);
  1556. }
  1557. //---------------------------------------------------------------------------
  1558. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1559. {
  1560. return eolCRLF;
  1561. }
  1562. //---------------------------------------------------------------------
  1563. bool __fastcall TConfiguration::GetCollectUsage()
  1564. {
  1565. return FUsage->Collect;
  1566. }
  1567. //---------------------------------------------------------------------
  1568. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1569. {
  1570. FUsage->Collect = value;
  1571. }
  1572. //---------------------------------------------------------------------
  1573. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1574. {
  1575. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1576. {
  1577. TemporaryActionsLogging(ALogFileName);
  1578. }
  1579. else
  1580. {
  1581. FLogging = true;
  1582. FLogFileName = ALogFileName;
  1583. UpdateActualLogProtocol();
  1584. }
  1585. }
  1586. //---------------------------------------------------------------------
  1587. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1588. {
  1589. FLogActions = true;
  1590. FActionsLogFileName = ALogFileName;
  1591. }
  1592. //---------------------------------------------------------------------
  1593. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1594. {
  1595. FLogProtocol = ALogProtocol;
  1596. UpdateActualLogProtocol();
  1597. }
  1598. //---------------------------------------------------------------------
  1599. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1600. {
  1601. FLogSensitive = ALogSensitive;
  1602. }
  1603. //---------------------------------------------------------------------
  1604. void __fastcall TConfiguration::TemporaryLogMaxSize(__int64 ALogMaxSize)
  1605. {
  1606. FLogMaxSize = ALogMaxSize;
  1607. }
  1608. //---------------------------------------------------------------------
  1609. void __fastcall TConfiguration::TemporaryLogMaxCount(int ALogMaxCount)
  1610. {
  1611. FLogMaxCount = ALogMaxCount;
  1612. }
  1613. //---------------------------------------------------------------------
  1614. void __fastcall TConfiguration::SetLogging(bool value)
  1615. {
  1616. TGuard Guard(FCriticalSection);
  1617. if (Logging != value)
  1618. {
  1619. FPermanentLogging = value;
  1620. FLogging = value;
  1621. UpdateActualLogProtocol();
  1622. Changed();
  1623. }
  1624. }
  1625. //---------------------------------------------------------------------
  1626. bool __fastcall TConfiguration::GetLogging()
  1627. {
  1628. TGuard Guard(FCriticalSection);
  1629. return FPermanentLogging;
  1630. }
  1631. //---------------------------------------------------------------------
  1632. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  1633. {
  1634. TGuard Guard(FCriticalSection);
  1635. if (LogFileName != value)
  1636. {
  1637. FPermanentLogFileName = value;
  1638. FLogFileName = value;
  1639. Changed();
  1640. }
  1641. }
  1642. //---------------------------------------------------------------------
  1643. UnicodeString __fastcall TConfiguration::GetLogFileName()
  1644. {
  1645. TGuard Guard(FCriticalSection);
  1646. return FPermanentLogFileName;
  1647. }
  1648. //---------------------------------------------------------------------
  1649. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  1650. {
  1651. TGuard Guard(FCriticalSection);
  1652. if (ActionsLogFileName != value)
  1653. {
  1654. FPermanentActionsLogFileName = value;
  1655. FActionsLogFileName = value;
  1656. Changed();
  1657. }
  1658. }
  1659. //---------------------------------------------------------------------
  1660. UnicodeString __fastcall TConfiguration::GetPermanentActionsLogFileName()
  1661. {
  1662. TGuard Guard(FCriticalSection);
  1663. return FPermanentActionsLogFileName;
  1664. }
  1665. //---------------------------------------------------------------------
  1666. UnicodeString __fastcall TConfiguration::GetActionsLogFileName()
  1667. {
  1668. TGuard Guard(FCriticalSection);
  1669. return FActionsLogFileName;
  1670. }
  1671. //---------------------------------------------------------------------
  1672. bool __fastcall TConfiguration::GetLogToFile()
  1673. {
  1674. // guarded within GetLogFileName
  1675. return !LogFileName.IsEmpty();
  1676. }
  1677. //---------------------------------------------------------------------
  1678. void __fastcall TConfiguration::UpdateActualLogProtocol()
  1679. {
  1680. FActualLogProtocol = FLogging ? FLogProtocol : (-BelowNormalLogLevels - 1);
  1681. }
  1682. //---------------------------------------------------------------------
  1683. void __fastcall TConfiguration::SetLogProtocol(int value)
  1684. {
  1685. TGuard Guard(FCriticalSection);
  1686. if (LogProtocol != value)
  1687. {
  1688. FPermanentLogProtocol = value;
  1689. FLogProtocol = value;
  1690. Changed();
  1691. UpdateActualLogProtocol();
  1692. }
  1693. }
  1694. //---------------------------------------------------------------------
  1695. void __fastcall TConfiguration::SetLogActions(bool value)
  1696. {
  1697. TGuard Guard(FCriticalSection);
  1698. if (LogActions != value)
  1699. {
  1700. FPermanentLogActions = value;
  1701. FLogActions = value;
  1702. Changed();
  1703. }
  1704. }
  1705. //---------------------------------------------------------------------
  1706. bool __fastcall TConfiguration::GetLogActions()
  1707. {
  1708. TGuard Guard(FCriticalSection);
  1709. return FPermanentLogActions;
  1710. }
  1711. //---------------------------------------------------------------------
  1712. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  1713. {
  1714. SET_CONFIG_PROPERTY(LogFileAppend);
  1715. }
  1716. //---------------------------------------------------------------------
  1717. void __fastcall TConfiguration::SetLogSensitive(bool value)
  1718. {
  1719. if (LogSensitive != value)
  1720. {
  1721. FPermanentLogSensitive = value;
  1722. FLogSensitive = value;
  1723. Changed();
  1724. }
  1725. }
  1726. //---------------------------------------------------------------------
  1727. void __fastcall TConfiguration::SetLogMaxSize(__int64 value)
  1728. {
  1729. TGuard Guard(FCriticalSection);
  1730. if (LogMaxSize != value)
  1731. {
  1732. FPermanentLogMaxSize = value;
  1733. FLogMaxSize = value;
  1734. Changed();
  1735. }
  1736. }
  1737. //---------------------------------------------------------------------
  1738. __int64 __fastcall TConfiguration::GetLogMaxSize()
  1739. {
  1740. TGuard Guard(FCriticalSection);
  1741. return FPermanentLogMaxSize;
  1742. }
  1743. //---------------------------------------------------------------------
  1744. void __fastcall TConfiguration::SetLogMaxCount(int value)
  1745. {
  1746. if (LogMaxCount != value)
  1747. {
  1748. FPermanentLogMaxCount = value;
  1749. FLogMaxCount = value;
  1750. Changed();
  1751. }
  1752. }
  1753. //---------------------------------------------------------------------
  1754. int __fastcall TConfiguration::GetLogMaxCount()
  1755. {
  1756. TGuard Guard(FCriticalSection);
  1757. return FPermanentLogMaxCount;
  1758. }
  1759. //---------------------------------------------------------------------
  1760. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  1761. {
  1762. return L"%TEMP%\\!S.log";
  1763. }
  1764. //---------------------------------------------------------------------------
  1765. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  1766. {
  1767. TGuard Guard(FCriticalSection);
  1768. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  1769. }
  1770. //---------------------------------------------------------------------------
  1771. bool __fastcall TConfiguration::GetConfirmOverwriting()
  1772. {
  1773. TGuard Guard(FCriticalSection);
  1774. return FConfirmOverwriting;
  1775. }
  1776. //---------------------------------------------------------------------------
  1777. void __fastcall TConfiguration::SetConfirmResume(bool value)
  1778. {
  1779. TGuard Guard(FCriticalSection);
  1780. SET_CONFIG_PROPERTY(ConfirmResume);
  1781. }
  1782. //---------------------------------------------------------------------------
  1783. bool __fastcall TConfiguration::GetConfirmResume()
  1784. {
  1785. TGuard Guard(FCriticalSection);
  1786. return FConfirmResume;
  1787. }
  1788. //---------------------------------------------------------------------------
  1789. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  1790. {
  1791. TGuard Guard(FCriticalSection);
  1792. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  1793. }
  1794. //---------------------------------------------------------------------------
  1795. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  1796. {
  1797. TGuard Guard(FCriticalSection);
  1798. return FAutoReadDirectoryAfterOp;
  1799. }
  1800. //---------------------------------------------------------------------------
  1801. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  1802. {
  1803. return L"h:nn:ss";
  1804. }
  1805. //---------------------------------------------------------------------------
  1806. UnicodeString __fastcall TConfiguration::GetPartialExt() const
  1807. {
  1808. return PARTIAL_EXT;
  1809. }
  1810. //---------------------------------------------------------------------------
  1811. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  1812. {
  1813. return L"";
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. bool __fastcall TConfiguration::GetRememberPassword()
  1817. {
  1818. return false;
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  1822. {
  1823. SET_CONFIG_PROPERTY(SessionReopenAuto);
  1824. }
  1825. //---------------------------------------------------------------------------
  1826. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  1827. {
  1828. SET_CONFIG_PROPERTY(SessionReopenBackground);
  1829. }
  1830. //---------------------------------------------------------------------------
  1831. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  1832. {
  1833. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  1834. }
  1835. //---------------------------------------------------------------------------
  1836. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  1837. {
  1838. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  1839. }
  1840. //---------------------------------------------------------------------------
  1841. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  1842. {
  1843. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  1844. }
  1845. //---------------------------------------------------------------------------
  1846. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  1847. {
  1848. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  1849. }
  1850. //---------------------------------------------------------------------------
  1851. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  1852. {
  1853. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  1854. }
  1855. //---------------------------------------------------------------------------
  1856. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  1857. {
  1858. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  1859. }
  1860. //---------------------------------------------------------------------------
  1861. bool __fastcall TConfiguration::GetPersistent()
  1862. {
  1863. return (Storage != stNul) && !FDontSave;
  1864. }
  1865. //---------------------------------------------------------------------------
  1866. //---------------------------------------------------------------------------
  1867. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  1868. {
  1869. FShortCuts.insert(ShortCut);
  1870. }
  1871. //---------------------------------------------------------------------------
  1872. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  1873. {
  1874. return (FShortCuts.count(ShortCut) != 0);
  1875. }