SessionData.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "SessionData.h"
  5. #include "Common.h"
  6. #include "Exceptions.h"
  7. #include "FileBuffer.h"
  8. #include "CoreMain.h"
  9. #include "TextsCore.h"
  10. #include "PuttyIntf.h"
  11. #include "RemoteFiles.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. enum TProxyType { pxNone, pxHTTP, pxSocks, pxTelnet }; // 0.53b and older
  16. const wchar_t * DefaultName = L"Default Settings";
  17. const wchar_t CipherNames[CIPHER_COUNT][10] = {L"WARN", L"3des", L"blowfish", L"aes", L"des", L"arcfour"};
  18. const wchar_t KexNames[KEX_COUNT][20] = {L"WARN", L"dh-group1-sha1", L"dh-group14-sha1", L"dh-gex-sha1", L"rsa" };
  19. const wchar_t ProtocolNames[PROTOCOL_COUNT][10] = { L"raw", L"telnet", L"rlogin", L"ssh" };
  20. const wchar_t SshProtList[][10] = {L"1 only", L"1", L"2", L"2 only"};
  21. const wchar_t ProxyMethodList[][10] = {L"none", L"SOCKS4", L"SOCKS5", L"HTTP", L"Telnet", L"Cmd" };
  22. const TCipher DefaultCipherList[CIPHER_COUNT] =
  23. { cipAES, cipBlowfish, cip3DES, cipWarn, cipArcfour, cipDES };
  24. const TKex DefaultKexList[KEX_COUNT] =
  25. { kexDHGEx, kexDHGroup14, kexDHGroup1, kexRSA, kexWarn };
  26. const wchar_t FSProtocolNames[FSPROTOCOL_COUNT][11] = { L"SCP", L"SFTP (SCP)", L"SFTP", L"", L"", L"FTP" };
  27. const int SshPortNumber = 22;
  28. const int FtpPortNumber = 21;
  29. const int FtpsImplicitPortNumber = 990;
  30. const int DefaultSendBuf = 262144;
  31. const UnicodeString AnonymousUserName("anonymous");
  32. const UnicodeString AnonymousPassword("[email protected]");
  33. //---------------------------------------------------------------------
  34. TDateTime __fastcall SecToDateTime(int Sec)
  35. {
  36. return TDateTime((unsigned short)(Sec/SecsPerHour),
  37. (unsigned short)(Sec/SecsPerMin%MinsPerHour), (unsigned short)(Sec%SecsPerMin), 0);
  38. }
  39. //--- TSessionData ----------------------------------------------------
  40. __fastcall TSessionData::TSessionData(UnicodeString aName):
  41. TNamedObject(aName)
  42. {
  43. Default();
  44. FModified = true;
  45. }
  46. //---------------------------------------------------------------------
  47. void __fastcall TSessionData::Default()
  48. {
  49. HostName = L"";
  50. PortNumber = SshPortNumber;
  51. UserName = L"";
  52. Password = L"";
  53. PingInterval = 30;
  54. // when changing default, update load/save logic
  55. PingType = ptOff;
  56. Timeout = 15;
  57. TryAgent = true;
  58. AgentFwd = false;
  59. AuthTIS = false;
  60. AuthKI = true;
  61. AuthKIPassword = true;
  62. AuthGSSAPI = false;
  63. GSSAPIFwdTGT = false;
  64. GSSAPIServerRealm = L"";
  65. ChangeUsername = false;
  66. Compression = false;
  67. SshProt = ssh2;
  68. Ssh2DES = false;
  69. SshNoUserAuth = false;
  70. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  71. {
  72. Cipher[Index] = DefaultCipherList[Index];
  73. }
  74. for (int Index = 0; Index < KEX_COUNT; Index++)
  75. {
  76. Kex[Index] = DefaultKexList[Index];
  77. }
  78. PublicKeyFile = L"";
  79. FProtocol = ptSSH;
  80. TcpNoDelay = true;
  81. SendBuf = DefaultSendBuf;
  82. SshSimple = true;
  83. HostKey = L"";
  84. ProxyMethod = ::pmNone;
  85. ProxyHost = L"proxy";
  86. ProxyPort = 80;
  87. ProxyUsername = L"";
  88. ProxyPassword = L"";
  89. ProxyTelnetCommand = L"connect %host %port\\n";
  90. ProxyLocalCommand = L"";
  91. ProxyDNS = asAuto;
  92. ProxyLocalhost = false;
  93. for (unsigned int Index = 0; Index < LENOF(FBugs); Index++)
  94. {
  95. Bug[(TSshBug)Index] = asAuto;
  96. }
  97. Special = false;
  98. FSProtocol = fsSFTP;
  99. AddressFamily = afAuto;
  100. RekeyData = L"1G";
  101. RekeyTime = MinsPerHour;
  102. // FS common
  103. LocalDirectory = L"";
  104. RemoteDirectory = L"";
  105. SynchronizeBrowsing = false;
  106. UpdateDirectories = true;
  107. CacheDirectories = true;
  108. CacheDirectoryChanges = true;
  109. PreserveDirectoryChanges = true;
  110. LockInHome = false;
  111. ResolveSymlinks = true;
  112. DSTMode = dstmUnix;
  113. DeleteToRecycleBin = false;
  114. OverwrittenToRecycleBin = false;
  115. RecycleBinPath = L"";
  116. Color = 0;
  117. PostLoginCommands = L"";
  118. // SCP
  119. ReturnVar = L"";
  120. LookupUserGroups = asAuto;
  121. EOLType = eolLF;
  122. Shell = L""; //default shell
  123. ReturnVar = L"";
  124. ClearAliases = true;
  125. UnsetNationalVars = true;
  126. ListingCommand = L"ls -la";
  127. IgnoreLsWarnings = true;
  128. Scp1Compatibility = false;
  129. TimeDifference = 0;
  130. SCPLsFullTime = asAuto;
  131. NotUtf = asAuto;
  132. // SFTP
  133. SftpServer = L"";
  134. SFTPDownloadQueue = 4;
  135. SFTPUploadQueue = 4;
  136. SFTPListingQueue = 2;
  137. SFTPMaxVersion = 5;
  138. SFTPMaxPacketSize = 0;
  139. for (unsigned int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  140. {
  141. SFTPBug[(TSftpBug)Index] = asAuto;
  142. }
  143. Tunnel = false;
  144. TunnelHostName = L"";
  145. TunnelPortNumber = SshPortNumber;
  146. TunnelUserName = L"";
  147. TunnelPassword = L"";
  148. TunnelPublicKeyFile = L"";
  149. TunnelLocalPortNumber = 0;
  150. TunnelPortFwd = L"";
  151. TunnelHostKey = L"";
  152. // FTP
  153. FtpPasvMode = true;
  154. FtpForcePasvIp = asAuto;
  155. FtpAccount = L"";
  156. FtpPingInterval = 30;
  157. FtpPingType = ptDummyCommand;
  158. Ftps = ftpsNone;
  159. FtpListAll = asAuto;
  160. SslSessionReuse = true;
  161. FtpProxyLogonType = 0; // none
  162. CustomParam1 = L"";
  163. CustomParam2 = L"";
  164. FIsWorkspace = false;
  165. Selected = false;
  166. FModified = false;
  167. FSource = ::ssNone;
  168. // add also to TSessionLog::AddStartupInfo()
  169. }
  170. //---------------------------------------------------------------------
  171. void __fastcall TSessionData::NonPersistant()
  172. {
  173. UpdateDirectories = false;
  174. PreserveDirectoryChanges = false;
  175. }
  176. //---------------------------------------------------------------------
  177. #define BASE_PROPERTIES \
  178. PROPERTY(HostName); \
  179. PROPERTY(PortNumber); \
  180. PROPERTY(UserName); \
  181. PROPERTY(Password); \
  182. PROPERTY(PublicKeyFile); \
  183. PROPERTY(FSProtocol); \
  184. PROPERTY(Ftps); \
  185. PROPERTY(LocalDirectory); \
  186. PROPERTY(RemoteDirectory); \
  187. //---------------------------------------------------------------------
  188. #define ADVANCED_PROPERTIES \
  189. PROPERTY(PingInterval); \
  190. PROPERTY(PingType); \
  191. PROPERTY(Timeout); \
  192. PROPERTY(TryAgent); \
  193. PROPERTY(AgentFwd); \
  194. PROPERTY(AuthTIS); \
  195. PROPERTY(ChangeUsername); \
  196. PROPERTY(Compression); \
  197. PROPERTY(SshProt); \
  198. PROPERTY(Ssh2DES); \
  199. PROPERTY(SshNoUserAuth); \
  200. PROPERTY(CipherList); \
  201. PROPERTY(KexList); \
  202. PROPERTY(AddressFamily); \
  203. PROPERTY(RekeyData); \
  204. PROPERTY(RekeyTime); \
  205. PROPERTY(HostKey); \
  206. \
  207. PROPERTY(SynchronizeBrowsing); \
  208. PROPERTY(UpdateDirectories); \
  209. PROPERTY(CacheDirectories); \
  210. PROPERTY(CacheDirectoryChanges); \
  211. PROPERTY(PreserveDirectoryChanges); \
  212. \
  213. PROPERTY(ResolveSymlinks); \
  214. PROPERTY(DSTMode); \
  215. PROPERTY(LockInHome); \
  216. PROPERTY(Special); \
  217. PROPERTY(Selected); \
  218. PROPERTY(ReturnVar); \
  219. PROPERTY(LookupUserGroups); \
  220. PROPERTY(EOLType); \
  221. PROPERTY(Shell); \
  222. PROPERTY(ClearAliases); \
  223. PROPERTY(Scp1Compatibility); \
  224. PROPERTY(UnsetNationalVars); \
  225. PROPERTY(ListingCommand); \
  226. PROPERTY(IgnoreLsWarnings); \
  227. PROPERTY(SCPLsFullTime); \
  228. \
  229. PROPERTY(TimeDifference); \
  230. PROPERTY(TcpNoDelay); \
  231. PROPERTY(SendBuf); \
  232. PROPERTY(SshSimple); \
  233. PROPERTY(AuthKI); \
  234. PROPERTY(AuthKIPassword); \
  235. PROPERTY(AuthGSSAPI); \
  236. PROPERTY(GSSAPIFwdTGT); \
  237. PROPERTY(GSSAPIServerRealm); \
  238. PROPERTY(DeleteToRecycleBin); \
  239. PROPERTY(OverwrittenToRecycleBin); \
  240. PROPERTY(RecycleBinPath); \
  241. PROPERTY(NotUtf); \
  242. PROPERTY(PostLoginCommands); \
  243. \
  244. PROPERTY(ProxyMethod); \
  245. PROPERTY(ProxyHost); \
  246. PROPERTY(ProxyPort); \
  247. PROPERTY(ProxyUsername); \
  248. PROPERTY(ProxyPassword); \
  249. PROPERTY(ProxyTelnetCommand); \
  250. PROPERTY(ProxyLocalCommand); \
  251. PROPERTY(ProxyDNS); \
  252. PROPERTY(ProxyLocalhost); \
  253. \
  254. for (unsigned int Index = 0; Index < LENOF(FBugs); Index++) \
  255. { \
  256. PROPERTY(Bug[(TSshBug)Index]); \
  257. } \
  258. \
  259. PROPERTY(SftpServer); \
  260. PROPERTY(SFTPDownloadQueue); \
  261. PROPERTY(SFTPUploadQueue); \
  262. PROPERTY(SFTPListingQueue); \
  263. PROPERTY(SFTPMaxVersion); \
  264. PROPERTY(SFTPMaxPacketSize); \
  265. \
  266. for (unsigned int Index = 0; Index < LENOF(FSFTPBugs); Index++) \
  267. { \
  268. PROPERTY(SFTPBug[(TSftpBug)Index]); \
  269. } \
  270. \
  271. PROPERTY(Color); \
  272. \
  273. PROPERTY(Tunnel); \
  274. PROPERTY(TunnelHostName); \
  275. PROPERTY(TunnelPortNumber); \
  276. PROPERTY(TunnelUserName); \
  277. PROPERTY(TunnelPassword); \
  278. PROPERTY(TunnelPublicKeyFile); \
  279. PROPERTY(TunnelLocalPortNumber); \
  280. PROPERTY(TunnelPortFwd); \
  281. PROPERTY(TunnelHostKey); \
  282. \
  283. PROPERTY(FtpPasvMode); \
  284. PROPERTY(FtpForcePasvIp); \
  285. PROPERTY(FtpAccount); \
  286. PROPERTY(FtpPingInterval); \
  287. PROPERTY(FtpPingType); \
  288. PROPERTY(FtpListAll); \
  289. PROPERTY(SslSessionReuse); \
  290. \
  291. PROPERTY(FtpProxyLogonType); \
  292. \
  293. PROPERTY(CustomParam1); \
  294. PROPERTY(CustomParam2);
  295. //---------------------------------------------------------------------
  296. void __fastcall TSessionData::Assign(TPersistent * Source)
  297. {
  298. if (Source && Source->InheritsFrom(__classid(TSessionData)))
  299. {
  300. #define PROPERTY(P) P = ((TSessionData *)Source)->P
  301. PROPERTY(Name);
  302. BASE_PROPERTIES;
  303. ADVANCED_PROPERTIES;
  304. #undef PROPERTY
  305. FModified = ((TSessionData *)Source)->Modified;
  306. FSource = ((TSessionData *)Source)->FSource;
  307. }
  308. else
  309. {
  310. TNamedObject::Assign(Source);
  311. }
  312. }
  313. //---------------------------------------------------------------------
  314. bool __fastcall TSessionData::IsSame(const TSessionData * Default, bool AdvancedOnly)
  315. {
  316. #define PROPERTY(P) if (P != Default->P) return false;
  317. if (!AdvancedOnly)
  318. {
  319. BASE_PROPERTIES;
  320. }
  321. ADVANCED_PROPERTIES;
  322. #undef PROPERTY
  323. return true;
  324. }
  325. //---------------------------------------------------------------------
  326. void __fastcall TSessionData::DoLoad(THierarchicalStorage * Storage, bool & RewritePassword)
  327. {
  328. PortNumber = Storage->ReadInteger(L"PortNumber", PortNumber);
  329. UserName = Storage->ReadString(L"UserName", UserName);
  330. // must be loaded after UserName, because HostName may be in format user@host
  331. HostName = Storage->ReadString(L"HostName", HostName);
  332. if (!Configuration->DisablePasswordStoring)
  333. {
  334. if (Storage->ValueExists(L"PasswordPlain"))
  335. {
  336. Password = Storage->ReadString(L"PasswordPlain", Password);
  337. RewritePassword = true;
  338. }
  339. else
  340. {
  341. FPassword = Storage->ReadStringAsBinaryData(L"Password", FPassword);
  342. }
  343. }
  344. HostKey = Storage->ReadString(L"HostKey", HostKey);
  345. // Putty uses PingIntervalSecs
  346. int PingIntervalSecs = Storage->ReadInteger(L"PingIntervalSecs", -1);
  347. if (PingIntervalSecs < 0)
  348. {
  349. PingIntervalSecs = Storage->ReadInteger(L"PingIntervalSec", PingInterval%SecsPerMin);
  350. }
  351. PingInterval =
  352. Storage->ReadInteger(L"PingInterval", PingInterval/SecsPerMin)*SecsPerMin +
  353. PingIntervalSecs;
  354. if (PingInterval == 0)
  355. {
  356. PingInterval = 30;
  357. }
  358. // PingType has not existed before 3.5, where PingInterval > 0 meant today's ptNullPacket
  359. // Since 3.5, until 4.1 PingType was stored unconditionally.
  360. // Since 4.1 PingType is stored when it is not ptOff (default) or
  361. // when PingInterval is stored.
  362. if (!Storage->ValueExists(L"PingType"))
  363. {
  364. if (Storage->ReadInteger(L"PingInterval", 0) > 0)
  365. {
  366. PingType = ptNullPacket;
  367. }
  368. }
  369. else
  370. {
  371. PingType = static_cast<TPingType>(Storage->ReadInteger(L"PingType", ptOff));
  372. }
  373. Timeout = Storage->ReadInteger(L"Timeout", Timeout);
  374. TryAgent = Storage->ReadBool(L"TryAgent", TryAgent);
  375. AgentFwd = Storage->ReadBool(L"AgentFwd", AgentFwd);
  376. AuthTIS = Storage->ReadBool(L"AuthTIS", AuthTIS);
  377. AuthKI = Storage->ReadBool(L"AuthKI", AuthKI);
  378. AuthKIPassword = Storage->ReadBool(L"AuthKIPassword", AuthKIPassword);
  379. // Continue to use setting keys of previous kerberos implementation (vaclav tomec),
  380. // but fallback to keys of other implementations (official putty and vintela quest putty),
  381. // to allow imports from all putty versions.
  382. // Both vaclav tomec and official putty use AuthGSSAPI
  383. AuthGSSAPI = Storage->ReadBool(L"AuthGSSAPI", Storage->ReadBool(L"AuthSSPI", AuthGSSAPI));
  384. GSSAPIFwdTGT = Storage->ReadBool(L"GSSAPIFwdTGT", Storage->ReadBool(L"GssapiFwd", Storage->ReadBool(L"SSPIFwdTGT", GSSAPIFwdTGT)));
  385. GSSAPIServerRealm = Storage->ReadString(L"GSSAPIServerRealm", Storage->ReadString(L"KerbPrincipal", GSSAPIServerRealm));
  386. ChangeUsername = Storage->ReadBool(L"ChangeUsername", ChangeUsername);
  387. Compression = Storage->ReadBool(L"Compression", Compression);
  388. SshProt = (TSshProt)Storage->ReadInteger(L"SshProt", SshProt);
  389. Ssh2DES = Storage->ReadBool(L"Ssh2DES", Ssh2DES);
  390. SshNoUserAuth = Storage->ReadBool(L"SshNoUserAuth", SshNoUserAuth);
  391. CipherList = Storage->ReadString(L"Cipher", CipherList);
  392. KexList = Storage->ReadString(L"KEX", KexList);
  393. PublicKeyFile = Storage->ReadString(L"PublicKeyFile", PublicKeyFile);
  394. AddressFamily = static_cast<TAddressFamily>
  395. (Storage->ReadInteger(L"AddressFamily", AddressFamily));
  396. RekeyData = Storage->ReadString(L"RekeyBytes", RekeyData);
  397. RekeyTime = Storage->ReadInteger(L"RekeyTime", RekeyTime);
  398. FSProtocol = (TFSProtocol)Storage->ReadInteger(L"FSProtocol", FSProtocol);
  399. LocalDirectory = Storage->ReadString(L"LocalDirectory", LocalDirectory);
  400. RemoteDirectory = Storage->ReadString(L"RemoteDirectory", RemoteDirectory);
  401. SynchronizeBrowsing = Storage->ReadBool(L"SynchronizeBrowsing", SynchronizeBrowsing);
  402. UpdateDirectories = Storage->ReadBool(L"UpdateDirectories", UpdateDirectories);
  403. CacheDirectories = Storage->ReadBool(L"CacheDirectories", CacheDirectories);
  404. CacheDirectoryChanges = Storage->ReadBool(L"CacheDirectoryChanges", CacheDirectoryChanges);
  405. PreserveDirectoryChanges = Storage->ReadBool(L"PreserveDirectoryChanges", PreserveDirectoryChanges);
  406. ResolveSymlinks = Storage->ReadBool(L"ResolveSymlinks", ResolveSymlinks);
  407. DSTMode = (TDSTMode)Storage->ReadInteger(L"ConsiderDST", DSTMode);
  408. LockInHome = Storage->ReadBool(L"LockInHome", LockInHome);
  409. Special = Storage->ReadBool(L"Special", Special);
  410. Shell = Storage->ReadString(L"Shell", Shell);
  411. ClearAliases = Storage->ReadBool(L"ClearAliases", ClearAliases);
  412. UnsetNationalVars = Storage->ReadBool(L"UnsetNationalVars", UnsetNationalVars);
  413. ListingCommand = Storage->ReadString(L"ListingCommand",
  414. Storage->ReadBool(L"AliasGroupList", false) ? UnicodeString(L"ls -gla") : ListingCommand);
  415. IgnoreLsWarnings = Storage->ReadBool(L"IgnoreLsWarnings", IgnoreLsWarnings);
  416. SCPLsFullTime = TAutoSwitch(Storage->ReadInteger(L"SCPLsFullTime", SCPLsFullTime));
  417. Scp1Compatibility = Storage->ReadBool(L"Scp1Compatibility", Scp1Compatibility);
  418. TimeDifference = Storage->ReadFloat(L"TimeDifference", TimeDifference);
  419. DeleteToRecycleBin = Storage->ReadBool(L"DeleteToRecycleBin", DeleteToRecycleBin);
  420. OverwrittenToRecycleBin = Storage->ReadBool(L"OverwrittenToRecycleBin", OverwrittenToRecycleBin);
  421. RecycleBinPath = Storage->ReadString(L"RecycleBinPath", RecycleBinPath);
  422. PostLoginCommands = Storage->ReadString(L"PostLoginCommands", PostLoginCommands);
  423. ReturnVar = Storage->ReadString(L"ReturnVar", ReturnVar);
  424. LookupUserGroups = TAutoSwitch(Storage->ReadInteger(L"LookupUserGroups2", LookupUserGroups));
  425. EOLType = (TEOLType)Storage->ReadInteger(L"EOLType", EOLType);
  426. NotUtf = TAutoSwitch(Storage->ReadInteger(L"Utf", Storage->ReadInteger(L"SFTPUtfBug", NotUtf)));
  427. TcpNoDelay = Storage->ReadBool(L"TcpNoDelay", TcpNoDelay);
  428. SendBuf = Storage->ReadInteger(L"SendBuf", Storage->ReadInteger("SshSendBuf", SendBuf));
  429. SshSimple = Storage->ReadBool(L"SshSimple", SshSimple);
  430. ProxyMethod = (TProxyMethod)Storage->ReadInteger(L"ProxyMethod", -1);
  431. if (ProxyMethod < 0)
  432. {
  433. int ProxyType = Storage->ReadInteger(L"ProxyType", pxNone);
  434. int ProxySOCKSVersion;
  435. switch (ProxyType) {
  436. case pxHTTP:
  437. ProxyMethod = pmHTTP;
  438. break;
  439. case pxTelnet:
  440. ProxyMethod = pmTelnet;
  441. break;
  442. case pxSocks:
  443. ProxySOCKSVersion = Storage->ReadInteger(L"ProxySOCKSVersion", 5);
  444. ProxyMethod = ProxySOCKSVersion == 5 ? pmSocks5 : pmSocks4;
  445. break;
  446. default:
  447. case pxNone:
  448. ProxyMethod = ::pmNone;
  449. break;
  450. }
  451. }
  452. ProxyHost = Storage->ReadString(L"ProxyHost", ProxyHost);
  453. ProxyPort = Storage->ReadInteger(L"ProxyPort", ProxyPort);
  454. ProxyUsername = Storage->ReadString(L"ProxyUsername", ProxyUsername);
  455. if (Storage->ValueExists(L"ProxyPassword"))
  456. {
  457. // encrypt unencrypted password
  458. ProxyPassword = Storage->ReadString(L"ProxyPassword", L"");
  459. }
  460. else
  461. {
  462. // load encrypted password
  463. FProxyPassword = Storage->ReadStringAsBinaryData(L"ProxyPasswordEnc", FProxyPassword);
  464. }
  465. if (ProxyMethod == pmCmd)
  466. {
  467. ProxyLocalCommand = Storage->ReadStringRaw(L"ProxyTelnetCommand", ProxyLocalCommand);
  468. }
  469. else
  470. {
  471. ProxyTelnetCommand = Storage->ReadStringRaw(L"ProxyTelnetCommand", ProxyTelnetCommand);
  472. }
  473. ProxyDNS = TAutoSwitch((Storage->ReadInteger(L"ProxyDNS", (ProxyDNS + 2) % 3) + 1) % 3);
  474. ProxyLocalhost = Storage->ReadBool(L"ProxyLocalhost", ProxyLocalhost);
  475. #define READ_BUG(BUG) \
  476. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger(L"Bug"#BUG, \
  477. 2 - Bug[sb##BUG]));
  478. READ_BUG(Ignore1);
  479. READ_BUG(PlainPW1);
  480. READ_BUG(RSA1);
  481. READ_BUG(HMAC2);
  482. READ_BUG(DeriveKey2);
  483. READ_BUG(RSAPad2);
  484. READ_BUG(PKSessID2);
  485. READ_BUG(Rekey2);
  486. READ_BUG(MaxPkt2);
  487. READ_BUG(Ignore2);
  488. #undef READ_BUG
  489. if ((Bug[sbHMAC2] == asAuto) &&
  490. Storage->ReadBool(L"BuggyMAC", false))
  491. {
  492. Bug[sbHMAC2] = asOn;
  493. }
  494. SftpServer = Storage->ReadString(L"SftpServer", SftpServer);
  495. #define READ_SFTP_BUG(BUG) \
  496. SFTPBug[sb##BUG] = TAutoSwitch(Storage->ReadInteger(L"SFTP" #BUG "Bug", SFTPBug[sb##BUG]));
  497. READ_SFTP_BUG(Symlink);
  498. READ_SFTP_BUG(SignedTS);
  499. #undef READ_SFTP_BUG
  500. SFTPMaxVersion = Storage->ReadInteger(L"SFTPMaxVersion", SFTPMaxVersion);
  501. SFTPMaxPacketSize = Storage->ReadInteger(L"SFTPMaxPacketSize", SFTPMaxPacketSize);
  502. Color = Storage->ReadInteger(L"Color", Color);
  503. ProtocolStr = Storage->ReadString(L"Protocol", ProtocolStr);
  504. Tunnel = Storage->ReadBool(L"Tunnel", Tunnel);
  505. TunnelPortNumber = Storage->ReadInteger(L"TunnelPortNumber", TunnelPortNumber);
  506. TunnelUserName = Storage->ReadString(L"TunnelUserName", TunnelUserName);
  507. // must be loaded after TunnelUserName,
  508. // because TunnelHostName may be in format user@host
  509. TunnelHostName = Storage->ReadString(L"TunnelHostName", TunnelHostName);
  510. if (!Configuration->DisablePasswordStoring)
  511. {
  512. if (Storage->ValueExists(L"TunnelPasswordPlain"))
  513. {
  514. TunnelPassword = Storage->ReadString(L"TunnelPasswordPlain", TunnelPassword);
  515. RewritePassword = true;
  516. }
  517. else
  518. {
  519. FTunnelPassword = Storage->ReadStringAsBinaryData(L"TunnelPassword", FTunnelPassword);
  520. }
  521. }
  522. TunnelPublicKeyFile = Storage->ReadString(L"TunnelPublicKeyFile", TunnelPublicKeyFile);
  523. TunnelLocalPortNumber = Storage->ReadInteger(L"TunnelLocalPortNumber", TunnelLocalPortNumber);
  524. TunnelHostKey = Storage->ReadString(L"TunnelHostKey", TunnelHostKey);
  525. // Ftp prefix
  526. FtpPasvMode = Storage->ReadBool(L"FtpPasvMode", FtpPasvMode);
  527. FtpForcePasvIp = TAutoSwitch(Storage->ReadInteger(L"FtpForcePasvIp2", FtpForcePasvIp));
  528. FtpAccount = Storage->ReadString(L"FtpAccount", FtpAccount);
  529. FtpPingInterval = Storage->ReadInteger(L"FtpPingInterval", FtpPingInterval);
  530. FtpPingType = static_cast<TPingType>(Storage->ReadInteger(L"FtpPingType", FtpPingType));
  531. Ftps = static_cast<TFtps>(Storage->ReadInteger(L"Ftps", Ftps));
  532. FtpListAll = TAutoSwitch(Storage->ReadInteger(L"FtpListAll", FtpListAll));
  533. SslSessionReuse = Storage->ReadBool(L"SslSessionReuse", SslSessionReuse);
  534. FtpProxyLogonType = Storage->ReadInteger(L"FtpProxyLogonType", FtpProxyLogonType);
  535. FIsWorkspace = Storage->ReadBool(L"IsWorkspace", IsWorkspace);
  536. CustomParam1 = Storage->ReadString(L"CustomParam1", CustomParam1);
  537. CustomParam2 = Storage->ReadString(L"CustomParam2", CustomParam2);
  538. }
  539. //---------------------------------------------------------------------
  540. void __fastcall TSessionData::Load(THierarchicalStorage * Storage)
  541. {
  542. bool RewritePassword = false;
  543. if (Storage->OpenSubKey(InternalStorageKey, False))
  544. {
  545. DoLoad(Storage, RewritePassword);
  546. Storage->CloseSubKey();
  547. }
  548. if (RewritePassword)
  549. {
  550. TStorageAccessMode AccessMode = Storage->AccessMode;
  551. Storage->AccessMode = smReadWrite;
  552. try
  553. {
  554. if (Storage->OpenSubKey(InternalStorageKey, true))
  555. {
  556. Storage->DeleteValue(L"PasswordPlain");
  557. if (!Password.IsEmpty())
  558. {
  559. Storage->WriteBinaryDataAsString(L"Password", FPassword);
  560. }
  561. Storage->DeleteValue(L"TunnelPasswordPlain");
  562. if (!TunnelPassword.IsEmpty())
  563. {
  564. Storage->WriteBinaryDataAsString(L"TunnelPassword", FTunnelPassword);
  565. }
  566. Storage->CloseSubKey();
  567. }
  568. }
  569. catch(...)
  570. {
  571. // ignore errors (like read-only INI file)
  572. }
  573. Storage->AccessMode = AccessMode;
  574. }
  575. FModified = false;
  576. FSource = ssStored;
  577. }
  578. //---------------------------------------------------------------------
  579. void __fastcall TSessionData::Save(THierarchicalStorage * Storage,
  580. bool PuttyExport, const TSessionData * Default)
  581. {
  582. if (Storage->OpenSubKey(InternalStorageKey, true))
  583. {
  584. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  585. if ((Default != NULL) && (CONV(Default->PROPERTY) == CONV(PROPERTY))) \
  586. { \
  587. Storage->DeleteValue(NAME); \
  588. } \
  589. else \
  590. { \
  591. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  592. }
  593. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  594. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, TEXT(#PROPERTY), PROPERTY, )
  595. WRITE_DATA(String, HostName);
  596. WRITE_DATA(Integer, PortNumber);
  597. WRITE_DATA_EX(Integer, L"PingInterval", PingInterval / SecsPerMin, );
  598. WRITE_DATA_EX(Integer, L"PingIntervalSecs", PingInterval % SecsPerMin, );
  599. Storage->DeleteValue(L"PingIntervalSec"); // obsolete
  600. // when PingInterval is stored always store PingType not to attempt to
  601. // deduce PingType from PingInterval (backward compatibility with pre 3.5)
  602. if (((Default != NULL) && (PingType != Default->PingType)) ||
  603. Storage->ValueExists(L"PingInterval"))
  604. {
  605. Storage->WriteInteger(L"PingType", PingType);
  606. }
  607. else
  608. {
  609. Storage->DeleteValue(L"PingType");
  610. }
  611. WRITE_DATA(Integer, Timeout);
  612. WRITE_DATA(Bool, TryAgent);
  613. WRITE_DATA(Bool, AgentFwd);
  614. WRITE_DATA(Bool, AuthTIS);
  615. WRITE_DATA(Bool, AuthKI);
  616. WRITE_DATA(Bool, AuthKIPassword);
  617. WRITE_DATA(Bool, AuthGSSAPI);
  618. WRITE_DATA(Bool, GSSAPIFwdTGT);
  619. WRITE_DATA(String, GSSAPIServerRealm);
  620. Storage->DeleteValue(L"TryGSSKEX");
  621. Storage->DeleteValue(L"UserNameFromEnvironment");
  622. Storage->DeleteValue("GSSAPIServerChoosesUserName");
  623. Storage->DeleteValue(L"GSSAPITrustDNS");
  624. if (PuttyExport)
  625. {
  626. // duplicate kerberos setting with keys of the vintela quest putty
  627. WRITE_DATA_EX(Bool, L"AuthSSPI", AuthGSSAPI, );
  628. WRITE_DATA_EX(Bool, L"SSPIFwdTGT", GSSAPIFwdTGT, );
  629. WRITE_DATA_EX(String, L"KerbPrincipal", GSSAPIServerRealm, );
  630. // duplicate kerberos setting with keys of the official putty
  631. WRITE_DATA_EX(Bool, L"GssapiFwd", GSSAPIFwdTGT, );
  632. }
  633. WRITE_DATA(Bool, ChangeUsername);
  634. WRITE_DATA(Bool, Compression);
  635. WRITE_DATA(Integer, SshProt);
  636. WRITE_DATA(Bool, Ssh2DES);
  637. WRITE_DATA(Bool, SshNoUserAuth);
  638. WRITE_DATA_EX(String, L"Cipher", CipherList, );
  639. WRITE_DATA_EX(String, L"KEX", KexList, );
  640. WRITE_DATA(Integer, AddressFamily);
  641. WRITE_DATA_EX(String, L"RekeyBytes", RekeyData, );
  642. WRITE_DATA(Integer, RekeyTime);
  643. WRITE_DATA(Bool, TcpNoDelay);
  644. if (PuttyExport)
  645. {
  646. WRITE_DATA(StringRaw, UserName);
  647. WRITE_DATA(StringRaw, PublicKeyFile);
  648. }
  649. else
  650. {
  651. WRITE_DATA(String, UserName);
  652. WRITE_DATA(String, PublicKeyFile);
  653. WRITE_DATA(Integer, FSProtocol);
  654. WRITE_DATA(String, LocalDirectory);
  655. WRITE_DATA(String, RemoteDirectory);
  656. WRITE_DATA(Bool, SynchronizeBrowsing);
  657. WRITE_DATA(Bool, UpdateDirectories);
  658. WRITE_DATA(Bool, CacheDirectories);
  659. WRITE_DATA(Bool, CacheDirectoryChanges);
  660. WRITE_DATA(Bool, PreserveDirectoryChanges);
  661. WRITE_DATA(Bool, ResolveSymlinks);
  662. WRITE_DATA_EX(Integer, L"ConsiderDST", DSTMode, );
  663. WRITE_DATA(Bool, LockInHome);
  664. // Special is never stored (if it would, login dialog must be modified not to
  665. // duplicate Special parameter when Special session is loaded and then stored
  666. // under different name)
  667. // WRITE_DATA(Bool, Special);
  668. WRITE_DATA(String, Shell);
  669. WRITE_DATA(Bool, ClearAliases);
  670. WRITE_DATA(Bool, UnsetNationalVars);
  671. WRITE_DATA(String, ListingCommand);
  672. WRITE_DATA(Bool, IgnoreLsWarnings);
  673. WRITE_DATA(Integer, SCPLsFullTime);
  674. WRITE_DATA(Bool, Scp1Compatibility);
  675. WRITE_DATA(Float, TimeDifference);
  676. WRITE_DATA(Bool, DeleteToRecycleBin);
  677. WRITE_DATA(Bool, OverwrittenToRecycleBin);
  678. WRITE_DATA(String, RecycleBinPath);
  679. WRITE_DATA(String, PostLoginCommands);
  680. WRITE_DATA(String, ReturnVar);
  681. WRITE_DATA_EX(Integer, L"LookupUserGroups2", LookupUserGroups, );
  682. WRITE_DATA(Integer, EOLType);
  683. Storage->DeleteValue(L"SFTPUtfBug");
  684. WRITE_DATA_EX(Integer, L"Utf", NotUtf, );
  685. WRITE_DATA(Integer, SendBuf);
  686. WRITE_DATA(Bool, SshSimple);
  687. }
  688. WRITE_DATA(Integer, ProxyMethod);
  689. if (PuttyExport)
  690. {
  691. // support for Putty 0.53b and older
  692. int ProxyType;
  693. int ProxySOCKSVersion = 5;
  694. switch (ProxyMethod) {
  695. case pmHTTP:
  696. ProxyType = pxHTTP;
  697. break;
  698. case pmTelnet:
  699. ProxyType = pxTelnet;
  700. break;
  701. case pmSocks5:
  702. ProxyType = pxSocks;
  703. ProxySOCKSVersion = 5;
  704. break;
  705. case pmSocks4:
  706. ProxyType = pxSocks;
  707. ProxySOCKSVersion = 4;
  708. break;
  709. default:
  710. case ::pmNone:
  711. ProxyType = pxNone;
  712. break;
  713. }
  714. Storage->WriteInteger(L"ProxyType", ProxyType);
  715. Storage->WriteInteger(L"ProxySOCKSVersion", ProxySOCKSVersion);
  716. }
  717. else
  718. {
  719. Storage->DeleteValue(L"ProxyType");
  720. Storage->DeleteValue(L"ProxySOCKSVersion");
  721. }
  722. WRITE_DATA(String, ProxyHost);
  723. WRITE_DATA(Integer, ProxyPort);
  724. WRITE_DATA(String, ProxyUsername);
  725. if (ProxyMethod == pmCmd)
  726. {
  727. WRITE_DATA_EX(StringRaw, L"ProxyTelnetCommand", ProxyLocalCommand, );
  728. }
  729. else
  730. {
  731. WRITE_DATA(StringRaw, ProxyTelnetCommand);
  732. }
  733. #define WRITE_DATA_CONV_FUNC(X) (((X) + 2) % 3)
  734. WRITE_DATA_CONV(Integer, L"ProxyDNS", ProxyDNS);
  735. #undef WRITE_DATA_CONV_FUNC
  736. WRITE_DATA(Bool, ProxyLocalhost);
  737. #define WRITE_DATA_CONV_FUNC(X) (2 - (X))
  738. #define WRITE_BUG(BUG) WRITE_DATA_CONV(Integer, L"Bug" #BUG, Bug[sb##BUG]);
  739. WRITE_BUG(Ignore1);
  740. WRITE_BUG(PlainPW1);
  741. WRITE_BUG(RSA1);
  742. WRITE_BUG(HMAC2);
  743. WRITE_BUG(DeriveKey2);
  744. WRITE_BUG(RSAPad2);
  745. WRITE_BUG(PKSessID2);
  746. WRITE_BUG(Rekey2);
  747. WRITE_BUG(MaxPkt2);
  748. WRITE_BUG(Ignore2);
  749. #undef WRITE_BUG
  750. #undef WRITE_DATA_CONV_FUNC
  751. Storage->DeleteValue(L"BuggyMAC");
  752. Storage->DeleteValue(L"AliasGroupList");
  753. if (PuttyExport)
  754. {
  755. WRITE_DATA_EX(String, L"Protocol", ProtocolStr, );
  756. }
  757. if (!PuttyExport)
  758. {
  759. WRITE_DATA(String, SftpServer);
  760. #define WRITE_SFTP_BUG(BUG) WRITE_DATA_EX(Integer, L"SFTP" #BUG "Bug", SFTPBug[sb##BUG], );
  761. WRITE_SFTP_BUG(Symlink);
  762. WRITE_SFTP_BUG(SignedTS);
  763. #undef WRITE_SFTP_BUG
  764. WRITE_DATA(Integer, SFTPMaxVersion);
  765. WRITE_DATA(Integer, SFTPMaxPacketSize);
  766. WRITE_DATA(Integer, Color);
  767. WRITE_DATA(Bool, Tunnel);
  768. WRITE_DATA(String, TunnelHostName);
  769. WRITE_DATA(Integer, TunnelPortNumber);
  770. WRITE_DATA(String, TunnelUserName);
  771. WRITE_DATA(String, TunnelPublicKeyFile);
  772. WRITE_DATA(Integer, TunnelLocalPortNumber);
  773. WRITE_DATA(Bool, FtpPasvMode);
  774. WRITE_DATA_EX(Integer, L"FtpForcePasvIp2", FtpForcePasvIp, );
  775. WRITE_DATA(String, FtpAccount);
  776. WRITE_DATA(Integer, FtpPingInterval);
  777. WRITE_DATA(Integer, FtpPingType);
  778. WRITE_DATA(Integer, Ftps);
  779. WRITE_DATA(Integer, FtpListAll);
  780. WRITE_DATA(Bool, SslSessionReuse);
  781. WRITE_DATA(Integer, FtpProxyLogonType);
  782. WRITE_DATA(String, CustomParam1);
  783. WRITE_DATA(String, CustomParam2);
  784. }
  785. SavePasswords(Storage, PuttyExport);
  786. Storage->CloseSubKey();
  787. }
  788. }
  789. //---------------------------------------------------------------------
  790. void __fastcall TSessionData::SavePasswords(THierarchicalStorage * Storage, bool PuttyExport)
  791. {
  792. if (!Configuration->DisablePasswordStoring && !PuttyExport && !FPassword.IsEmpty())
  793. {
  794. Storage->WriteBinaryDataAsString(L"Password", StronglyRecryptPassword(FPassword, UserName+HostName));
  795. }
  796. else
  797. {
  798. Storage->DeleteValue(L"Password");
  799. }
  800. Storage->DeleteValue(L"PasswordPlain");
  801. if (PuttyExport)
  802. {
  803. // save password unencrypted
  804. Storage->WriteString(L"ProxyPassword", ProxyPassword);
  805. }
  806. else
  807. {
  808. // save password encrypted
  809. if (!FProxyPassword.IsEmpty())
  810. {
  811. Storage->WriteBinaryDataAsString(L"ProxyPasswordEnc", StronglyRecryptPassword(FProxyPassword, ProxyUsername+ProxyHost));
  812. }
  813. else
  814. {
  815. Storage->DeleteValue(L"ProxyPasswordEnc");
  816. }
  817. Storage->DeleteValue(L"ProxyPassword");
  818. if (!Configuration->DisablePasswordStoring && !FTunnelPassword.IsEmpty())
  819. {
  820. Storage->WriteBinaryDataAsString(L"TunnelPassword", StronglyRecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName));
  821. }
  822. else
  823. {
  824. Storage->DeleteValue(L"TunnelPassword");
  825. }
  826. }
  827. }
  828. //---------------------------------------------------------------------
  829. void __fastcall TSessionData::RecryptPasswords()
  830. {
  831. Password = Password;
  832. ProxyPassword = ProxyPassword;
  833. TunnelPassword = TunnelPassword;
  834. }
  835. //---------------------------------------------------------------------
  836. bool __fastcall TSessionData::HasAnyPassword()
  837. {
  838. return !FPassword.IsEmpty() || !FProxyPassword.IsEmpty() || !FTunnelPassword.IsEmpty();
  839. }
  840. //---------------------------------------------------------------------
  841. void __fastcall TSessionData::Modify()
  842. {
  843. FModified = true;
  844. if (FSource == ssStored)
  845. {
  846. FSource = ssStoredModified;
  847. }
  848. }
  849. //---------------------------------------------------------------------
  850. UnicodeString __fastcall TSessionData::GetSource()
  851. {
  852. switch (FSource)
  853. {
  854. case ::ssNone:
  855. return L"Ad-Hoc session";
  856. case ssStored:
  857. return L"Stored session";
  858. case ssStoredModified:
  859. return L"Modified stored session";
  860. default:
  861. assert(false);
  862. return L"";
  863. }
  864. }
  865. //---------------------------------------------------------------------
  866. void __fastcall TSessionData::SaveRecryptedPasswords(THierarchicalStorage * Storage)
  867. {
  868. if (Storage->OpenSubKey(InternalStorageKey, true))
  869. {
  870. RecryptPasswords();
  871. SavePasswords(Storage, false);
  872. Storage->CloseSubKey();
  873. }
  874. }
  875. //---------------------------------------------------------------------
  876. void __fastcall TSessionData::Remove()
  877. {
  878. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  879. try
  880. {
  881. Storage->Explicit = true;
  882. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  883. {
  884. Storage->RecursiveDeleteSubKey(InternalStorageKey);
  885. }
  886. }
  887. __finally
  888. {
  889. delete Storage;
  890. }
  891. }
  892. //---------------------------------------------------------------------
  893. bool __fastcall TSessionData::ParseUrl(UnicodeString Url, TOptions * Options,
  894. TStoredSessionList * StoredSessions, bool & DefaultsOnly, UnicodeString * FileName,
  895. bool * AProtocolDefined)
  896. {
  897. bool ProtocolDefined = false;
  898. bool PortNumberDefined = false;
  899. TFSProtocol AFSProtocol;
  900. int APortNumber;
  901. TFtps AFtps = ftpsNone;
  902. if (Url.SubString(1, 4).LowerCase() == L"scp:")
  903. {
  904. AFSProtocol = fsSCPonly;
  905. APortNumber = SshPortNumber;
  906. Url.Delete(1, 4);
  907. ProtocolDefined = true;
  908. }
  909. else if (Url.SubString(1, 5).LowerCase() == L"sftp:")
  910. {
  911. AFSProtocol = fsSFTPonly;
  912. APortNumber = SshPortNumber;
  913. Url.Delete(1, 5);
  914. ProtocolDefined = true;
  915. }
  916. else if (Url.SubString(1, 4).LowerCase() == L"ftp:")
  917. {
  918. AFSProtocol = fsFTP;
  919. Ftps = ftpsNone;
  920. APortNumber = FtpPortNumber;
  921. Url.Delete(1, 4);
  922. ProtocolDefined = true;
  923. }
  924. else if (Url.SubString(1, 5).LowerCase() == L"ftps:")
  925. {
  926. AFSProtocol = fsFTP;
  927. AFtps = ftpsImplicit;
  928. APortNumber = FtpsImplicitPortNumber;
  929. Url.Delete(1, 5);
  930. ProtocolDefined = true;
  931. }
  932. if (ProtocolDefined && (Url.SubString(1, 2) == L"//"))
  933. {
  934. Url.Delete(1, 2);
  935. }
  936. if (AProtocolDefined != NULL)
  937. {
  938. *AProtocolDefined = ProtocolDefined;
  939. }
  940. if (!Url.IsEmpty())
  941. {
  942. UnicodeString DecodedUrl = DecodeUrlChars(Url);
  943. // lookup stored session even if protocol was defined
  944. // (this allows setting for example default username for host
  945. // by creating stored session named by host)
  946. TSessionData * Data = NULL;
  947. for (Integer Index = 0; Index < StoredSessions->Count + StoredSessions->HiddenCount; Index++)
  948. {
  949. TSessionData * AData = (TSessionData *)StoredSessions->Items[Index];
  950. if (AnsiSameText(AData->Name, DecodedUrl) ||
  951. AnsiSameText(AData->Name + L"/", DecodedUrl.SubString(1, AData->Name.Length() + 1)))
  952. {
  953. Data = AData;
  954. break;
  955. }
  956. }
  957. UnicodeString ARemoteDirectory;
  958. if (Data != NULL)
  959. {
  960. DefaultsOnly = false;
  961. Assign(Data);
  962. int P = 1;
  963. while (!AnsiSameText(DecodeUrlChars(Url.SubString(1, P)), Data->Name))
  964. {
  965. P++;
  966. assert(P <= Url.Length());
  967. }
  968. ARemoteDirectory = Url.SubString(P + 1, Url.Length() - P);
  969. if (Data->Hidden)
  970. {
  971. Data->Remove();
  972. StoredSessions->Remove(Data);
  973. // only modified, implicit
  974. StoredSessions->Save(false, false);
  975. }
  976. }
  977. else
  978. {
  979. Assign(StoredSessions->DefaultSettings);
  980. Name = L"";
  981. int PSlash = Url.Pos(L"/");
  982. if (PSlash == 0)
  983. {
  984. PSlash = Url.Length() + 1;
  985. }
  986. UnicodeString ConnectInfo = Url.SubString(1, PSlash - 1);
  987. int P = ConnectInfo.LastDelimiter(L"@");
  988. UnicodeString UserInfo;
  989. UnicodeString HostInfo;
  990. if (P > 0)
  991. {
  992. UserInfo = ConnectInfo.SubString(1, P - 1);
  993. HostInfo = ConnectInfo.SubString(P + 1, ConnectInfo.Length() - P);
  994. }
  995. else
  996. {
  997. HostInfo = ConnectInfo;
  998. }
  999. if ((HostInfo.Length() >= 2) && (HostInfo[1] == L'[') && ((P = HostInfo.Pos(L"]")) > 0))
  1000. {
  1001. HostName = HostInfo.SubString(2, P - 2);
  1002. HostInfo.Delete(1, P);
  1003. if (!HostInfo.IsEmpty() && (HostInfo[1] == L':'))
  1004. {
  1005. HostInfo.Delete(1, 1);
  1006. }
  1007. }
  1008. else
  1009. {
  1010. HostName = DecodeUrlChars(CutToChar(HostInfo, L':', true));
  1011. }
  1012. // expanded from ?: operator, as it caused strange "access violation" errors
  1013. if (!HostInfo.IsEmpty())
  1014. {
  1015. PortNumber = StrToIntDef(DecodeUrlChars(HostInfo), -1);
  1016. PortNumberDefined = true;
  1017. }
  1018. else if (ProtocolDefined)
  1019. {
  1020. PortNumber = APortNumber;
  1021. }
  1022. if (ProtocolDefined)
  1023. {
  1024. Ftps = AFtps;
  1025. }
  1026. UserName = DecodeUrlChars(CutToChar(UserInfo, L':', false));
  1027. Password = DecodeUrlChars(UserInfo);
  1028. ARemoteDirectory = Url.SubString(PSlash, Url.Length() - PSlash + 1);
  1029. }
  1030. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory != L"/"))
  1031. {
  1032. if ((ARemoteDirectory[ARemoteDirectory.Length()] != L'/') &&
  1033. (FileName != NULL))
  1034. {
  1035. *FileName = DecodeUrlChars(UnixExtractFileName(ARemoteDirectory));
  1036. ARemoteDirectory = UnixExtractFilePath(ARemoteDirectory);
  1037. }
  1038. RemoteDirectory = DecodeUrlChars(ARemoteDirectory);
  1039. }
  1040. DefaultsOnly = false;
  1041. }
  1042. else
  1043. {
  1044. Assign(StoredSessions->DefaultSettings);
  1045. DefaultsOnly = true;
  1046. }
  1047. if (ProtocolDefined)
  1048. {
  1049. FSProtocol = AFSProtocol;
  1050. }
  1051. if (Options != NULL)
  1052. {
  1053. // we deliberately do keep defaultonly to false, in presence of any option,
  1054. // as the option should not make session "connectable"
  1055. UnicodeString Value;
  1056. if (Options->FindSwitch(L"privatekey", Value))
  1057. {
  1058. PublicKeyFile = Value;
  1059. }
  1060. if (Options->FindSwitch(L"timeout", Value))
  1061. {
  1062. Timeout = StrToInt(Value);
  1063. }
  1064. if (Options->FindSwitch(L"hostkey", Value) ||
  1065. Options->FindSwitch(L"certificate", Value))
  1066. {
  1067. HostKey = Value;
  1068. }
  1069. FtpPasvMode = Options->SwitchValue(L"passive", FtpPasvMode);
  1070. if (Options->FindSwitch(L"implicit"))
  1071. {
  1072. bool Enabled = Options->SwitchValue(L"implicit", true);
  1073. Ftps = Enabled ? ftpsImplicit : ftpsNone;
  1074. if (!PortNumberDefined && Enabled)
  1075. {
  1076. PortNumber = FtpsImplicitPortNumber;
  1077. }
  1078. }
  1079. if (Options->FindSwitch(L"explicitssl"))
  1080. {
  1081. bool Enabled = Options->SwitchValue(L"explicitssl", true);
  1082. Ftps = Enabled ? ftpsExplicitSsl : ftpsNone;
  1083. if (!PortNumberDefined && Enabled)
  1084. {
  1085. PortNumber = FtpPortNumber;
  1086. }
  1087. }
  1088. if (Options->FindSwitch(L"explicittls", Value))
  1089. {
  1090. bool Enabled = Options->SwitchValue(L"explicittls", true);
  1091. Ftps = Enabled ? ftpsExplicitTls : ftpsNone;
  1092. if (!PortNumberDefined && Enabled)
  1093. {
  1094. PortNumber = FtpPortNumber;
  1095. }
  1096. }
  1097. if (Options->FindSwitch(L"rawsettings"))
  1098. {
  1099. TStrings * RawSettings = NULL;
  1100. TOptionsStorage * OptionsStorage = NULL;
  1101. try
  1102. {
  1103. RawSettings = new TStringList();
  1104. if (Options->FindSwitch(L"rawsettings", RawSettings))
  1105. {
  1106. OptionsStorage = new TOptionsStorage(RawSettings);
  1107. bool Dummy;
  1108. DoLoad(OptionsStorage, Dummy);
  1109. }
  1110. }
  1111. __finally
  1112. {
  1113. delete RawSettings;
  1114. delete OptionsStorage;
  1115. }
  1116. }
  1117. }
  1118. return true;
  1119. }
  1120. //---------------------------------------------------------------------
  1121. void __fastcall TSessionData::ConfigureTunnel(int APortNumber)
  1122. {
  1123. FOrigHostName = HostName;
  1124. FOrigPortNumber = PortNumber;
  1125. FOrigProxyMethod = ProxyMethod;
  1126. HostName = L"127.0.0.1";
  1127. PortNumber = APortNumber;
  1128. // proxy settings is used for tunnel
  1129. ProxyMethod = ::pmNone;
  1130. }
  1131. //---------------------------------------------------------------------
  1132. void __fastcall TSessionData::RollbackTunnel()
  1133. {
  1134. HostName = FOrigHostName;
  1135. PortNumber = FOrigPortNumber;
  1136. ProxyMethod = FOrigProxyMethod;
  1137. }
  1138. //---------------------------------------------------------------------
  1139. void __fastcall TSessionData::ExpandEnvironmentVariables()
  1140. {
  1141. HostName = HostNameExpanded;
  1142. UserName = UserNameExpanded;
  1143. PublicKeyFile = ::ExpandEnvironmentVariables(PublicKeyFile);
  1144. }
  1145. //---------------------------------------------------------------------
  1146. void __fastcall TSessionData::ValidatePath(const UnicodeString Path)
  1147. {
  1148. // noop
  1149. }
  1150. //---------------------------------------------------------------------
  1151. void __fastcall TSessionData::ValidateName(const UnicodeString Name)
  1152. {
  1153. if (Name.LastDelimiter(L"/") > 0)
  1154. {
  1155. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, L"/")));
  1156. }
  1157. }
  1158. //---------------------------------------------------------------------
  1159. RawByteString __fastcall TSessionData::EncryptPassword(const UnicodeString & Password, UnicodeString Key)
  1160. {
  1161. return Configuration->EncryptPassword(Password, Key);
  1162. }
  1163. //---------------------------------------------------------------------
  1164. RawByteString __fastcall TSessionData::StronglyRecryptPassword(const RawByteString & Password, UnicodeString Key)
  1165. {
  1166. return Configuration->StronglyRecryptPassword(Password, Key);
  1167. }
  1168. //---------------------------------------------------------------------
  1169. UnicodeString __fastcall TSessionData::DecryptPassword(const RawByteString & Password, UnicodeString Key)
  1170. {
  1171. UnicodeString Result;
  1172. try
  1173. {
  1174. Result = Configuration->DecryptPassword(Password, Key);
  1175. }
  1176. catch(EAbort &)
  1177. {
  1178. // silently ignore aborted prompts for master password and return empty password
  1179. }
  1180. return Result;
  1181. }
  1182. //---------------------------------------------------------------------
  1183. bool __fastcall TSessionData::GetCanLogin()
  1184. {
  1185. return !FHostName.IsEmpty();
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. UnicodeString __fastcall TSessionData::GetSessionKey()
  1189. {
  1190. return FORMAT(L"%s@%s", (UserName, HostName));
  1191. }
  1192. //---------------------------------------------------------------------
  1193. UnicodeString __fastcall TSessionData::GetInternalStorageKey()
  1194. {
  1195. if (Name.IsEmpty())
  1196. {
  1197. return SessionKey;
  1198. }
  1199. else
  1200. {
  1201. return Name;
  1202. }
  1203. }
  1204. //---------------------------------------------------------------------
  1205. UnicodeString __fastcall TSessionData::GetStorageKey()
  1206. {
  1207. return SessionName;
  1208. }
  1209. //---------------------------------------------------------------------
  1210. void __fastcall TSessionData::SetHostName(UnicodeString value)
  1211. {
  1212. if (FHostName != value)
  1213. {
  1214. // HostName is key for password encryption
  1215. UnicodeString XPassword = Password;
  1216. int P = value.LastDelimiter(L"@");
  1217. if (P > 0)
  1218. {
  1219. UserName = value.SubString(1, P - 1);
  1220. value = value.SubString(P + 1, value.Length() - P);
  1221. }
  1222. FHostName = value;
  1223. Modify();
  1224. Password = XPassword;
  1225. Shred(XPassword);
  1226. }
  1227. }
  1228. //---------------------------------------------------------------------
  1229. UnicodeString __fastcall TSessionData::GetHostNameExpanded()
  1230. {
  1231. return ::ExpandEnvironmentVariables(HostName);
  1232. }
  1233. //---------------------------------------------------------------------
  1234. void __fastcall TSessionData::SetPortNumber(int value)
  1235. {
  1236. SET_SESSION_PROPERTY(PortNumber);
  1237. }
  1238. //---------------------------------------------------------------------------
  1239. void __fastcall TSessionData::SetShell(UnicodeString value)
  1240. {
  1241. SET_SESSION_PROPERTY(Shell);
  1242. }
  1243. //---------------------------------------------------------------------------
  1244. void __fastcall TSessionData::SetSftpServer(UnicodeString value)
  1245. {
  1246. SET_SESSION_PROPERTY(SftpServer);
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall TSessionData::SetClearAliases(bool value)
  1250. {
  1251. SET_SESSION_PROPERTY(ClearAliases);
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. void __fastcall TSessionData::SetListingCommand(UnicodeString value)
  1255. {
  1256. SET_SESSION_PROPERTY(ListingCommand);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  1260. {
  1261. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  1265. {
  1266. SET_SESSION_PROPERTY(UnsetNationalVars);
  1267. }
  1268. //---------------------------------------------------------------------
  1269. void __fastcall TSessionData::SetUserName(UnicodeString value)
  1270. {
  1271. // UserName is key for password encryption
  1272. UnicodeString XPassword = Password;
  1273. SET_SESSION_PROPERTY(UserName);
  1274. Password = XPassword;
  1275. Shred(XPassword);
  1276. }
  1277. //---------------------------------------------------------------------
  1278. UnicodeString __fastcall TSessionData::GetUserNameExpanded()
  1279. {
  1280. return ::ExpandEnvironmentVariables(UserName);
  1281. }
  1282. //---------------------------------------------------------------------
  1283. void __fastcall TSessionData::SetPassword(UnicodeString avalue)
  1284. {
  1285. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  1286. SET_SESSION_PROPERTY(Password);
  1287. }
  1288. //---------------------------------------------------------------------
  1289. UnicodeString __fastcall TSessionData::GetPassword() const
  1290. {
  1291. return DecryptPassword(FPassword, UserName+HostName);
  1292. }
  1293. //---------------------------------------------------------------------
  1294. void __fastcall TSessionData::SetPingInterval(int value)
  1295. {
  1296. SET_SESSION_PROPERTY(PingInterval);
  1297. }
  1298. //---------------------------------------------------------------------
  1299. void __fastcall TSessionData::SetTryAgent(bool value)
  1300. {
  1301. SET_SESSION_PROPERTY(TryAgent);
  1302. }
  1303. //---------------------------------------------------------------------
  1304. void __fastcall TSessionData::SetAgentFwd(bool value)
  1305. {
  1306. SET_SESSION_PROPERTY(AgentFwd);
  1307. }
  1308. //---------------------------------------------------------------------
  1309. void __fastcall TSessionData::SetAuthTIS(bool value)
  1310. {
  1311. SET_SESSION_PROPERTY(AuthTIS);
  1312. }
  1313. //---------------------------------------------------------------------
  1314. void __fastcall TSessionData::SetAuthKI(bool value)
  1315. {
  1316. SET_SESSION_PROPERTY(AuthKI);
  1317. }
  1318. //---------------------------------------------------------------------
  1319. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  1320. {
  1321. SET_SESSION_PROPERTY(AuthKIPassword);
  1322. }
  1323. //---------------------------------------------------------------------
  1324. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  1325. {
  1326. SET_SESSION_PROPERTY(AuthGSSAPI);
  1327. }
  1328. //---------------------------------------------------------------------
  1329. void __fastcall TSessionData::SetGSSAPIFwdTGT(bool value)
  1330. {
  1331. SET_SESSION_PROPERTY(GSSAPIFwdTGT);
  1332. }
  1333. //---------------------------------------------------------------------
  1334. void __fastcall TSessionData::SetGSSAPIServerRealm(UnicodeString value)
  1335. {
  1336. SET_SESSION_PROPERTY(GSSAPIServerRealm);
  1337. }
  1338. //---------------------------------------------------------------------
  1339. void __fastcall TSessionData::SetChangeUsername(bool value)
  1340. {
  1341. SET_SESSION_PROPERTY(ChangeUsername);
  1342. }
  1343. //---------------------------------------------------------------------
  1344. void __fastcall TSessionData::SetCompression(bool value)
  1345. {
  1346. SET_SESSION_PROPERTY(Compression);
  1347. }
  1348. //---------------------------------------------------------------------
  1349. void __fastcall TSessionData::SetSshProt(TSshProt value)
  1350. {
  1351. SET_SESSION_PROPERTY(SshProt);
  1352. }
  1353. //---------------------------------------------------------------------
  1354. void __fastcall TSessionData::SetSsh2DES(bool value)
  1355. {
  1356. SET_SESSION_PROPERTY(Ssh2DES);
  1357. }
  1358. //---------------------------------------------------------------------
  1359. void __fastcall TSessionData::SetSshNoUserAuth(bool value)
  1360. {
  1361. SET_SESSION_PROPERTY(SshNoUserAuth);
  1362. }
  1363. //---------------------------------------------------------------------
  1364. UnicodeString __fastcall TSessionData::GetSshProtStr()
  1365. {
  1366. return SshProtList[FSshProt];
  1367. }
  1368. //---------------------------------------------------------------------
  1369. bool __fastcall TSessionData::GetUsesSsh()
  1370. {
  1371. return (FSProtocol != fsFTP);
  1372. }
  1373. //---------------------------------------------------------------------
  1374. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  1375. {
  1376. assert(Index >= 0 && Index < CIPHER_COUNT);
  1377. SET_SESSION_PROPERTY(Ciphers[Index]);
  1378. }
  1379. //---------------------------------------------------------------------
  1380. TCipher __fastcall TSessionData::GetCipher(int Index) const
  1381. {
  1382. assert(Index >= 0 && Index < CIPHER_COUNT);
  1383. return FCiphers[Index];
  1384. }
  1385. //---------------------------------------------------------------------
  1386. void __fastcall TSessionData::SetCipherList(UnicodeString value)
  1387. {
  1388. bool Used[CIPHER_COUNT];
  1389. for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
  1390. UnicodeString CipherStr;
  1391. int Index = 0;
  1392. while (!value.IsEmpty() && (Index < CIPHER_COUNT))
  1393. {
  1394. CipherStr = CutToChar(value, L',', true);
  1395. for (int C = 0; C < CIPHER_COUNT; C++)
  1396. {
  1397. if (!CipherStr.CompareIC(CipherNames[C]))
  1398. {
  1399. Cipher[Index] = (TCipher)C;
  1400. Used[C] = true;
  1401. Index++;
  1402. break;
  1403. }
  1404. }
  1405. }
  1406. for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
  1407. {
  1408. if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
  1409. }
  1410. }
  1411. //---------------------------------------------------------------------
  1412. UnicodeString __fastcall TSessionData::GetCipherList() const
  1413. {
  1414. UnicodeString Result;
  1415. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  1416. {
  1417. Result += UnicodeString(Index ? L"," : L"") + CipherNames[Cipher[Index]];
  1418. }
  1419. return Result;
  1420. }
  1421. //---------------------------------------------------------------------
  1422. void __fastcall TSessionData::SetKex(int Index, TKex value)
  1423. {
  1424. assert(Index >= 0 && Index < KEX_COUNT);
  1425. SET_SESSION_PROPERTY(Kex[Index]);
  1426. }
  1427. //---------------------------------------------------------------------
  1428. TKex __fastcall TSessionData::GetKex(int Index) const
  1429. {
  1430. assert(Index >= 0 && Index < KEX_COUNT);
  1431. return FKex[Index];
  1432. }
  1433. //---------------------------------------------------------------------
  1434. void __fastcall TSessionData::SetKexList(UnicodeString value)
  1435. {
  1436. bool Used[KEX_COUNT];
  1437. for (int K = 0; K < KEX_COUNT; K++) Used[K] = false;
  1438. UnicodeString KexStr;
  1439. int Index = 0;
  1440. while (!value.IsEmpty() && (Index < KEX_COUNT))
  1441. {
  1442. KexStr = CutToChar(value, L',', true);
  1443. for (int K = 0; K < KEX_COUNT; K++)
  1444. {
  1445. if (!KexStr.CompareIC(KexNames[K]))
  1446. {
  1447. Kex[Index] = (TKex)K;
  1448. Used[K] = true;
  1449. Index++;
  1450. break;
  1451. }
  1452. }
  1453. }
  1454. for (int K = 0; K < KEX_COUNT && Index < KEX_COUNT; K++)
  1455. {
  1456. if (!Used[DefaultKexList[K]]) Kex[Index++] = DefaultKexList[K];
  1457. }
  1458. }
  1459. //---------------------------------------------------------------------
  1460. UnicodeString __fastcall TSessionData::GetKexList() const
  1461. {
  1462. UnicodeString Result;
  1463. for (int Index = 0; Index < KEX_COUNT; Index++)
  1464. {
  1465. Result += UnicodeString(Index ? L"," : L"") + KexNames[Kex[Index]];
  1466. }
  1467. return Result;
  1468. }
  1469. //---------------------------------------------------------------------
  1470. void __fastcall TSessionData::SetPublicKeyFile(UnicodeString value)
  1471. {
  1472. if (FPublicKeyFile != value)
  1473. {
  1474. FPublicKeyFile = StripPathQuotes(value);
  1475. Modify();
  1476. }
  1477. }
  1478. //---------------------------------------------------------------------
  1479. void __fastcall TSessionData::SetReturnVar(UnicodeString value)
  1480. {
  1481. SET_SESSION_PROPERTY(ReturnVar);
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. void __fastcall TSessionData::SetLookupUserGroups(TAutoSwitch value)
  1485. {
  1486. SET_SESSION_PROPERTY(LookupUserGroups);
  1487. }
  1488. //---------------------------------------------------------------------------
  1489. void __fastcall TSessionData::SetEOLType(TEOLType value)
  1490. {
  1491. SET_SESSION_PROPERTY(EOLType);
  1492. }
  1493. //---------------------------------------------------------------------------
  1494. TDateTime __fastcall TSessionData::GetTimeoutDT()
  1495. {
  1496. return SecToDateTime(Timeout);
  1497. }
  1498. //---------------------------------------------------------------------------
  1499. void __fastcall TSessionData::SetTimeout(int value)
  1500. {
  1501. SET_SESSION_PROPERTY(Timeout);
  1502. }
  1503. //---------------------------------------------------------------------------
  1504. void __fastcall TSessionData::SetProtocol(TProtocol value)
  1505. {
  1506. SET_SESSION_PROPERTY(Protocol);
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  1510. {
  1511. SET_SESSION_PROPERTY(FSProtocol);
  1512. }
  1513. //---------------------------------------------------------------------
  1514. UnicodeString __fastcall TSessionData::GetFSProtocolStr()
  1515. {
  1516. assert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  1517. return FSProtocolNames[FSProtocol];
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  1521. {
  1522. if (value != DetectReturnVar)
  1523. {
  1524. ReturnVar = value ? L"" : L"$?";
  1525. }
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. bool __fastcall TSessionData::GetDetectReturnVar()
  1529. {
  1530. return ReturnVar.IsEmpty();
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. void __fastcall TSessionData::SetDefaultShell(bool value)
  1534. {
  1535. if (value != DefaultShell)
  1536. {
  1537. Shell = value ? L"" : L"/bin/bash";
  1538. }
  1539. }
  1540. //---------------------------------------------------------------------------
  1541. bool __fastcall TSessionData::GetDefaultShell()
  1542. {
  1543. return Shell.IsEmpty();
  1544. }
  1545. //---------------------------------------------------------------------------
  1546. void __fastcall TSessionData::SetProtocolStr(UnicodeString value)
  1547. {
  1548. FProtocol = ptRaw;
  1549. for (int Index = 0; Index < PROTOCOL_COUNT; Index++)
  1550. {
  1551. if (value.CompareIC(ProtocolNames[Index]) == 0)
  1552. {
  1553. FProtocol = TProtocol(Index);
  1554. break;
  1555. }
  1556. }
  1557. }
  1558. //---------------------------------------------------------------------
  1559. UnicodeString __fastcall TSessionData::GetProtocolStr() const
  1560. {
  1561. return ProtocolNames[Protocol];
  1562. }
  1563. //---------------------------------------------------------------------
  1564. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  1565. {
  1566. unsigned short hour, min, sec, msec;
  1567. value.DecodeTime(&hour, &min, &sec, &msec);
  1568. PingInterval = ((int)hour)*SecsPerHour + ((int)min)*SecsPerMin + sec;
  1569. }
  1570. //---------------------------------------------------------------------------
  1571. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  1572. {
  1573. return SecToDateTime(PingInterval);
  1574. }
  1575. //---------------------------------------------------------------------------
  1576. void __fastcall TSessionData::SetPingType(TPingType value)
  1577. {
  1578. SET_SESSION_PROPERTY(PingType);
  1579. }
  1580. //---------------------------------------------------------------------------
  1581. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  1582. {
  1583. SET_SESSION_PROPERTY(AddressFamily);
  1584. }
  1585. //---------------------------------------------------------------------------
  1586. void __fastcall TSessionData::SetRekeyData(UnicodeString value)
  1587. {
  1588. SET_SESSION_PROPERTY(RekeyData);
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  1592. {
  1593. SET_SESSION_PROPERTY(RekeyTime);
  1594. }
  1595. //---------------------------------------------------------------------
  1596. UnicodeString __fastcall TSessionData::GetDefaultSessionName()
  1597. {
  1598. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  1599. {
  1600. return FORMAT(L"%s@%s", (UserName, HostName));
  1601. }
  1602. else if (!HostName.IsEmpty())
  1603. {
  1604. return HostName;
  1605. }
  1606. else
  1607. {
  1608. return L"session";
  1609. }
  1610. }
  1611. //---------------------------------------------------------------------
  1612. bool __fastcall TSessionData::HasSessionName()
  1613. {
  1614. return (!Name.IsEmpty() && (Name != DefaultName));
  1615. }
  1616. //---------------------------------------------------------------------
  1617. UnicodeString __fastcall TSessionData::GetSessionName()
  1618. {
  1619. UnicodeString Result;
  1620. if (HasSessionName())
  1621. {
  1622. Result = Name;
  1623. if (Hidden)
  1624. {
  1625. Result = Result.SubString(TNamedObjectList::HiddenPrefix.Length() + 1, Result.Length() - TNamedObjectList::HiddenPrefix.Length());
  1626. }
  1627. }
  1628. else
  1629. {
  1630. Result = DefaultSessionName;
  1631. }
  1632. return Result;
  1633. }
  1634. //---------------------------------------------------------------------
  1635. UnicodeString __fastcall TSessionData::GetSessionUrl()
  1636. {
  1637. UnicodeString Url;
  1638. if (HasSessionName())
  1639. {
  1640. Url = Name;
  1641. }
  1642. else
  1643. {
  1644. switch (FSProtocol)
  1645. {
  1646. case fsSCPonly:
  1647. Url = L"scp://";
  1648. break;
  1649. default:
  1650. assert(false);
  1651. // fallback
  1652. case fsSFTP:
  1653. case fsSFTPonly:
  1654. Url = L"sftp://";
  1655. break;
  1656. case fsFTP:
  1657. Url = L"ftp://";
  1658. break;
  1659. }
  1660. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  1661. {
  1662. Url += FORMAT(L"%s@%s", (UserName, HostName));
  1663. }
  1664. else if (!HostName.IsEmpty())
  1665. {
  1666. Url += HostName;
  1667. }
  1668. else
  1669. {
  1670. Url = L"";
  1671. }
  1672. }
  1673. return Url;
  1674. }
  1675. //---------------------------------------------------------------------
  1676. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  1677. {
  1678. SET_SESSION_PROPERTY(TimeDifference);
  1679. }
  1680. //---------------------------------------------------------------------
  1681. void __fastcall TSessionData::SetLocalDirectory(UnicodeString value)
  1682. {
  1683. SET_SESSION_PROPERTY(LocalDirectory);
  1684. }
  1685. //---------------------------------------------------------------------
  1686. void __fastcall TSessionData::SetRemoteDirectory(UnicodeString value)
  1687. {
  1688. SET_SESSION_PROPERTY(RemoteDirectory);
  1689. }
  1690. //---------------------------------------------------------------------
  1691. void __fastcall TSessionData::SetSynchronizeBrowsing(bool value)
  1692. {
  1693. SET_SESSION_PROPERTY(SynchronizeBrowsing);
  1694. }
  1695. //---------------------------------------------------------------------
  1696. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  1697. {
  1698. SET_SESSION_PROPERTY(UpdateDirectories);
  1699. }
  1700. //---------------------------------------------------------------------
  1701. void __fastcall TSessionData::SetCacheDirectories(bool value)
  1702. {
  1703. SET_SESSION_PROPERTY(CacheDirectories);
  1704. }
  1705. //---------------------------------------------------------------------
  1706. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  1707. {
  1708. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  1709. }
  1710. //---------------------------------------------------------------------
  1711. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  1712. {
  1713. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  1714. }
  1715. //---------------------------------------------------------------------
  1716. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  1717. {
  1718. SET_SESSION_PROPERTY(ResolveSymlinks);
  1719. }
  1720. //---------------------------------------------------------------------------
  1721. void __fastcall TSessionData::SetDSTMode(TDSTMode value)
  1722. {
  1723. SET_SESSION_PROPERTY(DSTMode);
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  1727. {
  1728. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  1729. }
  1730. //---------------------------------------------------------------------------
  1731. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  1732. {
  1733. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  1734. }
  1735. //---------------------------------------------------------------------------
  1736. void __fastcall TSessionData::SetRecycleBinPath(UnicodeString value)
  1737. {
  1738. SET_SESSION_PROPERTY(RecycleBinPath);
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. void __fastcall TSessionData::SetPostLoginCommands(UnicodeString value)
  1742. {
  1743. SET_SESSION_PROPERTY(PostLoginCommands);
  1744. }
  1745. //---------------------------------------------------------------------
  1746. void __fastcall TSessionData::SetLockInHome(bool value)
  1747. {
  1748. SET_SESSION_PROPERTY(LockInHome);
  1749. }
  1750. //---------------------------------------------------------------------
  1751. void __fastcall TSessionData::SetSpecial(bool value)
  1752. {
  1753. SET_SESSION_PROPERTY(Special);
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  1757. {
  1758. SET_SESSION_PROPERTY(Scp1Compatibility);
  1759. }
  1760. //---------------------------------------------------------------------
  1761. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  1762. {
  1763. SET_SESSION_PROPERTY(TcpNoDelay);
  1764. }
  1765. //---------------------------------------------------------------------
  1766. void __fastcall TSessionData::SetSendBuf(int value)
  1767. {
  1768. SET_SESSION_PROPERTY(SendBuf);
  1769. }
  1770. //---------------------------------------------------------------------
  1771. void __fastcall TSessionData::SetSshSimple(bool value)
  1772. {
  1773. SET_SESSION_PROPERTY(SshSimple);
  1774. }
  1775. //---------------------------------------------------------------------
  1776. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  1777. {
  1778. SET_SESSION_PROPERTY(ProxyMethod);
  1779. }
  1780. //---------------------------------------------------------------------
  1781. void __fastcall TSessionData::SetProxyHost(UnicodeString value)
  1782. {
  1783. SET_SESSION_PROPERTY(ProxyHost);
  1784. }
  1785. //---------------------------------------------------------------------
  1786. void __fastcall TSessionData::SetProxyPort(int value)
  1787. {
  1788. SET_SESSION_PROPERTY(ProxyPort);
  1789. }
  1790. //---------------------------------------------------------------------
  1791. void __fastcall TSessionData::SetProxyUsername(UnicodeString value)
  1792. {
  1793. SET_SESSION_PROPERTY(ProxyUsername);
  1794. }
  1795. //---------------------------------------------------------------------
  1796. void __fastcall TSessionData::SetProxyPassword(UnicodeString avalue)
  1797. {
  1798. RawByteString value = EncryptPassword(avalue, ProxyUsername+ProxyHost);
  1799. SET_SESSION_PROPERTY(ProxyPassword);
  1800. }
  1801. //---------------------------------------------------------------------
  1802. UnicodeString __fastcall TSessionData::GetProxyPassword() const
  1803. {
  1804. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  1805. }
  1806. //---------------------------------------------------------------------
  1807. void __fastcall TSessionData::SetProxyTelnetCommand(UnicodeString value)
  1808. {
  1809. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  1810. }
  1811. //---------------------------------------------------------------------
  1812. void __fastcall TSessionData::SetProxyLocalCommand(UnicodeString value)
  1813. {
  1814. SET_SESSION_PROPERTY(ProxyLocalCommand);
  1815. }
  1816. //---------------------------------------------------------------------
  1817. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  1818. {
  1819. SET_SESSION_PROPERTY(ProxyDNS);
  1820. }
  1821. //---------------------------------------------------------------------
  1822. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  1823. {
  1824. SET_SESSION_PROPERTY(ProxyLocalhost);
  1825. }
  1826. //---------------------------------------------------------------------
  1827. void __fastcall TSessionData::SetFtpProxyLogonType(int value)
  1828. {
  1829. SET_SESSION_PROPERTY(FtpProxyLogonType);
  1830. }
  1831. //---------------------------------------------------------------------
  1832. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  1833. {
  1834. assert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  1835. SET_SESSION_PROPERTY(Bugs[Bug]);
  1836. }
  1837. //---------------------------------------------------------------------
  1838. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  1839. {
  1840. assert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  1841. return FBugs[Bug];
  1842. }
  1843. //---------------------------------------------------------------------
  1844. void __fastcall TSessionData::SetCustomParam1(UnicodeString value)
  1845. {
  1846. SET_SESSION_PROPERTY(CustomParam1);
  1847. }
  1848. //---------------------------------------------------------------------
  1849. void __fastcall TSessionData::SetCustomParam2(UnicodeString value)
  1850. {
  1851. SET_SESSION_PROPERTY(CustomParam2);
  1852. }
  1853. //---------------------------------------------------------------------
  1854. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  1855. {
  1856. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  1857. }
  1858. //---------------------------------------------------------------------
  1859. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  1860. {
  1861. SET_SESSION_PROPERTY(SFTPUploadQueue);
  1862. }
  1863. //---------------------------------------------------------------------
  1864. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  1865. {
  1866. SET_SESSION_PROPERTY(SFTPListingQueue);
  1867. }
  1868. //---------------------------------------------------------------------
  1869. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  1870. {
  1871. SET_SESSION_PROPERTY(SFTPMaxVersion);
  1872. }
  1873. //---------------------------------------------------------------------
  1874. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  1875. {
  1876. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  1877. }
  1878. //---------------------------------------------------------------------
  1879. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  1880. {
  1881. assert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  1882. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  1883. }
  1884. //---------------------------------------------------------------------
  1885. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  1886. {
  1887. assert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  1888. return FSFTPBugs[Bug];
  1889. }
  1890. //---------------------------------------------------------------------
  1891. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  1892. {
  1893. SET_SESSION_PROPERTY(SCPLsFullTime);
  1894. }
  1895. //---------------------------------------------------------------------------
  1896. void __fastcall TSessionData::SetColor(int value)
  1897. {
  1898. SET_SESSION_PROPERTY(Color);
  1899. }
  1900. //---------------------------------------------------------------------------
  1901. void __fastcall TSessionData::SetTunnel(bool value)
  1902. {
  1903. SET_SESSION_PROPERTY(Tunnel);
  1904. }
  1905. //---------------------------------------------------------------------
  1906. void __fastcall TSessionData::SetTunnelHostName(UnicodeString value)
  1907. {
  1908. if (FTunnelHostName != value)
  1909. {
  1910. // HostName is key for password encryption
  1911. UnicodeString XTunnelPassword = TunnelPassword;
  1912. int P = value.LastDelimiter(L"@");
  1913. if (P > 0)
  1914. {
  1915. TunnelUserName = value.SubString(1, P - 1);
  1916. value = value.SubString(P + 1, value.Length() - P);
  1917. }
  1918. FTunnelHostName = value;
  1919. Modify();
  1920. TunnelPassword = XTunnelPassword;
  1921. Shred(XTunnelPassword);
  1922. }
  1923. }
  1924. //---------------------------------------------------------------------
  1925. void __fastcall TSessionData::SetTunnelPortNumber(int value)
  1926. {
  1927. SET_SESSION_PROPERTY(TunnelPortNumber);
  1928. }
  1929. //---------------------------------------------------------------------
  1930. void __fastcall TSessionData::SetTunnelUserName(UnicodeString value)
  1931. {
  1932. // TunnelUserName is key for password encryption
  1933. UnicodeString XTunnelPassword = TunnelPassword;
  1934. SET_SESSION_PROPERTY(TunnelUserName);
  1935. TunnelPassword = XTunnelPassword;
  1936. Shred(XTunnelPassword);
  1937. }
  1938. //---------------------------------------------------------------------
  1939. void __fastcall TSessionData::SetTunnelPassword(UnicodeString avalue)
  1940. {
  1941. RawByteString value = EncryptPassword(avalue, TunnelUserName+TunnelHostName);
  1942. SET_SESSION_PROPERTY(TunnelPassword);
  1943. }
  1944. //---------------------------------------------------------------------
  1945. UnicodeString __fastcall TSessionData::GetTunnelPassword() const
  1946. {
  1947. return DecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName);
  1948. }
  1949. //---------------------------------------------------------------------
  1950. void __fastcall TSessionData::SetTunnelPublicKeyFile(UnicodeString value)
  1951. {
  1952. if (FTunnelPublicKeyFile != value)
  1953. {
  1954. FTunnelPublicKeyFile = StripPathQuotes(value);
  1955. Modify();
  1956. }
  1957. }
  1958. //---------------------------------------------------------------------
  1959. void __fastcall TSessionData::SetTunnelLocalPortNumber(int value)
  1960. {
  1961. SET_SESSION_PROPERTY(TunnelLocalPortNumber);
  1962. }
  1963. //---------------------------------------------------------------------
  1964. bool __fastcall TSessionData::GetTunnelAutoassignLocalPortNumber()
  1965. {
  1966. return (FTunnelLocalPortNumber <= 0);
  1967. }
  1968. //---------------------------------------------------------------------
  1969. void __fastcall TSessionData::SetTunnelPortFwd(UnicodeString value)
  1970. {
  1971. SET_SESSION_PROPERTY(TunnelPortFwd);
  1972. }
  1973. //---------------------------------------------------------------------
  1974. void __fastcall TSessionData::SetTunnelHostKey(UnicodeString value)
  1975. {
  1976. SET_SESSION_PROPERTY(TunnelHostKey);
  1977. }
  1978. //---------------------------------------------------------------------
  1979. void __fastcall TSessionData::SetFtpPasvMode(bool value)
  1980. {
  1981. SET_SESSION_PROPERTY(FtpPasvMode);
  1982. }
  1983. //---------------------------------------------------------------------
  1984. void __fastcall TSessionData::SetFtpForcePasvIp(TAutoSwitch value)
  1985. {
  1986. SET_SESSION_PROPERTY(FtpForcePasvIp);
  1987. }
  1988. //---------------------------------------------------------------------
  1989. void __fastcall TSessionData::SetFtpAccount(UnicodeString value)
  1990. {
  1991. SET_SESSION_PROPERTY(FtpAccount);
  1992. }
  1993. //---------------------------------------------------------------------
  1994. void __fastcall TSessionData::SetFtpPingInterval(int value)
  1995. {
  1996. SET_SESSION_PROPERTY(FtpPingInterval);
  1997. }
  1998. //---------------------------------------------------------------------------
  1999. TDateTime __fastcall TSessionData::GetFtpPingIntervalDT()
  2000. {
  2001. return SecToDateTime(FtpPingInterval);
  2002. }
  2003. //---------------------------------------------------------------------------
  2004. void __fastcall TSessionData::SetFtpPingType(TPingType value)
  2005. {
  2006. SET_SESSION_PROPERTY(FtpPingType);
  2007. }
  2008. //---------------------------------------------------------------------------
  2009. void __fastcall TSessionData::SetFtps(TFtps value)
  2010. {
  2011. SET_SESSION_PROPERTY(Ftps);
  2012. }
  2013. //---------------------------------------------------------------------
  2014. void __fastcall TSessionData::SetFtpListAll(TAutoSwitch value)
  2015. {
  2016. SET_SESSION_PROPERTY(FtpListAll);
  2017. }
  2018. //---------------------------------------------------------------------
  2019. void __fastcall TSessionData::SetSslSessionReuse(bool value)
  2020. {
  2021. SET_SESSION_PROPERTY(SslSessionReuse);
  2022. }
  2023. //---------------------------------------------------------------------
  2024. void __fastcall TSessionData::SetNotUtf(TAutoSwitch value)
  2025. {
  2026. SET_SESSION_PROPERTY(NotUtf);
  2027. }
  2028. //---------------------------------------------------------------------
  2029. void __fastcall TSessionData::SetHostKey(UnicodeString value)
  2030. {
  2031. SET_SESSION_PROPERTY(HostKey);
  2032. }
  2033. //---------------------------------------------------------------------
  2034. UnicodeString __fastcall TSessionData::GetInfoTip()
  2035. {
  2036. if (UsesSsh)
  2037. {
  2038. return FMTLOAD(SESSION_INFO_TIP,
  2039. (HostName, UserName,
  2040. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  2041. SshProtStr, FSProtocolStr));
  2042. }
  2043. else
  2044. {
  2045. return FMTLOAD(SESSION_INFO_TIP_NO_SSH,
  2046. (HostName, UserName, FSProtocolStr));
  2047. }
  2048. }
  2049. //---------------------------------------------------------------------
  2050. UnicodeString __fastcall TSessionData::GetLocalName()
  2051. {
  2052. UnicodeString Result;
  2053. if (HasSessionName())
  2054. {
  2055. Result = Name;
  2056. int P = Result.LastDelimiter(L"/");
  2057. if (P > 0)
  2058. {
  2059. Result.Delete(1, P);
  2060. }
  2061. }
  2062. else
  2063. {
  2064. Result = DefaultSessionName;
  2065. }
  2066. return Result;
  2067. }
  2068. //=== TStoredSessionList ----------------------------------------------
  2069. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  2070. TNamedObjectList(), FReadOnly(aReadOnly)
  2071. {
  2072. assert(Configuration);
  2073. FDefaultSettings = new TSessionData(DefaultName);
  2074. }
  2075. //---------------------------------------------------------------------
  2076. __fastcall TStoredSessionList::~TStoredSessionList()
  2077. {
  2078. assert(Configuration);
  2079. delete FDefaultSettings;
  2080. }
  2081. //---------------------------------------------------------------------
  2082. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  2083. bool AsModified, bool UseDefaults)
  2084. {
  2085. TStringList *SubKeys = new TStringList();
  2086. TList * Loaded = new TList;
  2087. try
  2088. {
  2089. Storage->GetSubKeyNames(SubKeys);
  2090. for (int Index = 0; Index < SubKeys->Count; Index++)
  2091. {
  2092. TSessionData *SessionData;
  2093. UnicodeString SessionName = SubKeys->Strings[Index];
  2094. bool ValidName = true;
  2095. try
  2096. {
  2097. TSessionData::ValidatePath(SessionName);
  2098. }
  2099. catch(...)
  2100. {
  2101. ValidName = false;
  2102. }
  2103. if (ValidName)
  2104. {
  2105. if (SessionName == FDefaultSettings->Name) SessionData = FDefaultSettings;
  2106. else SessionData = (TSessionData*)FindByName(SessionName);
  2107. if ((SessionData != FDefaultSettings) || !UseDefaults)
  2108. {
  2109. if (!SessionData)
  2110. {
  2111. SessionData = new TSessionData(L"");
  2112. if (UseDefaults)
  2113. {
  2114. SessionData->Assign(DefaultSettings);
  2115. }
  2116. SessionData->Name = SessionName;
  2117. Add(SessionData);
  2118. }
  2119. Loaded->Add(SessionData);
  2120. SessionData->Load(Storage);
  2121. if (AsModified)
  2122. {
  2123. SessionData->Modified = true;
  2124. }
  2125. if (SessionData->IsWorkspace)
  2126. {
  2127. Remove(SessionData);
  2128. }
  2129. }
  2130. }
  2131. }
  2132. if (!AsModified)
  2133. {
  2134. for (int Index = 0; Index < TObjectList::Count; Index++)
  2135. {
  2136. if (Loaded->IndexOf(Items[Index]) < 0)
  2137. {
  2138. Delete(Index);
  2139. Index--;
  2140. }
  2141. }
  2142. }
  2143. }
  2144. __finally
  2145. {
  2146. delete SubKeys;
  2147. delete Loaded;
  2148. }
  2149. }
  2150. //---------------------------------------------------------------------
  2151. void __fastcall TStoredSessionList::Load()
  2152. {
  2153. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  2154. try
  2155. {
  2156. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  2157. Load(Storage);
  2158. }
  2159. __finally
  2160. {
  2161. delete Storage;
  2162. }
  2163. }
  2164. //---------------------------------------------------------------------
  2165. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  2166. TSessionData * Data, bool All, bool RecryptPasswordOnly,
  2167. TSessionData * FactoryDefaults)
  2168. {
  2169. if (All || Data->Modified)
  2170. {
  2171. if (RecryptPasswordOnly)
  2172. {
  2173. Data->SaveRecryptedPasswords(Storage);
  2174. }
  2175. else
  2176. {
  2177. Data->Save(Storage, false, FactoryDefaults);
  2178. }
  2179. }
  2180. }
  2181. //---------------------------------------------------------------------
  2182. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  2183. bool All, bool RecryptPasswordOnly)
  2184. {
  2185. TSessionData * FactoryDefaults = new TSessionData(L"");
  2186. try
  2187. {
  2188. DoSave(Storage, FDefaultSettings, All, RecryptPasswordOnly, FactoryDefaults);
  2189. for (int Index = 0; Index < Count+HiddenCount; Index++)
  2190. {
  2191. TSessionData * SessionData = (TSessionData *)Items[Index];
  2192. DoSave(Storage, SessionData, All, RecryptPasswordOnly, FactoryDefaults);
  2193. }
  2194. }
  2195. __finally
  2196. {
  2197. delete FactoryDefaults;
  2198. }
  2199. }
  2200. //---------------------------------------------------------------------
  2201. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage, bool All)
  2202. {
  2203. DoSave(Storage, All, false);
  2204. }
  2205. //---------------------------------------------------------------------
  2206. void __fastcall TStoredSessionList::DoSave(bool All, bool Explicit, bool RecryptPasswordOnly)
  2207. {
  2208. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  2209. try
  2210. {
  2211. Storage->AccessMode = smReadWrite;
  2212. Storage->Explicit = Explicit;
  2213. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  2214. {
  2215. DoSave(Storage, All, RecryptPasswordOnly);
  2216. }
  2217. }
  2218. __finally
  2219. {
  2220. delete Storage;
  2221. }
  2222. Saved();
  2223. }
  2224. //---------------------------------------------------------------------
  2225. void __fastcall TStoredSessionList::Save(bool All, bool Explicit)
  2226. {
  2227. DoSave(All, Explicit, false);
  2228. }
  2229. //---------------------------------------------------------------------
  2230. void __fastcall TStoredSessionList::RecryptPasswords()
  2231. {
  2232. DoSave(true, true, true);
  2233. }
  2234. //---------------------------------------------------------------------
  2235. void __fastcall TStoredSessionList::Saved()
  2236. {
  2237. FDefaultSettings->Modified = false;
  2238. for (int Index = 0; Index < Count + HiddenCount; Index++)
  2239. {
  2240. ((TSessionData *)Items[Index])->Modified = false;
  2241. }
  2242. }
  2243. //---------------------------------------------------------------------
  2244. void __fastcall TStoredSessionList::Export(const UnicodeString FileName)
  2245. {
  2246. THierarchicalStorage * Storage = new TIniFileStorage(FileName);
  2247. try
  2248. {
  2249. Storage->AccessMode = smReadWrite;
  2250. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  2251. {
  2252. Save(Storage, true);
  2253. }
  2254. }
  2255. __finally
  2256. {
  2257. delete Storage;
  2258. }
  2259. }
  2260. //---------------------------------------------------------------------
  2261. void __fastcall TStoredSessionList::SelectAll(bool Select)
  2262. {
  2263. for (int Index = 0; Index < Count; Index++)
  2264. Sessions[Index]->Selected = Select;
  2265. }
  2266. //---------------------------------------------------------------------
  2267. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  2268. bool OnlySelected)
  2269. {
  2270. for (int Index = 0; Index < From->Count; Index++)
  2271. {
  2272. if (!OnlySelected || From->Sessions[Index]->Selected)
  2273. {
  2274. TSessionData *Session = new TSessionData(L"");
  2275. Session->Assign(From->Sessions[Index]);
  2276. Session->Modified = true;
  2277. Session->MakeUniqueIn(this);
  2278. Add(Session);
  2279. }
  2280. }
  2281. // only modified, explicit
  2282. Save(false, true);
  2283. }
  2284. //---------------------------------------------------------------------
  2285. void __fastcall TStoredSessionList::SelectSessionsToImport
  2286. (TStoredSessionList * Dest, bool SSHOnly)
  2287. {
  2288. for (int Index = 0; Index < Count; Index++)
  2289. {
  2290. Sessions[Index]->Selected =
  2291. (!SSHOnly || (Sessions[Index]->Protocol == ptSSH)) &&
  2292. !Dest->FindByName(Sessions[Index]->Name);
  2293. }
  2294. }
  2295. //---------------------------------------------------------------------
  2296. void __fastcall TStoredSessionList::Cleanup()
  2297. {
  2298. try
  2299. {
  2300. if (Configuration->Storage == stRegistry) Clear();
  2301. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  2302. try
  2303. {
  2304. Storage->AccessMode = smReadWrite;
  2305. if (Storage->OpenRootKey(False))
  2306. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  2307. }
  2308. __finally
  2309. {
  2310. delete Storage;
  2311. }
  2312. }
  2313. catch (Exception &E)
  2314. {
  2315. throw ExtException(&E, CLEANUP_SESSIONS_ERROR);
  2316. }
  2317. }
  2318. //---------------------------------------------------------------------------
  2319. void __fastcall TStoredSessionList::UpdateStaticUsage()
  2320. {
  2321. int SCP = 0;
  2322. int SFTP = 0;
  2323. int FTP = 0;
  2324. int FTPS = 0;
  2325. int Password = 0;
  2326. int Advanced = 0;
  2327. int Color = 0;
  2328. bool Folders = false;
  2329. std::auto_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  2330. for (int Index = 0; Index < Count; Index++)
  2331. {
  2332. TSessionData * Data = Sessions[Index];
  2333. switch (Data->FSProtocol)
  2334. {
  2335. case fsSCPonly:
  2336. SCP++;
  2337. break;
  2338. case fsSFTP:
  2339. case fsSFTPonly:
  2340. SFTP++;
  2341. break;
  2342. case fsFTP:
  2343. if (Data->Ftps == ftpsNone)
  2344. {
  2345. FTP++;
  2346. }
  2347. else
  2348. {
  2349. FTPS++;
  2350. }
  2351. break;
  2352. }
  2353. if (Data->HasAnyPassword())
  2354. {
  2355. Password++;
  2356. }
  2357. if (Data->Color != 0)
  2358. {
  2359. Color++;
  2360. }
  2361. if (!Data->IsSame(FactoryDefaults.get(), true))
  2362. {
  2363. Advanced++;
  2364. }
  2365. if (Data->Name.Pos(L"/") > 0)
  2366. {
  2367. Folders = true;
  2368. }
  2369. }
  2370. Configuration->Usage->Set(L"StoredSessionsCountSCP", SCP);
  2371. Configuration->Usage->Set(L"StoredSessionsCountSFTP", SFTP);
  2372. Configuration->Usage->Set(L"StoredSessionsCountFTP", FTP);
  2373. Configuration->Usage->Set(L"StoredSessionsCountFTPS", FTPS);
  2374. Configuration->Usage->Set(L"StoredSessionsCountPassword", Password);
  2375. Configuration->Usage->Set(L"StoredSessionsCountColor", Color);
  2376. Configuration->Usage->Set(L"StoredSessionsCountAdvanced", Advanced);
  2377. bool CustomDefaultStoredSession = !FDefaultSettings->IsSame(FactoryDefaults.get(), false);
  2378. Configuration->Usage->Set(L"UsingDefaultStoredSession", CustomDefaultStoredSession);
  2379. Configuration->Usage->Set(L"UsingStoredSessionsFolders", Folders);
  2380. }
  2381. //---------------------------------------------------------------------------
  2382. TSessionData * __fastcall TStoredSessionList::FindSame(TSessionData * Data)
  2383. {
  2384. TSessionData * Result;
  2385. if (Data->Hidden && Data->Name.IsEmpty())
  2386. {
  2387. Result = NULL;
  2388. }
  2389. else
  2390. {
  2391. Result = dynamic_cast<TSessionData *>(FindByName(Data->Name));
  2392. }
  2393. return Result;
  2394. }
  2395. //---------------------------------------------------------------------------
  2396. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  2397. {
  2398. for (int Index = 0; Index < Count; Index++)
  2399. if (Data == Sessions[Index]) return Index;
  2400. return -1;
  2401. }
  2402. //---------------------------------------------------------------------------
  2403. TSessionData * __fastcall TStoredSessionList::NewSession(
  2404. UnicodeString SessionName, TSessionData * Session)
  2405. {
  2406. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  2407. if (!DuplicateSession)
  2408. {
  2409. DuplicateSession = new TSessionData(L"");
  2410. DuplicateSession->Assign(Session);
  2411. DuplicateSession->Name = SessionName;
  2412. // make sure, that new stored session is saved to registry
  2413. DuplicateSession->Modified = true;
  2414. Add(DuplicateSession);
  2415. }
  2416. else
  2417. {
  2418. DuplicateSession->Assign(Session);
  2419. DuplicateSession->Name = SessionName;
  2420. DuplicateSession->Modified = true;
  2421. }
  2422. // list was saved here before to default storage, but it would not allow
  2423. // to work with special lists (export/import) not using default storage
  2424. return DuplicateSession;
  2425. }
  2426. //---------------------------------------------------------------------------
  2427. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  2428. {
  2429. assert(FDefaultSettings);
  2430. if (FDefaultSettings != value)
  2431. {
  2432. FDefaultSettings->Assign(value);
  2433. // make sure default settings are saved
  2434. FDefaultSettings->Modified = true;
  2435. FDefaultSettings->Name = DefaultName;
  2436. if (!FReadOnly)
  2437. {
  2438. // only modified, explicit
  2439. Save(false, true);
  2440. }
  2441. }
  2442. }
  2443. //---------------------------------------------------------------------------
  2444. void __fastcall TStoredSessionList::ImportHostKeys(const UnicodeString TargetKey,
  2445. const UnicodeString SourceKey, TStoredSessionList * Sessions,
  2446. bool OnlySelected)
  2447. {
  2448. TRegistryStorage * SourceStorage = NULL;
  2449. TRegistryStorage * TargetStorage = NULL;
  2450. TStringList * KeyList = NULL;
  2451. try
  2452. {
  2453. SourceStorage = new TRegistryStorage(SourceKey);
  2454. TargetStorage = new TRegistryStorage(TargetKey);
  2455. TargetStorage->AccessMode = smReadWrite;
  2456. KeyList = new TStringList();
  2457. if (SourceStorage->OpenRootKey(false) &&
  2458. TargetStorage->OpenRootKey(true))
  2459. {
  2460. SourceStorage->GetValueNames(KeyList);
  2461. TSessionData * Session;
  2462. UnicodeString HostKeyName;
  2463. assert(Sessions != NULL);
  2464. for (int Index = 0; Index < Sessions->Count; Index++)
  2465. {
  2466. Session = Sessions->Sessions[Index];
  2467. if (!OnlySelected || Session->Selected)
  2468. {
  2469. HostKeyName = PuttyMungeStr(FORMAT(L"@%d:%s", (Session->PortNumber, Session->HostName)));
  2470. UnicodeString KeyName;
  2471. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  2472. {
  2473. KeyName = KeyList->Strings[KeyIndex];
  2474. int P = KeyName.Pos(HostKeyName);
  2475. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  2476. {
  2477. TargetStorage->WriteStringRaw(KeyName,
  2478. SourceStorage->ReadStringRaw(KeyName, L""));
  2479. }
  2480. }
  2481. }
  2482. }
  2483. }
  2484. }
  2485. __finally
  2486. {
  2487. delete SourceStorage;
  2488. delete TargetStorage;
  2489. delete KeyList;
  2490. }
  2491. }
  2492. //---------------------------------------------------------------------------
  2493. TSessionData * __fastcall TStoredSessionList::ParseUrl(UnicodeString Url,
  2494. TOptions * Options, bool & DefaultsOnly, UnicodeString * FileName,
  2495. bool * AProtocolDefined)
  2496. {
  2497. TSessionData * Data = new TSessionData(L"");
  2498. try
  2499. {
  2500. Data->ParseUrl(Url, Options, this, DefaultsOnly, FileName, AProtocolDefined);
  2501. }
  2502. catch(...)
  2503. {
  2504. delete Data;
  2505. throw;
  2506. }
  2507. return Data;
  2508. }