SessionData.cpp 149 KB

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