Bitcoin Core  27.99.0
P2P Digital Currency
httprpc.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <httprpc.h>
6 
7 #include <common/args.h>
8 #include <crypto/hmac_sha256.h>
9 #include <httpserver.h>
10 #include <logging.h>
11 #include <netaddress.h>
12 #include <rpc/protocol.h>
13 #include <rpc/server.h>
14 #include <util/fs.h>
15 #include <util/fs_helpers.h>
16 #include <util/strencodings.h>
17 #include <util/string.h>
18 #include <walletinitinterface.h>
19 
20 #include <algorithm>
21 #include <iterator>
22 #include <map>
23 #include <memory>
24 #include <optional>
25 #include <set>
26 #include <string>
27 #include <vector>
28 
29 using util::SplitString;
31 
33 static const char* WWW_AUTH_HEADER_DATA = "Basic realm=\"jsonrpc\"";
34 
38 class HTTPRPCTimer : public RPCTimerBase
39 {
40 public:
41  HTTPRPCTimer(struct event_base* eventBase, std::function<void()>& func, int64_t millis) :
42  ev(eventBase, false, func)
43  {
44  struct timeval tv;
45  tv.tv_sec = millis/1000;
46  tv.tv_usec = (millis%1000)*1000;
47  ev.trigger(&tv);
48  }
49 private:
51 };
52 
54 {
55 public:
56  explicit HTTPRPCTimerInterface(struct event_base* _base) : base(_base)
57  {
58  }
59  const char* Name() override
60  {
61  return "HTTP";
62  }
63  RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) override
64  {
65  return new HTTPRPCTimer(base, func, millis);
66  }
67 private:
68  struct event_base* base;
69 };
70 
71 
72 /* Pre-base64-encoded authentication token */
73 static std::string strRPCUserColonPass;
74 /* Stored RPC timer interface (for unregistration) */
75 static std::unique_ptr<HTTPRPCTimerInterface> httpRPCTimerInterface;
76 /* List of -rpcauth values */
77 static std::vector<std::vector<std::string>> g_rpcauth;
78 /* RPC Auth Whitelist */
79 static std::map<std::string, std::set<std::string>> g_rpc_whitelist;
80 static bool g_rpc_whitelist_default = false;
81 
82 static void JSONErrorReply(HTTPRequest* req, UniValue objError, const JSONRPCRequest& jreq)
83 {
84  // Sending HTTP errors is a legacy JSON-RPC behavior.
86 
87  // Send error reply from json-rpc error object
88  int nStatus = HTTP_INTERNAL_SERVER_ERROR;
89  int code = objError.find_value("code").getInt<int>();
90 
91  if (code == RPC_INVALID_REQUEST)
92  nStatus = HTTP_BAD_REQUEST;
93  else if (code == RPC_METHOD_NOT_FOUND)
94  nStatus = HTTP_NOT_FOUND;
95 
96  std::string strReply = JSONRPCReplyObj(NullUniValue, std::move(objError), jreq.id, jreq.m_json_version).write() + "\n";
97 
98  req->WriteHeader("Content-Type", "application/json");
99  req->WriteReply(nStatus, strReply);
100 }
101 
102 //This function checks username and password against -rpcauth
103 //entries from config file.
104 static bool multiUserAuthorized(std::string strUserPass)
105 {
106  if (strUserPass.find(':') == std::string::npos) {
107  return false;
108  }
109  std::string strUser = strUserPass.substr(0, strUserPass.find(':'));
110  std::string strPass = strUserPass.substr(strUserPass.find(':') + 1);
111 
112  for (const auto& vFields : g_rpcauth) {
113  std::string strName = vFields[0];
114  if (!TimingResistantEqual(strName, strUser)) {
115  continue;
116  }
117 
118  std::string strSalt = vFields[1];
119  std::string strHash = vFields[2];
120 
121  static const unsigned int KEY_SIZE = 32;
122  unsigned char out[KEY_SIZE];
123 
124  CHMAC_SHA256(reinterpret_cast<const unsigned char*>(strSalt.data()), strSalt.size()).Write(reinterpret_cast<const unsigned char*>(strPass.data()), strPass.size()).Finalize(out);
125  std::vector<unsigned char> hexvec(out, out+KEY_SIZE);
126  std::string strHashFromPass = HexStr(hexvec);
127 
128  if (TimingResistantEqual(strHashFromPass, strHash)) {
129  return true;
130  }
131  }
132  return false;
133 }
134 
135 static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUsernameOut)
136 {
137  if (strRPCUserColonPass.empty()) // Belt-and-suspenders measure if InitRPCAuthentication was not called
138  return false;
139  if (strAuth.substr(0, 6) != "Basic ")
140  return false;
141  std::string_view strUserPass64 = TrimStringView(std::string_view{strAuth}.substr(6));
142  auto userpass_data = DecodeBase64(strUserPass64);
143  std::string strUserPass;
144  if (!userpass_data) return false;
145  strUserPass.assign(userpass_data->begin(), userpass_data->end());
146 
147  if (strUserPass.find(':') != std::string::npos)
148  strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(':'));
149 
150  //Check if authorized under single-user field
151  if (TimingResistantEqual(strUserPass, strRPCUserColonPass)) {
152  return true;
153  }
154  return multiUserAuthorized(strUserPass);
155 }
156 
157 static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
158 {
159  // JSONRPC handles only POST
160  if (req->GetRequestMethod() != HTTPRequest::POST) {
161  req->WriteReply(HTTP_BAD_METHOD, "JSONRPC server handles only POST requests");
162  return false;
163  }
164  // Check authorization
165  std::pair<bool, std::string> authHeader = req->GetHeader("authorization");
166  if (!authHeader.first) {
167  req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA);
169  return false;
170  }
171 
172  JSONRPCRequest jreq;
173  jreq.context = context;
174  jreq.peerAddr = req->GetPeer().ToStringAddrPort();
175  if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
176  LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr);
177 
178  /* Deter brute-forcing
179  If this results in a DoS the user really
180  shouldn't have their RPC port exposed. */
181  UninterruptibleSleep(std::chrono::milliseconds{250});
182 
183  req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA);
185  return false;
186  }
187 
188  try {
189  // Parse request
190  UniValue valRequest;
191  if (!valRequest.read(req->ReadBody()))
192  throw JSONRPCError(RPC_PARSE_ERROR, "Parse error");
193 
194  // Set the URI
195  jreq.URI = req->GetURI();
196 
197  UniValue reply;
198  bool user_has_whitelist = g_rpc_whitelist.count(jreq.authUser);
199  if (!user_has_whitelist && g_rpc_whitelist_default) {
200  LogPrintf("RPC User %s not allowed to call any methods\n", jreq.authUser);
202  return false;
203 
204  // singleton request
205  } else if (valRequest.isObject()) {
206  jreq.parse(valRequest);
207  if (user_has_whitelist && !g_rpc_whitelist[jreq.authUser].count(jreq.strMethod)) {
208  LogPrintf("RPC User %s not allowed to call method %s\n", jreq.authUser, jreq.strMethod);
210  return false;
211  }
212 
213  // Legacy 1.0/1.1 behavior is for failed requests to throw
214  // exceptions which return HTTP errors and RPC errors to the client.
215  // 2.0 behavior is to catch exceptions and return HTTP success with
216  // RPC errors, as long as there is not an actual HTTP server error.
217  const bool catch_errors{jreq.m_json_version == JSONRPCVersion::V2};
218  reply = JSONRPCExec(jreq, catch_errors);
219 
220  if (jreq.IsNotification()) {
221  // Even though we do execute notifications, we do not respond to them
223  return true;
224  }
225 
226  // array of requests
227  } else if (valRequest.isArray()) {
228  // Check authorization for each request's method
229  if (user_has_whitelist) {
230  for (unsigned int reqIdx = 0; reqIdx < valRequest.size(); reqIdx++) {
231  if (!valRequest[reqIdx].isObject()) {
232  throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
233  } else {
234  const UniValue& request = valRequest[reqIdx].get_obj();
235  // Parse method
236  std::string strMethod = request.find_value("method").get_str();
237  if (!g_rpc_whitelist[jreq.authUser].count(strMethod)) {
238  LogPrintf("RPC User %s not allowed to call method %s\n", jreq.authUser, strMethod);
240  return false;
241  }
242  }
243  }
244  }
245 
246  // Execute each request
247  reply = UniValue::VARR;
248  for (size_t i{0}; i < valRequest.size(); ++i) {
249  // Batches never throw HTTP errors, they are always just included
250  // in "HTTP OK" responses. Notifications never get any response.
251  UniValue response;
252  try {
253  jreq.parse(valRequest[i]);
254  response = JSONRPCExec(jreq, /*catch_errors=*/true);
255  } catch (UniValue& e) {
256  response = JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id, jreq.m_json_version);
257  } catch (const std::exception& e) {
258  response = JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id, jreq.m_json_version);
259  }
260  if (!jreq.IsNotification()) {
261  reply.push_back(std::move(response));
262  }
263  }
264  // Return no response for an all-notification batch, but only if the
265  // batch request is non-empty. Technically according to the JSON-RPC
266  // 2.0 spec, an empty batch request should also return no response,
267  // However, if the batch request is empty, it means the request did
268  // not contain any JSON-RPC version numbers, so returning an empty
269  // response could break backwards compatibility with old RPC clients
270  // relying on previous behavior. Return an empty array instead of an
271  // empty response in this case to favor being backwards compatible
272  // over complying with the JSON-RPC 2.0 spec in this case.
273  if (reply.size() == 0 && valRequest.size() > 0) {
275  return true;
276  }
277  }
278  else
279  throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");
280 
281  req->WriteHeader("Content-Type", "application/json");
282  req->WriteReply(HTTP_OK, reply.write() + "\n");
283  } catch (UniValue& e) {
284  JSONErrorReply(req, std::move(e), jreq);
285  return false;
286  } catch (const std::exception& e) {
287  JSONErrorReply(req, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq);
288  return false;
289  }
290  return true;
291 }
292 
294 {
295  if (gArgs.GetArg("-rpcpassword", "") == "")
296  {
297  LogInfo("Using random cookie authentication.\n");
298 
299  std::optional<fs::perms> cookie_perms{std::nullopt};
300  auto cookie_perms_arg{gArgs.GetArg("-rpccookieperms")};
301  if (cookie_perms_arg) {
302  auto perm_opt = InterpretPermString(*cookie_perms_arg);
303  if (!perm_opt) {
304  LogInfo("Invalid -rpccookieperms=%s; must be one of 'owner', 'group', or 'all'.\n", *cookie_perms_arg);
305  return false;
306  }
307  cookie_perms = *perm_opt;
308  }
309 
310  if (!GenerateAuthCookie(&strRPCUserColonPass, cookie_perms)) {
311  return false;
312  }
313  } else {
314  LogPrintf("Config options rpcuser and rpcpassword will soon be deprecated. Locally-run instances may remove rpcuser to use cookie-based auth, or may be replaced with rpcauth. Please see share/rpcauth for rpcauth auth generation.\n");
315  strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + gArgs.GetArg("-rpcpassword", "");
316  }
317  if (gArgs.GetArg("-rpcauth", "") != "") {
318  LogPrintf("Using rpcauth authentication.\n");
319  for (const std::string& rpcauth : gArgs.GetArgs("-rpcauth")) {
320  std::vector<std::string> fields{SplitString(rpcauth, ':')};
321  const std::vector<std::string> salt_hmac{SplitString(fields.back(), '$')};
322  if (fields.size() == 2 && salt_hmac.size() == 2) {
323  fields.pop_back();
324  fields.insert(fields.end(), salt_hmac.begin(), salt_hmac.end());
325  g_rpcauth.push_back(fields);
326  } else {
327  LogPrintf("Invalid -rpcauth argument.\n");
328  return false;
329  }
330  }
331  }
332 
333  g_rpc_whitelist_default = gArgs.GetBoolArg("-rpcwhitelistdefault", gArgs.IsArgSet("-rpcwhitelist"));
334  for (const std::string& strRPCWhitelist : gArgs.GetArgs("-rpcwhitelist")) {
335  auto pos = strRPCWhitelist.find(':');
336  std::string strUser = strRPCWhitelist.substr(0, pos);
337  bool intersect = g_rpc_whitelist.count(strUser);
338  std::set<std::string>& whitelist = g_rpc_whitelist[strUser];
339  if (pos != std::string::npos) {
340  std::string strWhitelist = strRPCWhitelist.substr(pos + 1);
341  std::vector<std::string> whitelist_split = SplitString(strWhitelist, ", ");
342  std::set<std::string> new_whitelist{
343  std::make_move_iterator(whitelist_split.begin()),
344  std::make_move_iterator(whitelist_split.end())};
345  if (intersect) {
346  std::set<std::string> tmp_whitelist;
347  std::set_intersection(new_whitelist.begin(), new_whitelist.end(),
348  whitelist.begin(), whitelist.end(), std::inserter(tmp_whitelist, tmp_whitelist.end()));
349  new_whitelist = std::move(tmp_whitelist);
350  }
351  whitelist = std::move(new_whitelist);
352  }
353  }
354 
355  return true;
356 }
357 
358 bool StartHTTPRPC(const std::any& context)
359 {
360  LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
361  if (!InitRPCAuthentication())
362  return false;
363 
364  auto handle_rpc = [context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };
365  RegisterHTTPHandler("/", true, handle_rpc);
367  RegisterHTTPHandler("/wallet/", false, handle_rpc);
368  }
369  struct event_base* eventBase = EventBase();
370  assert(eventBase);
371  httpRPCTimerInterface = std::make_unique<HTTPRPCTimerInterface>(eventBase);
373  return true;
374 }
375 
377 {
378  LogPrint(BCLog::RPC, "Interrupting HTTP RPC server\n");
379 }
380 
382 {
383  LogPrint(BCLog::RPC, "Stopping HTTP RPC server\n");
384  UnregisterHTTPHandler("/", true);
386  UnregisterHTTPHandler("/wallet/", false);
387  }
388  if (httpRPCTimerInterface) {
390  httpRPCTimerInterface.reset();
391  }
392 }
ArgsManager gArgs
Definition: args.cpp:41
#define Assume(val)
Assume is the identity function.
Definition: check.h:89
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: args.cpp:360
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:369
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:455
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:505
A hasher class for HMAC-SHA-256.
Definition: hmac_sha256.h:15
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hmac_sha256.cpp:29
CHMAC_SHA256 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha256.h:24
std::string ToStringAddrPort() const
Definition: netaddress.cpp:905
Event class.
Definition: httpserver.h:165
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:573
Simple one-shot callback timer to be used by the RPC mechanism to e.g.
Definition: httprpc.cpp:39
HTTPRPCTimer(struct event_base *eventBase, std::function< void()> &func, int64_t millis)
Definition: httprpc.cpp:41
HTTPEvent ev
Definition: httprpc.cpp:50
const char * Name() override
Implementation name.
Definition: httprpc.cpp:59
RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis) override
Factory function for timers.
Definition: httprpc.cpp:63
struct event_base * base
Definition: httprpc.cpp:68
HTTPRPCTimerInterface(struct event_base *_base)
Definition: httprpc.cpp:56
In-flight HTTP request.
Definition: httpserver.h:62
std::pair< bool, std::string > GetHeader(const std::string &hdr) const
Get the request header specified by hdr, or an empty string.
Definition: httpserver.cpp:595
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:688
void WriteReply(int nStatus, std::string_view reply="")
Write HTTP reply.
Definition: httpserver.h:132
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:626
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:693
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:606
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:668
std::string strMethod
Definition: request.h:39
JSONRPCVersion m_json_version
Definition: request.h:46
bool IsNotification() const
Definition: request.h:49
std::string peerAddr
Definition: request.h:44
void parse(const UniValue &valRequest)
Definition: request.cpp:188
std::string URI
Definition: request.h:42
std::optional< UniValue > id
Definition: request.h:38
std::string authUser
Definition: request.h:43
std::any context
Definition: request.h:45
Opaque base class for timers returned by NewTimerFunc.
Definition: server.h:49
RPC timer "driver".
Definition: server.h:58
void push_back(UniValue val)
Definition: univalue.cpp:104
const std::string & get_str() const
bool isArray() const
Definition: univalue.h:85
const UniValue & find_value(std::string_view key) const
Definition: univalue.cpp:233
@ VARR
Definition: univalue.h:24
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const UniValue & get_obj() const
size_t size() const
Definition: univalue.h:71
bool read(std::string_view raw)
Int getInt() const
Definition: univalue.h:138
bool isObject() const
Definition: univalue.h:86
virtual bool HasWalletSupport() const =0
Is the wallet component enabled.
const WalletInitInterface & g_wallet_init_interface
Definition: dummywallet.cpp:60
std::optional< fs::perms > InterpretPermString(const std::string &s)
Interpret a custom permissions level string as fs::perms.
Definition: fs_helpers.cpp:297
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
static const char * WWW_AUTH_HEADER_DATA
WWW-Authenticate to present with 401 Unauthorized response.
Definition: httprpc.cpp:33
void InterruptHTTPRPC()
Interrupt HTTP RPC subsystem.
Definition: httprpc.cpp:376
void StopHTTPRPC()
Stop HTTP RPC subsystem.
Definition: httprpc.cpp:381
static std::unique_ptr< HTTPRPCTimerInterface > httpRPCTimerInterface
Definition: httprpc.cpp:75
static bool multiUserAuthorized(std::string strUserPass)
Definition: httprpc.cpp:104
bool StartHTTPRPC(const std::any &context)
Start HTTP RPC subsystem.
Definition: httprpc.cpp:358
static bool g_rpc_whitelist_default
Definition: httprpc.cpp:80
static bool RPCAuthorized(const std::string &strAuth, std::string &strAuthUsernameOut)
Definition: httprpc.cpp:135
static std::vector< std::vector< std::string > > g_rpcauth
Definition: httprpc.cpp:77
static bool HTTPReq_JSONRPC(const std::any &context, HTTPRequest *req)
Definition: httprpc.cpp:157
static std::string strRPCUserColonPass
Definition: httprpc.cpp:73
static bool InitRPCAuthentication()
Definition: httprpc.cpp:293
static std::map< std::string, std::set< std::string > > g_rpc_whitelist
Definition: httprpc.cpp:79
static void JSONErrorReply(HTTPRequest *req, UniValue objError, const JSONRPCRequest &jreq)
Definition: httprpc.cpp:82
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:750
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:743
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:549
static struct event_base * eventBase
HTTP module state.
Definition: httpserver.cpp:140
#define LogPrint(category,...)
Definition: logging.h:293
#define LogInfo(...)
Definition: logging.h:269
#define LogPrintf(...)
Definition: logging.h:274
@ RPC
Definition: logging.h:49
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:69
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:59
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:70
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional< UniValue > id, JSONRPCVersion jsonrpc_version)
Definition: request.cpp:51
bool GenerateAuthCookie(std::string *cookie_out, std::optional< fs::perms > cookie_perms)
Generate a new RPC authentication cookie and write it to disk.
Definition: request.cpp:97
@ HTTP_BAD_REQUEST
Definition: protocol.h:14
@ HTTP_BAD_METHOD
Definition: protocol.h:18
@ HTTP_OK
Definition: protocol.h:12
@ HTTP_UNAUTHORIZED
Definition: protocol.h:15
@ HTTP_NOT_FOUND
Definition: protocol.h:17
@ HTTP_FORBIDDEN
Definition: protocol.h:16
@ HTTP_NO_CONTENT
Definition: protocol.h:13
@ HTTP_INTERNAL_SERVER_ERROR
Definition: protocol.h:19
@ RPC_PARSE_ERROR
Definition: protocol.h:37
@ RPC_METHOD_NOT_FOUND
Definition: protocol.h:32
@ RPC_INVALID_REQUEST
Standard JSON-RPC 2.0 errors.
Definition: protocol.h:29
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:574
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
Definition: server.cpp:365
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Definition: server.cpp:569
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:246
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:17
const UniValue NullUniValue
Definition: univalue.cpp:16
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
assert(!tx.IsCoinBase())