 |
PRCYCoin
2.0.0.7rc1
P2P Digital Currency
|
Go to the documentation of this file.
22 #include <boost/algorithm/string.hpp>
25 static const char* WWW_AUTH_HEADER_DATA =
"Basic realm=\"jsonrpc\"";
33 HTTPRPCTimer(
struct event_base* eventBase, boost::function<
void(
void)>& func, int64_t millis) :
34 ev(eventBase, false, func)
37 tv.tv_sec = millis/1000;
38 tv.tv_usec = (millis%1000)*1000;
65 static std::string strRPCUserColonPass;
82 req->
WriteHeader(
"Content-Type",
"application/json");
88 static bool multiUserAuthorized(std::string strUserPass)
90 if (strUserPass.find(
":") == std::string::npos) {
93 std::string strUser = strUserPass.substr(0, strUserPass.find(
":"));
94 std::string strPass = strUserPass.substr(strUserPass.find(
":") + 1);
100 std::vector<std::string> vFields;
101 boost::split(vFields, strRPCAuth, boost::is_any_of(
":$"));
102 if (vFields.size() != 3) {
107 std::string strName = vFields[0];
112 std::string strSalt = vFields[1];
113 std::string strHash = vFields[2];
115 unsigned int KEY_SIZE = 32;
116 unsigned char *out =
new unsigned char[KEY_SIZE];
118 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);
119 std::vector<unsigned char> hexvec(out, out+KEY_SIZE);
120 std::string strHashFromPass =
HexStr(hexvec);
130 static bool RPCAuthorized(
const std::string& strAuth)
132 if (strRPCUserColonPass.empty())
134 if (strAuth.substr(0, 6) !=
"Basic ")
136 std::string strUserPass64 = strAuth.substr(6);
137 boost::trim(strUserPass64);
144 return multiUserAuthorized(strUserPass);
147 static bool HTTPReq_JSONRPC(
HTTPRequest* req,
const std::string &)
155 std::pair<bool, std::string> authHeader = req->
GetHeader(
"authorization");
156 if (!authHeader.first) {
157 req->
WriteHeader(
"WWW-Authenticate", WWW_AUTH_HEADER_DATA);
162 if (!RPCAuthorized(authHeader.second)) {
170 req->
WriteHeader(
"WWW-Authenticate", WWW_AUTH_HEADER_DATA);
182 std::string strReply;
185 jreq.
parse(valRequest);
193 }
else if (valRequest.
isArray())
198 req->
WriteHeader(
"Content-Type",
"application/json");
200 }
catch (
const UniValue& objError) {
201 JSONErrorReply(req, objError, jreq.
id);
203 }
catch (
const std::exception& e) {
210 static bool InitRPCAuthentication()
212 if (
mapArgs[
"-rpcpassword"] ==
"")
214 LogPrintf(
"No rpcpassword set - using random cookie authentication\n");
217 _(
"Error: A fatal internal error occurred, see debug.log for details"),
222 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");
223 strRPCUserColonPass =
mapArgs[
"-rpcuser"] +
":" +
mapArgs[
"-rpcpassword"];
231 if (!InitRPCAuthentication())
251 if (httpRPCTimerInterface) {
253 delete httpRPCTimerInterface;
254 httpRPCTimerInterface = 0;
bool StartHTTPRPC()
Start HTTP RPC subsystem.
@ RPC_INVALID_REQUEST
Standard JSON-RPC 2.0 errors.
Simple one-shot callback timer to be used by the RPC mechanism to e.g.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CService GetPeer()
Get CService (address:ip) for the origin of the http request.
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
CClientUIInterface uiInterface
const UniValue NullUniValue
A hasher class for HMAC-SHA-256.
struct event_base * EventBase()
Return evhttp event base.
std::map< std::string, std::string > mapArgs
HTTPRPCTimerInterface(struct event_base *base)
Opaque base class for timers returned by NewTimerFunc.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
bool read(const char *raw, size_t len)
void InterruptHTTPRPC()
Interrupt HTTP RPC subsystem.
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
RPCTimerBase * NewTimer(boost::function< void(void)> &func, int64_t millis)
Factory function for timers.
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
std::string JSONRPCExecBatch(const UniValue &vReq)
std::string ToString() const
UniValue execute(const std::string &method, const UniValue ¶ms) const
Execute a method.
CHMAC_SHA256 & Write(const unsigned char *data, size_t len)
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
const char * Name()
Implementation name.
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.
void StopHTTPRPC()
Stop HTTP RPC subsystem.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
UniValue JSONRPCError(int code, const std::string &message)
std::pair< bool, std::string > GetHeader(const std::string &hdr)
Get the request header specified by hdr, or an empty string.
void parse(const UniValue &valRequest)
#define LogPrint(category,...)
void trigger(struct timeval *tv)
Trigger the event.
HTTPRPCTimer(struct event_base *eventBase, boost::function< void(void)> &func, int64_t millis)
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
void MilliSleep(int64_t n)
RequestMethod GetRequestMethod()
Get request method.
const UniValue & find_value(const UniValue &obj, const std::string &name)
const UniValue & get_array() const
std::map< std::string, std::vector< std::string > > mapMultiArgs
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
void RPCSetTimerInterface(RPCTimerInterface *iface)
Set factory function for timers.
@ HTTP_INTERNAL_SERVER_ERROR