Configuration.cpp 67 KB

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