SessionData.cpp 158 KB

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