Bitcoin Core  0.18.99
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <rpc/protocol.h>
7 
8 #include <random.h>
9 #include <tinyformat.h>
10 #include <util/system.h>
11 #include <util/strencodings.h>
12 #include <util/time.h>
13 
23 UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
24 {
25  UniValue request(UniValue::VOBJ);
26  request.pushKV("method", strMethod);
27  request.pushKV("params", params);
28  request.pushKV("id", id);
29  return request;
30 }
31 
32 UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
33 {
34  UniValue reply(UniValue::VOBJ);
35  if (!error.isNull())
36  reply.pushKV("result", NullUniValue);
37  else
38  reply.pushKV("result", result);
39  reply.pushKV("error", error);
40  reply.pushKV("id", id);
41  return reply;
42 }
43 
44 std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
45 {
46  UniValue reply = JSONRPCReplyObj(result, error, id);
47  return reply.write() + "\n";
48 }
49 
50 UniValue JSONRPCError(int code, const std::string& message)
51 {
53  error.pushKV("code", code);
54  error.pushKV("message", message);
55  return error;
56 }
57 
61 static const std::string COOKIEAUTH_USER = "__cookie__";
63 static const std::string COOKIEAUTH_FILE = ".cookie";
64 
66 static fs::path GetAuthCookieFile(bool temp=false)
67 {
68  std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE);
69  if (temp) {
70  arg += ".tmp";
71  }
72  return AbsPathForConfigVal(fs::path(arg));
73 }
74 
75 bool GenerateAuthCookie(std::string *cookie_out)
76 {
77  const size_t COOKIE_SIZE = 32;
78  unsigned char rand_pwd[COOKIE_SIZE];
79  GetRandBytes(rand_pwd, COOKIE_SIZE);
80  std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
81 
85  fsbridge::ofstream file;
86  fs::path filepath_tmp = GetAuthCookieFile(true);
87  file.open(filepath_tmp);
88  if (!file.is_open()) {
89  LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
90  return false;
91  }
92  file << cookie;
93  file.close();
94 
95  fs::path filepath = GetAuthCookieFile(false);
96  if (!RenameOver(filepath_tmp, filepath)) {
97  LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
98  return false;
99  }
100  LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
101 
102  if (cookie_out)
103  *cookie_out = cookie;
104  return true;
105 }
106 
107 bool GetAuthCookie(std::string *cookie_out)
108 {
109  fsbridge::ifstream file;
110  std::string cookie;
111  fs::path filepath = GetAuthCookieFile();
112  file.open(filepath);
113  if (!file.is_open())
114  return false;
115  std::getline(file, cookie);
116  file.close();
117 
118  if (cookie_out)
119  *cookie_out = cookie;
120  return true;
121 }
122 
124 {
125  try {
126  fs::remove(GetAuthCookieFile());
127  } catch (const fs::filesystem_error& e) {
128  LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
129  }
130 }
131 
132 std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue &in, size_t num)
133 {
134  if (!in.isArray()) {
135  throw std::runtime_error("Batch must be an array");
136  }
137  std::vector<UniValue> batch(num);
138  for (size_t i=0; i<in.size(); ++i) {
139  const UniValue &rec = in[i];
140  if (!rec.isObject()) {
141  throw std::runtime_error("Batch member must be object");
142  }
143  size_t id = rec["id"].get_int();
144  if (id >= num) {
145  throw std::runtime_error("Batch member id larger than size");
146  }
147  batch[id] = rec;
148  }
149  return batch;
150 }
bool isObject() const
Definition: univalue.h:85
UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue &params, const UniValue &id)
JSON-RPC protocol.
Definition: protocol.cpp:23
fs::ifstream ifstream
Definition: fs.h:91
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:144
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
Definition: protocol.cpp:123
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:659
fs::ofstream ofstream
Definition: fs.h:92
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:44
std::vector< UniValue > JSONRPCProcessBatchReply(const UniValue &in, size_t num)
Parse JSON-RPC batch reply into a vector.
Definition: protocol.cpp:132
bool GetAuthCookie(std::string *cookie_out)
Read the RPC authentication cookie from disk.
Definition: protocol.cpp:107
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
static fs::path GetAuthCookieFile(bool temp=false)
Get name of RPC authentication cookie file.
Definition: protocol.cpp:66
int get_int() const
fs::path AbsPathForConfigVal(const fs::path &path, bool net_specific)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
Definition: system.cpp:1203
bool isNull() const
Definition: univalue.h:78
bool RenameOver(fs::path src, fs::path dest)
Definition: system.cpp:959
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:103
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
Definition: protocol.cpp:75
static const std::string COOKIEAUTH_USER
Username used when cookie authentication is in use (arbitrary, only for recognizability in debugging/...
Definition: protocol.cpp:61
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:494
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:125
ArgsManager gArgs
Definition: system.cpp:72
static const std::string COOKIEAUTH_FILE
Default name for auth cookie file.
Definition: protocol.cpp:63
const UniValue NullUniValue
Definition: univalue.cpp:13
UniValue JSONRPCError(int code, const std::string &message)
Definition: protocol.cpp:50
size_t size() const
Definition: univalue.h:69
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:32
bool error(const char *fmt, const Args &... args)
Definition: system.h:59
bool isArray() const
Definition: univalue.h:84