SessionData.cpp 157 KB

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