Configuration.cpp 66 KB

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