Configuration.cpp 28 KB

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