SessionData.cpp 175 KB

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