1
0

SessionData.cpp 172 KB

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