SessionData.cpp 139 KB

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