From be354f113ef5731574153b34dde31f87607ced80 Mon Sep 17 00:00:00 2001 From: prozacx Date: Thu, 16 Mar 2006 00:23:55 +0000 Subject: [PATCH] Added auth class git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@690 726aef4b-f618-498e-8847-2d620e286838 --- Client.cpp | 106 ++++++++++++++++++++++++++++++++++++++++------------- Client.h | 58 ++++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 26 deletions(-) diff --git a/Client.cpp b/Client.cpp index 5ee117d8..ca3bc949 100644 --- a/Client.cpp +++ b/Client.cpp @@ -1,7 +1,7 @@ #include "main.h" #include "Client.h" -#include "User.h" #include "znc.h" +#include "User.h" #include "IRCSock.h" #include "DCCBounce.h" #include "DCCSock.h" @@ -597,7 +597,11 @@ void CClient::UserCommand(const CString& sLine) { PutStatus("There is no MOTD set."); } } else if (m_pUser->IsAdmin() && sCommand.CaseCmp("SaveConfig") == 0) { - CZNC::Get().WriteConfig(); + if (CZNC::Get().WriteConfig()) { + PutStatus("Wrote config to [" + CZNC::Get().GetConfigFile() + "]"); + } else { + PutStatus("Error while trying to write config."); + } } else if (sCommand.CaseCmp("LISTCLIENTS") == 0) { if (m_pUser) { CUser* pUser = m_pUser; @@ -1187,39 +1191,91 @@ bool CClient::ConnectionFrom(const CString& sHost, unsigned short uPort) { } void CClient::AuthUser() { + /* +#ifdef _MODULES + if (CZNC::Get().GetModules().OnLoginAttempt(m_sUser, m_sPass, *this)) { + return; + } +#endif + CUser* pUser = CZNC::Get().GetUser(m_sUser); - if ((!pUser) || (!pUser->CheckPass(m_sPass))) { - if (pUser) { - pUser->PutStatus("Another user attempted to login as you, with a bad password."); - } - - PutStatus("Bad username and/or password."); - PutClient(":irc.znc.com 464 " + GetNick() + " :Password Incorrect"); - Close(); + if (pUser && pUser->CheckPass(m_sPass)) { + AcceptLogin(*pUser); } else { - m_sPass = ""; - m_pUser = pUser; - - if (!m_pUser->IsHostAllowed(GetRemoteIP())) { - Close(); - return; + if (pUser) { + pUser->PutStatus("Another client attempted to login as you, with a bad password."); } - m_bAuthed = true; - SetSockName("USR::" + pUser->GetUserName()); + RefuseLogin(); + } + */ - m_pIRCSock = (CIRCSock*) CZNC::Get().FindSockByName("IRC::" + pUser->GetUserName()); - m_pUser->UserConnected(this); + m_spAuth = new CClientAuth(this, m_sUser, m_sPass); - SendMotd(); + CClientAuth::AuthUser(m_spAuth); +} + +void CAuthBase::AuthUser(CSmartPtr AuthClass) { +#ifdef _MODULES + if (CZNC::Get().GetModules().OnLoginAttempt(AuthClass)) { + return; + } +#endif + + CUser* pUser = CZNC::Get().GetUser(AuthClass->GetUsername()); + + if (pUser && pUser->CheckPass(AuthClass->GetPassword())) { + AuthClass->AcceptLogin(*pUser); + } else { + if (pUser) { + pUser->PutStatus("Another client attempted to login as you, with a bad password."); + } + + AuthClass->RefuseLogin("Invalid Password"); + } +} + +void CClientAuth::RefuseLogin(const CString& sReason) { + if (m_pClient) { + m_pClient->RefuseLogin(sReason); + } +} + +void CClient::RefuseLogin(const CString& sReason) { + PutStatus("Bad username and/or password."); + PutClient(":irc.znc.com 464 " + GetNick() + " :" + sReason); + Close(); +} + +void CClientAuth::AcceptLogin(CUser& User) { + if (m_pClient) { + m_pClient->AcceptLogin(User); + } +} + +void CClient::AcceptLogin(CUser& User) { + m_sPass = ""; + m_pUser = &User; + + if (!m_pUser->IsHostAllowed(GetRemoteIP())) { + Close(); + return; + } + + m_bAuthed = true; + SetSockName("USR::" + m_pUser->GetUserName()); + + m_pIRCSock = (CIRCSock*) CZNC::Get().FindSockByName("IRC::" + m_pUser->GetUserName()); + m_pUser->UserConnected(this); + + SendMotd(); #ifdef _MODULES - CZNC::Get().GetModules().SetClient(this); - VOIDMODULECALL(OnUserAttached()); - CZNC::Get().GetModules().SetClient(NULL); + CZNC::Get().GetModules().SetClient(this); + VOIDMODULECALL(OnUserAttached()); + CZNC::Get().GetModules().SetClient(NULL); #endif - } } void CClient::Connected() { diff --git a/Client.h b/Client.h index 671fbf4f..c70dbbe5 100644 --- a/Client.h +++ b/Client.h @@ -7,18 +7,70 @@ class CZNC; class CUser; class CIRCSock; +class CClient; // !Forward Declarations +class CAuthBase { +public: + CAuthBase() {} + CAuthBase(const CString& sUsername, const CString& sPassword) { + SetLoginInfo(sUsername, sPassword); + } + + virtual ~CAuthBase() {} + + virtual void SetLoginInfo(const CString& sUsername, const CString& sPassword) { + m_sUsername = sUsername; + m_sPassword = sPassword; + } + + virtual void AcceptLogin(CUser& User) = 0; + virtual void RefuseLogin(const CString& sReason) = 0; + + virtual const CString& GetUsername() const { return m_sUsername; } + virtual const CString& GetPassword() const { return m_sPassword; } + + static void AuthUser(CSmartPtr AuthClass); + +private: + CString m_sUsername; + CString m_sPassword; +}; + + +class CClientAuth : public CAuthBase { +public: + CClientAuth(CClient* pClient, const CString& sUsername, const CString& sPassword) : CAuthBase(sUsername, sPassword) { + m_pClient = pClient; + } + + virtual ~CClientAuth() {} + + void SetClient(CClient* pClient) { m_pClient = pClient; } + void AcceptLogin(CUser& User); + void RefuseLogin(const CString& sReason); +private: +protected: + CClient* m_pClient; +}; + class CClient : public Csock { public: CClient() : Csock() { Init(); } + CClient(const CString& sHostname, unsigned short uPort, int iTimeout = 60) : Csock(sHostname, uPort, iTimeout) { Init(); } - virtual ~CClient() {} + + virtual ~CClient() { + if (!m_spAuth.IsNull()) { + CClientAuth* pAuth = (CClientAuth*) &(*m_spAuth); + pAuth->SetClient(NULL); + } + } void Init() { m_pUser = NULL; @@ -31,6 +83,9 @@ public: EnableReadLine(); } + void AcceptLogin(CUser& User); + void RefuseLogin(const CString& sReason); + CString GetNick() const; CString GetNickMask() const; @@ -71,6 +126,7 @@ protected: CString m_sUser; CIRCSock* m_pIRCSock; unsigned int m_uKeepNickCounter; + CSmartPtr m_spAuth; }; #endif // !_CLIENT_H