36static std::vector<std::vector<std::string>>
g_rpcauth;
48 int code =
objError.find_value(
"code").getInt<
int>();
57 req->
WriteHeader(
"Content-Type",
"application/json");
71 const std::string& hash =
fields[2];
73 std::array<unsigned char, CHMAC_SHA256::OUTPUT_SIZE> out;
86 if (!
strAuth.starts_with(
"Basic "))
123 LogWarning(
"ThreadRPCServer incorrect password attempt from %s",
jreq.peerAddr);
147 LogWarning(
"RPC User %s not allowed to call any methods",
jreq.authUser);
155 LogWarning(
"RPC User %s not allowed to call method %s",
jreq.authUser,
jreq.strMethod);
167 if (
jreq.IsNotification()) {
185 LogWarning(
"RPC User %s not allowed to call method %s",
jreq.authUser, strMethod);
195 for (
size_t i{0}; i <
valRequest.size(); ++i) {
204 }
catch (
const std::exception&
e) {
207 if (!
jreq.IsNotification()) {
228 req->
WriteHeader(
"Content-Type",
"application/json");
233 }
catch (
const std::exception&
e) {
262 LogInfo(
"RPC authentication cookie file generation is disabled.");
265 LogInfo(
"Using random cookie authentication.");
269 LogInfo(
"Using rpcuser/rpcpassword authentication.");
270 LogWarning(
"The use of rpcuser/rpcpassword is less secure, because credentials are configured in plain text. It is recommended that locally-run instances switch to cookie-based auth, or otherwise to use hashed rpcauth credentials. See share/rpcauth in the source directory for more information.");
276 if (!
user.empty() || !pass.empty()) {
278 std::array<unsigned char, 16>
raw_salt;
283 std::array<unsigned char, CHMAC_SHA256::OUTPUT_SIZE> out;
285 std::string hash =
HexStr(out);
291 LogInfo(
"Using rpcauth authentication.\n");
294 const std::vector<std::string>
salt_hmac{SplitString(
fields.back(),
'$')};
312 if (pos != std::string::npos) {
#define Assume(val)
Assume is the identity function.
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
A hasher class for HMAC-SHA-256.
CHMAC_SHA256 & Write(const unsigned char *data, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
std::string ToStringAddrPort() const
std::pair< bool, std::string > GetHeader(const std::string &hdr) const
Get the request header specified by hdr, or an empty string.
std::string GetURI() const
Get requested URI.
void WriteReply(int nStatus, std::string_view reply="")
Write HTTP reply.
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
RequestMethod GetRequestMethod() const
Get request method.
std::string ReadBody()
Read request body.
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
void push_back(UniValue val)
const std::string & get_str() const
const UniValue & find_value(std::string_view key) const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const UniValue & get_obj() const
virtual bool HasWalletSupport() const =0
Is the wallet component enabled.
const WalletInitInterface & g_wallet_init_interface
std::optional< fs::perms > InterpretPermString(const std::string &s)
Interpret a custom permissions level string as fs::perms.
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
static const char * WWW_AUTH_HEADER_DATA
WWW-Authenticate to present with 401 Unauthorized response.
void InterruptHTTPRPC()
Interrupt HTTP RPC subsystem.
void StopHTTPRPC()
Stop HTTP RPC subsystem.
static bool CheckUserAuthorized(std::string_view user, std::string_view pass)
bool StartHTTPRPC(const std::any &context)
Start HTTP RPC subsystem.
static bool g_rpc_whitelist_default
static bool RPCAuthorized(const std::string &strAuth, std::string &strAuthUsernameOut)
static std::vector< std::vector< std::string > > g_rpcauth
static bool HTTPReq_JSONRPC(const std::any &context, HTTPRequest *req)
static bool InitRPCAuthentication()
static std::map< std::string, std::set< std::string > > g_rpc_whitelist
static void JSONErrorReply(HTTPRequest *req, UniValue objError, const JSONRPCRequest &jreq)
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
static struct event_base * eventBase
HTTP module state.
struct event_base * EventBase()
Return evhttp event base.
#define LogDebug(category,...)
std::vector< std::string > SplitString(std::string_view str, char sep)
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
void GetStrongRandBytes(std::span< unsigned char > bytes) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
GenerateAuthCookieResult GenerateAuthCookie(const std::optional< fs::perms > &cookie_perms, std::string &user, std::string &pass)
Generate a new RPC authentication cookie and write it to disk.
UniValue JSONRPCError(int code, const std::string &message)
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional< UniValue > id, JSONRPCVersion jsonrpc_version)
@ HTTP_INTERNAL_SERVER_ERROR
@ RPC_INVALID_REQUEST
Standard JSON-RPC 2.0 errors.
UniValue JSONRPCExec(const JSONRPCRequest &jreq, bool catch_errors)
unsigned char * UCharCast(char *c)
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
const UniValue NullUniValue
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
void UninterruptibleSleep(const std::chrono::microseconds &n)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.