SessionData.cpp 97 KB

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