SessionData.cpp 146 KB

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