Configuration.cpp 66 KB

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