| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- //---------------------------------------------------------------------------
- #ifndef SecureShellH
- #define SecureShellH
- #include "Interface.h"
- #include "Configuration.h"
- #include "Exceptions.h"
- #include "SessionData.h"
- #include "FileSystems.h"
- #define SSH_ERROR(x) throw ESsh(NULL, x)
- #define SSH_FATAL_ERROR_EXT(E, x) throw ESshFatal(E, x)
- #define SSH_FATAL_ERROR(x) SSH_FATAL_ERROR_EXT(NULL, x)
- #define sshClosed 0
- #define sshInitWinSock 1
- #define sshLookupHost 2
- #define sshConnect 3
- #define sshAuthenticate 4
- #define sshAuthenticated 5
- #define sshStartup 6
- #define sshOpenDirectory 7
- #define sshReady 8
- //---------------------------------------------------------------------------
- const ropNoConfirmation = 0x01;
- const ropNoReadDirectory = 0x02;
- //---------------------------------------------------------------------------
- const boDisableNeverShowAgain = 0x01;
- //---------------------------------------------------------------------------
- class TSecureShell;
- class TConfiguration;
- enum TCompressionType { ctNone, ctZLib };
- //---------------------------------------------------------------------------
- typedef void __fastcall (__closure *TQueryUserEvent)
- (TObject* Sender, const AnsiString Query, TStrings * MoreMessages, int Answers,
- const TQueryParams * Params, int & Answer, TQueryType QueryType, void * Arg);
- typedef void __fastcall (__closure *TPromptUserEvent)
- (TSecureShell * SecureShell, AnsiString Prompt, TPromptKind Kind,
- AnsiString & Response, bool & Result, void * Arg);
- typedef void __fastcall (__closure *TDisplayBannerEvent)
- (TSecureShell * SecureShell, AnsiString SessionName, const AnsiString & Banner,
- bool & NeverShowAgain, int Options);
- typedef void __fastcall (__closure *TExtendedExceptionEvent)
- (TSecureShell * SecureShell, Exception * E, void * Arg);
- typedef void __fastcall (__closure *TUpdateStatusEvent)
- (TSecureShell * SecureShell, bool Active);
- //---------------------------------------------------------------------------
- typedef Set<TLogLineType, llOutput, llException> TLogLineTypes;
- extern const TColor LogLineColors[];
- //---------------------------------------------------------------------------
- class TSessionLog : public TStringList
- {
- private:
- TSecureShell * FOwner;
- TConfiguration * FConfiguration;
- bool FEnabled;
- void * FFile;
- AnsiString FFileName;
- Integer FLoggedLines;
- TLogAddLineEvent FOnAddLine;
- Integer FTopIndex;
- unsigned int FId;
- void __fastcall SetLine(Integer Index, AnsiString value);
- AnsiString __fastcall GetLine(Integer Index);
- void __fastcall SetType(Integer Index, TLogLineType value);
- TLogLineType __fastcall GetType(Integer Index);
- void DeleteUnnecessary();
- void OpenLogFile();
- TColor __fastcall GetColor(Integer Index);
- void __fastcall DoAddLine(TLogLineType Type, const AnsiString AddedLine);
- Integer __fastcall GetBottomIndex();
- Integer __fastcall GetIndexes(Integer Index);
- AnsiString __fastcall GetLogFileName();
- Boolean __fastcall GetLoggingToFile();
- Boolean __fastcall GetLogToFile();
- void __fastcall SetEnabled(bool value);
- void __fastcall SetConfiguration(TConfiguration * value);
- AnsiString __fastcall GetSessionName();
- void __fastcall DoAdd(TLogLineType aType, AnsiString aLine);
- public:
- __fastcall TSessionLog(TSecureShell * AOwner);
- __fastcall ~TSessionLog();
- HIDESBASE void __fastcall Add(TLogLineType aType, AnsiString aLine);
- void __fastcall AddStartupInfo();
- void __fastcall AddException(Exception * E);
- void __fastcall AddSeparator();
- void __fastcall AddFromOtherLog(TObject * Sender, TLogLineType aType,
- const AnsiString AddedLine);
- virtual void __fastcall Clear();
- void __fastcall ReflectSettings();
- bool __fastcall inline IsLogging()
- {
- return Enabled && (Configuration->Logging || (OnAddLine != NULL));
- }
- __property Integer BottomIndex = { read = GetBottomIndex };
- __property AnsiString Line[Integer Index] = { read=GetLine, write=SetLine };
- __property TLogLineType Type[Integer Index] = { read=GetType, write=SetType };
- __property TColor Color[Integer Index] = { read=GetColor };
- __property TConfiguration * Configuration = { read = FConfiguration, write = SetConfiguration };
- __property OnChange;
- __property bool Enabled = { read = FEnabled, write = SetEnabled };
- __property Integer Indexes[Integer Index] = { read = GetIndexes };
- __property AnsiString LogFileName = { read = GetLogFileName };
- __property Integer LoggedLines = { read = FLoggedLines };
- __property Boolean LoggingToFile = { read = GetLoggingToFile };
- __property TLogAddLineEvent OnAddLine = { read = FOnAddLine, write = FOnAddLine };
- __property Integer TopIndex = { read = FTopIndex };
- __property AnsiString SessionName = { read = GetSessionName };
- __property unsigned int Id = { read = FId, write = FId };
- protected:
- void __fastcall CloseLogFile();
- __property Boolean LogToFile = { read = GetLogToFile };
- };
- //---------------------------------------------------------------------------
- #ifndef PuttyIntfH
- struct Backend;
- struct Config;
- #endif
- //---------------------------------------------------------------------------
- class TSecureShell : public TObject
- {
- private:
- bool FStoredPasswordTried;
- bool FStoredPasswordTriedForKI;
- void * FSocket;
- TSessionData * FSessionData;
- bool FActive;
- __int64 FBytesReceived;
- __int64 FBytesSent;
- AnsiString FRealHost;
- TDateTime FLastDataSent;
- TQueryUserEvent FOnQueryUser;
- TPromptUserEvent FOnPromptUser;
- TDisplayBannerEvent FOnDisplayBanner;
- TExtendedExceptionEvent FOnShowExtendedException;
- Backend * FBackend;
- void * FBackendHandle;
- const unsigned int * FMaxPacketSize;
- int FBufSize;
- Config * FConfig;
- AnsiString FSshVersionString;
- AnsiString FPassword;
- AnsiString FHostKeyFingerprint;
- TLogAddLineEvent FOnStdError;
- unsigned PendLen;
- unsigned PendSize;
- unsigned OutLen;
- char * OutPtr;
- char * Pending;
- TSessionLog * FLog;
- TConfiguration *FConfiguration;
- TDateTime FLoginTime;
- TUpdateStatusEvent FOnUpdateStatus;
- TNotifyEvent FOnClose;
- int FStatus;
- int FReachedStatus;
- AnsiString FStdErrorTemp;
- AnsiString FAuthenticationLog;
- TObject * FUserObject;
- TCipher FCSCipher;
- TCipher FSCCipher;
- TCipher __fastcall FuncToSsh1Cipher(const void * Cipher) const;
- TCipher __fastcall FuncToSsh2Cipher(const void * Cipher) const;
- TCompressionType __fastcall FuncToCompression(const void * Compress) const;
- void __fastcall Init();
- void __fastcall SetActive(bool value);
- bool __fastcall GetActive() const;
- TCipher __fastcall GetCSCipher();
- TCompressionType __fastcall GetCSCompression() const;
- TDateTime __fastcall GetDuration() const;
- TCipher __fastcall GetSCCipher();
- TCompressionType __fastcall GetSCCompression() const;
- int __fastcall GetSshVersion() const;
- int __fastcall GetStatus() const;
- void inline __fastcall CheckConnection(int Message = -1);
- void __fastcall WaitForData(bool Sending);
- void __fastcall SetLog(TSessionLog * value);
- void __fastcall SetConfiguration(TConfiguration * value);
- void __fastcall SetUserObject(TObject * value);
- void __fastcall Discard();
- AnsiString __fastcall GetSshImplementation();
- AnsiString __fastcall GetPassword();
- bool __fastcall Select(int Sec);
- void __fastcall PoolForData(unsigned int & Result);
- TDateTime __fastcall GetIdleInterval();
- bool __fastcall GetStoredPasswordTried();
- inline void __fastcall CaptureOutput(TLogLineType Type,
- const AnsiString & Line, bool LogOnly);
- void __fastcall ResetConnection();
- protected:
- AnsiString StdError;
- TLogAddLineEvent FOnCaptureOutput;
- void __fastcall Error(const AnsiString Error) const;
- virtual void __fastcall UpdateStatus(int Value, bool Active = true);
- bool __fastcall SshFallbackCmd() const;
- void __fastcall GotHostKey();
- unsigned long __fastcall MaxPacketSize();
- int __fastcall RemainingSendBuffer();
- virtual void __fastcall KeepAlive();
- virtual void __fastcall SetSessionData(TSessionData * value);
- virtual void __fastcall DoDisplayBanner(const AnsiString & Banner, bool & Log);
- virtual void __fastcall DoOpen();
- void __fastcall TranslateAuthenticationMessage(AnsiString & Message);
- virtual bool __fastcall DoQueryReopen(Exception * E, int Params);
- public:
- __fastcall TSecureShell();
- __fastcall ~TSecureShell();
- virtual void __fastcall Open();
- virtual void __fastcall Close();
- virtual void __fastcall Reopen(int Params);
- bool __fastcall PromptUser(const AnsiString Prompt, AnsiString & Response,
- bool IsPassword);
- int __fastcall Receive(char * Buf, int Len);
- AnsiString __fastcall ReceiveLine();
- void __fastcall Send(const char * Buf, int Len);
- void __fastcall SendStr(AnsiString Str);
- void __fastcall SendSpecial(int Code);
- void __fastcall AddStdError(AnsiString Str, bool LogOnly);
- void __fastcall AddStdErrorLine(AnsiString Str, bool LogOnly);
- void __fastcall ClearStdError();
- virtual void __fastcall Idle();
- void __fastcall SendEOF();
- void __fastcall SendLine(AnsiString Line);
- void __fastcall FatalError(Exception * E, AnsiString Msg);
- void __fastcall SendNull();
- void __fastcall SetSocket(void * value);
- void __fastcall FatalError(AnsiString Error);
- void __fastcall FromBackend(bool IsStdErr, char * Data, int Length);
- void __fastcall VerifyHostKey(const AnsiString Host, int Port,
- const AnsiString KeyType, const AnsiString KeyStr, const AnsiString Fingerprint);
- void __fastcall AskAlg(const AnsiString AlgType, const AnsiString AlgName);
- void __fastcall DisplayBanner(const AnsiString & Banner, bool & Log);
- void __fastcall OldKeyfileWarning();
- bool __fastcall QueryReopen(Exception * E, int Params);
- virtual int __fastcall DoQueryUser(const AnsiString Query, TStrings * MoreMessages,
- int Answers, const TQueryParams * Params, TQueryType Type = qtConfirmation);
- int __fastcall DoQueryUser(const AnsiString Query, const AnsiString OtherMessage,
- int Answers, const TQueryParams * Params, TQueryType Type);
- int __fastcall DoQueryUser(const AnsiString Query, int Answers,
- const TQueryParams * Params, TQueryType Type = qtConfirmation);
- int __fastcall DoQueryUser(const AnsiString Query, Exception * E,
- int Answers, const TQueryParams * Params, TQueryType Type);
- virtual void __fastcall DoShowExtendedException(Exception * E);
- void __fastcall DoHandleExtendedException(Exception * E);
- virtual bool __fastcall DoPromptUser(AnsiString Prompt, TPromptKind Kind,
- AnsiString & Response);
- bool __fastcall inline IsLogging()
- {
- return Log->IsLogging();
- }
- void __fastcall PuttyLogEvent(const AnsiString & Str);
- void __fastcall inline LogEvent(const AnsiString & Str)
- {
- if (IsLogging()) Log->Add(llMessage, Str);
- }
- __property TSessionData * SessionData = { read = FSessionData, write = SetSessionData };
- __property bool Active = { read = GetActive, write = SetActive };
- __property __int64 BytesReceived = { read = FBytesReceived };
- __property __int64 BytesSent = { read = FBytesSent };
- __property AnsiString RealHost = { read = FRealHost };
- __property TSessionLog * Log = { read=FLog, write=SetLog };
- __property TConfiguration * Configuration = { read=FConfiguration, write=SetConfiguration };
- __property TCipher CSCipher = { read = GetCSCipher };
- __property TCompressionType CSCompression = { read = GetCSCompression };
- __property TDateTime Duration = { read = GetDuration };
- __property TDateTime LoginTime = { read = FLoginTime };
- __property TCipher SCCipher = { read = GetSCCipher };
- __property TCompressionType SCCompression = { read = GetSCCompression };
- __property int SshVersion = { read = GetSshVersion };
- __property AnsiString SshVersionString = { read = FSshVersionString };
- __property AnsiString SshImplementation = { read = GetSshImplementation };
- __property AnsiString HostKeyFingerprint = { read = FHostKeyFingerprint };
- __property TQueryUserEvent OnQueryUser = { read = FOnQueryUser, write = FOnQueryUser };
- __property TPromptUserEvent OnPromptUser = { read = FOnPromptUser, write = FOnPromptUser };
- __property TDisplayBannerEvent OnDisplayBanner = { read = FOnDisplayBanner, write = FOnDisplayBanner };
- __property TExtendedExceptionEvent OnShowExtendedException = { read = FOnShowExtendedException, write = FOnShowExtendedException };
- __property TUpdateStatusEvent OnUpdateStatus = { read = FOnUpdateStatus, write = FOnUpdateStatus };
- __property TLogAddLineEvent OnStdError = { read = FOnStdError, write = FOnStdError };
- __property TNotifyEvent OnClose = { read = FOnClose, write = FOnClose };
- __property int Status = { read = GetStatus };
- __property TObject * UserObject = { read = FUserObject, write = SetUserObject };
- __property AnsiString Password = { read = GetPassword };
- __property TDateTime IdleInterval = { read = GetIdleInterval };
- __property bool StoredPasswordTried = { read = GetStoredPasswordTried };
- };
- //---------------------------------------------------------------------------
- #endif
|