SessionData.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. #include "RemoteFiles.h"
  14. //---------------------------------------------------------------------------
  15. #pragma package(smart_init)
  16. //---------------------------------------------------------------------------
  17. const char * DefaultSessionName = "Default Settings";
  18. const char CipherNames[CIPHER_COUNT][10] = {"WARN", "3des", "blowfish", "aes", "des"};
  19. const char SshProtList[][10] = {"1 only", "1", "2", "2 only"};
  20. const char ProxyMethodList[][10] = {"none", "SOCKS4", "SOCKS5", "HTTP", "Telnet", "Cmd" };
  21. const TCipher DefaultCipherList[CIPHER_COUNT] =
  22. { cipAES, cipBlowfish, cip3DES, cipWarn, cipDES };
  23. const char FSProtocolNames[FSPROTOCOL_COUNT][11] = { "SCP", "SFTP (SCP)", "SFTP" };
  24. //--- TSessionData ----------------------------------------------------
  25. __fastcall TSessionData::TSessionData(AnsiString aName):
  26. TNamedObject(aName)
  27. {
  28. Default();
  29. FModified = true;
  30. }
  31. //---------------------------------------------------------------------
  32. void __fastcall TSessionData::Default()
  33. {
  34. HostName = "";
  35. PortNumber = default_port;
  36. UserName = "";
  37. Password = "";
  38. PingInterval = 30;
  39. PingType = ptOff;
  40. Timeout = 15;
  41. AgentFwd = false;
  42. AuthTIS = false;
  43. AuthKI = true;
  44. AuthKIPassword = true;
  45. AuthGSSAPI = false;
  46. Compression = false;
  47. SshProt = ssh2;
  48. Ssh2DES = false;
  49. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  50. {
  51. Cipher[Index] = DefaultCipherList[Index];
  52. }
  53. PublicKeyFile = "";
  54. FProtocol = ptSSH;
  55. TcpNoDelay = true;
  56. ProxyMethod = pmNone;
  57. ProxyHost = "proxy";
  58. ProxyPort = 80;
  59. ProxyUsername = "";
  60. ProxyPassword = "";
  61. ProxyTelnetCommand = "connect %host %port\\n";
  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. // Putty uses PingIntervalSecs
  303. int PingIntervalSecs = Storage->ReadInteger("PingIntervalSecs", -1);
  304. if (PingIntervalSecs < 0)
  305. {
  306. PingIntervalSecs = Storage->ReadInteger("PingIntervalSec", PingInterval%60);
  307. }
  308. PingInterval =
  309. Storage->ReadInteger("PingInterval", PingInterval/60)*60 +
  310. PingIntervalSecs;
  311. PingType = static_cast<TPingType>
  312. (Storage->ReadInteger("PingType", PingInterval > 0 ? ptNullPacket : ptOff));
  313. if (PingInterval == 0)
  314. {
  315. PingInterval = 30;
  316. }
  317. Timeout = Storage->ReadInteger("Timeout", Timeout);
  318. AgentFwd = Storage->ReadBool("AgentFwd", AgentFwd);
  319. AuthTIS = Storage->ReadBool("AuthTIS", AuthTIS);
  320. AuthKI = Storage->ReadBool("AuthKI", AuthKI);
  321. AuthKIPassword = Storage->ReadBool("AuthKIPassword", AuthKIPassword);
  322. AuthGSSAPI = Storage->ReadBool("AuthGSSAPI", AuthGSSAPI);
  323. Compression = Storage->ReadBool("Compression", Compression);
  324. SshProt = (TSshProt)Storage->ReadInteger("SshProt", SshProt);
  325. Ssh2DES = Storage->ReadBool("Ssh2DES", Ssh2DES);
  326. CipherList = Storage->ReadString("Cipher", CipherList);
  327. PublicKeyFile = Storage->ReadString("PublicKeyFile", PublicKeyFile);
  328. FSProtocol = (TFSProtocol)Storage->ReadInteger("FSProtocol", FSProtocol);
  329. LocalDirectory = Storage->ReadString("LocalDirectory", LocalDirectory);
  330. RemoteDirectory = Storage->ReadString("RemoteDirectory", RemoteDirectory);
  331. UpdateDirectories = Storage->ReadBool("UpdateDirectories", UpdateDirectories);
  332. CacheDirectories = Storage->ReadBool("CacheDirectories", CacheDirectories);
  333. CacheDirectoryChanges = Storage->ReadBool("CacheDirectoryChanges", CacheDirectoryChanges);
  334. PreserveDirectoryChanges = Storage->ReadBool("PreserveDirectoryChanges", PreserveDirectoryChanges);
  335. ResolveSymlinks = Storage->ReadBool("ResolveSymlinks", ResolveSymlinks);
  336. ConsiderDST = Storage->ReadBool("ConsiderDST", ConsiderDST);
  337. LockInHome = Storage->ReadBool("LockInHome", LockInHome);
  338. Special = Storage->ReadBool("Special", Special);
  339. Shell = Storage->ReadString("Shell", Shell);
  340. ClearAliases = Storage->ReadBool("ClearAliases", ClearAliases);
  341. UnsetNationalVars = Storage->ReadBool("UnsetNationalVars", UnsetNationalVars);
  342. AliasGroupList = Storage->ReadBool("AliasGroupList", AliasGroupList);
  343. IgnoreLsWarnings = Storage->ReadBool("IgnoreLsWarnings", IgnoreLsWarnings);
  344. Scp1Compatibility = Storage->ReadBool("Scp1Compatibility", Scp1Compatibility);
  345. TimeDifference = Storage->ReadFloat("TimeDifference", TimeDifference);
  346. ReturnVar = Storage->ReadString("ReturnVar", ReturnVar);
  347. LookupUserGroups = Storage->ReadBool("LookupUserGroups", LookupUserGroups);
  348. EOLType = (TEOLType)Storage->ReadInteger("EOLType", EOLType);
  349. // new in 53b
  350. TcpNoDelay = Storage->ReadBool("TcpNoDelay", TcpNoDelay);
  351. ProxyMethod = (TProxyMethod)Storage->ReadInteger("ProxyMethod", -1);
  352. if (ProxyMethod < 0)
  353. {
  354. int ProxyType = Storage->ReadInteger("ProxyType", pxNone);
  355. int ProxySOCKSVersion;
  356. switch (ProxyType) {
  357. case pxHTTP:
  358. ProxyMethod = pmHTTP;
  359. break;
  360. case pxTelnet:
  361. ProxyMethod = pmTelnet;
  362. break;
  363. case pxSocks:
  364. ProxySOCKSVersion = Storage->ReadInteger("ProxySOCKSVersion", 5);
  365. ProxyMethod = ProxySOCKSVersion == 5 ? pmSocks5 : pmSocks4;
  366. break;
  367. default:
  368. case pxNone:
  369. ProxyMethod = pmNone;
  370. break;
  371. }
  372. }
  373. ProxyHost = Storage->ReadString("ProxyHost", ProxyHost);
  374. ProxyPort = Storage->ReadInteger("ProxyPort", ProxyPort);
  375. ProxyUsername = Storage->ReadString("ProxyUsername", ProxyUsername);
  376. FProxyPassword = Storage->ReadString("ProxyPassword", FProxyPassword);
  377. ProxyTelnetCommand = Storage->ReadStringRaw("ProxyTelnetCommand", ProxyTelnetCommand);
  378. ProxyDNS = TAutoSwitch((Storage->ReadInteger("ProxyDNS", (ProxyDNS + 2) % 3) + 1) % 3);
  379. ProxyLocalhost = Storage->ReadBool("ProxyLocalhost", ProxyLocalhost);
  380. #define READ_BUG(BUG) \
  381. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger("Bug"#BUG, \
  382. 2 - Bug[sb##BUG]));
  383. READ_BUG(Ignore1);
  384. READ_BUG(PlainPW1);
  385. READ_BUG(RSA1);
  386. READ_BUG(HMAC2);
  387. READ_BUG(DeriveKey2);
  388. READ_BUG(RSAPad2);
  389. READ_BUG(DHGEx2);
  390. READ_BUG(PKSessID2);
  391. #undef READ_BUG
  392. if ((Bug[sbHMAC2] == asAuto) &&
  393. Storage->ReadBool("BuggyMAC", false))
  394. {
  395. Bug[sbHMAC2] = asOn;
  396. }
  397. SFTPSymlinkBug = TAutoSwitch(Storage->ReadInteger("SFTPSymlinkBug", SFTPSymlinkBug));
  398. // read only (used only on Import from Putty dialog)
  399. ProtocolStr = Storage->ReadString("Protocol", ProtocolStr);
  400. CustomParam1 = Storage->ReadString("CustomParam1", CustomParam1);
  401. CustomParam2 = Storage->ReadString("CustomParam2", CustomParam2);
  402. Storage->CloseSubKey();
  403. };
  404. FModified = false;
  405. }
  406. //---------------------------------------------------------------------
  407. void __fastcall TSessionData::Save(THierarchicalStorage * Storage,
  408. bool PuttyExport, const TSessionData * Default)
  409. {
  410. if (Modified && Storage->OpenSubKey(StorageKey, true))
  411. {
  412. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  413. if ((Default != NULL) && (CONV(Default->PROPERTY) == CONV(PROPERTY))) \
  414. { \
  415. Storage->DeleteValue(NAME); \
  416. } \
  417. else \
  418. { \
  419. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  420. }
  421. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  422. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, #PROPERTY, PROPERTY, )
  423. WRITE_DATA(String, HostName);
  424. WRITE_DATA(Integer, PortNumber);
  425. WRITE_DATA(String, UserName);
  426. if (!Configuration->DisablePasswordStoring && !PuttyExport && !Password.IsEmpty())
  427. {
  428. WRITE_DATA_EX(String, "Password", FPassword, );
  429. }
  430. else
  431. {
  432. Storage->DeleteValue("Password");
  433. }
  434. WRITE_DATA_EX(Integer, "PingInterval", PingInterval / 60, );
  435. WRITE_DATA_EX(Integer, "PingIntervalSecs", PingInterval % 60, );
  436. Storage->DeleteValue("PingIntervalSec"); // obsolete
  437. // store PingType always as it does not have fixed default
  438. Storage->WriteInteger("PingType", PingType);
  439. WRITE_DATA(Integer, Timeout);
  440. WRITE_DATA(Bool, AgentFwd);
  441. WRITE_DATA(Bool, AuthTIS);
  442. WRITE_DATA(Bool, AuthKI);
  443. WRITE_DATA(Bool, AuthKIPassword);
  444. WRITE_DATA(Bool, Compression);
  445. WRITE_DATA(Integer, SshProt);
  446. WRITE_DATA(Bool, Ssh2DES);
  447. WRITE_DATA_EX(String, "Cipher", CipherList, );
  448. WRITE_DATA(Bool, TcpNoDelay);
  449. if (PuttyExport)
  450. {
  451. WRITE_DATA(StringRaw, PublicKeyFile);
  452. }
  453. else
  454. {
  455. WRITE_DATA(Bool, AuthGSSAPI);
  456. WRITE_DATA(String, PublicKeyFile);
  457. WRITE_DATA(Integer, FSProtocol);
  458. WRITE_DATA(String, LocalDirectory);
  459. WRITE_DATA(String, RemoteDirectory);
  460. WRITE_DATA(Bool, UpdateDirectories);
  461. WRITE_DATA(Bool, CacheDirectories);
  462. WRITE_DATA(Bool, CacheDirectoryChanges);
  463. WRITE_DATA(Bool, PreserveDirectoryChanges);
  464. WRITE_DATA(Bool, ResolveSymlinks);
  465. WRITE_DATA(Bool, ConsiderDST);
  466. WRITE_DATA(Bool, LockInHome);
  467. // Special is never stored (if it would, login dialog must be modified not to
  468. // duplicate Special parameter when Special session is loaded and then stored
  469. // under different name)
  470. // WRITE_DATA(Bool, Special);
  471. WRITE_DATA(String, Shell);
  472. WRITE_DATA(Bool, ClearAliases);
  473. WRITE_DATA(Bool, UnsetNationalVars);
  474. WRITE_DATA(Bool, AliasGroupList);
  475. WRITE_DATA(Bool, IgnoreLsWarnings);
  476. WRITE_DATA(Bool, Scp1Compatibility);
  477. WRITE_DATA(Float, TimeDifference);
  478. WRITE_DATA(String, ReturnVar);
  479. WRITE_DATA(Bool, LookupUserGroups);
  480. WRITE_DATA(Integer, EOLType);
  481. }
  482. WRITE_DATA(Integer, ProxyMethod);
  483. if (PuttyExport)
  484. {
  485. // support for Putty 0.53b and older
  486. int ProxyType;
  487. int ProxySOCKSVersion = 5;
  488. switch (ProxyMethod) {
  489. case pmHTTP:
  490. ProxyType = pxHTTP;
  491. break;
  492. case pmTelnet:
  493. ProxyType = pxTelnet;
  494. break;
  495. case pmSocks5:
  496. ProxyType = pxSocks;
  497. ProxySOCKSVersion = 5;
  498. break;
  499. case pmSocks4:
  500. ProxyType = pxSocks;
  501. ProxySOCKSVersion = 4;
  502. break;
  503. default:
  504. case pmNone:
  505. ProxyType = pxNone;
  506. break;
  507. }
  508. Storage->WriteInteger("ProxyType", ProxyType);
  509. Storage->WriteInteger("ProxySOCKSVersion", ProxySOCKSVersion);
  510. }
  511. else
  512. {
  513. Storage->DeleteValue("ProxyType");
  514. Storage->DeleteValue("ProxySOCKSVersion");
  515. }
  516. WRITE_DATA(String, ProxyHost);
  517. WRITE_DATA(Integer, ProxyPort);
  518. WRITE_DATA(String, ProxyUsername);
  519. WRITE_DATA_EX(String, "ProxyPassword", FProxyPassword, );
  520. WRITE_DATA(StringRaw, ProxyTelnetCommand);
  521. #define WRITE_DATA_CONV_FUNC(X) (((X) + 2) % 3)
  522. WRITE_DATA_CONV(Integer, "ProxyDNS", ProxyDNS);
  523. #undef WRITE_DATA_CONV_FUNC
  524. WRITE_DATA(Bool, ProxyLocalhost);
  525. #define WRITE_DATA_CONV_FUNC(X) (2 - (X))
  526. #define WRITE_BUG(BUG) WRITE_DATA_CONV(Integer, "Bug" #BUG, Bug[sb##BUG]);
  527. WRITE_BUG(Ignore1);
  528. WRITE_BUG(PlainPW1);
  529. WRITE_BUG(RSA1);
  530. WRITE_BUG(HMAC2);
  531. WRITE_BUG(DeriveKey2);
  532. WRITE_BUG(RSAPad2);
  533. WRITE_BUG(DHGEx2);
  534. WRITE_BUG(PKSessID2);
  535. #undef WRITE_BUG
  536. #undef WRITE_DATA_CONV_FUNC
  537. Storage->DeleteValue("BuggyMAC");
  538. if (PuttyExport)
  539. {
  540. WRITE_DATA_EX(String, "Protocol", ProtocolStr, );
  541. }
  542. if (!PuttyExport)
  543. {
  544. WRITE_DATA(Integer, SFTPSymlinkBug);
  545. WRITE_DATA(String, CustomParam1);
  546. WRITE_DATA(String, CustomParam2);
  547. }
  548. Storage->CloseSubKey();
  549. FModified = false;
  550. }
  551. }
  552. //---------------------------------------------------------------------
  553. void __fastcall TSessionData::Remove()
  554. {
  555. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  556. try
  557. {
  558. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  559. {
  560. Storage->RecursiveDeleteSubKey(StorageKey);
  561. }
  562. }
  563. __finally
  564. {
  565. delete Storage;
  566. }
  567. }
  568. //---------------------------------------------------------------------
  569. bool __fastcall TSessionData::ParseUrl(AnsiString Url, int Params,
  570. AnsiString * ConnectInfo, AnsiString * HostName, int * PortNumber,
  571. AnsiString * UserName, AnsiString * Password, AnsiString * Path,
  572. AnsiString * FileName)
  573. {
  574. int PSlash = Url.Pos("/");
  575. if (PSlash == 0)
  576. {
  577. PSlash = Url.Length() + 1;
  578. }
  579. AnsiString AConnectInfo;
  580. AConnectInfo = Url.SubString(1, PSlash - 1);
  581. int P = AConnectInfo.Pos("@");
  582. bool Result = (P > 0) || ((Params & puRequireUsername) == 0);
  583. if (Result)
  584. {
  585. if ((Path != NULL) || (FileName != NULL))
  586. {
  587. bool ExcludeLeadingSlash = (Params & puExcludeLeadingSlash) != 0;
  588. int Delta = ExcludeLeadingSlash ? 1 : 0;
  589. AnsiString APath = Url.SubString(PSlash + Delta,
  590. Url.Length() - PSlash - Delta + 1);
  591. if (ExcludeLeadingSlash || (APath != "/"))
  592. {
  593. if ((APath.Length() > 0) && (APath[APath.Length()] != '/'))
  594. {
  595. if (FileName != NULL)
  596. {
  597. *FileName = UnixExtractFileName(APath);
  598. }
  599. if (FLAGSET(Params, puExtractFileName))
  600. {
  601. APath = UnixExtractFilePath(APath);
  602. }
  603. }
  604. if (Path != NULL)
  605. {
  606. *Path = APath;
  607. }
  608. }
  609. }
  610. if (ConnectInfo != NULL)
  611. {
  612. *ConnectInfo = AConnectInfo;
  613. }
  614. AnsiString UserInfo;
  615. AnsiString HostInfo;
  616. if (P > 0)
  617. {
  618. UserInfo = AConnectInfo.SubString(1, P - 1);
  619. HostInfo = AConnectInfo.SubString(P + 1, AConnectInfo.Length() - P);
  620. }
  621. else
  622. {
  623. HostInfo = AConnectInfo;
  624. }
  625. if (HostName != NULL)
  626. {
  627. *HostName = CutToChar(HostInfo, ':', true);
  628. }
  629. else
  630. {
  631. CutToChar(HostInfo, ':', true);
  632. }
  633. if (PortNumber != NULL)
  634. {
  635. *PortNumber = HostInfo.IsEmpty() ? -1 : StrToIntDef(HostInfo, -1);
  636. }
  637. if (UserName != NULL)
  638. {
  639. *UserName = CutToChar(UserInfo, ':', false);
  640. }
  641. else
  642. {
  643. CutToChar(UserInfo, ':', false);
  644. }
  645. if (Password != NULL)
  646. {
  647. *Password = UserInfo;
  648. }
  649. }
  650. return Result;
  651. }
  652. //---------------------------------------------------------------------
  653. bool __fastcall TSessionData::ParseUrl(AnsiString Url, int Params,
  654. AnsiString * FileName)
  655. {
  656. AnsiString AHostName = HostName;
  657. int APortNumber = PortNumber;
  658. AnsiString AUserName = UserName;
  659. AnsiString APassword = Password;
  660. AnsiString ARemoteDirectory = RemoteDirectory;
  661. bool Result = ParseUrl(Url, Params, NULL, &AHostName, &APortNumber,
  662. &AUserName, &APassword, &ARemoteDirectory, FileName);
  663. if (Result)
  664. {
  665. HostName = AHostName;
  666. if (APortNumber >= 0)
  667. {
  668. PortNumber = APortNumber;
  669. }
  670. UserName = AUserName;
  671. Password = APassword;
  672. RemoteDirectory = ARemoteDirectory;
  673. }
  674. return Result;
  675. }
  676. //---------------------------------------------------------------------
  677. bool __fastcall TSessionData::GetCanLogin()
  678. {
  679. return !FHostName.IsEmpty() && !FUserName.IsEmpty();
  680. }
  681. //---------------------------------------------------------------------------
  682. AnsiString __fastcall TSessionData::GetSessionKey()
  683. {
  684. return FORMAT("%s@%s", (UserName, HostName));
  685. }
  686. //---------------------------------------------------------------------
  687. AnsiString __fastcall TSessionData::GetStorageKey()
  688. {
  689. return MungeStr(Name);
  690. }
  691. //---------------------------------------------------------------------
  692. void __fastcall TSessionData::SetHostName(AnsiString value)
  693. {
  694. if (FHostName != value)
  695. {
  696. // HostName is key for password encryption
  697. AnsiString XPassword = Password;
  698. int P = value.LastDelimiter("@");
  699. if (P > 0)
  700. {
  701. UserName = value.SubString(1, P - 1);
  702. value = value.SubString(P + 1, value.Length() - P);
  703. }
  704. FHostName = value;
  705. FModified = true;
  706. Password = XPassword;
  707. if (!XPassword.IsEmpty())
  708. {
  709. memset(XPassword.c_str(), 0, XPassword.Length());
  710. }
  711. }
  712. }
  713. //---------------------------------------------------------------------
  714. void __fastcall TSessionData::SetPortNumber(int value)
  715. {
  716. SET_SESSION_PROPERTY(PortNumber);
  717. }
  718. //---------------------------------------------------------------------------
  719. void __fastcall TSessionData::SetShell(AnsiString value)
  720. {
  721. SET_SESSION_PROPERTY(Shell);
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TSessionData::SetClearAliases(bool value)
  725. {
  726. SET_SESSION_PROPERTY(ClearAliases);
  727. }
  728. //---------------------------------------------------------------------------
  729. void __fastcall TSessionData::SetAliasGroupList(bool value)
  730. {
  731. SET_SESSION_PROPERTY(AliasGroupList);
  732. }
  733. //---------------------------------------------------------------------------
  734. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  735. {
  736. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  737. }
  738. //---------------------------------------------------------------------------
  739. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  740. {
  741. SET_SESSION_PROPERTY(UnsetNationalVars);
  742. }
  743. //---------------------------------------------------------------------
  744. void __fastcall TSessionData::SetUserName(AnsiString value)
  745. {
  746. // UserName is key for password encryption
  747. AnsiString XPassword = Password;
  748. SET_SESSION_PROPERTY(UserName);
  749. Password = XPassword;
  750. if (!XPassword.IsEmpty())
  751. {
  752. memset(XPassword.c_str(), 0, XPassword.Length());
  753. }
  754. }
  755. //---------------------------------------------------------------------
  756. void __fastcall TSessionData::SetPassword(AnsiString value)
  757. {
  758. value = EncryptPassword(value, UserName+HostName);
  759. SET_SESSION_PROPERTY(Password);
  760. }
  761. //---------------------------------------------------------------------
  762. AnsiString __fastcall TSessionData::GetPassword()
  763. {
  764. return DecryptPassword(FPassword, UserName+HostName);
  765. }
  766. //---------------------------------------------------------------------
  767. void __fastcall TSessionData::SetPingInterval(int value)
  768. {
  769. SET_SESSION_PROPERTY(PingInterval);
  770. }
  771. //---------------------------------------------------------------------
  772. void __fastcall TSessionData::SetAgentFwd(bool value)
  773. {
  774. SET_SESSION_PROPERTY(AgentFwd);
  775. }
  776. //---------------------------------------------------------------------
  777. void __fastcall TSessionData::SetAuthTIS(bool value)
  778. {
  779. SET_SESSION_PROPERTY(AuthTIS);
  780. }
  781. //---------------------------------------------------------------------
  782. void __fastcall TSessionData::SetAuthKI(bool value)
  783. {
  784. SET_SESSION_PROPERTY(AuthKI);
  785. }
  786. //---------------------------------------------------------------------
  787. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  788. {
  789. SET_SESSION_PROPERTY(AuthKIPassword);
  790. }
  791. //---------------------------------------------------------------------
  792. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  793. {
  794. SET_SESSION_PROPERTY(AuthGSSAPI);
  795. }
  796. //---------------------------------------------------------------------
  797. void __fastcall TSessionData::SetCompression(bool value)
  798. {
  799. SET_SESSION_PROPERTY(Compression);
  800. }
  801. //---------------------------------------------------------------------
  802. void __fastcall TSessionData::SetSshProt(TSshProt value)
  803. {
  804. SET_SESSION_PROPERTY(SshProt);
  805. }
  806. //---------------------------------------------------------------------
  807. void __fastcall TSessionData::SetSsh2DES(bool value)
  808. {
  809. SET_SESSION_PROPERTY(Ssh2DES);
  810. }
  811. //---------------------------------------------------------------------
  812. AnsiString __fastcall TSessionData::GetSshProtStr()
  813. {
  814. return SshProtList[FSshProt];
  815. }
  816. //---------------------------------------------------------------------
  817. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  818. {
  819. assert(Index >= 0 && Index < CIPHER_COUNT);
  820. SET_SESSION_PROPERTY(Ciphers[Index]);
  821. }
  822. //---------------------------------------------------------------------
  823. TCipher __fastcall TSessionData::GetCipher(int Index) const
  824. {
  825. assert(Index >= 0 && Index < CIPHER_COUNT);
  826. return FCiphers[Index];
  827. }
  828. //---------------------------------------------------------------------
  829. void __fastcall TSessionData::SetCipherList(AnsiString value)
  830. {
  831. bool Used[CIPHER_COUNT];
  832. for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
  833. AnsiString CipherStr;
  834. int Index = 0;
  835. while (!value.IsEmpty() && (Index < CIPHER_COUNT))
  836. {
  837. CipherStr = CutToChar(value, ',', true);
  838. for (int C = 0; C < CIPHER_COUNT; C++)
  839. {
  840. if (!CipherStr.AnsiCompareIC(CipherNames[C]))
  841. {
  842. Cipher[Index] = (TCipher)C;
  843. Used[C] = true;
  844. Index++;
  845. break;
  846. }
  847. }
  848. }
  849. for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
  850. {
  851. if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
  852. }
  853. }
  854. //---------------------------------------------------------------------
  855. AnsiString __fastcall TSessionData::GetCipherList() const
  856. {
  857. AnsiString Result;
  858. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  859. {
  860. Result += AnsiString(Index ? "," : "") + CipherNames[Cipher[Index]];
  861. }
  862. return Result;
  863. }
  864. //---------------------------------------------------------------------
  865. void __fastcall TSessionData::SetPublicKeyFile(AnsiString value)
  866. {
  867. if (FPublicKeyFile != value)
  868. {
  869. FPublicKeyFile = StripPathQuotes(value);
  870. FModified = true;
  871. }
  872. }
  873. //---------------------------------------------------------------------
  874. AnsiString __fastcall TSessionData::GetDefaultLogFileName()
  875. {
  876. return GetTemporaryPath() + MakeValidFileName(SessionName) + ".log";
  877. }
  878. //---------------------------------------------------------------------
  879. void __fastcall TSessionData::SetReturnVar(AnsiString value)
  880. {
  881. SET_SESSION_PROPERTY(ReturnVar);
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TSessionData::SetLookupUserGroups(bool value)
  885. {
  886. SET_SESSION_PROPERTY(LookupUserGroups);
  887. }
  888. //---------------------------------------------------------------------------
  889. void __fastcall TSessionData::SetEOLType(TEOLType value)
  890. {
  891. SET_SESSION_PROPERTY(EOLType);
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TSessionData::SetTimeout(int value)
  895. {
  896. SET_SESSION_PROPERTY(Timeout);
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  900. {
  901. SET_SESSION_PROPERTY(FSProtocol);
  902. }
  903. //---------------------------------------------------------------------
  904. AnsiString __fastcall TSessionData::GetFSProtocolStr()
  905. {
  906. assert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  907. return FSProtocolNames[FSProtocol];
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  911. {
  912. if (value != DetectReturnVar)
  913. {
  914. ReturnVar = value ? "" : "$?";
  915. }
  916. }
  917. //---------------------------------------------------------------------------
  918. bool __fastcall TSessionData::GetDetectReturnVar()
  919. {
  920. return ReturnVar.IsEmpty();
  921. }
  922. //---------------------------------------------------------------------------
  923. void __fastcall TSessionData::SetDefaultShell(bool value)
  924. {
  925. if (value != DefaultShell)
  926. {
  927. Shell = value ? "" : "/bin/bash";
  928. }
  929. }
  930. //---------------------------------------------------------------------------
  931. bool __fastcall TSessionData::GetDefaultShell()
  932. {
  933. return Shell.IsEmpty();
  934. }
  935. //---------------------------------------------------------------------------
  936. void __fastcall TSessionData::SetProtocolStr(AnsiString value)
  937. {
  938. for (int Index = 0; backends[Index].name != NULL; Index++)
  939. {
  940. if (!value.AnsiCompareIC(backends[Index].name))
  941. {
  942. FProtocol = (TProtocol)backends[Index].protocol;
  943. break;
  944. }
  945. }
  946. }
  947. //---------------------------------------------------------------------
  948. AnsiString __fastcall TSessionData::GetProtocolStr() const
  949. {
  950. for (int Index = 0; backends[Index].name != NULL; Index++)
  951. {
  952. if ((TProtocol)backends[Index].protocol == Protocol)
  953. {
  954. return backends[Index].name;
  955. }
  956. }
  957. return "raw";
  958. }
  959. //---------------------------------------------------------------------
  960. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  961. {
  962. unsigned short hour, min, sec, msec;
  963. value.DecodeTime(&hour, &min, &sec, &msec);
  964. PingInterval = ((int)hour)*60*60 + ((int)min)*60 + sec;
  965. }
  966. //---------------------------------------------------------------------------
  967. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  968. {
  969. return TDateTime((unsigned short)(PingInterval/60/60),
  970. (unsigned short)(PingInterval/60%60), (unsigned short)(PingInterval%60), 0);
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TSessionData::SetPingType(TPingType value)
  974. {
  975. SET_SESSION_PROPERTY(PingType);
  976. }
  977. //---------------------------------------------------------------------
  978. AnsiString __fastcall TSessionData::GetSessionName()
  979. {
  980. if (!Name.IsEmpty() && !TNamedObjectList::IsHidden(this) &&
  981. (Name != DefaultSessionName))
  982. {
  983. return Name;
  984. }
  985. else if (!HostName.IsEmpty() && !UserName.IsEmpty())
  986. {
  987. return FORMAT("%s@%s", (UserName, HostName));
  988. }
  989. else if (!HostName.IsEmpty())
  990. {
  991. return HostName;
  992. }
  993. else
  994. {
  995. return "session";
  996. }
  997. }
  998. //---------------------------------------------------------------------
  999. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  1000. {
  1001. SET_SESSION_PROPERTY(TimeDifference);
  1002. }
  1003. //---------------------------------------------------------------------
  1004. void __fastcall TSessionData::SetLocalDirectory(AnsiString value)
  1005. {
  1006. SET_SESSION_PROPERTY(LocalDirectory);
  1007. }
  1008. //---------------------------------------------------------------------
  1009. void __fastcall TSessionData::SetRemoteDirectory(AnsiString value)
  1010. {
  1011. SET_SESSION_PROPERTY(RemoteDirectory);
  1012. }
  1013. //---------------------------------------------------------------------
  1014. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  1015. {
  1016. SET_SESSION_PROPERTY(UpdateDirectories);
  1017. }
  1018. //---------------------------------------------------------------------
  1019. void __fastcall TSessionData::SetCacheDirectories(bool value)
  1020. {
  1021. SET_SESSION_PROPERTY(CacheDirectories);
  1022. }
  1023. //---------------------------------------------------------------------
  1024. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  1025. {
  1026. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  1027. }
  1028. //---------------------------------------------------------------------
  1029. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  1030. {
  1031. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  1032. }
  1033. //---------------------------------------------------------------------
  1034. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  1035. {
  1036. SET_SESSION_PROPERTY(ResolveSymlinks);
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. void __fastcall TSessionData::SetConsiderDST(bool value)
  1040. {
  1041. SET_SESSION_PROPERTY(ConsiderDST);
  1042. }
  1043. //---------------------------------------------------------------------
  1044. void __fastcall TSessionData::SetLockInHome(bool value)
  1045. {
  1046. SET_SESSION_PROPERTY(LockInHome);
  1047. }
  1048. //---------------------------------------------------------------------
  1049. void __fastcall TSessionData::SetSpecial(bool value)
  1050. {
  1051. SET_SESSION_PROPERTY(Special);
  1052. }
  1053. //---------------------------------------------------------------------------
  1054. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  1055. {
  1056. SET_SESSION_PROPERTY(Scp1Compatibility);
  1057. }
  1058. //---------------------------------------------------------------------
  1059. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  1060. {
  1061. SET_SESSION_PROPERTY(TcpNoDelay);
  1062. }
  1063. //---------------------------------------------------------------------
  1064. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  1065. {
  1066. SET_SESSION_PROPERTY(ProxyMethod);
  1067. }
  1068. //---------------------------------------------------------------------
  1069. void __fastcall TSessionData::SetProxyHost(AnsiString value)
  1070. {
  1071. SET_SESSION_PROPERTY(ProxyHost);
  1072. }
  1073. //---------------------------------------------------------------------
  1074. void __fastcall TSessionData::SetProxyPort(int value)
  1075. {
  1076. SET_SESSION_PROPERTY(ProxyPort);
  1077. }
  1078. //---------------------------------------------------------------------
  1079. void __fastcall TSessionData::SetProxyUsername(AnsiString value)
  1080. {
  1081. SET_SESSION_PROPERTY(ProxyUsername);
  1082. }
  1083. //---------------------------------------------------------------------
  1084. void __fastcall TSessionData::SetProxyPassword(AnsiString value)
  1085. {
  1086. // proxy password unencrypted to maintain compatibility with Putty
  1087. SET_SESSION_PROPERTY(ProxyPassword);
  1088. }
  1089. //---------------------------------------------------------------------
  1090. AnsiString __fastcall TSessionData::GetProxyPassword()
  1091. {
  1092. return FProxyPassword;
  1093. }
  1094. //---------------------------------------------------------------------
  1095. void __fastcall TSessionData::SetProxyTelnetCommand(AnsiString value)
  1096. {
  1097. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  1098. }
  1099. //---------------------------------------------------------------------
  1100. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  1101. {
  1102. SET_SESSION_PROPERTY(ProxyDNS);
  1103. }
  1104. //---------------------------------------------------------------------
  1105. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  1106. {
  1107. SET_SESSION_PROPERTY(ProxyLocalhost);
  1108. }
  1109. //---------------------------------------------------------------------
  1110. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  1111. {
  1112. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1113. SET_SESSION_PROPERTY(Bugs[Bug]);
  1114. }
  1115. //---------------------------------------------------------------------
  1116. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  1117. {
  1118. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1119. return FBugs[Bug];
  1120. }
  1121. //---------------------------------------------------------------------
  1122. void __fastcall TSessionData::SetCustomParam1(AnsiString value)
  1123. {
  1124. SET_SESSION_PROPERTY(CustomParam1);
  1125. }
  1126. //---------------------------------------------------------------------
  1127. void __fastcall TSessionData::SetCustomParam2(AnsiString value)
  1128. {
  1129. SET_SESSION_PROPERTY(CustomParam2);
  1130. }
  1131. //---------------------------------------------------------------------
  1132. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  1133. {
  1134. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  1135. }
  1136. //---------------------------------------------------------------------
  1137. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  1138. {
  1139. SET_SESSION_PROPERTY(SFTPUploadQueue);
  1140. }
  1141. //---------------------------------------------------------------------
  1142. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  1143. {
  1144. SET_SESSION_PROPERTY(SFTPListingQueue);
  1145. }
  1146. //---------------------------------------------------------------------
  1147. void __fastcall TSessionData::SetSFTPSymlinkBug(TAutoSwitch value)
  1148. {
  1149. SET_SESSION_PROPERTY(SFTPSymlinkBug);
  1150. }
  1151. //---------------------------------------------------------------------
  1152. AnsiString __fastcall TSessionData::GetInfoTip()
  1153. {
  1154. return FmtLoadStr(SESSION_INFO_TIP,
  1155. ARRAYOFCONST((
  1156. (HostName.IsEmpty() ? AnsiString() : HostName),
  1157. (UserName.IsEmpty() ? AnsiString() : UserName),
  1158. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  1159. SshProtStr,
  1160. FSProtocolStr)));
  1161. }
  1162. //=== TStoredSessionList ----------------------------------------------
  1163. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  1164. TNamedObjectList(), FReadOnly(aReadOnly)
  1165. {
  1166. assert(Configuration);
  1167. LastStorage = Configuration->Storage;
  1168. FDefaultSettings = new TSessionData(DefaultSessionName);
  1169. }
  1170. //---------------------------------------------------------------------
  1171. __fastcall TStoredSessionList::~TStoredSessionList()
  1172. {
  1173. assert(Configuration);
  1174. if (!FReadOnly && (Configuration->Storage != LastStorage)) Save();
  1175. delete FDefaultSettings;
  1176. }
  1177. //---------------------------------------------------------------------
  1178. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage, bool AsModified)
  1179. {
  1180. TStringList *SubKeys = new TStringList();
  1181. try {
  1182. Storage->GetSubKeyNames(SubKeys);
  1183. for (int Index = 0; Index < SubKeys->Count; Index++)
  1184. {
  1185. TSessionData *SessionData;
  1186. AnsiString SessionName = UnMungeStr(SubKeys->Strings[Index]);
  1187. if (SessionName == FDefaultSettings->Name) SessionData = FDefaultSettings;
  1188. else SessionData = (TSessionData*)FindByName(SessionName);
  1189. if (!SessionData)
  1190. {
  1191. SessionData = new TSessionData(SessionName);
  1192. Add(SessionData);
  1193. }
  1194. SessionData->Load(Storage);
  1195. if (AsModified)
  1196. {
  1197. SessionData->Modified = true;
  1198. }
  1199. }
  1200. } __finally {
  1201. delete SubKeys;
  1202. }
  1203. }
  1204. //---------------------------------------------------------------------
  1205. void __fastcall TStoredSessionList::Load(AnsiString aKey)
  1206. {
  1207. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  1208. try {
  1209. LastStorage = stRegistry;
  1210. if (Storage->OpenRootKey(False)) Load(Storage);
  1211. } __finally {
  1212. delete Storage;
  1213. }
  1214. }
  1215. //---------------------------------------------------------------------
  1216. void __fastcall TStoredSessionList::Load()
  1217. {
  1218. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  1219. try {
  1220. LastStorage = Configuration->Storage;
  1221. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  1222. Load(Storage);
  1223. } __finally {
  1224. delete Storage;
  1225. }
  1226. }
  1227. //---------------------------------------------------------------------
  1228. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage)
  1229. {
  1230. TSessionData * FactoryDefaults = new TSessionData("");
  1231. try
  1232. {
  1233. for (int Index = 0; Index < Count+HiddenCount; Index++)
  1234. {
  1235. TSessionData *SessionData = (TSessionData *)Items[Index];
  1236. if (SessionData->Modified)
  1237. SessionData->Save(Storage, false, FactoryDefaults);
  1238. }
  1239. if (FDefaultSettings->Modified)
  1240. FDefaultSettings->Save(Storage, false, FactoryDefaults);
  1241. }
  1242. __finally
  1243. {
  1244. delete FactoryDefaults;
  1245. }
  1246. }
  1247. //---------------------------------------------------------------------
  1248. void __fastcall TStoredSessionList::Save(AnsiString aKey)
  1249. {
  1250. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  1251. try
  1252. {
  1253. LastStorage = stRegistry;
  1254. Storage->AccessMode = smReadWrite;
  1255. if (Storage->OpenRootKey(True)) Save(Storage);
  1256. }
  1257. __finally
  1258. {
  1259. delete Storage;
  1260. }
  1261. }
  1262. //---------------------------------------------------------------------
  1263. void __fastcall TStoredSessionList::Save()
  1264. {
  1265. if (LastStorage != Configuration->Storage)
  1266. {
  1267. // make sure, that all data will be stored, when saving to new storage
  1268. for (int Index = 0; Index < Count+HiddenCount; Index++)
  1269. ((TSessionData*)Items[Index])->Modified = true;
  1270. FDefaultSettings->Modified = true;
  1271. LastStorage = Configuration->Storage;
  1272. }
  1273. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  1274. try {
  1275. Storage->AccessMode = smReadWrite;
  1276. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, True))
  1277. Save(Storage);
  1278. } __finally {
  1279. delete Storage;
  1280. }
  1281. }
  1282. //---------------------------------------------------------------------
  1283. void __fastcall TStoredSessionList::SelectAll(bool Select)
  1284. {
  1285. for (int Index = 0; Index < Count; Index++)
  1286. Sessions[Index]->Selected = Select;
  1287. }
  1288. //---------------------------------------------------------------------
  1289. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  1290. bool OnlySelected)
  1291. {
  1292. for (int Index = 0; Index < From->Count; Index++)
  1293. {
  1294. if (!OnlySelected || From->Sessions[Index]->Selected)
  1295. {
  1296. TSessionData *Session = new TSessionData("");
  1297. Session->Assign(From->Sessions[Index]);
  1298. Session->Modified = true;
  1299. Session->MakeUniqueIn(this);
  1300. Add(Session);
  1301. }
  1302. }
  1303. Save();
  1304. }
  1305. //---------------------------------------------------------------------
  1306. void __fastcall TStoredSessionList::SelectSessionsToImport
  1307. (TStoredSessionList * Dest, bool SSHOnly)
  1308. {
  1309. for (int Index = 0; Index < Count; Index++)
  1310. {
  1311. Sessions[Index]->Selected =
  1312. (!SSHOnly || (Sessions[Index]->Protocol == ptSSH)) &&
  1313. !Dest->FindByName(Sessions[Index]->Name);
  1314. }
  1315. }
  1316. //---------------------------------------------------------------------
  1317. void __fastcall TStoredSessionList::Cleanup()
  1318. {
  1319. try {
  1320. if (Configuration->Storage == stRegistry) Clear();
  1321. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  1322. try {
  1323. Storage->AccessMode = smReadWrite;
  1324. if (Storage->OpenRootKey(False))
  1325. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  1326. } __finally {
  1327. delete Storage;
  1328. }
  1329. } catch (Exception &E) {
  1330. throw ExtException(&E, CLEANUP_SESSIONS_ERROR);
  1331. }
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  1335. {
  1336. for (int Index = 0; Index < Count; Index++)
  1337. if (Data == Sessions[Index]) return Index;
  1338. return -1;
  1339. }
  1340. //---------------------------------------------------------------------------
  1341. TSessionData * __fastcall TStoredSessionList::NewSession(
  1342. AnsiString SessionName, TSessionData * Session)
  1343. {
  1344. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  1345. if (!DuplicateSession)
  1346. {
  1347. DuplicateSession = new TSessionData("");
  1348. DuplicateSession->Assign(Session);
  1349. DuplicateSession->Name = SessionName;
  1350. // make sure, that new stored session is saved to registry
  1351. DuplicateSession->Modified = true;
  1352. Add(DuplicateSession);
  1353. }
  1354. else
  1355. {
  1356. DuplicateSession->Assign(Session);
  1357. DuplicateSession->Name = SessionName;
  1358. DuplicateSession->Modified = true;
  1359. }
  1360. // list was saved here before to default storage, but it would not allow
  1361. // to work with special lists (export/import) not using default storage
  1362. return DuplicateSession;
  1363. }
  1364. //---------------------------------------------------------------------------
  1365. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  1366. {
  1367. assert(FDefaultSettings);
  1368. if (FDefaultSettings != value)
  1369. {
  1370. FDefaultSettings->Assign(value);
  1371. FDefaultSettings->Name = DefaultSessionName;
  1372. Save();
  1373. }
  1374. }
  1375. //---------------------------------------------------------------------------
  1376. void __fastcall TStoredSessionList::ImportHostKeys(const AnsiString TargetKey,
  1377. const AnsiString SourceKey, TStoredSessionList * Sessions,
  1378. bool OnlySelected)
  1379. {
  1380. TRegistryStorage * SourceStorage = NULL;
  1381. TRegistryStorage * TargetStorage = NULL;
  1382. TStringList * KeyList = NULL;
  1383. try
  1384. {
  1385. SourceStorage = new TRegistryStorage(SourceKey);
  1386. TargetStorage = new TRegistryStorage(TargetKey);
  1387. TargetStorage->AccessMode = smReadWrite;
  1388. KeyList = new TStringList();
  1389. if (SourceStorage->OpenRootKey(false) &&
  1390. TargetStorage->OpenRootKey(true))
  1391. {
  1392. SourceStorage->GetValueNames(KeyList);
  1393. TSessionData * Session;
  1394. AnsiString HostKeyName;
  1395. assert(Sessions != NULL);
  1396. for (int Index = 0; Index < Sessions->Count; Index++)
  1397. {
  1398. Session = Sessions->Sessions[Index];
  1399. if (!OnlySelected || Session->Selected)
  1400. {
  1401. HostKeyName = MungeStr(FORMAT("@%d:%s", (Session->PortNumber, Session->HostName)));
  1402. AnsiString KeyName;
  1403. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  1404. {
  1405. KeyName = KeyList->Strings[KeyIndex];
  1406. int P = KeyName.Pos(HostKeyName);
  1407. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  1408. {
  1409. TargetStorage->WriteStringRaw(KeyName,
  1410. SourceStorage->ReadStringRaw(KeyName, ""));
  1411. }
  1412. }
  1413. }
  1414. }
  1415. }
  1416. }
  1417. __finally
  1418. {
  1419. delete SourceStorage;
  1420. delete TargetStorage;
  1421. delete KeyList;
  1422. }
  1423. }