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