icinga2/lib/remote/configfileshandler.cpp

90 lines
2.6 KiB
C++
Raw Permalink Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "remote/configfileshandler.hpp"
#include "remote/configpackageutility.hpp"
#include "remote/httputility.hpp"
2015-09-28 02:57:25 -04:00
#include "remote/filterutility.hpp"
#include "base/exception.hpp"
2019-04-01 12:54:13 -04:00
#include "base/utility.hpp"
#include <boost/algorithm/string/join.hpp>
#include <fstream>
using namespace icinga;
REGISTER_URLHANDLER("/v1/config/files", ConfigFilesHandler);
bool ConfigFilesHandler::HandleRequest(
const WaitGroup::Ptr&,
const HttpRequest& request,
HttpResponse& response,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;
auto url = request.Url();
auto user = request.User();
auto params = request.Params();
if (request.method() != http::verb::get)
2015-09-28 10:08:14 -04:00
return false;
const std::vector<String>& urlPath = url->GetPath();
if (urlPath.size() >= 4)
params->Set("package", urlPath[3]);
if (urlPath.size() >= 5)
params->Set("stage", urlPath[4]);
if (urlPath.size() >= 6) {
std::vector<String> tmpPath(urlPath.begin() + 5, urlPath.end());
params->Set("path", boost::algorithm::join(tmpPath, "/"));
}
if (request[http::field::accept] == "application/json") {
HttpUtility::SendJsonError(response, params, 400, "Invalid Accept header. Either remove the Accept header or set it to 'application/octet-stream'.");
return true;
}
2015-09-28 02:57:25 -04:00
FilterUtility::CheckPermission(user, "config/query");
String packageName = HttpUtility::GetLastParameter(params, "package");
2015-07-29 07:39:58 -04:00
String stageName = HttpUtility::GetLastParameter(params, "stage");
if (!ConfigPackageUtility::ValidatePackageName(packageName)) {
HttpUtility::SendJsonError(response, params, 400, "Invalid package name.");
2015-09-28 10:08:14 -04:00
return true;
}
if (!ConfigPackageUtility::ValidateStageName(stageName)) {
HttpUtility::SendJsonError(response, params, 400, "Invalid stage name.");
2015-09-28 10:08:14 -04:00
return true;
}
2015-07-29 07:39:58 -04:00
String relativePath = HttpUtility::GetLastParameter(params, "path");
2015-09-28 10:08:14 -04:00
if (ConfigPackageUtility::ContainsDotDot(relativePath)) {
HttpUtility::SendJsonError(response, params, 400, "Path contains '..' (not allowed).");
2015-09-28 10:08:14 -04:00
return true;
}
String path = ConfigPackageUtility::GetPackageDir() + "/" + packageName + "/" + stageName + "/" + relativePath;
2015-09-28 10:08:14 -04:00
if (!Utility::PathExists(path)) {
HttpUtility::SendJsonError(response, params, 404, "Path not found.");
2015-09-28 10:08:14 -04:00
return true;
}
try {
response.result(http::status::ok);
response.set(http::field::content_type, "application/octet-stream");
response.SendFile(path, yc);
} catch (const std::exception& ex) {
HttpUtility::SendJsonError(response, params, 500, "Could not read file.",
DiagnosticInformation(ex));
}
2015-09-28 10:08:14 -04:00
return true;
}