SecureShell.h 6.2 KB

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