Configuration.cpp 55 KB

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