SecureShell.h 7.9 KB

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