Configuration.cpp 53 KB

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