SessionData.cpp 148 KB

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