SessionData.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "SessionData.h"
  5. #include "Common.h"
  6. #include "Exceptions.h"
  7. #include "FileBuffer.h"
  8. #include "CoreMain.h"
  9. #include "TextsCore.h"
  10. #include "PuttyIntf.h"
  11. #include "RemoteFiles.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. enum TProxyType { pxNone, pxHTTP, pxSocks, pxTelnet }; // 0.53b and older
  16. const char * DefaultName = "Default Settings";
  17. const char CipherNames[CIPHER_COUNT][10] = {"WARN", "3des", "blowfish", "aes", "des", "arcfour"};
  18. const char KexNames[KEX_COUNT][20] = {"WARN", "dh-group1-sha1", "dh-group14-sha1", "dh-gex-sha1", "rsa" };
  19. const char ProtocolNames[PROTOCOL_COUNT][10] = { "raw", "telnet", "rlogin", "ssh" };
  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, cipArcfour, cipDES };
  24. const TKex DefaultKexList[KEX_COUNT] =
  25. { kexDHGEx, kexDHGroup14, kexDHGroup1, kexRSA, kexWarn };
  26. const char FSProtocolNames[FSPROTOCOL_COUNT][11] = { "SCP", "SFTP (SCP)", "SFTP", "", "", "FTP" };
  27. const int SshPortNumber = 22;
  28. const int FtpPortNumber = 21;
  29. const int FtpsImplicitPortNumber = 990;
  30. //---------------------------------------------------------------------
  31. TDateTime __fastcall SecToDateTime(int Sec)
  32. {
  33. return TDateTime((unsigned short)(Sec/60/60),
  34. (unsigned short)(Sec/60%60), (unsigned short)(Sec%60), 0);
  35. }
  36. //--- TSessionData ----------------------------------------------------
  37. __fastcall TSessionData::TSessionData(AnsiString aName):
  38. TNamedObject(aName)
  39. {
  40. Default();
  41. FModified = true;
  42. }
  43. //---------------------------------------------------------------------
  44. void __fastcall TSessionData::Default()
  45. {
  46. HostName = "";
  47. PortNumber = SshPortNumber;
  48. UserName = "";
  49. Password = "";
  50. Passwordless = false;
  51. PingInterval = 30;
  52. // when changing default, update load/save logic
  53. PingType = ptOff;
  54. Timeout = 15;
  55. TryAgent = true;
  56. AgentFwd = false;
  57. AuthTIS = false;
  58. AuthKI = true;
  59. AuthKIPassword = true;
  60. AuthGSSAPI = false;
  61. GSSAPIFwdTGT = false;
  62. GSSAPIServerRealm = "";
  63. ChangeUsername = false;
  64. Compression = false;
  65. SshProt = ssh2;
  66. Ssh2DES = false;
  67. SshNoUserAuth = false;
  68. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  69. {
  70. Cipher[Index] = DefaultCipherList[Index];
  71. }
  72. for (int Index = 0; Index < KEX_COUNT; Index++)
  73. {
  74. Kex[Index] = DefaultKexList[Index];
  75. }
  76. PublicKeyFile = "";
  77. FProtocol = ptSSH;
  78. TcpNoDelay = true;
  79. HostKey = "";
  80. ProxyMethod = pmNone;
  81. ProxyHost = "proxy";
  82. ProxyPort = 80;
  83. ProxyUsername = "";
  84. ProxyPassword = "";
  85. ProxyTelnetCommand = "connect %host %port\\n";
  86. ProxyLocalCommand = "";
  87. ProxyDNS = asAuto;
  88. ProxyLocalhost = false;
  89. for (int Index = 0; Index < LENOF(FBugs); Index++)
  90. {
  91. Bug[(TSshBug)Index] = asAuto;
  92. }
  93. Special = false;
  94. FSProtocol = fsSFTP;
  95. AddressFamily = afAuto;
  96. RekeyData = "1G";
  97. RekeyTime = 60;
  98. // FS common
  99. LocalDirectory = "";
  100. RemoteDirectory = "";
  101. UpdateDirectories = false;
  102. CacheDirectories = true;
  103. CacheDirectoryChanges = true;
  104. PreserveDirectoryChanges = true;
  105. LockInHome = false;
  106. ResolveSymlinks = true;
  107. DSTMode = dstmUnix;
  108. DeleteToRecycleBin = false;
  109. OverwrittenToRecycleBin = false;
  110. RecycleBinPath = "/tmp";
  111. Color = 0;
  112. PostLoginCommands = "";
  113. // SCP
  114. ReturnVar = "";
  115. LookupUserGroups = true;
  116. EOLType = eolLF;
  117. Shell = ""; //default shell
  118. ReturnVar = "";
  119. ClearAliases = true;
  120. UnsetNationalVars = true;
  121. ListingCommand = "ls -la";
  122. IgnoreLsWarnings = true;
  123. Scp1Compatibility = false;
  124. TimeDifference = 0;
  125. SCPLsFullTime = asAuto;
  126. NotUtf = asAuto;
  127. // SFTP
  128. SftpServer = "";
  129. SFTPDownloadQueue = 4;
  130. SFTPUploadQueue = 4;
  131. SFTPListingQueue = 2;
  132. SFTPMaxVersion = 5;
  133. SFTPMaxPacketSize = 0;
  134. for (int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  135. {
  136. SFTPBug[(TSftpBug)Index] = asAuto;
  137. }
  138. Tunnel = false;
  139. TunnelHostName = "";
  140. TunnelPortNumber = SshPortNumber;
  141. TunnelUserName = "";
  142. TunnelPassword = "";
  143. TunnelPublicKeyFile = "";
  144. TunnelLocalPortNumber = 0;
  145. TunnelPortFwd = "";
  146. // FTP
  147. FtpPasvMode = true;
  148. FtpForcePasvIp = asAuto;
  149. FtpAccount = "";
  150. FtpPingInterval = 30;
  151. FtpPingType = ptDummyCommand;
  152. Ftps = ftpsNone;
  153. FtpListAll = asAuto;
  154. SslSessionReuse = true;
  155. FtpProxyLogonType = 0; // none
  156. CustomParam1 = "";
  157. CustomParam2 = "";
  158. Selected = false;
  159. FModified = false;
  160. FSource = ::ssNone;
  161. // add also to TSessionLog::AddStartupInfo()
  162. }
  163. //---------------------------------------------------------------------
  164. void __fastcall TSessionData::NonPersistant()
  165. {
  166. UpdateDirectories = false;
  167. PreserveDirectoryChanges = false;
  168. }
  169. //---------------------------------------------------------------------
  170. void __fastcall TSessionData::Assign(TPersistent * Source)
  171. {
  172. if (Source && Source->InheritsFrom(__classid(TSessionData)))
  173. {
  174. #define DUPL(P) P = ((TSessionData *)Source)->P
  175. DUPL(Name);
  176. DUPL(HostName);
  177. DUPL(PortNumber);
  178. DUPL(UserName);
  179. DUPL(Password);
  180. DUPL(Passwordless);
  181. DUPL(PingInterval);
  182. DUPL(PingType);
  183. DUPL(Timeout);
  184. DUPL(TryAgent);
  185. DUPL(AgentFwd);
  186. DUPL(AuthTIS);
  187. DUPL(ChangeUsername);
  188. DUPL(Compression);
  189. DUPL(SshProt);
  190. DUPL(Ssh2DES);
  191. DUPL(SshNoUserAuth);
  192. DUPL(CipherList);
  193. DUPL(KexList);
  194. DUPL(PublicKeyFile);
  195. DUPL(AddressFamily);
  196. DUPL(RekeyData);
  197. DUPL(RekeyTime);
  198. DUPL(HostKey);
  199. DUPL(FSProtocol);
  200. DUPL(LocalDirectory);
  201. DUPL(RemoteDirectory);
  202. DUPL(UpdateDirectories);
  203. DUPL(CacheDirectories);
  204. DUPL(CacheDirectoryChanges);
  205. DUPL(PreserveDirectoryChanges);
  206. DUPL(ResolveSymlinks);
  207. DUPL(DSTMode);
  208. DUPL(LockInHome);
  209. DUPL(Special);
  210. DUPL(Selected);
  211. DUPL(ReturnVar);
  212. DUPL(LookupUserGroups);
  213. DUPL(EOLType);
  214. DUPL(Shell);
  215. DUPL(ClearAliases);
  216. DUPL(Scp1Compatibility);
  217. DUPL(UnsetNationalVars);
  218. DUPL(ListingCommand);
  219. DUPL(IgnoreLsWarnings);
  220. DUPL(SCPLsFullTime);
  221. DUPL(TimeDifference);
  222. // new in 53b
  223. DUPL(TcpNoDelay);
  224. DUPL(AuthKI);
  225. DUPL(AuthKIPassword);
  226. DUPL(AuthGSSAPI);
  227. DUPL(GSSAPIFwdTGT);
  228. DUPL(GSSAPIServerRealm);
  229. DUPL(DeleteToRecycleBin);
  230. DUPL(OverwrittenToRecycleBin);
  231. DUPL(RecycleBinPath);
  232. DUPL(NotUtf);
  233. DUPL(PostLoginCommands);
  234. DUPL(ProxyMethod);
  235. DUPL(ProxyHost);
  236. DUPL(ProxyPort);
  237. DUPL(ProxyUsername);
  238. DUPL(ProxyPassword);
  239. DUPL(ProxyTelnetCommand);
  240. DUPL(ProxyLocalCommand);
  241. DUPL(ProxyDNS);
  242. DUPL(ProxyLocalhost);
  243. for (int Index = 0; Index < LENOF(FBugs); Index++)
  244. {
  245. DUPL(Bug[(TSshBug)Index]);
  246. }
  247. // SFTP
  248. DUPL(SftpServer);
  249. DUPL(SFTPDownloadQueue);
  250. DUPL(SFTPUploadQueue);
  251. DUPL(SFTPListingQueue);
  252. DUPL(SFTPMaxVersion);
  253. DUPL(SFTPMaxPacketSize);
  254. for (int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  255. {
  256. DUPL(SFTPBug[(TSftpBug)Index]);
  257. }
  258. DUPL(Color);
  259. DUPL(Tunnel);
  260. DUPL(TunnelHostName);
  261. DUPL(TunnelPortNumber);
  262. DUPL(TunnelUserName);
  263. DUPL(TunnelPassword);
  264. DUPL(TunnelPublicKeyFile);
  265. DUPL(TunnelLocalPortNumber);
  266. DUPL(TunnelPortFwd);
  267. DUPL(FtpPasvMode);
  268. DUPL(FtpForcePasvIp);
  269. DUPL(FtpAccount);
  270. DUPL(FtpPingInterval);
  271. DUPL(FtpPingType);
  272. DUPL(Ftps);
  273. DUPL(FtpListAll);
  274. DUPL(SslSessionReuse);
  275. DUPL(FtpProxyLogonType);
  276. DUPL(CustomParam1);
  277. DUPL(CustomParam2);
  278. #undef DUPL
  279. FModified = ((TSessionData *)Source)->Modified;
  280. FSource = ((TSessionData *)Source)->FSource;
  281. }
  282. else
  283. {
  284. TNamedObject::Assign(Source);
  285. }
  286. }
  287. //---------------------------------------------------------------------
  288. void __fastcall TSessionData::Load(THierarchicalStorage * Storage)
  289. {
  290. bool RewritePassword = false;
  291. if (Storage->OpenSubKey(InternalStorageKey, False))
  292. {
  293. PortNumber = Storage->ReadInteger("PortNumber", PortNumber);
  294. UserName = Storage->ReadString("UserName", UserName);
  295. // must be loaded after UserName, because HostName may be in format user@host
  296. HostName = Storage->ReadString("HostName", HostName);
  297. if (!Configuration->DisablePasswordStoring)
  298. {
  299. if (Storage->ValueExists("PasswordPlain"))
  300. {
  301. Password = Storage->ReadString("PasswordPlain", Password);
  302. RewritePassword = true;
  303. }
  304. else
  305. {
  306. FPassword = Storage->ReadString("Password", FPassword);
  307. }
  308. }
  309. Passwordless = Storage->ReadBool("Passwordless", Passwordless);
  310. // Putty uses PingIntervalSecs
  311. int PingIntervalSecs = Storage->ReadInteger("PingIntervalSecs", -1);
  312. if (PingIntervalSecs < 0)
  313. {
  314. PingIntervalSecs = Storage->ReadInteger("PingIntervalSec", PingInterval%60);
  315. }
  316. PingInterval =
  317. Storage->ReadInteger("PingInterval", PingInterval/60)*60 +
  318. PingIntervalSecs;
  319. if (PingInterval == 0)
  320. {
  321. PingInterval = 30;
  322. }
  323. // PingType has not existed before 3.5, where PingInterval > 0 meant today's ptNullPacket
  324. // Since 3.5, until 4.1 PingType was stored unconditionally.
  325. // Since 4.1 PingType is stored when it is not ptOff (default) or
  326. // when PingInterval is stored.
  327. if (!Storage->ValueExists("PingType"))
  328. {
  329. if (Storage->ReadInteger("PingInterval", 0) > 0)
  330. {
  331. PingType = ptNullPacket;
  332. }
  333. }
  334. else
  335. {
  336. PingType = static_cast<TPingType>(Storage->ReadInteger("PingType", ptOff));
  337. }
  338. Timeout = Storage->ReadInteger("Timeout", Timeout);
  339. TryAgent = Storage->ReadBool("TryAgent", TryAgent);
  340. AgentFwd = Storage->ReadBool("AgentFwd", AgentFwd);
  341. AuthTIS = Storage->ReadBool("AuthTIS", AuthTIS);
  342. AuthKI = Storage->ReadBool("AuthKI", AuthKI);
  343. AuthKIPassword = Storage->ReadBool("AuthKIPassword", AuthKIPassword);
  344. // Continue to use setting keys of previous kerberos implementation (vaclav tomec),
  345. // but fallback to keys of other implementations (official putty and vintela quest putty),
  346. // to allow imports from all putty versions.
  347. // Both vaclav tomec and official putty use AuthGSSAPI
  348. AuthGSSAPI = Storage->ReadBool("AuthGSSAPI", Storage->ReadBool("AuthSSPI", AuthGSSAPI));
  349. GSSAPIFwdTGT = Storage->ReadBool("GSSAPIFwdTGT", Storage->ReadBool("GssapiFwd", Storage->ReadBool("SSPIFwdTGT", GSSAPIFwdTGT)));
  350. GSSAPIServerRealm = Storage->ReadString("GSSAPIServerRealm", Storage->ReadString("KerbPrincipal", GSSAPIServerRealm));
  351. ChangeUsername = Storage->ReadBool("ChangeUsername", ChangeUsername);
  352. Compression = Storage->ReadBool("Compression", Compression);
  353. SshProt = (TSshProt)Storage->ReadInteger("SshProt", SshProt);
  354. Ssh2DES = Storage->ReadBool("Ssh2DES", Ssh2DES);
  355. SshNoUserAuth = Storage->ReadBool("SshNoUserAuth", SshNoUserAuth);
  356. CipherList = Storage->ReadString("Cipher", CipherList);
  357. KexList = Storage->ReadString("KEX", KexList);
  358. PublicKeyFile = Storage->ReadString("PublicKeyFile", PublicKeyFile);
  359. AddressFamily = static_cast<TAddressFamily>
  360. (Storage->ReadInteger("AddressFamily", AddressFamily));
  361. RekeyData = Storage->ReadString("RekeyBytes", RekeyData);
  362. RekeyTime = Storage->ReadInteger("RekeyTime", RekeyTime);
  363. FSProtocol = (TFSProtocol)Storage->ReadInteger("FSProtocol", FSProtocol);
  364. LocalDirectory = Storage->ReadString("LocalDirectory", LocalDirectory);
  365. RemoteDirectory = Storage->ReadString("RemoteDirectory", RemoteDirectory);
  366. UpdateDirectories = Storage->ReadBool("UpdateDirectories", UpdateDirectories);
  367. CacheDirectories = Storage->ReadBool("CacheDirectories", CacheDirectories);
  368. CacheDirectoryChanges = Storage->ReadBool("CacheDirectoryChanges", CacheDirectoryChanges);
  369. PreserveDirectoryChanges = Storage->ReadBool("PreserveDirectoryChanges", PreserveDirectoryChanges);
  370. ResolveSymlinks = Storage->ReadBool("ResolveSymlinks", ResolveSymlinks);
  371. DSTMode = (TDSTMode)Storage->ReadInteger("ConsiderDST", DSTMode);
  372. LockInHome = Storage->ReadBool("LockInHome", LockInHome);
  373. Special = Storage->ReadBool("Special", Special);
  374. Shell = Storage->ReadString("Shell", Shell);
  375. ClearAliases = Storage->ReadBool("ClearAliases", ClearAliases);
  376. UnsetNationalVars = Storage->ReadBool("UnsetNationalVars", UnsetNationalVars);
  377. ListingCommand = Storage->ReadString("ListingCommand",
  378. Storage->ReadBool("AliasGroupList", false) ? AnsiString("ls -gla") : ListingCommand);
  379. IgnoreLsWarnings = Storage->ReadBool("IgnoreLsWarnings", IgnoreLsWarnings);
  380. SCPLsFullTime = TAutoSwitch(Storage->ReadInteger("SCPLsFullTime", SCPLsFullTime));
  381. Scp1Compatibility = Storage->ReadBool("Scp1Compatibility", Scp1Compatibility);
  382. TimeDifference = Storage->ReadFloat("TimeDifference", TimeDifference);
  383. DeleteToRecycleBin = Storage->ReadBool("DeleteToRecycleBin", DeleteToRecycleBin);
  384. OverwrittenToRecycleBin = Storage->ReadBool("OverwrittenToRecycleBin", OverwrittenToRecycleBin);
  385. RecycleBinPath = Storage->ReadString("RecycleBinPath", RecycleBinPath);
  386. PostLoginCommands = Storage->ReadString("PostLoginCommands", PostLoginCommands);
  387. ReturnVar = Storage->ReadString("ReturnVar", ReturnVar);
  388. LookupUserGroups = Storage->ReadBool("LookupUserGroups", LookupUserGroups);
  389. EOLType = (TEOLType)Storage->ReadInteger("EOLType", EOLType);
  390. NotUtf = TAutoSwitch(Storage->ReadInteger("Utf", Storage->ReadInteger("SFTPUtfBug", NotUtf)));
  391. TcpNoDelay = Storage->ReadBool("TcpNoDelay", TcpNoDelay);
  392. ProxyMethod = (TProxyMethod)Storage->ReadInteger("ProxyMethod", -1);
  393. if (ProxyMethod < 0)
  394. {
  395. int ProxyType = Storage->ReadInteger("ProxyType", pxNone);
  396. int ProxySOCKSVersion;
  397. switch (ProxyType) {
  398. case pxHTTP:
  399. ProxyMethod = pmHTTP;
  400. break;
  401. case pxTelnet:
  402. ProxyMethod = pmTelnet;
  403. break;
  404. case pxSocks:
  405. ProxySOCKSVersion = Storage->ReadInteger("ProxySOCKSVersion", 5);
  406. ProxyMethod = ProxySOCKSVersion == 5 ? pmSocks5 : pmSocks4;
  407. break;
  408. default:
  409. case pxNone:
  410. ProxyMethod = pmNone;
  411. break;
  412. }
  413. }
  414. ProxyHost = Storage->ReadString("ProxyHost", ProxyHost);
  415. ProxyPort = Storage->ReadInteger("ProxyPort", ProxyPort);
  416. ProxyUsername = Storage->ReadString("ProxyUsername", ProxyUsername);
  417. if (Storage->ValueExists("ProxyPassword"))
  418. {
  419. // encrypt unencrypted password
  420. ProxyPassword = Storage->ReadString("ProxyPassword", "");
  421. }
  422. else
  423. {
  424. // load encrypted password
  425. FProxyPassword = Storage->ReadString("ProxyPasswordEnc", FProxyPassword);
  426. }
  427. if (ProxyMethod == pmCmd)
  428. {
  429. ProxyLocalCommand = Storage->ReadStringRaw("ProxyTelnetCommand", ProxyLocalCommand);
  430. }
  431. else
  432. {
  433. ProxyTelnetCommand = Storage->ReadStringRaw("ProxyTelnetCommand", ProxyTelnetCommand);
  434. }
  435. ProxyDNS = TAutoSwitch((Storage->ReadInteger("ProxyDNS", (ProxyDNS + 2) % 3) + 1) % 3);
  436. ProxyLocalhost = Storage->ReadBool("ProxyLocalhost", ProxyLocalhost);
  437. #define READ_BUG(BUG) \
  438. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger("Bug"#BUG, \
  439. 2 - Bug[sb##BUG]));
  440. READ_BUG(Ignore1);
  441. READ_BUG(PlainPW1);
  442. READ_BUG(RSA1);
  443. READ_BUG(HMAC2);
  444. READ_BUG(DeriveKey2);
  445. READ_BUG(RSAPad2);
  446. READ_BUG(Rekey2);
  447. READ_BUG(PKSessID2);
  448. READ_BUG(MaxPkt2);
  449. #undef READ_BUG
  450. if ((Bug[sbHMAC2] == asAuto) &&
  451. Storage->ReadBool("BuggyMAC", false))
  452. {
  453. Bug[sbHMAC2] = asOn;
  454. }
  455. SftpServer = Storage->ReadString("SftpServer", SftpServer);
  456. #define READ_SFTP_BUG(BUG) \
  457. SFTPBug[sb##BUG] = TAutoSwitch(Storage->ReadInteger("SFTP" #BUG "Bug", SFTPBug[sb##BUG]));
  458. READ_SFTP_BUG(Symlink);
  459. READ_SFTP_BUG(SignedTS);
  460. #undef READ_SFTP_BUG
  461. SFTPMaxVersion = Storage->ReadInteger("SFTPMaxVersion", SFTPMaxVersion);
  462. SFTPMaxPacketSize = Storage->ReadInteger("SFTPMaxPacketSize", SFTPMaxPacketSize);
  463. Color = Storage->ReadInteger("Color", Color);
  464. ProtocolStr = Storage->ReadString("Protocol", ProtocolStr);
  465. Tunnel = Storage->ReadBool("Tunnel", Tunnel);
  466. TunnelPortNumber = Storage->ReadInteger("TunnelPortNumber", TunnelPortNumber);
  467. TunnelUserName = Storage->ReadString("TunnelUserName", TunnelUserName);
  468. // must be loaded after TunnelUserName,
  469. // because TunnelHostName may be in format user@host
  470. TunnelHostName = Storage->ReadString("TunnelHostName", TunnelHostName);
  471. if (!Configuration->DisablePasswordStoring)
  472. {
  473. if (Storage->ValueExists("TunnelPasswordPlain"))
  474. {
  475. TunnelPassword = Storage->ReadString("TunnelPasswordPlain", TunnelPassword);
  476. RewritePassword = true;
  477. }
  478. else
  479. {
  480. FTunnelPassword = Storage->ReadString("TunnelPassword", FTunnelPassword);
  481. }
  482. }
  483. TunnelPublicKeyFile = Storage->ReadString("TunnelPublicKeyFile", TunnelPublicKeyFile);
  484. TunnelLocalPortNumber = Storage->ReadInteger("TunnelLocalPortNumber", TunnelLocalPortNumber);
  485. // Ftp prefix
  486. FtpPasvMode = Storage->ReadBool("FtpPasvMode", FtpPasvMode);
  487. FtpForcePasvIp = TAutoSwitch(Storage->ReadInteger(L"FtpForcePasvIp2", FtpForcePasvIp));
  488. FtpAccount = Storage->ReadString("FtpAccount", FtpAccount);
  489. FtpPingInterval = Storage->ReadInteger("FtpPingInterval", FtpPingInterval);
  490. FtpPingType = static_cast<TPingType>(Storage->ReadInteger("FtpPingType", FtpPingType));
  491. Ftps = static_cast<TFtps>(Storage->ReadInteger("Ftps", Ftps));
  492. FtpListAll = TAutoSwitch(Storage->ReadInteger("FtpListAll", FtpListAll));
  493. SslSessionReuse = Storage->ReadBool("SslSessionReuse", SslSessionReuse);
  494. FtpProxyLogonType = Storage->ReadInteger("FtpProxyLogonType", FtpProxyLogonType);
  495. CustomParam1 = Storage->ReadString("CustomParam1", CustomParam1);
  496. CustomParam2 = Storage->ReadString("CustomParam2", CustomParam2);
  497. Storage->CloseSubKey();
  498. };
  499. if (RewritePassword)
  500. {
  501. TStorageAccessMode AccessMode = Storage->AccessMode;
  502. Storage->AccessMode = smReadWrite;
  503. try
  504. {
  505. if (Storage->OpenSubKey(InternalStorageKey, true))
  506. {
  507. Storage->DeleteValue("PasswordPlain");
  508. if (!Password.IsEmpty())
  509. {
  510. Storage->WriteString("Password", FPassword);
  511. }
  512. Storage->DeleteValue("TunnelPasswordPlain");
  513. if (!TunnelPassword.IsEmpty())
  514. {
  515. Storage->WriteString("TunnelPassword", FTunnelPassword);
  516. }
  517. Storage->CloseSubKey();
  518. }
  519. }
  520. catch(...)
  521. {
  522. // ignore errors (like read-only INI file)
  523. }
  524. Storage->AccessMode = AccessMode;
  525. }
  526. FModified = false;
  527. FSource = ssStored;
  528. }
  529. //---------------------------------------------------------------------
  530. void __fastcall TSessionData::Save(THierarchicalStorage * Storage,
  531. bool PuttyExport, const TSessionData * Default)
  532. {
  533. if (Storage->OpenSubKey(InternalStorageKey, true))
  534. {
  535. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  536. if ((Default != NULL) && (CONV(Default->PROPERTY) == CONV(PROPERTY))) \
  537. { \
  538. Storage->DeleteValue(NAME); \
  539. } \
  540. else \
  541. { \
  542. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  543. }
  544. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  545. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, #PROPERTY, PROPERTY, )
  546. WRITE_DATA(String, HostName);
  547. WRITE_DATA(Integer, PortNumber);
  548. WRITE_DATA(Bool, Passwordless);
  549. WRITE_DATA_EX(Integer, "PingInterval", PingInterval / 60, );
  550. WRITE_DATA_EX(Integer, "PingIntervalSecs", PingInterval % 60, );
  551. Storage->DeleteValue("PingIntervalSec"); // obsolete
  552. // when PingInterval is stored always store PingType not to attempt to
  553. // deduce PingType from PingInterval (backward compatibility with pre 3.5)
  554. if (((Default != NULL) && (PingType != Default->PingType)) ||
  555. Storage->ValueExists("PingInterval"))
  556. {
  557. Storage->WriteInteger("PingType", PingType);
  558. }
  559. else
  560. {
  561. Storage->DeleteValue("PingType");
  562. }
  563. WRITE_DATA(Integer, Timeout);
  564. WRITE_DATA(Bool, TryAgent);
  565. WRITE_DATA(Bool, AgentFwd);
  566. WRITE_DATA(Bool, AuthTIS);
  567. WRITE_DATA(Bool, AuthKI);
  568. WRITE_DATA(Bool, AuthKIPassword);
  569. WRITE_DATA(Bool, AuthGSSAPI);
  570. WRITE_DATA(Bool, GSSAPIFwdTGT);
  571. WRITE_DATA(String, GSSAPIServerRealm);
  572. Storage->DeleteValue("TryGSSKEX");
  573. Storage->DeleteValue("UserNameFromEnvironment");
  574. Storage->DeleteValue("GSSAPIServerChoosesUserName");
  575. Storage->DeleteValue("GSSAPITrustDNS");
  576. if (PuttyExport)
  577. {
  578. // duplicate kerberos setting with keys of the vintela quest putty
  579. WRITE_DATA_EX(Bool, "AuthSSPI", AuthGSSAPI, );
  580. WRITE_DATA_EX(Bool, "SSPIFwdTGT", GSSAPIFwdTGT, );
  581. WRITE_DATA_EX(String, "KerbPrincipal", GSSAPIServerRealm, );
  582. // duplicate kerberos setting with keys of the official putty
  583. WRITE_DATA_EX(Bool, "GssapiFwd", GSSAPIFwdTGT, );
  584. }
  585. WRITE_DATA(Bool, ChangeUsername);
  586. WRITE_DATA(Bool, Compression);
  587. WRITE_DATA(Integer, SshProt);
  588. WRITE_DATA(Bool, Ssh2DES);
  589. WRITE_DATA(Bool, SshNoUserAuth);
  590. WRITE_DATA_EX(String, "Cipher", CipherList, );
  591. WRITE_DATA_EX(String, "KEX", KexList, );
  592. WRITE_DATA(Integer, AddressFamily);
  593. WRITE_DATA_EX(String, "RekeyBytes", RekeyData, );
  594. WRITE_DATA(Integer, RekeyTime);
  595. WRITE_DATA(Bool, TcpNoDelay);
  596. if (PuttyExport)
  597. {
  598. WRITE_DATA(StringRaw, UserName);
  599. WRITE_DATA(StringRaw, PublicKeyFile);
  600. }
  601. else
  602. {
  603. WRITE_DATA(String, UserName);
  604. WRITE_DATA(String, PublicKeyFile);
  605. WRITE_DATA(Integer, FSProtocol);
  606. WRITE_DATA(String, LocalDirectory);
  607. WRITE_DATA(String, RemoteDirectory);
  608. WRITE_DATA(Bool, UpdateDirectories);
  609. WRITE_DATA(Bool, CacheDirectories);
  610. WRITE_DATA(Bool, CacheDirectoryChanges);
  611. WRITE_DATA(Bool, PreserveDirectoryChanges);
  612. WRITE_DATA(Bool, ResolveSymlinks);
  613. WRITE_DATA_EX(Integer, "ConsiderDST", DSTMode, );
  614. WRITE_DATA(Bool, LockInHome);
  615. // Special is never stored (if it would, login dialog must be modified not to
  616. // duplicate Special parameter when Special session is loaded and then stored
  617. // under different name)
  618. // WRITE_DATA(Bool, Special);
  619. WRITE_DATA(String, Shell);
  620. WRITE_DATA(Bool, ClearAliases);
  621. WRITE_DATA(Bool, UnsetNationalVars);
  622. WRITE_DATA(String, ListingCommand);
  623. WRITE_DATA(Bool, IgnoreLsWarnings);
  624. WRITE_DATA(Integer, SCPLsFullTime);
  625. WRITE_DATA(Bool, Scp1Compatibility);
  626. WRITE_DATA(Float, TimeDifference);
  627. WRITE_DATA(Bool, DeleteToRecycleBin);
  628. WRITE_DATA(Bool, OverwrittenToRecycleBin);
  629. WRITE_DATA(String, RecycleBinPath);
  630. WRITE_DATA(String, PostLoginCommands);
  631. WRITE_DATA(String, ReturnVar);
  632. WRITE_DATA(Bool, LookupUserGroups);
  633. WRITE_DATA(Integer, EOLType);
  634. Storage->DeleteValue("SFTPUtfBug");
  635. WRITE_DATA_EX(Integer, "Utf", NotUtf, );
  636. }
  637. WRITE_DATA(Integer, ProxyMethod);
  638. if (PuttyExport)
  639. {
  640. // support for Putty 0.53b and older
  641. int ProxyType;
  642. int ProxySOCKSVersion = 5;
  643. switch (ProxyMethod) {
  644. case pmHTTP:
  645. ProxyType = pxHTTP;
  646. break;
  647. case pmTelnet:
  648. ProxyType = pxTelnet;
  649. break;
  650. case pmSocks5:
  651. ProxyType = pxSocks;
  652. ProxySOCKSVersion = 5;
  653. break;
  654. case pmSocks4:
  655. ProxyType = pxSocks;
  656. ProxySOCKSVersion = 4;
  657. break;
  658. default:
  659. case pmNone:
  660. ProxyType = pxNone;
  661. break;
  662. }
  663. Storage->WriteInteger("ProxyType", ProxyType);
  664. Storage->WriteInteger("ProxySOCKSVersion", ProxySOCKSVersion);
  665. }
  666. else
  667. {
  668. Storage->DeleteValue("ProxyType");
  669. Storage->DeleteValue("ProxySOCKSVersion");
  670. }
  671. WRITE_DATA(String, ProxyHost);
  672. WRITE_DATA(Integer, ProxyPort);
  673. WRITE_DATA(String, ProxyUsername);
  674. if (ProxyMethod == pmCmd)
  675. {
  676. WRITE_DATA_EX(StringRaw, "ProxyTelnetCommand", ProxyLocalCommand, );
  677. }
  678. else
  679. {
  680. WRITE_DATA(StringRaw, ProxyTelnetCommand);
  681. }
  682. #define WRITE_DATA_CONV_FUNC(X) (((X) + 2) % 3)
  683. WRITE_DATA_CONV(Integer, "ProxyDNS", ProxyDNS);
  684. #undef WRITE_DATA_CONV_FUNC
  685. WRITE_DATA(Bool, ProxyLocalhost);
  686. #define WRITE_DATA_CONV_FUNC(X) (2 - (X))
  687. #define WRITE_BUG(BUG) WRITE_DATA_CONV(Integer, "Bug" #BUG, Bug[sb##BUG]);
  688. WRITE_BUG(Ignore1);
  689. WRITE_BUG(PlainPW1);
  690. WRITE_BUG(RSA1);
  691. WRITE_BUG(HMAC2);
  692. WRITE_BUG(DeriveKey2);
  693. WRITE_BUG(RSAPad2);
  694. WRITE_BUG(Rekey2);
  695. WRITE_BUG(PKSessID2);
  696. WRITE_BUG(MaxPkt2);
  697. #undef WRITE_BUG
  698. #undef WRITE_DATA_CONV_FUNC
  699. Storage->DeleteValue("BuggyMAC");
  700. Storage->DeleteValue("AliasGroupList");
  701. if (PuttyExport)
  702. {
  703. WRITE_DATA_EX(String, "Protocol", ProtocolStr, );
  704. }
  705. if (!PuttyExport)
  706. {
  707. WRITE_DATA(String, SftpServer);
  708. #define WRITE_SFTP_BUG(BUG) WRITE_DATA_EX(Integer, "SFTP" #BUG "Bug", SFTPBug[sb##BUG], );
  709. WRITE_SFTP_BUG(Symlink);
  710. WRITE_SFTP_BUG(SignedTS);
  711. #undef WRITE_SFTP_BUG
  712. WRITE_DATA(Integer, SFTPMaxVersion);
  713. WRITE_DATA(Integer, SFTPMaxPacketSize);
  714. WRITE_DATA(Integer, Color);
  715. WRITE_DATA(Bool, Tunnel);
  716. WRITE_DATA(String, TunnelHostName);
  717. WRITE_DATA(Integer, TunnelPortNumber);
  718. WRITE_DATA(String, TunnelUserName);
  719. WRITE_DATA(String, TunnelPublicKeyFile);
  720. WRITE_DATA(Integer, TunnelLocalPortNumber);
  721. WRITE_DATA(Bool, FtpPasvMode);
  722. WRITE_DATA_EX(Integer, "FtpForcePasvIp2", FtpForcePasvIp, );
  723. WRITE_DATA(String, FtpAccount);
  724. WRITE_DATA(Integer, FtpPingInterval);
  725. WRITE_DATA(Integer, FtpPingType);
  726. WRITE_DATA(Integer, Ftps);
  727. WRITE_DATA(Integer, FtpListAll);
  728. WRITE_DATA(Bool, SslSessionReuse);
  729. WRITE_DATA(Integer, FtpProxyLogonType);
  730. WRITE_DATA(String, CustomParam1);
  731. WRITE_DATA(String, CustomParam2);
  732. }
  733. SavePasswords(Storage, PuttyExport);
  734. Storage->CloseSubKey();
  735. }
  736. }
  737. //---------------------------------------------------------------------
  738. void __fastcall TSessionData::SavePasswords(THierarchicalStorage * Storage, bool PuttyExport)
  739. {
  740. if (!Configuration->DisablePasswordStoring && !PuttyExport && !FPassword.IsEmpty())
  741. {
  742. Storage->WriteString("Password", StronglyRecryptPassword(FPassword, UserName+HostName));
  743. }
  744. else
  745. {
  746. Storage->DeleteValue("Password");
  747. }
  748. Storage->DeleteValue("PasswordPlain");
  749. if (PuttyExport)
  750. {
  751. // save password unencrypted
  752. Storage->WriteString("ProxyPassword", ProxyPassword);
  753. }
  754. else
  755. {
  756. // save password encrypted
  757. if (!FProxyPassword.IsEmpty())
  758. {
  759. Storage->WriteString("ProxyPasswordEnc", StronglyRecryptPassword(FProxyPassword, ProxyUsername+ProxyHost));
  760. }
  761. else
  762. {
  763. Storage->DeleteValue("ProxyPasswordEnc");
  764. }
  765. Storage->DeleteValue("ProxyPassword");
  766. if (!Configuration->DisablePasswordStoring && !FTunnelPassword.IsEmpty())
  767. {
  768. Storage->WriteString("TunnelPassword", StronglyRecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName));
  769. }
  770. else
  771. {
  772. Storage->DeleteValue("TunnelPassword");
  773. }
  774. }
  775. }
  776. //---------------------------------------------------------------------
  777. void __fastcall TSessionData::RecryptPasswords()
  778. {
  779. Password = Password;
  780. ProxyPassword = ProxyPassword;
  781. TunnelPassword = TunnelPassword;
  782. }
  783. //---------------------------------------------------------------------
  784. bool __fastcall TSessionData::HasAnyPassword()
  785. {
  786. return !FPassword.IsEmpty() || !FProxyPassword.IsEmpty() || !FTunnelPassword.IsEmpty();
  787. }
  788. //---------------------------------------------------------------------
  789. void __fastcall TSessionData::Modify()
  790. {
  791. FModified = true;
  792. if (FSource == ssStored)
  793. {
  794. FSource = ssStoredModified;
  795. }
  796. }
  797. //---------------------------------------------------------------------
  798. AnsiString __fastcall TSessionData::GetSource()
  799. {
  800. switch (FSource)
  801. {
  802. case ::ssNone:
  803. return "Ad-Hoc session";
  804. case ssStored:
  805. return "Stored session";
  806. case ssStoredModified:
  807. return "Modified stored session";
  808. default:
  809. assert(false);
  810. return "";
  811. }
  812. }
  813. //---------------------------------------------------------------------
  814. void __fastcall TSessionData::SaveRecryptedPasswords(THierarchicalStorage * Storage)
  815. {
  816. if (Storage->OpenSubKey(InternalStorageKey, true))
  817. {
  818. RecryptPasswords();
  819. SavePasswords(Storage, false);
  820. Storage->CloseSubKey();
  821. }
  822. }
  823. //---------------------------------------------------------------------
  824. void __fastcall TSessionData::Remove()
  825. {
  826. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  827. try
  828. {
  829. Storage->Explicit = true;
  830. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  831. {
  832. Storage->RecursiveDeleteSubKey(InternalStorageKey);
  833. }
  834. }
  835. __finally
  836. {
  837. delete Storage;
  838. }
  839. }
  840. //---------------------------------------------------------------------
  841. bool __fastcall TSessionData::ParseUrl(AnsiString Url, TOptions * Options,
  842. TStoredSessionList * StoredSessions, bool & DefaultsOnly, AnsiString * FileName,
  843. bool * AProtocolDefined)
  844. {
  845. bool ProtocolDefined = false;
  846. bool PortNumberDefined = false;
  847. TFSProtocol AFSProtocol;
  848. int APortNumber;
  849. TFtps AFtps = ftpsNone;
  850. if (Url.SubString(1, 4).LowerCase() == "scp:")
  851. {
  852. AFSProtocol = fsSCPonly;
  853. APortNumber = SshPortNumber;
  854. Url.Delete(1, 4);
  855. ProtocolDefined = true;
  856. }
  857. else if (Url.SubString(1, 5).LowerCase() == "sftp:")
  858. {
  859. AFSProtocol = fsSFTPonly;
  860. APortNumber = SshPortNumber;
  861. Url.Delete(1, 5);
  862. ProtocolDefined = true;
  863. }
  864. else if (Url.SubString(1, 4).LowerCase() == "ftp:")
  865. {
  866. AFSProtocol = fsFTP;
  867. Ftps = ftpsNone;
  868. APortNumber = FtpPortNumber;
  869. Url.Delete(1, 4);
  870. ProtocolDefined = true;
  871. }
  872. else if (Url.SubString(1, 5).LowerCase() == "ftps:")
  873. {
  874. AFSProtocol = fsFTP;
  875. AFtps = ftpsImplicit;
  876. APortNumber = FtpsImplicitPortNumber;
  877. Url.Delete(1, 5);
  878. ProtocolDefined = true;
  879. }
  880. if (ProtocolDefined && (Url.SubString(1, 2) == "//"))
  881. {
  882. Url.Delete(1, 2);
  883. }
  884. if (AProtocolDefined != NULL)
  885. {
  886. *AProtocolDefined = ProtocolDefined;
  887. }
  888. if (!Url.IsEmpty())
  889. {
  890. AnsiString DecodedUrl = DecodeUrlChars(Url);
  891. // lookup stored session even if protocol was defined
  892. // (this allows setting for example default username for host
  893. // by creating stored session named by host)
  894. TSessionData * Data = NULL;
  895. for (Integer Index = 0; Index < StoredSessions->Count + StoredSessions->HiddenCount; Index++)
  896. {
  897. TSessionData * AData = (TSessionData *)StoredSessions->Items[Index];
  898. if (AnsiSameText(AData->Name, DecodedUrl) ||
  899. AnsiSameText(AData->Name + "/", DecodedUrl.SubString(1, AData->Name.Length() + 1)))
  900. {
  901. Data = AData;
  902. break;
  903. }
  904. }
  905. AnsiString ARemoteDirectory;
  906. if (Data != NULL)
  907. {
  908. DefaultsOnly = false;
  909. Assign(Data);
  910. int P = 1;
  911. while (!AnsiSameText(DecodeUrlChars(Url.SubString(1, P)), Data->Name))
  912. {
  913. P++;
  914. assert(P <= Url.Length());
  915. }
  916. ARemoteDirectory = Url.SubString(P + 1, Url.Length() - P);
  917. if (Data->Hidden)
  918. {
  919. Data->Remove();
  920. StoredSessions->Remove(Data);
  921. // only modified, implicit
  922. StoredSessions->Save(false, false);
  923. }
  924. }
  925. else
  926. {
  927. Assign(StoredSessions->DefaultSettings);
  928. Name = "";
  929. int PSlash = Url.Pos("/");
  930. if (PSlash == 0)
  931. {
  932. PSlash = Url.Length() + 1;
  933. }
  934. AnsiString ConnectInfo = Url.SubString(1, PSlash - 1);
  935. int P = ConnectInfo.LastDelimiter("@");
  936. AnsiString UserInfo;
  937. AnsiString HostInfo;
  938. if (P > 0)
  939. {
  940. UserInfo = ConnectInfo.SubString(1, P - 1);
  941. HostInfo = ConnectInfo.SubString(P + 1, ConnectInfo.Length() - P);
  942. }
  943. else
  944. {
  945. HostInfo = ConnectInfo;
  946. }
  947. if ((HostInfo.Length() >= 2) && (HostInfo[1] == '[') && ((P = HostInfo.Pos("]")) > 0))
  948. {
  949. HostName = HostInfo.SubString(2, P - 2);
  950. HostInfo.Delete(1, P);
  951. if (!HostInfo.IsEmpty() && (HostInfo[1] == ':'))
  952. {
  953. HostInfo.Delete(1, 1);
  954. }
  955. }
  956. else
  957. {
  958. HostName = DecodeUrlChars(CutToChar(HostInfo, ':', true));
  959. }
  960. // expanded from ?: operator, as it caused strange "access violation" errors
  961. if (!HostInfo.IsEmpty())
  962. {
  963. PortNumber = StrToIntDef(DecodeUrlChars(HostInfo), -1);
  964. PortNumberDefined = true;
  965. }
  966. else if (ProtocolDefined)
  967. {
  968. PortNumber = APortNumber;
  969. }
  970. if (ProtocolDefined)
  971. {
  972. Ftps = AFtps;
  973. }
  974. bool PasswordSeparator = (UserInfo.Pos(':') != 0);
  975. UserName = DecodeUrlChars(CutToChar(UserInfo, ':', false));
  976. Password = DecodeUrlChars(UserInfo);
  977. Passwordless = Password.IsEmpty() && PasswordSeparator;
  978. ARemoteDirectory = Url.SubString(PSlash, Url.Length() - PSlash + 1);
  979. }
  980. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory != "/"))
  981. {
  982. if ((ARemoteDirectory[ARemoteDirectory.Length()] != '/') &&
  983. (FileName != NULL))
  984. {
  985. *FileName = DecodeUrlChars(UnixExtractFileName(ARemoteDirectory));
  986. ARemoteDirectory = UnixExtractFilePath(ARemoteDirectory);
  987. }
  988. RemoteDirectory = DecodeUrlChars(ARemoteDirectory);
  989. }
  990. DefaultsOnly = false;
  991. }
  992. else
  993. {
  994. Assign(StoredSessions->DefaultSettings);
  995. DefaultsOnly = true;
  996. }
  997. if (ProtocolDefined)
  998. {
  999. FSProtocol = AFSProtocol;
  1000. }
  1001. if (Options != NULL)
  1002. {
  1003. // we deliberatelly do keep defaultonly to false, in presence of any option,
  1004. // as the option should not make session "connectable"
  1005. AnsiString Value;
  1006. if (Options->FindSwitch("privatekey", Value))
  1007. {
  1008. PublicKeyFile = Value;
  1009. }
  1010. if (Options->FindSwitch("timeout", Value))
  1011. {
  1012. Timeout = StrToInt(Value);
  1013. }
  1014. if (Options->FindSwitch("hostkey", Value) ||
  1015. Options->FindSwitch("certificate", Value))
  1016. {
  1017. HostKey = Value;
  1018. }
  1019. if (Options->FindSwitch("passive", Value))
  1020. {
  1021. FtpPasvMode = (StrToIntDef(Value, 1) != 0);
  1022. }
  1023. if (Options->FindSwitch("implicit", Value))
  1024. {
  1025. bool Enabled = (StrToIntDef(Value, 1) != 0);
  1026. Ftps = Enabled ? ftpsImplicit : ftpsNone;
  1027. if (!PortNumberDefined && Enabled)
  1028. {
  1029. PortNumber = FtpsImplicitPortNumber;
  1030. }
  1031. }
  1032. if (Options->FindSwitch("explicitssl", Value))
  1033. {
  1034. bool Enabled = (StrToIntDef(Value, 1) != 0);
  1035. Ftps = Enabled ? ftpsExplicitSsl : ftpsNone;
  1036. if (!PortNumberDefined && Enabled)
  1037. {
  1038. PortNumber = FtpPortNumber;
  1039. }
  1040. }
  1041. if (Options->FindSwitch("explicittls", Value))
  1042. {
  1043. bool Enabled = (StrToIntDef(Value, 1) != 0);
  1044. Ftps = Enabled ? ftpsExplicitTls : ftpsNone;
  1045. if (!PortNumberDefined && Enabled)
  1046. {
  1047. PortNumber = FtpPortNumber;
  1048. }
  1049. }
  1050. }
  1051. return true;
  1052. }
  1053. //---------------------------------------------------------------------
  1054. void __fastcall TSessionData::ConfigureTunnel(int APortNumber)
  1055. {
  1056. FOrigHostName = HostName;
  1057. FOrigPortNumber = PortNumber;
  1058. FOrigProxyMethod = ProxyMethod;
  1059. HostName = "127.0.0.1";
  1060. PortNumber = APortNumber;
  1061. // proxy settings is used for tunnel
  1062. ProxyMethod = pmNone;
  1063. }
  1064. //---------------------------------------------------------------------
  1065. void __fastcall TSessionData::RollbackTunnel()
  1066. {
  1067. HostName = FOrigHostName;
  1068. PortNumber = FOrigPortNumber;
  1069. ProxyMethod = FOrigProxyMethod;
  1070. }
  1071. //---------------------------------------------------------------------
  1072. void __fastcall TSessionData::ExpandEnvironmentVariables()
  1073. {
  1074. PublicKeyFile = ::ExpandEnvironmentVariables(PublicKeyFile);
  1075. }
  1076. //---------------------------------------------------------------------
  1077. void __fastcall TSessionData::ValidatePath(const AnsiString Path)
  1078. {
  1079. // noop
  1080. }
  1081. //---------------------------------------------------------------------
  1082. void __fastcall TSessionData::ValidateName(const AnsiString Name)
  1083. {
  1084. if (Name.LastDelimiter("/") > 0)
  1085. {
  1086. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, "/")));
  1087. }
  1088. }
  1089. //---------------------------------------------------------------------
  1090. AnsiString __fastcall TSessionData::EncryptPassword(const AnsiString & Password, AnsiString Key)
  1091. {
  1092. return Configuration->EncryptPassword(Password, Key);
  1093. }
  1094. //---------------------------------------------------------------------
  1095. AnsiString __fastcall TSessionData::StronglyRecryptPassword(const AnsiString & Password, AnsiString Key)
  1096. {
  1097. return Configuration->StronglyRecryptPassword(Password, Key);
  1098. }
  1099. //---------------------------------------------------------------------
  1100. AnsiString __fastcall TSessionData::DecryptPassword(const AnsiString & Password, AnsiString Key)
  1101. {
  1102. AnsiString Result;
  1103. try
  1104. {
  1105. Result = Configuration->DecryptPassword(Password, Key);
  1106. }
  1107. catch(EAbort &)
  1108. {
  1109. // silently ignore aborted prompts for master password and return empty password
  1110. }
  1111. return Result;
  1112. }
  1113. //---------------------------------------------------------------------
  1114. bool __fastcall TSessionData::GetCanLogin()
  1115. {
  1116. return !FHostName.IsEmpty();
  1117. }
  1118. //---------------------------------------------------------------------------
  1119. AnsiString __fastcall TSessionData::GetSessionKey()
  1120. {
  1121. return FORMAT("%s@%s", (UserName, HostName));
  1122. }
  1123. //---------------------------------------------------------------------
  1124. AnsiString __fastcall TSessionData::GetInternalStorageKey()
  1125. {
  1126. if (Name.IsEmpty())
  1127. {
  1128. return SessionKey;
  1129. }
  1130. else
  1131. {
  1132. return Name;
  1133. }
  1134. }
  1135. //---------------------------------------------------------------------
  1136. AnsiString __fastcall TSessionData::GetStorageKey()
  1137. {
  1138. return SessionName;
  1139. }
  1140. //---------------------------------------------------------------------
  1141. void __fastcall TSessionData::SetHostName(AnsiString value)
  1142. {
  1143. if (FHostName != value)
  1144. {
  1145. // HostName is key for password encryption
  1146. AnsiString XPassword = Password;
  1147. int P = value.LastDelimiter("@");
  1148. if (P > 0)
  1149. {
  1150. UserName = value.SubString(1, P - 1);
  1151. value = value.SubString(P + 1, value.Length() - P);
  1152. }
  1153. FHostName = value;
  1154. Modify();
  1155. Password = XPassword;
  1156. if (!XPassword.IsEmpty())
  1157. {
  1158. XPassword.Unique();
  1159. memset(XPassword.c_str(), 0, XPassword.Length());
  1160. }
  1161. }
  1162. }
  1163. //---------------------------------------------------------------------
  1164. void __fastcall TSessionData::SetPortNumber(int value)
  1165. {
  1166. SET_SESSION_PROPERTY(PortNumber);
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TSessionData::SetShell(AnsiString value)
  1170. {
  1171. SET_SESSION_PROPERTY(Shell);
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. void __fastcall TSessionData::SetSftpServer(AnsiString value)
  1175. {
  1176. SET_SESSION_PROPERTY(SftpServer);
  1177. }
  1178. //---------------------------------------------------------------------------
  1179. void __fastcall TSessionData::SetClearAliases(bool value)
  1180. {
  1181. SET_SESSION_PROPERTY(ClearAliases);
  1182. }
  1183. //---------------------------------------------------------------------------
  1184. void __fastcall TSessionData::SetListingCommand(AnsiString value)
  1185. {
  1186. SET_SESSION_PROPERTY(ListingCommand);
  1187. }
  1188. //---------------------------------------------------------------------------
  1189. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  1190. {
  1191. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  1195. {
  1196. SET_SESSION_PROPERTY(UnsetNationalVars);
  1197. }
  1198. //---------------------------------------------------------------------
  1199. void __fastcall TSessionData::SetUserName(AnsiString value)
  1200. {
  1201. // UserName is key for password encryption
  1202. AnsiString XPassword = Password;
  1203. SET_SESSION_PROPERTY(UserName);
  1204. Password = XPassword;
  1205. if (!XPassword.IsEmpty())
  1206. {
  1207. XPassword.Unique();
  1208. memset(XPassword.c_str(), 0, XPassword.Length());
  1209. }
  1210. }
  1211. //---------------------------------------------------------------------
  1212. void __fastcall TSessionData::SetPassword(AnsiString value)
  1213. {
  1214. if (!value.IsEmpty())
  1215. {
  1216. Passwordless = false;
  1217. }
  1218. value = EncryptPassword(value, UserName+HostName);
  1219. SET_SESSION_PROPERTY(Password);
  1220. }
  1221. //---------------------------------------------------------------------
  1222. AnsiString __fastcall TSessionData::GetPassword()
  1223. {
  1224. return DecryptPassword(FPassword, UserName+HostName);
  1225. }
  1226. //---------------------------------------------------------------------
  1227. void __fastcall TSessionData::SetPasswordless(bool value)
  1228. {
  1229. SET_SESSION_PROPERTY(Passwordless);
  1230. }
  1231. //---------------------------------------------------------------------
  1232. void __fastcall TSessionData::SetPingInterval(int value)
  1233. {
  1234. SET_SESSION_PROPERTY(PingInterval);
  1235. }
  1236. //---------------------------------------------------------------------
  1237. void __fastcall TSessionData::SetTryAgent(bool value)
  1238. {
  1239. SET_SESSION_PROPERTY(TryAgent);
  1240. }
  1241. //---------------------------------------------------------------------
  1242. void __fastcall TSessionData::SetAgentFwd(bool value)
  1243. {
  1244. SET_SESSION_PROPERTY(AgentFwd);
  1245. }
  1246. //---------------------------------------------------------------------
  1247. void __fastcall TSessionData::SetAuthTIS(bool value)
  1248. {
  1249. SET_SESSION_PROPERTY(AuthTIS);
  1250. }
  1251. //---------------------------------------------------------------------
  1252. void __fastcall TSessionData::SetAuthKI(bool value)
  1253. {
  1254. SET_SESSION_PROPERTY(AuthKI);
  1255. }
  1256. //---------------------------------------------------------------------
  1257. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  1258. {
  1259. SET_SESSION_PROPERTY(AuthKIPassword);
  1260. }
  1261. //---------------------------------------------------------------------
  1262. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  1263. {
  1264. SET_SESSION_PROPERTY(AuthGSSAPI);
  1265. }
  1266. //---------------------------------------------------------------------
  1267. void __fastcall TSessionData::SetGSSAPIFwdTGT(bool value)
  1268. {
  1269. SET_SESSION_PROPERTY(GSSAPIFwdTGT);
  1270. }
  1271. //---------------------------------------------------------------------
  1272. void __fastcall TSessionData::SetGSSAPIServerRealm(AnsiString value)
  1273. {
  1274. SET_SESSION_PROPERTY(GSSAPIServerRealm);
  1275. }
  1276. //---------------------------------------------------------------------
  1277. void __fastcall TSessionData::SetChangeUsername(bool value)
  1278. {
  1279. SET_SESSION_PROPERTY(ChangeUsername);
  1280. }
  1281. //---------------------------------------------------------------------
  1282. void __fastcall TSessionData::SetCompression(bool value)
  1283. {
  1284. SET_SESSION_PROPERTY(Compression);
  1285. }
  1286. //---------------------------------------------------------------------
  1287. void __fastcall TSessionData::SetSshProt(TSshProt value)
  1288. {
  1289. SET_SESSION_PROPERTY(SshProt);
  1290. }
  1291. //---------------------------------------------------------------------
  1292. void __fastcall TSessionData::SetSsh2DES(bool value)
  1293. {
  1294. SET_SESSION_PROPERTY(Ssh2DES);
  1295. }
  1296. //---------------------------------------------------------------------
  1297. void __fastcall TSessionData::SetSshNoUserAuth(bool value)
  1298. {
  1299. SET_SESSION_PROPERTY(SshNoUserAuth);
  1300. }
  1301. //---------------------------------------------------------------------
  1302. AnsiString __fastcall TSessionData::GetSshProtStr()
  1303. {
  1304. return SshProtList[FSshProt];
  1305. }
  1306. //---------------------------------------------------------------------
  1307. bool __fastcall TSessionData::GetUsesSsh()
  1308. {
  1309. return (FSProtocol != fsFTP);
  1310. }
  1311. //---------------------------------------------------------------------
  1312. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  1313. {
  1314. assert(Index >= 0 && Index < CIPHER_COUNT);
  1315. SET_SESSION_PROPERTY(Ciphers[Index]);
  1316. }
  1317. //---------------------------------------------------------------------
  1318. TCipher __fastcall TSessionData::GetCipher(int Index) const
  1319. {
  1320. assert(Index >= 0 && Index < CIPHER_COUNT);
  1321. return FCiphers[Index];
  1322. }
  1323. //---------------------------------------------------------------------
  1324. void __fastcall TSessionData::SetCipherList(AnsiString value)
  1325. {
  1326. bool Used[CIPHER_COUNT];
  1327. for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
  1328. AnsiString CipherStr;
  1329. int Index = 0;
  1330. while (!value.IsEmpty() && (Index < CIPHER_COUNT))
  1331. {
  1332. CipherStr = CutToChar(value, ',', true);
  1333. for (int C = 0; C < CIPHER_COUNT; C++)
  1334. {
  1335. if (!CipherStr.AnsiCompareIC(CipherNames[C]))
  1336. {
  1337. Cipher[Index] = (TCipher)C;
  1338. Used[C] = true;
  1339. Index++;
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
  1345. {
  1346. if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
  1347. }
  1348. }
  1349. //---------------------------------------------------------------------
  1350. AnsiString __fastcall TSessionData::GetCipherList() const
  1351. {
  1352. AnsiString Result;
  1353. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  1354. {
  1355. Result += AnsiString(Index ? "," : "") + CipherNames[Cipher[Index]];
  1356. }
  1357. return Result;
  1358. }
  1359. //---------------------------------------------------------------------
  1360. void __fastcall TSessionData::SetKex(int Index, TKex value)
  1361. {
  1362. assert(Index >= 0 && Index < KEX_COUNT);
  1363. SET_SESSION_PROPERTY(Kex[Index]);
  1364. }
  1365. //---------------------------------------------------------------------
  1366. TKex __fastcall TSessionData::GetKex(int Index) const
  1367. {
  1368. assert(Index >= 0 && Index < KEX_COUNT);
  1369. return FKex[Index];
  1370. }
  1371. //---------------------------------------------------------------------
  1372. void __fastcall TSessionData::SetKexList(AnsiString value)
  1373. {
  1374. bool Used[KEX_COUNT];
  1375. for (int K = 0; K < KEX_COUNT; K++) Used[K] = false;
  1376. AnsiString KexStr;
  1377. int Index = 0;
  1378. while (!value.IsEmpty() && (Index < KEX_COUNT))
  1379. {
  1380. KexStr = CutToChar(value, ',', true);
  1381. for (int K = 0; K < KEX_COUNT; K++)
  1382. {
  1383. if (!KexStr.AnsiCompareIC(KexNames[K]))
  1384. {
  1385. Kex[Index] = (TKex)K;
  1386. Used[K] = true;
  1387. Index++;
  1388. break;
  1389. }
  1390. }
  1391. }
  1392. for (int K = 0; K < KEX_COUNT && Index < KEX_COUNT; K++)
  1393. {
  1394. if (!Used[DefaultKexList[K]]) Kex[Index++] = DefaultKexList[K];
  1395. }
  1396. }
  1397. //---------------------------------------------------------------------
  1398. AnsiString __fastcall TSessionData::GetKexList() const
  1399. {
  1400. AnsiString Result;
  1401. for (int Index = 0; Index < KEX_COUNT; Index++)
  1402. {
  1403. Result += AnsiString(Index ? "," : "") + KexNames[Kex[Index]];
  1404. }
  1405. return Result;
  1406. }
  1407. //---------------------------------------------------------------------
  1408. void __fastcall TSessionData::SetPublicKeyFile(AnsiString value)
  1409. {
  1410. if (FPublicKeyFile != value)
  1411. {
  1412. FPublicKeyFile = StripPathQuotes(value);
  1413. Modify();
  1414. }
  1415. }
  1416. //---------------------------------------------------------------------
  1417. void __fastcall TSessionData::SetReturnVar(AnsiString value)
  1418. {
  1419. SET_SESSION_PROPERTY(ReturnVar);
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. void __fastcall TSessionData::SetLookupUserGroups(bool value)
  1423. {
  1424. SET_SESSION_PROPERTY(LookupUserGroups);
  1425. }
  1426. //---------------------------------------------------------------------------
  1427. void __fastcall TSessionData::SetEOLType(TEOLType value)
  1428. {
  1429. SET_SESSION_PROPERTY(EOLType);
  1430. }
  1431. //---------------------------------------------------------------------------
  1432. TDateTime __fastcall TSessionData::GetTimeoutDT()
  1433. {
  1434. return SecToDateTime(Timeout);
  1435. }
  1436. //---------------------------------------------------------------------------
  1437. void __fastcall TSessionData::SetTimeout(int value)
  1438. {
  1439. SET_SESSION_PROPERTY(Timeout);
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. void __fastcall TSessionData::SetProtocol(TProtocol value)
  1443. {
  1444. SET_SESSION_PROPERTY(Protocol);
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  1448. {
  1449. SET_SESSION_PROPERTY(FSProtocol);
  1450. }
  1451. //---------------------------------------------------------------------
  1452. AnsiString __fastcall TSessionData::GetFSProtocolStr()
  1453. {
  1454. assert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  1455. return FSProtocolNames[FSProtocol];
  1456. }
  1457. //---------------------------------------------------------------------------
  1458. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  1459. {
  1460. if (value != DetectReturnVar)
  1461. {
  1462. ReturnVar = value ? "" : "$?";
  1463. }
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. bool __fastcall TSessionData::GetDetectReturnVar()
  1467. {
  1468. return ReturnVar.IsEmpty();
  1469. }
  1470. //---------------------------------------------------------------------------
  1471. void __fastcall TSessionData::SetDefaultShell(bool value)
  1472. {
  1473. if (value != DefaultShell)
  1474. {
  1475. Shell = value ? "" : "/bin/bash";
  1476. }
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. bool __fastcall TSessionData::GetDefaultShell()
  1480. {
  1481. return Shell.IsEmpty();
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. void __fastcall TSessionData::SetProtocolStr(AnsiString value)
  1485. {
  1486. FProtocol = ptRaw;
  1487. for (int Index = 0; Index < PROTOCOL_COUNT; Index++)
  1488. {
  1489. if (value.AnsiCompareIC(ProtocolNames[Index]) == 0)
  1490. {
  1491. FProtocol = TProtocol(Index);
  1492. break;
  1493. }
  1494. }
  1495. }
  1496. //---------------------------------------------------------------------
  1497. AnsiString __fastcall TSessionData::GetProtocolStr() const
  1498. {
  1499. return ProtocolNames[Protocol];
  1500. }
  1501. //---------------------------------------------------------------------
  1502. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  1503. {
  1504. unsigned short hour, min, sec, msec;
  1505. value.DecodeTime(&hour, &min, &sec, &msec);
  1506. PingInterval = ((int)hour)*60*60 + ((int)min)*60 + sec;
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  1510. {
  1511. return SecToDateTime(PingInterval);
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. void __fastcall TSessionData::SetPingType(TPingType value)
  1515. {
  1516. SET_SESSION_PROPERTY(PingType);
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  1520. {
  1521. SET_SESSION_PROPERTY(AddressFamily);
  1522. }
  1523. //---------------------------------------------------------------------------
  1524. void __fastcall TSessionData::SetRekeyData(AnsiString value)
  1525. {
  1526. SET_SESSION_PROPERTY(RekeyData);
  1527. }
  1528. //---------------------------------------------------------------------------
  1529. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  1530. {
  1531. SET_SESSION_PROPERTY(RekeyTime);
  1532. }
  1533. //---------------------------------------------------------------------
  1534. AnsiString __fastcall TSessionData::GetDefaultSessionName()
  1535. {
  1536. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  1537. {
  1538. return FORMAT("%s@%s", (UserName, HostName));
  1539. }
  1540. else if (!HostName.IsEmpty())
  1541. {
  1542. return HostName;
  1543. }
  1544. else
  1545. {
  1546. return "session";
  1547. }
  1548. }
  1549. //---------------------------------------------------------------------
  1550. bool __fastcall TSessionData::HasSessionName()
  1551. {
  1552. return (!Name.IsEmpty() && (Name != DefaultName));
  1553. }
  1554. //---------------------------------------------------------------------
  1555. AnsiString __fastcall TSessionData::GetSessionName()
  1556. {
  1557. AnsiString Result;
  1558. if (HasSessionName())
  1559. {
  1560. Result = Name;
  1561. if (Hidden)
  1562. {
  1563. Result = Result.SubString(TNamedObjectList::HiddenPrefix.Length() + 1, Result.Length() - TNamedObjectList::HiddenPrefix.Length());
  1564. }
  1565. }
  1566. else
  1567. {
  1568. Result = DefaultSessionName;
  1569. }
  1570. return Result;
  1571. }
  1572. //---------------------------------------------------------------------
  1573. AnsiString __fastcall TSessionData::GetSessionUrl()
  1574. {
  1575. AnsiString Url;
  1576. if (HasSessionName())
  1577. {
  1578. Url = Name;
  1579. }
  1580. else
  1581. {
  1582. switch (FSProtocol)
  1583. {
  1584. case fsSCPonly:
  1585. Url = "scp://";
  1586. break;
  1587. default:
  1588. assert(false);
  1589. // fallback
  1590. case fsSFTP:
  1591. case fsSFTPonly:
  1592. Url = "sftp://";
  1593. break;
  1594. case fsFTP:
  1595. Url = "ftp://";
  1596. break;
  1597. }
  1598. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  1599. {
  1600. Url += FORMAT("%s@%s", (UserName, HostName));
  1601. }
  1602. else if (!HostName.IsEmpty())
  1603. {
  1604. Url += HostName;
  1605. }
  1606. else
  1607. {
  1608. Url = "";
  1609. }
  1610. }
  1611. return Url;
  1612. }
  1613. //---------------------------------------------------------------------
  1614. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  1615. {
  1616. SET_SESSION_PROPERTY(TimeDifference);
  1617. }
  1618. //---------------------------------------------------------------------
  1619. void __fastcall TSessionData::SetLocalDirectory(AnsiString value)
  1620. {
  1621. SET_SESSION_PROPERTY(LocalDirectory);
  1622. }
  1623. //---------------------------------------------------------------------
  1624. void __fastcall TSessionData::SetRemoteDirectory(AnsiString value)
  1625. {
  1626. SET_SESSION_PROPERTY(RemoteDirectory);
  1627. }
  1628. //---------------------------------------------------------------------
  1629. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  1630. {
  1631. SET_SESSION_PROPERTY(UpdateDirectories);
  1632. }
  1633. //---------------------------------------------------------------------
  1634. void __fastcall TSessionData::SetCacheDirectories(bool value)
  1635. {
  1636. SET_SESSION_PROPERTY(CacheDirectories);
  1637. }
  1638. //---------------------------------------------------------------------
  1639. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  1640. {
  1641. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  1642. }
  1643. //---------------------------------------------------------------------
  1644. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  1645. {
  1646. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  1647. }
  1648. //---------------------------------------------------------------------
  1649. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  1650. {
  1651. SET_SESSION_PROPERTY(ResolveSymlinks);
  1652. }
  1653. //---------------------------------------------------------------------------
  1654. void __fastcall TSessionData::SetDSTMode(TDSTMode value)
  1655. {
  1656. SET_SESSION_PROPERTY(DSTMode);
  1657. }
  1658. //---------------------------------------------------------------------------
  1659. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  1660. {
  1661. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  1662. }
  1663. //---------------------------------------------------------------------------
  1664. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  1665. {
  1666. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  1667. }
  1668. //---------------------------------------------------------------------------
  1669. void __fastcall TSessionData::SetRecycleBinPath(AnsiString value)
  1670. {
  1671. SET_SESSION_PROPERTY(RecycleBinPath);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. void __fastcall TSessionData::SetPostLoginCommands(AnsiString value)
  1675. {
  1676. SET_SESSION_PROPERTY(PostLoginCommands);
  1677. }
  1678. //---------------------------------------------------------------------
  1679. void __fastcall TSessionData::SetLockInHome(bool value)
  1680. {
  1681. SET_SESSION_PROPERTY(LockInHome);
  1682. }
  1683. //---------------------------------------------------------------------
  1684. void __fastcall TSessionData::SetSpecial(bool value)
  1685. {
  1686. SET_SESSION_PROPERTY(Special);
  1687. }
  1688. //---------------------------------------------------------------------------
  1689. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  1690. {
  1691. SET_SESSION_PROPERTY(Scp1Compatibility);
  1692. }
  1693. //---------------------------------------------------------------------
  1694. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  1695. {
  1696. SET_SESSION_PROPERTY(TcpNoDelay);
  1697. }
  1698. //---------------------------------------------------------------------
  1699. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  1700. {
  1701. SET_SESSION_PROPERTY(ProxyMethod);
  1702. }
  1703. //---------------------------------------------------------------------
  1704. void __fastcall TSessionData::SetProxyHost(AnsiString value)
  1705. {
  1706. SET_SESSION_PROPERTY(ProxyHost);
  1707. }
  1708. //---------------------------------------------------------------------
  1709. void __fastcall TSessionData::SetProxyPort(int value)
  1710. {
  1711. SET_SESSION_PROPERTY(ProxyPort);
  1712. }
  1713. //---------------------------------------------------------------------
  1714. void __fastcall TSessionData::SetProxyUsername(AnsiString value)
  1715. {
  1716. SET_SESSION_PROPERTY(ProxyUsername);
  1717. }
  1718. //---------------------------------------------------------------------
  1719. void __fastcall TSessionData::SetProxyPassword(AnsiString value)
  1720. {
  1721. value = EncryptPassword(value, ProxyUsername+ProxyHost);
  1722. SET_SESSION_PROPERTY(ProxyPassword);
  1723. }
  1724. //---------------------------------------------------------------------
  1725. AnsiString __fastcall TSessionData::GetProxyPassword() const
  1726. {
  1727. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  1728. }
  1729. //---------------------------------------------------------------------
  1730. void __fastcall TSessionData::SetProxyTelnetCommand(AnsiString value)
  1731. {
  1732. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  1733. }
  1734. //---------------------------------------------------------------------
  1735. void __fastcall TSessionData::SetProxyLocalCommand(AnsiString value)
  1736. {
  1737. SET_SESSION_PROPERTY(ProxyLocalCommand);
  1738. }
  1739. //---------------------------------------------------------------------
  1740. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  1741. {
  1742. SET_SESSION_PROPERTY(ProxyDNS);
  1743. }
  1744. //---------------------------------------------------------------------
  1745. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  1746. {
  1747. SET_SESSION_PROPERTY(ProxyLocalhost);
  1748. }
  1749. //---------------------------------------------------------------------
  1750. void __fastcall TSessionData::SetFtpProxyLogonType(int value)
  1751. {
  1752. SET_SESSION_PROPERTY(FtpProxyLogonType);
  1753. }
  1754. //---------------------------------------------------------------------
  1755. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  1756. {
  1757. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1758. SET_SESSION_PROPERTY(Bugs[Bug]);
  1759. }
  1760. //---------------------------------------------------------------------
  1761. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  1762. {
  1763. assert(Bug >= 0 && Bug < LENOF(FBugs));
  1764. return FBugs[Bug];
  1765. }
  1766. //---------------------------------------------------------------------
  1767. void __fastcall TSessionData::SetCustomParam1(AnsiString value)
  1768. {
  1769. SET_SESSION_PROPERTY(CustomParam1);
  1770. }
  1771. //---------------------------------------------------------------------
  1772. void __fastcall TSessionData::SetCustomParam2(AnsiString value)
  1773. {
  1774. SET_SESSION_PROPERTY(CustomParam2);
  1775. }
  1776. //---------------------------------------------------------------------
  1777. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  1778. {
  1779. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  1780. }
  1781. //---------------------------------------------------------------------
  1782. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  1783. {
  1784. SET_SESSION_PROPERTY(SFTPUploadQueue);
  1785. }
  1786. //---------------------------------------------------------------------
  1787. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  1788. {
  1789. SET_SESSION_PROPERTY(SFTPListingQueue);
  1790. }
  1791. //---------------------------------------------------------------------
  1792. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  1793. {
  1794. SET_SESSION_PROPERTY(SFTPMaxVersion);
  1795. }
  1796. //---------------------------------------------------------------------
  1797. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  1798. {
  1799. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  1800. }
  1801. //---------------------------------------------------------------------
  1802. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  1803. {
  1804. assert(Bug >= 0 && Bug < LENOF(FSFTPBugs));
  1805. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  1806. }
  1807. //---------------------------------------------------------------------
  1808. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  1809. {
  1810. assert(Bug >= 0 && Bug < LENOF(FSFTPBugs));
  1811. return FSFTPBugs[Bug];
  1812. }
  1813. //---------------------------------------------------------------------
  1814. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  1815. {
  1816. SET_SESSION_PROPERTY(SCPLsFullTime);
  1817. }
  1818. //---------------------------------------------------------------------------
  1819. void __fastcall TSessionData::SetColor(int value)
  1820. {
  1821. SET_SESSION_PROPERTY(Color);
  1822. }
  1823. //---------------------------------------------------------------------------
  1824. void __fastcall TSessionData::SetTunnel(bool value)
  1825. {
  1826. SET_SESSION_PROPERTY(Tunnel);
  1827. }
  1828. //---------------------------------------------------------------------
  1829. void __fastcall TSessionData::SetTunnelHostName(AnsiString value)
  1830. {
  1831. if (FTunnelHostName != value)
  1832. {
  1833. // HostName is key for password encryption
  1834. AnsiString XTunnelPassword = TunnelPassword;
  1835. int P = value.LastDelimiter("@");
  1836. if (P > 0)
  1837. {
  1838. TunnelUserName = value.SubString(1, P - 1);
  1839. value = value.SubString(P + 1, value.Length() - P);
  1840. }
  1841. FTunnelHostName = value;
  1842. Modify();
  1843. TunnelPassword = XTunnelPassword;
  1844. if (!XTunnelPassword.IsEmpty())
  1845. {
  1846. XTunnelPassword.Unique();
  1847. memset(XTunnelPassword.c_str(), 0, XTunnelPassword.Length());
  1848. }
  1849. }
  1850. }
  1851. //---------------------------------------------------------------------
  1852. void __fastcall TSessionData::SetTunnelPortNumber(int value)
  1853. {
  1854. SET_SESSION_PROPERTY(TunnelPortNumber);
  1855. }
  1856. //---------------------------------------------------------------------
  1857. void __fastcall TSessionData::SetTunnelUserName(AnsiString value)
  1858. {
  1859. // TunnelUserName is key for password encryption
  1860. AnsiString XTunnelPassword = TunnelPassword;
  1861. SET_SESSION_PROPERTY(TunnelUserName);
  1862. TunnelPassword = XTunnelPassword;
  1863. if (!XTunnelPassword.IsEmpty())
  1864. {
  1865. XTunnelPassword.Unique();
  1866. memset(XTunnelPassword.c_str(), 0, XTunnelPassword.Length());
  1867. }
  1868. }
  1869. //---------------------------------------------------------------------
  1870. void __fastcall TSessionData::SetTunnelPassword(AnsiString value)
  1871. {
  1872. value = EncryptPassword(value, TunnelUserName+TunnelHostName);
  1873. SET_SESSION_PROPERTY(TunnelPassword);
  1874. }
  1875. //---------------------------------------------------------------------
  1876. AnsiString __fastcall TSessionData::GetTunnelPassword()
  1877. {
  1878. return DecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName);
  1879. }
  1880. //---------------------------------------------------------------------
  1881. void __fastcall TSessionData::SetTunnelPublicKeyFile(AnsiString value)
  1882. {
  1883. if (FTunnelPublicKeyFile != value)
  1884. {
  1885. FTunnelPublicKeyFile = StripPathQuotes(value);
  1886. Modify();
  1887. }
  1888. }
  1889. //---------------------------------------------------------------------
  1890. void __fastcall TSessionData::SetTunnelLocalPortNumber(int value)
  1891. {
  1892. SET_SESSION_PROPERTY(TunnelLocalPortNumber);
  1893. }
  1894. //---------------------------------------------------------------------
  1895. bool __fastcall TSessionData::GetTunnelAutoassignLocalPortNumber()
  1896. {
  1897. return (FTunnelLocalPortNumber <= 0);
  1898. }
  1899. //---------------------------------------------------------------------
  1900. void __fastcall TSessionData::SetTunnelPortFwd(AnsiString value)
  1901. {
  1902. SET_SESSION_PROPERTY(TunnelPortFwd);
  1903. }
  1904. //---------------------------------------------------------------------
  1905. void __fastcall TSessionData::SetFtpPasvMode(bool value)
  1906. {
  1907. SET_SESSION_PROPERTY(FtpPasvMode);
  1908. }
  1909. //---------------------------------------------------------------------
  1910. void __fastcall TSessionData::SetFtpForcePasvIp(TAutoSwitch value)
  1911. {
  1912. SET_SESSION_PROPERTY(FtpForcePasvIp);
  1913. }
  1914. //---------------------------------------------------------------------
  1915. void __fastcall TSessionData::SetFtpAccount(AnsiString value)
  1916. {
  1917. SET_SESSION_PROPERTY(FtpAccount);
  1918. }
  1919. //---------------------------------------------------------------------
  1920. void __fastcall TSessionData::SetFtpPingInterval(int value)
  1921. {
  1922. SET_SESSION_PROPERTY(FtpPingInterval);
  1923. }
  1924. //---------------------------------------------------------------------------
  1925. TDateTime __fastcall TSessionData::GetFtpPingIntervalDT()
  1926. {
  1927. return SecToDateTime(FtpPingInterval);
  1928. }
  1929. //---------------------------------------------------------------------------
  1930. void __fastcall TSessionData::SetFtpPingType(TPingType value)
  1931. {
  1932. SET_SESSION_PROPERTY(FtpPingType);
  1933. }
  1934. //---------------------------------------------------------------------------
  1935. void __fastcall TSessionData::SetFtps(TFtps value)
  1936. {
  1937. SET_SESSION_PROPERTY(Ftps);
  1938. }
  1939. //---------------------------------------------------------------------
  1940. void __fastcall TSessionData::SetFtpListAll(TAutoSwitch value)
  1941. {
  1942. SET_SESSION_PROPERTY(FtpListAll);
  1943. }
  1944. //---------------------------------------------------------------------
  1945. void __fastcall TSessionData::SetSslSessionReuse(bool value)
  1946. {
  1947. SET_SESSION_PROPERTY(SslSessionReuse);
  1948. }
  1949. //---------------------------------------------------------------------
  1950. void __fastcall TSessionData::SetNotUtf(TAutoSwitch value)
  1951. {
  1952. SET_SESSION_PROPERTY(NotUtf);
  1953. }
  1954. //---------------------------------------------------------------------
  1955. void __fastcall TSessionData::SetHostKey(AnsiString value)
  1956. {
  1957. SET_SESSION_PROPERTY(HostKey);
  1958. }
  1959. //---------------------------------------------------------------------
  1960. AnsiString __fastcall TSessionData::GetInfoTip()
  1961. {
  1962. if (UsesSsh)
  1963. {
  1964. return FMTLOAD(SESSION_INFO_TIP,
  1965. (HostName, UserName,
  1966. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  1967. SshProtStr, FSProtocolStr));
  1968. }
  1969. else
  1970. {
  1971. return FMTLOAD(SESSION_INFO_TIP_NO_SSH,
  1972. (HostName, UserName, FSProtocolStr));
  1973. }
  1974. }
  1975. //---------------------------------------------------------------------
  1976. AnsiString __fastcall TSessionData::GetLocalName()
  1977. {
  1978. AnsiString Result;
  1979. if (HasSessionName())
  1980. {
  1981. Result = Name;
  1982. int P = Result.LastDelimiter(L"/");
  1983. if (P > 0)
  1984. {
  1985. Result.Delete(1, P);
  1986. }
  1987. }
  1988. else
  1989. {
  1990. Result = DefaultSessionName;
  1991. }
  1992. return Result;
  1993. }
  1994. //=== TStoredSessionList ----------------------------------------------
  1995. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  1996. TNamedObjectList(), FReadOnly(aReadOnly)
  1997. {
  1998. assert(Configuration);
  1999. FDefaultSettings = new TSessionData(DefaultName);
  2000. }
  2001. //---------------------------------------------------------------------
  2002. __fastcall TStoredSessionList::~TStoredSessionList()
  2003. {
  2004. assert(Configuration);
  2005. delete FDefaultSettings;
  2006. }
  2007. //---------------------------------------------------------------------
  2008. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  2009. bool AsModified, bool UseDefaults)
  2010. {
  2011. TStringList *SubKeys = new TStringList();
  2012. TList * Loaded = new TList;
  2013. try
  2014. {
  2015. Storage->GetSubKeyNames(SubKeys);
  2016. for (int Index = 0; Index < SubKeys->Count; Index++)
  2017. {
  2018. TSessionData *SessionData;
  2019. AnsiString SessionName = SubKeys->Strings[Index];
  2020. bool ValidName = true;
  2021. try
  2022. {
  2023. TSessionData::ValidatePath(SessionName);
  2024. }
  2025. catch(...)
  2026. {
  2027. ValidName = false;
  2028. }
  2029. if (ValidName)
  2030. {
  2031. if (SessionName == FDefaultSettings->Name) SessionData = FDefaultSettings;
  2032. else SessionData = (TSessionData*)FindByName(SessionName);
  2033. if ((SessionData != FDefaultSettings) || !UseDefaults)
  2034. {
  2035. if (!SessionData)
  2036. {
  2037. SessionData = new TSessionData("");
  2038. if (UseDefaults)
  2039. {
  2040. SessionData->Assign(DefaultSettings);
  2041. }
  2042. SessionData->Name = SessionName;
  2043. Add(SessionData);
  2044. }
  2045. Loaded->Add(SessionData);
  2046. SessionData->Load(Storage);
  2047. if (AsModified)
  2048. {
  2049. SessionData->Modified = true;
  2050. }
  2051. }
  2052. }
  2053. }
  2054. if (!AsModified)
  2055. {
  2056. for (int Index = 0; Index < TObjectList::Count; Index++)
  2057. {
  2058. if (Loaded->IndexOf(Items[Index]) < 0)
  2059. {
  2060. Delete(Index);
  2061. Index--;
  2062. }
  2063. }
  2064. }
  2065. }
  2066. __finally
  2067. {
  2068. delete SubKeys;
  2069. delete Loaded;
  2070. }
  2071. }
  2072. //---------------------------------------------------------------------
  2073. void __fastcall TStoredSessionList::Load(AnsiString aKey, bool UseDefaults)
  2074. {
  2075. TRegistryStorage * Storage = new TRegistryStorage(aKey);
  2076. try {
  2077. if (Storage->OpenRootKey(False)) Load(Storage, false, UseDefaults);
  2078. } __finally {
  2079. delete Storage;
  2080. }
  2081. }
  2082. //---------------------------------------------------------------------
  2083. void __fastcall TStoredSessionList::Load()
  2084. {
  2085. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  2086. try {
  2087. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  2088. Load(Storage);
  2089. } __finally {
  2090. delete Storage;
  2091. }
  2092. }
  2093. //---------------------------------------------------------------------
  2094. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  2095. TSessionData * Data, bool All, bool RecryptPasswordOnly,
  2096. TSessionData * FactoryDefaults)
  2097. {
  2098. if (All || Data->Modified)
  2099. {
  2100. if (RecryptPasswordOnly)
  2101. {
  2102. Data->SaveRecryptedPasswords(Storage);
  2103. }
  2104. else
  2105. {
  2106. Data->Save(Storage, false, FactoryDefaults);
  2107. }
  2108. }
  2109. }
  2110. //---------------------------------------------------------------------
  2111. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  2112. bool All, bool RecryptPasswordOnly)
  2113. {
  2114. TSessionData * FactoryDefaults = new TSessionData("");
  2115. try
  2116. {
  2117. DoSave(Storage, FDefaultSettings, All, RecryptPasswordOnly, FactoryDefaults);
  2118. for (int Index = 0; Index < Count+HiddenCount; Index++)
  2119. {
  2120. TSessionData * SessionData = (TSessionData *)Items[Index];
  2121. DoSave(Storage, SessionData, All, RecryptPasswordOnly, FactoryDefaults);
  2122. }
  2123. }
  2124. __finally
  2125. {
  2126. delete FactoryDefaults;
  2127. }
  2128. }
  2129. //---------------------------------------------------------------------
  2130. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage, bool All)
  2131. {
  2132. DoSave(Storage, All, false);
  2133. }
  2134. //---------------------------------------------------------------------
  2135. void __fastcall TStoredSessionList::DoSave(bool All, bool Explicit, bool RecryptPasswordOnly)
  2136. {
  2137. THierarchicalStorage * Storage = Configuration->CreateScpStorage(true);
  2138. try
  2139. {
  2140. Storage->AccessMode = smReadWrite;
  2141. Storage->Explicit = Explicit;
  2142. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  2143. {
  2144. DoSave(Storage, All, RecryptPasswordOnly);
  2145. }
  2146. }
  2147. __finally
  2148. {
  2149. delete Storage;
  2150. }
  2151. Saved();
  2152. }
  2153. //---------------------------------------------------------------------
  2154. void __fastcall TStoredSessionList::Save(bool All, bool Explicit)
  2155. {
  2156. DoSave(All, Explicit, false);
  2157. }
  2158. //---------------------------------------------------------------------
  2159. void __fastcall TStoredSessionList::RecryptPasswords()
  2160. {
  2161. DoSave(true, true, true);
  2162. }
  2163. //---------------------------------------------------------------------
  2164. void __fastcall TStoredSessionList::Saved()
  2165. {
  2166. FDefaultSettings->Modified = false;
  2167. for (int Index = 0; Index < Count + HiddenCount; Index++)
  2168. {
  2169. ((TSessionData *)Items[Index])->Modified = false;
  2170. }
  2171. }
  2172. //---------------------------------------------------------------------
  2173. void __fastcall TStoredSessionList::Export(const AnsiString FileName)
  2174. {
  2175. THierarchicalStorage * Storage = new TIniFileStorage(FileName);
  2176. try
  2177. {
  2178. Storage->AccessMode = smReadWrite;
  2179. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  2180. {
  2181. Save(Storage, true);
  2182. }
  2183. }
  2184. __finally
  2185. {
  2186. delete Storage;
  2187. }
  2188. }
  2189. //---------------------------------------------------------------------
  2190. void __fastcall TStoredSessionList::SelectAll(bool Select)
  2191. {
  2192. for (int Index = 0; Index < Count; Index++)
  2193. Sessions[Index]->Selected = Select;
  2194. }
  2195. //---------------------------------------------------------------------
  2196. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  2197. bool OnlySelected)
  2198. {
  2199. for (int Index = 0; Index < From->Count; Index++)
  2200. {
  2201. if (!OnlySelected || From->Sessions[Index]->Selected)
  2202. {
  2203. TSessionData *Session = new TSessionData("");
  2204. Session->Assign(From->Sessions[Index]);
  2205. Session->Modified = true;
  2206. Session->MakeUniqueIn(this);
  2207. Add(Session);
  2208. }
  2209. }
  2210. // only modified, explicit
  2211. Save(false, true);
  2212. }
  2213. //---------------------------------------------------------------------
  2214. void __fastcall TStoredSessionList::SelectSessionsToImport
  2215. (TStoredSessionList * Dest, bool SSHOnly)
  2216. {
  2217. for (int Index = 0; Index < Count; Index++)
  2218. {
  2219. Sessions[Index]->Selected =
  2220. (!SSHOnly || (Sessions[Index]->Protocol == ptSSH)) &&
  2221. !Dest->FindByName(Sessions[Index]->Name);
  2222. }
  2223. }
  2224. //---------------------------------------------------------------------
  2225. void __fastcall TStoredSessionList::Cleanup()
  2226. {
  2227. try {
  2228. if (Configuration->Storage == stRegistry) Clear();
  2229. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  2230. try {
  2231. Storage->AccessMode = smReadWrite;
  2232. if (Storage->OpenRootKey(False))
  2233. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  2234. } __finally {
  2235. delete Storage;
  2236. }
  2237. } catch (Exception &E) {
  2238. throw ExtException(&E, CLEANUP_SESSIONS_ERROR);
  2239. }
  2240. }
  2241. //---------------------------------------------------------------------------
  2242. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  2243. {
  2244. for (int Index = 0; Index < Count; Index++)
  2245. if (Data == Sessions[Index]) return Index;
  2246. return -1;
  2247. }
  2248. //---------------------------------------------------------------------------
  2249. TSessionData * __fastcall TStoredSessionList::NewSession(
  2250. AnsiString SessionName, TSessionData * Session)
  2251. {
  2252. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  2253. if (!DuplicateSession)
  2254. {
  2255. DuplicateSession = new TSessionData("");
  2256. DuplicateSession->Assign(Session);
  2257. DuplicateSession->Name = SessionName;
  2258. // make sure, that new stored session is saved to registry
  2259. DuplicateSession->Modified = true;
  2260. Add(DuplicateSession);
  2261. }
  2262. else
  2263. {
  2264. DuplicateSession->Assign(Session);
  2265. DuplicateSession->Name = SessionName;
  2266. DuplicateSession->Modified = true;
  2267. }
  2268. // list was saved here before to default storage, but it would not allow
  2269. // to work with special lists (export/import) not using default storage
  2270. return DuplicateSession;
  2271. }
  2272. //---------------------------------------------------------------------------
  2273. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  2274. {
  2275. assert(FDefaultSettings);
  2276. if (FDefaultSettings != value)
  2277. {
  2278. FDefaultSettings->Assign(value);
  2279. // make sure default settings are saved
  2280. FDefaultSettings->Modified = true;
  2281. FDefaultSettings->Name = DefaultName;
  2282. if (!FReadOnly)
  2283. {
  2284. // only modified, explicit
  2285. Save(false, true);
  2286. }
  2287. }
  2288. }
  2289. //---------------------------------------------------------------------------
  2290. void __fastcall TStoredSessionList::ImportHostKeys(const AnsiString TargetKey,
  2291. const AnsiString SourceKey, TStoredSessionList * Sessions,
  2292. bool OnlySelected)
  2293. {
  2294. TRegistryStorage * SourceStorage = NULL;
  2295. TRegistryStorage * TargetStorage = NULL;
  2296. TStringList * KeyList = NULL;
  2297. try
  2298. {
  2299. SourceStorage = new TRegistryStorage(SourceKey);
  2300. TargetStorage = new TRegistryStorage(TargetKey);
  2301. TargetStorage->AccessMode = smReadWrite;
  2302. KeyList = new TStringList();
  2303. if (SourceStorage->OpenRootKey(false) &&
  2304. TargetStorage->OpenRootKey(true))
  2305. {
  2306. SourceStorage->GetValueNames(KeyList);
  2307. TSessionData * Session;
  2308. AnsiString HostKeyName;
  2309. assert(Sessions != NULL);
  2310. for (int Index = 0; Index < Sessions->Count; Index++)
  2311. {
  2312. Session = Sessions->Sessions[Index];
  2313. if (!OnlySelected || Session->Selected)
  2314. {
  2315. HostKeyName = PuttyMungeStr(FORMAT("@%d:%s", (Session->PortNumber, Session->HostName)));
  2316. AnsiString KeyName;
  2317. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  2318. {
  2319. KeyName = KeyList->Strings[KeyIndex];
  2320. int P = KeyName.Pos(HostKeyName);
  2321. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  2322. {
  2323. TargetStorage->WriteStringRaw(KeyName,
  2324. SourceStorage->ReadStringRaw(KeyName, ""));
  2325. }
  2326. }
  2327. }
  2328. }
  2329. }
  2330. }
  2331. __finally
  2332. {
  2333. delete SourceStorage;
  2334. delete TargetStorage;
  2335. delete KeyList;
  2336. }
  2337. }
  2338. //---------------------------------------------------------------------------
  2339. TSessionData * __fastcall TStoredSessionList::ParseUrl(AnsiString Url,
  2340. TOptions * Options, bool & DefaultsOnly, AnsiString * FileName,
  2341. bool * AProtocolDefined)
  2342. {
  2343. TSessionData * Data = new TSessionData("");
  2344. try
  2345. {
  2346. Data->ParseUrl(Url, Options, this, DefaultsOnly, FileName, AProtocolDefined);
  2347. }
  2348. catch(...)
  2349. {
  2350. delete Data;
  2351. throw;
  2352. }
  2353. return Data;
  2354. }