Skip to content

Commit

Permalink
Merge bitcoin#8788: [RPC] Give RPC commands more information about th…
Browse files Browse the repository at this point in the history
…e RPC request

e7156ad [RPC] pass HTTP basic authentication username to the JSONRequest object (Jonas Schnelli)
69d1c25 [RPC] Give RPC commands more information about the RPC request (Jonas Schnelli)
23c32a9 rpc: Change JSONRPCRequest to JSONRPCRequestObj (Wladimir J. van der Laan)
  • Loading branch information
laanwj authored and codablock committed Jan 13, 2018
1 parent b5b7cd7 commit dd6b9ad
Show file tree
Hide file tree
Showing 18 changed files with 778 additions and 761 deletions.
2 changes: 1 addition & 1 deletion src/dash-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ UniValue CallRPC(const string& strMethod, const UniValue& params)
evhttp_add_header(output_headers, "Authorization", (std::string("Basic ") + EncodeBase64(strRPCUserColonPass)).c_str());

// Attach request data
std::string strRequest = JSONRPCRequest(strMethod, params, 1);
std::string strRequest = JSONRPCRequestObj(strMethod, params, 1).write() + "\n";
struct evbuffer * output_buffer = evhttp_request_get_output_buffer(req);
assert(output_buffer);
evbuffer_add(output_buffer, strRequest.data(), strRequest.size());
Expand Down
16 changes: 11 additions & 5 deletions src/httprpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static bool multiUserAuthorized(std::string strUserPass)
return false;
}

static bool RPCAuthorized(const std::string& strAuth)
static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUsernameOut)
{
if (strRPCUserColonPass.empty()) // Belt-and-suspenders measure if InitRPCAuthentication was not called
return false;
Expand All @@ -136,7 +136,10 @@ static bool RPCAuthorized(const std::string& strAuth)
std::string strUserPass64 = strAuth.substr(6);
boost::trim(strUserPass64);
std::string strUserPass = DecodeBase64(strUserPass64);


if (strUserPass.find(":") != std::string::npos)
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(":"));

//Check if authorized under single-user field
if (TimingResistantEqual(strUserPass, strRPCUserColonPass)) {
return true;
Expand All @@ -159,7 +162,8 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
return false;
}

if (!RPCAuthorized(authHeader.second)) {
JSONRPCRequest jreq;
if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", req->GetPeer().ToString());

/* Deter brute-forcing
Expand All @@ -172,19 +176,21 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
return false;
}

JSONRequest jreq;
try {
// Parse request
UniValue valRequest;
if (!valRequest.read(req->ReadBody()))
throw JSONRPCError(RPC_PARSE_ERROR, "Parse error");

// Set the URI
jreq.URI = req->GetURI();

std::string strReply;
// singleton request
if (valRequest.isObject()) {
jreq.parse(valRequest);

UniValue result = tableRPC.execute(jreq.strMethod, jreq.params);
UniValue result = tableRPC.execute(jreq);

// Send reply
strReply = JSONRPCReply(result, NullUniValue, jreq.id);
Expand Down
5 changes: 4 additions & 1 deletion src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ bool RPCConsole::RPCExecuteCommandLine(std::string &strResult, const std::string
std::string strPrint;
// Convert argument list to JSON objects in method-dependent way,
// and pass it along with the method name to the dispatcher.
lastResult = tableRPC.execute(stack.back()[0], RPCConvertValues(stack.back()[0], std::vector<std::string>(stack.back().begin() + 1, stack.back().end())));
JSONRPCRequest req;
req.params = RPCConvertValues(stack.back()[0], std::vector<std::string>(stack.back().begin() + 1, stack.back().end()));
req.strMethod = stack.back()[0];
lastResult = tableRPC.execute(req);

state = STATE_COMMAND_EXECUTED;
curarg.clear();
Expand Down
7 changes: 4 additions & 3 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ static bool rest_block_notxdetails(HTTPRequest* req, const std::string& strURIPa
}

// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
UniValue getblockchaininfo(const UniValue& params, bool fHelp);
UniValue getblockchaininfo(const JSONRPCRequest& request);

static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
{
Expand All @@ -288,8 +288,9 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)

switch (rf) {
case RF_JSON: {
UniValue rpcParams(UniValue::VARR);
UniValue chainInfoObject = getblockchaininfo(rpcParams, false);
JSONRPCRequest jsonRequest;
jsonRequest.params = UniValue(UniValue::VARR);
UniValue chainInfoObject = getblockchaininfo(jsonRequest);
string strJSON = chainInfoObject.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strJSON);
Expand Down
Loading

0 comments on commit dd6b9ad

Please sign in to comment.