SessionData.cpp 156 KB

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