SessionData.cpp 166 KB

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