2013-11-20 14:18:57 +01:00
|
|
|
// Copyright (c) 2010 Satoshi Nakamoto
|
2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
2014-11-20 10:19:29 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-20 14:18:57 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2019-06-20 02:39:38 +09:00
|
|
|
#include <rpc/request.h>
|
|
|
|
|
2023-03-23 12:23:29 +01:00
|
|
|
#include <common/args.h>
|
|
|
|
#include <logging.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <random.h>
|
2019-06-20 02:39:38 +09:00
|
|
|
#include <rpc/protocol.h>
|
2024-01-08 15:02:44 +00:00
|
|
|
#include <util/fs.h>
|
2023-03-06 23:41:46 +01:00
|
|
|
#include <util/fs_helpers.h>
|
2018-10-22 15:51:11 -07:00
|
|
|
#include <util/strencodings.h>
|
2013-11-20 14:18:57 +01:00
|
|
|
|
2020-06-11 08:58:46 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-11-20 10:19:29 +08:00
|
|
|
/**
|
|
|
|
* JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility,
|
|
|
|
* but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
|
|
|
|
* unspecified (HTTP errors and contents of 'error').
|
2016-09-29 16:45:19 +02:00
|
|
|
*
|
2014-11-20 10:19:29 +08:00
|
|
|
* 1.0 spec: http://json-rpc.org/wiki/specification
|
|
|
|
* 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
|
2023-03-01 14:03:26 -05:00
|
|
|
*
|
|
|
|
* If the server receives a request with the JSON-RPC 2.0 marker `{"jsonrpc": "2.0"}`
|
|
|
|
* then Bitcoin will respond with a strictly specified response.
|
|
|
|
* It will only return an HTTP error code if an actual HTTP error is encountered
|
|
|
|
* such as the endpoint is not found (404) or the request is not formatted correctly (500).
|
|
|
|
* Otherwise the HTTP code is always OK (200) and RPC errors will be included in the
|
|
|
|
* response body.
|
|
|
|
*
|
|
|
|
* 2.0 spec: https://www.jsonrpc.org/specification
|
|
|
|
*
|
|
|
|
* Also see http://www.simple-is-better.org/rpc/#differences-between-1-0-and-2-0
|
2014-11-20 10:19:29 +08:00
|
|
|
*/
|
2013-11-20 14:18:57 +01:00
|
|
|
|
2017-01-04 13:22:19 +09:00
|
|
|
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
|
2013-11-20 14:18:57 +01:00
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue request(UniValue::VOBJ);
|
2017-09-22 20:04:07 +02:00
|
|
|
request.pushKV("method", strMethod);
|
|
|
|
request.pushKV("params", params);
|
|
|
|
request.pushKV("id", id);
|
2024-06-06 10:25:48 -04:00
|
|
|
request.pushKV("jsonrpc", "2.0");
|
2016-09-29 18:48:27 +02:00
|
|
|
return request;
|
2013-11-20 14:18:57 +01:00
|
|
|
}
|
|
|
|
|
2023-07-07 15:06:35 -04:00
|
|
|
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version)
|
2013-11-20 14:18:57 +01:00
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue reply(UniValue::VOBJ);
|
2023-07-07 14:41:23 -04:00
|
|
|
// Add JSON-RPC version number field in v2 only.
|
|
|
|
if (jsonrpc_version == JSONRPCVersion::V2) reply.pushKV("jsonrpc", "2.0");
|
|
|
|
|
|
|
|
// Add both result and error fields in v1, even though one will be null.
|
|
|
|
// Omit the null field in v2.
|
|
|
|
if (error.isNull()) {
|
2024-01-23 10:33:26 -05:00
|
|
|
reply.pushKV("result", std::move(result));
|
2023-07-07 14:41:23 -04:00
|
|
|
if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("error", NullUniValue);
|
|
|
|
} else {
|
|
|
|
if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("result", NullUniValue);
|
|
|
|
reply.pushKV("error", std::move(error));
|
|
|
|
}
|
2023-07-07 15:06:35 -04:00
|
|
|
if (id.has_value()) reply.pushKV("id", std::move(id.value()));
|
2013-11-20 14:18:57 +01:00
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2017-01-04 13:22:19 +09:00
|
|
|
UniValue JSONRPCError(int code, const std::string& message)
|
2013-11-20 14:18:57 +01:00
|
|
|
{
|
2015-05-10 13:35:44 +02:00
|
|
|
UniValue error(UniValue::VOBJ);
|
2017-09-22 20:04:07 +02:00
|
|
|
error.pushKV("code", code);
|
|
|
|
error.pushKV("message", message);
|
2013-11-20 14:18:57 +01:00
|
|
|
return error;
|
|
|
|
}
|
2015-07-07 14:53:48 +02:00
|
|
|
|
|
|
|
/** Username used when cookie authentication is in use (arbitrary, only for
|
|
|
|
* recognizability in debugging/logging purposes)
|
|
|
|
*/
|
|
|
|
static const std::string COOKIEAUTH_USER = "__cookie__";
|
|
|
|
/** Default name for auth cookie file */
|
2022-03-25 21:37:28 +01:00
|
|
|
static const char* const COOKIEAUTH_FILE = ".cookie";
|
2015-07-07 14:53:48 +02:00
|
|
|
|
2017-08-25 12:39:30 +02:00
|
|
|
/** Get name of RPC authentication cookie file */
|
|
|
|
static fs::path GetAuthCookieFile(bool temp=false)
|
2015-07-07 14:53:48 +02:00
|
|
|
{
|
2022-03-25 21:37:28 +01:00
|
|
|
fs::path arg = gArgs.GetPathArg("-rpccookiefile", COOKIEAUTH_FILE);
|
2017-08-25 12:39:30 +02:00
|
|
|
if (temp) {
|
|
|
|
arg += ".tmp";
|
|
|
|
}
|
2023-02-27 14:21:13 -05:00
|
|
|
return AbsPathForConfigVal(gArgs, arg);
|
2015-07-07 14:53:48 +02:00
|
|
|
}
|
|
|
|
|
2023-11-03 16:34:42 +02:00
|
|
|
static bool g_generated_cookie = false;
|
|
|
|
|
2024-01-08 15:02:44 +00:00
|
|
|
bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie_perms)
|
2015-07-07 14:53:48 +02:00
|
|
|
{
|
2016-10-01 16:57:25 +02:00
|
|
|
const size_t COOKIE_SIZE = 32;
|
|
|
|
unsigned char rand_pwd[COOKIE_SIZE];
|
2022-01-31 19:29:33 +07:00
|
|
|
GetRandBytes(rand_pwd);
|
2020-06-24 17:26:47 +02:00
|
|
|
std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
|
2015-07-07 14:53:48 +02:00
|
|
|
|
|
|
|
/** the umask determines what permissions are used to create this file -
|
2023-05-08 11:32:13 +02:00
|
|
|
* these are set to 0077 in common/system.cpp.
|
2015-07-07 14:53:48 +02:00
|
|
|
*/
|
2020-06-11 08:58:46 +02:00
|
|
|
std::ofstream file;
|
2017-08-25 12:39:30 +02:00
|
|
|
fs::path filepath_tmp = GetAuthCookieFile(true);
|
2018-08-04 13:32:13 +00:00
|
|
|
file.open(filepath_tmp);
|
2015-07-07 14:53:48 +02:00
|
|
|
if (!file.is_open()) {
|
2024-01-08 15:02:44 +00:00
|
|
|
LogInfo("Unable to open cookie authentication file %s for writing\n", fs::PathToString(filepath_tmp));
|
2015-07-07 14:53:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
file << cookie;
|
|
|
|
file.close();
|
2017-08-25 12:39:30 +02:00
|
|
|
|
|
|
|
fs::path filepath = GetAuthCookieFile(false);
|
|
|
|
if (!RenameOver(filepath_tmp, filepath)) {
|
2024-01-08 15:02:44 +00:00
|
|
|
LogInfo("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
|
2017-08-25 12:39:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2024-01-08 15:02:44 +00:00
|
|
|
if (cookie_perms) {
|
|
|
|
std::error_code code;
|
|
|
|
fs::permissions(filepath, cookie_perms.value(), fs::perm_options::replace, code);
|
|
|
|
if (code) {
|
|
|
|
LogInfo("Unable to set permissions on cookie authentication file %s\n", fs::PathToString(filepath_tmp));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 16:34:42 +02:00
|
|
|
g_generated_cookie = true;
|
2024-01-08 15:02:44 +00:00
|
|
|
LogInfo("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
|
|
|
|
LogInfo("Permissions used for cookie: %s\n", PermsToSymbolicString(fs::status(filepath).permissions()));
|
2015-07-07 14:53:48 +02:00
|
|
|
|
|
|
|
if (cookie_out)
|
|
|
|
*cookie_out = cookie;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetAuthCookie(std::string *cookie_out)
|
|
|
|
{
|
2020-06-11 08:58:46 +02:00
|
|
|
std::ifstream file;
|
2015-07-07 14:53:48 +02:00
|
|
|
std::string cookie;
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path filepath = GetAuthCookieFile();
|
2018-08-04 13:32:13 +00:00
|
|
|
file.open(filepath);
|
2015-07-07 14:53:48 +02:00
|
|
|
if (!file.is_open())
|
|
|
|
return false;
|
|
|
|
std::getline(file, cookie);
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (cookie_out)
|
|
|
|
*cookie_out = cookie;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteAuthCookie()
|
|
|
|
{
|
|
|
|
try {
|
2023-11-03 16:34:42 +02:00
|
|
|
if (g_generated_cookie) {
|
|
|
|
// Delete the cookie file if it was generated by this process
|
|
|
|
fs::remove(GetAuthCookieFile());
|
|
|
|
}
|
2017-03-01 17:05:50 +01:00
|
|
|
} catch (const fs::filesystem_error& e) {
|
2018-09-11 02:08:56 +08:00
|
|
|
LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
|
2015-07-07 14:53:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 21:46:16 +02:00
|
|
|
std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in)
|
2016-09-29 16:45:19 +02:00
|
|
|
{
|
|
|
|
if (!in.isArray()) {
|
|
|
|
throw std::runtime_error("Batch must be an array");
|
|
|
|
}
|
2020-04-12 21:46:16 +02:00
|
|
|
const size_t num {in.size()};
|
2016-09-29 16:45:19 +02:00
|
|
|
std::vector<UniValue> batch(num);
|
2020-04-12 21:46:16 +02:00
|
|
|
for (const UniValue& rec : in.getValues()) {
|
2016-09-29 16:45:19 +02:00
|
|
|
if (!rec.isObject()) {
|
2020-04-12 21:46:16 +02:00
|
|
|
throw std::runtime_error("Batch member must be an object");
|
2016-09-29 16:45:19 +02:00
|
|
|
}
|
2022-05-13 12:32:59 +02:00
|
|
|
size_t id = rec["id"].getInt<int>();
|
2016-09-29 16:45:19 +02:00
|
|
|
if (id >= num) {
|
2020-04-12 21:46:16 +02:00
|
|
|
throw std::runtime_error("Batch member id is larger than batch size");
|
2016-09-29 16:45:19 +02:00
|
|
|
}
|
|
|
|
batch[id] = rec;
|
|
|
|
}
|
|
|
|
return batch;
|
|
|
|
}
|
2019-06-20 02:39:38 +09:00
|
|
|
|
|
|
|
void JSONRPCRequest::parse(const UniValue& valRequest)
|
|
|
|
{
|
|
|
|
// Parse request
|
|
|
|
if (!valRequest.isObject())
|
|
|
|
throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
|
|
|
|
const UniValue& request = valRequest.get_obj();
|
|
|
|
|
|
|
|
// Parse id now so errors from here on will have the id
|
2023-07-07 15:06:35 -04:00
|
|
|
if (request.exists("id")) {
|
|
|
|
id = request.find_value("id");
|
|
|
|
} else {
|
|
|
|
id = std::nullopt;
|
|
|
|
}
|
2019-06-20 02:39:38 +09:00
|
|
|
|
2023-07-07 14:31:18 -04:00
|
|
|
// Check for JSON-RPC 2.0 (default 1.1)
|
|
|
|
m_json_version = JSONRPCVersion::V1_LEGACY;
|
|
|
|
const UniValue& jsonrpc_version = request.find_value("jsonrpc");
|
|
|
|
if (!jsonrpc_version.isNull()) {
|
|
|
|
if (!jsonrpc_version.isStr()) {
|
|
|
|
throw JSONRPCError(RPC_INVALID_REQUEST, "jsonrpc field must be a string");
|
|
|
|
}
|
|
|
|
// The "jsonrpc" key was added in the 2.0 spec, but some older documentation
|
|
|
|
// incorrectly included {"jsonrpc":"1.0"} in a request object, so we
|
|
|
|
// maintain that for backwards compatibility.
|
|
|
|
if (jsonrpc_version.get_str() == "1.0") {
|
|
|
|
m_json_version = JSONRPCVersion::V1_LEGACY;
|
|
|
|
} else if (jsonrpc_version.get_str() == "2.0") {
|
|
|
|
m_json_version = JSONRPCVersion::V2;
|
|
|
|
} else {
|
|
|
|
throw JSONRPCError(RPC_INVALID_REQUEST, "JSON-RPC version not supported");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 02:39:38 +09:00
|
|
|
// Parse method
|
2023-05-09 11:26:58 +02:00
|
|
|
const UniValue& valMethod{request.find_value("method")};
|
2019-06-20 02:39:38 +09:00
|
|
|
if (valMethod.isNull())
|
|
|
|
throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
|
|
|
|
if (!valMethod.isStr())
|
|
|
|
throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
|
|
|
|
strMethod = valMethod.get_str();
|
|
|
|
if (fLogIPs)
|
|
|
|
LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod),
|
|
|
|
this->authUser, this->peerAddr);
|
|
|
|
else
|
|
|
|
LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
|
|
|
|
|
|
|
|
// Parse params
|
2023-05-09 11:26:58 +02:00
|
|
|
const UniValue& valParams{request.find_value("params")};
|
2019-06-20 02:39:38 +09:00
|
|
|
if (valParams.isArray() || valParams.isObject())
|
|
|
|
params = valParams;
|
|
|
|
else if (valParams.isNull())
|
|
|
|
params = UniValue(UniValue::VARR);
|
|
|
|
else
|
|
|
|
throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object");
|
|
|
|
}
|