SessionData.cpp 139 KB

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