SecureShell.h 8.2 KB

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