SessionData.cpp 159 KB

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