1
0

Configuration.cpp 56 KB

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