SessionData.cpp 162 KB

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