SessionData.cpp 176 KB

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