1
0

SessionData.cpp 176 KB

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