| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510 | 
							- //---------------------------------------------------------------------------
 
- #include <vcl.h>
 
- #pragma hdrstop
 
- #include "Common.h"
 
- #include "PuttyIntf.h"
 
- #include "Exceptions.h"
 
- #include "Interface.h"
 
- #include "SecureShell.h"
 
- #include "TextsCore.h"
 
- #include "HelpCore.h"
 
- #include "CoreMain.h"
 
- #include <StrUtils.hpp>
 
- #ifndef AUTO_WINSOCK
 
- #include <winsock2.h>
 
- #endif
 
- #include <ws2ipdef.h>
 
- //---------------------------------------------------------------------------
 
- #pragma package(smart_init)
 
- //---------------------------------------------------------------------------
 
- #define MAX_BUFSIZE 32768
 
- //---------------------------------------------------------------------------
 
- const wchar_t HostKeyDelimiter = L';';
 
- //---------------------------------------------------------------------------
 
- struct TPuttyTranslation
 
- {
 
-   const wchar_t * Original;
 
-   int Translation;
 
-   UnicodeString HelpKeyword;
 
- };
 
- //---------------------------------------------------------------------------
 
- __fastcall TSecureShell::TSecureShell(TSessionUI* UI,
 
-   TSessionData * SessionData, TSessionLog * Log, TConfiguration * Configuration)
 
- {
 
-   FUI = UI;
 
-   FSessionData = SessionData;
 
-   FLog = Log;
 
-   FConfiguration = Configuration;
 
-   FActive = false;
 
-   FWaiting = 0;
 
-   FOpened = false;
 
-   FSshImplementation = sshiUnknown;
 
-   OutPtr = NULL;
 
-   Pending = NULL;
 
-   FBackendHandle = NULL;
 
-   ResetConnection();
 
-   FOnCaptureOutput = NULL;
 
-   FOnReceive = NULL;
 
-   FSocket = INVALID_SOCKET;
 
-   FSocketEvent = CreateEvent(NULL, false, false, NULL);
 
-   FFrozen = false;
 
-   FSimple = false;
 
-   FCollectPrivateKeyUsage = false;
 
-   FWaitingForData = 0;
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TSecureShell::~TSecureShell()
 
- {
 
-   DebugAssert(FWaiting == 0);
 
-   Active = false;
 
-   ResetConnection();
 
-   CloseHandle(FSocketEvent);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::ResetConnection()
 
- {
 
-   FreeBackend();
 
-   ClearStdError();
 
-   PendLen = 0;
 
-   PendSize = 0;
 
-   sfree(Pending);
 
-   Pending = NULL;
 
-   FCWriteTemp = L"";
 
-   ResetSessionInfo();
 
-   FAuthenticating = false;
 
-   FAuthenticated = false;
 
-   FStoredPasswordTried = false;
 
-   FStoredPasswordTriedForKI = false;
 
-   FStoredPassphraseTried = false;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::ResetSessionInfo()
 
- {
 
-   FSessionInfoValid = false;
 
-   FMaxPacketSize = NULL;
 
- }
 
- //---------------------------------------------------------------------------
 
- inline void __fastcall TSecureShell::UpdateSessionInfo()
 
- {
 
-   if (!FSessionInfoValid)
 
-   {
 
-     FSshVersion = get_ssh_version(FBackendHandle);
 
-     FSessionInfo.ProtocolBaseName = L"SSH";
 
-     FSessionInfo.ProtocolName =
 
-       FORMAT(L"%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;
 
- }
 
- //---------------------------------------------------------------------
 
- UnicodeString __fastcall TSecureShell::GetHostKeyFingerprint()
 
- {
 
-   return FSessionInfo.HostKeyFingerprint;
 
- }
 
- //---------------------------------------------------------------------
 
- Conf * __fastcall TSecureShell::StoreToConfig(TSessionData * Data, bool Simple)
 
- {
 
-   Conf * conf = conf_new();
 
-   DebugAssert((asOn == FORCE_ON) && (asOff == FORCE_OFF) && (asAuto == AUTO));
 
-   #define CONF_ssh_cipherlist_MAX CIPHER_MAX
 
-   #define CONF_DEF_INT_NONE(KEY) conf_set_int(conf, KEY, 0);
 
-   #define CONF_DEF_STR_NONE(KEY) conf_set_str(conf, KEY, "");
 
-   // noop, used only for these and we set the first four explicitly below and latter two are not used in our code
 
-   #define CONF_DEF_INT_INT(KEY) DebugAssert((KEY == CONF_ssh_cipherlist) || (KEY == CONF_ssh_kexlist) || (KEY == CONF_ssh_gsslist) || (KEY == CONF_ssh_hklist) || (KEY == CONF_colours) || (KEY == CONF_wordness));
 
-   // noop, used only for these three and they all can handle undef value
 
-   #define CONF_DEF_STR_STR(KEY) DebugAssert((KEY == CONF_ttymodes) || (KEY == CONF_portfwd) || (KEY == CONF_environmt) || (KEY == CONF_ssh_manual_hostkeys));
 
-   // noop, not used in our code
 
-   #define CONF_DEF_FONT_NONE(KEY) DebugAssert((KEY == CONF_font) || (KEY == CONF_boldfont) || (KEY == CONF_widefont) || (KEY == CONF_wideboldfont));
 
-   #define CONF_DEF_FILENAME_NONE(KEY) \
 
-     { \
 
-       Filename * filename = filename_from_str(""); \
 
-       conf_set_filename(conf, KEY, filename); \
 
-       filename_free(filename); \
 
-     }
 
-   #define CONF_SET_DEFAULT(VALTYPE, KEYTYPE, KEYWORD) CONF_DEF_ ## VALTYPE ## _ ## KEYTYPE(CONF_ ## KEYWORD);
 
-   CONFIG_OPTIONS(CONF_SET_DEFAULT);
 
-   #undef CONF_SET_DEFAULT
 
-   #undef CONF_DEF_FILENAME_NONE
 
-   #undef CONF_DEF_FONT_NONE
 
-   #undef CONF_DEF_STR_STR
 
-   #undef CONF_DEF_INT_INT
 
-   #undef CONF_DEF_STR_NONE
 
-   #undef CONF_DEF_INT_NONE
 
-   // user-configurable settings
 
-   conf_set_str(conf, CONF_host, AnsiString(Data->HostNameExpanded).c_str());
 
-   conf_set_str(conf, CONF_username, UTF8String(Data->UserNameExpanded).c_str());
 
-   conf_set_int(conf, CONF_port, Data->PortNumber);
 
-   conf_set_int(conf, CONF_protocol, PROT_SSH);
 
-   // always set 0, as we will handle keepalives ourselves to avoid
 
-   // multi-threaded issues in putty timer list
 
-   conf_set_int(conf, CONF_ping_interval, 0);
 
-   conf_set_int(conf, CONF_compression, Data->Compression);
 
-   conf_set_int(conf, CONF_tryagent, Data->TryAgent);
 
-   conf_set_int(conf, CONF_agentfwd, Data->AgentFwd);
 
-   conf_set_int(conf, CONF_addressfamily, Data->AddressFamily);
 
-   conf_set_str(conf, CONF_ssh_rekey_data, AnsiString(Data->RekeyData).c_str());
 
-   conf_set_int(conf, CONF_ssh_rekey_time, Data->RekeyTime);
 
-   DebugAssert(CIPHER_MAX == CIPHER_COUNT);
 
-   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;
 
-       case cipChaCha20: pcipher = CIPHER_CHACHA20; break;
 
-       default: DebugFail();
 
-     }
 
-     conf_set_int_int(conf, CONF_ssh_cipherlist, c, pcipher);
 
-   }
 
-   DebugAssert(KEX_MAX == KEX_COUNT);
 
-   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 kexRSA: pkex = KEX_RSA; break;
 
-       case kexECDH: pkex = KEX_ECDH; break;
 
-       default: DebugFail();
 
-     }
 
-     conf_set_int_int(conf, CONF_ssh_kexlist, k, pkex);
 
-   }
 
-   UnicodeString SPublicKeyFile = Data->PublicKeyFile;
 
-   if (SPublicKeyFile.IsEmpty()) SPublicKeyFile = Configuration->DefaultKeyFile;
 
-   // StripPathQuotes should not be needed as we do not feed quotes anymore
 
-   SPublicKeyFile = StripPathQuotes(ExpandEnvironmentVariables(SPublicKeyFile));
 
-   Filename * KeyFileFileName = filename_from_str(UTF8String(SPublicKeyFile).c_str());
 
-   conf_set_filename(conf, CONF_keyfile, KeyFileFileName);
 
-   filename_free(KeyFileFileName);
 
-   conf_set_int(conf, CONF_sshprot, Data->SshProt);
 
-   conf_set_int(conf, CONF_ssh2_des_cbc, Data->Ssh2DES);
 
-   conf_set_int(conf, CONF_ssh_no_userauth, Data->SshNoUserAuth);
 
-   conf_set_int(conf, CONF_try_tis_auth, Data->AuthTIS);
 
-   conf_set_int(conf, CONF_try_ki_auth, Data->AuthKI);
 
-   conf_set_int(conf, CONF_try_gssapi_auth, Data->AuthGSSAPI);
 
-   conf_set_int(conf, CONF_gssapifwd, Data->GSSAPIFwdTGT);
 
-   conf_set_int(conf, CONF_change_username, Data->ChangeUsername);
 
-   conf_set_int(conf, CONF_proxy_type, Data->ProxyMethod);
 
-   conf_set_str(conf, CONF_proxy_host, AnsiString(Data->ProxyHost).c_str());
 
-   conf_set_int(conf, CONF_proxy_port, Data->ProxyPort);
 
-   conf_set_str(conf, CONF_proxy_username, UTF8String(Data->ProxyUsername).c_str());
 
-   conf_set_str(conf, CONF_proxy_password, UTF8String(Data->ProxyPassword).c_str());
 
-   if (Data->ProxyMethod == pmCmd)
 
-   {
 
-     conf_set_str(conf, CONF_proxy_telnet_command, AnsiString(Data->ProxyLocalCommand).c_str());
 
-   }
 
-   else
 
-   {
 
-     conf_set_str(conf, CONF_proxy_telnet_command, AnsiString(Data->ProxyTelnetCommand).c_str());
 
-   }
 
-   conf_set_int(conf, CONF_proxy_dns, Data->ProxyDNS);
 
-   conf_set_int(conf, CONF_even_proxy_localhost, Data->ProxyLocalhost);
 
-   conf_set_int(conf, CONF_sshbug_ignore1, Data->Bug[sbIgnore1]);
 
-   conf_set_int(conf, CONF_sshbug_plainpw1, Data->Bug[sbPlainPW1]);
 
-   conf_set_int(conf, CONF_sshbug_rsa1, Data->Bug[sbRSA1]);
 
-   conf_set_int(conf, CONF_sshbug_hmac2, Data->Bug[sbHMAC2]);
 
-   conf_set_int(conf, CONF_sshbug_derivekey2, Data->Bug[sbDeriveKey2]);
 
-   conf_set_int(conf, CONF_sshbug_rsapad2, Data->Bug[sbRSAPad2]);
 
-   conf_set_int(conf, CONF_sshbug_rekey2, Data->Bug[sbRekey2]);
 
-   conf_set_int(conf, CONF_sshbug_pksessid2, Data->Bug[sbPKSessID2]);
 
-   conf_set_int(conf, CONF_sshbug_maxpkt2, Data->Bug[sbMaxPkt2]);
 
-   conf_set_int(conf, CONF_sshbug_ignore2, Data->Bug[sbIgnore2]);
 
-   conf_set_int(conf, CONF_sshbug_winadj, Data->Bug[sbWinAdj]);
 
-   conf_set_int(conf, CONF_sshbug_oldgex2, Data->Bug[sbOldGex2]);
 
-   if (!Data->TunnelPortFwd.IsEmpty())
 
-   {
 
-     DebugAssert(!Simple);
 
-     UnicodeString TunnelPortFwd = Data->TunnelPortFwd;
 
-     while (!TunnelPortFwd.IsEmpty())
 
-     {
 
-       UnicodeString Buf = CutToChar(TunnelPortFwd, L',', true);
 
-       AnsiString Key = AnsiString(CutToChar(Buf, L'\t', true));
 
-       AnsiString Value = AnsiString(Buf);
 
-       conf_set_str_str(conf, CONF_portfwd, Key.c_str(), Value.c_str());
 
-     }
 
-     // when setting up a tunnel, do not open shell/sftp
 
-     conf_set_int(conf, CONF_ssh_no_shell, TRUE);
 
-   }
 
-   else
 
-   {
 
-     DebugAssert(Simple);
 
-     conf_set_int(conf, CONF_ssh_simple, Data->SshSimple && Simple);
 
-     if (Data->FSProtocol == fsSCPonly)
 
-     {
 
-       conf_set_int(conf, CONF_ssh_subsys, FALSE);
 
-       if (Data->Shell.IsEmpty())
 
-       {
 
-         // Following forces Putty to open default shell
 
-         // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
 
-         conf_set_str(conf, CONF_remote_cmd, "");
 
-       }
 
-       else
 
-       {
 
-         conf_set_str(conf, CONF_remote_cmd, AnsiString(Data->Shell).c_str());
 
-       }
 
-     }
 
-     else
 
-     {
 
-       if (Data->SftpServer.IsEmpty())
 
-       {
 
-         conf_set_int(conf, CONF_ssh_subsys, TRUE);
 
-         conf_set_str(conf, CONF_remote_cmd, "sftp");
 
-       }
 
-       else
 
-       {
 
-         conf_set_int(conf, CONF_ssh_subsys, FALSE);
 
-         conf_set_str(conf, CONF_remote_cmd, AnsiString(Data->SftpServer).c_str());
 
-       }
 
-       if (Data->FSProtocol != fsSFTPonly)
 
-       {
 
-         conf_set_int(conf, CONF_ssh_subsys2, FALSE);
 
-         if (Data->Shell.IsEmpty())
 
-         {
 
-           // Following forces Putty to open default shell
 
-           // see ssh.c: do_ssh2_authconn() and ssh1_protocol()
 
-           conf_set_str(conf, CONF_remote_cmd2, "");
 
-           // PuTTY ignores CONF_remote_cmd2 set to "",
 
-           // so we have to enforce it
 
-           // (CONF_force_remote_cmd2 is our config option)
 
-           conf_set_int(conf, CONF_force_remote_cmd2, 1);
 
-         }
 
-         else
 
-         {
 
-           conf_set_str(conf, CONF_remote_cmd2, AnsiString(Data->Shell).c_str());
 
-         }
 
-       }
 
-       if ((Data->FSProtocol == fsSFTPonly) && Data->SftpServer.IsEmpty())
 
-       {
 
-         // see psftp_connect() from psftp.c
 
-         conf_set_int(conf, CONF_ssh_subsys2, FALSE);
 
-         conf_set_str(conf, CONF_remote_cmd2,
 
-           "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");
 
-       }
 
-     }
 
-   }
 
-   conf_set_int(conf, CONF_connect_timeout, Data->Timeout * MSecsPerSec);
 
-   conf_set_int(conf, CONF_sndbuf, Data->SendBuf);
 
-   // permanent settings
 
-   conf_set_int(conf, CONF_nopty, TRUE);
 
-   conf_set_int(conf, CONF_tcp_keepalives, 0);
 
-   conf_set_int(conf, CONF_ssh_show_banner, TRUE);
 
-   for (int Index = 0; Index < ngsslibs; Index++)
 
-   {
 
-     conf_set_int_int(conf, CONF_ssh_gsslist, Index, gsslibkeywords[Index].v);
 
-   }
 
-   conf_set_int(conf, CONF_proxy_log_to_term, FORCE_OFF);
 
-   conf_set_int_int(conf, CONF_ssh_hklist, 0, HK_ED25519);
 
-   conf_set_int_int(conf, CONF_ssh_hklist, 1, HK_ECDSA);
 
-   conf_set_int_int(conf, CONF_ssh_hklist, 2, HK_RSA);
 
-   conf_set_int_int(conf, CONF_ssh_hklist, 3, HK_DSA);
 
-   conf_set_int_int(conf, CONF_ssh_hklist, 4, HK_WARN);
 
-   DebugAssert(HK_MAX == 5);
 
-   conf_set_str(conf, CONF_loghost, AnsiString(Data->LogicalHostName).c_str());
 
-   return conf;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::Open()
 
- {
 
-   FBackend = &ssh_backend;
 
-   ResetConnection();
 
-   FAuthenticating = false;
 
-   FAuthenticated = false;
 
-   FLastSendBufferUpdate = 0;
 
-   // do not use UTF-8 until decided otherwise (see TSCPFileSystem::DetectUtf())
 
-   FUtfStrings = false;
 
-   Active = false;
 
-   FAuthenticationLog = L"";
 
-   FNoConnectionResponse = false;
 
-   FUI->Information(LoadStr(STATUS_LOOKUPHOST), true);
 
-   try
 
-   {
 
-     char * RealHost;
 
-     FreeBackend(); // in case we are reconnecting
 
-     const char * InitError;
 
-     Conf * conf = StoreToConfig(FSessionData, Simple);
 
-     FSendBuf = FSessionData->SendBuf;
 
-     try
 
-     {
 
-       InitError = FBackend->init(this, &FBackendHandle, conf,
 
-         AnsiString(FSessionData->HostNameExpanded).c_str(), FSessionData->PortNumber, &RealHost,
 
-         (FSessionData->TcpNoDelay ? 1 : 0),
 
-         conf_get_int(conf, CONF_tcp_keepalives));
 
-     }
 
-     __finally
 
-     {
 
-       conf_free(conf);
 
-     }
 
-     sfree(RealHost);
 
-     if (InitError)
 
-     {
 
-       PuttyFatalError(InitError);
 
-     }
 
-     FUI->Information(LoadStr(STATUS_CONNECT), true);
 
-     Init();
 
-     CheckConnection(CONNECTION_FAILED);
 
-   }
 
-   catch (Exception & E)
 
-   {
 
-     if (FNoConnectionResponse && TryFtp())
 
-     {
 
-       Configuration->Usage->Inc(L"ProtocolSuggestions");
 
-       // HELP_FTP_SUGGESTION won't be used as all errors that set
 
-       // FNoConnectionResponse have already their own help keyword
 
-       FUI->FatalError(&E, LoadStr(FTP_SUGGESTION), HELP_FTP_SUGGESTION);
 
-     }
 
-     else
 
-     {
 
-       throw;
 
-     }
 
-   }
 
-   FLastDataSent = Now();
 
-   FSessionInfo.LoginTime = Now();
 
-   FAuthenticating = false;
 
-   FAuthenticated = true;
 
-   FUI->Information(LoadStr(STATUS_AUTHENTICATED), true);
 
-   ResetSessionInfo();
 
-   DebugAssert(!FSessionInfo.SshImplementation.IsEmpty());
 
-   FOpened = true;
 
-   UnicodeString SshImplementation = GetSessionInfo().SshImplementation;
 
-   if (// e.g. "OpenSSH_5.3"
 
-       (SshImplementation.Pos(L"OpenSSH") == 1) ||
 
-       // Sun SSH is based on OpenSSH (suffers the same bugs)
 
-       (SshImplementation.Pos(L"Sun_SSH") == 1))
 
-   {
 
-     FSshImplementation = sshiOpenSSH;
 
-   }
 
-   // e.g. "mod_sftp/0.9.8"
 
-   else if (SshImplementation.Pos(L"mod_sftp") == 1)
 
-   {
 
-     FSshImplementation = sshiProFTPD;
 
-   }
 
-   // e.g. "5.25 FlowSsh: Bitvise SSH Server (WinSSHD) 6.07: free only for personal non-commercial use"
 
-   else if (SshImplementation.Pos(L"FlowSsh") > 0)
 
-   {
 
-     FSshImplementation = sshiBitvise;
 
-   }
 
-   // e.g. "srtSSHServer_10.00"
 
-   else if (ContainsText(SshImplementation, L"srtSSHServer"))
 
-   {
 
-     FSshImplementation = sshiTitan;
 
-   }
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"OpenVMS"))
 
-   {
 
-     FSshImplementation = sshiOpenVMS;
 
-   }
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"CerberusFTPServer"))
 
-   {
 
-     FSshImplementation = sshiCerberus;
 
-   }
 
-   else
 
-   {
 
-     FSshImplementation = sshiUnknown;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TSecureShell::TryFtp()
 
- {
 
-   bool Result;
 
-   if (!FConfiguration->TryFtpWhenSshFails)
 
-   {
 
-     Result = false;
 
-   }
 
-   else
 
-   {
 
-     if (((FSessionData->FSProtocol != fsSFTP) && (FSessionData->FSProtocol != fsSFTPonly)) ||
 
-         (FSessionData->PortNumber != SshPortNumber) ||
 
-         FSessionData->Tunnel || (FSessionData->ProxyMethod != ::pmNone))
 
-     {
 
-       LogEvent(L"Using non-standard protocol or port, tunnel or proxy, will not knock FTP port.");
 
-       Result = false;
 
-     }
 
-     else
 
-     {
 
-       LogEvent(L"Knocking FTP port.");
 
-       SOCKET Socket = socket(AF_INET, SOCK_STREAM, 0);
 
-       Result = (Socket != INVALID_SOCKET);
 
-       if (Result)
 
-       {
 
-         LPHOSTENT HostEntry = gethostbyname(AnsiString(FSessionData->HostNameExpanded).c_str());
 
-         Result = (HostEntry != NULL);
 
-         if (Result)
 
-         {
 
-           SOCKADDR_IN Address;
 
-           memset(&Address, 0, sizeof(Address));
 
-           Address.sin_family = AF_INET;
 
-           int Port = FtpPortNumber;
 
-           Address.sin_port = htons(static_cast<short>(Port));
 
-           Address.sin_addr.s_addr = *((unsigned long *)*HostEntry->h_addr_list);
 
-           HANDLE Event = CreateEvent(NULL, false, false, NULL);
 
-           Result = (WSAEventSelect(Socket, (WSAEVENT)Event, FD_CONNECT | FD_CLOSE) != SOCKET_ERROR);
 
-           if (Result)
 
-           {
 
-             Result =
 
-               (connect(Socket, reinterpret_cast<sockaddr *>(&Address), sizeof(Address)) != SOCKET_ERROR) ||
 
-               (WSAGetLastError() == WSAEWOULDBLOCK);
 
-             if (Result)
 
-             {
 
-               Result = (WaitForSingleObject(Event, 2000) == WAIT_OBJECT_0);
 
-             }
 
-           }
 
-           CloseHandle(Event);
 
-         }
 
-         closesocket(Socket);
 
-       }
 
-       if (Result)
 
-       {
 
-         LogEvent(L"FTP port opened, will suggest using FTP protocol.");
 
-       }
 
-       else
 
-       {
 
-         LogEvent(L"FTP port did not open.");
 
-       }
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::Init()
 
- {
 
-   try
 
-   {
 
-     try
 
-     {
 
-       // Recent pscp checks FBackend->exitcode(FBackendHandle) in the loop
 
-       // (see comment in putty revision 8110)
 
-       // It seems that we do not need to do it.
 
-       while (!get_ssh_state_session(FBackendHandle))
 
-       {
 
-         if (Configuration->ActualLogProtocol >= 1)
 
-         {
 
-           LogEvent(L"Waiting for the server to continue with the initialization");
 
-         }
 
-         WaitForData();
 
-       }
 
-       // unless this is tunnel session, it must be safe to send now
 
-       DebugAssert(FBackend->sendok(FBackendHandle) || !FSessionData->TunnelPortFwd.IsEmpty());
 
-     }
 
-     catch(Exception & E)
 
-     {
 
-       if (FAuthenticating && !FAuthenticationLog.IsEmpty())
 
-       {
 
-         FUI->FatalError(&E, FMTLOAD(AUTHENTICATION_LOG, (FAuthenticationLog)));
 
-       }
 
-       else
 
-       {
 
-         throw;
 
-       }
 
-     }
 
-   }
 
-   catch(Exception & E)
 
-   {
 
-     if (FAuthenticating)
 
-     {
 
-       FUI->FatalError(&E, LoadStr(AUTHENTICATION_FAILED));
 
-     }
 
-     else
 
-     {
 
-       throw;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TSecureShell::ConvertFromPutty(const char * Str, int Length)
 
- {
 
-   int BomLength = strlen(MPEXT_BOM);
 
-   if ((Length >= BomLength) &&
 
-       (strncmp(Str, MPEXT_BOM, BomLength) == 0))
 
-   {
 
-     return UTF8ToString(Str + BomLength, Length - BomLength);
 
-   }
 
-   else
 
-   {
 
-     return AnsiToString(Str, Length);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::PuttyLogEvent(const char * AStr)
 
- {
 
-   UnicodeString Str = ConvertFromPutty(AStr, strlen(AStr));
 
-   #define SERVER_VERSION_MSG L"Server version: "
 
-   // Gross hack
 
-   if (Str.Pos(SERVER_VERSION_MSG) == 1)
 
-   {
 
-     FSessionInfo.SshVersionString = Str.SubString(wcslen(SERVER_VERSION_MSG) + 1,
 
-       Str.Length() - wcslen(SERVER_VERSION_MSG));
 
-     const wchar_t * Ptr = wcschr(FSessionInfo.SshVersionString.c_str(), L'-');
 
-     if (Ptr != NULL)
 
-     {
 
-       Ptr = wcschr(Ptr + 1, L'-');
 
-     }
 
-     FSessionInfo.SshImplementation = (Ptr != NULL) ? Ptr + 1 : L"";
 
-   }
 
-   #define FORWARDING_FAILURE_MSG L"Forwarded connection refused by server: "
 
-   else if (Str.Pos(FORWARDING_FAILURE_MSG) == 1)
 
-   {
 
-     FLastTunnelError = Str.SubString(wcslen(FORWARDING_FAILURE_MSG) + 1,
 
-       Str.Length() - wcslen(FORWARDING_FAILURE_MSG));
 
-     static const TPuttyTranslation Translation[] = {
 
-       { L"Administratively prohibited [%]", PFWD_TRANSL_ADMIN },
 
-       { L"Connect failed [%]", PFWD_TRANSL_CONNECT },
 
-     };
 
-     TranslatePuttyMessage(Translation, LENOF(Translation), FLastTunnelError);
 
-   }
 
-   LogEvent(Str);
 
- }
 
- //---------------------------------------------------------------------------
 
- TPromptKind __fastcall TSecureShell::IdentifyPromptKind(UnicodeString & Name)
 
- {
 
-   // beware of changing order
 
-   static const TPuttyTranslation NameTranslation[] = {
 
-     { L"SSH login name", USERNAME_TITLE },
 
-     { L"SSH key passphrase", PASSPHRASE_TITLE },
 
-     { L"SSH TIS authentication", SERVER_PROMPT_TITLE },
 
-     { L"SSH CryptoCard authentication", SERVER_PROMPT_TITLE },
 
-     { L"SSH server: %", SERVER_PROMPT_TITLE2 },
 
-     { L"SSH server authentication", SERVER_PROMPT_TITLE },
 
-     { L"SSH password", PASSWORD_TITLE },
 
-     { L"New SSH password", NEW_PASSWORD_TITLE },
 
-   };
 
-   int Index = TranslatePuttyMessage(NameTranslation, LENOF(NameTranslation), Name);
 
-   TPromptKind PromptKind;
 
-   if (Index == 0) // username
 
-   {
 
-     PromptKind = pkUserName;
 
-   }
 
-   else if (Index == 1) // passphrase
 
-   {
 
-     PromptKind = pkPassphrase;
 
-   }
 
-   else if (Index == 2) // TIS
 
-   {
 
-     PromptKind = pkTIS;
 
-   }
 
-   else if (Index == 3) // CryptoCard
 
-   {
 
-     PromptKind = pkCryptoCard;
 
-   }
 
-   else if ((Index == 4) || (Index == 5))
 
-   {
 
-     PromptKind = pkKeybInteractive;
 
-   }
 
-   else if (Index == 6)
 
-   {
 
-     PromptKind = pkPassword;
 
-   }
 
-   else if (Index == 7)
 
-   {
 
-     PromptKind = pkNewPassword;
 
-   }
 
-   else
 
-   {
 
-     PromptKind = pkPrompt;
 
-     DebugFail();
 
-   }
 
-   return PromptKind;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TSecureShell::PromptUser(bool /*ToServer*/,
 
-   UnicodeString AName, bool /*NameRequired*/,
 
-   UnicodeString Instructions, bool InstructionsRequired,
 
-   TStrings * Prompts, TStrings * Results)
 
- {
 
-   // there can be zero prompts!
 
-   DebugAssert(Results->Count == Prompts->Count);
 
-   UnicodeString Name = AName;
 
-   TPromptKind PromptKind = IdentifyPromptKind(Name);
 
-   const TPuttyTranslation * InstructionTranslation = NULL;
 
-   const TPuttyTranslation * PromptTranslation = NULL;
 
-   size_t PromptTranslationCount = 1;
 
-   UnicodeString PromptDesc;
 
-   if (PromptKind == pkUserName)
 
-   {
 
-     static const TPuttyTranslation UsernamePromptTranslation[] = {
 
-       { L"login as: ", USERNAME_PROMPT2 },
 
-     };
 
-     PromptTranslation = UsernamePromptTranslation;
 
-     PromptDesc = L"username";
 
-   }
 
-   else if (PromptKind == pkPassphrase)
 
-   {
 
-     static const TPuttyTranslation PassphrasePromptTranslation[] = {
 
-       { L"Passphrase for key \"%\": ", PROMPT_KEY_PASSPHRASE },
 
-     };
 
-     PromptTranslation = PassphrasePromptTranslation;
 
-     PromptDesc = L"passphrase";
 
-   }
 
-   else if (PromptKind == pkTIS)
 
-   {
 
-     static const TPuttyTranslation TISInstructionTranslation[] = {
 
-       { L"Using TIS authentication.%", TIS_INSTRUCTION },
 
-     };
 
-     static const TPuttyTranslation TISPromptTranslation[] = {
 
-       { L"Response: ", PROMPT_PROMPT },
 
-     };
 
-     InstructionTranslation = TISInstructionTranslation;
 
-     PromptTranslation = TISPromptTranslation;
 
-     PromptDesc = L"tis";
 
-   }
 
-   else if (PromptKind == pkCryptoCard)
 
-   {
 
-     static const TPuttyTranslation CryptoCardInstructionTranslation[] = {
 
-       { L"Using CryptoCard authentication.%", CRYPTOCARD_INSTRUCTION },
 
-     };
 
-     static const TPuttyTranslation CryptoCardPromptTranslation[] = {
 
-       { L"Response: ", PROMPT_PROMPT },
 
-     };
 
-     InstructionTranslation = CryptoCardInstructionTranslation;
 
-     PromptTranslation = CryptoCardPromptTranslation;
 
-     PromptDesc = L"cryptocard";
 
-   }
 
-   else if (PromptKind == pkKeybInteractive)
 
-   {
 
-     static const TPuttyTranslation KeybInteractiveInstructionTranslation[] = {
 
-       { L"Using keyboard-interactive authentication.%", KEYBINTER_INSTRUCTION },
 
-     };
 
-     static const TPuttyTranslation KeybInteractivePromptTranslation[] = {
 
-       // as used by Linux-PAM (pam_exec/pam_exec.c, libpam/pam_get_authtok.c,
 
-       // pam_unix/pam_unix_auth.c, pam_userdb/pam_userdb.c)
 
-       { L"Password: ", PASSWORD_PROMPT },
 
-     };
 
-     InstructionTranslation = KeybInteractiveInstructionTranslation;
 
-     PromptTranslation = KeybInteractivePromptTranslation;
 
-     PromptDesc = L"keyboard interactive";
 
-   }
 
-   else if (PromptKind == pkPassword)
 
-   {
 
-     DebugAssert(Prompts->Count == 1);
 
-     Prompts->Strings[0] = LoadStr(PASSWORD_PROMPT);
 
-     PromptDesc = L"password";
 
-   }
 
-   else if (PromptKind == pkNewPassword)
 
-   {
 
-     // Can be tested with WS_FTP server
 
-     static const TPuttyTranslation NewPasswordPromptTranslation[] = {
 
-       { L"Current password (blank for previously entered password): ", NEW_PASSWORD_CURRENT_PROMPT },
 
-       { L"Enter new password: ", NEW_PASSWORD_NEW_PROMPT },
 
-       { L"Confirm new password: ", NEW_PASSWORD_CONFIRM_PROMPT },
 
-     };
 
-     PromptTranslation = NewPasswordPromptTranslation;
 
-     PromptTranslationCount = LENOF(NewPasswordPromptTranslation);
 
-     PromptDesc = L"new password";
 
-   }
 
-   else
 
-   {
 
-     PromptDesc = L"unknown";
 
-     DebugFail();
 
-   }
 
-   UnicodeString InstructionsLog =
 
-     (Instructions.IsEmpty() ? UnicodeString(L"<no instructions>") : FORMAT(L"\"%s\"", (Instructions)));
 
-   UnicodeString PromptsLog =
 
-     (Prompts->Count > 0 ? FORMAT(L"\"%s\"", (Prompts->Strings[0])) : UnicodeString(L"<no prompt>")) +
 
-     (Prompts->Count > 1 ? FORMAT(L"%d more", (Prompts->Count - 1)) : UnicodeString());
 
-   LogEvent(FORMAT(L"Prompt (%s, \"%s\", %s, %s)", (PromptDesc, AName, InstructionsLog, PromptsLog)));
 
-   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++)
 
-   {
 
-     UnicodeString 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 == pkTIS) || (PromptKind == pkCryptoCard) ||
 
-       (PromptKind == pkKeybInteractive))
 
-   {
 
-     if (FSessionData->AuthKIPassword && !FSessionData->Password.IsEmpty() &&
 
-         !FStoredPasswordTriedForKI && (Prompts->Count == 1) &&
 
-         FLAGCLEAR(int(Prompts->Objects[0]), pupEcho))
 
-     {
 
-       LogEvent(L"Using stored password.");
 
-       FUI->Information(LoadStr(AUTH_PASSWORD), false);
 
-       Result = true;
 
-       Results->Strings[0] = FSessionData->Password;
 
-       FStoredPasswordTriedForKI = true;
 
-     }
 
-     else if (Instructions.IsEmpty() && !InstructionsRequired && (Prompts->Count == 0))
 
-     {
 
-       LogEvent(L"Ignoring empty SSH server authentication request");
 
-       Result = true;
 
-     }
 
-   }
 
-   else if (PromptKind == pkPassword)
 
-   {
 
-     if (!FSessionData->Password.IsEmpty() && !FStoredPasswordTried)
 
-     {
 
-       LogEvent(L"Using stored password.");
 
-       FUI->Information(LoadStr(AUTH_PASSWORD), false);
 
-       Result = true;
 
-       Results->Strings[0] = FSessionData->Password;
 
-       FStoredPasswordTried = true;
 
-     }
 
-   }
 
-   else if (PromptKind == pkPassphrase)
 
-   {
 
-     if (!FSessionData->Passphrase.IsEmpty() && !FStoredPassphraseTried)
 
-     {
 
-       LogEvent(L"Using configured passphrase.");
 
-       Result = true;
 
-       Results->Strings[0] = FSessionData->Passphrase;
 
-       FStoredPassphraseTried = true;
 
-     }
 
-   }
 
-   if (!Result)
 
-   {
 
-     Result = FUI->PromptUser(FSessionData,
 
-       PromptKind, Name, Instructions, Prompts, Results);
 
-     if (Result)
 
-     {
 
-       if ((Prompts->Count >= 1) &&
 
-           (FLAGSET(int(Prompts->Objects[0]), pupEcho) || Configuration->LogSensitive))
 
-       {
 
-         LogEvent(FORMAT(L"Response: \"%s\"", (Results->Strings[0])));
 
-       }
 
-       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(ConvertFromPutty(Data, Length), L'\r');
 
-   UnicodeString Line;
 
-   // Do we have at least one complete line in std error cache?
 
-   while (FCWriteTemp.Pos(L"\n") > 0)
 
-   {
 
-     UnicodeString Line = CutToChar(FCWriteTemp, L'\n', false);
 
-     FLog->Add(llStdError, Line);
 
-     if (FAuthenticating)
 
-     {
 
-       TranslateAuthenticationMessage(Line);
 
-       FAuthenticationLog += (FAuthenticationLog.IsEmpty() ? L"" : L"\n") + Line;
 
-     }
 
-     FUI->Information(Line, false);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::RegisterReceiveHandler(TNotifyEvent Handler)
 
- {
 
-   DebugAssert(FOnReceive == NULL);
 
-   FOnReceive = Handler;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::UnregisterReceiveHandler(TNotifyEvent Handler)
 
- {
 
-   DebugAssert(FOnReceive == Handler);
 
-   DebugUsedParam(Handler);
 
-   FOnReceive = NULL;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::FromBackend(bool IsStdErr, const unsigned char * Data, int Length)
 
- {
 
-   // Note that we do not apply ConvertFromPutty to Data yet (as opposite to CWrite).
 
-   // as there's no use for this atm.
 
-   CheckConnection();
 
-   if (Configuration->ActualLogProtocol >= 1)
 
-   {
 
-     LogEvent(FORMAT(L"Received %u bytes (%d)", (Length, int(IsStdErr))));
 
-   }
 
-   // Following is taken from scp.c from_backend() and modified
 
-   if (IsStdErr)
 
-   {
 
-     AddStdError(ConvertInput(RawByteString(reinterpret_cast<const char *>(Data), Length)));
 
-   }
 
-   else
 
-   {
 
-     const unsigned char *p = 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;
 
-       memmove(OutPtr, p, Used);
 
-       OutPtr += Used; OutLen -= Used;
 
-       p += Used; Len -= Used;
 
-     }
 
-     if (Len > 0)
 
-     {
 
-       if (PendSize < PendLen + Len)
 
-       {
 
-         PendSize = PendLen + Len + 4096;
 
-         Pending = (unsigned char *)
 
-           (Pending ? srealloc(Pending, PendSize) : smalloc(PendSize));
 
-         if (!Pending) FatalError(L"Out of memory");
 
-       }
 
-       memmove(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(unsigned char *& Buf, int Len)
 
- {
 
-   bool Result = (int(PendLen) >= Len);
 
-   if (Result)
 
-   {
 
-     Buf = Pending;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- Integer __fastcall TSecureShell::Receive(unsigned 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;
 
-         }
 
-         memmove(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->ActualLogProtocol >= 1)
 
-         {
 
-           LogEvent(FORMAT(L"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->ActualLogProtocol >= 1)
 
-   {
 
-     LogEvent(FORMAT(L"Read %u bytes (%d pending)",
 
-       (static_cast<int>(Len), static_cast<int>(PendLen))));
 
-   }
 
-   return Len;
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TSecureShell::ReceiveLine()
 
- {
 
-   unsigned Index;
 
-   RawByteString 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(reinterpret_cast<unsigned char *>(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)
 
-     {
 
-       unsigned char Ch;
 
-       Receive(&Ch, 1);
 
-       Line += static_cast<char>(Ch);
 
-       EOL = (static_cast<char>(Ch) == '\n');
 
-     }
 
-   }
 
-   while (!EOL);
 
-   // We don't want end-of-line character
 
-   Line.SetLength(Line.Length()-1);
 
-   UnicodeString Result = ConvertInput(Line);
 
-   CaptureOutput(llOutput, Result);
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TSecureShell::ConvertInput(const RawByteString & Input)
 
- {
 
-   UnicodeString Result;
 
-   if (UtfStrings)
 
-   {
 
-     Result = UTF8ToString(Input);
 
-   }
 
-   else
 
-   {
 
-     Result = AnsiToString(Input);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::SendSpecial(int Code)
 
- {
 
-   LogEvent(FORMAT(L"Sending special code: %d", (Code)));
 
-   CheckConnection();
 
-   FBackend->special(FBackendHandle, (Telnet_Special)Code);
 
-   CheckConnection();
 
-   FLastDataSent = Now();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::SendEOF()
 
- {
 
-   SendSpecial(TS_EOF);
 
- }
 
- //---------------------------------------------------------------------------
 
- unsigned int __fastcall TSecureShell::TimeoutPrompt(TQueryParamsTimerEvent PoolEvent)
 
- {
 
-   FWaiting++;
 
-   unsigned int Answer;
 
-   try
 
-   {
 
-     TQueryParams Params(qpFatalAbort | qpAllowContinueOnError | qpIgnoreAbort);
 
-     Params.HelpKeyword = HELP_MESSAGE_HOST_IS_NOT_COMMUNICATING;
 
-     Params.Timer = 500;
 
-     Params.TimerEvent = PoolEvent;
 
-     Params.TimerMessage = MainInstructionsFirstParagraph(FMTLOAD(TIMEOUT_STILL_WAITING3, (FSessionData->Timeout)));
 
-     Params.TimerAnswers = qaAbort;
 
-     Params.TimerQueryType = qtInformation;
 
-     if (FConfiguration->SessionReopenAutoStall > 0)
 
-     {
 
-       Params.Timeout = FConfiguration->SessionReopenAutoStall;
 
-       Params.TimeoutAnswer = qaAbort;
 
-     }
 
-     Answer = FUI->QueryUser(MainInstructions(FMTLOAD(CONFIRM_PROLONG_TIMEOUT3, (FSessionData->Timeout))),
 
-       NULL, qaRetry | qaAbort, &Params);
 
-   }
 
-   __finally
 
-   {
 
-     FWaiting--;
 
-   }
 
-   return Answer;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::SendBuffer(unsigned int & Result)
 
- {
 
-   // for comments see PoolForData
 
-   if (!Active)
 
-   {
 
-     Result = qaRetry;
 
-   }
 
-   else
 
-   {
 
-     try
 
-     {
 
-       if (FBackend->sendbuffer(FBackendHandle) <= MAX_BUFSIZE)
 
-       {
 
-         Result = qaOK;
 
-       }
 
-     }
 
-     catch(...)
 
-     {
 
-       Result = qaRetry;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::DispatchSendBuffer(int BufSize)
 
- {
 
-   TDateTime Start = Now();
 
-   do
 
-   {
 
-     CheckConnection();
 
-     if (Configuration->ActualLogProtocol >= 1)
 
-     {
 
-       LogEvent(FORMAT(L"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->ActualLogProtocol >= 1)
 
-     {
 
-       LogEvent(FORMAT(L"There are %u bytes remaining in the send buffer", (BufSize)));
 
-     }
 
-     if (Now() - Start > FSessionData->TimeoutDT)
 
-     {
 
-       LogEvent(L"Waiting for dispatching send buffer timed out, asking user what to do.");
 
-       unsigned int Answer = TimeoutPrompt(SendBuffer);
 
-       switch (Answer)
 
-       {
 
-         case qaRetry:
 
-           Start = Now();
 
-           break;
 
-         case qaOK:
 
-           BufSize = 0;
 
-           break;
 
-         default:
 
-           DebugFail();
 
-           // fallthru
 
-         case qaAbort:
 
-           FatalError(MainInstructions(LoadStr(USER_TERMINATED)));
 
-           break;
 
-       }
 
-     }
 
-   }
 
-   while (BufSize > MAX_BUFSIZE);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::Send(const unsigned char * Buf, Integer Len)
 
- {
 
-   CheckConnection();
 
-   int BufSize = FBackend->send(FBackendHandle, const_cast<char *>(reinterpret_cast<const char *>(Buf)), Len);
 
-   if (Configuration->ActualLogProtocol >= 1)
 
-   {
 
-     LogEvent(FORMAT(L"Sent %u bytes", (static_cast<int>(Len))));
 
-     LogEvent(FORMAT(L"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);
 
-   if (BufSize > MAX_BUFSIZE)
 
-   {
 
-     DispatchSendBuffer(BufSize);
 
-   }
 
-   CheckConnection();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::SendNull()
 
- {
 
-   LogEvent(L"Sending NULL.");
 
-   unsigned char Null = 0;
 
-   Send(&Null, 1);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::SendLine(const UnicodeString & Line)
 
- {
 
-   CheckConnection();
 
-   RawByteString Buf;
 
-   if (UtfStrings)
 
-   {
 
-     Buf = RawByteString(UTF8String(Line));
 
-   }
 
-   else
 
-   {
 
-     Buf = RawByteString(AnsiString(Line));
 
-   }
 
-   Buf += "\n";
 
-   FLog->Add(llInput, Line);
 
-   Send(reinterpret_cast<const unsigned char *>(Buf.c_str()), Buf.Length());
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TSecureShell::TranslatePuttyMessage(
 
-   const TPuttyTranslation * Translation, size_t Count, UnicodeString & Message,
 
-   UnicodeString * HelpKeyword)
 
- {
 
-   int Result = -1;
 
-   for (unsigned int Index = 0; Index < Count; Index++)
 
-   {
 
-     const wchar_t * Original = Translation[Index].Original;
 
-     const wchar_t * Div = wcschr(Original, L'%');
 
-     if (Div == NULL)
 
-     {
 
-       if (wcscmp(Message.c_str(), Original) == 0)
 
-       {
 
-         Message = LoadStr(Translation[Index].Translation);
 
-         Result = int(Index);
 
-         break;
 
-       }
 
-     }
 
-     else
 
-     {
 
-       size_t OriginalLen = wcslen(Original);
 
-       size_t PrefixLen = Div - Original;
 
-       size_t SuffixLen = OriginalLen - PrefixLen - 1;
 
-       if (((size_t)Message.Length() >= OriginalLen - 1) &&
 
-           (wcsncmp(Message.c_str(), Original, PrefixLen) == 0) &&
 
-           (wcsncmp(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;
 
-       }
 
-     }
 
-   }
 
-   if ((HelpKeyword != NULL) && (Result >= 0))
 
-   {
 
-     *HelpKeyword = Translation[Result].HelpKeyword;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TSecureShell::TranslateAuthenticationMessage(
 
-   UnicodeString & Message, UnicodeString * HelpKeyword)
 
- {
 
-   static const TPuttyTranslation Translation[] = {
 
-     { L"Using username \"%\".", AUTH_TRANSL_USERNAME },
 
-     { L"Using keyboard-interactive authentication.", AUTH_TRANSL_KEYB_INTER }, // not used anymore
 
-     { L"Authenticating with public key \"%\" from agent", AUTH_TRANSL_PUBLIC_KEY_AGENT },
 
-     { L"Authenticating with public key \"%\"", AUTH_TRANSL_PUBLIC_KEY },
 
-     { L"Authenticated using RSA key \"%\" from agent", AUTH_TRANSL_PUBLIC_KEY_AGENT },
 
-     { L"Wrong passphrase", AUTH_TRANSL_WRONG_PASSPHRASE },
 
-     { L"Wrong passphrase.", AUTH_TRANSL_WRONG_PASSPHRASE },
 
-     { L"Access denied", AUTH_TRANSL_ACCESS_DENIED },
 
-     { L"Trying public key authentication.", AUTH_TRANSL_TRY_PUBLIC_KEY },
 
-     { L"Server refused our public key.", AUTH_TRANSL_KEY_REFUSED, HELP_AUTH_TRANSL_KEY_REFUSED }, // help mapping probably never used
 
-     { L"Server refused our key", AUTH_TRANSL_KEY_REFUSED, HELP_AUTH_TRANSL_KEY_REFUSED }
 
-   };
 
-   int Result = TranslatePuttyMessage(Translation, LENOF(Translation), Message, HelpKeyword);
 
-   if ((Result == 2) || (Result == 3) || (Result == 4))
 
-   {
 
-     FCollectPrivateKeyUsage = true;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::AddStdError(UnicodeString Str)
 
- {
 
-   FStdError += Str;
 
-   Integer P;
 
-   Str = DeleteChar(Str, L'\r');
 
-   // We send only whole line at once to log, so we have to cache
 
-   // incoming std error data
 
-   FStdErrorTemp += Str;
 
-   UnicodeString Line;
 
-   // Do we have at least one complete line in std error cache?
 
-   while ((P = FStdErrorTemp.Pos(L"\n")) > 0)
 
-   {
 
-     Line = FStdErrorTemp.SubString(1, P-1);
 
-     FStdErrorTemp.Delete(1, P);
 
-     AddStdErrorLine(Line);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::AddStdErrorLine(const UnicodeString & Str)
 
- {
 
-   if (FAuthenticating)
 
-   {
 
-     FAuthenticationLog += (FAuthenticationLog.IsEmpty() ? L"" : L"\n") + Str;
 
-   }
 
-   CaptureOutput(llStdError, Str);
 
- }
 
- //---------------------------------------------------------------------------
 
- const UnicodeString & __fastcall TSecureShell::GetStdError()
 
- {
 
-   return FStdError;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::ClearStdError()
 
- {
 
-   // Flush std error cache
 
-   if (!FStdErrorTemp.IsEmpty())
 
-   {
 
-     if (FAuthenticating)
 
-     {
 
-       FAuthenticationLog +=
 
-         (FAuthenticationLog.IsEmpty() ? L"" : L"\n") + FStdErrorTemp;
 
-     }
 
-     CaptureOutput(llStdError, FStdErrorTemp);
 
-     FStdErrorTemp = L"";
 
-   }
 
-   FStdError = L"";
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::CaptureOutput(TLogLineType Type,
 
-   const UnicodeString & Line)
 
- {
 
-   if (FOnCaptureOutput != NULL)
 
-   {
 
-     FOnCaptureOutput(Line, (Type == llStdError) ? cotError : cotOutput);
 
-   }
 
-   FLog->Add(Type, Line);
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TSecureShell::TranslateErrorMessage(
 
-   UnicodeString & Message, UnicodeString * HelpKeyword)
 
- {
 
-   static const TPuttyTranslation Translation[] = {
 
-     { L"Server unexpectedly closed network connection", UNEXPECTED_CLOSE_ERROR, HELP_UNEXPECTED_CLOSE_ERROR },
 
-     { L"Network error: Connection refused", NET_TRANSL_REFUSED2, HELP_NET_TRANSL_REFUSED },
 
-     { L"Network error: Connection reset by peer", NET_TRANSL_RESET, HELP_NET_TRANSL_RESET },
 
-     { L"Network error: Connection timed out", NET_TRANSL_TIMEOUT2, HELP_NET_TRANSL_TIMEOUT },
 
-     { L"Network error: No route to host", NET_TRANSL_NO_ROUTE2, HELP_NET_TRANSL_NO_ROUTE },
 
-     { L"Network error: Software caused connection abort", NET_TRANSL_CONN_ABORTED, HELP_NET_TRANSL_CONN_ABORTED },
 
-     { L"Host does not exist", NET_TRANSL_HOST_NOT_EXIST2, HELP_NET_TRANSL_HOST_NOT_EXIST },
 
-     { L"Incoming packet was garbled on decryption", NET_TRANSL_PACKET_GARBLED, HELP_NET_TRANSL_PACKET_GARBLED },
 
-   };
 
-   int Index = TranslatePuttyMessage(Translation, LENOF(Translation), Message, HelpKeyword);
 
-   if ((Index == 0) || (Index == 1) || (Index == 2) || (Index == 3))
 
-   {
 
-     FNoConnectionResponse = true;
 
-   }
 
-   Message = ReplaceStr(Message, L"%HOST%", FSessionData->HostNameExpanded);
 
-   return Index;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::PuttyFatalError(UnicodeString Error)
 
- {
 
-   UnicodeString HelpKeyword;
 
-   TranslateErrorMessage(Error, &HelpKeyword);
 
-   FatalError(Error, HelpKeyword);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::FatalError(UnicodeString Error, UnicodeString HelpKeyword)
 
- {
 
-   FUI->FatalError(NULL, Error, HelpKeyword);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall inline TSecureShell::LogEvent(const UnicodeString & 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->ActualLogProtocol >= 2)
 
-   {
 
-     LogEvent(FORMAT(L"Selecting events %d for socket %d", (int(Events), int(Socket))));
 
-   }
 
-   if (WSAEventSelect(Socket, (WSAEVENT)Event, Events) == SOCKET_ERROR)
 
-   {
 
-     if (Configuration->ActualLogProtocol >= 2)
 
-     {
 
-       LogEvent(FORMAT(L"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 eventually:
 
-     // When TCP connection fails, PuTTY does not release the memory allocated 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 calls do_select,
 
-     // so we must filter that out.
 
-   }
 
-   else
 
-   {
 
-     DebugAssert(value);
 
-     DebugAssert((FActive && (FSocket == value)) || (!FActive && Startup));
 
-     // filter our "local proxy" connection, which have no socket
 
-     if (value != INVALID_SOCKET)
 
-     {
 
-       SocketEventSelect(value, FSocketEvent, Startup);
 
-     }
 
-     else
 
-     {
 
-       DebugAssert(FSessionData->ProxyMethod == pmCmd);
 
-     }
 
-     if (Startup)
 
-     {
 
-       FSocket = value;
 
-       FActive = true;
 
-     }
 
-     else
 
-     {
 
-       FSocket = INVALID_SOCKET;
 
-       Discard();
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::UpdatePortFwdSocket(SOCKET value, bool Startup)
 
- {
 
-   if (Configuration->ActualLogProtocol >= 2)
 
-   {
 
-     LogEvent(FORMAT(L"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;
 
-   FOpened = false;
 
-   if (WasActive)
 
-   {
 
-     FUI->Closed();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::Close()
 
- {
 
-   LogEvent(L"Closing connection.");
 
-   DebugAssert(FActive);
 
-   // this is particularly necessary when using local proxy command
 
-   // (e.g. plink), otherwise it hangs in sk_localproxy_close
 
-   SendEOF();
 
-   FreeBackend();
 
-   Discard();
 
- }
 
- //---------------------------------------------------------------------------
 
- void inline __fastcall TSecureShell::CheckConnection(int Message)
 
- {
 
-   if (!FActive || get_ssh_state_closed(FBackendHandle))
 
-   {
 
-     UnicodeString Str;
 
-     UnicodeString HelpKeyword;
 
-     if (Message >= 0)
 
-     {
 
-       Str = LoadStr(Message);
 
-     }
 
-     else
 
-     {
 
-       Str = LoadStr(NOT_CONNECTED);
 
-       HelpKeyword = HELP_NOT_CONNECTED;
 
-     }
 
-     Str = MainInstructions(Str);
 
-     int ExitCode = get_ssh_exitcode(FBackendHandle);
 
-     if (ExitCode >= 0)
 
-     {
 
-       Str += L" " + FMTLOAD(SSH_EXITCODE, (ExitCode));
 
-     }
 
-     FatalError(Str, HelpKeyword);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::PoolForData(WSANETWORKEVENTS & Events, unsigned int & Result)
 
- {
 
-   if (!Active)
 
-   {
 
-     // see comment below
 
-     Result = qaRetry;
 
-   }
 
-   else
 
-   {
 
-     try
 
-     {
 
-       if (Configuration->ActualLogProtocol >= 2)
 
-       {
 
-         LogEvent(L"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 coming 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(L"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->ActualLogProtocol >= 2)
 
-     {
 
-       LogEvent(L"Looking for incoming data");
 
-     }
 
-     IncomingData = EventSelectLoop(FSessionData->Timeout * MSecsPerSec, true, NULL);
 
-     if (!IncomingData)
 
-     {
 
-       DebugAssert(FWaitingForData == 0);
 
-       TAutoNestingCounter NestingCounter(FWaitingForData);
 
-       WSANETWORKEVENTS Events;
 
-       memset(&Events, 0, sizeof(Events));
 
-       TPoolForDataEvent Event(this, Events);
 
-       LogEvent(L"Waiting for data timed out, asking user what to do.");
 
-       unsigned int Answer = TimeoutPrompt(Event.PoolForData);
 
-       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:
 
-           DebugFail();
 
-           // fallthru
 
-         case qaAbort:
 
-           FatalError(MainInstructions(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->ActualLogProtocol >= 2)
 
-   {
 
-     LogEvent(FORMAT(L"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->ActualLogProtocol >= 2)
 
-     {
 
-       LogEvent(FORMAT(L"Enumerated %d network events making %d cumulative events for socket %d",
 
-         (int(AEvents.lNetworkEvents), int(Events.lNetworkEvents), int(Socket))));
 
-     }
 
-   }
 
-   else
 
-   {
 
-     if (Configuration->ActualLogProtocol >= 2)
 
-     {
 
-       LogEvent(FORMAT(L"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 wchar_t * Desc; } EventTypes[] =
 
-   {
 
-     { FD_READ_BIT, FD_READ, L"read" },
 
-     { FD_WRITE_BIT, FD_WRITE, L"write" },
 
-     { FD_OOB_BIT, FD_OOB, L"oob" },
 
-     { FD_ACCEPT_BIT, FD_ACCEPT, L"accept" },
 
-     { FD_CONNECT_BIT, FD_CONNECT, L"connect" },
 
-     { FD_CLOSE_BIT, FD_CLOSE, L"close" },
 
-   };
 
-   for (unsigned int Event = 0; Event < LENOF(EventTypes); Event++)
 
-   {
 
-     if (FLAGSET(Events.lNetworkEvents, EventTypes[Event].Mask))
 
-     {
 
-       int Err = Events.iErrorCode[EventTypes[Event].Bit];
 
-       if (Configuration->ActualLogProtocol >= 2)
 
-       {
 
-         LogEvent(FORMAT(L"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->ActualLogProtocol >= 2)
 
-     {
 
-       LogEvent(L"Looking for network events");
 
-     }
 
-     unsigned int TicksBefore = GetTickCount();
 
-     int HandleCount;
 
-     // note that this returns all handles, not only the session-related handles
 
-     HANDLE * Handles = handle_get_events(&HandleCount);
 
-     try
 
-     {
 
-       Handles = sresize(Handles, HandleCount + 1, HANDLE);
 
-       Handles[HandleCount] = FSocketEvent;
 
-       unsigned int Timeout = MSec;
 
-       if (toplevel_callback_pending())
 
-       {
 
-         Timeout = 0;
 
-       }
 
-       unsigned int WaitResult;
 
-       do
 
-       {
 
-         unsigned int TimeoutStep = std::min(GUIUpdateInterval, Timeout);
 
-         Timeout -= TimeoutStep;
 
-         WaitResult = WaitForMultipleObjects(HandleCount + 1, Handles, FALSE, TimeoutStep);
 
-         FUI->ProcessGUI();
 
-       } while ((WaitResult == WAIT_TIMEOUT) && (Timeout > 0));
 
-       if (WaitResult < WAIT_OBJECT_0 + HandleCount)
 
-       {
 
-         if (handle_got_event(Handles[WaitResult - WAIT_OBJECT_0]))
 
-         {
 
-           Result = true;
 
-         }
 
-       }
 
-       else if (WaitResult == WAIT_OBJECT_0 + HandleCount)
 
-       {
 
-         if (Configuration->ActualLogProtocol >= 1)
 
-         {
 
-           LogEvent(L"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++;
 
-           }
 
-         }
 
-       }
 
-       else if (WaitResult == WAIT_TIMEOUT)
 
-       {
 
-         if (Configuration->ActualLogProtocol >= 2)
 
-         {
 
-           LogEvent(L"Timeout waiting for network events");
 
-         }
 
-         MSec = 0;
 
-       }
 
-       else
 
-       {
 
-         if (Configuration->ActualLogProtocol >= 2)
 
-         {
 
-           LogEvent(FORMAT(L"Unknown waiting result %d", (int(WaitResult))));
 
-         }
 
-         MSec = 0;
 
-       }
 
-     }
 
-     __finally
 
-     {
 
-       sfree(Handles);
 
-     }
 
-     run_toplevel_callbacks();
 
-     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;
 
-       }
 
-     }
 
-     if ((FSendBuf > 0) && (TicksAfter - FLastSendBufferUpdate >= 1000))
 
-     {
 
-       DWORD BufferLen = 0;
 
-       DWORD OutLen = 0;
 
-       if (WSAIoctl(FSocket, SIO_IDEAL_SEND_BACKLOG_QUERY, NULL, 0, &BufferLen, sizeof(BufferLen), &OutLen, 0, 0) == 0)
 
-       {
 
-         DebugAssert(OutLen == sizeof(BufferLen));
 
-         if (FSendBuf < static_cast<int>(BufferLen))
 
-         {
 
-           LogEvent(FORMAT(L"Increasing send buffer from %d to %d", (FSendBuf, static_cast<int>(BufferLen))));
 
-           FSendBuf = BufferLen;
 
-           setsockopt(FSocket, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char *>(&BufferLen), sizeof(BufferLen));
 
-         }
 
-       }
 
-       FLastSendBufferUpdate = TicksAfter;
 
-     }
 
-   }
 
-   while (ReadEventRequired && (MSec > 0) && !Result);
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::Idle(unsigned int MSec)
 
- {
 
-   noise_regular();
 
-   call_ssh_timer(FBackendHandle);
 
-   // if we are actively waiting for data in WaitForData,
 
-   // do not read here, otherwise we swallow read event and never wake
 
-   if (FWaitingForData <= 0)
 
-   {
 
-     EventSelectLoop(MSec, false, NULL);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::KeepAlive()
 
- {
 
-   if (FActive && (FWaiting == 0))
 
-   {
 
-     LogEvent(L"Sending null packet to keep session alive.");
 
-     SendSpecial(TS_PING);
 
-   }
 
-   else
 
-   {
 
-     // defer next keepalive attempt
 
-     FLastDataSent = Now();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- unsigned long __fastcall TSecureShell::MaxPacketSize()
 
- {
 
-   if (!FSessionInfoValid)
 
-   {
 
-     UpdateSessionInfo();
 
-   }
 
-   if (FSshVersion == 1)
 
-   {
 
-     return 0;
 
-   }
 
-   else
 
-   {
 
-     if (FMaxPacketSize == NULL)
 
-     {
 
-       FMaxPacketSize = ssh2_remmaxpkt(FBackendHandle);
 
-     }
 
-     return *FMaxPacketSize;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TSecureShell::FuncToCompression(
 
-   int SshVersion, const void * Compress) const
 
- {
 
-   enum TCompressionType { ctNone, ctZLib };
 
-   if (SshVersion == 1)
 
-   {
 
-     return get_ssh1_compressing(FBackendHandle) ? L"ZLib" : L"";
 
-   }
 
-   else
 
-   {
 
-     return (ssh_compress *)Compress == &ssh_zlib ? L"ZLib" : L"";
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TCipher __fastcall TSecureShell::FuncToSsh1Cipher(const void * Cipher)
 
- {
 
-   const ssh_cipher *CipherFuncs[] =
 
-     {&ssh_3des, &ssh_des, &ssh_blowfish_ssh1};
 
-   const TCipher TCiphers[] = {cip3DES, cipDES, cipBlowfish};
 
-   DebugAssert(LENOF(CipherFuncs) == LENOF(TCiphers));
 
-   TCipher Result = cipWarn;
 
-   for (unsigned int Index = 0; Index < LENOF(TCiphers); Index++)
 
-   {
 
-     if ((ssh_cipher *)Cipher == CipherFuncs[Index])
 
-     {
 
-       Result = TCiphers[Index];
 
-     }
 
-   }
 
-   DebugAssert(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, &ssh2_ccp};
 
-   const TCipher TCiphers[] = {cip3DES, cipDES, cipAES, cipBlowfish, cipArcfour, cipChaCha20};
 
-   DebugAssert(LENOF(CipherFuncs) == LENOF(TCiphers));
 
-   TCipher Result = cipWarn;
 
-   for (unsigned 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];
 
-       }
 
-     }
 
-   }
 
-   DebugAssert(Result != cipWarn);
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TSecureShell::FormatKeyStr(UnicodeString KeyStr)
 
- {
 
-   int Index = 1;
 
-   int Digits = 0;
 
-   while (Index <= KeyStr.Length())
 
-   {
 
-     if (IsHex(KeyStr[Index]))
 
-     {
 
-       Digits++;
 
-       if (Digits >= 16)
 
-       {
 
-         KeyStr.Insert(L" ", Index + 1);
 
-         Index++;
 
-         Digits = 0;
 
-       }
 
-     }
 
-     else
 
-     {
 
-       Digits = 0;
 
-     }
 
-     Index++;
 
-   }
 
-   return KeyStr;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::GetRealHost(UnicodeString & Host, int & Port)
 
- {
 
-   if (FSessionData->Tunnel)
 
-   {
 
-     // Now that we set the CONF_loghost, the hostname is correct already
 
-     Host = FSessionData->OrigHostName;
 
-     Port = FSessionData->OrigPortNumber;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TSecureShell::RetrieveHostKey(UnicodeString Host, int Port, const UnicodeString KeyType)
 
- {
 
-   AnsiString AnsiStoredKeys;
 
-   AnsiStoredKeys.SetLength(10240);
 
-   UnicodeString Result;
 
-   if (retrieve_host_key(AnsiString(Host).c_str(), Port, AnsiString(KeyType).c_str(),
 
-         AnsiStoredKeys.c_str(), AnsiStoredKeys.Length()) == 0)
 
-   {
 
-     PackStr(AnsiStoredKeys);
 
-     Result = UnicodeString(AnsiStoredKeys);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::VerifyHostKey(UnicodeString Host, int Port,
 
-   const UnicodeString KeyType, UnicodeString KeyStr, UnicodeString Fingerprint)
 
- {
 
-   LogEvent(FORMAT(L"Verifying host key %s %s with fingerprint %s", (KeyType, FormatKeyStr(KeyStr), Fingerprint)));
 
-   GotHostKey();
 
-   DebugAssert(KeyStr.Pos(HostKeyDelimiter) == 0);
 
-   GetRealHost(Host, Port);
 
-   FSessionInfo.HostKeyFingerprint = Fingerprint;
 
-   if (FSessionData->FingerprintScan)
 
-   {
 
-     Abort();
 
-   }
 
-   UnicodeString NormalizedFingerprint = NormalizeFingerprint(Fingerprint);
 
-   bool Result = false;
 
-   UnicodeString StoredKeys = RetrieveHostKey(Host, Port, KeyType);
 
-   UnicodeString Buf = StoredKeys;
 
-   while (!Result && !Buf.IsEmpty())
 
-   {
 
-     UnicodeString StoredKey = CutToChar(Buf, HostKeyDelimiter, false);
 
-     // skip leading ECDH subtype identification
 
-     int P = StoredKey.Pos(L",");
 
-     // start from beginning or after the comma, if there 's any
 
-     bool Fingerprint = (StoredKey.SubString(P + 1, 2) != L"0x");
 
-     // it's probably a fingerprint (stored by TSessionData::CacheHostKey)
 
-     UnicodeString NormalizedExpectedKey;
 
-     if (Fingerprint)
 
-     {
 
-       NormalizedExpectedKey = NormalizeFingerprint(StoredKey);
 
-     }
 
-     if ((!Fingerprint && (StoredKey == KeyStr)) ||
 
-         (Fingerprint && (NormalizedExpectedKey == NormalizedFingerprint)))
 
-     {
 
-       LogEvent(L"Host key matches cached key");
 
-       Result = true;
 
-     }
 
-     else
 
-     {
 
-       UnicodeString FormattedKey = Fingerprint ? StoredKey : FormatKeyStr(StoredKey);
 
-       LogEvent(FORMAT(L"Host key does not match cached key %s", (FormattedKey)));
 
-     }
 
-   }
 
-   bool ConfiguredKeyNotMatch = false;
 
-   if (!Result && !FSessionData->HostKey.IsEmpty() &&
 
-       (StoredKeys.IsEmpty() || FSessionData->OverrideCachedHostKey))
 
-   {
 
-     UnicodeString Buf = FSessionData->HostKey;
 
-     while (!Result && !Buf.IsEmpty())
 
-     {
 
-       UnicodeString ExpectedKey = CutToChar(Buf, HostKeyDelimiter, false);
 
-       UnicodeString NormalizedExpectedKey = NormalizeFingerprint(ExpectedKey);
 
-       if (ExpectedKey == L"*")
 
-       {
 
-         UnicodeString Message = LoadStr(ANY_HOSTKEY);
 
-         FUI->Information(Message, true);
 
-         FLog->Add(llException, Message);
 
-         Result = true;
 
-       }
 
-       else if (NormalizedExpectedKey == NormalizedFingerprint)
 
-       {
 
-         LogEvent(L"Host key matches configured key");
 
-         Result = true;
 
-       }
 
-       else
 
-       {
 
-         LogEvent(FORMAT(L"Host key does not match configured key %s", (ExpectedKey)));
 
-       }
 
-     }
 
-     if (!Result)
 
-     {
 
-       ConfiguredKeyNotMatch = true;
 
-     }
 
-   }
 
-   if (!Result)
 
-   {
 
-     bool Verified;
 
-     if (ConfiguredKeyNotMatch || Configuration->DisableAcceptingHostKeys)
 
-     {
 
-       Verified = false;
 
-     }
 
-     // no point offering manual verification, if we cannot persist the verified key
 
-     else if (!Configuration->Persistent && Configuration->Scripting)
 
-     {
 
-       Verified = false;
 
-     }
 
-     else
 
-     {
 
-       // We should not offer caching if !Configuration->Persistent,
 
-       // but as scripting mode is handled earlier and in GUI it hardly happens,
 
-       // it's a small issue.
 
-       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(qpWaitInBatch);
 
-       Params.NoBatchAnswers = qaYes | qaRetry | qaSkip | qaOK;
 
-       Params.HelpKeyword = (Unknown ? HELP_UNKNOWN_KEY : HELP_DIFFERENT_KEY);
 
-       Params.Aliases = Aliases;
 
-       Params.AliasesCount = AliasesCount;
 
-       UnicodeString Message = FMTLOAD((Unknown ? UNKNOWN_KEY3 : DIFFERENT_KEY4), (KeyType, Fingerprint));
 
-       if (Configuration->Scripting)
 
-       {
 
-         AddToList(Message, LoadStr(SCRIPTING_USE_HOSTKEY), L"\n");
 
-       }
 
-       unsigned int R =
 
-         FUI->QueryUser(Message, NULL, Answers, &Params, qtWarning);
 
-       switch (R) {
 
-         case qaOK:
 
-           DebugAssert(!Unknown);
 
-           KeyStr = (StoredKeys + HostKeyDelimiter + KeyStr);
 
-           // fall thru
 
-         case qaYes:
 
-           store_host_key(AnsiString(Host).c_str(), Port, AnsiString(KeyType).c_str(), AnsiString(KeyStr).c_str());
 
-           Verified = true;
 
-           break;
 
-         case qaCancel:
 
-           Verified = false;
 
-           break;
 
-         default:
 
-           Verified = true;
 
-           break;
 
-       }
 
-     }
 
-     if (!Verified)
 
-     {
 
-       Configuration->Usage->Inc(L"HostNotVerified");
 
-       UnicodeString Message;
 
-       if (ConfiguredKeyNotMatch)
 
-       {
 
-         Message = FMTLOAD(CONFIGURED_KEY_NOT_MATCH, (FSessionData->HostKey));
 
-       }
 
-       else if (!Configuration->Persistent && Configuration->Scripting)
 
-       {
 
-         Message = LoadStr(HOSTKEY_NOT_CONFIGURED);
 
-       }
 
-       else
 
-       {
 
-         Message = LoadStr(KEY_NOT_VERIFIED);
 
-       }
 
-       Exception * E = new Exception(MainInstructions(Message));
 
-       try
 
-       {
 
-         FUI->FatalError(E, FMTLOAD(HOSTKEY, (Fingerprint)));
 
-       }
 
-       __finally
 
-       {
 
-         delete E;
 
-       }
 
-     }
 
-   }
 
-   Configuration->RememberLastFingerprint(FSessionData->SiteKey, SshFingerprintType, Fingerprint);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TSecureShell::HaveHostKey(UnicodeString Host, int Port, const UnicodeString KeyType)
 
- {
 
-   // Return true, if we have any host key fingerprint of a particular type
 
-   GetRealHost(Host, Port);
 
-   UnicodeString StoredKeys = RetrieveHostKey(Host, Port, KeyType);
 
-   bool Result = !StoredKeys.IsEmpty();
 
-   if (!FSessionData->HostKey.IsEmpty())
 
-   {
 
-     UnicodeString Buf = FSessionData->HostKey;
 
-     while (!Result && !Buf.IsEmpty())
 
-     {
 
-       UnicodeString ExpectedKey = CutToChar(Buf, HostKeyDelimiter, false);
 
-       UnicodeString ExpectedKeyType = KeyTypeFromFingerprint(ExpectedKey);
 
-       Result = SameText(ExpectedKeyType, KeyType);
 
-     }
 
-   }
 
-   if (Result)
 
-   {
 
-     LogEvent(FORMAT(L"Have a known host key of type %s", (KeyType)));
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::AskAlg(const UnicodeString AlgType,
 
-   const UnicodeString AlgName)
 
- {
 
-   UnicodeString Msg;
 
-   UnicodeString Error;
 
-   if (AlgType == L"key-exchange algorithm")
 
-   {
 
-     Msg = FMTLOAD(KEX_BELOW_TRESHOLD, (AlgName));
 
-     Error = FMTLOAD(KEX_NOT_VERIFIED, (AlgName));
 
-   }
 
-   else if (AlgType == L"hostkey type")
 
-   {
 
-     // noop as we do not allow host key algorithm configuration,
 
-     // so no algorithm can get below WARN level
 
-   }
 
-   else
 
-   {
 
-     int CipherType;
 
-     if (AlgType == L"cipher")
 
-     {
 
-       CipherType = CIPHER_TYPE_BOTH;
 
-     }
 
-     else if (AlgType == L"client-to-server cipher")
 
-     {
 
-       CipherType = CIPHER_TYPE_CS;
 
-     }
 
-     else if (AlgType == L"server-to-client cipher")
 
-     {
 
-       CipherType = CIPHER_TYPE_SC;
 
-     }
 
-     else
 
-     {
 
-       DebugFail();
 
-       CipherType = 0;
 
-     }
 
-     if (CipherType != 0)
 
-     {
 
-       Msg = FMTLOAD(CIPHER_BELOW_TRESHOLD, (LoadStr(CipherType), AlgName));
 
-       Error = FMTLOAD(CIPHER_NOT_VERIFIED, (AlgName));
 
-     }
 
-   }
 
-   if (!Msg.IsEmpty())
 
-   {
 
-     if (FUI->QueryUser(Msg, NULL, qaYes | qaNo, NULL, qtWarning) == qaNo)
 
-     {
 
-       FUI->FatalError(NULL, Error);
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::DisplayBanner(const UnicodeString & 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 || FStoredPassphraseTried;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TSecureShell::GetReady()
 
- {
 
-   return FOpened && (FWaiting == 0);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSecureShell::CollectUsage()
 
- {
 
-   if (FCollectPrivateKeyUsage)
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsPrivateKey2");
 
-   }
 
-   if (FSshVersion == 1)
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSH1");
 
-   }
 
-   else if (FSshVersion == 2)
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSH2");
 
-   }
 
-   if (SshImplementation == sshiOpenSSH)
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHOpenSSH");
 
-   }
 
-   else if (SshImplementation == sshiProFTPD)
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHProFTPD");
 
-   }
 
-   else if (SshImplementation == sshiBitvise)
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHBitvise");
 
-   }
 
-   else if (SshImplementation == sshiTitan)
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHTitan");
 
-   }
 
-   else if (SshImplementation == sshiOpenVMS)
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHOpenVMS");
 
-   }
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"Serv-U"))
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHServU");
 
-   }
 
-   else if (SshImplementation == sshiCerberus)
 
-   {
 
-     // Ntb, Cerberus can also be detected using vendor-id extension
 
-     // Cerberus FTP Server 7.0.5.3 (70005003) by Cerberus, LLC
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHCerberus");
 
-   }
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"WS_FTP"))
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHWSFTP");
 
-   }
 
-   // SSH-2.0-1.36_sshlib GlobalSCAPE
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"GlobalSCAPE"))
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHGlobalScape");
 
-   }
 
-   // SSH-2.0-CompleteFTP-8.1.3
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"CompleteFTP"))
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHComplete");
 
-   }
 
-   // SSH-2.0-CoreFTP-0.3.3
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"CoreFTP"))
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHCore");
 
-   }
 
-   // SSH-2.0-SSHD-CORE-0.11.0 (value is configurable, this is a default)
 
-   // (Apache Mina SSHD, e.g. on brickftp.com)
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"SSHD-CORE"))
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHApache");
 
-   }
 
-   // SSH-2.0-Syncplify_Me_Server
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"Syncplify"))
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHSyncplify");
 
-   }
 
-   else if (ContainsText(FSessionInfo.SshImplementation, L"zFTPServer"))
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHzFTP");
 
-   }
 
-   else
 
-   {
 
-     Configuration->Usage->Inc(L"OpenedSessionsSSHOther");
 
-   }
 
- }
 
 
  |