Configuration.cpp 66 KB

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