SessionData.cpp 116 KB

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