From e1bf2d21ca9b358f9dfe349a86ad1d35a33e47ec Mon Sep 17 00:00:00 2001 From: psychon Date: Sat, 24 May 2008 17:09:25 +0000 Subject: [PATCH] Disconnect unauthed connections after a timeout of 60 secs git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1067 726aef4b-f618-498e-8847-2d620e286838 --- Client.cpp | 29 +++++++++++++++++++++++++++++ Client.h | 6 ++++++ Timers.h | 20 ++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/Client.cpp b/Client.cpp index ace69430..20b38796 100644 --- a/Client.cpp +++ b/Client.cpp @@ -12,6 +12,7 @@ #include "DCCSock.h" #include "IRCSock.h" #include "Server.h" +#include "Timers.h" #include "User.h" CClient::~CClient() { @@ -23,6 +24,9 @@ CClient::~CClient() { m_pUser->AddBytesRead(GetBytesRead()); m_pUser->AddBytesWritten(GetBytesWritten()); } + if (m_pTimeout) { + CZNC::Get().GetManager().DelCronByAddr(m_pTimeout); + } } void CClient::ReadLine(const CString& sData) { @@ -1828,6 +1832,11 @@ void CClientAuth::RefuseLogin(const CString& sReason) { } void CClient::RefuseLogin(const CString& sReason) { + if (m_pTimeout) { + m_pTimeout->Stop(); + m_pTimeout = NULL; + } + PutStatus("Bad username and/or password."); PutClient(":irc.znc.com 464 " + GetNick() + " :" + sReason); Close(); @@ -1843,6 +1852,11 @@ void CClient::AcceptLogin(CUser& User) { m_sPass = ""; m_pUser = &User; + if (m_pTimeout) { + m_pTimeout->Stop(); + m_pTimeout = NULL; + } + if (!m_pUser->IsHostAllowed(GetRemoteIP())) { PutClient(":irc.znc.com 463 " + GetNick() + " :Your host (" + GetRemoteIP() + ") is not allowed"); @@ -1860,6 +1874,20 @@ void CClient::AcceptLogin(CUser& User) { MODULECALL(OnUserAttached(), m_pUser, this, ); } +void CClient::StartLoginTimeout() { + m_pTimeout = new CClientTimeout(this); + CZNC::Get().GetManager().AddCron(m_pTimeout); +} + +void CClient::LoginTimeout() { + PutClient("ERROR :Closing link [Timeout]"); + Close(Csock::CLT_AFTERWRITE); + if (m_pTimeout) { + m_pTimeout->Stop(); + m_pTimeout = NULL; + } +} + void CClient::Connected() { DEBUG_ONLY(cout << GetSockName() << " == Connected();" << endl); SetTimeout(900); // Now that we are connected, let nature take its course @@ -1895,6 +1923,7 @@ void CClient::IRCDisconnected() { Csock* CClient::GetSockObj(const CString& sHost, unsigned short uPort) { CClient* pSock = new CClient(sHost, uPort); + pSock->StartLoginTimeout(); return pSock; } diff --git a/Client.h b/Client.h index e88e0ca3..80b6c7b1 100644 --- a/Client.h +++ b/Client.h @@ -17,6 +17,7 @@ class CZNC; class CUser; class CIRCSock; class CClient; +class CClientTimeout; // !Forward Declarations class CAuthBase { @@ -77,6 +78,7 @@ public: void InitClient() { m_pUser = NULL; + m_pTimeout = NULL; m_pIRCSock = NULL; m_bGotPass = false; m_bGotNick = false; @@ -89,6 +91,8 @@ public: void AcceptLogin(CUser& User); void RefuseLogin(const CString& sReason); + void StartLoginTimeout(); + void LoginTimeout(); CString GetNick(bool bAllowIRCNick = true) const; CString GetNickMask() const; @@ -123,6 +127,7 @@ public: void SetNick(const CString& s); CUser* GetUser() const { return m_pUser; } private: + protected: bool m_bGotPass; bool m_bGotNick; @@ -136,6 +141,7 @@ protected: CIRCSock* m_pIRCSock; unsigned int m_uKeepNickCounter; CSmartPtr m_spAuth; + CClientTimeout* m_pTimeout; }; #endif // !_CLIENT_H diff --git a/Timers.h b/Timers.h index f1158d03..421891d5 100644 --- a/Timers.h +++ b/Timers.h @@ -94,4 +94,24 @@ protected: CUser* m_pUser; }; +class CClientTimeout : public CCron { +public: + CClientTimeout(CClient* pClient) : CCron() { + m_pClient = pClient; + SetName("CClientTimeout::UNKNOWN"); + StartMaxCycles(60, 1); + } + virtual ~CClientTimeout() {} + +protected: + virtual void RunJob() { + if (m_pClient) { + m_pClient->LoginTimeout(); + m_pClient = NULL; + } + } + + CClient* m_pClient; +}; + #endif // !_TIMERS_H