1
0

SecureShell.h 7.7 KB

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