SessionData.cpp 139 KB

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