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