SecureShell.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //---------------------------------------------------------------------------
  2. #ifndef SecureShellH
  3. #define SecureShellH
  4. #include "Interface.h"
  5. #include "Configuration.h"
  6. #include "Exceptions.h"
  7. #include "SessionData.h"
  8. #define SSH_ERROR(x) throw ESsh(NULL, x)
  9. #define SSH_FATAL_ERROR_EXT(E, x) throw ESshFatal(E, x)
  10. #define SSH_FATAL_ERROR(x) SSH_FATAL_ERROR_EXT(NULL, x)
  11. #define sshClosed 0
  12. #define sshInitWinSock 1
  13. #define sshLookupHost 2
  14. #define sshConnect 3
  15. #define sshAuthenticate 4
  16. #define sshAuthenticated 5
  17. #define sshStartup 6
  18. #define sshOpenDirectory 7
  19. #define sshReady 8
  20. //---------------------------------------------------------------------------
  21. typedef void __fastcall (__closure *TQueryUserEvent)
  22. (TObject* Sender, const AnsiString Query, TStrings * MoreMessages, int Answers,
  23. int Params, int & Answer, TQueryType QueryType);
  24. //---------------------------------------------------------------------------
  25. class TSessionLog;
  26. class TConfiguration;
  27. enum TCompressionType { ctNone, ctZLib };
  28. //---------------------------------------------------------------------------
  29. // Duplicated in LogMemo.h for design-time-only purposes
  30. enum TLogLineType {llOutput, llInput, llStdError, llMessage, llException};
  31. typedef Set<TLogLineType, llOutput, llException> TLogLineTypes;
  32. extern const TColor LogLineColors[];
  33. typedef void __fastcall (__closure *TLogAddLineEvent)(System::TObject* Sender, const AnsiString AddedLine);
  34. //---------------------------------------------------------------------------
  35. class TSessionLog : public TStringList
  36. {
  37. private:
  38. TConfiguration * FConfiguration;
  39. TSessionData * FData;
  40. void * FFile;
  41. AnsiString FFileName;
  42. Integer FLoggedLines;
  43. TLogAddLineEvent FOnAddLine;
  44. Integer FTopIndex;
  45. void __fastcall SetLine(Integer Index, AnsiString value);
  46. AnsiString __fastcall GetLine(Integer Index);
  47. void __fastcall SetType(Integer Index, TLogLineType value);
  48. TLogLineType __fastcall GetType(Integer Index);
  49. void SetData(TSessionData * value);
  50. void DeleteUnnecessary();
  51. void OpenLogFile();
  52. TColor __fastcall GetColor(Integer Index);
  53. void __fastcall DoAddLine(const AnsiString AddedLine);
  54. Integer __fastcall GetBottomIndex();
  55. Integer __fastcall GetIndexes(Integer Index);
  56. AnsiString __fastcall GetLogFileName();
  57. Boolean __fastcall GetLoggingToFile();
  58. Boolean __fastcall GetLogToFile();
  59. void __fastcall SetConfiguration(TConfiguration * value);
  60. public:
  61. HIDESBASE void __fastcall Add(TLogLineType aType, AnsiString aLine);
  62. void __fastcall AddStartupInfo();
  63. __fastcall TSessionLog();
  64. __fastcall ~TSessionLog();
  65. void __fastcall AddException(Exception * E);
  66. void __fastcall AddSeparator();
  67. virtual void __fastcall Clear();
  68. void __fastcall ReflectSettings();
  69. __property Integer BottomIndex = { read = GetBottomIndex };
  70. __property TSessionData * Data = { read=FData, write=SetData };
  71. __property AnsiString Line[Integer Index] = { read=GetLine, write=SetLine };
  72. __property TLogLineType Type[Integer Index] = { read=GetType, write=SetType };
  73. __property TColor Color[Integer Index] = { read=GetColor };
  74. __property TConfiguration * Configuration = { read = FConfiguration, write = SetConfiguration };
  75. __property OnChange;
  76. __property Integer Indexes[Integer Index] = { read = GetIndexes };
  77. __property AnsiString LogFileName = { read = GetLogFileName };
  78. __property Integer LoggedLines = { read = FLoggedLines };
  79. __property Boolean LoggingToFile = { read = GetLoggingToFile };
  80. __property TLogAddLineEvent OnAddLine = { read = FOnAddLine, write = FOnAddLine };
  81. __property Integer TopIndex = { read = FTopIndex };
  82. protected:
  83. void __fastcall CloseLogFile();
  84. __property Boolean LogToFile = { read = GetLogToFile };
  85. };
  86. //---------------------------------------------------------------------------
  87. #ifndef PuttyIntfH
  88. struct Backend;
  89. struct Config;
  90. #endif
  91. //---------------------------------------------------------------------------
  92. class TSecureShell : public TObject
  93. {
  94. private:
  95. bool FPasswordTried;
  96. void * FSocket;
  97. TSessionData * FSessionData;
  98. bool FActive;
  99. __int64 FBytesReceived;
  100. __int64 FBytesSent;
  101. AnsiString FRealHost;
  102. TDateTime FLastDataSent;
  103. TQueryUserEvent FOnQueryUser;
  104. Backend * FBackend;
  105. void * FBackendHandle;
  106. unsigned long FMaxPacketSize;
  107. Config * FConfig;
  108. unsigned PendLen;
  109. unsigned PendSize;
  110. unsigned OutLen;
  111. char * OutPtr;
  112. char * Pending;
  113. TSessionLog * FLog;
  114. TConfiguration *FConfiguration;
  115. TDateTime FLoginTime;
  116. TNotifyEvent FOnUpdateStatus;
  117. TNotifyEvent FOnClose;
  118. int FStatus;
  119. int FReachedStatus;
  120. AnsiString FStdErrorTemp;
  121. AnsiString FAuthenticationLog;
  122. TObject * FUserObject;
  123. TCipher FCSCipher;
  124. TCipher FSCCipher;
  125. TCipher __fastcall FuncToSsh1Cipher(const void * Cipher) const;
  126. TCipher __fastcall FuncToSsh2Cipher(const void * Cipher) const;
  127. TCompressionType __fastcall FuncToCompression(const void * Compress) const;
  128. void __fastcall Init();
  129. void __fastcall SetSessionData(TSessionData * value);
  130. void __fastcall SetActive(bool value);
  131. bool __fastcall GetActive() const;
  132. TCipher __fastcall GetCSCipher();
  133. TCompressionType __fastcall GetCSCompression() const;
  134. TDateTime __fastcall GetDuration() const;
  135. TCipher __fastcall GetSCCipher();
  136. TCompressionType __fastcall GetSCCompression() const;
  137. int __fastcall GetSshVersion() const;
  138. int __fastcall GetStatus() const;
  139. void inline __fastcall CheckConnection(int Message = -1);
  140. void __fastcall WaitForData();
  141. void __fastcall SetLog(TSessionLog * value);
  142. void __fastcall SetConfiguration(TConfiguration * value);
  143. void __fastcall SetUserObject(TObject * value);
  144. void __fastcall Discard();
  145. protected:
  146. AnsiString StdError;
  147. void __fastcall Error(const AnsiString Error) const;
  148. virtual void __fastcall UpdateStatus(int Value);
  149. bool __fastcall SshFallbackCmd() const;
  150. void __fastcall GotHostKey();
  151. unsigned long __fastcall MaxPacketSize();
  152. virtual void __fastcall KeepAlive();
  153. public:
  154. __fastcall TSecureShell();
  155. __fastcall ~TSecureShell();
  156. virtual void __fastcall Open();
  157. virtual void __fastcall Close();
  158. int __fastcall GetPassword(AnsiString & Password);
  159. int __fastcall Receive(char * Buf, int Len);
  160. AnsiString __fastcall ReceiveLine();
  161. void __fastcall Send(const char * Buf, int Len);
  162. void __fastcall SendStr(AnsiString Str);
  163. void __fastcall SendSpecial(int Code);
  164. void __fastcall AddStdError(AnsiString Str);
  165. void __fastcall ClearStdError();
  166. void __fastcall Idle();
  167. void __fastcall SendEOF();
  168. void __fastcall SendLine(AnsiString Line);
  169. void __fastcall FatalError(Exception * E, AnsiString Msg);
  170. void __fastcall SendNull();
  171. void __fastcall SetSocket(void * value);
  172. void __fastcall FatalError(AnsiString Error);
  173. void __fastcall FromBackend(bool IsStdErr, char * Data, int Length);
  174. void __fastcall VerifyHostKey(const AnsiString Host, int Port,
  175. const AnsiString KeyType, const AnsiString KeyStr, const AnsiString Fingerprint);
  176. void __fastcall AskCipher(const AnsiString CipherName, int CipherType);
  177. void __fastcall OldKeyfileWarning();
  178. int __fastcall DoQueryUser(const AnsiString Query, const AnsiString OtherMessage,
  179. int Answers, int Params);
  180. int __fastcall DoQueryUser(const AnsiString Query, int Answers, int Params);
  181. int __fastcall DoQueryUser(const AnsiString Query, TStrings * MoreMessages,
  182. int Answers, int Params, TQueryType Type = qtConfirmation);
  183. int __fastcall DoQueryUser(const AnsiString Query, Exception * E,
  184. int Answers, int Params);
  185. bool __fastcall inline IsLogging()
  186. {
  187. return Configuration->Logging || Log->OnAddLine;
  188. }
  189. void __fastcall inline LogEvent(const AnsiString & Str)
  190. {
  191. if (IsLogging()) Log->Add(llMessage, Str);
  192. }
  193. __property TSessionData * SessionData = { read = FSessionData, write = SetSessionData };
  194. __property bool Active = { read = GetActive, write = SetActive };
  195. __property __int64 BytesReceived = { read = FBytesReceived };
  196. __property __int64 BytesSent = { read = FBytesSent };
  197. __property AnsiString RealHost = { read = FRealHost };
  198. __property TSessionLog * Log = { read=FLog, write=SetLog };
  199. __property TConfiguration * Configuration = { read=FConfiguration, write=SetConfiguration };
  200. __property TCipher CSCipher = { read = GetCSCipher };
  201. __property TCompressionType CSCompression = { read = GetCSCompression };
  202. __property TDateTime Duration = { read = GetDuration };
  203. __property TDateTime LoginTime = { read = FLoginTime };
  204. __property TCipher SCCipher = { read = GetSCCipher };
  205. __property TCompressionType SCCompression = { read = GetSCCompression };
  206. __property int SshVersion = { read = GetSshVersion };
  207. __property TQueryUserEvent OnQueryUser = { read = FOnQueryUser, write = FOnQueryUser };
  208. __property TNotifyEvent OnUpdateStatus = { read = FOnUpdateStatus, write = FOnUpdateStatus };
  209. __property TNotifyEvent OnClose = { read = FOnClose, write = FOnClose };
  210. __property int Status = { read = GetStatus };
  211. __property TObject * UserObject = { read = FUserObject, write = SetUserObject };
  212. };
  213. //---------------------------------------------------------------------------
  214. #endif