2013-11-20 14:18:57 +01:00
|
|
|
// Copyright (c) 2010 Satoshi Nakamoto
|
2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin 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.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <rpc/protocol.h>
|
2013-11-20 14:18:57 +01:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <random.h>
|
|
|
|
#include <tinyformat.h>
|
2018-10-23 00:51:11 +02:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/strencodings.h>
|
|
|
|
#include <util/time.h>
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <version.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').
|
2016-09-29 16:45:19 +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-01-04 05:22:19 +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);
|
2017-09-22 20:04:07 +02:00
|
|
|
request.pushKV("method", strMethod);
|
|
|
|
request.pushKV("params", params);
|
|
|
|
request.pushKV("id", id);
|
2016-09-29 18:48:27 +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())
|
2017-09-22 20:04:07 +02:00
|
|
|
reply.pushKV("result", NullUniValue);
|
2013-11-20 14:18:57 +01:00
|
|
|
else
|
2017-09-22 20:04:07 +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-01-04 05:22:19 +01:00
|
|
|
std::string JSONRPCReply(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 = JSONRPCReplyObj(result, error, id);
|
2014-08-20 21:15:16 +02:00
|
|
|
return reply.write() + "\n";
|
2013-11-20 14:18:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 05:22:19 +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);
|
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 */
|
|
|
|
static const std::string COOKIEAUTH_FILE = ".cookie";
|
|
|
|
|
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
|
|
|
{
|
2017-08-25 12:39:30 +02:00
|
|
|
std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE);
|
|
|
|
if (temp) {
|
|
|
|
arg += ".tmp";
|
|
|
|
}
|
2018-01-31 04:33:49 +01:00
|
|
|
return AbsPathForConfigVal(fs::path(arg));
|
2015-07-07 14:53:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GenerateAuthCookie(std::string *cookie_out)
|
|
|
|
{
|
2016-10-01 16:57:25 +02:00
|
|
|
const size_t COOKIE_SIZE = 32;
|
|
|
|
unsigned char rand_pwd[COOKIE_SIZE];
|
|
|
|
GetRandBytes(rand_pwd, COOKIE_SIZE);
|
|
|
|
std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
|
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:32:13 +02:00
|
|
|
fsbridge::ofstream file;
|
2017-08-25 12:39:30 +02:00
|
|
|
fs::path filepath_tmp = GetAuthCookieFile(true);
|
2018-08-04 15:32:13 +02:00
|
|
|
file.open(filepath_tmp);
|
2015-07-07 14:53:48 +02:00
|
|
|
if (!file.is_open()) {
|
2017-08-25 12:39:30 +02:00
|
|
|
LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
|
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)) {
|
|
|
|
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());
|
|
|
|
|
|
|
|
if (cookie_out)
|
|
|
|
*cookie_out = cookie;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetAuthCookie(std::string *cookie_out)
|
|
|
|
{
|
2018-08-04 15:32:13 +02:00
|
|
|
fsbridge::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 15:32:13 +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();
|
|
|
|
|
|
|
|
if (cookie_out)
|
|
|
|
*cookie_out = cookie;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteAuthCookie()
|
|
|
|
{
|
|
|
|
try {
|
2017-03-01 17:05:50 +01: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));
|
2015-07-07 14:53:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 16:45:19 +02:00
|
|
|
std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue &in, size_t num)
|
|
|
|
{
|
|
|
|
if (!in.isArray()) {
|
|
|
|
throw std::runtime_error("Batch must be an array");
|
|
|
|
}
|
|
|
|
std::vector<UniValue> batch(num);
|
|
|
|
for (size_t i=0; i<in.size(); ++i) {
|
|
|
|
const UniValue &rec = in[i];
|
|
|
|
if (!rec.isObject()) {
|
|
|
|
throw std::runtime_error("Batch member must be object");
|
|
|
|
}
|
|
|
|
size_t id = rec["id"].get_int();
|
|
|
|
if (id >= num) {
|
|
|
|
throw std::runtime_error("Batch member id larger than size");
|
|
|
|
}
|
|
|
|
batch[id] = rec;
|
|
|
|
}
|
|
|
|
return batch;
|
|
|
|
}
|