SessionData.cpp 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900
  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 "S3FileSystem.h"
  14. #include <Soap.EncdDecd.hpp>
  15. #include <StrUtils.hpp>
  16. #include <XMLDoc.hpp>
  17. #include <StrUtils.hpp>
  18. #include <algorithm>
  19. //---------------------------------------------------------------------------
  20. #pragma package(smart_init)
  21. //---------------------------------------------------------------------------
  22. const wchar_t * PingTypeNames = L"Off;Null;Dummy";
  23. const wchar_t * ProxyMethodNames = L"None;SOCKS4;SOCKS5;HTTP;Telnet;Cmd";
  24. const wchar_t * DefaultName = L"Default Settings";
  25. const UnicodeString CipherNames[CIPHER_COUNT] = {L"WARN", L"3des", L"blowfish", L"aes", L"des", L"arcfour", L"chacha20"};
  26. const UnicodeString KexNames[KEX_COUNT] = {L"WARN", L"dh-group1-sha1", L"dh-group14-sha1", L"dh-gex-sha1", L"rsa", L"ecdh"};
  27. const UnicodeString HostKeyNames[HOSTKEY_COUNT] = {L"WARN", L"rsa", L"dsa", L"ecdsa", L"ed25519"};
  28. const UnicodeString GssLibNames[GSSLIB_COUNT] = {L"gssapi32", L"sspi", L"custom"};
  29. const wchar_t SshProtList[][10] = {L"1", L"1>2", L"2>1", L"2"};
  30. const TCipher DefaultCipherList[CIPHER_COUNT] =
  31. { cipAES, cipChaCha20, cipBlowfish, cip3DES, cipWarn, cipArcfour, cipDES };
  32. const TKex DefaultKexList[KEX_COUNT] =
  33. { kexECDH, kexDHGEx, kexDHGroup14, kexRSA, kexWarn, kexDHGroup1 };
  34. const THostKey DefaultHostKeyList[HOSTKEY_COUNT] =
  35. { hkED25519, hkECDSA, hkRSA, hkDSA, hkWarn };
  36. const TGssLib DefaultGssLibList[GSSLIB_COUNT] =
  37. { gssGssApi32, gssSspi, gssCustom };
  38. const wchar_t FSProtocolNames[FSPROTOCOL_COUNT][16] = { L"SCP", L"SFTP (SCP)", L"SFTP", L"", L"", L"FTP", L"WebDAV", L"S3" };
  39. const int SshPortNumber = 22;
  40. const int FtpPortNumber = 21;
  41. const int FtpsImplicitPortNumber = 990;
  42. const int HTTPPortNumber = 80;
  43. const int HTTPSPortNumber = 443;
  44. const int TelnetPortNumber = 23;
  45. const int DefaultSendBuf = 262144;
  46. const int ProxyPortNumber = 80;
  47. const UnicodeString AnonymousUserName(L"anonymous");
  48. const UnicodeString AnonymousPassword(L"[email protected]");
  49. const UnicodeString PuttySshProtocol(L"ssh");
  50. const UnicodeString PuttyTelnetProtocol(L"telnet");
  51. const UnicodeString SftpProtocol(L"sftp");
  52. const UnicodeString ScpProtocol(L"scp");
  53. const UnicodeString FtpProtocol(L"ftp");
  54. const UnicodeString FtpsProtocol(L"ftps");
  55. const UnicodeString FtpesProtocol(L"ftpes");
  56. const UnicodeString WebDAVProtocol(L"dav");
  57. const UnicodeString WebDAVSProtocol(L"davs");
  58. const UnicodeString S3Protocol(L"s3");
  59. const UnicodeString SshProtocol(L"ssh");
  60. const UnicodeString WinSCPProtocolPrefix(L"winscp-");
  61. const wchar_t UrlParamSeparator = L';';
  62. const wchar_t UrlParamValueSeparator = L'=';
  63. const UnicodeString UrlHostKeyParamName(L"fingerprint");
  64. const UnicodeString UrlSaveParamName(L"save");
  65. const UnicodeString PassphraseOption(L"passphrase");
  66. const UnicodeString S3HostName(S3LibDefaultHostName());
  67. //---------------------------------------------------------------------
  68. TDateTime __fastcall SecToDateTime(int Sec)
  69. {
  70. return TDateTime(double(Sec) / SecsPerDay);
  71. }
  72. //--- TSessionData ----------------------------------------------------
  73. __fastcall TSessionData::TSessionData(UnicodeString aName):
  74. TNamedObject(aName)
  75. {
  76. Default();
  77. FModified = true;
  78. }
  79. //---------------------------------------------------------------------
  80. _fastcall TSessionData::~TSessionData()
  81. {
  82. }
  83. //---------------------------------------------------------------------
  84. int __fastcall TSessionData::Compare(TNamedObject * Other)
  85. {
  86. int Result;
  87. // To avoid using CompareLogicalText on hex names of sessions in workspace.
  88. // The session 000A would be sorted before 0001.
  89. if (IsWorkspace && DebugNotNull(dynamic_cast<TSessionData *>(Other))->IsWorkspace)
  90. {
  91. Result = CompareText(Name, Other->Name);
  92. }
  93. else
  94. {
  95. Result = TNamedObject::Compare(Other);
  96. }
  97. return Result;
  98. }
  99. //---------------------------------------------------------------------
  100. TSessionData * __fastcall TSessionData::Clone()
  101. {
  102. std::unique_ptr<TSessionData> Data(new TSessionData(L""));
  103. Data->Assign(this);
  104. return Data.release();
  105. }
  106. //---------------------------------------------------------------------
  107. void __fastcall TSessionData::Default()
  108. {
  109. HostName = L"";
  110. PortNumber = SshPortNumber;
  111. UserName = L"";
  112. Password = L"";
  113. NewPassword = L"";
  114. ChangePassword = false;
  115. PingInterval = 30;
  116. PingType = ptOff;
  117. Timeout = 15;
  118. TryAgent = true;
  119. AgentFwd = false;
  120. AuthTIS = false;
  121. AuthKI = true;
  122. AuthKIPassword = true;
  123. AuthGSSAPI = true;
  124. GSSAPIFwdTGT = false;
  125. LogicalHostName = L"";
  126. ChangeUsername = false;
  127. Compression = false;
  128. SshProt = ssh2only;
  129. Ssh2DES = false;
  130. SshNoUserAuth = false;
  131. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  132. {
  133. Cipher[Index] = DefaultCipherList[Index];
  134. }
  135. for (int Index = 0; Index < KEX_COUNT; Index++)
  136. {
  137. Kex[Index] = DefaultKexList[Index];
  138. }
  139. for (int Index = 0; Index < HOSTKEY_COUNT; Index++)
  140. {
  141. HostKeys[Index] = DefaultHostKeyList[Index];
  142. }
  143. for (int Index = 0; Index < GSSLIB_COUNT; Index++)
  144. {
  145. GssLib[Index] = DefaultGssLibList[Index];
  146. }
  147. GssLibCustom = L"";
  148. PublicKeyFile = L"";
  149. Passphrase = L"";
  150. FPuttyProtocol = L"";
  151. TcpNoDelay = false;
  152. SendBuf = DefaultSendBuf;
  153. SshSimple = true;
  154. HostKey = L"";
  155. FingerprintScan = false;
  156. FOverrideCachedHostKey = true;
  157. Note = L"";
  158. WinTitle = L"";
  159. ProxyMethod = ::pmNone;
  160. ProxyHost = L"proxy";
  161. ProxyPort = ProxyPortNumber;
  162. ProxyUsername = L"";
  163. ProxyPassword = L"";
  164. ProxyTelnetCommand = L"connect %host %port\\n";
  165. ProxyLocalCommand = L"";
  166. ProxyDNS = asAuto;
  167. ProxyLocalhost = false;
  168. for (unsigned int Index = 0; Index < LENOF(FBugs); Index++)
  169. {
  170. Bug[(TSshBug)Index] = asAuto;
  171. }
  172. Special = false;
  173. FSProtocol = fsSFTP;
  174. AddressFamily = afAuto;
  175. RekeyData = L"1G";
  176. RekeyTime = MinsPerHour;
  177. // FS common
  178. LocalDirectory = L"";
  179. RemoteDirectory = L"";
  180. SynchronizeBrowsing = false;
  181. UpdateDirectories = true;
  182. CacheDirectories = true;
  183. CacheDirectoryChanges = true;
  184. PreserveDirectoryChanges = true;
  185. LockInHome = false;
  186. ResolveSymlinks = true;
  187. FollowDirectorySymlinks = false;
  188. DSTMode = dstmUnix;
  189. DeleteToRecycleBin = false;
  190. OverwrittenToRecycleBin = false;
  191. RecycleBinPath = L"";
  192. Color = 0;
  193. PostLoginCommands = L"";
  194. // SCP
  195. ReturnVar = L"";
  196. LookupUserGroups = asAuto;
  197. EOLType = eolLF;
  198. TrimVMSVersions = false;
  199. Shell = L""; //default shell
  200. ReturnVar = L"";
  201. ClearAliases = true;
  202. UnsetNationalVars = true;
  203. ListingCommand = L"ls -la";
  204. IgnoreLsWarnings = true;
  205. Scp1Compatibility = false;
  206. TimeDifference = 0;
  207. TimeDifferenceAuto = true;
  208. SCPLsFullTime = asAuto;
  209. NotUtf = asAuto;
  210. // SFTP
  211. SftpServer = L"";
  212. SFTPDownloadQueue = 32;
  213. SFTPUploadQueue = 32;
  214. SFTPListingQueue = 2;
  215. SFTPMaxVersion = ::SFTPMaxVersion;
  216. SFTPMaxPacketSize = 0;
  217. for (unsigned int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  218. {
  219. SFTPBug[(TSftpBug)Index] = asAuto;
  220. }
  221. Tunnel = false;
  222. TunnelHostName = L"";
  223. TunnelPortNumber = SshPortNumber;
  224. TunnelUserName = L"";
  225. TunnelPassword = L"";
  226. TunnelPublicKeyFile = L"";
  227. TunnelLocalPortNumber = 0;
  228. TunnelPortFwd = L"";
  229. TunnelHostKey = L"";
  230. // FTP
  231. FtpPasvMode = true;
  232. FtpForcePasvIp = asAuto;
  233. FtpUseMlsd = asAuto;
  234. FtpAccount = L"";
  235. FtpPingInterval = 30;
  236. FtpPingType = ptDummyCommand;
  237. FtpTransferActiveImmediately = asAuto;
  238. Ftps = ftpsNone;
  239. MinTlsVersion = tls10;
  240. MaxTlsVersion = tls12;
  241. FtpListAll = asAuto;
  242. FtpHost = asAuto;
  243. FtpDeleteFromCwd = asAuto;
  244. SslSessionReuse = true;
  245. TlsCertificateFile = L"";
  246. FtpProxyLogonType = 0; // none
  247. CustomParam1 = L"";
  248. CustomParam2 = L"";
  249. IsWorkspace = false;
  250. Link = L"";
  251. Selected = false;
  252. FModified = false;
  253. FSource = ::ssNone;
  254. FSaveOnly = false;
  255. // add also to TSessionLog::AddStartupInfo()
  256. }
  257. //---------------------------------------------------------------------
  258. void __fastcall TSessionData::NonPersistant()
  259. {
  260. UpdateDirectories = false;
  261. PreserveDirectoryChanges = false;
  262. }
  263. //---------------------------------------------------------------------
  264. #define BASE_PROPERTIES \
  265. PROPERTY(HostName); \
  266. PROPERTY(PortNumber); \
  267. PROPERTY(UserName); \
  268. PROPERTY(Password); \
  269. PROPERTY(PublicKeyFile); \
  270. PROPERTY(Passphrase); \
  271. PROPERTY(FSProtocol); \
  272. PROPERTY(Ftps); \
  273. PROPERTY(LocalDirectory); \
  274. PROPERTY(RemoteDirectory); \
  275. PROPERTY(Color); \
  276. PROPERTY(SynchronizeBrowsing); \
  277. PROPERTY(Note);
  278. //---------------------------------------------------------------------
  279. #define ADVANCED_PROPERTIES \
  280. PROPERTY(NewPassword); \
  281. PROPERTY(ChangePassword); \
  282. PROPERTY(PingInterval); \
  283. PROPERTY(PingType); \
  284. PROPERTY(Timeout); \
  285. PROPERTY(TryAgent); \
  286. PROPERTY(AgentFwd); \
  287. PROPERTY(AuthTIS); \
  288. PROPERTY(LogicalHostName); \
  289. PROPERTY(ChangeUsername); \
  290. PROPERTY(Compression); \
  291. PROPERTY(SshProt); \
  292. PROPERTY(Ssh2DES); \
  293. PROPERTY(SshNoUserAuth); \
  294. PROPERTY(CipherList); \
  295. PROPERTY(KexList); \
  296. PROPERTY(HostKeyList); \
  297. PROPERTY(GssLibList); \
  298. PROPERTY(GssLibCustom); \
  299. PROPERTY(AddressFamily); \
  300. PROPERTY(RekeyData); \
  301. PROPERTY(RekeyTime); \
  302. PROPERTY(HostKey); \
  303. PROPERTY(FingerprintScan); \
  304. \
  305. PROPERTY(UpdateDirectories); \
  306. PROPERTY(CacheDirectories); \
  307. PROPERTY(CacheDirectoryChanges); \
  308. PROPERTY(PreserveDirectoryChanges); \
  309. \
  310. PROPERTY(ResolveSymlinks); \
  311. PROPERTY(FollowDirectorySymlinks); \
  312. PROPERTY(DSTMode); \
  313. PROPERTY(LockInHome); \
  314. PROPERTY(Special); \
  315. PROPERTY(Selected); \
  316. PROPERTY(ReturnVar); \
  317. PROPERTY(LookupUserGroups); \
  318. PROPERTY(EOLType); \
  319. PROPERTY(TrimVMSVersions); \
  320. PROPERTY(Shell); \
  321. PROPERTY(ClearAliases); \
  322. PROPERTY(Scp1Compatibility); \
  323. PROPERTY(UnsetNationalVars); \
  324. PROPERTY(ListingCommand); \
  325. PROPERTY(IgnoreLsWarnings); \
  326. PROPERTY(SCPLsFullTime); \
  327. \
  328. PROPERTY(TimeDifference); \
  329. PROPERTY(TimeDifferenceAuto); \
  330. PROPERTY(TcpNoDelay); \
  331. PROPERTY(SendBuf); \
  332. PROPERTY(SshSimple); \
  333. PROPERTY(AuthKI); \
  334. PROPERTY(AuthKIPassword); \
  335. PROPERTY(AuthGSSAPI); \
  336. PROPERTY(GSSAPIFwdTGT); \
  337. PROPERTY(DeleteToRecycleBin); \
  338. PROPERTY(OverwrittenToRecycleBin); \
  339. PROPERTY(RecycleBinPath); \
  340. PROPERTY(NotUtf); \
  341. PROPERTY(PostLoginCommands); \
  342. \
  343. PROPERTY(ProxyMethod); \
  344. PROPERTY(ProxyHost); \
  345. PROPERTY(ProxyPort); \
  346. PROPERTY(ProxyUsername); \
  347. PROPERTY(ProxyPassword); \
  348. PROPERTY(ProxyTelnetCommand); \
  349. PROPERTY(ProxyLocalCommand); \
  350. PROPERTY(ProxyDNS); \
  351. PROPERTY(ProxyLocalhost); \
  352. \
  353. for (unsigned int Index = 0; Index < LENOF(FBugs); Index++) \
  354. { \
  355. PROPERTY(Bug[(TSshBug)Index]); \
  356. } \
  357. \
  358. PROPERTY(SftpServer); \
  359. PROPERTY(SFTPDownloadQueue); \
  360. PROPERTY(SFTPUploadQueue); \
  361. PROPERTY(SFTPListingQueue); \
  362. PROPERTY(SFTPMaxVersion); \
  363. PROPERTY(SFTPMaxPacketSize); \
  364. \
  365. for (unsigned int Index = 0; Index < LENOF(FSFTPBugs); Index++) \
  366. { \
  367. PROPERTY(SFTPBug[(TSftpBug)Index]); \
  368. } \
  369. \
  370. PROPERTY(Tunnel); \
  371. PROPERTY(TunnelHostName); \
  372. PROPERTY(TunnelPortNumber); \
  373. PROPERTY(TunnelUserName); \
  374. PROPERTY(TunnelPassword); \
  375. PROPERTY(TunnelPublicKeyFile); \
  376. PROPERTY(TunnelLocalPortNumber); \
  377. PROPERTY(TunnelPortFwd); \
  378. PROPERTY(TunnelHostKey); \
  379. \
  380. PROPERTY(FtpPasvMode); \
  381. PROPERTY(FtpForcePasvIp); \
  382. PROPERTY(FtpUseMlsd); \
  383. PROPERTY(FtpAccount); \
  384. PROPERTY(FtpPingInterval); \
  385. PROPERTY(FtpPingType); \
  386. PROPERTY(FtpTransferActiveImmediately); \
  387. PROPERTY(FtpListAll); \
  388. PROPERTY(FtpHost); \
  389. PROPERTY(FtpDeleteFromCwd); \
  390. PROPERTY(SslSessionReuse); \
  391. PROPERTY(TlsCertificateFile); \
  392. \
  393. PROPERTY(FtpProxyLogonType); \
  394. \
  395. PROPERTY(MinTlsVersion); \
  396. PROPERTY(MaxTlsVersion); \
  397. \
  398. PROPERTY(WinTitle); \
  399. PROPERTY(CustomParam1); \
  400. PROPERTY(CustomParam2);
  401. #define META_PROPERTIES \
  402. PROPERTY(IsWorkspace); \
  403. PROPERTY(Link);
  404. //---------------------------------------------------------------------
  405. void __fastcall TSessionData::Assign(TPersistent * Source)
  406. {
  407. if (Source && Source->InheritsFrom(__classid(TSessionData)))
  408. {
  409. TSessionData * SourceData = (TSessionData *)Source;
  410. CopyData(SourceData);
  411. FSource = SourceData->FSource;
  412. }
  413. else
  414. {
  415. TNamedObject::Assign(Source);
  416. }
  417. }
  418. //---------------------------------------------------------------------
  419. void __fastcall TSessionData::CopyData(TSessionData * SourceData)
  420. {
  421. #define PROPERTY(P) P = SourceData->P
  422. PROPERTY(Name);
  423. BASE_PROPERTIES;
  424. ADVANCED_PROPERTIES;
  425. META_PROPERTIES;
  426. #undef PROPERTY
  427. FOverrideCachedHostKey = SourceData->FOverrideCachedHostKey;
  428. FModified = SourceData->Modified;
  429. FSaveOnly = SourceData->FSaveOnly;
  430. }
  431. //---------------------------------------------------------------------
  432. void __fastcall TSessionData::CopyDirectoriesStateData(TSessionData * SourceData)
  433. {
  434. RemoteDirectory = SourceData->RemoteDirectory;
  435. LocalDirectory = SourceData->LocalDirectory;
  436. SynchronizeBrowsing = SourceData->SynchronizeBrowsing;
  437. }
  438. //---------------------------------------------------------------------
  439. bool __fastcall TSessionData::HasStateData()
  440. {
  441. return
  442. !RemoteDirectory.IsEmpty() ||
  443. !LocalDirectory.IsEmpty() ||
  444. (Color != 0);
  445. }
  446. //---------------------------------------------------------------------
  447. void __fastcall TSessionData::CopyStateData(TSessionData * SourceData)
  448. {
  449. // Keep in sync with TCustomScpExplorerForm::UpdateSessionData.
  450. CopyDirectoriesStateData(SourceData);
  451. Color = SourceData->Color;
  452. }
  453. //---------------------------------------------------------------------
  454. void __fastcall TSessionData::CopyNonCoreData(TSessionData * SourceData)
  455. {
  456. CopyStateData(SourceData);
  457. UpdateDirectories = SourceData->UpdateDirectories;
  458. Note = SourceData->Note;
  459. }
  460. //---------------------------------------------------------------------
  461. bool __fastcall TSessionData::IsSame(const TSessionData * Default, bool AdvancedOnly, TStrings * DifferentProperties)
  462. {
  463. bool Result = true;
  464. #define PROPERTY(P) \
  465. if (P != Default->P) \
  466. { \
  467. if (DifferentProperties != NULL) \
  468. { \
  469. DifferentProperties->Add(#P); \
  470. } \
  471. Result = false; \
  472. }
  473. if (!AdvancedOnly)
  474. {
  475. BASE_PROPERTIES;
  476. META_PROPERTIES;
  477. }
  478. ADVANCED_PROPERTIES;
  479. #undef PROPERTY
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------
  483. bool __fastcall TSessionData::IsSame(const TSessionData * Default, bool AdvancedOnly)
  484. {
  485. return IsSame(Default, AdvancedOnly, NULL);
  486. }
  487. //---------------------------------------------------------------------
  488. bool __fastcall TSessionData::IsSameSite(const TSessionData * Other)
  489. {
  490. return
  491. (FSProtocol == Other->FSProtocol) &&
  492. (HostName == Other->HostName) &&
  493. (PortNumber == Other->PortNumber) &&
  494. (UserName == Other->UserName);
  495. }
  496. //---------------------------------------------------------------------
  497. bool __fastcall TSessionData::IsInFolderOrWorkspace(UnicodeString AFolder)
  498. {
  499. return StartsText(UnixIncludeTrailingBackslash(AFolder), Name);
  500. }
  501. //---------------------------------------------------------------------
  502. void __fastcall TSessionData::DoLoad(THierarchicalStorage * Storage, bool PuttyImport, bool & RewritePassword)
  503. {
  504. // Make sure we only ever use methods supported by TOptionsStorage
  505. // (implemented by TOptionsIniFile)
  506. PortNumber = Storage->ReadInteger(L"PortNumber", PortNumber);
  507. UserName = Storage->ReadString(L"UserName", UserName);
  508. // must be loaded after UserName, because HostName may be in format user@host
  509. HostName = Storage->ReadString(L"HostName", HostName);
  510. if (!Configuration->DisablePasswordStoring)
  511. {
  512. if (Storage->ValueExists(L"PasswordPlain"))
  513. {
  514. Password = Storage->ReadString(L"PasswordPlain", Password);
  515. RewritePassword = true;
  516. }
  517. else
  518. {
  519. FPassword = Storage->ReadStringAsBinaryData(L"Password", FPassword);
  520. }
  521. }
  522. HostKey = Storage->ReadString(L"HostKey", HostKey);
  523. Note = Storage->ReadString(L"Note", Note);
  524. // Putty uses PingIntervalSecs
  525. int PingIntervalSecs = Storage->ReadInteger(L"PingIntervalSecs", -1);
  526. if (PingIntervalSecs < 0)
  527. {
  528. PingIntervalSecs = Storage->ReadInteger(L"PingIntervalSec", PingInterval%SecsPerMin);
  529. }
  530. PingInterval =
  531. Storage->ReadInteger(L"PingInterval", PingInterval/SecsPerMin)*SecsPerMin +
  532. PingIntervalSecs;
  533. if (PingInterval == 0)
  534. {
  535. PingInterval = 30;
  536. }
  537. PingType = static_cast<TPingType>(Storage->ReadInteger(L"PingType", PingType));
  538. Timeout = Storage->ReadInteger(L"Timeout", Timeout);
  539. TryAgent = Storage->ReadBool(L"TryAgent", TryAgent);
  540. AgentFwd = Storage->ReadBool(L"AgentFwd", AgentFwd);
  541. AuthTIS = Storage->ReadBool(L"AuthTIS", AuthTIS);
  542. AuthKI = Storage->ReadBool(L"AuthKI", AuthKI);
  543. AuthKIPassword = Storage->ReadBool(L"AuthKIPassword", AuthKIPassword);
  544. // Continue to use setting keys of previous kerberos implementation (vaclav tomec),
  545. // but fallback to keys of other implementations (official putty and vintela quest putty),
  546. // to allow imports from all putty versions.
  547. // Both vaclav tomec and official putty use AuthGSSAPI
  548. AuthGSSAPI = Storage->ReadBool(L"AuthGSSAPI", Storage->ReadBool(L"AuthSSPI", AuthGSSAPI));
  549. GSSAPIFwdTGT = Storage->ReadBool(L"GSSAPIFwdTGT", Storage->ReadBool(L"GssapiFwd", Storage->ReadBool(L"SSPIFwdTGT", GSSAPIFwdTGT)));
  550. // KerbPrincipal was used by Quest PuTTY
  551. // GSSAPIServerRealm was used by Vaclav Tomec
  552. LogicalHostName = Storage->ReadString(L"LogicalHostName", Storage->ReadString(L"GSSAPIServerRealm", Storage->ReadString(L"KerbPrincipal", LogicalHostName)));
  553. ChangeUsername = Storage->ReadBool(L"ChangeUsername", ChangeUsername);
  554. Compression = Storage->ReadBool(L"Compression", Compression);
  555. TSshProt ASshProt = (TSshProt)Storage->ReadInteger(L"SshProt", SshProt);
  556. // Old sessions may contain the values correponding to the fallbacks we used to allow; migrate them
  557. if (ASshProt == ssh2deprecated)
  558. {
  559. ASshProt = ssh2only;
  560. }
  561. else if (ASshProt == ssh1deprecated)
  562. {
  563. ASshProt = ssh1only;
  564. }
  565. SshProt = ASshProt;
  566. Ssh2DES = Storage->ReadBool(L"Ssh2DES", Ssh2DES);
  567. SshNoUserAuth = Storage->ReadBool(L"SshNoUserAuth", SshNoUserAuth);
  568. CipherList = Storage->ReadString(L"Cipher", CipherList);
  569. KexList = Storage->ReadString(L"KEX", KexList);
  570. HostKeyList = Storage->ReadString(L"HostKey", HostKeyList);
  571. GssLibList = Storage->ReadString(L"GSSLibs", GssLibList);
  572. GssLibCustom = Storage->ReadString(L"GSSCustom", GssLibCustom);
  573. PublicKeyFile = Storage->ReadString(L"PublicKeyFile", PublicKeyFile);
  574. AddressFamily = static_cast<TAddressFamily>
  575. (Storage->ReadInteger(L"AddressFamily", AddressFamily));
  576. RekeyData = Storage->ReadString(L"RekeyBytes", RekeyData);
  577. RekeyTime = Storage->ReadInteger(L"RekeyTime", RekeyTime);
  578. FSProtocol = (TFSProtocol)Storage->ReadInteger(L"FSProtocol", FSProtocol);
  579. LocalDirectory = Storage->ReadString(L"LocalDirectory", LocalDirectory);
  580. RemoteDirectory = Storage->ReadString(L"RemoteDirectory", RemoteDirectory);
  581. SynchronizeBrowsing = Storage->ReadBool(L"SynchronizeBrowsing", SynchronizeBrowsing);
  582. UpdateDirectories = Storage->ReadBool(L"UpdateDirectories", UpdateDirectories);
  583. CacheDirectories = Storage->ReadBool(L"CacheDirectories", CacheDirectories);
  584. CacheDirectoryChanges = Storage->ReadBool(L"CacheDirectoryChanges", CacheDirectoryChanges);
  585. PreserveDirectoryChanges = Storage->ReadBool(L"PreserveDirectoryChanges", PreserveDirectoryChanges);
  586. ResolveSymlinks = Storage->ReadBool(L"ResolveSymlinks", ResolveSymlinks);
  587. FollowDirectorySymlinks = Storage->ReadBool(L"FollowDirectorySymlinks", FollowDirectorySymlinks);
  588. DSTMode = (TDSTMode)Storage->ReadInteger(L"ConsiderDST", DSTMode);
  589. LockInHome = Storage->ReadBool(L"LockInHome", LockInHome);
  590. Special = Storage->ReadBool(L"Special", Special);
  591. Shell = Storage->ReadString(L"Shell", Shell);
  592. ClearAliases = Storage->ReadBool(L"ClearAliases", ClearAliases);
  593. UnsetNationalVars = Storage->ReadBool(L"UnsetNationalVars", UnsetNationalVars);
  594. ListingCommand = Storage->ReadString(L"ListingCommand",
  595. Storage->ReadBool(L"AliasGroupList", false) ? UnicodeString(L"ls -gla") : ListingCommand);
  596. IgnoreLsWarnings = Storage->ReadBool(L"IgnoreLsWarnings", IgnoreLsWarnings);
  597. SCPLsFullTime = TAutoSwitch(Storage->ReadInteger(L"SCPLsFullTime", SCPLsFullTime));
  598. Scp1Compatibility = Storage->ReadBool(L"Scp1Compatibility", Scp1Compatibility);
  599. TimeDifference = Storage->ReadFloat(L"TimeDifference", TimeDifference);
  600. TimeDifferenceAuto = Storage->ReadBool(L"TimeDifferenceAuto", (TimeDifference == TDateTime()));
  601. DeleteToRecycleBin = Storage->ReadBool(L"DeleteToRecycleBin", DeleteToRecycleBin);
  602. OverwrittenToRecycleBin = Storage->ReadBool(L"OverwrittenToRecycleBin", OverwrittenToRecycleBin);
  603. RecycleBinPath = Storage->ReadString(L"RecycleBinPath", RecycleBinPath);
  604. PostLoginCommands = Storage->ReadString(L"PostLoginCommands", PostLoginCommands);
  605. ReturnVar = Storage->ReadString(L"ReturnVar", ReturnVar);
  606. LookupUserGroups = TAutoSwitch(Storage->ReadInteger(L"LookupUserGroups2", LookupUserGroups));
  607. EOLType = (TEOLType)Storage->ReadInteger(L"EOLType", EOLType);
  608. TrimVMSVersions = Storage->ReadBool(L"TrimVMSVersions", TrimVMSVersions);
  609. NotUtf = TAutoSwitch(Storage->ReadInteger(L"Utf", Storage->ReadInteger(L"SFTPUtfBug", NotUtf)));
  610. // PuTTY defaults to TcpNoDelay, but the psftp/pscp ignores this preference, and always set this to off (what is our default too)
  611. if (!PuttyImport)
  612. {
  613. TcpNoDelay = Storage->ReadBool(L"TcpNoDelay", TcpNoDelay);
  614. }
  615. SendBuf = Storage->ReadInteger(L"SendBuf", Storage->ReadInteger("SshSendBuf", SendBuf));
  616. SshSimple = Storage->ReadBool(L"SshSimple", SshSimple);
  617. ProxyMethod = (TProxyMethod)Storage->ReadInteger(L"ProxyMethod", ProxyMethod);
  618. ProxyHost = Storage->ReadString(L"ProxyHost", ProxyHost);
  619. ProxyPort = Storage->ReadInteger(L"ProxyPort", ProxyPort);
  620. ProxyUsername = Storage->ReadString(L"ProxyUsername", ProxyUsername);
  621. if (Storage->ValueExists(L"ProxyPassword"))
  622. {
  623. // encrypt unencrypted password
  624. ProxyPassword = Storage->ReadString(L"ProxyPassword", L"");
  625. }
  626. else
  627. {
  628. // load encrypted password
  629. FProxyPassword = Storage->ReadStringAsBinaryData(L"ProxyPasswordEnc", FProxyPassword);
  630. }
  631. if (ProxyMethod == pmCmd)
  632. {
  633. ProxyLocalCommand = Storage->ReadStringRaw(L"ProxyTelnetCommand", ProxyLocalCommand);
  634. }
  635. else
  636. {
  637. ProxyTelnetCommand = Storage->ReadStringRaw(L"ProxyTelnetCommand", ProxyTelnetCommand);
  638. }
  639. ProxyDNS = TAutoSwitch((Storage->ReadInteger(L"ProxyDNS", (ProxyDNS + 2) % 3) + 1) % 3);
  640. ProxyLocalhost = Storage->ReadBool(L"ProxyLocalhost", ProxyLocalhost);
  641. #define READ_BUG(BUG) \
  642. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger(L"Bug"#BUG, \
  643. 2 - Bug[sb##BUG]));
  644. READ_BUG(Ignore1);
  645. READ_BUG(PlainPW1);
  646. READ_BUG(RSA1);
  647. READ_BUG(HMAC2);
  648. READ_BUG(DeriveKey2);
  649. READ_BUG(RSAPad2);
  650. READ_BUG(PKSessID2);
  651. READ_BUG(Rekey2);
  652. READ_BUG(MaxPkt2);
  653. READ_BUG(Ignore2);
  654. READ_BUG(OldGex2);
  655. READ_BUG(WinAdj);
  656. READ_BUG(ChanReq);
  657. #undef READ_BUG
  658. if ((Bug[sbHMAC2] == asAuto) &&
  659. Storage->ReadBool(L"BuggyMAC", false))
  660. {
  661. Bug[sbHMAC2] = asOn;
  662. }
  663. SftpServer = Storage->ReadString(L"SftpServer", SftpServer);
  664. #define READ_SFTP_BUG(BUG) \
  665. SFTPBug[sb##BUG] = TAutoSwitch(Storage->ReadInteger(L"SFTP" #BUG "Bug", SFTPBug[sb##BUG]));
  666. READ_SFTP_BUG(Symlink);
  667. READ_SFTP_BUG(SignedTS);
  668. #undef READ_SFTP_BUG
  669. SFTPMaxVersion = Storage->ReadInteger(L"SFTPMaxVersion", SFTPMaxVersion);
  670. SFTPMaxPacketSize = Storage->ReadInteger(L"SFTPMaxPacketSize", SFTPMaxPacketSize);
  671. SFTPDownloadQueue = Storage->ReadInteger(L"SFTPDownloadQueue", SFTPDownloadQueue);
  672. SFTPUploadQueue = Storage->ReadInteger(L"SFTPUploadQueue", SFTPUploadQueue);
  673. SFTPListingQueue = Storage->ReadInteger(L"SFTPListingQueue", SFTPListingQueue);
  674. Color = Storage->ReadInteger(L"Color", Color);
  675. PuttyProtocol = Storage->ReadString(L"Protocol", PuttyProtocol);
  676. Tunnel = Storage->ReadBool(L"Tunnel", Tunnel);
  677. TunnelPortNumber = Storage->ReadInteger(L"TunnelPortNumber", TunnelPortNumber);
  678. TunnelUserName = Storage->ReadString(L"TunnelUserName", TunnelUserName);
  679. // must be loaded after TunnelUserName,
  680. // because TunnelHostName may be in format user@host
  681. TunnelHostName = Storage->ReadString(L"TunnelHostName", TunnelHostName);
  682. if (!Configuration->DisablePasswordStoring)
  683. {
  684. if (Storage->ValueExists(L"TunnelPasswordPlain"))
  685. {
  686. TunnelPassword = Storage->ReadString(L"TunnelPasswordPlain", TunnelPassword);
  687. RewritePassword = true;
  688. }
  689. else
  690. {
  691. FTunnelPassword = Storage->ReadStringAsBinaryData(L"TunnelPassword", FTunnelPassword);
  692. }
  693. }
  694. TunnelPublicKeyFile = Storage->ReadString(L"TunnelPublicKeyFile", TunnelPublicKeyFile);
  695. TunnelLocalPortNumber = Storage->ReadInteger(L"TunnelLocalPortNumber", TunnelLocalPortNumber);
  696. TunnelHostKey = Storage->ReadString(L"TunnelHostKey", TunnelHostKey);
  697. // Ftp prefix
  698. FtpPasvMode = Storage->ReadBool(L"FtpPasvMode", FtpPasvMode);
  699. FtpForcePasvIp = TAutoSwitch(Storage->ReadInteger(L"FtpForcePasvIp2", FtpForcePasvIp));
  700. FtpUseMlsd = TAutoSwitch(Storage->ReadInteger(L"FtpUseMlsd", FtpUseMlsd));
  701. FtpAccount = Storage->ReadString(L"FtpAccount", FtpAccount);
  702. FtpPingInterval = Storage->ReadInteger(L"FtpPingInterval", FtpPingInterval);
  703. FtpPingType = static_cast<TPingType>(Storage->ReadInteger(L"FtpPingType", FtpPingType));
  704. FtpTransferActiveImmediately = static_cast<TAutoSwitch>(Storage->ReadInteger(L"FtpTransferActiveImmediately2", FtpTransferActiveImmediately));
  705. Ftps = static_cast<TFtps>(Storage->ReadInteger(L"Ftps", Ftps));
  706. FtpListAll = TAutoSwitch(Storage->ReadInteger(L"FtpListAll", FtpListAll));
  707. FtpHost = TAutoSwitch(Storage->ReadInteger(L"FtpHost", FtpHost));
  708. FtpDeleteFromCwd = TAutoSwitch(Storage->ReadInteger(L"FtpDeleteFromCwd", FtpDeleteFromCwd));
  709. SslSessionReuse = Storage->ReadBool(L"SslSessionReuse", SslSessionReuse);
  710. TlsCertificateFile = Storage->ReadString(L"TlsCertificateFile", TlsCertificateFile);
  711. FtpProxyLogonType = Storage->ReadInteger(L"FtpProxyLogonType", FtpProxyLogonType);
  712. MinTlsVersion = static_cast<TTlsVersion>(Storage->ReadInteger(L"MinTlsVersion", MinTlsVersion));
  713. MaxTlsVersion = static_cast<TTlsVersion>(Storage->ReadInteger(L"MaxTlsVersion", MaxTlsVersion));
  714. IsWorkspace = Storage->ReadBool(L"IsWorkspace", IsWorkspace);
  715. Link = Storage->ReadString(L"Link", Link);
  716. CustomParam1 = Storage->ReadString(L"CustomParam1", CustomParam1);
  717. CustomParam2 = Storage->ReadString(L"CustomParam2", CustomParam2);
  718. #ifdef TEST
  719. #define KEX_TEST(VALUE, EXPECTED) KexList = VALUE; DebugAssert(KexList == EXPECTED);
  720. #define KEX_DEFAULT L"ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1"
  721. // Empty source should result in default list
  722. KEX_TEST(L"", KEX_DEFAULT);
  723. // Default of pre 5.8.1
  724. KEX_TEST(L"dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN", L"ecdh,dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN");
  725. // Missing first two priority algos, and last non-priority algo
  726. KEX_TEST(L"dh-group14-sha1,dh-group1-sha1,WARN", L"ecdh,dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN");
  727. // Missing first two priority algos, last non-priority algo and WARN
  728. KEX_TEST(L"dh-group14-sha1,dh-group1-sha1", L"ecdh,dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN");
  729. // Old algos, with all but the first below WARN
  730. KEX_TEST(L"dh-gex-sha1,WARN,dh-group14-sha1,dh-group1-sha1,rsa", L"ecdh,dh-gex-sha1,WARN,dh-group14-sha1,dh-group1-sha1,rsa");
  731. // Unknown algo at front
  732. KEX_TEST(L"unknown,ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1", KEX_DEFAULT);
  733. // Unknown algo at back
  734. KEX_TEST(L"ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1,unknown", KEX_DEFAULT);
  735. // Unknown algo in the middle
  736. KEX_TEST(L"ecdh,dh-gex-sha1,dh-group14-sha1,unknown,rsa,WARN,dh-group1-sha1", KEX_DEFAULT);
  737. #undef KEX_DEFAULT
  738. #undef KEX_TEST
  739. #define CIPHER_TEST(VALUE, EXPECTED) CipherList = VALUE; DebugAssert(CipherList == EXPECTED);
  740. #define CIPHER_DEFAULT L"aes,chacha20,blowfish,3des,WARN,arcfour,des"
  741. // Empty source should result in default list
  742. CIPHER_TEST(L"", CIPHER_DEFAULT);
  743. // Default of pre 5.8.1
  744. CIPHER_TEST(L"aes,blowfish,3des,WARN,arcfour,des", L"aes,blowfish,3des,chacha20,WARN,arcfour,des");
  745. // Missing priority algo
  746. CIPHER_TEST(L"chacha20,blowfish,3des,WARN,arcfour,des", CIPHER_DEFAULT);
  747. // Missing non-priority algo
  748. CIPHER_TEST(L"aes,chacha20,3des,WARN,arcfour,des", L"aes,chacha20,3des,blowfish,WARN,arcfour,des");
  749. // Missing last warn algo
  750. CIPHER_TEST(L"aes,blowfish,chacha20,3des,WARN,arcfour", L"aes,blowfish,chacha20,3des,WARN,arcfour,des");
  751. // Missing first warn algo
  752. CIPHER_TEST(L"aes,blowfish,chacha20,3des,WARN,des", L"aes,blowfish,chacha20,3des,WARN,des,arcfour");
  753. #undef CIPHER_DEFAULT
  754. #undef CIPHER_TEST
  755. #define HOSTKEY_TEST(VALUE, EXPECTED) HostKeyList = VALUE; DebugAssert(HostKeyList == EXPECTED);
  756. #define HOSTKEY_DEFAULT L"ed25519,ecdsa,rsa,dsa,WARN"
  757. // Empty source should result in default list
  758. HOSTKEY_TEST(L"", HOSTKEY_DEFAULT);
  759. // Missing priority algo
  760. HOSTKEY_TEST(L"ecdsa,rsa,dsa,WARN", HOSTKEY_DEFAULT);
  761. // Missing non-priority algo
  762. HOSTKEY_TEST(L"ed25519,ecdsa,dsa,WARN", L"ed25519,ecdsa,dsa,rsa,WARN");
  763. #undef HOSTKEY_DEFAULT
  764. #undef HOSTKEY_TEST
  765. #endif
  766. }
  767. //---------------------------------------------------------------------
  768. void __fastcall TSessionData::Load(THierarchicalStorage * Storage, bool PuttyImport)
  769. {
  770. bool RewritePassword = false;
  771. if (Storage->OpenSubKey(InternalStorageKey, False))
  772. {
  773. // In case we are re-loading, reset passwords, to avoid pointless
  774. // re-cryption, while loading username/hostname. And moreover, when
  775. // the password is wrongly encrypted (using a different master password),
  776. // this breaks sites reload and consequently an overal operation,
  777. // such as opening Sites menu
  778. ClearSessionPasswords();
  779. FProxyPassword = L"";
  780. DoLoad(Storage, PuttyImport, RewritePassword);
  781. Storage->CloseSubKey();
  782. }
  783. if (RewritePassword)
  784. {
  785. TStorageAccessMode AccessMode = Storage->AccessMode;
  786. Storage->AccessMode = smReadWrite;
  787. try
  788. {
  789. if (Storage->OpenSubKey(InternalStorageKey, true))
  790. {
  791. Storage->DeleteValue(L"PasswordPlain");
  792. if (!Password.IsEmpty())
  793. {
  794. Storage->WriteBinaryDataAsString(L"Password", FPassword);
  795. }
  796. Storage->DeleteValue(L"TunnelPasswordPlain");
  797. if (!TunnelPassword.IsEmpty())
  798. {
  799. Storage->WriteBinaryDataAsString(L"TunnelPassword", FTunnelPassword);
  800. }
  801. Storage->CloseSubKey();
  802. }
  803. }
  804. catch(...)
  805. {
  806. // ignore errors (like read-only INI file)
  807. }
  808. Storage->AccessMode = AccessMode;
  809. }
  810. FModified = false;
  811. FSource = ssStored;
  812. }
  813. //---------------------------------------------------------------------
  814. void __fastcall TSessionData::DoSave(THierarchicalStorage * Storage,
  815. bool PuttyExport, const TSessionData * Default, bool DoNotEncryptPasswords)
  816. {
  817. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  818. if ((Default != NULL) && (CONV(Default->PROPERTY) == CONV(PROPERTY))) \
  819. { \
  820. Storage->DeleteValue(NAME); \
  821. } \
  822. else \
  823. { \
  824. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  825. }
  826. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  827. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, TEXT(#PROPERTY), PROPERTY, )
  828. WRITE_DATA(String, HostName);
  829. WRITE_DATA(Integer, PortNumber);
  830. WRITE_DATA_EX(Integer, L"PingInterval", PingInterval / SecsPerMin, );
  831. WRITE_DATA_EX(Integer, L"PingIntervalSecs", PingInterval % SecsPerMin, );
  832. Storage->DeleteValue(L"PingIntervalSec"); // obsolete
  833. WRITE_DATA(Integer, PingType);
  834. WRITE_DATA(Integer, Timeout);
  835. WRITE_DATA(Bool, TryAgent);
  836. WRITE_DATA(Bool, AgentFwd);
  837. WRITE_DATA(Bool, AuthTIS);
  838. WRITE_DATA(Bool, AuthKI);
  839. WRITE_DATA(Bool, AuthKIPassword);
  840. WRITE_DATA(String, Note);
  841. WRITE_DATA(Bool, AuthGSSAPI);
  842. WRITE_DATA(Bool, GSSAPIFwdTGT);
  843. Storage->DeleteValue(L"TryGSSKEX");
  844. Storage->DeleteValue(L"UserNameFromEnvironment");
  845. Storage->DeleteValue("GSSAPIServerChoosesUserName");
  846. Storage->DeleteValue(L"GSSAPITrustDNS");
  847. WRITE_DATA(String, LogicalHostName);
  848. if (PuttyExport)
  849. {
  850. // duplicate kerberos setting with keys of the vintela quest putty
  851. WRITE_DATA_EX(Bool, L"AuthSSPI", AuthGSSAPI, );
  852. WRITE_DATA_EX(Bool, L"SSPIFwdTGT", GSSAPIFwdTGT, );
  853. WRITE_DATA_EX(String, L"KerbPrincipal", LogicalHostName, );
  854. // duplicate kerberos setting with keys of the official putty
  855. WRITE_DATA_EX(Bool, L"GssapiFwd", GSSAPIFwdTGT, );
  856. }
  857. WRITE_DATA(Bool, ChangeUsername);
  858. WRITE_DATA(Bool, Compression);
  859. WRITE_DATA(Integer, SshProt);
  860. WRITE_DATA(Bool, Ssh2DES);
  861. WRITE_DATA(Bool, SshNoUserAuth);
  862. WRITE_DATA_EX(String, L"Cipher", CipherList, );
  863. WRITE_DATA_EX(String, L"KEX", KexList, );
  864. WRITE_DATA_EX(String, L"HostKey", HostKeyList, );
  865. WRITE_DATA_EX(String, L"GSSLibs", GssLibList, );
  866. WRITE_DATA_EX(String, L"GSSCustom", GssLibCustom, );
  867. WRITE_DATA(Integer, AddressFamily);
  868. WRITE_DATA_EX(String, L"RekeyBytes", RekeyData, );
  869. WRITE_DATA(Integer, RekeyTime);
  870. WRITE_DATA(Bool, TcpNoDelay);
  871. if (PuttyExport)
  872. {
  873. WRITE_DATA(StringRaw, UserName);
  874. // PuTTY is started in its binary directory to allow relative paths when opening PuTTY's own stored session.
  875. // To allow relative paths in our sessions, we have to expand them for PuTTY.
  876. WRITE_DATA_EX(StringRaw, L"PublicKeyFile", PublicKeyFile, ExpandFileName);
  877. }
  878. else
  879. {
  880. WRITE_DATA(String, UserName);
  881. WRITE_DATA(String, PublicKeyFile);
  882. WRITE_DATA(Integer, FSProtocol);
  883. WRITE_DATA(String, LocalDirectory);
  884. WRITE_DATA(String, RemoteDirectory);
  885. WRITE_DATA(Bool, SynchronizeBrowsing);
  886. WRITE_DATA(Bool, UpdateDirectories);
  887. WRITE_DATA(Bool, CacheDirectories);
  888. WRITE_DATA(Bool, CacheDirectoryChanges);
  889. WRITE_DATA(Bool, PreserveDirectoryChanges);
  890. WRITE_DATA(Bool, ResolveSymlinks);
  891. WRITE_DATA(Bool, FollowDirectorySymlinks);
  892. WRITE_DATA_EX(Integer, L"ConsiderDST", DSTMode, );
  893. WRITE_DATA(Bool, LockInHome);
  894. // Special is never stored (if it would, login dialog must be modified not to
  895. // duplicate Special parameter when Special session is loaded and then stored
  896. // under different name)
  897. // WRITE_DATA(Bool, Special);
  898. WRITE_DATA(String, Shell);
  899. WRITE_DATA(Bool, ClearAliases);
  900. WRITE_DATA(Bool, UnsetNationalVars);
  901. WRITE_DATA(String, ListingCommand);
  902. WRITE_DATA(Bool, IgnoreLsWarnings);
  903. WRITE_DATA(Integer, SCPLsFullTime);
  904. WRITE_DATA(Bool, Scp1Compatibility);
  905. // TimeDifferenceAuto is valid for FTP protocol only.
  906. // For other protocols it's typically true (default value),
  907. // but ignored so TimeDifference is still taken into account (SCP only actually)
  908. if (TimeDifferenceAuto && (FSProtocol == fsFTP))
  909. {
  910. // Have to delete it as TimeDifferenceAuto is not saved when enabled,
  911. // but the default is derived from value of TimeDifference.
  912. Storage->DeleteValue(L"TimeDifference");
  913. }
  914. else
  915. {
  916. WRITE_DATA(Float, TimeDifference);
  917. }
  918. WRITE_DATA(Bool, TimeDifferenceAuto);
  919. WRITE_DATA(Bool, DeleteToRecycleBin);
  920. WRITE_DATA(Bool, OverwrittenToRecycleBin);
  921. WRITE_DATA(String, RecycleBinPath);
  922. WRITE_DATA(String, PostLoginCommands);
  923. WRITE_DATA(String, ReturnVar);
  924. WRITE_DATA_EX(Integer, L"LookupUserGroups2", LookupUserGroups, );
  925. WRITE_DATA(Integer, EOLType);
  926. WRITE_DATA(Bool, TrimVMSVersions);
  927. Storage->DeleteValue(L"SFTPUtfBug");
  928. WRITE_DATA_EX(Integer, L"Utf", NotUtf, );
  929. WRITE_DATA(Integer, SendBuf);
  930. WRITE_DATA(Bool, SshSimple);
  931. }
  932. WRITE_DATA(Integer, ProxyMethod);
  933. WRITE_DATA(String, ProxyHost);
  934. WRITE_DATA(Integer, ProxyPort);
  935. WRITE_DATA(String, ProxyUsername);
  936. if (ProxyMethod == pmCmd)
  937. {
  938. WRITE_DATA_EX(StringRaw, L"ProxyTelnetCommand", ProxyLocalCommand, );
  939. }
  940. else
  941. {
  942. WRITE_DATA(StringRaw, ProxyTelnetCommand);
  943. }
  944. #define WRITE_DATA_CONV_FUNC(X) (((X) + 2) % 3)
  945. WRITE_DATA_CONV(Integer, L"ProxyDNS", ProxyDNS);
  946. #undef WRITE_DATA_CONV_FUNC
  947. WRITE_DATA(Bool, ProxyLocalhost);
  948. #define WRITE_DATA_CONV_FUNC(X) (2 - (X))
  949. #define WRITE_BUG(BUG) WRITE_DATA_CONV(Integer, L"Bug" #BUG, Bug[sb##BUG]);
  950. WRITE_BUG(Ignore1);
  951. WRITE_BUG(PlainPW1);
  952. WRITE_BUG(RSA1);
  953. WRITE_BUG(HMAC2);
  954. WRITE_BUG(DeriveKey2);
  955. WRITE_BUG(RSAPad2);
  956. WRITE_BUG(PKSessID2);
  957. WRITE_BUG(Rekey2);
  958. WRITE_BUG(MaxPkt2);
  959. WRITE_BUG(Ignore2);
  960. WRITE_BUG(OldGex2);
  961. WRITE_BUG(WinAdj);
  962. WRITE_BUG(ChanReq);
  963. #undef WRITE_BUG
  964. #undef WRITE_DATA_CONV_FUNC
  965. Storage->DeleteValue(L"BuggyMAC");
  966. Storage->DeleteValue(L"AliasGroupList");
  967. if (PuttyExport)
  968. {
  969. WRITE_DATA_EX(String, L"Protocol", GetNormalizedPuttyProtocol(), );
  970. WRITE_DATA_EX(String, L"WinTitle", WinTitle, );
  971. }
  972. if (!PuttyExport)
  973. {
  974. WRITE_DATA(String, SftpServer);
  975. #define WRITE_SFTP_BUG(BUG) WRITE_DATA_EX(Integer, L"SFTP" #BUG "Bug", SFTPBug[sb##BUG], );
  976. WRITE_SFTP_BUG(Symlink);
  977. WRITE_SFTP_BUG(SignedTS);
  978. #undef WRITE_SFTP_BUG
  979. WRITE_DATA(Integer, SFTPMaxVersion);
  980. WRITE_DATA(Integer, SFTPMaxPacketSize);
  981. WRITE_DATA(Integer, SFTPDownloadQueue);
  982. WRITE_DATA(Integer, SFTPUploadQueue);
  983. WRITE_DATA(Integer, SFTPListingQueue);
  984. WRITE_DATA(Integer, Color);
  985. WRITE_DATA(Bool, Tunnel);
  986. WRITE_DATA(String, TunnelHostName);
  987. WRITE_DATA(Integer, TunnelPortNumber);
  988. WRITE_DATA(String, TunnelUserName);
  989. WRITE_DATA(String, TunnelPublicKeyFile);
  990. WRITE_DATA(Integer, TunnelLocalPortNumber);
  991. WRITE_DATA(Bool, FtpPasvMode);
  992. WRITE_DATA_EX(Integer, L"FtpForcePasvIp2", FtpForcePasvIp, );
  993. WRITE_DATA(Integer, FtpUseMlsd);
  994. WRITE_DATA(String, FtpAccount);
  995. WRITE_DATA(Integer, FtpPingInterval);
  996. WRITE_DATA(Integer, FtpPingType);
  997. WRITE_DATA_EX(Integer, L"FtpTransferActiveImmediately2", FtpTransferActiveImmediately, );
  998. WRITE_DATA(Integer, Ftps);
  999. WRITE_DATA(Integer, FtpListAll);
  1000. WRITE_DATA(Integer, FtpHost);
  1001. WRITE_DATA(Integer, FtpDeleteFromCwd);
  1002. WRITE_DATA(Bool, SslSessionReuse);
  1003. WRITE_DATA(String, TlsCertificateFile);
  1004. WRITE_DATA(Integer, FtpProxyLogonType);
  1005. WRITE_DATA(Integer, MinTlsVersion);
  1006. WRITE_DATA(Integer, MaxTlsVersion);
  1007. WRITE_DATA(Bool, IsWorkspace);
  1008. WRITE_DATA(String, Link);
  1009. WRITE_DATA(String, CustomParam1);
  1010. WRITE_DATA(String, CustomParam2);
  1011. }
  1012. SavePasswords(Storage, PuttyExport, DoNotEncryptPasswords);
  1013. }
  1014. //---------------------------------------------------------------------
  1015. TStrings * __fastcall TSessionData::SaveToOptions(const TSessionData * Default)
  1016. {
  1017. std::unique_ptr<TStringList> Options(new TStringList());
  1018. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  1019. DoSave(OptionsStorage.get(), false, Default, true);
  1020. return Options.release();
  1021. }
  1022. //---------------------------------------------------------------------
  1023. void __fastcall TSessionData::Save(THierarchicalStorage * Storage,
  1024. bool PuttyExport, const TSessionData * Default)
  1025. {
  1026. if (Storage->OpenSubKey(InternalStorageKey, true))
  1027. {
  1028. DoSave(Storage, PuttyExport, Default, false);
  1029. Storage->CloseSubKey();
  1030. }
  1031. }
  1032. //---------------------------------------------------------------------
  1033. UnicodeString __fastcall TSessionData::ReadXmlNode(_di_IXMLNode Node, const UnicodeString & Name, const UnicodeString & Default)
  1034. {
  1035. _di_IXMLNode TheNode = Node->ChildNodes->FindNode(Name);
  1036. UnicodeString Result;
  1037. if (TheNode != NULL)
  1038. {
  1039. Result = TheNode->Text.Trim();
  1040. }
  1041. if (Result.IsEmpty())
  1042. {
  1043. Result = Default;
  1044. }
  1045. return Result;
  1046. }
  1047. //---------------------------------------------------------------------
  1048. int __fastcall TSessionData::ReadXmlNode(_di_IXMLNode Node, const UnicodeString & Name, int Default)
  1049. {
  1050. _di_IXMLNode TheNode = Node->ChildNodes->FindNode(Name);
  1051. int Result;
  1052. if (TheNode != NULL)
  1053. {
  1054. Result = StrToIntDef(TheNode->Text.Trim(), Default);
  1055. }
  1056. else
  1057. {
  1058. Result = Default;
  1059. }
  1060. return Result;
  1061. }
  1062. //---------------------------------------------------------------------
  1063. _di_IXMLNode __fastcall TSessionData::FindSettingsNode(_di_IXMLNode Node, const UnicodeString & Name)
  1064. {
  1065. for (int Index = 0; Index < Node->ChildNodes->Count; Index++)
  1066. {
  1067. _di_IXMLNode ChildNode = Node->ChildNodes->Get(Index);
  1068. if (ChildNode->NodeName == L"Setting")
  1069. {
  1070. OleVariant SettingName = ChildNode->GetAttribute(L"name");
  1071. if (SettingName == Name)
  1072. {
  1073. return ChildNode;
  1074. }
  1075. }
  1076. }
  1077. return NULL;
  1078. }
  1079. //---------------------------------------------------------------------
  1080. UnicodeString __fastcall TSessionData::ReadSettingsNode(_di_IXMLNode Node, const UnicodeString & Name, const UnicodeString & Default)
  1081. {
  1082. _di_IXMLNode TheNode = FindSettingsNode(Node, Name);
  1083. UnicodeString Result;
  1084. if (TheNode != NULL)
  1085. {
  1086. Result = TheNode->Text.Trim();
  1087. }
  1088. if (Result.IsEmpty())
  1089. {
  1090. Result = Default;
  1091. }
  1092. return Result;
  1093. }
  1094. //---------------------------------------------------------------------
  1095. int __fastcall TSessionData::ReadSettingsNode(_di_IXMLNode Node, const UnicodeString & Name, int Default)
  1096. {
  1097. _di_IXMLNode TheNode = FindSettingsNode(Node, Name);
  1098. int Result;
  1099. if (TheNode != NULL)
  1100. {
  1101. Result = StrToIntDef(TheNode->Text.Trim(), Default);
  1102. }
  1103. else
  1104. {
  1105. Result = Default;
  1106. }
  1107. return Result;
  1108. }
  1109. //---------------------------------------------------------------------
  1110. void __fastcall TSessionData::ImportFromFilezilla(
  1111. _di_IXMLNode Node, const UnicodeString & Path, _di_IXMLNode SettingsNode)
  1112. {
  1113. Name = UnixIncludeTrailingBackslash(Path) + MakeValidName(ReadXmlNode(Node, L"Name", Name));
  1114. HostName = ReadXmlNode(Node, L"Host", HostName);
  1115. PortNumber = ReadXmlNode(Node, L"Port", PortNumber);
  1116. int AProtocol = ReadXmlNode(Node, L"Protocol", 0);
  1117. // ServerProtocol enum
  1118. switch (AProtocol)
  1119. {
  1120. case 0: // FTP
  1121. default: // UNKNOWN, HTTP, HTTPS, INSECURE_FTP
  1122. FSProtocol = fsFTP;
  1123. break;
  1124. case 1: // SFTP
  1125. FSProtocol = fsSFTP;
  1126. break;
  1127. case 3: // FTPS
  1128. FSProtocol = fsFTP;
  1129. Ftps = ftpsImplicit;
  1130. break;
  1131. case 4: // FTPES
  1132. FSProtocol = fsFTP;
  1133. Ftps = ftpsExplicitTls;
  1134. break;
  1135. }
  1136. // LogonType enum
  1137. int LogonType = ReadXmlNode(Node, L"Logontype", 0);
  1138. if (LogonType == 0) // ANONYMOUS
  1139. {
  1140. UserName = AnonymousUserName;
  1141. Password = AnonymousPassword;
  1142. }
  1143. else
  1144. {
  1145. UserName = ReadXmlNode(Node, L"User", UserName);
  1146. FtpAccount = ReadXmlNode(Node, L"Account", FtpAccount);
  1147. _di_IXMLNode PassNode = Node->ChildNodes->FindNode(L"Pass");
  1148. if (PassNode != NULL)
  1149. {
  1150. UnicodeString APassword = PassNode->Text.Trim();
  1151. OleVariant EncodingValue = PassNode->GetAttribute(L"encoding");
  1152. if (!EncodingValue.IsNull())
  1153. {
  1154. UnicodeString EncodingValueStr = EncodingValue;
  1155. if (SameText(EncodingValueStr, L"base64"))
  1156. {
  1157. TBytes Bytes = DecodeBase64(APassword);
  1158. APassword = TEncoding::UTF8->GetString(Bytes);
  1159. }
  1160. }
  1161. Password = APassword;
  1162. }
  1163. }
  1164. int DefaultTimeDifference = TimeToSeconds(TimeDifference);
  1165. TimeDifference =
  1166. (double(ReadXmlNode(Node, L"TimezoneOffset", DefaultTimeDifference) / SecsPerDay));
  1167. TimeDifferenceAuto = (TimeDifference == TDateTime());
  1168. UnicodeString PasvMode = ReadXmlNode(Node, L"PasvMode", L"");
  1169. if (SameText(PasvMode, L"MODE_PASSIVE"))
  1170. {
  1171. FtpPasvMode = true;
  1172. }
  1173. else if (SameText(PasvMode, L"MODE_ACTIVE"))
  1174. {
  1175. FtpPasvMode = false;
  1176. }
  1177. UnicodeString EncodingType = ReadXmlNode(Node, L"EncodingType", L"");
  1178. if (SameText(EncodingType, L"Auto"))
  1179. {
  1180. NotUtf = asAuto;
  1181. }
  1182. else if (SameText(EncodingType, L"UTF-8"))
  1183. {
  1184. NotUtf = asOff;
  1185. }
  1186. // todo PostLoginCommands
  1187. Note = ReadXmlNode(Node, L"Comments", Note);
  1188. LocalDirectory = ReadXmlNode(Node, L"LocalDir", LocalDirectory);
  1189. UnicodeString RemoteDir = ReadXmlNode(Node, L"RemoteDir", L"");
  1190. if (!RemoteDir.IsEmpty())
  1191. {
  1192. CutToChar(RemoteDir, L' ', false); // type
  1193. int PrefixSize = StrToIntDef(CutToChar(RemoteDir, L' ', false), 0); // prefix size
  1194. if (PrefixSize > 0)
  1195. {
  1196. RemoteDir.Delete(1, PrefixSize);
  1197. }
  1198. RemoteDirectory = L"/";
  1199. while (!RemoteDir.IsEmpty())
  1200. {
  1201. int SegmentSize = StrToIntDef(CutToChar(RemoteDir, L' ', false), 0);
  1202. UnicodeString Segment = RemoteDir.SubString(1, SegmentSize);
  1203. RemoteDirectory = UnixIncludeTrailingBackslash(RemoteDirectory) + Segment;
  1204. RemoteDir.Delete(1, SegmentSize + 1);
  1205. }
  1206. }
  1207. SynchronizeBrowsing = (ReadXmlNode(Node, L"SyncBrowsing", SynchronizeBrowsing ? 1 : 0) != 0);
  1208. if (SettingsNode != NULL)
  1209. {
  1210. if (UsesSsh)
  1211. {
  1212. UnicodeString KeyFiles = ReadSettingsNode(SettingsNode, L"SFTP keyfiles", UnicodeString());
  1213. UnicodeString KeyFile = CutToChar(KeyFiles, L'\n', true).Trim();
  1214. KeyFiles = KeyFiles.Trim();
  1215. // If there are more keys, ignore them, as we do not know which one to use
  1216. if (!KeyFile.IsEmpty() && KeyFiles.IsEmpty())
  1217. {
  1218. PublicKeyFile = KeyFile;
  1219. }
  1220. }
  1221. bool BypassProxy = (ReadXmlNode(Node, L"BypassProxy", 0) != 0);
  1222. if (!BypassProxy)
  1223. {
  1224. int FtpProxyType = ReadSettingsNode(SettingsNode, L"FTP Proxy type", -1);
  1225. if (FtpProxyType > 0)
  1226. {
  1227. switch (FtpProxyType)
  1228. {
  1229. case 1:
  1230. FtpProxyLogonType = 2;
  1231. break;
  1232. case 2:
  1233. FtpProxyLogonType = 1;
  1234. break;
  1235. case 3:
  1236. FtpProxyLogonType = 3;
  1237. break;
  1238. case 4:
  1239. // custom
  1240. // TODO: map known sequences to our enumeration
  1241. FtpProxyLogonType = 0;
  1242. break;
  1243. default:
  1244. DebugFail();
  1245. FtpProxyLogonType = 0;
  1246. break;
  1247. }
  1248. ProxyHost = ReadSettingsNode(SettingsNode, L"FTP Proxy host", ProxyHost);
  1249. ProxyUsername = ReadSettingsNode(SettingsNode, L"FTP Proxy user", ProxyUsername);
  1250. ProxyPassword = ReadSettingsNode(SettingsNode, L"FTP Proxy password", ProxyPassword);
  1251. // ProxyPort is not used with FtpProxyLogonType
  1252. }
  1253. else
  1254. {
  1255. int ProxyType = ReadSettingsNode(SettingsNode, L"Proxy type", -1);
  1256. if (ProxyType >= 0)
  1257. {
  1258. switch (ProxyType)
  1259. {
  1260. case 0:
  1261. ProxyMethod = ::pmNone;
  1262. break;
  1263. case 1:
  1264. ProxyMethod = pmHTTP;
  1265. break;
  1266. case 2:
  1267. ProxyMethod = pmSocks5;
  1268. break;
  1269. case 3:
  1270. ProxyMethod = pmSocks4;
  1271. break;
  1272. default:
  1273. DebugFail();
  1274. ProxyMethod = ::pmNone;
  1275. break;
  1276. }
  1277. ProxyHost = ReadSettingsNode(SettingsNode, L"Proxy host", ProxyHost);
  1278. ProxyPort = ReadSettingsNode(SettingsNode, L"Proxy port", ProxyPort);
  1279. ProxyUsername = ReadSettingsNode(SettingsNode, L"Proxy user", ProxyUsername);
  1280. ProxyPassword = ReadSettingsNode(SettingsNode, L"Proxy password", ProxyPassword);
  1281. }
  1282. }
  1283. }
  1284. }
  1285. }
  1286. //---------------------------------------------------------------------
  1287. void __fastcall TSessionData::SavePasswords(THierarchicalStorage * Storage, bool PuttyExport, bool DoNotEncryptPasswords)
  1288. {
  1289. if (!Configuration->DisablePasswordStoring && !PuttyExport && !FPassword.IsEmpty())
  1290. {
  1291. // DoNotEncryptPasswords is set when called from GenerateOpenCommandArgs only
  1292. // and it never saves session password
  1293. DebugAssert(!DoNotEncryptPasswords);
  1294. Storage->WriteBinaryDataAsString(L"Password", StronglyRecryptPassword(FPassword, UserName+HostName));
  1295. }
  1296. else
  1297. {
  1298. Storage->DeleteValue(L"Password");
  1299. }
  1300. Storage->DeleteValue(L"PasswordPlain");
  1301. if (PuttyExport)
  1302. {
  1303. // save password unencrypted
  1304. Storage->WriteString(L"ProxyPassword", ProxyPassword);
  1305. }
  1306. else
  1307. {
  1308. if (DoNotEncryptPasswords)
  1309. {
  1310. if (!FProxyPassword.IsEmpty())
  1311. {
  1312. Storage->WriteString(L"ProxyPassword", ProxyPassword);
  1313. }
  1314. else
  1315. {
  1316. Storage->DeleteValue(L"ProxyPassword");
  1317. }
  1318. Storage->DeleteValue(L"ProxyPasswordEnc");
  1319. }
  1320. else
  1321. {
  1322. // save password encrypted
  1323. if (!FProxyPassword.IsEmpty())
  1324. {
  1325. Storage->WriteBinaryDataAsString(L"ProxyPasswordEnc", StronglyRecryptPassword(FProxyPassword, ProxyUsername+ProxyHost));
  1326. }
  1327. else
  1328. {
  1329. Storage->DeleteValue(L"ProxyPasswordEnc");
  1330. }
  1331. Storage->DeleteValue(L"ProxyPassword");
  1332. }
  1333. if (DoNotEncryptPasswords)
  1334. {
  1335. if (!FTunnelPassword.IsEmpty())
  1336. {
  1337. Storage->WriteString(L"TunnelPasswordPlain", TunnelPassword);
  1338. }
  1339. else
  1340. {
  1341. Storage->DeleteValue(L"TunnelPasswordPlain");
  1342. }
  1343. }
  1344. else
  1345. {
  1346. if (!Configuration->DisablePasswordStoring && !FTunnelPassword.IsEmpty())
  1347. {
  1348. Storage->WriteBinaryDataAsString(L"TunnelPassword", StronglyRecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName));
  1349. }
  1350. else
  1351. {
  1352. Storage->DeleteValue(L"TunnelPassword");
  1353. }
  1354. }
  1355. }
  1356. }
  1357. //---------------------------------------------------------------------
  1358. void __fastcall TSessionData::RecryptPasswords()
  1359. {
  1360. Password = Password;
  1361. NewPassword = NewPassword;
  1362. ProxyPassword = ProxyPassword;
  1363. TunnelPassword = TunnelPassword;
  1364. Passphrase = Passphrase;
  1365. }
  1366. //---------------------------------------------------------------------
  1367. bool __fastcall TSessionData::HasPassword()
  1368. {
  1369. return !FPassword.IsEmpty();
  1370. }
  1371. //---------------------------------------------------------------------
  1372. bool __fastcall TSessionData::HasAnySessionPassword()
  1373. {
  1374. return HasPassword() || !FTunnelPassword.IsEmpty();
  1375. }
  1376. //---------------------------------------------------------------------
  1377. bool __fastcall TSessionData::HasAnyPassword()
  1378. {
  1379. return
  1380. HasAnySessionPassword() ||
  1381. !FProxyPassword.IsEmpty() ||
  1382. // will probably be never used
  1383. FNewPassword.IsEmpty();
  1384. }
  1385. //---------------------------------------------------------------------
  1386. void __fastcall TSessionData::ClearSessionPasswords()
  1387. {
  1388. FPassword = L"";
  1389. FNewPassword = L"";
  1390. FTunnelPassword = L"";
  1391. }
  1392. //---------------------------------------------------------------------
  1393. void __fastcall TSessionData::Modify()
  1394. {
  1395. FModified = true;
  1396. if (FSource == ssStored)
  1397. {
  1398. FSource = ssStoredModified;
  1399. }
  1400. }
  1401. //---------------------------------------------------------------------
  1402. UnicodeString __fastcall TSessionData::GetSource()
  1403. {
  1404. switch (FSource)
  1405. {
  1406. case ::ssNone:
  1407. return L"Ad-Hoc site";
  1408. case ssStored:
  1409. return L"Site";
  1410. case ssStoredModified:
  1411. return L"Modified site";
  1412. default:
  1413. DebugFail();
  1414. return L"";
  1415. }
  1416. }
  1417. //---------------------------------------------------------------------
  1418. void __fastcall TSessionData::SaveRecryptedPasswords(THierarchicalStorage * Storage)
  1419. {
  1420. if (Storage->OpenSubKey(InternalStorageKey, true))
  1421. {
  1422. try
  1423. {
  1424. RecryptPasswords();
  1425. SavePasswords(Storage, false, false);
  1426. }
  1427. __finally
  1428. {
  1429. Storage->CloseSubKey();
  1430. }
  1431. }
  1432. }
  1433. //---------------------------------------------------------------------
  1434. void __fastcall TSessionData::Remove()
  1435. {
  1436. bool SessionList = true;
  1437. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  1438. try
  1439. {
  1440. Storage->Explicit = true;
  1441. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  1442. {
  1443. Storage->RecursiveDeleteSubKey(InternalStorageKey);
  1444. }
  1445. }
  1446. __finally
  1447. {
  1448. delete Storage;
  1449. }
  1450. }
  1451. //---------------------------------------------------------------------
  1452. void __fastcall TSessionData::CacheHostKeyIfNotCached()
  1453. {
  1454. UnicodeString KeyType = KeyTypeFromFingerprint(HostKey);
  1455. // Should allow importing to INI file as ImportHostKeys
  1456. UnicodeString TargetKey = Configuration->RegistryStorageKey + L"\\" + Configuration->SshHostKeysSubKey;
  1457. std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(TargetKey));
  1458. Storage->AccessMode = smReadWrite;
  1459. if (Storage->OpenRootKey(true))
  1460. {
  1461. UnicodeString HostKeyName = PuttyMungeStr(FORMAT(L"%s@%d:%s", (KeyType, PortNumber, HostName)));
  1462. if (!Storage->ValueExists(HostKeyName))
  1463. {
  1464. // fingerprint is MD5 of host key, so it cannot be translated back to host key,
  1465. // so we store fingerprint and TSecureShell::VerifyHostKey was
  1466. // modified to accept also fingerprint
  1467. Storage->WriteString(HostKeyName, HostKey);
  1468. }
  1469. }
  1470. }
  1471. //---------------------------------------------------------------------
  1472. inline void __fastcall MoveStr(UnicodeString & Source, UnicodeString * Dest, int Count)
  1473. {
  1474. if (Dest != NULL)
  1475. {
  1476. (*Dest) += Source.SubString(1, Count);
  1477. }
  1478. Source.Delete(1, Count);
  1479. }
  1480. //---------------------------------------------------------------------
  1481. bool __fastcall TSessionData::DoIsProtocolUrl(
  1482. const UnicodeString & Url, const UnicodeString & Protocol, int & ProtocolLen)
  1483. {
  1484. bool Result = SameText(Url.SubString(1, Protocol.Length() + 1), Protocol + L":");
  1485. if (Result)
  1486. {
  1487. ProtocolLen = Protocol.Length() + 1;
  1488. }
  1489. return Result;
  1490. }
  1491. //---------------------------------------------------------------------
  1492. bool __fastcall TSessionData::IsProtocolUrl(
  1493. const UnicodeString & Url, const UnicodeString & Protocol, int & ProtocolLen)
  1494. {
  1495. return
  1496. DoIsProtocolUrl(Url, Protocol, ProtocolLen) ||
  1497. DoIsProtocolUrl(Url, WinSCPProtocolPrefix + Protocol, ProtocolLen);
  1498. }
  1499. //---------------------------------------------------------------------
  1500. bool __fastcall TSessionData::IsSensitiveOption(const UnicodeString & Option)
  1501. {
  1502. return
  1503. SameText(Option, PassphraseOption) ||
  1504. SameText(Option, NEWPASSWORD_SWITCH);
  1505. }
  1506. //---------------------------------------------------------------------
  1507. bool __fastcall TSessionData::ParseUrl(UnicodeString Url, TOptions * Options,
  1508. TStoredSessionList * StoredSessions, bool & DefaultsOnly, UnicodeString * FileName,
  1509. bool * AProtocolDefined, UnicodeString * MaskedUrl)
  1510. {
  1511. bool ProtocolDefined = false;
  1512. bool PortNumberDefined = false;
  1513. TFSProtocol AFSProtocol;
  1514. int APortNumber;
  1515. TFtps AFtps = ftpsNone;
  1516. int ProtocolLen = 0;
  1517. if (IsProtocolUrl(Url, ScpProtocol, ProtocolLen))
  1518. {
  1519. AFSProtocol = fsSCPonly;
  1520. APortNumber = SshPortNumber;
  1521. MoveStr(Url, MaskedUrl, ProtocolLen);
  1522. ProtocolDefined = true;
  1523. }
  1524. else if (IsProtocolUrl(Url, SftpProtocol, ProtocolLen))
  1525. {
  1526. AFSProtocol = fsSFTPonly;
  1527. APortNumber = SshPortNumber;
  1528. MoveStr(Url, MaskedUrl, ProtocolLen);
  1529. ProtocolDefined = true;
  1530. }
  1531. else if (IsProtocolUrl(Url, FtpProtocol, ProtocolLen))
  1532. {
  1533. AFSProtocol = fsFTP;
  1534. Ftps = ftpsNone;
  1535. APortNumber = FtpPortNumber;
  1536. MoveStr(Url, MaskedUrl, ProtocolLen);
  1537. ProtocolDefined = true;
  1538. }
  1539. else if (IsProtocolUrl(Url, FtpsProtocol, ProtocolLen))
  1540. {
  1541. AFSProtocol = fsFTP;
  1542. AFtps = ftpsImplicit;
  1543. APortNumber = FtpsImplicitPortNumber;
  1544. MoveStr(Url, MaskedUrl, ProtocolLen);
  1545. ProtocolDefined = true;
  1546. }
  1547. else if (IsProtocolUrl(Url, FtpesProtocol, ProtocolLen))
  1548. {
  1549. AFSProtocol = fsFTP;
  1550. AFtps = ftpsExplicitTls;
  1551. APortNumber = FtpPortNumber;
  1552. MoveStr(Url, MaskedUrl, ProtocolLen);
  1553. ProtocolDefined = true;
  1554. }
  1555. else if (IsProtocolUrl(Url, WebDAVProtocol, ProtocolLen) ||
  1556. IsProtocolUrl(Url, HttpProtocol, ProtocolLen))
  1557. {
  1558. AFSProtocol = fsWebDAV;
  1559. AFtps = ftpsNone;
  1560. APortNumber = HTTPPortNumber;
  1561. MoveStr(Url, MaskedUrl, ProtocolLen);
  1562. ProtocolDefined = true;
  1563. }
  1564. else if (IsProtocolUrl(Url, WebDAVSProtocol, ProtocolLen) ||
  1565. IsProtocolUrl(Url, HttpsProtocol, ProtocolLen))
  1566. {
  1567. AFSProtocol = fsWebDAV;
  1568. AFtps = ftpsImplicit;
  1569. APortNumber = HTTPSPortNumber;
  1570. MoveStr(Url, MaskedUrl, ProtocolLen);
  1571. ProtocolDefined = true;
  1572. }
  1573. else if (IsProtocolUrl(Url, S3Protocol, ProtocolLen))
  1574. {
  1575. AFSProtocol = fsS3;
  1576. AFtps = ftpsImplicit;
  1577. APortNumber = HTTPSPortNumber;
  1578. MoveStr(Url, MaskedUrl, ProtocolLen);
  1579. ProtocolDefined = true;
  1580. }
  1581. else if (IsProtocolUrl(Url, SshProtocol, ProtocolLen))
  1582. {
  1583. // For most uses, handling ssh:// the same way as sftp://
  1584. // The only place where a difference is made is GetLoginData() in WinMain.cpp
  1585. AFSProtocol = fsSFTPonly;
  1586. PuttyProtocol = PuttySshProtocol;
  1587. APortNumber = SshPortNumber;
  1588. MoveStr(Url, MaskedUrl, ProtocolLen);
  1589. ProtocolDefined = true;
  1590. }
  1591. if (ProtocolDefined && (Url.SubString(1, 2) == L"//"))
  1592. {
  1593. MoveStr(Url, MaskedUrl, 2);
  1594. }
  1595. if (AProtocolDefined != NULL)
  1596. {
  1597. *AProtocolDefined = ProtocolDefined;
  1598. }
  1599. if (!Url.IsEmpty())
  1600. {
  1601. UnicodeString DecodedUrl = DecodeUrlChars(Url);
  1602. // lookup stored session even if protocol was defined
  1603. // (this allows setting for example default username for host
  1604. // by creating stored session named by host)
  1605. TSessionData * Data = NULL;
  1606. // When using to paste URL on Login dialog, we do not want to lookup the stored sites
  1607. if (StoredSessions != NULL)
  1608. {
  1609. // this can be optimized as the list is sorted
  1610. for (Integer Index = 0; Index < StoredSessions->CountIncludingHidden; Index++)
  1611. {
  1612. TSessionData * AData = (TSessionData *)StoredSessions->Items[Index];
  1613. if (!AData->IsWorkspace)
  1614. {
  1615. bool Match = false;
  1616. // Comparison optimizations as this is called many times
  1617. // e.g. when updating jumplist
  1618. if ((AData->Name.Length() == DecodedUrl.Length()) &&
  1619. SameText(AData->Name, DecodedUrl))
  1620. {
  1621. Match = true;
  1622. }
  1623. else if ((AData->Name.Length() < DecodedUrl.Length()) &&
  1624. (DecodedUrl[AData->Name.Length() + 1] == L'/') &&
  1625. // StrLIComp is an equivalent of SameText
  1626. (StrLIComp(AData->Name.c_str(), DecodedUrl.c_str(), AData->Name.Length()) == 0))
  1627. {
  1628. Match = true;
  1629. }
  1630. if (Match)
  1631. {
  1632. Data = AData;
  1633. break;
  1634. }
  1635. }
  1636. }
  1637. }
  1638. UnicodeString ARemoteDirectory;
  1639. if (Data != NULL)
  1640. {
  1641. Assign(Data);
  1642. int P = 1;
  1643. while (!AnsiSameText(DecodeUrlChars(Url.SubString(1, P)), Data->Name))
  1644. {
  1645. P++;
  1646. DebugAssert(P <= Url.Length());
  1647. }
  1648. ARemoteDirectory = Url.SubString(P + 1, Url.Length() - P);
  1649. if (Data->Hidden)
  1650. {
  1651. Data->Remove();
  1652. StoredSessions->Remove(Data);
  1653. // only modified, implicit
  1654. StoredSessions->Save(false, false);
  1655. }
  1656. if (MaskedUrl != NULL)
  1657. {
  1658. (*MaskedUrl) += Url;
  1659. }
  1660. }
  1661. else
  1662. {
  1663. // This happens when pasting URL on Login dialog
  1664. if (StoredSessions != NULL)
  1665. {
  1666. CopyData(StoredSessions->DefaultSettings);
  1667. }
  1668. Name = L"";
  1669. int PSlash = Url.Pos(L"/");
  1670. if (PSlash == 0)
  1671. {
  1672. PSlash = Url.Length() + 1;
  1673. }
  1674. UnicodeString ConnectInfo = Url.SubString(1, PSlash - 1);
  1675. int P = ConnectInfo.LastDelimiter(L"@");
  1676. UnicodeString UserInfo;
  1677. UnicodeString HostInfo;
  1678. if (P > 0)
  1679. {
  1680. UserInfo = ConnectInfo.SubString(1, P - 1);
  1681. HostInfo = ConnectInfo.SubString(P + 1, ConnectInfo.Length() - P);
  1682. }
  1683. else
  1684. {
  1685. HostInfo = ConnectInfo;
  1686. }
  1687. UnicodeString OrigHostInfo = HostInfo;
  1688. if ((HostInfo.Length() >= 2) && (HostInfo[1] == L'[') && ((P = HostInfo.Pos(L"]")) > 0))
  1689. {
  1690. HostName = HostInfo.SubString(2, P - 2);
  1691. HostInfo.Delete(1, P);
  1692. if (!HostInfo.IsEmpty() && (HostInfo[1] == L':'))
  1693. {
  1694. HostInfo.Delete(1, 1);
  1695. }
  1696. }
  1697. else
  1698. {
  1699. HostName = DecodeUrlChars(CutToChar(HostInfo, L':', true));
  1700. }
  1701. // expanded from ?: operator, as it caused strange "access violation" errors
  1702. if (!HostInfo.IsEmpty())
  1703. {
  1704. PortNumber = StrToIntDef(DecodeUrlChars(HostInfo), -1);
  1705. PortNumberDefined = true;
  1706. }
  1707. else if (ProtocolDefined)
  1708. {
  1709. PortNumber = APortNumber;
  1710. }
  1711. if (ProtocolDefined)
  1712. {
  1713. Ftps = AFtps;
  1714. }
  1715. UnicodeString UserInfoWithoutConnectionParams = CutToChar(UserInfo, UrlParamSeparator, false);
  1716. UnicodeString ConnectionParams = UserInfo;
  1717. UserInfo = UserInfoWithoutConnectionParams;
  1718. while (!ConnectionParams.IsEmpty())
  1719. {
  1720. UnicodeString ConnectionParam = CutToChar(ConnectionParams, UrlParamSeparator, false);
  1721. UnicodeString ConnectionParamName = CutToChar(ConnectionParam, UrlParamValueSeparator, false);
  1722. if (SameText(ConnectionParamName, UrlHostKeyParamName))
  1723. {
  1724. HostKey = ConnectionParam;
  1725. FOverrideCachedHostKey = false;
  1726. }
  1727. }
  1728. UnicodeString RawUserName = CutToChar(UserInfo, L':', false);
  1729. UserName = DecodeUrlChars(RawUserName);
  1730. Password = DecodeUrlChars(UserInfo);
  1731. UnicodeString RemoteDirectoryWithSessionParams = Url.SubString(PSlash, Url.Length() - PSlash + 1);
  1732. ARemoteDirectory = CutToChar(RemoteDirectoryWithSessionParams, UrlParamSeparator, false);
  1733. UnicodeString SessionParams = RemoteDirectoryWithSessionParams;
  1734. // We should handle session params in "stored session" branch too.
  1735. // And particularly if there's a "save" param, we should actually not try to match the
  1736. // URL against site names
  1737. while (!SessionParams.IsEmpty())
  1738. {
  1739. UnicodeString SessionParam = CutToChar(SessionParams, UrlParamSeparator, false);
  1740. UnicodeString SessionParamName = CutToChar(SessionParam, UrlParamValueSeparator, false);
  1741. if (SameText(SessionParamName, UrlSaveParamName))
  1742. {
  1743. FSaveOnly = (StrToIntDef(SessionParam, 1) != 0);
  1744. }
  1745. }
  1746. if (MaskedUrl != NULL)
  1747. {
  1748. (*MaskedUrl) += RawUserName;
  1749. if (!UserInfo.IsEmpty())
  1750. {
  1751. (*MaskedUrl) += L":" + PasswordMask;
  1752. }
  1753. if (!RawUserName.IsEmpty() || !UserInfo.IsEmpty())
  1754. {
  1755. (*MaskedUrl) += L"@";
  1756. }
  1757. (*MaskedUrl) += OrigHostInfo + ARemoteDirectory;
  1758. }
  1759. }
  1760. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory != L"/"))
  1761. {
  1762. if ((ARemoteDirectory[ARemoteDirectory.Length()] != L'/') &&
  1763. (FileName != NULL))
  1764. {
  1765. *FileName = DecodeUrlChars(UnixExtractFileName(ARemoteDirectory));
  1766. ARemoteDirectory = UnixExtractFilePath(ARemoteDirectory);
  1767. }
  1768. RemoteDirectory = DecodeUrlChars(ARemoteDirectory);
  1769. }
  1770. DefaultsOnly = false;
  1771. }
  1772. else
  1773. {
  1774. // This happens when pasting URL on Login dialog
  1775. if (StoredSessions != NULL)
  1776. {
  1777. CopyData(StoredSessions->DefaultSettings);
  1778. }
  1779. DefaultsOnly = true;
  1780. }
  1781. if (ProtocolDefined)
  1782. {
  1783. FSProtocol = AFSProtocol;
  1784. }
  1785. if (Options != NULL)
  1786. {
  1787. // we deliberately do keep defaultonly to false, in presence of any option,
  1788. // as the option should not make session "connectable"
  1789. UnicodeString Value;
  1790. if (Options->FindSwitch(SESSIONNAME_SWICH, Value))
  1791. {
  1792. Name = Value;
  1793. }
  1794. if (Options->FindSwitch(NEWPASSWORD_SWITCH, Value))
  1795. {
  1796. ChangePassword = true;
  1797. NewPassword = Value;
  1798. }
  1799. if (Options->FindSwitch(L"privatekey", Value))
  1800. {
  1801. PublicKeyFile = Value;
  1802. }
  1803. if (Options->FindSwitch(L"clientcert", Value))
  1804. {
  1805. TlsCertificateFile = Value;
  1806. }
  1807. if (Options->FindSwitch(PassphraseOption, Value))
  1808. {
  1809. Passphrase = Value;
  1810. }
  1811. if (Options->FindSwitch(L"timeout", Value))
  1812. {
  1813. Timeout = StrToInt(Value);
  1814. }
  1815. if (Options->FindSwitch(L"hostkey", Value) ||
  1816. Options->FindSwitch(L"certificate", Value))
  1817. {
  1818. HostKey = Value;
  1819. FOverrideCachedHostKey = true;
  1820. }
  1821. FtpPasvMode = Options->SwitchValue(L"passive", FtpPasvMode);
  1822. if (Options->FindSwitch(L"implicit"))
  1823. {
  1824. bool Enabled = Options->SwitchValue(L"implicit", true);
  1825. Ftps = Enabled ? ftpsImplicit : ftpsNone;
  1826. if (!PortNumberDefined && Enabled)
  1827. {
  1828. PortNumber = FtpsImplicitPortNumber;
  1829. }
  1830. }
  1831. // BACKWARD COMPATIBILITY with 5.5.x
  1832. if (Options->FindSwitch(L"explicitssl"))
  1833. {
  1834. bool Enabled = Options->SwitchValue(L"explicitssl", true);
  1835. Ftps = Enabled ? ftpsExplicitSsl : ftpsNone;
  1836. if (!PortNumberDefined && Enabled)
  1837. {
  1838. PortNumber = FtpPortNumber;
  1839. }
  1840. }
  1841. if (Options->FindSwitch(L"explicit") ||
  1842. // BACKWARD COMPATIBILITY with 5.5.x
  1843. Options->FindSwitch(L"explicittls"))
  1844. {
  1845. UnicodeString SwitchName =
  1846. Options->FindSwitch(L"explicit") ? L"explicit" : L"explicittls";
  1847. bool Enabled = Options->SwitchValue(SwitchName, true);
  1848. Ftps = Enabled ? ftpsExplicitTls : ftpsNone;
  1849. if (!PortNumberDefined && Enabled)
  1850. {
  1851. PortNumber = FtpPortNumber;
  1852. }
  1853. }
  1854. if (Options->FindSwitch(L"rawsettings"))
  1855. {
  1856. TStrings * RawSettings = NULL;
  1857. TOptionsStorage * OptionsStorage = NULL;
  1858. try
  1859. {
  1860. RawSettings = new TStringList();
  1861. if (Options->FindSwitch(L"rawsettings", RawSettings))
  1862. {
  1863. OptionsStorage = new TOptionsStorage(RawSettings, false);
  1864. ApplyRawSettings(OptionsStorage);
  1865. }
  1866. }
  1867. __finally
  1868. {
  1869. delete RawSettings;
  1870. delete OptionsStorage;
  1871. }
  1872. }
  1873. }
  1874. return true;
  1875. }
  1876. //---------------------------------------------------------------------
  1877. void __fastcall TSessionData::ApplyRawSettings(THierarchicalStorage * Storage)
  1878. {
  1879. bool Dummy;
  1880. DoLoad(Storage, false, Dummy);
  1881. }
  1882. //---------------------------------------------------------------------
  1883. void __fastcall TSessionData::ConfigureTunnel(int APortNumber)
  1884. {
  1885. FOrigHostName = HostName;
  1886. FOrigPortNumber = PortNumber;
  1887. FOrigProxyMethod = ProxyMethod;
  1888. HostName = L"127.0.0.1";
  1889. PortNumber = APortNumber;
  1890. // proxy settings is used for tunnel
  1891. ProxyMethod = ::pmNone;
  1892. FLogicalHostName = FOrigHostName;
  1893. }
  1894. //---------------------------------------------------------------------
  1895. void __fastcall TSessionData::RollbackTunnel()
  1896. {
  1897. HostName = FOrigHostName;
  1898. PortNumber = FOrigPortNumber;
  1899. ProxyMethod = FOrigProxyMethod;
  1900. FLogicalHostName = L"";
  1901. }
  1902. //---------------------------------------------------------------------
  1903. void __fastcall TSessionData::ExpandEnvironmentVariables()
  1904. {
  1905. HostName = HostNameExpanded;
  1906. UserName = UserNameExpanded;
  1907. PublicKeyFile = ::ExpandEnvironmentVariables(PublicKeyFile);
  1908. }
  1909. //---------------------------------------------------------------------
  1910. void __fastcall TSessionData::ValidatePath(const UnicodeString Path)
  1911. {
  1912. // noop
  1913. }
  1914. //---------------------------------------------------------------------
  1915. void __fastcall TSessionData::ValidateName(const UnicodeString Name)
  1916. {
  1917. // keep consistent with MakeValidName
  1918. if (Name.LastDelimiter(L"/") > 0)
  1919. {
  1920. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, L"/")));
  1921. }
  1922. }
  1923. //---------------------------------------------------------------------
  1924. UnicodeString __fastcall TSessionData::MakeValidName(const UnicodeString & Name)
  1925. {
  1926. // keep consistent with ValidateName
  1927. return ReplaceStr(Name, L"/", L"\\");
  1928. }
  1929. //---------------------------------------------------------------------
  1930. RawByteString __fastcall TSessionData::EncryptPassword(const UnicodeString & Password, UnicodeString Key)
  1931. {
  1932. return Configuration->EncryptPassword(Password, Key);
  1933. }
  1934. //---------------------------------------------------------------------
  1935. RawByteString __fastcall TSessionData::StronglyRecryptPassword(const RawByteString & Password, UnicodeString Key)
  1936. {
  1937. return Configuration->StronglyRecryptPassword(Password, Key);
  1938. }
  1939. //---------------------------------------------------------------------
  1940. UnicodeString __fastcall TSessionData::DecryptPassword(const RawByteString & Password, UnicodeString Key)
  1941. {
  1942. UnicodeString Result;
  1943. try
  1944. {
  1945. Result = Configuration->DecryptPassword(Password, Key);
  1946. }
  1947. catch(EAbort &)
  1948. {
  1949. // silently ignore aborted prompts for master password and return empty password
  1950. }
  1951. return Result;
  1952. }
  1953. //---------------------------------------------------------------------
  1954. bool __fastcall TSessionData::GetCanLogin()
  1955. {
  1956. return !FHostName.IsEmpty();
  1957. }
  1958. //---------------------------------------------------------------------------
  1959. UnicodeString __fastcall TSessionData::GetSessionKey()
  1960. {
  1961. UnicodeString Result = FORMAT(L"%s@%s", (UserName, HostName));
  1962. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  1963. {
  1964. Result += FORMAT(L":%d", (PortNumber));
  1965. }
  1966. return Result;
  1967. }
  1968. //---------------------------------------------------------------------
  1969. UnicodeString __fastcall TSessionData::GetInternalStorageKey()
  1970. {
  1971. // This is probably useless remnant of previous use of this method from OpenSessionInPutty
  1972. // that needs the method to return something even for ad-hoc sessions
  1973. if (Name.IsEmpty())
  1974. {
  1975. return SessionKey;
  1976. }
  1977. else
  1978. {
  1979. return Name;
  1980. }
  1981. }
  1982. //---------------------------------------------------------------------
  1983. UnicodeString __fastcall TSessionData::GetStorageKey()
  1984. {
  1985. return SessionName;
  1986. }
  1987. //---------------------------------------------------------------------
  1988. UnicodeString __fastcall TSessionData::FormatSiteKey(const UnicodeString & HostName, int PortNumber)
  1989. {
  1990. return FORMAT(L"%s:%d", (HostName, PortNumber));
  1991. }
  1992. //---------------------------------------------------------------------
  1993. UnicodeString __fastcall TSessionData::GetSiteKey()
  1994. {
  1995. return FormatSiteKey(HostNameExpanded, PortNumber);
  1996. }
  1997. //---------------------------------------------------------------------
  1998. void __fastcall TSessionData::SetHostName(UnicodeString value)
  1999. {
  2000. if (FHostName != value)
  2001. {
  2002. // HostName is key for password encryption
  2003. UnicodeString XPassword = Password;
  2004. UnicodeString XNewPassword = Password;
  2005. // This is now hardly used as hostname is parsed directly on login dialog.
  2006. // But can be used when importing sites from PuTTY, as it allows same format too.
  2007. int P = value.LastDelimiter(L"@");
  2008. if (P > 0)
  2009. {
  2010. UserName = value.SubString(1, P - 1);
  2011. value = value.SubString(P + 1, value.Length() - P);
  2012. }
  2013. FHostName = value;
  2014. Modify();
  2015. Password = XPassword;
  2016. NewPassword = XNewPassword;
  2017. Shred(XPassword);
  2018. Shred(XNewPassword);
  2019. }
  2020. }
  2021. //---------------------------------------------------------------------
  2022. UnicodeString __fastcall TSessionData::GetHostNameExpanded()
  2023. {
  2024. return ::ExpandEnvironmentVariables(HostName);
  2025. }
  2026. //---------------------------------------------------------------------
  2027. void __fastcall TSessionData::SetPortNumber(int value)
  2028. {
  2029. SET_SESSION_PROPERTY(PortNumber);
  2030. }
  2031. //---------------------------------------------------------------------------
  2032. void __fastcall TSessionData::SetShell(UnicodeString value)
  2033. {
  2034. SET_SESSION_PROPERTY(Shell);
  2035. }
  2036. //---------------------------------------------------------------------------
  2037. void __fastcall TSessionData::SetSftpServer(UnicodeString value)
  2038. {
  2039. SET_SESSION_PROPERTY(SftpServer);
  2040. }
  2041. //---------------------------------------------------------------------------
  2042. void __fastcall TSessionData::SetClearAliases(bool value)
  2043. {
  2044. SET_SESSION_PROPERTY(ClearAliases);
  2045. }
  2046. //---------------------------------------------------------------------------
  2047. void __fastcall TSessionData::SetListingCommand(UnicodeString value)
  2048. {
  2049. SET_SESSION_PROPERTY(ListingCommand);
  2050. }
  2051. //---------------------------------------------------------------------------
  2052. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  2053. {
  2054. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  2055. }
  2056. //---------------------------------------------------------------------------
  2057. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  2058. {
  2059. SET_SESSION_PROPERTY(UnsetNationalVars);
  2060. }
  2061. //---------------------------------------------------------------------
  2062. void __fastcall TSessionData::SetUserName(UnicodeString value)
  2063. {
  2064. // Avoid password recryption (what may popup master password prompt)
  2065. if (FUserName != value)
  2066. {
  2067. // UserName is key for password encryption
  2068. UnicodeString XPassword = Password;
  2069. UnicodeString XNewPassword = NewPassword;
  2070. SET_SESSION_PROPERTY(UserName);
  2071. Password = XPassword;
  2072. NewPassword = XNewPassword;
  2073. Shred(XPassword);
  2074. Shred(XNewPassword);
  2075. }
  2076. }
  2077. //---------------------------------------------------------------------
  2078. UnicodeString __fastcall TSessionData::GetUserNameExpanded()
  2079. {
  2080. return ::ExpandEnvironmentVariables(UserName);
  2081. }
  2082. //---------------------------------------------------------------------
  2083. void __fastcall TSessionData::SetPassword(UnicodeString avalue)
  2084. {
  2085. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  2086. SET_SESSION_PROPERTY(Password);
  2087. }
  2088. //---------------------------------------------------------------------
  2089. UnicodeString __fastcall TSessionData::GetPassword() const
  2090. {
  2091. return DecryptPassword(FPassword, UserName+HostName);
  2092. }
  2093. //---------------------------------------------------------------------
  2094. void __fastcall TSessionData::SetNewPassword(UnicodeString avalue)
  2095. {
  2096. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  2097. SET_SESSION_PROPERTY(NewPassword);
  2098. }
  2099. //---------------------------------------------------------------------
  2100. UnicodeString __fastcall TSessionData::GetNewPassword() const
  2101. {
  2102. return DecryptPassword(FNewPassword, UserName+HostName);
  2103. }
  2104. //---------------------------------------------------------------------
  2105. void __fastcall TSessionData::SetChangePassword(bool value)
  2106. {
  2107. SET_SESSION_PROPERTY(ChangePassword);
  2108. }
  2109. //---------------------------------------------------------------------
  2110. void __fastcall TSessionData::SetPingInterval(int value)
  2111. {
  2112. SET_SESSION_PROPERTY(PingInterval);
  2113. }
  2114. //---------------------------------------------------------------------
  2115. void __fastcall TSessionData::SetTryAgent(bool value)
  2116. {
  2117. SET_SESSION_PROPERTY(TryAgent);
  2118. }
  2119. //---------------------------------------------------------------------
  2120. void __fastcall TSessionData::SetAgentFwd(bool value)
  2121. {
  2122. SET_SESSION_PROPERTY(AgentFwd);
  2123. }
  2124. //---------------------------------------------------------------------
  2125. void __fastcall TSessionData::SetAuthTIS(bool value)
  2126. {
  2127. SET_SESSION_PROPERTY(AuthTIS);
  2128. }
  2129. //---------------------------------------------------------------------
  2130. void __fastcall TSessionData::SetAuthKI(bool value)
  2131. {
  2132. SET_SESSION_PROPERTY(AuthKI);
  2133. }
  2134. //---------------------------------------------------------------------
  2135. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  2136. {
  2137. SET_SESSION_PROPERTY(AuthKIPassword);
  2138. }
  2139. //---------------------------------------------------------------------
  2140. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  2141. {
  2142. SET_SESSION_PROPERTY(AuthGSSAPI);
  2143. }
  2144. //---------------------------------------------------------------------
  2145. void __fastcall TSessionData::SetGSSAPIFwdTGT(bool value)
  2146. {
  2147. SET_SESSION_PROPERTY(GSSAPIFwdTGT);
  2148. }
  2149. //---------------------------------------------------------------------
  2150. void __fastcall TSessionData::SetChangeUsername(bool value)
  2151. {
  2152. SET_SESSION_PROPERTY(ChangeUsername);
  2153. }
  2154. //---------------------------------------------------------------------
  2155. void __fastcall TSessionData::SetCompression(bool value)
  2156. {
  2157. SET_SESSION_PROPERTY(Compression);
  2158. }
  2159. //---------------------------------------------------------------------
  2160. void __fastcall TSessionData::SetSshProt(TSshProt value)
  2161. {
  2162. SET_SESSION_PROPERTY(SshProt);
  2163. }
  2164. //---------------------------------------------------------------------
  2165. void __fastcall TSessionData::SetSsh2DES(bool value)
  2166. {
  2167. SET_SESSION_PROPERTY(Ssh2DES);
  2168. }
  2169. //---------------------------------------------------------------------
  2170. void __fastcall TSessionData::SetSshNoUserAuth(bool value)
  2171. {
  2172. SET_SESSION_PROPERTY(SshNoUserAuth);
  2173. }
  2174. //---------------------------------------------------------------------
  2175. UnicodeString __fastcall TSessionData::GetSshProtStr()
  2176. {
  2177. return SshProtList[FSshProt];
  2178. }
  2179. //---------------------------------------------------------------------
  2180. bool __fastcall TSessionData::GetUsesSsh()
  2181. {
  2182. return IsSshProtocol(FSProtocol);
  2183. }
  2184. //---------------------------------------------------------------------
  2185. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  2186. {
  2187. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  2188. SET_SESSION_PROPERTY(Ciphers[Index]);
  2189. }
  2190. //---------------------------------------------------------------------
  2191. TCipher __fastcall TSessionData::GetCipher(int Index) const
  2192. {
  2193. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  2194. return FCiphers[Index];
  2195. }
  2196. //---------------------------------------------------------------------
  2197. template<class AlgoT>
  2198. void __fastcall TSessionData::SetAlgoList(AlgoT * List, const AlgoT * DefaultList, const UnicodeString * Names,
  2199. int Count, AlgoT WarnAlgo, UnicodeString value)
  2200. {
  2201. std::vector<bool> Used(Count); // initialized to false
  2202. std::vector<AlgoT> NewList(Count);
  2203. bool HasWarnAlgo = (WarnAlgo >= AlgoT());
  2204. const AlgoT * WarnPtr;
  2205. int WarnDefaultIndex;
  2206. if (!HasWarnAlgo)
  2207. {
  2208. WarnPtr = NULL;
  2209. WarnDefaultIndex = -1;
  2210. }
  2211. else
  2212. {
  2213. WarnPtr = std::find(DefaultList, DefaultList + Count, WarnAlgo);
  2214. DebugAssert(WarnPtr != NULL);
  2215. WarnDefaultIndex = (WarnPtr - DefaultList);
  2216. }
  2217. int Index = 0;
  2218. while (!value.IsEmpty())
  2219. {
  2220. UnicodeString AlgoStr = CutToChar(value, L',', true);
  2221. for (int Algo = 0; Algo < Count; Algo++)
  2222. {
  2223. if (!AlgoStr.CompareIC(Names[Algo]) &&
  2224. !Used[Algo] && DebugAlwaysTrue(Index < Count))
  2225. {
  2226. NewList[Index] = (AlgoT)Algo;
  2227. Used[Algo] = true;
  2228. Index++;
  2229. break;
  2230. }
  2231. }
  2232. }
  2233. if (HasWarnAlgo && !Used[WarnAlgo] && DebugAlwaysTrue(Index < Count))
  2234. {
  2235. NewList[Index] = WarnAlgo;
  2236. Used[WarnAlgo] = true;
  2237. Index++;
  2238. }
  2239. int WarnIndex = -1;
  2240. if (HasWarnAlgo)
  2241. {
  2242. WarnIndex = std::find(NewList.begin(), NewList.end(), WarnAlgo) - NewList.begin();
  2243. }
  2244. bool Priority = true;
  2245. for (int DefaultIndex = 0; (DefaultIndex < Count); DefaultIndex++)
  2246. {
  2247. AlgoT DefaultAlgo = DefaultList[DefaultIndex];
  2248. if (!Used[DefaultAlgo] && DebugAlwaysTrue(Index < Count))
  2249. {
  2250. int TargetIndex;
  2251. // Unused algs that are prioritized in the default list,
  2252. // should be merged before the existing custom list
  2253. if (Priority)
  2254. {
  2255. TargetIndex = DefaultIndex;
  2256. }
  2257. else
  2258. {
  2259. if (HasWarnAlgo && (DefaultIndex < WarnDefaultIndex))
  2260. {
  2261. TargetIndex = WarnIndex;
  2262. }
  2263. else
  2264. {
  2265. TargetIndex = Index;
  2266. }
  2267. }
  2268. NewList.insert(NewList.begin() + TargetIndex, DefaultAlgo);
  2269. DebugAssert(NewList.back() == AlgoT());
  2270. NewList.pop_back();
  2271. if (HasWarnAlgo && (TargetIndex <= WarnIndex))
  2272. {
  2273. WarnIndex++;
  2274. }
  2275. Index++;
  2276. }
  2277. else
  2278. {
  2279. Priority = false;
  2280. }
  2281. }
  2282. if (!std::equal(NewList.begin(), NewList.end(), List))
  2283. {
  2284. std::copy(NewList.begin(), NewList.end(), List);
  2285. Modify();
  2286. }
  2287. }
  2288. //---------------------------------------------------------------------
  2289. void __fastcall TSessionData::SetCipherList(UnicodeString value)
  2290. {
  2291. SetAlgoList(FCiphers, DefaultCipherList, CipherNames, CIPHER_COUNT, cipWarn, value);
  2292. }
  2293. //---------------------------------------------------------------------
  2294. UnicodeString __fastcall TSessionData::GetCipherList() const
  2295. {
  2296. UnicodeString Result;
  2297. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  2298. {
  2299. Result += UnicodeString(Index ? L"," : L"") + CipherNames[Cipher[Index]];
  2300. }
  2301. return Result;
  2302. }
  2303. //---------------------------------------------------------------------
  2304. void __fastcall TSessionData::SetKex(int Index, TKex value)
  2305. {
  2306. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  2307. SET_SESSION_PROPERTY(Kex[Index]);
  2308. }
  2309. //---------------------------------------------------------------------
  2310. TKex __fastcall TSessionData::GetKex(int Index) const
  2311. {
  2312. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  2313. return FKex[Index];
  2314. }
  2315. //---------------------------------------------------------------------
  2316. void __fastcall TSessionData::SetKexList(UnicodeString value)
  2317. {
  2318. SetAlgoList(FKex, DefaultKexList, KexNames, KEX_COUNT, kexWarn, value);
  2319. }
  2320. //---------------------------------------------------------------------
  2321. UnicodeString __fastcall TSessionData::GetKexList() const
  2322. {
  2323. UnicodeString Result;
  2324. for (int Index = 0; Index < KEX_COUNT; Index++)
  2325. {
  2326. Result += UnicodeString(Index ? L"," : L"") + KexNames[Kex[Index]];
  2327. }
  2328. return Result;
  2329. }
  2330. //---------------------------------------------------------------------
  2331. void __fastcall TSessionData::SetHostKeys(int Index, THostKey value)
  2332. {
  2333. DebugAssert(Index >= 0 && Index < HOSTKEY_COUNT);
  2334. SET_SESSION_PROPERTY(HostKeys[Index]);
  2335. }
  2336. //---------------------------------------------------------------------
  2337. THostKey __fastcall TSessionData::GetHostKeys(int Index) const
  2338. {
  2339. DebugAssert(Index >= 0 && Index < HOSTKEY_COUNT);
  2340. return FHostKeys[Index];
  2341. }
  2342. //---------------------------------------------------------------------
  2343. void __fastcall TSessionData::SetHostKeyList(UnicodeString value)
  2344. {
  2345. SetAlgoList(FHostKeys, DefaultHostKeyList, HostKeyNames, HOSTKEY_COUNT, hkWarn, value);
  2346. }
  2347. //---------------------------------------------------------------------
  2348. UnicodeString __fastcall TSessionData::GetHostKeyList() const
  2349. {
  2350. UnicodeString Result;
  2351. for (int Index = 0; Index < HOSTKEY_COUNT; Index++)
  2352. {
  2353. Result += UnicodeString(Index ? L"," : L"") + HostKeyNames[HostKeys[Index]];
  2354. }
  2355. return Result;
  2356. }
  2357. //---------------------------------------------------------------------
  2358. void __fastcall TSessionData::SetGssLib(int Index, TGssLib value)
  2359. {
  2360. DebugAssert(Index >= 0 && Index < GSSLIB_COUNT);
  2361. SET_SESSION_PROPERTY(GssLib[Index]);
  2362. }
  2363. //---------------------------------------------------------------------
  2364. TGssLib __fastcall TSessionData::GetGssLib(int Index) const
  2365. {
  2366. DebugAssert(Index >= 0 && Index < GSSLIB_COUNT);
  2367. return FGssLib[Index];
  2368. }
  2369. //---------------------------------------------------------------------
  2370. void __fastcall TSessionData::SetGssLibList(UnicodeString value)
  2371. {
  2372. SetAlgoList(FGssLib, DefaultGssLibList, GssLibNames, GSSLIB_COUNT, TGssLib(-1), value);
  2373. }
  2374. //---------------------------------------------------------------------
  2375. UnicodeString __fastcall TSessionData::GetGssLibList() const
  2376. {
  2377. UnicodeString Result;
  2378. for (int Index = 0; Index < GSSLIB_COUNT; Index++)
  2379. {
  2380. Result += UnicodeString(Index ? L"," : L"") + GssLibNames[GssLib[Index]];
  2381. }
  2382. return Result;
  2383. }
  2384. //---------------------------------------------------------------------
  2385. void __fastcall TSessionData::SetGssLibCustom(UnicodeString value)
  2386. {
  2387. SET_SESSION_PROPERTY(GssLibCustom);
  2388. }
  2389. //---------------------------------------------------------------------
  2390. void __fastcall TSessionData::SetPublicKeyFile(UnicodeString value)
  2391. {
  2392. if (FPublicKeyFile != value)
  2393. {
  2394. // PublicKeyFile is key for Passphrase encryption
  2395. UnicodeString XPassphrase = Passphrase;
  2396. // StripPathQuotes should not be needed as we do not feed quotes anymore
  2397. FPublicKeyFile = StripPathQuotes(value);
  2398. Modify();
  2399. Passphrase = XPassphrase;
  2400. Shred(XPassphrase);
  2401. }
  2402. }
  2403. //---------------------------------------------------------------------
  2404. void __fastcall TSessionData::SetPassphrase(UnicodeString avalue)
  2405. {
  2406. RawByteString value = EncryptPassword(avalue, PublicKeyFile);
  2407. SET_SESSION_PROPERTY(Passphrase);
  2408. }
  2409. //---------------------------------------------------------------------
  2410. UnicodeString __fastcall TSessionData::GetPassphrase() const
  2411. {
  2412. return DecryptPassword(FPassphrase, PublicKeyFile);
  2413. }
  2414. //---------------------------------------------------------------------
  2415. void __fastcall TSessionData::SetReturnVar(UnicodeString value)
  2416. {
  2417. SET_SESSION_PROPERTY(ReturnVar);
  2418. }
  2419. //---------------------------------------------------------------------------
  2420. void __fastcall TSessionData::SetLookupUserGroups(TAutoSwitch value)
  2421. {
  2422. SET_SESSION_PROPERTY(LookupUserGroups);
  2423. }
  2424. //---------------------------------------------------------------------------
  2425. void __fastcall TSessionData::SetEOLType(TEOLType value)
  2426. {
  2427. SET_SESSION_PROPERTY(EOLType);
  2428. }
  2429. //---------------------------------------------------------------------------
  2430. void __fastcall TSessionData::SetTrimVMSVersions(bool value)
  2431. {
  2432. SET_SESSION_PROPERTY(TrimVMSVersions);
  2433. }
  2434. //---------------------------------------------------------------------------
  2435. TDateTime __fastcall TSessionData::GetTimeoutDT()
  2436. {
  2437. return SecToDateTime(Timeout);
  2438. }
  2439. //---------------------------------------------------------------------------
  2440. void __fastcall TSessionData::SetTimeout(int value)
  2441. {
  2442. SET_SESSION_PROPERTY(Timeout);
  2443. }
  2444. //---------------------------------------------------------------------------
  2445. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  2446. {
  2447. SET_SESSION_PROPERTY(FSProtocol);
  2448. }
  2449. //---------------------------------------------------------------------
  2450. UnicodeString __fastcall TSessionData::GetFSProtocolStr()
  2451. {
  2452. DebugAssert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  2453. return FSProtocolNames[FSProtocol];
  2454. }
  2455. //---------------------------------------------------------------------------
  2456. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  2457. {
  2458. if (value != DetectReturnVar)
  2459. {
  2460. ReturnVar = value ? L"" : L"$?";
  2461. }
  2462. }
  2463. //---------------------------------------------------------------------------
  2464. bool __fastcall TSessionData::GetDetectReturnVar()
  2465. {
  2466. return ReturnVar.IsEmpty();
  2467. }
  2468. //---------------------------------------------------------------------------
  2469. void __fastcall TSessionData::SetDefaultShell(bool value)
  2470. {
  2471. if (value != DefaultShell)
  2472. {
  2473. Shell = value ? L"" : L"/bin/bash";
  2474. }
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. bool __fastcall TSessionData::GetDefaultShell()
  2478. {
  2479. return Shell.IsEmpty();
  2480. }
  2481. //---------------------------------------------------------------------------
  2482. void __fastcall TSessionData::SetPuttyProtocol(UnicodeString value)
  2483. {
  2484. SET_SESSION_PROPERTY(PuttyProtocol);
  2485. }
  2486. //---------------------------------------------------------------------
  2487. UnicodeString __fastcall TSessionData::GetNormalizedPuttyProtocol() const
  2488. {
  2489. return DefaultStr(PuttyProtocol, PuttySshProtocol);
  2490. }
  2491. //---------------------------------------------------------------------
  2492. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  2493. {
  2494. unsigned short hour, min, sec, msec;
  2495. value.DecodeTime(&hour, &min, &sec, &msec);
  2496. PingInterval = ((int)hour)*SecsPerHour + ((int)min)*SecsPerMin + sec;
  2497. }
  2498. //---------------------------------------------------------------------------
  2499. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  2500. {
  2501. return SecToDateTime(PingInterval);
  2502. }
  2503. //---------------------------------------------------------------------------
  2504. void __fastcall TSessionData::SetPingType(TPingType value)
  2505. {
  2506. SET_SESSION_PROPERTY(PingType);
  2507. }
  2508. //---------------------------------------------------------------------------
  2509. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  2510. {
  2511. SET_SESSION_PROPERTY(AddressFamily);
  2512. }
  2513. //---------------------------------------------------------------------------
  2514. void __fastcall TSessionData::SetRekeyData(UnicodeString value)
  2515. {
  2516. SET_SESSION_PROPERTY(RekeyData);
  2517. }
  2518. //---------------------------------------------------------------------------
  2519. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  2520. {
  2521. SET_SESSION_PROPERTY(RekeyTime);
  2522. }
  2523. //---------------------------------------------------------------------
  2524. UnicodeString __fastcall TSessionData::GetDefaultSessionName()
  2525. {
  2526. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  2527. {
  2528. // If we ever choose to include port number,
  2529. // we have to escape IPv6 literals in HostName
  2530. return FORMAT(L"%s@%s", (UserName, HostName));
  2531. }
  2532. else if (!HostName.IsEmpty())
  2533. {
  2534. return HostName;
  2535. }
  2536. else
  2537. {
  2538. return L"session";
  2539. }
  2540. }
  2541. //---------------------------------------------------------------------
  2542. UnicodeString __fastcall TSessionData::GetNameWithoutHiddenPrefix()
  2543. {
  2544. UnicodeString Result = Name;
  2545. if (Hidden)
  2546. {
  2547. Result = Result.SubString(TNamedObjectList::HiddenPrefix.Length() + 1, Result.Length() - TNamedObjectList::HiddenPrefix.Length());
  2548. }
  2549. return Result;
  2550. }
  2551. //---------------------------------------------------------------------
  2552. bool __fastcall TSessionData::HasSessionName()
  2553. {
  2554. return (!GetNameWithoutHiddenPrefix().IsEmpty() && (Name != DefaultName) && !IsWorkspace);
  2555. }
  2556. //---------------------------------------------------------------------
  2557. UnicodeString __fastcall TSessionData::GetSessionName()
  2558. {
  2559. UnicodeString Result;
  2560. if (HasSessionName())
  2561. {
  2562. Result = GetNameWithoutHiddenPrefix();
  2563. }
  2564. else
  2565. {
  2566. Result = DefaultSessionName;
  2567. }
  2568. return Result;
  2569. }
  2570. //---------------------------------------------------------------------
  2571. bool __fastcall TSessionData::IsSecure()
  2572. {
  2573. bool Result;
  2574. switch (FSProtocol)
  2575. {
  2576. case fsSCPonly:
  2577. case fsSFTP:
  2578. case fsSFTPonly:
  2579. Result = true;
  2580. break;
  2581. case fsFTP:
  2582. case fsWebDAV:
  2583. case fsS3:
  2584. Result = (Ftps != ftpsNone);
  2585. break;
  2586. default:
  2587. DebugFail();
  2588. break;
  2589. }
  2590. return Result;
  2591. }
  2592. //---------------------------------------------------------------------
  2593. UnicodeString __fastcall TSessionData::GetProtocolUrl()
  2594. {
  2595. UnicodeString Url;
  2596. switch (FSProtocol)
  2597. {
  2598. case fsSCPonly:
  2599. Url = ScpProtocol;
  2600. break;
  2601. default:
  2602. DebugFail();
  2603. // fallback
  2604. case fsSFTP:
  2605. case fsSFTPonly:
  2606. Url = SftpProtocol;
  2607. break;
  2608. case fsFTP:
  2609. if (Ftps == ftpsImplicit)
  2610. {
  2611. Url = FtpsProtocol;
  2612. }
  2613. else if ((Ftps == ftpsExplicitTls) || (Ftps == ftpsExplicitSsl))
  2614. {
  2615. Url = FtpesProtocol;
  2616. }
  2617. else
  2618. {
  2619. Url = FtpProtocol;
  2620. }
  2621. break;
  2622. case fsWebDAV:
  2623. if (Ftps == ftpsImplicit)
  2624. {
  2625. Url = WebDAVSProtocol;
  2626. }
  2627. else
  2628. {
  2629. Url = WebDAVProtocol;
  2630. }
  2631. break;
  2632. case fsS3:
  2633. Url = S3Protocol;
  2634. break;
  2635. }
  2636. Url += ProtocolSeparator;
  2637. return Url;
  2638. }
  2639. //---------------------------------------------------------------------
  2640. bool __fastcall IsIPv6Literal(const UnicodeString & HostName)
  2641. {
  2642. bool Result = (HostName.Pos(L":") > 0);
  2643. if (Result)
  2644. {
  2645. for (int Index = 1; Result && (Index <= HostName.Length()); Index++)
  2646. {
  2647. wchar_t C = HostName[Index];
  2648. Result = IsHex(C) || (C == L':');
  2649. }
  2650. }
  2651. return Result;
  2652. }
  2653. //---------------------------------------------------------------------
  2654. UnicodeString __fastcall EscapeIPv6Literal(const UnicodeString & IP)
  2655. {
  2656. return L"[" + IP + L"]";
  2657. }
  2658. //---------------------------------------------------------------------
  2659. UnicodeString __fastcall TSessionData::GenerateSessionUrl(unsigned int Flags)
  2660. {
  2661. UnicodeString Url;
  2662. if (FLAGSET(Flags, sufSpecific))
  2663. {
  2664. Url += WinSCPProtocolPrefix;
  2665. }
  2666. Url += GetProtocolUrl();
  2667. if (FLAGSET(Flags, sufUserName) && !UserNameExpanded.IsEmpty())
  2668. {
  2669. Url += EncodeUrlString(UserNameExpanded);
  2670. if (FLAGSET(Flags, sufPassword) && !Password.IsEmpty())
  2671. {
  2672. Url += L":" + EncodeUrlString(Password);
  2673. }
  2674. if (FLAGSET(Flags, sufHostKey) && !HostKey.IsEmpty())
  2675. {
  2676. Url +=
  2677. UnicodeString(UrlParamSeparator) + UrlHostKeyParamName +
  2678. UnicodeString(UrlParamValueSeparator) + NormalizeFingerprint(HostKey);
  2679. }
  2680. Url += L"@";
  2681. }
  2682. DebugAssert(!HostNameExpanded.IsEmpty());
  2683. if (IsIPv6Literal(HostNameExpanded))
  2684. {
  2685. Url += EscapeIPv6Literal(HostNameExpanded);
  2686. }
  2687. else
  2688. {
  2689. Url += EncodeUrlString(HostNameExpanded);
  2690. }
  2691. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  2692. {
  2693. Url += L":" + IntToStr(PortNumber);
  2694. }
  2695. Url += L"/";
  2696. return Url;
  2697. }
  2698. //---------------------------------------------------------------------
  2699. UnicodeString ScriptCommandOpenLink(TraceInitStr(ScriptCommandLink(L"open")));
  2700. //---------------------------------------------------------------------
  2701. void __fastcall TSessionData::AddSwitch(
  2702. UnicodeString & Result, const UnicodeString & Name, bool Rtf)
  2703. {
  2704. Result += RtfSwitch(Name, ScriptCommandOpenLink, Rtf);
  2705. }
  2706. //---------------------------------------------------------------------
  2707. void __fastcall TSessionData::AddSwitch(
  2708. UnicodeString & Result, const UnicodeString & Name, const UnicodeString & Value, bool Rtf)
  2709. {
  2710. Result += RtfSwitch(Name, ScriptCommandOpenLink, Value, Rtf);
  2711. }
  2712. //---------------------------------------------------------------------
  2713. void __fastcall TSessionData::AddSwitch(
  2714. UnicodeString & Result, const UnicodeString & Name, int Value, bool Rtf)
  2715. {
  2716. Result += RtfSwitch(Name, ScriptCommandOpenLink, Value, Rtf);
  2717. }
  2718. //---------------------------------------------------------------------
  2719. void __fastcall TSessionData::LookupLastFingerprint()
  2720. {
  2721. UnicodeString FingerprintType;
  2722. if (IsSshProtocol(FSProtocol))
  2723. {
  2724. FingerprintType = SshFingerprintType;
  2725. }
  2726. else if (Ftps != ftpsNone)
  2727. {
  2728. FingerprintType = TlsFingerprintType;
  2729. }
  2730. if (!FingerprintType.IsEmpty())
  2731. {
  2732. HostKey = Configuration->LastFingerprint(SiteKey, FingerprintType);
  2733. }
  2734. }
  2735. //---------------------------------------------------------------------
  2736. UnicodeString __fastcall TSessionData::GenerateOpenCommandArgs(bool Rtf)
  2737. {
  2738. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  2739. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  2740. SessionData->Assign(this);
  2741. UnicodeString Result = SessionData->GenerateSessionUrl(sufOpen);
  2742. // Before we reset the FSProtocol
  2743. bool AUsesSsh = SessionData->UsesSsh;
  2744. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  2745. if (SessionData->FSProtocol != fsSFTPonly)
  2746. {
  2747. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  2748. }
  2749. SessionData->HostName = FactoryDefaults->HostName;
  2750. SessionData->PortNumber = FactoryDefaults->PortNumber;
  2751. SessionData->UserName = FactoryDefaults->UserName;
  2752. SessionData->Password = FactoryDefaults->Password;
  2753. SessionData->CopyNonCoreData(FactoryDefaults.get());
  2754. SessionData->Ftps = FactoryDefaults->Ftps;
  2755. if (SessionData->HostKey != FactoryDefaults->HostKey)
  2756. {
  2757. UnicodeString SwitchName = AUsesSsh ? L"hostkey" : L"certificate";
  2758. AddSwitch(Result, SwitchName, SessionData->HostKey, Rtf);
  2759. SessionData->HostKey = FactoryDefaults->HostKey;
  2760. }
  2761. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  2762. {
  2763. AddSwitch(Result, L"privatekey", SessionData->PublicKeyFile, Rtf);
  2764. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  2765. }
  2766. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  2767. {
  2768. AddSwitch(Result, L"clientcert", SessionData->TlsCertificateFile, Rtf);
  2769. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  2770. }
  2771. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  2772. {
  2773. AddSwitch(Result, PassphraseOption, SessionData->Passphrase, Rtf);
  2774. SessionData->Passphrase = FactoryDefaults->Passphrase;
  2775. }
  2776. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  2777. {
  2778. AddSwitch(Result, L"passive", SessionData->FtpPasvMode ? 1 : 0, Rtf);
  2779. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  2780. }
  2781. if (SessionData->Timeout != FactoryDefaults->Timeout)
  2782. {
  2783. AddSwitch(Result, L"timeout", SessionData->Timeout, Rtf);
  2784. SessionData->Timeout = FactoryDefaults->Timeout;
  2785. }
  2786. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get()));
  2787. if (RawSettings->Count > 0)
  2788. {
  2789. AddSwitch(Result, L"rawsettings", Rtf);
  2790. for (int Index = 0; Index < RawSettings->Count; Index++)
  2791. {
  2792. UnicodeString Name = RawSettings->Names[Index];
  2793. UnicodeString Value = RawSettings->ValueFromIndex[Index];
  2794. // Do not quote if it is all-numeric
  2795. if (IntToStr(StrToIntDef(Value, -1)) != Value)
  2796. {
  2797. Value = FORMAT(L"\"%s\"", (EscapeParam(Value)));
  2798. }
  2799. Result += FORMAT(L" %s=%s", (Name, Value));
  2800. }
  2801. }
  2802. return Result;
  2803. }
  2804. //---------------------------------------------------------------------
  2805. UnicodeString SessionOptionsClassName(L"SessionOptions");
  2806. //---------------------------------------------------------------------
  2807. void __fastcall TSessionData::AddAssemblyProperty(
  2808. UnicodeString & Result, TAssemblyLanguage Language,
  2809. const UnicodeString & Name, const UnicodeString & Type,
  2810. const UnicodeString & Member)
  2811. {
  2812. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Type, Member, false);
  2813. }
  2814. //---------------------------------------------------------------------
  2815. void __fastcall TSessionData::AddAssemblyProperty(
  2816. UnicodeString & Result, TAssemblyLanguage Language,
  2817. const UnicodeString & Name, const UnicodeString & Value)
  2818. {
  2819. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  2820. }
  2821. //---------------------------------------------------------------------
  2822. void __fastcall TSessionData::AddAssemblyProperty(
  2823. UnicodeString & Result, TAssemblyLanguage Language,
  2824. const UnicodeString & Name, int Value)
  2825. {
  2826. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  2827. }
  2828. //---------------------------------------------------------------------
  2829. void __fastcall TSessionData::AddAssemblyProperty(
  2830. UnicodeString & Result, TAssemblyLanguage Language,
  2831. const UnicodeString & Name, bool Value)
  2832. {
  2833. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  2834. }
  2835. //---------------------------------------------------------------------
  2836. void __fastcall TSessionData::GenerateAssemblyCode(
  2837. TAssemblyLanguage Language, UnicodeString & Head, UnicodeString & Tail, int & Indent)
  2838. {
  2839. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  2840. std::unique_ptr<TSessionData> SessionData(Clone());
  2841. switch (Language)
  2842. {
  2843. case alCSharp:
  2844. case alVBNET:
  2845. // noop
  2846. break;
  2847. case alPowerShell:
  2848. Head +=
  2849. AssemblyCommentLine(Language, LoadStr(CODE_PS_ADD_TYPE)) +
  2850. RtfKeyword(L"Add-Type") + RtfText(" -Path ") + AssemblyString(Language, "WinSCPnet.dll") + RtfPara +
  2851. RtfPara;
  2852. break;
  2853. default:
  2854. DebugFail();
  2855. break;
  2856. }
  2857. Head +=
  2858. AssemblyCommentLine(Language, LoadStr(CODE_SESSION_OPTIONS)) +
  2859. AssemblyNewClassInstanceStart(Language, SessionOptionsClassName, false);
  2860. UnicodeString ProtocolMember;
  2861. switch (SessionData->FSProtocol)
  2862. {
  2863. case fsSCPonly:
  2864. ProtocolMember = "Scp";
  2865. break;
  2866. default:
  2867. DebugFail();
  2868. // fallback
  2869. case fsSFTP:
  2870. case fsSFTPonly:
  2871. ProtocolMember = "Sftp";
  2872. break;
  2873. case fsFTP:
  2874. ProtocolMember = "Ftp";
  2875. break;
  2876. case fsWebDAV:
  2877. ProtocolMember = "Webdav";
  2878. break;
  2879. case fsS3:
  2880. ProtocolMember = "S3";
  2881. break;
  2882. }
  2883. // Before we reset the FSProtocol
  2884. bool AUsesSsh = SessionData->UsesSsh;
  2885. // Protocol is set unconditionally, we want even the default SFTP
  2886. AddAssemblyProperty(Head, Language, L"Protocol", L"Protocol", ProtocolMember);
  2887. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  2888. if (SessionData->FSProtocol != fsSFTPonly)
  2889. {
  2890. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  2891. }
  2892. if (SessionData->HostName != FactoryDefaults->HostName)
  2893. {
  2894. AddAssemblyProperty(Head, Language, L"HostName", HostName);
  2895. SessionData->HostName = FactoryDefaults->HostName;
  2896. }
  2897. if (SessionData->PortNumber != FactoryDefaults->PortNumber)
  2898. {
  2899. AddAssemblyProperty(Head, Language, L"PortNumber", PortNumber);
  2900. SessionData->PortNumber = FactoryDefaults->PortNumber;
  2901. }
  2902. if (SessionData->UserName != FactoryDefaults->UserName)
  2903. {
  2904. AddAssemblyProperty(Head, Language, L"UserName", UserName);
  2905. SessionData->UserName = FactoryDefaults->UserName;
  2906. }
  2907. if (SessionData->Password != FactoryDefaults->Password)
  2908. {
  2909. AddAssemblyProperty(Head, Language, L"Password", Password);
  2910. SessionData->Password = FactoryDefaults->Password;
  2911. }
  2912. SessionData->CopyNonCoreData(FactoryDefaults.get());
  2913. if (SessionData->Ftps != FactoryDefaults->Ftps)
  2914. {
  2915. // SessionData->FSProtocol is reset already
  2916. switch (FSProtocol)
  2917. {
  2918. case fsFTP:
  2919. {
  2920. UnicodeString FtpSecureMember;
  2921. switch (SessionData->Ftps)
  2922. {
  2923. case ftpsNone:
  2924. // noop
  2925. break;
  2926. case ftpsImplicit:
  2927. FtpSecureMember = L"Implicit";
  2928. break;
  2929. case ftpsExplicitTls:
  2930. case ftpsExplicitSsl:
  2931. FtpSecureMember = L"Explicit";
  2932. break;
  2933. default:
  2934. DebugFail();
  2935. break;
  2936. }
  2937. AddAssemblyProperty(Head, Language, L"FtpSecure", L"FtpSecure", FtpSecureMember);
  2938. }
  2939. break;
  2940. case fsWebDAV:
  2941. AddAssemblyProperty(Head, Language, L"WebdavSecure", (SessionData->Ftps != ftpsNone));
  2942. break;
  2943. case fsS3:
  2944. // implicit
  2945. break;
  2946. default:
  2947. DebugFail();
  2948. break;
  2949. }
  2950. SessionData->Ftps = FactoryDefaults->Ftps;
  2951. }
  2952. if (SessionData->HostKey != FactoryDefaults->HostKey)
  2953. {
  2954. UnicodeString PropertyName = AUsesSsh ? L"SshHostKeyFingerprint" : L"TlsHostCertificateFingerprint";
  2955. AddAssemblyProperty(Head, Language, PropertyName, SessionData->HostKey);
  2956. SessionData->HostKey = FactoryDefaults->HostKey;
  2957. }
  2958. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  2959. {
  2960. AddAssemblyProperty(Head, Language, L"SshPrivateKeyPath", SessionData->PublicKeyFile);
  2961. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  2962. }
  2963. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  2964. {
  2965. AddAssemblyProperty(Head, Language, L"TlsClientCertificatePath", SessionData->TlsCertificateFile);
  2966. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  2967. }
  2968. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  2969. {
  2970. AddAssemblyProperty(Head, Language, L"PrivateKeyPassphrase", SessionData->Passphrase);
  2971. SessionData->Passphrase = FactoryDefaults->Passphrase;
  2972. }
  2973. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  2974. {
  2975. AddAssemblyProperty(Head, Language, L"FtpMode", L"FtpMode", (SessionData->FtpPasvMode ? L"Passive" : L"Active"));
  2976. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  2977. }
  2978. if (SessionData->Timeout != FactoryDefaults->Timeout)
  2979. {
  2980. AddAssemblyProperty(Head, Language, L"TimeoutInMilliseconds", SessionData->Timeout * 1000);
  2981. SessionData->Timeout = FactoryDefaults->Timeout;
  2982. }
  2983. switch (Language)
  2984. {
  2985. case alCSharp:
  2986. case alPowerShell:
  2987. Head += AssemblyNewClassInstanceEnd(Language, false);
  2988. break;
  2989. case alVBNET:
  2990. // noop
  2991. // Ending With only after AddRawSettings
  2992. break;
  2993. }
  2994. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get()));
  2995. UnicodeString SessionOptionsVariableName = AssemblyVariableName(Language, SessionOptionsClassName);
  2996. if (RawSettings->Count > 0)
  2997. {
  2998. Head += RtfPara;
  2999. for (int Index = 0; Index < RawSettings->Count; Index++)
  3000. {
  3001. UnicodeString Name = RawSettings->Names[Index];
  3002. UnicodeString Value = RawSettings->ValueFromIndex[Index];
  3003. UnicodeString AddRawSettingsMethod =
  3004. RtfLibraryMethod(SessionOptionsClassName, L"AddRawSettings", false) +
  3005. FORMAT(L"(%s, %s)", (AssemblyString(Language, Name), AssemblyString(Language, Value)));
  3006. switch (Language)
  3007. {
  3008. case alCSharp:
  3009. Head += RtfText(SessionOptionsVariableName + L".") + AddRawSettingsMethod + RtfText(L";") + RtfPara;
  3010. break;
  3011. case alVBNET:
  3012. Head += RtfText(L" .") + AddRawSettingsMethod + RtfPara;
  3013. break;
  3014. case alPowerShell:
  3015. Head += RtfText(SessionOptionsVariableName + L".") + AddRawSettingsMethod + RtfPara;
  3016. break;
  3017. }
  3018. }
  3019. }
  3020. UnicodeString Indentation = L" ";
  3021. UnicodeString SessionVariableName = AssemblyVariableName(Language, SessionClassName);
  3022. UnicodeString RtfSessionClass = RtfLibraryClass(SessionClassName);
  3023. UnicodeString RtfSessionOpenMethod = RtfLibraryMethod(SessionClassName, L"Open", false);
  3024. UnicodeString NewSessionInstance = AssemblyNewClassInstance(Language, SessionClassName, false);
  3025. UnicodeString OpenCall =
  3026. Indentation + AssemblyCommentLine(Language, LoadStr(CODE_CONNECT)) +
  3027. Indentation + RtfText(SessionVariableName + L".") + RtfSessionOpenMethod + RtfText(L"(" + SessionOptionsVariableName + L")") +
  3028. AssemblyStatementSeparator(Language) + RtfPara;
  3029. switch (Language)
  3030. {
  3031. case alCSharp:
  3032. Head +=
  3033. RtfPara +
  3034. RtfKeyword(L"using") + RtfText(" (") + NewSessionInstance + RtfText(L"())") + RtfPara +
  3035. RtfText(L"{") + RtfPara +
  3036. OpenCall;
  3037. Tail =
  3038. RtfText(L"}") + RtfPara;
  3039. break;
  3040. case alVBNET:
  3041. Head +=
  3042. AssemblyNewClassInstanceEnd(Language, false) +
  3043. RtfPara +
  3044. RtfKeyword(L"Using") + RtfText(L" ") + NewSessionInstance + RtfPara +
  3045. OpenCall;
  3046. Tail =
  3047. RtfKeyword(L"End Using");
  3048. break;
  3049. case alPowerShell:
  3050. Head +=
  3051. RtfPara +
  3052. NewSessionInstance + RtfPara +
  3053. RtfPara +
  3054. RtfKeyword(L"try") + RtfPara +
  3055. RtfText(L"{") + RtfPara +
  3056. OpenCall;
  3057. Tail =
  3058. RtfText(L"}") + RtfPara +
  3059. RtfKeyword(L"finally") + RtfPara +
  3060. RtfText(L"{") + RtfPara +
  3061. RtfText(Indentation + SessionVariableName + L".") +
  3062. RtfLibraryMethod(SessionClassName, L"Dispose", false) + RtfText(L"()") + RtfPara +
  3063. RtfText(L"}") + RtfPara;
  3064. break;
  3065. }
  3066. Head += RtfPara;
  3067. Indent = 4; // the same for all languages so far
  3068. }
  3069. //---------------------------------------------------------------------
  3070. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  3071. {
  3072. SET_SESSION_PROPERTY(TimeDifference);
  3073. }
  3074. //---------------------------------------------------------------------
  3075. void __fastcall TSessionData::SetTimeDifferenceAuto(bool value)
  3076. {
  3077. SET_SESSION_PROPERTY(TimeDifferenceAuto);
  3078. }
  3079. //---------------------------------------------------------------------
  3080. void __fastcall TSessionData::SetLocalDirectory(UnicodeString value)
  3081. {
  3082. SET_SESSION_PROPERTY(LocalDirectory);
  3083. }
  3084. //---------------------------------------------------------------------
  3085. void __fastcall TSessionData::SetRemoteDirectory(UnicodeString value)
  3086. {
  3087. SET_SESSION_PROPERTY(RemoteDirectory);
  3088. }
  3089. //---------------------------------------------------------------------
  3090. void __fastcall TSessionData::SetSynchronizeBrowsing(bool value)
  3091. {
  3092. SET_SESSION_PROPERTY(SynchronizeBrowsing);
  3093. }
  3094. //---------------------------------------------------------------------
  3095. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  3096. {
  3097. SET_SESSION_PROPERTY(UpdateDirectories);
  3098. }
  3099. //---------------------------------------------------------------------
  3100. void __fastcall TSessionData::SetCacheDirectories(bool value)
  3101. {
  3102. SET_SESSION_PROPERTY(CacheDirectories);
  3103. }
  3104. //---------------------------------------------------------------------
  3105. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  3106. {
  3107. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  3108. }
  3109. //---------------------------------------------------------------------
  3110. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  3111. {
  3112. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  3113. }
  3114. //---------------------------------------------------------------------
  3115. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  3116. {
  3117. SET_SESSION_PROPERTY(ResolveSymlinks);
  3118. }
  3119. //---------------------------------------------------------------------
  3120. void __fastcall TSessionData::SetFollowDirectorySymlinks(bool value)
  3121. {
  3122. SET_SESSION_PROPERTY(FollowDirectorySymlinks);
  3123. }
  3124. //---------------------------------------------------------------------------
  3125. void __fastcall TSessionData::SetDSTMode(TDSTMode value)
  3126. {
  3127. SET_SESSION_PROPERTY(DSTMode);
  3128. }
  3129. //---------------------------------------------------------------------------
  3130. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  3131. {
  3132. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  3133. }
  3134. //---------------------------------------------------------------------------
  3135. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  3136. {
  3137. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  3138. }
  3139. //---------------------------------------------------------------------------
  3140. void __fastcall TSessionData::SetRecycleBinPath(UnicodeString value)
  3141. {
  3142. SET_SESSION_PROPERTY(RecycleBinPath);
  3143. }
  3144. //---------------------------------------------------------------------------
  3145. void __fastcall TSessionData::SetPostLoginCommands(UnicodeString value)
  3146. {
  3147. SET_SESSION_PROPERTY(PostLoginCommands);
  3148. }
  3149. //---------------------------------------------------------------------
  3150. void __fastcall TSessionData::SetLockInHome(bool value)
  3151. {
  3152. SET_SESSION_PROPERTY(LockInHome);
  3153. }
  3154. //---------------------------------------------------------------------
  3155. void __fastcall TSessionData::SetSpecial(bool value)
  3156. {
  3157. SET_SESSION_PROPERTY(Special);
  3158. }
  3159. //---------------------------------------------------------------------------
  3160. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  3161. {
  3162. SET_SESSION_PROPERTY(Scp1Compatibility);
  3163. }
  3164. //---------------------------------------------------------------------
  3165. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  3166. {
  3167. SET_SESSION_PROPERTY(TcpNoDelay);
  3168. }
  3169. //---------------------------------------------------------------------
  3170. void __fastcall TSessionData::SetSendBuf(int value)
  3171. {
  3172. SET_SESSION_PROPERTY(SendBuf);
  3173. }
  3174. //---------------------------------------------------------------------
  3175. void __fastcall TSessionData::SetSshSimple(bool value)
  3176. {
  3177. SET_SESSION_PROPERTY(SshSimple);
  3178. }
  3179. //---------------------------------------------------------------------
  3180. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  3181. {
  3182. SET_SESSION_PROPERTY(ProxyMethod);
  3183. }
  3184. //---------------------------------------------------------------------
  3185. void __fastcall TSessionData::SetProxyHost(UnicodeString value)
  3186. {
  3187. SET_SESSION_PROPERTY(ProxyHost);
  3188. }
  3189. //---------------------------------------------------------------------
  3190. void __fastcall TSessionData::SetProxyPort(int value)
  3191. {
  3192. SET_SESSION_PROPERTY(ProxyPort);
  3193. }
  3194. //---------------------------------------------------------------------
  3195. void __fastcall TSessionData::SetProxyUsername(UnicodeString value)
  3196. {
  3197. SET_SESSION_PROPERTY(ProxyUsername);
  3198. }
  3199. //---------------------------------------------------------------------
  3200. void __fastcall TSessionData::SetProxyPassword(UnicodeString avalue)
  3201. {
  3202. RawByteString value = EncryptPassword(avalue, ProxyUsername+ProxyHost);
  3203. SET_SESSION_PROPERTY(ProxyPassword);
  3204. }
  3205. //---------------------------------------------------------------------
  3206. UnicodeString __fastcall TSessionData::GetProxyPassword() const
  3207. {
  3208. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  3209. }
  3210. //---------------------------------------------------------------------
  3211. void __fastcall TSessionData::SetProxyTelnetCommand(UnicodeString value)
  3212. {
  3213. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  3214. }
  3215. //---------------------------------------------------------------------
  3216. void __fastcall TSessionData::SetProxyLocalCommand(UnicodeString value)
  3217. {
  3218. SET_SESSION_PROPERTY(ProxyLocalCommand);
  3219. }
  3220. //---------------------------------------------------------------------
  3221. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  3222. {
  3223. SET_SESSION_PROPERTY(ProxyDNS);
  3224. }
  3225. //---------------------------------------------------------------------
  3226. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  3227. {
  3228. SET_SESSION_PROPERTY(ProxyLocalhost);
  3229. }
  3230. //---------------------------------------------------------------------
  3231. void __fastcall TSessionData::SetFtpProxyLogonType(int value)
  3232. {
  3233. SET_SESSION_PROPERTY(FtpProxyLogonType);
  3234. }
  3235. //---------------------------------------------------------------------
  3236. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  3237. {
  3238. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  3239. SET_SESSION_PROPERTY(Bugs[Bug]);
  3240. }
  3241. //---------------------------------------------------------------------
  3242. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  3243. {
  3244. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  3245. return FBugs[Bug];
  3246. }
  3247. //---------------------------------------------------------------------
  3248. void __fastcall TSessionData::SetCustomParam1(UnicodeString value)
  3249. {
  3250. SET_SESSION_PROPERTY(CustomParam1);
  3251. }
  3252. //---------------------------------------------------------------------
  3253. void __fastcall TSessionData::SetCustomParam2(UnicodeString value)
  3254. {
  3255. SET_SESSION_PROPERTY(CustomParam2);
  3256. }
  3257. //---------------------------------------------------------------------
  3258. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  3259. {
  3260. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  3261. }
  3262. //---------------------------------------------------------------------
  3263. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  3264. {
  3265. SET_SESSION_PROPERTY(SFTPUploadQueue);
  3266. }
  3267. //---------------------------------------------------------------------
  3268. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  3269. {
  3270. SET_SESSION_PROPERTY(SFTPListingQueue);
  3271. }
  3272. //---------------------------------------------------------------------
  3273. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  3274. {
  3275. SET_SESSION_PROPERTY(SFTPMaxVersion);
  3276. }
  3277. //---------------------------------------------------------------------
  3278. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  3279. {
  3280. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  3281. }
  3282. //---------------------------------------------------------------------
  3283. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  3284. {
  3285. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  3286. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  3287. }
  3288. //---------------------------------------------------------------------
  3289. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  3290. {
  3291. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  3292. return FSFTPBugs[Bug];
  3293. }
  3294. //---------------------------------------------------------------------
  3295. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  3296. {
  3297. SET_SESSION_PROPERTY(SCPLsFullTime);
  3298. }
  3299. //---------------------------------------------------------------------------
  3300. void __fastcall TSessionData::SetColor(int value)
  3301. {
  3302. SET_SESSION_PROPERTY(Color);
  3303. }
  3304. //---------------------------------------------------------------------------
  3305. void __fastcall TSessionData::SetTunnel(bool value)
  3306. {
  3307. SET_SESSION_PROPERTY(Tunnel);
  3308. }
  3309. //---------------------------------------------------------------------
  3310. void __fastcall TSessionData::SetTunnelHostName(UnicodeString value)
  3311. {
  3312. if (FTunnelHostName != value)
  3313. {
  3314. // HostName is key for password encryption
  3315. UnicodeString XTunnelPassword = TunnelPassword;
  3316. int P = value.LastDelimiter(L"@");
  3317. if (P > 0)
  3318. {
  3319. TunnelUserName = value.SubString(1, P - 1);
  3320. value = value.SubString(P + 1, value.Length() - P);
  3321. }
  3322. FTunnelHostName = value;
  3323. Modify();
  3324. TunnelPassword = XTunnelPassword;
  3325. Shred(XTunnelPassword);
  3326. }
  3327. }
  3328. //---------------------------------------------------------------------
  3329. void __fastcall TSessionData::SetTunnelPortNumber(int value)
  3330. {
  3331. SET_SESSION_PROPERTY(TunnelPortNumber);
  3332. }
  3333. //---------------------------------------------------------------------
  3334. void __fastcall TSessionData::SetTunnelUserName(UnicodeString value)
  3335. {
  3336. // Avoid password recryption (what may popup master password prompt)
  3337. if (FTunnelUserName != value)
  3338. {
  3339. // TunnelUserName is key for password encryption
  3340. UnicodeString XTunnelPassword = TunnelPassword;
  3341. SET_SESSION_PROPERTY(TunnelUserName);
  3342. TunnelPassword = XTunnelPassword;
  3343. Shred(XTunnelPassword);
  3344. }
  3345. }
  3346. //---------------------------------------------------------------------
  3347. void __fastcall TSessionData::SetTunnelPassword(UnicodeString avalue)
  3348. {
  3349. RawByteString value = EncryptPassword(avalue, TunnelUserName+TunnelHostName);
  3350. SET_SESSION_PROPERTY(TunnelPassword);
  3351. }
  3352. //---------------------------------------------------------------------
  3353. UnicodeString __fastcall TSessionData::GetTunnelPassword() const
  3354. {
  3355. return DecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName);
  3356. }
  3357. //---------------------------------------------------------------------
  3358. void __fastcall TSessionData::SetTunnelPublicKeyFile(UnicodeString value)
  3359. {
  3360. if (FTunnelPublicKeyFile != value)
  3361. {
  3362. // StripPathQuotes should not be needed as we do not feed quotes anymore
  3363. FTunnelPublicKeyFile = StripPathQuotes(value);
  3364. Modify();
  3365. }
  3366. }
  3367. //---------------------------------------------------------------------
  3368. void __fastcall TSessionData::SetTunnelLocalPortNumber(int value)
  3369. {
  3370. SET_SESSION_PROPERTY(TunnelLocalPortNumber);
  3371. }
  3372. //---------------------------------------------------------------------
  3373. bool __fastcall TSessionData::GetTunnelAutoassignLocalPortNumber()
  3374. {
  3375. return (FTunnelLocalPortNumber <= 0);
  3376. }
  3377. //---------------------------------------------------------------------
  3378. void __fastcall TSessionData::SetTunnelPortFwd(UnicodeString value)
  3379. {
  3380. SET_SESSION_PROPERTY(TunnelPortFwd);
  3381. }
  3382. //---------------------------------------------------------------------
  3383. void __fastcall TSessionData::SetTunnelHostKey(UnicodeString value)
  3384. {
  3385. SET_SESSION_PROPERTY(TunnelHostKey);
  3386. }
  3387. //---------------------------------------------------------------------
  3388. void __fastcall TSessionData::SetFtpPasvMode(bool value)
  3389. {
  3390. SET_SESSION_PROPERTY(FtpPasvMode);
  3391. }
  3392. //---------------------------------------------------------------------
  3393. void __fastcall TSessionData::SetFtpForcePasvIp(TAutoSwitch value)
  3394. {
  3395. SET_SESSION_PROPERTY(FtpForcePasvIp);
  3396. }
  3397. //---------------------------------------------------------------------
  3398. void __fastcall TSessionData::SetFtpUseMlsd(TAutoSwitch value)
  3399. {
  3400. SET_SESSION_PROPERTY(FtpUseMlsd);
  3401. }
  3402. //---------------------------------------------------------------------
  3403. void __fastcall TSessionData::SetFtpAccount(UnicodeString value)
  3404. {
  3405. SET_SESSION_PROPERTY(FtpAccount);
  3406. }
  3407. //---------------------------------------------------------------------
  3408. void __fastcall TSessionData::SetFtpPingInterval(int value)
  3409. {
  3410. SET_SESSION_PROPERTY(FtpPingInterval);
  3411. }
  3412. //---------------------------------------------------------------------------
  3413. TDateTime __fastcall TSessionData::GetFtpPingIntervalDT()
  3414. {
  3415. return SecToDateTime(FtpPingInterval);
  3416. }
  3417. //---------------------------------------------------------------------------
  3418. void __fastcall TSessionData::SetFtpPingType(TPingType value)
  3419. {
  3420. SET_SESSION_PROPERTY(FtpPingType);
  3421. }
  3422. //---------------------------------------------------------------------------
  3423. void __fastcall TSessionData::SetFtpTransferActiveImmediately(TAutoSwitch value)
  3424. {
  3425. SET_SESSION_PROPERTY(FtpTransferActiveImmediately);
  3426. }
  3427. //---------------------------------------------------------------------------
  3428. void __fastcall TSessionData::SetFtps(TFtps value)
  3429. {
  3430. SET_SESSION_PROPERTY(Ftps);
  3431. }
  3432. //---------------------------------------------------------------------------
  3433. void __fastcall TSessionData::SetMinTlsVersion(TTlsVersion value)
  3434. {
  3435. SET_SESSION_PROPERTY(MinTlsVersion);
  3436. }
  3437. //---------------------------------------------------------------------------
  3438. void __fastcall TSessionData::SetMaxTlsVersion(TTlsVersion value)
  3439. {
  3440. SET_SESSION_PROPERTY(MaxTlsVersion);
  3441. }
  3442. //---------------------------------------------------------------------------
  3443. void __fastcall TSessionData::SetLogicalHostName(UnicodeString value)
  3444. {
  3445. SET_SESSION_PROPERTY(LogicalHostName);
  3446. }
  3447. //---------------------------------------------------------------------
  3448. void __fastcall TSessionData::SetFtpListAll(TAutoSwitch value)
  3449. {
  3450. SET_SESSION_PROPERTY(FtpListAll);
  3451. }
  3452. //---------------------------------------------------------------------
  3453. void __fastcall TSessionData::SetFtpHost(TAutoSwitch value)
  3454. {
  3455. SET_SESSION_PROPERTY(FtpHost);
  3456. }
  3457. //---------------------------------------------------------------------
  3458. void __fastcall TSessionData::SetFtpDeleteFromCwd(TAutoSwitch value)
  3459. {
  3460. SET_SESSION_PROPERTY(FtpDeleteFromCwd);
  3461. }
  3462. //---------------------------------------------------------------------
  3463. void __fastcall TSessionData::SetSslSessionReuse(bool value)
  3464. {
  3465. SET_SESSION_PROPERTY(SslSessionReuse);
  3466. }
  3467. //---------------------------------------------------------------------
  3468. void __fastcall TSessionData::SetTlsCertificateFile(UnicodeString value)
  3469. {
  3470. SET_SESSION_PROPERTY(TlsCertificateFile);
  3471. }
  3472. //---------------------------------------------------------------------
  3473. void __fastcall TSessionData::SetNotUtf(TAutoSwitch value)
  3474. {
  3475. SET_SESSION_PROPERTY(NotUtf);
  3476. }
  3477. //---------------------------------------------------------------------
  3478. void __fastcall TSessionData::SetIsWorkspace(bool value)
  3479. {
  3480. SET_SESSION_PROPERTY(IsWorkspace);
  3481. }
  3482. //---------------------------------------------------------------------
  3483. void __fastcall TSessionData::SetLink(UnicodeString value)
  3484. {
  3485. SET_SESSION_PROPERTY(Link);
  3486. }
  3487. //---------------------------------------------------------------------
  3488. void __fastcall TSessionData::SetHostKey(UnicodeString value)
  3489. {
  3490. SET_SESSION_PROPERTY(HostKey);
  3491. }
  3492. //---------------------------------------------------------------------
  3493. void __fastcall TSessionData::SetNote(UnicodeString value)
  3494. {
  3495. SET_SESSION_PROPERTY(Note);
  3496. }
  3497. //---------------------------------------------------------------------
  3498. void __fastcall TSessionData::SetWinTitle(UnicodeString value)
  3499. {
  3500. SET_SESSION_PROPERTY(WinTitle);
  3501. }
  3502. //---------------------------------------------------------------------
  3503. UnicodeString __fastcall TSessionData::GetInfoTip()
  3504. {
  3505. if (UsesSsh)
  3506. {
  3507. return FMTLOAD(SESSION_INFO_TIP2,
  3508. (HostName, UserName,
  3509. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  3510. FSProtocolStr));
  3511. }
  3512. else
  3513. {
  3514. return FMTLOAD(SESSION_INFO_TIP_NO_SSH,
  3515. (HostName, UserName, FSProtocolStr));
  3516. }
  3517. }
  3518. //---------------------------------------------------------------------
  3519. UnicodeString __fastcall TSessionData::ExtractLocalName(const UnicodeString & Name)
  3520. {
  3521. UnicodeString Result = Name;
  3522. int P = Result.LastDelimiter(L"/");
  3523. if (P > 0)
  3524. {
  3525. Result.Delete(1, P);
  3526. }
  3527. return Result;
  3528. }
  3529. //---------------------------------------------------------------------
  3530. UnicodeString __fastcall TSessionData::GetLocalName()
  3531. {
  3532. UnicodeString Result;
  3533. if (HasSessionName())
  3534. {
  3535. Result = ExtractLocalName(Name);
  3536. }
  3537. else
  3538. {
  3539. Result = DefaultSessionName;
  3540. }
  3541. return Result;
  3542. }
  3543. //---------------------------------------------------------------------
  3544. UnicodeString __fastcall TSessionData::ExtractFolderName(const UnicodeString & Name)
  3545. {
  3546. UnicodeString Result;
  3547. int P = Name.LastDelimiter(L"/");
  3548. if (P > 0)
  3549. {
  3550. Result = Name.SubString(1, P - 1);
  3551. }
  3552. return Result;
  3553. }
  3554. //---------------------------------------------------------------------
  3555. UnicodeString __fastcall TSessionData::GetFolderName()
  3556. {
  3557. UnicodeString Result;
  3558. if (HasSessionName() || IsWorkspace)
  3559. {
  3560. Result = ExtractFolderName(Name);
  3561. }
  3562. return Result;
  3563. }
  3564. //---------------------------------------------------------------------
  3565. UnicodeString __fastcall TSessionData::ComposePath(
  3566. const UnicodeString & Path, const UnicodeString & Name)
  3567. {
  3568. return UnixIncludeTrailingBackslash(Path) + Name;
  3569. }
  3570. //---------------------------------------------------------------------
  3571. void __fastcall TSessionData::DisableAuthentationsExceptPassword()
  3572. {
  3573. SshNoUserAuth = false;
  3574. AuthTIS = false;
  3575. AuthKI = false;
  3576. AuthKIPassword = false;
  3577. AuthGSSAPI = false;
  3578. PublicKeyFile = L"";
  3579. TlsCertificateFile = L"";
  3580. Passphrase = L"";
  3581. TryAgent = false;
  3582. }
  3583. //=== TStoredSessionList ----------------------------------------------
  3584. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  3585. TNamedObjectList(), FReadOnly(aReadOnly)
  3586. {
  3587. DebugAssert(Configuration);
  3588. FDefaultSettings = new TSessionData(DefaultName);
  3589. }
  3590. //---------------------------------------------------------------------
  3591. __fastcall TStoredSessionList::~TStoredSessionList()
  3592. {
  3593. DebugAssert(Configuration);
  3594. delete FDefaultSettings;
  3595. }
  3596. //---------------------------------------------------------------------
  3597. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  3598. bool AsModified, bool UseDefaults, bool PuttyImport)
  3599. {
  3600. TStringList *SubKeys = new TStringList();
  3601. TList * Loaded = new TList;
  3602. try
  3603. {
  3604. DebugAssert(AutoSort);
  3605. AutoSort = false;
  3606. bool WasEmpty = (Count == 0);
  3607. Storage->GetSubKeyNames(SubKeys);
  3608. for (int Index = 0; Index < SubKeys->Count; Index++)
  3609. {
  3610. UnicodeString SessionName = SubKeys->Strings[Index];
  3611. bool ValidName = true;
  3612. try
  3613. {
  3614. TSessionData::ValidatePath(SessionName);
  3615. }
  3616. catch(...)
  3617. {
  3618. ValidName = false;
  3619. }
  3620. if (ValidName)
  3621. {
  3622. TSessionData * SessionData;
  3623. if (SessionName == FDefaultSettings->Name)
  3624. {
  3625. SessionData = FDefaultSettings;
  3626. }
  3627. else
  3628. {
  3629. // if the list was empty before loading, do not waste time trying to
  3630. // find existing sites to overwrite (we rely on underlying storage
  3631. // to secure uniqueness of the key names)
  3632. if (WasEmpty)
  3633. {
  3634. SessionData = NULL;
  3635. }
  3636. else
  3637. {
  3638. SessionData = (TSessionData*)FindByName(SessionName);
  3639. }
  3640. }
  3641. if ((SessionData != FDefaultSettings) || !UseDefaults)
  3642. {
  3643. if (SessionData == NULL)
  3644. {
  3645. SessionData = new TSessionData(L"");
  3646. if (UseDefaults)
  3647. {
  3648. SessionData->CopyData(DefaultSettings);
  3649. }
  3650. SessionData->Name = SessionName;
  3651. Add(SessionData);
  3652. }
  3653. Loaded->Add(SessionData);
  3654. SessionData->Load(Storage, PuttyImport);
  3655. if (AsModified)
  3656. {
  3657. SessionData->Modified = true;
  3658. }
  3659. }
  3660. }
  3661. }
  3662. if (!AsModified)
  3663. {
  3664. for (int Index = 0; Index < TObjectList::Count; Index++)
  3665. {
  3666. if (Loaded->IndexOf(Items[Index]) < 0)
  3667. {
  3668. Delete(Index);
  3669. Index--;
  3670. }
  3671. }
  3672. }
  3673. }
  3674. __finally
  3675. {
  3676. AutoSort = true;
  3677. AlphaSort();
  3678. delete SubKeys;
  3679. delete Loaded;
  3680. }
  3681. }
  3682. //---------------------------------------------------------------------
  3683. void __fastcall TStoredSessionList::Load()
  3684. {
  3685. bool SessionList = true;
  3686. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  3687. try
  3688. {
  3689. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  3690. Load(Storage);
  3691. }
  3692. __finally
  3693. {
  3694. delete Storage;
  3695. }
  3696. }
  3697. //---------------------------------------------------------------------
  3698. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  3699. TSessionData * Data, bool All, bool RecryptPasswordOnly,
  3700. TSessionData * FactoryDefaults)
  3701. {
  3702. if (All || Data->Modified)
  3703. {
  3704. if (RecryptPasswordOnly)
  3705. {
  3706. Data->SaveRecryptedPasswords(Storage);
  3707. }
  3708. else
  3709. {
  3710. Data->Save(Storage, false, FactoryDefaults);
  3711. }
  3712. }
  3713. }
  3714. //---------------------------------------------------------------------
  3715. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  3716. bool All, bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  3717. {
  3718. TSessionData * FactoryDefaults = new TSessionData(L"");
  3719. try
  3720. {
  3721. DoSave(Storage, FDefaultSettings, All, RecryptPasswordOnly, FactoryDefaults);
  3722. for (int Index = 0; Index < CountIncludingHidden; Index++)
  3723. {
  3724. TSessionData * SessionData = (TSessionData *)Items[Index];
  3725. try
  3726. {
  3727. DoSave(Storage, SessionData, All, RecryptPasswordOnly, FactoryDefaults);
  3728. }
  3729. catch (Exception & E)
  3730. {
  3731. UnicodeString Message;
  3732. if (RecryptPasswordOnly && DebugAlwaysTrue(RecryptPasswordErrors != NULL) &&
  3733. ExceptionMessage(&E, Message))
  3734. {
  3735. RecryptPasswordErrors->Add(FORMAT("%s: %s", (SessionData->SessionName, Message)));
  3736. }
  3737. else
  3738. {
  3739. throw;
  3740. }
  3741. }
  3742. }
  3743. }
  3744. __finally
  3745. {
  3746. delete FactoryDefaults;
  3747. }
  3748. }
  3749. //---------------------------------------------------------------------
  3750. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage, bool All)
  3751. {
  3752. DoSave(Storage, All, false, NULL);
  3753. }
  3754. //---------------------------------------------------------------------
  3755. void __fastcall TStoredSessionList::DoSave(bool All, bool Explicit,
  3756. bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  3757. {
  3758. bool SessionList = true;
  3759. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  3760. try
  3761. {
  3762. Storage->AccessMode = smReadWrite;
  3763. Storage->Explicit = Explicit;
  3764. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  3765. {
  3766. DoSave(Storage, All, RecryptPasswordOnly, RecryptPasswordErrors);
  3767. }
  3768. }
  3769. __finally
  3770. {
  3771. delete Storage;
  3772. }
  3773. Saved();
  3774. }
  3775. //---------------------------------------------------------------------
  3776. void __fastcall TStoredSessionList::Save(bool All, bool Explicit)
  3777. {
  3778. DoSave(All, Explicit, false, NULL);
  3779. }
  3780. //---------------------------------------------------------------------
  3781. void __fastcall TStoredSessionList::RecryptPasswords(TStrings * RecryptPasswordErrors)
  3782. {
  3783. DoSave(true, true, true, RecryptPasswordErrors);
  3784. }
  3785. //---------------------------------------------------------------------
  3786. void __fastcall TStoredSessionList::Saved()
  3787. {
  3788. FDefaultSettings->Modified = false;
  3789. for (int Index = 0; Index < CountIncludingHidden; Index++)
  3790. {
  3791. ((TSessionData *)Items[Index])->Modified = false;
  3792. }
  3793. }
  3794. //---------------------------------------------------------------------
  3795. void __fastcall TStoredSessionList::ImportLevelFromFilezilla(
  3796. _di_IXMLNode Node, const UnicodeString & Path, _di_IXMLNode SettingsNode)
  3797. {
  3798. for (int Index = 0; Index < Node->ChildNodes->Count; Index++)
  3799. {
  3800. _di_IXMLNode ChildNode = Node->ChildNodes->Get(Index);
  3801. if (ChildNode->NodeName == L"Server")
  3802. {
  3803. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  3804. SessionData->CopyData(DefaultSettings);
  3805. SessionData->ImportFromFilezilla(ChildNode, Path, SettingsNode);
  3806. Add(SessionData.release());
  3807. }
  3808. else if (ChildNode->NodeName == L"Folder")
  3809. {
  3810. UnicodeString Name;
  3811. for (int Index = 0; Index < ChildNode->ChildNodes->Count; Index++)
  3812. {
  3813. _di_IXMLNode PossibleTextMode = ChildNode->ChildNodes->Get(Index);
  3814. if (PossibleTextMode->NodeType == ntText)
  3815. {
  3816. UnicodeString NodeValue = PossibleTextMode->NodeValue;
  3817. AddToList(Name, NodeValue.Trim(), L" ");
  3818. }
  3819. }
  3820. Name = TSessionData::MakeValidName(Name).Trim();
  3821. ImportLevelFromFilezilla(ChildNode, TSessionData::ComposePath(Path, Name), SettingsNode);
  3822. }
  3823. }
  3824. }
  3825. //---------------------------------------------------------------------
  3826. void __fastcall TStoredSessionList::ImportFromFilezilla(
  3827. const UnicodeString FileName, const UnicodeString ConfigurationFileName)
  3828. {
  3829. // not sure if the document must exists if we want to use its node
  3830. _di_IXMLDocument ConfigurationDocument;
  3831. _di_IXMLNode SettingsNode;
  3832. if (FileExists(ApiPath(ConfigurationFileName)))
  3833. {
  3834. ConfigurationDocument = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  3835. ConfigurationDocument->LoadFromFile(ConfigurationFileName);
  3836. _di_IXMLNode FileZilla3Node = ConfigurationDocument->ChildNodes->FindNode(L"FileZilla3");
  3837. if (FileZilla3Node != NULL)
  3838. {
  3839. SettingsNode = FileZilla3Node->ChildNodes->FindNode(L"Settings");
  3840. }
  3841. }
  3842. const _di_IXMLDocument Document = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  3843. Document->LoadFromFile(FileName);
  3844. _di_IXMLNode FileZilla3Node = Document->ChildNodes->FindNode(L"FileZilla3");
  3845. if (FileZilla3Node != NULL)
  3846. {
  3847. _di_IXMLNode ServersNode = FileZilla3Node->ChildNodes->FindNode(L"Servers");
  3848. if (ServersNode != NULL)
  3849. {
  3850. ImportLevelFromFilezilla(ServersNode, L"", SettingsNode);
  3851. }
  3852. }
  3853. }
  3854. //---------------------------------------------------------------------
  3855. void __fastcall TStoredSessionList::ImportFromKnownHosts(TStrings * Lines)
  3856. {
  3857. bool SessionList = false;
  3858. std::unique_ptr<THierarchicalStorage> HostKeyStorage(Configuration->CreateScpStorage(SessionList));
  3859. std::unique_ptr<TStrings> KeyList(new TStringList());
  3860. if (OpenHostKeysSubKey(HostKeyStorage.get(), false))
  3861. {
  3862. HostKeyStorage->GetValueNames(KeyList.get());
  3863. }
  3864. HostKeyStorage.reset(NULL);
  3865. UnicodeString FirstError;
  3866. for (int Index = 0; Index < Lines->Count; Index++)
  3867. {
  3868. try
  3869. {
  3870. UnicodeString Line = Lines->Strings[Index];
  3871. Line = Trim(Line);
  3872. if (!Line.IsEmpty() && (Line[1] != L';'))
  3873. {
  3874. int P = Pos(L' ', Line);
  3875. if (P > 0)
  3876. {
  3877. UnicodeString HostNameStr = Line.SubString(1, P - 1);
  3878. Line = Line.SubString(P + 1, Line.Length() - P);
  3879. P = Pos(L',', HostNameStr);
  3880. if (P > 0)
  3881. {
  3882. HostNameStr.SetLength(P - 1);
  3883. }
  3884. P = Pos(L':', HostNameStr);
  3885. int PortNumber = -1;
  3886. if (P > 0)
  3887. {
  3888. UnicodeString PortNumberStr = HostNameStr.SubString(P + 1, HostNameStr.Length() - P);
  3889. PortNumber = StrToInt(PortNumberStr);
  3890. HostNameStr.SetLength(P - 1);
  3891. }
  3892. if ((HostNameStr.Length() >= 2) &&
  3893. (HostNameStr[1] == L'[') && (HostNameStr[HostNameStr.Length()] == L']'))
  3894. {
  3895. HostNameStr = HostNameStr.SubString(2, HostNameStr.Length() - 2);
  3896. }
  3897. UnicodeString NameStr = HostNameStr;
  3898. if (PortNumber >= 0)
  3899. {
  3900. NameStr = FORMAT(L"%s:%d", (NameStr, PortNumber));
  3901. }
  3902. std::unique_ptr<TSessionData> SessionDataOwner;
  3903. TSessionData * SessionData = dynamic_cast<TSessionData *>(FindByName(NameStr));
  3904. if (SessionData == NULL)
  3905. {
  3906. SessionData = new TSessionData(L"");
  3907. SessionDataOwner.reset(SessionData);
  3908. SessionData->CopyData(DefaultSettings);
  3909. SessionData->Name = NameStr;
  3910. SessionData->HostName = HostNameStr;
  3911. if (PortNumber >= 0)
  3912. {
  3913. SessionData->PortNumber = PortNumber;
  3914. }
  3915. }
  3916. const struct ssh_signkey * Algorithm;
  3917. UnicodeString Key = ParseOpenSshPubLine(Line, Algorithm);
  3918. UnicodeString KeyKey =
  3919. FORMAT(L"%s@%d:%s", (Algorithm->keytype, SessionData->PortNumber, HostNameStr));
  3920. UnicodeString HostKey =
  3921. FORMAT(L"%s:%s=%s", (Algorithm->name, KeyKey, Key));
  3922. UnicodeString HostKeyList = SessionData->HostKey;
  3923. AddToList(HostKeyList, HostKey, L";");
  3924. SessionData->HostKey = HostKeyList;
  3925. // If there's at least one unknown key type for this host, select it
  3926. if (KeyList->IndexOf(KeyKey) < 0)
  3927. {
  3928. SessionData->Selected = true;
  3929. }
  3930. if (SessionDataOwner.get() != NULL)
  3931. {
  3932. Add(SessionDataOwner.release());
  3933. }
  3934. }
  3935. }
  3936. }
  3937. catch (Exception & E)
  3938. {
  3939. if (FirstError.IsEmpty())
  3940. {
  3941. FirstError = E.Message;
  3942. }
  3943. }
  3944. }
  3945. if (Count == 0)
  3946. {
  3947. UnicodeString Message = LoadStr(KNOWN_HOSTS_NO_SITES);
  3948. if (!FirstError.IsEmpty())
  3949. {
  3950. Message = FORMAT(L"%s\n(%s)", (Message, FirstError));
  3951. }
  3952. throw Exception(Message);
  3953. }
  3954. }
  3955. //---------------------------------------------------------------------
  3956. void __fastcall TStoredSessionList::Export(const UnicodeString FileName)
  3957. {
  3958. THierarchicalStorage * Storage = TIniFileStorage::CreateFromPath(FileName);
  3959. try
  3960. {
  3961. Storage->AccessMode = smReadWrite;
  3962. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  3963. {
  3964. Save(Storage, true);
  3965. }
  3966. }
  3967. __finally
  3968. {
  3969. delete Storage;
  3970. }
  3971. }
  3972. //---------------------------------------------------------------------
  3973. void __fastcall TStoredSessionList::SelectAll(bool Select)
  3974. {
  3975. for (int Index = 0; Index < Count; Index++)
  3976. Sessions[Index]->Selected = Select;
  3977. }
  3978. //---------------------------------------------------------------------
  3979. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  3980. bool OnlySelected, TList * Imported)
  3981. {
  3982. for (int Index = 0; Index < From->Count; Index++)
  3983. {
  3984. if (!OnlySelected || From->Sessions[Index]->Selected)
  3985. {
  3986. TSessionData *Session = new TSessionData(L"");
  3987. Session->Assign(From->Sessions[Index]);
  3988. Session->Modified = true;
  3989. Session->MakeUniqueIn(this);
  3990. Add(Session);
  3991. if (Imported != NULL)
  3992. {
  3993. Imported->Add(Session);
  3994. }
  3995. }
  3996. }
  3997. // only modified, explicit
  3998. Save(false, true);
  3999. }
  4000. //---------------------------------------------------------------------
  4001. void __fastcall TStoredSessionList::SelectSessionsToImport
  4002. (TStoredSessionList * Dest, bool SSHOnly)
  4003. {
  4004. for (int Index = 0; Index < Count; Index++)
  4005. {
  4006. Sessions[Index]->Selected =
  4007. (!SSHOnly || (Sessions[Index]->GetNormalizedPuttyProtocol() == PuttySshProtocol)) &&
  4008. !Dest->FindByName(Sessions[Index]->Name);
  4009. }
  4010. }
  4011. //---------------------------------------------------------------------
  4012. void __fastcall TStoredSessionList::Cleanup()
  4013. {
  4014. try
  4015. {
  4016. if (Configuration->Storage == stRegistry) Clear();
  4017. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  4018. try
  4019. {
  4020. Storage->AccessMode = smReadWrite;
  4021. if (Storage->OpenRootKey(False))
  4022. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  4023. }
  4024. __finally
  4025. {
  4026. delete Storage;
  4027. }
  4028. }
  4029. catch (Exception &E)
  4030. {
  4031. throw ExtException(&E, LoadStr(CLEANUP_SESSIONS_ERROR));
  4032. }
  4033. }
  4034. //---------------------------------------------------------------------------
  4035. void __fastcall TStoredSessionList::UpdateStaticUsage()
  4036. {
  4037. int SCP = 0;
  4038. int SFTP = 0;
  4039. int FTP = 0;
  4040. int FTPS = 0;
  4041. int WebDAV = 0;
  4042. int WebDAVS = 0;
  4043. int S3 = 0;
  4044. int Password = 0;
  4045. int Advanced = 0;
  4046. int Color = 0;
  4047. int Note = 0;
  4048. int Tunnel = 0;
  4049. bool Folders = false;
  4050. bool Workspaces = false;
  4051. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  4052. std::unique_ptr<TStringList> DifferentAdvancedProperties(CreateSortedStringList());
  4053. for (int Index = 0; Index < Count; Index++)
  4054. {
  4055. TSessionData * Data = Sessions[Index];
  4056. if (Data->IsWorkspace)
  4057. {
  4058. Workspaces = true;
  4059. }
  4060. else
  4061. {
  4062. switch (Data->FSProtocol)
  4063. {
  4064. case fsSCPonly:
  4065. SCP++;
  4066. break;
  4067. case fsSFTP:
  4068. case fsSFTPonly:
  4069. SFTP++;
  4070. break;
  4071. case fsFTP:
  4072. if (Data->Ftps == ftpsNone)
  4073. {
  4074. FTP++;
  4075. }
  4076. else
  4077. {
  4078. FTPS++;
  4079. }
  4080. break;
  4081. case fsWebDAV:
  4082. if (Data->Ftps == ftpsNone)
  4083. {
  4084. WebDAV++;
  4085. }
  4086. else
  4087. {
  4088. WebDAVS++;
  4089. }
  4090. break;
  4091. case fsS3:
  4092. S3++;
  4093. break;
  4094. }
  4095. if (Data->HasAnySessionPassword())
  4096. {
  4097. Password++;
  4098. }
  4099. if (Data->Color != 0)
  4100. {
  4101. Color++;
  4102. }
  4103. if (!Data->Note.IsEmpty())
  4104. {
  4105. Note++;
  4106. }
  4107. // this effectively does not take passwords (proxy + tunnel) into account,
  4108. // when master password is set, as master password handler in not set up yet
  4109. if (!Data->IsSame(FactoryDefaults.get(), true, DifferentAdvancedProperties.get()))
  4110. {
  4111. Advanced++;
  4112. }
  4113. if (Data->Tunnel)
  4114. {
  4115. Tunnel++;
  4116. }
  4117. if (!Data->FolderName.IsEmpty())
  4118. {
  4119. Folders = true;
  4120. }
  4121. }
  4122. }
  4123. Configuration->Usage->Set(L"StoredSessionsCountSCP", SCP);
  4124. Configuration->Usage->Set(L"StoredSessionsCountSFTP", SFTP);
  4125. Configuration->Usage->Set(L"StoredSessionsCountFTP", FTP);
  4126. Configuration->Usage->Set(L"StoredSessionsCountFTPS", FTPS);
  4127. Configuration->Usage->Set(L"StoredSessionsCountWebDAV", WebDAV);
  4128. Configuration->Usage->Set(L"StoredSessionsCountWebDAVS", WebDAVS);
  4129. Configuration->Usage->Set(L"StoredSessionsCountS3", S3);
  4130. Configuration->Usage->Set(L"StoredSessionsCountPassword", Password);
  4131. Configuration->Usage->Set(L"StoredSessionsCountColor", Color);
  4132. Configuration->Usage->Set(L"StoredSessionsCountNote", Note);
  4133. Configuration->Usage->Set(L"StoredSessionsCountAdvanced", Advanced);
  4134. DifferentAdvancedProperties->Delimiter = L',';
  4135. Configuration->Usage->Set(L"StoredSessionsAdvancedSettings", DifferentAdvancedProperties->DelimitedText);
  4136. Configuration->Usage->Set(L"StoredSessionsCountTunnel", Tunnel);
  4137. // actually default might be true, see below for when the default is actually used
  4138. bool CustomDefaultStoredSession = false;
  4139. try
  4140. {
  4141. // this can throw, when the default session settings have password set
  4142. // (and no other basic property, like hostname/username),
  4143. // and master password is enabled as we are called before master password
  4144. // handler is set
  4145. CustomDefaultStoredSession = !FDefaultSettings->IsSame(FactoryDefaults.get(), false);
  4146. }
  4147. catch (...)
  4148. {
  4149. }
  4150. Configuration->Usage->Set(L"UsingDefaultStoredSession", CustomDefaultStoredSession);
  4151. Configuration->Usage->Set(L"UsingStoredSessionsFolders", Folders);
  4152. Configuration->Usage->Set(L"UsingWorkspaces", Workspaces);
  4153. }
  4154. //---------------------------------------------------------------------------
  4155. TSessionData * __fastcall TStoredSessionList::FindSame(TSessionData * Data)
  4156. {
  4157. TSessionData * Result;
  4158. if (Data->Hidden || Data->Name.IsEmpty() || Data->IsWorkspace)
  4159. {
  4160. Result = NULL;
  4161. }
  4162. else
  4163. {
  4164. Result = dynamic_cast<TSessionData *>(FindByName(Data->Name));
  4165. }
  4166. return Result;
  4167. }
  4168. //---------------------------------------------------------------------------
  4169. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  4170. {
  4171. for (int Index = 0; Index < Count; Index++)
  4172. if (Data == Sessions[Index]) return Index;
  4173. return -1;
  4174. }
  4175. //---------------------------------------------------------------------------
  4176. TSessionData * __fastcall TStoredSessionList::NewSession(
  4177. UnicodeString SessionName, TSessionData * Session)
  4178. {
  4179. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  4180. if (!DuplicateSession)
  4181. {
  4182. DuplicateSession = new TSessionData(L"");
  4183. DuplicateSession->Assign(Session);
  4184. DuplicateSession->Name = SessionName;
  4185. // make sure, that new stored session is saved to registry
  4186. DuplicateSession->Modified = true;
  4187. Add(DuplicateSession);
  4188. }
  4189. else
  4190. {
  4191. DuplicateSession->Assign(Session);
  4192. DuplicateSession->Name = SessionName;
  4193. DuplicateSession->Modified = true;
  4194. }
  4195. // list was saved here before to default storage, but it would not allow
  4196. // to work with special lists (export/import) not using default storage
  4197. return DuplicateSession;
  4198. }
  4199. //---------------------------------------------------------------------------
  4200. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  4201. {
  4202. DebugAssert(FDefaultSettings);
  4203. if (FDefaultSettings != value)
  4204. {
  4205. FDefaultSettings->Assign(value);
  4206. // make sure default settings are saved
  4207. FDefaultSettings->Modified = true;
  4208. FDefaultSettings->Name = DefaultName;
  4209. if (!FReadOnly)
  4210. {
  4211. // only modified, explicit
  4212. Save(false, true);
  4213. }
  4214. }
  4215. }
  4216. //---------------------------------------------------------------------------
  4217. bool __fastcall TStoredSessionList::OpenHostKeysSubKey(THierarchicalStorage * Storage, bool CanCreate)
  4218. {
  4219. return
  4220. Storage->OpenRootKey(CanCreate) &&
  4221. Storage->OpenSubKey(Configuration->SshHostKeysSubKey, CanCreate);
  4222. }
  4223. //---------------------------------------------------------------------------
  4224. THierarchicalStorage * __fastcall TStoredSessionList::CreateHostKeysStorageForWritting()
  4225. {
  4226. bool SessionList = false;
  4227. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateScpStorage(SessionList));
  4228. Storage->Explicit = true;
  4229. Storage->AccessMode = smReadWrite;
  4230. if (!OpenHostKeysSubKey(Storage.get(), true))
  4231. {
  4232. Storage.reset(NULL);
  4233. }
  4234. return Storage.release();
  4235. }
  4236. //---------------------------------------------------------------------------
  4237. void __fastcall TStoredSessionList::ImportHostKeys(
  4238. const UnicodeString SourceKey, TStoredSessionList * Sessions,
  4239. bool OnlySelected)
  4240. {
  4241. TRegistryStorage * SourceStorage = NULL;
  4242. THierarchicalStorage * TargetStorage = NULL;
  4243. TStringList * KeyList = NULL;
  4244. try
  4245. {
  4246. TargetStorage = CreateHostKeysStorageForWritting();
  4247. SourceStorage = new TRegistryStorage(SourceKey);
  4248. KeyList = new TStringList();
  4249. if ((TargetStorage != NULL) &&
  4250. SourceStorage->OpenRootKey(false))
  4251. {
  4252. SourceStorage->GetValueNames(KeyList);
  4253. TSessionData * Session;
  4254. UnicodeString HostKeyName;
  4255. DebugAssert(Sessions != NULL);
  4256. for (int Index = 0; Index < Sessions->Count; Index++)
  4257. {
  4258. Session = Sessions->Sessions[Index];
  4259. if (!OnlySelected || Session->Selected)
  4260. {
  4261. HostKeyName = PuttyMungeStr(FORMAT(L"@%d:%s", (Session->PortNumber, Session->HostNameExpanded)));
  4262. UnicodeString KeyName;
  4263. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  4264. {
  4265. KeyName = KeyList->Strings[KeyIndex];
  4266. int P = KeyName.Pos(HostKeyName);
  4267. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  4268. {
  4269. TargetStorage->WriteStringRaw(KeyName,
  4270. SourceStorage->ReadStringRaw(KeyName, L""));
  4271. }
  4272. }
  4273. }
  4274. }
  4275. }
  4276. }
  4277. __finally
  4278. {
  4279. delete SourceStorage;
  4280. delete TargetStorage;
  4281. delete KeyList;
  4282. }
  4283. }
  4284. //---------------------------------------------------------------------------
  4285. void __fastcall TStoredSessionList::ImportSelectedKnownHosts(TStoredSessionList * Sessions)
  4286. {
  4287. std::unique_ptr<THierarchicalStorage> Storage(CreateHostKeysStorageForWritting());
  4288. if (Storage.get() != NULL)
  4289. {
  4290. for (int Index = 0; Index < Sessions->Count; Index++)
  4291. {
  4292. TSessionData * Session = Sessions->Sessions[Index];
  4293. if (Session->Selected)
  4294. {
  4295. UnicodeString Algs;
  4296. UnicodeString HostKeys = Session->HostKey;
  4297. while (!HostKeys.IsEmpty())
  4298. {
  4299. UnicodeString HostKey = CutToChar(HostKeys, L';', true);
  4300. // skip alg
  4301. CutToChar(HostKey, L':', true);
  4302. UnicodeString Key = CutToChar(HostKey, L'=', true);
  4303. Storage->WriteStringRaw(Key, HostKey);
  4304. }
  4305. }
  4306. }
  4307. }
  4308. }
  4309. //---------------------------------------------------------------------------
  4310. bool __fastcall TStoredSessionList::IsFolderOrWorkspace(
  4311. const UnicodeString & Name, bool Workspace)
  4312. {
  4313. bool Result = false;
  4314. TSessionData * FirstData = NULL;
  4315. if (!Name.IsEmpty())
  4316. {
  4317. for (int Index = 0; !Result && (Index < Count); Index++)
  4318. {
  4319. Result = Sessions[Index]->IsInFolderOrWorkspace(Name);
  4320. if (Result)
  4321. {
  4322. FirstData = Sessions[Index];
  4323. }
  4324. }
  4325. }
  4326. return
  4327. Result &&
  4328. DebugAlwaysTrue(FirstData != NULL) &&
  4329. (FirstData->IsWorkspace == Workspace);
  4330. }
  4331. //---------------------------------------------------------------------------
  4332. bool __fastcall TStoredSessionList::IsFolder(const UnicodeString & Name)
  4333. {
  4334. return IsFolderOrWorkspace(Name, false);
  4335. }
  4336. //---------------------------------------------------------------------------
  4337. bool __fastcall TStoredSessionList::IsWorkspace(const UnicodeString & Name)
  4338. {
  4339. return IsFolderOrWorkspace(Name, true);
  4340. }
  4341. //---------------------------------------------------------------------------
  4342. TSessionData * __fastcall TStoredSessionList::CheckIsInFolderOrWorkspaceAndResolve(
  4343. TSessionData * Data, const UnicodeString & Name)
  4344. {
  4345. if (Data->IsInFolderOrWorkspace(Name))
  4346. {
  4347. Data = ResolveWorkspaceData(Data);
  4348. if ((Data != NULL) && Data->CanLogin &&
  4349. DebugAlwaysTrue(Data->Link.IsEmpty()))
  4350. {
  4351. return Data;
  4352. }
  4353. }
  4354. return NULL;
  4355. }
  4356. //---------------------------------------------------------------------------
  4357. void __fastcall TStoredSessionList::GetFolderOrWorkspace(const UnicodeString & Name, TList * List)
  4358. {
  4359. for (int Index = 0; (Index < Count); Index++)
  4360. {
  4361. TSessionData * RawData = Sessions[Index];
  4362. TSessionData * Data =
  4363. CheckIsInFolderOrWorkspaceAndResolve(RawData, Name);
  4364. if (Data != NULL)
  4365. {
  4366. TSessionData * Data2 = new TSessionData(L"");
  4367. Data2->Assign(Data);
  4368. if (!RawData->Link.IsEmpty() && (DebugAlwaysTrue(Data != RawData)) &&
  4369. // BACKWARD COMPATIBILITY
  4370. // When loading pre-5.6.4 workspace, that does not have state saved,
  4371. // do not overwrite the site "state" defaults
  4372. // with (empty) workspace state
  4373. RawData->HasStateData())
  4374. {
  4375. Data2->CopyStateData(RawData);
  4376. }
  4377. List->Add(Data2);
  4378. }
  4379. }
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. TStrings * __fastcall TStoredSessionList::GetFolderOrWorkspaceList(
  4383. const UnicodeString & Name)
  4384. {
  4385. std::unique_ptr<TStringList> Result(new TStringList());
  4386. for (int Index = 0; (Index < Count); Index++)
  4387. {
  4388. TSessionData * Data =
  4389. CheckIsInFolderOrWorkspaceAndResolve(Sessions[Index], Name);
  4390. if (Data != NULL)
  4391. {
  4392. Result->Add(Data->SessionName);
  4393. }
  4394. }
  4395. return Result.release();
  4396. }
  4397. //---------------------------------------------------------------------------
  4398. TStrings * __fastcall TStoredSessionList::GetWorkspaces()
  4399. {
  4400. std::unique_ptr<TStringList> Result(CreateSortedStringList());
  4401. for (int Index = 0; (Index < Count); Index++)
  4402. {
  4403. TSessionData * Data = Sessions[Index];
  4404. if (Data->IsWorkspace)
  4405. {
  4406. Result->Add(Data->FolderName);
  4407. }
  4408. }
  4409. return Result.release();
  4410. }
  4411. //---------------------------------------------------------------------------
  4412. void __fastcall TStoredSessionList::NewWorkspace(
  4413. UnicodeString Name, TList * DataList)
  4414. {
  4415. for (int Index = 0; (Index < Count); Index++)
  4416. {
  4417. TSessionData * Data = Sessions[Index];
  4418. if (Data->IsInFolderOrWorkspace(Name))
  4419. {
  4420. Data->Remove();
  4421. Remove(Data);
  4422. Index--;
  4423. }
  4424. }
  4425. for (int Index = 0; (Index < DataList->Count); Index++)
  4426. {
  4427. TSessionData * Data = static_cast<TSessionData *>(DataList->Items[Index]);
  4428. TSessionData * Data2 = new TSessionData(L"");
  4429. Data2->Assign(Data);
  4430. Data2->Name = TSessionData::ComposePath(Name, Data->Name);
  4431. // make sure, that new stored session is saved to registry
  4432. Data2->Modified = true;
  4433. Add(Data2);
  4434. }
  4435. }
  4436. //---------------------------------------------------------------------------
  4437. bool __fastcall TStoredSessionList::HasAnyWorkspace()
  4438. {
  4439. bool Result = false;
  4440. for (int Index = 0; !Result && (Index < Count); Index++)
  4441. {
  4442. TSessionData * Data = Sessions[Index];
  4443. Result = Data->IsWorkspace;
  4444. }
  4445. return Result;
  4446. }
  4447. //---------------------------------------------------------------------------
  4448. TSessionData * __fastcall TStoredSessionList::ParseUrl(UnicodeString Url,
  4449. TOptions * Options, bool & DefaultsOnly, UnicodeString * FileName,
  4450. bool * AProtocolDefined, UnicodeString * MaskedUrl)
  4451. {
  4452. TSessionData * Data = new TSessionData(L"");
  4453. try
  4454. {
  4455. Data->ParseUrl(Url, Options, this, DefaultsOnly, FileName, AProtocolDefined, MaskedUrl);
  4456. }
  4457. catch(...)
  4458. {
  4459. delete Data;
  4460. throw;
  4461. }
  4462. return Data;
  4463. }
  4464. //---------------------------------------------------------------------
  4465. bool __fastcall TStoredSessionList::IsUrl(UnicodeString Url)
  4466. {
  4467. bool DefaultsOnly;
  4468. bool ProtocolDefined = false;
  4469. std::unique_ptr<TSessionData> ParsedData(ParseUrl(Url, NULL, DefaultsOnly, NULL, &ProtocolDefined));
  4470. bool Result = ProtocolDefined;
  4471. return Result;
  4472. }
  4473. //---------------------------------------------------------------------
  4474. TSessionData * __fastcall TStoredSessionList::ResolveWorkspaceData(TSessionData * Data)
  4475. {
  4476. if (!Data->Link.IsEmpty())
  4477. {
  4478. Data = dynamic_cast<TSessionData *>(FindByName(Data->Link));
  4479. if (Data != NULL)
  4480. {
  4481. Data = ResolveWorkspaceData(Data);
  4482. }
  4483. }
  4484. return Data;
  4485. }
  4486. //---------------------------------------------------------------------
  4487. TSessionData * __fastcall TStoredSessionList::SaveWorkspaceData(TSessionData * Data)
  4488. {
  4489. std::unique_ptr<TSessionData> Result(new TSessionData(L""));
  4490. TSessionData * SameData = StoredSessions->FindSame(Data);
  4491. if (SameData != NULL)
  4492. {
  4493. Result->CopyStateData(Data);
  4494. Result->Link = Data->Name;
  4495. }
  4496. else
  4497. {
  4498. Result->Assign(Data);
  4499. }
  4500. Result->IsWorkspace = true;
  4501. return Result.release();
  4502. }
  4503. //---------------------------------------------------------------------
  4504. bool __fastcall TStoredSessionList::CanLogin(TSessionData * Data)
  4505. {
  4506. Data = ResolveWorkspaceData(Data);
  4507. return (Data != NULL) && Data->CanLogin;
  4508. }
  4509. //---------------------------------------------------------------------
  4510. UnicodeString GetExpandedLogFileName(UnicodeString LogFileName, TDateTime Started, TSessionData * SessionData)
  4511. {
  4512. // StripPathQuotes should not be needed as we do not feed quotes anymore
  4513. UnicodeString ANewFileName = StripPathQuotes(ExpandEnvironmentVariables(LogFileName));
  4514. for (int Index = 1; Index < ANewFileName.Length(); Index++)
  4515. {
  4516. if (ANewFileName[Index] == L'!')
  4517. {
  4518. UnicodeString Replacement;
  4519. // keep consistent with TFileCustomCommand::PatternReplacement
  4520. switch (tolower(ANewFileName[Index + 1]))
  4521. {
  4522. case L'y':
  4523. Replacement = FormatDateTime(L"yyyy", Started);
  4524. break;
  4525. case L'm':
  4526. Replacement = FormatDateTime(L"mm", Started);
  4527. break;
  4528. case L'd':
  4529. Replacement = FormatDateTime(L"dd", Started);
  4530. break;
  4531. case L't':
  4532. Replacement = FormatDateTime(L"hhnnss", Started);
  4533. break;
  4534. case 'p':
  4535. Replacement = IntToStr(static_cast<int>(GetCurrentProcessId()));
  4536. break;
  4537. case L'@':
  4538. if (SessionData != NULL)
  4539. {
  4540. Replacement = MakeValidFileName(SessionData->HostNameExpanded);
  4541. }
  4542. else
  4543. {
  4544. Replacement = L"nohost";
  4545. }
  4546. break;
  4547. case L's':
  4548. if (SessionData != NULL)
  4549. {
  4550. Replacement = MakeValidFileName(SessionData->SessionName);
  4551. }
  4552. else
  4553. {
  4554. Replacement = L"nosession";
  4555. }
  4556. break;
  4557. case L'!':
  4558. Replacement = L"!";
  4559. break;
  4560. default:
  4561. Replacement = UnicodeString(L"!") + ANewFileName[Index + 1];
  4562. break;
  4563. }
  4564. ANewFileName.Delete(Index, 2);
  4565. ANewFileName.Insert(Replacement, Index);
  4566. Index += Replacement.Length() - 1;
  4567. }
  4568. }
  4569. return ANewFileName;
  4570. }
  4571. //---------------------------------------------------------------------
  4572. bool __fastcall IsSshProtocol(TFSProtocol FSProtocol)
  4573. {
  4574. return
  4575. (FSProtocol == fsSFTPonly) || (FSProtocol == fsSFTP) ||
  4576. (FSProtocol == fsSCPonly);
  4577. }
  4578. //---------------------------------------------------------------------------
  4579. int __fastcall DefaultPort(TFSProtocol FSProtocol, TFtps Ftps)
  4580. {
  4581. int Result;
  4582. switch (FSProtocol)
  4583. {
  4584. case fsFTP:
  4585. if (Ftps == ftpsImplicit)
  4586. {
  4587. Result = FtpsImplicitPortNumber;
  4588. }
  4589. else
  4590. {
  4591. Result = FtpPortNumber;
  4592. }
  4593. break;
  4594. case fsWebDAV:
  4595. case fsS3:
  4596. if (Ftps == ftpsNone)
  4597. {
  4598. Result = HTTPPortNumber;
  4599. }
  4600. else
  4601. {
  4602. Result = HTTPSPortNumber;
  4603. }
  4604. break;
  4605. default:
  4606. if (IsSshProtocol(FSProtocol))
  4607. {
  4608. Result = SshPortNumber;
  4609. }
  4610. else
  4611. {
  4612. DebugFail();
  4613. Result = -1;
  4614. }
  4615. break;
  4616. }
  4617. return Result;
  4618. }
  4619. //---------------------------------------------------------------------