Raven Core  3.0.0
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "rpc/protocol.h"
8 
9 #include "random.h"
10 #include "tinyformat.h"
11 #include "util.h"
12 #include "utilstrencodings.h"
13 #include "utiltime.h"
14 #include "version.h"
15 
16 #include <stdint.h>
17 #include <fstream>
18 
28 UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
29 {
30  UniValue request(UniValue::VOBJ);
31  request.push_back(Pair("method", strMethod));
32  request.push_back(Pair("params", params));
33  request.push_back(Pair("id", id));
34  return request;
35 }
36 
37 UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
38 {
39  UniValue reply(UniValue::VOBJ);
40  if (!error.isNull())
41  reply.push_back(Pair("result", NullUniValue));
42  else
43  reply.push_back(Pair("result", result));
44  reply.push_back(Pair("error", error));
45  reply.push_back(Pair("id", id));
46  return reply;
47 }
48 
49 std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
50 {
51  UniValue reply = JSONRPCReplyObj(result, error, id);
52  return reply.write() + "\n";
53 }
54 
55 UniValue JSONRPCError(int code, const std::string& message)
56 {
58  error.push_back(Pair("code", code));
59  error.push_back(Pair("message", message));
60  return error;
61 }
62 
66 static const std::string COOKIEAUTH_USER = "__cookie__";
68 static const std::string COOKIEAUTH_FILE = ".cookie";
69 
71 static fs::path GetAuthCookieFile(bool temp=false)
72 {
73  std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE);
74  if (temp) {
75  arg += ".tmp";
76  }
77  fs::path path(arg);
78  if (!path.is_complete()) path = GetDataDir() / path;
79  return path;
80 }
81 
82 bool GenerateAuthCookie(std::string *cookie_out)
83 {
84  const size_t COOKIE_SIZE = 32;
85  unsigned char rand_pwd[COOKIE_SIZE];
86  GetRandBytes(rand_pwd, COOKIE_SIZE);
87  std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
88 
92  std::ofstream file;
93  fs::path filepath_tmp = GetAuthCookieFile(true);
94  file.open(filepath_tmp.string().c_str());
95  if (!file.is_open()) {
96  LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
97  return false;
98  }
99  file << cookie;
100  file.close();
101 
102  fs::path filepath = GetAuthCookieFile(false);
103  if (!RenameOver(filepath_tmp, filepath)) {
104  LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
105  return false;
106  }
107  LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
108 
109  if (cookie_out)
110  *cookie_out = cookie;
111  return true;
112 }
113 
114 bool GetAuthCookie(std::string *cookie_out)
115 {
116  std::ifstream file;
117  std::string cookie;
118  fs::path filepath = GetAuthCookieFile();
119  file.open(filepath.string().c_str());
120  if (!file.is_open())
121  return false;
122  std::getline(file, cookie);
123  file.close();
124 
125  if (cookie_out)
126  *cookie_out = cookie;
127  return true;
128 }
129 
131 {
132  try {
133  fs::remove(GetAuthCookieFile());
134  } catch (const fs::filesystem_error& e) {
135  LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());
136  }
137 }
138 
139 std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue &in, size_t num)
140 {
141  if (!in.isArray()) {
142  throw std::runtime_error("Batch must be an array");
143  }
144  std::vector<UniValue> batch(num);
145  for (size_t i=0; i<in.size(); ++i) {
146  const UniValue &rec = in[i];
147  if (!rec.isObject()) {
148  throw std::runtime_error("Batch member must be object");
149  }
150  size_t id = rec["id"].get_int();
151  if (id >= num) {
152  throw std::runtime_error("Batch member id larger than size");
153  }
154  batch[id] = rec;
155  }
156  return batch;
157 }
bool isObject() const
Definition: univalue.h:86
UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue &params, const UniValue &id)
JSON-RPC protocol.
Definition: protocol.cpp:28
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
Definition: protocol.cpp:130
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:49
std::vector< UniValue > JSONRPCProcessBatchReply(const UniValue &in, size_t num)
Parse JSON-RPC batch reply into a vector.
Definition: protocol.cpp:139
bool GetAuthCookie(std::string *cookie_out)
Read the RPC authentication cookie from disk.
Definition: protocol.cpp:114
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
#define LogPrintf(...)
Definition: util.h:149
int get_int() const
bool RenameOver(fs::path src, fs::path dest)
Definition: util.cpp:668
bool isNull() const
Definition: univalue.h:79
ArgsManager gArgs
Definition: util.cpp:94
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
Definition: protocol.cpp:82
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:454
const UniValue NullUniValue
Definition: univalue.cpp:15
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:274
bool error(const char *fmt, const Args &... args)
Definition: util.h:168
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:572
UniValue JSONRPCError(int code, const std::string &message)
Definition: protocol.cpp:55
size_t size() const
Definition: univalue.h:70
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:37
bool isArray() const
Definition: univalue.h:85