Configuration.cpp 73 KB

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