SecureShell.h 6.7 KB

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