SessionData.cpp 169 KB

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