From 7c64eba6936eb36b20c164677660c1b69f61da5e Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Thu, 16 Apr 2015 20:31:31 +0100 Subject: [PATCH] Last commit broke async auth, e.g. via imapauth. Fix it. See #946 --- src/HTTPSock.cpp | 27 ++++++++++++--------------- src/WebModules.cpp | 12 ++++++++++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/HTTPSock.cpp b/src/HTTPSock.cpp index 43c01958..456ff9c1 100644 --- a/src/HTTPSock.cpp +++ b/src/HTTPSock.cpp @@ -168,24 +168,21 @@ void CHTTPSock::ReadLine(const CString& sData) { sLine.Token(1, true).Split(",", ssEncodings, false, "", "", false, true); m_bAcceptGzip = (ssEncodings.find("gzip") != ssEncodings.end()); } else if (sLine.empty()) { - m_bGotHeader = true; - - if (!m_sUser.empty()) { + if (!m_sUser.empty() && !m_bLoggedIn) { m_bLoggedIn = OnLogin(m_sUser, m_sPass, true); - if (!m_bLoggedIn) { - // Error message already was sent - return; - } - } - - if (m_bPost) { - m_sPostData = GetInternalReadBuffer(); - CheckPost(); + // After successful login ReadLine("") will be called again to trigger "else" block } else { - GetPage(); - } + m_bGotHeader = true; - DisableReadLine(); + if (m_bPost) { + m_sPostData = GetInternalReadBuffer(); + CheckPost(); + } else { + GetPage(); + } + + DisableReadLine(); + } } } diff --git a/src/WebModules.cpp b/src/WebModules.cpp index 6928d1d8..a76db823 100644 --- a/src/WebModules.cpp +++ b/src/WebModules.cpp @@ -161,7 +161,9 @@ void CWebAuth::AcceptedLogin(CUser& User) { m_pWebSock->SetLoggedIn(true); m_pWebSock->UnPauseRead(); - if (!m_bBasic) { + if (m_bBasic) { + m_pWebSock->ReadLine(""); + } else { m_pWebSock->Redirect("/?cookie_check=true"); } @@ -178,7 +180,13 @@ void CWebAuth::RefusedLogin(const CString& sReason) { m_pWebSock->SetLoggedIn(false); m_pWebSock->UnPauseRead(); - m_pWebSock->Redirect("/?cookie_check=true"); + if (m_bBasic) { + m_pWebSock->AddHeader("WWW-Authenticate", "Basic realm=\"ZNC\""); + m_pWebSock->CHTTPSock::PrintErrorPage(401, "Unauthorized", "HTTP Basic authentication attemped with invalid credentials"); + // Why CWebSock makes this function protected?.. + } else { + m_pWebSock->Redirect("/?cookie_check=true"); + } DEBUG("UNSUCCESSFUL login attempt ==> REASON [" + sReason + "] ==> SESSION [" + spSession->GetId() + "]"); }