SessionData.cpp 174 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392
  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 = TAutoSwitch(Storage->ReadInteger(L"SCPLsFullTime", SCPLsFullTime));
  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 = TAutoSwitch(Storage->ReadInteger(L"LookupUserGroups2", LookupUserGroups));
  712. EOLType = (TEOLType)Storage->ReadInteger(L"EOLType", EOLType);
  713. TrimVMSVersions = Storage->ReadBool(L"TrimVMSVersions", TrimVMSVersions);
  714. NotUtf = TAutoSwitch(Storage->ReadInteger(L"Utf", Storage->ReadInteger(L"SFTPUtfBug", NotUtf)));
  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 = (TAutoSwitch)Storage->ReadInteger(L"S3MaxKeys", S3MaxKeys);
  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] = TAutoSwitch(Storage->ReadInteger(L"SFTP" #BUG "Bug", SFTPBug[sb##BUG]));
  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 = TAutoSwitch(Storage->ReadInteger(L"SFTPRealPath", SFTPRealPath));
  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 = TAutoSwitch(Storage->ReadInteger(L"FtpForcePasvIp2", FtpForcePasvIp));
  821. FtpUseMlsd = TAutoSwitch(Storage->ReadInteger(L"FtpUseMlsd", FtpUseMlsd));
  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 = static_cast<TAutoSwitch>(Storage->ReadInteger(L"FtpTransferActiveImmediately2", FtpTransferActiveImmediately));
  826. Ftps = static_cast<TFtps>(Storage->ReadInteger(L"Ftps", Ftps));
  827. FtpListAll = TAutoSwitch(Storage->ReadInteger(L"FtpListAll", FtpListAll));
  828. FtpHost = TAutoSwitch(Storage->ReadInteger(L"FtpHost", FtpHost));
  829. FtpWorkFromCwd = TAutoSwitch(Storage->ReadInteger(L"FtpWorkFromCwd", Storage->ReadInteger(L"FtpDeleteFromCwd", FtpWorkFromCwd)));
  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. bool __fastcall TSessionData::ParseUrl(UnicodeString Url, TOptions * Options,
  1776. TStoredSessionList * StoredSessions, bool & DefaultsOnly, UnicodeString * FileName,
  1777. bool * AProtocolDefined, UnicodeString * MaskedUrl, int Flags)
  1778. {
  1779. bool ProtocolDefined = false;
  1780. bool PortNumberDefined = false;
  1781. TFSProtocol AFSProtocol;
  1782. int APortNumber;
  1783. TFtps AFtps = ftpsNone;
  1784. int ProtocolLen = 0;
  1785. if (IsProtocolUrl(Url, ScpProtocol, ProtocolLen))
  1786. {
  1787. AFSProtocol = fsSCPonly;
  1788. APortNumber = SshPortNumber;
  1789. MoveStr(Url, MaskedUrl, ProtocolLen);
  1790. ProtocolDefined = true;
  1791. }
  1792. else if (IsProtocolUrl(Url, SftpProtocol, ProtocolLen))
  1793. {
  1794. AFSProtocol = fsSFTPonly;
  1795. APortNumber = SshPortNumber;
  1796. MoveStr(Url, MaskedUrl, ProtocolLen);
  1797. ProtocolDefined = true;
  1798. }
  1799. else if (IsProtocolUrl(Url, FtpProtocol, ProtocolLen))
  1800. {
  1801. AFSProtocol = fsFTP;
  1802. Ftps = ftpsNone;
  1803. APortNumber = FtpPortNumber;
  1804. MoveStr(Url, MaskedUrl, ProtocolLen);
  1805. ProtocolDefined = true;
  1806. }
  1807. else if (IsProtocolUrl(Url, FtpsProtocol, ProtocolLen))
  1808. {
  1809. AFSProtocol = fsFTP;
  1810. AFtps = ftpsImplicit;
  1811. APortNumber = FtpsImplicitPortNumber;
  1812. MoveStr(Url, MaskedUrl, ProtocolLen);
  1813. ProtocolDefined = true;
  1814. }
  1815. else if (IsProtocolUrl(Url, FtpesProtocol, ProtocolLen))
  1816. {
  1817. AFSProtocol = fsFTP;
  1818. AFtps = ftpsExplicitTls;
  1819. APortNumber = FtpPortNumber;
  1820. MoveStr(Url, MaskedUrl, ProtocolLen);
  1821. ProtocolDefined = true;
  1822. }
  1823. else if (IsProtocolUrl(Url, WebDAVProtocol, ProtocolLen) ||
  1824. IsProtocolUrl(Url, HttpProtocol, ProtocolLen))
  1825. {
  1826. AFSProtocol = fsWebDAV;
  1827. AFtps = ftpsNone;
  1828. APortNumber = HTTPPortNumber;
  1829. MoveStr(Url, MaskedUrl, ProtocolLen);
  1830. ProtocolDefined = true;
  1831. }
  1832. else if (IsProtocolUrl(Url, WebDAVSProtocol, ProtocolLen) ||
  1833. IsProtocolUrl(Url, HttpsProtocol, ProtocolLen))
  1834. {
  1835. AFSProtocol = fsWebDAV;
  1836. AFtps = ftpsImplicit;
  1837. APortNumber = HTTPSPortNumber;
  1838. MoveStr(Url, MaskedUrl, ProtocolLen);
  1839. ProtocolDefined = true;
  1840. }
  1841. else if (IsProtocolUrl(Url, S3Protocol, ProtocolLen))
  1842. {
  1843. AFSProtocol = fsS3;
  1844. AFtps = ftpsImplicit;
  1845. APortNumber = HTTPSPortNumber;
  1846. MoveStr(Url, MaskedUrl, ProtocolLen);
  1847. ProtocolDefined = true;
  1848. }
  1849. else if (IsProtocolUrl(Url, SshProtocol, ProtocolLen))
  1850. {
  1851. // For most uses, handling ssh:// the same way as sftp://
  1852. // The only place where a difference is made is GetLoginData() in WinMain.cpp
  1853. AFSProtocol = fsSFTPonly;
  1854. PuttyProtocol = PuttySshProtocol;
  1855. APortNumber = SshPortNumber;
  1856. MoveStr(Url, MaskedUrl, ProtocolLen);
  1857. ProtocolDefined = true;
  1858. }
  1859. if (ProtocolDefined && (Url.SubString(1, 2) == L"//"))
  1860. {
  1861. MoveStr(Url, MaskedUrl, 2);
  1862. }
  1863. if (AProtocolDefined != NULL)
  1864. {
  1865. *AProtocolDefined = ProtocolDefined;
  1866. }
  1867. bool Unsafe = FLAGSET(Flags, pufUnsafe);
  1868. if (!Url.IsEmpty())
  1869. {
  1870. UnicodeString DecodedUrl = DecodeUrlChars(Url);
  1871. // lookup stored session even if protocol was defined
  1872. // (this allows setting for example default username for host
  1873. // by creating stored session named by host)
  1874. TSessionData * Data = NULL;
  1875. // When using to paste URL on Login dialog, we do not want to lookup the stored sites
  1876. if ((StoredSessions != NULL) &&
  1877. (!ProtocolDefined || FLAGSET(Flags, pufAllowStoredSiteWithProtocol)))
  1878. {
  1879. // this can be optimized as the list is sorted
  1880. for (Integer Index = 0; Index < StoredSessions->CountIncludingHidden; Index++)
  1881. {
  1882. TSessionData * AData = (TSessionData *)StoredSessions->Items[Index];
  1883. if (!AData->IsWorkspace)
  1884. {
  1885. bool Match = false;
  1886. // Comparison optimizations as this is called many times
  1887. // e.g. when updating jumplist
  1888. if ((AData->Name.Length() == DecodedUrl.Length()) &&
  1889. SameText(AData->Name, DecodedUrl))
  1890. {
  1891. Match = true;
  1892. }
  1893. else if ((AData->Name.Length() < DecodedUrl.Length()) &&
  1894. (DecodedUrl[AData->Name.Length() + 1] == L'/') &&
  1895. // StrLIComp is an equivalent of SameText
  1896. (StrLIComp(AData->Name.c_str(), DecodedUrl.c_str(), AData->Name.Length()) == 0))
  1897. {
  1898. Match = true;
  1899. }
  1900. if (Match)
  1901. {
  1902. Data = AData;
  1903. break;
  1904. }
  1905. }
  1906. }
  1907. }
  1908. UnicodeString ARemoteDirectory;
  1909. if (Data != NULL)
  1910. {
  1911. Assign(Data);
  1912. int P = 1;
  1913. while (!AnsiSameText(DecodeUrlChars(Url.SubString(1, P)), Data->Name))
  1914. {
  1915. P++;
  1916. DebugAssert(P <= Url.Length());
  1917. }
  1918. ARemoteDirectory = Url.SubString(P + 1, Url.Length() - P);
  1919. if (Data->Hidden)
  1920. {
  1921. Data->Remove();
  1922. StoredSessions->Remove(Data);
  1923. // only modified, implicit
  1924. StoredSessions->Save(false, false);
  1925. }
  1926. if (MaskedUrl != NULL)
  1927. {
  1928. (*MaskedUrl) += Url;
  1929. }
  1930. }
  1931. else
  1932. {
  1933. // This happens when pasting URL on Login dialog
  1934. if (StoredSessions != NULL)
  1935. {
  1936. CopyData(StoredSessions->DefaultSettings);
  1937. }
  1938. Name = L"";
  1939. int PSlash = Url.Pos(L"/");
  1940. if (PSlash == 0)
  1941. {
  1942. PSlash = Url.Length() + 1;
  1943. }
  1944. UnicodeString ConnectInfo = Url.SubString(1, PSlash - 1);
  1945. int P = ConnectInfo.LastDelimiter(L"@");
  1946. UnicodeString UserInfo;
  1947. UnicodeString HostInfo;
  1948. if (P > 0)
  1949. {
  1950. UserInfo = ConnectInfo.SubString(1, P - 1);
  1951. HostInfo = ConnectInfo.SubString(P + 1, ConnectInfo.Length() - P);
  1952. }
  1953. else
  1954. {
  1955. HostInfo = ConnectInfo;
  1956. }
  1957. UnicodeString OrigHostInfo = HostInfo;
  1958. if ((HostInfo.Length() >= 2) && (HostInfo[1] == L'[') && ((P = HostInfo.Pos(L"]")) > 0))
  1959. {
  1960. HostName = HostInfo.SubString(2, P - 2);
  1961. HostInfo.Delete(1, P);
  1962. if (!HostInfo.IsEmpty() && (HostInfo[1] == L':'))
  1963. {
  1964. HostInfo.Delete(1, 1);
  1965. }
  1966. }
  1967. else
  1968. {
  1969. HostName = DecodeUrlChars(CutToChar(HostInfo, L':', true));
  1970. }
  1971. // expanded from ?: operator, as it caused strange "access violation" errors
  1972. if (!HostInfo.IsEmpty())
  1973. {
  1974. PortNumber = StrToIntDef(DecodeUrlChars(HostInfo), -1);
  1975. PortNumberDefined = true;
  1976. }
  1977. else if (ProtocolDefined)
  1978. {
  1979. PortNumber = APortNumber;
  1980. }
  1981. if (ProtocolDefined)
  1982. {
  1983. Ftps = AFtps;
  1984. }
  1985. UnicodeString UserInfoWithoutConnectionParams = CutToChar(UserInfo, UrlParamSeparator, false);
  1986. UnicodeString ConnectionParams = UserInfo;
  1987. UserInfo = UserInfoWithoutConnectionParams;
  1988. std::unique_ptr<TStrings> RawSettings(new TStringList());
  1989. while (!ConnectionParams.IsEmpty())
  1990. {
  1991. UnicodeString ConnectionParam = CutToChar(ConnectionParams, UrlParamSeparator, false);
  1992. UnicodeString ConnectionParamName = CutToChar(ConnectionParam, UrlParamValueSeparator, false);
  1993. if (SameText(ConnectionParamName, UrlHostKeyParamName))
  1994. {
  1995. HostKey = DecodeUrlChars(ConnectionParam);
  1996. FOverrideCachedHostKey = false;
  1997. }
  1998. else if (StartsText(UrlRawSettingsParamNamePrefix, ConnectionParamName))
  1999. {
  2000. UnicodeString AName = RightStr(ConnectionParamName, ConnectionParamName.Length() - UrlRawSettingsParamNamePrefix.Length());
  2001. AName = DecodeUrlChars(AName);
  2002. UnicodeString Value = DecodeUrlChars(ConnectionParam);
  2003. if (SameText(AName, L"Name"))
  2004. {
  2005. Name = Value;
  2006. }
  2007. else
  2008. {
  2009. RawSettings->Values[AName] = Value;
  2010. }
  2011. }
  2012. }
  2013. if (RawSettings->Count > 0) // optimization
  2014. {
  2015. ApplyRawSettings(RawSettings.get(), FLAGSET(Flags, pufUnsafe));
  2016. }
  2017. bool HasPassword = (UserInfo.Pos(L':') > 0);
  2018. UnicodeString RawUserName = CutToChar(UserInfo, L':', false);
  2019. UserName = DecodeUrlChars(RawUserName);
  2020. Password = DecodeUrlChars(UserInfo);
  2021. if (HasPassword && Password.IsEmpty())
  2022. {
  2023. Password = EmptyString;
  2024. }
  2025. UnicodeString RemoteDirectoryWithSessionParams = Url.SubString(PSlash, Url.Length() - PSlash + 1);
  2026. ARemoteDirectory = CutToChar(RemoteDirectoryWithSessionParams, UrlParamSeparator, false);
  2027. UnicodeString SessionParams = RemoteDirectoryWithSessionParams;
  2028. // We should handle session params in "stored session" branch too.
  2029. // And particularly if there's a "save" param, we should actually not try to match the
  2030. // URL against site names
  2031. while (!SessionParams.IsEmpty())
  2032. {
  2033. UnicodeString SessionParam = CutToChar(SessionParams, UrlParamSeparator, false);
  2034. UnicodeString SessionParamName = CutToChar(SessionParam, UrlParamValueSeparator, false);
  2035. if (SameText(SessionParamName, UrlSaveParamName))
  2036. {
  2037. FSaveOnly = (StrToIntDef(SessionParam, 1) != 0);
  2038. }
  2039. }
  2040. if (MaskedUrl != NULL)
  2041. {
  2042. (*MaskedUrl) += RawUserName;
  2043. if (HasPassword)
  2044. {
  2045. (*MaskedUrl) += L":" + PasswordMask;
  2046. }
  2047. if (!RawUserName.IsEmpty() || HasPassword)
  2048. {
  2049. (*MaskedUrl) += L"@";
  2050. }
  2051. (*MaskedUrl) += OrigHostInfo + ARemoteDirectory;
  2052. }
  2053. }
  2054. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory != L"/"))
  2055. {
  2056. if ((ARemoteDirectory[ARemoteDirectory.Length()] != L'/') &&
  2057. (FileName != NULL))
  2058. {
  2059. *FileName = DecodeUrlChars(UnixExtractFileName(ARemoteDirectory));
  2060. ARemoteDirectory = UnixExtractFilePath(ARemoteDirectory);
  2061. }
  2062. RemoteDirectory = DecodeUrlChars(ARemoteDirectory);
  2063. }
  2064. DefaultsOnly = false;
  2065. }
  2066. else
  2067. {
  2068. // This happens when pasting URL on Login dialog
  2069. if (StoredSessions != NULL)
  2070. {
  2071. CopyData(StoredSessions->DefaultSettings);
  2072. }
  2073. DefaultsOnly = true;
  2074. }
  2075. if (ProtocolDefined)
  2076. {
  2077. FSProtocol = AFSProtocol;
  2078. }
  2079. if (Options != NULL)
  2080. {
  2081. // we deliberately do keep defaultonly to false, in presence of any option,
  2082. // as the option should not make session "connectable"
  2083. UnicodeString Value;
  2084. if (Options->FindSwitch(USERNAME_SWITCH, Value))
  2085. {
  2086. UserName = Value;
  2087. }
  2088. if (Options->FindSwitch(PASSWORD_SWITCH, Value))
  2089. {
  2090. Password = Value;
  2091. }
  2092. if (Options->FindSwitch(SESSIONNAME_SWICH, Value))
  2093. {
  2094. Name = Value;
  2095. }
  2096. if (Options->FindSwitch(NEWPASSWORD_SWITCH, Value))
  2097. {
  2098. ChangePassword = true;
  2099. NewPassword = Value;
  2100. }
  2101. if (Options->FindSwitch(L"privatekey", Value))
  2102. {
  2103. PublicKeyFile = Value;
  2104. }
  2105. if (Options->FindSwitch(L"clientcert", Value))
  2106. {
  2107. TlsCertificateFile = Value;
  2108. }
  2109. if (Options->FindSwitch(PassphraseOption, Value))
  2110. {
  2111. Passphrase = Value;
  2112. }
  2113. if (Options->FindSwitch(L"timeout", Value))
  2114. {
  2115. Timeout = StrToInt(Value);
  2116. }
  2117. if (Options->FindSwitch(L"hostkey", Value) ||
  2118. Options->FindSwitch(L"certificate", Value))
  2119. {
  2120. HostKey = Value;
  2121. FOverrideCachedHostKey = true;
  2122. }
  2123. FtpPasvMode = Options->SwitchValue(L"passive", FtpPasvMode);
  2124. if (Options->FindSwitch(L"implicit"))
  2125. {
  2126. bool Enabled = Options->SwitchValue(L"implicit", true);
  2127. Ftps = Enabled ? ftpsImplicit : ftpsNone;
  2128. if (!PortNumberDefined && Enabled)
  2129. {
  2130. PortNumber = FtpsImplicitPortNumber;
  2131. }
  2132. }
  2133. // BACKWARD COMPATIBILITY with 5.5.x
  2134. if (Options->FindSwitch(L"explicitssl"))
  2135. {
  2136. bool Enabled = Options->SwitchValue(L"explicitssl", true);
  2137. Ftps = Enabled ? ftpsExplicitSsl : ftpsNone;
  2138. if (!PortNumberDefined && Enabled)
  2139. {
  2140. PortNumber = FtpPortNumber;
  2141. }
  2142. }
  2143. if (Options->FindSwitch(L"explicit") ||
  2144. // BACKWARD COMPATIBILITY with 5.5.x
  2145. Options->FindSwitch(L"explicittls"))
  2146. {
  2147. UnicodeString SwitchName =
  2148. Options->FindSwitch(L"explicit") ? L"explicit" : L"explicittls";
  2149. bool Enabled = Options->SwitchValue(SwitchName, true);
  2150. Ftps = Enabled ? ftpsExplicitTls : ftpsNone;
  2151. if (!PortNumberDefined && Enabled)
  2152. {
  2153. PortNumber = FtpPortNumber;
  2154. }
  2155. }
  2156. if (Options->FindSwitch(RawSettingsOption))
  2157. {
  2158. std::unique_ptr<TStrings> RawSettings(new TStringList());
  2159. if (Options->FindSwitch(RawSettingsOption, RawSettings.get()))
  2160. {
  2161. ApplyRawSettings(RawSettings.get(), Unsafe);
  2162. }
  2163. }
  2164. }
  2165. return true;
  2166. }
  2167. //---------------------------------------------------------------------
  2168. void __fastcall TSessionData::ApplyRawSettings(TStrings * RawSettings, bool Unsafe)
  2169. {
  2170. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawSettings, false));
  2171. ApplyRawSettings(OptionsStorage.get(), Unsafe);
  2172. }
  2173. //---------------------------------------------------------------------
  2174. void __fastcall TSessionData::ApplyRawSettings(THierarchicalStorage * Storage, bool Unsafe)
  2175. {
  2176. bool Dummy;
  2177. DoLoad(Storage, false, Dummy, Unsafe);
  2178. }
  2179. //---------------------------------------------------------------------
  2180. void __fastcall TSessionData::ConfigureTunnel(int APortNumber)
  2181. {
  2182. FOrigHostName = HostName;
  2183. FOrigPortNumber = PortNumber;
  2184. FOrigProxyMethod = ProxyMethod;
  2185. HostName = L"127.0.0.1";
  2186. PortNumber = APortNumber;
  2187. // proxy settings is used for tunnel
  2188. ProxyMethod = ::pmNone;
  2189. FLogicalHostName = FOrigHostName;
  2190. }
  2191. //---------------------------------------------------------------------
  2192. void __fastcall TSessionData::RollbackTunnel()
  2193. {
  2194. HostName = FOrigHostName;
  2195. PortNumber = FOrigPortNumber;
  2196. ProxyMethod = FOrigProxyMethod;
  2197. FLogicalHostName = L"";
  2198. }
  2199. //---------------------------------------------------------------------
  2200. void __fastcall TSessionData::ExpandEnvironmentVariables()
  2201. {
  2202. HostName = HostNameExpanded;
  2203. UserName = UserNameExpanded;
  2204. PublicKeyFile = ::ExpandEnvironmentVariables(PublicKeyFile);
  2205. }
  2206. //---------------------------------------------------------------------
  2207. void __fastcall TSessionData::ValidatePath(const UnicodeString Path)
  2208. {
  2209. // noop
  2210. }
  2211. //---------------------------------------------------------------------
  2212. void __fastcall TSessionData::ValidateName(const UnicodeString Name)
  2213. {
  2214. // keep consistent with MakeValidName
  2215. if (Name.LastDelimiter(L"/") > 0)
  2216. {
  2217. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, L"/")));
  2218. }
  2219. }
  2220. //---------------------------------------------------------------------
  2221. UnicodeString __fastcall TSessionData::MakeValidName(const UnicodeString & Name)
  2222. {
  2223. // keep consistent with ValidateName
  2224. return ReplaceStr(Name, L"/", L"\\");
  2225. }
  2226. //---------------------------------------------------------------------
  2227. RawByteString __fastcall TSessionData::EncryptPassword(const UnicodeString & Password, UnicodeString Key)
  2228. {
  2229. return Configuration->EncryptPassword(Password, Key);
  2230. }
  2231. //---------------------------------------------------------------------
  2232. RawByteString __fastcall TSessionData::StronglyRecryptPassword(const RawByteString & Password, UnicodeString Key)
  2233. {
  2234. return Configuration->StronglyRecryptPassword(Password, Key);
  2235. }
  2236. //---------------------------------------------------------------------
  2237. UnicodeString __fastcall TSessionData::DecryptPassword(const RawByteString & Password, UnicodeString Key)
  2238. {
  2239. UnicodeString Result;
  2240. try
  2241. {
  2242. Result = Configuration->DecryptPassword(Password, Key);
  2243. }
  2244. catch(EAbort &)
  2245. {
  2246. // silently ignore aborted prompts for master password and return empty password
  2247. }
  2248. return Result;
  2249. }
  2250. //---------------------------------------------------------------------
  2251. bool __fastcall TSessionData::GetCanLogin()
  2252. {
  2253. return !FHostName.IsEmpty();
  2254. }
  2255. //---------------------------------------------------------------------------
  2256. UnicodeString __fastcall TSessionData::GetSessionKey()
  2257. {
  2258. UnicodeString Result = FORMAT(L"%s@%s", (UserName, HostName));
  2259. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  2260. {
  2261. Result += FORMAT(L":%d", (PortNumber));
  2262. }
  2263. return Result;
  2264. }
  2265. //---------------------------------------------------------------------
  2266. UnicodeString __fastcall TSessionData::GetInternalStorageKey()
  2267. {
  2268. // This is probably useless remnant of previous use of this method from OpenSessionInPutty
  2269. // that needs the method to return something even for ad-hoc sessions
  2270. if (Name.IsEmpty())
  2271. {
  2272. return SessionKey;
  2273. }
  2274. else
  2275. {
  2276. return Name;
  2277. }
  2278. }
  2279. //---------------------------------------------------------------------
  2280. UnicodeString __fastcall TSessionData::GetStorageKey()
  2281. {
  2282. return SessionName;
  2283. }
  2284. //---------------------------------------------------------------------
  2285. UnicodeString __fastcall TSessionData::FormatSiteKey(const UnicodeString & HostName, int PortNumber)
  2286. {
  2287. return FORMAT(L"%s:%d", (HostName, PortNumber));
  2288. }
  2289. //---------------------------------------------------------------------
  2290. UnicodeString __fastcall TSessionData::GetSiteKey()
  2291. {
  2292. return FormatSiteKey(HostNameExpanded, PortNumber);
  2293. }
  2294. //---------------------------------------------------------------------
  2295. void __fastcall TSessionData::SetHostName(UnicodeString value)
  2296. {
  2297. if (FHostName != value)
  2298. {
  2299. // HostName is key for password encryption
  2300. UnicodeString XPassword = Password;
  2301. UnicodeString XNewPassword = Password;
  2302. UnicodeString XEncryptKey = EncryptKey;
  2303. // This is now hardly used as hostname is parsed directly on login dialog.
  2304. // But can be used when importing sites from PuTTY, as it allows same format too.
  2305. int P = value.LastDelimiter(L"@");
  2306. if (P > 0)
  2307. {
  2308. UserName = value.SubString(1, P - 1);
  2309. value = value.SubString(P + 1, value.Length() - P);
  2310. }
  2311. FHostName = value;
  2312. Modify();
  2313. Password = XPassword;
  2314. NewPassword = XNewPassword;
  2315. EncryptKey = XEncryptKey;
  2316. Shred(XPassword);
  2317. Shred(XNewPassword);
  2318. Shred(XEncryptKey);
  2319. }
  2320. }
  2321. //---------------------------------------------------------------------
  2322. UnicodeString __fastcall TSessionData::GetHostNameExpanded()
  2323. {
  2324. return ::ExpandEnvironmentVariables(HostName);
  2325. }
  2326. //---------------------------------------------------------------------
  2327. UnicodeString TSessionData::GetHostNameSource()
  2328. {
  2329. UnicodeString Result;
  2330. if (HostName != HostNameExpanded)
  2331. {
  2332. Result = HostName;
  2333. }
  2334. return Result;
  2335. }
  2336. //---------------------------------------------------------------------
  2337. void __fastcall TSessionData::SetPortNumber(int value)
  2338. {
  2339. SET_SESSION_PROPERTY(PortNumber);
  2340. }
  2341. //---------------------------------------------------------------------------
  2342. void __fastcall TSessionData::SetShell(UnicodeString value)
  2343. {
  2344. SET_SESSION_PROPERTY(Shell);
  2345. }
  2346. //---------------------------------------------------------------------------
  2347. void __fastcall TSessionData::SetSftpServer(UnicodeString value)
  2348. {
  2349. SET_SESSION_PROPERTY(SftpServer);
  2350. }
  2351. //---------------------------------------------------------------------------
  2352. void __fastcall TSessionData::SetClearAliases(bool value)
  2353. {
  2354. SET_SESSION_PROPERTY(ClearAliases);
  2355. }
  2356. //---------------------------------------------------------------------------
  2357. void __fastcall TSessionData::SetListingCommand(UnicodeString value)
  2358. {
  2359. SET_SESSION_PROPERTY(ListingCommand);
  2360. }
  2361. //---------------------------------------------------------------------------
  2362. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  2363. {
  2364. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  2365. }
  2366. //---------------------------------------------------------------------------
  2367. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  2368. {
  2369. SET_SESSION_PROPERTY(UnsetNationalVars);
  2370. }
  2371. //---------------------------------------------------------------------
  2372. void __fastcall TSessionData::SetUserName(UnicodeString value)
  2373. {
  2374. // Avoid password recryption (what may popup master password prompt)
  2375. if (FUserName != value)
  2376. {
  2377. // UserName is key for password encryption
  2378. UnicodeString XPassword = Password;
  2379. UnicodeString XNewPassword = NewPassword;
  2380. UnicodeString XEncryptKey = EncryptKey;
  2381. SET_SESSION_PROPERTY(UserName);
  2382. Password = XPassword;
  2383. NewPassword = XNewPassword;
  2384. EncryptKey = XEncryptKey;
  2385. Shred(XPassword);
  2386. Shred(XNewPassword);
  2387. Shred(XEncryptKey);
  2388. }
  2389. }
  2390. //---------------------------------------------------------------------
  2391. UnicodeString __fastcall TSessionData::GetUserNameExpanded()
  2392. {
  2393. return ::ExpandEnvironmentVariables(UserName);
  2394. }
  2395. //---------------------------------------------------------------------
  2396. UnicodeString TSessionData::GetUserNameSource()
  2397. {
  2398. UnicodeString Result;
  2399. if (UserName != UserNameExpanded)
  2400. {
  2401. Result = UserName;
  2402. }
  2403. return Result;
  2404. }
  2405. //---------------------------------------------------------------------
  2406. void __fastcall TSessionData::SetPassword(UnicodeString avalue)
  2407. {
  2408. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  2409. SET_SESSION_PROPERTY(Password);
  2410. }
  2411. //---------------------------------------------------------------------
  2412. UnicodeString __fastcall TSessionData::GetPassword() const
  2413. {
  2414. return DecryptPassword(FPassword, UserName+HostName);
  2415. }
  2416. //---------------------------------------------------------------------
  2417. void __fastcall TSessionData::SetNewPassword(UnicodeString avalue)
  2418. {
  2419. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  2420. SET_SESSION_PROPERTY(NewPassword);
  2421. }
  2422. //---------------------------------------------------------------------
  2423. UnicodeString __fastcall TSessionData::GetNewPassword() const
  2424. {
  2425. return DecryptPassword(FNewPassword, UserName+HostName);
  2426. }
  2427. //---------------------------------------------------------------------
  2428. void __fastcall TSessionData::SetChangePassword(bool value)
  2429. {
  2430. SET_SESSION_PROPERTY(ChangePassword);
  2431. }
  2432. //---------------------------------------------------------------------
  2433. void __fastcall TSessionData::SetPingInterval(int value)
  2434. {
  2435. SET_SESSION_PROPERTY(PingInterval);
  2436. }
  2437. //---------------------------------------------------------------------
  2438. void __fastcall TSessionData::SetTryAgent(bool value)
  2439. {
  2440. SET_SESSION_PROPERTY(TryAgent);
  2441. }
  2442. //---------------------------------------------------------------------
  2443. void __fastcall TSessionData::SetAgentFwd(bool value)
  2444. {
  2445. SET_SESSION_PROPERTY(AgentFwd);
  2446. }
  2447. //---------------------------------------------------------------------
  2448. void __fastcall TSessionData::SetAuthTIS(bool value)
  2449. {
  2450. SET_SESSION_PROPERTY(AuthTIS);
  2451. }
  2452. //---------------------------------------------------------------------
  2453. void __fastcall TSessionData::SetAuthKI(bool value)
  2454. {
  2455. SET_SESSION_PROPERTY(AuthKI);
  2456. }
  2457. //---------------------------------------------------------------------
  2458. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  2459. {
  2460. SET_SESSION_PROPERTY(AuthKIPassword);
  2461. }
  2462. //---------------------------------------------------------------------
  2463. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  2464. {
  2465. SET_SESSION_PROPERTY(AuthGSSAPI);
  2466. }
  2467. //---------------------------------------------------------------------
  2468. void __fastcall TSessionData::SetAuthGSSAPIKEX(bool value)
  2469. {
  2470. SET_SESSION_PROPERTY(AuthGSSAPIKEX);
  2471. }
  2472. //---------------------------------------------------------------------
  2473. void __fastcall TSessionData::SetGSSAPIFwdTGT(bool value)
  2474. {
  2475. SET_SESSION_PROPERTY(GSSAPIFwdTGT);
  2476. }
  2477. //---------------------------------------------------------------------
  2478. void __fastcall TSessionData::SetChangeUsername(bool value)
  2479. {
  2480. SET_SESSION_PROPERTY(ChangeUsername);
  2481. }
  2482. //---------------------------------------------------------------------
  2483. void __fastcall TSessionData::SetCompression(bool value)
  2484. {
  2485. SET_SESSION_PROPERTY(Compression);
  2486. }
  2487. //---------------------------------------------------------------------
  2488. void __fastcall TSessionData::SetSshProt(TSshProt value)
  2489. {
  2490. SET_SESSION_PROPERTY(SshProt);
  2491. }
  2492. //---------------------------------------------------------------------
  2493. void __fastcall TSessionData::SetSsh2DES(bool value)
  2494. {
  2495. SET_SESSION_PROPERTY(Ssh2DES);
  2496. }
  2497. //---------------------------------------------------------------------
  2498. void __fastcall TSessionData::SetSshNoUserAuth(bool value)
  2499. {
  2500. SET_SESSION_PROPERTY(SshNoUserAuth);
  2501. }
  2502. //---------------------------------------------------------------------
  2503. UnicodeString __fastcall TSessionData::GetSshProtStr()
  2504. {
  2505. return SshProtList[FSshProt];
  2506. }
  2507. //---------------------------------------------------------------------
  2508. bool __fastcall TSessionData::GetUsesSsh()
  2509. {
  2510. return IsSshProtocol(FSProtocol);
  2511. }
  2512. //---------------------------------------------------------------------
  2513. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  2514. {
  2515. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  2516. SET_SESSION_PROPERTY(Ciphers[Index]);
  2517. }
  2518. //---------------------------------------------------------------------
  2519. TCipher __fastcall TSessionData::GetCipher(int Index) const
  2520. {
  2521. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  2522. return FCiphers[Index];
  2523. }
  2524. //---------------------------------------------------------------------
  2525. template<class AlgoT>
  2526. void __fastcall TSessionData::SetAlgoList(AlgoT * List, const AlgoT * DefaultList, const UnicodeString * Names,
  2527. int Count, AlgoT WarnAlgo, UnicodeString value)
  2528. {
  2529. std::vector<bool> Used(Count); // initialized to false
  2530. std::vector<AlgoT> NewList(Count);
  2531. bool HasWarnAlgo = (WarnAlgo >= AlgoT());
  2532. const AlgoT * WarnPtr;
  2533. int WarnDefaultIndex;
  2534. if (!HasWarnAlgo)
  2535. {
  2536. WarnPtr = NULL;
  2537. WarnDefaultIndex = -1;
  2538. }
  2539. else
  2540. {
  2541. WarnPtr = std::find(DefaultList, DefaultList + Count, WarnAlgo);
  2542. DebugAssert(WarnPtr != NULL);
  2543. WarnDefaultIndex = (WarnPtr - DefaultList);
  2544. }
  2545. int Index = 0;
  2546. while (!value.IsEmpty())
  2547. {
  2548. UnicodeString AlgoStr = CutToChar(value, L',', true);
  2549. for (int Algo = 0; Algo < Count; Algo++)
  2550. {
  2551. if (!AlgoStr.CompareIC(Names[Algo]) &&
  2552. !Used[Algo] && DebugAlwaysTrue(Index < Count))
  2553. {
  2554. NewList[Index] = (AlgoT)Algo;
  2555. Used[Algo] = true;
  2556. Index++;
  2557. break;
  2558. }
  2559. }
  2560. }
  2561. if (HasWarnAlgo && !Used[WarnAlgo] && DebugAlwaysTrue(Index < Count))
  2562. {
  2563. NewList[Index] = WarnAlgo;
  2564. Used[WarnAlgo] = true;
  2565. Index++;
  2566. }
  2567. int WarnIndex = -1;
  2568. if (HasWarnAlgo)
  2569. {
  2570. WarnIndex = std::find(NewList.begin(), NewList.end(), WarnAlgo) - NewList.begin();
  2571. }
  2572. bool Priority = true;
  2573. for (int DefaultIndex = 0; (DefaultIndex < Count); DefaultIndex++)
  2574. {
  2575. AlgoT DefaultAlgo = DefaultList[DefaultIndex];
  2576. if (!Used[DefaultAlgo] && DebugAlwaysTrue(Index < Count))
  2577. {
  2578. int TargetIndex;
  2579. // Unused algs that are prioritized in the default list,
  2580. // should be merged before the existing custom list
  2581. if (Priority)
  2582. {
  2583. TargetIndex = DefaultIndex;
  2584. }
  2585. else
  2586. {
  2587. if (HasWarnAlgo && (DefaultIndex < WarnDefaultIndex))
  2588. {
  2589. TargetIndex = WarnIndex;
  2590. }
  2591. else
  2592. {
  2593. TargetIndex = Index;
  2594. }
  2595. }
  2596. NewList.insert(NewList.begin() + TargetIndex, DefaultAlgo);
  2597. DebugAssert(NewList.back() == AlgoT());
  2598. NewList.pop_back();
  2599. if (HasWarnAlgo && (TargetIndex <= WarnIndex))
  2600. {
  2601. WarnIndex++;
  2602. }
  2603. Index++;
  2604. }
  2605. else
  2606. {
  2607. Priority = false;
  2608. }
  2609. }
  2610. if (!std::equal(NewList.begin(), NewList.end(), List))
  2611. {
  2612. std::copy(NewList.begin(), NewList.end(), List);
  2613. Modify();
  2614. }
  2615. }
  2616. //---------------------------------------------------------------------
  2617. void __fastcall TSessionData::SetCipherList(UnicodeString value)
  2618. {
  2619. SetAlgoList(FCiphers, DefaultCipherList, CipherNames, CIPHER_COUNT, cipWarn, value);
  2620. }
  2621. //---------------------------------------------------------------------
  2622. UnicodeString __fastcall TSessionData::GetCipherList() const
  2623. {
  2624. UnicodeString Result;
  2625. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  2626. {
  2627. Result += UnicodeString(Index ? L"," : L"") + CipherNames[Cipher[Index]];
  2628. }
  2629. return Result;
  2630. }
  2631. //---------------------------------------------------------------------
  2632. void __fastcall TSessionData::SetKex(int Index, TKex value)
  2633. {
  2634. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  2635. SET_SESSION_PROPERTY(Kex[Index]);
  2636. }
  2637. //---------------------------------------------------------------------
  2638. TKex __fastcall TSessionData::GetKex(int Index) const
  2639. {
  2640. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  2641. return FKex[Index];
  2642. }
  2643. //---------------------------------------------------------------------
  2644. void __fastcall TSessionData::SetKexList(UnicodeString value)
  2645. {
  2646. SetAlgoList(FKex, DefaultKexList, KexNames, KEX_COUNT, kexWarn, value);
  2647. }
  2648. //---------------------------------------------------------------------
  2649. UnicodeString __fastcall TSessionData::GetKexList() const
  2650. {
  2651. UnicodeString Result;
  2652. for (int Index = 0; Index < KEX_COUNT; Index++)
  2653. {
  2654. Result += UnicodeString(Index ? L"," : L"") + KexNames[Kex[Index]];
  2655. }
  2656. return Result;
  2657. }
  2658. //---------------------------------------------------------------------
  2659. void __fastcall TSessionData::SetHostKeys(int Index, THostKey value)
  2660. {
  2661. DebugAssert(Index >= 0 && Index < HOSTKEY_COUNT);
  2662. SET_SESSION_PROPERTY(HostKeys[Index]);
  2663. }
  2664. //---------------------------------------------------------------------
  2665. THostKey __fastcall TSessionData::GetHostKeys(int Index) const
  2666. {
  2667. DebugAssert(Index >= 0 && Index < HOSTKEY_COUNT);
  2668. return FHostKeys[Index];
  2669. }
  2670. //---------------------------------------------------------------------
  2671. void __fastcall TSessionData::SetHostKeyList(UnicodeString value)
  2672. {
  2673. SetAlgoList(FHostKeys, DefaultHostKeyList, HostKeyNames, HOSTKEY_COUNT, hkWarn, value);
  2674. }
  2675. //---------------------------------------------------------------------
  2676. UnicodeString __fastcall TSessionData::GetHostKeyList() const
  2677. {
  2678. UnicodeString Result;
  2679. for (int Index = 0; Index < HOSTKEY_COUNT; Index++)
  2680. {
  2681. Result += UnicodeString(Index ? L"," : L"") + HostKeyNames[HostKeys[Index]];
  2682. }
  2683. return Result;
  2684. }
  2685. //---------------------------------------------------------------------
  2686. void __fastcall TSessionData::SetGssLib(int Index, TGssLib value)
  2687. {
  2688. DebugAssert(Index >= 0 && Index < GSSLIB_COUNT);
  2689. SET_SESSION_PROPERTY(GssLib[Index]);
  2690. }
  2691. //---------------------------------------------------------------------
  2692. TGssLib __fastcall TSessionData::GetGssLib(int Index) const
  2693. {
  2694. DebugAssert(Index >= 0 && Index < GSSLIB_COUNT);
  2695. return FGssLib[Index];
  2696. }
  2697. //---------------------------------------------------------------------
  2698. void __fastcall TSessionData::SetGssLibList(UnicodeString value)
  2699. {
  2700. SetAlgoList(FGssLib, DefaultGssLibList, GssLibNames, GSSLIB_COUNT, TGssLib(-1), value);
  2701. }
  2702. //---------------------------------------------------------------------
  2703. UnicodeString __fastcall TSessionData::GetGssLibList() const
  2704. {
  2705. UnicodeString Result;
  2706. for (int Index = 0; Index < GSSLIB_COUNT; Index++)
  2707. {
  2708. Result += UnicodeString(Index ? L"," : L"") + GssLibNames[GssLib[Index]];
  2709. }
  2710. return Result;
  2711. }
  2712. //---------------------------------------------------------------------
  2713. void __fastcall TSessionData::SetGssLibCustom(UnicodeString value)
  2714. {
  2715. SET_SESSION_PROPERTY(GssLibCustom);
  2716. }
  2717. //---------------------------------------------------------------------
  2718. void __fastcall TSessionData::SetPublicKeyFile(UnicodeString value)
  2719. {
  2720. if (FPublicKeyFile != value)
  2721. {
  2722. // PublicKeyFile is key for Passphrase encryption
  2723. UnicodeString XPassphrase = Passphrase;
  2724. // StripPathQuotes should not be needed as we do not feed quotes anymore
  2725. FPublicKeyFile = StripPathQuotes(value);
  2726. Modify();
  2727. Passphrase = XPassphrase;
  2728. Shred(XPassphrase);
  2729. }
  2730. }
  2731. //---------------------------------------------------------------------
  2732. void __fastcall TSessionData::SetPassphrase(UnicodeString avalue)
  2733. {
  2734. RawByteString value = EncryptPassword(avalue, PublicKeyFile);
  2735. SET_SESSION_PROPERTY(Passphrase);
  2736. }
  2737. //---------------------------------------------------------------------
  2738. UnicodeString __fastcall TSessionData::GetPassphrase() const
  2739. {
  2740. return DecryptPassword(FPassphrase, PublicKeyFile);
  2741. }
  2742. //---------------------------------------------------------------------
  2743. void __fastcall TSessionData::SetReturnVar(UnicodeString value)
  2744. {
  2745. SET_SESSION_PROPERTY(ReturnVar);
  2746. }
  2747. //---------------------------------------------------------------------
  2748. void __fastcall TSessionData::SetExitCode1IsError(bool value)
  2749. {
  2750. SET_SESSION_PROPERTY(ExitCode1IsError);
  2751. }
  2752. //---------------------------------------------------------------------------
  2753. void __fastcall TSessionData::SetLookupUserGroups(TAutoSwitch value)
  2754. {
  2755. SET_SESSION_PROPERTY(LookupUserGroups);
  2756. }
  2757. //---------------------------------------------------------------------------
  2758. void __fastcall TSessionData::SetEOLType(TEOLType value)
  2759. {
  2760. SET_SESSION_PROPERTY(EOLType);
  2761. }
  2762. //---------------------------------------------------------------------------
  2763. void __fastcall TSessionData::SetTrimVMSVersions(bool value)
  2764. {
  2765. SET_SESSION_PROPERTY(TrimVMSVersions);
  2766. }
  2767. //---------------------------------------------------------------------------
  2768. TDateTime __fastcall TSessionData::GetTimeoutDT()
  2769. {
  2770. return SecToDateTime(Timeout);
  2771. }
  2772. //---------------------------------------------------------------------------
  2773. void __fastcall TSessionData::SetTimeout(int value)
  2774. {
  2775. SET_SESSION_PROPERTY(Timeout);
  2776. }
  2777. //---------------------------------------------------------------------------
  2778. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  2779. {
  2780. SET_SESSION_PROPERTY(FSProtocol);
  2781. }
  2782. //---------------------------------------------------------------------
  2783. UnicodeString __fastcall TSessionData::GetFSProtocolStr()
  2784. {
  2785. DebugAssert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  2786. return FSProtocolNames[FSProtocol];
  2787. }
  2788. //---------------------------------------------------------------------------
  2789. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  2790. {
  2791. if (value != DetectReturnVar)
  2792. {
  2793. ReturnVar = value ? L"" : L"$?";
  2794. }
  2795. }
  2796. //---------------------------------------------------------------------------
  2797. bool __fastcall TSessionData::GetDetectReturnVar()
  2798. {
  2799. return ReturnVar.IsEmpty();
  2800. }
  2801. //---------------------------------------------------------------------------
  2802. void __fastcall TSessionData::SetDefaultShell(bool value)
  2803. {
  2804. if (value != DefaultShell)
  2805. {
  2806. Shell = value ? L"" : L"/bin/bash";
  2807. }
  2808. }
  2809. //---------------------------------------------------------------------------
  2810. bool __fastcall TSessionData::GetDefaultShell()
  2811. {
  2812. return Shell.IsEmpty();
  2813. }
  2814. //---------------------------------------------------------------------------
  2815. void __fastcall TSessionData::SetPuttyProtocol(UnicodeString value)
  2816. {
  2817. SET_SESSION_PROPERTY(PuttyProtocol);
  2818. }
  2819. //---------------------------------------------------------------------
  2820. UnicodeString __fastcall TSessionData::GetNormalizedPuttyProtocol() const
  2821. {
  2822. return DefaultStr(PuttyProtocol, PuttySshProtocol);
  2823. }
  2824. //---------------------------------------------------------------------
  2825. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  2826. {
  2827. unsigned short hour, min, sec, msec;
  2828. value.DecodeTime(&hour, &min, &sec, &msec);
  2829. PingInterval = ((int)hour)*SecsPerHour + ((int)min)*SecsPerMin + sec;
  2830. }
  2831. //---------------------------------------------------------------------------
  2832. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  2833. {
  2834. return SecToDateTime(PingInterval);
  2835. }
  2836. //---------------------------------------------------------------------------
  2837. void __fastcall TSessionData::SetPingType(TPingType value)
  2838. {
  2839. SET_SESSION_PROPERTY(PingType);
  2840. }
  2841. //---------------------------------------------------------------------------
  2842. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  2843. {
  2844. SET_SESSION_PROPERTY(AddressFamily);
  2845. }
  2846. //---------------------------------------------------------------------------
  2847. void __fastcall TSessionData::SetRekeyData(UnicodeString value)
  2848. {
  2849. SET_SESSION_PROPERTY(RekeyData);
  2850. }
  2851. //---------------------------------------------------------------------------
  2852. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  2853. {
  2854. SET_SESSION_PROPERTY(RekeyTime);
  2855. }
  2856. //---------------------------------------------------------------------
  2857. UnicodeString __fastcall TSessionData::GetDefaultSessionName()
  2858. {
  2859. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  2860. {
  2861. // If we ever choose to include port number,
  2862. // we have to escape IPv6 literals in HostName
  2863. return FORMAT(L"%s@%s", (UserName, HostName));
  2864. }
  2865. else if (!HostName.IsEmpty())
  2866. {
  2867. return HostName;
  2868. }
  2869. else
  2870. {
  2871. return L"session";
  2872. }
  2873. }
  2874. //---------------------------------------------------------------------
  2875. UnicodeString __fastcall TSessionData::GetNameWithoutHiddenPrefix()
  2876. {
  2877. UnicodeString Result = Name;
  2878. if (Hidden)
  2879. {
  2880. Result = Result.SubString(TNamedObjectList::HiddenPrefix.Length() + 1, Result.Length() - TNamedObjectList::HiddenPrefix.Length());
  2881. }
  2882. return Result;
  2883. }
  2884. //---------------------------------------------------------------------
  2885. bool __fastcall TSessionData::HasSessionName()
  2886. {
  2887. return (!GetNameWithoutHiddenPrefix().IsEmpty() && (Name != DefaultName));
  2888. }
  2889. //---------------------------------------------------------------------
  2890. UnicodeString __fastcall TSessionData::GetSessionName()
  2891. {
  2892. UnicodeString Result;
  2893. if (HasSessionName())
  2894. {
  2895. Result = GetNameWithoutHiddenPrefix();
  2896. }
  2897. else
  2898. {
  2899. Result = DefaultSessionName;
  2900. }
  2901. return Result;
  2902. }
  2903. //---------------------------------------------------------------------
  2904. bool __fastcall TSessionData::IsSecure()
  2905. {
  2906. bool Result;
  2907. switch (FSProtocol)
  2908. {
  2909. case fsSCPonly:
  2910. case fsSFTP:
  2911. case fsSFTPonly:
  2912. Result = true;
  2913. break;
  2914. case fsFTP:
  2915. case fsWebDAV:
  2916. case fsS3:
  2917. Result = (Ftps != ftpsNone);
  2918. break;
  2919. default:
  2920. DebugFail();
  2921. break;
  2922. }
  2923. return Result;
  2924. }
  2925. //---------------------------------------------------------------------
  2926. UnicodeString __fastcall TSessionData::GetProtocolUrl(bool HttpForWebDAV)
  2927. {
  2928. UnicodeString Url;
  2929. switch (FSProtocol)
  2930. {
  2931. case fsSCPonly:
  2932. Url = ScpProtocol;
  2933. break;
  2934. default:
  2935. DebugFail();
  2936. // fallback
  2937. case fsSFTP:
  2938. case fsSFTPonly:
  2939. Url = SftpProtocol;
  2940. break;
  2941. case fsFTP:
  2942. if (Ftps == ftpsImplicit)
  2943. {
  2944. Url = FtpsProtocol;
  2945. }
  2946. else if ((Ftps == ftpsExplicitTls) || (Ftps == ftpsExplicitSsl))
  2947. {
  2948. Url = FtpesProtocol;
  2949. }
  2950. else
  2951. {
  2952. Url = FtpProtocol;
  2953. }
  2954. break;
  2955. case fsWebDAV:
  2956. if (HttpForWebDAV)
  2957. {
  2958. if (Ftps == ftpsImplicit)
  2959. {
  2960. Url = HttpsProtocol;
  2961. }
  2962. else
  2963. {
  2964. Url = HttpProtocol;
  2965. }
  2966. }
  2967. else
  2968. {
  2969. if (Ftps == ftpsImplicit)
  2970. {
  2971. Url = WebDAVSProtocol;
  2972. }
  2973. else
  2974. {
  2975. Url = WebDAVProtocol;
  2976. }
  2977. }
  2978. break;
  2979. case fsS3:
  2980. Url = S3Protocol;
  2981. break;
  2982. }
  2983. Url += ProtocolSeparator;
  2984. return Url;
  2985. }
  2986. //---------------------------------------------------------------------
  2987. static bool HasIP6LiteralBrackets(const UnicodeString & HostName)
  2988. {
  2989. return
  2990. (HostName.Length() >= 2) &&
  2991. (HostName[1] == L'[') &&
  2992. (HostName[HostName.Length()] == L']');
  2993. }
  2994. //---------------------------------------------------------------------
  2995. static UnicodeString StripIP6LiteralBrackets(const UnicodeString & HostName)
  2996. {
  2997. UnicodeString Result = HostName;
  2998. if (DebugAlwaysTrue(HasIP6LiteralBrackets(Result)))
  2999. {
  3000. Result = Result.SubString(2, Result.Length() - 2);
  3001. }
  3002. return Result;
  3003. }
  3004. //---------------------------------------------------------------------
  3005. bool __fastcall IsIPv6Literal(const UnicodeString & HostName)
  3006. {
  3007. UnicodeString Buf = HostName;
  3008. if (HasIP6LiteralBrackets(Buf))
  3009. {
  3010. Buf = StripIP6LiteralBrackets(Buf);
  3011. }
  3012. int Colons = 0;
  3013. bool Result = true;
  3014. for (int Index = 1; Result && (Index <= Buf.Length()); Index++)
  3015. {
  3016. wchar_t C = Buf[Index];
  3017. if (C == L'%')
  3018. {
  3019. break;
  3020. }
  3021. else if (C == L':')
  3022. {
  3023. Colons++;
  3024. }
  3025. else
  3026. {
  3027. Result = IsHex(C);
  3028. }
  3029. }
  3030. Result = Result && (Colons >= 2);
  3031. return Result;
  3032. }
  3033. //---------------------------------------------------------------------
  3034. UnicodeString __fastcall EscapeIPv6Literal(const UnicodeString & IP)
  3035. {
  3036. UnicodeString Result = IP;
  3037. if (!HasIP6LiteralBrackets(Result))
  3038. {
  3039. Result = L"[" + IP + L"]";
  3040. }
  3041. return Result;
  3042. }
  3043. //---------------------------------------------------------------------
  3044. TStrings * __fastcall TSessionData::GetRawSettingsForUrl()
  3045. {
  3046. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  3047. std::unique_ptr<TSessionData> SessionData(Clone());
  3048. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  3049. SessionData->HostName = FactoryDefaults->HostName;
  3050. SessionData->PortNumber = FactoryDefaults->PortNumber;
  3051. SessionData->UserName = FactoryDefaults->UserName;
  3052. SessionData->Password = FactoryDefaults->Password;
  3053. SessionData->Ftps = FactoryDefaults->Ftps;
  3054. SessionData->HostKey = FactoryDefaults->HostKey;
  3055. SessionData->CopyNonCoreData(FactoryDefaults.get());
  3056. // Cannot be decided in SaveToOptions as it does not have HostName and UserName, so it cannot calculate DefaultSessionName.
  3057. bool SaveName = HasSessionName() && (Name != DefaultSessionName);
  3058. return SessionData->SaveToOptions(FactoryDefaults.get(), SaveName, false);
  3059. }
  3060. //---------------------------------------------------------------------
  3061. bool __fastcall TSessionData::HasRawSettingsForUrl()
  3062. {
  3063. std::unique_ptr<TStrings> RawSettings(GetRawSettingsForUrl());
  3064. return (RawSettings->Count > 0);
  3065. }
  3066. //---------------------------------------------------------------------
  3067. UnicodeString __fastcall TSessionData::GenerateSessionUrl(unsigned int Flags)
  3068. {
  3069. UnicodeString Url;
  3070. if (FLAGSET(Flags, sufSpecific))
  3071. {
  3072. Url += WinSCPProtocolPrefix;
  3073. }
  3074. Url += GetProtocolUrl(FLAGSET(Flags, sufHttpForWebDAV));
  3075. if (FLAGSET(Flags, sufUserName) && !UserNameExpanded.IsEmpty())
  3076. {
  3077. Url += EncodeUrlString(UserNameExpanded);
  3078. if (FLAGSET(Flags, sufPassword) && !Password.IsEmpty())
  3079. {
  3080. Url += L":" + EncodeUrlString(NormalizeString(Password));
  3081. }
  3082. if (FLAGSET(Flags, sufHostKey) && !HostKey.IsEmpty())
  3083. {
  3084. UnicodeString KeyName;
  3085. UnicodeString Fingerprint = HostKey;
  3086. NormalizeFingerprint(Fingerprint, KeyName);
  3087. UnicodeString S = Fingerprint;
  3088. if (!KeyName.IsEmpty())
  3089. {
  3090. S = KeyName + NormalizedFingerprintSeparator + S;
  3091. }
  3092. S = Base64ToUrlSafe(S); // Noop for MD5 (both in SSH host keys and TLS/SSL)
  3093. S = MD5ToUrlSafe(S); // TLS/SSL fingerprints
  3094. UnicodeString S2 = EncodeUrlString(S);
  3095. DebugAssert(S2 == S2); // There should be nothing left for encoding
  3096. Url +=
  3097. UnicodeString(UrlParamSeparator) + UrlHostKeyParamName +
  3098. UnicodeString(UrlParamValueSeparator) + S2;
  3099. }
  3100. if (FLAGSET(Flags, sufRawSettings))
  3101. {
  3102. std::unique_ptr<TStrings> RawSettings(GetRawSettingsForUrl());
  3103. for (int Index = 0; Index < RawSettings->Count; Index++)
  3104. {
  3105. Url +=
  3106. UnicodeString(UrlParamSeparator) +
  3107. UrlRawSettingsParamNamePrefix + EncodeUrlString(LowerCase(RawSettings->Names[Index])) +
  3108. UnicodeString(UrlParamValueSeparator) + EncodeUrlString(RawSettings->ValueFromIndex[Index]);
  3109. }
  3110. }
  3111. Url += L"@";
  3112. }
  3113. DebugAssert(!HostNameExpanded.IsEmpty());
  3114. if (IsIPv6Literal(HostNameExpanded))
  3115. {
  3116. Url += EscapeIPv6Literal(HostNameExpanded);
  3117. }
  3118. else
  3119. {
  3120. Url += EncodeUrlString(HostNameExpanded);
  3121. }
  3122. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  3123. {
  3124. Url += L":" + IntToStr(PortNumber);
  3125. }
  3126. Url += L"/";
  3127. return Url;
  3128. }
  3129. //---------------------------------------------------------------------
  3130. UnicodeString ScriptCommandOpenLink(TraceInitStr(ScriptCommandLink(L"open")));
  3131. //---------------------------------------------------------------------
  3132. void __fastcall TSessionData::AddSwitch(
  3133. UnicodeString & Result, const UnicodeString & Name, bool Rtf)
  3134. {
  3135. Result += RtfSwitch(Name, ScriptCommandOpenLink, Rtf);
  3136. }
  3137. //---------------------------------------------------------------------
  3138. void __fastcall TSessionData::AddSwitch(
  3139. UnicodeString & Result, const UnicodeString & Name, const UnicodeString & Value, bool Rtf)
  3140. {
  3141. Result += RtfSwitch(Name, ScriptCommandOpenLink, Value, Rtf);
  3142. }
  3143. //---------------------------------------------------------------------
  3144. void __fastcall TSessionData::AddSwitch(
  3145. UnicodeString & Result, const UnicodeString & Name, int Value, bool Rtf)
  3146. {
  3147. Result += RtfSwitch(Name, ScriptCommandOpenLink, Value, Rtf);
  3148. }
  3149. //---------------------------------------------------------------------
  3150. void __fastcall TSessionData::LookupLastFingerprint()
  3151. {
  3152. UnicodeString FingerprintType;
  3153. if (IsSshProtocol(FSProtocol))
  3154. {
  3155. FingerprintType = SshFingerprintType;
  3156. }
  3157. else if (Ftps != ftpsNone)
  3158. {
  3159. FingerprintType = TlsFingerprintType;
  3160. }
  3161. if (!FingerprintType.IsEmpty())
  3162. {
  3163. HostKey = Configuration->LastFingerprint(SiteKey, FingerprintType);
  3164. }
  3165. }
  3166. //---------------------------------------------------------------------
  3167. UnicodeString __fastcall TSessionData::GenerateOpenCommandArgs(bool Rtf)
  3168. {
  3169. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  3170. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  3171. SessionData->Assign(this);
  3172. UnicodeString Result = SessionData->GenerateSessionUrl(sufOpen);
  3173. // Before we reset the FSProtocol
  3174. bool AUsesSsh = SessionData->UsesSsh;
  3175. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  3176. if (SessionData->FSProtocol != fsSFTPonly)
  3177. {
  3178. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  3179. }
  3180. SessionData->HostName = FactoryDefaults->HostName;
  3181. SessionData->PortNumber = FactoryDefaults->PortNumber;
  3182. SessionData->UserName = FactoryDefaults->UserName;
  3183. SessionData->Password = FactoryDefaults->Password;
  3184. SessionData->CopyNonCoreData(FactoryDefaults.get());
  3185. SessionData->Ftps = FactoryDefaults->Ftps;
  3186. if (SessionData->HostKey != FactoryDefaults->HostKey)
  3187. {
  3188. UnicodeString SwitchName = AUsesSsh ? L"hostkey" : L"certificate";
  3189. AddSwitch(Result, SwitchName, SessionData->HostKey, Rtf);
  3190. SessionData->HostKey = FactoryDefaults->HostKey;
  3191. }
  3192. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  3193. {
  3194. AddSwitch(Result, L"privatekey", SessionData->PublicKeyFile, Rtf);
  3195. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  3196. }
  3197. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  3198. {
  3199. AddSwitch(Result, L"clientcert", SessionData->TlsCertificateFile, Rtf);
  3200. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  3201. }
  3202. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  3203. {
  3204. AddSwitch(Result, PassphraseOption, SessionData->Passphrase, Rtf);
  3205. SessionData->Passphrase = FactoryDefaults->Passphrase;
  3206. }
  3207. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  3208. {
  3209. AddSwitch(Result, L"passive", SessionData->FtpPasvMode ? 1 : 0, Rtf);
  3210. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  3211. }
  3212. if (SessionData->Timeout != FactoryDefaults->Timeout)
  3213. {
  3214. AddSwitch(Result, L"timeout", SessionData->Timeout, Rtf);
  3215. SessionData->Timeout = FactoryDefaults->Timeout;
  3216. }
  3217. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get(), false, false));
  3218. if (RawSettings->Count > 0)
  3219. {
  3220. AddSwitch(Result, RawSettingsOption, Rtf);
  3221. Result += StringsToParams(RawSettings.get());
  3222. }
  3223. return Result;
  3224. }
  3225. //---------------------------------------------------------------------
  3226. UnicodeString SessionOptionsClassName(L"SessionOptions");
  3227. //---------------------------------------------------------------------
  3228. void __fastcall TSessionData::AddAssemblyProperty(
  3229. UnicodeString & Result, TAssemblyLanguage Language,
  3230. const UnicodeString & Name, const UnicodeString & Type,
  3231. const UnicodeString & Member)
  3232. {
  3233. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Type, Member, false);
  3234. }
  3235. //---------------------------------------------------------------------
  3236. void __fastcall TSessionData::AddAssemblyProperty(
  3237. UnicodeString & Result, TAssemblyLanguage Language,
  3238. const UnicodeString & Name, const UnicodeString & Value)
  3239. {
  3240. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  3241. }
  3242. //---------------------------------------------------------------------
  3243. void __fastcall TSessionData::AddAssemblyProperty(
  3244. UnicodeString & Result, TAssemblyLanguage Language,
  3245. const UnicodeString & Name, int Value)
  3246. {
  3247. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  3248. }
  3249. //---------------------------------------------------------------------
  3250. void __fastcall TSessionData::AddAssemblyProperty(
  3251. UnicodeString & Result, TAssemblyLanguage Language,
  3252. const UnicodeString & Name, bool Value)
  3253. {
  3254. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  3255. }
  3256. //---------------------------------------------------------------------
  3257. void __fastcall TSessionData::GenerateAssemblyCode(
  3258. TAssemblyLanguage Language, UnicodeString & Head, UnicodeString & Tail, int & Indent)
  3259. {
  3260. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  3261. std::unique_ptr<TSessionData> SessionData(Clone());
  3262. switch (Language)
  3263. {
  3264. case alCSharp:
  3265. case alVBNET:
  3266. // noop
  3267. break;
  3268. case alPowerShell:
  3269. Head +=
  3270. AssemblyCommentLine(Language, LoadStr(CODE_PS_ADD_TYPE)) +
  3271. RtfKeyword(L"Add-Type") + RtfText(" -Path ") + AssemblyString(Language, "WinSCPnet.dll") + RtfPara +
  3272. RtfPara;
  3273. break;
  3274. default:
  3275. DebugFail();
  3276. break;
  3277. }
  3278. Head +=
  3279. AssemblyCommentLine(Language, LoadStr(CODE_SESSION_OPTIONS)) +
  3280. AssemblyNewClassInstanceStart(Language, SessionOptionsClassName, false);
  3281. UnicodeString ProtocolMember;
  3282. switch (SessionData->FSProtocol)
  3283. {
  3284. case fsSCPonly:
  3285. ProtocolMember = "Scp";
  3286. break;
  3287. default:
  3288. DebugFail();
  3289. // fallback
  3290. case fsSFTP:
  3291. case fsSFTPonly:
  3292. ProtocolMember = "Sftp";
  3293. break;
  3294. case fsFTP:
  3295. ProtocolMember = "Ftp";
  3296. break;
  3297. case fsWebDAV:
  3298. ProtocolMember = "Webdav";
  3299. break;
  3300. case fsS3:
  3301. ProtocolMember = "S3";
  3302. break;
  3303. }
  3304. // Before we reset the FSProtocol
  3305. bool AUsesSsh = SessionData->UsesSsh;
  3306. // Protocol is set unconditionally, we want even the default SFTP
  3307. AddAssemblyProperty(Head, Language, L"Protocol", L"Protocol", ProtocolMember);
  3308. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  3309. if (SessionData->FSProtocol != fsSFTPonly)
  3310. {
  3311. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  3312. }
  3313. if (SessionData->HostName != FactoryDefaults->HostName)
  3314. {
  3315. AddAssemblyProperty(Head, Language, L"HostName", HostName);
  3316. SessionData->HostName = FactoryDefaults->HostName;
  3317. }
  3318. int ADefaultPort = DefaultPort(FSProtocol, Ftps);
  3319. if (SessionData->PortNumber != ADefaultPort)
  3320. {
  3321. AddAssemblyProperty(Head, Language, L"PortNumber", PortNumber);
  3322. }
  3323. SessionData->PortNumber = FactoryDefaults->PortNumber;
  3324. if (SessionData->UserName != FactoryDefaults->UserName)
  3325. {
  3326. AddAssemblyProperty(Head, Language, L"UserName", UserName);
  3327. SessionData->UserName = FactoryDefaults->UserName;
  3328. }
  3329. if (SessionData->Password != FactoryDefaults->Password)
  3330. {
  3331. AddAssemblyProperty(Head, Language, L"Password", NormalizeString(Password));
  3332. SessionData->Password = FactoryDefaults->Password;
  3333. }
  3334. SessionData->CopyNonCoreData(FactoryDefaults.get());
  3335. if (SessionData->Ftps != FactoryDefaults->Ftps)
  3336. {
  3337. // SessionData->FSProtocol is reset already
  3338. switch (FSProtocol)
  3339. {
  3340. case fsFTP:
  3341. {
  3342. UnicodeString FtpSecureMember;
  3343. switch (SessionData->Ftps)
  3344. {
  3345. case ftpsNone:
  3346. // noop
  3347. break;
  3348. case ftpsImplicit:
  3349. FtpSecureMember = L"Implicit";
  3350. break;
  3351. case ftpsExplicitTls:
  3352. case ftpsExplicitSsl:
  3353. FtpSecureMember = L"Explicit";
  3354. break;
  3355. default:
  3356. DebugFail();
  3357. break;
  3358. }
  3359. AddAssemblyProperty(Head, Language, L"FtpSecure", L"FtpSecure", FtpSecureMember);
  3360. }
  3361. break;
  3362. case fsWebDAV:
  3363. AddAssemblyProperty(Head, Language, L"WebdavSecure", (SessionData->Ftps != ftpsNone));
  3364. break;
  3365. case fsS3:
  3366. // implicit
  3367. break;
  3368. default:
  3369. DebugFail();
  3370. break;
  3371. }
  3372. SessionData->Ftps = FactoryDefaults->Ftps;
  3373. }
  3374. if (SessionData->HostKey != FactoryDefaults->HostKey)
  3375. {
  3376. UnicodeString PropertyName = AUsesSsh ? L"SshHostKeyFingerprint" : L"TlsHostCertificateFingerprint";
  3377. AddAssemblyProperty(Head, Language, PropertyName, SessionData->HostKey);
  3378. SessionData->HostKey = FactoryDefaults->HostKey;
  3379. }
  3380. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  3381. {
  3382. AddAssemblyProperty(Head, Language, L"SshPrivateKeyPath", SessionData->PublicKeyFile);
  3383. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  3384. }
  3385. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  3386. {
  3387. AddAssemblyProperty(Head, Language, L"TlsClientCertificatePath", SessionData->TlsCertificateFile);
  3388. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  3389. }
  3390. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  3391. {
  3392. AddAssemblyProperty(Head, Language, L"PrivateKeyPassphrase", SessionData->Passphrase);
  3393. SessionData->Passphrase = FactoryDefaults->Passphrase;
  3394. }
  3395. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  3396. {
  3397. AddAssemblyProperty(Head, Language, L"FtpMode", L"FtpMode", (SessionData->FtpPasvMode ? L"Passive" : L"Active"));
  3398. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  3399. }
  3400. if (SessionData->Timeout != FactoryDefaults->Timeout)
  3401. {
  3402. AddAssemblyProperty(Head, Language, L"TimeoutInMilliseconds", SessionData->Timeout * 1000);
  3403. SessionData->Timeout = FactoryDefaults->Timeout;
  3404. }
  3405. Head += AssemblyNewClassInstanceEnd(Language, false);
  3406. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get(), false, false));
  3407. UnicodeString SessionOptionsVariableName = AssemblyVariableName(Language, SessionOptionsClassName);
  3408. if (RawSettings->Count > 0)
  3409. {
  3410. Head +=
  3411. RtfPara +
  3412. AssemblyAddRawSettings(Language, RawSettings.get(), SessionOptionsClassName, L"AddRawSettings");
  3413. }
  3414. Head += RtfPara;
  3415. UnicodeString Indentation = L" ";
  3416. UnicodeString SessionVariableName = AssemblyVariableName(Language, SessionClassName);
  3417. UnicodeString RtfSessionClass = RtfLibraryClass(SessionClassName);
  3418. UnicodeString RtfSessionOpenMethod = RtfLibraryMethod(SessionClassName, L"Open", false);
  3419. UnicodeString NewSessionInstance = AssemblyNewClassInstance(Language, SessionClassName, false);
  3420. UnicodeString OpenCall =
  3421. Indentation + AssemblyCommentLine(Language, LoadStr(CODE_CONNECT)) +
  3422. Indentation + RtfText(SessionVariableName + L".") + RtfSessionOpenMethod + RtfText(L"(" + SessionOptionsVariableName + L")") +
  3423. AssemblyStatementSeparator(Language) + RtfPara;
  3424. switch (Language)
  3425. {
  3426. case alCSharp:
  3427. Head +=
  3428. RtfKeyword(L"using") + RtfText(" (") + NewSessionInstance + RtfText(L"())") + RtfPara +
  3429. RtfText(L"{") + RtfPara +
  3430. OpenCall;
  3431. Tail =
  3432. RtfText(L"}") + RtfPara;
  3433. break;
  3434. case alVBNET:
  3435. Head +=
  3436. RtfKeyword(L"Using") + RtfText(L" ") + NewSessionInstance + RtfPara +
  3437. OpenCall;
  3438. Tail =
  3439. RtfKeyword(L"End Using") + RtfPara;
  3440. break;
  3441. case alPowerShell:
  3442. Head +=
  3443. NewSessionInstance + RtfPara +
  3444. RtfPara +
  3445. RtfKeyword(L"try") + RtfPara +
  3446. RtfText(L"{") + RtfPara +
  3447. OpenCall;
  3448. Tail =
  3449. RtfText(L"}") + RtfPara +
  3450. RtfKeyword(L"finally") + RtfPara +
  3451. RtfText(L"{") + RtfPara +
  3452. RtfText(Indentation + SessionVariableName + L".") +
  3453. RtfLibraryMethod(SessionClassName, L"Dispose", false) + RtfText(L"()") + RtfPara +
  3454. RtfText(L"}") + RtfPara;
  3455. break;
  3456. }
  3457. Head += RtfPara;
  3458. Indent = 4; // the same for all languages so far
  3459. }
  3460. //---------------------------------------------------------------------
  3461. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  3462. {
  3463. SET_SESSION_PROPERTY(TimeDifference);
  3464. }
  3465. //---------------------------------------------------------------------
  3466. void __fastcall TSessionData::SetTimeDifferenceAuto(bool value)
  3467. {
  3468. SET_SESSION_PROPERTY(TimeDifferenceAuto);
  3469. }
  3470. //---------------------------------------------------------------------
  3471. void __fastcall TSessionData::SetLocalDirectory(UnicodeString value)
  3472. {
  3473. SET_SESSION_PROPERTY(LocalDirectory);
  3474. }
  3475. //---------------------------------------------------------------------
  3476. UnicodeString __fastcall TSessionData::GetLocalDirectoryExpanded()
  3477. {
  3478. return ExpandFileName(::ExpandEnvironmentVariables(LocalDirectory));
  3479. }
  3480. //---------------------------------------------------------------------
  3481. void __fastcall TSessionData::SetRemoteDirectory(UnicodeString value)
  3482. {
  3483. SET_SESSION_PROPERTY(RemoteDirectory);
  3484. }
  3485. //---------------------------------------------------------------------
  3486. void __fastcall TSessionData::SetSynchronizeBrowsing(bool value)
  3487. {
  3488. SET_SESSION_PROPERTY(SynchronizeBrowsing);
  3489. }
  3490. //---------------------------------------------------------------------
  3491. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  3492. {
  3493. SET_SESSION_PROPERTY(UpdateDirectories);
  3494. }
  3495. //---------------------------------------------------------------------
  3496. void __fastcall TSessionData::SetCacheDirectories(bool value)
  3497. {
  3498. SET_SESSION_PROPERTY(CacheDirectories);
  3499. }
  3500. //---------------------------------------------------------------------
  3501. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  3502. {
  3503. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  3504. }
  3505. //---------------------------------------------------------------------
  3506. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  3507. {
  3508. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  3509. }
  3510. //---------------------------------------------------------------------
  3511. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  3512. {
  3513. SET_SESSION_PROPERTY(ResolveSymlinks);
  3514. }
  3515. //---------------------------------------------------------------------
  3516. void __fastcall TSessionData::SetFollowDirectorySymlinks(bool value)
  3517. {
  3518. SET_SESSION_PROPERTY(FollowDirectorySymlinks);
  3519. }
  3520. //---------------------------------------------------------------------------
  3521. void __fastcall TSessionData::SetDSTMode(TDSTMode value)
  3522. {
  3523. SET_SESSION_PROPERTY(DSTMode);
  3524. }
  3525. //---------------------------------------------------------------------------
  3526. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  3527. {
  3528. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  3529. }
  3530. //---------------------------------------------------------------------------
  3531. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  3532. {
  3533. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  3534. }
  3535. //---------------------------------------------------------------------------
  3536. void __fastcall TSessionData::SetRecycleBinPath(UnicodeString value)
  3537. {
  3538. SET_SESSION_PROPERTY(RecycleBinPath);
  3539. }
  3540. //---------------------------------------------------------------------------
  3541. void __fastcall TSessionData::SetPostLoginCommands(UnicodeString value)
  3542. {
  3543. SET_SESSION_PROPERTY(PostLoginCommands);
  3544. }
  3545. //---------------------------------------------------------------------
  3546. void __fastcall TSessionData::SetLockInHome(bool value)
  3547. {
  3548. SET_SESSION_PROPERTY(LockInHome);
  3549. }
  3550. //---------------------------------------------------------------------
  3551. void __fastcall TSessionData::SetSpecial(bool value)
  3552. {
  3553. SET_SESSION_PROPERTY(Special);
  3554. }
  3555. //---------------------------------------------------------------------------
  3556. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  3557. {
  3558. SET_SESSION_PROPERTY(Scp1Compatibility);
  3559. }
  3560. //---------------------------------------------------------------------
  3561. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  3562. {
  3563. SET_SESSION_PROPERTY(TcpNoDelay);
  3564. }
  3565. //---------------------------------------------------------------------
  3566. void __fastcall TSessionData::SetSendBuf(int value)
  3567. {
  3568. SET_SESSION_PROPERTY(SendBuf);
  3569. }
  3570. //---------------------------------------------------------------------
  3571. void __fastcall TSessionData::SetSourceAddress(const UnicodeString & value)
  3572. {
  3573. SET_SESSION_PROPERTY(SourceAddress);
  3574. }
  3575. //---------------------------------------------------------------------
  3576. void __fastcall TSessionData::SetProtocolFeatures(const UnicodeString & value)
  3577. {
  3578. SET_SESSION_PROPERTY(ProtocolFeatures);
  3579. }
  3580. //---------------------------------------------------------------------
  3581. void __fastcall TSessionData::SetSshSimple(bool value)
  3582. {
  3583. SET_SESSION_PROPERTY(SshSimple);
  3584. }
  3585. //---------------------------------------------------------------------
  3586. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  3587. {
  3588. SET_SESSION_PROPERTY(ProxyMethod);
  3589. }
  3590. //---------------------------------------------------------------------
  3591. void __fastcall TSessionData::SetProxyHost(UnicodeString value)
  3592. {
  3593. SET_SESSION_PROPERTY(ProxyHost);
  3594. }
  3595. //---------------------------------------------------------------------
  3596. void __fastcall TSessionData::SetProxyPort(int value)
  3597. {
  3598. SET_SESSION_PROPERTY(ProxyPort);
  3599. }
  3600. //---------------------------------------------------------------------
  3601. void __fastcall TSessionData::SetProxyUsername(UnicodeString value)
  3602. {
  3603. SET_SESSION_PROPERTY(ProxyUsername);
  3604. }
  3605. //---------------------------------------------------------------------
  3606. void __fastcall TSessionData::SetProxyPassword(UnicodeString avalue)
  3607. {
  3608. RawByteString value = EncryptPassword(avalue, ProxyUsername+ProxyHost);
  3609. SET_SESSION_PROPERTY(ProxyPassword);
  3610. }
  3611. //---------------------------------------------------------------------
  3612. UnicodeString __fastcall TSessionData::GetProxyPassword() const
  3613. {
  3614. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  3615. }
  3616. //---------------------------------------------------------------------
  3617. void __fastcall TSessionData::SetProxyTelnetCommand(UnicodeString value)
  3618. {
  3619. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  3620. }
  3621. //---------------------------------------------------------------------
  3622. void __fastcall TSessionData::SetProxyLocalCommand(UnicodeString value)
  3623. {
  3624. SET_SESSION_PROPERTY(ProxyLocalCommand);
  3625. }
  3626. //---------------------------------------------------------------------
  3627. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  3628. {
  3629. SET_SESSION_PROPERTY(ProxyDNS);
  3630. }
  3631. //---------------------------------------------------------------------
  3632. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  3633. {
  3634. SET_SESSION_PROPERTY(ProxyLocalhost);
  3635. }
  3636. //---------------------------------------------------------------------
  3637. void __fastcall TSessionData::SetFtpProxyLogonType(int value)
  3638. {
  3639. SET_SESSION_PROPERTY(FtpProxyLogonType);
  3640. }
  3641. //---------------------------------------------------------------------
  3642. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  3643. {
  3644. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  3645. SET_SESSION_PROPERTY(Bugs[Bug]);
  3646. }
  3647. //---------------------------------------------------------------------
  3648. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  3649. {
  3650. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  3651. return FBugs[Bug];
  3652. }
  3653. //---------------------------------------------------------------------
  3654. void __fastcall TSessionData::SetPuttySettings(UnicodeString value)
  3655. {
  3656. SET_SESSION_PROPERTY(PuttySettings);
  3657. }
  3658. //---------------------------------------------------------------------
  3659. void __fastcall TSessionData::SetCustomParam1(UnicodeString value)
  3660. {
  3661. SET_SESSION_PROPERTY(CustomParam1);
  3662. }
  3663. //---------------------------------------------------------------------
  3664. void __fastcall TSessionData::SetCustomParam2(UnicodeString value)
  3665. {
  3666. SET_SESSION_PROPERTY(CustomParam2);
  3667. }
  3668. //---------------------------------------------------------------------
  3669. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  3670. {
  3671. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  3672. }
  3673. //---------------------------------------------------------------------
  3674. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  3675. {
  3676. SET_SESSION_PROPERTY(SFTPUploadQueue);
  3677. }
  3678. //---------------------------------------------------------------------
  3679. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  3680. {
  3681. SET_SESSION_PROPERTY(SFTPListingQueue);
  3682. }
  3683. //---------------------------------------------------------------------
  3684. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  3685. {
  3686. SET_SESSION_PROPERTY(SFTPMaxVersion);
  3687. }
  3688. //---------------------------------------------------------------------
  3689. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  3690. {
  3691. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  3692. }
  3693. //---------------------------------------------------------------------
  3694. void __fastcall TSessionData::SetSFTPRealPath(TAutoSwitch value)
  3695. {
  3696. SET_SESSION_PROPERTY(SFTPRealPath);
  3697. }
  3698. //---------------------------------------------------------------------
  3699. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  3700. {
  3701. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  3702. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  3703. }
  3704. //---------------------------------------------------------------------
  3705. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  3706. {
  3707. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  3708. return FSFTPBugs[Bug];
  3709. }
  3710. //---------------------------------------------------------------------
  3711. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  3712. {
  3713. SET_SESSION_PROPERTY(SCPLsFullTime);
  3714. }
  3715. //---------------------------------------------------------------------------
  3716. void __fastcall TSessionData::SetColor(int value)
  3717. {
  3718. SET_SESSION_PROPERTY(Color);
  3719. }
  3720. //---------------------------------------------------------------------------
  3721. void __fastcall TSessionData::SetTunnel(bool value)
  3722. {
  3723. SET_SESSION_PROPERTY(Tunnel);
  3724. }
  3725. //---------------------------------------------------------------------
  3726. void __fastcall TSessionData::SetTunnelHostName(UnicodeString value)
  3727. {
  3728. if (FTunnelHostName != value)
  3729. {
  3730. // HostName is key for password encryption
  3731. UnicodeString XTunnelPassword = TunnelPassword;
  3732. int P = value.LastDelimiter(L"@");
  3733. if (P > 0)
  3734. {
  3735. TunnelUserName = value.SubString(1, P - 1);
  3736. value = value.SubString(P + 1, value.Length() - P);
  3737. }
  3738. FTunnelHostName = value;
  3739. Modify();
  3740. TunnelPassword = XTunnelPassword;
  3741. Shred(XTunnelPassword);
  3742. }
  3743. }
  3744. //---------------------------------------------------------------------
  3745. void __fastcall TSessionData::SetTunnelPortNumber(int value)
  3746. {
  3747. SET_SESSION_PROPERTY(TunnelPortNumber);
  3748. }
  3749. //---------------------------------------------------------------------
  3750. void __fastcall TSessionData::SetTunnelUserName(UnicodeString value)
  3751. {
  3752. // Avoid password recryption (what may popup master password prompt)
  3753. if (FTunnelUserName != value)
  3754. {
  3755. // TunnelUserName is key for password encryption
  3756. UnicodeString XTunnelPassword = TunnelPassword;
  3757. SET_SESSION_PROPERTY(TunnelUserName);
  3758. TunnelPassword = XTunnelPassword;
  3759. Shred(XTunnelPassword);
  3760. }
  3761. }
  3762. //---------------------------------------------------------------------
  3763. void __fastcall TSessionData::SetTunnelPassword(UnicodeString avalue)
  3764. {
  3765. RawByteString value = EncryptPassword(avalue, TunnelUserName+TunnelHostName);
  3766. SET_SESSION_PROPERTY(TunnelPassword);
  3767. }
  3768. //---------------------------------------------------------------------
  3769. UnicodeString __fastcall TSessionData::GetTunnelPassword() const
  3770. {
  3771. return DecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName);
  3772. }
  3773. //---------------------------------------------------------------------
  3774. void __fastcall TSessionData::SetTunnelPublicKeyFile(UnicodeString value)
  3775. {
  3776. if (FTunnelPublicKeyFile != value)
  3777. {
  3778. // StripPathQuotes should not be needed as we do not feed quotes anymore
  3779. FTunnelPublicKeyFile = StripPathQuotes(value);
  3780. Modify();
  3781. }
  3782. }
  3783. //---------------------------------------------------------------------
  3784. void __fastcall TSessionData::SetTunnelLocalPortNumber(int value)
  3785. {
  3786. SET_SESSION_PROPERTY(TunnelLocalPortNumber);
  3787. }
  3788. //---------------------------------------------------------------------
  3789. bool __fastcall TSessionData::GetTunnelAutoassignLocalPortNumber()
  3790. {
  3791. return (FTunnelLocalPortNumber <= 0);
  3792. }
  3793. //---------------------------------------------------------------------
  3794. void __fastcall TSessionData::SetTunnelPortFwd(UnicodeString value)
  3795. {
  3796. SET_SESSION_PROPERTY(TunnelPortFwd);
  3797. }
  3798. //---------------------------------------------------------------------
  3799. void __fastcall TSessionData::SetTunnelHostKey(UnicodeString value)
  3800. {
  3801. SET_SESSION_PROPERTY(TunnelHostKey);
  3802. }
  3803. //---------------------------------------------------------------------
  3804. void __fastcall TSessionData::SetFtpPasvMode(bool value)
  3805. {
  3806. SET_SESSION_PROPERTY(FtpPasvMode);
  3807. }
  3808. //---------------------------------------------------------------------
  3809. void __fastcall TSessionData::SetFtpForcePasvIp(TAutoSwitch value)
  3810. {
  3811. SET_SESSION_PROPERTY(FtpForcePasvIp);
  3812. }
  3813. //---------------------------------------------------------------------
  3814. void __fastcall TSessionData::SetFtpUseMlsd(TAutoSwitch value)
  3815. {
  3816. SET_SESSION_PROPERTY(FtpUseMlsd);
  3817. }
  3818. //---------------------------------------------------------------------
  3819. void __fastcall TSessionData::SetFtpAccount(UnicodeString value)
  3820. {
  3821. SET_SESSION_PROPERTY(FtpAccount);
  3822. }
  3823. //---------------------------------------------------------------------
  3824. void __fastcall TSessionData::SetFtpPingInterval(int value)
  3825. {
  3826. SET_SESSION_PROPERTY(FtpPingInterval);
  3827. }
  3828. //---------------------------------------------------------------------------
  3829. TDateTime __fastcall TSessionData::GetFtpPingIntervalDT()
  3830. {
  3831. return SecToDateTime(FtpPingInterval);
  3832. }
  3833. //---------------------------------------------------------------------------
  3834. void __fastcall TSessionData::SetFtpPingType(TPingType value)
  3835. {
  3836. SET_SESSION_PROPERTY(FtpPingType);
  3837. }
  3838. //---------------------------------------------------------------------------
  3839. void __fastcall TSessionData::SetFtpTransferActiveImmediately(TAutoSwitch value)
  3840. {
  3841. SET_SESSION_PROPERTY(FtpTransferActiveImmediately);
  3842. }
  3843. //---------------------------------------------------------------------------
  3844. void __fastcall TSessionData::SetFtps(TFtps value)
  3845. {
  3846. SET_SESSION_PROPERTY(Ftps);
  3847. }
  3848. //---------------------------------------------------------------------------
  3849. void __fastcall TSessionData::SetMinTlsVersion(TTlsVersion value)
  3850. {
  3851. SET_SESSION_PROPERTY(MinTlsVersion);
  3852. }
  3853. //---------------------------------------------------------------------------
  3854. void __fastcall TSessionData::SetMaxTlsVersion(TTlsVersion value)
  3855. {
  3856. SET_SESSION_PROPERTY(MaxTlsVersion);
  3857. }
  3858. //---------------------------------------------------------------------------
  3859. void __fastcall TSessionData::SetLogicalHostName(UnicodeString value)
  3860. {
  3861. SET_SESSION_PROPERTY(LogicalHostName);
  3862. }
  3863. //---------------------------------------------------------------------
  3864. void __fastcall TSessionData::SetFtpListAll(TAutoSwitch value)
  3865. {
  3866. SET_SESSION_PROPERTY(FtpListAll);
  3867. }
  3868. //---------------------------------------------------------------------
  3869. void __fastcall TSessionData::SetFtpHost(TAutoSwitch value)
  3870. {
  3871. SET_SESSION_PROPERTY(FtpHost);
  3872. }
  3873. //---------------------------------------------------------------------
  3874. void __fastcall TSessionData::SetFtpWorkFromCwd(TAutoSwitch value)
  3875. {
  3876. SET_SESSION_PROPERTY(FtpWorkFromCwd);
  3877. }
  3878. //---------------------------------------------------------------------
  3879. void __fastcall TSessionData::SetSslSessionReuse(bool value)
  3880. {
  3881. SET_SESSION_PROPERTY(SslSessionReuse);
  3882. }
  3883. //---------------------------------------------------------------------
  3884. void __fastcall TSessionData::SetTlsCertificateFile(UnicodeString value)
  3885. {
  3886. SET_SESSION_PROPERTY(TlsCertificateFile);
  3887. }
  3888. //---------------------------------------------------------------------
  3889. void __fastcall TSessionData::SetNotUtf(TAutoSwitch value)
  3890. {
  3891. SET_SESSION_PROPERTY(NotUtf);
  3892. }
  3893. //---------------------------------------------------------------------
  3894. void __fastcall TSessionData::SetInternalEditorEncoding(int value)
  3895. {
  3896. SET_SESSION_PROPERTY(InternalEditorEncoding);
  3897. }
  3898. //---------------------------------------------------------------------
  3899. void __fastcall TSessionData::SetS3DefaultRegion(UnicodeString value)
  3900. {
  3901. SET_SESSION_PROPERTY(S3DefaultRegion);
  3902. }
  3903. //---------------------------------------------------------------------
  3904. void __fastcall TSessionData::SetS3SessionToken(UnicodeString value)
  3905. {
  3906. SET_SESSION_PROPERTY(S3SessionToken);
  3907. }
  3908. //---------------------------------------------------------------------
  3909. void __fastcall TSessionData::SetS3UrlStyle(TS3UrlStyle value)
  3910. {
  3911. SET_SESSION_PROPERTY(S3UrlStyle);
  3912. }
  3913. //---------------------------------------------------------------------
  3914. void __fastcall TSessionData::SetS3MaxKeys(TAutoSwitch value)
  3915. {
  3916. SET_SESSION_PROPERTY(S3MaxKeys);
  3917. }
  3918. //---------------------------------------------------------------------
  3919. void __fastcall TSessionData::SetIsWorkspace(bool value)
  3920. {
  3921. SET_SESSION_PROPERTY(IsWorkspace);
  3922. }
  3923. //---------------------------------------------------------------------
  3924. void __fastcall TSessionData::SetLink(UnicodeString value)
  3925. {
  3926. SET_SESSION_PROPERTY(Link);
  3927. }
  3928. //---------------------------------------------------------------------
  3929. void __fastcall TSessionData::SetNameOverride(UnicodeString value)
  3930. {
  3931. SET_SESSION_PROPERTY(NameOverride);
  3932. }
  3933. //---------------------------------------------------------------------
  3934. void __fastcall TSessionData::SetHostKey(UnicodeString value)
  3935. {
  3936. SET_SESSION_PROPERTY(HostKey);
  3937. }
  3938. //---------------------------------------------------------------------
  3939. void __fastcall TSessionData::SetNote(UnicodeString value)
  3940. {
  3941. SET_SESSION_PROPERTY(Note);
  3942. }
  3943. //---------------------------------------------------------------------
  3944. void __fastcall TSessionData::SetWinTitle(UnicodeString value)
  3945. {
  3946. SET_SESSION_PROPERTY(WinTitle);
  3947. }
  3948. //---------------------------------------------------------------------
  3949. UnicodeString __fastcall TSessionData::GetEncryptKey() const
  3950. {
  3951. return DecryptPassword(FEncryptKey, UserName+HostName);
  3952. }
  3953. //---------------------------------------------------------------------
  3954. void __fastcall TSessionData::SetEncryptKey(UnicodeString avalue)
  3955. {
  3956. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  3957. SET_SESSION_PROPERTY(EncryptKey);
  3958. }
  3959. //---------------------------------------------------------------------
  3960. void __fastcall TSessionData::SetWebDavLiberalEscaping(bool value)
  3961. {
  3962. SET_SESSION_PROPERTY(WebDavLiberalEscaping);
  3963. }
  3964. //---------------------------------------------------------------------
  3965. UnicodeString __fastcall TSessionData::GetInfoTip()
  3966. {
  3967. if (UsesSsh)
  3968. {
  3969. return FMTLOAD(SESSION_INFO_TIP2,
  3970. (HostName, UserName,
  3971. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  3972. FSProtocolStr));
  3973. }
  3974. else
  3975. {
  3976. return FMTLOAD(SESSION_INFO_TIP_NO_SSH,
  3977. (HostName, UserName, FSProtocolStr));
  3978. }
  3979. }
  3980. //---------------------------------------------------------------------
  3981. UnicodeString __fastcall TSessionData::ExtractLocalName(const UnicodeString & Name)
  3982. {
  3983. UnicodeString Result = Name;
  3984. int P = Result.LastDelimiter(L"/");
  3985. if (P > 0)
  3986. {
  3987. Result.Delete(1, P);
  3988. }
  3989. return Result;
  3990. }
  3991. //---------------------------------------------------------------------
  3992. UnicodeString __fastcall TSessionData::GetLocalName()
  3993. {
  3994. UnicodeString Result;
  3995. if (HasSessionName())
  3996. {
  3997. Result = ExtractLocalName(Name);
  3998. }
  3999. else
  4000. {
  4001. Result = DefaultSessionName;
  4002. }
  4003. return Result;
  4004. }
  4005. //---------------------------------------------------------------------
  4006. UnicodeString __fastcall TSessionData::ExtractFolderName(const UnicodeString & Name)
  4007. {
  4008. UnicodeString Result;
  4009. int P = Name.LastDelimiter(L"/");
  4010. if (P > 0)
  4011. {
  4012. Result = Name.SubString(1, P - 1);
  4013. }
  4014. return Result;
  4015. }
  4016. //---------------------------------------------------------------------
  4017. UnicodeString __fastcall TSessionData::GetFolderName()
  4018. {
  4019. UnicodeString Result;
  4020. if (HasSessionName() || IsWorkspace)
  4021. {
  4022. Result = ExtractFolderName(Name);
  4023. }
  4024. return Result;
  4025. }
  4026. //---------------------------------------------------------------------
  4027. UnicodeString __fastcall TSessionData::ComposePath(
  4028. const UnicodeString & Path, const UnicodeString & Name)
  4029. {
  4030. return UnixIncludeTrailingBackslash(Path) + Name;
  4031. }
  4032. //---------------------------------------------------------------------
  4033. void __fastcall TSessionData::DisableAuthentationsExceptPassword()
  4034. {
  4035. SshNoUserAuth = false;
  4036. AuthTIS = false;
  4037. AuthKI = false;
  4038. AuthKIPassword = false;
  4039. AuthGSSAPI = false;
  4040. AuthGSSAPIKEX = false;
  4041. PublicKeyFile = L"";
  4042. TlsCertificateFile = L"";
  4043. Passphrase = L"";
  4044. TryAgent = false;
  4045. }
  4046. //---------------------------------------------------------------------
  4047. TStrings * TSessionData::GetAllOptionNames(bool PuttyExport)
  4048. {
  4049. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  4050. return FactoryDefaults->SaveToOptions(NULL, false, PuttyExport);
  4051. }
  4052. //=== TStoredSessionList ----------------------------------------------
  4053. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  4054. TNamedObjectList(), FReadOnly(aReadOnly)
  4055. {
  4056. DebugAssert(Configuration);
  4057. FDefaultSettings = new TSessionData(DefaultName);
  4058. FPendingRemovals.reset(new TStringList());
  4059. }
  4060. //---------------------------------------------------------------------
  4061. __fastcall TStoredSessionList::~TStoredSessionList()
  4062. {
  4063. DebugAssert(Configuration);
  4064. delete FDefaultSettings;
  4065. }
  4066. //---------------------------------------------------------------------
  4067. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  4068. bool AsModified, bool UseDefaults, bool PuttyImport)
  4069. {
  4070. TStringList *SubKeys = new TStringList();
  4071. TList * Loaded = new TList;
  4072. try
  4073. {
  4074. DebugAssert(AutoSort);
  4075. AutoSort = false;
  4076. bool WasEmpty = (Count == 0);
  4077. Storage->GetSubKeyNames(SubKeys);
  4078. for (int Index = 0; Index < SubKeys->Count; Index++)
  4079. {
  4080. UnicodeString SessionName = SubKeys->Strings[Index];
  4081. bool ValidName = true;
  4082. try
  4083. {
  4084. TSessionData::ValidatePath(SessionName);
  4085. }
  4086. catch(...)
  4087. {
  4088. ValidName = false;
  4089. }
  4090. if (ValidName)
  4091. {
  4092. TSessionData * SessionData;
  4093. if (SessionName == FDefaultSettings->Name)
  4094. {
  4095. SessionData = FDefaultSettings;
  4096. }
  4097. else
  4098. {
  4099. // if the list was empty before loading, do not waste time trying to
  4100. // find existing sites to overwrite (we rely on underlying storage
  4101. // to secure uniqueness of the key names)
  4102. if (WasEmpty)
  4103. {
  4104. SessionData = NULL;
  4105. }
  4106. else
  4107. {
  4108. SessionData = (TSessionData*)FindByName(SessionName);
  4109. }
  4110. }
  4111. if ((SessionData != FDefaultSettings) || !UseDefaults)
  4112. {
  4113. if (SessionData == NULL)
  4114. {
  4115. SessionData = new TSessionData(L"");
  4116. if (UseDefaults)
  4117. {
  4118. SessionData->CopyData(DefaultSettings);
  4119. }
  4120. SessionData->Name = SessionName;
  4121. Add(SessionData);
  4122. }
  4123. Loaded->Add(SessionData);
  4124. SessionData->Load(Storage, PuttyImport);
  4125. if (AsModified)
  4126. {
  4127. SessionData->Modified = true;
  4128. }
  4129. }
  4130. }
  4131. }
  4132. if (!AsModified)
  4133. {
  4134. for (int Index = 0; Index < TObjectList::Count; Index++)
  4135. {
  4136. if (Loaded->IndexOf(Items[Index]) < 0)
  4137. {
  4138. Delete(Index);
  4139. Index--;
  4140. }
  4141. }
  4142. }
  4143. }
  4144. __finally
  4145. {
  4146. AutoSort = true;
  4147. AlphaSort();
  4148. delete SubKeys;
  4149. delete Loaded;
  4150. }
  4151. }
  4152. //---------------------------------------------------------------------
  4153. void __fastcall TStoredSessionList::Reload()
  4154. {
  4155. if (Count <= Configuration->DontReloadMoreThanSessions)
  4156. {
  4157. bool SessionList = true;
  4158. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateScpStorage(SessionList));
  4159. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  4160. {
  4161. Load(Storage.get());
  4162. }
  4163. }
  4164. }
  4165. //---------------------------------------------------------------------
  4166. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  4167. TSessionData * Data, bool All, bool RecryptPasswordOnly,
  4168. TSessionData * FactoryDefaults)
  4169. {
  4170. if (All || Data->Modified)
  4171. {
  4172. if (RecryptPasswordOnly)
  4173. {
  4174. Data->SaveRecryptedPasswords(Storage);
  4175. }
  4176. else
  4177. {
  4178. Data->Save(Storage, false, FactoryDefaults);
  4179. }
  4180. }
  4181. }
  4182. //---------------------------------------------------------------------
  4183. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  4184. bool All, bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  4185. {
  4186. TSessionData * FactoryDefaults = new TSessionData(L"");
  4187. try
  4188. {
  4189. while (FPendingRemovals->Count > 0)
  4190. {
  4191. TSessionData::Remove(Storage, FPendingRemovals->Strings[0]);
  4192. FPendingRemovals->Delete(0);
  4193. }
  4194. DoSave(Storage, FDefaultSettings, All, RecryptPasswordOnly, FactoryDefaults);
  4195. for (int Index = 0; Index < CountIncludingHidden; Index++)
  4196. {
  4197. TSessionData * SessionData = (TSessionData *)Items[Index];
  4198. try
  4199. {
  4200. DoSave(Storage, SessionData, All, RecryptPasswordOnly, FactoryDefaults);
  4201. }
  4202. catch (Exception & E)
  4203. {
  4204. UnicodeString Message;
  4205. if (RecryptPasswordOnly && DebugAlwaysTrue(RecryptPasswordErrors != NULL) &&
  4206. ExceptionMessage(&E, Message))
  4207. {
  4208. RecryptPasswordErrors->Add(FORMAT("%s: %s", (SessionData->SessionName, Message)));
  4209. }
  4210. else
  4211. {
  4212. throw;
  4213. }
  4214. }
  4215. }
  4216. }
  4217. __finally
  4218. {
  4219. delete FactoryDefaults;
  4220. }
  4221. }
  4222. //---------------------------------------------------------------------
  4223. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage, bool All)
  4224. {
  4225. DoSave(Storage, All, false, NULL);
  4226. }
  4227. //---------------------------------------------------------------------
  4228. void __fastcall TStoredSessionList::DoSave(bool All, bool Explicit,
  4229. bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  4230. {
  4231. bool SessionList = true;
  4232. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  4233. try
  4234. {
  4235. Storage->AccessMode = smReadWrite;
  4236. Storage->Explicit = Explicit;
  4237. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  4238. {
  4239. DoSave(Storage, All, RecryptPasswordOnly, RecryptPasswordErrors);
  4240. }
  4241. }
  4242. __finally
  4243. {
  4244. delete Storage;
  4245. }
  4246. Saved();
  4247. }
  4248. //---------------------------------------------------------------------
  4249. void __fastcall TStoredSessionList::Save(bool All, bool Explicit)
  4250. {
  4251. DoSave(All, Explicit, false, NULL);
  4252. }
  4253. //---------------------------------------------------------------------
  4254. void __fastcall TStoredSessionList::RecryptPasswords(TStrings * RecryptPasswordErrors)
  4255. {
  4256. DoSave(true, true, true, RecryptPasswordErrors);
  4257. }
  4258. //---------------------------------------------------------------------
  4259. void __fastcall TStoredSessionList::Saved()
  4260. {
  4261. FDefaultSettings->Modified = false;
  4262. for (int Index = 0; Index < CountIncludingHidden; Index++)
  4263. {
  4264. ((TSessionData *)Items[Index])->Modified = false;
  4265. }
  4266. }
  4267. //---------------------------------------------------------------------
  4268. void __fastcall TStoredSessionList::ImportLevelFromFilezilla(
  4269. _di_IXMLNode Node, const UnicodeString & Path, _di_IXMLNode SettingsNode)
  4270. {
  4271. for (int Index = 0; Index < Node->ChildNodes->Count; Index++)
  4272. {
  4273. _di_IXMLNode ChildNode = Node->ChildNodes->Get(Index);
  4274. if (ChildNode->NodeName == L"Server")
  4275. {
  4276. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  4277. SessionData->CopyData(DefaultSettings);
  4278. SessionData->ImportFromFilezilla(ChildNode, Path, SettingsNode);
  4279. Add(SessionData.release());
  4280. }
  4281. else if (ChildNode->NodeName == L"Folder")
  4282. {
  4283. UnicodeString Name;
  4284. for (int Index = 0; Index < ChildNode->ChildNodes->Count; Index++)
  4285. {
  4286. _di_IXMLNode PossibleTextMode = ChildNode->ChildNodes->Get(Index);
  4287. if (PossibleTextMode->NodeType == ntText)
  4288. {
  4289. UnicodeString NodeValue = PossibleTextMode->NodeValue;
  4290. AddToList(Name, NodeValue.Trim(), L" ");
  4291. }
  4292. }
  4293. Name = TSessionData::MakeValidName(Name).Trim();
  4294. ImportLevelFromFilezilla(ChildNode, TSessionData::ComposePath(Path, Name), SettingsNode);
  4295. }
  4296. }
  4297. }
  4298. //---------------------------------------------------------------------
  4299. void __fastcall TStoredSessionList::ImportFromFilezilla(
  4300. const UnicodeString FileName, const UnicodeString ConfigurationFileName)
  4301. {
  4302. // not sure if the document must exists if we want to use its node
  4303. _di_IXMLDocument ConfigurationDocument;
  4304. _di_IXMLNode SettingsNode;
  4305. if (FileExists(ApiPath(ConfigurationFileName)))
  4306. {
  4307. ConfigurationDocument = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  4308. ConfigurationDocument->LoadFromFile(ConfigurationFileName);
  4309. _di_IXMLNode FileZilla3Node = ConfigurationDocument->ChildNodes->FindNode(L"FileZilla3");
  4310. if (FileZilla3Node != NULL)
  4311. {
  4312. SettingsNode = FileZilla3Node->ChildNodes->FindNode(L"Settings");
  4313. }
  4314. }
  4315. const _di_IXMLDocument Document = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  4316. Document->LoadFromFile(FileName);
  4317. _di_IXMLNode FileZilla3Node = Document->ChildNodes->FindNode(L"FileZilla3");
  4318. if (FileZilla3Node != NULL)
  4319. {
  4320. _di_IXMLNode ServersNode = FileZilla3Node->ChildNodes->FindNode(L"Servers");
  4321. if (ServersNode != NULL)
  4322. {
  4323. ImportLevelFromFilezilla(ServersNode, L"", SettingsNode);
  4324. }
  4325. }
  4326. }
  4327. //---------------------------------------------------------------------
  4328. void __fastcall TStoredSessionList::ImportFromKnownHosts(TStrings * Lines)
  4329. {
  4330. bool SessionList = false;
  4331. std::unique_ptr<THierarchicalStorage> HostKeyStorage(Configuration->CreateScpStorage(SessionList));
  4332. std::unique_ptr<TStrings> KeyList(new TStringList());
  4333. if (OpenHostKeysSubKey(HostKeyStorage.get(), false))
  4334. {
  4335. HostKeyStorage->GetValueNames(KeyList.get());
  4336. }
  4337. HostKeyStorage.reset(NULL);
  4338. UnicodeString FirstError;
  4339. for (int Index = 0; Index < Lines->Count; Index++)
  4340. {
  4341. try
  4342. {
  4343. UnicodeString Line = Lines->Strings[Index];
  4344. Line = Trim(Line);
  4345. if (!Line.IsEmpty() && (Line[1] != L';'))
  4346. {
  4347. int P = Pos(L' ', Line);
  4348. if (P > 0)
  4349. {
  4350. UnicodeString HostNameStr = Line.SubString(1, P - 1);
  4351. Line = Line.SubString(P + 1, Line.Length() - P);
  4352. P = Pos(L',', HostNameStr);
  4353. if (P > 0)
  4354. {
  4355. HostNameStr.SetLength(P - 1);
  4356. }
  4357. P = Pos(L':', HostNameStr);
  4358. int PortNumber = -1;
  4359. if (P > 0)
  4360. {
  4361. UnicodeString PortNumberStr = HostNameStr.SubString(P + 1, HostNameStr.Length() - P);
  4362. PortNumber = StrToInt(PortNumberStr);
  4363. HostNameStr.SetLength(P - 1);
  4364. }
  4365. if (HasIP6LiteralBrackets(HostNameStr))
  4366. {
  4367. HostNameStr = StripIP6LiteralBrackets(HostNameStr);
  4368. }
  4369. UnicodeString NameStr = HostNameStr;
  4370. if (PortNumber >= 0)
  4371. {
  4372. NameStr = FORMAT(L"%s:%d", (NameStr, PortNumber));
  4373. }
  4374. std::unique_ptr<TSessionData> SessionDataOwner;
  4375. TSessionData * SessionData = dynamic_cast<TSessionData *>(FindByName(NameStr));
  4376. if (SessionData == NULL)
  4377. {
  4378. SessionData = new TSessionData(L"");
  4379. SessionDataOwner.reset(SessionData);
  4380. SessionData->CopyData(DefaultSettings);
  4381. SessionData->Name = NameStr;
  4382. SessionData->HostName = HostNameStr;
  4383. if (PortNumber >= 0)
  4384. {
  4385. SessionData->PortNumber = PortNumber;
  4386. }
  4387. }
  4388. const struct ssh_keyalg * Algorithm;
  4389. UnicodeString Key = ParseOpenSshPubLine(Line, Algorithm);
  4390. UnicodeString KeyKey =
  4391. FORMAT(L"%s@%d:%s", (Algorithm->cache_id, SessionData->PortNumber, HostNameStr));
  4392. UnicodeString HostKey =
  4393. FORMAT(L"%s:%s=%s", (Algorithm->ssh_id, KeyKey, Key));
  4394. UnicodeString HostKeyList = SessionData->HostKey;
  4395. AddToList(HostKeyList, HostKey, L";");
  4396. SessionData->HostKey = HostKeyList;
  4397. // If there's at least one unknown key type for this host, select it
  4398. if (KeyList->IndexOf(KeyKey) < 0)
  4399. {
  4400. SessionData->Selected = true;
  4401. }
  4402. if (SessionDataOwner.get() != NULL)
  4403. {
  4404. Add(SessionDataOwner.release());
  4405. }
  4406. }
  4407. }
  4408. }
  4409. catch (Exception & E)
  4410. {
  4411. if (FirstError.IsEmpty())
  4412. {
  4413. FirstError = E.Message;
  4414. }
  4415. }
  4416. }
  4417. if (Count == 0)
  4418. {
  4419. UnicodeString Message = LoadStr(KNOWN_HOSTS_NO_SITES);
  4420. if (!FirstError.IsEmpty())
  4421. {
  4422. Message = FORMAT(L"%s\n(%s)", (Message, FirstError));
  4423. }
  4424. throw Exception(Message);
  4425. }
  4426. }
  4427. //---------------------------------------------------------------------
  4428. void __fastcall TStoredSessionList::Export(const UnicodeString FileName)
  4429. {
  4430. THierarchicalStorage * Storage = TIniFileStorage::CreateFromPath(FileName);
  4431. try
  4432. {
  4433. Storage->AccessMode = smReadWrite;
  4434. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  4435. {
  4436. Save(Storage, true);
  4437. }
  4438. }
  4439. __finally
  4440. {
  4441. delete Storage;
  4442. }
  4443. }
  4444. //---------------------------------------------------------------------
  4445. void __fastcall TStoredSessionList::SelectAll(bool Select)
  4446. {
  4447. for (int Index = 0; Index < Count; Index++)
  4448. Sessions[Index]->Selected = Select;
  4449. }
  4450. //---------------------------------------------------------------------
  4451. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  4452. bool OnlySelected, TList * Imported)
  4453. {
  4454. for (int Index = 0; Index < From->Count; Index++)
  4455. {
  4456. if (!OnlySelected || From->Sessions[Index]->Selected)
  4457. {
  4458. TSessionData *Session = new TSessionData(L"");
  4459. Session->Assign(From->Sessions[Index]);
  4460. Session->Modified = true;
  4461. Session->MakeUniqueIn(this);
  4462. Add(Session);
  4463. if (Imported != NULL)
  4464. {
  4465. Imported->Add(Session);
  4466. }
  4467. }
  4468. }
  4469. // only modified, explicit
  4470. Save(false, true);
  4471. }
  4472. //---------------------------------------------------------------------
  4473. void __fastcall TStoredSessionList::SelectSessionsToImport
  4474. (TStoredSessionList * Dest, bool SSHOnly)
  4475. {
  4476. for (int Index = 0; Index < Count; Index++)
  4477. {
  4478. Sessions[Index]->Selected =
  4479. (!SSHOnly || (Sessions[Index]->GetNormalizedPuttyProtocol() == PuttySshProtocol)) &&
  4480. !Dest->FindByName(Sessions[Index]->Name);
  4481. }
  4482. }
  4483. //---------------------------------------------------------------------
  4484. void __fastcall TStoredSessionList::Cleanup()
  4485. {
  4486. try
  4487. {
  4488. if (Configuration->Storage == stRegistry) Clear();
  4489. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  4490. try
  4491. {
  4492. Storage->AccessMode = smReadWrite;
  4493. if (Storage->OpenRootKey(False))
  4494. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  4495. }
  4496. __finally
  4497. {
  4498. delete Storage;
  4499. }
  4500. }
  4501. catch (Exception &E)
  4502. {
  4503. throw ExtException(&E, LoadStr(CLEANUP_SESSIONS_ERROR));
  4504. }
  4505. }
  4506. //---------------------------------------------------------------------------
  4507. void __fastcall TStoredSessionList::UpdateStaticUsage()
  4508. {
  4509. int SCP = 0;
  4510. int SFTP = 0;
  4511. int FTP = 0;
  4512. int FTPS = 0;
  4513. int WebDAV = 0;
  4514. int WebDAVS = 0;
  4515. int S3 = 0;
  4516. int Password = 0;
  4517. int Advanced = 0;
  4518. int Color = 0;
  4519. int Note = 0;
  4520. int Tunnel = 0;
  4521. bool Folders = false;
  4522. bool Workspaces = false;
  4523. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  4524. std::unique_ptr<TStringList> DifferentAdvancedProperties(CreateSortedStringList());
  4525. for (int Index = 0; Index < Count; Index++)
  4526. {
  4527. TSessionData * Data = Sessions[Index];
  4528. if (Data->IsWorkspace)
  4529. {
  4530. Workspaces = true;
  4531. }
  4532. else
  4533. {
  4534. switch (Data->FSProtocol)
  4535. {
  4536. case fsSCPonly:
  4537. SCP++;
  4538. break;
  4539. case fsSFTP:
  4540. case fsSFTPonly:
  4541. SFTP++;
  4542. break;
  4543. case fsFTP:
  4544. if (Data->Ftps == ftpsNone)
  4545. {
  4546. FTP++;
  4547. }
  4548. else
  4549. {
  4550. FTPS++;
  4551. }
  4552. break;
  4553. case fsWebDAV:
  4554. if (Data->Ftps == ftpsNone)
  4555. {
  4556. WebDAV++;
  4557. }
  4558. else
  4559. {
  4560. WebDAVS++;
  4561. }
  4562. break;
  4563. case fsS3:
  4564. S3++;
  4565. break;
  4566. }
  4567. if (Data->HasAnySessionPassword())
  4568. {
  4569. Password++;
  4570. }
  4571. if (Data->Color != 0)
  4572. {
  4573. Color++;
  4574. }
  4575. if (!Data->Note.IsEmpty())
  4576. {
  4577. Note++;
  4578. }
  4579. // This would not work for passwords, as they are compared in their encrypted form.
  4580. // But there are no passwords set in factory defaults anyway.
  4581. if (!Data->IsSame(FactoryDefaults.get(), true, DifferentAdvancedProperties.get(), false))
  4582. {
  4583. Advanced++;
  4584. }
  4585. if (Data->Tunnel)
  4586. {
  4587. Tunnel++;
  4588. }
  4589. if (!Data->FolderName.IsEmpty())
  4590. {
  4591. Folders = true;
  4592. }
  4593. }
  4594. }
  4595. Configuration->Usage->Set(L"StoredSessionsCountSCP", SCP);
  4596. Configuration->Usage->Set(L"StoredSessionsCountSFTP", SFTP);
  4597. Configuration->Usage->Set(L"StoredSessionsCountFTP", FTP);
  4598. Configuration->Usage->Set(L"StoredSessionsCountFTPS", FTPS);
  4599. Configuration->Usage->Set(L"StoredSessionsCountWebDAV", WebDAV);
  4600. Configuration->Usage->Set(L"StoredSessionsCountWebDAVS", WebDAVS);
  4601. Configuration->Usage->Set(L"StoredSessionsCountS3", S3);
  4602. Configuration->Usage->Set(L"StoredSessionsCountPassword", Password);
  4603. Configuration->Usage->Set(L"StoredSessionsCountColor", Color);
  4604. Configuration->Usage->Set(L"StoredSessionsCountNote", Note);
  4605. Configuration->Usage->Set(L"StoredSessionsCountAdvanced", Advanced);
  4606. DifferentAdvancedProperties->Delimiter = L',';
  4607. Configuration->Usage->Set(L"StoredSessionsAdvancedSettings", DifferentAdvancedProperties->DelimitedText);
  4608. Configuration->Usage->Set(L"StoredSessionsCountTunnel", Tunnel);
  4609. // actually default might be true, see below for when the default is actually used
  4610. bool CustomDefaultStoredSession = false;
  4611. try
  4612. {
  4613. // this can throw, when the default session settings have password set
  4614. // (and no other basic property, like hostname/username),
  4615. // and master password is enabled as we are called before master password
  4616. // handler is set
  4617. CustomDefaultStoredSession = !FDefaultSettings->IsSame(FactoryDefaults.get(), false);
  4618. }
  4619. catch (...)
  4620. {
  4621. }
  4622. Configuration->Usage->Set(L"UsingDefaultStoredSession", CustomDefaultStoredSession);
  4623. Configuration->Usage->Set(L"UsingStoredSessionsFolders", Folders);
  4624. Configuration->Usage->Set(L"UsingWorkspaces", Workspaces);
  4625. }
  4626. //---------------------------------------------------------------------------
  4627. TSessionData * __fastcall TStoredSessionList::FindSame(TSessionData * Data)
  4628. {
  4629. TSessionData * Result;
  4630. if (Data->Hidden || Data->Name.IsEmpty() || Data->IsWorkspace)
  4631. {
  4632. Result = NULL;
  4633. }
  4634. else
  4635. {
  4636. Result = dynamic_cast<TSessionData *>(FindByName(Data->Name));
  4637. }
  4638. return Result;
  4639. }
  4640. //---------------------------------------------------------------------------
  4641. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  4642. {
  4643. for (int Index = 0; Index < Count; Index++)
  4644. if (Data == Sessions[Index]) return Index;
  4645. return -1;
  4646. }
  4647. //---------------------------------------------------------------------------
  4648. TSessionData * __fastcall TStoredSessionList::NewSession(
  4649. UnicodeString SessionName, TSessionData * Session)
  4650. {
  4651. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  4652. if (!DuplicateSession)
  4653. {
  4654. DuplicateSession = new TSessionData(L"");
  4655. DuplicateSession->Assign(Session);
  4656. DuplicateSession->Name = SessionName;
  4657. // make sure, that new stored session is saved to registry
  4658. DuplicateSession->Modified = true;
  4659. Add(DuplicateSession);
  4660. }
  4661. else
  4662. {
  4663. DuplicateSession->Assign(Session);
  4664. DuplicateSession->Name = SessionName;
  4665. DuplicateSession->Modified = true;
  4666. }
  4667. // list was saved here before to default storage, but it would not allow
  4668. // to work with special lists (export/import) not using default storage
  4669. return DuplicateSession;
  4670. }
  4671. //---------------------------------------------------------------------------
  4672. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  4673. {
  4674. DebugAssert(FDefaultSettings);
  4675. if (FDefaultSettings != value)
  4676. {
  4677. FDefaultSettings->Assign(value);
  4678. // make sure default settings are saved
  4679. FDefaultSettings->Modified = true;
  4680. FDefaultSettings->Name = DefaultName;
  4681. if (!FReadOnly)
  4682. {
  4683. // only modified, explicit
  4684. Save(false, true);
  4685. }
  4686. }
  4687. }
  4688. //---------------------------------------------------------------------------
  4689. bool __fastcall TStoredSessionList::OpenHostKeysSubKey(THierarchicalStorage * Storage, bool CanCreate)
  4690. {
  4691. return
  4692. Storage->OpenRootKey(CanCreate) &&
  4693. Storage->OpenSubKey(Configuration->SshHostKeysSubKey, CanCreate);
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. THierarchicalStorage * __fastcall TStoredSessionList::CreateHostKeysStorageForWritting()
  4697. {
  4698. bool SessionList = false;
  4699. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateScpStorage(SessionList));
  4700. Storage->Explicit = true;
  4701. Storage->AccessMode = smReadWrite;
  4702. return Storage.release();
  4703. }
  4704. //---------------------------------------------------------------------------
  4705. void __fastcall TStoredSessionList::ImportHostKeys(
  4706. THierarchicalStorage * SourceStorage, THierarchicalStorage * TargetStorage, TStoredSessionList * Sessions, bool OnlySelected)
  4707. {
  4708. if (OpenHostKeysSubKey(SourceStorage, false) &&
  4709. OpenHostKeysSubKey(TargetStorage, true))
  4710. {
  4711. std::auto_ptr<TStringList> KeyList(new TStringList());
  4712. SourceStorage->GetValueNames(KeyList.get());
  4713. DebugAssert(Sessions != NULL);
  4714. for (int Index = 0; Index < Sessions->Count; Index++)
  4715. {
  4716. TSessionData * Session = Sessions->Sessions[Index];
  4717. if (!OnlySelected || Session->Selected)
  4718. {
  4719. UnicodeString HostKeyName = PuttyMungeStr(FORMAT(L"@%d:%s", (Session->PortNumber, Session->HostNameExpanded)));
  4720. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  4721. {
  4722. UnicodeString KeyName = KeyList->Strings[KeyIndex];
  4723. if (EndsText(HostKeyName, KeyName))
  4724. {
  4725. TargetStorage->WriteStringRaw(KeyName, SourceStorage->ReadStringRaw(KeyName, L""));
  4726. }
  4727. }
  4728. }
  4729. }
  4730. }
  4731. }
  4732. //---------------------------------------------------------------------------
  4733. void __fastcall TStoredSessionList::ImportHostKeys(
  4734. const UnicodeString & SourceKey, TStoredSessionList * Sessions, bool OnlySelected)
  4735. {
  4736. std::auto_ptr<THierarchicalStorage> TargetStorage(CreateHostKeysStorageForWritting());
  4737. std::auto_ptr<THierarchicalStorage> SourceStorage(new TRegistryStorage(SourceKey));
  4738. ImportHostKeys(SourceStorage.get(), TargetStorage.get(), Sessions, OnlySelected);
  4739. }
  4740. //---------------------------------------------------------------------------
  4741. void __fastcall TStoredSessionList::ImportSelectedKnownHosts(TStoredSessionList * Sessions)
  4742. {
  4743. std::unique_ptr<THierarchicalStorage> Storage(CreateHostKeysStorageForWritting());
  4744. if (OpenHostKeysSubKey(Storage.get(), true))
  4745. {
  4746. for (int Index = 0; Index < Sessions->Count; Index++)
  4747. {
  4748. TSessionData * Session = Sessions->Sessions[Index];
  4749. if (Session->Selected)
  4750. {
  4751. UnicodeString Algs;
  4752. UnicodeString HostKeys = Session->HostKey;
  4753. while (!HostKeys.IsEmpty())
  4754. {
  4755. UnicodeString HostKey = CutToChar(HostKeys, L';', true);
  4756. // skip alg
  4757. CutToChar(HostKey, L':', true);
  4758. UnicodeString Key = CutToChar(HostKey, L'=', true);
  4759. Storage->WriteStringRaw(Key, HostKey);
  4760. }
  4761. }
  4762. }
  4763. }
  4764. }
  4765. //---------------------------------------------------------------------------
  4766. bool __fastcall TStoredSessionList::IsFolderOrWorkspace(
  4767. const UnicodeString & Name, bool Workspace)
  4768. {
  4769. bool Result = false;
  4770. TSessionData * FirstData = NULL;
  4771. if (!Name.IsEmpty())
  4772. {
  4773. for (int Index = 0; !Result && (Index < Count); Index++)
  4774. {
  4775. Result = Sessions[Index]->IsInFolderOrWorkspace(Name);
  4776. if (Result)
  4777. {
  4778. FirstData = Sessions[Index];
  4779. }
  4780. }
  4781. }
  4782. return
  4783. Result &&
  4784. DebugAlwaysTrue(FirstData != NULL) &&
  4785. (FirstData->IsWorkspace == Workspace);
  4786. }
  4787. //---------------------------------------------------------------------------
  4788. bool __fastcall TStoredSessionList::IsFolder(const UnicodeString & Name)
  4789. {
  4790. return IsFolderOrWorkspace(Name, false);
  4791. }
  4792. //---------------------------------------------------------------------------
  4793. bool __fastcall TStoredSessionList::IsWorkspace(const UnicodeString & Name)
  4794. {
  4795. return IsFolderOrWorkspace(Name, true);
  4796. }
  4797. //---------------------------------------------------------------------------
  4798. TSessionData * __fastcall TStoredSessionList::CheckIsInFolderOrWorkspaceAndResolve(
  4799. TSessionData * Data, const UnicodeString & Name)
  4800. {
  4801. if (Data->IsInFolderOrWorkspace(Name))
  4802. {
  4803. Data = ResolveWorkspaceData(Data);
  4804. if ((Data != NULL) && Data->CanLogin &&
  4805. DebugAlwaysTrue(Data->Link.IsEmpty()))
  4806. {
  4807. return Data;
  4808. }
  4809. }
  4810. return NULL;
  4811. }
  4812. //---------------------------------------------------------------------------
  4813. void __fastcall TStoredSessionList::GetFolderOrWorkspace(const UnicodeString & Name, TList * List)
  4814. {
  4815. DoGetFolderOrWorkspace(Name, List, false);
  4816. }
  4817. //---------------------------------------------------------------------------
  4818. void __fastcall TStoredSessionList::DoGetFolderOrWorkspace(const UnicodeString & Name, TList * List, bool NoRecrypt)
  4819. {
  4820. for (int Index = 0; (Index < Count); Index++)
  4821. {
  4822. TSessionData * RawData = Sessions[Index];
  4823. TSessionData * Data =
  4824. CheckIsInFolderOrWorkspaceAndResolve(RawData, Name);
  4825. if (Data != NULL)
  4826. {
  4827. TSessionData * Data2 = new TSessionData(L"");
  4828. if (NoRecrypt)
  4829. {
  4830. Data2->CopyDataNoRecrypt(Data);
  4831. }
  4832. else
  4833. {
  4834. Data2->Assign(Data);
  4835. }
  4836. if (!RawData->Link.IsEmpty() && (DebugAlwaysTrue(Data != RawData)) &&
  4837. // BACKWARD COMPATIBILITY
  4838. // When loading pre-5.6.4 workspace, that does not have state saved,
  4839. // do not overwrite the site "state" defaults
  4840. // with (empty) workspace state
  4841. RawData->HasStateData())
  4842. {
  4843. Data2->CopyStateData(RawData);
  4844. }
  4845. if (!RawData->NameOverride.IsEmpty())
  4846. {
  4847. Data2->Name = RawData->NameOverride;
  4848. }
  4849. else if (RawData->Link.IsEmpty() && RawData->IsWorkspace)
  4850. {
  4851. // Newly opened ad-hoc session has no name, so restore the workspace that way too.
  4852. // Otherwise we would persist the generated internal workspace name as a real name.
  4853. Data2->Name = UnicodeString();
  4854. }
  4855. List->Add(Data2);
  4856. }
  4857. }
  4858. }
  4859. //---------------------------------------------------------------------------
  4860. TStrings * __fastcall TStoredSessionList::GetFolderOrWorkspaceList(
  4861. const UnicodeString & Name)
  4862. {
  4863. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4864. DoGetFolderOrWorkspace(Name, DataList.get(), true);
  4865. std::unique_ptr<TStringList> Result(new TStringList());
  4866. for (int Index = 0; (Index < DataList->Count); Index++)
  4867. {
  4868. Result->Add(dynamic_cast<TSessionData *>(DataList->Items[Index])->SessionName);
  4869. }
  4870. return Result.release();
  4871. }
  4872. //---------------------------------------------------------------------------
  4873. TStrings * __fastcall TStoredSessionList::GetWorkspaces()
  4874. {
  4875. std::unique_ptr<TStringList> Result(CreateSortedStringList());
  4876. for (int Index = 0; (Index < Count); Index++)
  4877. {
  4878. TSessionData * Data = Sessions[Index];
  4879. if (Data->IsWorkspace)
  4880. {
  4881. Result->Add(Data->FolderName);
  4882. }
  4883. }
  4884. return Result.release();
  4885. }
  4886. //---------------------------------------------------------------------------
  4887. void __fastcall TStoredSessionList::NewWorkspace(
  4888. UnicodeString Name, TList * DataList)
  4889. {
  4890. for (int Index = 0; (Index < Count); Index++)
  4891. {
  4892. TSessionData * Data = Sessions[Index];
  4893. if (Data->IsInFolderOrWorkspace(Name))
  4894. {
  4895. FPendingRemovals->Add(Data->Name);
  4896. Remove(Data);
  4897. Index--;
  4898. }
  4899. }
  4900. for (int Index = 0; (Index < DataList->Count); Index++)
  4901. {
  4902. TSessionData * Data = static_cast<TSessionData *>(DataList->Items[Index]);
  4903. TSessionData * Data2 = new TSessionData(L"");
  4904. Data2->Assign(Data);
  4905. Data2->Name = TSessionData::ComposePath(Name, Data->Name);
  4906. // make sure, that new stored session is saved to registry
  4907. Data2->Modified = true;
  4908. Add(Data2);
  4909. }
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. bool __fastcall TStoredSessionList::HasAnyWorkspace()
  4913. {
  4914. bool Result = false;
  4915. for (int Index = 0; !Result && (Index < Count); Index++)
  4916. {
  4917. TSessionData * Data = Sessions[Index];
  4918. Result = Data->IsWorkspace;
  4919. }
  4920. return Result;
  4921. }
  4922. //---------------------------------------------------------------------------
  4923. TSessionData * __fastcall TStoredSessionList::ParseUrl(UnicodeString Url,
  4924. TOptions * Options, bool & DefaultsOnly, UnicodeString * FileName,
  4925. bool * AProtocolDefined, UnicodeString * MaskedUrl, int Flags)
  4926. {
  4927. TSessionData * Data = new TSessionData(L"");
  4928. try
  4929. {
  4930. Data->ParseUrl(Url, Options, this, DefaultsOnly, FileName, AProtocolDefined, MaskedUrl, Flags);
  4931. }
  4932. catch(...)
  4933. {
  4934. delete Data;
  4935. throw;
  4936. }
  4937. return Data;
  4938. }
  4939. //---------------------------------------------------------------------
  4940. bool __fastcall TStoredSessionList::IsUrl(UnicodeString Url)
  4941. {
  4942. bool DefaultsOnly;
  4943. bool ProtocolDefined = false;
  4944. std::unique_ptr<TSessionData> ParsedData(ParseUrl(Url, NULL, DefaultsOnly, NULL, &ProtocolDefined));
  4945. bool Result = ProtocolDefined;
  4946. return Result;
  4947. }
  4948. //---------------------------------------------------------------------
  4949. TSessionData * __fastcall TStoredSessionList::ResolveWorkspaceData(TSessionData * Data)
  4950. {
  4951. if (!Data->Link.IsEmpty())
  4952. {
  4953. Data = dynamic_cast<TSessionData *>(FindByName(Data->Link));
  4954. if (Data != NULL)
  4955. {
  4956. Data = ResolveWorkspaceData(Data);
  4957. }
  4958. }
  4959. return Data;
  4960. }
  4961. //---------------------------------------------------------------------
  4962. TSessionData * __fastcall TStoredSessionList::SaveWorkspaceData(TSessionData * Data, int Index)
  4963. {
  4964. std::unique_ptr<TSessionData> Result(new TSessionData(L""));
  4965. TSessionData * SameData = StoredSessions->FindSame(Data);
  4966. if (SameData != NULL)
  4967. {
  4968. Result->CopyStateData(Data);
  4969. Result->Link = Data->Name;
  4970. }
  4971. else
  4972. {
  4973. Result->Assign(Data);
  4974. Result->NameOverride = Data->Name;
  4975. }
  4976. Result->IsWorkspace = true;
  4977. Result->Name = IntToHex(Index, 4); // See HasSessionName()
  4978. return Result.release();
  4979. }
  4980. //---------------------------------------------------------------------
  4981. bool __fastcall TStoredSessionList::CanLogin(TSessionData * Data)
  4982. {
  4983. Data = ResolveWorkspaceData(Data);
  4984. return (Data != NULL) && Data->CanLogin;
  4985. }
  4986. //---------------------------------------------------------------------
  4987. UnicodeString GetExpandedLogFileName(UnicodeString LogFileName, TDateTime Started, TSessionData * SessionData)
  4988. {
  4989. // StripPathQuotes should not be needed as we do not feed quotes anymore
  4990. UnicodeString ANewFileName = StripPathQuotes(ExpandEnvironmentVariables(LogFileName));
  4991. for (int Index = 1; Index < ANewFileName.Length(); Index++)
  4992. {
  4993. if (ANewFileName[Index] == L'!')
  4994. {
  4995. UnicodeString Replacement;
  4996. // keep consistent with TFileCustomCommand::PatternReplacement
  4997. switch (tolower(ANewFileName[Index + 1]))
  4998. {
  4999. case L'y':
  5000. Replacement = FormatDateTime(L"yyyy", Started);
  5001. break;
  5002. case L'm':
  5003. Replacement = FormatDateTime(L"mm", Started);
  5004. break;
  5005. case L'd':
  5006. Replacement = FormatDateTime(L"dd", Started);
  5007. break;
  5008. case L't':
  5009. Replacement = FormatDateTime(L"hhnnss", Started);
  5010. break;
  5011. case 'p':
  5012. Replacement = IntToStr(static_cast<int>(GetCurrentProcessId()));
  5013. break;
  5014. case L'@':
  5015. if (SessionData != NULL)
  5016. {
  5017. Replacement = MakeValidFileName(SessionData->HostNameExpanded);
  5018. }
  5019. else
  5020. {
  5021. Replacement = L"nohost";
  5022. }
  5023. break;
  5024. case L's':
  5025. if (SessionData != NULL)
  5026. {
  5027. Replacement = MakeValidFileName(SessionData->SessionName);
  5028. }
  5029. else
  5030. {
  5031. Replacement = L"nosession";
  5032. }
  5033. break;
  5034. case L'!':
  5035. Replacement = L"!";
  5036. break;
  5037. default:
  5038. Replacement = UnicodeString(L"!") + ANewFileName[Index + 1];
  5039. break;
  5040. }
  5041. ANewFileName.Delete(Index, 2);
  5042. ANewFileName.Insert(Replacement, Index);
  5043. Index += Replacement.Length() - 1;
  5044. }
  5045. }
  5046. return ANewFileName;
  5047. }
  5048. //---------------------------------------------------------------------
  5049. bool __fastcall IsSshProtocol(TFSProtocol FSProtocol)
  5050. {
  5051. return
  5052. (FSProtocol == fsSFTPonly) || (FSProtocol == fsSFTP) ||
  5053. (FSProtocol == fsSCPonly);
  5054. }
  5055. //---------------------------------------------------------------------------
  5056. int __fastcall DefaultPort(TFSProtocol FSProtocol, TFtps Ftps)
  5057. {
  5058. int Result;
  5059. switch (FSProtocol)
  5060. {
  5061. case fsFTP:
  5062. if (Ftps == ftpsImplicit)
  5063. {
  5064. Result = FtpsImplicitPortNumber;
  5065. }
  5066. else
  5067. {
  5068. Result = FtpPortNumber;
  5069. }
  5070. break;
  5071. case fsWebDAV:
  5072. case fsS3:
  5073. if (Ftps == ftpsNone)
  5074. {
  5075. Result = HTTPPortNumber;
  5076. }
  5077. else
  5078. {
  5079. Result = HTTPSPortNumber;
  5080. }
  5081. break;
  5082. default:
  5083. if (IsSshProtocol(FSProtocol))
  5084. {
  5085. Result = SshPortNumber;
  5086. }
  5087. else
  5088. {
  5089. DebugFail();
  5090. Result = -1;
  5091. }
  5092. break;
  5093. }
  5094. return Result;
  5095. }
  5096. //---------------------------------------------------------------------