SessionData.cpp 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978
  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. static 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 = ConnectionParam;
  1800. FOverrideCachedHostKey = false;
  1801. }
  1802. }
  1803. UnicodeString RawUserName = CutToChar(UserInfo, L':', false);
  1804. UserName = DecodeUrlChars(RawUserName);
  1805. Password = DecodeUrlChars(UserInfo);
  1806. UnicodeString RemoteDirectoryWithSessionParams = Url.SubString(PSlash, Url.Length() - PSlash + 1);
  1807. ARemoteDirectory = CutToChar(RemoteDirectoryWithSessionParams, UrlParamSeparator, false);
  1808. UnicodeString SessionParams = RemoteDirectoryWithSessionParams;
  1809. // We should handle session params in "stored session" branch too.
  1810. // And particularly if there's a "save" param, we should actually not try to match the
  1811. // URL against site names
  1812. while (!SessionParams.IsEmpty())
  1813. {
  1814. UnicodeString SessionParam = CutToChar(SessionParams, UrlParamSeparator, false);
  1815. UnicodeString SessionParamName = CutToChar(SessionParam, UrlParamValueSeparator, false);
  1816. if (SameText(SessionParamName, UrlSaveParamName))
  1817. {
  1818. FSaveOnly = (StrToIntDef(SessionParam, 1) != 0);
  1819. }
  1820. }
  1821. if (MaskedUrl != NULL)
  1822. {
  1823. (*MaskedUrl) += RawUserName;
  1824. if (!UserInfo.IsEmpty())
  1825. {
  1826. (*MaskedUrl) += L":" + PasswordMask;
  1827. }
  1828. if (!RawUserName.IsEmpty() || !UserInfo.IsEmpty())
  1829. {
  1830. (*MaskedUrl) += L"@";
  1831. }
  1832. (*MaskedUrl) += OrigHostInfo + ARemoteDirectory;
  1833. }
  1834. }
  1835. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory != L"/"))
  1836. {
  1837. if ((ARemoteDirectory[ARemoteDirectory.Length()] != L'/') &&
  1838. (FileName != NULL))
  1839. {
  1840. *FileName = DecodeUrlChars(UnixExtractFileName(ARemoteDirectory));
  1841. ARemoteDirectory = UnixExtractFilePath(ARemoteDirectory);
  1842. }
  1843. RemoteDirectory = DecodeUrlChars(ARemoteDirectory);
  1844. }
  1845. DefaultsOnly = false;
  1846. }
  1847. else
  1848. {
  1849. // This happens when pasting URL on Login dialog
  1850. if (StoredSessions != NULL)
  1851. {
  1852. CopyData(StoredSessions->DefaultSettings);
  1853. }
  1854. DefaultsOnly = true;
  1855. }
  1856. if (ProtocolDefined)
  1857. {
  1858. FSProtocol = AFSProtocol;
  1859. }
  1860. if (Options != NULL)
  1861. {
  1862. // we deliberately do keep defaultonly to false, in presence of any option,
  1863. // as the option should not make session "connectable"
  1864. UnicodeString Value;
  1865. if (Options->FindSwitch(SESSIONNAME_SWICH, Value))
  1866. {
  1867. Name = Value;
  1868. }
  1869. if (Options->FindSwitch(NEWPASSWORD_SWITCH, Value))
  1870. {
  1871. ChangePassword = true;
  1872. NewPassword = Value;
  1873. }
  1874. if (Options->FindSwitch(L"privatekey", Value))
  1875. {
  1876. PublicKeyFile = Value;
  1877. }
  1878. if (Options->FindSwitch(L"clientcert", Value))
  1879. {
  1880. TlsCertificateFile = Value;
  1881. }
  1882. if (Options->FindSwitch(PassphraseOption, Value))
  1883. {
  1884. Passphrase = Value;
  1885. }
  1886. if (Options->FindSwitch(L"timeout", Value))
  1887. {
  1888. Timeout = StrToInt(Value);
  1889. }
  1890. if (Options->FindSwitch(L"hostkey", Value) ||
  1891. Options->FindSwitch(L"certificate", Value))
  1892. {
  1893. HostKey = Value;
  1894. FOverrideCachedHostKey = true;
  1895. }
  1896. FtpPasvMode = Options->SwitchValue(L"passive", FtpPasvMode);
  1897. if (Options->FindSwitch(L"implicit"))
  1898. {
  1899. bool Enabled = Options->SwitchValue(L"implicit", true);
  1900. Ftps = Enabled ? ftpsImplicit : ftpsNone;
  1901. if (!PortNumberDefined && Enabled)
  1902. {
  1903. PortNumber = FtpsImplicitPortNumber;
  1904. }
  1905. }
  1906. // BACKWARD COMPATIBILITY with 5.5.x
  1907. if (Options->FindSwitch(L"explicitssl"))
  1908. {
  1909. bool Enabled = Options->SwitchValue(L"explicitssl", true);
  1910. Ftps = Enabled ? ftpsExplicitSsl : ftpsNone;
  1911. if (!PortNumberDefined && Enabled)
  1912. {
  1913. PortNumber = FtpPortNumber;
  1914. }
  1915. }
  1916. if (Options->FindSwitch(L"explicit") ||
  1917. // BACKWARD COMPATIBILITY with 5.5.x
  1918. Options->FindSwitch(L"explicittls"))
  1919. {
  1920. UnicodeString SwitchName =
  1921. Options->FindSwitch(L"explicit") ? L"explicit" : L"explicittls";
  1922. bool Enabled = Options->SwitchValue(SwitchName, true);
  1923. Ftps = Enabled ? ftpsExplicitTls : ftpsNone;
  1924. if (!PortNumberDefined && Enabled)
  1925. {
  1926. PortNumber = FtpPortNumber;
  1927. }
  1928. }
  1929. if (Options->FindSwitch(RawSettingsOption))
  1930. {
  1931. TStrings * RawSettings = NULL;
  1932. TOptionsStorage * OptionsStorage = NULL;
  1933. try
  1934. {
  1935. RawSettings = new TStringList();
  1936. if (Options->FindSwitch(RawSettingsOption, RawSettings))
  1937. {
  1938. OptionsStorage = new TOptionsStorage(RawSettings, false);
  1939. ApplyRawSettings(OptionsStorage);
  1940. }
  1941. }
  1942. __finally
  1943. {
  1944. delete RawSettings;
  1945. delete OptionsStorage;
  1946. }
  1947. }
  1948. }
  1949. return true;
  1950. }
  1951. //---------------------------------------------------------------------
  1952. void __fastcall TSessionData::ApplyRawSettings(THierarchicalStorage * Storage)
  1953. {
  1954. bool Dummy;
  1955. DoLoad(Storage, false, Dummy);
  1956. }
  1957. //---------------------------------------------------------------------
  1958. void __fastcall TSessionData::ConfigureTunnel(int APortNumber)
  1959. {
  1960. FOrigHostName = HostName;
  1961. FOrigPortNumber = PortNumber;
  1962. FOrigProxyMethod = ProxyMethod;
  1963. HostName = L"127.0.0.1";
  1964. PortNumber = APortNumber;
  1965. // proxy settings is used for tunnel
  1966. ProxyMethod = ::pmNone;
  1967. FLogicalHostName = FOrigHostName;
  1968. }
  1969. //---------------------------------------------------------------------
  1970. void __fastcall TSessionData::RollbackTunnel()
  1971. {
  1972. HostName = FOrigHostName;
  1973. PortNumber = FOrigPortNumber;
  1974. ProxyMethod = FOrigProxyMethod;
  1975. FLogicalHostName = L"";
  1976. }
  1977. //---------------------------------------------------------------------
  1978. void __fastcall TSessionData::ExpandEnvironmentVariables()
  1979. {
  1980. HostName = HostNameExpanded;
  1981. UserName = UserNameExpanded;
  1982. PublicKeyFile = ::ExpandEnvironmentVariables(PublicKeyFile);
  1983. }
  1984. //---------------------------------------------------------------------
  1985. void __fastcall TSessionData::ValidatePath(const UnicodeString Path)
  1986. {
  1987. // noop
  1988. }
  1989. //---------------------------------------------------------------------
  1990. void __fastcall TSessionData::ValidateName(const UnicodeString Name)
  1991. {
  1992. // keep consistent with MakeValidName
  1993. if (Name.LastDelimiter(L"/") > 0)
  1994. {
  1995. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, L"/")));
  1996. }
  1997. }
  1998. //---------------------------------------------------------------------
  1999. UnicodeString __fastcall TSessionData::MakeValidName(const UnicodeString & Name)
  2000. {
  2001. // keep consistent with ValidateName
  2002. return ReplaceStr(Name, L"/", L"\\");
  2003. }
  2004. //---------------------------------------------------------------------
  2005. RawByteString __fastcall TSessionData::EncryptPassword(const UnicodeString & Password, UnicodeString Key)
  2006. {
  2007. return Configuration->EncryptPassword(Password, Key);
  2008. }
  2009. //---------------------------------------------------------------------
  2010. RawByteString __fastcall TSessionData::StronglyRecryptPassword(const RawByteString & Password, UnicodeString Key)
  2011. {
  2012. return Configuration->StronglyRecryptPassword(Password, Key);
  2013. }
  2014. //---------------------------------------------------------------------
  2015. UnicodeString __fastcall TSessionData::DecryptPassword(const RawByteString & Password, UnicodeString Key)
  2016. {
  2017. UnicodeString Result;
  2018. try
  2019. {
  2020. Result = Configuration->DecryptPassword(Password, Key);
  2021. }
  2022. catch(EAbort &)
  2023. {
  2024. // silently ignore aborted prompts for master password and return empty password
  2025. }
  2026. return Result;
  2027. }
  2028. //---------------------------------------------------------------------
  2029. bool __fastcall TSessionData::GetCanLogin()
  2030. {
  2031. return !FHostName.IsEmpty();
  2032. }
  2033. //---------------------------------------------------------------------------
  2034. UnicodeString __fastcall TSessionData::GetSessionKey()
  2035. {
  2036. UnicodeString Result = FORMAT(L"%s@%s", (UserName, HostName));
  2037. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  2038. {
  2039. Result += FORMAT(L":%d", (PortNumber));
  2040. }
  2041. return Result;
  2042. }
  2043. //---------------------------------------------------------------------
  2044. UnicodeString __fastcall TSessionData::GetInternalStorageKey()
  2045. {
  2046. // This is probably useless remnant of previous use of this method from OpenSessionInPutty
  2047. // that needs the method to return something even for ad-hoc sessions
  2048. if (Name.IsEmpty())
  2049. {
  2050. return SessionKey;
  2051. }
  2052. else
  2053. {
  2054. return Name;
  2055. }
  2056. }
  2057. //---------------------------------------------------------------------
  2058. UnicodeString __fastcall TSessionData::GetStorageKey()
  2059. {
  2060. return SessionName;
  2061. }
  2062. //---------------------------------------------------------------------
  2063. UnicodeString __fastcall TSessionData::FormatSiteKey(const UnicodeString & HostName, int PortNumber)
  2064. {
  2065. return FORMAT(L"%s:%d", (HostName, PortNumber));
  2066. }
  2067. //---------------------------------------------------------------------
  2068. UnicodeString __fastcall TSessionData::GetSiteKey()
  2069. {
  2070. return FormatSiteKey(HostNameExpanded, PortNumber);
  2071. }
  2072. //---------------------------------------------------------------------
  2073. void __fastcall TSessionData::SetHostName(UnicodeString value)
  2074. {
  2075. if (FHostName != value)
  2076. {
  2077. // HostName is key for password encryption
  2078. UnicodeString XPassword = Password;
  2079. UnicodeString XNewPassword = Password;
  2080. // This is now hardly used as hostname is parsed directly on login dialog.
  2081. // But can be used when importing sites from PuTTY, as it allows same format too.
  2082. int P = value.LastDelimiter(L"@");
  2083. if (P > 0)
  2084. {
  2085. UserName = value.SubString(1, P - 1);
  2086. value = value.SubString(P + 1, value.Length() - P);
  2087. }
  2088. FHostName = value;
  2089. Modify();
  2090. Password = XPassword;
  2091. NewPassword = XNewPassword;
  2092. Shred(XPassword);
  2093. Shred(XNewPassword);
  2094. }
  2095. }
  2096. //---------------------------------------------------------------------
  2097. UnicodeString __fastcall TSessionData::GetHostNameExpanded()
  2098. {
  2099. return ::ExpandEnvironmentVariables(HostName);
  2100. }
  2101. //---------------------------------------------------------------------
  2102. void __fastcall TSessionData::SetPortNumber(int value)
  2103. {
  2104. SET_SESSION_PROPERTY(PortNumber);
  2105. }
  2106. //---------------------------------------------------------------------------
  2107. void __fastcall TSessionData::SetShell(UnicodeString value)
  2108. {
  2109. SET_SESSION_PROPERTY(Shell);
  2110. }
  2111. //---------------------------------------------------------------------------
  2112. void __fastcall TSessionData::SetSftpServer(UnicodeString value)
  2113. {
  2114. SET_SESSION_PROPERTY(SftpServer);
  2115. }
  2116. //---------------------------------------------------------------------------
  2117. void __fastcall TSessionData::SetClearAliases(bool value)
  2118. {
  2119. SET_SESSION_PROPERTY(ClearAliases);
  2120. }
  2121. //---------------------------------------------------------------------------
  2122. void __fastcall TSessionData::SetListingCommand(UnicodeString value)
  2123. {
  2124. SET_SESSION_PROPERTY(ListingCommand);
  2125. }
  2126. //---------------------------------------------------------------------------
  2127. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  2128. {
  2129. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  2130. }
  2131. //---------------------------------------------------------------------------
  2132. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  2133. {
  2134. SET_SESSION_PROPERTY(UnsetNationalVars);
  2135. }
  2136. //---------------------------------------------------------------------
  2137. void __fastcall TSessionData::SetUserName(UnicodeString value)
  2138. {
  2139. // Avoid password recryption (what may popup master password prompt)
  2140. if (FUserName != value)
  2141. {
  2142. // UserName is key for password encryption
  2143. UnicodeString XPassword = Password;
  2144. UnicodeString XNewPassword = NewPassword;
  2145. SET_SESSION_PROPERTY(UserName);
  2146. Password = XPassword;
  2147. NewPassword = XNewPassword;
  2148. Shred(XPassword);
  2149. Shred(XNewPassword);
  2150. }
  2151. }
  2152. //---------------------------------------------------------------------
  2153. UnicodeString __fastcall TSessionData::GetUserNameExpanded()
  2154. {
  2155. return ::ExpandEnvironmentVariables(UserName);
  2156. }
  2157. //---------------------------------------------------------------------
  2158. void __fastcall TSessionData::SetPassword(UnicodeString avalue)
  2159. {
  2160. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  2161. SET_SESSION_PROPERTY(Password);
  2162. }
  2163. //---------------------------------------------------------------------
  2164. UnicodeString __fastcall TSessionData::GetPassword() const
  2165. {
  2166. return DecryptPassword(FPassword, UserName+HostName);
  2167. }
  2168. //---------------------------------------------------------------------
  2169. void __fastcall TSessionData::SetNewPassword(UnicodeString avalue)
  2170. {
  2171. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  2172. SET_SESSION_PROPERTY(NewPassword);
  2173. }
  2174. //---------------------------------------------------------------------
  2175. UnicodeString __fastcall TSessionData::GetNewPassword() const
  2176. {
  2177. return DecryptPassword(FNewPassword, UserName+HostName);
  2178. }
  2179. //---------------------------------------------------------------------
  2180. void __fastcall TSessionData::SetChangePassword(bool value)
  2181. {
  2182. SET_SESSION_PROPERTY(ChangePassword);
  2183. }
  2184. //---------------------------------------------------------------------
  2185. void __fastcall TSessionData::SetPingInterval(int value)
  2186. {
  2187. SET_SESSION_PROPERTY(PingInterval);
  2188. }
  2189. //---------------------------------------------------------------------
  2190. void __fastcall TSessionData::SetTryAgent(bool value)
  2191. {
  2192. SET_SESSION_PROPERTY(TryAgent);
  2193. }
  2194. //---------------------------------------------------------------------
  2195. void __fastcall TSessionData::SetAgentFwd(bool value)
  2196. {
  2197. SET_SESSION_PROPERTY(AgentFwd);
  2198. }
  2199. //---------------------------------------------------------------------
  2200. void __fastcall TSessionData::SetAuthTIS(bool value)
  2201. {
  2202. SET_SESSION_PROPERTY(AuthTIS);
  2203. }
  2204. //---------------------------------------------------------------------
  2205. void __fastcall TSessionData::SetAuthKI(bool value)
  2206. {
  2207. SET_SESSION_PROPERTY(AuthKI);
  2208. }
  2209. //---------------------------------------------------------------------
  2210. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  2211. {
  2212. SET_SESSION_PROPERTY(AuthKIPassword);
  2213. }
  2214. //---------------------------------------------------------------------
  2215. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  2216. {
  2217. SET_SESSION_PROPERTY(AuthGSSAPI);
  2218. }
  2219. //---------------------------------------------------------------------
  2220. void __fastcall TSessionData::SetGSSAPIFwdTGT(bool value)
  2221. {
  2222. SET_SESSION_PROPERTY(GSSAPIFwdTGT);
  2223. }
  2224. //---------------------------------------------------------------------
  2225. void __fastcall TSessionData::SetChangeUsername(bool value)
  2226. {
  2227. SET_SESSION_PROPERTY(ChangeUsername);
  2228. }
  2229. //---------------------------------------------------------------------
  2230. void __fastcall TSessionData::SetCompression(bool value)
  2231. {
  2232. SET_SESSION_PROPERTY(Compression);
  2233. }
  2234. //---------------------------------------------------------------------
  2235. void __fastcall TSessionData::SetSshProt(TSshProt value)
  2236. {
  2237. SET_SESSION_PROPERTY(SshProt);
  2238. }
  2239. //---------------------------------------------------------------------
  2240. void __fastcall TSessionData::SetSsh2DES(bool value)
  2241. {
  2242. SET_SESSION_PROPERTY(Ssh2DES);
  2243. }
  2244. //---------------------------------------------------------------------
  2245. void __fastcall TSessionData::SetSshNoUserAuth(bool value)
  2246. {
  2247. SET_SESSION_PROPERTY(SshNoUserAuth);
  2248. }
  2249. //---------------------------------------------------------------------
  2250. UnicodeString __fastcall TSessionData::GetSshProtStr()
  2251. {
  2252. return SshProtList[FSshProt];
  2253. }
  2254. //---------------------------------------------------------------------
  2255. bool __fastcall TSessionData::GetUsesSsh()
  2256. {
  2257. return IsSshProtocol(FSProtocol);
  2258. }
  2259. //---------------------------------------------------------------------
  2260. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  2261. {
  2262. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  2263. SET_SESSION_PROPERTY(Ciphers[Index]);
  2264. }
  2265. //---------------------------------------------------------------------
  2266. TCipher __fastcall TSessionData::GetCipher(int Index) const
  2267. {
  2268. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  2269. return FCiphers[Index];
  2270. }
  2271. //---------------------------------------------------------------------
  2272. template<class AlgoT>
  2273. void __fastcall TSessionData::SetAlgoList(AlgoT * List, const AlgoT * DefaultList, const UnicodeString * Names,
  2274. int Count, AlgoT WarnAlgo, UnicodeString value)
  2275. {
  2276. std::vector<bool> Used(Count); // initialized to false
  2277. std::vector<AlgoT> NewList(Count);
  2278. bool HasWarnAlgo = (WarnAlgo >= AlgoT());
  2279. const AlgoT * WarnPtr;
  2280. int WarnDefaultIndex;
  2281. if (!HasWarnAlgo)
  2282. {
  2283. WarnPtr = NULL;
  2284. WarnDefaultIndex = -1;
  2285. }
  2286. else
  2287. {
  2288. WarnPtr = std::find(DefaultList, DefaultList + Count, WarnAlgo);
  2289. DebugAssert(WarnPtr != NULL);
  2290. WarnDefaultIndex = (WarnPtr - DefaultList);
  2291. }
  2292. int Index = 0;
  2293. while (!value.IsEmpty())
  2294. {
  2295. UnicodeString AlgoStr = CutToChar(value, L',', true);
  2296. for (int Algo = 0; Algo < Count; Algo++)
  2297. {
  2298. if (!AlgoStr.CompareIC(Names[Algo]) &&
  2299. !Used[Algo] && DebugAlwaysTrue(Index < Count))
  2300. {
  2301. NewList[Index] = (AlgoT)Algo;
  2302. Used[Algo] = true;
  2303. Index++;
  2304. break;
  2305. }
  2306. }
  2307. }
  2308. if (HasWarnAlgo && !Used[WarnAlgo] && DebugAlwaysTrue(Index < Count))
  2309. {
  2310. NewList[Index] = WarnAlgo;
  2311. Used[WarnAlgo] = true;
  2312. Index++;
  2313. }
  2314. int WarnIndex = -1;
  2315. if (HasWarnAlgo)
  2316. {
  2317. WarnIndex = std::find(NewList.begin(), NewList.end(), WarnAlgo) - NewList.begin();
  2318. }
  2319. bool Priority = true;
  2320. for (int DefaultIndex = 0; (DefaultIndex < Count); DefaultIndex++)
  2321. {
  2322. AlgoT DefaultAlgo = DefaultList[DefaultIndex];
  2323. if (!Used[DefaultAlgo] && DebugAlwaysTrue(Index < Count))
  2324. {
  2325. int TargetIndex;
  2326. // Unused algs that are prioritized in the default list,
  2327. // should be merged before the existing custom list
  2328. if (Priority)
  2329. {
  2330. TargetIndex = DefaultIndex;
  2331. }
  2332. else
  2333. {
  2334. if (HasWarnAlgo && (DefaultIndex < WarnDefaultIndex))
  2335. {
  2336. TargetIndex = WarnIndex;
  2337. }
  2338. else
  2339. {
  2340. TargetIndex = Index;
  2341. }
  2342. }
  2343. NewList.insert(NewList.begin() + TargetIndex, DefaultAlgo);
  2344. DebugAssert(NewList.back() == AlgoT());
  2345. NewList.pop_back();
  2346. if (HasWarnAlgo && (TargetIndex <= WarnIndex))
  2347. {
  2348. WarnIndex++;
  2349. }
  2350. Index++;
  2351. }
  2352. else
  2353. {
  2354. Priority = false;
  2355. }
  2356. }
  2357. if (!std::equal(NewList.begin(), NewList.end(), List))
  2358. {
  2359. std::copy(NewList.begin(), NewList.end(), List);
  2360. Modify();
  2361. }
  2362. }
  2363. //---------------------------------------------------------------------
  2364. void __fastcall TSessionData::SetCipherList(UnicodeString value)
  2365. {
  2366. SetAlgoList(FCiphers, DefaultCipherList, CipherNames, CIPHER_COUNT, cipWarn, value);
  2367. }
  2368. //---------------------------------------------------------------------
  2369. UnicodeString __fastcall TSessionData::GetCipherList() const
  2370. {
  2371. UnicodeString Result;
  2372. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  2373. {
  2374. Result += UnicodeString(Index ? L"," : L"") + CipherNames[Cipher[Index]];
  2375. }
  2376. return Result;
  2377. }
  2378. //---------------------------------------------------------------------
  2379. void __fastcall TSessionData::SetKex(int Index, TKex value)
  2380. {
  2381. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  2382. SET_SESSION_PROPERTY(Kex[Index]);
  2383. }
  2384. //---------------------------------------------------------------------
  2385. TKex __fastcall TSessionData::GetKex(int Index) const
  2386. {
  2387. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  2388. return FKex[Index];
  2389. }
  2390. //---------------------------------------------------------------------
  2391. void __fastcall TSessionData::SetKexList(UnicodeString value)
  2392. {
  2393. SetAlgoList(FKex, DefaultKexList, KexNames, KEX_COUNT, kexWarn, value);
  2394. }
  2395. //---------------------------------------------------------------------
  2396. UnicodeString __fastcall TSessionData::GetKexList() const
  2397. {
  2398. UnicodeString Result;
  2399. for (int Index = 0; Index < KEX_COUNT; Index++)
  2400. {
  2401. Result += UnicodeString(Index ? L"," : L"") + KexNames[Kex[Index]];
  2402. }
  2403. return Result;
  2404. }
  2405. //---------------------------------------------------------------------
  2406. void __fastcall TSessionData::SetHostKeys(int Index, THostKey value)
  2407. {
  2408. DebugAssert(Index >= 0 && Index < HOSTKEY_COUNT);
  2409. SET_SESSION_PROPERTY(HostKeys[Index]);
  2410. }
  2411. //---------------------------------------------------------------------
  2412. THostKey __fastcall TSessionData::GetHostKeys(int Index) const
  2413. {
  2414. DebugAssert(Index >= 0 && Index < HOSTKEY_COUNT);
  2415. return FHostKeys[Index];
  2416. }
  2417. //---------------------------------------------------------------------
  2418. void __fastcall TSessionData::SetHostKeyList(UnicodeString value)
  2419. {
  2420. SetAlgoList(FHostKeys, DefaultHostKeyList, HostKeyNames, HOSTKEY_COUNT, hkWarn, value);
  2421. }
  2422. //---------------------------------------------------------------------
  2423. UnicodeString __fastcall TSessionData::GetHostKeyList() const
  2424. {
  2425. UnicodeString Result;
  2426. for (int Index = 0; Index < HOSTKEY_COUNT; Index++)
  2427. {
  2428. Result += UnicodeString(Index ? L"," : L"") + HostKeyNames[HostKeys[Index]];
  2429. }
  2430. return Result;
  2431. }
  2432. //---------------------------------------------------------------------
  2433. void __fastcall TSessionData::SetGssLib(int Index, TGssLib value)
  2434. {
  2435. DebugAssert(Index >= 0 && Index < GSSLIB_COUNT);
  2436. SET_SESSION_PROPERTY(GssLib[Index]);
  2437. }
  2438. //---------------------------------------------------------------------
  2439. TGssLib __fastcall TSessionData::GetGssLib(int Index) const
  2440. {
  2441. DebugAssert(Index >= 0 && Index < GSSLIB_COUNT);
  2442. return FGssLib[Index];
  2443. }
  2444. //---------------------------------------------------------------------
  2445. void __fastcall TSessionData::SetGssLibList(UnicodeString value)
  2446. {
  2447. SetAlgoList(FGssLib, DefaultGssLibList, GssLibNames, GSSLIB_COUNT, TGssLib(-1), value);
  2448. }
  2449. //---------------------------------------------------------------------
  2450. UnicodeString __fastcall TSessionData::GetGssLibList() const
  2451. {
  2452. UnicodeString Result;
  2453. for (int Index = 0; Index < GSSLIB_COUNT; Index++)
  2454. {
  2455. Result += UnicodeString(Index ? L"," : L"") + GssLibNames[GssLib[Index]];
  2456. }
  2457. return Result;
  2458. }
  2459. //---------------------------------------------------------------------
  2460. void __fastcall TSessionData::SetGssLibCustom(UnicodeString value)
  2461. {
  2462. SET_SESSION_PROPERTY(GssLibCustom);
  2463. }
  2464. //---------------------------------------------------------------------
  2465. void __fastcall TSessionData::SetPublicKeyFile(UnicodeString value)
  2466. {
  2467. if (FPublicKeyFile != value)
  2468. {
  2469. // PublicKeyFile is key for Passphrase encryption
  2470. UnicodeString XPassphrase = Passphrase;
  2471. // StripPathQuotes should not be needed as we do not feed quotes anymore
  2472. FPublicKeyFile = StripPathQuotes(value);
  2473. Modify();
  2474. Passphrase = XPassphrase;
  2475. Shred(XPassphrase);
  2476. }
  2477. }
  2478. //---------------------------------------------------------------------
  2479. void __fastcall TSessionData::SetPassphrase(UnicodeString avalue)
  2480. {
  2481. RawByteString value = EncryptPassword(avalue, PublicKeyFile);
  2482. SET_SESSION_PROPERTY(Passphrase);
  2483. }
  2484. //---------------------------------------------------------------------
  2485. UnicodeString __fastcall TSessionData::GetPassphrase() const
  2486. {
  2487. return DecryptPassword(FPassphrase, PublicKeyFile);
  2488. }
  2489. //---------------------------------------------------------------------
  2490. void __fastcall TSessionData::SetReturnVar(UnicodeString value)
  2491. {
  2492. SET_SESSION_PROPERTY(ReturnVar);
  2493. }
  2494. //---------------------------------------------------------------------------
  2495. void __fastcall TSessionData::SetLookupUserGroups(TAutoSwitch value)
  2496. {
  2497. SET_SESSION_PROPERTY(LookupUserGroups);
  2498. }
  2499. //---------------------------------------------------------------------------
  2500. void __fastcall TSessionData::SetEOLType(TEOLType value)
  2501. {
  2502. SET_SESSION_PROPERTY(EOLType);
  2503. }
  2504. //---------------------------------------------------------------------------
  2505. void __fastcall TSessionData::SetTrimVMSVersions(bool value)
  2506. {
  2507. SET_SESSION_PROPERTY(TrimVMSVersions);
  2508. }
  2509. //---------------------------------------------------------------------------
  2510. TDateTime __fastcall TSessionData::GetTimeoutDT()
  2511. {
  2512. return SecToDateTime(Timeout);
  2513. }
  2514. //---------------------------------------------------------------------------
  2515. void __fastcall TSessionData::SetTimeout(int value)
  2516. {
  2517. SET_SESSION_PROPERTY(Timeout);
  2518. }
  2519. //---------------------------------------------------------------------------
  2520. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  2521. {
  2522. SET_SESSION_PROPERTY(FSProtocol);
  2523. }
  2524. //---------------------------------------------------------------------
  2525. UnicodeString __fastcall TSessionData::GetFSProtocolStr()
  2526. {
  2527. DebugAssert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  2528. return FSProtocolNames[FSProtocol];
  2529. }
  2530. //---------------------------------------------------------------------------
  2531. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  2532. {
  2533. if (value != DetectReturnVar)
  2534. {
  2535. ReturnVar = value ? L"" : L"$?";
  2536. }
  2537. }
  2538. //---------------------------------------------------------------------------
  2539. bool __fastcall TSessionData::GetDetectReturnVar()
  2540. {
  2541. return ReturnVar.IsEmpty();
  2542. }
  2543. //---------------------------------------------------------------------------
  2544. void __fastcall TSessionData::SetDefaultShell(bool value)
  2545. {
  2546. if (value != DefaultShell)
  2547. {
  2548. Shell = value ? L"" : L"/bin/bash";
  2549. }
  2550. }
  2551. //---------------------------------------------------------------------------
  2552. bool __fastcall TSessionData::GetDefaultShell()
  2553. {
  2554. return Shell.IsEmpty();
  2555. }
  2556. //---------------------------------------------------------------------------
  2557. void __fastcall TSessionData::SetPuttyProtocol(UnicodeString value)
  2558. {
  2559. SET_SESSION_PROPERTY(PuttyProtocol);
  2560. }
  2561. //---------------------------------------------------------------------
  2562. UnicodeString __fastcall TSessionData::GetNormalizedPuttyProtocol() const
  2563. {
  2564. return DefaultStr(PuttyProtocol, PuttySshProtocol);
  2565. }
  2566. //---------------------------------------------------------------------
  2567. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  2568. {
  2569. unsigned short hour, min, sec, msec;
  2570. value.DecodeTime(&hour, &min, &sec, &msec);
  2571. PingInterval = ((int)hour)*SecsPerHour + ((int)min)*SecsPerMin + sec;
  2572. }
  2573. //---------------------------------------------------------------------------
  2574. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  2575. {
  2576. return SecToDateTime(PingInterval);
  2577. }
  2578. //---------------------------------------------------------------------------
  2579. void __fastcall TSessionData::SetPingType(TPingType value)
  2580. {
  2581. SET_SESSION_PROPERTY(PingType);
  2582. }
  2583. //---------------------------------------------------------------------------
  2584. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  2585. {
  2586. SET_SESSION_PROPERTY(AddressFamily);
  2587. }
  2588. //---------------------------------------------------------------------------
  2589. void __fastcall TSessionData::SetRekeyData(UnicodeString value)
  2590. {
  2591. SET_SESSION_PROPERTY(RekeyData);
  2592. }
  2593. //---------------------------------------------------------------------------
  2594. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  2595. {
  2596. SET_SESSION_PROPERTY(RekeyTime);
  2597. }
  2598. //---------------------------------------------------------------------
  2599. UnicodeString __fastcall TSessionData::GetDefaultSessionName()
  2600. {
  2601. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  2602. {
  2603. // If we ever choose to include port number,
  2604. // we have to escape IPv6 literals in HostName
  2605. return FORMAT(L"%s@%s", (UserName, HostName));
  2606. }
  2607. else if (!HostName.IsEmpty())
  2608. {
  2609. return HostName;
  2610. }
  2611. else
  2612. {
  2613. return L"session";
  2614. }
  2615. }
  2616. //---------------------------------------------------------------------
  2617. UnicodeString __fastcall TSessionData::GetNameWithoutHiddenPrefix()
  2618. {
  2619. UnicodeString Result = Name;
  2620. if (Hidden)
  2621. {
  2622. Result = Result.SubString(TNamedObjectList::HiddenPrefix.Length() + 1, Result.Length() - TNamedObjectList::HiddenPrefix.Length());
  2623. }
  2624. return Result;
  2625. }
  2626. //---------------------------------------------------------------------
  2627. bool __fastcall TSessionData::HasSessionName()
  2628. {
  2629. return (!GetNameWithoutHiddenPrefix().IsEmpty() && (Name != DefaultName) && !IsWorkspace);
  2630. }
  2631. //---------------------------------------------------------------------
  2632. UnicodeString __fastcall TSessionData::GetSessionName()
  2633. {
  2634. UnicodeString Result;
  2635. if (HasSessionName())
  2636. {
  2637. Result = GetNameWithoutHiddenPrefix();
  2638. }
  2639. else
  2640. {
  2641. Result = DefaultSessionName;
  2642. }
  2643. return Result;
  2644. }
  2645. //---------------------------------------------------------------------
  2646. bool __fastcall TSessionData::IsSecure()
  2647. {
  2648. bool Result;
  2649. switch (FSProtocol)
  2650. {
  2651. case fsSCPonly:
  2652. case fsSFTP:
  2653. case fsSFTPonly:
  2654. Result = true;
  2655. break;
  2656. case fsFTP:
  2657. case fsWebDAV:
  2658. case fsS3:
  2659. Result = (Ftps != ftpsNone);
  2660. break;
  2661. default:
  2662. DebugFail();
  2663. break;
  2664. }
  2665. return Result;
  2666. }
  2667. //---------------------------------------------------------------------
  2668. UnicodeString __fastcall TSessionData::GetProtocolUrl()
  2669. {
  2670. UnicodeString Url;
  2671. switch (FSProtocol)
  2672. {
  2673. case fsSCPonly:
  2674. Url = ScpProtocol;
  2675. break;
  2676. default:
  2677. DebugFail();
  2678. // fallback
  2679. case fsSFTP:
  2680. case fsSFTPonly:
  2681. Url = SftpProtocol;
  2682. break;
  2683. case fsFTP:
  2684. if (Ftps == ftpsImplicit)
  2685. {
  2686. Url = FtpsProtocol;
  2687. }
  2688. else if ((Ftps == ftpsExplicitTls) || (Ftps == ftpsExplicitSsl))
  2689. {
  2690. Url = FtpesProtocol;
  2691. }
  2692. else
  2693. {
  2694. Url = FtpProtocol;
  2695. }
  2696. break;
  2697. case fsWebDAV:
  2698. if (Ftps == ftpsImplicit)
  2699. {
  2700. Url = WebDAVSProtocol;
  2701. }
  2702. else
  2703. {
  2704. Url = WebDAVProtocol;
  2705. }
  2706. break;
  2707. case fsS3:
  2708. Url = S3Protocol;
  2709. break;
  2710. }
  2711. Url += ProtocolSeparator;
  2712. return Url;
  2713. }
  2714. //---------------------------------------------------------------------
  2715. bool __fastcall IsIPv6Literal(const UnicodeString & HostName)
  2716. {
  2717. bool Result = (HostName.Pos(L":") > 0);
  2718. if (Result)
  2719. {
  2720. for (int Index = 1; Result && (Index <= HostName.Length()); Index++)
  2721. {
  2722. wchar_t C = HostName[Index];
  2723. Result = IsHex(C) || (C == L':');
  2724. }
  2725. }
  2726. return Result;
  2727. }
  2728. //---------------------------------------------------------------------
  2729. UnicodeString __fastcall EscapeIPv6Literal(const UnicodeString & IP)
  2730. {
  2731. return L"[" + IP + L"]";
  2732. }
  2733. //---------------------------------------------------------------------
  2734. UnicodeString __fastcall TSessionData::GenerateSessionUrl(unsigned int Flags)
  2735. {
  2736. UnicodeString Url;
  2737. if (FLAGSET(Flags, sufSpecific))
  2738. {
  2739. Url += WinSCPProtocolPrefix;
  2740. }
  2741. Url += GetProtocolUrl();
  2742. if (FLAGSET(Flags, sufUserName) && !UserNameExpanded.IsEmpty())
  2743. {
  2744. Url += EncodeUrlString(UserNameExpanded);
  2745. if (FLAGSET(Flags, sufPassword) && !Password.IsEmpty())
  2746. {
  2747. Url += L":" + EncodeUrlString(Password);
  2748. }
  2749. if (FLAGSET(Flags, sufHostKey) && !HostKey.IsEmpty())
  2750. {
  2751. Url +=
  2752. UnicodeString(UrlParamSeparator) + UrlHostKeyParamName +
  2753. UnicodeString(UrlParamValueSeparator) + NormalizeFingerprint(HostKey);
  2754. }
  2755. Url += L"@";
  2756. }
  2757. DebugAssert(!HostNameExpanded.IsEmpty());
  2758. if (IsIPv6Literal(HostNameExpanded))
  2759. {
  2760. Url += EscapeIPv6Literal(HostNameExpanded);
  2761. }
  2762. else
  2763. {
  2764. Url += EncodeUrlString(HostNameExpanded);
  2765. }
  2766. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  2767. {
  2768. Url += L":" + IntToStr(PortNumber);
  2769. }
  2770. Url += L"/";
  2771. return Url;
  2772. }
  2773. //---------------------------------------------------------------------
  2774. UnicodeString ScriptCommandOpenLink(TraceInitStr(ScriptCommandLink(L"open")));
  2775. //---------------------------------------------------------------------
  2776. void __fastcall TSessionData::AddSwitch(
  2777. UnicodeString & Result, const UnicodeString & Name, bool Rtf)
  2778. {
  2779. Result += RtfSwitch(Name, ScriptCommandOpenLink, Rtf);
  2780. }
  2781. //---------------------------------------------------------------------
  2782. void __fastcall TSessionData::AddSwitch(
  2783. UnicodeString & Result, const UnicodeString & Name, const UnicodeString & Value, bool Rtf)
  2784. {
  2785. Result += RtfSwitch(Name, ScriptCommandOpenLink, Value, Rtf);
  2786. }
  2787. //---------------------------------------------------------------------
  2788. void __fastcall TSessionData::AddSwitch(
  2789. UnicodeString & Result, const UnicodeString & Name, int Value, bool Rtf)
  2790. {
  2791. Result += RtfSwitch(Name, ScriptCommandOpenLink, Value, Rtf);
  2792. }
  2793. //---------------------------------------------------------------------
  2794. void __fastcall TSessionData::LookupLastFingerprint()
  2795. {
  2796. UnicodeString FingerprintType;
  2797. if (IsSshProtocol(FSProtocol))
  2798. {
  2799. FingerprintType = SshFingerprintType;
  2800. }
  2801. else if (Ftps != ftpsNone)
  2802. {
  2803. FingerprintType = TlsFingerprintType;
  2804. }
  2805. if (!FingerprintType.IsEmpty())
  2806. {
  2807. HostKey = Configuration->LastFingerprint(SiteKey, FingerprintType);
  2808. }
  2809. }
  2810. //---------------------------------------------------------------------
  2811. UnicodeString __fastcall TSessionData::GenerateOpenCommandArgs(bool Rtf)
  2812. {
  2813. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  2814. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  2815. SessionData->Assign(this);
  2816. UnicodeString Result = SessionData->GenerateSessionUrl(sufOpen);
  2817. // Before we reset the FSProtocol
  2818. bool AUsesSsh = SessionData->UsesSsh;
  2819. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  2820. if (SessionData->FSProtocol != fsSFTPonly)
  2821. {
  2822. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  2823. }
  2824. SessionData->HostName = FactoryDefaults->HostName;
  2825. SessionData->PortNumber = FactoryDefaults->PortNumber;
  2826. SessionData->UserName = FactoryDefaults->UserName;
  2827. SessionData->Password = FactoryDefaults->Password;
  2828. SessionData->CopyNonCoreData(FactoryDefaults.get());
  2829. SessionData->Ftps = FactoryDefaults->Ftps;
  2830. if (SessionData->HostKey != FactoryDefaults->HostKey)
  2831. {
  2832. UnicodeString SwitchName = AUsesSsh ? L"hostkey" : L"certificate";
  2833. AddSwitch(Result, SwitchName, SessionData->HostKey, Rtf);
  2834. SessionData->HostKey = FactoryDefaults->HostKey;
  2835. }
  2836. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  2837. {
  2838. AddSwitch(Result, L"privatekey", SessionData->PublicKeyFile, Rtf);
  2839. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  2840. }
  2841. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  2842. {
  2843. AddSwitch(Result, L"clientcert", SessionData->TlsCertificateFile, Rtf);
  2844. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  2845. }
  2846. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  2847. {
  2848. AddSwitch(Result, PassphraseOption, SessionData->Passphrase, Rtf);
  2849. SessionData->Passphrase = FactoryDefaults->Passphrase;
  2850. }
  2851. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  2852. {
  2853. AddSwitch(Result, L"passive", SessionData->FtpPasvMode ? 1 : 0, Rtf);
  2854. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  2855. }
  2856. if (SessionData->Timeout != FactoryDefaults->Timeout)
  2857. {
  2858. AddSwitch(Result, L"timeout", SessionData->Timeout, Rtf);
  2859. SessionData->Timeout = FactoryDefaults->Timeout;
  2860. }
  2861. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get()));
  2862. if (RawSettings->Count > 0)
  2863. {
  2864. AddSwitch(Result, RawSettingsOption, Rtf);
  2865. Result += StringsToParams(RawSettings.get());
  2866. }
  2867. return Result;
  2868. }
  2869. //---------------------------------------------------------------------
  2870. UnicodeString SessionOptionsClassName(L"SessionOptions");
  2871. //---------------------------------------------------------------------
  2872. void __fastcall TSessionData::AddAssemblyProperty(
  2873. UnicodeString & Result, TAssemblyLanguage Language,
  2874. const UnicodeString & Name, const UnicodeString & Type,
  2875. const UnicodeString & Member)
  2876. {
  2877. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Type, Member, false);
  2878. }
  2879. //---------------------------------------------------------------------
  2880. void __fastcall TSessionData::AddAssemblyProperty(
  2881. UnicodeString & Result, TAssemblyLanguage Language,
  2882. const UnicodeString & Name, const UnicodeString & Value)
  2883. {
  2884. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  2885. }
  2886. //---------------------------------------------------------------------
  2887. void __fastcall TSessionData::AddAssemblyProperty(
  2888. UnicodeString & Result, TAssemblyLanguage Language,
  2889. const UnicodeString & Name, int Value)
  2890. {
  2891. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  2892. }
  2893. //---------------------------------------------------------------------
  2894. void __fastcall TSessionData::AddAssemblyProperty(
  2895. UnicodeString & Result, TAssemblyLanguage Language,
  2896. const UnicodeString & Name, bool Value)
  2897. {
  2898. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  2899. }
  2900. //---------------------------------------------------------------------
  2901. void __fastcall TSessionData::GenerateAssemblyCode(
  2902. TAssemblyLanguage Language, UnicodeString & Head, UnicodeString & Tail, int & Indent)
  2903. {
  2904. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  2905. std::unique_ptr<TSessionData> SessionData(Clone());
  2906. switch (Language)
  2907. {
  2908. case alCSharp:
  2909. case alVBNET:
  2910. // noop
  2911. break;
  2912. case alPowerShell:
  2913. Head +=
  2914. AssemblyCommentLine(Language, LoadStr(CODE_PS_ADD_TYPE)) +
  2915. RtfKeyword(L"Add-Type") + RtfText(" -Path ") + AssemblyString(Language, "WinSCPnet.dll") + RtfPara +
  2916. RtfPara;
  2917. break;
  2918. default:
  2919. DebugFail();
  2920. break;
  2921. }
  2922. Head +=
  2923. AssemblyCommentLine(Language, LoadStr(CODE_SESSION_OPTIONS)) +
  2924. AssemblyNewClassInstanceStart(Language, SessionOptionsClassName, false);
  2925. UnicodeString ProtocolMember;
  2926. switch (SessionData->FSProtocol)
  2927. {
  2928. case fsSCPonly:
  2929. ProtocolMember = "Scp";
  2930. break;
  2931. default:
  2932. DebugFail();
  2933. // fallback
  2934. case fsSFTP:
  2935. case fsSFTPonly:
  2936. ProtocolMember = "Sftp";
  2937. break;
  2938. case fsFTP:
  2939. ProtocolMember = "Ftp";
  2940. break;
  2941. case fsWebDAV:
  2942. ProtocolMember = "Webdav";
  2943. break;
  2944. case fsS3:
  2945. ProtocolMember = "S3";
  2946. break;
  2947. }
  2948. // Before we reset the FSProtocol
  2949. bool AUsesSsh = SessionData->UsesSsh;
  2950. // Protocol is set unconditionally, we want even the default SFTP
  2951. AddAssemblyProperty(Head, Language, L"Protocol", L"Protocol", ProtocolMember);
  2952. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  2953. if (SessionData->FSProtocol != fsSFTPonly)
  2954. {
  2955. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  2956. }
  2957. if (SessionData->HostName != FactoryDefaults->HostName)
  2958. {
  2959. AddAssemblyProperty(Head, Language, L"HostName", HostName);
  2960. SessionData->HostName = FactoryDefaults->HostName;
  2961. }
  2962. if (SessionData->PortNumber != FactoryDefaults->PortNumber)
  2963. {
  2964. AddAssemblyProperty(Head, Language, L"PortNumber", PortNumber);
  2965. SessionData->PortNumber = FactoryDefaults->PortNumber;
  2966. }
  2967. if (SessionData->UserName != FactoryDefaults->UserName)
  2968. {
  2969. AddAssemblyProperty(Head, Language, L"UserName", UserName);
  2970. SessionData->UserName = FactoryDefaults->UserName;
  2971. }
  2972. if (SessionData->Password != FactoryDefaults->Password)
  2973. {
  2974. AddAssemblyProperty(Head, Language, L"Password", Password);
  2975. SessionData->Password = FactoryDefaults->Password;
  2976. }
  2977. SessionData->CopyNonCoreData(FactoryDefaults.get());
  2978. if (SessionData->Ftps != FactoryDefaults->Ftps)
  2979. {
  2980. // SessionData->FSProtocol is reset already
  2981. switch (FSProtocol)
  2982. {
  2983. case fsFTP:
  2984. {
  2985. UnicodeString FtpSecureMember;
  2986. switch (SessionData->Ftps)
  2987. {
  2988. case ftpsNone:
  2989. // noop
  2990. break;
  2991. case ftpsImplicit:
  2992. FtpSecureMember = L"Implicit";
  2993. break;
  2994. case ftpsExplicitTls:
  2995. case ftpsExplicitSsl:
  2996. FtpSecureMember = L"Explicit";
  2997. break;
  2998. default:
  2999. DebugFail();
  3000. break;
  3001. }
  3002. AddAssemblyProperty(Head, Language, L"FtpSecure", L"FtpSecure", FtpSecureMember);
  3003. }
  3004. break;
  3005. case fsWebDAV:
  3006. AddAssemblyProperty(Head, Language, L"WebdavSecure", (SessionData->Ftps != ftpsNone));
  3007. break;
  3008. case fsS3:
  3009. // implicit
  3010. break;
  3011. default:
  3012. DebugFail();
  3013. break;
  3014. }
  3015. SessionData->Ftps = FactoryDefaults->Ftps;
  3016. }
  3017. if (SessionData->HostKey != FactoryDefaults->HostKey)
  3018. {
  3019. UnicodeString PropertyName = AUsesSsh ? L"SshHostKeyFingerprint" : L"TlsHostCertificateFingerprint";
  3020. AddAssemblyProperty(Head, Language, PropertyName, SessionData->HostKey);
  3021. SessionData->HostKey = FactoryDefaults->HostKey;
  3022. }
  3023. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  3024. {
  3025. AddAssemblyProperty(Head, Language, L"SshPrivateKeyPath", SessionData->PublicKeyFile);
  3026. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  3027. }
  3028. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  3029. {
  3030. AddAssemblyProperty(Head, Language, L"TlsClientCertificatePath", SessionData->TlsCertificateFile);
  3031. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  3032. }
  3033. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  3034. {
  3035. AddAssemblyProperty(Head, Language, L"PrivateKeyPassphrase", SessionData->Passphrase);
  3036. SessionData->Passphrase = FactoryDefaults->Passphrase;
  3037. }
  3038. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  3039. {
  3040. AddAssemblyProperty(Head, Language, L"FtpMode", L"FtpMode", (SessionData->FtpPasvMode ? L"Passive" : L"Active"));
  3041. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  3042. }
  3043. if (SessionData->Timeout != FactoryDefaults->Timeout)
  3044. {
  3045. AddAssemblyProperty(Head, Language, L"TimeoutInMilliseconds", SessionData->Timeout * 1000);
  3046. SessionData->Timeout = FactoryDefaults->Timeout;
  3047. }
  3048. switch (Language)
  3049. {
  3050. case alCSharp:
  3051. case alPowerShell:
  3052. Head += AssemblyNewClassInstanceEnd(Language, false);
  3053. break;
  3054. case alVBNET:
  3055. // noop
  3056. // Ending With only after AddRawSettings
  3057. break;
  3058. }
  3059. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get()));
  3060. UnicodeString SessionOptionsVariableName = AssemblyVariableName(Language, SessionOptionsClassName);
  3061. if (RawSettings->Count > 0)
  3062. {
  3063. Head += RtfPara;
  3064. for (int Index = 0; Index < RawSettings->Count; Index++)
  3065. {
  3066. UnicodeString Name = RawSettings->Names[Index];
  3067. UnicodeString Value = RawSettings->ValueFromIndex[Index];
  3068. UnicodeString AddRawSettingsMethod =
  3069. RtfLibraryMethod(SessionOptionsClassName, L"AddRawSettings", false) +
  3070. FORMAT(L"(%s, %s)", (AssemblyString(Language, Name), AssemblyString(Language, Value)));
  3071. switch (Language)
  3072. {
  3073. case alCSharp:
  3074. Head += RtfText(SessionOptionsVariableName + L".") + AddRawSettingsMethod + RtfText(L";") + RtfPara;
  3075. break;
  3076. case alVBNET:
  3077. Head += RtfText(L" .") + AddRawSettingsMethod + RtfPara;
  3078. break;
  3079. case alPowerShell:
  3080. Head += RtfText(SessionOptionsVariableName + L".") + AddRawSettingsMethod + RtfPara;
  3081. break;
  3082. }
  3083. }
  3084. }
  3085. UnicodeString Indentation = L" ";
  3086. UnicodeString SessionVariableName = AssemblyVariableName(Language, SessionClassName);
  3087. UnicodeString RtfSessionClass = RtfLibraryClass(SessionClassName);
  3088. UnicodeString RtfSessionOpenMethod = RtfLibraryMethod(SessionClassName, L"Open", false);
  3089. UnicodeString NewSessionInstance = AssemblyNewClassInstance(Language, SessionClassName, false);
  3090. UnicodeString OpenCall =
  3091. Indentation + AssemblyCommentLine(Language, LoadStr(CODE_CONNECT)) +
  3092. Indentation + RtfText(SessionVariableName + L".") + RtfSessionOpenMethod + RtfText(L"(" + SessionOptionsVariableName + L")") +
  3093. AssemblyStatementSeparator(Language) + RtfPara;
  3094. switch (Language)
  3095. {
  3096. case alCSharp:
  3097. Head +=
  3098. RtfPara +
  3099. RtfKeyword(L"using") + RtfText(" (") + NewSessionInstance + RtfText(L"())") + RtfPara +
  3100. RtfText(L"{") + RtfPara +
  3101. OpenCall;
  3102. Tail =
  3103. RtfText(L"}") + RtfPara;
  3104. break;
  3105. case alVBNET:
  3106. Head +=
  3107. AssemblyNewClassInstanceEnd(Language, false) +
  3108. RtfPara +
  3109. RtfKeyword(L"Using") + RtfText(L" ") + NewSessionInstance + RtfPara +
  3110. OpenCall;
  3111. Tail =
  3112. RtfKeyword(L"End Using");
  3113. break;
  3114. case alPowerShell:
  3115. Head +=
  3116. RtfPara +
  3117. NewSessionInstance + RtfPara +
  3118. RtfPara +
  3119. RtfKeyword(L"try") + RtfPara +
  3120. RtfText(L"{") + RtfPara +
  3121. OpenCall;
  3122. Tail =
  3123. RtfText(L"}") + RtfPara +
  3124. RtfKeyword(L"finally") + RtfPara +
  3125. RtfText(L"{") + RtfPara +
  3126. RtfText(Indentation + SessionVariableName + L".") +
  3127. RtfLibraryMethod(SessionClassName, L"Dispose", false) + RtfText(L"()") + RtfPara +
  3128. RtfText(L"}") + RtfPara;
  3129. break;
  3130. }
  3131. Head += RtfPara;
  3132. Indent = 4; // the same for all languages so far
  3133. }
  3134. //---------------------------------------------------------------------
  3135. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  3136. {
  3137. SET_SESSION_PROPERTY(TimeDifference);
  3138. }
  3139. //---------------------------------------------------------------------
  3140. void __fastcall TSessionData::SetTimeDifferenceAuto(bool value)
  3141. {
  3142. SET_SESSION_PROPERTY(TimeDifferenceAuto);
  3143. }
  3144. //---------------------------------------------------------------------
  3145. void __fastcall TSessionData::SetLocalDirectory(UnicodeString value)
  3146. {
  3147. SET_SESSION_PROPERTY(LocalDirectory);
  3148. }
  3149. //---------------------------------------------------------------------
  3150. void __fastcall TSessionData::SetRemoteDirectory(UnicodeString value)
  3151. {
  3152. SET_SESSION_PROPERTY(RemoteDirectory);
  3153. }
  3154. //---------------------------------------------------------------------
  3155. void __fastcall TSessionData::SetSynchronizeBrowsing(bool value)
  3156. {
  3157. SET_SESSION_PROPERTY(SynchronizeBrowsing);
  3158. }
  3159. //---------------------------------------------------------------------
  3160. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  3161. {
  3162. SET_SESSION_PROPERTY(UpdateDirectories);
  3163. }
  3164. //---------------------------------------------------------------------
  3165. void __fastcall TSessionData::SetCacheDirectories(bool value)
  3166. {
  3167. SET_SESSION_PROPERTY(CacheDirectories);
  3168. }
  3169. //---------------------------------------------------------------------
  3170. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  3171. {
  3172. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  3173. }
  3174. //---------------------------------------------------------------------
  3175. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  3176. {
  3177. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  3178. }
  3179. //---------------------------------------------------------------------
  3180. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  3181. {
  3182. SET_SESSION_PROPERTY(ResolveSymlinks);
  3183. }
  3184. //---------------------------------------------------------------------
  3185. void __fastcall TSessionData::SetFollowDirectorySymlinks(bool value)
  3186. {
  3187. SET_SESSION_PROPERTY(FollowDirectorySymlinks);
  3188. }
  3189. //---------------------------------------------------------------------------
  3190. void __fastcall TSessionData::SetDSTMode(TDSTMode value)
  3191. {
  3192. SET_SESSION_PROPERTY(DSTMode);
  3193. }
  3194. //---------------------------------------------------------------------------
  3195. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  3196. {
  3197. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  3198. }
  3199. //---------------------------------------------------------------------------
  3200. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  3201. {
  3202. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  3203. }
  3204. //---------------------------------------------------------------------------
  3205. void __fastcall TSessionData::SetRecycleBinPath(UnicodeString value)
  3206. {
  3207. SET_SESSION_PROPERTY(RecycleBinPath);
  3208. }
  3209. //---------------------------------------------------------------------------
  3210. void __fastcall TSessionData::SetPostLoginCommands(UnicodeString value)
  3211. {
  3212. SET_SESSION_PROPERTY(PostLoginCommands);
  3213. }
  3214. //---------------------------------------------------------------------
  3215. void __fastcall TSessionData::SetLockInHome(bool value)
  3216. {
  3217. SET_SESSION_PROPERTY(LockInHome);
  3218. }
  3219. //---------------------------------------------------------------------
  3220. void __fastcall TSessionData::SetSpecial(bool value)
  3221. {
  3222. SET_SESSION_PROPERTY(Special);
  3223. }
  3224. //---------------------------------------------------------------------------
  3225. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  3226. {
  3227. SET_SESSION_PROPERTY(Scp1Compatibility);
  3228. }
  3229. //---------------------------------------------------------------------
  3230. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  3231. {
  3232. SET_SESSION_PROPERTY(TcpNoDelay);
  3233. }
  3234. //---------------------------------------------------------------------
  3235. void __fastcall TSessionData::SetSendBuf(int value)
  3236. {
  3237. SET_SESSION_PROPERTY(SendBuf);
  3238. }
  3239. //---------------------------------------------------------------------
  3240. void __fastcall TSessionData::SetSshSimple(bool value)
  3241. {
  3242. SET_SESSION_PROPERTY(SshSimple);
  3243. }
  3244. //---------------------------------------------------------------------
  3245. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  3246. {
  3247. SET_SESSION_PROPERTY(ProxyMethod);
  3248. }
  3249. //---------------------------------------------------------------------
  3250. void __fastcall TSessionData::SetProxyHost(UnicodeString value)
  3251. {
  3252. SET_SESSION_PROPERTY(ProxyHost);
  3253. }
  3254. //---------------------------------------------------------------------
  3255. void __fastcall TSessionData::SetProxyPort(int value)
  3256. {
  3257. SET_SESSION_PROPERTY(ProxyPort);
  3258. }
  3259. //---------------------------------------------------------------------
  3260. void __fastcall TSessionData::SetProxyUsername(UnicodeString value)
  3261. {
  3262. SET_SESSION_PROPERTY(ProxyUsername);
  3263. }
  3264. //---------------------------------------------------------------------
  3265. void __fastcall TSessionData::SetProxyPassword(UnicodeString avalue)
  3266. {
  3267. RawByteString value = EncryptPassword(avalue, ProxyUsername+ProxyHost);
  3268. SET_SESSION_PROPERTY(ProxyPassword);
  3269. }
  3270. //---------------------------------------------------------------------
  3271. UnicodeString __fastcall TSessionData::GetProxyPassword() const
  3272. {
  3273. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  3274. }
  3275. //---------------------------------------------------------------------
  3276. void __fastcall TSessionData::SetProxyTelnetCommand(UnicodeString value)
  3277. {
  3278. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  3279. }
  3280. //---------------------------------------------------------------------
  3281. void __fastcall TSessionData::SetProxyLocalCommand(UnicodeString value)
  3282. {
  3283. SET_SESSION_PROPERTY(ProxyLocalCommand);
  3284. }
  3285. //---------------------------------------------------------------------
  3286. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  3287. {
  3288. SET_SESSION_PROPERTY(ProxyDNS);
  3289. }
  3290. //---------------------------------------------------------------------
  3291. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  3292. {
  3293. SET_SESSION_PROPERTY(ProxyLocalhost);
  3294. }
  3295. //---------------------------------------------------------------------
  3296. void __fastcall TSessionData::SetFtpProxyLogonType(int value)
  3297. {
  3298. SET_SESSION_PROPERTY(FtpProxyLogonType);
  3299. }
  3300. //---------------------------------------------------------------------
  3301. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  3302. {
  3303. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  3304. SET_SESSION_PROPERTY(Bugs[Bug]);
  3305. }
  3306. //---------------------------------------------------------------------
  3307. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  3308. {
  3309. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  3310. return FBugs[Bug];
  3311. }
  3312. //---------------------------------------------------------------------
  3313. void __fastcall TSessionData::SetCustomParam1(UnicodeString value)
  3314. {
  3315. SET_SESSION_PROPERTY(CustomParam1);
  3316. }
  3317. //---------------------------------------------------------------------
  3318. void __fastcall TSessionData::SetCustomParam2(UnicodeString value)
  3319. {
  3320. SET_SESSION_PROPERTY(CustomParam2);
  3321. }
  3322. //---------------------------------------------------------------------
  3323. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  3324. {
  3325. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  3326. }
  3327. //---------------------------------------------------------------------
  3328. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  3329. {
  3330. SET_SESSION_PROPERTY(SFTPUploadQueue);
  3331. }
  3332. //---------------------------------------------------------------------
  3333. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  3334. {
  3335. SET_SESSION_PROPERTY(SFTPListingQueue);
  3336. }
  3337. //---------------------------------------------------------------------
  3338. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  3339. {
  3340. SET_SESSION_PROPERTY(SFTPMaxVersion);
  3341. }
  3342. //---------------------------------------------------------------------
  3343. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  3344. {
  3345. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  3346. }
  3347. //---------------------------------------------------------------------
  3348. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  3349. {
  3350. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  3351. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  3352. }
  3353. //---------------------------------------------------------------------
  3354. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  3355. {
  3356. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  3357. return FSFTPBugs[Bug];
  3358. }
  3359. //---------------------------------------------------------------------
  3360. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  3361. {
  3362. SET_SESSION_PROPERTY(SCPLsFullTime);
  3363. }
  3364. //---------------------------------------------------------------------------
  3365. void __fastcall TSessionData::SetColor(int value)
  3366. {
  3367. SET_SESSION_PROPERTY(Color);
  3368. }
  3369. //---------------------------------------------------------------------------
  3370. void __fastcall TSessionData::SetTunnel(bool value)
  3371. {
  3372. SET_SESSION_PROPERTY(Tunnel);
  3373. }
  3374. //---------------------------------------------------------------------
  3375. void __fastcall TSessionData::SetTunnelHostName(UnicodeString value)
  3376. {
  3377. if (FTunnelHostName != value)
  3378. {
  3379. // HostName is key for password encryption
  3380. UnicodeString XTunnelPassword = TunnelPassword;
  3381. int P = value.LastDelimiter(L"@");
  3382. if (P > 0)
  3383. {
  3384. TunnelUserName = value.SubString(1, P - 1);
  3385. value = value.SubString(P + 1, value.Length() - P);
  3386. }
  3387. FTunnelHostName = value;
  3388. Modify();
  3389. TunnelPassword = XTunnelPassword;
  3390. Shred(XTunnelPassword);
  3391. }
  3392. }
  3393. //---------------------------------------------------------------------
  3394. void __fastcall TSessionData::SetTunnelPortNumber(int value)
  3395. {
  3396. SET_SESSION_PROPERTY(TunnelPortNumber);
  3397. }
  3398. //---------------------------------------------------------------------
  3399. void __fastcall TSessionData::SetTunnelUserName(UnicodeString value)
  3400. {
  3401. // Avoid password recryption (what may popup master password prompt)
  3402. if (FTunnelUserName != value)
  3403. {
  3404. // TunnelUserName is key for password encryption
  3405. UnicodeString XTunnelPassword = TunnelPassword;
  3406. SET_SESSION_PROPERTY(TunnelUserName);
  3407. TunnelPassword = XTunnelPassword;
  3408. Shred(XTunnelPassword);
  3409. }
  3410. }
  3411. //---------------------------------------------------------------------
  3412. void __fastcall TSessionData::SetTunnelPassword(UnicodeString avalue)
  3413. {
  3414. RawByteString value = EncryptPassword(avalue, TunnelUserName+TunnelHostName);
  3415. SET_SESSION_PROPERTY(TunnelPassword);
  3416. }
  3417. //---------------------------------------------------------------------
  3418. UnicodeString __fastcall TSessionData::GetTunnelPassword() const
  3419. {
  3420. return DecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName);
  3421. }
  3422. //---------------------------------------------------------------------
  3423. void __fastcall TSessionData::SetTunnelPublicKeyFile(UnicodeString value)
  3424. {
  3425. if (FTunnelPublicKeyFile != value)
  3426. {
  3427. // StripPathQuotes should not be needed as we do not feed quotes anymore
  3428. FTunnelPublicKeyFile = StripPathQuotes(value);
  3429. Modify();
  3430. }
  3431. }
  3432. //---------------------------------------------------------------------
  3433. void __fastcall TSessionData::SetTunnelLocalPortNumber(int value)
  3434. {
  3435. SET_SESSION_PROPERTY(TunnelLocalPortNumber);
  3436. }
  3437. //---------------------------------------------------------------------
  3438. bool __fastcall TSessionData::GetTunnelAutoassignLocalPortNumber()
  3439. {
  3440. return (FTunnelLocalPortNumber <= 0);
  3441. }
  3442. //---------------------------------------------------------------------
  3443. void __fastcall TSessionData::SetTunnelPortFwd(UnicodeString value)
  3444. {
  3445. SET_SESSION_PROPERTY(TunnelPortFwd);
  3446. }
  3447. //---------------------------------------------------------------------
  3448. void __fastcall TSessionData::SetTunnelHostKey(UnicodeString value)
  3449. {
  3450. SET_SESSION_PROPERTY(TunnelHostKey);
  3451. }
  3452. //---------------------------------------------------------------------
  3453. void __fastcall TSessionData::SetFtpPasvMode(bool value)
  3454. {
  3455. SET_SESSION_PROPERTY(FtpPasvMode);
  3456. }
  3457. //---------------------------------------------------------------------
  3458. void __fastcall TSessionData::SetFtpForcePasvIp(TAutoSwitch value)
  3459. {
  3460. SET_SESSION_PROPERTY(FtpForcePasvIp);
  3461. }
  3462. //---------------------------------------------------------------------
  3463. void __fastcall TSessionData::SetFtpUseMlsd(TAutoSwitch value)
  3464. {
  3465. SET_SESSION_PROPERTY(FtpUseMlsd);
  3466. }
  3467. //---------------------------------------------------------------------
  3468. void __fastcall TSessionData::SetFtpAccount(UnicodeString value)
  3469. {
  3470. SET_SESSION_PROPERTY(FtpAccount);
  3471. }
  3472. //---------------------------------------------------------------------
  3473. void __fastcall TSessionData::SetFtpPingInterval(int value)
  3474. {
  3475. SET_SESSION_PROPERTY(FtpPingInterval);
  3476. }
  3477. //---------------------------------------------------------------------------
  3478. TDateTime __fastcall TSessionData::GetFtpPingIntervalDT()
  3479. {
  3480. return SecToDateTime(FtpPingInterval);
  3481. }
  3482. //---------------------------------------------------------------------------
  3483. void __fastcall TSessionData::SetFtpPingType(TPingType value)
  3484. {
  3485. SET_SESSION_PROPERTY(FtpPingType);
  3486. }
  3487. //---------------------------------------------------------------------------
  3488. void __fastcall TSessionData::SetFtpTransferActiveImmediately(TAutoSwitch value)
  3489. {
  3490. SET_SESSION_PROPERTY(FtpTransferActiveImmediately);
  3491. }
  3492. //---------------------------------------------------------------------------
  3493. void __fastcall TSessionData::SetFtps(TFtps value)
  3494. {
  3495. SET_SESSION_PROPERTY(Ftps);
  3496. }
  3497. //---------------------------------------------------------------------------
  3498. void __fastcall TSessionData::SetMinTlsVersion(TTlsVersion value)
  3499. {
  3500. SET_SESSION_PROPERTY(MinTlsVersion);
  3501. }
  3502. //---------------------------------------------------------------------------
  3503. void __fastcall TSessionData::SetMaxTlsVersion(TTlsVersion value)
  3504. {
  3505. SET_SESSION_PROPERTY(MaxTlsVersion);
  3506. }
  3507. //---------------------------------------------------------------------------
  3508. void __fastcall TSessionData::SetLogicalHostName(UnicodeString value)
  3509. {
  3510. SET_SESSION_PROPERTY(LogicalHostName);
  3511. }
  3512. //---------------------------------------------------------------------
  3513. void __fastcall TSessionData::SetFtpListAll(TAutoSwitch value)
  3514. {
  3515. SET_SESSION_PROPERTY(FtpListAll);
  3516. }
  3517. //---------------------------------------------------------------------
  3518. void __fastcall TSessionData::SetFtpHost(TAutoSwitch value)
  3519. {
  3520. SET_SESSION_PROPERTY(FtpHost);
  3521. }
  3522. //---------------------------------------------------------------------
  3523. void __fastcall TSessionData::SetFtpDeleteFromCwd(TAutoSwitch value)
  3524. {
  3525. SET_SESSION_PROPERTY(FtpDeleteFromCwd);
  3526. }
  3527. //---------------------------------------------------------------------
  3528. void __fastcall TSessionData::SetSslSessionReuse(bool value)
  3529. {
  3530. SET_SESSION_PROPERTY(SslSessionReuse);
  3531. }
  3532. //---------------------------------------------------------------------
  3533. void __fastcall TSessionData::SetTlsCertificateFile(UnicodeString value)
  3534. {
  3535. SET_SESSION_PROPERTY(TlsCertificateFile);
  3536. }
  3537. //---------------------------------------------------------------------
  3538. void __fastcall TSessionData::SetNotUtf(TAutoSwitch value)
  3539. {
  3540. SET_SESSION_PROPERTY(NotUtf);
  3541. }
  3542. //---------------------------------------------------------------------
  3543. void __fastcall TSessionData::SetInternalEditorEncoding(int value)
  3544. {
  3545. SET_SESSION_PROPERTY(InternalEditorEncoding);
  3546. }
  3547. //---------------------------------------------------------------------
  3548. void __fastcall TSessionData::SetS3DefaultRegion(UnicodeString value)
  3549. {
  3550. SET_SESSION_PROPERTY(S3DefaultRegion);
  3551. }
  3552. //---------------------------------------------------------------------
  3553. void __fastcall TSessionData::SetIsWorkspace(bool value)
  3554. {
  3555. SET_SESSION_PROPERTY(IsWorkspace);
  3556. }
  3557. //---------------------------------------------------------------------
  3558. void __fastcall TSessionData::SetLink(UnicodeString value)
  3559. {
  3560. SET_SESSION_PROPERTY(Link);
  3561. }
  3562. //---------------------------------------------------------------------
  3563. void __fastcall TSessionData::SetHostKey(UnicodeString value)
  3564. {
  3565. SET_SESSION_PROPERTY(HostKey);
  3566. }
  3567. //---------------------------------------------------------------------
  3568. void __fastcall TSessionData::SetNote(UnicodeString value)
  3569. {
  3570. SET_SESSION_PROPERTY(Note);
  3571. }
  3572. //---------------------------------------------------------------------
  3573. void __fastcall TSessionData::SetWinTitle(UnicodeString value)
  3574. {
  3575. SET_SESSION_PROPERTY(WinTitle);
  3576. }
  3577. //---------------------------------------------------------------------
  3578. UnicodeString __fastcall TSessionData::GetInfoTip()
  3579. {
  3580. if (UsesSsh)
  3581. {
  3582. return FMTLOAD(SESSION_INFO_TIP2,
  3583. (HostName, UserName,
  3584. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  3585. FSProtocolStr));
  3586. }
  3587. else
  3588. {
  3589. return FMTLOAD(SESSION_INFO_TIP_NO_SSH,
  3590. (HostName, UserName, FSProtocolStr));
  3591. }
  3592. }
  3593. //---------------------------------------------------------------------
  3594. UnicodeString __fastcall TSessionData::ExtractLocalName(const UnicodeString & Name)
  3595. {
  3596. UnicodeString Result = Name;
  3597. int P = Result.LastDelimiter(L"/");
  3598. if (P > 0)
  3599. {
  3600. Result.Delete(1, P);
  3601. }
  3602. return Result;
  3603. }
  3604. //---------------------------------------------------------------------
  3605. UnicodeString __fastcall TSessionData::GetLocalName()
  3606. {
  3607. UnicodeString Result;
  3608. if (HasSessionName())
  3609. {
  3610. Result = ExtractLocalName(Name);
  3611. }
  3612. else
  3613. {
  3614. Result = DefaultSessionName;
  3615. }
  3616. return Result;
  3617. }
  3618. //---------------------------------------------------------------------
  3619. UnicodeString __fastcall TSessionData::ExtractFolderName(const UnicodeString & Name)
  3620. {
  3621. UnicodeString Result;
  3622. int P = Name.LastDelimiter(L"/");
  3623. if (P > 0)
  3624. {
  3625. Result = Name.SubString(1, P - 1);
  3626. }
  3627. return Result;
  3628. }
  3629. //---------------------------------------------------------------------
  3630. UnicodeString __fastcall TSessionData::GetFolderName()
  3631. {
  3632. UnicodeString Result;
  3633. if (HasSessionName() || IsWorkspace)
  3634. {
  3635. Result = ExtractFolderName(Name);
  3636. }
  3637. return Result;
  3638. }
  3639. //---------------------------------------------------------------------
  3640. UnicodeString __fastcall TSessionData::ComposePath(
  3641. const UnicodeString & Path, const UnicodeString & Name)
  3642. {
  3643. return UnixIncludeTrailingBackslash(Path) + Name;
  3644. }
  3645. //---------------------------------------------------------------------
  3646. void __fastcall TSessionData::DisableAuthentationsExceptPassword()
  3647. {
  3648. SshNoUserAuth = false;
  3649. AuthTIS = false;
  3650. AuthKI = false;
  3651. AuthKIPassword = false;
  3652. AuthGSSAPI = false;
  3653. PublicKeyFile = L"";
  3654. TlsCertificateFile = L"";
  3655. Passphrase = L"";
  3656. TryAgent = false;
  3657. }
  3658. //=== TStoredSessionList ----------------------------------------------
  3659. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  3660. TNamedObjectList(), FReadOnly(aReadOnly)
  3661. {
  3662. DebugAssert(Configuration);
  3663. FDefaultSettings = new TSessionData(DefaultName);
  3664. }
  3665. //---------------------------------------------------------------------
  3666. __fastcall TStoredSessionList::~TStoredSessionList()
  3667. {
  3668. DebugAssert(Configuration);
  3669. delete FDefaultSettings;
  3670. }
  3671. //---------------------------------------------------------------------
  3672. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  3673. bool AsModified, bool UseDefaults, bool PuttyImport)
  3674. {
  3675. TStringList *SubKeys = new TStringList();
  3676. TList * Loaded = new TList;
  3677. try
  3678. {
  3679. DebugAssert(AutoSort);
  3680. AutoSort = false;
  3681. bool WasEmpty = (Count == 0);
  3682. Storage->GetSubKeyNames(SubKeys);
  3683. for (int Index = 0; Index < SubKeys->Count; Index++)
  3684. {
  3685. UnicodeString SessionName = SubKeys->Strings[Index];
  3686. bool ValidName = true;
  3687. try
  3688. {
  3689. TSessionData::ValidatePath(SessionName);
  3690. }
  3691. catch(...)
  3692. {
  3693. ValidName = false;
  3694. }
  3695. if (ValidName)
  3696. {
  3697. TSessionData * SessionData;
  3698. if (SessionName == FDefaultSettings->Name)
  3699. {
  3700. SessionData = FDefaultSettings;
  3701. }
  3702. else
  3703. {
  3704. // if the list was empty before loading, do not waste time trying to
  3705. // find existing sites to overwrite (we rely on underlying storage
  3706. // to secure uniqueness of the key names)
  3707. if (WasEmpty)
  3708. {
  3709. SessionData = NULL;
  3710. }
  3711. else
  3712. {
  3713. SessionData = (TSessionData*)FindByName(SessionName);
  3714. }
  3715. }
  3716. if ((SessionData != FDefaultSettings) || !UseDefaults)
  3717. {
  3718. if (SessionData == NULL)
  3719. {
  3720. SessionData = new TSessionData(L"");
  3721. if (UseDefaults)
  3722. {
  3723. SessionData->CopyData(DefaultSettings);
  3724. }
  3725. SessionData->Name = SessionName;
  3726. Add(SessionData);
  3727. }
  3728. Loaded->Add(SessionData);
  3729. SessionData->Load(Storage, PuttyImport);
  3730. if (AsModified)
  3731. {
  3732. SessionData->Modified = true;
  3733. }
  3734. }
  3735. }
  3736. }
  3737. if (!AsModified)
  3738. {
  3739. for (int Index = 0; Index < TObjectList::Count; Index++)
  3740. {
  3741. if (Loaded->IndexOf(Items[Index]) < 0)
  3742. {
  3743. Delete(Index);
  3744. Index--;
  3745. }
  3746. }
  3747. }
  3748. }
  3749. __finally
  3750. {
  3751. AutoSort = true;
  3752. AlphaSort();
  3753. delete SubKeys;
  3754. delete Loaded;
  3755. }
  3756. }
  3757. //---------------------------------------------------------------------
  3758. void __fastcall TStoredSessionList::Load()
  3759. {
  3760. bool SessionList = true;
  3761. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  3762. try
  3763. {
  3764. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  3765. Load(Storage);
  3766. }
  3767. __finally
  3768. {
  3769. delete Storage;
  3770. }
  3771. }
  3772. //---------------------------------------------------------------------
  3773. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  3774. TSessionData * Data, bool All, bool RecryptPasswordOnly,
  3775. TSessionData * FactoryDefaults)
  3776. {
  3777. if (All || Data->Modified)
  3778. {
  3779. if (RecryptPasswordOnly)
  3780. {
  3781. Data->SaveRecryptedPasswords(Storage);
  3782. }
  3783. else
  3784. {
  3785. Data->Save(Storage, false, FactoryDefaults);
  3786. }
  3787. }
  3788. }
  3789. //---------------------------------------------------------------------
  3790. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  3791. bool All, bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  3792. {
  3793. TSessionData * FactoryDefaults = new TSessionData(L"");
  3794. try
  3795. {
  3796. DoSave(Storage, FDefaultSettings, All, RecryptPasswordOnly, FactoryDefaults);
  3797. for (int Index = 0; Index < CountIncludingHidden; Index++)
  3798. {
  3799. TSessionData * SessionData = (TSessionData *)Items[Index];
  3800. try
  3801. {
  3802. DoSave(Storage, SessionData, All, RecryptPasswordOnly, FactoryDefaults);
  3803. }
  3804. catch (Exception & E)
  3805. {
  3806. UnicodeString Message;
  3807. if (RecryptPasswordOnly && DebugAlwaysTrue(RecryptPasswordErrors != NULL) &&
  3808. ExceptionMessage(&E, Message))
  3809. {
  3810. RecryptPasswordErrors->Add(FORMAT("%s: %s", (SessionData->SessionName, Message)));
  3811. }
  3812. else
  3813. {
  3814. throw;
  3815. }
  3816. }
  3817. }
  3818. }
  3819. __finally
  3820. {
  3821. delete FactoryDefaults;
  3822. }
  3823. }
  3824. //---------------------------------------------------------------------
  3825. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage, bool All)
  3826. {
  3827. DoSave(Storage, All, false, NULL);
  3828. }
  3829. //---------------------------------------------------------------------
  3830. void __fastcall TStoredSessionList::DoSave(bool All, bool Explicit,
  3831. bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  3832. {
  3833. bool SessionList = true;
  3834. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  3835. try
  3836. {
  3837. Storage->AccessMode = smReadWrite;
  3838. Storage->Explicit = Explicit;
  3839. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  3840. {
  3841. DoSave(Storage, All, RecryptPasswordOnly, RecryptPasswordErrors);
  3842. }
  3843. }
  3844. __finally
  3845. {
  3846. delete Storage;
  3847. }
  3848. Saved();
  3849. }
  3850. //---------------------------------------------------------------------
  3851. void __fastcall TStoredSessionList::Save(bool All, bool Explicit)
  3852. {
  3853. DoSave(All, Explicit, false, NULL);
  3854. }
  3855. //---------------------------------------------------------------------
  3856. void __fastcall TStoredSessionList::RecryptPasswords(TStrings * RecryptPasswordErrors)
  3857. {
  3858. DoSave(true, true, true, RecryptPasswordErrors);
  3859. }
  3860. //---------------------------------------------------------------------
  3861. void __fastcall TStoredSessionList::Saved()
  3862. {
  3863. FDefaultSettings->Modified = false;
  3864. for (int Index = 0; Index < CountIncludingHidden; Index++)
  3865. {
  3866. ((TSessionData *)Items[Index])->Modified = false;
  3867. }
  3868. }
  3869. //---------------------------------------------------------------------
  3870. void __fastcall TStoredSessionList::ImportLevelFromFilezilla(
  3871. _di_IXMLNode Node, const UnicodeString & Path, _di_IXMLNode SettingsNode)
  3872. {
  3873. for (int Index = 0; Index < Node->ChildNodes->Count; Index++)
  3874. {
  3875. _di_IXMLNode ChildNode = Node->ChildNodes->Get(Index);
  3876. if (ChildNode->NodeName == L"Server")
  3877. {
  3878. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  3879. SessionData->CopyData(DefaultSettings);
  3880. SessionData->ImportFromFilezilla(ChildNode, Path, SettingsNode);
  3881. Add(SessionData.release());
  3882. }
  3883. else if (ChildNode->NodeName == L"Folder")
  3884. {
  3885. UnicodeString Name;
  3886. for (int Index = 0; Index < ChildNode->ChildNodes->Count; Index++)
  3887. {
  3888. _di_IXMLNode PossibleTextMode = ChildNode->ChildNodes->Get(Index);
  3889. if (PossibleTextMode->NodeType == ntText)
  3890. {
  3891. UnicodeString NodeValue = PossibleTextMode->NodeValue;
  3892. AddToList(Name, NodeValue.Trim(), L" ");
  3893. }
  3894. }
  3895. Name = TSessionData::MakeValidName(Name).Trim();
  3896. ImportLevelFromFilezilla(ChildNode, TSessionData::ComposePath(Path, Name), SettingsNode);
  3897. }
  3898. }
  3899. }
  3900. //---------------------------------------------------------------------
  3901. void __fastcall TStoredSessionList::ImportFromFilezilla(
  3902. const UnicodeString FileName, const UnicodeString ConfigurationFileName)
  3903. {
  3904. // not sure if the document must exists if we want to use its node
  3905. _di_IXMLDocument ConfigurationDocument;
  3906. _di_IXMLNode SettingsNode;
  3907. if (FileExists(ApiPath(ConfigurationFileName)))
  3908. {
  3909. ConfigurationDocument = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  3910. ConfigurationDocument->LoadFromFile(ConfigurationFileName);
  3911. _di_IXMLNode FileZilla3Node = ConfigurationDocument->ChildNodes->FindNode(L"FileZilla3");
  3912. if (FileZilla3Node != NULL)
  3913. {
  3914. SettingsNode = FileZilla3Node->ChildNodes->FindNode(L"Settings");
  3915. }
  3916. }
  3917. const _di_IXMLDocument Document = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  3918. Document->LoadFromFile(FileName);
  3919. _di_IXMLNode FileZilla3Node = Document->ChildNodes->FindNode(L"FileZilla3");
  3920. if (FileZilla3Node != NULL)
  3921. {
  3922. _di_IXMLNode ServersNode = FileZilla3Node->ChildNodes->FindNode(L"Servers");
  3923. if (ServersNode != NULL)
  3924. {
  3925. ImportLevelFromFilezilla(ServersNode, L"", SettingsNode);
  3926. }
  3927. }
  3928. }
  3929. //---------------------------------------------------------------------
  3930. void __fastcall TStoredSessionList::ImportFromKnownHosts(TStrings * Lines)
  3931. {
  3932. bool SessionList = false;
  3933. std::unique_ptr<THierarchicalStorage> HostKeyStorage(Configuration->CreateScpStorage(SessionList));
  3934. std::unique_ptr<TStrings> KeyList(new TStringList());
  3935. if (OpenHostKeysSubKey(HostKeyStorage.get(), false))
  3936. {
  3937. HostKeyStorage->GetValueNames(KeyList.get());
  3938. }
  3939. HostKeyStorage.reset(NULL);
  3940. UnicodeString FirstError;
  3941. for (int Index = 0; Index < Lines->Count; Index++)
  3942. {
  3943. try
  3944. {
  3945. UnicodeString Line = Lines->Strings[Index];
  3946. Line = Trim(Line);
  3947. if (!Line.IsEmpty() && (Line[1] != L';'))
  3948. {
  3949. int P = Pos(L' ', Line);
  3950. if (P > 0)
  3951. {
  3952. UnicodeString HostNameStr = Line.SubString(1, P - 1);
  3953. Line = Line.SubString(P + 1, Line.Length() - P);
  3954. P = Pos(L',', HostNameStr);
  3955. if (P > 0)
  3956. {
  3957. HostNameStr.SetLength(P - 1);
  3958. }
  3959. P = Pos(L':', HostNameStr);
  3960. int PortNumber = -1;
  3961. if (P > 0)
  3962. {
  3963. UnicodeString PortNumberStr = HostNameStr.SubString(P + 1, HostNameStr.Length() - P);
  3964. PortNumber = StrToInt(PortNumberStr);
  3965. HostNameStr.SetLength(P - 1);
  3966. }
  3967. if ((HostNameStr.Length() >= 2) &&
  3968. (HostNameStr[1] == L'[') && (HostNameStr[HostNameStr.Length()] == L']'))
  3969. {
  3970. HostNameStr = HostNameStr.SubString(2, HostNameStr.Length() - 2);
  3971. }
  3972. UnicodeString NameStr = HostNameStr;
  3973. if (PortNumber >= 0)
  3974. {
  3975. NameStr = FORMAT(L"%s:%d", (NameStr, PortNumber));
  3976. }
  3977. std::unique_ptr<TSessionData> SessionDataOwner;
  3978. TSessionData * SessionData = dynamic_cast<TSessionData *>(FindByName(NameStr));
  3979. if (SessionData == NULL)
  3980. {
  3981. SessionData = new TSessionData(L"");
  3982. SessionDataOwner.reset(SessionData);
  3983. SessionData->CopyData(DefaultSettings);
  3984. SessionData->Name = NameStr;
  3985. SessionData->HostName = HostNameStr;
  3986. if (PortNumber >= 0)
  3987. {
  3988. SessionData->PortNumber = PortNumber;
  3989. }
  3990. }
  3991. const struct ssh_signkey * Algorithm;
  3992. UnicodeString Key = ParseOpenSshPubLine(Line, Algorithm);
  3993. UnicodeString KeyKey =
  3994. FORMAT(L"%s@%d:%s", (Algorithm->keytype, SessionData->PortNumber, HostNameStr));
  3995. UnicodeString HostKey =
  3996. FORMAT(L"%s:%s=%s", (Algorithm->name, KeyKey, Key));
  3997. UnicodeString HostKeyList = SessionData->HostKey;
  3998. AddToList(HostKeyList, HostKey, L";");
  3999. SessionData->HostKey = HostKeyList;
  4000. // If there's at least one unknown key type for this host, select it
  4001. if (KeyList->IndexOf(KeyKey) < 0)
  4002. {
  4003. SessionData->Selected = true;
  4004. }
  4005. if (SessionDataOwner.get() != NULL)
  4006. {
  4007. Add(SessionDataOwner.release());
  4008. }
  4009. }
  4010. }
  4011. }
  4012. catch (Exception & E)
  4013. {
  4014. if (FirstError.IsEmpty())
  4015. {
  4016. FirstError = E.Message;
  4017. }
  4018. }
  4019. }
  4020. if (Count == 0)
  4021. {
  4022. UnicodeString Message = LoadStr(KNOWN_HOSTS_NO_SITES);
  4023. if (!FirstError.IsEmpty())
  4024. {
  4025. Message = FORMAT(L"%s\n(%s)", (Message, FirstError));
  4026. }
  4027. throw Exception(Message);
  4028. }
  4029. }
  4030. //---------------------------------------------------------------------
  4031. void __fastcall TStoredSessionList::Export(const UnicodeString FileName)
  4032. {
  4033. THierarchicalStorage * Storage = TIniFileStorage::CreateFromPath(FileName);
  4034. try
  4035. {
  4036. Storage->AccessMode = smReadWrite;
  4037. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  4038. {
  4039. Save(Storage, true);
  4040. }
  4041. }
  4042. __finally
  4043. {
  4044. delete Storage;
  4045. }
  4046. }
  4047. //---------------------------------------------------------------------
  4048. void __fastcall TStoredSessionList::SelectAll(bool Select)
  4049. {
  4050. for (int Index = 0; Index < Count; Index++)
  4051. Sessions[Index]->Selected = Select;
  4052. }
  4053. //---------------------------------------------------------------------
  4054. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  4055. bool OnlySelected, TList * Imported)
  4056. {
  4057. for (int Index = 0; Index < From->Count; Index++)
  4058. {
  4059. if (!OnlySelected || From->Sessions[Index]->Selected)
  4060. {
  4061. TSessionData *Session = new TSessionData(L"");
  4062. Session->Assign(From->Sessions[Index]);
  4063. Session->Modified = true;
  4064. Session->MakeUniqueIn(this);
  4065. Add(Session);
  4066. if (Imported != NULL)
  4067. {
  4068. Imported->Add(Session);
  4069. }
  4070. }
  4071. }
  4072. // only modified, explicit
  4073. Save(false, true);
  4074. }
  4075. //---------------------------------------------------------------------
  4076. void __fastcall TStoredSessionList::SelectSessionsToImport
  4077. (TStoredSessionList * Dest, bool SSHOnly)
  4078. {
  4079. for (int Index = 0; Index < Count; Index++)
  4080. {
  4081. Sessions[Index]->Selected =
  4082. (!SSHOnly || (Sessions[Index]->GetNormalizedPuttyProtocol() == PuttySshProtocol)) &&
  4083. !Dest->FindByName(Sessions[Index]->Name);
  4084. }
  4085. }
  4086. //---------------------------------------------------------------------
  4087. void __fastcall TStoredSessionList::Cleanup()
  4088. {
  4089. try
  4090. {
  4091. if (Configuration->Storage == stRegistry) Clear();
  4092. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  4093. try
  4094. {
  4095. Storage->AccessMode = smReadWrite;
  4096. if (Storage->OpenRootKey(False))
  4097. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  4098. }
  4099. __finally
  4100. {
  4101. delete Storage;
  4102. }
  4103. }
  4104. catch (Exception &E)
  4105. {
  4106. throw ExtException(&E, LoadStr(CLEANUP_SESSIONS_ERROR));
  4107. }
  4108. }
  4109. //---------------------------------------------------------------------------
  4110. void __fastcall TStoredSessionList::UpdateStaticUsage()
  4111. {
  4112. int SCP = 0;
  4113. int SFTP = 0;
  4114. int FTP = 0;
  4115. int FTPS = 0;
  4116. int WebDAV = 0;
  4117. int WebDAVS = 0;
  4118. int S3 = 0;
  4119. int Password = 0;
  4120. int Advanced = 0;
  4121. int Color = 0;
  4122. int Note = 0;
  4123. int Tunnel = 0;
  4124. bool Folders = false;
  4125. bool Workspaces = false;
  4126. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  4127. std::unique_ptr<TStringList> DifferentAdvancedProperties(CreateSortedStringList());
  4128. for (int Index = 0; Index < Count; Index++)
  4129. {
  4130. TSessionData * Data = Sessions[Index];
  4131. if (Data->IsWorkspace)
  4132. {
  4133. Workspaces = true;
  4134. }
  4135. else
  4136. {
  4137. switch (Data->FSProtocol)
  4138. {
  4139. case fsSCPonly:
  4140. SCP++;
  4141. break;
  4142. case fsSFTP:
  4143. case fsSFTPonly:
  4144. SFTP++;
  4145. break;
  4146. case fsFTP:
  4147. if (Data->Ftps == ftpsNone)
  4148. {
  4149. FTP++;
  4150. }
  4151. else
  4152. {
  4153. FTPS++;
  4154. }
  4155. break;
  4156. case fsWebDAV:
  4157. if (Data->Ftps == ftpsNone)
  4158. {
  4159. WebDAV++;
  4160. }
  4161. else
  4162. {
  4163. WebDAVS++;
  4164. }
  4165. break;
  4166. case fsS3:
  4167. S3++;
  4168. break;
  4169. }
  4170. if (Data->HasAnySessionPassword())
  4171. {
  4172. Password++;
  4173. }
  4174. if (Data->Color != 0)
  4175. {
  4176. Color++;
  4177. }
  4178. if (!Data->Note.IsEmpty())
  4179. {
  4180. Note++;
  4181. }
  4182. // this effectively does not take passwords (proxy + tunnel) into account,
  4183. // when master password is set, as master password handler in not set up yet
  4184. if (!Data->IsSame(FactoryDefaults.get(), true, DifferentAdvancedProperties.get()))
  4185. {
  4186. Advanced++;
  4187. }
  4188. if (Data->Tunnel)
  4189. {
  4190. Tunnel++;
  4191. }
  4192. if (!Data->FolderName.IsEmpty())
  4193. {
  4194. Folders = true;
  4195. }
  4196. }
  4197. }
  4198. Configuration->Usage->Set(L"StoredSessionsCountSCP", SCP);
  4199. Configuration->Usage->Set(L"StoredSessionsCountSFTP", SFTP);
  4200. Configuration->Usage->Set(L"StoredSessionsCountFTP", FTP);
  4201. Configuration->Usage->Set(L"StoredSessionsCountFTPS", FTPS);
  4202. Configuration->Usage->Set(L"StoredSessionsCountWebDAV", WebDAV);
  4203. Configuration->Usage->Set(L"StoredSessionsCountWebDAVS", WebDAVS);
  4204. Configuration->Usage->Set(L"StoredSessionsCountS3", S3);
  4205. Configuration->Usage->Set(L"StoredSessionsCountPassword", Password);
  4206. Configuration->Usage->Set(L"StoredSessionsCountColor", Color);
  4207. Configuration->Usage->Set(L"StoredSessionsCountNote", Note);
  4208. Configuration->Usage->Set(L"StoredSessionsCountAdvanced", Advanced);
  4209. DifferentAdvancedProperties->Delimiter = L',';
  4210. Configuration->Usage->Set(L"StoredSessionsAdvancedSettings", DifferentAdvancedProperties->DelimitedText);
  4211. Configuration->Usage->Set(L"StoredSessionsCountTunnel", Tunnel);
  4212. // actually default might be true, see below for when the default is actually used
  4213. bool CustomDefaultStoredSession = false;
  4214. try
  4215. {
  4216. // this can throw, when the default session settings have password set
  4217. // (and no other basic property, like hostname/username),
  4218. // and master password is enabled as we are called before master password
  4219. // handler is set
  4220. CustomDefaultStoredSession = !FDefaultSettings->IsSame(FactoryDefaults.get(), false);
  4221. }
  4222. catch (...)
  4223. {
  4224. }
  4225. Configuration->Usage->Set(L"UsingDefaultStoredSession", CustomDefaultStoredSession);
  4226. Configuration->Usage->Set(L"UsingStoredSessionsFolders", Folders);
  4227. Configuration->Usage->Set(L"UsingWorkspaces", Workspaces);
  4228. }
  4229. //---------------------------------------------------------------------------
  4230. TSessionData * __fastcall TStoredSessionList::FindSame(TSessionData * Data)
  4231. {
  4232. TSessionData * Result;
  4233. if (Data->Hidden || Data->Name.IsEmpty() || Data->IsWorkspace)
  4234. {
  4235. Result = NULL;
  4236. }
  4237. else
  4238. {
  4239. Result = dynamic_cast<TSessionData *>(FindByName(Data->Name));
  4240. }
  4241. return Result;
  4242. }
  4243. //---------------------------------------------------------------------------
  4244. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  4245. {
  4246. for (int Index = 0; Index < Count; Index++)
  4247. if (Data == Sessions[Index]) return Index;
  4248. return -1;
  4249. }
  4250. //---------------------------------------------------------------------------
  4251. TSessionData * __fastcall TStoredSessionList::NewSession(
  4252. UnicodeString SessionName, TSessionData * Session)
  4253. {
  4254. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  4255. if (!DuplicateSession)
  4256. {
  4257. DuplicateSession = new TSessionData(L"");
  4258. DuplicateSession->Assign(Session);
  4259. DuplicateSession->Name = SessionName;
  4260. // make sure, that new stored session is saved to registry
  4261. DuplicateSession->Modified = true;
  4262. Add(DuplicateSession);
  4263. }
  4264. else
  4265. {
  4266. DuplicateSession->Assign(Session);
  4267. DuplicateSession->Name = SessionName;
  4268. DuplicateSession->Modified = true;
  4269. }
  4270. // list was saved here before to default storage, but it would not allow
  4271. // to work with special lists (export/import) not using default storage
  4272. return DuplicateSession;
  4273. }
  4274. //---------------------------------------------------------------------------
  4275. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  4276. {
  4277. DebugAssert(FDefaultSettings);
  4278. if (FDefaultSettings != value)
  4279. {
  4280. FDefaultSettings->Assign(value);
  4281. // make sure default settings are saved
  4282. FDefaultSettings->Modified = true;
  4283. FDefaultSettings->Name = DefaultName;
  4284. if (!FReadOnly)
  4285. {
  4286. // only modified, explicit
  4287. Save(false, true);
  4288. }
  4289. }
  4290. }
  4291. //---------------------------------------------------------------------------
  4292. bool __fastcall TStoredSessionList::OpenHostKeysSubKey(THierarchicalStorage * Storage, bool CanCreate)
  4293. {
  4294. return
  4295. Storage->OpenRootKey(CanCreate) &&
  4296. Storage->OpenSubKey(Configuration->SshHostKeysSubKey, CanCreate);
  4297. }
  4298. //---------------------------------------------------------------------------
  4299. THierarchicalStorage * __fastcall TStoredSessionList::CreateHostKeysStorageForWritting()
  4300. {
  4301. bool SessionList = false;
  4302. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateScpStorage(SessionList));
  4303. Storage->Explicit = true;
  4304. Storage->AccessMode = smReadWrite;
  4305. if (!OpenHostKeysSubKey(Storage.get(), true))
  4306. {
  4307. Storage.reset(NULL);
  4308. }
  4309. return Storage.release();
  4310. }
  4311. //---------------------------------------------------------------------------
  4312. void __fastcall TStoredSessionList::ImportHostKeys(
  4313. const UnicodeString SourceKey, TStoredSessionList * Sessions,
  4314. bool OnlySelected)
  4315. {
  4316. TRegistryStorage * SourceStorage = NULL;
  4317. THierarchicalStorage * TargetStorage = NULL;
  4318. TStringList * KeyList = NULL;
  4319. try
  4320. {
  4321. TargetStorage = CreateHostKeysStorageForWritting();
  4322. SourceStorage = new TRegistryStorage(SourceKey);
  4323. KeyList = new TStringList();
  4324. if ((TargetStorage != NULL) &&
  4325. SourceStorage->OpenRootKey(false))
  4326. {
  4327. SourceStorage->GetValueNames(KeyList);
  4328. TSessionData * Session;
  4329. UnicodeString HostKeyName;
  4330. DebugAssert(Sessions != NULL);
  4331. for (int Index = 0; Index < Sessions->Count; Index++)
  4332. {
  4333. Session = Sessions->Sessions[Index];
  4334. if (!OnlySelected || Session->Selected)
  4335. {
  4336. HostKeyName = PuttyMungeStr(FORMAT(L"@%d:%s", (Session->PortNumber, Session->HostNameExpanded)));
  4337. UnicodeString KeyName;
  4338. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  4339. {
  4340. KeyName = KeyList->Strings[KeyIndex];
  4341. int P = KeyName.Pos(HostKeyName);
  4342. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  4343. {
  4344. TargetStorage->WriteStringRaw(KeyName,
  4345. SourceStorage->ReadStringRaw(KeyName, L""));
  4346. }
  4347. }
  4348. }
  4349. }
  4350. }
  4351. }
  4352. __finally
  4353. {
  4354. delete SourceStorage;
  4355. delete TargetStorage;
  4356. delete KeyList;
  4357. }
  4358. }
  4359. //---------------------------------------------------------------------------
  4360. void __fastcall TStoredSessionList::ImportSelectedKnownHosts(TStoredSessionList * Sessions)
  4361. {
  4362. std::unique_ptr<THierarchicalStorage> Storage(CreateHostKeysStorageForWritting());
  4363. if (Storage.get() != NULL)
  4364. {
  4365. for (int Index = 0; Index < Sessions->Count; Index++)
  4366. {
  4367. TSessionData * Session = Sessions->Sessions[Index];
  4368. if (Session->Selected)
  4369. {
  4370. UnicodeString Algs;
  4371. UnicodeString HostKeys = Session->HostKey;
  4372. while (!HostKeys.IsEmpty())
  4373. {
  4374. UnicodeString HostKey = CutToChar(HostKeys, L';', true);
  4375. // skip alg
  4376. CutToChar(HostKey, L':', true);
  4377. UnicodeString Key = CutToChar(HostKey, L'=', true);
  4378. Storage->WriteStringRaw(Key, HostKey);
  4379. }
  4380. }
  4381. }
  4382. }
  4383. }
  4384. //---------------------------------------------------------------------------
  4385. bool __fastcall TStoredSessionList::IsFolderOrWorkspace(
  4386. const UnicodeString & Name, bool Workspace)
  4387. {
  4388. bool Result = false;
  4389. TSessionData * FirstData = NULL;
  4390. if (!Name.IsEmpty())
  4391. {
  4392. for (int Index = 0; !Result && (Index < Count); Index++)
  4393. {
  4394. Result = Sessions[Index]->IsInFolderOrWorkspace(Name);
  4395. if (Result)
  4396. {
  4397. FirstData = Sessions[Index];
  4398. }
  4399. }
  4400. }
  4401. return
  4402. Result &&
  4403. DebugAlwaysTrue(FirstData != NULL) &&
  4404. (FirstData->IsWorkspace == Workspace);
  4405. }
  4406. //---------------------------------------------------------------------------
  4407. bool __fastcall TStoredSessionList::IsFolder(const UnicodeString & Name)
  4408. {
  4409. return IsFolderOrWorkspace(Name, false);
  4410. }
  4411. //---------------------------------------------------------------------------
  4412. bool __fastcall TStoredSessionList::IsWorkspace(const UnicodeString & Name)
  4413. {
  4414. return IsFolderOrWorkspace(Name, true);
  4415. }
  4416. //---------------------------------------------------------------------------
  4417. TSessionData * __fastcall TStoredSessionList::CheckIsInFolderOrWorkspaceAndResolve(
  4418. TSessionData * Data, const UnicodeString & Name)
  4419. {
  4420. if (Data->IsInFolderOrWorkspace(Name))
  4421. {
  4422. Data = ResolveWorkspaceData(Data);
  4423. if ((Data != NULL) && Data->CanLogin &&
  4424. DebugAlwaysTrue(Data->Link.IsEmpty()))
  4425. {
  4426. return Data;
  4427. }
  4428. }
  4429. return NULL;
  4430. }
  4431. //---------------------------------------------------------------------------
  4432. void __fastcall TStoredSessionList::GetFolderOrWorkspace(const UnicodeString & Name, TList * List)
  4433. {
  4434. for (int Index = 0; (Index < Count); Index++)
  4435. {
  4436. TSessionData * RawData = Sessions[Index];
  4437. TSessionData * Data =
  4438. CheckIsInFolderOrWorkspaceAndResolve(RawData, Name);
  4439. if (Data != NULL)
  4440. {
  4441. TSessionData * Data2 = new TSessionData(L"");
  4442. Data2->Assign(Data);
  4443. if (!RawData->Link.IsEmpty() && (DebugAlwaysTrue(Data != RawData)) &&
  4444. // BACKWARD COMPATIBILITY
  4445. // When loading pre-5.6.4 workspace, that does not have state saved,
  4446. // do not overwrite the site "state" defaults
  4447. // with (empty) workspace state
  4448. RawData->HasStateData())
  4449. {
  4450. Data2->CopyStateData(RawData);
  4451. }
  4452. List->Add(Data2);
  4453. }
  4454. }
  4455. }
  4456. //---------------------------------------------------------------------------
  4457. TStrings * __fastcall TStoredSessionList::GetFolderOrWorkspaceList(
  4458. const UnicodeString & Name)
  4459. {
  4460. std::unique_ptr<TStringList> Result(new TStringList());
  4461. for (int Index = 0; (Index < Count); Index++)
  4462. {
  4463. TSessionData * Data =
  4464. CheckIsInFolderOrWorkspaceAndResolve(Sessions[Index], Name);
  4465. if (Data != NULL)
  4466. {
  4467. Result->Add(Data->SessionName);
  4468. }
  4469. }
  4470. return Result.release();
  4471. }
  4472. //---------------------------------------------------------------------------
  4473. TStrings * __fastcall TStoredSessionList::GetWorkspaces()
  4474. {
  4475. std::unique_ptr<TStringList> Result(CreateSortedStringList());
  4476. for (int Index = 0; (Index < Count); Index++)
  4477. {
  4478. TSessionData * Data = Sessions[Index];
  4479. if (Data->IsWorkspace)
  4480. {
  4481. Result->Add(Data->FolderName);
  4482. }
  4483. }
  4484. return Result.release();
  4485. }
  4486. //---------------------------------------------------------------------------
  4487. void __fastcall TStoredSessionList::NewWorkspace(
  4488. UnicodeString Name, TList * DataList)
  4489. {
  4490. for (int Index = 0; (Index < Count); Index++)
  4491. {
  4492. TSessionData * Data = Sessions[Index];
  4493. if (Data->IsInFolderOrWorkspace(Name))
  4494. {
  4495. Data->Remove();
  4496. Remove(Data);
  4497. Index--;
  4498. }
  4499. }
  4500. for (int Index = 0; (Index < DataList->Count); Index++)
  4501. {
  4502. TSessionData * Data = static_cast<TSessionData *>(DataList->Items[Index]);
  4503. TSessionData * Data2 = new TSessionData(L"");
  4504. Data2->Assign(Data);
  4505. Data2->Name = TSessionData::ComposePath(Name, Data->Name);
  4506. // make sure, that new stored session is saved to registry
  4507. Data2->Modified = true;
  4508. Add(Data2);
  4509. }
  4510. }
  4511. //---------------------------------------------------------------------------
  4512. bool __fastcall TStoredSessionList::HasAnyWorkspace()
  4513. {
  4514. bool Result = false;
  4515. for (int Index = 0; !Result && (Index < Count); Index++)
  4516. {
  4517. TSessionData * Data = Sessions[Index];
  4518. Result = Data->IsWorkspace;
  4519. }
  4520. return Result;
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. TSessionData * __fastcall TStoredSessionList::ParseUrl(UnicodeString Url,
  4524. TOptions * Options, bool & DefaultsOnly, UnicodeString * FileName,
  4525. bool * AProtocolDefined, UnicodeString * MaskedUrl)
  4526. {
  4527. TSessionData * Data = new TSessionData(L"");
  4528. try
  4529. {
  4530. Data->ParseUrl(Url, Options, this, DefaultsOnly, FileName, AProtocolDefined, MaskedUrl);
  4531. }
  4532. catch(...)
  4533. {
  4534. delete Data;
  4535. throw;
  4536. }
  4537. return Data;
  4538. }
  4539. //---------------------------------------------------------------------
  4540. bool __fastcall TStoredSessionList::IsUrl(UnicodeString Url)
  4541. {
  4542. bool DefaultsOnly;
  4543. bool ProtocolDefined = false;
  4544. std::unique_ptr<TSessionData> ParsedData(ParseUrl(Url, NULL, DefaultsOnly, NULL, &ProtocolDefined));
  4545. bool Result = ProtocolDefined;
  4546. return Result;
  4547. }
  4548. //---------------------------------------------------------------------
  4549. TSessionData * __fastcall TStoredSessionList::ResolveWorkspaceData(TSessionData * Data)
  4550. {
  4551. if (!Data->Link.IsEmpty())
  4552. {
  4553. Data = dynamic_cast<TSessionData *>(FindByName(Data->Link));
  4554. if (Data != NULL)
  4555. {
  4556. Data = ResolveWorkspaceData(Data);
  4557. }
  4558. }
  4559. return Data;
  4560. }
  4561. //---------------------------------------------------------------------
  4562. TSessionData * __fastcall TStoredSessionList::SaveWorkspaceData(TSessionData * Data)
  4563. {
  4564. std::unique_ptr<TSessionData> Result(new TSessionData(L""));
  4565. TSessionData * SameData = StoredSessions->FindSame(Data);
  4566. if (SameData != NULL)
  4567. {
  4568. Result->CopyStateData(Data);
  4569. Result->Link = Data->Name;
  4570. }
  4571. else
  4572. {
  4573. Result->Assign(Data);
  4574. }
  4575. Result->IsWorkspace = true;
  4576. return Result.release();
  4577. }
  4578. //---------------------------------------------------------------------
  4579. bool __fastcall TStoredSessionList::CanLogin(TSessionData * Data)
  4580. {
  4581. Data = ResolveWorkspaceData(Data);
  4582. return (Data != NULL) && Data->CanLogin;
  4583. }
  4584. //---------------------------------------------------------------------
  4585. UnicodeString GetExpandedLogFileName(UnicodeString LogFileName, TDateTime Started, TSessionData * SessionData)
  4586. {
  4587. // StripPathQuotes should not be needed as we do not feed quotes anymore
  4588. UnicodeString ANewFileName = StripPathQuotes(ExpandEnvironmentVariables(LogFileName));
  4589. for (int Index = 1; Index < ANewFileName.Length(); Index++)
  4590. {
  4591. if (ANewFileName[Index] == L'!')
  4592. {
  4593. UnicodeString Replacement;
  4594. // keep consistent with TFileCustomCommand::PatternReplacement
  4595. switch (tolower(ANewFileName[Index + 1]))
  4596. {
  4597. case L'y':
  4598. Replacement = FormatDateTime(L"yyyy", Started);
  4599. break;
  4600. case L'm':
  4601. Replacement = FormatDateTime(L"mm", Started);
  4602. break;
  4603. case L'd':
  4604. Replacement = FormatDateTime(L"dd", Started);
  4605. break;
  4606. case L't':
  4607. Replacement = FormatDateTime(L"hhnnss", Started);
  4608. break;
  4609. case 'p':
  4610. Replacement = IntToStr(static_cast<int>(GetCurrentProcessId()));
  4611. break;
  4612. case L'@':
  4613. if (SessionData != NULL)
  4614. {
  4615. Replacement = MakeValidFileName(SessionData->HostNameExpanded);
  4616. }
  4617. else
  4618. {
  4619. Replacement = L"nohost";
  4620. }
  4621. break;
  4622. case L's':
  4623. if (SessionData != NULL)
  4624. {
  4625. Replacement = MakeValidFileName(SessionData->SessionName);
  4626. }
  4627. else
  4628. {
  4629. Replacement = L"nosession";
  4630. }
  4631. break;
  4632. case L'!':
  4633. Replacement = L"!";
  4634. break;
  4635. default:
  4636. Replacement = UnicodeString(L"!") + ANewFileName[Index + 1];
  4637. break;
  4638. }
  4639. ANewFileName.Delete(Index, 2);
  4640. ANewFileName.Insert(Replacement, Index);
  4641. Index += Replacement.Length() - 1;
  4642. }
  4643. }
  4644. return ANewFileName;
  4645. }
  4646. //---------------------------------------------------------------------
  4647. bool __fastcall IsSshProtocol(TFSProtocol FSProtocol)
  4648. {
  4649. return
  4650. (FSProtocol == fsSFTPonly) || (FSProtocol == fsSFTP) ||
  4651. (FSProtocol == fsSCPonly);
  4652. }
  4653. //---------------------------------------------------------------------------
  4654. int __fastcall DefaultPort(TFSProtocol FSProtocol, TFtps Ftps)
  4655. {
  4656. int Result;
  4657. switch (FSProtocol)
  4658. {
  4659. case fsFTP:
  4660. if (Ftps == ftpsImplicit)
  4661. {
  4662. Result = FtpsImplicitPortNumber;
  4663. }
  4664. else
  4665. {
  4666. Result = FtpPortNumber;
  4667. }
  4668. break;
  4669. case fsWebDAV:
  4670. case fsS3:
  4671. if (Ftps == ftpsNone)
  4672. {
  4673. Result = HTTPPortNumber;
  4674. }
  4675. else
  4676. {
  4677. Result = HTTPSPortNumber;
  4678. }
  4679. break;
  4680. default:
  4681. if (IsSshProtocol(FSProtocol))
  4682. {
  4683. Result = SshPortNumber;
  4684. }
  4685. else
  4686. {
  4687. DebugFail();
  4688. Result = -1;
  4689. }
  4690. break;
  4691. }
  4692. return Result;
  4693. }
  4694. //---------------------------------------------------------------------