SessionData.cpp 192 KB

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