Configuration.cpp 48 KB

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