SecureShell.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, sshiTitan, sshiOpenVMS, sshiCerberus };
  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. bool FUtfStrings;
  67. static TCipher __fastcall FuncToSsh1Cipher(const void * Cipher);
  68. static TCipher __fastcall FuncToSsh2Cipher(const void * Cipher);
  69. UnicodeString __fastcall FuncToCompression(int SshVersion, const void * Compress) const;
  70. void __fastcall Init();
  71. void __fastcall SetActive(bool value);
  72. void inline __fastcall CheckConnection(int Message = -1);
  73. void __fastcall WaitForData();
  74. void __fastcall Discard();
  75. void __fastcall FreeBackend();
  76. void __fastcall PoolForData(WSANETWORKEVENTS & Events, unsigned int & Result);
  77. inline void __fastcall CaptureOutput(TLogLineType Type,
  78. const UnicodeString & Line);
  79. void __fastcall ResetConnection();
  80. void __fastcall ResetSessionInfo();
  81. void __fastcall SocketEventSelect(SOCKET Socket, HANDLE Event, bool Startup);
  82. bool __fastcall EnumNetworkEvents(SOCKET Socket, WSANETWORKEVENTS & Events);
  83. void __fastcall HandleNetworkEvents(SOCKET Socket, WSANETWORKEVENTS & Events);
  84. bool __fastcall ProcessNetworkEvents(SOCKET Socket);
  85. bool __fastcall EventSelectLoop(unsigned int MSec, bool ReadEventRequired,
  86. WSANETWORKEVENTS * Events);
  87. void __fastcall UpdateSessionInfo();
  88. bool __fastcall GetReady();
  89. void __fastcall DispatchSendBuffer(int BufSize);
  90. void __fastcall SendBuffer(unsigned int & Result);
  91. unsigned int __fastcall TimeoutPrompt(TQueryParamsTimerEvent PoolEvent);
  92. bool __fastcall TryFtp();
  93. UnicodeString __fastcall ConvertInput(const RawByteString & Input);
  94. void __fastcall GetRealHost(UnicodeString & Host, int & Port);
  95. UnicodeString __fastcall RetrieveHostKey(UnicodeString Host, int Port, const UnicodeString KeyType);
  96. protected:
  97. TCaptureOutputEvent FOnCaptureOutput;
  98. void __fastcall GotHostKey();
  99. int __fastcall TranslatePuttyMessage(const TPuttyTranslation * Translation,
  100. size_t Count, UnicodeString & Message, UnicodeString * HelpKeyword = NULL);
  101. int __fastcall TranslateAuthenticationMessage(UnicodeString & Message, UnicodeString * HelpKeyword = NULL);
  102. int __fastcall TranslateErrorMessage(UnicodeString & Message, UnicodeString * HelpKeyword = NULL);
  103. void __fastcall AddStdError(UnicodeString Str);
  104. void __fastcall AddStdErrorLine(const UnicodeString & Str);
  105. void __fastcall inline LogEvent(const UnicodeString & Str);
  106. void __fastcall FatalError(UnicodeString Error, UnicodeString HelpKeyword = L"");
  107. UnicodeString __fastcall FormatKeyStr(UnicodeString KeyStr);
  108. static Conf * __fastcall StoreToConfig(TSessionData * Data, bool Simple);
  109. public:
  110. __fastcall TSecureShell(TSessionUI * UI, TSessionData * SessionData,
  111. TSessionLog * Log, TConfiguration * Configuration);
  112. __fastcall ~TSecureShell();
  113. void __fastcall Open();
  114. void __fastcall Close();
  115. void __fastcall KeepAlive();
  116. int __fastcall Receive(unsigned char * Buf, int Len);
  117. bool __fastcall Peek(unsigned char *& Buf, int Len);
  118. UnicodeString __fastcall ReceiveLine();
  119. void __fastcall Send(const unsigned char * Buf, int Len);
  120. void __fastcall SendSpecial(int Code);
  121. void __fastcall Idle(unsigned int MSec = 0);
  122. void __fastcall SendEOF();
  123. void __fastcall SendLine(const UnicodeString & Line);
  124. void __fastcall SendNull();
  125. const TSessionInfo & __fastcall GetSessionInfo();
  126. UnicodeString __fastcall GetHostKeyFingerprint();
  127. bool __fastcall SshFallbackCmd() const;
  128. unsigned long __fastcall MaxPacketSize();
  129. void __fastcall ClearStdError();
  130. bool __fastcall GetStoredCredentialsTried();
  131. void __fastcall CollectUsage();
  132. void __fastcall RegisterReceiveHandler(TNotifyEvent Handler);
  133. void __fastcall UnregisterReceiveHandler(TNotifyEvent Handler);
  134. // interface to PuTTY core
  135. void __fastcall UpdateSocket(SOCKET value, bool Startup);
  136. void __fastcall UpdatePortFwdSocket(SOCKET value, bool Startup);
  137. void __fastcall PuttyFatalError(UnicodeString Error);
  138. TPromptKind __fastcall IdentifyPromptKind(UnicodeString & Name);
  139. bool __fastcall PromptUser(bool ToServer,
  140. UnicodeString AName, bool NameRequired,
  141. UnicodeString Instructions, bool InstructionsRequired,
  142. TStrings * Prompts, TStrings * Results);
  143. void __fastcall FromBackend(bool IsStdErr, const unsigned char * Data, int Length);
  144. void __fastcall CWrite(const char * Data, int Length);
  145. const UnicodeString & __fastcall GetStdError();
  146. void __fastcall VerifyHostKey(UnicodeString Host, int Port,
  147. const UnicodeString KeyType, UnicodeString KeyStr, UnicodeString Fingerprint);
  148. bool __fastcall HaveHostKey(UnicodeString Host, int Port, const UnicodeString KeyType);
  149. void __fastcall AskAlg(const UnicodeString AlgType, const UnicodeString AlgName);
  150. void __fastcall DisplayBanner(const UnicodeString & Banner);
  151. void __fastcall OldKeyfileWarning();
  152. void __fastcall PuttyLogEvent(const char * Str);
  153. UnicodeString __fastcall ConvertFromPutty(const char * Str, int Length);
  154. __property bool Active = { read = FActive, write = SetActive };
  155. __property bool Ready = { read = GetReady };
  156. __property TCaptureOutputEvent OnCaptureOutput = { read = FOnCaptureOutput, write = FOnCaptureOutput };
  157. __property TDateTime LastDataSent = { read = FLastDataSent };
  158. __property UnicodeString LastTunnelError = { read = FLastTunnelError };
  159. __property UnicodeString UserName = { read = FUserName };
  160. __property bool Simple = { read = FSimple, write = FSimple };
  161. __property TSshImplementation SshImplementation = { read = FSshImplementation };
  162. __property bool UtfStrings = { read = FUtfStrings, write = FUtfStrings };
  163. };
  164. //---------------------------------------------------------------------------
  165. #endif