SessionData.cpp 116 KB

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