SessionData.cpp 175 KB

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