SessionData.cpp 148 KB

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