SessionData.cpp 173 KB

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