2015-06-22 11:11:21 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2015-06-22 11:11:21 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
#include "remote/httpserverconnection.hpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "remote/httphandler.hpp"
|
2017-02-09 09:13:58 +01:00
|
|
|
#include "remote/httputility.hpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
#include "remote/apifunction.hpp"
|
|
|
|
#include "remote/jsonrpc.hpp"
|
2017-09-11 16:51:13 +02:00
|
|
|
#include "base/base64.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include <boost/thread/once.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
static boost::once_flag l_HttpServerConnectionOnceFlag = BOOST_ONCE_INIT;
|
|
|
|
static Timer::Ptr l_HttpServerConnectionTimeoutTimer;
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const TlsStream::Ptr& stream)
|
2016-08-24 19:59:13 +02:00
|
|
|
: m_Stream(stream), m_Seen(Utility::GetTime()), m_CurrentRequest(stream), m_PendingRequests(0)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
2015-08-29 01:16:16 +02:00
|
|
|
boost::call_once(l_HttpServerConnectionOnceFlag, &HttpServerConnection::StaticInitialize);
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2016-06-14 08:19:13 +02:00
|
|
|
m_RequestQueue.SetName("HttpServerConnection");
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
if (authenticated)
|
2015-07-09 15:27:14 +02:00
|
|
|
m_ApiUser = ApiUser::GetByClientCN(identity);
|
2015-06-22 11:11:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void HttpServerConnection::StaticInitialize(void)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
2015-08-29 01:16:16 +02:00
|
|
|
l_HttpServerConnectionTimeoutTimer = new Timer();
|
|
|
|
l_HttpServerConnectionTimeoutTimer->OnTimerExpired.connect(boost::bind(&HttpServerConnection::TimeoutTimerHandler));
|
|
|
|
l_HttpServerConnectionTimeoutTimer->SetInterval(15);
|
|
|
|
l_HttpServerConnectionTimeoutTimer->Start();
|
2015-06-22 11:11:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void HttpServerConnection::Start(void)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
2015-11-02 17:34:01 +01:00
|
|
|
/* the stream holds an owning reference to this object through the callback we're registering here */
|
|
|
|
m_Stream->RegisterDataHandler(boost::bind(&HttpServerConnection::DataAvailableHandler, HttpServerConnection::Ptr(this)));
|
2015-06-22 11:11:21 +02:00
|
|
|
if (m_Stream->IsDataAvailable())
|
|
|
|
DataAvailableHandler();
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
ApiUser::Ptr HttpServerConnection::GetApiUser(void) const
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
|
|
|
return m_ApiUser;
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
TlsStream::Ptr HttpServerConnection::GetStream(void) const
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
|
|
|
return m_Stream;
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void HttpServerConnection::Disconnect(void)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
2015-08-29 01:16:16 +02:00
|
|
|
Log(LogDebug, "HttpServerConnection", "Http client disconnected");
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
|
|
|
listener->RemoveHttpClient(this);
|
|
|
|
|
2016-08-19 20:35:20 +02:00
|
|
|
m_CurrentRequest.~HttpRequest();
|
|
|
|
new (&m_CurrentRequest) HttpRequest(Stream::Ptr());
|
|
|
|
|
2016-08-22 16:35:16 +02:00
|
|
|
m_Stream->Close();
|
2015-06-22 11:11:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
bool HttpServerConnection::ProcessMessage(void)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
try {
|
2015-08-29 01:16:16 +02:00
|
|
|
res = m_CurrentRequest.Parse(m_Context, false);
|
2015-10-27 15:26:19 +01:00
|
|
|
} catch (const std::invalid_argument& ex) {
|
|
|
|
HttpResponse response(m_Stream, m_CurrentRequest);
|
|
|
|
response.SetStatus(400, "Bad request");
|
|
|
|
String msg = String("<h1>Bad request</h1><p><pre>") + ex.what() + "</pre></p>";
|
|
|
|
response.WriteBody(msg.CStr(), msg.GetLength());
|
|
|
|
response.Finish();
|
|
|
|
|
|
|
|
m_Stream->Shutdown();
|
|
|
|
return false;
|
2015-06-22 11:11:21 +02:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
HttpResponse response(m_Stream, m_CurrentRequest);
|
|
|
|
response.SetStatus(400, "Bad request");
|
2015-07-28 14:40:44 +02:00
|
|
|
String msg = "<h1>Bad request</h1><p><pre>" + DiagnosticInformation(ex) + "</pre></p>";
|
2015-06-22 11:11:21 +02:00
|
|
|
response.WriteBody(msg.CStr(), msg.GetLength());
|
2015-07-09 12:28:15 +02:00
|
|
|
response.Finish();
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
m_Stream->Shutdown();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_CurrentRequest.Complete) {
|
2015-09-22 17:58:12 +02:00
|
|
|
m_RequestQueue.Enqueue(boost::bind(&HttpServerConnection::ProcessMessageAsync,
|
|
|
|
HttpServerConnection::Ptr(this), m_CurrentRequest));
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
m_Seen = Utility::GetTime();
|
|
|
|
m_PendingRequests++;
|
|
|
|
|
|
|
|
m_CurrentRequest.~HttpRequest();
|
2015-08-29 01:16:16 +02:00
|
|
|
new (&m_CurrentRequest) HttpRequest(m_Stream);
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void HttpServerConnection::ProcessMessageAsync(HttpRequest& request)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
2015-07-09 15:27:14 +02:00
|
|
|
String auth_header = request.Headers->Get("authorization");
|
|
|
|
|
|
|
|
String::SizeType pos = auth_header.FindFirstOf(" ");
|
|
|
|
String username, password;
|
|
|
|
|
|
|
|
if (pos != String::NPos && auth_header.SubStr(0, pos) == "Basic") {
|
|
|
|
String credentials_base64 = auth_header.SubStr(pos + 1);
|
|
|
|
String credentials = Base64::Decode(credentials_base64);
|
|
|
|
|
|
|
|
String::SizeType cpos = credentials.FindFirstOf(":");
|
|
|
|
|
|
|
|
if (cpos != String::NPos) {
|
|
|
|
username = credentials.SubStr(0, cpos);
|
|
|
|
password = credentials.SubStr(cpos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ApiUser::Ptr user;
|
|
|
|
|
2016-04-04 16:38:47 +02:00
|
|
|
/* client_cn matched. */
|
2015-07-09 15:27:14 +02:00
|
|
|
if (m_ApiUser)
|
|
|
|
user = m_ApiUser;
|
|
|
|
else {
|
|
|
|
user = ApiUser::GetByName(username);
|
|
|
|
|
2016-04-04 16:38:47 +02:00
|
|
|
/* Deny authentication if 1) given password is empty 2) configured password does not match. */
|
|
|
|
if (password.IsEmpty())
|
|
|
|
user.reset();
|
|
|
|
else if (user && user->GetPassword() != password)
|
2015-07-09 15:27:14 +02:00
|
|
|
user.reset();
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:04:58 +01:00
|
|
|
String requestUrl = request.RequestUrl->Format();
|
2015-10-16 15:32:02 +02:00
|
|
|
|
2015-09-30 08:41:09 +02:00
|
|
|
Log(LogInformation, "HttpServerConnection")
|
2015-10-16 15:32:02 +02:00
|
|
|
<< "Request: " << request.RequestMethod << " " << requestUrl
|
2016-07-25 09:43:13 +02:00
|
|
|
<< " (from " << m_Stream->GetSocket()->GetPeerAddress() << ", user: " << (user ? user->GetName() : "<unauthenticated>") << ")";
|
2015-09-30 08:41:09 +02:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
HttpResponse response(m_Stream, request);
|
2015-07-09 15:27:14 +02:00
|
|
|
|
2015-11-05 15:18:53 +01:00
|
|
|
String accept_header = request.Headers->Get("accept");
|
|
|
|
|
|
|
|
if (request.RequestMethod != "GET" && accept_header != "application/json") {
|
|
|
|
response.SetStatus(400, "Wrong Accept header");
|
|
|
|
response.AddHeader("Content-Type", "text/html");
|
|
|
|
String msg = "<h1>Accept header is missing or not set to 'application/json'.</h1>";
|
|
|
|
response.WriteBody(msg.CStr(), msg.GetLength());
|
|
|
|
} else if (!user) {
|
2015-10-16 15:32:02 +02:00
|
|
|
Log(LogWarning, "HttpServerConnection")
|
|
|
|
<< "Unauthorized request: " << request.RequestMethod << " " << requestUrl;
|
2017-02-09 09:13:58 +01:00
|
|
|
|
2015-07-09 15:27:14 +02:00
|
|
|
response.SetStatus(401, "Unauthorized");
|
|
|
|
response.AddHeader("WWW-Authenticate", "Basic realm=\"Icinga 2\"");
|
2017-02-09 09:13:58 +01:00
|
|
|
|
|
|
|
if (request.Headers->Get("accept") == "application/json") {
|
|
|
|
Dictionary::Ptr result = new Dictionary();
|
2017-02-09 12:20:46 +01:00
|
|
|
|
|
|
|
result->Set("error", 401);
|
|
|
|
result->Set("status", "Unauthorized. Please check your user credentials.");
|
2017-02-09 09:13:58 +01:00
|
|
|
|
|
|
|
HttpUtility::SendJsonBody(response, result);
|
|
|
|
} else {
|
|
|
|
response.AddHeader("Content-Type", "text/html");
|
|
|
|
String msg = "<h1>Unauthorized. Please check your user credentials.</h1>";
|
|
|
|
response.WriteBody(msg.CStr(), msg.GetLength());
|
|
|
|
}
|
2015-07-09 15:27:14 +02:00
|
|
|
} else {
|
2015-07-21 16:10:13 +02:00
|
|
|
try {
|
|
|
|
HttpHandler::ProcessRequest(user, request, response);
|
|
|
|
} catch (const std::exception& ex) {
|
2015-09-29 10:31:16 +02:00
|
|
|
Log(LogCritical, "HttpServerConnection")
|
|
|
|
<< "Unhandled exception while processing Http request: " << DiagnosticInformation(ex);
|
2015-07-21 16:10:13 +02:00
|
|
|
response.SetStatus(503, "Unhandled exception");
|
2017-02-09 09:13:58 +01:00
|
|
|
|
2015-07-21 16:10:13 +02:00
|
|
|
String errorInfo = DiagnosticInformation(ex);
|
2017-02-09 09:13:58 +01:00
|
|
|
|
|
|
|
if (request.Headers->Get("accept") == "application/json") {
|
|
|
|
Dictionary::Ptr result = new Dictionary();
|
2017-02-09 12:20:46 +01:00
|
|
|
|
|
|
|
result->Set("error", 503);
|
|
|
|
result->Set("status", errorInfo);
|
2017-02-09 09:13:58 +01:00
|
|
|
|
|
|
|
HttpUtility::SendJsonBody(response, result);
|
|
|
|
} else {
|
|
|
|
response.AddHeader("Content-Type", "text/plain");
|
|
|
|
response.WriteBody(errorInfo.CStr(), errorInfo.GetLength());
|
|
|
|
}
|
2015-07-21 16:10:13 +02:00
|
|
|
}
|
2015-07-09 15:27:14 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
response.Finish();
|
|
|
|
|
|
|
|
m_PendingRequests--;
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void HttpServerConnection::DataAvailableHandler(void)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
2016-02-01 08:35:55 +01:00
|
|
|
bool close = false;
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2016-02-01 08:35:55 +01:00
|
|
|
if (!m_Stream->IsEof()) {
|
|
|
|
boost::mutex::scoped_lock lock(m_DataHandlerMutex);
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2016-02-01 08:35:55 +01:00
|
|
|
try {
|
|
|
|
while (ProcessMessage())
|
|
|
|
; /* empty loop body */
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogWarning, "HttpServerConnection")
|
|
|
|
<< "Error while reading Http request: " << DiagnosticInformation(ex);
|
2016-01-13 10:30:38 +01:00
|
|
|
|
2016-02-01 08:35:55 +01:00
|
|
|
close = true;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
close = true;
|
2016-01-13 10:30:38 +01:00
|
|
|
|
2016-02-01 08:35:55 +01:00
|
|
|
if (close)
|
2016-01-13 10:30:38 +01:00
|
|
|
Disconnect();
|
2015-06-22 11:11:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void HttpServerConnection::CheckLiveness(void)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
|
|
|
if (m_Seen < Utility::GetTime() - 10 && m_PendingRequests == 0) {
|
2015-08-29 01:16:16 +02:00
|
|
|
Log(LogInformation, "HttpServerConnection")
|
2015-06-22 11:11:21 +02:00
|
|
|
<< "No messages for Http connection have been received in the last 10 seconds.";
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void HttpServerConnection::TimeoutTimerHandler(void)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const HttpServerConnection::Ptr& client : listener->GetHttpClients()) {
|
2015-06-22 11:11:21 +02:00
|
|
|
client->CheckLiveness();
|
|
|
|
}
|
|
|
|
}
|
2015-09-22 17:58:12 +02:00
|
|
|
|