SessionData.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 = 0;
  38. Timeout = 15;
  39. AgentFwd = false;
  40. AuthTIS = false;
  41. AuthKI = true;
  42. Compression = false;
  43. SshProt = ssh2;
  44. Ssh2DES = false;
  45. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  46. {
  47. Cipher[Index] = DefaultCipherList[Index];
  48. }
  49. PublicKeyFile = "";
  50. FProtocol = ptSSH;
  51. TcpNoDelay = true;
  52. ProxyMethod = pmNone;
  53. ProxyHost = "proxy";
  54. ProxyPort = 80;
  55. ProxyUsername = "";
  56. ProxyPassword = "";
  57. ProxyTelnetCommand = "connect %host %port\\n";
  58. //ProxySOCKSVersion = 5;
  59. ProxyDNS = asAuto;
  60. ProxyLocalhost = false;
  61. for (int Index = 0; Index < LENOF(FBugs); Index++)
  62. {
  63. Bug[(TSshBug)Index] = asAuto;
  64. }
  65. Special = false;
  66. FSProtocol = fsSCPonly;
  67. // FS common
  68. LocalDirectory = "";
  69. RemoteDirectory = "";
  70. UpdateDirectories = false;
  71. CacheDirectories = true;
  72. LockInHome = false;
  73. ResolveSymlinks = true;
  74. // SCP
  75. ReturnVar = "";
  76. LookupUserGroups = true;
  77. EOLType = eolLF;
  78. Shell = ""; //default shell
  79. ReturnVar = "";
  80. ClearAliases = true;
  81. UnsetNationalVars = true;
  82. AliasGroupList = false;
  83. IgnoreLsWarnings = true;
  84. Scp1Compatibility = false;
  85. TimeDifference = 0;
  86. CustomParam1 = "";
  87. CustomParam2 = "";
  88. Selected = false;
  89. FModified = false;
  90. // add also to TSessionLog::AddStartupInfo()
  91. }
  92. //---------------------------------------------------------------------
  93. void __fastcall TSessionData::Assign(TPersistent * Source)
  94. {
  95. if (Source && Source->InheritsFrom(__classid(TSessionData)))
  96. {
  97. #define DUPL(P) P = ((TSessionData *)Source)->P
  98. DUPL(Name);
  99. DUPL(HostName);
  100. DUPL(PortNumber);
  101. DUPL(UserName);
  102. DUPL(Password);
  103. DUPL(PingInterval);
  104. DUPL(Timeout);
  105. DUPL(AgentFwd);
  106. DUPL(AuthTIS);
  107. DUPL(Compression);
  108. DUPL(SshProt);
  109. DUPL(Ssh2DES);
  110. DUPL(CipherList);
  111. DUPL(PublicKeyFile);
  112. DUPL(FSProtocol);
  113. DUPL(LocalDirectory);
  114. DUPL(RemoteDirectory);
  115. DUPL(UpdateDirectories);
  116. DUPL(CacheDirectories);
  117. DUPL(ResolveSymlinks);
  118. DUPL(LockInHome);
  119. DUPL(Special);
  120. DUPL(Selected);
  121. DUPL(ReturnVar);
  122. DUPL(LookupUserGroups);
  123. DUPL(EOLType);
  124. DUPL(Shell);
  125. DUPL(ClearAliases);
  126. DUPL(Scp1Compatibility);
  127. DUPL(UnsetNationalVars);
  128. DUPL(AliasGroupList);
  129. DUPL(IgnoreLsWarnings);
  130. DUPL(TimeDifference);
  131. // new in 53b
  132. DUPL(TcpNoDelay);
  133. DUPL(AuthKI);
  134. DUPL(ProxyMethod);
  135. DUPL(ProxyHost);
  136. DUPL(ProxyPort);
  137. DUPL(ProxyUsername);
  138. DUPL(ProxyPassword);
  139. DUPL(ProxyTelnetCommand);
  140. DUPL(ProxyDNS);
  141. DUPL(ProxyLocalhost);
  142. for (int Index = 0; Index < LENOF(FBugs); Index++)
  143. {
  144. DUPL(Bug[(TSshBug)Index]);
  145. }
  146. DUPL(CustomParam1);
  147. DUPL(CustomParam2);
  148. #undef DUPL
  149. FModified = ((TSessionData *)Source)->Modified;
  150. }
  151. else
  152. {
  153. TNamedObject::Assign(Source);
  154. }
  155. }
  156. //---------------------------------------------------------------------
  157. void __fastcall TSessionData::StoreToConfig(void * config)
  158. {
  159. Config * cfg = (Config *)config;
  160. // clear all (parameters not set below)
  161. memset(cfg, 0, sizeof(*cfg));
  162. // user-configurable settings
  163. ASCOPY(cfg->host, HostName);
  164. ASCOPY(cfg->username, UserName);
  165. cfg->port = PortNumber;
  166. cfg->protocol = PROT_SSH;
  167. cfg->ping_interval = PingInterval;
  168. cfg->compression = Compression;
  169. cfg->agentfwd = AgentFwd;
  170. for (int c = 0; c < CIPHER_COUNT; c++)
  171. {
  172. int pcipher;
  173. switch (Cipher[c]) {
  174. case cipWarn: pcipher = CIPHER_WARN; break;
  175. case cip3DES: pcipher = CIPHER_3DES; break;
  176. case cipBlowfish: pcipher = CIPHER_BLOWFISH; break;
  177. case cipAES: pcipher = CIPHER_AES; break;
  178. case cipDES: pcipher = CIPHER_DES; break;
  179. default: assert(false);
  180. }
  181. cfg->ssh_cipherlist[c] = pcipher;
  182. }
  183. AnsiString SPublicKeyFile = PublicKeyFile;
  184. if (SPublicKeyFile.IsEmpty()) SPublicKeyFile = Configuration->DefaultKeyFile;
  185. SPublicKeyFile = StripPathQuotes(SPublicKeyFile);
  186. ASCOPY(cfg->keyfile.path, SPublicKeyFile);
  187. cfg->sshprot = SshProt;
  188. cfg->ssh2_des_cbc = Ssh2DES;
  189. cfg->try_tis_auth = AuthTIS;
  190. cfg->try_ki_auth = AuthKI;
  191. cfg->proxy_type = ProxyMethod;
  192. ASCOPY(cfg->proxy_host, ProxyHost);
  193. cfg->proxy_port = ProxyPort;
  194. ASCOPY(cfg->proxy_username, ProxyUsername);
  195. ASCOPY(cfg->proxy_password, ProxyPassword);
  196. ASCOPY(cfg->proxy_telnet_command, ProxyTelnetCommand);
  197. cfg->proxy_dns = ProxyDNS;
  198. cfg->even_proxy_localhost = ProxyLocalhost;
  199. #pragma option push -w-eas
  200. // after 0.53b values were reversed, however putty still stores
  201. // settings to registry in save way as before
  202. cfg->sshbug_ignore1 = Bug[sbIgnore1];
  203. cfg->sshbug_plainpw1 = Bug[sbPlainPW1];
  204. cfg->sshbug_rsa1 = Bug[sbRSA1];
  205. cfg->sshbug_hmac2 = Bug[sbHMAC2];
  206. cfg->sshbug_derivekey2 = Bug[sbDeriveKey2];
  207. cfg->sshbug_rsapad2 = Bug[sbRSAPad2];
  208. cfg->sshbug_dhgex2 = Bug[sbDHGEx2];
  209. // new after 0.53b
  210. cfg->sshbug_pksessid2 = Bug[sbPKSessID2];
  211. #pragma option pop
  212. if (FSProtocol == fsSCPonly)
  213. {
  214. cfg->ssh_subsys = FALSE;
  215. if (Shell.IsEmpty())
  216. {
  217. // Following forces Putty to open default shell
  218. // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
  219. cfg->remote_cmd[0] = '\0';
  220. }
  221. else
  222. {
  223. ASCOPY(cfg->remote_cmd, Shell);
  224. }
  225. cfg->remote_cmd_ptr = &cfg->remote_cmd[0];
  226. cfg->remote_cmd_ptr2 = NULL; // no second attempt for SCPonly
  227. }
  228. else
  229. {
  230. cfg->ssh_subsys = TRUE;
  231. strcpy(cfg->remote_cmd, "sftp");
  232. cfg->remote_cmd_ptr = &cfg->remote_cmd[0];
  233. if (FSProtocol != fsSFTPonly)
  234. {
  235. cfg->ssh_subsys2 = FALSE;
  236. if (Shell.IsEmpty())
  237. {
  238. cfg->remote_cmd2[0] = '\0';
  239. }
  240. else
  241. {
  242. ASCOPY(cfg->remote_cmd2, Shell);
  243. }
  244. cfg->remote_cmd_ptr2 = &cfg->remote_cmd2[0];
  245. }
  246. else
  247. {
  248. // see psftp_connect() from psftp.c
  249. cfg->ssh_subsys2 = FALSE;
  250. cfg->remote_cmd_ptr2 =
  251. "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n"
  252. "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n"
  253. "exec sftp-server";
  254. }
  255. }
  256. // permanent settings
  257. cfg->nopty = TRUE;
  258. }
  259. //---------------------------------------------------------------------
  260. void __fastcall TSessionData::Load(THierarchicalStorage * Storage)
  261. {
  262. if (Storage->OpenSubKey(MungeStr(Name), False))
  263. {
  264. HostName = Storage->ReadString("HostName", HostName);
  265. PortNumber = Storage->ReadInteger("PortNumber", PortNumber);
  266. UserName = Storage->ReadString("UserName", UserName);
  267. if (!Configuration->DisablePasswordStoring)
  268. {
  269. FPassword = Storage->ReadString("Password", FPassword);
  270. }
  271. PingInterval =
  272. Storage->ReadInteger("PingInterval", PingInterval/60)*60 +
  273. Storage->ReadInteger("PingIntervalSec", PingInterval%60);
  274. Timeout = Storage->ReadInteger("Timeout", Timeout);
  275. AgentFwd = Storage->ReadBool("AgentFwd", AgentFwd);
  276. AuthTIS = Storage->ReadBool("AuthTIS", AuthTIS);
  277. AuthKI = Storage->ReadBool("AuthKI", AuthKI);
  278. Compression = Storage->ReadBool("Compression", Compression);
  279. SshProt = (TSshProt)Storage->ReadInteger("SshProt", SshProt);
  280. Ssh2DES = Storage->ReadBool("Ssh2DES", Ssh2DES);
  281. CipherList = Storage->ReadString("Cipher", CipherList);
  282. PublicKeyFile = Storage->ReadString("PublicKeyFile", PublicKeyFile);
  283. FSProtocol = (TFSProtocol)Storage->ReadInteger("FSProtocol", FSProtocol);
  284. LocalDirectory = Storage->ReadString("LocalDirectory", LocalDirectory);
  285. RemoteDirectory = Storage->ReadString("RemoteDirectory", RemoteDirectory);
  286. UpdateDirectories = Storage->ReadBool("UpdateDirectories", UpdateDirectories);
  287. CacheDirectories = Storage->ReadBool("CacheDirectories", CacheDirectories);
  288. ResolveSymlinks = Storage->ReadBool("ResolveSymlinks", ResolveSymlinks);
  289. LockInHome = Storage->ReadBool("LockInHome", LockInHome);
  290. Special = Storage->ReadBool("Special", Special);
  291. Shell = Storage->ReadString("Shell", Shell);
  292. ClearAliases = Storage->ReadBool("ClearAliases", ClearAliases);
  293. UnsetNationalVars = Storage->ReadBool("UnsetNationalVars", UnsetNationalVars);
  294. AliasGroupList = Storage->ReadBool("AliasGroupList", AliasGroupList);
  295. IgnoreLsWarnings = Storage->ReadBool("IgnoreLsWarnings", IgnoreLsWarnings);
  296. Scp1Compatibility = Storage->ReadBool("Scp1Compatibility", Scp1Compatibility);
  297. TimeDifference = Storage->ReadFloat("TimeDifference", TimeDifference);
  298. ReturnVar = Storage->ReadString("ReturnVar", ReturnVar);
  299. LookupUserGroups = Storage->ReadBool("LookupUserGroups", LookupUserGroups);
  300. EOLType = (TEOLType)Storage->ReadInteger("EOLType", EOLType);
  301. // new in 53b
  302. TcpNoDelay = Storage->ReadBool("TcpNoDelay", TcpNoDelay);
  303. ProxyMethod = (TProxyMethod)Storage->ReadInteger("ProxyMethod", -1);
  304. if (ProxyMethod < 0)
  305. {
  306. int ProxyType = Storage->ReadInteger("ProxyType", pxNone);
  307. int ProxySOCKSVersion;
  308. switch (ProxyType) {
  309. case pxHTTP:
  310. ProxyMethod = pmHTTP;
  311. break;
  312. case pxTelnet:
  313. ProxyMethod = pmTelnet;
  314. break;
  315. case pxSocks:
  316. ProxySOCKSVersion = Storage->ReadInteger("ProxySOCKSVersion", 5);
  317. ProxyMethod = ProxySOCKSVersion == 5 ? pmSocks5 : pmSocks4;
  318. break;
  319. default:
  320. case pxNone:
  321. ProxyMethod = pmNone;
  322. break;
  323. }
  324. }
  325. ProxyHost = Storage->ReadString("ProxyHost", ProxyHost);
  326. ProxyPort = Storage->ReadInteger("ProxyPort", ProxyPort);
  327. ProxyUsername = Storage->ReadString("ProxyUsername", ProxyUsername);
  328. FProxyPassword = Storage->ReadString("ProxyPassword", FProxyPassword);
  329. ProxyTelnetCommand = Storage->ReadStringRaw("ProxyTelnetCommand", ProxyTelnetCommand);
  330. ProxyDNS = TAutoSwitch((Storage->ReadInteger("ProxyDNS", ProxyDNS) + 1) % 3);
  331. ProxyLocalhost = Storage->ReadBool("ProxyLocalhost", ProxyLocalhost);
  332. #define READ_BUG(BUG) \
  333. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger("Bug"#BUG, \
  334. 2 - Bug[sb##BUG]));
  335. READ_BUG(Ignore1);
  336. READ_BUG(PlainPW1);
  337. READ_BUG(RSA1);
  338. READ_BUG(HMAC2);
  339. READ_BUG(DeriveKey2);
  340. READ_BUG(RSAPad2);
  341. READ_BUG(DHGEx2);
  342. READ_BUG(PKSessID2);
  343. #undef READ_BUG
  344. if ((Bug[sbHMAC2] == asAuto) &&
  345. Storage->ReadBool("BuggyMAC", false))
  346. {
  347. Bug[sbHMAC2] = asOn;
  348. }
  349. // read only (used only on Import from Putty dialog)
  350. ProtocolStr = Storage->ReadString("Protocol", ProtocolStr);
  351. CustomParam1 = Storage->ReadString("CustomParam1", CustomParam1);
  352. CustomParam2 = Storage->ReadString("CustomParam2", CustomParam2);
  353. Storage->CloseSubKey();
  354. };
  355. FModified = false;
  356. }
  357. //---------------------------------------------------------------------
  358. void __fastcall TSessionData::Save(THierarchicalStorage * Storage, bool PuttyExport)
  359. {
  360. if (Modified && Storage->OpenSubKey(MungeStr(Name), true))
  361. {
  362. Storage->WriteString("HostName", HostName);
  363. Storage->WriteInteger("PortNumber", PortNumber);
  364. Storage->WriteString("UserName", UserName);
  365. if (!Configuration->DisablePasswordStoring && !PuttyExport)
  366. {
  367. Storage->WriteString("Password", FPassword);
  368. }
  369. Storage->WriteInteger("PingInterval", PingInterval/60);
  370. Storage->WriteInteger("PingIntervalSec", PingInterval%60);
  371. Storage->WriteInteger("Timeout", Timeout);
  372. Storage->WriteBool("AgentFwd", AgentFwd);
  373. Storage->WriteBool("AuthTIS", AuthTIS);
  374. Storage->WriteBool("AuthKI", AuthKI);
  375. Storage->WriteBool("Compression", Compression);
  376. Storage->WriteInteger("SshProt", SshProt);
  377. Storage->WriteBool("Ssh2DES", Ssh2DES);
  378. Storage->WriteString("Cipher", CipherList);
  379. Storage->WriteString("PublicKeyFile", PublicKeyFile);
  380. Storage->WriteBool("TcpNoDelay", TcpNoDelay);
  381. if (!PuttyExport)
  382. {
  383. Storage->WriteInteger("FSProtocol", FSProtocol);
  384. Storage->WriteString("LocalDirectory", LocalDirectory);
  385. Storage->WriteString("RemoteDirectory", RemoteDirectory);
  386. Storage->WriteBool("UpdateDirectories", UpdateDirectories);
  387. Storage->WriteBool("CacheDirectories", CacheDirectories);
  388. Storage->WriteBool("ResolveSymlinks", ResolveSymlinks);
  389. Storage->WriteBool("LockInHome", LockInHome);
  390. // Special is never stored (if it would, login dialog must be modified not to
  391. // duplicate Special parameter when Special session is loaded and then stored
  392. // under different name)
  393. // Storage->WriteBool("Special", Special);
  394. Storage->WriteString("Shell", Shell);
  395. Storage->WriteBool("ClearAliases", ClearAliases);
  396. Storage->WriteBool("UnsetNationalVars", UnsetNationalVars);
  397. Storage->WriteBool("AliasGroupList", AliasGroupList);
  398. Storage->WriteBool("IgnoreLsWarnings", IgnoreLsWarnings);
  399. Storage->WriteBool("Scp1Compatibility", Scp1Compatibility);
  400. Storage->WriteFloat("TimeDifference", TimeDifference);
  401. Storage->WriteString("ReturnVar", ReturnVar);
  402. Storage->WriteBool("LookupUserGroups", LookupUserGroups);
  403. Storage->WriteInteger("EOLType", EOLType);
  404. }
  405. Storage->WriteInteger("ProxyMethod", ProxyMethod);
  406. Storage->WriteString("ProxyHost", ProxyHost);
  407. Storage->WriteInteger("ProxyPort", ProxyPort);
  408. Storage->WriteString("ProxyUsername", ProxyUsername);
  409. Storage->WriteString("ProxyPassword", FProxyPassword);
  410. Storage->WriteStringRaw("ProxyTelnetCommand", ProxyTelnetCommand);
  411. Storage->WriteInteger("ProxyDNS", (ProxyDNS+2) % 3);
  412. Storage->WriteBool("ProxyLocalhost", ProxyLocalhost);
  413. #define WRITE_BUG(BUG) Storage->WriteInteger("Bug"#BUG, 2 - Bug[sb##BUG]);
  414. WRITE_BUG(Ignore1);
  415. WRITE_BUG(PlainPW1);
  416. WRITE_BUG(RSA1);
  417. WRITE_BUG(HMAC2);
  418. WRITE_BUG(DeriveKey2);
  419. WRITE_BUG(RSAPad2);
  420. WRITE_BUG(DHGEx2);
  421. WRITE_BUG(PKSessID2);
  422. #undef WRITE_BUG
  423. if (PuttyExport)
  424. {
  425. Storage->WriteString("Protocol", ProtocolStr);
  426. }
  427. if (!PuttyExport)
  428. {
  429. Storage->WriteString("CustomParam1", CustomParam1);
  430. Storage->WriteString("CustomParam2", CustomParam2);
  431. }
  432. Storage->CloseSubKey();
  433. FModified = false;
  434. }
  435. }
  436. //---------------------------------------------------------------------
  437. void __fastcall TSessionData::Remove()
  438. {
  439. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  440. try
  441. {
  442. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  443. {
  444. Storage->RecursiveDeleteSubKey(MungeStr(Name));
  445. }
  446. }
  447. __finally
  448. {
  449. delete Storage;
  450. }
  451. }
  452. //---------------------------------------------------------------------
  453. bool __fastcall TSessionData::GetCanLogin()
  454. {
  455. return !FHostName.IsEmpty() && !FUserName.IsEmpty();
  456. }
  457. //---------------------------------------------------------------------------
  458. AnsiString __fastcall TSessionData::GetSessionKey()
  459. {
  460. return FORMAT("%s@%s", (UserName, HostName));
  461. }
  462. //---------------------------------------------------------------------
  463. void __fastcall TSessionData::SetHostName(AnsiString value)
  464. {
  465. // HostName is key for password encryption
  466. AnsiString XPassword = Password;
  467. SET_SESSION_PROPERTY(HostName);
  468. Password = XPassword;
  469. }
  470. //---------------------------------------------------------------------
  471. void __fastcall TSessionData::SetPortNumber(int value)
  472. {
  473. SET_SESSION_PROPERTY(PortNumber);
  474. }
  475. //---------------------------------------------------------------------------
  476. void __fastcall TSessionData::SetShell(AnsiString value)
  477. {
  478. SET_SESSION_PROPERTY(Shell);
  479. }
  480. //---------------------------------------------------------------------------
  481. void __fastcall TSessionData::SetClearAliases(bool value)
  482. {
  483. SET_SESSION_PROPERTY(ClearAliases);
  484. }
  485. //---------------------------------------------------------------------------
  486. void __fastcall TSessionData::SetAliasGroupList(bool value)
  487. {
  488. SET_SESSION_PROPERTY(AliasGroupList);
  489. }
  490. //---------------------------------------------------------------------------
  491. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  492. {
  493. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  497. {
  498. SET_SESSION_PROPERTY(UnsetNationalVars);
  499. }
  500. //---------------------------------------------------------------------
  501. void __fastcall TSessionData::SetUserName(AnsiString value)
  502. {
  503. // UserName is key for password encryption
  504. AnsiString XPassword = Password;
  505. SET_SESSION_PROPERTY(UserName);
  506. Password = XPassword;
  507. }
  508. //---------------------------------------------------------------------
  509. void __fastcall TSessionData::SetPassword(AnsiString value)
  510. {
  511. value = EncryptPassword(value, UserName+HostName);
  512. SET_SESSION_PROPERTY(Password);
  513. }
  514. //---------------------------------------------------------------------
  515. AnsiString __fastcall TSessionData::GetPassword()
  516. {
  517. return DecryptPassword(FPassword, UserName+HostName);
  518. }
  519. //---------------------------------------------------------------------
  520. void __fastcall TSessionData::SetPingInterval(int value)
  521. {
  522. SET_SESSION_PROPERTY(PingInterval);
  523. }
  524. //---------------------------------------------------------------------
  525. void __fastcall TSessionData::SetAgentFwd(bool value)
  526. {
  527. SET_SESSION_PROPERTY(AgentFwd);
  528. }
  529. //---------------------------------------------------------------------
  530. void __fastcall TSessionData::SetAuthTIS(bool value)
  531. {
  532. SET_SESSION_PROPERTY(AuthTIS);
  533. }
  534. //---------------------------------------------------------------------
  535. void __fastcall TSessionData::SetAuthKI(bool value)
  536. {
  537. SET_SESSION_PROPERTY(AuthKI);
  538. }
  539. //---------------------------------------------------------------------
  540. void __fastcall TSessionData::SetCompression(bool value)
  541. {
  542. SET_SESSION_PROPERTY(Compression);
  543. }
  544. //---------------------------------------------------------------------
  545. void __fastcall TSessionData::SetSshProt(TSshProt value)
  546. {
  547. SET_SESSION_PROPERTY(SshProt);
  548. }
  549. //---------------------------------------------------------------------
  550. void __fastcall TSessionData::SetSsh2DES(bool value)
  551. {
  552. SET_SESSION_PROPERTY(Ssh2DES);
  553. }
  554. //---------------------------------------------------------------------
  555. AnsiString __fastcall TSessionData::GetSshProtStr()
  556. {
  557. return SshProtList[FSshProt];
  558. }
  559. //---------------------------------------------------------------------
  560. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  561. {
  562. assert(Index >= 0 && Index < CIPHER_COUNT);
  563. SET_SESSION_PROPERTY(Ciphers[Index]);
  564. }
  565. //---------------------------------------------------------------------
  566. TCipher __fastcall TSessionData::GetCipher(int Index)
  567. {
  568. assert(Index >= 0 && Index < CIPHER_COUNT);
  569. return FCiphers[Index];
  570. }
  571. //---------------------------------------------------------------------
  572. void __fastcall TSessionData::SetCipherList(AnsiString value)
  573. {
  574. bool Used[CIPHER_COUNT];
  575. for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
  576. AnsiString CipherStr;
  577. int Index = 0;
  578. while (!value.IsEmpty() && (Index < CIPHER_COUNT))
  579. {
  580. CipherStr = CutToChar(value, ',', true);
  581. for (int C = 0; C < CIPHER_COUNT; C++)
  582. {
  583. if (!CipherStr.AnsiCompareIC(CipherNames[C]))
  584. {
  585. Cipher[Index] = (TCipher)C;
  586. Used[C] = true;
  587. Index++;
  588. break;
  589. }
  590. }
  591. }
  592. for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
  593. {
  594. if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
  595. }
  596. }
  597. //---------------------------------------------------------------------
  598. AnsiString __fastcall TSessionData::GetCipherList()
  599. {
  600. AnsiString Result;
  601. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  602. {
  603. Result += AnsiString(Index ? "," : "") + CipherNames[Cipher[Index]];
  604. }
  605. return Result;
  606. }
  607. //---------------------------------------------------------------------
  608. void __fastcall TSessionData::SetPublicKeyFile(AnsiString value)
  609. {
  610. SET_SESSION_PROPERTY(PublicKeyFile);
  611. }
  612. //---------------------------------------------------------------------
  613. AnsiString __fastcall TSessionData::GetDefaultLogFileName()
  614. {
  615. return GetTemporaryPath() + MakeValidFileName(SessionName) + ".log";
  616. }
  617. //---------------------------------------------------------------------
  618. void __fastcall TSessionData::SetReturnVar(AnsiString value)
  619. {
  620. SET_SESSION_PROPERTY(ReturnVar);
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TSessionData::SetLookupUserGroups(bool value)
  624. {
  625. SET_SESSION_PROPERTY(LookupUserGroups);
  626. }
  627. //---------------------------------------------------------------------------
  628. void __fastcall TSessionData::SetEOLType(TEOLType value)
  629. {
  630. SET_SESSION_PROPERTY(EOLType);
  631. }
  632. //---------------------------------------------------------------------------
  633. void __fastcall TSessionData::SetTimeout(int value)
  634. {
  635. SET_SESSION_PROPERTY(Timeout);
  636. }
  637. //---------------------------------------------------------------------------
  638. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  639. {
  640. SET_SESSION_PROPERTY(FSProtocol);
  641. }
  642. //---------------------------------------------------------------------
  643. AnsiString __fastcall TSessionData::GetFSProtocolStr()
  644. {
  645. assert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  646. return FSProtocolNames[FSProtocol];
  647. }
  648. //---------------------------------------------------------------------------
  649. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  650. {
  651. if (value != DetectReturnVar)
  652. {
  653. ReturnVar = value ? "" : "$?";
  654. }
  655. }
  656. //---------------------------------------------------------------------------
  657. bool __fastcall TSessionData::GetDetectReturnVar()
  658. {
  659. return ReturnVar.IsEmpty();
  660. }
  661. //---------------------------------------------------------------------------
  662. void __fastcall TSessionData::SetDefaultShell(bool value)
  663. {
  664. if (value != DefaultShell)
  665. {
  666. Shell = value ? "" : "/bin/bash";
  667. }
  668. }
  669. //---------------------------------------------------------------------------
  670. bool __fastcall TSessionData::GetDefaultShell()
  671. {
  672. return Shell.IsEmpty();
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TSessionData::SetProtocolStr(AnsiString value)
  676. {
  677. for (int Index = 0; backends[Index].name != NULL; Index++)
  678. {
  679. if (!value.AnsiCompareIC(backends[Index].name))
  680. {
  681. FProtocol = (TProtocol)backends[Index].protocol;
  682. break;
  683. }
  684. }
  685. }
  686. //---------------------------------------------------------------------
  687. AnsiString __fastcall TSessionData::GetProtocolStr()
  688. {
  689. for (int Index = 0; backends[Index].name != NULL; Index++)
  690. {
  691. if ((TProtocol)backends[Index].protocol == Protocol)
  692. {
  693. return backends[Index].name;
  694. }
  695. }
  696. return "raw";
  697. }
  698. //---------------------------------------------------------------------
  699. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  700. {
  701. unsigned short hour, min, sec, msec;
  702. value.DecodeTime(&hour, &min, &sec, &msec);
  703. PingInterval = ((int)hour)*60*60 + ((int)min)*60 + sec;
  704. }
  705. //---------------------------------------------------------------------------
  706. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  707. {
  708. return TDateTime((unsigned short)(PingInterval/60/60),
  709. (unsigned short)(PingInterval/60%60), (unsigned short)(PingInterval%60), 0);
  710. }
  711. //---------------------------------------------------------------------------
  712. void __fastcall TSessionData::SetPingEnabled(bool value)
  713. {
  714. if (value && !FPingInterval) FPingInterval = 60;
  715. else
  716. if (!value) FPingInterval = 0;
  717. }
  718. //---------------------------------------------------------------------------
  719. bool __fastcall TSessionData::GetPingEnabled()
  720. {
  721. return (bool)(FPingInterval > 0);
  722. }
  723. //---------------------------------------------------------------------
  724. AnsiString __fastcall TSessionData::GetSessionName()
  725. {
  726. if (!Name.IsEmpty() && !TNamedObjectList::IsHidden(this)) return Name;
  727. else
  728. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  729. return Format("%s@%s", ARRAYOFCONST((UserName, HostName)));
  730. else
  731. if (!HostName.IsEmpty()) return HostName;
  732. else return "session";
  733. }
  734. //---------------------------------------------------------------------
  735. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  736. {
  737. SET_SESSION_PROPERTY(TimeDifference);
  738. }
  739. //---------------------------------------------------------------------
  740. void __fastcall TSessionData::SetLocalDirectory(AnsiString value)
  741. {
  742. SET_SESSION_PROPERTY(LocalDirectory);
  743. }
  744. //---------------------------------------------------------------------
  745. void __fastcall TSessionData::SetRemoteDirectory(AnsiString value)
  746. {
  747. SET_SESSION_PROPERTY(RemoteDirectory);
  748. }
  749. //---------------------------------------------------------------------
  750. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  751. {
  752. SET_SESSION_PROPERTY(UpdateDirectories);
  753. }
  754. //---------------------------------------------------------------------
  755. void __fastcall TSessionData::SetCacheDirectories(bool value)
  756. {
  757. SET_SESSION_PROPERTY(CacheDirectories);
  758. }
  759. //---------------------------------------------------------------------
  760. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  761. {
  762. SET_SESSION_PROPERTY(ResolveSymlinks);
  763. }
  764. //---------------------------------------------------------------------
  765. void __fastcall TSessionData::SetLockInHome(bool value)
  766. {
  767. SET_SESSION_PROPERTY(LockInHome);
  768. }
  769. //---------------------------------------------------------------------
  770. void __fastcall TSessionData::SetSpecial(bool value)
  771. {
  772. SET_SESSION_PROPERTY(Special);
  773. }
  774. //---------------------------------------------------------------------------
  775. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  776. {
  777. SET_SESSION_PROPERTY(Scp1Compatibility);
  778. }
  779. //---------------------------------------------------------------------
  780. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  781. {
  782. SET_SESSION_PROPERTY(TcpNoDelay);
  783. }
  784. //---------------------------------------------------------------------
  785. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  786. {
  787. SET_SESSION_PROPERTY(ProxyMethod);
  788. }
  789. //---------------------------------------------------------------------
  790. void __fastcall TSessionData::SetProxyHost(AnsiString value)
  791. {
  792. SET_SESSION_PROPERTY(ProxyHost);
  793. }
  794. //---------------------------------------------------------------------
  795. void __fastcall TSessionData::SetProxyPort(int value)
  796. {
  797. SET_SESSION_PROPERTY(ProxyPort);
  798. }
  799. //---------------------------------------------------------------------
  800. void __fastcall TSessionData::SetProxyUsername(AnsiString value)
  801. {
  802. SET_SESSION_PROPERTY(ProxyUsername);
  803. }
  804. //---------------------------------------------------------------------
  805. void __fastcall TSessionData::SetProxyPassword(AnsiString value)
  806. {
  807. // proxy password unencrypted to maintain compatibility with Putty
  808. SET_SESSION_PROPERTY(ProxyPassword);
  809. }
  810. //---------------------------------------------------------------------
  811. AnsiString __fastcall TSessionData::GetProxyPassword()
  812. {
  813. return FProxyPassword;
  814. }
  815. //---------------------------------------------------------------------
  816. void __fastcall TSessionData::SetProxyTelnetCommand(AnsiString value)
  817. {
  818. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  819. }
  820. //---------------------------------------------------------------------
  821. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  822. {
  823. SET_SESSION_PROPERTY(ProxyDNS);
  824. }
  825. //---------------------------------------------------------------------
  826. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  827. {
  828. SET_SESSION_PROPERTY(ProxyLocalhost);
  829. }
  830. //---------------------------------------------------------------------
  831. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  832. {
  833. assert(Bug >= 0 && Bug < LENOF(FBugs));
  834. SET_SESSION_PROPERTY(Bugs[Bug]);
  835. }
  836. //---------------------------------------------------------------------
  837. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug)
  838. {
  839. assert(Bug >= 0 && Bug < LENOF(FBugs));
  840. return FBugs[Bug];
  841. }
  842. //---------------------------------------------------------------------
  843. void __fastcall TSessionData::SetCustomParam1(AnsiString value)
  844. {
  845. SET_SESSION_PROPERTY(CustomParam1);
  846. }
  847. //---------------------------------------------------------------------
  848. void __fastcall TSessionData::SetCustomParam2(AnsiString value)
  849. {
  850. SET_SESSION_PROPERTY(CustomParam2);
  851. }
  852. //---------------------------------------------------------------------
  853. AnsiString __fastcall TSessionData::GetInfoTip()
  854. {
  855. return FmtLoadStr(SESSION_INFO_TIP,
  856. ARRAYOFCONST((
  857. (HostName.IsEmpty() ? AnsiString() : HostName),
  858. (UserName.IsEmpty() ? AnsiString() : UserName),
  859. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  860. SshProtStr,
  861. FSProtocolStr)));
  862. }
  863. //=== TStoredSessionList ----------------------------------------------
  864. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  865. TNamedObjectList(), FReadOnly(aReadOnly)
  866. {
  867. assert(Configuration);
  868. LastStorage = Configuration->Storage;
  869. FDefaultSettings = new TSessionData(DefaultSessionName);
  870. }
  871. //---------------------------------------------------------------------
  872. __fastcall TStoredSessionList::~TStoredSessionList()
  873. {
  874. assert(Configuration);
  875. if (!FReadOnly && (Configuration->Storage != LastStorage)) Save();
  876. delete FDefaultSettings;
  877. }
  878. //---------------------------------------------------------------------
  879. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage, bool AsModified)
  880. {
  881. TStringList *SubKeys = new TStringList();
  882. try {
  883. Storage->GetSubKeyNames(SubKeys);
  884. for (int Index = 0; Index < SubKeys->Count; Index++)
  885. {
  886. TSessionData *SessionData;
  887. AnsiString SessionName = UnMungeStr(SubKeys->Strings[Index]);
  888. if (SessionName == FDefaultSettings->Name) SessionData = FDefaultSettings;
  889. else SessionData = (TSessionData*)FindByName(SessionName);
  890. if (!SessionData)
  891. {
  892. SessionData = new TSessionData(SessionName);
  893. Add(SessionData);
  894. }
  895. SessionData->Load(Storage);
  896. if (AsModified)
  897. {
  898. SessionData->Modified = true;
  899. }
  900. }
  901. } __finally {
  902. delete SubKeys;
  903. }
  904. }
  905. //---------------------------------------------------------------------
  906. void __fastcall TStoredSessionList::Load(AnsiString aKey)
  907. {
  908. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  909. try {
  910. LastStorage = stRegistry;
  911. if (Storage->OpenRootKey(False)) Load(Storage);
  912. } __finally {
  913. delete Storage;
  914. }
  915. }
  916. //---------------------------------------------------------------------
  917. void __fastcall TStoredSessionList::Load()
  918. {
  919. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  920. try {
  921. LastStorage = Configuration->Storage;
  922. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  923. Load(Storage);
  924. } __finally {
  925. delete Storage;
  926. }
  927. }
  928. //---------------------------------------------------------------------
  929. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage)
  930. {
  931. for (int Index = 0; Index < Count+HiddenCount; Index++)
  932. {
  933. TSessionData *SessionData = (TSessionData *)Items[Index];
  934. if (SessionData->Modified)
  935. SessionData->Save(Storage);
  936. }
  937. if (FDefaultSettings->Modified)
  938. FDefaultSettings->Save(Storage);
  939. }
  940. //---------------------------------------------------------------------
  941. void __fastcall TStoredSessionList::Save(AnsiString aKey)
  942. {
  943. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  944. try
  945. {
  946. LastStorage = stRegistry;
  947. Storage->AccessMode = smReadWrite;
  948. if (Storage->OpenRootKey(True)) Save(Storage);
  949. }
  950. __finally
  951. {
  952. delete Storage;
  953. }
  954. }
  955. //---------------------------------------------------------------------
  956. void __fastcall TStoredSessionList::Save()
  957. {
  958. if (LastStorage != Configuration->Storage)
  959. {
  960. // make sure, that all data will be stored, when saving to new storage
  961. for (int Index = 0; Index < Count+HiddenCount; Index++)
  962. ((TSessionData*)Items[Index])->Modified = true;
  963. FDefaultSettings->Modified = true;
  964. LastStorage = Configuration->Storage;
  965. }
  966. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  967. try {
  968. Storage->AccessMode = smReadWrite;
  969. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, True))
  970. Save(Storage);
  971. } __finally {
  972. delete Storage;
  973. }
  974. }
  975. //---------------------------------------------------------------------
  976. void __fastcall TStoredSessionList::SelectAll(bool Select)
  977. {
  978. for (int Index = 0; Index < Count; Index++)
  979. Sessions[Index]->Selected = Select;
  980. }
  981. //---------------------------------------------------------------------
  982. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  983. bool OnlySelected)
  984. {
  985. for (int Index = 0; Index < From->Count; Index++)
  986. {
  987. if (!OnlySelected || From->Sessions[Index]->Selected)
  988. {
  989. TSessionData *Session = new TSessionData("");
  990. Session->Assign(From->Sessions[Index]);
  991. Session->Modified = true;
  992. Session->MakeUniqueIn(this);
  993. Add(Session);
  994. }
  995. }
  996. Save();
  997. }
  998. //---------------------------------------------------------------------
  999. void __fastcall TStoredSessionList::SelectSessionsToImport
  1000. (TStoredSessionList * Dest, bool SSHOnly)
  1001. {
  1002. for (int Index = 0; Index < Count; Index++)
  1003. {
  1004. Sessions[Index]->Selected =
  1005. (!SSHOnly || (Sessions[Index]->Protocol == ptSSH)) &&
  1006. !Dest->FindByName(Sessions[Index]->Name);
  1007. }
  1008. }
  1009. //---------------------------------------------------------------------
  1010. void __fastcall TStoredSessionList::Cleanup()
  1011. {
  1012. try {
  1013. if (Configuration->Storage == stRegistry) Clear();
  1014. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  1015. try {
  1016. Storage->AccessMode = smReadWrite;
  1017. if (Storage->OpenRootKey(False))
  1018. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  1019. } __finally {
  1020. delete Storage;
  1021. }
  1022. } catch (Exception &E) {
  1023. throw ExtException(&E, CLEANUP_SESSIONS_ERROR);
  1024. }
  1025. }
  1026. //---------------------------------------------------------------------------
  1027. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  1028. {
  1029. for (int Index = 0; Index < Count; Index++)
  1030. if (Data == Sessions[Index]) return Index;
  1031. return -1;
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. TSessionData * __fastcall TStoredSessionList::NewSession(
  1035. AnsiString SessionName, TSessionData * Session)
  1036. {
  1037. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  1038. if (!DuplicateSession)
  1039. {
  1040. DuplicateSession = new TSessionData("");
  1041. DuplicateSession->Assign(Session);
  1042. DuplicateSession->Name = SessionName;
  1043. // make sure, that new stored session is saved to registry
  1044. DuplicateSession->Modified = true;
  1045. Add(DuplicateSession);
  1046. }
  1047. else
  1048. {
  1049. DuplicateSession->Assign(Session);
  1050. DuplicateSession->Name = SessionName;
  1051. DuplicateSession->Modified = true;
  1052. }
  1053. // list was saved here before to default storage, but it would not allow
  1054. // to work with special lists (export/import) not using default storage
  1055. return DuplicateSession;
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  1059. {
  1060. assert(FDefaultSettings);
  1061. if (FDefaultSettings != value)
  1062. {
  1063. FDefaultSettings->Assign(value);
  1064. FDefaultSettings->Name = DefaultSessionName;
  1065. Save();
  1066. }
  1067. }