SessionData.cpp 156 KB

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