Configuration.cpp 66 KB

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