SessionData.cpp 176 KB

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