SessionData.cpp 177 KB

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