SessionData.cpp 162 KB

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