Configuration.cpp 75 KB

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