SessionData.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  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 KexNames[KEX_COUNT][20] = {"WARN", "dh-group1-sha1", "dh-group14-sha1", "dh-gex-sha1" };
  20. const char SshProtList[][10] = {"1 only", "1", "2", "2 only"};
  21. const char ProxyMethodList[][10] = {"none", "SOCKS4", "SOCKS5", "HTTP", "Telnet", "Cmd" };
  22. const TCipher DefaultCipherList[CIPHER_COUNT] =
  23. { cipAES, cipBlowfish, cip3DES, cipWarn, cipDES };
  24. const TKex DefaultKexList[KEX_COUNT] =
  25. { kexDHGEx, kexDHGroup14, kexDHGroup1, kexWarn };
  26. const char FSProtocolNames[FSPROTOCOL_COUNT][11] = { "SCP", "SFTP (SCP)", "SFTP" };
  27. //--- TSessionData ----------------------------------------------------
  28. AnsiString TSessionData::FInvalidChars("/\\[]");
  29. //---------------------------------------------------------------------
  30. __fastcall TSessionData::TSessionData(AnsiString aName):
  31. TNamedObject(aName)
  32. {
  33. Default();
  34. FModified = true;
  35. }
  36. //---------------------------------------------------------------------
  37. void __fastcall TSessionData::Default()
  38. {
  39. HostName = "";
  40. PortNumber = default_port;
  41. UserName = "";
  42. Password = "";
  43. PingInterval = 30;
  44. PingType = ptOff;
  45. Timeout = 15;
  46. AgentFwd = false;
  47. AuthTIS = false;
  48. AuthKI = true;
  49. AuthKIPassword = true;
  50. AuthGSSAPI = false;
  51. ChangeUsername = false;
  52. Compression = false;
  53. SshProt = ssh2;
  54. Ssh2DES = false;
  55. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  56. {
  57. Cipher[Index] = DefaultCipherList[Index];
  58. }
  59. for (int Index = 0; Index < KEX_COUNT; Index++)
  60. {
  61. Kex[Index] = DefaultKexList[Index];
  62. }
  63. PublicKeyFile = "";
  64. FProtocol = ptSSH;
  65. TcpNoDelay = true;
  66. ProxyMethod = pmNone;
  67. ProxyHost = "proxy";
  68. ProxyPort = 80;
  69. ProxyUsername = "";
  70. ProxyPassword = "";
  71. ProxyTelnetCommand = "connect %host %port\\n";
  72. ProxyDNS = asAuto;
  73. ProxyLocalhost = false;
  74. for (int Index = 0; Index < LENOF(FBugs); Index++)
  75. {
  76. Bug[(TSshBug)Index] = asAuto;
  77. }
  78. Special = false;
  79. FSProtocol = fsSFTP;
  80. AddressFamily = afAuto;
  81. RekeyData = "1G";
  82. RekeyTime = 60;
  83. // FS common
  84. LocalDirectory = "";
  85. RemoteDirectory = "";
  86. UpdateDirectories = false;
  87. CacheDirectories = true;
  88. CacheDirectoryChanges = true;
  89. PreserveDirectoryChanges = true;
  90. LockInHome = false;
  91. ResolveSymlinks = true;
  92. ConsiderDST = true;
  93. DeleteToRecycleBin = false;
  94. OverwrittenToRecycleBin = false;
  95. RecycleBinPath = "/tmp";
  96. // SCP
  97. ReturnVar = "";
  98. LookupUserGroups = true;
  99. EOLType = eolLF;
  100. Shell = ""; //default shell
  101. ReturnVar = "";
  102. ClearAliases = true;
  103. UnsetNationalVars = true;
  104. AliasGroupList = false;
  105. IgnoreLsWarnings = true;
  106. Scp1Compatibility = false;
  107. TimeDifference = 0;
  108. SCPLsFullTime = asAuto;
  109. // SFTP
  110. SFTPDownloadQueue = 4;
  111. SFTPUploadQueue = 4;
  112. SFTPListingQueue = 2;
  113. SFTPMaxVersion = 5;
  114. SFTPMaxPacketSize = 0;
  115. for (int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  116. {
  117. SFTPBug[(TSftpBug)Index] = asAuto;
  118. }
  119. CustomParam1 = "";
  120. CustomParam2 = "";
  121. Selected = false;
  122. FModified = false;
  123. // add also to TSessionLog::AddStartupInfo()
  124. }
  125. //---------------------------------------------------------------------
  126. void __fastcall TSessionData::NonPersistant()
  127. {
  128. UpdateDirectories = false;
  129. PreserveDirectoryChanges = false;
  130. }
  131. //---------------------------------------------------------------------
  132. void __fastcall TSessionData::Assign(TPersistent * Source)
  133. {
  134. if (Source && Source->InheritsFrom(__classid(TSessionData)))
  135. {
  136. #define DUPL(P) P = ((TSessionData *)Source)->P
  137. DUPL(Name);
  138. DUPL(HostName);
  139. DUPL(PortNumber);
  140. DUPL(UserName);
  141. DUPL(Password);
  142. DUPL(PingInterval);
  143. DUPL(PingType);
  144. DUPL(Timeout);
  145. DUPL(AgentFwd);
  146. DUPL(AuthTIS);
  147. DUPL(ChangeUsername);
  148. DUPL(Compression);
  149. DUPL(SshProt);
  150. DUPL(Ssh2DES);
  151. DUPL(CipherList);
  152. DUPL(KexList);
  153. DUPL(PublicKeyFile);
  154. DUPL(AddressFamily);
  155. DUPL(RekeyData);
  156. DUPL(RekeyTime);
  157. DUPL(FSProtocol);
  158. DUPL(LocalDirectory);
  159. DUPL(RemoteDirectory);
  160. DUPL(UpdateDirectories);
  161. DUPL(CacheDirectories);
  162. DUPL(CacheDirectoryChanges);
  163. DUPL(PreserveDirectoryChanges);
  164. DUPL(ResolveSymlinks);
  165. DUPL(ConsiderDST);
  166. DUPL(LockInHome);
  167. DUPL(Special);
  168. DUPL(Selected);
  169. DUPL(ReturnVar);
  170. DUPL(LookupUserGroups);
  171. DUPL(EOLType);
  172. DUPL(Shell);
  173. DUPL(ClearAliases);
  174. DUPL(Scp1Compatibility);
  175. DUPL(UnsetNationalVars);
  176. DUPL(AliasGroupList);
  177. DUPL(IgnoreLsWarnings);
  178. DUPL(SCPLsFullTime);
  179. DUPL(TimeDifference);
  180. // new in 53b
  181. DUPL(TcpNoDelay);
  182. DUPL(AuthKI);
  183. DUPL(AuthKIPassword);
  184. DUPL(AuthGSSAPI);
  185. DUPL(DeleteToRecycleBin);
  186. DUPL(OverwrittenToRecycleBin);
  187. DUPL(RecycleBinPath);
  188. DUPL(ProxyMethod);
  189. DUPL(ProxyHost);
  190. DUPL(ProxyPort);
  191. DUPL(ProxyUsername);
  192. DUPL(ProxyPassword);
  193. DUPL(ProxyTelnetCommand);
  194. DUPL(ProxyDNS);
  195. DUPL(ProxyLocalhost);
  196. for (int Index = 0; Index < LENOF(FBugs); Index++)
  197. {
  198. DUPL(Bug[(TSshBug)Index]);
  199. }
  200. // SFTP
  201. DUPL(SFTPDownloadQueue);
  202. DUPL(SFTPUploadQueue);
  203. DUPL(SFTPListingQueue);
  204. DUPL(SFTPMaxVersion);
  205. DUPL(SFTPMaxPacketSize);
  206. for (int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  207. {
  208. DUPL(SFTPBug[(TSftpBug)Index]);
  209. }
  210. DUPL(CustomParam1);
  211. DUPL(CustomParam2);
  212. #undef DUPL
  213. FModified = ((TSessionData *)Source)->Modified;
  214. }
  215. else
  216. {
  217. TNamedObject::Assign(Source);
  218. }
  219. }
  220. //---------------------------------------------------------------------
  221. void __fastcall TSessionData::StoreToConfig(void * config)
  222. {
  223. Config * cfg = (Config *)config;
  224. // clear all (parameters not set below)
  225. memset(cfg, 0, sizeof(*cfg));
  226. // user-configurable settings
  227. ASCOPY(cfg->host, HostName);
  228. ASCOPY(cfg->username, UserName);
  229. cfg->port = PortNumber;
  230. cfg->protocol = PROT_SSH;
  231. // always set 0, as we will handle keepalives ourselves to avoid
  232. // multi-threaded issues in putty timer list
  233. cfg->ping_interval = 0;
  234. cfg->compression = Compression;
  235. cfg->agentfwd = AgentFwd;
  236. cfg->addressfamily = AddressFamily;
  237. ASCOPY(cfg->ssh_rekey_data, RekeyData);
  238. cfg->ssh_rekey_time = RekeyTime;
  239. for (int c = 0; c < CIPHER_COUNT; c++)
  240. {
  241. int pcipher;
  242. switch (Cipher[c]) {
  243. case cipWarn: pcipher = CIPHER_WARN; break;
  244. case cip3DES: pcipher = CIPHER_3DES; break;
  245. case cipBlowfish: pcipher = CIPHER_BLOWFISH; break;
  246. case cipAES: pcipher = CIPHER_AES; break;
  247. case cipDES: pcipher = CIPHER_DES; break;
  248. default: assert(false);
  249. }
  250. cfg->ssh_cipherlist[c] = pcipher;
  251. }
  252. for (int k = 0; k < KEX_COUNT; k++)
  253. {
  254. int pkex;
  255. switch (Kex[k]) {
  256. case kexWarn: pkex = KEX_WARN; break;
  257. case kexDHGroup1: pkex = KEX_DHGROUP1; break;
  258. case kexDHGroup14: pkex = KEX_DHGROUP14; break;
  259. case kexDHGEx: pkex = KEX_DHGEX; break;
  260. default: assert(false);
  261. }
  262. cfg->ssh_kexlist[k] = pkex;
  263. }
  264. AnsiString SPublicKeyFile = PublicKeyFile;
  265. if (SPublicKeyFile.IsEmpty()) SPublicKeyFile = Configuration->DefaultKeyFile;
  266. SPublicKeyFile = StripPathQuotes(SPublicKeyFile);
  267. ASCOPY(cfg->keyfile.path, SPublicKeyFile);
  268. cfg->sshprot = SshProt;
  269. cfg->ssh2_des_cbc = Ssh2DES;
  270. cfg->try_tis_auth = AuthTIS;
  271. cfg->try_ki_auth = AuthKI;
  272. cfg->try_gssapi_auth = AuthGSSAPI;
  273. cfg->change_username = ChangeUsername;
  274. cfg->proxy_type = ProxyMethod;
  275. ASCOPY(cfg->proxy_host, ProxyHost);
  276. cfg->proxy_port = ProxyPort;
  277. ASCOPY(cfg->proxy_username, ProxyUsername);
  278. ASCOPY(cfg->proxy_password, ProxyPassword);
  279. ASCOPY(cfg->proxy_telnet_command, ProxyTelnetCommand);
  280. cfg->proxy_dns = ProxyDNS;
  281. cfg->even_proxy_localhost = ProxyLocalhost;
  282. #pragma option push -w-eas
  283. // after 0.53b values were reversed, however putty still stores
  284. // settings to registry in save way as before
  285. cfg->sshbug_ignore1 = Bug[sbIgnore1];
  286. cfg->sshbug_plainpw1 = Bug[sbPlainPW1];
  287. cfg->sshbug_rsa1 = Bug[sbRSA1];
  288. cfg->sshbug_hmac2 = Bug[sbHMAC2];
  289. cfg->sshbug_derivekey2 = Bug[sbDeriveKey2];
  290. cfg->sshbug_rsapad2 = Bug[sbRSAPad2];
  291. cfg->sshbug_rekey2 = Bug[sbRekey2];
  292. // new after 0.53b
  293. cfg->sshbug_pksessid2 = Bug[sbPKSessID2];
  294. #pragma option pop
  295. if (FSProtocol == fsSCPonly)
  296. {
  297. cfg->ssh_subsys = FALSE;
  298. if (Shell.IsEmpty())
  299. {
  300. // Following forces Putty to open default shell
  301. // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
  302. cfg->remote_cmd[0] = '\0';
  303. }
  304. else
  305. {
  306. ASCOPY(cfg->remote_cmd, Shell);
  307. }
  308. }
  309. else
  310. {
  311. cfg->ssh_subsys = TRUE;
  312. strcpy(cfg->remote_cmd, "sftp");
  313. if (FSProtocol != fsSFTPonly)
  314. {
  315. cfg->ssh_subsys2 = FALSE;
  316. if (!Shell.IsEmpty())
  317. {
  318. ASCOPY(cfg->remote_cmd2, Shell);
  319. // Putty reads only "ptr" member for fallback
  320. cfg->remote_cmd_ptr2 = cfg->remote_cmd2;
  321. }
  322. }
  323. else
  324. {
  325. // see psftp_connect() from psftp.c
  326. cfg->ssh_subsys2 = FALSE;
  327. cfg->remote_cmd_ptr2 =
  328. "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n"
  329. "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n"
  330. "exec sftp-server";
  331. }
  332. }
  333. // permanent settings
  334. cfg->nopty = TRUE;
  335. cfg->tcp_keepalives = 0;
  336. }
  337. //---------------------------------------------------------------------
  338. void __fastcall TSessionData::Load(THierarchicalStorage * Storage)
  339. {
  340. if (Storage->OpenSubKey(StorageKey, False))
  341. {
  342. PortNumber = Storage->ReadInteger("PortNumber", PortNumber);
  343. UserName = Storage->ReadString("UserName", UserName);
  344. // must be loaded after UserName, because HostName may be in format user@host
  345. HostName = Storage->ReadString("HostName", HostName);
  346. if (!Configuration->DisablePasswordStoring)
  347. {
  348. FPassword = Storage->ReadString("Password", FPassword);
  349. }
  350. // Putty uses PingIntervalSecs
  351. int PingIntervalSecs = Storage->ReadInteger("PingIntervalSecs", -1);
  352. if (PingIntervalSecs < 0)
  353. {
  354. PingIntervalSecs = Storage->ReadInteger("PingIntervalSec", PingInterval%60);
  355. }
  356. PingInterval =
  357. Storage->ReadInteger("PingInterval", PingInterval/60)*60 +
  358. PingIntervalSecs;
  359. PingType = static_cast<TPingType>
  360. (Storage->ReadInteger("PingType", PingInterval > 0 ? ptNullPacket : ptOff));
  361. if (PingInterval == 0)
  362. {
  363. PingInterval = 30;
  364. }
  365. Timeout = Storage->ReadInteger("Timeout", Timeout);
  366. AgentFwd = Storage->ReadBool("AgentFwd", AgentFwd);
  367. AuthTIS = Storage->ReadBool("AuthTIS", AuthTIS);
  368. AuthKI = Storage->ReadBool("AuthKI", AuthKI);
  369. AuthKIPassword = Storage->ReadBool("AuthKIPassword", AuthKIPassword);
  370. AuthGSSAPI = Storage->ReadBool("AuthGSSAPI", AuthGSSAPI);
  371. ChangeUsername = Storage->ReadBool("ChangeUsername", ChangeUsername);
  372. Compression = Storage->ReadBool("Compression", Compression);
  373. SshProt = (TSshProt)Storage->ReadInteger("SshProt", SshProt);
  374. Ssh2DES = Storage->ReadBool("Ssh2DES", Ssh2DES);
  375. CipherList = Storage->ReadString("Cipher", CipherList);
  376. KexList = Storage->ReadString("KEX", KexList);
  377. PublicKeyFile = Storage->ReadString("PublicKeyFile", PublicKeyFile);
  378. AddressFamily = static_cast<TAddressFamily>
  379. (Storage->ReadInteger("AddressFamily", AddressFamily));
  380. RekeyData = Storage->ReadString("RekeyBytes", RekeyData);
  381. RekeyTime = Storage->ReadInteger("RekeyTime", RekeyTime);
  382. FSProtocol = (TFSProtocol)Storage->ReadInteger("FSProtocol", FSProtocol);
  383. LocalDirectory = Storage->ReadString("LocalDirectory", LocalDirectory);
  384. RemoteDirectory = Storage->ReadString("RemoteDirectory", RemoteDirectory);
  385. UpdateDirectories = Storage->ReadBool("UpdateDirectories", UpdateDirectories);
  386. CacheDirectories = Storage->ReadBool("CacheDirectories", CacheDirectories);
  387. CacheDirectoryChanges = Storage->ReadBool("CacheDirectoryChanges", CacheDirectoryChanges);
  388. PreserveDirectoryChanges = Storage->ReadBool("PreserveDirectoryChanges", PreserveDirectoryChanges);
  389. ResolveSymlinks = Storage->ReadBool("ResolveSymlinks", ResolveSymlinks);
  390. ConsiderDST = Storage->ReadBool("ConsiderDST", ConsiderDST);
  391. LockInHome = Storage->ReadBool("LockInHome", LockInHome);
  392. Special = Storage->ReadBool("Special", Special);
  393. Shell = Storage->ReadString("Shell", Shell);
  394. ClearAliases = Storage->ReadBool("ClearAliases", ClearAliases);
  395. UnsetNationalVars = Storage->ReadBool("UnsetNationalVars", UnsetNationalVars);
  396. AliasGroupList = Storage->ReadBool("AliasGroupList", AliasGroupList);
  397. IgnoreLsWarnings = Storage->ReadBool("IgnoreLsWarnings", IgnoreLsWarnings);
  398. SCPLsFullTime = TAutoSwitch(Storage->ReadInteger("SCPLsFullTime", SCPLsFullTime));
  399. Scp1Compatibility = Storage->ReadBool("Scp1Compatibility", Scp1Compatibility);
  400. TimeDifference = Storage->ReadFloat("TimeDifference", TimeDifference);
  401. DeleteToRecycleBin = Storage->ReadBool("DeleteToRecycleBin", DeleteToRecycleBin);
  402. OverwrittenToRecycleBin = Storage->ReadBool("OverwrittenToRecycleBin", OverwrittenToRecycleBin);
  403. RecycleBinPath = Storage->ReadString("RecycleBinPath", RecycleBinPath);
  404. ReturnVar = Storage->ReadString("ReturnVar", ReturnVar);
  405. LookupUserGroups = Storage->ReadBool("LookupUserGroups", LookupUserGroups);
  406. EOLType = (TEOLType)Storage->ReadInteger("EOLType", EOLType);
  407. // new in 53b
  408. TcpNoDelay = Storage->ReadBool("TcpNoDelay", TcpNoDelay);
  409. ProxyMethod = (TProxyMethod)Storage->ReadInteger("ProxyMethod", -1);
  410. if (ProxyMethod < 0)
  411. {
  412. int ProxyType = Storage->ReadInteger("ProxyType", pxNone);
  413. int ProxySOCKSVersion;
  414. switch (ProxyType) {
  415. case pxHTTP:
  416. ProxyMethod = pmHTTP;
  417. break;
  418. case pxTelnet:
  419. ProxyMethod = pmTelnet;
  420. break;
  421. case pxSocks:
  422. ProxySOCKSVersion = Storage->ReadInteger("ProxySOCKSVersion", 5);
  423. ProxyMethod = ProxySOCKSVersion == 5 ? pmSocks5 : pmSocks4;
  424. break;
  425. default:
  426. case pxNone:
  427. ProxyMethod = pmNone;
  428. break;
  429. }
  430. }
  431. ProxyHost = Storage->ReadString("ProxyHost", ProxyHost);
  432. ProxyPort = Storage->ReadInteger("ProxyPort", ProxyPort);
  433. ProxyUsername = Storage->ReadString("ProxyUsername", ProxyUsername);
  434. if (Storage->ValueExists("ProxyPassword"))
  435. {
  436. // encrypt unencrypted password
  437. ProxyPassword = Storage->ReadString("ProxyPassword", "");
  438. }
  439. else
  440. {
  441. // load encrypted password
  442. FProxyPassword = Storage->ReadString("ProxyPasswordEnc", FProxyPassword);
  443. }
  444. ProxyTelnetCommand = Storage->ReadStringRaw("ProxyTelnetCommand", ProxyTelnetCommand);
  445. ProxyDNS = TAutoSwitch((Storage->ReadInteger("ProxyDNS", (ProxyDNS + 2) % 3) + 1) % 3);
  446. ProxyLocalhost = Storage->ReadBool("ProxyLocalhost", ProxyLocalhost);
  447. #define READ_BUG(BUG) \
  448. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger("Bug"#BUG, \
  449. 2 - Bug[sb##BUG]));
  450. READ_BUG(Ignore1);
  451. READ_BUG(PlainPW1);
  452. READ_BUG(RSA1);
  453. READ_BUG(HMAC2);
  454. READ_BUG(DeriveKey2);
  455. READ_BUG(RSAPad2);
  456. READ_BUG(Rekey2);
  457. READ_BUG(PKSessID2);
  458. #undef READ_BUG
  459. if ((Bug[sbHMAC2] == asAuto) &&
  460. Storage->ReadBool("BuggyMAC", false))
  461. {
  462. Bug[sbHMAC2] = asOn;
  463. }
  464. #define READ_SFTP_BUG(BUG) \
  465. SFTPBug[sb##BUG] = TAutoSwitch(Storage->ReadInteger("SFTP" #BUG "Bug", SFTPBug[sb##BUG]));
  466. READ_SFTP_BUG(Symlink);
  467. READ_SFTP_BUG(Utf);
  468. READ_SFTP_BUG(SignedTS);
  469. #undef READ_SFTP_BUG
  470. SFTPMaxVersion = Storage->ReadInteger("SFTPMaxVersion", SFTPMaxVersion);
  471. SFTPMaxPacketSize = Storage->ReadInteger("SFTPMaxPacketSize", SFTPMaxPacketSize);
  472. // read only (used only on Import from Putty dialog)
  473. ProtocolStr = Storage->ReadString("Protocol", ProtocolStr);
  474. CustomParam1 = Storage->ReadString("CustomParam1", CustomParam1);
  475. CustomParam2 = Storage->ReadString("CustomParam2", CustomParam2);
  476. Storage->CloseSubKey();
  477. };
  478. FModified = false;
  479. }
  480. //---------------------------------------------------------------------
  481. void __fastcall TSessionData::Save(THierarchicalStorage * Storage,
  482. bool PuttyExport, const TSessionData * Default)
  483. {
  484. if (Modified && Storage->OpenSubKey(StorageKey, true))
  485. {
  486. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  487. if ((Default != NULL) && (CONV(Default->PROPERTY) == CONV(PROPERTY))) \
  488. { \
  489. Storage->DeleteValue(NAME); \
  490. } \
  491. else \
  492. { \
  493. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  494. }
  495. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  496. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, #PROPERTY, PROPERTY, )
  497. WRITE_DATA(String, HostName);
  498. WRITE_DATA(Integer, PortNumber);
  499. WRITE_DATA(String, UserName);
  500. if (!Configuration->DisablePasswordStoring && !PuttyExport && !Password.IsEmpty())
  501. {
  502. WRITE_DATA_EX(String, "Password", FPassword, );
  503. }
  504. else
  505. {
  506. Storage->DeleteValue("Password");
  507. }
  508. WRITE_DATA_EX(Integer, "PingInterval", PingInterval / 60, );
  509. WRITE_DATA_EX(Integer, "PingIntervalSecs", PingInterval % 60, );
  510. Storage->DeleteValue("PingIntervalSec"); // obsolete
  511. // store PingType always as it does not have fixed default
  512. Storage->WriteInteger("PingType", PingType);
  513. WRITE_DATA(Integer, Timeout);
  514. WRITE_DATA(Bool, AgentFwd);
  515. WRITE_DATA(Bool, AuthTIS);
  516. WRITE_DATA(Bool, AuthKI);
  517. WRITE_DATA(Bool, AuthKIPassword);
  518. WRITE_DATA(Bool, ChangeUsername);
  519. WRITE_DATA(Bool, Compression);
  520. WRITE_DATA(Integer, SshProt);
  521. WRITE_DATA(Bool, Ssh2DES);
  522. WRITE_DATA_EX(String, "Cipher", CipherList, );
  523. WRITE_DATA_EX(String, "KEX", KexList, );
  524. WRITE_DATA(Integer, AddressFamily);
  525. WRITE_DATA_EX(String, "RekeyBytes", RekeyData, );
  526. WRITE_DATA(Integer, RekeyTime);
  527. WRITE_DATA(Bool, TcpNoDelay);
  528. if (PuttyExport)
  529. {
  530. WRITE_DATA(StringRaw, PublicKeyFile);
  531. }
  532. else
  533. {
  534. WRITE_DATA(Bool, AuthGSSAPI);
  535. WRITE_DATA(String, PublicKeyFile);
  536. WRITE_DATA(Integer, FSProtocol);
  537. WRITE_DATA(String, LocalDirectory);
  538. WRITE_DATA(String, RemoteDirectory);
  539. WRITE_DATA(Bool, UpdateDirectories);
  540. WRITE_DATA(Bool, CacheDirectories);
  541. WRITE_DATA(Bool, CacheDirectoryChanges);
  542. WRITE_DATA(Bool, PreserveDirectoryChanges);
  543. WRITE_DATA(Bool, ResolveSymlinks);
  544. WRITE_DATA(Bool, ConsiderDST);
  545. WRITE_DATA(Bool, LockInHome);
  546. // Special is never stored (if it would, login dialog must be modified not to
  547. // duplicate Special parameter when Special session is loaded and then stored
  548. // under different name)
  549. // WRITE_DATA(Bool, Special);
  550. WRITE_DATA(String, Shell);
  551. WRITE_DATA(Bool, ClearAliases);
  552. WRITE_DATA(Bool, UnsetNationalVars);
  553. WRITE_DATA(Bool, AliasGroupList);
  554. WRITE_DATA(Bool, IgnoreLsWarnings);
  555. WRITE_DATA(Integer, SCPLsFullTime);
  556. WRITE_DATA(Bool, Scp1Compatibility);
  557. WRITE_DATA(Float, TimeDifference);
  558. WRITE_DATA(Bool, DeleteToRecycleBin);
  559. WRITE_DATA(Bool, OverwrittenToRecycleBin);
  560. WRITE_DATA(String, RecycleBinPath);
  561. WRITE_DATA(String, ReturnVar);
  562. WRITE_DATA(Bool, LookupUserGroups);
  563. WRITE_DATA(Integer, EOLType);
  564. }
  565. WRITE_DATA(Integer, ProxyMethod);
  566. if (PuttyExport)
  567. {
  568. // support for Putty 0.53b and older
  569. int ProxyType;
  570. int ProxySOCKSVersion = 5;
  571. switch (ProxyMethod) {
  572. case pmHTTP:
  573. ProxyType = pxHTTP;
  574. break;
  575. case pmTelnet:
  576. ProxyType = pxTelnet;
  577. break;
  578. case pmSocks5:
  579. ProxyType = pxSocks;
  580. ProxySOCKSVersion = 5;
  581. break;
  582. case pmSocks4:
  583. ProxyType = pxSocks;
  584. ProxySOCKSVersion = 4;
  585. break;
  586. default:
  587. case pmNone:
  588. ProxyType = pxNone;
  589. break;
  590. }
  591. Storage->WriteInteger("ProxyType", ProxyType);
  592. Storage->WriteInteger("ProxySOCKSVersion", ProxySOCKSVersion);
  593. }
  594. else
  595. {
  596. Storage->DeleteValue("ProxyType");
  597. Storage->DeleteValue("ProxySOCKSVersion");
  598. }
  599. WRITE_DATA(String, ProxyHost);
  600. WRITE_DATA(Integer, ProxyPort);
  601. WRITE_DATA(String, ProxyUsername);
  602. if (PuttyExport)
  603. {
  604. // save password unencrypted
  605. WRITE_DATA(String, ProxyPassword);
  606. }
  607. else
  608. {
  609. // save password encrypted
  610. if (!ProxyPassword.IsEmpty())
  611. {
  612. WRITE_DATA_EX(String, "ProxyPasswordEnc", FProxyPassword, );
  613. }
  614. else
  615. {
  616. Storage->DeleteValue("ProxyPasswordEnc");
  617. }
  618. Storage->DeleteValue("ProxyPassword");
  619. }
  620. WRITE_DATA(StringRaw, ProxyTelnetCommand);
  621. #define WRITE_DATA_CONV_FUNC(X) (((X) + 2) % 3)
  622. WRITE_DATA_CONV(Integer, "ProxyDNS", ProxyDNS);
  623. #undef WRITE_DATA_CONV_FUNC
  624. WRITE_DATA(Bool, ProxyLocalhost);
  625. #define WRITE_DATA_CONV_FUNC(X) (2 - (X))
  626. #define WRITE_BUG(BUG) WRITE_DATA_CONV(Integer, "Bug" #BUG, Bug[sb##BUG]);
  627. WRITE_BUG(Ignore1);
  628. WRITE_BUG(PlainPW1);
  629. WRITE_BUG(RSA1);
  630. WRITE_BUG(HMAC2);
  631. WRITE_BUG(DeriveKey2);
  632. WRITE_BUG(RSAPad2);
  633. WRITE_BUG(Rekey2);
  634. WRITE_BUG(PKSessID2);
  635. #undef WRITE_BUG
  636. #undef WRITE_DATA_CONV_FUNC
  637. Storage->DeleteValue("BuggyMAC");
  638. if (PuttyExport)
  639. {
  640. WRITE_DATA_EX(String, "Protocol", ProtocolStr, );
  641. }
  642. if (!PuttyExport)
  643. {
  644. #define WRITE_SFTP_BUG(BUG) WRITE_DATA_EX(Integer, "SFTP" #BUG "Bug", SFTPBug[sb##BUG], );
  645. WRITE_SFTP_BUG(Symlink);
  646. WRITE_SFTP_BUG(Utf);
  647. WRITE_SFTP_BUG(SignedTS);
  648. #undef WRITE_SFTP_BUG
  649. WRITE_DATA(Integer, SFTPMaxVersion);
  650. WRITE_DATA(Integer, SFTPMaxPacketSize);
  651. WRITE_DATA(String, CustomParam1);
  652. WRITE_DATA(String, CustomParam2);
  653. }
  654. Storage->CloseSubKey();
  655. FModified = false;
  656. }
  657. }
  658. //---------------------------------------------------------------------
  659. void __fastcall TSessionData::Remove()
  660. {
  661. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  662. try
  663. {
  664. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  665. {
  666. Storage->RecursiveDeleteSubKey(StorageKey);
  667. }
  668. }
  669. __finally
  670. {
  671. delete Storage;
  672. }
  673. }
  674. //---------------------------------------------------------------------
  675. bool __fastcall TSessionData::ParseUrl(AnsiString Url, int Params,
  676. AnsiString * ConnectInfo, AnsiString * HostName, int * PortNumber,
  677. AnsiString * UserName, AnsiString * Password, AnsiString * Path,
  678. AnsiString * FileName)
  679. {
  680. #define DECODE(S) (FLAGSET(Params, puDecodeUrlChars) ? DecodeUrlChars(S) : S)
  681. int PSlash = Url.Pos("/");
  682. if (PSlash == 0)
  683. {
  684. PSlash = Url.Length() + 1;
  685. }
  686. AnsiString AConnectInfo;
  687. AConnectInfo = Url.SubString(1, PSlash - 1);
  688. int P = AConnectInfo.Pos("@");
  689. bool Result = (P > 0) || ((Params & puRequireUsername) == 0);
  690. if (Result)
  691. {
  692. if ((Path != NULL) || (FileName != NULL))
  693. {
  694. bool ExcludeLeadingSlash = (Params & puExcludeLeadingSlash) != 0;
  695. int Delta = ExcludeLeadingSlash ? 1 : 0;
  696. AnsiString APath = Url.SubString(PSlash + Delta,
  697. Url.Length() - PSlash - Delta + 1);
  698. if (ExcludeLeadingSlash || (APath != "/"))
  699. {
  700. if ((APath.Length() > 0) && (APath[APath.Length()] != '/'))
  701. {
  702. if (FileName != NULL)
  703. {
  704. *FileName = DECODE(UnixExtractFileName(APath));
  705. }
  706. if (FLAGSET(Params, puExtractFileName))
  707. {
  708. APath = UnixExtractFilePath(APath);
  709. }
  710. }
  711. if (Path != NULL)
  712. {
  713. *Path = DECODE(APath);
  714. }
  715. }
  716. }
  717. if (ConnectInfo != NULL)
  718. {
  719. *ConnectInfo = AConnectInfo;
  720. }
  721. AnsiString UserInfo;
  722. AnsiString HostInfo;
  723. if (P > 0)
  724. {
  725. UserInfo = AConnectInfo.SubString(1, P - 1);
  726. HostInfo = AConnectInfo.SubString(P + 1, AConnectInfo.Length() - P);
  727. }
  728. else
  729. {
  730. HostInfo = AConnectInfo;
  731. }
  732. if (HostName != NULL)
  733. {
  734. *HostName = DECODE(CutToChar(HostInfo, ':', true));
  735. }
  736. else
  737. {
  738. CutToChar(HostInfo, ':', true);
  739. }
  740. if (PortNumber != NULL)
  741. {
  742. *PortNumber = HostInfo.IsEmpty() ? -1 : StrToIntDef(DECODE(HostInfo), -1);
  743. }
  744. if (UserName != NULL)
  745. {
  746. *UserName = DECODE(CutToChar(UserInfo, ':', false));
  747. }
  748. else
  749. {
  750. CutToChar(UserInfo, ':', false);
  751. }
  752. if (Password != NULL)
  753. {
  754. *Password = DECODE(UserInfo);
  755. }
  756. }
  757. return Result;
  758. #undef DECODE
  759. }
  760. //---------------------------------------------------------------------
  761. bool __fastcall TSessionData::ParseUrl(AnsiString Url, int Params,
  762. AnsiString * FileName)
  763. {
  764. AnsiString AHostName = HostName;
  765. int APortNumber = PortNumber;
  766. AnsiString AUserName = UserName;
  767. AnsiString APassword = Password;
  768. AnsiString ARemoteDirectory = RemoteDirectory;
  769. bool Result = ParseUrl(Url, Params, NULL, &AHostName, &APortNumber,
  770. &AUserName, &APassword, &ARemoteDirectory, FileName);
  771. if (Result)
  772. {
  773. HostName = AHostName;
  774. if (APortNumber >= 0)
  775. {
  776. PortNumber = APortNumber;
  777. }
  778. UserName = AUserName;
  779. Password = APassword;
  780. RemoteDirectory = ARemoteDirectory;
  781. }
  782. return Result;
  783. }
  784. //---------------------------------------------------------------------
  785. void __fastcall TSessionData::ValidateName(const AnsiString Name)
  786. {
  787. if (Name.LastDelimiter(FInvalidChars) > 0)
  788. {
  789. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, FInvalidChars)));
  790. }
  791. }
  792. //---------------------------------------------------------------------
  793. bool __fastcall TSessionData::GetCanLogin()
  794. {
  795. return !FHostName.IsEmpty() && !FUserName.IsEmpty();
  796. }
  797. //---------------------------------------------------------------------------
  798. AnsiString __fastcall TSessionData::GetSessionKey()
  799. {
  800. return FORMAT("%s@%s", (UserName, HostName));
  801. }
  802. //---------------------------------------------------------------------
  803. AnsiString __fastcall TSessionData::GetStorageKey()
  804. {
  805. // particularly OpenSessionInPutty expect that StorageKey always returns something
  806. return MungeStr(Name.IsEmpty() ? SessionKey : Name);
  807. }
  808. //---------------------------------------------------------------------
  809. void __fastcall TSessionData::SetHostName(AnsiString value)
  810. {
  811. if (FHostName != value)
  812. {
  813. // HostName is key for password encryption
  814. AnsiString XPassword = Password;
  815. int P = value.LastDelimiter("@");
  816. if (P > 0)
  817. {
  818. UserName = value.SubString(1, P - 1);
  819. value = value.SubString(P + 1, value.Length() - P);
  820. }
  821. FHostName = value;
  822. FModified = true;
  823. Password = XPassword;
  824. if (!XPassword.IsEmpty())
  825. {
  826. memset(XPassword.c_str(), 0, XPassword.Length());
  827. }
  828. }
  829. }
  830. //---------------------------------------------------------------------
  831. void __fastcall TSessionData::SetPortNumber(int value)
  832. {
  833. SET_SESSION_PROPERTY(PortNumber);
  834. }
  835. //---------------------------------------------------------------------------
  836. void __fastcall TSessionData::SetShell(AnsiString value)
  837. {
  838. SET_SESSION_PROPERTY(Shell);
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TSessionData::SetClearAliases(bool value)
  842. {
  843. SET_SESSION_PROPERTY(ClearAliases);
  844. }
  845. //---------------------------------------------------------------------------
  846. void __fastcall TSessionData::SetAliasGroupList(bool value)
  847. {
  848. SET_SESSION_PROPERTY(AliasGroupList);
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  852. {
  853. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  854. }
  855. //---------------------------------------------------------------------------
  856. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  857. {
  858. SET_SESSION_PROPERTY(UnsetNationalVars);
  859. }
  860. //---------------------------------------------------------------------
  861. void __fastcall TSessionData::SetUserName(AnsiString value)
  862. {
  863. // UserName is key for password encryption
  864. AnsiString XPassword = Password;
  865. SET_SESSION_PROPERTY(UserName);
  866. Password = XPassword;
  867. if (!XPassword.IsEmpty())
  868. {
  869. memset(XPassword.c_str(), 0, XPassword.Length());
  870. }
  871. }
  872. //---------------------------------------------------------------------
  873. void __fastcall TSessionData::SetPassword(AnsiString value)
  874. {
  875. value = EncryptPassword(value, UserName+HostName);
  876. SET_SESSION_PROPERTY(Password);
  877. }
  878. //---------------------------------------------------------------------
  879. AnsiString __fastcall TSessionData::GetPassword()
  880. {
  881. return DecryptPassword(FPassword, UserName+HostName);
  882. }
  883. //---------------------------------------------------------------------
  884. void __fastcall TSessionData::SetPingInterval(int value)
  885. {
  886. SET_SESSION_PROPERTY(PingInterval);
  887. }
  888. //---------------------------------------------------------------------
  889. void __fastcall TSessionData::SetAgentFwd(bool value)
  890. {
  891. SET_SESSION_PROPERTY(AgentFwd);
  892. }
  893. //---------------------------------------------------------------------
  894. void __fastcall TSessionData::SetAuthTIS(bool value)
  895. {
  896. SET_SESSION_PROPERTY(AuthTIS);
  897. }
  898. //---------------------------------------------------------------------
  899. void __fastcall TSessionData::SetAuthKI(bool value)
  900. {
  901. SET_SESSION_PROPERTY(AuthKI);
  902. }
  903. //---------------------------------------------------------------------
  904. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  905. {
  906. SET_SESSION_PROPERTY(AuthKIPassword);
  907. }
  908. //---------------------------------------------------------------------
  909. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  910. {
  911. SET_SESSION_PROPERTY(AuthGSSAPI);
  912. }
  913. //---------------------------------------------------------------------
  914. void __fastcall TSessionData::SetChangeUsername(bool value)
  915. {
  916. SET_SESSION_PROPERTY(ChangeUsername);
  917. }
  918. //---------------------------------------------------------------------
  919. void __fastcall TSessionData::SetCompression(bool value)
  920. {
  921. SET_SESSION_PROPERTY(Compression);
  922. }
  923. //---------------------------------------------------------------------
  924. void __fastcall TSessionData::SetSshProt(TSshProt value)
  925. {
  926. SET_SESSION_PROPERTY(SshProt);
  927. }
  928. //---------------------------------------------------------------------
  929. void __fastcall TSessionData::SetSsh2DES(bool value)
  930. {
  931. SET_SESSION_PROPERTY(Ssh2DES);
  932. }
  933. //---------------------------------------------------------------------
  934. AnsiString __fastcall TSessionData::GetSshProtStr()
  935. {
  936. return SshProtList[FSshProt];
  937. }
  938. //---------------------------------------------------------------------
  939. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  940. {
  941. assert(Index >= 0 && Index < CIPHER_COUNT);
  942. SET_SESSION_PROPERTY(Ciphers[Index]);
  943. }
  944. //---------------------------------------------------------------------
  945. TCipher __fastcall TSessionData::GetCipher(int Index) const
  946. {
  947. assert(Index >= 0 && Index < CIPHER_COUNT);
  948. return FCiphers[Index];
  949. }
  950. //---------------------------------------------------------------------
  951. void __fastcall TSessionData::SetCipherList(AnsiString value)
  952. {
  953. bool Used[CIPHER_COUNT];
  954. for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
  955. AnsiString CipherStr;
  956. int Index = 0;
  957. while (!value.IsEmpty() && (Index < CIPHER_COUNT))
  958. {
  959. CipherStr = CutToChar(value, ',', true);
  960. for (int C = 0; C < CIPHER_COUNT; C++)
  961. {
  962. if (!CipherStr.AnsiCompareIC(CipherNames[C]))
  963. {
  964. Cipher[Index] = (TCipher)C;
  965. Used[C] = true;
  966. Index++;
  967. break;
  968. }
  969. }
  970. }
  971. for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
  972. {
  973. if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
  974. }
  975. }
  976. //---------------------------------------------------------------------
  977. AnsiString __fastcall TSessionData::GetCipherList() const
  978. {
  979. AnsiString Result;
  980. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  981. {
  982. Result += AnsiString(Index ? "," : "") + CipherNames[Cipher[Index]];
  983. }
  984. return Result;
  985. }
  986. //---------------------------------------------------------------------
  987. void __fastcall TSessionData::SetKex(int Index, TKex value)
  988. {
  989. assert(Index >= 0 && Index < KEX_COUNT);
  990. SET_SESSION_PROPERTY(Kex[Index]);
  991. }
  992. //---------------------------------------------------------------------
  993. TKex __fastcall TSessionData::GetKex(int Index) const
  994. {
  995. assert(Index >= 0 && Index < KEX_COUNT);
  996. return FKex[Index];
  997. }
  998. //---------------------------------------------------------------------
  999. void __fastcall TSessionData::SetKexList(AnsiString value)
  1000. {
  1001. bool Used[KEX_COUNT];
  1002. for (int K = 0; K < KEX_COUNT; K++) Used[K] = false;
  1003. AnsiString KexStr;
  1004. int Index = 0;
  1005. while (!value.IsEmpty() && (Index < KEX_COUNT))
  1006. {
  1007. KexStr = CutToChar(value, ',', true);
  1008. for (int K = 0; K < KEX_COUNT; K++)
  1009. {
  1010. if (!KexStr.AnsiCompareIC(KexNames[K]))
  1011. {
  1012. Kex[Index] = (TKex)K;
  1013. Used[K] = true;
  1014. Index++;
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. for (int K = 0; K < KEX_COUNT && Index < KEX_COUNT; K++)
  1020. {
  1021. if (!Used[DefaultKexList[K]]) Kex[Index++] = DefaultKexList[K];
  1022. }
  1023. }
  1024. //---------------------------------------------------------------------
  1025. AnsiString __fastcall TSessionData::GetKexList() const
  1026. {
  1027. AnsiString Result;
  1028. for (int Index = 0; Index < KEX_COUNT; Index++)
  1029. {
  1030. Result += AnsiString(Index ? "," : "") + KexNames[Kex[Index]];
  1031. }
  1032. return Result;
  1033. }
  1034. //---------------------------------------------------------------------
  1035. void __fastcall TSessionData::SetPublicKeyFile(AnsiString value)
  1036. {
  1037. if (FPublicKeyFile != value)
  1038. {
  1039. FPublicKeyFile = StripPathQuotes(value);
  1040. FModified = true;
  1041. }
  1042. }
  1043. //---------------------------------------------------------------------
  1044. AnsiString __fastcall TSessionData::GetDefaultLogFileName()
  1045. {
  1046. return IncludeTrailingBackslash(SystemTemporaryDirectory()) +
  1047. MakeValidFileName(SessionName) + ".log";
  1048. }
  1049. //---------------------------------------------------------------------
  1050. void __fastcall TSessionData::SetReturnVar(AnsiString value)
  1051. {
  1052. SET_SESSION_PROPERTY(ReturnVar);
  1053. }
  1054. //---------------------------------------------------------------------------
  1055. void __fastcall TSessionData::SetLookupUserGroups(bool value)
  1056. {
  1057. SET_SESSION_PROPERTY(LookupUserGroups);
  1058. }
  1059. //---------------------------------------------------------------------------
  1060. void __fastcall TSessionData::SetEOLType(TEOLType value)
  1061. {
  1062. SET_SESSION_PROPERTY(EOLType);
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. void __fastcall TSessionData::SetTimeout(int value)
  1066. {
  1067. SET_SESSION_PROPERTY(Timeout);
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  1071. {
  1072. SET_SESSION_PROPERTY(FSProtocol);
  1073. }
  1074. //---------------------------------------------------------------------
  1075. AnsiString __fastcall TSessionData::GetFSProtocolStr()
  1076. {
  1077. assert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  1078. return FSProtocolNames[FSProtocol];
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  1082. {
  1083. if (value != DetectReturnVar)
  1084. {
  1085. ReturnVar = value ? "" : "$?";
  1086. }
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. bool __fastcall TSessionData::GetDetectReturnVar()
  1090. {
  1091. return ReturnVar.IsEmpty();
  1092. }
  1093. //---------------------------------------------------------------------------
  1094. void __fastcall TSessionData::SetDefaultShell(bool value)
  1095. {
  1096. if (value != DefaultShell)
  1097. {
  1098. Shell = value ? "" : "/bin/bash";
  1099. }
  1100. }
  1101. //---------------------------------------------------------------------------
  1102. bool __fastcall TSessionData::GetDefaultShell()
  1103. {
  1104. return Shell.IsEmpty();
  1105. }
  1106. //---------------------------------------------------------------------------
  1107. void __fastcall TSessionData::SetProtocolStr(AnsiString value)
  1108. {
  1109. for (int Index = 0; backends[Index].name != NULL; Index++)
  1110. {
  1111. if (!value.AnsiCompareIC(backends[Index].name))
  1112. {
  1113. FProtocol = (TProtocol)backends[Index].protocol;
  1114. break;
  1115. }
  1116. }
  1117. }
  1118. //---------------------------------------------------------------------
  1119. AnsiString __fastcall TSessionData::GetProtocolStr() const
  1120. {
  1121. for (int Index = 0; backends[Index].name != NULL; Index++)
  1122. {
  1123. if ((TProtocol)backends[Index].protocol == Protocol)
  1124. {
  1125. return backends[Index].name;
  1126. }
  1127. }
  1128. return "raw";
  1129. }
  1130. //---------------------------------------------------------------------
  1131. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  1132. {
  1133. unsigned short hour, min, sec, msec;
  1134. value.DecodeTime(&hour, &min, &sec, &msec);
  1135. PingInterval = ((int)hour)*60*60 + ((int)min)*60 + sec;
  1136. }
  1137. //---------------------------------------------------------------------------
  1138. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  1139. {
  1140. return TDateTime((unsigned short)(PingInterval/60/60),
  1141. (unsigned short)(PingInterval/60%60), (unsigned short)(PingInterval%60), 0);
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. void __fastcall TSessionData::SetPingType(TPingType value)
  1145. {
  1146. SET_SESSION_PROPERTY(PingType);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  1150. {
  1151. SET_SESSION_PROPERTY(AddressFamily);
  1152. }
  1153. //---------------------------------------------------------------------------
  1154. void __fastcall TSessionData::SetRekeyData(AnsiString value)
  1155. {
  1156. SET_SESSION_PROPERTY(RekeyData);
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  1160. {
  1161. SET_SESSION_PROPERTY(RekeyTime);
  1162. }
  1163. //---------------------------------------------------------------------
  1164. AnsiString __fastcall TSessionData::GetSessionName()
  1165. {
  1166. if (!Name.IsEmpty() && !TNamedObjectList::IsHidden(this) &&
  1167. (Name != DefaultSessionName))
  1168. {
  1169. return Name;
  1170. }
  1171. else if (!HostName.IsEmpty() && !UserName.IsEmpty())
  1172. {
  1173. return FORMAT("%s@%s", (UserName, HostName));
  1174. }
  1175. else if (!HostName.IsEmpty())
  1176. {
  1177. return HostName;
  1178. }
  1179. else
  1180. {
  1181. return "session";
  1182. }
  1183. }
  1184. //---------------------------------------------------------------------
  1185. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  1186. {
  1187. SET_SESSION_PROPERTY(TimeDifference);
  1188. }
  1189. //---------------------------------------------------------------------
  1190. void __fastcall TSessionData::SetLocalDirectory(AnsiString value)
  1191. {
  1192. SET_SESSION_PROPERTY(LocalDirectory);
  1193. }
  1194. //---------------------------------------------------------------------
  1195. void __fastcall TSessionData::SetRemoteDirectory(AnsiString value)
  1196. {
  1197. SET_SESSION_PROPERTY(RemoteDirectory);
  1198. }
  1199. //---------------------------------------------------------------------
  1200. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  1201. {
  1202. SET_SESSION_PROPERTY(UpdateDirectories);
  1203. }
  1204. //---------------------------------------------------------------------
  1205. void __fastcall TSessionData::SetCacheDirectories(bool value)
  1206. {
  1207. SET_SESSION_PROPERTY(CacheDirectories);
  1208. }
  1209. //---------------------------------------------------------------------
  1210. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  1211. {
  1212. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  1213. }
  1214. //---------------------------------------------------------------------
  1215. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  1216. {
  1217. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  1218. }
  1219. //---------------------------------------------------------------------
  1220. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  1221. {
  1222. SET_SESSION_PROPERTY(ResolveSymlinks);
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. void __fastcall TSessionData::SetConsiderDST(bool value)
  1226. {
  1227. SET_SESSION_PROPERTY(ConsiderDST);
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  1231. {
  1232. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  1236. {
  1237. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  1238. }
  1239. //---------------------------------------------------------------------------
  1240. void __fastcall TSessionData::SetRecycleBinPath(AnsiString value)
  1241. {
  1242. SET_SESSION_PROPERTY(RecycleBinPath);
  1243. }
  1244. //---------------------------------------------------------------------
  1245. void __fastcall TSessionData::SetLockInHome(bool value)
  1246. {
  1247. SET_SESSION_PROPERTY(LockInHome);
  1248. }
  1249. //---------------------------------------------------------------------
  1250. void __fastcall TSessionData::SetSpecial(bool value)
  1251. {
  1252. SET_SESSION_PROPERTY(Special);
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  1256. {
  1257. SET_SESSION_PROPERTY(Scp1Compatibility);
  1258. }
  1259. //---------------------------------------------------------------------
  1260. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  1261. {
  1262. SET_SESSION_PROPERTY(TcpNoDelay);
  1263. }
  1264. //---------------------------------------------------------------------
  1265. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  1266. {
  1267. SET_SESSION_PROPERTY(ProxyMethod);
  1268. }
  1269. //---------------------------------------------------------------------
  1270. void __fastcall TSessionData::SetProxyHost(AnsiString value)
  1271. {
  1272. SET_SESSION_PROPERTY(ProxyHost);
  1273. }
  1274. //---------------------------------------------------------------------
  1275. void __fastcall TSessionData::SetProxyPort(int value)
  1276. {
  1277. SET_SESSION_PROPERTY(ProxyPort);
  1278. }
  1279. //---------------------------------------------------------------------
  1280. void __fastcall TSessionData::SetProxyUsername(AnsiString value)
  1281. {
  1282. SET_SESSION_PROPERTY(ProxyUsername);
  1283. }
  1284. //---------------------------------------------------------------------
  1285. void __fastcall TSessionData::SetProxyPassword(AnsiString value)
  1286. {
  1287. value = EncryptPassword(value, ProxyUsername+ProxyHost);
  1288. SET_SESSION_PROPERTY(ProxyPassword);
  1289. }
  1290. //---------------------------------------------------------------------
  1291. AnsiString __fastcall TSessionData::GetProxyPassword() const
  1292. {
  1293. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  1294. }
  1295. //---------------------------------------------------------------------
  1296. void __fastcall TSessionData::SetProxyTelnetCommand(AnsiString value)
  1297. {
  1298. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  1299. }
  1300. //---------------------------------------------------------------------
  1301. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  1302. {
  1303. SET_SESSION_PROPERTY(ProxyDNS);
  1304. }
  1305. //---------------------------------------------------------------------
  1306. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  1307. {
  1308. SET_SESSION_PROPERTY(ProxyLocalhost);
  1309. }
  1310. //---------------------------------------------------------------------
  1311. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  1312. {
  1313. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1314. SET_SESSION_PROPERTY(Bugs[Bug]);
  1315. }
  1316. //---------------------------------------------------------------------
  1317. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  1318. {
  1319. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1320. return FBugs[Bug];
  1321. }
  1322. //---------------------------------------------------------------------
  1323. void __fastcall TSessionData::SetCustomParam1(AnsiString value)
  1324. {
  1325. SET_SESSION_PROPERTY(CustomParam1);
  1326. }
  1327. //---------------------------------------------------------------------
  1328. void __fastcall TSessionData::SetCustomParam2(AnsiString value)
  1329. {
  1330. SET_SESSION_PROPERTY(CustomParam2);
  1331. }
  1332. //---------------------------------------------------------------------
  1333. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  1334. {
  1335. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  1336. }
  1337. //---------------------------------------------------------------------
  1338. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  1339. {
  1340. SET_SESSION_PROPERTY(SFTPUploadQueue);
  1341. }
  1342. //---------------------------------------------------------------------
  1343. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  1344. {
  1345. SET_SESSION_PROPERTY(SFTPListingQueue);
  1346. }
  1347. //---------------------------------------------------------------------
  1348. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  1349. {
  1350. SET_SESSION_PROPERTY(SFTPMaxVersion);
  1351. }
  1352. //---------------------------------------------------------------------
  1353. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  1354. {
  1355. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  1356. }
  1357. //---------------------------------------------------------------------
  1358. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  1359. {
  1360. assert(Bug >= 0 && Bug < LENOF(FSFTPBugs));
  1361. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  1362. }
  1363. //---------------------------------------------------------------------
  1364. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  1365. {
  1366. assert(Bug >= 0 && Bug < LENOF(FSFTPBugs));
  1367. return FSFTPBugs[Bug];
  1368. }
  1369. //---------------------------------------------------------------------
  1370. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  1371. {
  1372. SET_SESSION_PROPERTY(SCPLsFullTime);
  1373. }
  1374. //---------------------------------------------------------------------
  1375. AnsiString __fastcall TSessionData::GetInfoTip()
  1376. {
  1377. return FmtLoadStr(SESSION_INFO_TIP,
  1378. ARRAYOFCONST((
  1379. (HostName.IsEmpty() ? AnsiString() : HostName),
  1380. (UserName.IsEmpty() ? AnsiString() : UserName),
  1381. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  1382. SshProtStr,
  1383. FSProtocolStr)));
  1384. }
  1385. //=== TStoredSessionList ----------------------------------------------
  1386. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  1387. TNamedObjectList(), FReadOnly(aReadOnly)
  1388. {
  1389. assert(Configuration);
  1390. LastStorage = Configuration->Storage;
  1391. FDefaultSettings = new TSessionData(DefaultSessionName);
  1392. }
  1393. //---------------------------------------------------------------------
  1394. __fastcall TStoredSessionList::~TStoredSessionList()
  1395. {
  1396. assert(Configuration);
  1397. if (!FReadOnly && (Configuration->Storage != LastStorage)) Save();
  1398. delete FDefaultSettings;
  1399. }
  1400. //---------------------------------------------------------------------
  1401. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  1402. bool AsModified, bool UseDefaults)
  1403. {
  1404. TStringList *SubKeys = new TStringList();
  1405. try {
  1406. Storage->GetSubKeyNames(SubKeys);
  1407. for (int Index = 0; Index < SubKeys->Count; Index++)
  1408. {
  1409. TSessionData *SessionData;
  1410. AnsiString SessionName = UnMungeStr(SubKeys->Strings[Index]);
  1411. bool ValidName = true;
  1412. try
  1413. {
  1414. TSessionData::ValidateName(SessionName);
  1415. }
  1416. catch(...)
  1417. {
  1418. ValidName = false;
  1419. }
  1420. if (ValidName)
  1421. {
  1422. if (SessionName == FDefaultSettings->Name) SessionData = FDefaultSettings;
  1423. else SessionData = (TSessionData*)FindByName(SessionName);
  1424. if ((SessionData != FDefaultSettings) || !UseDefaults)
  1425. {
  1426. if (!SessionData)
  1427. {
  1428. SessionData = new TSessionData("");
  1429. if (UseDefaults)
  1430. {
  1431. SessionData->Assign(DefaultSettings);
  1432. }
  1433. SessionData->Name = SessionName;
  1434. Add(SessionData);
  1435. }
  1436. SessionData->Load(Storage);
  1437. if (AsModified)
  1438. {
  1439. SessionData->Modified = true;
  1440. }
  1441. }
  1442. }
  1443. }
  1444. } __finally {
  1445. delete SubKeys;
  1446. }
  1447. }
  1448. //---------------------------------------------------------------------
  1449. void __fastcall TStoredSessionList::Load(AnsiString aKey, bool UseDefaults)
  1450. {
  1451. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  1452. try {
  1453. LastStorage = stRegistry;
  1454. if (Storage->OpenRootKey(False)) Load(Storage, false, UseDefaults);
  1455. } __finally {
  1456. delete Storage;
  1457. }
  1458. }
  1459. //---------------------------------------------------------------------
  1460. void __fastcall TStoredSessionList::Load()
  1461. {
  1462. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  1463. try {
  1464. LastStorage = Configuration->Storage;
  1465. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  1466. Load(Storage);
  1467. } __finally {
  1468. delete Storage;
  1469. }
  1470. }
  1471. //---------------------------------------------------------------------
  1472. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage)
  1473. {
  1474. TSessionData * FactoryDefaults = new TSessionData("");
  1475. try
  1476. {
  1477. for (int Index = 0; Index < Count+HiddenCount; Index++)
  1478. {
  1479. TSessionData *SessionData = (TSessionData *)Items[Index];
  1480. if (SessionData->Modified)
  1481. SessionData->Save(Storage, false, FactoryDefaults);
  1482. }
  1483. if (FDefaultSettings->Modified)
  1484. FDefaultSettings->Save(Storage, false, FactoryDefaults);
  1485. }
  1486. __finally
  1487. {
  1488. delete FactoryDefaults;
  1489. }
  1490. }
  1491. //---------------------------------------------------------------------
  1492. void __fastcall TStoredSessionList::Save(AnsiString aKey)
  1493. {
  1494. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  1495. try
  1496. {
  1497. LastStorage = stRegistry;
  1498. Storage->AccessMode = smReadWrite;
  1499. if (Storage->OpenRootKey(True)) Save(Storage);
  1500. }
  1501. __finally
  1502. {
  1503. delete Storage;
  1504. }
  1505. }
  1506. //---------------------------------------------------------------------
  1507. void __fastcall TStoredSessionList::Save()
  1508. {
  1509. if (LastStorage != Configuration->Storage)
  1510. {
  1511. // make sure, that all data will be stored, when saving to new storage
  1512. for (int Index = 0; Index < Count+HiddenCount; Index++)
  1513. ((TSessionData*)Items[Index])->Modified = true;
  1514. FDefaultSettings->Modified = true;
  1515. LastStorage = Configuration->Storage;
  1516. }
  1517. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  1518. try {
  1519. Storage->AccessMode = smReadWrite;
  1520. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, True))
  1521. Save(Storage);
  1522. } __finally {
  1523. delete Storage;
  1524. }
  1525. }
  1526. //---------------------------------------------------------------------
  1527. void __fastcall TStoredSessionList::SelectAll(bool Select)
  1528. {
  1529. for (int Index = 0; Index < Count; Index++)
  1530. Sessions[Index]->Selected = Select;
  1531. }
  1532. //---------------------------------------------------------------------
  1533. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  1534. bool OnlySelected)
  1535. {
  1536. for (int Index = 0; Index < From->Count; Index++)
  1537. {
  1538. if (!OnlySelected || From->Sessions[Index]->Selected)
  1539. {
  1540. TSessionData *Session = new TSessionData("");
  1541. Session->Assign(From->Sessions[Index]);
  1542. Session->Modified = true;
  1543. Session->MakeUniqueIn(this);
  1544. Add(Session);
  1545. }
  1546. }
  1547. Save();
  1548. }
  1549. //---------------------------------------------------------------------
  1550. void __fastcall TStoredSessionList::SelectSessionsToImport
  1551. (TStoredSessionList * Dest, bool SSHOnly)
  1552. {
  1553. for (int Index = 0; Index < Count; Index++)
  1554. {
  1555. Sessions[Index]->Selected =
  1556. (!SSHOnly || (Sessions[Index]->Protocol == ptSSH)) &&
  1557. !Dest->FindByName(Sessions[Index]->Name);
  1558. }
  1559. }
  1560. //---------------------------------------------------------------------
  1561. void __fastcall TStoredSessionList::Cleanup()
  1562. {
  1563. try {
  1564. if (Configuration->Storage == stRegistry) Clear();
  1565. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  1566. try {
  1567. Storage->AccessMode = smReadWrite;
  1568. if (Storage->OpenRootKey(False))
  1569. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  1570. } __finally {
  1571. delete Storage;
  1572. }
  1573. } catch (Exception &E) {
  1574. throw ExtException(&E, CLEANUP_SESSIONS_ERROR);
  1575. }
  1576. }
  1577. //---------------------------------------------------------------------------
  1578. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  1579. {
  1580. for (int Index = 0; Index < Count; Index++)
  1581. if (Data == Sessions[Index]) return Index;
  1582. return -1;
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. TSessionData * __fastcall TStoredSessionList::NewSession(
  1586. AnsiString SessionName, TSessionData * Session)
  1587. {
  1588. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  1589. if (!DuplicateSession)
  1590. {
  1591. DuplicateSession = new TSessionData("");
  1592. DuplicateSession->Assign(Session);
  1593. DuplicateSession->Name = SessionName;
  1594. // make sure, that new stored session is saved to registry
  1595. DuplicateSession->Modified = true;
  1596. Add(DuplicateSession);
  1597. }
  1598. else
  1599. {
  1600. DuplicateSession->Assign(Session);
  1601. DuplicateSession->Name = SessionName;
  1602. DuplicateSession->Modified = true;
  1603. }
  1604. // list was saved here before to default storage, but it would not allow
  1605. // to work with special lists (export/import) not using default storage
  1606. return DuplicateSession;
  1607. }
  1608. //---------------------------------------------------------------------------
  1609. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  1610. {
  1611. assert(FDefaultSettings);
  1612. if (FDefaultSettings != value)
  1613. {
  1614. FDefaultSettings->Assign(value);
  1615. FDefaultSettings->Name = DefaultSessionName;
  1616. if (!FReadOnly)
  1617. {
  1618. Save();
  1619. }
  1620. }
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. void __fastcall TStoredSessionList::ImportHostKeys(const AnsiString TargetKey,
  1624. const AnsiString SourceKey, TStoredSessionList * Sessions,
  1625. bool OnlySelected)
  1626. {
  1627. TRegistryStorage * SourceStorage = NULL;
  1628. TRegistryStorage * TargetStorage = NULL;
  1629. TStringList * KeyList = NULL;
  1630. try
  1631. {
  1632. SourceStorage = new TRegistryStorage(SourceKey);
  1633. TargetStorage = new TRegistryStorage(TargetKey);
  1634. TargetStorage->AccessMode = smReadWrite;
  1635. KeyList = new TStringList();
  1636. if (SourceStorage->OpenRootKey(false) &&
  1637. TargetStorage->OpenRootKey(true))
  1638. {
  1639. SourceStorage->GetValueNames(KeyList);
  1640. TSessionData * Session;
  1641. AnsiString HostKeyName;
  1642. assert(Sessions != NULL);
  1643. for (int Index = 0; Index < Sessions->Count; Index++)
  1644. {
  1645. Session = Sessions->Sessions[Index];
  1646. if (!OnlySelected || Session->Selected)
  1647. {
  1648. HostKeyName = MungeStr(FORMAT("@%d:%s", (Session->PortNumber, Session->HostName)));
  1649. AnsiString KeyName;
  1650. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  1651. {
  1652. KeyName = KeyList->Strings[KeyIndex];
  1653. int P = KeyName.Pos(HostKeyName);
  1654. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  1655. {
  1656. TargetStorage->WriteStringRaw(KeyName,
  1657. SourceStorage->ReadStringRaw(KeyName, ""));
  1658. }
  1659. }
  1660. }
  1661. }
  1662. }
  1663. }
  1664. __finally
  1665. {
  1666. delete SourceStorage;
  1667. delete TargetStorage;
  1668. delete KeyList;
  1669. }
  1670. }
  1671. //---------------------------------------------------------------------------
  1672. TSessionData * __fastcall TStoredSessionList::ParseUrl(AnsiString Url,
  1673. bool & DefaultsOnly, int Params, AnsiString * FileName)
  1674. {
  1675. bool ProtocolDefined = false;
  1676. TFSProtocol Protocol;
  1677. if (Url.SubString(1, 6).LowerCase() == "scp://")
  1678. {
  1679. Protocol = fsSCPonly;
  1680. Url.Delete(1, 6);
  1681. ProtocolDefined = true;
  1682. }
  1683. else if (Url.SubString(1, 7).LowerCase() == "sftp://")
  1684. {
  1685. Protocol = fsSFTPonly;
  1686. Url.Delete(1, 7);
  1687. ProtocolDefined = true;
  1688. }
  1689. DefaultsOnly = true;
  1690. TSessionData * Data = new TSessionData("");
  1691. try
  1692. {
  1693. if (!Url.IsEmpty())
  1694. {
  1695. TSessionData * AData = NULL;
  1696. // lookup stored session session even if protocol was defined
  1697. // (this allows setting for example default username for host
  1698. // by creating stored session named by host)
  1699. AnsiString ConnectInfo;
  1700. AnsiString RemoteDirectory;
  1701. if (TSessionData::ParseUrl(Url, Params, &ConnectInfo, NULL, NULL, NULL,
  1702. NULL, &RemoteDirectory, FileName))
  1703. {
  1704. AData = dynamic_cast<TSessionData *>(FindByName(ConnectInfo, false));
  1705. }
  1706. if (AData == NULL)
  1707. {
  1708. Data->Assign(DefaultSettings);
  1709. if (Data->ParseUrl(Url, Params, FileName))
  1710. {
  1711. Data->Name = "";
  1712. DefaultsOnly = false;
  1713. }
  1714. else
  1715. {
  1716. throw Exception(FMTLOAD(SESSION_NOT_EXISTS_ERROR, (Url)));
  1717. }
  1718. }
  1719. else
  1720. {
  1721. DefaultsOnly = false;
  1722. Data->Assign(AData);
  1723. if (!RemoteDirectory.IsEmpty())
  1724. {
  1725. Data->RemoteDirectory = RemoteDirectory;
  1726. }
  1727. if (IsHidden(AData))
  1728. {
  1729. AData->Remove();
  1730. Remove(AData);
  1731. Save();
  1732. }
  1733. }
  1734. }
  1735. else
  1736. {
  1737. Data->Assign(DefaultSettings);
  1738. }
  1739. if (ProtocolDefined)
  1740. {
  1741. Data->FSProtocol = Protocol;
  1742. }
  1743. }
  1744. catch(...)
  1745. {
  1746. delete Data;
  1747. throw;
  1748. }
  1749. return Data;
  1750. }