1
0

SecureShell.h 8.2 KB

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