38 tv.tv_sec = millis/1000;
39 tv.tv_usec = (millis%1000)*1000;
52 const char*
Name()
override 70 static std::vector<std::vector<std::string>>
g_rpcauth;
88 req->
WriteHeader(
"Content-Type",
"application/json");
96 if (strUserPass.find(
':') == std::string::npos) {
99 std::string strUser = strUserPass.substr(0, strUserPass.find(
':'));
100 std::string strPass = strUserPass.substr(strUserPass.find(
':') + 1);
103 std::string strName = vFields[0];
108 std::string strSalt = vFields[1];
109 std::string strHash = vFields[2];
111 static const unsigned int KEY_SIZE = 32;
112 unsigned char out[KEY_SIZE];
114 CHMAC_SHA256(reinterpret_cast<const unsigned char*>(strSalt.data()), strSalt.size()).Write(reinterpret_cast<const unsigned char*>(strPass.data()), strPass.size()).Finalize(
out);
115 std::vector<unsigned char> hexvec(
out,
out+KEY_SIZE);
116 std::string strHashFromPass =
HexStr(hexvec);
125 static bool RPCAuthorized(
const std::string& strAuth, std::string& strAuthUsernameOut)
129 if (strAuth.substr(0, 6) !=
"Basic ")
131 std::string_view strUserPass64 =
TrimStringView(std::string_view{strAuth}.substr(6));
133 std::string strUserPass;
134 if (!userpass_data)
return false;
135 strUserPass.assign(userpass_data->begin(), userpass_data->end());
137 if (strUserPass.find(
':') != std::string::npos)
138 strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(
':'));
155 std::pair<bool, std::string> authHeader = req->
GetHeader(
"authorization");
156 if (!authHeader.first) {
166 LogPrintf(
"ThreadRPCServer incorrect password attempt from %s\n", jreq.
peerAddr);
187 std::string strReply;
196 jreq.
parse(valRequest);
208 }
else if (valRequest.
isArray()) {
209 if (user_has_whitelist) {
210 for (
unsigned int reqIdx = 0; reqIdx < valRequest.
size(); reqIdx++) {
211 if (!valRequest[reqIdx].isObject()) {
218 LogPrintf(
"RPC User %s not allowed to call method %s\n", jreq.
authUser, strMethod);
230 req->
WriteHeader(
"Content-Type",
"application/json");
232 }
catch (
const UniValue& objError) {
235 }
catch (
const std::exception& e) {
246 LogPrintf(
"Using random cookie authentication.\n");
251 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");
255 LogPrintf(
"Using rpcauth authentication.\n");
256 for (
const std::string& rpcauth :
gArgs.
GetArgs(
"-rpcauth")) {
257 std::vector<std::string> fields{
SplitString(rpcauth,
':')};
258 const std::vector<std::string> salt_hmac{
SplitString(fields.back(),
'$')};
259 if (fields.size() == 2 && salt_hmac.size() == 2) {
261 fields.insert(fields.end(), salt_hmac.begin(), salt_hmac.end());
264 LogPrintf(
"Invalid -rpcauth argument.\n");
271 for (
const std::string& strRPCWhitelist :
gArgs.
GetArgs(
"-rpcwhitelist")) {
272 auto pos = strRPCWhitelist.find(
':');
273 std::string strUser = strRPCWhitelist.substr(0, pos);
276 if (pos != std::string::npos) {
277 std::string strWhitelist = strRPCWhitelist.substr(pos + 1);
278 std::vector<std::string> whitelist_split =
SplitString(strWhitelist,
", ");
279 std::set<std::string> new_whitelist{
280 std::make_move_iterator(whitelist_split.begin()),
281 std::make_move_iterator(whitelist_split.end())};
283 std::set<std::string> tmp_whitelist;
284 std::set_intersection(new_whitelist.begin(), new_whitelist.end(),
285 whitelist.begin(), whitelist.end(), std::inserter(tmp_whitelist, tmp_whitelist.end()));
286 new_whitelist = std::move(tmp_whitelist);
288 whitelist = std::move(new_whitelist);
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
#define LogPrint(category,...)
HTTPRPCTimerInterface(struct event_base *_base)
const char * Name() override
Implementation name.
static const char * WWW_AUTH_HEADER_DATA
WWW-Authenticate to present with 401 Unauthorized response.
static std::string strRPCUserColonPass
bool read(std::string_view raw)
static std::vector< std::vector< std::string > > g_rpcauth
static bool InitRPCAuthentication()
static void JSONErrorReply(HTTPRequest *req, const UniValue &objError, const UniValue &id)
A hasher class for HMAC-SHA-256.
RPCTimerBase * NewTimer(std::function< void()> &func, int64_t millis) override
Factory function for timers.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \\\)
const std::string & get_str() const
const UniValue & get_array() const
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
bool StartHTTPRPC(const std::any &context)
Start HTTP RPC subsystem.
void InterruptHTTPRPC()
Interrupt HTTP RPC subsystem.
std::vector< std::string > SplitString(std::string_view str, char sep)
std::string ToStringAddrPort() const
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
const UniValue & find_value(std::string_view key) const
static std::unique_ptr< HTTPRPCTimerInterface > httpRPCTimerInterface
static bool RPCAuthorized(const std::string &strAuth, std::string &strAuthUsernameOut)
static bool multiUserAuthorized(std::string strUserPass)
UniValue JSONRPCError(int code, const std::string &message)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Simple one-shot callback timer to be used by the RPC mechanism to e.g.
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
RequestMethod GetRequestMethod() const
Get request method.
std::string JSONRPCExecBatch(const JSONRPCRequest &jreq, const UniValue &vReq)
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
struct event_base * EventBase()
Return evhttp event base.
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
void StopHTTPRPC()
Stop HTTP RPC subsystem.
static bool HTTPReq_JSONRPC(const std::any &context, HTTPRequest *req)
const WalletInitInterface & g_wallet_init_interface
void parse(const UniValue &valRequest)
static bool g_rpc_whitelist_default
HTTPRPCTimer(struct event_base *eventBase, std::function< void()> &func, int64_t millis)
void UninterruptibleSleep(const std::chrono::microseconds &n)
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
void trigger(struct timeval *tv)
Trigger the event.
virtual bool HasWalletSupport() const =0
Is the wallet component enabled.
const UniValue & get_obj() const
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
std::pair< bool, std::string > GetHeader(const std::string &hdr) const
Get the request header specified by hdr, or an empty string.
Opaque base class for timers returned by NewTimerFunc.
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
const UniValue NullUniValue
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
std::string ReadBody()
Read request body.
Standard JSON-RPC 2.0 errors.
static std::map< std::string, std::set< std::string > > g_rpc_whitelist
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
std::string GetURI() const
Get requested URI.
static struct event_base * eventBase
HTTP module state.