SessionData.cpp 139 KB

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