1
0

Configuration.cpp 75 KB

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