SessionData.cpp 139 KB

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