SecureShell.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //---------------------------------------------------------------------------
  2. #ifndef SecureShellH
  3. #define SecureShellH
  4. #include <set>
  5. #include "Configuration.h"
  6. #include "SessionData.h"
  7. #include "SessionInfo.h"
  8. //---------------------------------------------------------------------------
  9. #ifndef PuttyIntfH
  10. struct Backend;
  11. struct Config;
  12. #endif
  13. //---------------------------------------------------------------------------
  14. typedef UINT_PTR SOCKET;
  15. typedef std::set<SOCKET> TSockets;
  16. struct TPuttyTranslation;
  17. //---------------------------------------------------------------------------
  18. class TSecureShell
  19. {
  20. private:
  21. SOCKET FSocket;
  22. HANDLE FSocketEvent;
  23. TSockets FPortFwdSockets;
  24. TSessionUI* FUI;
  25. TSessionData * FSessionData;
  26. bool FActive;
  27. TSessionInfo FSessionInfo;
  28. bool FSessionInfoValid;
  29. TDateTime FLastDataSent;
  30. Backend * FBackend;
  31. void * FBackendHandle;
  32. const unsigned int * FMaxPacketSize;
  33. Config * FConfig;
  34. TNotifyEvent FOnReceive;
  35. bool FFrozen;
  36. bool FDataWhileFrozen;
  37. bool FStoredPasswordTried;
  38. bool FStoredPasswordTriedForKI;
  39. int FSshVersion;
  40. unsigned PendLen;
  41. unsigned PendSize;
  42. unsigned OutLen;
  43. char * OutPtr;
  44. char * Pending;
  45. TSessionLog * FLog;
  46. TConfiguration * FConfiguration;
  47. bool FAuthenticating;
  48. bool FAuthenticated;
  49. AnsiString FStdErrorTemp;
  50. AnsiString FStdError;
  51. AnsiString FCWriteTemp;
  52. AnsiString FAuthenticationLog;
  53. AnsiString FLastTunnelError;
  54. AnsiString FUserName;
  55. static TCipher __fastcall FuncToSsh1Cipher(const void * Cipher);
  56. static TCipher __fastcall FuncToSsh2Cipher(const void * Cipher);
  57. AnsiString __fastcall FuncToCompression(int SshVersion, const void * Compress) const;
  58. void __fastcall Init();
  59. void __fastcall SetActive(bool value);
  60. void inline __fastcall CheckConnection(int Message = -1);
  61. void __fastcall WaitForData();
  62. void __fastcall Discard();
  63. void __fastcall FreeBackend();
  64. void __fastcall PoolForData(unsigned int & Result);
  65. inline void __fastcall CaptureOutput(TLogLineType Type,
  66. const AnsiString & Line);
  67. void __fastcall ResetConnection();
  68. void __fastcall ResetSessionInfo();
  69. void __fastcall SocketEventSelect(SOCKET Socket, HANDLE Event, bool Startup);
  70. bool __fastcall ProcessNetworkEvents(SOCKET Socket);
  71. bool __fastcall EventSelectLoop(unsigned int MSec, bool ReadEventRequired);
  72. void __fastcall UpdateSessionInfo();
  73. protected:
  74. TCaptureOutputEvent FOnCaptureOutput;
  75. void __fastcall GotHostKey();
  76. bool __fastcall TranslatePuttyMessage(const TPuttyTranslation * Translation,
  77. size_t Count, AnsiString & Message);
  78. bool __fastcall TranslateAuthenticationMessage(AnsiString & Message);
  79. bool __fastcall TranslateErrorMessage(AnsiString & Message);
  80. void __fastcall AddStdError(AnsiString Str);
  81. void __fastcall AddStdErrorLine(const AnsiString & Str);
  82. void __fastcall FatalError(Exception * E, AnsiString Msg);
  83. void __fastcall inline LogEvent(const AnsiString & Str);
  84. void __fastcall FatalError(AnsiString Error);
  85. void __fastcall StoreToConfig(TSessionData * Data, Config * cfg);
  86. public:
  87. __fastcall TSecureShell(TSessionUI * UI, TSessionData * SessionData,
  88. TSessionLog * Log, TConfiguration * Configuration);
  89. __fastcall ~TSecureShell();
  90. void __fastcall Open();
  91. void __fastcall Close();
  92. void __fastcall KeepAlive();
  93. int __fastcall Receive(char * Buf, int Len);
  94. bool __fastcall Peek(char *& Buf, int Len);
  95. AnsiString __fastcall ReceiveLine();
  96. void __fastcall Send(const char * Buf, int Len);
  97. void __fastcall SendStr(AnsiString Str);
  98. void __fastcall SendSpecial(int Code);
  99. void __fastcall Idle(unsigned int MSec = 0);
  100. void __fastcall SendEOF();
  101. void __fastcall SendLine(AnsiString Line);
  102. void __fastcall SendNull();
  103. const TSessionInfo & __fastcall GetSessionInfo();
  104. bool __fastcall SshFallbackCmd() const;
  105. unsigned long __fastcall MaxPacketSize();
  106. void __fastcall ClearStdError();
  107. bool __fastcall GetStoredCredentialsTried();
  108. void __fastcall RegisterReceiveHandler(TNotifyEvent Handler);
  109. void __fastcall UnregisterReceiveHandler(TNotifyEvent Handler);
  110. // interface to PuTTY core
  111. void __fastcall UpdateSocket(SOCKET value, bool Startup);
  112. void __fastcall UpdatePortFwdSocket(SOCKET value, bool Startup);
  113. void __fastcall PuttyFatalError(AnsiString Error);
  114. bool __fastcall PromptUser(const AnsiString Prompt, AnsiString & Response,
  115. bool IsPassword);
  116. void __fastcall FromBackend(bool IsStdErr, const char * Data, int Length);
  117. void __fastcall CWrite(const char * Data, int Length, bool Untrusted);
  118. const AnsiString & __fastcall GetStdError();
  119. void __fastcall VerifyHostKey(const AnsiString Host, int Port,
  120. const AnsiString KeyType, const AnsiString KeyStr, const AnsiString Fingerprint);
  121. void __fastcall AskAlg(const AnsiString AlgType, const AnsiString AlgName);
  122. void __fastcall DisplayBanner(const AnsiString & Banner);
  123. void __fastcall OldKeyfileWarning();
  124. void __fastcall PuttyLogEvent(const AnsiString & Str);
  125. __property bool Active = { read = FActive, write = SetActive };
  126. __property TCaptureOutputEvent OnCaptureOutput = { read = FOnCaptureOutput, write = FOnCaptureOutput };
  127. __property TDateTime LastDataSent = { read = FLastDataSent };
  128. __property AnsiString LastTunnelError = { read = FLastTunnelError };
  129. __property AnsiString UserName = { read = FUserName };
  130. };
  131. //---------------------------------------------------------------------------
  132. #endif