SessionData.cpp 160 KB

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