SessionData.cpp 159 KB

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