SecureShell.h 9.1 KB

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