SessionData.cpp 146 KB

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