Configuration.cpp 79 KB

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