SessionData.cpp 141 KB

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