SessionData.cpp 106 KB

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