Configuration.cpp 67 KB

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