SecureShell.h 8.3 KB

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