SessionData.cpp 115 KB

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