SessionData.cpp 139 KB

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