SecureShell.h 12 KB

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