SecureShell.h 5.8 KB

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