51 return reply.
write() +
"\n";
65 static const std::string COOKIEAUTH_USER =
"__cookie__";
67 static const std::string COOKIEAUTH_FILE =
".cookie";
70 static fs::path GetAuthCookieFile(
bool temp=
false)
72 std::string arg =
gArgs.
GetArg(
"-rpccookiefile", COOKIEAUTH_FILE);
77 if (!path.is_complete()) path =
GetDataDir() / path;
83 const size_t COOKIE_SIZE = 32;
84 unsigned char rand_pwd[COOKIE_SIZE];
86 std::string cookie = COOKIEAUTH_USER +
":" +
HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
92 fs::path filepath_tmp = GetAuthCookieFile(
true);
93 file.open(filepath_tmp.string().c_str());
94 if (!file.is_open()) {
95 LogPrintf(
"Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
101 fs::path filepath = GetAuthCookieFile(
false);
103 LogPrintf(
"Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
106 LogPrintf(
"Generated RPC authentication cookie %s\n", filepath.string());
109 *cookie_out = cookie;
117 fs::path filepath = GetAuthCookieFile();
118 file.open(filepath.string().c_str());
121 std::getline(file, cookie);
125 *cookie_out = cookie;
132 fs::remove(GetAuthCookieFile());
133 }
catch (
const fs::filesystem_error&
e) {
134 LogPrintf(
"%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());
bool error(const char *fmt, const Args &...args)
UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue ¶ms, const UniValue &id)
JSON-RPC protocol.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
bool GetAuthCookie(std::string *cookie_out)
Read the RPC authentication cookie from disk.
bool push_back(const UniValue &val)
bool RenameOver(fs::path src, fs::path dest)
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
const UniValue NullUniValue
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
const fs::path & GetDataDir(bool fNetSpecific)
UniValue JSONRPCError(int code, const std::string &message)
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const