Configuration.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  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. #pragma clang diagnostic push
  1133. #pragma clang diagnostic ignored "-Wdate-time"
  1134. UnicodeString BuildDateStr = __DATE__;
  1135. #pragma clang diagnostic pop
  1136. UnicodeString MonthStr = CutToChar(BuildDateStr, L' ', true);
  1137. int Month = ParseShortEngMonthName(MonthStr);
  1138. int Day = StrToInt(CutToChar(BuildDateStr, L' ', true));
  1139. int Year = StrToInt(Trim(BuildDateStr));
  1140. TDateTime Result = EncodeDateVerbose(static_cast<Word>(Year), static_cast<Word>(Month), static_cast<Word>(Day));
  1141. return Result;
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. UnicodeString TConfiguration::GetFullVersion()
  1145. {
  1146. UnicodeString Result = Version;
  1147. UnicodeString AReleaseType = GetReleaseType();
  1148. if (DebugAlwaysTrue(!AReleaseType.IsEmpty()) &&
  1149. !SameText(AReleaseType, L"stable") &&
  1150. !SameText(AReleaseType, L"development"))
  1151. {
  1152. Result += L" " + AReleaseType;
  1153. }
  1154. return Result;
  1155. }
  1156. //---------------------------------------------------------------------------
  1157. static UnicodeString GetUnofficialBuildTag()
  1158. {
  1159. DebugAssert(Configuration->IsUnofficial);
  1160. UnicodeString Result;
  1161. #ifdef _DEBUG
  1162. Result = LoadStr(VERSION_DEBUG_BUILD);
  1163. #else
  1164. Result = LoadStr(VERSION_DEV_BUILD);
  1165. #endif
  1166. return Result;
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. UnicodeString TConfiguration::GetVersionStrHuman()
  1170. {
  1171. TGuard Guard(FCriticalSection);
  1172. try
  1173. {
  1174. TDateTime BuildDate = GetBuildDate();
  1175. UnicodeString FullVersion = GetFullVersion();
  1176. if (IsUnofficial)
  1177. {
  1178. UnicodeString BuildStr = GetUnofficialBuildTag();
  1179. FullVersion += L" " + BuildStr;
  1180. }
  1181. UnicodeString DateStr;
  1182. TDateTime ANow = Now();
  1183. if (BuildDate < ANow)
  1184. {
  1185. DateStr = FormatRelativeTime(ANow, BuildDate, true);
  1186. }
  1187. else
  1188. {
  1189. DateStr = FormatDateTime(L"ddddd", BuildDate);
  1190. }
  1191. UnicodeString Result = FORMAT(L"%s (%s)", (FullVersion, DateStr));
  1192. return Result;
  1193. }
  1194. catch (Exception &E)
  1195. {
  1196. throw ExtException(&E, L"Can't get application version");
  1197. }
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. UnicodeString __fastcall TConfiguration::GetVersionStr()
  1201. {
  1202. TGuard Guard(FCriticalSection);
  1203. try
  1204. {
  1205. UnicodeString BuildStr;
  1206. if (!IsUnofficial)
  1207. {
  1208. BuildStr = LoadStr(VERSION_BUILD);
  1209. }
  1210. else
  1211. {
  1212. BuildStr = GetUnofficialBuildTag();
  1213. }
  1214. UnicodeString FullVersion = GetFullVersion();
  1215. int Build = LOWORD(FixedApplicationInfo->dwFileVersionLS);
  1216. if (Build > 0)
  1217. {
  1218. BuildStr += L" " + IntToStr(Build);
  1219. }
  1220. UnicodeString DateStr = FormatDateTime(L"yyyy-mm-dd", GetBuildDate());
  1221. AddToList(BuildStr, DateStr, L" ");
  1222. UnicodeString Result = FMTLOAD(VERSION2, (FullVersion, BuildStr));
  1223. if (IsUnofficial)
  1224. {
  1225. Result += L" " + LoadStr(VERSION_DONT_DISTRIBUTE);
  1226. }
  1227. return Result;
  1228. }
  1229. catch (Exception &E)
  1230. {
  1231. throw ExtException(&E, L"Can't get application version");
  1232. }
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. UnicodeString __fastcall TConfiguration::GetFileVersion(const UnicodeString & FileName)
  1236. {
  1237. UnicodeString Result;
  1238. void * FileInfo = CreateFileInfo(FileName);
  1239. try
  1240. {
  1241. Result = GetFileVersion(GetFixedFileInfo(FileInfo));
  1242. }
  1243. __finally
  1244. {
  1245. FreeFileInfo(FileInfo);
  1246. }
  1247. return Result;
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. UnicodeString __fastcall TConfiguration::GetFileVersion(TVSFixedFileInfo * Info)
  1251. {
  1252. TGuard Guard(FCriticalSection);
  1253. try
  1254. {
  1255. UnicodeString Result =
  1256. FormatVersion(
  1257. HIWORD(Info->dwFileVersionMS),
  1258. LOWORD(Info->dwFileVersionMS),
  1259. HIWORD(Info->dwFileVersionLS));
  1260. return Result;
  1261. }
  1262. catch (Exception &E)
  1263. {
  1264. throw ExtException(&E, L"Can't get file version");
  1265. }
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. UnicodeString __fastcall TConfiguration::GetVersion()
  1269. {
  1270. return GetFileVersion(FixedApplicationInfo);
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. UnicodeString __fastcall TConfiguration::GetFileFileInfoString(const UnicodeString Key,
  1274. const UnicodeString FileName, bool AllowEmpty)
  1275. {
  1276. TGuard Guard(FCriticalSection);
  1277. UnicodeString Result;
  1278. void * Info = GetFileApplicationInfo(FileName);
  1279. try
  1280. {
  1281. if ((Info != NULL) && (GetTranslationCount(Info) > 0))
  1282. {
  1283. TTranslation Translation;
  1284. Translation = GetTranslation(Info, 0);
  1285. Result = ::GetFileInfoString(Info, Translation, Key, AllowEmpty);
  1286. }
  1287. else
  1288. {
  1289. DebugAssert(!FileName.IsEmpty());
  1290. }
  1291. }
  1292. __finally
  1293. {
  1294. if (!FileName.IsEmpty())
  1295. {
  1296. FreeFileInfo(Info);
  1297. }
  1298. }
  1299. return Result;
  1300. }
  1301. //---------------------------------------------------------------------------
  1302. UnicodeString __fastcall TConfiguration::GetFileInfoString(const UnicodeString Key)
  1303. {
  1304. return GetFileFileInfoString(Key, L"");
  1305. }
  1306. //---------------------------------------------------------------------------
  1307. UnicodeString __fastcall TConfiguration::GetFileMimeType(const UnicodeString & FileName)
  1308. {
  1309. UnicodeString Result;
  1310. bool Found = false;
  1311. if (!MimeTypes.IsEmpty())
  1312. {
  1313. UnicodeString FileNameOnly = ExtractFileName(FileName);
  1314. UnicodeString AMimeTypes = MimeTypes;
  1315. while (!Found && !AMimeTypes.IsEmpty())
  1316. {
  1317. UnicodeString Token = CutToChar(AMimeTypes, L',', true);
  1318. UnicodeString MaskStr = CutToChar(Token, L'=', true);
  1319. TFileMasks Mask(MaskStr);
  1320. if (Mask.MatchesFileName(FileNameOnly))
  1321. {
  1322. Result = Token.Trim();
  1323. Found = true;
  1324. }
  1325. }
  1326. }
  1327. if (!Found) // allow an override to "no" Content-Type
  1328. {
  1329. Result = ::GetFileMimeType(FileName);
  1330. }
  1331. return Result;
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. UnicodeString __fastcall TConfiguration::GetRegistryStorageKey()
  1335. {
  1336. return GetRegistryKey();
  1337. }
  1338. //---------------------------------------------------------------------------
  1339. void __fastcall TConfiguration::SetNulStorage()
  1340. {
  1341. FStorage = stNul;
  1342. }
  1343. //---------------------------------------------------------------------------
  1344. void __fastcall TConfiguration::SetExplicitIniFileStorageName(const UnicodeString & FileName)
  1345. {
  1346. FIniFileStorageName = FileName;
  1347. FStorage = stIniFile;
  1348. }
  1349. //---------------------------------------------------------------------------
  1350. UnicodeString __fastcall TConfiguration::GetDefaultIniFileExportPath()
  1351. {
  1352. UnicodeString PersonalDirectory = GetPersonalFolder();
  1353. UnicodeString FileName = IncludeTrailingBackslash(PersonalDirectory) +
  1354. ExtractFileName(ExpandEnvironmentVariables(IniFileStorageName));
  1355. return FileName;
  1356. }
  1357. //---------------------------------------------------------------------------
  1358. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReading()
  1359. {
  1360. return GetIniFileStorageName(true);
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReadingWriting()
  1364. {
  1365. return GetIniFileStorageName(false);
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. UnicodeString __fastcall TConfiguration::GetAutomaticIniFileStorageName(bool ReadingOnly)
  1369. {
  1370. UnicodeString ProgramPath = ParamStr(0);
  1371. UnicodeString ProgramIniPath = ChangeFileExt(ProgramPath, L".ini");
  1372. UnicodeString IniPath;
  1373. if (FileExists(ApiPath(ProgramIniPath)))
  1374. {
  1375. IniPath = ProgramIniPath;
  1376. }
  1377. else
  1378. {
  1379. UnicodeString AppDataIniPath =
  1380. IncludeTrailingBackslash(GetShellFolderPath(CSIDL_APPDATA)) +
  1381. ExtractFileName(ProgramIniPath);
  1382. if (FileExists(ApiPath(AppDataIniPath)))
  1383. {
  1384. IniPath = AppDataIniPath;
  1385. }
  1386. else
  1387. {
  1388. // avoid expensive test if we are interested in existing files only
  1389. if (!ReadingOnly && (FProgramIniPathWritable < 0))
  1390. {
  1391. UnicodeString ProgramDir = ExtractFilePath(ProgramPath);
  1392. FProgramIniPathWritable = IsDirectoryWriteable(ProgramDir) ? 1 : 0;
  1393. }
  1394. // does not really matter what we return when < 0
  1395. IniPath = (FProgramIniPathWritable == 0) ? AppDataIniPath : ProgramIniPath;
  1396. }
  1397. }
  1398. // BACKWARD COMPATIBILITY with 4.x
  1399. if (FVirtualIniFileStorageName.IsEmpty() &&
  1400. TPath::IsDriveRooted(IniPath))
  1401. {
  1402. UnicodeString LocalAppDataPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  1403. // virtual store for non-system drives have a different virtual store,
  1404. // do not bother about them
  1405. if (TPath::IsDriveRooted(LocalAppDataPath) &&
  1406. SameText(ExtractFileDrive(IniPath), ExtractFileDrive(LocalAppDataPath)))
  1407. {
  1408. FVirtualIniFileStorageName =
  1409. IncludeTrailingBackslash(LocalAppDataPath) +
  1410. L"VirtualStore\\" +
  1411. IniPath.SubString(4, IniPath.Length() - 3);
  1412. }
  1413. }
  1414. if (!FVirtualIniFileStorageName.IsEmpty() &&
  1415. FileExists(ApiPath(FVirtualIniFileStorageName)))
  1416. {
  1417. return FVirtualIniFileStorageName;
  1418. }
  1419. else
  1420. {
  1421. return IniPath;
  1422. }
  1423. }
  1424. //---------------------------------------------------------------------------
  1425. UnicodeString __fastcall TConfiguration::GetIniFileParamValue()
  1426. {
  1427. UnicodeString Result;
  1428. if (Storage == stNul)
  1429. {
  1430. Result = INI_NUL;
  1431. }
  1432. else if ((Storage == stIniFile) && !FIniFileStorageName.IsEmpty())
  1433. {
  1434. Result = FIniFileStorageName;
  1435. }
  1436. return Result;
  1437. }
  1438. //---------------------------------------------------------------------------
  1439. UnicodeString __fastcall TConfiguration::GetIniFileStorageName(bool ReadingOnly)
  1440. {
  1441. UnicodeString Result;
  1442. if (!FIniFileStorageName.IsEmpty())
  1443. {
  1444. Result = FIniFileStorageName;
  1445. }
  1446. else if (!FCustomIniFileStorageName.IsEmpty())
  1447. {
  1448. Result = ExpandEnvironmentVariables(FCustomIniFileStorageName);
  1449. }
  1450. else
  1451. {
  1452. Result = GetAutomaticIniFileStorageName(ReadingOnly);
  1453. }
  1454. return Result;
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. void __fastcall TConfiguration::SetOptionsStorage(TStrings * value)
  1458. {
  1459. TGuard Guard(FCriticalSection);
  1460. if (FOptionsStorage.get() == NULL)
  1461. {
  1462. FOptionsStorage.reset(new TStringList());
  1463. }
  1464. FOptionsStorage->AddStrings(value);
  1465. }
  1466. //---------------------------------------------------------------------------
  1467. TStrings * __fastcall TConfiguration::GetOptionsStorage()
  1468. {
  1469. return FOptionsStorage.get();
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. UnicodeString __fastcall TConfiguration::GetPuttySessionsSubKey()
  1473. {
  1474. return StoredSessionsSubKey;
  1475. }
  1476. //---------------------------------------------------------------------------
  1477. UnicodeString TConfiguration::GetPuttySessionsKey(const UnicodeString & RootKey)
  1478. {
  1479. return RootKey + L"\\" + PuttySessionsSubKey;
  1480. }
  1481. //---------------------------------------------------------------------------
  1482. UnicodeString __fastcall TConfiguration::DoGetPuttySessionsKey()
  1483. {
  1484. return GetPuttySessionsKey(PuttyRegistryStorageKey);
  1485. }
  1486. //---------------------------------------------------------------------------
  1487. UnicodeString __fastcall TConfiguration::GetStoredSessionsSubKey()
  1488. {
  1489. return L"Sessions";
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. UnicodeString __fastcall TConfiguration::GetSshHostKeysSubKey()
  1493. {
  1494. return L"SshHostKeys";
  1495. }
  1496. //---------------------------------------------------------------------------
  1497. UnicodeString __fastcall TConfiguration::GetConfigurationSubKey()
  1498. {
  1499. return L"Configuration";
  1500. }
  1501. //---------------------------------------------------------------------------
  1502. UnicodeString __fastcall TConfiguration::GetRootKeyStr()
  1503. {
  1504. return RootKeyToStr(HKEY_CURRENT_USER);
  1505. }
  1506. //---------------------------------------------------------------------------
  1507. void __fastcall TConfiguration::MoveStorage(TStorage AStorage, const UnicodeString & ACustomIniFileStorageName)
  1508. {
  1509. if ((FStorage != AStorage) ||
  1510. ((FStorage == stIniFile) && !FIniFileStorageName.IsEmpty()) ||
  1511. // Not expanding, as we want to allow change from explicit path to path with variables and vice versa
  1512. !IsPathToSameFile(FCustomIniFileStorageName, ACustomIniFileStorageName))
  1513. {
  1514. TStorage StorageBak = FStorage;
  1515. UnicodeString CustomIniFileStorageNameBak = FCustomIniFileStorageName;
  1516. UnicodeString IniFileStorageNameBak = FIniFileStorageName;
  1517. try
  1518. {
  1519. THierarchicalStorage * SourceStorage = NULL;
  1520. THierarchicalStorage * TargetStorage = NULL;
  1521. try
  1522. {
  1523. SourceStorage = CreateConfigStorage();
  1524. SourceStorage->AccessMode = smRead;
  1525. FStorage = AStorage;
  1526. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1527. FIniFileStorageName = UnicodeString();
  1528. TargetStorage = CreateConfigStorage();
  1529. TargetStorage->AccessMode = smReadWrite;
  1530. TargetStorage->Explicit = true;
  1531. // copy before save as it removes the ini file,
  1532. // when switching from ini to registry
  1533. CopyData(SourceStorage, TargetStorage);
  1534. }
  1535. __finally
  1536. {
  1537. delete SourceStorage;
  1538. delete TargetStorage;
  1539. }
  1540. // save all and explicit,
  1541. // this also removes an INI file, when switching to registry storage
  1542. DoSave(true, true);
  1543. }
  1544. catch (...)
  1545. {
  1546. // If this fails, do not pretend that storage was switched.
  1547. // For instance:
  1548. // - When writing to an INI file fails (unlikely, as we fallback to user profile)
  1549. // - When removing INI file fails, when switching to registry
  1550. // (possible, when the INI file is in Program Files folder)
  1551. FStorage = StorageBak;
  1552. FCustomIniFileStorageName = CustomIniFileStorageNameBak;
  1553. FIniFileStorageName = IniFileStorageNameBak;
  1554. throw;
  1555. }
  1556. }
  1557. }
  1558. //---------------------------------------------------------------------------
  1559. void __fastcall TConfiguration::ScheduleCustomIniFileStorageUse(const UnicodeString & ACustomIniFileStorageName)
  1560. {
  1561. FStorage = stIniFile;
  1562. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1563. SaveCustomIniFileStorageName();
  1564. }
  1565. //---------------------------------------------------------------------------
  1566. void __fastcall TConfiguration::Saved()
  1567. {
  1568. // nothing
  1569. }
  1570. //---------------------------------------------------------------------------
  1571. TStorage __fastcall TConfiguration::GetStorage()
  1572. {
  1573. TGuard Guard(FCriticalSection);
  1574. if (FStorage == stDetect)
  1575. {
  1576. DebugFail(); // This is never called, as the detection is completely overriden by TWinConfiguration
  1577. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  1578. {
  1579. FStorage = stIniFile;
  1580. }
  1581. else
  1582. {
  1583. FStorage = stRegistry;
  1584. }
  1585. }
  1586. return FStorage;
  1587. }
  1588. //---------------------------------------------------------------------
  1589. static TStoredSessionList * CreateSessionsForImport(TStoredSessionList * Sessions)
  1590. {
  1591. std::unique_ptr<TStoredSessionList> Result(new TStoredSessionList(true));
  1592. Result->DefaultSettings = Sessions->DefaultSettings;
  1593. return Result.release();
  1594. }
  1595. //---------------------------------------------------------------------
  1596. void TConfiguration::SelectSessionsToImportIfAny(
  1597. TStoredSessionList * ImportSessionList, TStoredSessionList * Sessions,
  1598. UnicodeString & Error, const UnicodeString & NoSessionsError)
  1599. {
  1600. if (ImportSessionList->Count > 0)
  1601. {
  1602. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1603. }
  1604. else
  1605. {
  1606. Error = NoSessionsError;
  1607. }
  1608. }
  1609. //---------------------------------------------------------------------
  1610. TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport(
  1611. TStoredSessionList * Sessions, UnicodeString & Error)
  1612. {
  1613. std::unique_ptr<TStoredSessionList> ImportSessionList(CreateSessionsForImport(Sessions));
  1614. UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
  1615. UnicodeString FilezillaSiteManagerFile = CombinePaths(AppDataPath, L"FileZilla\\sitemanager.xml");
  1616. UnicodeString FilezillaConfigurationFile = CombinePaths(AppDataPath, L"FileZilla\\filezilla.xml");
  1617. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1618. {
  1619. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile, FilezillaConfigurationFile);
  1620. UnicodeString NoSessionsError = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1621. SelectSessionsToImportIfAny(ImportSessionList.get(), Sessions, Error, NoSessionsError);
  1622. }
  1623. else
  1624. {
  1625. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1626. }
  1627. return ImportSessionList.release();
  1628. }
  1629. //---------------------------------------------------------------------
  1630. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1631. {
  1632. try
  1633. {
  1634. UnicodeString Error;
  1635. std::unique_ptr<TStoredSessionList> SessionsForImport(SelectFilezillaSessionsForImport(Sessions, Error));
  1636. return (SessionsForImport->Count > 0);
  1637. }
  1638. catch (...)
  1639. {
  1640. return false;
  1641. }
  1642. }
  1643. //---------------------------------------------------------------------
  1644. UnicodeString GetOpensshFolder()
  1645. {
  1646. UnicodeString ProfilePath = GetShellFolderPath(CSIDL_PROFILE);
  1647. UnicodeString Result = CombinePaths(ProfilePath, OpensshFolderName);
  1648. return Result;
  1649. }
  1650. //---------------------------------------------------------------------
  1651. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1652. TStoredSessionList * Sessions, UnicodeString & Error)
  1653. {
  1654. std::unique_ptr<TStoredSessionList> ImportSessionList(CreateSessionsForImport(Sessions));
  1655. UnicodeString KnownHostsFile = CombinePaths(GetOpensshFolder(), L"known_hosts");
  1656. try
  1657. {
  1658. if (FileExists(ApiPath(KnownHostsFile)))
  1659. {
  1660. std::unique_ptr<TStrings> Lines(new TStringList());
  1661. LoadScriptFromFile(KnownHostsFile, Lines.get(), true);
  1662. ImportSessionList->ImportFromKnownHosts(Lines.get());
  1663. }
  1664. else
  1665. {
  1666. throw Exception(LoadStr(KNOWN_HOSTS_NOT_FOUND));
  1667. }
  1668. }
  1669. catch (Exception & E)
  1670. {
  1671. Error = FORMAT(L"%s\n(%s)", (E.Message, KnownHostsFile));
  1672. }
  1673. return ImportSessionList.release();
  1674. }
  1675. //---------------------------------------------------------------------
  1676. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1677. TStrings * Lines, TStoredSessionList * Sessions, UnicodeString & Error)
  1678. {
  1679. std::unique_ptr<TStoredSessionList> ImportSessionList(CreateSessionsForImport(Sessions));
  1680. try
  1681. {
  1682. ImportSessionList->ImportFromKnownHosts(Lines);
  1683. }
  1684. catch (Exception & E)
  1685. {
  1686. Error = E.Message;
  1687. }
  1688. return ImportSessionList.release();
  1689. }
  1690. //---------------------------------------------------------------------------
  1691. TStoredSessionList * TConfiguration::SelectOpensshSessionsForImport(
  1692. TStoredSessionList * Sessions, UnicodeString & Error)
  1693. {
  1694. std::unique_ptr<TStoredSessionList> ImportSessionList(CreateSessionsForImport(Sessions));
  1695. UnicodeString ConfigFile = CombinePaths(GetOpensshFolder(), L"config");
  1696. try
  1697. {
  1698. if (FileExists(ApiPath(ConfigFile)))
  1699. {
  1700. std::unique_ptr<TStrings> Lines(new TStringList());
  1701. LoadScriptFromFile(ConfigFile, Lines.get(), true);
  1702. const UnicodeString OpensshIncludeDirective(L"Include");
  1703. for (int Index = 0; Index < Lines->Count; Index++)
  1704. {
  1705. UnicodeString Line = Lines->Strings[Index];
  1706. UnicodeString Directive, Args;
  1707. if (ParseOpensshDirective(Line, Directive, Args))
  1708. {
  1709. if (SameText(Directive, OpensshIncludeDirective))
  1710. {
  1711. while (!Args.IsEmpty())
  1712. {
  1713. UnicodeString IncludePath = ConvertPathFromOpenssh(CutOpensshToken(Args));
  1714. // If path does not exist, try if it works relatively to .ssh/
  1715. if (!FileExists(ApiPath(IncludePath)))
  1716. {
  1717. IncludePath = CombinePaths(GetOpensshFolder(), IncludePath);
  1718. }
  1719. if (FileExists(ApiPath(IncludePath)))
  1720. {
  1721. std::unique_ptr <TStrings> LinesToInclude(new TStringList());
  1722. LoadScriptFromFile(IncludePath, LinesToInclude.get(), true);
  1723. Lines->Delete(Index); // Not really needed
  1724. for (int Index2 = 0; Index2 < LinesToInclude->Count; Index2++)
  1725. {
  1726. Lines->Insert(Index + Index2, LinesToInclude->Strings[Index2]);
  1727. }
  1728. Index--;
  1729. }
  1730. }
  1731. }
  1732. }
  1733. }
  1734. ImportSessionList->ImportFromOpenssh(Lines.get());
  1735. UnicodeString NoSessionsError = FORMAT(L"%s\n(%s)", (LoadStr(OPENSSH_CONFIG_NO_SITES), ConfigFile));
  1736. SelectSessionsToImportIfAny(ImportSessionList.get(), Sessions, Error, NoSessionsError);
  1737. }
  1738. else
  1739. {
  1740. throw Exception(LoadStr(OPENSSH_CONFIG_NOT_FOUND));
  1741. }
  1742. }
  1743. catch (Exception & E)
  1744. {
  1745. Error = FORMAT(L"%s\n(%s)", (E.Message, ConfigFile));
  1746. }
  1747. return ImportSessionList.release();
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. TStoredSessionList * TConfiguration::SelectSessionsForImport(
  1751. TStoredSessionList * Sessions, const UnicodeString & FileName, UnicodeString & Error)
  1752. {
  1753. std::unique_ptr<TStoredSessionList> ImportSessionList(CreateSessionsForImport(Sessions));
  1754. try
  1755. {
  1756. if (FileName.IsEmpty())
  1757. {
  1758. throw Exception(LoadStr(INI_SELECT));
  1759. }
  1760. else
  1761. {
  1762. std::unique_ptr<THierarchicalStorage> ImportStorage(TIniFileStorage::CreateFromPath(FileName));
  1763. ImportStorage->AccessMode = smRead;
  1764. if (ImportStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  1765. {
  1766. ImportSessionList->Load(ImportStorage.get());
  1767. }
  1768. UnicodeString NoSessionsError = FMTLOAD(INI_NO_SITES, (FileName));
  1769. SelectSessionsToImportIfAny(ImportSessionList.get(), Sessions, Error, NoSessionsError);
  1770. }
  1771. }
  1772. catch (Exception & E)
  1773. {
  1774. Error = E.Message;
  1775. }
  1776. return ImportSessionList.release();
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1780. {
  1781. if (RandomSeedFile != value)
  1782. {
  1783. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1784. FRandomSeedFile = value;
  1785. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1786. if (RandomSeedFileName.IsEmpty())
  1787. {
  1788. FRandomSeedFile = FDefaultRandomSeedFile;
  1789. }
  1790. if (!PrevRandomSeedFileName.IsEmpty() &&
  1791. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1792. FileExists(ApiPath(PrevRandomSeedFileName)))
  1793. {
  1794. // ignore any error
  1795. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1796. }
  1797. }
  1798. }
  1799. //---------------------------------------------------------------------------
  1800. UnicodeString __fastcall TConfiguration::GetDirectoryStatisticsCacheKey(
  1801. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1802. {
  1803. std::unique_ptr<TStringList> RawOptions(new TStringList());
  1804. RawOptions->Add(SessionKey);
  1805. RawOptions->Add(UnixExcludeTrailingBackslash(Path));
  1806. TCopyParamType Defaults;
  1807. TCopyParamType FilterCopyParam;
  1808. FilterCopyParam.IncludeFileMask = CopyParam.IncludeFileMask;
  1809. FilterCopyParam.ExcludeHiddenFiles = CopyParam.ExcludeHiddenFiles;
  1810. FilterCopyParam.ExcludeEmptyDirectories = CopyParam.ExcludeEmptyDirectories;
  1811. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawOptions.get(), true));
  1812. FilterCopyParam.Save(OptionsStorage.get(), &Defaults);
  1813. UTF8String RawOptionsBuf(RawOptions->CommaText.LowerCase());
  1814. UnicodeString Result = Sha256(RawOptionsBuf.c_str(), RawOptionsBuf.Length());
  1815. return Result;
  1816. }
  1817. //---------------------------------------------------------------------------
  1818. THierarchicalStorage * TConfiguration::OpenDirectoryStatisticsCache(bool CanCreate)
  1819. {
  1820. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateConfigStorage());
  1821. Storage->AccessMode = CanCreate ? smReadWrite : smRead;
  1822. if (!Storage->OpenSubKey(DirectoryStatisticsCacheKey, CanCreate))
  1823. {
  1824. Storage.reset(NULL);
  1825. }
  1826. return Storage.release();
  1827. }
  1828. //---------------------------------------------------------------------------
  1829. TStrings * __fastcall TConfiguration::LoadDirectoryStatisticsCache(
  1830. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1831. {
  1832. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(false));
  1833. TStrings * Result;
  1834. if (Storage.get() != NULL)
  1835. {
  1836. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1837. UnicodeString Buf = Storage->ReadString(Key, UnicodeString());
  1838. Result = CommaTextToStringList(Buf);
  1839. }
  1840. else
  1841. {
  1842. Result = new TStringList();
  1843. }
  1844. return Result;
  1845. }
  1846. //---------------------------------------------------------------------------
  1847. void __fastcall TConfiguration::SaveDirectoryStatisticsCache(
  1848. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam, TStrings * DataList)
  1849. {
  1850. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(true));
  1851. if (Storage.get() != NULL)
  1852. {
  1853. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1854. UnicodeString Buf = DataList->CommaText;
  1855. Storage->WriteString(Key, Buf);
  1856. }
  1857. }
  1858. //---------------------------------------------------------------------
  1859. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1860. {
  1861. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1862. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1863. }
  1864. //---------------------------------------------------------------------
  1865. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1866. {
  1867. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1868. }
  1869. //---------------------------------------------------------------------
  1870. bool TConfiguration::HasLocalPortNumberLimits()
  1871. {
  1872. return (LocalPortNumberMin > 0) && (LocalPortNumberMax >= LocalPortNumberMin);
  1873. }
  1874. //---------------------------------------------------------------------
  1875. void TConfiguration::SetLocalPortNumberMin(int value)
  1876. {
  1877. SET_CONFIG_PROPERTY(LocalPortNumberMin);
  1878. }
  1879. //---------------------------------------------------------------------
  1880. void TConfiguration::SetLocalPortNumberMax(int value)
  1881. {
  1882. SET_CONFIG_PROPERTY(LocalPortNumberMax);
  1883. }
  1884. //---------------------------------------------------------------------
  1885. void __fastcall TConfiguration::SetMimeTypes(UnicodeString value)
  1886. {
  1887. SET_CONFIG_PROPERTY(MimeTypes);
  1888. }
  1889. //---------------------------------------------------------------------
  1890. void TConfiguration::SetCertificateStorage(const UnicodeString & value)
  1891. {
  1892. SET_CONFIG_PROPERTY(CertificateStorage);
  1893. }
  1894. //---------------------------------------------------------------------
  1895. UnicodeString TConfiguration::GetCertificateStorageExpanded()
  1896. {
  1897. UnicodeString Result = FCertificateStorage;
  1898. if (Result.IsEmpty())
  1899. {
  1900. UnicodeString DefaultCertificateStorage = CombinePaths(ExtractFilePath(ModuleFileName()), L"cacert.pem");
  1901. if (FileExists(DefaultCertificateStorage))
  1902. {
  1903. Result = DefaultCertificateStorage;
  1904. }
  1905. }
  1906. return Result;
  1907. }
  1908. //---------------------------------------------------------------------
  1909. void TConfiguration::SetAWSAPI(const UnicodeString & value)
  1910. {
  1911. SET_CONFIG_PROPERTY(AWSAPI);
  1912. }
  1913. //---------------------------------------------------------------------
  1914. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1915. {
  1916. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1917. }
  1918. //---------------------------------------------------------------------
  1919. void __fastcall TConfiguration::SetParallelDurationThreshold(int value)
  1920. {
  1921. SET_CONFIG_PROPERTY(ParallelDurationThreshold);
  1922. }
  1923. //---------------------------------------------------------------------
  1924. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1925. {
  1926. SET_CONFIG_PROPERTY_EX(PuttyRegistryStorageKey,
  1927. RefreshPuttySshHostCAList());
  1928. }
  1929. //---------------------------------------------------------------------------
  1930. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1931. {
  1932. return eolCRLF;
  1933. }
  1934. //---------------------------------------------------------------------
  1935. bool __fastcall TConfiguration::GetCollectUsage()
  1936. {
  1937. return FUsage->Collect;
  1938. }
  1939. //---------------------------------------------------------------------
  1940. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1941. {
  1942. FUsage->Collect = value;
  1943. }
  1944. //---------------------------------------------------------------------
  1945. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1946. {
  1947. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1948. {
  1949. TemporaryActionsLogging(ALogFileName);
  1950. }
  1951. else
  1952. {
  1953. FLogging = true;
  1954. FLogFileName = ALogFileName;
  1955. UpdateActualLogProtocol();
  1956. }
  1957. }
  1958. //---------------------------------------------------------------------
  1959. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1960. {
  1961. FLogActions = true;
  1962. FActionsLogFileName = ALogFileName;
  1963. }
  1964. //---------------------------------------------------------------------
  1965. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1966. {
  1967. FLogProtocol = ALogProtocol;
  1968. UpdateActualLogProtocol();
  1969. }
  1970. //---------------------------------------------------------------------
  1971. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1972. {
  1973. FLogSensitive = ALogSensitive;
  1974. }
  1975. //---------------------------------------------------------------------
  1976. void __fastcall TConfiguration::TemporaryLogMaxSize(__int64 ALogMaxSize)
  1977. {
  1978. FLogMaxSize = ALogMaxSize;
  1979. }
  1980. //---------------------------------------------------------------------
  1981. void __fastcall TConfiguration::TemporaryLogMaxCount(int ALogMaxCount)
  1982. {
  1983. FLogMaxCount = ALogMaxCount;
  1984. }
  1985. //---------------------------------------------------------------------
  1986. void __fastcall TConfiguration::SetLogging(bool value)
  1987. {
  1988. TGuard Guard(FCriticalSection);
  1989. if (Logging != value)
  1990. {
  1991. FPermanentLogging = value;
  1992. FLogging = value;
  1993. UpdateActualLogProtocol();
  1994. Changed();
  1995. }
  1996. }
  1997. //---------------------------------------------------------------------
  1998. bool __fastcall TConfiguration::GetLogging()
  1999. {
  2000. TGuard Guard(FCriticalSection);
  2001. return FPermanentLogging;
  2002. }
  2003. //---------------------------------------------------------------------
  2004. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  2005. {
  2006. TGuard Guard(FCriticalSection);
  2007. if (LogFileName != value)
  2008. {
  2009. FPermanentLogFileName = value;
  2010. FLogFileName = value;
  2011. Changed();
  2012. }
  2013. }
  2014. //---------------------------------------------------------------------
  2015. UnicodeString __fastcall TConfiguration::GetLogFileName()
  2016. {
  2017. TGuard Guard(FCriticalSection);
  2018. return FPermanentLogFileName;
  2019. }
  2020. //---------------------------------------------------------------------
  2021. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  2022. {
  2023. TGuard Guard(FCriticalSection);
  2024. if (ActionsLogFileName != value)
  2025. {
  2026. FPermanentActionsLogFileName = value;
  2027. FActionsLogFileName = value;
  2028. Changed();
  2029. }
  2030. }
  2031. //---------------------------------------------------------------------
  2032. UnicodeString __fastcall TConfiguration::GetPermanentActionsLogFileName()
  2033. {
  2034. TGuard Guard(FCriticalSection);
  2035. return FPermanentActionsLogFileName;
  2036. }
  2037. //---------------------------------------------------------------------
  2038. UnicodeString __fastcall TConfiguration::GetActionsLogFileName()
  2039. {
  2040. TGuard Guard(FCriticalSection);
  2041. return FActionsLogFileName;
  2042. }
  2043. //---------------------------------------------------------------------
  2044. bool __fastcall TConfiguration::GetLogToFile()
  2045. {
  2046. // guarded within GetLogFileName
  2047. return !LogFileName.IsEmpty();
  2048. }
  2049. //---------------------------------------------------------------------
  2050. void __fastcall TConfiguration::UpdateActualLogProtocol()
  2051. {
  2052. FActualLogProtocol = FLogging ? FLogProtocol : (-BelowNormalLogLevels - 1);
  2053. }
  2054. //---------------------------------------------------------------------
  2055. void __fastcall TConfiguration::SetLogProtocol(int value)
  2056. {
  2057. TGuard Guard(FCriticalSection);
  2058. if (LogProtocol != value)
  2059. {
  2060. FPermanentLogProtocol = value;
  2061. FLogProtocol = value;
  2062. Changed();
  2063. UpdateActualLogProtocol();
  2064. }
  2065. }
  2066. //---------------------------------------------------------------------
  2067. void __fastcall TConfiguration::SetLogActions(bool value)
  2068. {
  2069. TGuard Guard(FCriticalSection);
  2070. if (LogActions != value)
  2071. {
  2072. FPermanentLogActions = value;
  2073. FLogActions = value;
  2074. Changed();
  2075. }
  2076. }
  2077. //---------------------------------------------------------------------
  2078. bool __fastcall TConfiguration::GetLogActions()
  2079. {
  2080. TGuard Guard(FCriticalSection);
  2081. return FPermanentLogActions;
  2082. }
  2083. //---------------------------------------------------------------------
  2084. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  2085. {
  2086. SET_CONFIG_PROPERTY(LogFileAppend);
  2087. }
  2088. //---------------------------------------------------------------------
  2089. void __fastcall TConfiguration::SetLogSensitive(bool value)
  2090. {
  2091. if (LogSensitive != value)
  2092. {
  2093. FPermanentLogSensitive = value;
  2094. FLogSensitive = value;
  2095. Changed();
  2096. }
  2097. }
  2098. //---------------------------------------------------------------------
  2099. void __fastcall TConfiguration::SetLogMaxSize(__int64 value)
  2100. {
  2101. TGuard Guard(FCriticalSection);
  2102. if (LogMaxSize != value)
  2103. {
  2104. FPermanentLogMaxSize = value;
  2105. FLogMaxSize = value;
  2106. Changed();
  2107. }
  2108. }
  2109. //---------------------------------------------------------------------
  2110. __int64 __fastcall TConfiguration::GetLogMaxSize()
  2111. {
  2112. TGuard Guard(FCriticalSection);
  2113. return FPermanentLogMaxSize;
  2114. }
  2115. //---------------------------------------------------------------------
  2116. void __fastcall TConfiguration::SetLogMaxCount(int value)
  2117. {
  2118. if (LogMaxCount != value)
  2119. {
  2120. FPermanentLogMaxCount = value;
  2121. FLogMaxCount = value;
  2122. Changed();
  2123. }
  2124. }
  2125. //---------------------------------------------------------------------
  2126. int __fastcall TConfiguration::GetLogMaxCount()
  2127. {
  2128. TGuard Guard(FCriticalSection);
  2129. return FPermanentLogMaxCount;
  2130. }
  2131. //---------------------------------------------------------------------
  2132. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  2133. {
  2134. return L"%TEMP%\\!S.log";
  2135. }
  2136. //---------------------------------------------------------------------------
  2137. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  2138. {
  2139. TGuard Guard(FCriticalSection);
  2140. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  2141. }
  2142. //---------------------------------------------------------------------------
  2143. bool __fastcall TConfiguration::GetConfirmOverwriting()
  2144. {
  2145. TGuard Guard(FCriticalSection);
  2146. return FConfirmOverwriting;
  2147. }
  2148. //---------------------------------------------------------------------------
  2149. void __fastcall TConfiguration::SetConfirmResume(bool value)
  2150. {
  2151. TGuard Guard(FCriticalSection);
  2152. SET_CONFIG_PROPERTY(ConfirmResume);
  2153. }
  2154. //---------------------------------------------------------------------------
  2155. bool __fastcall TConfiguration::GetConfirmResume()
  2156. {
  2157. TGuard Guard(FCriticalSection);
  2158. return FConfirmResume;
  2159. }
  2160. //---------------------------------------------------------------------------
  2161. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  2162. {
  2163. TGuard Guard(FCriticalSection);
  2164. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  2165. }
  2166. //---------------------------------------------------------------------------
  2167. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  2168. {
  2169. TGuard Guard(FCriticalSection);
  2170. return FAutoReadDirectoryAfterOp;
  2171. }
  2172. //---------------------------------------------------------------------------
  2173. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  2174. {
  2175. return L"h:nn:ss";
  2176. }
  2177. //---------------------------------------------------------------------------
  2178. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  2179. {
  2180. return L"";
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. bool __fastcall TConfiguration::GetRememberPassword()
  2184. {
  2185. return false;
  2186. }
  2187. //---------------------------------------------------------------------------
  2188. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  2189. {
  2190. SET_CONFIG_PROPERTY(SessionReopenAuto);
  2191. }
  2192. //---------------------------------------------------------------------------
  2193. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  2194. {
  2195. SET_CONFIG_PROPERTY(SessionReopenBackground);
  2196. }
  2197. //---------------------------------------------------------------------------
  2198. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  2199. {
  2200. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  2201. }
  2202. //---------------------------------------------------------------------------
  2203. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  2204. {
  2205. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  2206. }
  2207. //---------------------------------------------------------------------------
  2208. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  2209. {
  2210. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  2211. }
  2212. //---------------------------------------------------------------------------
  2213. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  2214. {
  2215. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  2216. }
  2217. //---------------------------------------------------------------------------
  2218. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  2219. {
  2220. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  2221. }
  2222. //---------------------------------------------------------------------------
  2223. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  2224. {
  2225. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  2226. }
  2227. //---------------------------------------------------------------------------
  2228. void TConfiguration::SetQueueTransfersLimit(int value)
  2229. {
  2230. SET_CONFIG_PROPERTY(QueueTransfersLimit);
  2231. }
  2232. //---------------------------------------------------------------------------
  2233. const TSshHostCAList * TConfiguration::GetSshHostCAList()
  2234. {
  2235. return FSshHostCAList.get();
  2236. }
  2237. //---------------------------------------------------------------------------
  2238. void TConfiguration::SetSshHostCAList(const TSshHostCAList * value)
  2239. {
  2240. *FSshHostCAList = *value;
  2241. }
  2242. //---------------------------------------------------------------------------
  2243. const TSshHostCAList * TConfiguration::GetPuttySshHostCAList()
  2244. {
  2245. if (FPuttySshHostCAList.get() == NULL)
  2246. {
  2247. std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(PuttyRegistryStorageKey));
  2248. Storage->ConfigureForPutty();
  2249. FPuttySshHostCAList.reset(new TSshHostCAList());
  2250. LoadSshHostCAList(FPuttySshHostCAList.get(), Storage.get());
  2251. }
  2252. return FPuttySshHostCAList.get();
  2253. }
  2254. //---------------------------------------------------------------------------
  2255. void TConfiguration::RefreshPuttySshHostCAList()
  2256. {
  2257. FPuttySshHostCAList.reset(NULL);
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. const TSshHostCAList * TConfiguration::GetActiveSshHostCAList()
  2261. {
  2262. return FSshHostCAsFromPuTTY ? PuttySshHostCAList : SshHostCAList;
  2263. }
  2264. //---------------------------------------------------------------------------
  2265. bool __fastcall TConfiguration::GetPersistent()
  2266. {
  2267. return (Storage != stNul) && !FDontSave;
  2268. }
  2269. //---------------------------------------------------------------------------
  2270. //---------------------------------------------------------------------------
  2271. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  2272. {
  2273. FShortCuts.insert(ShortCut);
  2274. }
  2275. //---------------------------------------------------------------------------
  2276. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  2277. {
  2278. return (FShortCuts.count(ShortCut) != 0);
  2279. }