SessionData.cpp 98 KB

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