SessionData.cpp 145 KB

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