SessionData.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402
  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. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. enum TProxyType { pxNone, pxHTTP, pxSocks, pxTelnet }; // 0.53b and older
  16. const char * DefaultName = "Default Settings";
  17. const char CipherNames[CIPHER_COUNT][10] = {"WARN", "3des", "blowfish", "aes", "des", "arcfour"};
  18. const char KexNames[KEX_COUNT][20] = {"WARN", "dh-group1-sha1", "dh-group14-sha1", "dh-gex-sha1", "rsa" };
  19. const char ProtocolNames[PROTOCOL_COUNT][10] = { "raw", "telnet", "rlogin", "ssh" };
  20. const char SshProtList[][10] = {"1 only", "1", "2", "2 only"};
  21. const char ProxyMethodList[][10] = {"none", "SOCKS4", "SOCKS5", "HTTP", "Telnet", "Cmd" };
  22. const TCipher DefaultCipherList[CIPHER_COUNT] =
  23. { cipAES, cipBlowfish, cip3DES, cipWarn, cipArcfour, cipDES };
  24. const TKex DefaultKexList[KEX_COUNT] =
  25. { kexDHGEx, kexDHGroup14, kexDHGroup1, kexRSA, kexWarn };
  26. const char FSProtocolNames[FSPROTOCOL_COUNT][11] = { "SCP", "SFTP (SCP)", "SFTP", "", "", "FTP" };
  27. const int SshPortNumber = 22;
  28. const int FtpPortNumber = 21;
  29. const int FtpsImplicitPortNumber = 990;
  30. //---------------------------------------------------------------------
  31. TDateTime __fastcall SecToDateTime(int Sec)
  32. {
  33. return TDateTime((unsigned short)(Sec/60/60),
  34. (unsigned short)(Sec/60%60), (unsigned short)(Sec%60), 0);
  35. }
  36. //--- TSessionData ----------------------------------------------------
  37. __fastcall TSessionData::TSessionData(AnsiString aName):
  38. TNamedObject(aName)
  39. {
  40. Default();
  41. FModified = true;
  42. }
  43. //---------------------------------------------------------------------
  44. void __fastcall TSessionData::Default()
  45. {
  46. HostName = "";
  47. PortNumber = SshPortNumber;
  48. UserName = "";
  49. Password = "";
  50. Passwordless = false;
  51. PingInterval = 30;
  52. // when changing default, update load/save logic
  53. PingType = ptOff;
  54. Timeout = 15;
  55. TryAgent = true;
  56. AgentFwd = false;
  57. AuthTIS = false;
  58. AuthKI = true;
  59. AuthKIPassword = true;
  60. AuthGSSAPI = false;
  61. GSSAPIFwdTGT = false;
  62. GSSAPIServerRealm = "";
  63. ChangeUsername = false;
  64. Compression = false;
  65. SshProt = ssh2;
  66. Ssh2DES = false;
  67. SshNoUserAuth = false;
  68. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  69. {
  70. Cipher[Index] = DefaultCipherList[Index];
  71. }
  72. for (int Index = 0; Index < KEX_COUNT; Index++)
  73. {
  74. Kex[Index] = DefaultKexList[Index];
  75. }
  76. PublicKeyFile = "";
  77. FProtocol = ptSSH;
  78. TcpNoDelay = true;
  79. HostKey = "";
  80. ProxyMethod = pmNone;
  81. ProxyHost = "proxy";
  82. ProxyPort = 80;
  83. ProxyUsername = "";
  84. ProxyPassword = "";
  85. ProxyTelnetCommand = "connect %host %port\\n";
  86. ProxyLocalCommand = "";
  87. ProxyDNS = asAuto;
  88. ProxyLocalhost = false;
  89. for (int Index = 0; Index < LENOF(FBugs); Index++)
  90. {
  91. Bug[(TSshBug)Index] = asAuto;
  92. }
  93. Special = false;
  94. FSProtocol = fsSFTP;
  95. AddressFamily = afAuto;
  96. RekeyData = "1G";
  97. RekeyTime = 60;
  98. // FS common
  99. LocalDirectory = "";
  100. RemoteDirectory = "";
  101. UpdateDirectories = false;
  102. CacheDirectories = true;
  103. CacheDirectoryChanges = true;
  104. PreserveDirectoryChanges = true;
  105. LockInHome = false;
  106. ResolveSymlinks = true;
  107. DSTMode = dstmUnix;
  108. DeleteToRecycleBin = false;
  109. OverwrittenToRecycleBin = false;
  110. RecycleBinPath = "/tmp";
  111. Color = 0;
  112. PostLoginCommands = "";
  113. // SCP
  114. ReturnVar = "";
  115. LookupUserGroups = true;
  116. EOLType = eolLF;
  117. Shell = ""; //default shell
  118. ReturnVar = "";
  119. ClearAliases = true;
  120. UnsetNationalVars = true;
  121. ListingCommand = "ls -la";
  122. IgnoreLsWarnings = true;
  123. Scp1Compatibility = false;
  124. TimeDifference = 0;
  125. SCPLsFullTime = asAuto;
  126. NotUtf = asAuto;
  127. FtpListAll = asAuto;
  128. // SFTP
  129. SftpServer = "";
  130. SFTPDownloadQueue = 4;
  131. SFTPUploadQueue = 4;
  132. SFTPListingQueue = 2;
  133. SFTPMaxVersion = 5;
  134. SFTPMaxPacketSize = 0;
  135. for (int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  136. {
  137. SFTPBug[(TSftpBug)Index] = asAuto;
  138. }
  139. Tunnel = false;
  140. TunnelHostName = "";
  141. TunnelPortNumber = SshPortNumber;
  142. TunnelUserName = "";
  143. TunnelPassword = "";
  144. TunnelPublicKeyFile = "";
  145. TunnelLocalPortNumber = 0;
  146. TunnelPortFwd = "";
  147. // FTP
  148. FtpPasvMode = false;
  149. FtpForcePasvIp = false;
  150. FtpAccount = "";
  151. FtpPingInterval = 30;
  152. FtpPingType = ptDummyCommand;
  153. Ftps = ftpsNone;
  154. FtpProxyLogonType = 0; // none
  155. CustomParam1 = "";
  156. CustomParam2 = "";
  157. Selected = false;
  158. FModified = false;
  159. // add also to TSessionLog::AddStartupInfo()
  160. }
  161. //---------------------------------------------------------------------
  162. void __fastcall TSessionData::NonPersistant()
  163. {
  164. UpdateDirectories = false;
  165. PreserveDirectoryChanges = false;
  166. }
  167. //---------------------------------------------------------------------
  168. void __fastcall TSessionData::Assign(TPersistent * Source)
  169. {
  170. if (Source && Source->InheritsFrom(__classid(TSessionData)))
  171. {
  172. #define DUPL(P) P = ((TSessionData *)Source)->P
  173. DUPL(Name);
  174. DUPL(HostName);
  175. DUPL(PortNumber);
  176. DUPL(UserName);
  177. DUPL(Password);
  178. DUPL(Passwordless);
  179. DUPL(PingInterval);
  180. DUPL(PingType);
  181. DUPL(Timeout);
  182. DUPL(TryAgent);
  183. DUPL(AgentFwd);
  184. DUPL(AuthTIS);
  185. DUPL(ChangeUsername);
  186. DUPL(Compression);
  187. DUPL(SshProt);
  188. DUPL(Ssh2DES);
  189. DUPL(SshNoUserAuth);
  190. DUPL(CipherList);
  191. DUPL(KexList);
  192. DUPL(PublicKeyFile);
  193. DUPL(AddressFamily);
  194. DUPL(RekeyData);
  195. DUPL(RekeyTime);
  196. DUPL(HostKey);
  197. DUPL(FSProtocol);
  198. DUPL(LocalDirectory);
  199. DUPL(RemoteDirectory);
  200. DUPL(UpdateDirectories);
  201. DUPL(CacheDirectories);
  202. DUPL(CacheDirectoryChanges);
  203. DUPL(PreserveDirectoryChanges);
  204. DUPL(ResolveSymlinks);
  205. DUPL(DSTMode);
  206. DUPL(LockInHome);
  207. DUPL(Special);
  208. DUPL(Selected);
  209. DUPL(ReturnVar);
  210. DUPL(LookupUserGroups);
  211. DUPL(EOLType);
  212. DUPL(Shell);
  213. DUPL(ClearAliases);
  214. DUPL(Scp1Compatibility);
  215. DUPL(UnsetNationalVars);
  216. DUPL(ListingCommand);
  217. DUPL(IgnoreLsWarnings);
  218. DUPL(SCPLsFullTime);
  219. DUPL(FtpListAll);
  220. DUPL(TimeDifference);
  221. // new in 53b
  222. DUPL(TcpNoDelay);
  223. DUPL(AuthKI);
  224. DUPL(AuthKIPassword);
  225. DUPL(AuthGSSAPI);
  226. DUPL(GSSAPIFwdTGT);
  227. DUPL(GSSAPIServerRealm);
  228. DUPL(DeleteToRecycleBin);
  229. DUPL(OverwrittenToRecycleBin);
  230. DUPL(RecycleBinPath);
  231. DUPL(NotUtf);
  232. DUPL(PostLoginCommands);
  233. DUPL(ProxyMethod);
  234. DUPL(ProxyHost);
  235. DUPL(ProxyPort);
  236. DUPL(ProxyUsername);
  237. DUPL(ProxyPassword);
  238. DUPL(ProxyTelnetCommand);
  239. DUPL(ProxyLocalCommand);
  240. DUPL(ProxyDNS);
  241. DUPL(ProxyLocalhost);
  242. for (int Index = 0; Index < LENOF(FBugs); Index++)
  243. {
  244. DUPL(Bug[(TSshBug)Index]);
  245. }
  246. // SFTP
  247. DUPL(SftpServer);
  248. DUPL(SFTPDownloadQueue);
  249. DUPL(SFTPUploadQueue);
  250. DUPL(SFTPListingQueue);
  251. DUPL(SFTPMaxVersion);
  252. DUPL(SFTPMaxPacketSize);
  253. for (int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  254. {
  255. DUPL(SFTPBug[(TSftpBug)Index]);
  256. }
  257. DUPL(Color);
  258. DUPL(Tunnel);
  259. DUPL(TunnelHostName);
  260. DUPL(TunnelPortNumber);
  261. DUPL(TunnelUserName);
  262. DUPL(TunnelPassword);
  263. DUPL(TunnelPublicKeyFile);
  264. DUPL(TunnelLocalPortNumber);
  265. DUPL(TunnelPortFwd);
  266. DUPL(FtpPasvMode);
  267. DUPL(FtpForcePasvIp);
  268. DUPL(FtpAccount);
  269. DUPL(FtpPingInterval);
  270. DUPL(FtpPingType);
  271. DUPL(Ftps);
  272. DUPL(FtpProxyLogonType);
  273. DUPL(CustomParam1);
  274. DUPL(CustomParam2);
  275. #undef DUPL
  276. FModified = ((TSessionData *)Source)->Modified;
  277. }
  278. else
  279. {
  280. TNamedObject::Assign(Source);
  281. }
  282. }
  283. //---------------------------------------------------------------------
  284. void __fastcall TSessionData::Load(THierarchicalStorage * Storage)
  285. {
  286. bool RewritePassword = false;
  287. if (Storage->OpenSubKey(InternalStorageKey, False))
  288. {
  289. PortNumber = Storage->ReadInteger("PortNumber", PortNumber);
  290. UserName = Storage->ReadString("UserName", UserName);
  291. // must be loaded after UserName, because HostName may be in format user@host
  292. HostName = Storage->ReadString("HostName", HostName);
  293. if (!Configuration->DisablePasswordStoring)
  294. {
  295. if (Storage->ValueExists("PasswordPlain"))
  296. {
  297. Password = Storage->ReadString("PasswordPlain", Password);
  298. RewritePassword = true;
  299. }
  300. else
  301. {
  302. FPassword = Storage->ReadString("Password", FPassword);
  303. }
  304. }
  305. Passwordless = Storage->ReadBool("Passwordless", Passwordless);
  306. // Putty uses PingIntervalSecs
  307. int PingIntervalSecs = Storage->ReadInteger("PingIntervalSecs", -1);
  308. if (PingIntervalSecs < 0)
  309. {
  310. PingIntervalSecs = Storage->ReadInteger("PingIntervalSec", PingInterval%60);
  311. }
  312. PingInterval =
  313. Storage->ReadInteger("PingInterval", PingInterval/60)*60 +
  314. PingIntervalSecs;
  315. if (PingInterval == 0)
  316. {
  317. PingInterval = 30;
  318. }
  319. // PingType has not existed before 3.5, where PingInterval > 0 meant today's ptNullPacket
  320. // Since 3.5, until 4.1 PingType was stored unconditionally.
  321. // Since 4.1 PingType is stored when it is not ptOff (default) or
  322. // when PingInterval is stored.
  323. if (!Storage->ValueExists("PingType"))
  324. {
  325. if (Storage->ReadInteger("PingInterval", 0) > 0)
  326. {
  327. PingType = ptNullPacket;
  328. }
  329. }
  330. else
  331. {
  332. PingType = static_cast<TPingType>(Storage->ReadInteger("PingType", ptOff));
  333. }
  334. Timeout = Storage->ReadInteger("Timeout", Timeout);
  335. TryAgent = Storage->ReadBool("TryAgent", TryAgent);
  336. AgentFwd = Storage->ReadBool("AgentFwd", AgentFwd);
  337. AuthTIS = Storage->ReadBool("AuthTIS", AuthTIS);
  338. AuthKI = Storage->ReadBool("AuthKI", AuthKI);
  339. AuthKIPassword = Storage->ReadBool("AuthKIPassword", AuthKIPassword);
  340. // Continue to use setting keys of previous kerberos implementation (vaclav tomec),
  341. // but fallback to keys of other implementations (official putty and vintela quest putty),
  342. // to allow imports from all putty versions.
  343. // Both vaclav tomec and official putty use AuthGSSAPI
  344. AuthGSSAPI = Storage->ReadBool("AuthGSSAPI", Storage->ReadBool("AuthSSPI", AuthGSSAPI));
  345. GSSAPIFwdTGT = Storage->ReadBool("GSSAPIFwdTGT", Storage->ReadBool("GssapiFwd", Storage->ReadBool("SSPIFwdTGT", GSSAPIFwdTGT)));
  346. GSSAPIServerRealm = Storage->ReadString("GSSAPIServerRealm", Storage->ReadString("KerbPrincipal", GSSAPIServerRealm));
  347. ChangeUsername = Storage->ReadBool("ChangeUsername", ChangeUsername);
  348. Compression = Storage->ReadBool("Compression", Compression);
  349. SshProt = (TSshProt)Storage->ReadInteger("SshProt", SshProt);
  350. Ssh2DES = Storage->ReadBool("Ssh2DES", Ssh2DES);
  351. SshNoUserAuth = Storage->ReadBool("SshNoUserAuth", SshNoUserAuth);
  352. CipherList = Storage->ReadString("Cipher", CipherList);
  353. KexList = Storage->ReadString("KEX", KexList);
  354. PublicKeyFile = Storage->ReadString("PublicKeyFile", PublicKeyFile);
  355. AddressFamily = static_cast<TAddressFamily>
  356. (Storage->ReadInteger("AddressFamily", AddressFamily));
  357. RekeyData = Storage->ReadString("RekeyBytes", RekeyData);
  358. RekeyTime = Storage->ReadInteger("RekeyTime", RekeyTime);
  359. FSProtocol = (TFSProtocol)Storage->ReadInteger("FSProtocol", FSProtocol);
  360. LocalDirectory = Storage->ReadString("LocalDirectory", LocalDirectory);
  361. RemoteDirectory = Storage->ReadString("RemoteDirectory", RemoteDirectory);
  362. UpdateDirectories = Storage->ReadBool("UpdateDirectories", UpdateDirectories);
  363. CacheDirectories = Storage->ReadBool("CacheDirectories", CacheDirectories);
  364. CacheDirectoryChanges = Storage->ReadBool("CacheDirectoryChanges", CacheDirectoryChanges);
  365. PreserveDirectoryChanges = Storage->ReadBool("PreserveDirectoryChanges", PreserveDirectoryChanges);
  366. ResolveSymlinks = Storage->ReadBool("ResolveSymlinks", ResolveSymlinks);
  367. DSTMode = (TDSTMode)Storage->ReadInteger("ConsiderDST", DSTMode);
  368. LockInHome = Storage->ReadBool("LockInHome", LockInHome);
  369. Special = Storage->ReadBool("Special", Special);
  370. Shell = Storage->ReadString("Shell", Shell);
  371. ClearAliases = Storage->ReadBool("ClearAliases", ClearAliases);
  372. UnsetNationalVars = Storage->ReadBool("UnsetNationalVars", UnsetNationalVars);
  373. ListingCommand = Storage->ReadString("ListingCommand",
  374. Storage->ReadBool("AliasGroupList", false) ? AnsiString("ls -gla") : ListingCommand);
  375. IgnoreLsWarnings = Storage->ReadBool("IgnoreLsWarnings", IgnoreLsWarnings);
  376. SCPLsFullTime = TAutoSwitch(Storage->ReadInteger("SCPLsFullTime", SCPLsFullTime));
  377. FtpListAll = TAutoSwitch(Storage->ReadInteger("FtpListAll", FtpListAll));
  378. Scp1Compatibility = Storage->ReadBool("Scp1Compatibility", Scp1Compatibility);
  379. TimeDifference = Storage->ReadFloat("TimeDifference", TimeDifference);
  380. DeleteToRecycleBin = Storage->ReadBool("DeleteToRecycleBin", DeleteToRecycleBin);
  381. OverwrittenToRecycleBin = Storage->ReadBool("OverwrittenToRecycleBin", OverwrittenToRecycleBin);
  382. RecycleBinPath = Storage->ReadString("RecycleBinPath", RecycleBinPath);
  383. PostLoginCommands = Storage->ReadString("PostLoginCommands", PostLoginCommands);
  384. ReturnVar = Storage->ReadString("ReturnVar", ReturnVar);
  385. LookupUserGroups = Storage->ReadBool("LookupUserGroups", LookupUserGroups);
  386. EOLType = (TEOLType)Storage->ReadInteger("EOLType", EOLType);
  387. NotUtf = TAutoSwitch(Storage->ReadInteger("Utf", Storage->ReadInteger("SFTPUtfBug", NotUtf)));
  388. TcpNoDelay = Storage->ReadBool("TcpNoDelay", TcpNoDelay);
  389. ProxyMethod = (TProxyMethod)Storage->ReadInteger("ProxyMethod", -1);
  390. if (ProxyMethod < 0)
  391. {
  392. int ProxyType = Storage->ReadInteger("ProxyType", pxNone);
  393. int ProxySOCKSVersion;
  394. switch (ProxyType) {
  395. case pxHTTP:
  396. ProxyMethod = pmHTTP;
  397. break;
  398. case pxTelnet:
  399. ProxyMethod = pmTelnet;
  400. break;
  401. case pxSocks:
  402. ProxySOCKSVersion = Storage->ReadInteger("ProxySOCKSVersion", 5);
  403. ProxyMethod = ProxySOCKSVersion == 5 ? pmSocks5 : pmSocks4;
  404. break;
  405. default:
  406. case pxNone:
  407. ProxyMethod = pmNone;
  408. break;
  409. }
  410. }
  411. ProxyHost = Storage->ReadString("ProxyHost", ProxyHost);
  412. ProxyPort = Storage->ReadInteger("ProxyPort", ProxyPort);
  413. ProxyUsername = Storage->ReadString("ProxyUsername", ProxyUsername);
  414. if (Storage->ValueExists("ProxyPassword"))
  415. {
  416. // encrypt unencrypted password
  417. ProxyPassword = Storage->ReadString("ProxyPassword", "");
  418. }
  419. else
  420. {
  421. // load encrypted password
  422. FProxyPassword = Storage->ReadString("ProxyPasswordEnc", FProxyPassword);
  423. }
  424. if (ProxyMethod == pmCmd)
  425. {
  426. ProxyLocalCommand = Storage->ReadStringRaw("ProxyTelnetCommand", ProxyLocalCommand);
  427. }
  428. else
  429. {
  430. ProxyTelnetCommand = Storage->ReadStringRaw("ProxyTelnetCommand", ProxyTelnetCommand);
  431. }
  432. ProxyDNS = TAutoSwitch((Storage->ReadInteger("ProxyDNS", (ProxyDNS + 2) % 3) + 1) % 3);
  433. ProxyLocalhost = Storage->ReadBool("ProxyLocalhost", ProxyLocalhost);
  434. #define READ_BUG(BUG) \
  435. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger("Bug"#BUG, \
  436. 2 - Bug[sb##BUG]));
  437. READ_BUG(Ignore1);
  438. READ_BUG(PlainPW1);
  439. READ_BUG(RSA1);
  440. READ_BUG(HMAC2);
  441. READ_BUG(DeriveKey2);
  442. READ_BUG(RSAPad2);
  443. READ_BUG(Rekey2);
  444. READ_BUG(PKSessID2);
  445. READ_BUG(MaxPkt2);
  446. #undef READ_BUG
  447. if ((Bug[sbHMAC2] == asAuto) &&
  448. Storage->ReadBool("BuggyMAC", false))
  449. {
  450. Bug[sbHMAC2] = asOn;
  451. }
  452. SftpServer = Storage->ReadString("SftpServer", SftpServer);
  453. #define READ_SFTP_BUG(BUG) \
  454. SFTPBug[sb##BUG] = TAutoSwitch(Storage->ReadInteger("SFTP" #BUG "Bug", SFTPBug[sb##BUG]));
  455. READ_SFTP_BUG(Symlink);
  456. READ_SFTP_BUG(SignedTS);
  457. #undef READ_SFTP_BUG
  458. SFTPMaxVersion = Storage->ReadInteger("SFTPMaxVersion", SFTPMaxVersion);
  459. SFTPMaxPacketSize = Storage->ReadInteger("SFTPMaxPacketSize", SFTPMaxPacketSize);
  460. Color = Storage->ReadInteger("Color", Color);
  461. ProtocolStr = Storage->ReadString("Protocol", ProtocolStr);
  462. Tunnel = Storage->ReadBool("Tunnel", Tunnel);
  463. TunnelPortNumber = Storage->ReadInteger("TunnelPortNumber", TunnelPortNumber);
  464. TunnelUserName = Storage->ReadString("TunnelUserName", TunnelUserName);
  465. // must be loaded after TunnelUserName,
  466. // because TunnelHostName may be in format user@host
  467. TunnelHostName = Storage->ReadString("TunnelHostName", TunnelHostName);
  468. if (!Configuration->DisablePasswordStoring)
  469. {
  470. if (Storage->ValueExists("TunnelPasswordPlain"))
  471. {
  472. TunnelPassword = Storage->ReadString("TunnelPasswordPlain", TunnelPassword);
  473. RewritePassword = true;
  474. }
  475. else
  476. {
  477. FTunnelPassword = Storage->ReadString("TunnelPassword", FTunnelPassword);
  478. }
  479. }
  480. TunnelPublicKeyFile = Storage->ReadString("TunnelPublicKeyFile", TunnelPublicKeyFile);
  481. TunnelLocalPortNumber = Storage->ReadInteger("TunnelLocalPortNumber", TunnelLocalPortNumber);
  482. // Ftp prefix
  483. FtpPasvMode = Storage->ReadBool("FtpPasvMode", FtpPasvMode);
  484. FtpForcePasvIp = Storage->ReadBool("FtpForcePasvIp", FtpForcePasvIp);
  485. FtpAccount = Storage->ReadString("FtpAccount", FtpAccount);
  486. FtpPingInterval = Storage->ReadInteger("FtpPingInterval", FtpPingInterval);
  487. FtpPingType = static_cast<TPingType>(Storage->ReadInteger("FtpPingType", FtpPingType));
  488. Ftps = static_cast<TFtps>(Storage->ReadInteger("Ftps", Ftps));
  489. FtpProxyLogonType = Storage->ReadInteger("FtpProxyLogonType", FtpProxyLogonType);
  490. CustomParam1 = Storage->ReadString("CustomParam1", CustomParam1);
  491. CustomParam2 = Storage->ReadString("CustomParam2", CustomParam2);
  492. Storage->CloseSubKey();
  493. };
  494. if (RewritePassword)
  495. {
  496. TStorageAccessMode AccessMode = Storage->AccessMode;
  497. Storage->AccessMode = smReadWrite;
  498. try
  499. {
  500. if (Storage->OpenSubKey(InternalStorageKey, true))
  501. {
  502. Storage->DeleteValue("PasswordPlain");
  503. if (!Password.IsEmpty())
  504. {
  505. Storage->WriteString("Password", FPassword);
  506. }
  507. Storage->DeleteValue("TunnelPasswordPlain");
  508. if (!TunnelPassword.IsEmpty())
  509. {
  510. Storage->WriteString("TunnelPassword", FTunnelPassword);
  511. }
  512. Storage->CloseSubKey();
  513. }
  514. }
  515. catch(...)
  516. {
  517. // ignore errors (like read-only INI file)
  518. }
  519. Storage->AccessMode = AccessMode;
  520. }
  521. FModified = false;
  522. }
  523. //---------------------------------------------------------------------
  524. void __fastcall TSessionData::Save(THierarchicalStorage * Storage,
  525. bool PuttyExport, const TSessionData * Default)
  526. {
  527. if (Storage->OpenSubKey(InternalStorageKey, true))
  528. {
  529. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  530. if ((Default != NULL) && (CONV(Default->PROPERTY) == CONV(PROPERTY))) \
  531. { \
  532. Storage->DeleteValue(NAME); \
  533. } \
  534. else \
  535. { \
  536. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  537. }
  538. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  539. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, #PROPERTY, PROPERTY, )
  540. WRITE_DATA(String, HostName);
  541. WRITE_DATA(Integer, PortNumber);
  542. WRITE_DATA(Bool, Passwordless);
  543. WRITE_DATA_EX(Integer, "PingInterval", PingInterval / 60, );
  544. WRITE_DATA_EX(Integer, "PingIntervalSecs", PingInterval % 60, );
  545. Storage->DeleteValue("PingIntervalSec"); // obsolete
  546. // when PingInterval is stored always store PingType not to attempt to
  547. // deduce PingType from PingInterval (backward compatibility with pre 3.5)
  548. if (((Default != NULL) && (PingType != Default->PingType)) ||
  549. Storage->ValueExists("PingInterval"))
  550. {
  551. Storage->WriteInteger("PingType", PingType);
  552. }
  553. else
  554. {
  555. Storage->DeleteValue("PingType");
  556. }
  557. WRITE_DATA(Integer, Timeout);
  558. WRITE_DATA(Bool, TryAgent);
  559. WRITE_DATA(Bool, AgentFwd);
  560. WRITE_DATA(Bool, AuthTIS);
  561. WRITE_DATA(Bool, AuthKI);
  562. WRITE_DATA(Bool, AuthKIPassword);
  563. WRITE_DATA(Bool, AuthGSSAPI);
  564. WRITE_DATA(Bool, GSSAPIFwdTGT);
  565. WRITE_DATA(String, GSSAPIServerRealm);
  566. Storage->DeleteValue("TryGSSKEX");
  567. Storage->DeleteValue("UserNameFromEnvironment");
  568. Storage->DeleteValue("GSSAPIServerChoosesUserName");
  569. Storage->DeleteValue("GSSAPITrustDNS");
  570. if (PuttyExport)
  571. {
  572. // duplicate kerberos setting with keys of the vintela quest putty
  573. WRITE_DATA_EX(Bool, "AuthSSPI", AuthGSSAPI, );
  574. WRITE_DATA_EX(Bool, "SSPIFwdTGT", GSSAPIFwdTGT, );
  575. WRITE_DATA_EX(String, "KerbPrincipal", GSSAPIServerRealm, );
  576. // duplicate kerberos setting with keys of the official putty
  577. WRITE_DATA_EX(Bool, "GssapiFwd", GSSAPIFwdTGT, );
  578. }
  579. WRITE_DATA(Bool, ChangeUsername);
  580. WRITE_DATA(Bool, Compression);
  581. WRITE_DATA(Integer, SshProt);
  582. WRITE_DATA(Bool, Ssh2DES);
  583. WRITE_DATA(Bool, SshNoUserAuth);
  584. WRITE_DATA_EX(String, "Cipher", CipherList, );
  585. WRITE_DATA_EX(String, "KEX", KexList, );
  586. WRITE_DATA(Integer, AddressFamily);
  587. WRITE_DATA_EX(String, "RekeyBytes", RekeyData, );
  588. WRITE_DATA(Integer, RekeyTime);
  589. WRITE_DATA(Bool, TcpNoDelay);
  590. if (PuttyExport)
  591. {
  592. WRITE_DATA(StringRaw, UserName);
  593. WRITE_DATA(StringRaw, PublicKeyFile);
  594. }
  595. else
  596. {
  597. WRITE_DATA(String, UserName);
  598. WRITE_DATA(String, PublicKeyFile);
  599. WRITE_DATA(Integer, FSProtocol);
  600. WRITE_DATA(String, LocalDirectory);
  601. WRITE_DATA(String, RemoteDirectory);
  602. WRITE_DATA(Bool, UpdateDirectories);
  603. WRITE_DATA(Bool, CacheDirectories);
  604. WRITE_DATA(Bool, CacheDirectoryChanges);
  605. WRITE_DATA(Bool, PreserveDirectoryChanges);
  606. WRITE_DATA(Bool, ResolveSymlinks);
  607. WRITE_DATA_EX(Integer, "ConsiderDST", DSTMode, );
  608. WRITE_DATA(Bool, LockInHome);
  609. // Special is never stored (if it would, login dialog must be modified not to
  610. // duplicate Special parameter when Special session is loaded and then stored
  611. // under different name)
  612. // WRITE_DATA(Bool, Special);
  613. WRITE_DATA(String, Shell);
  614. WRITE_DATA(Bool, ClearAliases);
  615. WRITE_DATA(Bool, UnsetNationalVars);
  616. WRITE_DATA(String, ListingCommand);
  617. WRITE_DATA(Bool, IgnoreLsWarnings);
  618. WRITE_DATA(Integer, SCPLsFullTime);
  619. WRITE_DATA(Integer, FtpListAll);
  620. WRITE_DATA(Bool, Scp1Compatibility);
  621. WRITE_DATA(Float, TimeDifference);
  622. WRITE_DATA(Bool, DeleteToRecycleBin);
  623. WRITE_DATA(Bool, OverwrittenToRecycleBin);
  624. WRITE_DATA(String, RecycleBinPath);
  625. WRITE_DATA(String, PostLoginCommands);
  626. WRITE_DATA(String, ReturnVar);
  627. WRITE_DATA(Bool, LookupUserGroups);
  628. WRITE_DATA(Integer, EOLType);
  629. Storage->DeleteValue("SFTPUtfBug");
  630. WRITE_DATA_EX(Integer, "Utf", NotUtf, );
  631. }
  632. WRITE_DATA(Integer, ProxyMethod);
  633. if (PuttyExport)
  634. {
  635. // support for Putty 0.53b and older
  636. int ProxyType;
  637. int ProxySOCKSVersion = 5;
  638. switch (ProxyMethod) {
  639. case pmHTTP:
  640. ProxyType = pxHTTP;
  641. break;
  642. case pmTelnet:
  643. ProxyType = pxTelnet;
  644. break;
  645. case pmSocks5:
  646. ProxyType = pxSocks;
  647. ProxySOCKSVersion = 5;
  648. break;
  649. case pmSocks4:
  650. ProxyType = pxSocks;
  651. ProxySOCKSVersion = 4;
  652. break;
  653. default:
  654. case pmNone:
  655. ProxyType = pxNone;
  656. break;
  657. }
  658. Storage->WriteInteger("ProxyType", ProxyType);
  659. Storage->WriteInteger("ProxySOCKSVersion", ProxySOCKSVersion);
  660. }
  661. else
  662. {
  663. Storage->DeleteValue("ProxyType");
  664. Storage->DeleteValue("ProxySOCKSVersion");
  665. }
  666. WRITE_DATA(String, ProxyHost);
  667. WRITE_DATA(Integer, ProxyPort);
  668. WRITE_DATA(String, ProxyUsername);
  669. if (ProxyMethod == pmCmd)
  670. {
  671. WRITE_DATA_EX(StringRaw, "ProxyTelnetCommand", ProxyLocalCommand, );
  672. }
  673. else
  674. {
  675. WRITE_DATA(StringRaw, ProxyTelnetCommand);
  676. }
  677. #define WRITE_DATA_CONV_FUNC(X) (((X) + 2) % 3)
  678. WRITE_DATA_CONV(Integer, "ProxyDNS", ProxyDNS);
  679. #undef WRITE_DATA_CONV_FUNC
  680. WRITE_DATA(Bool, ProxyLocalhost);
  681. #define WRITE_DATA_CONV_FUNC(X) (2 - (X))
  682. #define WRITE_BUG(BUG) WRITE_DATA_CONV(Integer, "Bug" #BUG, Bug[sb##BUG]);
  683. WRITE_BUG(Ignore1);
  684. WRITE_BUG(PlainPW1);
  685. WRITE_BUG(RSA1);
  686. WRITE_BUG(HMAC2);
  687. WRITE_BUG(DeriveKey2);
  688. WRITE_BUG(RSAPad2);
  689. WRITE_BUG(Rekey2);
  690. WRITE_BUG(PKSessID2);
  691. WRITE_BUG(MaxPkt2);
  692. #undef WRITE_BUG
  693. #undef WRITE_DATA_CONV_FUNC
  694. Storage->DeleteValue("BuggyMAC");
  695. Storage->DeleteValue("AliasGroupList");
  696. if (PuttyExport)
  697. {
  698. WRITE_DATA_EX(String, "Protocol", ProtocolStr, );
  699. }
  700. if (!PuttyExport)
  701. {
  702. WRITE_DATA(String, SftpServer);
  703. #define WRITE_SFTP_BUG(BUG) WRITE_DATA_EX(Integer, "SFTP" #BUG "Bug", SFTPBug[sb##BUG], );
  704. WRITE_SFTP_BUG(Symlink);
  705. WRITE_SFTP_BUG(SignedTS);
  706. #undef WRITE_SFTP_BUG
  707. WRITE_DATA(Integer, SFTPMaxVersion);
  708. WRITE_DATA(Integer, SFTPMaxPacketSize);
  709. WRITE_DATA(Integer, Color);
  710. WRITE_DATA(Bool, Tunnel);
  711. WRITE_DATA(String, TunnelHostName);
  712. WRITE_DATA(Integer, TunnelPortNumber);
  713. WRITE_DATA(String, TunnelUserName);
  714. WRITE_DATA(String, TunnelPublicKeyFile);
  715. WRITE_DATA(Integer, TunnelLocalPortNumber);
  716. WRITE_DATA(Bool, FtpPasvMode);
  717. WRITE_DATA(Bool, FtpForcePasvIp);
  718. WRITE_DATA(String, FtpAccount);
  719. WRITE_DATA(Integer, FtpPingInterval);
  720. WRITE_DATA(Integer, FtpPingType);
  721. WRITE_DATA(Integer, Ftps);
  722. WRITE_DATA(Integer, FtpProxyLogonType);
  723. WRITE_DATA(String, CustomParam1);
  724. WRITE_DATA(String, CustomParam2);
  725. }
  726. SavePasswords(Storage, PuttyExport);
  727. Storage->CloseSubKey();
  728. }
  729. }
  730. //---------------------------------------------------------------------
  731. void __fastcall TSessionData::SavePasswords(THierarchicalStorage * Storage, bool PuttyExport)
  732. {
  733. if (!Configuration->DisablePasswordStoring && !PuttyExport && !FPassword.IsEmpty())
  734. {
  735. Storage->WriteString("Password", StronglyRecryptPassword(FPassword, UserName+HostName));
  736. }
  737. else
  738. {
  739. Storage->DeleteValue("Password");
  740. }
  741. Storage->DeleteValue("PasswordPlain");
  742. if (PuttyExport)
  743. {
  744. // save password unencrypted
  745. Storage->WriteString("ProxyPassword", ProxyPassword);
  746. }
  747. else
  748. {
  749. // save password encrypted
  750. if (!FProxyPassword.IsEmpty())
  751. {
  752. Storage->WriteString("ProxyPasswordEnc", StronglyRecryptPassword(FProxyPassword, ProxyUsername+ProxyHost));
  753. }
  754. else
  755. {
  756. Storage->DeleteValue("ProxyPasswordEnc");
  757. }
  758. Storage->DeleteValue("ProxyPassword");
  759. if (!Configuration->DisablePasswordStoring && !FTunnelPassword.IsEmpty())
  760. {
  761. Storage->WriteString("TunnelPassword", StronglyRecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName));
  762. }
  763. else
  764. {
  765. Storage->DeleteValue("TunnelPassword");
  766. }
  767. }
  768. }
  769. //---------------------------------------------------------------------
  770. void __fastcall TSessionData::RecryptPasswords()
  771. {
  772. Password = Password;
  773. ProxyPassword = ProxyPassword;
  774. TunnelPassword = TunnelPassword;
  775. }
  776. //---------------------------------------------------------------------
  777. bool __fastcall TSessionData::HasAnyPassword()
  778. {
  779. return !FPassword.IsEmpty() || !FProxyPassword.IsEmpty() || !FTunnelPassword.IsEmpty();
  780. }
  781. //---------------------------------------------------------------------
  782. void __fastcall TSessionData::SaveRecryptedPasswords(THierarchicalStorage * Storage)
  783. {
  784. if (Storage->OpenSubKey(InternalStorageKey, true))
  785. {
  786. RecryptPasswords();
  787. SavePasswords(Storage, false);
  788. Storage->CloseSubKey();
  789. }
  790. }
  791. //---------------------------------------------------------------------
  792. void __fastcall TSessionData::Remove()
  793. {
  794. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  795. try
  796. {
  797. Storage->Explicit = true;
  798. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  799. {
  800. Storage->RecursiveDeleteSubKey(InternalStorageKey);
  801. }
  802. }
  803. __finally
  804. {
  805. delete Storage;
  806. }
  807. }
  808. //---------------------------------------------------------------------
  809. bool __fastcall TSessionData::ParseUrl(AnsiString Url, TOptions * Options,
  810. TStoredSessionList * StoredSessions, bool & DefaultsOnly, AnsiString * FileName,
  811. bool * AProtocolDefined)
  812. {
  813. bool ProtocolDefined = false;
  814. bool PortNumberDefined = false;
  815. TFSProtocol AFSProtocol;
  816. int APortNumber;
  817. TFtps AFtps = ftpsNone;
  818. if (Url.SubString(1, 4).LowerCase() == "scp:")
  819. {
  820. AFSProtocol = fsSCPonly;
  821. APortNumber = SshPortNumber;
  822. Url.Delete(1, 4);
  823. ProtocolDefined = true;
  824. }
  825. else if (Url.SubString(1, 5).LowerCase() == "sftp:")
  826. {
  827. AFSProtocol = fsSFTPonly;
  828. APortNumber = SshPortNumber;
  829. Url.Delete(1, 5);
  830. ProtocolDefined = true;
  831. }
  832. else if (Url.SubString(1, 4).LowerCase() == "ftp:")
  833. {
  834. AFSProtocol = fsFTP;
  835. Ftps = ftpsNone;
  836. APortNumber = FtpPortNumber;
  837. Url.Delete(1, 4);
  838. ProtocolDefined = true;
  839. }
  840. else if (Url.SubString(1, 5).LowerCase() == "ftps:")
  841. {
  842. AFSProtocol = fsFTP;
  843. AFtps = ftpsImplicit;
  844. APortNumber = FtpsImplicitPortNumber;
  845. Url.Delete(1, 5);
  846. ProtocolDefined = true;
  847. }
  848. if (ProtocolDefined && (Url.SubString(1, 2) == "//"))
  849. {
  850. Url.Delete(1, 2);
  851. }
  852. if (AProtocolDefined != NULL)
  853. {
  854. *AProtocolDefined = ProtocolDefined;
  855. }
  856. if (!Url.IsEmpty())
  857. {
  858. AnsiString DecodedUrl = DecodeUrlChars(Url);
  859. // lookup stored session even if protocol was defined
  860. // (this allows setting for example default username for host
  861. // by creating stored session named by host)
  862. TSessionData * Data = NULL;
  863. for (Integer Index = 0; Index < StoredSessions->Count + StoredSessions->HiddenCount; Index++)
  864. {
  865. TSessionData * AData = (TSessionData *)StoredSessions->Items[Index];
  866. if (AnsiSameText(AData->Name, DecodedUrl) ||
  867. AnsiSameText(AData->Name + "/", DecodedUrl.SubString(1, AData->Name.Length() + 1)))
  868. {
  869. Data = AData;
  870. break;
  871. }
  872. }
  873. AnsiString ARemoteDirectory;
  874. if (Data != NULL)
  875. {
  876. DefaultsOnly = false;
  877. Assign(Data);
  878. int P = 1;
  879. while (!AnsiSameText(DecodeUrlChars(Url.SubString(1, P)), Data->Name))
  880. {
  881. P++;
  882. assert(P <= Url.Length());
  883. }
  884. ARemoteDirectory = Url.SubString(P + 1, Url.Length() - P);
  885. if (StoredSessions->IsHidden(Data))
  886. {
  887. Data->Remove();
  888. StoredSessions->Remove(Data);
  889. // only modified, implicit
  890. StoredSessions->Save(false, false);
  891. }
  892. }
  893. else
  894. {
  895. Assign(StoredSessions->DefaultSettings);
  896. Name = "";
  897. int PSlash = Url.Pos("/");
  898. if (PSlash == 0)
  899. {
  900. PSlash = Url.Length() + 1;
  901. }
  902. AnsiString ConnectInfo = Url.SubString(1, PSlash - 1);
  903. int P = ConnectInfo.LastDelimiter("@");
  904. AnsiString UserInfo;
  905. AnsiString HostInfo;
  906. if (P > 0)
  907. {
  908. UserInfo = ConnectInfo.SubString(1, P - 1);
  909. HostInfo = ConnectInfo.SubString(P + 1, ConnectInfo.Length() - P);
  910. }
  911. else
  912. {
  913. HostInfo = ConnectInfo;
  914. }
  915. HostName = DecodeUrlChars(CutToChar(HostInfo, ':', true));
  916. // expanded from ?: operator, as it caused strange "access violation" errors
  917. if (!HostInfo.IsEmpty())
  918. {
  919. PortNumber = StrToIntDef(DecodeUrlChars(HostInfo), -1);
  920. PortNumberDefined = true;
  921. }
  922. else if (ProtocolDefined)
  923. {
  924. PortNumber = APortNumber;
  925. }
  926. if (ProtocolDefined)
  927. {
  928. Ftps = AFtps;
  929. }
  930. bool PasswordSeparator = (UserInfo.Pos(':') != 0);
  931. UserName = DecodeUrlChars(CutToChar(UserInfo, ':', false));
  932. Password = DecodeUrlChars(UserInfo);
  933. Passwordless = Password.IsEmpty() && PasswordSeparator;
  934. ARemoteDirectory = Url.SubString(PSlash, Url.Length() - PSlash + 1);
  935. }
  936. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory != "/"))
  937. {
  938. if ((ARemoteDirectory[ARemoteDirectory.Length()] != '/') &&
  939. (FileName != NULL))
  940. {
  941. *FileName = DecodeUrlChars(UnixExtractFileName(ARemoteDirectory));
  942. ARemoteDirectory = UnixExtractFilePath(ARemoteDirectory);
  943. }
  944. RemoteDirectory = DecodeUrlChars(ARemoteDirectory);
  945. }
  946. DefaultsOnly = false;
  947. }
  948. else
  949. {
  950. Assign(StoredSessions->DefaultSettings);
  951. DefaultsOnly = true;
  952. }
  953. if (ProtocolDefined)
  954. {
  955. FSProtocol = AFSProtocol;
  956. }
  957. if (Options != NULL)
  958. {
  959. // we deliberatelly do keep defaultonly to false, in presence of any option,
  960. // as the option should not make session "connectable"
  961. AnsiString Value;
  962. if (Options->FindSwitch("privatekey", Value))
  963. {
  964. PublicKeyFile = Value;
  965. }
  966. if (Options->FindSwitch("timeout", Value))
  967. {
  968. Timeout = StrToInt(Value);
  969. }
  970. if (Options->FindSwitch("hostkey", Value) ||
  971. Options->FindSwitch("certificate", Value))
  972. {
  973. HostKey = Value;
  974. }
  975. if (Options->FindSwitch("passive", Value))
  976. {
  977. FtpPasvMode = (StrToIntDef(Value, 1) != 0);
  978. }
  979. if (Options->FindSwitch("implicit", Value))
  980. {
  981. bool Enabled = (StrToIntDef(Value, 1) != 0);
  982. Ftps = Enabled ? ftpsImplicit : ftpsNone;
  983. if (!PortNumberDefined && Enabled)
  984. {
  985. PortNumber = FtpsImplicitPortNumber;
  986. }
  987. }
  988. if (Options->FindSwitch("explicitssl", Value))
  989. {
  990. bool Enabled = (StrToIntDef(Value, 1) != 0);
  991. Ftps = Enabled ? ftpsExplicitSsl : ftpsNone;
  992. if (!PortNumberDefined && Enabled)
  993. {
  994. PortNumber = FtpPortNumber;
  995. }
  996. }
  997. if (Options->FindSwitch("explicittls", Value))
  998. {
  999. bool Enabled = (StrToIntDef(Value, 1) != 0);
  1000. Ftps = Enabled ? ftpsExplicitTls : ftpsNone;
  1001. if (!PortNumberDefined && Enabled)
  1002. {
  1003. PortNumber = FtpPortNumber;
  1004. }
  1005. }
  1006. }
  1007. return true;
  1008. }
  1009. //---------------------------------------------------------------------
  1010. void __fastcall TSessionData::ConfigureTunnel(int APortNumber)
  1011. {
  1012. FOrigHostName = HostName;
  1013. FOrigPortNumber = PortNumber;
  1014. FOrigProxyMethod = ProxyMethod;
  1015. HostName = "127.0.0.1";
  1016. PortNumber = APortNumber;
  1017. // proxy settings is used for tunnel
  1018. ProxyMethod = pmNone;
  1019. }
  1020. //---------------------------------------------------------------------
  1021. void __fastcall TSessionData::RollbackTunnel()
  1022. {
  1023. HostName = FOrigHostName;
  1024. PortNumber = FOrigPortNumber;
  1025. ProxyMethod = FOrigProxyMethod;
  1026. }
  1027. //---------------------------------------------------------------------
  1028. void __fastcall TSessionData::ExpandEnvironmentVariables()
  1029. {
  1030. PublicKeyFile = ::ExpandEnvironmentVariables(PublicKeyFile);
  1031. }
  1032. //---------------------------------------------------------------------
  1033. void __fastcall TSessionData::ValidatePath(const AnsiString Path)
  1034. {
  1035. // noop
  1036. }
  1037. //---------------------------------------------------------------------
  1038. void __fastcall TSessionData::ValidateName(const AnsiString Name)
  1039. {
  1040. if (Name.LastDelimiter("/") > 0)
  1041. {
  1042. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, "/")));
  1043. }
  1044. }
  1045. //---------------------------------------------------------------------
  1046. AnsiString __fastcall TSessionData::EncryptPassword(const AnsiString & Password, AnsiString Key)
  1047. {
  1048. return Configuration->EncryptPassword(Password, Key);
  1049. }
  1050. //---------------------------------------------------------------------
  1051. AnsiString __fastcall TSessionData::StronglyRecryptPassword(const AnsiString & Password, AnsiString Key)
  1052. {
  1053. return Configuration->StronglyRecryptPassword(Password, Key);
  1054. }
  1055. //---------------------------------------------------------------------
  1056. AnsiString __fastcall TSessionData::DecryptPassword(const AnsiString & Password, AnsiString Key)
  1057. {
  1058. AnsiString Result;
  1059. try
  1060. {
  1061. Result = Configuration->DecryptPassword(Password, Key);
  1062. }
  1063. catch(EAbort &)
  1064. {
  1065. // silently ignore aborted prompts for master password and return empty password
  1066. }
  1067. return Result;
  1068. }
  1069. //---------------------------------------------------------------------
  1070. bool __fastcall TSessionData::GetCanLogin()
  1071. {
  1072. return !FHostName.IsEmpty();
  1073. }
  1074. //---------------------------------------------------------------------------
  1075. AnsiString __fastcall TSessionData::GetSessionKey()
  1076. {
  1077. return FORMAT("%s@%s", (UserName, HostName));
  1078. }
  1079. //---------------------------------------------------------------------
  1080. AnsiString __fastcall TSessionData::GetInternalStorageKey()
  1081. {
  1082. if (Name.IsEmpty())
  1083. {
  1084. return SessionKey;
  1085. }
  1086. else
  1087. {
  1088. return Name;
  1089. }
  1090. }
  1091. //---------------------------------------------------------------------
  1092. AnsiString __fastcall TSessionData::GetStorageKey()
  1093. {
  1094. return SessionName;
  1095. }
  1096. //---------------------------------------------------------------------
  1097. void __fastcall TSessionData::SetHostName(AnsiString value)
  1098. {
  1099. if (FHostName != value)
  1100. {
  1101. // HostName is key for password encryption
  1102. AnsiString XPassword = Password;
  1103. int P = value.LastDelimiter("@");
  1104. if (P > 0)
  1105. {
  1106. UserName = value.SubString(1, P - 1);
  1107. value = value.SubString(P + 1, value.Length() - P);
  1108. }
  1109. FHostName = value;
  1110. FModified = true;
  1111. Password = XPassword;
  1112. if (!XPassword.IsEmpty())
  1113. {
  1114. XPassword.Unique();
  1115. memset(XPassword.c_str(), 0, XPassword.Length());
  1116. }
  1117. }
  1118. }
  1119. //---------------------------------------------------------------------
  1120. void __fastcall TSessionData::SetPortNumber(int value)
  1121. {
  1122. SET_SESSION_PROPERTY(PortNumber);
  1123. }
  1124. //---------------------------------------------------------------------------
  1125. void __fastcall TSessionData::SetShell(AnsiString value)
  1126. {
  1127. SET_SESSION_PROPERTY(Shell);
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void __fastcall TSessionData::SetSftpServer(AnsiString value)
  1131. {
  1132. SET_SESSION_PROPERTY(SftpServer);
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. void __fastcall TSessionData::SetClearAliases(bool value)
  1136. {
  1137. SET_SESSION_PROPERTY(ClearAliases);
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TSessionData::SetListingCommand(AnsiString value)
  1141. {
  1142. SET_SESSION_PROPERTY(ListingCommand);
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  1146. {
  1147. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  1151. {
  1152. SET_SESSION_PROPERTY(UnsetNationalVars);
  1153. }
  1154. //---------------------------------------------------------------------
  1155. void __fastcall TSessionData::SetUserName(AnsiString value)
  1156. {
  1157. // UserName is key for password encryption
  1158. AnsiString XPassword = Password;
  1159. SET_SESSION_PROPERTY(UserName);
  1160. Password = XPassword;
  1161. if (!XPassword.IsEmpty())
  1162. {
  1163. XPassword.Unique();
  1164. memset(XPassword.c_str(), 0, XPassword.Length());
  1165. }
  1166. }
  1167. //---------------------------------------------------------------------
  1168. void __fastcall TSessionData::SetPassword(AnsiString value)
  1169. {
  1170. if (!value.IsEmpty())
  1171. {
  1172. Passwordless = false;
  1173. }
  1174. value = EncryptPassword(value, UserName+HostName);
  1175. SET_SESSION_PROPERTY(Password);
  1176. }
  1177. //---------------------------------------------------------------------
  1178. AnsiString __fastcall TSessionData::GetPassword()
  1179. {
  1180. return DecryptPassword(FPassword, UserName+HostName);
  1181. }
  1182. //---------------------------------------------------------------------
  1183. void __fastcall TSessionData::SetPasswordless(bool value)
  1184. {
  1185. SET_SESSION_PROPERTY(Passwordless);
  1186. }
  1187. //---------------------------------------------------------------------
  1188. void __fastcall TSessionData::SetPingInterval(int value)
  1189. {
  1190. SET_SESSION_PROPERTY(PingInterval);
  1191. }
  1192. //---------------------------------------------------------------------
  1193. void __fastcall TSessionData::SetTryAgent(bool value)
  1194. {
  1195. SET_SESSION_PROPERTY(TryAgent);
  1196. }
  1197. //---------------------------------------------------------------------
  1198. void __fastcall TSessionData::SetAgentFwd(bool value)
  1199. {
  1200. SET_SESSION_PROPERTY(AgentFwd);
  1201. }
  1202. //---------------------------------------------------------------------
  1203. void __fastcall TSessionData::SetAuthTIS(bool value)
  1204. {
  1205. SET_SESSION_PROPERTY(AuthTIS);
  1206. }
  1207. //---------------------------------------------------------------------
  1208. void __fastcall TSessionData::SetAuthKI(bool value)
  1209. {
  1210. SET_SESSION_PROPERTY(AuthKI);
  1211. }
  1212. //---------------------------------------------------------------------
  1213. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  1214. {
  1215. SET_SESSION_PROPERTY(AuthKIPassword);
  1216. }
  1217. //---------------------------------------------------------------------
  1218. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  1219. {
  1220. SET_SESSION_PROPERTY(AuthGSSAPI);
  1221. }
  1222. //---------------------------------------------------------------------
  1223. void __fastcall TSessionData::SetGSSAPIFwdTGT(bool value)
  1224. {
  1225. SET_SESSION_PROPERTY(GSSAPIFwdTGT);
  1226. }
  1227. //---------------------------------------------------------------------
  1228. void __fastcall TSessionData::SetGSSAPIServerRealm(AnsiString value)
  1229. {
  1230. SET_SESSION_PROPERTY(GSSAPIServerRealm);
  1231. }
  1232. //---------------------------------------------------------------------
  1233. void __fastcall TSessionData::SetChangeUsername(bool value)
  1234. {
  1235. SET_SESSION_PROPERTY(ChangeUsername);
  1236. }
  1237. //---------------------------------------------------------------------
  1238. void __fastcall TSessionData::SetCompression(bool value)
  1239. {
  1240. SET_SESSION_PROPERTY(Compression);
  1241. }
  1242. //---------------------------------------------------------------------
  1243. void __fastcall TSessionData::SetSshProt(TSshProt value)
  1244. {
  1245. SET_SESSION_PROPERTY(SshProt);
  1246. }
  1247. //---------------------------------------------------------------------
  1248. void __fastcall TSessionData::SetSsh2DES(bool value)
  1249. {
  1250. SET_SESSION_PROPERTY(Ssh2DES);
  1251. }
  1252. //---------------------------------------------------------------------
  1253. void __fastcall TSessionData::SetSshNoUserAuth(bool value)
  1254. {
  1255. SET_SESSION_PROPERTY(SshNoUserAuth);
  1256. }
  1257. //---------------------------------------------------------------------
  1258. AnsiString __fastcall TSessionData::GetSshProtStr()
  1259. {
  1260. return SshProtList[FSshProt];
  1261. }
  1262. //---------------------------------------------------------------------
  1263. bool __fastcall TSessionData::GetUsesSsh()
  1264. {
  1265. return (FSProtocol != fsFTP);
  1266. }
  1267. //---------------------------------------------------------------------
  1268. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  1269. {
  1270. assert(Index >= 0 && Index < CIPHER_COUNT);
  1271. SET_SESSION_PROPERTY(Ciphers[Index]);
  1272. }
  1273. //---------------------------------------------------------------------
  1274. TCipher __fastcall TSessionData::GetCipher(int Index) const
  1275. {
  1276. assert(Index >= 0 && Index < CIPHER_COUNT);
  1277. return FCiphers[Index];
  1278. }
  1279. //---------------------------------------------------------------------
  1280. void __fastcall TSessionData::SetCipherList(AnsiString value)
  1281. {
  1282. bool Used[CIPHER_COUNT];
  1283. for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
  1284. AnsiString CipherStr;
  1285. int Index = 0;
  1286. while (!value.IsEmpty() && (Index < CIPHER_COUNT))
  1287. {
  1288. CipherStr = CutToChar(value, ',', true);
  1289. for (int C = 0; C < CIPHER_COUNT; C++)
  1290. {
  1291. if (!CipherStr.AnsiCompareIC(CipherNames[C]))
  1292. {
  1293. Cipher[Index] = (TCipher)C;
  1294. Used[C] = true;
  1295. Index++;
  1296. break;
  1297. }
  1298. }
  1299. }
  1300. for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
  1301. {
  1302. if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
  1303. }
  1304. }
  1305. //---------------------------------------------------------------------
  1306. AnsiString __fastcall TSessionData::GetCipherList() const
  1307. {
  1308. AnsiString Result;
  1309. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  1310. {
  1311. Result += AnsiString(Index ? "," : "") + CipherNames[Cipher[Index]];
  1312. }
  1313. return Result;
  1314. }
  1315. //---------------------------------------------------------------------
  1316. void __fastcall TSessionData::SetKex(int Index, TKex value)
  1317. {
  1318. assert(Index >= 0 && Index < KEX_COUNT);
  1319. SET_SESSION_PROPERTY(Kex[Index]);
  1320. }
  1321. //---------------------------------------------------------------------
  1322. TKex __fastcall TSessionData::GetKex(int Index) const
  1323. {
  1324. assert(Index >= 0 && Index < KEX_COUNT);
  1325. return FKex[Index];
  1326. }
  1327. //---------------------------------------------------------------------
  1328. void __fastcall TSessionData::SetKexList(AnsiString value)
  1329. {
  1330. bool Used[KEX_COUNT];
  1331. for (int K = 0; K < KEX_COUNT; K++) Used[K] = false;
  1332. AnsiString KexStr;
  1333. int Index = 0;
  1334. while (!value.IsEmpty() && (Index < KEX_COUNT))
  1335. {
  1336. KexStr = CutToChar(value, ',', true);
  1337. for (int K = 0; K < KEX_COUNT; K++)
  1338. {
  1339. if (!KexStr.AnsiCompareIC(KexNames[K]))
  1340. {
  1341. Kex[Index] = (TKex)K;
  1342. Used[K] = true;
  1343. Index++;
  1344. break;
  1345. }
  1346. }
  1347. }
  1348. for (int K = 0; K < KEX_COUNT && Index < KEX_COUNT; K++)
  1349. {
  1350. if (!Used[DefaultKexList[K]]) Kex[Index++] = DefaultKexList[K];
  1351. }
  1352. }
  1353. //---------------------------------------------------------------------
  1354. AnsiString __fastcall TSessionData::GetKexList() const
  1355. {
  1356. AnsiString Result;
  1357. for (int Index = 0; Index < KEX_COUNT; Index++)
  1358. {
  1359. Result += AnsiString(Index ? "," : "") + KexNames[Kex[Index]];
  1360. }
  1361. return Result;
  1362. }
  1363. //---------------------------------------------------------------------
  1364. void __fastcall TSessionData::SetPublicKeyFile(AnsiString value)
  1365. {
  1366. if (FPublicKeyFile != value)
  1367. {
  1368. FPublicKeyFile = StripPathQuotes(value);
  1369. FModified = true;
  1370. }
  1371. }
  1372. //---------------------------------------------------------------------
  1373. void __fastcall TSessionData::SetReturnVar(AnsiString value)
  1374. {
  1375. SET_SESSION_PROPERTY(ReturnVar);
  1376. }
  1377. //---------------------------------------------------------------------------
  1378. void __fastcall TSessionData::SetLookupUserGroups(bool value)
  1379. {
  1380. SET_SESSION_PROPERTY(LookupUserGroups);
  1381. }
  1382. //---------------------------------------------------------------------------
  1383. void __fastcall TSessionData::SetEOLType(TEOLType value)
  1384. {
  1385. SET_SESSION_PROPERTY(EOLType);
  1386. }
  1387. //---------------------------------------------------------------------------
  1388. TDateTime __fastcall TSessionData::GetTimeoutDT()
  1389. {
  1390. return SecToDateTime(Timeout);
  1391. }
  1392. //---------------------------------------------------------------------------
  1393. void __fastcall TSessionData::SetTimeout(int value)
  1394. {
  1395. SET_SESSION_PROPERTY(Timeout);
  1396. }
  1397. //---------------------------------------------------------------------------
  1398. void __fastcall TSessionData::SetProtocol(TProtocol value)
  1399. {
  1400. SET_SESSION_PROPERTY(Protocol);
  1401. }
  1402. //---------------------------------------------------------------------------
  1403. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  1404. {
  1405. SET_SESSION_PROPERTY(FSProtocol);
  1406. }
  1407. //---------------------------------------------------------------------
  1408. AnsiString __fastcall TSessionData::GetFSProtocolStr()
  1409. {
  1410. assert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  1411. return FSProtocolNames[FSProtocol];
  1412. }
  1413. //---------------------------------------------------------------------------
  1414. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  1415. {
  1416. if (value != DetectReturnVar)
  1417. {
  1418. ReturnVar = value ? "" : "$?";
  1419. }
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. bool __fastcall TSessionData::GetDetectReturnVar()
  1423. {
  1424. return ReturnVar.IsEmpty();
  1425. }
  1426. //---------------------------------------------------------------------------
  1427. void __fastcall TSessionData::SetDefaultShell(bool value)
  1428. {
  1429. if (value != DefaultShell)
  1430. {
  1431. Shell = value ? "" : "/bin/bash";
  1432. }
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. bool __fastcall TSessionData::GetDefaultShell()
  1436. {
  1437. return Shell.IsEmpty();
  1438. }
  1439. //---------------------------------------------------------------------------
  1440. void __fastcall TSessionData::SetProtocolStr(AnsiString value)
  1441. {
  1442. FProtocol = ptRaw;
  1443. for (int Index = 0; Index < PROTOCOL_COUNT; Index++)
  1444. {
  1445. if (value.AnsiCompareIC(ProtocolNames[Index]) == 0)
  1446. {
  1447. FProtocol = TProtocol(Index);
  1448. break;
  1449. }
  1450. }
  1451. }
  1452. //---------------------------------------------------------------------
  1453. AnsiString __fastcall TSessionData::GetProtocolStr() const
  1454. {
  1455. return ProtocolNames[Protocol];
  1456. }
  1457. //---------------------------------------------------------------------
  1458. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  1459. {
  1460. unsigned short hour, min, sec, msec;
  1461. value.DecodeTime(&hour, &min, &sec, &msec);
  1462. PingInterval = ((int)hour)*60*60 + ((int)min)*60 + sec;
  1463. }
  1464. //---------------------------------------------------------------------------
  1465. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  1466. {
  1467. return SecToDateTime(PingInterval);
  1468. }
  1469. //---------------------------------------------------------------------------
  1470. void __fastcall TSessionData::SetPingType(TPingType value)
  1471. {
  1472. SET_SESSION_PROPERTY(PingType);
  1473. }
  1474. //---------------------------------------------------------------------------
  1475. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  1476. {
  1477. SET_SESSION_PROPERTY(AddressFamily);
  1478. }
  1479. //---------------------------------------------------------------------------
  1480. void __fastcall TSessionData::SetRekeyData(AnsiString value)
  1481. {
  1482. SET_SESSION_PROPERTY(RekeyData);
  1483. }
  1484. //---------------------------------------------------------------------------
  1485. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  1486. {
  1487. SET_SESSION_PROPERTY(RekeyTime);
  1488. }
  1489. //---------------------------------------------------------------------
  1490. AnsiString __fastcall TSessionData::GetDefaultSessionName()
  1491. {
  1492. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  1493. {
  1494. return FORMAT("%s@%s", (UserName, HostName));
  1495. }
  1496. else if (!HostName.IsEmpty())
  1497. {
  1498. return HostName;
  1499. }
  1500. else
  1501. {
  1502. return "session";
  1503. }
  1504. }
  1505. //---------------------------------------------------------------------
  1506. AnsiString __fastcall TSessionData::GetSessionName()
  1507. {
  1508. if (!Name.IsEmpty() && !TNamedObjectList::IsHidden(this) &&
  1509. (Name != DefaultName))
  1510. {
  1511. return Name;
  1512. }
  1513. else
  1514. {
  1515. return DefaultSessionName;
  1516. }
  1517. }
  1518. //---------------------------------------------------------------------
  1519. AnsiString __fastcall TSessionData::GetSessionUrl()
  1520. {
  1521. AnsiString Url;
  1522. if (!Name.IsEmpty() && !TNamedObjectList::IsHidden(this) &&
  1523. (Name != DefaultName))
  1524. {
  1525. Url = Name;
  1526. }
  1527. else
  1528. {
  1529. switch (FSProtocol)
  1530. {
  1531. case fsSCPonly:
  1532. Url = "scp://";
  1533. break;
  1534. default:
  1535. assert(false);
  1536. // fallback
  1537. case fsSFTP:
  1538. case fsSFTPonly:
  1539. Url = "sftp://";
  1540. break;
  1541. case fsFTP:
  1542. Url = "ftp://";
  1543. break;
  1544. }
  1545. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  1546. {
  1547. Url += FORMAT("%s@%s", (UserName, HostName));
  1548. }
  1549. else if (!HostName.IsEmpty())
  1550. {
  1551. Url += HostName;
  1552. }
  1553. else
  1554. {
  1555. Url = "";
  1556. }
  1557. }
  1558. return Url;
  1559. }
  1560. //---------------------------------------------------------------------
  1561. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  1562. {
  1563. SET_SESSION_PROPERTY(TimeDifference);
  1564. }
  1565. //---------------------------------------------------------------------
  1566. void __fastcall TSessionData::SetLocalDirectory(AnsiString value)
  1567. {
  1568. SET_SESSION_PROPERTY(LocalDirectory);
  1569. }
  1570. //---------------------------------------------------------------------
  1571. void __fastcall TSessionData::SetRemoteDirectory(AnsiString value)
  1572. {
  1573. SET_SESSION_PROPERTY(RemoteDirectory);
  1574. }
  1575. //---------------------------------------------------------------------
  1576. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  1577. {
  1578. SET_SESSION_PROPERTY(UpdateDirectories);
  1579. }
  1580. //---------------------------------------------------------------------
  1581. void __fastcall TSessionData::SetCacheDirectories(bool value)
  1582. {
  1583. SET_SESSION_PROPERTY(CacheDirectories);
  1584. }
  1585. //---------------------------------------------------------------------
  1586. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  1587. {
  1588. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  1589. }
  1590. //---------------------------------------------------------------------
  1591. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  1592. {
  1593. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  1594. }
  1595. //---------------------------------------------------------------------
  1596. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  1597. {
  1598. SET_SESSION_PROPERTY(ResolveSymlinks);
  1599. }
  1600. //---------------------------------------------------------------------------
  1601. void __fastcall TSessionData::SetDSTMode(TDSTMode value)
  1602. {
  1603. SET_SESSION_PROPERTY(DSTMode);
  1604. }
  1605. //---------------------------------------------------------------------------
  1606. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  1607. {
  1608. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  1612. {
  1613. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  1614. }
  1615. //---------------------------------------------------------------------------
  1616. void __fastcall TSessionData::SetRecycleBinPath(AnsiString value)
  1617. {
  1618. SET_SESSION_PROPERTY(RecycleBinPath);
  1619. }
  1620. //---------------------------------------------------------------------------
  1621. void __fastcall TSessionData::SetPostLoginCommands(AnsiString value)
  1622. {
  1623. SET_SESSION_PROPERTY(PostLoginCommands);
  1624. }
  1625. //---------------------------------------------------------------------
  1626. void __fastcall TSessionData::SetLockInHome(bool value)
  1627. {
  1628. SET_SESSION_PROPERTY(LockInHome);
  1629. }
  1630. //---------------------------------------------------------------------
  1631. void __fastcall TSessionData::SetSpecial(bool value)
  1632. {
  1633. SET_SESSION_PROPERTY(Special);
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  1637. {
  1638. SET_SESSION_PROPERTY(Scp1Compatibility);
  1639. }
  1640. //---------------------------------------------------------------------
  1641. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  1642. {
  1643. SET_SESSION_PROPERTY(TcpNoDelay);
  1644. }
  1645. //---------------------------------------------------------------------
  1646. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  1647. {
  1648. SET_SESSION_PROPERTY(ProxyMethod);
  1649. }
  1650. //---------------------------------------------------------------------
  1651. void __fastcall TSessionData::SetProxyHost(AnsiString value)
  1652. {
  1653. SET_SESSION_PROPERTY(ProxyHost);
  1654. }
  1655. //---------------------------------------------------------------------
  1656. void __fastcall TSessionData::SetProxyPort(int value)
  1657. {
  1658. SET_SESSION_PROPERTY(ProxyPort);
  1659. }
  1660. //---------------------------------------------------------------------
  1661. void __fastcall TSessionData::SetProxyUsername(AnsiString value)
  1662. {
  1663. SET_SESSION_PROPERTY(ProxyUsername);
  1664. }
  1665. //---------------------------------------------------------------------
  1666. void __fastcall TSessionData::SetProxyPassword(AnsiString value)
  1667. {
  1668. value = EncryptPassword(value, ProxyUsername+ProxyHost);
  1669. SET_SESSION_PROPERTY(ProxyPassword);
  1670. }
  1671. //---------------------------------------------------------------------
  1672. AnsiString __fastcall TSessionData::GetProxyPassword() const
  1673. {
  1674. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  1675. }
  1676. //---------------------------------------------------------------------
  1677. void __fastcall TSessionData::SetProxyTelnetCommand(AnsiString value)
  1678. {
  1679. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  1680. }
  1681. //---------------------------------------------------------------------
  1682. void __fastcall TSessionData::SetProxyLocalCommand(AnsiString value)
  1683. {
  1684. SET_SESSION_PROPERTY(ProxyLocalCommand);
  1685. }
  1686. //---------------------------------------------------------------------
  1687. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  1688. {
  1689. SET_SESSION_PROPERTY(ProxyDNS);
  1690. }
  1691. //---------------------------------------------------------------------
  1692. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  1693. {
  1694. SET_SESSION_PROPERTY(ProxyLocalhost);
  1695. }
  1696. //---------------------------------------------------------------------
  1697. void __fastcall TSessionData::SetFtpProxyLogonType(int value)
  1698. {
  1699. SET_SESSION_PROPERTY(FtpProxyLogonType);
  1700. }
  1701. //---------------------------------------------------------------------
  1702. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  1703. {
  1704. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1705. SET_SESSION_PROPERTY(Bugs[Bug]);
  1706. }
  1707. //---------------------------------------------------------------------
  1708. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  1709. {
  1710. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1711. return FBugs[Bug];
  1712. }
  1713. //---------------------------------------------------------------------
  1714. void __fastcall TSessionData::SetCustomParam1(AnsiString value)
  1715. {
  1716. SET_SESSION_PROPERTY(CustomParam1);
  1717. }
  1718. //---------------------------------------------------------------------
  1719. void __fastcall TSessionData::SetCustomParam2(AnsiString value)
  1720. {
  1721. SET_SESSION_PROPERTY(CustomParam2);
  1722. }
  1723. //---------------------------------------------------------------------
  1724. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  1725. {
  1726. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  1727. }
  1728. //---------------------------------------------------------------------
  1729. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  1730. {
  1731. SET_SESSION_PROPERTY(SFTPUploadQueue);
  1732. }
  1733. //---------------------------------------------------------------------
  1734. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  1735. {
  1736. SET_SESSION_PROPERTY(SFTPListingQueue);
  1737. }
  1738. //---------------------------------------------------------------------
  1739. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  1740. {
  1741. SET_SESSION_PROPERTY(SFTPMaxVersion);
  1742. }
  1743. //---------------------------------------------------------------------
  1744. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  1745. {
  1746. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  1747. }
  1748. //---------------------------------------------------------------------
  1749. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  1750. {
  1751. assert(Bug >= 0 && Bug < LENOF(FSFTPBugs));
  1752. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  1753. }
  1754. //---------------------------------------------------------------------
  1755. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  1756. {
  1757. assert(Bug >= 0 && Bug < LENOF(FSFTPBugs));
  1758. return FSFTPBugs[Bug];
  1759. }
  1760. //---------------------------------------------------------------------
  1761. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  1762. {
  1763. SET_SESSION_PROPERTY(SCPLsFullTime);
  1764. }
  1765. //---------------------------------------------------------------------
  1766. void __fastcall TSessionData::SetFtpListAll(TAutoSwitch value)
  1767. {
  1768. SET_SESSION_PROPERTY(FtpListAll);
  1769. }
  1770. //---------------------------------------------------------------------------
  1771. void __fastcall TSessionData::SetColor(int value)
  1772. {
  1773. SET_SESSION_PROPERTY(Color);
  1774. }
  1775. //---------------------------------------------------------------------------
  1776. void __fastcall TSessionData::SetTunnel(bool value)
  1777. {
  1778. SET_SESSION_PROPERTY(Tunnel);
  1779. }
  1780. //---------------------------------------------------------------------
  1781. void __fastcall TSessionData::SetTunnelHostName(AnsiString value)
  1782. {
  1783. if (FTunnelHostName != value)
  1784. {
  1785. // HostName is key for password encryption
  1786. AnsiString XTunnelPassword = TunnelPassword;
  1787. int P = value.LastDelimiter("@");
  1788. if (P > 0)
  1789. {
  1790. TunnelUserName = value.SubString(1, P - 1);
  1791. value = value.SubString(P + 1, value.Length() - P);
  1792. }
  1793. FTunnelHostName = value;
  1794. FModified = true;
  1795. TunnelPassword = XTunnelPassword;
  1796. if (!XTunnelPassword.IsEmpty())
  1797. {
  1798. XTunnelPassword.Unique();
  1799. memset(XTunnelPassword.c_str(), 0, XTunnelPassword.Length());
  1800. }
  1801. }
  1802. }
  1803. //---------------------------------------------------------------------
  1804. void __fastcall TSessionData::SetTunnelPortNumber(int value)
  1805. {
  1806. SET_SESSION_PROPERTY(TunnelPortNumber);
  1807. }
  1808. //---------------------------------------------------------------------
  1809. void __fastcall TSessionData::SetTunnelUserName(AnsiString value)
  1810. {
  1811. // TunnelUserName is key for password encryption
  1812. AnsiString XTunnelPassword = TunnelPassword;
  1813. SET_SESSION_PROPERTY(TunnelUserName);
  1814. TunnelPassword = XTunnelPassword;
  1815. if (!XTunnelPassword.IsEmpty())
  1816. {
  1817. XTunnelPassword.Unique();
  1818. memset(XTunnelPassword.c_str(), 0, XTunnelPassword.Length());
  1819. }
  1820. }
  1821. //---------------------------------------------------------------------
  1822. void __fastcall TSessionData::SetTunnelPassword(AnsiString value)
  1823. {
  1824. value = EncryptPassword(value, TunnelUserName+TunnelHostName);
  1825. SET_SESSION_PROPERTY(TunnelPassword);
  1826. }
  1827. //---------------------------------------------------------------------
  1828. AnsiString __fastcall TSessionData::GetTunnelPassword()
  1829. {
  1830. return DecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName);
  1831. }
  1832. //---------------------------------------------------------------------
  1833. void __fastcall TSessionData::SetTunnelPublicKeyFile(AnsiString value)
  1834. {
  1835. if (FTunnelPublicKeyFile != value)
  1836. {
  1837. FTunnelPublicKeyFile = StripPathQuotes(value);
  1838. FModified = true;
  1839. }
  1840. }
  1841. //---------------------------------------------------------------------
  1842. void __fastcall TSessionData::SetTunnelLocalPortNumber(int value)
  1843. {
  1844. SET_SESSION_PROPERTY(TunnelLocalPortNumber);
  1845. }
  1846. //---------------------------------------------------------------------
  1847. bool __fastcall TSessionData::GetTunnelAutoassignLocalPortNumber()
  1848. {
  1849. return (FTunnelLocalPortNumber <= 0);
  1850. }
  1851. //---------------------------------------------------------------------
  1852. void __fastcall TSessionData::SetTunnelPortFwd(AnsiString value)
  1853. {
  1854. SET_SESSION_PROPERTY(TunnelPortFwd);
  1855. }
  1856. //---------------------------------------------------------------------
  1857. void __fastcall TSessionData::SetFtpPasvMode(bool value)
  1858. {
  1859. SET_SESSION_PROPERTY(FtpPasvMode);
  1860. }
  1861. //---------------------------------------------------------------------
  1862. void __fastcall TSessionData::SetFtpForcePasvIp(bool value)
  1863. {
  1864. SET_SESSION_PROPERTY(FtpForcePasvIp);
  1865. }
  1866. //---------------------------------------------------------------------
  1867. void __fastcall TSessionData::SetFtpAccount(AnsiString value)
  1868. {
  1869. SET_SESSION_PROPERTY(FtpAccount);
  1870. }
  1871. //---------------------------------------------------------------------
  1872. void __fastcall TSessionData::SetFtpPingInterval(int value)
  1873. {
  1874. SET_SESSION_PROPERTY(FtpPingInterval);
  1875. }
  1876. //---------------------------------------------------------------------------
  1877. TDateTime __fastcall TSessionData::GetFtpPingIntervalDT()
  1878. {
  1879. return SecToDateTime(FtpPingInterval);
  1880. }
  1881. //---------------------------------------------------------------------------
  1882. void __fastcall TSessionData::SetFtpPingType(TPingType value)
  1883. {
  1884. SET_SESSION_PROPERTY(FtpPingType);
  1885. }
  1886. //---------------------------------------------------------------------------
  1887. void __fastcall TSessionData::SetFtps(TFtps value)
  1888. {
  1889. SET_SESSION_PROPERTY(Ftps);
  1890. }
  1891. //---------------------------------------------------------------------
  1892. void __fastcall TSessionData::SetNotUtf(TAutoSwitch value)
  1893. {
  1894. SET_SESSION_PROPERTY(NotUtf);
  1895. }
  1896. //---------------------------------------------------------------------
  1897. void __fastcall TSessionData::SetHostKey(AnsiString value)
  1898. {
  1899. SET_SESSION_PROPERTY(HostKey);
  1900. }
  1901. //---------------------------------------------------------------------
  1902. AnsiString __fastcall TSessionData::GetInfoTip()
  1903. {
  1904. if (UsesSsh)
  1905. {
  1906. return FMTLOAD(SESSION_INFO_TIP,
  1907. (HostName, UserName,
  1908. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  1909. SshProtStr, FSProtocolStr));
  1910. }
  1911. else
  1912. {
  1913. return FMTLOAD(SESSION_INFO_TIP_NO_SSH,
  1914. (HostName, UserName, FSProtocolStr));
  1915. }
  1916. }
  1917. //---------------------------------------------------------------------
  1918. AnsiString __fastcall TSessionData::GetLocalName()
  1919. {
  1920. AnsiString Result = Name;
  1921. int P = Result.LastDelimiter("/");
  1922. if (P > 0)
  1923. {
  1924. Result.Delete(1, P);
  1925. }
  1926. return Result;
  1927. }
  1928. //=== TStoredSessionList ----------------------------------------------
  1929. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  1930. TNamedObjectList(), FReadOnly(aReadOnly)
  1931. {
  1932. assert(Configuration);
  1933. FDefaultSettings = new TSessionData(DefaultName);
  1934. }
  1935. //---------------------------------------------------------------------
  1936. __fastcall TStoredSessionList::~TStoredSessionList()
  1937. {
  1938. assert(Configuration);
  1939. delete FDefaultSettings;
  1940. }
  1941. //---------------------------------------------------------------------
  1942. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  1943. bool AsModified, bool UseDefaults)
  1944. {
  1945. TStringList *SubKeys = new TStringList();
  1946. TList * Loaded = new TList;
  1947. try
  1948. {
  1949. Storage->GetSubKeyNames(SubKeys);
  1950. for (int Index = 0; Index < SubKeys->Count; Index++)
  1951. {
  1952. TSessionData *SessionData;
  1953. AnsiString SessionName = SubKeys->Strings[Index];
  1954. bool ValidName = true;
  1955. try
  1956. {
  1957. TSessionData::ValidatePath(SessionName);
  1958. }
  1959. catch(...)
  1960. {
  1961. ValidName = false;
  1962. }
  1963. if (ValidName)
  1964. {
  1965. if (SessionName == FDefaultSettings->Name) SessionData = FDefaultSettings;
  1966. else SessionData = (TSessionData*)FindByName(SessionName);
  1967. if ((SessionData != FDefaultSettings) || !UseDefaults)
  1968. {
  1969. if (!SessionData)
  1970. {
  1971. SessionData = new TSessionData("");
  1972. if (UseDefaults)
  1973. {
  1974. SessionData->Assign(DefaultSettings);
  1975. }
  1976. SessionData->Name = SessionName;
  1977. Add(SessionData);
  1978. }
  1979. Loaded->Add(SessionData);
  1980. SessionData->Load(Storage);
  1981. if (AsModified)
  1982. {
  1983. SessionData->Modified = true;
  1984. }
  1985. }
  1986. }
  1987. }
  1988. if (!AsModified)
  1989. {
  1990. for (int Index = 0; Index < TObjectList::Count; Index++)
  1991. {
  1992. if (Loaded->IndexOf(Items[Index]) < 0)
  1993. {
  1994. Delete(Index);
  1995. Index--;
  1996. }
  1997. }
  1998. }
  1999. }
  2000. __finally
  2001. {
  2002. delete SubKeys;
  2003. delete Loaded;
  2004. }
  2005. }
  2006. //---------------------------------------------------------------------
  2007. void __fastcall TStoredSessionList::Load(AnsiString aKey, bool UseDefaults)
  2008. {
  2009. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  2010. try {
  2011. if (Storage->OpenRootKey(False)) Load(Storage, false, UseDefaults);
  2012. } __finally {
  2013. delete Storage;
  2014. }
  2015. }
  2016. //---------------------------------------------------------------------
  2017. void __fastcall TStoredSessionList::Load()
  2018. {
  2019. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  2020. try {
  2021. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  2022. Load(Storage);
  2023. } __finally {
  2024. delete Storage;
  2025. }
  2026. }
  2027. //---------------------------------------------------------------------
  2028. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  2029. TSessionData * Data, bool All, bool RecryptPasswordOnly,
  2030. TSessionData * FactoryDefaults)
  2031. {
  2032. if (All || Data->Modified)
  2033. {
  2034. if (RecryptPasswordOnly)
  2035. {
  2036. Data->SaveRecryptedPasswords(Storage);
  2037. }
  2038. else
  2039. {
  2040. Data->Save(Storage, false, FactoryDefaults);
  2041. }
  2042. }
  2043. }
  2044. //---------------------------------------------------------------------
  2045. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  2046. bool All, bool RecryptPasswordOnly)
  2047. {
  2048. TSessionData * FactoryDefaults = new TSessionData("");
  2049. try
  2050. {
  2051. DoSave(Storage, FDefaultSettings, All, RecryptPasswordOnly, FactoryDefaults);
  2052. for (int Index = 0; Index < Count+HiddenCount; Index++)
  2053. {
  2054. TSessionData * SessionData = (TSessionData *)Items[Index];
  2055. DoSave(Storage, SessionData, All, RecryptPasswordOnly, FactoryDefaults);
  2056. }
  2057. }
  2058. __finally
  2059. {
  2060. delete FactoryDefaults;
  2061. }
  2062. }
  2063. //---------------------------------------------------------------------
  2064. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage, bool All)
  2065. {
  2066. DoSave(Storage, All, false);
  2067. }
  2068. //---------------------------------------------------------------------
  2069. void __fastcall TStoredSessionList::DoSave(bool All, bool Explicit, bool RecryptPasswordOnly)
  2070. {
  2071. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  2072. try
  2073. {
  2074. Storage->AccessMode = smReadWrite;
  2075. Storage->Explicit = Explicit;
  2076. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  2077. {
  2078. DoSave(Storage, All, RecryptPasswordOnly);
  2079. }
  2080. }
  2081. __finally
  2082. {
  2083. delete Storage;
  2084. }
  2085. Saved();
  2086. }
  2087. //---------------------------------------------------------------------
  2088. void __fastcall TStoredSessionList::Save(bool All, bool Explicit)
  2089. {
  2090. DoSave(All, Explicit, false);
  2091. }
  2092. //---------------------------------------------------------------------
  2093. void __fastcall TStoredSessionList::RecryptPasswords()
  2094. {
  2095. DoSave(true, true, true);
  2096. }
  2097. //---------------------------------------------------------------------
  2098. void __fastcall TStoredSessionList::Saved()
  2099. {
  2100. FDefaultSettings->Modified = false;
  2101. for (int Index = 0; Index < Count + HiddenCount; Index++)
  2102. {
  2103. ((TSessionData *)Items[Index])->Modified = false;
  2104. }
  2105. }
  2106. //---------------------------------------------------------------------
  2107. void __fastcall TStoredSessionList::Export(const AnsiString FileName)
  2108. {
  2109. THierarchicalStorage * Storage = new TIniFileStorage(FileName);
  2110. try
  2111. {
  2112. Storage->AccessMode = smReadWrite;
  2113. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  2114. {
  2115. Save(Storage, true);
  2116. }
  2117. }
  2118. __finally
  2119. {
  2120. delete Storage;
  2121. }
  2122. }
  2123. //---------------------------------------------------------------------
  2124. void __fastcall TStoredSessionList::SelectAll(bool Select)
  2125. {
  2126. for (int Index = 0; Index < Count; Index++)
  2127. Sessions[Index]->Selected = Select;
  2128. }
  2129. //---------------------------------------------------------------------
  2130. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  2131. bool OnlySelected)
  2132. {
  2133. for (int Index = 0; Index < From->Count; Index++)
  2134. {
  2135. if (!OnlySelected || From->Sessions[Index]->Selected)
  2136. {
  2137. TSessionData *Session = new TSessionData("");
  2138. Session->Assign(From->Sessions[Index]);
  2139. Session->Modified = true;
  2140. Session->MakeUniqueIn(this);
  2141. Add(Session);
  2142. }
  2143. }
  2144. // only modified, explicit
  2145. Save(false, true);
  2146. }
  2147. //---------------------------------------------------------------------
  2148. void __fastcall TStoredSessionList::SelectSessionsToImport
  2149. (TStoredSessionList * Dest, bool SSHOnly)
  2150. {
  2151. for (int Index = 0; Index < Count; Index++)
  2152. {
  2153. Sessions[Index]->Selected =
  2154. (!SSHOnly || (Sessions[Index]->Protocol == ptSSH)) &&
  2155. !Dest->FindByName(Sessions[Index]->Name);
  2156. }
  2157. }
  2158. //---------------------------------------------------------------------
  2159. void __fastcall TStoredSessionList::Cleanup()
  2160. {
  2161. try {
  2162. if (Configuration->Storage == stRegistry) Clear();
  2163. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  2164. try {
  2165. Storage->AccessMode = smReadWrite;
  2166. if (Storage->OpenRootKey(False))
  2167. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  2168. } __finally {
  2169. delete Storage;
  2170. }
  2171. } catch (Exception &E) {
  2172. throw ExtException(&E, CLEANUP_SESSIONS_ERROR);
  2173. }
  2174. }
  2175. //---------------------------------------------------------------------------
  2176. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  2177. {
  2178. for (int Index = 0; Index < Count; Index++)
  2179. if (Data == Sessions[Index]) return Index;
  2180. return -1;
  2181. }
  2182. //---------------------------------------------------------------------------
  2183. TSessionData * __fastcall TStoredSessionList::NewSession(
  2184. AnsiString SessionName, TSessionData * Session)
  2185. {
  2186. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  2187. if (!DuplicateSession)
  2188. {
  2189. DuplicateSession = new TSessionData("");
  2190. DuplicateSession->Assign(Session);
  2191. DuplicateSession->Name = SessionName;
  2192. // make sure, that new stored session is saved to registry
  2193. DuplicateSession->Modified = true;
  2194. Add(DuplicateSession);
  2195. }
  2196. else
  2197. {
  2198. DuplicateSession->Assign(Session);
  2199. DuplicateSession->Name = SessionName;
  2200. DuplicateSession->Modified = true;
  2201. }
  2202. // list was saved here before to default storage, but it would not allow
  2203. // to work with special lists (export/import) not using default storage
  2204. return DuplicateSession;
  2205. }
  2206. //---------------------------------------------------------------------------
  2207. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  2208. {
  2209. assert(FDefaultSettings);
  2210. if (FDefaultSettings != value)
  2211. {
  2212. FDefaultSettings->Assign(value);
  2213. FDefaultSettings->Name = DefaultName;
  2214. if (!FReadOnly)
  2215. {
  2216. // only modified, explicit
  2217. Save(false, true);
  2218. }
  2219. }
  2220. }
  2221. //---------------------------------------------------------------------------
  2222. void __fastcall TStoredSessionList::ImportHostKeys(const AnsiString TargetKey,
  2223. const AnsiString SourceKey, TStoredSessionList * Sessions,
  2224. bool OnlySelected)
  2225. {
  2226. TRegistryStorage * SourceStorage = NULL;
  2227. TRegistryStorage * TargetStorage = NULL;
  2228. TStringList * KeyList = NULL;
  2229. try
  2230. {
  2231. SourceStorage = new TRegistryStorage(SourceKey);
  2232. TargetStorage = new TRegistryStorage(TargetKey);
  2233. TargetStorage->AccessMode = smReadWrite;
  2234. KeyList = new TStringList();
  2235. if (SourceStorage->OpenRootKey(false) &&
  2236. TargetStorage->OpenRootKey(true))
  2237. {
  2238. SourceStorage->GetValueNames(KeyList);
  2239. TSessionData * Session;
  2240. AnsiString HostKeyName;
  2241. assert(Sessions != NULL);
  2242. for (int Index = 0; Index < Sessions->Count; Index++)
  2243. {
  2244. Session = Sessions->Sessions[Index];
  2245. if (!OnlySelected || Session->Selected)
  2246. {
  2247. HostKeyName = PuttyMungeStr(FORMAT("@%d:%s", (Session->PortNumber, Session->HostName)));
  2248. AnsiString KeyName;
  2249. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  2250. {
  2251. KeyName = KeyList->Strings[KeyIndex];
  2252. int P = KeyName.Pos(HostKeyName);
  2253. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  2254. {
  2255. TargetStorage->WriteStringRaw(KeyName,
  2256. SourceStorage->ReadStringRaw(KeyName, ""));
  2257. }
  2258. }
  2259. }
  2260. }
  2261. }
  2262. }
  2263. __finally
  2264. {
  2265. delete SourceStorage;
  2266. delete TargetStorage;
  2267. delete KeyList;
  2268. }
  2269. }
  2270. //---------------------------------------------------------------------------
  2271. TSessionData * __fastcall TStoredSessionList::ParseUrl(AnsiString Url,
  2272. TOptions * Options, bool & DefaultsOnly, AnsiString * FileName,
  2273. bool * AProtocolDefined)
  2274. {
  2275. TSessionData * Data = new TSessionData("");
  2276. try
  2277. {
  2278. Data->ParseUrl(Url, Options, this, DefaultsOnly, FileName, AProtocolDefined);
  2279. }
  2280. catch(...)
  2281. {
  2282. delete Data;
  2283. throw;
  2284. }
  2285. return Data;
  2286. }