SessionData.cpp 142 KB

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