SessionData.cpp 166 KB

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