Configuration.cpp 79 KB

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