Configuration.cpp 77 KB

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