You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.7 KiB
128 lines
3.7 KiB
11 years ago
|
// Copyright (c) 2010 Satoshi Nakamoto
|
||
9 years ago
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
||
10 years ago
|
// Distributed under the MIT software license, see the accompanying
|
||
11 years ago
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
|
|
||
9 years ago
|
#include "rpc/protocol.h"
|
||
11 years ago
|
|
||
9 years ago
|
#include "random.h"
|
||
10 years ago
|
#include "tinyformat.h"
|
||
10 years ago
|
#include "util.h"
|
||
10 years ago
|
#include "utilstrencodings.h"
|
||
|
#include "utiltime.h"
|
||
10 years ago
|
#include "version.h"
|
||
11 years ago
|
|
||
|
#include <stdint.h>
|
||
9 years ago
|
#include <fstream>
|
||
11 years ago
|
|
||
|
using namespace std;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* 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
|
||
|
*/
|
||
11 years ago
|
|
||
10 years ago
|
string JSONRPCRequest(const string& strMethod, const UniValue& params, const UniValue& id)
|
||
11 years ago
|
{
|
||
10 years ago
|
UniValue request(UniValue::VOBJ);
|
||
11 years ago
|
request.push_back(Pair("method", strMethod));
|
||
|
request.push_back(Pair("params", params));
|
||
|
request.push_back(Pair("id", id));
|
||
10 years ago
|
return request.write() + "\n";
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
|
||
11 years ago
|
{
|
||
10 years ago
|
UniValue reply(UniValue::VOBJ);
|
||
10 years ago
|
if (!error.isNull())
|
||
|
reply.push_back(Pair("result", NullUniValue));
|
||
11 years ago
|
else
|
||
|
reply.push_back(Pair("result", result));
|
||
|
reply.push_back(Pair("error", error));
|
||
|
reply.push_back(Pair("id", id));
|
||
|
return reply;
|
||
|
}
|
||
|
|
||
10 years ago
|
string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
|
||
11 years ago
|
{
|
||
10 years ago
|
UniValue reply = JSONRPCReplyObj(result, error, id);
|
||
10 years ago
|
return reply.write() + "\n";
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
UniValue JSONRPCError(int code, const string& message)
|
||
11 years ago
|
{
|
||
10 years ago
|
UniValue error(UniValue::VOBJ);
|
||
11 years ago
|
error.push_back(Pair("code", code));
|
||
|
error.push_back(Pair("message", message));
|
||
|
return error;
|
||
|
}
|
||
9 years ago
|
|
||
|
/** 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";
|
||
|
|
||
|
boost::filesystem::path GetAuthCookieFile()
|
||
|
{
|
||
|
boost::filesystem::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE));
|
||
|
if (!path.is_complete()) path = GetDataDir() / path;
|
||
|
return path;
|
||
|
}
|
||
|
|
||
|
bool GenerateAuthCookie(std::string *cookie_out)
|
||
|
{
|
||
|
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());
|
||
|
return false;
|
||
|
}
|
||
|
file << cookie;
|
||
|
file.close();
|
||
|
LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
|
||
|
|
||
|
if (cookie_out)
|
||
|
*cookie_out = cookie;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool GetAuthCookie(std::string *cookie_out)
|
||
|
{
|
||
|
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();
|
||
|
|
||
|
if (cookie_out)
|
||
|
*cookie_out = cookie;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void DeleteAuthCookie()
|
||
|
{
|
||
|
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());
|
||
|
}
|
||
|
}
|
||
|
|