SessionData.cpp 192 KB

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