20 #include <boost/algorithm/string.hpp>
28 HTTPRPCTimer(
struct event_base* eventBase, std::function<
void(
void)>& func, int64_t millis) :
32 tv.tv_sec = millis/1000;
33 tv.tv_usec = (millis%1000)*1000;
60 static std::string strRPCUserColonPass;
62 static std::unique_ptr<HTTPRPCTimerInterface> httpRPCTimerInterface;
77 req->
WriteHeader(
"Content-Type",
"application/json");
83 static bool multiUserAuthorized(std::string strUserPass)
85 if (strUserPass.find(
':') == std::string::npos) {
88 std::string strUser = strUserPass.substr(0, strUserPass.find(
':'));
89 std::string strPass = strUserPass.substr(strUserPass.find(
':') + 1);
91 for (
const std::string& strRPCAuth :
gArgs.
GetArgs(
"-rpcauth")) {
93 std::vector<std::string> vFields;
94 boost::split(vFields, strRPCAuth, boost::is_any_of(
":$"));
95 if (vFields.size() != 3) {
100 std::string strName = vFields[0];
105 std::string strSalt = vFields[1];
106 std::string strHash = vFields[2];
108 static const unsigned int KEY_SIZE = 32;
109 unsigned char out[KEY_SIZE];
111 CHMAC_SHA256(
reinterpret_cast<const unsigned char*
>(strSalt.c_str()), strSalt.size()).
Write(
reinterpret_cast<const unsigned char*
>(strPass.c_str()), strPass.size()).
Finalize(out);
112 std::vector<unsigned char> hexvec(out, out+KEY_SIZE);
113 std::string strHashFromPass =
HexStr(hexvec);
122 static bool RPCAuthorized(
const std::string& strAuth, std::string& strAuthUsernameOut)
124 if (strRPCUserColonPass.empty())
126 if (strAuth.substr(0, 6) !=
"Basic ")
128 std::string strUserPass64 = strAuth.substr(6);
129 boost::trim(strUserPass64);
132 if (strUserPass.find(
':') != std::string::npos)
133 strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(
':'));
139 return multiUserAuthorized(strUserPass);
142 static bool HTTPReq_JSONRPC(
HTTPRequest* req,
const std::string &)
150 std::pair<bool, std::string> authHeader = req->
GetHeader(
"authorization");
151 if (!authHeader.first) {
157 if (!RPCAuthorized(authHeader.second, jreq.
authUser)) {
158 LogPrintf(
"ThreadRPCServer incorrect password attempt from %s\n", req->
GetPeer().
ToString());
178 std::string strReply;
181 jreq.
parse(valRequest);
189 }
else if (valRequest.
isArray())
194 req->
WriteHeader(
"Content-Type",
"application/json");
196 }
catch (
const UniValue& objError) {
197 JSONErrorReply(req, objError, jreq.
id);
199 }
catch (
const std::exception& e) {
206 static bool InitRPCAuthentication()
210 LogPrintf(
"No rpcpassword set - using random cookie authentication\n");
213 _(
"Error: A fatal internal error occurred, see debug.log for details"),
218 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/rpcuser for rpcauth auth generation.\n");
227 if (!InitRPCAuthentication())
236 httpRPCTimerInterface = std::make_unique<HTTPRPCTimerInterface>(
EventBase());
250 if (httpRPCTimerInterface) {
252 httpRPCTimerInterface.reset();
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.
boost::signals2::signal< bool(const std::string &message, const std::string &caption, unsigned int style), boost::signals2::last_value< bool > > ThreadSafeMessageBox
Show message box.
A hasher class for HMAC-SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CHMAC_SHA256 & Write(const unsigned char *data, size_t len)
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
std::string ToString() const
void trigger(struct timeval *tv)
Trigger the event.
Simple one-shot callback timer to be used by the RPC mechanism to e.g.
HTTPRPCTimer(struct event_base *eventBase, std::function< void(void)> &func, int64_t millis)
RPCTimerBase * NewTimer(std::function< void(void)> &func, int64_t millis)
Factory function for timers.
const char * Name()
Implementation name.
HTTPRPCTimerInterface(struct event_base *base)
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
std::string GetURI()
Get requested URI.
CService GetPeer()
Get CService (address:ip) for the origin of the http request.
std::pair< bool, std::string > GetHeader(const std::string &hdr)
Get the request header specified by hdr, or an empty string.
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
std::string ReadBody()
Read request body.
RequestMethod GetRequestMethod()
Get request method.
void parse(const UniValue &valRequest)
Opaque base class for timers returned by NewTimerFunc.
const UniValue & get_array() const
bool read(const char *raw, size_t len)
CClientUIInterface uiInterface
void InterruptHTTPRPC()
Interrupt HTTP RPC subsystem.
void StopHTTPRPC()
Stop HTTP RPC subsystem.
bool StartHTTPRPC()
Start HTTP RPC subsystem.
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.
struct event_base * EventBase()
Return evhttp event base.
#define LogPrint(category,...)
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
UniValue JSONRPCError(int code, const std::string &message)
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
@ HTTP_INTERNAL_SERVER_ERROR
@ RPC_INVALID_REQUEST
Standard JSON-RPC 2.0 errors.
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
std::string JSONRPCExecBatch(const UniValue &vReq)
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set factory function for timers.
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a Optional result.
const UniValue & find_value(const UniValue &obj, const std::string &name)
const UniValue NullUniValue
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
void MilliSleep(int64_t n)