Configuration.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <FileInfo.h>
  5. #include "Exceptions.h"
  6. #include "Common.h"
  7. #include "Configuration.h"
  8. #include "PuttyIntf.h"
  9. #include "TextsCore.h"
  10. #include "Interface.h"
  11. #include "CoreMain.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. __fastcall TConfiguration::TConfiguration()
  16. {
  17. FCriticalSection = new TCriticalSection();
  18. FUpdating = 0;
  19. FStorage = stDetect;
  20. FDontSave = false;
  21. FApplicationInfo = NULL;
  22. FInitialized = false;
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TConfiguration::Initialize()
  26. {
  27. AnsiString ARandomSeedFile = seedpath_ptr();
  28. FDefaultRandomSeedFile = StringReplace(ExtractFilePath(ARandomSeedFile) +
  29. "winscp" + ExtractFileExt(ARandomSeedFile), "\\\\", "\\",
  30. TReplaceFlags() << rfReplaceAll);
  31. FInitialized = true;
  32. }
  33. //---------------------------------------------------------------------------
  34. void __fastcall TConfiguration::Default()
  35. {
  36. TGuard Guard(FCriticalSection);
  37. RandomSeedFile = FDefaultRandomSeedFile;
  38. FConfirmOverwriting = true;
  39. FConfirmResume = true;
  40. FAutoReadDirectoryAfterOp = true;
  41. FSessionReopenAuto = 5000;
  42. FSessionReopenBackground = 2000;
  43. FTunnelLocalPortNumberLow = 6000;
  44. FTunnelLocalPortNumberHigh = 6100;
  45. FShowFtpWelcomeMessage = false;
  46. FLogging = false;
  47. FPermanentLogging = false;
  48. FLogFileName = "";
  49. FPermanentLogFileName = "";
  50. FLogFileAppend = true;
  51. FLogWindowLines = 100;
  52. FLogProtocol = 0;
  53. FDisablePasswordStoring = false;
  54. FForceBanners = false;
  55. FDisableAcceptingHostKeys = false;
  56. Changed();
  57. }
  58. //---------------------------------------------------------------------------
  59. __fastcall TConfiguration::~TConfiguration()
  60. {
  61. assert(!FUpdating);
  62. if (FApplicationInfo) FreeFileInfo(FApplicationInfo);
  63. delete FCriticalSection;
  64. }
  65. //---------------------------------------------------------------------------
  66. THierarchicalStorage * TConfiguration::CreateScpStorage(bool /*SessionList*/)
  67. {
  68. if (Storage == stRegistry)
  69. {
  70. return new TRegistryStorage(RegistryStorageKey);
  71. }
  72. else
  73. {
  74. return new TIniFileStorage(IniFileStorageName);
  75. }
  76. }
  77. //---------------------------------------------------------------------------
  78. #define LASTELEM(ELEM) \
  79. ELEM.SubString(ELEM.LastDelimiter(".>")+1, ELEM.Length() - ELEM.LastDelimiter(".>"))
  80. #define BLOCK(KEY, CANCREATE, BLOCK) \
  81. if (Storage->OpenSubKey(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKey(); }
  82. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, VAR)
  83. #define REGCONFIG(CANCREATE) \
  84. BLOCK("Interface", CANCREATE, \
  85. KEY(String, RandomSeedFile); \
  86. KEY(Bool, ConfirmOverwriting); \
  87. KEY(Bool, ConfirmResume); \
  88. KEY(Bool, AutoReadDirectoryAfterOp); \
  89. KEY(Integer, SessionReopenAuto); \
  90. KEY(Integer, SessionReopenBackground); \
  91. KEY(Integer, TunnelLocalPortNumberLow); \
  92. KEY(Integer, TunnelLocalPortNumberHigh); \
  93. KEY(Bool, ShowFtpWelcomeMessage); \
  94. ); \
  95. BLOCK("Logging", CANCREATE, \
  96. KEYEX(Bool, PermanentLogging, Logging); \
  97. KEYEX(String,PermanentLogFileName, LogFileName); \
  98. KEY(Bool, LogFileAppend); \
  99. KEY(Integer, LogWindowLines); \
  100. KEY(Integer, LogProtocol); \
  101. );
  102. //---------------------------------------------------------------------------
  103. void __fastcall TConfiguration::SaveData(THierarchicalStorage * Storage, bool /*All*/)
  104. {
  105. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(LASTELEM(AnsiString(#NAME)), VAR)
  106. REGCONFIG(true);
  107. #undef KEYEX
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TConfiguration::Save(bool All)
  111. {
  112. if (FDontSave) return;
  113. if (Storage == stRegistry) CleanupIniFile();
  114. THierarchicalStorage * Storage = CreateScpStorage(false);
  115. try
  116. {
  117. Storage->AccessMode = smReadWrite;
  118. if (Storage->OpenSubKey(ConfigurationSubKey, true))
  119. {
  120. SaveData(Storage, All);
  121. }
  122. }
  123. __finally
  124. {
  125. delete Storage;
  126. }
  127. Saved();
  128. if (All)
  129. {
  130. StoredSessions->Save(true);
  131. }
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TConfiguration::Export(const AnsiString FileName)
  135. {
  136. THierarchicalStorage * Storage = NULL;
  137. THierarchicalStorage * ExportStorage = NULL;
  138. try
  139. {
  140. ExportStorage = new TIniFileStorage(FileName);
  141. ExportStorage->AccessMode = smReadWrite;
  142. Storage = CreateScpStorage(false);
  143. Storage->AccessMode = smRead;
  144. CopyData(Storage, ExportStorage);
  145. if (ExportStorage->OpenSubKey(ConfigurationSubKey, true))
  146. {
  147. SaveData(ExportStorage, true);
  148. }
  149. }
  150. __finally
  151. {
  152. delete ExportStorage;
  153. delete Storage;
  154. }
  155. StoredSessions->Export(FileName);
  156. }
  157. //---------------------------------------------------------------------------
  158. void __fastcall TConfiguration::LoadData(THierarchicalStorage * Storage)
  159. {
  160. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(LASTELEM(AnsiString(#NAME)), VAR)
  161. #pragma warn -eas
  162. REGCONFIG(false);
  163. #pragma warn +eas
  164. #undef KEYEX
  165. }
  166. //---------------------------------------------------------------------------
  167. void __fastcall TConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  168. {
  169. FDisablePasswordStoring = Storage->ReadBool("DisablePasswordStoring", FDisablePasswordStoring);
  170. FForceBanners = Storage->ReadBool("ForceBanners", FForceBanners);
  171. FDisableAcceptingHostKeys = Storage->ReadBool("DisableAcceptingHostKeys", FDisableAcceptingHostKeys);
  172. }
  173. //---------------------------------------------------------------------------
  174. void __fastcall TConfiguration::Load()
  175. {
  176. TGuard Guard(FCriticalSection);
  177. THierarchicalStorage * Storage = CreateScpStorage(false);
  178. try
  179. {
  180. Storage->AccessMode = smRead;
  181. if (Storage->OpenSubKey(ConfigurationSubKey, false))
  182. {
  183. LoadData(Storage);
  184. }
  185. }
  186. __finally
  187. {
  188. delete Storage;
  189. }
  190. TRegistryStorage * AdminStorage;
  191. AdminStorage = new TRegistryStorage(RegistryStorageKey, HKEY_LOCAL_MACHINE);
  192. try
  193. {
  194. if (AdminStorage->OpenRootKey(false))
  195. {
  196. LoadAdmin(AdminStorage);
  197. AdminStorage->CloseSubKey();
  198. }
  199. }
  200. __finally
  201. {
  202. delete AdminStorage;
  203. }
  204. }
  205. //---------------------------------------------------------------------------
  206. void __fastcall TConfiguration::CopyData(THierarchicalStorage * Source,
  207. THierarchicalStorage * Target)
  208. {
  209. TStrings * Names = new TStringList();
  210. try
  211. {
  212. if (Source->OpenSubKey(ConfigurationSubKey, false))
  213. {
  214. if (Target->OpenSubKey(ConfigurationSubKey, true))
  215. {
  216. if (Source->OpenSubKey("CDCache", false))
  217. {
  218. if (Target->OpenSubKey("CDCache", true))
  219. {
  220. Names->Clear();
  221. Source->GetValueNames(Names);
  222. for (int Index = 0; Index < Names->Count; Index++)
  223. {
  224. Target->WriteBinaryData(Names->Strings[Index],
  225. Source->ReadBinaryData(Names->Strings[Index]));
  226. }
  227. Target->CloseSubKey();
  228. }
  229. Source->CloseSubKey();
  230. }
  231. if (Source->OpenSubKey("Banners", false))
  232. {
  233. if (Target->OpenSubKey("Banners", true))
  234. {
  235. Names->Clear();
  236. Source->GetValueNames(Names);
  237. for (int Index = 0; Index < Names->Count; Index++)
  238. {
  239. Target->WriteString(Names->Strings[Index],
  240. Source->ReadString(Names->Strings[Index], ""));
  241. }
  242. Target->CloseSubKey();
  243. }
  244. Source->CloseSubKey();
  245. }
  246. Target->CloseSubKey();
  247. }
  248. Source->CloseSubKey();
  249. }
  250. if (Source->OpenSubKey(SshHostKeysSubKey, false))
  251. {
  252. if (Target->OpenSubKey(SshHostKeysSubKey, true))
  253. {
  254. Names->Clear();
  255. Source->GetValueNames(Names);
  256. for (int Index = 0; Index < Names->Count; Index++)
  257. {
  258. Target->WriteStringRaw(Names->Strings[Index],
  259. Source->ReadStringRaw(Names->Strings[Index], ""));
  260. }
  261. Target->CloseSubKey();
  262. }
  263. Source->CloseSubKey();
  264. }
  265. }
  266. __finally
  267. {
  268. delete Names;
  269. }
  270. }
  271. //---------------------------------------------------------------------------
  272. void __fastcall TConfiguration::LoadDirectoryChangesCache(const AnsiString SessionKey,
  273. TRemoteDirectoryChangesCache * DirectoryChangesCache)
  274. {
  275. THierarchicalStorage * Storage = CreateScpStorage(false);
  276. try
  277. {
  278. Storage->AccessMode = smRead;
  279. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  280. Storage->OpenSubKey("CDCache", false) &&
  281. Storage->ValueExists(SessionKey))
  282. {
  283. DirectoryChangesCache->Deserialize(Storage->ReadBinaryData(SessionKey));
  284. }
  285. }
  286. __finally
  287. {
  288. delete Storage;
  289. }
  290. }
  291. //---------------------------------------------------------------------------
  292. void __fastcall TConfiguration::SaveDirectoryChangesCache(const AnsiString SessionKey,
  293. TRemoteDirectoryChangesCache * DirectoryChangesCache)
  294. {
  295. THierarchicalStorage * Storage = CreateScpStorage(false);
  296. try
  297. {
  298. Storage->AccessMode = smReadWrite;
  299. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  300. Storage->OpenSubKey("CDCache", true))
  301. {
  302. AnsiString Data;
  303. DirectoryChangesCache->Serialize(Data);
  304. Storage->WriteBinaryData(SessionKey, Data);
  305. }
  306. }
  307. __finally
  308. {
  309. delete Storage;
  310. }
  311. }
  312. //---------------------------------------------------------------------------
  313. AnsiString __fastcall TConfiguration::BannerHash(const AnsiString & Banner)
  314. {
  315. AnsiString Result;
  316. Result.SetLength(16);
  317. md5checksum(Banner.c_str(), Banner.Length(), (unsigned char*)Result.c_str());
  318. return Result;
  319. }
  320. //---------------------------------------------------------------------------
  321. bool __fastcall TConfiguration::ShowBanner(const AnsiString SessionKey,
  322. const AnsiString & Banner)
  323. {
  324. bool Result;
  325. THierarchicalStorage * Storage = CreateScpStorage(false);
  326. try
  327. {
  328. Storage->AccessMode = smRead;
  329. Result =
  330. !Storage->OpenSubKey(ConfigurationSubKey, false) ||
  331. !Storage->OpenSubKey("Banners", false) ||
  332. !Storage->ValueExists(SessionKey) ||
  333. (Storage->ReadString(SessionKey, "") != StrToHex(BannerHash(Banner)));
  334. }
  335. __finally
  336. {
  337. delete Storage;
  338. }
  339. return Result;
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TConfiguration::NeverShowBanner(const AnsiString SessionKey,
  343. const AnsiString & Banner)
  344. {
  345. THierarchicalStorage * Storage = CreateScpStorage(false);
  346. try
  347. {
  348. Storage->AccessMode = smReadWrite;
  349. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  350. Storage->OpenSubKey("Banners", true))
  351. {
  352. Storage->WriteString(SessionKey, StrToHex(BannerHash(Banner)));
  353. }
  354. }
  355. __finally
  356. {
  357. delete Storage;
  358. }
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TConfiguration::Changed()
  362. {
  363. if (FUpdating == 0)
  364. {
  365. if (OnChange)
  366. {
  367. OnChange(this);
  368. }
  369. }
  370. else
  371. {
  372. FChanged = true;
  373. }
  374. }
  375. //---------------------------------------------------------------------------
  376. void __fastcall TConfiguration::BeginUpdate()
  377. {
  378. if (FUpdating == 0)
  379. {
  380. FChanged = false;
  381. }
  382. FUpdating++;
  383. // Greater value would probably indicate some nesting problem in code
  384. assert(FUpdating < 6);
  385. }
  386. //---------------------------------------------------------------------------
  387. void __fastcall TConfiguration::EndUpdate()
  388. {
  389. assert(FUpdating > 0);
  390. FUpdating--;
  391. if ((FUpdating == 0) && FChanged)
  392. {
  393. FChanged = false;
  394. Changed();
  395. }
  396. }
  397. //---------------------------------------------------------------------------
  398. void __fastcall TConfiguration::CleanupConfiguration()
  399. {
  400. try
  401. {
  402. CleanupRegistry(ConfigurationSubKey);
  403. if (Storage == stRegistry)
  404. {
  405. FDontSave = true;
  406. }
  407. }
  408. catch (Exception &E)
  409. {
  410. throw ExtException(&E, CLEANUP_CONFIG_ERROR);
  411. }
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TConfiguration::CleanupRegistry(AnsiString CleanupSubKey)
  415. {
  416. TRegistryStorage *Registry = new TRegistryStorage(RegistryStorageKey);
  417. try
  418. {
  419. Registry->RecursiveDeleteSubKey(CleanupSubKey);
  420. }
  421. __finally
  422. {
  423. delete Registry;
  424. }
  425. }
  426. //---------------------------------------------------------------------------
  427. void __fastcall TConfiguration::CleanupHostKeys()
  428. {
  429. try
  430. {
  431. CleanupRegistry(SshHostKeysSubKey);
  432. }
  433. catch (Exception &E)
  434. {
  435. throw ExtException(&E, CLEANUP_HOSTKEYS_ERROR);
  436. }
  437. }
  438. //---------------------------------------------------------------------------
  439. void __fastcall TConfiguration::CleanupRandomSeedFile()
  440. {
  441. try
  442. {
  443. DontSaveRandomSeed();
  444. AnsiString ExpandedRandomSeedFile = ExpandEnvironmentVariables(RandomSeedFile);
  445. if (FileExists(ExpandedRandomSeedFile))
  446. {
  447. if (!DeleteFile(ExpandedRandomSeedFile)) Abort();
  448. }
  449. }
  450. catch (Exception &E)
  451. {
  452. throw ExtException(&E, CLEANUP_SEEDFILE_ERROR);
  453. }
  454. }
  455. //---------------------------------------------------------------------------
  456. void __fastcall TConfiguration::CleanupIniFile()
  457. {
  458. try
  459. {
  460. if (FileExists(IniFileStorageName))
  461. {
  462. if (!DeleteFile(IniFileStorageName)) Abort();
  463. }
  464. if (Storage == stIniFile)
  465. {
  466. FDontSave = true;
  467. }
  468. }
  469. catch (Exception &E)
  470. {
  471. throw ExtException(&E, CLEANUP_INIFILE_ERROR);
  472. }
  473. }
  474. //---------------------------------------------------------------------------
  475. AnsiString __fastcall TConfiguration::GetOSVersionStr()
  476. {
  477. AnsiString Result;
  478. OSVERSIONINFO OSVersionInfo;
  479. OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
  480. if (GetVersionEx(&OSVersionInfo) != 0)
  481. {
  482. Result = FORMAT("%d.%d.%d %s", (int(OSVersionInfo.dwMajorVersion),
  483. int(OSVersionInfo.dwMinorVersion), int(OSVersionInfo.dwBuildNumber),
  484. OSVersionInfo.szCSDVersion)).Trim();
  485. }
  486. return Result;
  487. }
  488. //---------------------------------------------------------------------------
  489. TVSFixedFileInfo *__fastcall TConfiguration::GetFixedApplicationInfo()
  490. {
  491. return GetFixedFileInfo(ApplicationInfo);
  492. }
  493. //---------------------------------------------------------------------------
  494. AnsiString __fastcall TConfiguration::ModuleFileName()
  495. {
  496. return ParamStr(0);
  497. }
  498. //---------------------------------------------------------------------------
  499. void * __fastcall TConfiguration::GetFileApplicationInfo(const AnsiString FileName)
  500. {
  501. void * Result;
  502. if (FileName.IsEmpty())
  503. {
  504. if (!FApplicationInfo)
  505. {
  506. FApplicationInfo = CreateFileInfo(ModuleFileName());
  507. }
  508. Result = FApplicationInfo;
  509. }
  510. else
  511. {
  512. Result = CreateFileInfo(FileName);
  513. }
  514. return Result;
  515. }
  516. //---------------------------------------------------------------------------
  517. void * __fastcall TConfiguration::GetApplicationInfo()
  518. {
  519. return GetFileApplicationInfo("");
  520. }
  521. //---------------------------------------------------------------------------
  522. AnsiString __fastcall TConfiguration::GetFileProductName(const AnsiString FileName)
  523. {
  524. return GetFileFileInfoString("ProductName", FileName);
  525. }
  526. //---------------------------------------------------------------------------
  527. AnsiString __fastcall TConfiguration::GetFileCompanyName(const AnsiString FileName)
  528. {
  529. return GetFileFileInfoString("CompanyName", FileName);
  530. }
  531. //---------------------------------------------------------------------------
  532. AnsiString __fastcall TConfiguration::GetProductName()
  533. {
  534. return GetFileProductName("");
  535. }
  536. //---------------------------------------------------------------------------
  537. AnsiString __fastcall TConfiguration::GetCompanyName()
  538. {
  539. return GetFileCompanyName("");
  540. }
  541. //---------------------------------------------------------------------------
  542. AnsiString __fastcall TConfiguration::GetFileProductVersion(const AnsiString FileName)
  543. {
  544. return TrimVersion(GetFileFileInfoString("ProductVersion", FileName));
  545. }
  546. //---------------------------------------------------------------------------
  547. AnsiString __fastcall TConfiguration::GetProductVersion()
  548. {
  549. return GetFileProductVersion("");
  550. }
  551. //---------------------------------------------------------------------------
  552. AnsiString __fastcall TConfiguration::TrimVersion(AnsiString Version)
  553. {
  554. while ((Version.Pos(".") != Version.LastDelimiter(".")) &&
  555. (Version.SubString(Version.Length() - 1, 2) == ".0"))
  556. {
  557. Version.SetLength(Version.Length() - 2);
  558. }
  559. return Version;
  560. }
  561. //---------------------------------------------------------------------------
  562. AnsiString __fastcall TConfiguration::GetVersionStr()
  563. {
  564. TGuard Guard(FCriticalSection);
  565. try
  566. {
  567. return FmtLoadStr(VERSION, ARRAYOFCONST((
  568. HIWORD(FixedApplicationInfo->dwFileVersionMS),
  569. LOWORD(FixedApplicationInfo->dwFileVersionMS),
  570. HIWORD(FixedApplicationInfo->dwFileVersionLS),
  571. LOWORD(FixedApplicationInfo->dwFileVersionLS))));
  572. }
  573. catch (Exception &E)
  574. {
  575. throw ExtException(&E, "Can't get application version");
  576. }
  577. }
  578. //---------------------------------------------------------------------------
  579. AnsiString __fastcall TConfiguration::GetVersion()
  580. {
  581. TGuard Guard(FCriticalSection);
  582. try
  583. {
  584. AnsiString Result;
  585. Result = TrimVersion(FORMAT("%d.%d.%d", (
  586. HIWORD(FixedApplicationInfo->dwFileVersionMS),
  587. LOWORD(FixedApplicationInfo->dwFileVersionMS),
  588. HIWORD(FixedApplicationInfo->dwFileVersionLS))));
  589. return Result;
  590. }
  591. catch (Exception &E)
  592. {
  593. throw ExtException(&E, "Can't get application version");
  594. }
  595. }
  596. //---------------------------------------------------------------------------
  597. AnsiString __fastcall TConfiguration::GetFileFileInfoString(const AnsiString Key,
  598. const AnsiString FileName)
  599. {
  600. TGuard Guard(FCriticalSection);
  601. AnsiString Result;
  602. void * Info = GetFileApplicationInfo(FileName);
  603. try
  604. {
  605. if ((Info != NULL) && (GetTranslationCount(Info) > 0))
  606. {
  607. TTranslation Translation;
  608. Translation = GetTranslation(Info, 0);
  609. Result = ::GetFileInfoString(Info, Translation, Key);
  610. }
  611. else
  612. {
  613. assert(!FileName.IsEmpty());
  614. }
  615. }
  616. __finally
  617. {
  618. if (!FileName.IsEmpty())
  619. {
  620. FreeFileInfo(Info);
  621. }
  622. }
  623. return Result;
  624. }
  625. //---------------------------------------------------------------------------
  626. AnsiString __fastcall TConfiguration::GetFileInfoString(const AnsiString Key)
  627. {
  628. return GetFileFileInfoString(Key, "");
  629. }
  630. //---------------------------------------------------------------------------
  631. AnsiString __fastcall TConfiguration::GetRegistryStorageKey()
  632. {
  633. return GetRegistryKey();
  634. }
  635. //---------------------------------------------------------------------------
  636. void __fastcall TConfiguration::SetIniFileStorageName(AnsiString value)
  637. {
  638. FIniFileStorageName = value;
  639. FStorage = stIniFile;
  640. }
  641. //---------------------------------------------------------------------------
  642. AnsiString __fastcall TConfiguration::GetIniFileStorageName()
  643. {
  644. if (FIniFileStorageName.IsEmpty())
  645. {
  646. return ChangeFileExt(ParamStr(0), ".ini");
  647. }
  648. else
  649. {
  650. return FIniFileStorageName;
  651. }
  652. }
  653. //---------------------------------------------------------------------------
  654. AnsiString __fastcall TConfiguration::GetPuttyRegistryStorageKey()
  655. {
  656. return PUTTY_REG_POS;
  657. }
  658. //---------------------------------------------------------------------------
  659. AnsiString __fastcall TConfiguration::GetPuttySessionsKey()
  660. {
  661. return PuttyRegistryStorageKey + "\\Sessions";
  662. }
  663. //---------------------------------------------------------------------------
  664. AnsiString __fastcall TConfiguration::GetStoredSessionsSubKey()
  665. {
  666. return "Sessions";
  667. }
  668. //---------------------------------------------------------------------------
  669. AnsiString __fastcall TConfiguration::GetSshHostKeysSubKey()
  670. {
  671. return "SshHostKeys";
  672. }
  673. //---------------------------------------------------------------------------
  674. AnsiString __fastcall TConfiguration::GetConfigurationSubKey()
  675. {
  676. return "Configuration";
  677. }
  678. //---------------------------------------------------------------------------
  679. AnsiString __fastcall TConfiguration::GetRootKeyStr()
  680. {
  681. return RootKeyToStr(HKEY_CURRENT_USER);
  682. }
  683. //---------------------------------------------------------------------------
  684. bool __fastcall TConfiguration::GetGSSAPIInstalled()
  685. {
  686. return HasGSSAPI();
  687. }
  688. //---------------------------------------------------------------------------
  689. void __fastcall TConfiguration::SetStorage(TStorage value)
  690. {
  691. if (FStorage != value)
  692. {
  693. THierarchicalStorage * SourceStorage = NULL;
  694. THierarchicalStorage * TargetStorage = NULL;
  695. try
  696. {
  697. SourceStorage = CreateScpStorage(false);
  698. SourceStorage->AccessMode = smRead;
  699. FStorage = value;
  700. TargetStorage = CreateScpStorage(false);
  701. TargetStorage->AccessMode = smReadWrite;
  702. // copy before save as it removes the ini file,
  703. // when switching from ini to registry
  704. CopyData(SourceStorage, TargetStorage);
  705. }
  706. __finally
  707. {
  708. delete SourceStorage;
  709. delete TargetStorage;
  710. }
  711. Save(true);
  712. }
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TConfiguration::Saved()
  716. {
  717. // nothing
  718. }
  719. //---------------------------------------------------------------------------
  720. TStorage __fastcall TConfiguration::GetStorage()
  721. {
  722. if (FStorage == stDetect)
  723. {
  724. FStorage = FileExists(IniFileStorageName) ? stIniFile : stRegistry;
  725. }
  726. return FStorage;
  727. }
  728. //---------------------------------------------------------------------------
  729. void __fastcall TConfiguration::SetRandomSeedFile(AnsiString value)
  730. {
  731. if (RandomSeedFile != value)
  732. {
  733. // never allow empty seed file to avoid Putty trying to reinitialize the path
  734. if (value.Trim().IsEmpty())
  735. {
  736. FRandomSeedFile = FDefaultRandomSeedFile;
  737. }
  738. else
  739. {
  740. FRandomSeedFile = value;
  741. }
  742. char *seedpath = seedpath_ptr();
  743. if (value.Length() >= seedpath_size())
  744. {
  745. value.SetLength(seedpath_size() - 1);
  746. }
  747. strcpy(seedpath, StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).c_str());
  748. }
  749. }
  750. //---------------------------------------------------------------------------
  751. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  752. {
  753. return eolCRLF;
  754. }
  755. //---------------------------------------------------------------------
  756. void __fastcall TConfiguration::TemporaryLogging(const AnsiString ALogFileName)
  757. {
  758. FLogging = true;
  759. FLogFileName = ALogFileName;
  760. }
  761. //---------------------------------------------------------------------
  762. void __fastcall TConfiguration::SetLogging(bool value)
  763. {
  764. if (Logging != value)
  765. {
  766. FPermanentLogging = value;
  767. FLogging = value;
  768. Changed();
  769. }
  770. }
  771. //---------------------------------------------------------------------
  772. void __fastcall TConfiguration::SetLogFileName(AnsiString value)
  773. {
  774. if (LogFileName != value)
  775. {
  776. FPermanentLogFileName = value;
  777. FLogFileName = value;
  778. Changed();
  779. }
  780. }
  781. //---------------------------------------------------------------------
  782. void __fastcall TConfiguration::SetLogToFile(bool value)
  783. {
  784. if (value != LogToFile)
  785. {
  786. LogFileName = value ? DefaultLogFileName : AnsiString("");
  787. Changed();
  788. }
  789. }
  790. //---------------------------------------------------------------------
  791. bool __fastcall TConfiguration::GetLogToFile()
  792. {
  793. return !LogFileName.IsEmpty();
  794. }
  795. //---------------------------------------------------------------------
  796. void __fastcall TConfiguration::SetLogProtocol(int value)
  797. {
  798. SET_CONFIG_PROPERTY(LogProtocol);
  799. }
  800. //---------------------------------------------------------------------
  801. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  802. {
  803. SET_CONFIG_PROPERTY(LogFileAppend);
  804. }
  805. //---------------------------------------------------------------------
  806. void __fastcall TConfiguration::SetLogWindowLines(int value)
  807. {
  808. SET_CONFIG_PROPERTY(LogWindowLines);
  809. }
  810. //---------------------------------------------------------------------
  811. void __fastcall TConfiguration::SetLogWindowComplete(bool value)
  812. {
  813. if (value != LogWindowComplete)
  814. {
  815. LogWindowLines = value ? 0 : 50;
  816. Changed();
  817. }
  818. }
  819. //---------------------------------------------------------------------
  820. bool __fastcall TConfiguration::GetLogWindowComplete()
  821. {
  822. return (bool)(LogWindowLines == 0);
  823. }
  824. //---------------------------------------------------------------------
  825. AnsiString __fastcall TConfiguration::GetDefaultLogFileName()
  826. {
  827. return IncludeTrailingBackslash(SystemTemporaryDirectory()) + "winscp.log";
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  831. {
  832. TGuard Guard(FCriticalSection);
  833. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  834. }
  835. //---------------------------------------------------------------------------
  836. bool __fastcall TConfiguration::GetConfirmOverwriting()
  837. {
  838. TGuard Guard(FCriticalSection);
  839. return FConfirmOverwriting;
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TConfiguration::SetConfirmResume(bool value)
  843. {
  844. TGuard Guard(FCriticalSection);
  845. SET_CONFIG_PROPERTY(ConfirmResume);
  846. }
  847. //---------------------------------------------------------------------------
  848. bool __fastcall TConfiguration::GetConfirmResume()
  849. {
  850. TGuard Guard(FCriticalSection);
  851. return FConfirmResume;
  852. }
  853. //---------------------------------------------------------------------------
  854. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  855. {
  856. TGuard Guard(FCriticalSection);
  857. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  858. }
  859. //---------------------------------------------------------------------------
  860. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  861. {
  862. TGuard Guard(FCriticalSection);
  863. return FAutoReadDirectoryAfterOp;
  864. }
  865. //---------------------------------------------------------------------------
  866. AnsiString __fastcall TConfiguration::GetTimeFormat()
  867. {
  868. return "h:nn:ss";
  869. }
  870. //---------------------------------------------------------------------------
  871. AnsiString __fastcall TConfiguration::GetPartialExt() const
  872. {
  873. return PARTIAL_EXT;
  874. }
  875. //---------------------------------------------------------------------------
  876. AnsiString __fastcall TConfiguration::GetDefaultKeyFile()
  877. {
  878. return "";
  879. }
  880. //---------------------------------------------------------------------------
  881. AnsiString __fastcall TConfiguration::GetLocalInvalidChars()
  882. {
  883. return "/\\:*?\"<>|";
  884. }
  885. //---------------------------------------------------------------------------
  886. bool __fastcall TConfiguration::GetRememberPassword()
  887. {
  888. return false;
  889. }
  890. //---------------------------------------------------------------------------
  891. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  892. {
  893. SET_CONFIG_PROPERTY(SessionReopenAuto);
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  897. {
  898. SET_CONFIG_PROPERTY(SessionReopenBackground);
  899. }
  900. //---------------------------------------------------------------------------
  901. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  902. {
  903. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  907. {
  908. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  909. }
  910. //---------------------------------------------------------------------------
  911. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  912. {
  913. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  914. }