2013-11-20 14:18:57 +01:00
|
|
|
// Copyright (c) 2010 Satoshi Nakamoto
|
2022-02-27 08:46:53 +01:00
|
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
|
|
|
// Copyright (c) 2014-2022 The Dash Core developers
|
2014-11-20 03:19:29 +01: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.
|
|
|
|
|
2022-02-27 08:46:53 +01:00
|
|
|
#include <rpc/request.h>
|
2013-11-20 14:18:57 +01:00
|
|
|
|
2022-02-27 08:46:53 +01:00
|
|
|
#include <fs.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <random.h>
|
2022-02-27 08:46:53 +01:00
|
|
|
#include <rpc/protocol.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/strencodings.h>
|
2013-11-20 14:18:57 +01:00
|
|
|
|
2014-11-20 03:19:29 +01: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').
|
2017-09-28 08:33:12 +02:00
|
|
|
*
|
2014-11-20 03:19:29 +01:00
|
|
|
* 1.0 spec: http://json-rpc.org/wiki/specification
|
|
|
|
* 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
|
|
|
|
*/
|
2013-11-20 14:18:57 +01:00
|
|
|
|
2017-03-09 08:14:27 +01: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);
|
2020-06-18 11:17:23 +02:00
|
|
|
request.pushKV("method", strMethod);
|
|
|
|
request.pushKV("params", params);
|
|
|
|
request.pushKV("id", id);
|
2016-10-19 15:01:33 +02:00
|
|
|
return request;
|
2013-11-20 14:18:57 +01:00
|
|
|
}
|
|
|
|
|
2015-05-18 14:02:18 +02:00
|
|
|
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
|
2013-11-20 14:18:57 +01:00
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue reply(UniValue::VOBJ);
|
2014-08-20 21:15:16 +02:00
|
|
|
if (!error.isNull())
|
2020-06-18 11:17:23 +02:00
|
|
|
reply.pushKV("result", NullUniValue);
|
2013-11-20 14:18:57 +01:00
|
|
|
else
|
2020-06-18 11:17:23 +02:00
|
|
|
reply.pushKV("result", result);
|
|
|
|
reply.pushKV("error", error);
|
|
|
|
reply.pushKV("id", id);
|
2013-11-20 14:18:57 +01:00
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:14:27 +01:00
|
|
|
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
|
2014-06-29 03:14:36 +02:00
|
|
|
{
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue reply = JSONRPCReplyObj(result, error, id);
|
2014-08-20 21:15:16 +02:00
|
|
|
return reply.write() + "\n";
|
2014-06-29 03:14:36 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:14:27 +01: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);
|
2020-06-18 11:17:23 +02:00
|
|
|
error.pushKV("code", code);
|
|
|
|
error.pushKV("message", message);
|
2013-11-20 14:18:57 +01:00
|
|
|
return error;
|
2014-06-29 03:14:36 +02:00
|
|
|
}
|
2014-06-04 17:24:43 +02:00
|
|
|
|
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 */
|
|
|
|
static const std::string COOKIEAUTH_FILE = ".cookie";
|
2014-08-06 13:01:49 +02:00
|
|
|
|
2017-08-28 09:40:08 +02:00
|
|
|
/** Get name of RPC authentication cookie file */
|
|
|
|
static fs::path GetAuthCookieFile(bool temp=false)
|
2014-08-06 13:01:49 +02:00
|
|
|
{
|
2017-08-28 09:40:08 +02:00
|
|
|
std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE);
|
|
|
|
if (temp) {
|
|
|
|
arg += ".tmp";
|
|
|
|
}
|
2018-02-06 16:00:41 +01:00
|
|
|
return AbsPathForConfigVal(fs::path(arg));
|
2013-11-20 14:18:57 +01:00
|
|
|
}
|
|
|
|
|
2015-07-07 14:53:48 +02:00
|
|
|
bool GenerateAuthCookie(std::string *cookie_out)
|
2013-11-20 14:18:57 +01:00
|
|
|
{
|
2017-05-05 13:34:34 +02:00
|
|
|
const size_t COOKIE_SIZE = 32;
|
|
|
|
unsigned char rand_pwd[COOKIE_SIZE];
|
|
|
|
GetRandBytes(rand_pwd, COOKIE_SIZE);
|
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 -
|
|
|
|
* these are set to 077 in init.cpp unless overridden with -sysperms.
|
|
|
|
*/
|
2018-08-04 15:27:38 +02:00
|
|
|
fsbridge::ofstream file;
|
2017-08-28 09:40:08 +02:00
|
|
|
fs::path filepath_tmp = GetAuthCookieFile(true);
|
2018-08-04 15:27:38 +02:00
|
|
|
file.open(filepath_tmp);
|
2015-07-07 14:53:48 +02:00
|
|
|
if (!file.is_open()) {
|
2017-08-28 09:40:08 +02:00
|
|
|
LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
|
2013-11-20 14:18:57 +01:00
|
|
|
return false;
|
2015-07-07 14:53:48 +02:00
|
|
|
}
|
|
|
|
file << cookie;
|
|
|
|
file.close();
|
2017-08-28 09:40:08 +02:00
|
|
|
|
|
|
|
fs::path filepath = GetAuthCookieFile(false);
|
|
|
|
if (!RenameOver(filepath_tmp, filepath)) {
|
|
|
|
LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-07 14:53:48 +02:00
|
|
|
LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
|
2013-11-20 14:18:57 +01:00
|
|
|
|
2015-07-07 14:53:48 +02:00
|
|
|
if (cookie_out)
|
|
|
|
*cookie_out = cookie;
|
2013-11-20 14:18:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-07 14:53:48 +02:00
|
|
|
bool GetAuthCookie(std::string *cookie_out)
|
2013-11-20 14:18:57 +01:00
|
|
|
{
|
2018-08-04 15:27:38 +02:00
|
|
|
fsbridge::ifstream file;
|
2015-07-07 14:53:48 +02:00
|
|
|
std::string cookie;
|
2017-04-06 20:19:21 +02:00
|
|
|
fs::path filepath = GetAuthCookieFile();
|
2018-08-04 15:27:38 +02:00
|
|
|
file.open(filepath);
|
2015-07-07 14:53:48 +02:00
|
|
|
if (!file.is_open())
|
|
|
|
return false;
|
|
|
|
std::getline(file, cookie);
|
|
|
|
file.close();
|
2013-11-20 14:18:57 +01:00
|
|
|
|
2015-07-07 14:53:48 +02:00
|
|
|
if (cookie_out)
|
|
|
|
*cookie_out = cookie;
|
|
|
|
return true;
|
2013-11-20 14:18:57 +01:00
|
|
|
}
|
|
|
|
|
2015-07-07 14:53:48 +02:00
|
|
|
void DeleteAuthCookie()
|
2013-11-20 14:18:57 +01:00
|
|
|
{
|
2015-07-07 14:53:48 +02:00
|
|
|
try {
|
2017-04-06 20:19:21 +02:00
|
|
|
fs::remove(GetAuthCookieFile());
|
|
|
|
} catch (const fs::filesystem_error& e) {
|
2018-09-10 20:08:56 +02:00
|
|
|
LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
|
2013-11-20 14:18:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 10:28:19 +02:00
|
|
|
std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in)
|
2017-09-28 08:33:12 +02:00
|
|
|
{
|
|
|
|
if (!in.isArray()) {
|
|
|
|
throw std::runtime_error("Batch must be an array");
|
|
|
|
}
|
2023-04-17 10:28:19 +02:00
|
|
|
const size_t num {in.size()};
|
2017-09-28 08:33:12 +02:00
|
|
|
std::vector<UniValue> batch(num);
|
2023-04-17 10:28:19 +02:00
|
|
|
for (const UniValue& rec : in.getValues()) {
|
2017-09-28 08:33:12 +02:00
|
|
|
if (!rec.isObject()) {
|
2023-04-17 10:28:19 +02:00
|
|
|
throw std::runtime_error("Batch member must be an object");
|
2017-09-28 08:33:12 +02:00
|
|
|
}
|
|
|
|
size_t id = rec["id"].get_int();
|
|
|
|
if (id >= num) {
|
2023-04-17 10:28:19 +02:00
|
|
|
throw std::runtime_error("Batch member id is larger than batch size");
|
2017-09-28 08:33:12 +02:00
|
|
|
}
|
|
|
|
batch[id] = rec;
|
|
|
|
}
|
|
|
|
return batch;
|
|
|
|
}
|
2022-02-27 08:46:53 +01: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
|
|
|
|
id = find_value(request, "id");
|
|
|
|
|
|
|
|
// Parse method
|
|
|
|
UniValue valMethod = find_value(request, "method");
|
|
|
|
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 (strMethod != "getblocktemplate") {
|
|
|
|
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
|
|
|
|
UniValue valParams = find_value(request, "params");
|
|
|
|
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");
|
|
|
|
}
|
2022-05-12 22:29:17 +02:00
|
|
|
|
|
|
|
const JSONRPCRequest JSONRPCRequest::squashed() const
|
|
|
|
{
|
|
|
|
if (params.empty()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
JSONRPCRequest new_request{*this};
|
|
|
|
new_request.strMethod = strMethod + params[0].get_str();
|
|
|
|
new_request.params.setArray();
|
|
|
|
for (unsigned int i = 1; i < params.size(); ++i) {
|
|
|
|
new_request.params.push_back(params[i]);
|
|
|
|
}
|
|
|
|
return new_request;
|
|
|
|
}
|