SecureShell.h 13 KB

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