2013-11-20 14:18:57 +01:00
|
|
|
// Copyright (c) 2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2016-02-02 16:28:56 +01:00
|
|
|
// Copyright (c) 2014-2016 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.
|
|
|
|
|
|
|
|
#include "rpcprotocol.h"
|
|
|
|
|
2015-07-07 14:53:48 +02:00
|
|
|
#include "random.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include "tinyformat.h"
|
2013-11-20 14:18:57 +01:00
|
|
|
#include "util.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include "utilstrencodings.h"
|
|
|
|
#include "utiltime.h"
|
2014-08-21 16:11:05 +02:00
|
|
|
#include "version.h"
|
2013-11-20 14:18:57 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2015-07-07 14:53:48 +02:00
|
|
|
#include <fstream>
|
2013-11-20 14:18:57 +01:00
|
|
|
|
|
|
|
using namespace std;
|
2014-06-20 15:21:30 +02: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').
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2015-05-18 14:02:18 +02:00
|
|
|
string JSONRPCRequest(const 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);
|
2013-11-20 14:18:57 +01:00
|
|
|
request.push_back(Pair("method", strMethod));
|
|
|
|
request.push_back(Pair("params", params));
|
|
|
|
request.push_back(Pair("id", id));
|
2014-08-20 21:15:16 +02:00
|
|
|
return request.write() + "\n";
|
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())
|
|
|
|
reply.push_back(Pair("result", NullUniValue));
|
2013-11-20 14:18:57 +01:00
|
|
|
else
|
|
|
|
reply.push_back(Pair("result", result));
|
|
|
|
reply.push_back(Pair("error", error));
|
|
|
|
reply.push_back(Pair("id", id));
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2015-05-18 14:02:18 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-13 21:29:19 +02:00
|
|
|
UniValue JSONRPCError(int code, const string& message)
|
2013-11-20 14:18:57 +01:00
|
|
|
{
|
2015-05-10 13:35:44 +02:00
|
|
|
UniValue error(UniValue::VOBJ);
|
2013-11-20 14:18:57 +01:00
|
|
|
error.push_back(Pair("code", code));
|
|
|
|
error.push_back(Pair("message", message));
|
|
|
|
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
|
|
|
|
2015-07-07 14:53:48 +02:00
|
|
|
boost::filesystem::path GetAuthCookieFile()
|
2014-08-06 13:01:49 +02:00
|
|
|
{
|
2015-07-07 14:53:48 +02:00
|
|
|
boost::filesystem::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE));
|
|
|
|
if (!path.is_complete()) path = GetDataDir() / path;
|
|
|
|
return path;
|
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
|
|
|
{
|
2015-07-07 14:53:48 +02:00
|
|
|
unsigned char rand_pwd[32];
|
|
|
|
GetRandBytes(rand_pwd, 32);
|
|
|
|
std::string cookie = COOKIEAUTH_USER + ":" + EncodeBase64(&rand_pwd[0],32);
|
|
|
|
|
|
|
|
/** the umask determines what permissions are used to create this file -
|
|
|
|
* these are set to 077 in init.cpp unless overridden with -sysperms.
|
|
|
|
*/
|
|
|
|
std::ofstream file;
|
|
|
|
boost::filesystem::path filepath = GetAuthCookieFile();
|
|
|
|
file.open(filepath.string().c_str());
|
|
|
|
if (!file.is_open()) {
|
|
|
|
LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath.string());
|
2013-11-20 14:18:57 +01:00
|
|
|
return false;
|
2015-07-07 14:53:48 +02:00
|
|
|
}
|
|
|
|
file << cookie;
|
|
|
|
file.close();
|
|
|
|
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
|
|
|
{
|
2015-07-07 14:53:48 +02:00
|
|
|
std::ifstream file;
|
|
|
|
std::string cookie;
|
|
|
|
boost::filesystem::path filepath = GetAuthCookieFile();
|
|
|
|
file.open(filepath.string().c_str());
|
|
|
|
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 {
|
|
|
|
boost::filesystem::remove(GetAuthCookieFile());
|
|
|
|
} catch (const boost::filesystem::filesystem_error& e) {
|
|
|
|
LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());
|
2013-11-20 14:18:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|