SecureShell.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "PuttyIntf.h"
  5. #include "Exceptions.h"
  6. #include "Interface.h"
  7. #include "SecureShell.h"
  8. #include "TextsCore.h"
  9. #include "HelpCore.h"
  10. #include "Common.h"
  11. #include "CoreMain.h"
  12. #ifndef AUTO_WINSOCK
  13. #include <winsock2.h>
  14. #endif
  15. //---------------------------------------------------------------------------
  16. #pragma package(smart_init)
  17. //---------------------------------------------------------------------------
  18. #define MAX_BUFSIZE 32768
  19. //---------------------------------------------------------------------------
  20. struct TPuttyTranslation
  21. {
  22. const char * Original;
  23. int Translation;
  24. };
  25. //---------------------------------------------------------------------------
  26. __fastcall TSecureShell::TSecureShell(TSessionUI* UI,
  27. TSessionData * SessionData, TSessionLog * Log, TConfiguration * Configuration)
  28. {
  29. FUI = UI;
  30. FSessionData = SessionData;
  31. FLog = Log;
  32. FConfiguration = Configuration;
  33. FActive = false;
  34. FWaiting = 0;
  35. FOpened = false;
  36. OutPtr = NULL;
  37. Pending = NULL;
  38. FBackendHandle = NULL;
  39. ResetConnection();
  40. FOnCaptureOutput = NULL;
  41. FOnReceive = NULL;
  42. FConfig = new Config();
  43. memset(FConfig, 0, sizeof(*FConfig));
  44. FSocket = INVALID_SOCKET;
  45. FSocketEvent = CreateEvent(NULL, false, false, NULL);
  46. FFrozen = false;
  47. FSimple = false;
  48. }
  49. //---------------------------------------------------------------------------
  50. __fastcall TSecureShell::~TSecureShell()
  51. {
  52. assert(FWaiting == 0);
  53. Active = false;
  54. ResetConnection();
  55. CloseHandle(FSocketEvent);
  56. ClearConfig(FConfig);
  57. delete FConfig;
  58. FConfig = NULL;
  59. }
  60. //---------------------------------------------------------------------------
  61. void __fastcall TSecureShell::ResetConnection()
  62. {
  63. FreeBackend();
  64. ClearStdError();
  65. PendLen = 0;
  66. PendSize = 0;
  67. sfree(Pending);
  68. Pending = NULL;
  69. FCWriteTemp = "";
  70. ResetSessionInfo();
  71. FAuthenticating = false;
  72. FAuthenticated = false;
  73. FStoredPasswordTried = false;
  74. FStoredPasswordTriedForKI = false;
  75. }
  76. //---------------------------------------------------------------------------
  77. void __fastcall TSecureShell::ResetSessionInfo()
  78. {
  79. FSessionInfoValid = false;
  80. FMaxPacketSize = NULL;
  81. }
  82. //---------------------------------------------------------------------------
  83. inline void __fastcall TSecureShell::UpdateSessionInfo()
  84. {
  85. if (!FSessionInfoValid)
  86. {
  87. FSshVersion = get_ssh_version(FBackendHandle);
  88. FSessionInfo.ProtocolBaseName = "SSH";
  89. FSessionInfo.ProtocolName =
  90. FORMAT("%s-%d", (FSessionInfo.ProtocolBaseName, get_ssh_version(FBackendHandle)));
  91. FSessionInfo.SecurityProtocolName = FSessionInfo.ProtocolName;
  92. FSessionInfo.CSCompression =
  93. FuncToCompression(FSshVersion, get_cscomp(FBackendHandle));
  94. FSessionInfo.SCCompression =
  95. FuncToCompression(FSshVersion, get_sccomp(FBackendHandle));
  96. if (FSshVersion == 1)
  97. {
  98. FSessionInfo.CSCipher = CipherNames[FuncToSsh1Cipher(get_cipher(FBackendHandle))];
  99. FSessionInfo.SCCipher = CipherNames[FuncToSsh1Cipher(get_cipher(FBackendHandle))];
  100. }
  101. else
  102. {
  103. FSessionInfo.CSCipher = CipherNames[FuncToSsh2Cipher(get_cscipher(FBackendHandle))];
  104. FSessionInfo.SCCipher = CipherNames[FuncToSsh2Cipher(get_sccipher(FBackendHandle))];
  105. }
  106. FSessionInfoValid = true;
  107. }
  108. }
  109. //---------------------------------------------------------------------------
  110. const TSessionInfo & __fastcall TSecureShell::GetSessionInfo()
  111. {
  112. if (!FSessionInfoValid)
  113. {
  114. UpdateSessionInfo();
  115. }
  116. return FSessionInfo;
  117. }
  118. //---------------------------------------------------------------------
  119. void __fastcall TSecureShell::ClearConfig(Config * cfg)
  120. {
  121. StrDispose(cfg->remote_cmd_ptr);
  122. StrDispose(cfg->remote_cmd_ptr2);
  123. // clear all
  124. memset(cfg, 0, sizeof(*cfg));
  125. }
  126. //---------------------------------------------------------------------
  127. void __fastcall TSecureShell::StoreToConfig(TSessionData * Data, Config * cfg, bool Simple)
  128. {
  129. ClearConfig(cfg);
  130. // user-configurable settings
  131. ASCOPY(cfg->host, Data->HostName);
  132. ASCOPY(cfg->username, Data->UserName);
  133. cfg->port = Data->PortNumber;
  134. cfg->protocol = PROT_SSH;
  135. // always set 0, as we will handle keepalives ourselves to avoid
  136. // multi-threaded issues in putty timer list
  137. cfg->ping_interval = 0;
  138. cfg->compression = Data->Compression;
  139. cfg->tryagent = Data->TryAgent;
  140. cfg->agentfwd = Data->AgentFwd;
  141. cfg->addressfamily = Data->AddressFamily;
  142. ASCOPY(cfg->ssh_rekey_data, Data->RekeyData);
  143. cfg->ssh_rekey_time = Data->RekeyTime;
  144. for (int c = 0; c < CIPHER_COUNT; c++)
  145. {
  146. int pcipher;
  147. switch (Data->Cipher[c]) {
  148. case cipWarn: pcipher = CIPHER_WARN; break;
  149. case cip3DES: pcipher = CIPHER_3DES; break;
  150. case cipBlowfish: pcipher = CIPHER_BLOWFISH; break;
  151. case cipAES: pcipher = CIPHER_AES; break;
  152. case cipDES: pcipher = CIPHER_DES; break;
  153. case cipArcfour: pcipher = CIPHER_ARCFOUR; break;
  154. default: assert(false);
  155. }
  156. cfg->ssh_cipherlist[c] = pcipher;
  157. }
  158. for (int k = 0; k < KEX_COUNT; k++)
  159. {
  160. int pkex;
  161. switch (Data->Kex[k]) {
  162. case kexWarn: pkex = KEX_WARN; break;
  163. case kexDHGroup1: pkex = KEX_DHGROUP1; break;
  164. case kexDHGroup14: pkex = KEX_DHGROUP14; break;
  165. case kexDHGEx: pkex = KEX_DHGEX; break;
  166. case kexRSA: pkex = KEX_RSA; break;
  167. default: assert(false);
  168. }
  169. cfg->ssh_kexlist[k] = pkex;
  170. }
  171. AnsiString SPublicKeyFile = Data->PublicKeyFile;
  172. if (SPublicKeyFile.IsEmpty()) SPublicKeyFile = Configuration->DefaultKeyFile;
  173. SPublicKeyFile = StripPathQuotes(ExpandEnvironmentVariables(SPublicKeyFile));
  174. ASCOPY(cfg->keyfile.path, SPublicKeyFile);
  175. cfg->sshprot = Data->SshProt;
  176. cfg->ssh2_des_cbc = Data->Ssh2DES;
  177. cfg->ssh_no_userauth = Data->SshNoUserAuth;
  178. cfg->try_tis_auth = Data->AuthTIS;
  179. cfg->try_ki_auth = Data->AuthKI;
  180. cfg->try_gssapi_auth = Data->AuthGSSAPI;
  181. cfg->gssapifwd = Data->GSSAPIFwdTGT;
  182. cfg->change_username = Data->ChangeUsername;
  183. cfg->proxy_type = Data->ProxyMethod;
  184. ASCOPY(cfg->proxy_host, Data->ProxyHost);
  185. cfg->proxy_port = Data->ProxyPort;
  186. ASCOPY(cfg->proxy_username, Data->ProxyUsername);
  187. ASCOPY(cfg->proxy_password, Data->ProxyPassword);
  188. if (Data->ProxyMethod == pmCmd)
  189. {
  190. ASCOPY(cfg->proxy_telnet_command, Data->ProxyLocalCommand);
  191. }
  192. else
  193. {
  194. ASCOPY(cfg->proxy_telnet_command, Data->ProxyTelnetCommand);
  195. }
  196. cfg->proxy_dns = Data->ProxyDNS;
  197. cfg->even_proxy_localhost = Data->ProxyLocalhost;
  198. #pragma option push -w-eas
  199. // after 0.53b values were reversed, however putty still stores
  200. // settings to registry in save way as before
  201. cfg->sshbug_ignore1 = Data->Bug[sbIgnore1];
  202. cfg->sshbug_plainpw1 = Data->Bug[sbPlainPW1];
  203. cfg->sshbug_rsa1 = Data->Bug[sbRSA1];
  204. cfg->sshbug_hmac2 = Data->Bug[sbHMAC2];
  205. cfg->sshbug_derivekey2 = Data->Bug[sbDeriveKey2];
  206. cfg->sshbug_rsapad2 = Data->Bug[sbRSAPad2];
  207. cfg->sshbug_rekey2 = Data->Bug[sbRekey2];
  208. // new after 0.53b
  209. cfg->sshbug_pksessid2 = Data->Bug[sbPKSessID2];
  210. cfg->sshbug_maxpkt2 = Data->Bug[sbMaxPkt2];
  211. cfg->sshbug_ignore2 = asAuto;
  212. #pragma option pop
  213. if (!Data->TunnelPortFwd.IsEmpty())
  214. {
  215. assert(!Simple);
  216. ASCOPY(cfg->portfwd, Data->TunnelPortFwd);
  217. // when setting up a tunnel, do not open shell/sftp
  218. cfg->ssh_no_shell = TRUE;
  219. }
  220. else
  221. {
  222. assert(Simple);
  223. cfg->ssh_simple = Simple;
  224. if (Data->FSProtocol == fsSCPonly)
  225. {
  226. cfg->ssh_subsys = FALSE;
  227. if (Data->Shell.IsEmpty())
  228. {
  229. // Following forces Putty to open default shell
  230. // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
  231. cfg->remote_cmd[0] = '\0';
  232. }
  233. else
  234. {
  235. cfg->remote_cmd_ptr = StrNew(Data->Shell.c_str());
  236. }
  237. }
  238. else
  239. {
  240. if (Data->SftpServer.IsEmpty())
  241. {
  242. cfg->ssh_subsys = TRUE;
  243. strcpy(cfg->remote_cmd, "sftp");
  244. }
  245. else
  246. {
  247. cfg->ssh_subsys = FALSE;
  248. cfg->remote_cmd_ptr = StrNew(Data->SftpServer.c_str());
  249. }
  250. if (Data->FSProtocol != fsSFTPonly)
  251. {
  252. cfg->ssh_subsys2 = FALSE;
  253. if (Data->Shell.IsEmpty())
  254. {
  255. // Following forces Putty to open default shell
  256. // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
  257. cfg->remote_cmd_ptr2 = StrNew("\0");
  258. }
  259. else
  260. {
  261. cfg->remote_cmd_ptr2 = StrNew(Data->Shell.c_str());
  262. }
  263. }
  264. if ((Data->FSProtocol == fsSFTPonly) && Data->SftpServer.IsEmpty())
  265. {
  266. // see psftp_connect() from psftp.c
  267. cfg->ssh_subsys2 = FALSE;
  268. cfg->remote_cmd_ptr2 = StrNew(
  269. "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n"
  270. "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n"
  271. "exec sftp-server");
  272. }
  273. }
  274. }
  275. cfg->connect_timeout = Data->Timeout * 1000;
  276. // permanent settings
  277. cfg->nopty = TRUE;
  278. cfg->tcp_keepalives = 0;
  279. cfg->ssh_show_banner = TRUE;
  280. for (int Index = 0; Index < ngsslibs; Index++)
  281. {
  282. cfg->ssh_gsslist[Index] = gsslibkeywords[Index].v;
  283. }
  284. }
  285. //---------------------------------------------------------------------------
  286. void __fastcall TSecureShell::Open()
  287. {
  288. FBackend = &ssh_backend;
  289. ResetConnection();
  290. FAuthenticating = false;
  291. FAuthenticated = false;
  292. Active = false;
  293. FAuthenticationLog = "";
  294. FUI->Information(LoadStr(STATUS_LOOKUPHOST), true);
  295. StoreToConfig(FSessionData, FConfig, Simple);
  296. char * RealHost;
  297. FreeBackend(); // in case we are reconnecting
  298. const char * InitError = FBackend->init(this, &FBackendHandle, FConfig,
  299. FSessionData->HostName.c_str(), FSessionData->PortNumber, &RealHost, 0,
  300. FConfig->tcp_keepalives);
  301. sfree(RealHost);
  302. if (InitError)
  303. {
  304. PuttyFatalError(InitError);
  305. }
  306. FUI->Information(LoadStr(STATUS_CONNECT), true);
  307. Init();
  308. CheckConnection(CONNECTION_FAILED);
  309. FLastDataSent = Now();
  310. FSessionInfo.LoginTime = Now();
  311. FAuthenticating = false;
  312. FAuthenticated = true;
  313. FUI->Information(LoadStr(STATUS_AUTHENTICATED), true);
  314. ResetSessionInfo();
  315. assert(!FSessionInfo.SshImplementation.IsEmpty());
  316. FOpened = true;
  317. }
  318. //---------------------------------------------------------------------------
  319. void __fastcall TSecureShell::Init()
  320. {
  321. try
  322. {
  323. try
  324. {
  325. // Recent pscp checks FBackend->exitcode(FBackendHandle) in the loop
  326. // (see comment in putty revision 8110)
  327. // It seems that we do not need to do it.
  328. while (!get_ssh_state_session(FBackendHandle))
  329. {
  330. if (Configuration->ActualLogProtocol >= 1)
  331. {
  332. LogEvent("Waiting for the server to continue with the initialisation");
  333. }
  334. WaitForData();
  335. }
  336. // unless this is tunnel session, it must be safe to send now
  337. assert(FBackend->sendok(FBackendHandle) || !FSessionData->TunnelPortFwd.IsEmpty());
  338. }
  339. catch(Exception & E)
  340. {
  341. if (FAuthenticating && !FAuthenticationLog.IsEmpty())
  342. {
  343. FUI->FatalError(&E, FMTLOAD(AUTHENTICATION_LOG, (FAuthenticationLog)));
  344. }
  345. else
  346. {
  347. throw;
  348. }
  349. }
  350. }
  351. catch(Exception & E)
  352. {
  353. if (FAuthenticating)
  354. {
  355. FUI->FatalError(&E, LoadStr(AUTHENTICATION_FAILED));
  356. }
  357. else
  358. {
  359. throw;
  360. }
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TSecureShell::PuttyLogEvent(const AnsiString & Str)
  365. {
  366. #define SERVER_VERSION_MSG "Server version: "
  367. // Gross hack
  368. if (Str.Pos(SERVER_VERSION_MSG) == 1)
  369. {
  370. FSessionInfo.SshVersionString = Str.SubString(strlen(SERVER_VERSION_MSG) + 1,
  371. Str.Length() - strlen(SERVER_VERSION_MSG));
  372. const char * Ptr = strchr(FSessionInfo.SshVersionString.c_str(), '-');
  373. if (Ptr != NULL)
  374. {
  375. Ptr = strchr(Ptr + 1, '-');
  376. }
  377. FSessionInfo.SshImplementation = (Ptr != NULL) ? Ptr + 1 : "";
  378. }
  379. #define FORWARDING_FAILURE_MSG "Forwarded connection refused by server: "
  380. else if (Str.Pos(FORWARDING_FAILURE_MSG) == 1)
  381. {
  382. FLastTunnelError = Str.SubString(strlen(FORWARDING_FAILURE_MSG) + 1,
  383. Str.Length() - strlen(FORWARDING_FAILURE_MSG));
  384. static const TPuttyTranslation Translation[] = {
  385. { "Administratively prohibited [%]", PFWD_TRANSL_ADMIN },
  386. { "Connect failed [%]", PFWD_TRANSL_CONNECT },
  387. };
  388. TranslatePuttyMessage(Translation, LENOF(Translation), FLastTunnelError);
  389. }
  390. LogEvent(Str);
  391. }
  392. //---------------------------------------------------------------------------
  393. bool __fastcall TSecureShell::PromptUser(bool /*ToServer*/,
  394. AnsiString AName, bool /*NameRequired*/,
  395. AnsiString Instructions, bool InstructionsRequired,
  396. TStrings * Prompts, TStrings * Results)
  397. {
  398. // there can be zero prompts!
  399. assert(Results->Count == Prompts->Count);
  400. TPromptKind PromptKind;
  401. // beware of changing order
  402. static const TPuttyTranslation NameTranslation[] = {
  403. { "SSH login name", USERNAME_TITLE },
  404. { "SSH key passphrase", PASSPHRASE_TITLE },
  405. { "SSH TIS authentication", SERVER_PROMPT_TITLE },
  406. { "SSH CryptoCard authentication", SERVER_PROMPT_TITLE },
  407. { "SSH server: %", SERVER_PROMPT_TITLE2 },
  408. { "SSH server authentication", SERVER_PROMPT_TITLE },
  409. { "SSH password", PASSWORD_TITLE },
  410. { "New SSH password", NEW_PASSWORD_TITLE },
  411. };
  412. AnsiString Name = AName;
  413. int Index = TranslatePuttyMessage(NameTranslation, LENOF(NameTranslation), Name);
  414. const TPuttyTranslation * InstructionTranslation = NULL;
  415. const TPuttyTranslation * PromptTranslation = NULL;
  416. size_t PromptTranslationCount = 1;
  417. if (Index == 0) // username
  418. {
  419. static const TPuttyTranslation UsernamePromptTranslation[] = {
  420. { "login as: ", USERNAME_PROMPT2 },
  421. };
  422. PromptTranslation = UsernamePromptTranslation;
  423. PromptKind = pkUserName;
  424. }
  425. else if (Index == 1) // passhrase
  426. {
  427. static const TPuttyTranslation PassphrasePromptTranslation[] = {
  428. { "Passphrase for key \"%\": ", PROMPT_KEY_PASSPHRASE },
  429. };
  430. PromptTranslation = PassphrasePromptTranslation;
  431. PromptKind = pkPassphrase;
  432. }
  433. else if (Index == 2) // TIS
  434. {
  435. static const TPuttyTranslation TISInstructionTranslation[] = {
  436. { "Using TIS authentication.%", TIS_INSTRUCTION },
  437. };
  438. static const TPuttyTranslation TISPromptTranslation[] = {
  439. { "Response: ", PROMPT_PROMPT },
  440. };
  441. InstructionTranslation = TISInstructionTranslation;
  442. PromptTranslation = TISPromptTranslation;
  443. PromptKind = pkTIS;
  444. }
  445. else if (Index == 3) // CryptoCard
  446. {
  447. static const TPuttyTranslation CryptoCardInstructionTranslation[] = {
  448. { "Using CryptoCard authentication.%", CRYPTOCARD_INSTRUCTION },
  449. };
  450. static const TPuttyTranslation CryptoCardPromptTranslation[] = {
  451. { "Response: ", PROMPT_PROMPT },
  452. };
  453. InstructionTranslation = CryptoCardInstructionTranslation;
  454. PromptTranslation = CryptoCardPromptTranslation;
  455. PromptKind = pkCryptoCard;
  456. }
  457. else if ((Index == 4) || (Index == 5))
  458. {
  459. static const TPuttyTranslation KeybInteractiveInstructionTranslation[] = {
  460. { "Using keyboard-interactive authentication.%", KEYBINTER_INSTRUCTION },
  461. };
  462. InstructionTranslation = KeybInteractiveInstructionTranslation;
  463. PromptKind = pkKeybInteractive;
  464. }
  465. else if (Index == 6)
  466. {
  467. assert(Prompts->Count == 1);
  468. Prompts->Strings[0] = LoadStr(PASSWORD_PROMPT);
  469. PromptKind = pkPassword;
  470. }
  471. else if (Index == 7)
  472. {
  473. static const TPuttyTranslation NewPasswordPromptTranslation[] = {
  474. { "Current password (blank for previously entered password): ", NEW_PASSWORD_CURRENT_PROMPT },
  475. { "Enter new password: ", NEW_PASSWORD_NEW_PROMPT },
  476. { "Confirm new password: ", NEW_PASSWORD_CONFIRM_PROMPT },
  477. };
  478. PromptTranslation = NewPasswordPromptTranslation;
  479. PromptTranslationCount = LENOF(NewPasswordPromptTranslation);
  480. PromptKind = pkNewPassword;
  481. }
  482. else
  483. {
  484. PromptKind = pkPrompt;
  485. assert(false);
  486. }
  487. LogEvent(FORMAT("Prompt (%d, %s, %s, %s)", (PromptKind, AName, Instructions, (Prompts->Count > 0 ? Prompts->Strings[0] : AnsiString("<no prompt>")))));
  488. Name = Name.Trim();
  489. if (InstructionTranslation != NULL)
  490. {
  491. TranslatePuttyMessage(InstructionTranslation, 1, Instructions);
  492. }
  493. // some servers add leading blank line to make the prompt look prettier
  494. // on terminal console
  495. Instructions = Instructions.Trim();
  496. for (int Index = 0; Index < Prompts->Count; Index++)
  497. {
  498. AnsiString Prompt = Prompts->Strings[Index];
  499. if (PromptTranslation != NULL)
  500. {
  501. TranslatePuttyMessage(PromptTranslation, PromptTranslationCount, Prompt);
  502. }
  503. // some servers add leading blank line to make the prompt look prettier
  504. // on terminal console
  505. Prompts->Strings[Index] = Prompt.Trim();
  506. }
  507. bool Result = false;
  508. if (PromptKind == pkUserName)
  509. {
  510. if (FSessionData->AuthGSSAPI)
  511. {
  512. // use empty username if no username was filled on login dialog
  513. // and GSSAPI auth is enabled, hence there's chance that the server can
  514. // deduce the username otherwise
  515. Results->Strings[0] = "";
  516. Result = true;
  517. }
  518. }
  519. else if ((PromptKind == pkTIS) || (PromptKind == pkCryptoCard) ||
  520. (PromptKind == pkKeybInteractive))
  521. {
  522. if (FSessionData->AuthKIPassword && !FSessionData->Password.IsEmpty() &&
  523. !FStoredPasswordTriedForKI && (Prompts->Count == 1) &&
  524. !bool(Prompts->Objects[0]))
  525. {
  526. LogEvent("Using stored password.");
  527. FUI->Information(LoadStr(AUTH_PASSWORD), false);
  528. Result = true;
  529. Results->Strings[0] = FSessionData->Password;
  530. FStoredPasswordTriedForKI = true;
  531. }
  532. else if (Instructions.IsEmpty() && !InstructionsRequired && (Prompts->Count == 0))
  533. {
  534. LogEvent("Ignoring empty SSH server authentication request");
  535. Result = true;
  536. }
  537. }
  538. else if (PromptKind == pkPassword)
  539. {
  540. if (!FSessionData->Password.IsEmpty() && !FStoredPasswordTried)
  541. {
  542. LogEvent("Using stored password.");
  543. FUI->Information(LoadStr(AUTH_PASSWORD), false);
  544. Result = true;
  545. Results->Strings[0] = FSessionData->Password;
  546. FStoredPasswordTried = true;
  547. }
  548. }
  549. if (!Result)
  550. {
  551. Result = FUI->PromptUser(FSessionData,
  552. PromptKind, Name, Instructions, Prompts, Results);
  553. if (Result)
  554. {
  555. if ((PromptKind == pkUserName) && (Prompts->Count == 1))
  556. {
  557. FUserName = Results->Strings[0];
  558. }
  559. }
  560. }
  561. return Result;
  562. }
  563. //---------------------------------------------------------------------------
  564. void __fastcall TSecureShell::GotHostKey()
  565. {
  566. // due to re-key GotHostKey() may be called again later during session
  567. if (!FAuthenticating && !FAuthenticated)
  568. {
  569. FAuthenticating = true;
  570. FUI->Information(LoadStr(STATUS_AUTHENTICATE), true);
  571. }
  572. }
  573. //---------------------------------------------------------------------------
  574. void __fastcall TSecureShell::CWrite(const char * Data, int Length)
  575. {
  576. // some messages to stderr may indicate that something has changed with the
  577. // session, so reset the session info
  578. ResetSessionInfo();
  579. // We send only whole line at once, so we have to cache incoming data
  580. FCWriteTemp += DeleteChar(AnsiString(Data, Length), '\r');
  581. AnsiString Line;
  582. // Do we have at least one complete line in std error cache?
  583. while (FCWriteTemp.Pos("\n") > 0)
  584. {
  585. AnsiString Line = CutToChar(FCWriteTemp, '\n', false);
  586. FLog->Add(llStdError, Line);
  587. if (FAuthenticating)
  588. {
  589. TranslateAuthenticationMessage(Line);
  590. FAuthenticationLog += (FAuthenticationLog.IsEmpty() ? "" : "\n") + Line;
  591. }
  592. FUI->Information(Line, false);
  593. }
  594. }
  595. //---------------------------------------------------------------------------
  596. void __fastcall TSecureShell::RegisterReceiveHandler(TNotifyEvent Handler)
  597. {
  598. assert(FOnReceive == NULL);
  599. FOnReceive = Handler;
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TSecureShell::UnregisterReceiveHandler(TNotifyEvent Handler)
  603. {
  604. assert(FOnReceive == Handler);
  605. USEDPARAM(Handler);
  606. FOnReceive = NULL;
  607. }
  608. //---------------------------------------------------------------------------
  609. void __fastcall TSecureShell::FromBackend(bool IsStdErr, const char * Data, int Length)
  610. {
  611. CheckConnection();
  612. if (Configuration->ActualLogProtocol >= 1)
  613. {
  614. LogEvent(FORMAT("Received %u bytes (%d)", (Length, int(IsStdErr))));
  615. }
  616. // Following is taken from scp.c from_backend() and modified
  617. if (IsStdErr)
  618. {
  619. AddStdError(AnsiString(Data, Length));
  620. }
  621. else
  622. {
  623. unsigned char *p = (unsigned char *)Data;
  624. unsigned Len = (unsigned)Length;
  625. // with event-select mechanism we can now receive data even before we
  626. // actually expect them (OutPtr can be NULL)
  627. if ((OutPtr != NULL) && (OutLen > 0) && (Len > 0))
  628. {
  629. unsigned Used = OutLen;
  630. if (Used > Len) Used = Len;
  631. memcpy(OutPtr, p, Used);
  632. OutPtr += Used; OutLen -= Used;
  633. p += Used; Len -= Used;
  634. }
  635. if (Len > 0)
  636. {
  637. if (PendSize < PendLen + Len)
  638. {
  639. PendSize = PendLen + Len + 4096;
  640. Pending = (char *)
  641. (Pending ? srealloc(Pending, PendSize) : smalloc(PendSize));
  642. if (!Pending) FatalError("Out of memory");
  643. }
  644. memcpy(Pending + PendLen, p, Len);
  645. PendLen += Len;
  646. }
  647. if (FOnReceive != NULL)
  648. {
  649. if (!FFrozen)
  650. {
  651. FFrozen = true;
  652. try
  653. {
  654. do
  655. {
  656. FDataWhileFrozen = false;
  657. FOnReceive(NULL);
  658. }
  659. while (FDataWhileFrozen);
  660. }
  661. __finally
  662. {
  663. FFrozen = false;
  664. }
  665. }
  666. else
  667. {
  668. FDataWhileFrozen = true;
  669. }
  670. }
  671. }
  672. }
  673. //---------------------------------------------------------------------------
  674. bool __fastcall TSecureShell::Peek(char *& Buf, int Len)
  675. {
  676. bool Result = (int(PendLen) >= Len);
  677. if (Result)
  678. {
  679. Buf = Pending;
  680. }
  681. return Result;
  682. }
  683. //---------------------------------------------------------------------------
  684. Integer __fastcall TSecureShell::Receive(char * Buf, Integer Len)
  685. {
  686. CheckConnection();
  687. if (Len > 0)
  688. {
  689. // Following is taken from scp.c ssh_scp_recv() and modified
  690. OutPtr = Buf;
  691. OutLen = Len;
  692. try
  693. {
  694. /*
  695. * See if the pending-input block contains some of what we
  696. * need.
  697. */
  698. if (PendLen > 0)
  699. {
  700. unsigned PendUsed = PendLen;
  701. if (PendUsed > OutLen)
  702. {
  703. PendUsed = OutLen;
  704. }
  705. memcpy(OutPtr, Pending, PendUsed);
  706. memmove(Pending, Pending + PendUsed, PendLen - PendUsed);
  707. OutPtr += PendUsed;
  708. OutLen -= PendUsed;
  709. PendLen -= PendUsed;
  710. if (PendLen == 0)
  711. {
  712. PendSize = 0;
  713. sfree(Pending);
  714. Pending = NULL;
  715. }
  716. }
  717. while (OutLen > 0)
  718. {
  719. if (Configuration->ActualLogProtocol >= 1)
  720. {
  721. LogEvent(FORMAT("Waiting for another %u bytes", (static_cast<int>(OutLen))));
  722. }
  723. WaitForData();
  724. }
  725. // This seems ambiguous
  726. if (Len <= 0) FatalError(LoadStr(LOST_CONNECTION));
  727. }
  728. __finally
  729. {
  730. OutPtr = NULL;
  731. }
  732. };
  733. if (Configuration->ActualLogProtocol >= 1)
  734. {
  735. LogEvent(FORMAT("Read %u bytes (%d pending)",
  736. (static_cast<int>(Len), static_cast<int>(PendLen))));
  737. }
  738. return Len;
  739. }
  740. //---------------------------------------------------------------------------
  741. AnsiString __fastcall TSecureShell::ReceiveLine()
  742. {
  743. unsigned Index;
  744. Char Ch;
  745. AnsiString Line;
  746. Boolean EOL = False;
  747. do
  748. {
  749. // If there is any buffer of received chars
  750. if (PendLen > 0)
  751. {
  752. Index = 0;
  753. // Repeat until we walk thru whole buffer or reach end-of-line
  754. while ((Index < PendLen) && (!Index || (Pending[Index-1] != '\n')))
  755. {
  756. Index++;
  757. }
  758. EOL = (Boolean)(Index && (Pending[Index-1] == '\n'));
  759. Integer PrevLen = Line.Length();
  760. Line.SetLength(PrevLen + Index);
  761. Receive(Line.c_str() + PrevLen, Index);
  762. }
  763. // If buffer don't contain end-of-line character
  764. // we read one more which causes receiving new buffer of chars
  765. if (!EOL)
  766. {
  767. Receive(&Ch, 1);
  768. Line += Ch;
  769. EOL = (Ch == '\n');
  770. }
  771. }
  772. while (!EOL);
  773. // We don't want end-of-line character
  774. Line.SetLength(Line.Length()-1);
  775. CaptureOutput(llOutput, Line);
  776. return Line;
  777. }
  778. //---------------------------------------------------------------------------
  779. void __fastcall TSecureShell::SendSpecial(int Code)
  780. {
  781. LogEvent(FORMAT("Sending special code: %d", (Code)));
  782. CheckConnection();
  783. FBackend->special(FBackendHandle, (Telnet_Special)Code);
  784. CheckConnection();
  785. FLastDataSent = Now();
  786. }
  787. //---------------------------------------------------------------------------
  788. void __fastcall TSecureShell::SendEOF()
  789. {
  790. SendSpecial(TS_EOF);
  791. }
  792. //---------------------------------------------------------------------------
  793. int __fastcall TSecureShell::TimeoutPrompt(TQueryParamsTimerEvent PoolEvent)
  794. {
  795. FWaiting++;
  796. int Answer;
  797. try
  798. {
  799. TQueryParams Params(qpFatalAbort | qpAllowContinueOnError | qpIgnoreAbort);
  800. Params.Timer = 500;
  801. Params.TimerEvent = PoolEvent;
  802. Params.TimerMessage = FMTLOAD(TIMEOUT_STILL_WAITING2, (FSessionData->Timeout));
  803. Params.TimerAnswers = qaAbort;
  804. Answer = FUI->QueryUser(FMTLOAD(CONFIRM_PROLONG_TIMEOUT3, (FSessionData->Timeout)),
  805. NULL, qaRetry | qaAbort, &Params);
  806. }
  807. __finally
  808. {
  809. FWaiting--;
  810. }
  811. return Answer;
  812. }
  813. //---------------------------------------------------------------------------
  814. void __fastcall TSecureShell::SendBuffer(unsigned int & Result)
  815. {
  816. // for comments see PoolForData
  817. if (!Active)
  818. {
  819. Result = qaRetry;
  820. }
  821. else
  822. {
  823. try
  824. {
  825. if (FBackend->sendbuffer(FBackendHandle) <= MAX_BUFSIZE)
  826. {
  827. Result = qaOK;
  828. }
  829. }
  830. catch(...)
  831. {
  832. Result = qaRetry;
  833. }
  834. }
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TSecureShell::DispatchSendBuffer(int BufSize)
  838. {
  839. TDateTime Start = Now();
  840. do
  841. {
  842. CheckConnection();
  843. if (Configuration->ActualLogProtocol >= 1)
  844. {
  845. LogEvent(FORMAT("There are %u bytes remaining in the send buffer, "
  846. "need to send at least another %u bytes",
  847. (BufSize, BufSize - MAX_BUFSIZE)));
  848. }
  849. EventSelectLoop(100, false, NULL);
  850. BufSize = FBackend->sendbuffer(FBackendHandle);
  851. if (Configuration->ActualLogProtocol >= 1)
  852. {
  853. LogEvent(FORMAT("There are %u bytes remaining in the send buffer", (BufSize)));
  854. }
  855. if (Now() - Start > FSessionData->TimeoutDT)
  856. {
  857. LogEvent("Waiting for dispatching send buffer timed out, asking user what to do.");
  858. int Answer = TimeoutPrompt(SendBuffer);
  859. switch (Answer)
  860. {
  861. case qaRetry:
  862. Start = Now();
  863. break;
  864. case qaOK:
  865. BufSize = 0;
  866. break;
  867. default:
  868. assert(false);
  869. // fallthru
  870. case qaAbort:
  871. FatalError(LoadStr(USER_TERMINATED));
  872. break;
  873. }
  874. }
  875. }
  876. while (BufSize > MAX_BUFSIZE);
  877. }
  878. //---------------------------------------------------------------------------
  879. void __fastcall TSecureShell::Send(const char * Buf, Integer Len)
  880. {
  881. CheckConnection();
  882. int BufSize = FBackend->send(FBackendHandle, (char *)Buf, Len);
  883. if (Configuration->ActualLogProtocol >= 1)
  884. {
  885. LogEvent(FORMAT("Sent %u bytes", (static_cast<int>(Len))));
  886. LogEvent(FORMAT("There are %u bytes remaining in the send buffer", (BufSize)));
  887. }
  888. FLastDataSent = Now();
  889. // among other forces receive of pending data to free the servers's send buffer
  890. EventSelectLoop(0, false, NULL);
  891. if (BufSize > MAX_BUFSIZE)
  892. {
  893. DispatchSendBuffer(BufSize);
  894. }
  895. CheckConnection();
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TSecureShell::SendNull()
  899. {
  900. LogEvent("Sending NULL.");
  901. Send("", 1);
  902. }
  903. //---------------------------------------------------------------------------
  904. void __fastcall TSecureShell::SendStr(AnsiString Str)
  905. {
  906. CheckConnection();
  907. Send(Str.c_str(), Str.Length());
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TSecureShell::SendLine(AnsiString Line)
  911. {
  912. SendStr(Line);
  913. Send("\n", 1);
  914. FLog->Add(llInput, Line);
  915. }
  916. //---------------------------------------------------------------------------
  917. int __fastcall TSecureShell::TranslatePuttyMessage(
  918. const TPuttyTranslation * Translation, size_t Count, AnsiString & Message)
  919. {
  920. int Result = -1;
  921. for (unsigned int Index = 0; Index < Count; Index++)
  922. {
  923. const char * Original = Translation[Index].Original;
  924. const char * Div = strchr(Original, '%');
  925. if (Div == NULL)
  926. {
  927. if (strcmp(Message.c_str(), Original) == 0)
  928. {
  929. Message = LoadStr(Translation[Index].Translation);
  930. Result = int(Index);
  931. break;
  932. }
  933. }
  934. else
  935. {
  936. size_t OriginalLen = strlen(Original);
  937. size_t PrefixLen = Div - Original;
  938. size_t SuffixLen = OriginalLen - PrefixLen - 1;
  939. if (((size_t)Message.Length() >= OriginalLen - 1) &&
  940. (strncmp(Message.c_str(), Original, PrefixLen) == 0) &&
  941. (strncmp(Message.c_str() + Message.Length() - SuffixLen, Div + 1, SuffixLen) == 0))
  942. {
  943. Message = FMTLOAD(Translation[Index].Translation,
  944. (Message.SubString(PrefixLen + 1, Message.Length() - PrefixLen - SuffixLen).TrimRight()));
  945. Result = int(Index);
  946. break;
  947. }
  948. }
  949. }
  950. return Result;
  951. }
  952. //---------------------------------------------------------------------------
  953. int __fastcall TSecureShell::TranslateAuthenticationMessage(AnsiString & Message)
  954. {
  955. static const TPuttyTranslation Translation[] = {
  956. { "Using username \"%\".", AUTH_TRANSL_USERNAME },
  957. { "Using keyboard-interactive authentication.", AUTH_TRANSL_KEYB_INTER }, // not used anymore
  958. { "Authenticating with public key \"%\" from agent", AUTH_TRANSL_PUBLIC_KEY_AGENT },
  959. { "Authenticating with public key \"%\"", AUTH_TRANSL_PUBLIC_KEY },
  960. { "Authenticated using RSA key \"%\" from agent", AUTH_TRANSL_PUBLIC_KEY_AGENT },
  961. { "Wrong passphrase", AUTH_TRANSL_WRONG_PASSPHRASE },
  962. { "Wrong passphrase.", AUTH_TRANSL_WRONG_PASSPHRASE },
  963. { "Access denied", AUTH_TRANSL_ACCESS_DENIED },
  964. { "Trying public key authentication.", AUTH_TRANSL_TRY_PUBLIC_KEY },
  965. { "Server refused our public key.", AUTH_TRANSL_KEY_REFUSED },
  966. { "Server refused our key", AUTH_TRANSL_KEY_REFUSED }
  967. };
  968. return TranslatePuttyMessage(Translation, LENOF(Translation), Message);
  969. }
  970. //---------------------------------------------------------------------------
  971. void __fastcall TSecureShell::AddStdError(AnsiString Str)
  972. {
  973. FStdError += Str;
  974. Integer P;
  975. Str = DeleteChar(Str, '\r');
  976. // We send only whole line at once to log, so we have to cache
  977. // incoming std error data
  978. FStdErrorTemp += Str;
  979. AnsiString Line;
  980. // Do we have at least one complete line in std error cache?
  981. while ((P = FStdErrorTemp.Pos("\n")) > 0)
  982. {
  983. Line = FStdErrorTemp.SubString(1, P-1);
  984. FStdErrorTemp.Delete(1, P);
  985. AddStdErrorLine(Line);
  986. }
  987. }
  988. //---------------------------------------------------------------------------
  989. void __fastcall TSecureShell::AddStdErrorLine(const AnsiString & Str)
  990. {
  991. if (FAuthenticating)
  992. {
  993. FAuthenticationLog += (FAuthenticationLog.IsEmpty() ? "" : "\n") + Str;
  994. }
  995. CaptureOutput(llStdError, Str);
  996. }
  997. //---------------------------------------------------------------------------
  998. const AnsiString & __fastcall TSecureShell::GetStdError()
  999. {
  1000. return FStdError;
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. void __fastcall TSecureShell::ClearStdError()
  1004. {
  1005. // Flush std error cache
  1006. if (!FStdErrorTemp.IsEmpty())
  1007. {
  1008. if (FAuthenticating)
  1009. {
  1010. FAuthenticationLog +=
  1011. (FAuthenticationLog.IsEmpty() ? "" : "\n") + FStdErrorTemp;
  1012. }
  1013. CaptureOutput(llStdError, FStdErrorTemp);
  1014. FStdErrorTemp = "";
  1015. }
  1016. FStdError = "";
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. void __fastcall TSecureShell::CaptureOutput(TLogLineType Type,
  1020. const AnsiString & Line)
  1021. {
  1022. if (FOnCaptureOutput != NULL)
  1023. {
  1024. FOnCaptureOutput(Line, (Type == llStdError));
  1025. }
  1026. FLog->Add(Type, Line);
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. int __fastcall TSecureShell::TranslateErrorMessage(AnsiString & Message)
  1030. {
  1031. static const TPuttyTranslation Translation[] = {
  1032. { "Server unexpectedly closed network connection", UNEXPECTED_CLOSE_ERROR },
  1033. { "Network error: Connection refused", NET_TRANSL_REFUSED },
  1034. { "Network error: Connection reset by peer", NET_TRANSL_RESET },
  1035. { "Network error: Connection timed out", NET_TRANSL_TIMEOUT },
  1036. };
  1037. return TranslatePuttyMessage(Translation, LENOF(Translation), Message);
  1038. }
  1039. //---------------------------------------------------------------------------
  1040. void __fastcall TSecureShell::PuttyFatalError(AnsiString Error)
  1041. {
  1042. TranslateErrorMessage(Error);
  1043. FatalError(Error);
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TSecureShell::FatalError(AnsiString Error)
  1047. {
  1048. FUI->FatalError(NULL, Error);
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall inline TSecureShell::LogEvent(const AnsiString & Str)
  1052. {
  1053. if (FLog->Logging)
  1054. {
  1055. FLog->Add(llMessage, Str);
  1056. }
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. void __fastcall TSecureShell::SocketEventSelect(SOCKET Socket, HANDLE Event, bool Startup)
  1060. {
  1061. int Events;
  1062. if (Startup)
  1063. {
  1064. Events = (FD_CONNECT | FD_READ | FD_WRITE | FD_OOB | FD_CLOSE | FD_ACCEPT);
  1065. }
  1066. else
  1067. {
  1068. Events = 0;
  1069. }
  1070. if (Configuration->ActualLogProtocol >= 2)
  1071. {
  1072. LogEvent(FORMAT("Selecting events %d for socket %d", (int(Events), int(Socket))));
  1073. }
  1074. if (WSAEventSelect(Socket, (WSAEVENT)Event, Events) == SOCKET_ERROR)
  1075. {
  1076. if (Configuration->ActualLogProtocol >= 2)
  1077. {
  1078. LogEvent(FORMAT("Error selecting events %d for socket %d", (int(Events), int(Socket))));
  1079. }
  1080. if (Startup)
  1081. {
  1082. FatalError(FMTLOAD(EVENT_SELECT_ERROR, (WSAGetLastError())));
  1083. }
  1084. }
  1085. }
  1086. //---------------------------------------------------------------------------
  1087. void __fastcall TSecureShell::UpdateSocket(SOCKET value, bool Startup)
  1088. {
  1089. if (!FActive && !Startup)
  1090. {
  1091. // no-op
  1092. // Remove the branch eventualy:
  1093. // When TCP connection fails, PuTTY does not release the memory allocate for
  1094. // socket. As a simple hack we call sk_tcp_close() in ssh.c to release the memory,
  1095. // until they fix it better. Unfortunately sk_tcp_close calls do_select,
  1096. // so we must filter that out.
  1097. }
  1098. else
  1099. {
  1100. assert(value);
  1101. assert((FActive && (FSocket == value)) || (!FActive && Startup));
  1102. // filter our "local proxy" connection, which have no socket
  1103. if (value != INVALID_SOCKET)
  1104. {
  1105. SocketEventSelect(value, FSocketEvent, Startup);
  1106. }
  1107. else
  1108. {
  1109. assert(FSessionData->ProxyMethod == pmCmd);
  1110. }
  1111. if (Startup)
  1112. {
  1113. FSocket = value;
  1114. FActive = true;
  1115. }
  1116. else
  1117. {
  1118. FSocket = INVALID_SOCKET;
  1119. Discard();
  1120. }
  1121. }
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. void __fastcall TSecureShell::UpdatePortFwdSocket(SOCKET value, bool Startup)
  1125. {
  1126. if (Configuration->ActualLogProtocol >= 2)
  1127. {
  1128. LogEvent(FORMAT("Updating forwarding socket %d (%d)", (int(value), int(Startup))));
  1129. }
  1130. SocketEventSelect(value, FSocketEvent, Startup);
  1131. if (Startup)
  1132. {
  1133. FPortFwdSockets.insert(value);
  1134. }
  1135. else
  1136. {
  1137. FPortFwdSockets.erase(value);
  1138. }
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. void __fastcall TSecureShell::SetActive(bool value)
  1142. {
  1143. if (FActive != value)
  1144. {
  1145. if (value)
  1146. {
  1147. Open();
  1148. }
  1149. else
  1150. {
  1151. Close();
  1152. }
  1153. }
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. void __fastcall TSecureShell::FreeBackend()
  1157. {
  1158. if (FBackendHandle != NULL)
  1159. {
  1160. FBackend->free(FBackendHandle);
  1161. FBackendHandle = NULL;
  1162. }
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. void __fastcall TSecureShell::Discard()
  1166. {
  1167. bool WasActive = FActive;
  1168. FActive = false;
  1169. FOpened = false;
  1170. if (WasActive)
  1171. {
  1172. FUI->Closed();
  1173. }
  1174. }
  1175. //---------------------------------------------------------------------------
  1176. void __fastcall TSecureShell::Close()
  1177. {
  1178. LogEvent("Closing connection.");
  1179. assert(FActive);
  1180. // this is particularly necessary when using local proxy command
  1181. // (e.g. plink), otherwise it hangs in sk_localproxy_close
  1182. SendEOF();
  1183. FreeBackend();
  1184. Discard();
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. void inline __fastcall TSecureShell::CheckConnection(int Message)
  1188. {
  1189. if (!FActive || get_ssh_state_closed(FBackendHandle))
  1190. {
  1191. AnsiString Str = LoadStr(Message >= 0 ? Message : NOT_CONNECTED);
  1192. int ExitCode = get_ssh_exitcode(FBackendHandle);
  1193. if (ExitCode >= 0)
  1194. {
  1195. Str += " " + FMTLOAD(SSH_EXITCODE, (ExitCode));
  1196. }
  1197. FatalError(Str);
  1198. }
  1199. }
  1200. //---------------------------------------------------------------------------
  1201. void __fastcall TSecureShell::PoolForData(WSANETWORKEVENTS & Events, unsigned int & Result)
  1202. {
  1203. if (!Active)
  1204. {
  1205. // see comment below
  1206. Result = qaRetry;
  1207. }
  1208. else
  1209. {
  1210. try
  1211. {
  1212. if (Configuration->ActualLogProtocol >= 2)
  1213. {
  1214. LogEvent("Pooling for data in case they finally arrives");
  1215. }
  1216. // in extreme condition it may happen that send buffer is full, but there
  1217. // will be no data comming and we may not empty the send buffer because we
  1218. // do not process FD_WRITE until we receive any FD_READ
  1219. if (EventSelectLoop(0, false, &Events))
  1220. {
  1221. LogEvent("Data has arrived, closing query to user.");
  1222. Result = qaOK;
  1223. }
  1224. }
  1225. catch(...)
  1226. {
  1227. // if we let the exception out, it may popup another message dialog
  1228. // in whole event loop, another call to PoolForData from original dialog
  1229. // would be invoked, leading to an infinite loop.
  1230. // by retrying we hope (that probably fatal) error would repeat in WaitForData.
  1231. // anyway now once no actual work is done in EventSelectLoop,
  1232. // hardly any exception can occur actually
  1233. Result = qaRetry;
  1234. }
  1235. }
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. class TPoolForDataEvent
  1239. {
  1240. public:
  1241. __fastcall TPoolForDataEvent(TSecureShell * SecureShell, WSANETWORKEVENTS & Events) :
  1242. FSecureShell(SecureShell),
  1243. FEvents(Events)
  1244. {
  1245. }
  1246. void __fastcall PoolForData(unsigned int & Result)
  1247. {
  1248. FSecureShell->PoolForData(FEvents, Result);
  1249. }
  1250. private:
  1251. TSecureShell * FSecureShell;
  1252. WSANETWORKEVENTS & FEvents;
  1253. };
  1254. //---------------------------------------------------------------------------
  1255. void __fastcall TSecureShell::WaitForData()
  1256. {
  1257. // see winsftp.c
  1258. bool IncomingData;
  1259. do
  1260. {
  1261. if (Configuration->ActualLogProtocol >= 2)
  1262. {
  1263. LogEvent("Looking for incoming data");
  1264. }
  1265. IncomingData = EventSelectLoop(FSessionData->Timeout * 1000, true, NULL);
  1266. if (!IncomingData)
  1267. {
  1268. WSANETWORKEVENTS Events;
  1269. memset(&Events, 0, sizeof(Events));
  1270. TPoolForDataEvent Event(this, Events);
  1271. LogEvent("Waiting for data timed out, asking user what to do.");
  1272. int Answer = TimeoutPrompt(Event.PoolForData);
  1273. switch (Answer)
  1274. {
  1275. case qaRetry:
  1276. // noop
  1277. break;
  1278. case qaOK:
  1279. // read event was already captured in PoolForData(),
  1280. // make sure we do not try to select it again as it would timeout
  1281. // unless another read event occurs
  1282. IncomingData = true;
  1283. HandleNetworkEvents(FSocket, Events);
  1284. break;
  1285. default:
  1286. assert(false);
  1287. // fallthru
  1288. case qaAbort:
  1289. FatalError(LoadStr(USER_TERMINATED));
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. while (!IncomingData);
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. bool __fastcall TSecureShell::SshFallbackCmd() const
  1298. {
  1299. return ssh_fallback_cmd(FBackendHandle);
  1300. }
  1301. //---------------------------------------------------------------------------
  1302. bool __fastcall TSecureShell::EnumNetworkEvents(SOCKET Socket, WSANETWORKEVENTS & Events)
  1303. {
  1304. if (Configuration->ActualLogProtocol >= 2)
  1305. {
  1306. LogEvent(FORMAT("Enumerating network events for socket %d", (int(Socket))));
  1307. }
  1308. // see winplink.c
  1309. WSANETWORKEVENTS AEvents;
  1310. if (WSAEnumNetworkEvents(Socket, NULL, &AEvents) == 0)
  1311. {
  1312. noise_ultralight(Socket);
  1313. noise_ultralight(AEvents.lNetworkEvents);
  1314. Events.lNetworkEvents |= AEvents.lNetworkEvents;
  1315. for (int Index = 0; Index < FD_MAX_EVENTS; Index++)
  1316. {
  1317. if (AEvents.iErrorCode[Index] != 0)
  1318. {
  1319. Events.iErrorCode[Index] = AEvents.iErrorCode[Index];
  1320. }
  1321. }
  1322. if (Configuration->ActualLogProtocol >= 2)
  1323. {
  1324. LogEvent(FORMAT("Enumerated %d network events making %d cumulative events for socket %d",
  1325. (int(AEvents.lNetworkEvents), int(Events.lNetworkEvents), int(Socket))));
  1326. }
  1327. }
  1328. else
  1329. {
  1330. if (Configuration->ActualLogProtocol >= 2)
  1331. {
  1332. LogEvent(FORMAT("Error enumerating network events for socket %d", (int(Socket))));
  1333. }
  1334. }
  1335. return
  1336. FLAGSET(Events.lNetworkEvents, FD_READ) ||
  1337. FLAGSET(Events.lNetworkEvents, FD_CLOSE);
  1338. }
  1339. //---------------------------------------------------------------------------
  1340. void __fastcall TSecureShell::HandleNetworkEvents(SOCKET Socket, WSANETWORKEVENTS & Events)
  1341. {
  1342. static const struct { int Bit, Mask; const char * Desc; } EventTypes[] =
  1343. {
  1344. { FD_READ_BIT, FD_READ, "read" },
  1345. { FD_WRITE_BIT, FD_WRITE, "write" },
  1346. { FD_OOB_BIT, FD_OOB, "oob" },
  1347. { FD_ACCEPT_BIT, FD_ACCEPT, "accept" },
  1348. { FD_CONNECT_BIT, FD_CONNECT, "connect" },
  1349. { FD_CLOSE_BIT, FD_CLOSE, "close" },
  1350. };
  1351. for (int Event = 0; Event < LENOF(EventTypes); Event++)
  1352. {
  1353. if (FLAGSET(Events.lNetworkEvents, EventTypes[Event].Mask))
  1354. {
  1355. int Err = Events.iErrorCode[EventTypes[Event].Bit];
  1356. if (Configuration->ActualLogProtocol >= 2)
  1357. {
  1358. LogEvent(FORMAT("Handling network %s event on socket %d with error %d",
  1359. (EventTypes[Event].Desc, int(Socket), Err)));
  1360. }
  1361. #pragma option push -w-prc
  1362. LPARAM SelectEvent = WSAMAKESELECTREPLY(EventTypes[Event].Mask, Err);
  1363. #pragma option pop
  1364. if (!select_result((WPARAM)Socket, SelectEvent))
  1365. {
  1366. // note that connection was closed definitely,
  1367. // so "check" is actually not required
  1368. CheckConnection();
  1369. }
  1370. }
  1371. }
  1372. }
  1373. //---------------------------------------------------------------------------
  1374. bool __fastcall TSecureShell::ProcessNetworkEvents(SOCKET Socket)
  1375. {
  1376. WSANETWORKEVENTS Events;
  1377. memset(&Events, 0, sizeof(Events));
  1378. bool Result = EnumNetworkEvents(Socket, Events);
  1379. HandleNetworkEvents(Socket, Events);
  1380. return Result;
  1381. }
  1382. //---------------------------------------------------------------------------
  1383. bool __fastcall TSecureShell::EventSelectLoop(unsigned int MSec, bool ReadEventRequired,
  1384. WSANETWORKEVENTS * Events)
  1385. {
  1386. CheckConnection();
  1387. bool Result = false;
  1388. do
  1389. {
  1390. if (Configuration->ActualLogProtocol >= 2)
  1391. {
  1392. LogEvent("Looking for network events");
  1393. }
  1394. unsigned int TicksBefore = GetTickCount();
  1395. int HandleCount;
  1396. // note that this returns all handles, not only the session-related handles
  1397. HANDLE * Handles = handle_get_events(&HandleCount);
  1398. try
  1399. {
  1400. Handles = sresize(Handles, HandleCount + 1, HANDLE);
  1401. Handles[HandleCount] = FSocketEvent;
  1402. unsigned int WaitResult = WaitForMultipleObjects(HandleCount + 1, Handles, FALSE, MSec);
  1403. if (WaitResult < WAIT_OBJECT_0 + HandleCount)
  1404. {
  1405. if (handle_got_event(Handles[WaitResult - WAIT_OBJECT_0]))
  1406. {
  1407. Result = true;
  1408. }
  1409. }
  1410. else if (WaitResult == WAIT_OBJECT_0 + HandleCount)
  1411. {
  1412. if (Configuration->ActualLogProtocol >= 1)
  1413. {
  1414. LogEvent("Detected network event");
  1415. }
  1416. if (Events == NULL)
  1417. {
  1418. if (ProcessNetworkEvents(FSocket))
  1419. {
  1420. Result = true;
  1421. }
  1422. }
  1423. else
  1424. {
  1425. if (EnumNetworkEvents(FSocket, *Events))
  1426. {
  1427. Result = true;
  1428. }
  1429. }
  1430. {
  1431. TSockets::iterator i = FPortFwdSockets.begin();
  1432. while (i != FPortFwdSockets.end())
  1433. {
  1434. ProcessNetworkEvents(*i);
  1435. i++;
  1436. }
  1437. }
  1438. }
  1439. else if (WaitResult == WAIT_TIMEOUT)
  1440. {
  1441. if (Configuration->ActualLogProtocol >= 2)
  1442. {
  1443. LogEvent("Timeout waiting for network events");
  1444. }
  1445. MSec = 0;
  1446. }
  1447. else
  1448. {
  1449. if (Configuration->ActualLogProtocol >= 2)
  1450. {
  1451. LogEvent(FORMAT("Unknown waiting result %d", (int(WaitResult))));
  1452. }
  1453. MSec = 0;
  1454. }
  1455. }
  1456. __finally
  1457. {
  1458. sfree(Handles);
  1459. }
  1460. unsigned int TicksAfter = GetTickCount();
  1461. // ticks wraps once in 49.7 days
  1462. if (TicksBefore < TicksAfter)
  1463. {
  1464. unsigned int Ticks = TicksAfter - TicksBefore;
  1465. if (Ticks > MSec)
  1466. {
  1467. MSec = 0;
  1468. }
  1469. else
  1470. {
  1471. MSec -= Ticks;
  1472. }
  1473. }
  1474. }
  1475. while (ReadEventRequired && (MSec > 0) && !Result);
  1476. return Result;
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. void __fastcall TSecureShell::Idle(unsigned int MSec)
  1480. {
  1481. noise_regular();
  1482. call_ssh_timer(FBackendHandle);
  1483. EventSelectLoop(MSec, false, NULL);
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. void __fastcall TSecureShell::KeepAlive()
  1487. {
  1488. if (FActive && (FWaiting == 0))
  1489. {
  1490. LogEvent("Sending null packet to keep session alive.");
  1491. SendSpecial(TS_PING);
  1492. }
  1493. else
  1494. {
  1495. // defer next keepalive attempt
  1496. FLastDataSent = Now();
  1497. }
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. unsigned long __fastcall TSecureShell::MaxPacketSize()
  1501. {
  1502. if (!FSessionInfoValid)
  1503. {
  1504. UpdateSessionInfo();
  1505. }
  1506. if (FSshVersion == 1)
  1507. {
  1508. return 0;
  1509. }
  1510. else
  1511. {
  1512. if (FMaxPacketSize == NULL)
  1513. {
  1514. FMaxPacketSize = ssh2_remmaxpkt(FBackendHandle);
  1515. }
  1516. return *FMaxPacketSize;
  1517. }
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. AnsiString __fastcall TSecureShell::FuncToCompression(
  1521. int SshVersion, const void * Compress) const
  1522. {
  1523. enum TCompressionType { ctNone, ctZLib };
  1524. if (SshVersion == 1)
  1525. {
  1526. return get_ssh1_compressing(FBackendHandle) ? "ZLib" : "";
  1527. }
  1528. else
  1529. {
  1530. return (ssh_compress *)Compress == &ssh_zlib ? "ZLib" : "";
  1531. }
  1532. }
  1533. //---------------------------------------------------------------------------
  1534. TCipher __fastcall TSecureShell::FuncToSsh1Cipher(const void * Cipher)
  1535. {
  1536. const ssh_cipher *CipherFuncs[] =
  1537. {&ssh_3des, &ssh_des, &ssh_blowfish_ssh1};
  1538. const TCipher TCiphers[] = {cip3DES, cipDES, cipBlowfish};
  1539. assert(LENOF(CipherFuncs) == LENOF(TCiphers));
  1540. TCipher Result = cipWarn;
  1541. for (int Index = 0; Index < LENOF(TCiphers); Index++)
  1542. {
  1543. if ((ssh_cipher *)Cipher == CipherFuncs[Index])
  1544. {
  1545. Result = TCiphers[Index];
  1546. }
  1547. }
  1548. assert(Result != cipWarn);
  1549. return Result;
  1550. }
  1551. //---------------------------------------------------------------------------
  1552. TCipher __fastcall TSecureShell::FuncToSsh2Cipher(const void * Cipher)
  1553. {
  1554. const ssh2_ciphers *CipherFuncs[] =
  1555. {&ssh2_3des, &ssh2_des, &ssh2_aes, &ssh2_blowfish, &ssh2_arcfour};
  1556. const TCipher TCiphers[] = {cip3DES, cipDES, cipAES, cipBlowfish, cipArcfour};
  1557. assert(LENOF(CipherFuncs) == LENOF(TCiphers));
  1558. TCipher Result = cipWarn;
  1559. for (int C = 0; C < LENOF(TCiphers); C++)
  1560. {
  1561. for (int F = 0; F < CipherFuncs[C]->nciphers; F++)
  1562. {
  1563. if ((ssh2_cipher *)Cipher == CipherFuncs[C]->list[F])
  1564. {
  1565. Result = TCiphers[C];
  1566. }
  1567. }
  1568. }
  1569. assert(Result != cipWarn);
  1570. return Result;
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. struct TClipboardHandler
  1574. {
  1575. AnsiString Text;
  1576. void __fastcall Copy(TObject * /*Sender*/)
  1577. {
  1578. CopyToClipboard(Text);
  1579. }
  1580. };
  1581. //---------------------------------------------------------------------------
  1582. void __fastcall TSecureShell::VerifyHostKey(AnsiString Host, int Port,
  1583. const AnsiString KeyType, AnsiString KeyStr, const AnsiString Fingerprint)
  1584. {
  1585. GotHostKey();
  1586. char Delimiter = ';';
  1587. assert(KeyStr.Pos(Delimiter) == 0);
  1588. if (FSessionData->Tunnel)
  1589. {
  1590. Host = FSessionData->OrigHostName;
  1591. Port = FSessionData->OrigPortNumber;
  1592. }
  1593. FSessionInfo.HostKeyFingerprint = Fingerprint;
  1594. bool Result = false;
  1595. AnsiString Buf = FSessionData->HostKey;
  1596. while (!Result && !Buf.IsEmpty())
  1597. {
  1598. AnsiString ExpectedKey = CutToChar(Buf, Delimiter, false);
  1599. if (ExpectedKey == Fingerprint)
  1600. {
  1601. Result = true;
  1602. }
  1603. }
  1604. AnsiString StoredKeys;
  1605. if (!Result)
  1606. {
  1607. StoredKeys.SetLength(10240);
  1608. if (retrieve_host_key(Host.c_str(), Port, KeyType.c_str(),
  1609. StoredKeys.c_str(), StoredKeys.Length()) == 0)
  1610. {
  1611. PackStr(StoredKeys);
  1612. AnsiString Buf = StoredKeys;
  1613. while (!Result && !Buf.IsEmpty())
  1614. {
  1615. AnsiString StoredKey = CutToChar(Buf, Delimiter, false);
  1616. if (StoredKey == KeyStr)
  1617. {
  1618. Result = true;
  1619. }
  1620. }
  1621. }
  1622. else
  1623. {
  1624. StoredKeys = "";
  1625. }
  1626. }
  1627. if (!Result)
  1628. {
  1629. if (Configuration->DisableAcceptingHostKeys)
  1630. {
  1631. FatalError(LoadStr(KEY_NOT_VERIFIED));
  1632. }
  1633. else
  1634. {
  1635. TClipboardHandler ClipboardHandler;
  1636. ClipboardHandler.Text = Fingerprint;
  1637. bool Unknown = StoredKeys.IsEmpty();
  1638. int Answers;
  1639. int AliasesCount;
  1640. TQueryButtonAlias Aliases[3];
  1641. Aliases[0].Button = qaRetry;
  1642. Aliases[0].Alias = LoadStr(COPY_KEY_BUTTON);
  1643. Aliases[0].OnClick = &ClipboardHandler.Copy;
  1644. Answers = qaYes | qaCancel | qaRetry;
  1645. AliasesCount = 1;
  1646. if (!Unknown)
  1647. {
  1648. Aliases[1].Button = qaYes;
  1649. Aliases[1].Alias = LoadStr(UPDATE_KEY_BUTTON);
  1650. Aliases[2].Button = qaOK;
  1651. Aliases[2].Alias = LoadStr(ADD_KEY_BUTTON);
  1652. AliasesCount += 2;
  1653. Answers |= qaSkip | qaOK;
  1654. }
  1655. else
  1656. {
  1657. Answers |= qaNo;
  1658. }
  1659. TQueryParams Params;
  1660. Params.NoBatchAnswers = qaYes | qaRetry | qaSkip | qaOK;
  1661. Params.HelpKeyword = (Unknown ? HELP_UNKNOWN_KEY : HELP_DIFFERENT_KEY);
  1662. Params.Aliases = Aliases;
  1663. Params.AliasesCount = AliasesCount;
  1664. int R = FUI->QueryUser(
  1665. FMTLOAD((Unknown ? UNKNOWN_KEY2 : DIFFERENT_KEY3), (KeyType, Fingerprint)),
  1666. NULL, Answers, &Params, qtWarning);
  1667. switch (R) {
  1668. case qaOK:
  1669. assert(!Unknown);
  1670. KeyStr = (StoredKeys + Delimiter + KeyStr);
  1671. // fall thru
  1672. case qaYes:
  1673. store_host_key(Host.c_str(), Port, KeyType.c_str(), KeyStr.c_str());
  1674. break;
  1675. case qaCancel:
  1676. FatalError(LoadStr(KEY_NOT_VERIFIED));
  1677. }
  1678. }
  1679. }
  1680. }
  1681. //---------------------------------------------------------------------------
  1682. void __fastcall TSecureShell::AskAlg(const AnsiString AlgType,
  1683. const AnsiString AlgName)
  1684. {
  1685. AnsiString Msg;
  1686. if (AlgType == "key-exchange algorithm")
  1687. {
  1688. Msg = FMTLOAD(KEX_BELOW_TRESHOLD, (AlgName));
  1689. }
  1690. else
  1691. {
  1692. int CipherType;
  1693. if (AlgType == "cipher")
  1694. {
  1695. CipherType = CIPHER_TYPE_BOTH;
  1696. }
  1697. else if (AlgType == "client-to-server cipher")
  1698. {
  1699. CipherType = CIPHER_TYPE_CS;
  1700. }
  1701. else if (AlgType == "server-to-client cipher")
  1702. {
  1703. CipherType = CIPHER_TYPE_SC;
  1704. }
  1705. else
  1706. {
  1707. assert(false);
  1708. }
  1709. Msg = FMTLOAD(CIPHER_BELOW_TRESHOLD, (LoadStr(CipherType), AlgName));
  1710. }
  1711. if (FUI->QueryUser(Msg, NULL, qaYes | qaNo, NULL, qtWarning) == qaNo)
  1712. {
  1713. Abort();
  1714. }
  1715. }
  1716. //---------------------------------------------------------------------------
  1717. void __fastcall TSecureShell::DisplayBanner(const AnsiString & Banner)
  1718. {
  1719. FUI->DisplayBanner(Banner);
  1720. }
  1721. //---------------------------------------------------------------------------
  1722. void __fastcall TSecureShell::OldKeyfileWarning()
  1723. {
  1724. // actually never called, see Net.cpp
  1725. FUI->QueryUser(LoadStr(OLD_KEY), NULL, qaOK, NULL, qtWarning);
  1726. }
  1727. //---------------------------------------------------------------------------
  1728. bool __fastcall TSecureShell::GetStoredCredentialsTried()
  1729. {
  1730. return FStoredPasswordTried || FStoredPasswordTriedForKI;
  1731. }
  1732. //---------------------------------------------------------------------------
  1733. bool __fastcall TSecureShell::GetReady()
  1734. {
  1735. return FOpened && (FWaiting == 0);
  1736. }