2020-07-06 12:23:36 +00:00
|
|
|
#include "SchemaAllowedHandler.h"
|
|
|
|
|
|
|
|
#if USE_ODBC
|
|
|
|
|
2021-03-22 11:40:29 +00:00
|
|
|
#include <Server/HTTP/HTMLForm.h>
|
|
|
|
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
#include <Poco/Net/HTTPServerResponse.h>
|
|
|
|
#include <common/logger_useful.h>
|
|
|
|
#include "validateODBCConnectionString.h"
|
2021-03-31 12:41:12 +00:00
|
|
|
#include "ODBCConnectionFactory.h"
|
2021-03-22 11:40:29 +00:00
|
|
|
#include <sql.h>
|
|
|
|
#include <sqlext.h>
|
2020-07-06 12:23:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
2021-03-22 11:40:29 +00:00
|
|
|
bool isSchemaAllowed(nanodbc::connection & connection)
|
2020-07-06 12:23:36 +00:00
|
|
|
{
|
2021-03-22 11:40:29 +00:00
|
|
|
uint32_t result = connection.get_info<uint32_t>(SQL_SCHEMA_USAGE);
|
|
|
|
return result != 0;
|
2020-07-06 12:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
void SchemaAllowedHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response)
|
2020-07-06 12:23:36 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
HTMLForm params(request, request.getStream());
|
2020-07-06 12:23:36 +00:00
|
|
|
LOG_TRACE(log, "Request URI: {}", request.getURI());
|
|
|
|
|
|
|
|
auto process_error = [&response, this](const std::string & message)
|
|
|
|
{
|
|
|
|
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
if (!response.sent())
|
2021-02-19 12:51:26 +00:00
|
|
|
*response.send() << message << std::endl;
|
2020-07-06 12:23:36 +00:00
|
|
|
LOG_WARNING(log, message);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!params.has("connection_string"))
|
|
|
|
{
|
|
|
|
process_error("No 'connection_string' in request URL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string connection_string = params.get("connection_string");
|
2021-04-06 18:59:34 +00:00
|
|
|
|
|
|
|
auto connection = ODBCConnectionFactory::instance().get(
|
|
|
|
validateODBCConnectionString(connection_string),
|
2021-04-11 06:13:11 +00:00
|
|
|
getContext()->getSettingsRef().odbc_bridge_connection_pool_size);
|
2021-04-06 18:59:34 +00:00
|
|
|
|
2021-05-07 11:18:49 +00:00
|
|
|
bool result = isSchemaAllowed(connection->get());
|
2020-07-06 12:23:36 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
WriteBufferFromHTTPServerResponse out(response, request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD, keep_alive_timeout);
|
2021-02-20 05:31:05 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
writeBoolText(result, out);
|
|
|
|
out.finalize();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
out.finalize();
|
|
|
|
}
|
2020-07-06 12:23:36 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
process_error("Error getting schema usage from ODBC '" + getCurrentExceptionMessage(false) + "'");
|
|
|
|
tryLogCurrentException(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|