Configuration.cpp 55 KB

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