Configuration.cpp 79 KB

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