SecureShell.h 11 KB

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