SecureShell.h 6.6 KB

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