Configuration.cpp 79 KB

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