| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "PuttyIntf.h"
- #include "Exceptions.h"
- #include "Interface.h"
- #include "SecureShell.h"
- #include "TextsCore.h"
- #include "HelpCore.h"
- #include "Common.h"
- #include "CoreMain.h"
- #ifndef AUTO_WINSOCK
- #include <winsock2.h>
- #endif
- //---------------------------------------------------------------------------
- #define SSH_ERROR(x) throw ESsh(NULL, x)
- #define SSH_FATAL_ERROR_EXT(E, x) throw ESshFatal(E, x)
- #define SSH_FATAL_ERROR(x) SSH_FATAL_ERROR_EXT(NULL, x)
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- #define MAX_BUFSIZE 32768
- //---------------------------------------------------------------------------
- struct TPuttyTranslation
- {
- const char * Original;
- int Translation;
- };
- //---------------------------------------------------------------------------
- __fastcall TSecureShell::TSecureShell(TSessionUI* UI,
- TSessionData * SessionData, TSessionLog * Log, TConfiguration * Configuration)
- {
- FUI = UI;
- FSessionData = SessionData;
- FLog = Log;
- FConfiguration = Configuration;
- FActive = false;
- OutPtr = NULL;
- Pending = NULL;
- FBackendHandle = NULL;
- ResetConnection();
- FOnCaptureOutput = NULL;
- FOnReceive = NULL;
- FConfig = new Config();
- memset(FConfig, 0, sizeof(*FConfig));
- FSocket = INVALID_SOCKET;
- FSocketEvent = CreateEvent(NULL, false, false, NULL);
- FFrozen = false;
- }
- //---------------------------------------------------------------------------
- __fastcall TSecureShell::~TSecureShell()
- {
- Active = false;
- ResetConnection();
- CloseHandle(FSocketEvent);
- ClearConfig(FConfig);
- delete FConfig;
- FConfig = NULL;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::ResetConnection()
- {
- FreeBackend();
- ClearStdError();
- PendLen = 0;
- PendSize = 0;
- sfree(Pending);
- Pending = NULL;
- FCWriteTemp = "";
- ResetSessionInfo();
- FAuthenticating = false;
- FAuthenticated = false;
- FStoredPasswordTried = false;
- FStoredPasswordTriedForKI = false;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::ResetSessionInfo()
- {
- FSessionInfoValid = false;
- FMaxPacketSize = NULL;
- }
- //---------------------------------------------------------------------------
- inline void __fastcall TSecureShell::UpdateSessionInfo()
- {
- if (!FSessionInfoValid)
- {
- FSshVersion = get_ssh_version(FBackendHandle);
- FSessionInfo.ProtocolBaseName = "SSH";
- FSessionInfo.ProtocolName =
- FORMAT("%s-%d", (FSessionInfo.ProtocolBaseName, get_ssh_version(FBackendHandle)));
- FSessionInfo.SecurityProtocolName = FSessionInfo.ProtocolName;
- FSessionInfo.CSCompression =
- FuncToCompression(FSshVersion, get_cscomp(FBackendHandle));
- FSessionInfo.SCCompression =
- FuncToCompression(FSshVersion, get_sccomp(FBackendHandle));
- if (FSshVersion == 1)
- {
- FSessionInfo.CSCipher = CipherNames[FuncToSsh1Cipher(get_cipher(FBackendHandle))];
- FSessionInfo.SCCipher = CipherNames[FuncToSsh1Cipher(get_cipher(FBackendHandle))];
- }
- else
- {
- FSessionInfo.CSCipher = CipherNames[FuncToSsh2Cipher(get_cscipher(FBackendHandle))];
- FSessionInfo.SCCipher = CipherNames[FuncToSsh2Cipher(get_sccipher(FBackendHandle))];
- }
- FSessionInfoValid = true;
- }
- }
- //---------------------------------------------------------------------------
- const TSessionInfo & __fastcall TSecureShell::GetSessionInfo()
- {
- if (!FSessionInfoValid)
- {
- UpdateSessionInfo();
- }
- return FSessionInfo;
- }
- //---------------------------------------------------------------------
- void __fastcall TSecureShell::ClearConfig(Config * cfg)
- {
- StrDispose(cfg->remote_cmd_ptr);
- StrDispose(cfg->remote_cmd_ptr2);
- // clear all
- memset(cfg, 0, sizeof(*cfg));
- }
- //---------------------------------------------------------------------
- void __fastcall TSecureShell::StoreToConfig(TSessionData * Data, Config * cfg)
- {
- ClearConfig(cfg);
- // user-configurable settings
- ASCOPY(cfg->host, Data->HostName);
- ASCOPY(cfg->username, Data->UserName);
- cfg->port = Data->PortNumber;
- cfg->protocol = PROT_SSH;
- // always set 0, as we will handle keepalives ourselves to avoid
- // multi-threaded issues in putty timer list
- cfg->ping_interval = 0;
- cfg->compression = Data->Compression;
- cfg->tryagent = Data->TryAgent;
- cfg->agentfwd = Data->AgentFwd;
- cfg->addressfamily = Data->AddressFamily;
- ASCOPY(cfg->ssh_rekey_data, Data->RekeyData);
- cfg->ssh_rekey_time = Data->RekeyTime;
- for (int c = 0; c < CIPHER_COUNT; c++)
- {
- int pcipher;
- switch (Data->Cipher[c]) {
- case cipWarn: pcipher = CIPHER_WARN; break;
- case cip3DES: pcipher = CIPHER_3DES; break;
- case cipBlowfish: pcipher = CIPHER_BLOWFISH; break;
- case cipAES: pcipher = CIPHER_AES; break;
- case cipDES: pcipher = CIPHER_DES; break;
- case cipArcfour: pcipher = CIPHER_ARCFOUR; break;
- default: assert(false);
- }
- cfg->ssh_cipherlist[c] = pcipher;
- }
- for (int k = 0; k < KEX_COUNT; k++)
- {
- int pkex;
- switch (Data->Kex[k]) {
- case kexWarn: pkex = KEX_WARN; break;
- case kexDHGroup1: pkex = KEX_DHGROUP1; break;
- case kexDHGroup14: pkex = KEX_DHGROUP14; break;
- case kexDHGEx: pkex = KEX_DHGEX; break;
- case kexGSSGroup1: pkex = KEX_GSSGROUP1; break;
- case kexGSSGroup14: pkex = KEX_GSSGROUP14; break;
- case kexGSSGEx: pkex = KEX_GSSGEX; break;
- default: assert(false);
- }
- cfg->ssh_kexlist[k] = pkex;
- }
- AnsiString SPublicKeyFile = Data->PublicKeyFile;
- if (SPublicKeyFile.IsEmpty()) SPublicKeyFile = Configuration->DefaultKeyFile;
- SPublicKeyFile = StripPathQuotes(ExpandEnvironmentVariables(SPublicKeyFile));
- ASCOPY(cfg->keyfile.path, SPublicKeyFile);
- cfg->sshprot = Data->SshProt;
- cfg->ssh2_des_cbc = Data->Ssh2DES;
- cfg->ssh_no_userauth = Data->SshNoUserAuth;
- cfg->try_tis_auth = Data->AuthTIS;
- cfg->try_ki_auth = Data->AuthKI;
- cfg->try_sspi_auth = Data->AuthGSSAPI;
- cfg->sspi_fwd_ticket = Data->GSSAPIFwdTGT;
- ASCOPY(cfg->service_principal_name, Data->GSSAPIServerRealm);
- cfg->try_gsskex = Data->TryGSSKEX;
- cfg->username_from_env = Data->UserNameFromEnvironment;
- cfg->sspi_no_username = Data->GSSAPIServerChoosesUserName;
- cfg->sspi_trust_dns = Data->GSSAPITrustDNS;
- cfg->change_username = Data->ChangeUsername;
- cfg->proxy_type = Data->ProxyMethod;
- ASCOPY(cfg->proxy_host, Data->ProxyHost);
- cfg->proxy_port = Data->ProxyPort;
- ASCOPY(cfg->proxy_username, Data->ProxyUsername);
- ASCOPY(cfg->proxy_password, Data->ProxyPassword);
- if (Data->ProxyMethod == pmCmd)
- {
- ASCOPY(cfg->proxy_telnet_command, Data->ProxyLocalCommand);
- }
- else
- {
- ASCOPY(cfg->proxy_telnet_command, Data->ProxyTelnetCommand);
- }
- cfg->proxy_dns = Data->ProxyDNS;
- cfg->even_proxy_localhost = Data->ProxyLocalhost;
- #pragma option push -w-eas
- // after 0.53b values were reversed, however putty still stores
- // settings to registry in save way as before
- cfg->sshbug_ignore1 = Data->Bug[sbIgnore1];
- cfg->sshbug_plainpw1 = Data->Bug[sbPlainPW1];
- cfg->sshbug_rsa1 = Data->Bug[sbRSA1];
- cfg->sshbug_hmac2 = Data->Bug[sbHMAC2];
- cfg->sshbug_derivekey2 = Data->Bug[sbDeriveKey2];
- cfg->sshbug_rsapad2 = Data->Bug[sbRSAPad2];
- cfg->sshbug_rekey2 = Data->Bug[sbRekey2];
- // new after 0.53b
- cfg->sshbug_pksessid2 = Data->Bug[sbPKSessID2];
- #pragma option pop
- if (!Data->TunnelPortFwd.IsEmpty())
- {
- ASCOPY(cfg->portfwd, Data->TunnelPortFwd);
- // when setting up a tunnel, do not open shell/sftp
- cfg->ssh_no_shell = TRUE;
- }
- else
- {
- if (Data->FSProtocol == fsSCPonly)
- {
- cfg->ssh_subsys = FALSE;
- if (Data->Shell.IsEmpty())
- {
- // Following forces Putty to open default shell
- // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
- cfg->remote_cmd[0] = '\0';
- }
- else
- {
- cfg->remote_cmd_ptr = StrNew(Data->Shell.c_str());
- }
- }
- else
- {
- if (Data->SftpServer.IsEmpty())
- {
- cfg->ssh_subsys = TRUE;
- strcpy(cfg->remote_cmd, "sftp");
- }
- else
- {
- cfg->ssh_subsys = FALSE;
- cfg->remote_cmd_ptr = StrNew(Data->SftpServer.c_str());
- }
- if (Data->FSProtocol != fsSFTPonly)
- {
- cfg->ssh_subsys2 = FALSE;
- if (Data->Shell.IsEmpty())
- {
- // Following forces Putty to open default shell
- // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
- cfg->remote_cmd_ptr2 = StrNew("\0");
- }
- else
- {
- cfg->remote_cmd_ptr2 = StrNew(Data->Shell.c_str());
- }
- }
- if ((Data->FSProtocol == fsSFTPonly) && Data->SftpServer.IsEmpty())
- {
- // see psftp_connect() from psftp.c
- cfg->ssh_subsys2 = FALSE;
- cfg->remote_cmd_ptr2 = StrNew(
- "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n"
- "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n"
- "exec sftp-server");
- }
- }
- }
- // permanent settings
- cfg->nopty = TRUE;
- cfg->tcp_keepalives = 0;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::Open()
- {
- ResetConnection();
- FAuthenticating = false;
- FAuthenticated = false;
- Active = false;
- FBackend = &ssh_backend;
- FAuthenticationLog = "";
- FUI->Information(LoadStr(STATUS_LOOKUPHOST), true);
- StoreToConfig(FSessionData, FConfig);
- char * RealHost;
- FreeBackend(); // in case we are reconnecting
- const char * InitError = FBackend->init(this, &FBackendHandle, FConfig,
- FSessionData->HostName.c_str(), FSessionData->PortNumber, &RealHost, 0,
- FConfig->tcp_keepalives);
- sfree(RealHost);
- if (InitError)
- {
- PuttyFatalError(InitError);
- }
- FUI->Information(LoadStr(STATUS_CONNECT), true);
- Init();
- CheckConnection(CONNECTION_FAILED);
- FLastDataSent = Now();
- FSessionInfo.LoginTime = Now();
- FAuthenticating = false;
- FAuthenticated = true;
- FUI->Information(LoadStr(STATUS_AUTHENTICATED), true);
- ResetSessionInfo();
- assert(!FSessionInfo.SshImplementation.IsEmpty());
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::Init()
- {
- try
- {
- try
- {
- while (!get_ssh_state_session(FBackendHandle))
- {
- if (Configuration->LogProtocol >= 1)
- {
- LogEvent("Waiting for the server to continue with the initialisation");
- }
- WaitForData();
- }
- // unless this is tunnel session, it must be safe to send now
- assert(FBackend->sendok(FBackendHandle) || !FSessionData->TunnelPortFwd.IsEmpty());
- }
- catch(Exception & E)
- {
- if (FAuthenticating && !FAuthenticationLog.IsEmpty())
- {
- FatalError(&E, FMTLOAD(AUTHENTICATION_LOG, (FAuthenticationLog)));
- }
- else
- {
- throw;
- }
- }
- }
- catch(Exception & E)
- {
- if (FAuthenticating)
- {
- FatalError(&E, LoadStr(AUTHENTICATION_FAILED));
- }
- else
- {
- throw;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::PuttyLogEvent(const AnsiString & Str)
- {
- #define SERVER_VERSION_MSG "Server version: "
- // Gross hack
- if (Str.Pos(SERVER_VERSION_MSG) == 1)
- {
- FSessionInfo.SshVersionString = Str.SubString(strlen(SERVER_VERSION_MSG) + 1,
- Str.Length() - strlen(SERVER_VERSION_MSG));
- const char * Ptr = strchr(FSessionInfo.SshVersionString.c_str(), '-');
- if (Ptr != NULL)
- {
- Ptr = strchr(Ptr + 1, '-');
- }
- FSessionInfo.SshImplementation = (Ptr != NULL) ? Ptr + 1 : "";
- }
- #define FORWARDING_FAILURE_MSG "Forwarded connection refused by server: "
- else if (Str.Pos(FORWARDING_FAILURE_MSG) == 1)
- {
- FLastTunnelError = Str.SubString(strlen(FORWARDING_FAILURE_MSG) + 1,
- Str.Length() - strlen(FORWARDING_FAILURE_MSG));
- static const TPuttyTranslation Translation[] = {
- { "Administratively prohibited [%]", PFWD_TRANSL_ADMIN },
- { "Connect failed [%]", PFWD_TRANSL_CONNECT },
- };
- TranslatePuttyMessage(Translation, LENOF(Translation), FLastTunnelError);
- }
- LogEvent(Str);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSecureShell::PromptUser(bool /*ToServer*/,
- AnsiString AName, bool /*NameRequired*/,
- AnsiString Instructions, bool /*InstructionsRequired*/,
- TStrings * Prompts, TStrings * Results)
- {
- assert(Prompts->Count > 0);
- assert(Results->Count == Prompts->Count);
- TPromptKind PromptKind;
- // beware of changing order
- static const TPuttyTranslation NameTranslation[] = {
- { "SSH login name", USERNAME_TITLE },
- { "SSH key passphrase", PASSPHRASE_TITLE },
- { "SSH TIS authentication", SERVER_PROMPT_TITLE },
- { "SSH CryptoCard authentication", SERVER_PROMPT_TITLE },
- { "SSH server: %", SERVER_PROMPT_TITLE2 },
- { "SSH server authentication", SERVER_PROMPT_TITLE },
- { "SSH password", PASSWORD_TITLE },
- { "New SSH password", NEW_PASSWORD_TITLE },
- };
- AnsiString Name = AName;
- int Index = TranslatePuttyMessage(NameTranslation, LENOF(NameTranslation), Name);
- const TPuttyTranslation * InstructionTranslation = NULL;
- const TPuttyTranslation * PromptTranslation = NULL;
- size_t PromptTranslationCount = 1;
- if (Index == 0) // username
- {
- static const TPuttyTranslation UsernamePromptTranslation[] = {
- { "login as: ", USERNAME_PROMPT2 },
- };
- PromptTranslation = UsernamePromptTranslation;
- PromptKind = pkUserName;
- }
- else if (Index == 1) // passhrase
- {
- static const TPuttyTranslation PassphrasePromptTranslation[] = {
- { "Passphrase for key \"%s\": ", PROMPT_KEY_PASSPHRASE },
- };
- PromptTranslation = PassphrasePromptTranslation;
- PromptKind = pkPassphrase;
- }
- else if (Index == 2) // TIS
- {
- static const TPuttyTranslation TISInstructionTranslation[] = {
- { "Using TIS authentication.%", TIS_INSTRUCTION },
- };
- static const TPuttyTranslation TISPromptTranslation[] = {
- { "Response: ", PROMPT_PROMPT },
- };
- InstructionTranslation = TISInstructionTranslation;
- PromptTranslation = TISPromptTranslation;
- PromptKind = pkTIS;
- }
- else if (Index == 3) // CryptoCard
- {
- static const TPuttyTranslation CryptoCardInstructionTranslation[] = {
- { "Using CryptoCard authentication.%", CRYPTOCARD_INSTRUCTION },
- };
- static const TPuttyTranslation CryptoCardPromptTranslation[] = {
- { "Response: ", PROMPT_PROMPT },
- };
- InstructionTranslation = CryptoCardInstructionTranslation;
- PromptTranslation = CryptoCardPromptTranslation;
- PromptKind = pkCryptoCard;
- }
- else if ((Index == 4) || (Index == 5))
- {
- static const TPuttyTranslation KeybInteractiveInstructionTranslation[] = {
- { "Using keyboard-interactive authentication.%", KEYBINTER_INSTRUCTION },
- };
- InstructionTranslation = KeybInteractiveInstructionTranslation;
- PromptKind = pkKeybInteractive;
- }
- else if (Index == 6)
- {
- assert(Prompts->Count == 1);
- Prompts->Strings[0] = LoadStr(PASSWORD_PROMPT);
- PromptKind = pkPassword;
- }
- else if (Index == 7)
- {
- static const TPuttyTranslation NewPasswordPromptTranslation[] = {
- { "Current password (blank for previously entered password): ", NEW_PASSWORD_CURRENT_PROMPT },
- { "Enter new password: ", NEW_PASSWORD_NEW_PROMPT },
- { "Confirm new password: ", NEW_PASSWORD_CONFIRM_PROMPT },
- };
- PromptTranslation = NewPasswordPromptTranslation;
- PromptTranslationCount = LENOF(NewPasswordPromptTranslation);
- PromptKind = pkNewPassword;
- }
- else
- {
- PromptKind = pkPrompt;
- assert(false);
- }
- LogEvent(FORMAT("Prompt (%d, %s, %s, %s)", (PromptKind, AName, Instructions, Prompts->Strings[0])));
- Name = Name.Trim();
- if (InstructionTranslation != NULL)
- {
- TranslatePuttyMessage(InstructionTranslation, 1, Instructions);
- }
- // some servers add leading blank line to make the prompt look prettier
- // on terminal console
- Instructions = Instructions.Trim();
- for (int Index = 0; Index < Prompts->Count; Index++)
- {
- AnsiString Prompt = Prompts->Strings[Index];
- if (PromptTranslation != NULL)
- {
- TranslatePuttyMessage(PromptTranslation, PromptTranslationCount, Prompt);
- }
- // some servers add leading blank line to make the prompt look prettier
- // on terminal console
- Prompts->Strings[Index] = Prompt.Trim();
- }
- bool Result = false;
- if (PromptKind == pkUserName)
- {
- if (FSessionData->AuthGSSAPI)
- {
- // use empty username if no username was filled on login dialog
- // and GSSAPI auth is enabled, hence there's chance that the server can
- // deduce the username otherwise
- Results->Strings[0] = "";
- Result = true;
- }
- }
- else if ((PromptKind == pkTIS) || (PromptKind == pkCryptoCard) ||
- (PromptKind == pkKeybInteractive))
- {
- if (FSessionData->AuthKIPassword && !FSessionData->Password.IsEmpty() &&
- !FStoredPasswordTriedForKI && (Prompts->Count == 1) &&
- !bool(Prompts->Objects[0]))
- {
- LogEvent("Using stored password.");
- FUI->Information(LoadStr(AUTH_PASSWORD), false);
- Result = true;
- Results->Strings[0] = FSessionData->Password;
- FStoredPasswordTriedForKI = true;
- }
- }
- else if (PromptKind == pkPassword)
- {
- if (!FSessionData->Password.IsEmpty() && !FStoredPasswordTried)
- {
- LogEvent("Using stored password.");
- FUI->Information(LoadStr(AUTH_PASSWORD), false);
- Result = true;
- Results->Strings[0] = FSessionData->Password;
- FStoredPasswordTried = true;
- }
- }
- if (!Result)
- {
- Result = FUI->PromptUser(FSessionData,
- PromptKind, Name, Instructions, Prompts, Results);
- if (Result)
- {
- if ((PromptKind == pkUserName) && (Prompts->Count == 1))
- {
- FUserName = Results->Strings[0];
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::GotHostKey()
- {
- // due to re-key GotHostKey() may be called again later during session
- if (!FAuthenticating && !FAuthenticated)
- {
- FAuthenticating = true;
- FUI->Information(LoadStr(STATUS_AUTHENTICATE), true);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::CWrite(const char * Data, int Length)
- {
- // some messages to stderr may indicate that something has changed with the
- // session, so reset the session info
- ResetSessionInfo();
- // We send only whole line at once, so we have to cache incoming data
- FCWriteTemp += DeleteChar(AnsiString(Data, Length), '\r');
- AnsiString Line;
- // Do we have at least one complete line in std error cache?
- while (FCWriteTemp.Pos("\n") > 0)
- {
- AnsiString Line = CutToChar(FCWriteTemp, '\n', false);
- FLog->Add(llStdError, Line);
- if (FAuthenticating)
- {
- TranslateAuthenticationMessage(Line);
- FAuthenticationLog += (FAuthenticationLog.IsEmpty() ? "" : "\n") + Line;
- }
- FUI->Information(Line, false);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::RegisterReceiveHandler(TNotifyEvent Handler)
- {
- assert(FOnReceive == NULL);
- FOnReceive = Handler;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::UnregisterReceiveHandler(TNotifyEvent Handler)
- {
- assert(FOnReceive == Handler);
- USEDPARAM(Handler);
- FOnReceive = NULL;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::FromBackend(bool IsStdErr, const char * Data, int Length)
- {
- CheckConnection();
- if (Configuration->LogProtocol >= 1)
- {
- LogEvent(FORMAT("Received %u bytes (%d)", (Length, int(IsStdErr))));
- }
- // Following is taken from scp.c from_backend() and modified
- if (IsStdErr)
- {
- AddStdError(AnsiString(Data, Length));
- }
- else
- {
- unsigned char *p = (unsigned char *)Data;
- unsigned Len = (unsigned)Length;
- // with event-select mechanism we can now receive data even before we
- // actually expect them (OutPtr can be NULL)
- if ((OutPtr != NULL) && (OutLen > 0) && (Len > 0))
- {
- unsigned Used = OutLen;
- if (Used > Len) Used = Len;
- memcpy(OutPtr, p, Used);
- OutPtr += Used; OutLen -= Used;
- p += Used; Len -= Used;
- }
- if (Len > 0)
- {
- if (PendSize < PendLen + Len)
- {
- PendSize = PendLen + Len + 4096;
- Pending = (char *)
- (Pending ? srealloc(Pending, PendSize) : smalloc(PendSize));
- if (!Pending) FatalError("Out of memory");
- }
- memcpy(Pending + PendLen, p, Len);
- PendLen += Len;
- }
- if (FOnReceive != NULL)
- {
- if (!FFrozen)
- {
- FFrozen = true;
- try
- {
- do
- {
- FDataWhileFrozen = false;
- FOnReceive(NULL);
- }
- while (FDataWhileFrozen);
- }
- __finally
- {
- FFrozen = false;
- }
- }
- else
- {
- FDataWhileFrozen = true;
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSecureShell::Peek(char *& Buf, int Len)
- {
- bool Result = (int(PendLen) >= Len);
- if (Result)
- {
- Buf = Pending;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- Integer __fastcall TSecureShell::Receive(char * Buf, Integer Len)
- {
- CheckConnection();
- if (Len > 0)
- {
- // Following is taken from scp.c ssh_scp_recv() and modified
- OutPtr = Buf;
- OutLen = Len;
- try
- {
- /*
- * See if the pending-input block contains some of what we
- * need.
- */
- if (PendLen > 0)
- {
- unsigned PendUsed = PendLen;
- if (PendUsed > OutLen)
- {
- PendUsed = OutLen;
- }
- memcpy(OutPtr, Pending, PendUsed);
- memmove(Pending, Pending + PendUsed, PendLen - PendUsed);
- OutPtr += PendUsed;
- OutLen -= PendUsed;
- PendLen -= PendUsed;
- if (PendLen == 0)
- {
- PendSize = 0;
- sfree(Pending);
- Pending = NULL;
- }
- }
- while (OutLen > 0)
- {
- if (Configuration->LogProtocol >= 1)
- {
- LogEvent(FORMAT("Waiting for another %u bytes", (static_cast<int>(OutLen))));
- }
- WaitForData();
- }
- // This seems ambiguous
- if (Len <= 0) FatalError(LoadStr(LOST_CONNECTION));
- }
- __finally
- {
- OutPtr = NULL;
- }
- };
- if (Configuration->LogProtocol >= 1)
- {
- LogEvent(FORMAT("Read %u bytes (%d pending)",
- (static_cast<int>(Len), static_cast<int>(PendLen))));
- }
- return Len;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TSecureShell::ReceiveLine()
- {
- unsigned Index;
- Char Ch;
- AnsiString Line;
- Boolean EOL = False;
- do
- {
- // If there is any buffer of received chars
- if (PendLen > 0)
- {
- Index = 0;
- // Repeat until we walk thru whole buffer or reach end-of-line
- while ((Index < PendLen) && (!Index || (Pending[Index-1] != '\n')))
- {
- Index++;
- }
- EOL = (Boolean)(Index && (Pending[Index-1] == '\n'));
- Integer PrevLen = Line.Length();
- Line.SetLength(PrevLen + Index);
- Receive(Line.c_str() + PrevLen, Index);
- }
- // If buffer don't contain end-of-line character
- // we read one more which causes receiving new buffer of chars
- if (!EOL)
- {
- Receive(&Ch, 1);
- Line += Ch;
- EOL = (Ch == '\n');
- }
- }
- while (!EOL);
- // We don't want end-of-line character
- Line.SetLength(Line.Length()-1);
- CaptureOutput(llOutput, Line);
- return Line;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::SendSpecial(int Code)
- {
- LogEvent(FORMAT("Sending special code: %d", (Code)));
- CheckConnection();
- FBackend->special(FBackendHandle, (Telnet_Special)Code);
- CheckConnection();
- FLastDataSent = Now();
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::SendEOF()
- {
- SendSpecial(TS_EOF);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::Send(const char * Buf, Integer Len)
- {
- CheckConnection();
- int BufSize = FBackend->send(FBackendHandle, (char *)Buf, Len);
- if (Configuration->LogProtocol >= 1)
- {
- LogEvent(FORMAT("Sent %u bytes", (static_cast<int>(Len))));
- LogEvent(FORMAT("There are %u bytes remaining in the send buffer", (BufSize)));
- }
- FLastDataSent = Now();
- // among other forces receive of pending data to free the servers's send buffer
- EventSelectLoop(0, false, NULL);
- while (BufSize > MAX_BUFSIZE)
- {
- if (Configuration->LogProtocol >= 1)
- {
- LogEvent(FORMAT("There are %u bytes remaining in the send buffer, "
- "need to send at least another %u bytes",
- (BufSize, BufSize - MAX_BUFSIZE)));
- }
- EventSelectLoop(100, false, NULL);
- BufSize = FBackend->sendbuffer(FBackendHandle);
- if (Configuration->LogProtocol >= 1)
- {
- LogEvent(FORMAT("There are %u bytes remaining in the send buffer", (BufSize)));
- }
- }
- CheckConnection();
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::SendNull()
- {
- LogEvent("Sending NULL.");
- Send("", 1);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::SendStr(AnsiString Str)
- {
- CheckConnection();
- Send(Str.c_str(), Str.Length());
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::SendLine(AnsiString Line)
- {
- SendStr(Line);
- Send("\n", 1);
- FLog->Add(llInput, Line);
- }
- //---------------------------------------------------------------------------
- int __fastcall TSecureShell::TranslatePuttyMessage(
- const TPuttyTranslation * Translation, size_t Count, AnsiString & Message)
- {
- int Result = -1;
- for (unsigned int Index = 0; Index < Count; Index++)
- {
- const char * Original = Translation[Index].Original;
- const char * Div = strchr(Original, '%');
- if (Div == NULL)
- {
- if (strcmp(Message.c_str(), Original) == 0)
- {
- Message = LoadStr(Translation[Index].Translation);
- Result = int(Index);
- break;
- }
- }
- else
- {
- size_t OriginalLen = strlen(Original);
- size_t PrefixLen = Div - Original;
- size_t SuffixLen = OriginalLen - PrefixLen - 1;
- if (((size_t)Message.Length() >= OriginalLen - 1) &&
- (strncmp(Message.c_str(), Original, PrefixLen) == 0) &&
- (strncmp(Message.c_str() + Message.Length() - SuffixLen, Div + 1, SuffixLen) == 0))
- {
- Message = FMTLOAD(Translation[Index].Translation,
- (Message.SubString(PrefixLen + 1, Message.Length() - PrefixLen - SuffixLen).TrimRight()));
- Result = int(Index);
- break;
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TSecureShell::TranslateAuthenticationMessage(AnsiString & Message)
- {
- static const TPuttyTranslation Translation[] = {
- { "Using username \"%\".", AUTH_TRANSL_USERNAME },
- { "Using keyboard-interactive authentication.", AUTH_TRANSL_KEYB_INTER }, // not used anymore
- { "Authenticating with public key \"%\" from agent", AUTH_TRANSL_PUBLIC_KEY_AGENT },
- { "Authenticating with public key \"%\"", AUTH_TRANSL_PUBLIC_KEY },
- { "Authenticated using RSA key \"%\" from agent", AUTH_TRANSL_PUBLIC_KEY_AGENT },
- { "Wrong passphrase", AUTH_TRANSL_WRONG_PASSPHRASE },
- { "Wrong passphrase.", AUTH_TRANSL_WRONG_PASSPHRASE },
- { "Access denied", AUTH_TRANSL_ACCESS_DENIED },
- { "Trying public key authentication.", AUTH_TRANSL_TRY_PUBLIC_KEY },
- { "Server refused our public key.", AUTH_TRANSL_KEY_REFUSED },
- { "Server refused our key", AUTH_TRANSL_KEY_REFUSED }
- };
- return TranslatePuttyMessage(Translation, LENOF(Translation), Message);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::AddStdError(AnsiString Str)
- {
- FStdError += Str;
- Integer P;
- Str = DeleteChar(Str, '\r');
- // We send only whole line at once to log, so we have to cache
- // incoming std error data
- FStdErrorTemp += Str;
- AnsiString Line;
- // Do we have at least one complete line in std error cache?
- while ((P = FStdErrorTemp.Pos("\n")) > 0)
- {
- Line = FStdErrorTemp.SubString(1, P-1);
- FStdErrorTemp.Delete(1, P);
- AddStdErrorLine(Line);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::AddStdErrorLine(const AnsiString & Str)
- {
- if (FAuthenticating)
- {
- FAuthenticationLog += (FAuthenticationLog.IsEmpty() ? "" : "\n") + Str;
- }
- CaptureOutput(llStdError, Str);
- }
- //---------------------------------------------------------------------------
- const AnsiString & __fastcall TSecureShell::GetStdError()
- {
- return FStdError;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::ClearStdError()
- {
- // Flush std error cache
- if (!FStdErrorTemp.IsEmpty())
- {
- if (FAuthenticating)
- {
- FAuthenticationLog +=
- (FAuthenticationLog.IsEmpty() ? "" : "\n") + FStdErrorTemp;
- }
- CaptureOutput(llStdError, FStdErrorTemp);
- FStdErrorTemp = "";
- }
- FStdError = "";
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::CaptureOutput(TLogLineType Type,
- const AnsiString & Line)
- {
- if (FOnCaptureOutput != NULL)
- {
- FOnCaptureOutput(Line, (Type == llStdError));
- }
- FLog->Add(Type, Line);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::FatalError(Exception * E, AnsiString Msg)
- {
- // the same code is in TTerminal
- if (FActive)
- {
- // We log this instead of exception handler, because Close() would
- // probably cause exception handler to loose pointer to TShellLog()
- LogEvent("Attempt to close connection due to fatal exception:");
- FLog->Add(llException, Msg);
- FLog->AddException(E);
- Close();
- }
- SSH_FATAL_ERROR_EXT(E, Msg);
- }
- //---------------------------------------------------------------------------
- int __fastcall TSecureShell::TranslateErrorMessage(AnsiString & Message)
- {
- static const TPuttyTranslation Translation[] = {
- { "Server unexpectedly closed network connection", UNEXPECTED_CLOSE_ERROR },
- { "Network error: Connection refused", NET_TRANSL_REFUSED },
- { "Network error: Connection reset by peer", NET_TRANSL_RESET },
- { "Network error: Connection timed out", NET_TRANSL_TIMEOUT },
- };
- return TranslatePuttyMessage(Translation, LENOF(Translation), Message);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::PuttyFatalError(AnsiString Error)
- {
- TranslateErrorMessage(Error);
- FatalError(Error);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::FatalError(AnsiString Error)
- {
- FatalError(NULL, Error);
- }
- //---------------------------------------------------------------------------
- void __fastcall inline TSecureShell::LogEvent(const AnsiString & Str)
- {
- if (FLog->Logging)
- {
- FLog->Add(llMessage, Str);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::SocketEventSelect(SOCKET Socket, HANDLE Event, bool Startup)
- {
- int Events;
- if (Startup)
- {
- Events = (FD_CONNECT | FD_READ | FD_WRITE | FD_OOB | FD_CLOSE | FD_ACCEPT);
- }
- else
- {
- Events = 0;
- }
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent(FORMAT("Selecting events %d for socket %d", (int(Events), int(Socket))));
- }
- if (WSAEventSelect(Socket, (WSAEVENT)Event, Events) == SOCKET_ERROR)
- {
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent(FORMAT("Error selecting events %d for socket %d", (int(Events), int(Socket))));
- }
- if (Startup)
- {
- FatalError(FMTLOAD(EVENT_SELECT_ERROR, (WSAGetLastError())));
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::UpdateSocket(SOCKET value, bool Startup)
- {
- if (!FActive && !Startup)
- {
- // no-op
- // Remove the branch eventualy:
- // When TCP connection fails, PuTTY does not release the memory allocate for
- // socket. As a simple hack we call sk_tcp_close() in ssh.c to release the memory,
- // until they fix it better. Unfortunately sk_tcp_close calles do_select,
- // so we must filter that out.
- }
- else
- {
- assert(value);
- assert((FActive && (FSocket == value)) || (!FActive && Startup));
- SocketEventSelect(value, FSocketEvent, Startup);
- if (Startup)
- {
- FSocket = value;
- FActive = true;
- }
- else
- {
- FSocket = INVALID_SOCKET;
- Discard();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::UpdatePortFwdSocket(SOCKET value, bool Startup)
- {
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent(FORMAT("Updating forwarding socket %d (%d)", (int(value), int(Startup))));
- }
- SocketEventSelect(value, FSocketEvent, Startup);
- if (Startup)
- {
- FPortFwdSockets.insert(value);
- }
- else
- {
- FPortFwdSockets.erase(value);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::SetActive(bool value)
- {
- if (FActive != value)
- {
- if (value)
- {
- Open();
- }
- else
- {
- Close();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::FreeBackend()
- {
- if (FBackendHandle != NULL)
- {
- FBackend->free(FBackendHandle);
- FBackendHandle = NULL;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::Discard()
- {
- bool WasActive = FActive;
- FActive = false;
- if (WasActive)
- {
- FUI->Closed();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::Close()
- {
- LogEvent("Closing connection.");
- assert(FActive);
- FreeBackend();
- Discard();
- }
- //---------------------------------------------------------------------------
- void inline __fastcall TSecureShell::CheckConnection(int Message)
- {
- if (!FActive || get_ssh_state_closed(FBackendHandle))
- {
- AnsiString Str = LoadStr(Message >= 0 ? Message : NOT_CONNECTED);
- int ExitCode = get_ssh_exitcode(FBackendHandle);
- if (ExitCode >= 0)
- {
- Str += " " + FMTLOAD(SSH_EXITCODE, (ExitCode));
- }
- FatalError(Str);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::PoolForData(WSANETWORKEVENTS & Events, unsigned int & Result)
- {
- try
- {
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent("Pooling for data in case they finally arrives");
- }
- // in extreme condition it may happen that send buffer is full, but there
- // will be no data comming and we may not empty the send buffer because we
- // do not process FD_WRITE until we receive any FD_READ
- if (EventSelectLoop(0, false, &Events))
- {
- LogEvent("Data has arrived, closing query to user.");
- Result = qaOK;
- }
- }
- catch(...)
- {
- // if we let the exception out, it may popup another message dialog
- // in whole event loop, another call to PoolForData from original dialog
- // would be invoked, leading to an infinite loop.
- // by retrying we hope (that probably fatal) error would repeat in WaitForData.
- // anyway now once no actual work is done in EventSelectLoop,
- // hardly any exception can occur actually
- Result = qaRetry;
- }
- }
- //---------------------------------------------------------------------------
- class TPoolForDataEvent
- {
- public:
- __fastcall TPoolForDataEvent(TSecureShell * SecureShell, WSANETWORKEVENTS & Events) :
- FSecureShell(SecureShell),
- FEvents(Events)
- {
- }
- void __fastcall PoolForData(unsigned int & Result)
- {
- FSecureShell->PoolForData(FEvents, Result);
- }
- private:
- TSecureShell * FSecureShell;
- WSANETWORKEVENTS & FEvents;
- };
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::WaitForData()
- {
- // see winsftp.c
- bool IncomingData;
- do
- {
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent("Looking for incoming data");
- }
- IncomingData = EventSelectLoop(FSessionData->Timeout * 1000, true, NULL);
- if (!IncomingData)
- {
- WSANETWORKEVENTS Events;
- memset(&Events, 0, sizeof(Events));
- TPoolForDataEvent Event(this, Events);
- LogEvent("Waiting for data timed out, asking user what to do.");
- TQueryParams Params(qpFatalAbort | qpAllowContinueOnError);
- Params.Timer = 500;
- Params.TimerEvent = Event.PoolForData;
- Params.TimerMessage = FMTLOAD(TIMEOUT_STILL_WAITING, (FSessionData->Timeout));
- Params.TimerAnswers = qaAbort;
- int Answer = FUI->QueryUser(FMTLOAD(CONFIRM_PROLONG_TIMEOUT2, (FSessionData->Timeout)),
- NULL, qaRetry | qaAbort, &Params);
- switch (Answer)
- {
- case qaRetry:
- // noop
- break;
- case qaOK:
- // read event was already captured in PoolForData(),
- // make sure we do not try to select it again as it would timeout
- // unless another read event occurs
- IncomingData = true;
- HandleNetworkEvents(FSocket, Events);
- break;
- default:
- assert(false);
- // fallthru
- case qaAbort:
- FatalError(LoadStr(USER_TERMINATED));
- break;
- }
- }
- }
- while (!IncomingData);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSecureShell::SshFallbackCmd() const
- {
- return ssh_fallback_cmd(FBackendHandle);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSecureShell::EnumNetworkEvents(SOCKET Socket, WSANETWORKEVENTS & Events)
- {
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent(FORMAT("Enumerating network events for socket %d", (int(Socket))));
- }
- // see winplink.c
- WSANETWORKEVENTS AEvents;
- if (WSAEnumNetworkEvents(Socket, NULL, &AEvents) == 0)
- {
- noise_ultralight(Socket);
- noise_ultralight(AEvents.lNetworkEvents);
- Events.lNetworkEvents |= AEvents.lNetworkEvents;
- for (int Index = 0; Index < FD_MAX_EVENTS; Index++)
- {
- if (AEvents.iErrorCode[Index] != 0)
- {
- Events.iErrorCode[Index] = AEvents.iErrorCode[Index];
- }
- }
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent(FORMAT("Enumerated %d network events making %d cumulative events for socket %d",
- (int(AEvents.lNetworkEvents), int(Events.lNetworkEvents), int(Socket))));
- }
- }
- else
- {
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent(FORMAT("Error enumerating network events for socket %d", (int(Socket))));
- }
- }
- return
- FLAGSET(Events.lNetworkEvents, FD_READ) ||
- FLAGSET(Events.lNetworkEvents, FD_CLOSE);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::HandleNetworkEvents(SOCKET Socket, WSANETWORKEVENTS & Events)
- {
- static const struct { int Bit, Mask; const char * Desc; } EventTypes[] =
- {
- { FD_READ_BIT, FD_READ, "read" },
- { FD_WRITE_BIT, FD_WRITE, "write" },
- { FD_OOB_BIT, FD_OOB, "oob" },
- { FD_ACCEPT_BIT, FD_ACCEPT, "accept" },
- { FD_CONNECT_BIT, FD_CONNECT, "connect" },
- { FD_CLOSE_BIT, FD_CLOSE, "close" },
- };
- for (int Event = 0; Event < LENOF(EventTypes); Event++)
- {
- if (FLAGSET(Events.lNetworkEvents, EventTypes[Event].Mask))
- {
- int Err = Events.iErrorCode[EventTypes[Event].Bit];
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent(FORMAT("Handling network %s event on socket %d with error %d",
- (EventTypes[Event].Desc, int(Socket), Err)));
- }
- #pragma option push -w-prc
- LPARAM SelectEvent = WSAMAKESELECTREPLY(EventTypes[Event].Mask, Err);
- #pragma option pop
- if (!select_result((WPARAM)Socket, SelectEvent))
- {
- // note that connection was closed definitely,
- // so "check" is actually not required
- CheckConnection();
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSecureShell::ProcessNetworkEvents(SOCKET Socket)
- {
- WSANETWORKEVENTS Events;
- memset(&Events, 0, sizeof(Events));
- bool Result = EnumNetworkEvents(Socket, Events);
- HandleNetworkEvents(Socket, Events);
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSecureShell::EventSelectLoop(unsigned int MSec, bool ReadEventRequired,
- WSANETWORKEVENTS * Events)
- {
- CheckConnection();
- bool Result = false;
- do
- {
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent("Looking for network events");
- }
- unsigned int TicksBefore = GetTickCount();
- int WaitResult = WaitForSingleObject(FSocketEvent, MSec);
- switch (WaitResult)
- {
- case WAIT_OBJECT_0:
- if (Configuration->LogProtocol >= 1)
- {
- LogEvent("Detected network event");
- }
- if (Events == NULL)
- {
- if (ProcessNetworkEvents(FSocket))
- {
- Result = true;
- }
- }
- else
- {
- if (EnumNetworkEvents(FSocket, *Events))
- {
- Result = true;
- }
- }
- {
- TSockets::iterator i = FPortFwdSockets.begin();
- while (i != FPortFwdSockets.end())
- {
- ProcessNetworkEvents(*i);
- i++;
- }
- }
- break;
- case WAIT_TIMEOUT:
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent("Timeout waiting for network events");
- }
- MSec = 0;
- break;
- default:
- if (Configuration->LogProtocol >= 2)
- {
- LogEvent(FORMAT("Unknown waiting result %d", (WaitResult)));
- }
- MSec = 0;
- break;
- }
- unsigned int TicksAfter = GetTickCount();
- // ticks wraps once in 49.7 days
- if (TicksBefore < TicksAfter)
- {
- unsigned int Ticks = TicksAfter - TicksBefore;
- if (Ticks > MSec)
- {
- MSec = 0;
- }
- else
- {
- MSec -= Ticks;
- }
- }
- }
- while (ReadEventRequired && (MSec > 0) && !Result);
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::Idle(unsigned int MSec)
- {
- noise_regular();
- call_ssh_timer(FBackendHandle);
- EventSelectLoop(MSec, false, NULL);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::KeepAlive()
- {
- LogEvent("Sending null packet to keep session alive.");
- SendSpecial(TS_PING);
- }
- //---------------------------------------------------------------------------
- unsigned long __fastcall TSecureShell::MaxPacketSize()
- {
- if (!FSessionInfoValid)
- {
- UpdateSessionInfo();
- }
- if (FSshVersion == 1)
- {
- return 0;
- }
- else
- {
- if (FMaxPacketSize == NULL)
- {
- FMaxPacketSize = ssh2_remmaxpkt(FBackendHandle);
- }
- return *FMaxPacketSize;
- }
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TSecureShell::FuncToCompression(
- int SshVersion, const void * Compress) const
- {
- enum TCompressionType { ctNone, ctZLib };
- if (SshVersion == 1)
- {
- return get_ssh1_compressing(FBackendHandle) ? "ZLib" : "";
- }
- else
- {
- return (ssh_compress *)Compress == &ssh_zlib ? "ZLib" : "";
- }
- }
- //---------------------------------------------------------------------------
- TCipher __fastcall TSecureShell::FuncToSsh1Cipher(const void * Cipher)
- {
- const ssh_cipher *CipherFuncs[] =
- {&ssh_3des, &ssh_des, &ssh_blowfish_ssh1};
- const TCipher TCiphers[] = {cip3DES, cipDES, cipBlowfish};
- assert(LENOF(CipherFuncs) == LENOF(TCiphers));
- TCipher Result = cipWarn;
- for (int Index = 0; Index < LENOF(TCiphers); Index++)
- {
- if ((ssh_cipher *)Cipher == CipherFuncs[Index])
- {
- Result = TCiphers[Index];
- }
- }
- assert(Result != cipWarn);
- return Result;
- }
- //---------------------------------------------------------------------------
- TCipher __fastcall TSecureShell::FuncToSsh2Cipher(const void * Cipher)
- {
- const ssh2_ciphers *CipherFuncs[] =
- {&ssh2_3des, &ssh2_des, &ssh2_aes, &ssh2_blowfish, &ssh2_arcfour};
- const TCipher TCiphers[] = {cip3DES, cipDES, cipAES, cipBlowfish, cipArcfour};
- assert(LENOF(CipherFuncs) == LENOF(TCiphers));
- TCipher Result = cipWarn;
- for (int C = 0; C < LENOF(TCiphers); C++)
- {
- for (int F = 0; F < CipherFuncs[C]->nciphers; F++)
- {
- if ((ssh2_cipher *)Cipher == CipherFuncs[C]->list[F])
- {
- Result = TCiphers[C];
- }
- }
- }
- assert(Result != cipWarn);
- return Result;
- }
- //---------------------------------------------------------------------------
- struct TClipboardHandler
- {
- AnsiString Text;
- void __fastcall Copy(TObject * /*Sender*/)
- {
- CopyToClipboard(Text);
- }
- };
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::VerifyHostKey(AnsiString Host, int Port,
- const AnsiString KeyType, AnsiString KeyStr, const AnsiString Fingerprint)
- {
- GotHostKey();
- char Delimiter = ';';
- assert(KeyStr.Pos(Delimiter) == 0);
- if (FSessionData->Tunnel)
- {
- Host = FSessionData->OrigHostName;
- Port = FSessionData->OrigPortNumber;
- }
- FSessionInfo.HostKeyFingerprint = Fingerprint;
- bool Result = false;
- AnsiString Buf = FSessionData->HostKey;
- while (!Result && !Buf.IsEmpty())
- {
- AnsiString ExpectedKey = CutToChar(Buf, Delimiter, false);
- if (ExpectedKey == Fingerprint)
- {
- Result = true;
- }
- }
- AnsiString StoredKeys;
- if (!Result)
- {
- StoredKeys.SetLength(10240);
- if (retrieve_host_key(Host.c_str(), Port, KeyType.c_str(),
- StoredKeys.c_str(), StoredKeys.Length()) == 0)
- {
- PackStr(StoredKeys);
- AnsiString Buf = StoredKeys;
- while (!Result && !Buf.IsEmpty())
- {
- AnsiString StoredKey = CutToChar(Buf, Delimiter, false);
- if (StoredKey == KeyStr)
- {
- Result = true;
- }
- }
- }
- else
- {
- StoredKeys = "";
- }
- }
- if (!Result)
- {
- if (Configuration->DisableAcceptingHostKeys)
- {
- FatalError(LoadStr(KEY_NOT_VERIFIED));
- }
- else
- {
- TClipboardHandler ClipboardHandler;
- ClipboardHandler.Text = Fingerprint;
- bool Unknown = StoredKeys.IsEmpty();
- int Answers;
- int AliasesCount;
- TQueryButtonAlias Aliases[3];
- Aliases[0].Button = qaRetry;
- Aliases[0].Alias = LoadStr(COPY_KEY_BUTTON);
- Aliases[0].OnClick = &ClipboardHandler.Copy;
- Answers = qaYes | qaCancel | qaRetry;
- AliasesCount = 1;
- if (!Unknown)
- {
- Aliases[1].Button = qaYes;
- Aliases[1].Alias = LoadStr(UPDATE_KEY_BUTTON);
- Aliases[2].Button = qaOK;
- Aliases[2].Alias = LoadStr(ADD_KEY_BUTTON);
- AliasesCount += 2;
- Answers |= qaSkip | qaOK;
- }
- else
- {
- Answers |= qaNo;
- }
- TQueryParams Params;
- Params.HelpKeyword = (Unknown ? HELP_UNKNOWN_KEY : HELP_DIFFERENT_KEY);
- Params.Aliases = Aliases;
- Params.AliasesCount = AliasesCount;
- int R = FUI->QueryUser(
- FMTLOAD((Unknown ? UNKNOWN_KEY2 : DIFFERENT_KEY3), (KeyType, Fingerprint)),
- NULL, Answers, &Params, qtWarning);
- switch (R) {
- case qaOK:
- assert(!Unknown);
- KeyStr = (StoredKeys + Delimiter + KeyStr);
- // fall thru
- case qaYes:
- store_host_key(Host.c_str(), Port, KeyType.c_str(), KeyStr.c_str());
- break;
- case qaCancel:
- FatalError(LoadStr(KEY_NOT_VERIFIED));
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::AskAlg(const AnsiString AlgType,
- const AnsiString AlgName)
- {
- AnsiString Msg;
- if (AlgType == "key-exchange algorithm")
- {
- Msg = FMTLOAD(KEX_BELOW_TRESHOLD, (AlgName));
- }
- else
- {
- int CipherType;
- if (AlgType == "cipher")
- {
- CipherType = CIPHER_TYPE_BOTH;
- }
- else if (AlgType == "client-to-server cipher")
- {
- CipherType = CIPHER_TYPE_CS;
- }
- else if (AlgType == "server-to-client cipher")
- {
- CipherType = CIPHER_TYPE_SC;
- }
- else
- {
- assert(false);
- }
- Msg = FMTLOAD(CIPHER_BELOW_TRESHOLD, (LoadStr(CipherType), AlgName));
- }
- if (FUI->QueryUser(Msg, NULL, qaYes | qaNo, NULL, qtWarning) == qaNo)
- {
- Abort();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::DisplayBanner(const AnsiString & Banner)
- {
- FUI->DisplayBanner(Banner);
- }
- //---------------------------------------------------------------------------
- void __fastcall TSecureShell::OldKeyfileWarning()
- {
- // actually never called, see Net.cpp
- FUI->QueryUser(LoadStr(OLD_KEY), NULL, qaOK, NULL, qtWarning);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TSecureShell::GetStoredCredentialsTried()
- {
- return FStoredPasswordTried || FStoredPasswordTriedForKI;
- }
|