SessionData.cpp 139 KB

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