SecureShell.h 6.8 KB

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