SessionData.cpp 175 KB

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