SessionData.cpp 155 KB

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