SessionData.cpp 63 KB

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