SessionData.cpp 76 KB

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