SecureShell.h 12 KB

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