SessionData.cpp 170 KB

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