SecureShell.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. bool FOpened;
  44. int FWaiting;
  45. unsigned PendLen;
  46. unsigned PendSize;
  47. unsigned OutLen;
  48. char * OutPtr;
  49. char * Pending;
  50. TSessionLog * FLog;
  51. TConfiguration * FConfiguration;
  52. bool FAuthenticating;
  53. bool FAuthenticated;
  54. AnsiString FStdErrorTemp;
  55. AnsiString FStdError;
  56. AnsiString FCWriteTemp;
  57. AnsiString FAuthenticationLog;
  58. AnsiString FLastTunnelError;
  59. AnsiString FUserName;
  60. static TCipher __fastcall FuncToSsh1Cipher(const void * Cipher);
  61. static TCipher __fastcall FuncToSsh2Cipher(const void * Cipher);
  62. AnsiString __fastcall FuncToCompression(int SshVersion, const void * Compress) const;
  63. void __fastcall Init();
  64. void __fastcall SetActive(bool value);
  65. void inline __fastcall CheckConnection(int Message = -1);
  66. void __fastcall WaitForData();
  67. void __fastcall Discard();
  68. void __fastcall FreeBackend();
  69. void __fastcall PoolForData(WSANETWORKEVENTS & Events, unsigned int & Result);
  70. inline void __fastcall CaptureOutput(TLogLineType Type,
  71. const AnsiString & Line);
  72. void __fastcall ResetConnection();
  73. void __fastcall ResetSessionInfo();
  74. void __fastcall SocketEventSelect(SOCKET Socket, HANDLE Event, bool Startup);
  75. bool __fastcall EnumNetworkEvents(SOCKET Socket, WSANETWORKEVENTS & Events);
  76. void __fastcall HandleNetworkEvents(SOCKET Socket, WSANETWORKEVENTS & Events);
  77. bool __fastcall ProcessNetworkEvents(SOCKET Socket);
  78. bool __fastcall EventSelectLoop(unsigned int MSec, bool ReadEventRequired,
  79. WSANETWORKEVENTS * Events);
  80. void __fastcall UpdateSessionInfo();
  81. bool __fastcall GetReady();
  82. void __fastcall DispatchSendBuffer(int BufSize);
  83. void __fastcall SendBuffer(unsigned int & Result);
  84. int __fastcall TimeoutPrompt(TQueryParamsTimerEvent PoolEvent);
  85. protected:
  86. TCaptureOutputEvent FOnCaptureOutput;
  87. void __fastcall GotHostKey();
  88. int __fastcall TranslatePuttyMessage(const TPuttyTranslation * Translation,
  89. size_t Count, AnsiString & Message);
  90. int __fastcall TranslateAuthenticationMessage(AnsiString & Message);
  91. int __fastcall TranslateErrorMessage(AnsiString & Message);
  92. void __fastcall AddStdError(AnsiString Str);
  93. void __fastcall AddStdErrorLine(const AnsiString & Str);
  94. void __fastcall FatalError(Exception * E, AnsiString Msg);
  95. void __fastcall inline LogEvent(const AnsiString & Str);
  96. void __fastcall FatalError(AnsiString Error);
  97. static void __fastcall ClearConfig(Config * cfg);
  98. static void __fastcall StoreToConfig(TSessionData * Data, Config * cfg);
  99. public:
  100. __fastcall TSecureShell(TSessionUI * UI, TSessionData * SessionData,
  101. TSessionLog * Log, TConfiguration * Configuration);
  102. __fastcall ~TSecureShell();
  103. void __fastcall Open();
  104. void __fastcall Close();
  105. void __fastcall KeepAlive();
  106. int __fastcall Receive(char * Buf, int Len);
  107. bool __fastcall Peek(char *& Buf, int Len);
  108. AnsiString __fastcall ReceiveLine();
  109. void __fastcall Send(const char * Buf, int Len);
  110. void __fastcall SendStr(AnsiString Str);
  111. void __fastcall SendSpecial(int Code);
  112. void __fastcall Idle(unsigned int MSec = 0);
  113. void __fastcall SendEOF();
  114. void __fastcall SendLine(AnsiString Line);
  115. void __fastcall SendNull();
  116. const TSessionInfo & __fastcall GetSessionInfo();
  117. bool __fastcall SshFallbackCmd() const;
  118. unsigned long __fastcall MaxPacketSize();
  119. void __fastcall ClearStdError();
  120. bool __fastcall GetStoredCredentialsTried();
  121. void __fastcall RegisterReceiveHandler(TNotifyEvent Handler);
  122. void __fastcall UnregisterReceiveHandler(TNotifyEvent Handler);
  123. // interface to PuTTY core
  124. void __fastcall UpdateSocket(SOCKET value, bool Startup);
  125. void __fastcall UpdatePortFwdSocket(SOCKET value, bool Startup);
  126. void __fastcall PuttyFatalError(AnsiString Error);
  127. bool __fastcall PromptUser(bool ToServer,
  128. AnsiString AName, bool NameRequired,
  129. AnsiString Instructions, bool InstructionsRequired,
  130. TStrings * Prompts, TStrings * Results);
  131. void __fastcall FromBackend(bool IsStdErr, const char * Data, int Length);
  132. void __fastcall CWrite(const char * Data, int Length);
  133. const AnsiString & __fastcall GetStdError();
  134. void __fastcall VerifyHostKey(AnsiString Host, int Port,
  135. const AnsiString KeyType, AnsiString KeyStr, const AnsiString Fingerprint);
  136. void __fastcall AskAlg(const AnsiString AlgType, const AnsiString AlgName);
  137. void __fastcall DisplayBanner(const AnsiString & Banner);
  138. void __fastcall OldKeyfileWarning();
  139. void __fastcall PuttyLogEvent(const AnsiString & Str);
  140. __property bool Active = { read = FActive, write = SetActive };
  141. __property bool Ready = { read = GetReady };
  142. __property TCaptureOutputEvent OnCaptureOutput = { read = FOnCaptureOutput, write = FOnCaptureOutput };
  143. __property TDateTime LastDataSent = { read = FLastDataSent };
  144. __property AnsiString LastTunnelError = { read = FLastTunnelError };
  145. __property AnsiString UserName = { read = FUserName };
  146. };
  147. //---------------------------------------------------------------------------
  148. #endif