Configuration.cpp 31 KB

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