Configuration.cpp 67 KB

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