SessionData.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "SessionData.h"
  5. #include "Common.h"
  6. #include "Configuration.h"
  7. #include "Exceptions.h"
  8. #include "FileBuffer.h"
  9. #include "ScpMain.h"
  10. #include "Security.h"
  11. #include "TextsCore.h"
  12. #include "PuttyIntf.h"
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. //---------------------------------------------------------------------------
  16. const char * DefaultSessionName = "Default Settings";
  17. const char CipherNames[CIPHER_COUNT][10] = {"WARN", "3des", "blowfish", "aes", "des"};
  18. const char SshProtList[][10] = {"1 only", "1", "2", "2 only"};
  19. const char ProxyMethodList[][10] = {"none", "SOCKS4", "SOCKS5", "HTTP", "Telnet", "Cmd" };
  20. const TCipher DefaultCipherList[CIPHER_COUNT] =
  21. { cipAES, cipBlowfish, cip3DES, cipWarn, cipDES };
  22. const char FSProtocolNames[FSPROTOCOL_COUNT][11] = { "SCP", "SFTP (SCP)", "SFTP" };
  23. //--- TSessionData ----------------------------------------------------
  24. __fastcall TSessionData::TSessionData(AnsiString aName):
  25. TNamedObject(aName)
  26. {
  27. Default();
  28. FModified = true;
  29. }
  30. //---------------------------------------------------------------------
  31. void __fastcall TSessionData::Default()
  32. {
  33. HostName = "";
  34. PortNumber = default_port;
  35. UserName = "";
  36. Password = "";
  37. PingInterval = 30;
  38. PingType = ptOff;
  39. Timeout = 15;
  40. AgentFwd = false;
  41. AuthTIS = false;
  42. AuthKI = true;
  43. AuthKIPassword = true;
  44. AuthGSSAPI = false;
  45. Compression = false;
  46. SshProt = ssh2;
  47. Ssh2DES = false;
  48. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  49. {
  50. Cipher[Index] = DefaultCipherList[Index];
  51. }
  52. PublicKeyFile = "";
  53. FProtocol = ptSSH;
  54. TcpNoDelay = true;
  55. ProxyMethod = pmNone;
  56. ProxyHost = "proxy";
  57. ProxyPort = 80;
  58. ProxyUsername = "";
  59. ProxyPassword = "";
  60. ProxyTelnetCommand = "connect %host %port\\n";
  61. //ProxySOCKSVersion = 5;
  62. ProxyDNS = asAuto;
  63. ProxyLocalhost = false;
  64. for (int Index = 0; Index < LENOF(FBugs); Index++)
  65. {
  66. Bug[(TSshBug)Index] = asAuto;
  67. }
  68. Special = false;
  69. FSProtocol = fsSFTP;
  70. // FS common
  71. LocalDirectory = "";
  72. RemoteDirectory = "";
  73. UpdateDirectories = false;
  74. CacheDirectories = true;
  75. CacheDirectoryChanges = true;
  76. PreserveDirectoryChanges = true;
  77. LockInHome = false;
  78. ResolveSymlinks = true;
  79. ConsiderDST = false;
  80. // SCP
  81. ReturnVar = "";
  82. LookupUserGroups = true;
  83. EOLType = eolLF;
  84. Shell = ""; //default shell
  85. ReturnVar = "";
  86. ClearAliases = true;
  87. UnsetNationalVars = true;
  88. AliasGroupList = false;
  89. IgnoreLsWarnings = true;
  90. Scp1Compatibility = false;
  91. TimeDifference = 0;
  92. // SFTP
  93. SFTPDownloadQueue = 4;
  94. SFTPUploadQueue = 4;
  95. SFTPListingQueue = 2;
  96. SFTPSymlinkBug = asAuto;
  97. CustomParam1 = "";
  98. CustomParam2 = "";
  99. Selected = false;
  100. FModified = false;
  101. // add also to TSessionLog::AddStartupInfo()
  102. }
  103. //---------------------------------------------------------------------
  104. void __fastcall TSessionData::NonPersistant()
  105. {
  106. UpdateDirectories = false;
  107. PreserveDirectoryChanges = false;
  108. }
  109. //---------------------------------------------------------------------
  110. void __fastcall TSessionData::Assign(TPersistent * Source)
  111. {
  112. if (Source && Source->InheritsFrom(__classid(TSessionData)))
  113. {
  114. #define DUPL(P) P = ((TSessionData *)Source)->P
  115. DUPL(Name);
  116. DUPL(HostName);
  117. DUPL(PortNumber);
  118. DUPL(UserName);
  119. DUPL(Password);
  120. DUPL(PingInterval);
  121. DUPL(PingType);
  122. DUPL(Timeout);
  123. DUPL(AgentFwd);
  124. DUPL(AuthTIS);
  125. DUPL(Compression);
  126. DUPL(SshProt);
  127. DUPL(Ssh2DES);
  128. DUPL(CipherList);
  129. DUPL(PublicKeyFile);
  130. DUPL(FSProtocol);
  131. DUPL(LocalDirectory);
  132. DUPL(RemoteDirectory);
  133. DUPL(UpdateDirectories);
  134. DUPL(CacheDirectories);
  135. DUPL(CacheDirectoryChanges);
  136. DUPL(PreserveDirectoryChanges);
  137. DUPL(ResolveSymlinks);
  138. DUPL(ConsiderDST);
  139. DUPL(LockInHome);
  140. DUPL(Special);
  141. DUPL(Selected);
  142. DUPL(ReturnVar);
  143. DUPL(LookupUserGroups);
  144. DUPL(EOLType);
  145. DUPL(Shell);
  146. DUPL(ClearAliases);
  147. DUPL(Scp1Compatibility);
  148. DUPL(UnsetNationalVars);
  149. DUPL(AliasGroupList);
  150. DUPL(IgnoreLsWarnings);
  151. DUPL(TimeDifference);
  152. // new in 53b
  153. DUPL(TcpNoDelay);
  154. DUPL(AuthKI);
  155. DUPL(AuthKIPassword);
  156. DUPL(AuthGSSAPI);
  157. DUPL(ProxyMethod);
  158. DUPL(ProxyHost);
  159. DUPL(ProxyPort);
  160. DUPL(ProxyUsername);
  161. DUPL(ProxyPassword);
  162. DUPL(ProxyTelnetCommand);
  163. DUPL(ProxyDNS);
  164. DUPL(ProxyLocalhost);
  165. for (int Index = 0; Index < LENOF(FBugs); Index++)
  166. {
  167. DUPL(Bug[(TSshBug)Index]);
  168. }
  169. // SFTP
  170. DUPL(SFTPDownloadQueue);
  171. DUPL(SFTPUploadQueue);
  172. DUPL(SFTPListingQueue);
  173. DUPL(SFTPSymlinkBug);
  174. DUPL(CustomParam1);
  175. DUPL(CustomParam2);
  176. #undef DUPL
  177. FModified = ((TSessionData *)Source)->Modified;
  178. }
  179. else
  180. {
  181. TNamedObject::Assign(Source);
  182. }
  183. }
  184. //---------------------------------------------------------------------
  185. void __fastcall TSessionData::StoreToConfig(void * config)
  186. {
  187. Config * cfg = (Config *)config;
  188. // clear all (parameters not set below)
  189. memset(cfg, 0, sizeof(*cfg));
  190. // user-configurable settings
  191. ASCOPY(cfg->host, HostName);
  192. ASCOPY(cfg->username, UserName);
  193. cfg->port = PortNumber;
  194. cfg->protocol = PROT_SSH;
  195. // ping_interval is not used anyway
  196. cfg->ping_interval = (PingType == ptNullPacket) ? PingInterval : 0;
  197. cfg->compression = Compression;
  198. cfg->agentfwd = AgentFwd;
  199. for (int c = 0; c < CIPHER_COUNT; c++)
  200. {
  201. int pcipher;
  202. switch (Cipher[c]) {
  203. case cipWarn: pcipher = CIPHER_WARN; break;
  204. case cip3DES: pcipher = CIPHER_3DES; break;
  205. case cipBlowfish: pcipher = CIPHER_BLOWFISH; break;
  206. case cipAES: pcipher = CIPHER_AES; break;
  207. case cipDES: pcipher = CIPHER_DES; break;
  208. default: assert(false);
  209. }
  210. cfg->ssh_cipherlist[c] = pcipher;
  211. }
  212. AnsiString SPublicKeyFile = PublicKeyFile;
  213. if (SPublicKeyFile.IsEmpty()) SPublicKeyFile = Configuration->DefaultKeyFile;
  214. SPublicKeyFile = StripPathQuotes(SPublicKeyFile);
  215. ASCOPY(cfg->keyfile.path, SPublicKeyFile);
  216. cfg->sshprot = SshProt;
  217. cfg->ssh2_des_cbc = Ssh2DES;
  218. cfg->try_tis_auth = AuthTIS;
  219. cfg->try_ki_auth = AuthKI;
  220. cfg->try_gssapi_auth = AuthGSSAPI;
  221. cfg->proxy_type = ProxyMethod;
  222. ASCOPY(cfg->proxy_host, ProxyHost);
  223. cfg->proxy_port = ProxyPort;
  224. ASCOPY(cfg->proxy_username, ProxyUsername);
  225. ASCOPY(cfg->proxy_password, ProxyPassword);
  226. ASCOPY(cfg->proxy_telnet_command, ProxyTelnetCommand);
  227. cfg->proxy_dns = ProxyDNS;
  228. cfg->even_proxy_localhost = ProxyLocalhost;
  229. #pragma option push -w-eas
  230. // after 0.53b values were reversed, however putty still stores
  231. // settings to registry in save way as before
  232. cfg->sshbug_ignore1 = Bug[sbIgnore1];
  233. cfg->sshbug_plainpw1 = Bug[sbPlainPW1];
  234. cfg->sshbug_rsa1 = Bug[sbRSA1];
  235. cfg->sshbug_hmac2 = Bug[sbHMAC2];
  236. cfg->sshbug_derivekey2 = Bug[sbDeriveKey2];
  237. cfg->sshbug_rsapad2 = Bug[sbRSAPad2];
  238. cfg->sshbug_dhgex2 = Bug[sbDHGEx2];
  239. // new after 0.53b
  240. cfg->sshbug_pksessid2 = Bug[sbPKSessID2];
  241. #pragma option pop
  242. if (FSProtocol == fsSCPonly)
  243. {
  244. cfg->ssh_subsys = FALSE;
  245. if (Shell.IsEmpty())
  246. {
  247. // Following forces Putty to open default shell
  248. // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
  249. cfg->remote_cmd[0] = '\0';
  250. }
  251. else
  252. {
  253. ASCOPY(cfg->remote_cmd, Shell);
  254. }
  255. cfg->remote_cmd_ptr = &cfg->remote_cmd[0];
  256. cfg->remote_cmd_ptr2 = NULL; // no second attempt for SCPonly
  257. }
  258. else
  259. {
  260. cfg->ssh_subsys = TRUE;
  261. strcpy(cfg->remote_cmd, "sftp");
  262. cfg->remote_cmd_ptr = &cfg->remote_cmd[0];
  263. if (FSProtocol != fsSFTPonly)
  264. {
  265. cfg->ssh_subsys2 = FALSE;
  266. if (Shell.IsEmpty())
  267. {
  268. cfg->remote_cmd2[0] = '\0';
  269. }
  270. else
  271. {
  272. ASCOPY(cfg->remote_cmd2, Shell);
  273. }
  274. cfg->remote_cmd_ptr2 = &cfg->remote_cmd2[0];
  275. }
  276. else
  277. {
  278. // see psftp_connect() from psftp.c
  279. cfg->ssh_subsys2 = FALSE;
  280. cfg->remote_cmd_ptr2 =
  281. "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n"
  282. "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n"
  283. "exec sftp-server";
  284. }
  285. }
  286. // permanent settings
  287. cfg->nopty = TRUE;
  288. }
  289. //---------------------------------------------------------------------
  290. void __fastcall TSessionData::Load(THierarchicalStorage * Storage)
  291. {
  292. if (Storage->OpenSubKey(StorageKey, False))
  293. {
  294. PortNumber = Storage->ReadInteger("PortNumber", PortNumber);
  295. UserName = Storage->ReadString("UserName", UserName);
  296. // must be loaded after UserName, because HostName may be in format user@host
  297. HostName = Storage->ReadString("HostName", HostName);
  298. if (!Configuration->DisablePasswordStoring)
  299. {
  300. FPassword = Storage->ReadString("Password", FPassword);
  301. }
  302. PingInterval =
  303. Storage->ReadInteger("PingInterval", PingInterval/60)*60 +
  304. Storage->ReadInteger("PingIntervalSec", PingInterval%60);
  305. PingType = static_cast<TPingType>
  306. (Storage->ReadInteger("PingType", PingInterval > 0 ? ptNullPacket : ptOff));
  307. if (PingInterval == 0)
  308. {
  309. PingInterval = 60;
  310. }
  311. Timeout = Storage->ReadInteger("Timeout", Timeout);
  312. AgentFwd = Storage->ReadBool("AgentFwd", AgentFwd);
  313. AuthTIS = Storage->ReadBool("AuthTIS", AuthTIS);
  314. AuthKI = Storage->ReadBool("AuthKI", AuthKI);
  315. AuthKIPassword = Storage->ReadBool("AuthKIPassword", AuthKIPassword);
  316. AuthGSSAPI = Storage->ReadBool("AuthGSSAPI", AuthGSSAPI);
  317. Compression = Storage->ReadBool("Compression", Compression);
  318. SshProt = (TSshProt)Storage->ReadInteger("SshProt", SshProt);
  319. Ssh2DES = Storage->ReadBool("Ssh2DES", Ssh2DES);
  320. CipherList = Storage->ReadString("Cipher", CipherList);
  321. PublicKeyFile = Storage->ReadString("PublicKeyFile", PublicKeyFile);
  322. FSProtocol = (TFSProtocol)Storage->ReadInteger("FSProtocol", FSProtocol);
  323. LocalDirectory = Storage->ReadString("LocalDirectory", LocalDirectory);
  324. RemoteDirectory = Storage->ReadString("RemoteDirectory", RemoteDirectory);
  325. UpdateDirectories = Storage->ReadBool("UpdateDirectories", UpdateDirectories);
  326. CacheDirectories = Storage->ReadBool("CacheDirectories", CacheDirectories);
  327. CacheDirectoryChanges = Storage->ReadBool("CacheDirectoryChanges", CacheDirectoryChanges);
  328. PreserveDirectoryChanges = Storage->ReadBool("PreserveDirectoryChanges", PreserveDirectoryChanges);
  329. ResolveSymlinks = Storage->ReadBool("ResolveSymlinks", ResolveSymlinks);
  330. ConsiderDST = Storage->ReadBool("ConsiderDST", ConsiderDST);
  331. LockInHome = Storage->ReadBool("LockInHome", LockInHome);
  332. Special = Storage->ReadBool("Special", Special);
  333. Shell = Storage->ReadString("Shell", Shell);
  334. ClearAliases = Storage->ReadBool("ClearAliases", ClearAliases);
  335. UnsetNationalVars = Storage->ReadBool("UnsetNationalVars", UnsetNationalVars);
  336. AliasGroupList = Storage->ReadBool("AliasGroupList", AliasGroupList);
  337. IgnoreLsWarnings = Storage->ReadBool("IgnoreLsWarnings", IgnoreLsWarnings);
  338. Scp1Compatibility = Storage->ReadBool("Scp1Compatibility", Scp1Compatibility);
  339. TimeDifference = Storage->ReadFloat("TimeDifference", TimeDifference);
  340. ReturnVar = Storage->ReadString("ReturnVar", ReturnVar);
  341. LookupUserGroups = Storage->ReadBool("LookupUserGroups", LookupUserGroups);
  342. EOLType = (TEOLType)Storage->ReadInteger("EOLType", EOLType);
  343. // new in 53b
  344. TcpNoDelay = Storage->ReadBool("TcpNoDelay", TcpNoDelay);
  345. ProxyMethod = (TProxyMethod)Storage->ReadInteger("ProxyMethod", -1);
  346. if (ProxyMethod < 0)
  347. {
  348. int ProxyType = Storage->ReadInteger("ProxyType", pxNone);
  349. int ProxySOCKSVersion;
  350. switch (ProxyType) {
  351. case pxHTTP:
  352. ProxyMethod = pmHTTP;
  353. break;
  354. case pxTelnet:
  355. ProxyMethod = pmTelnet;
  356. break;
  357. case pxSocks:
  358. ProxySOCKSVersion = Storage->ReadInteger("ProxySOCKSVersion", 5);
  359. ProxyMethod = ProxySOCKSVersion == 5 ? pmSocks5 : pmSocks4;
  360. break;
  361. default:
  362. case pxNone:
  363. ProxyMethod = pmNone;
  364. break;
  365. }
  366. }
  367. ProxyHost = Storage->ReadString("ProxyHost", ProxyHost);
  368. ProxyPort = Storage->ReadInteger("ProxyPort", ProxyPort);
  369. ProxyUsername = Storage->ReadString("ProxyUsername", ProxyUsername);
  370. FProxyPassword = Storage->ReadString("ProxyPassword", FProxyPassword);
  371. ProxyTelnetCommand = Storage->ReadStringRaw("ProxyTelnetCommand", ProxyTelnetCommand);
  372. ProxyDNS = TAutoSwitch((Storage->ReadInteger("ProxyDNS", ProxyDNS) + 1) % 3);
  373. ProxyLocalhost = Storage->ReadBool("ProxyLocalhost", ProxyLocalhost);
  374. #define READ_BUG(BUG) \
  375. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger("Bug"#BUG, \
  376. 2 - Bug[sb##BUG]));
  377. READ_BUG(Ignore1);
  378. READ_BUG(PlainPW1);
  379. READ_BUG(RSA1);
  380. READ_BUG(HMAC2);
  381. READ_BUG(DeriveKey2);
  382. READ_BUG(RSAPad2);
  383. READ_BUG(DHGEx2);
  384. READ_BUG(PKSessID2);
  385. #undef READ_BUG
  386. if ((Bug[sbHMAC2] == asAuto) &&
  387. Storage->ReadBool("BuggyMAC", false))
  388. {
  389. Bug[sbHMAC2] = asOn;
  390. }
  391. SFTPSymlinkBug = TAutoSwitch(Storage->ReadInteger("SFTPSymlinkBug", SFTPSymlinkBug));
  392. // read only (used only on Import from Putty dialog)
  393. ProtocolStr = Storage->ReadString("Protocol", ProtocolStr);
  394. CustomParam1 = Storage->ReadString("CustomParam1", CustomParam1);
  395. CustomParam2 = Storage->ReadString("CustomParam2", CustomParam2);
  396. Storage->CloseSubKey();
  397. };
  398. FModified = false;
  399. }
  400. //---------------------------------------------------------------------
  401. void __fastcall TSessionData::Save(THierarchicalStorage * Storage, bool PuttyExport)
  402. {
  403. if (Modified && Storage->OpenSubKey(StorageKey, true))
  404. {
  405. Storage->WriteString("HostName", HostName);
  406. Storage->WriteInteger("PortNumber", PortNumber);
  407. Storage->WriteString("UserName", UserName);
  408. if (!Configuration->DisablePasswordStoring && !PuttyExport)
  409. {
  410. Storage->WriteString("Password", FPassword);
  411. }
  412. Storage->WriteInteger("PingInterval", PingInterval/60);
  413. Storage->WriteInteger("PingIntervalSec", PingInterval%60);
  414. Storage->WriteInteger("PingType", PingType);
  415. Storage->WriteInteger("Timeout", Timeout);
  416. Storage->WriteBool("AgentFwd", AgentFwd);
  417. Storage->WriteBool("AuthTIS", AuthTIS);
  418. Storage->WriteBool("AuthKI", AuthKI);
  419. Storage->WriteBool("AuthKIPassword", AuthKIPassword);
  420. Storage->WriteBool("Compression", Compression);
  421. Storage->WriteInteger("SshProt", SshProt);
  422. Storage->WriteBool("Ssh2DES", Ssh2DES);
  423. Storage->WriteString("Cipher", CipherList);
  424. Storage->WriteBool("TcpNoDelay", TcpNoDelay);
  425. if (PuttyExport)
  426. {
  427. Storage->WriteStringRaw("PublicKeyFile", PublicKeyFile);
  428. }
  429. else
  430. {
  431. Storage->WriteBool("AuthGSSAPI", AuthGSSAPI);
  432. Storage->WriteString("PublicKeyFile", PublicKeyFile);
  433. Storage->WriteInteger("FSProtocol", FSProtocol);
  434. Storage->WriteString("LocalDirectory", LocalDirectory);
  435. Storage->WriteString("RemoteDirectory", RemoteDirectory);
  436. Storage->WriteBool("UpdateDirectories", UpdateDirectories);
  437. Storage->WriteBool("CacheDirectories", CacheDirectories);
  438. Storage->WriteBool("CacheDirectoryChanges", CacheDirectoryChanges);
  439. Storage->WriteBool("PreserveDirectoryChanges", PreserveDirectoryChanges);
  440. Storage->WriteBool("ResolveSymlinks", ResolveSymlinks);
  441. Storage->WriteBool("ConsiderDST", ConsiderDST);
  442. Storage->WriteBool("LockInHome", LockInHome);
  443. // Special is never stored (if it would, login dialog must be modified not to
  444. // duplicate Special parameter when Special session is loaded and then stored
  445. // under different name)
  446. // Storage->WriteBool("Special", Special);
  447. Storage->WriteString("Shell", Shell);
  448. Storage->WriteBool("ClearAliases", ClearAliases);
  449. Storage->WriteBool("UnsetNationalVars", UnsetNationalVars);
  450. Storage->WriteBool("AliasGroupList", AliasGroupList);
  451. Storage->WriteBool("IgnoreLsWarnings", IgnoreLsWarnings);
  452. Storage->WriteBool("Scp1Compatibility", Scp1Compatibility);
  453. Storage->WriteFloat("TimeDifference", TimeDifference);
  454. Storage->WriteString("ReturnVar", ReturnVar);
  455. Storage->WriteBool("LookupUserGroups", LookupUserGroups);
  456. Storage->WriteInteger("EOLType", EOLType);
  457. }
  458. Storage->WriteInteger("ProxyMethod", ProxyMethod);
  459. if (PuttyExport)
  460. {
  461. // support for Putty 0.53b and older
  462. int ProxyType;
  463. int ProxySOCKSVersion = 5;
  464. switch (ProxyMethod) {
  465. case pmHTTP:
  466. ProxyType = pxHTTP;
  467. break;
  468. case pmTelnet:
  469. ProxyType = pxTelnet;
  470. break;
  471. case pmSocks5:
  472. ProxyType = pxSocks;
  473. ProxySOCKSVersion = 5;
  474. break;
  475. case pmSocks4:
  476. ProxyType = pxSocks;
  477. ProxySOCKSVersion = 4;
  478. break;
  479. default:
  480. case pmNone:
  481. ProxyType = pxNone;
  482. break;
  483. }
  484. Storage->WriteInteger("ProxyType", ProxyType);
  485. Storage->WriteInteger("ProxySOCKSVersion", ProxySOCKSVersion);
  486. }
  487. Storage->WriteString("ProxyHost", ProxyHost);
  488. Storage->WriteInteger("ProxyPort", ProxyPort);
  489. Storage->WriteString("ProxyUsername", ProxyUsername);
  490. Storage->WriteString("ProxyPassword", FProxyPassword);
  491. Storage->WriteStringRaw("ProxyTelnetCommand", ProxyTelnetCommand);
  492. Storage->WriteInteger("ProxyDNS", (ProxyDNS+2) % 3);
  493. Storage->WriteBool("ProxyLocalhost", ProxyLocalhost);
  494. #define WRITE_BUG(BUG) Storage->WriteInteger("Bug"#BUG, 2 - Bug[sb##BUG]);
  495. WRITE_BUG(Ignore1);
  496. WRITE_BUG(PlainPW1);
  497. WRITE_BUG(RSA1);
  498. WRITE_BUG(HMAC2);
  499. WRITE_BUG(DeriveKey2);
  500. WRITE_BUG(RSAPad2);
  501. WRITE_BUG(DHGEx2);
  502. WRITE_BUG(PKSessID2);
  503. #undef WRITE_BUG
  504. if (PuttyExport)
  505. {
  506. Storage->WriteString("Protocol", ProtocolStr);
  507. }
  508. if (!PuttyExport)
  509. {
  510. Storage->WriteInteger("SFTPSymlinkBug", SFTPSymlinkBug);
  511. Storage->WriteString("CustomParam1", CustomParam1);
  512. Storage->WriteString("CustomParam2", CustomParam2);
  513. }
  514. Storage->CloseSubKey();
  515. FModified = false;
  516. }
  517. }
  518. //---------------------------------------------------------------------
  519. void __fastcall TSessionData::Remove()
  520. {
  521. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  522. try
  523. {
  524. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  525. {
  526. Storage->RecursiveDeleteSubKey(StorageKey);
  527. }
  528. }
  529. __finally
  530. {
  531. delete Storage;
  532. }
  533. }
  534. //---------------------------------------------------------------------
  535. bool __fastcall TSessionData::ParseUrl(AnsiString Url, int Params,
  536. AnsiString * ConnectInfo, AnsiString * HostName, int * PortNumber,
  537. AnsiString * UserName, AnsiString * Password, AnsiString * Path)
  538. {
  539. int PSlash = Url.Pos("/");
  540. if (PSlash == 0)
  541. {
  542. PSlash = Url.Length() + 1;
  543. }
  544. AnsiString AConnectInfo;
  545. AConnectInfo = Url.SubString(1, PSlash - 1);
  546. int P = AConnectInfo.Pos("@");
  547. bool Result = (P > 0) || ((Params & puRequireUsername) == 0);
  548. if (Result)
  549. {
  550. if (Path != NULL)
  551. {
  552. bool ExcludeLeadingSlash = (Params & puExcludeLeadingSlash) != 0;
  553. int Delta = ExcludeLeadingSlash ? 1 : 0;
  554. AnsiString APath = Url.SubString(PSlash + Delta,
  555. Url.Length() - PSlash - Delta + 1);
  556. if (ExcludeLeadingSlash || (APath != "/"))
  557. {
  558. *Path = APath;
  559. }
  560. }
  561. if (ConnectInfo != NULL)
  562. {
  563. *ConnectInfo = AConnectInfo;
  564. }
  565. AnsiString UserInfo;
  566. AnsiString HostInfo;
  567. if (P > 0)
  568. {
  569. UserInfo = AConnectInfo.SubString(1, P - 1);
  570. HostInfo = AConnectInfo.SubString(P + 1, AConnectInfo.Length() - P);
  571. }
  572. else
  573. {
  574. HostInfo = AConnectInfo;
  575. }
  576. if (HostName != NULL)
  577. {
  578. *HostName = CutToChar(HostInfo, ':', true);
  579. }
  580. else
  581. {
  582. CutToChar(HostInfo, ':', true);
  583. }
  584. if (PortNumber != NULL)
  585. {
  586. *PortNumber = HostInfo.IsEmpty() ? -1 : StrToIntDef(HostInfo, -1);
  587. }
  588. if (UserName != NULL)
  589. {
  590. *UserName = CutToChar(UserInfo, ':', false);
  591. }
  592. else
  593. {
  594. CutToChar(UserInfo, ':', false);
  595. }
  596. if (Password != NULL)
  597. {
  598. *Password = UserInfo;
  599. }
  600. }
  601. return Result;
  602. }
  603. //---------------------------------------------------------------------
  604. bool __fastcall TSessionData::ParseUrl(AnsiString Url, int Params)
  605. {
  606. AnsiString AHostName = HostName;
  607. int APortNumber = PortNumber;
  608. AnsiString AUserName = UserName;
  609. AnsiString APassword = Password;
  610. AnsiString ARemoteDirectory = RemoteDirectory;
  611. bool Result = ParseUrl(Url, Params, NULL, &AHostName, &APortNumber,
  612. &AUserName, &APassword, &ARemoteDirectory);
  613. if (Result)
  614. {
  615. HostName = AHostName;
  616. if (APortNumber >= 0)
  617. {
  618. PortNumber = APortNumber;
  619. }
  620. UserName = AUserName;
  621. Password = APassword;
  622. RemoteDirectory = ARemoteDirectory;
  623. }
  624. return Result;
  625. }
  626. //---------------------------------------------------------------------
  627. bool __fastcall TSessionData::GetCanLogin()
  628. {
  629. return !FHostName.IsEmpty() && !FUserName.IsEmpty();
  630. }
  631. //---------------------------------------------------------------------------
  632. AnsiString __fastcall TSessionData::GetSessionKey()
  633. {
  634. return FORMAT("%s@%s", (UserName, HostName));
  635. }
  636. //---------------------------------------------------------------------
  637. AnsiString __fastcall TSessionData::GetStorageKey()
  638. {
  639. return MungeStr(Name);
  640. }
  641. //---------------------------------------------------------------------
  642. void __fastcall TSessionData::SetHostName(AnsiString value)
  643. {
  644. if (FHostName != value)
  645. {
  646. // HostName is key for password encryption
  647. AnsiString XPassword = Password;
  648. int P = value.LastDelimiter("@");
  649. if (P > 0)
  650. {
  651. UserName = value.SubString(1, P - 1);
  652. value = value.SubString(P + 1, value.Length() - P);
  653. }
  654. FHostName = value;
  655. FModified = true;
  656. Password = XPassword;
  657. if (!XPassword.IsEmpty())
  658. {
  659. memset(XPassword.c_str(), 0, XPassword.Length());
  660. }
  661. }
  662. }
  663. //---------------------------------------------------------------------
  664. void __fastcall TSessionData::SetPortNumber(int value)
  665. {
  666. SET_SESSION_PROPERTY(PortNumber);
  667. }
  668. //---------------------------------------------------------------------------
  669. void __fastcall TSessionData::SetShell(AnsiString value)
  670. {
  671. SET_SESSION_PROPERTY(Shell);
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TSessionData::SetClearAliases(bool value)
  675. {
  676. SET_SESSION_PROPERTY(ClearAliases);
  677. }
  678. //---------------------------------------------------------------------------
  679. void __fastcall TSessionData::SetAliasGroupList(bool value)
  680. {
  681. SET_SESSION_PROPERTY(AliasGroupList);
  682. }
  683. //---------------------------------------------------------------------------
  684. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  685. {
  686. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  687. }
  688. //---------------------------------------------------------------------------
  689. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  690. {
  691. SET_SESSION_PROPERTY(UnsetNationalVars);
  692. }
  693. //---------------------------------------------------------------------
  694. void __fastcall TSessionData::SetUserName(AnsiString value)
  695. {
  696. // UserName is key for password encryption
  697. AnsiString XPassword = Password;
  698. SET_SESSION_PROPERTY(UserName);
  699. Password = XPassword;
  700. if (!XPassword.IsEmpty())
  701. {
  702. memset(XPassword.c_str(), 0, XPassword.Length());
  703. }
  704. }
  705. //---------------------------------------------------------------------
  706. void __fastcall TSessionData::SetPassword(AnsiString value)
  707. {
  708. value = EncryptPassword(value, UserName+HostName);
  709. SET_SESSION_PROPERTY(Password);
  710. }
  711. //---------------------------------------------------------------------
  712. AnsiString __fastcall TSessionData::GetPassword()
  713. {
  714. return DecryptPassword(FPassword, UserName+HostName);
  715. }
  716. //---------------------------------------------------------------------
  717. void __fastcall TSessionData::SetPingInterval(int value)
  718. {
  719. SET_SESSION_PROPERTY(PingInterval);
  720. }
  721. //---------------------------------------------------------------------
  722. void __fastcall TSessionData::SetAgentFwd(bool value)
  723. {
  724. SET_SESSION_PROPERTY(AgentFwd);
  725. }
  726. //---------------------------------------------------------------------
  727. void __fastcall TSessionData::SetAuthTIS(bool value)
  728. {
  729. SET_SESSION_PROPERTY(AuthTIS);
  730. }
  731. //---------------------------------------------------------------------
  732. void __fastcall TSessionData::SetAuthKI(bool value)
  733. {
  734. SET_SESSION_PROPERTY(AuthKI);
  735. }
  736. //---------------------------------------------------------------------
  737. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  738. {
  739. SET_SESSION_PROPERTY(AuthKIPassword);
  740. }
  741. //---------------------------------------------------------------------
  742. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  743. {
  744. SET_SESSION_PROPERTY(AuthGSSAPI);
  745. }
  746. //---------------------------------------------------------------------
  747. void __fastcall TSessionData::SetCompression(bool value)
  748. {
  749. SET_SESSION_PROPERTY(Compression);
  750. }
  751. //---------------------------------------------------------------------
  752. void __fastcall TSessionData::SetSshProt(TSshProt value)
  753. {
  754. SET_SESSION_PROPERTY(SshProt);
  755. }
  756. //---------------------------------------------------------------------
  757. void __fastcall TSessionData::SetSsh2DES(bool value)
  758. {
  759. SET_SESSION_PROPERTY(Ssh2DES);
  760. }
  761. //---------------------------------------------------------------------
  762. AnsiString __fastcall TSessionData::GetSshProtStr()
  763. {
  764. return SshProtList[FSshProt];
  765. }
  766. //---------------------------------------------------------------------
  767. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  768. {
  769. assert(Index >= 0 && Index < CIPHER_COUNT);
  770. SET_SESSION_PROPERTY(Ciphers[Index]);
  771. }
  772. //---------------------------------------------------------------------
  773. TCipher __fastcall TSessionData::GetCipher(int Index)
  774. {
  775. assert(Index >= 0 && Index < CIPHER_COUNT);
  776. return FCiphers[Index];
  777. }
  778. //---------------------------------------------------------------------
  779. void __fastcall TSessionData::SetCipherList(AnsiString value)
  780. {
  781. bool Used[CIPHER_COUNT];
  782. for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
  783. AnsiString CipherStr;
  784. int Index = 0;
  785. while (!value.IsEmpty() && (Index < CIPHER_COUNT))
  786. {
  787. CipherStr = CutToChar(value, ',', true);
  788. for (int C = 0; C < CIPHER_COUNT; C++)
  789. {
  790. if (!CipherStr.AnsiCompareIC(CipherNames[C]))
  791. {
  792. Cipher[Index] = (TCipher)C;
  793. Used[C] = true;
  794. Index++;
  795. break;
  796. }
  797. }
  798. }
  799. for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
  800. {
  801. if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
  802. }
  803. }
  804. //---------------------------------------------------------------------
  805. AnsiString __fastcall TSessionData::GetCipherList()
  806. {
  807. AnsiString Result;
  808. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  809. {
  810. Result += AnsiString(Index ? "," : "") + CipherNames[Cipher[Index]];
  811. }
  812. return Result;
  813. }
  814. //---------------------------------------------------------------------
  815. void __fastcall TSessionData::SetPublicKeyFile(AnsiString value)
  816. {
  817. if (FPublicKeyFile != value)
  818. {
  819. FPublicKeyFile = StripPathQuotes(value);
  820. FModified = true;
  821. }
  822. }
  823. //---------------------------------------------------------------------
  824. AnsiString __fastcall TSessionData::GetDefaultLogFileName()
  825. {
  826. return GetTemporaryPath() + MakeValidFileName(SessionName) + ".log";
  827. }
  828. //---------------------------------------------------------------------
  829. void __fastcall TSessionData::SetReturnVar(AnsiString value)
  830. {
  831. SET_SESSION_PROPERTY(ReturnVar);
  832. }
  833. //---------------------------------------------------------------------------
  834. void __fastcall TSessionData::SetLookupUserGroups(bool value)
  835. {
  836. SET_SESSION_PROPERTY(LookupUserGroups);
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall TSessionData::SetEOLType(TEOLType value)
  840. {
  841. SET_SESSION_PROPERTY(EOLType);
  842. }
  843. //---------------------------------------------------------------------------
  844. void __fastcall TSessionData::SetTimeout(int value)
  845. {
  846. SET_SESSION_PROPERTY(Timeout);
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  850. {
  851. SET_SESSION_PROPERTY(FSProtocol);
  852. }
  853. //---------------------------------------------------------------------
  854. AnsiString __fastcall TSessionData::GetFSProtocolStr()
  855. {
  856. assert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  857. return FSProtocolNames[FSProtocol];
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  861. {
  862. if (value != DetectReturnVar)
  863. {
  864. ReturnVar = value ? "" : "$?";
  865. }
  866. }
  867. //---------------------------------------------------------------------------
  868. bool __fastcall TSessionData::GetDetectReturnVar()
  869. {
  870. return ReturnVar.IsEmpty();
  871. }
  872. //---------------------------------------------------------------------------
  873. void __fastcall TSessionData::SetDefaultShell(bool value)
  874. {
  875. if (value != DefaultShell)
  876. {
  877. Shell = value ? "" : "/bin/bash";
  878. }
  879. }
  880. //---------------------------------------------------------------------------
  881. bool __fastcall TSessionData::GetDefaultShell()
  882. {
  883. return Shell.IsEmpty();
  884. }
  885. //---------------------------------------------------------------------------
  886. void __fastcall TSessionData::SetProtocolStr(AnsiString value)
  887. {
  888. for (int Index = 0; backends[Index].name != NULL; Index++)
  889. {
  890. if (!value.AnsiCompareIC(backends[Index].name))
  891. {
  892. FProtocol = (TProtocol)backends[Index].protocol;
  893. break;
  894. }
  895. }
  896. }
  897. //---------------------------------------------------------------------
  898. AnsiString __fastcall TSessionData::GetProtocolStr()
  899. {
  900. for (int Index = 0; backends[Index].name != NULL; Index++)
  901. {
  902. if ((TProtocol)backends[Index].protocol == Protocol)
  903. {
  904. return backends[Index].name;
  905. }
  906. }
  907. return "raw";
  908. }
  909. //---------------------------------------------------------------------
  910. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  911. {
  912. unsigned short hour, min, sec, msec;
  913. value.DecodeTime(&hour, &min, &sec, &msec);
  914. PingInterval = ((int)hour)*60*60 + ((int)min)*60 + sec;
  915. }
  916. //---------------------------------------------------------------------------
  917. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  918. {
  919. return TDateTime((unsigned short)(PingInterval/60/60),
  920. (unsigned short)(PingInterval/60%60), (unsigned short)(PingInterval%60), 0);
  921. }
  922. //---------------------------------------------------------------------------
  923. void __fastcall TSessionData::SetPingType(TPingType value)
  924. {
  925. SET_SESSION_PROPERTY(PingType);
  926. }
  927. //---------------------------------------------------------------------
  928. AnsiString __fastcall TSessionData::GetSessionName()
  929. {
  930. if (!Name.IsEmpty() && !TNamedObjectList::IsHidden(this) &&
  931. (Name != DefaultSessionName))
  932. {
  933. return Name;
  934. }
  935. else if (!HostName.IsEmpty() && !UserName.IsEmpty())
  936. {
  937. return FORMAT("%s@%s", (UserName, HostName));
  938. }
  939. else if (!HostName.IsEmpty())
  940. {
  941. return HostName;
  942. }
  943. else
  944. {
  945. return "session";
  946. }
  947. }
  948. //---------------------------------------------------------------------
  949. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  950. {
  951. SET_SESSION_PROPERTY(TimeDifference);
  952. }
  953. //---------------------------------------------------------------------
  954. void __fastcall TSessionData::SetLocalDirectory(AnsiString value)
  955. {
  956. SET_SESSION_PROPERTY(LocalDirectory);
  957. }
  958. //---------------------------------------------------------------------
  959. void __fastcall TSessionData::SetRemoteDirectory(AnsiString value)
  960. {
  961. SET_SESSION_PROPERTY(RemoteDirectory);
  962. }
  963. //---------------------------------------------------------------------
  964. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  965. {
  966. SET_SESSION_PROPERTY(UpdateDirectories);
  967. }
  968. //---------------------------------------------------------------------
  969. void __fastcall TSessionData::SetCacheDirectories(bool value)
  970. {
  971. SET_SESSION_PROPERTY(CacheDirectories);
  972. }
  973. //---------------------------------------------------------------------
  974. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  975. {
  976. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  977. }
  978. //---------------------------------------------------------------------
  979. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  980. {
  981. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  982. }
  983. //---------------------------------------------------------------------
  984. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  985. {
  986. SET_SESSION_PROPERTY(ResolveSymlinks);
  987. }
  988. //---------------------------------------------------------------------------
  989. void __fastcall TSessionData::SetConsiderDST(bool value)
  990. {
  991. SET_SESSION_PROPERTY(ConsiderDST);
  992. }
  993. //---------------------------------------------------------------------
  994. void __fastcall TSessionData::SetLockInHome(bool value)
  995. {
  996. SET_SESSION_PROPERTY(LockInHome);
  997. }
  998. //---------------------------------------------------------------------
  999. void __fastcall TSessionData::SetSpecial(bool value)
  1000. {
  1001. SET_SESSION_PROPERTY(Special);
  1002. }
  1003. //---------------------------------------------------------------------------
  1004. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  1005. {
  1006. SET_SESSION_PROPERTY(Scp1Compatibility);
  1007. }
  1008. //---------------------------------------------------------------------
  1009. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  1010. {
  1011. SET_SESSION_PROPERTY(TcpNoDelay);
  1012. }
  1013. //---------------------------------------------------------------------
  1014. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  1015. {
  1016. SET_SESSION_PROPERTY(ProxyMethod);
  1017. }
  1018. //---------------------------------------------------------------------
  1019. void __fastcall TSessionData::SetProxyHost(AnsiString value)
  1020. {
  1021. SET_SESSION_PROPERTY(ProxyHost);
  1022. }
  1023. //---------------------------------------------------------------------
  1024. void __fastcall TSessionData::SetProxyPort(int value)
  1025. {
  1026. SET_SESSION_PROPERTY(ProxyPort);
  1027. }
  1028. //---------------------------------------------------------------------
  1029. void __fastcall TSessionData::SetProxyUsername(AnsiString value)
  1030. {
  1031. SET_SESSION_PROPERTY(ProxyUsername);
  1032. }
  1033. //---------------------------------------------------------------------
  1034. void __fastcall TSessionData::SetProxyPassword(AnsiString value)
  1035. {
  1036. // proxy password unencrypted to maintain compatibility with Putty
  1037. SET_SESSION_PROPERTY(ProxyPassword);
  1038. }
  1039. //---------------------------------------------------------------------
  1040. AnsiString __fastcall TSessionData::GetProxyPassword()
  1041. {
  1042. return FProxyPassword;
  1043. }
  1044. //---------------------------------------------------------------------
  1045. void __fastcall TSessionData::SetProxyTelnetCommand(AnsiString value)
  1046. {
  1047. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  1048. }
  1049. //---------------------------------------------------------------------
  1050. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  1051. {
  1052. SET_SESSION_PROPERTY(ProxyDNS);
  1053. }
  1054. //---------------------------------------------------------------------
  1055. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  1056. {
  1057. SET_SESSION_PROPERTY(ProxyLocalhost);
  1058. }
  1059. //---------------------------------------------------------------------
  1060. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  1061. {
  1062. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1063. SET_SESSION_PROPERTY(Bugs[Bug]);
  1064. }
  1065. //---------------------------------------------------------------------
  1066. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug)
  1067. {
  1068. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1069. return FBugs[Bug];
  1070. }
  1071. //---------------------------------------------------------------------
  1072. void __fastcall TSessionData::SetCustomParam1(AnsiString value)
  1073. {
  1074. SET_SESSION_PROPERTY(CustomParam1);
  1075. }
  1076. //---------------------------------------------------------------------
  1077. void __fastcall TSessionData::SetCustomParam2(AnsiString value)
  1078. {
  1079. SET_SESSION_PROPERTY(CustomParam2);
  1080. }
  1081. //---------------------------------------------------------------------
  1082. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  1083. {
  1084. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  1085. }
  1086. //---------------------------------------------------------------------
  1087. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  1088. {
  1089. SET_SESSION_PROPERTY(SFTPUploadQueue);
  1090. }
  1091. //---------------------------------------------------------------------
  1092. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  1093. {
  1094. SET_SESSION_PROPERTY(SFTPListingQueue);
  1095. }
  1096. //---------------------------------------------------------------------
  1097. void __fastcall TSessionData::SetSFTPSymlinkBug(TAutoSwitch value)
  1098. {
  1099. SET_SESSION_PROPERTY(SFTPSymlinkBug);
  1100. }
  1101. //---------------------------------------------------------------------
  1102. AnsiString __fastcall TSessionData::GetInfoTip()
  1103. {
  1104. return FmtLoadStr(SESSION_INFO_TIP,
  1105. ARRAYOFCONST((
  1106. (HostName.IsEmpty() ? AnsiString() : HostName),
  1107. (UserName.IsEmpty() ? AnsiString() : UserName),
  1108. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  1109. SshProtStr,
  1110. FSProtocolStr)));
  1111. }
  1112. //=== TStoredSessionList ----------------------------------------------
  1113. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  1114. TNamedObjectList(), FReadOnly(aReadOnly)
  1115. {
  1116. assert(Configuration);
  1117. LastStorage = Configuration->Storage;
  1118. FDefaultSettings = new TSessionData(DefaultSessionName);
  1119. }
  1120. //---------------------------------------------------------------------
  1121. __fastcall TStoredSessionList::~TStoredSessionList()
  1122. {
  1123. assert(Configuration);
  1124. if (!FReadOnly && (Configuration->Storage != LastStorage)) Save();
  1125. delete FDefaultSettings;
  1126. }
  1127. //---------------------------------------------------------------------
  1128. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage, bool AsModified)
  1129. {
  1130. TStringList *SubKeys = new TStringList();
  1131. try {
  1132. Storage->GetSubKeyNames(SubKeys);
  1133. for (int Index = 0; Index < SubKeys->Count; Index++)
  1134. {
  1135. TSessionData *SessionData;
  1136. AnsiString SessionName = UnMungeStr(SubKeys->Strings[Index]);
  1137. if (SessionName == FDefaultSettings->Name) SessionData = FDefaultSettings;
  1138. else SessionData = (TSessionData*)FindByName(SessionName);
  1139. if (!SessionData)
  1140. {
  1141. SessionData = new TSessionData(SessionName);
  1142. Add(SessionData);
  1143. }
  1144. SessionData->Load(Storage);
  1145. if (AsModified)
  1146. {
  1147. SessionData->Modified = true;
  1148. }
  1149. }
  1150. } __finally {
  1151. delete SubKeys;
  1152. }
  1153. }
  1154. //---------------------------------------------------------------------
  1155. void __fastcall TStoredSessionList::Load(AnsiString aKey)
  1156. {
  1157. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  1158. try {
  1159. LastStorage = stRegistry;
  1160. if (Storage->OpenRootKey(False)) Load(Storage);
  1161. } __finally {
  1162. delete Storage;
  1163. }
  1164. }
  1165. //---------------------------------------------------------------------
  1166. void __fastcall TStoredSessionList::Load()
  1167. {
  1168. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  1169. try {
  1170. LastStorage = Configuration->Storage;
  1171. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  1172. Load(Storage);
  1173. } __finally {
  1174. delete Storage;
  1175. }
  1176. }
  1177. //---------------------------------------------------------------------
  1178. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage)
  1179. {
  1180. for (int Index = 0; Index < Count+HiddenCount; Index++)
  1181. {
  1182. TSessionData *SessionData = (TSessionData *)Items[Index];
  1183. if (SessionData->Modified)
  1184. SessionData->Save(Storage);
  1185. }
  1186. if (FDefaultSettings->Modified)
  1187. FDefaultSettings->Save(Storage);
  1188. }
  1189. //---------------------------------------------------------------------
  1190. void __fastcall TStoredSessionList::Save(AnsiString aKey)
  1191. {
  1192. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  1193. try
  1194. {
  1195. LastStorage = stRegistry;
  1196. Storage->AccessMode = smReadWrite;
  1197. if (Storage->OpenRootKey(True)) Save(Storage);
  1198. }
  1199. __finally
  1200. {
  1201. delete Storage;
  1202. }
  1203. }
  1204. //---------------------------------------------------------------------
  1205. void __fastcall TStoredSessionList::Save()
  1206. {
  1207. if (LastStorage != Configuration->Storage)
  1208. {
  1209. // make sure, that all data will be stored, when saving to new storage
  1210. for (int Index = 0; Index < Count+HiddenCount; Index++)
  1211. ((TSessionData*)Items[Index])->Modified = true;
  1212. FDefaultSettings->Modified = true;
  1213. LastStorage = Configuration->Storage;
  1214. }
  1215. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  1216. try {
  1217. Storage->AccessMode = smReadWrite;
  1218. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, True))
  1219. Save(Storage);
  1220. } __finally {
  1221. delete Storage;
  1222. }
  1223. }
  1224. //---------------------------------------------------------------------
  1225. void __fastcall TStoredSessionList::SelectAll(bool Select)
  1226. {
  1227. for (int Index = 0; Index < Count; Index++)
  1228. Sessions[Index]->Selected = Select;
  1229. }
  1230. //---------------------------------------------------------------------
  1231. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  1232. bool OnlySelected)
  1233. {
  1234. for (int Index = 0; Index < From->Count; Index++)
  1235. {
  1236. if (!OnlySelected || From->Sessions[Index]->Selected)
  1237. {
  1238. TSessionData *Session = new TSessionData("");
  1239. Session->Assign(From->Sessions[Index]);
  1240. Session->Modified = true;
  1241. Session->MakeUniqueIn(this);
  1242. Add(Session);
  1243. }
  1244. }
  1245. Save();
  1246. }
  1247. //---------------------------------------------------------------------
  1248. void __fastcall TStoredSessionList::SelectSessionsToImport
  1249. (TStoredSessionList * Dest, bool SSHOnly)
  1250. {
  1251. for (int Index = 0; Index < Count; Index++)
  1252. {
  1253. Sessions[Index]->Selected =
  1254. (!SSHOnly || (Sessions[Index]->Protocol == ptSSH)) &&
  1255. !Dest->FindByName(Sessions[Index]->Name);
  1256. }
  1257. }
  1258. //---------------------------------------------------------------------
  1259. void __fastcall TStoredSessionList::Cleanup()
  1260. {
  1261. try {
  1262. if (Configuration->Storage == stRegistry) Clear();
  1263. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  1264. try {
  1265. Storage->AccessMode = smReadWrite;
  1266. if (Storage->OpenRootKey(False))
  1267. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  1268. } __finally {
  1269. delete Storage;
  1270. }
  1271. } catch (Exception &E) {
  1272. throw ExtException(&E, CLEANUP_SESSIONS_ERROR);
  1273. }
  1274. }
  1275. //---------------------------------------------------------------------------
  1276. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  1277. {
  1278. for (int Index = 0; Index < Count; Index++)
  1279. if (Data == Sessions[Index]) return Index;
  1280. return -1;
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. TSessionData * __fastcall TStoredSessionList::NewSession(
  1284. AnsiString SessionName, TSessionData * Session)
  1285. {
  1286. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  1287. if (!DuplicateSession)
  1288. {
  1289. DuplicateSession = new TSessionData("");
  1290. DuplicateSession->Assign(Session);
  1291. DuplicateSession->Name = SessionName;
  1292. // make sure, that new stored session is saved to registry
  1293. DuplicateSession->Modified = true;
  1294. Add(DuplicateSession);
  1295. }
  1296. else
  1297. {
  1298. DuplicateSession->Assign(Session);
  1299. DuplicateSession->Name = SessionName;
  1300. DuplicateSession->Modified = true;
  1301. }
  1302. // list was saved here before to default storage, but it would not allow
  1303. // to work with special lists (export/import) not using default storage
  1304. return DuplicateSession;
  1305. }
  1306. //---------------------------------------------------------------------------
  1307. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  1308. {
  1309. assert(FDefaultSettings);
  1310. if (FDefaultSettings != value)
  1311. {
  1312. FDefaultSettings->Assign(value);
  1313. FDefaultSettings->Name = DefaultSessionName;
  1314. Save();
  1315. }
  1316. }
  1317. //---------------------------------------------------------------------------
  1318. void __fastcall TStoredSessionList::ImportHostKeys(const AnsiString TargetKey,
  1319. const AnsiString SourceKey, TStoredSessionList * Sessions,
  1320. bool OnlySelected)
  1321. {
  1322. TRegistryStorage * SourceStorage = NULL;
  1323. TRegistryStorage * TargetStorage = NULL;
  1324. TStringList * KeyList = NULL;
  1325. try
  1326. {
  1327. SourceStorage = new TRegistryStorage(SourceKey);
  1328. TargetStorage = new TRegistryStorage(TargetKey);
  1329. TargetStorage->AccessMode = smReadWrite;
  1330. KeyList = new TStringList();
  1331. if (SourceStorage->OpenRootKey(false) &&
  1332. TargetStorage->OpenRootKey(true))
  1333. {
  1334. SourceStorage->GetValueNames(KeyList);
  1335. TSessionData * Session;
  1336. AnsiString HostKeyName;
  1337. assert(Sessions != NULL);
  1338. for (int Index = 0; Index < Sessions->Count; Index++)
  1339. {
  1340. Session = Sessions->Sessions[Index];
  1341. if (!OnlySelected || Session->Selected)
  1342. {
  1343. HostKeyName = MungeStr(FORMAT("@%d:%s", (Session->PortNumber, Session->HostName)));
  1344. AnsiString KeyName;
  1345. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  1346. {
  1347. KeyName = KeyList->Strings[KeyIndex];
  1348. int P = KeyName.Pos(HostKeyName);
  1349. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  1350. {
  1351. TargetStorage->WriteStringRaw(KeyName,
  1352. SourceStorage->ReadStringRaw(KeyName, ""));
  1353. }
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. __finally
  1360. {
  1361. delete SourceStorage;
  1362. delete TargetStorage;
  1363. delete KeyList;
  1364. }
  1365. }