21 #include <boost/algorithm/string.hpp> 24 static const char* WWW_AUTH_HEADER_DATA =
"Basic realm=\"jsonrpc\"";
33 ev(eventBase, false, NULL, func)
36 tv.tv_sec = millis/1000;
37 tv.tv_usec = (millis%1000)*1000;
50 const char*
Name()
override 64 static std::string strRPCUserColonPass;
86 req->
WriteHeader(
"Content-Type",
"application/json");
93 static bool multiUserAuthorized(std::string strUserPass)
95 if (strUserPass.find(
":") == std::string::npos) {
98 std::string strUser = strUserPass.substr(0, strUserPass.find(
":"));
99 std::string strPass = strUserPass.substr(strUserPass.find(
":") + 1);
101 for (
const std::string& strRPCAuth :
gArgs.
GetArgs(
"-rpcauth")) {
103 std::vector<std::string> vFields;
104 boost::split(vFields, strRPCAuth, boost::is_any_of(
":$"));
105 if (vFields.size() != 3) {
110 std::string strName = vFields[0];
115 std::string strSalt = vFields[1];
116 std::string strHash = vFields[2];
118 static const unsigned int KEY_SIZE = 32;
119 unsigned char out[KEY_SIZE];
121 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);
122 std::vector<unsigned char> hexvec(out, out+KEY_SIZE);
123 std::string strHashFromPass =
HexStr(hexvec);
132 static bool RPCAuthorized(
const std::string& strAuth, std::string& strAuthUsernameOut)
134 if (strRPCUserColonPass.empty())
136 if (strAuth.substr(0, 6) !=
"Basic ")
138 std::string strUserPass64 = strAuth.substr(6);
139 boost::trim(strUserPass64);
142 if (strUserPass.find(
":") != std::string::npos)
143 strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(
":"));
149 return multiUserAuthorized(strUserPass);
152 static bool HTTPReq_JSONRPC(
HTTPRequest* req,
const std::string &)
160 std::pair<bool, std::string> authHeader = req->
GetHeader(
"authorization");
161 if (!authHeader.first) {
162 req->
WriteHeader(
"WWW-Authenticate", WWW_AUTH_HEADER_DATA);
168 if (!RPCAuthorized(authHeader.second, jreq.
authUser)) {
176 req->
WriteHeader(
"WWW-Authenticate", WWW_AUTH_HEADER_DATA);
190 std::string strReply;
193 jreq.
parse(valRequest);
206 }
else if (valRequest.
isArray())
211 req->
WriteHeader(
"Content-Type",
"application/json");
213 }
catch (
const UniValue& objError) {
214 JSONErrorReply(req, objError, jreq.
id);
216 }
catch (
const std::exception&
e) {
223 static bool InitRPCAuthentication()
227 LogPrintf(
"No rpcpassword set - using random cookie authentication\n");
230 _(
"Error: A fatal internal error occurred, see debug.log for details"),
235 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");
244 if (!InitRPCAuthentication())
267 if (httpRPCTimerInterface) {
269 delete httpRPCTimerInterface;
270 httpRPCTimerInterface = 0;
UniValue execute(const JSONRPCRequest &request) const
Execute a method.
#define function(a, b, c, d, k, s)
HTTPRPCTimer(struct event_base *eventBase, std::function< void(void)> &func, int64_t millis)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
HTTPRPCTimerInterface(struct event_base *_base)
void MilliSleep(int64_t n)
const char * Name() override
Implementation name.
bool read(const char *raw, size_t len)
std::string JSONRPCExecBatch(const UniValue &vReq)
std::pair< bool, std::string > GetHeader(const std::string &hdr)
Get the request header specified by hdr, or an empty string.
bool StartHTTPRPC()
Start HTTP RPC subsystem.
std::string GetURI()
Get requested URI.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
void Chunk(const std::string &chunk)
Start chunk transfer.
A hasher class for HMAC-SHA-256.
std::vector< std::string > GetArgs(const std::string &strArg)
assert(len-trim+(2 *lenIndices)<=WIDTH)
void InterruptHTTPRPC()
Interrupt HTTP RPC subsystem.
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
RequestMethod GetRequestMethod()
Get request method.
void ChunkEnd()
End chunk transfer.
void PollReply(const UniValue &result)
Return the JSON result of a long poll request.
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
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.
const Object_type::value_type::Value_type & find_value(const Object_type &obj, const String_type &name)
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
std::string ToString() const
CService GetPeer()
Get CService (address:ip) for the origin of the http request.
struct event_base * EventBase()
Return evhttp event base.
void StopHTTPRPC()
Stop HTTP RPC subsystem.
#define LogPrint(category,...)
void parse(const UniValue &valRequest)
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
const UniValue & get_array() const
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
void trigger(struct timeval *tv)
Trigger the event.
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set the factory function for timers.
Opaque base class for timers returned by NewTimerFunc.
const UniValue NullUniValue
boost::signals2::signal< bool(const std::string &message, const std::string &caption, unsigned int style), boost::signals2::last_value< bool > > ThreadSafeMessageBox
Show message box.
std::string ReadBody()
Read request body.
Standard JSON-RPC 2.0 errors.
UniValue JSONRPCError(int code, const std::string &message)
CClientUIInterface uiInterface
RPCTimerBase * NewTimer(std::function< void(void)> &func, int64_t millis) override
Factory function for timers.
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...