Configuration.cpp 69 KB

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