Fabcoin Core  0.16.2
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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.h>
11 #include <utilstrencodings.h>
12 #include <utiltime.h>
13 #include <version.h>
14 
15 #include <stdint.h>
16 #include <fstream>
17 
27 UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
28 {
29  UniValue request(UniValue::VOBJ);
30  request.push_back(Pair("method", strMethod));
31  request.push_back(Pair("params", params));
32  request.push_back(Pair("id", id));
33  return request;
34 }
35 
36 UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
37 {
38  UniValue reply(UniValue::VOBJ);
39  if (!error.isNull())
40  reply.push_back(Pair("result", NullUniValue));
41  else
42  reply.push_back(Pair("result", result));
43  reply.push_back(Pair("error", error));
44  reply.push_back(Pair("id", id));
45  return reply;
46 }
47 
48 std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
49 {
50  UniValue reply = JSONRPCReplyObj(result, error, id);
51  return reply.write() + "\n";
52 }
53 
54 UniValue JSONRPCError(int code, const std::string& message)
55 {
57  error.push_back(Pair("code", code));
58  error.push_back(Pair("message", message));
59  return error;
60 }
61 
65 static const std::string COOKIEAUTH_USER = "__cookie__";
67 static const std::string COOKIEAUTH_FILE = ".cookie";
68 
70 static fs::path GetAuthCookieFile(bool temp=false)
71 {
72  std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE);
73  if (temp) {
74  arg += ".tmp";
75  }
76  fs::path path(arg);
77  if (!path.is_complete()) path = GetDataDir() / path;
78  return path;
79 }
80 
81 bool GenerateAuthCookie(std::string *cookie_out)
82 {
83  const size_t COOKIE_SIZE = 32;
84  unsigned char rand_pwd[COOKIE_SIZE];
85  GetRandBytes(rand_pwd, COOKIE_SIZE);
86  std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
87 
91  std::ofstream file;
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());
96  return false;
97  }
98  file << cookie;
99  file.close();
100 
101  fs::path filepath = GetAuthCookieFile(false);
102  if (!RenameOver(filepath_tmp, filepath)) {
103  LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
104  return false;
105  }
106  LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
107 
108  if (cookie_out)
109  *cookie_out = cookie;
110  return true;
111 }
112 
113 bool GetAuthCookie(std::string *cookie_out)
114 {
115  std::ifstream file;
116  std::string cookie;
117  fs::path filepath = GetAuthCookieFile();
118  file.open(filepath.string().c_str());
119  if (!file.is_open())
120  return false;
121  std::getline(file, cookie);
122  file.close();
123 
124  if (cookie_out)
125  *cookie_out = cookie;
126  return true;
127 }
128 
130 {
131  try {
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());
135  }
136 }
137 
bool error(const char *fmt, const Args &...args)
Definition: util.h:178
UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue &params, const UniValue &id)
JSON-RPC protocol.
Definition: protocol.cpp:27
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
Definition: protocol.cpp:129
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:48
bytes code
Definition: SmartVM.cpp:45
bool GetAuthCookie(std::string *cookie_out)
Read the RPC authentication cookie from disk.
Definition: protocol.cpp:113
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
#define LogPrintf(...)
Definition: util.h:153
bool RenameOver(fs::path src, fs::path dest)
Definition: util.cpp:714
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:81
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:504
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:273
#define e(i)
Definition: sha.cpp:733
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:623
UniValue JSONRPCError(int code, const std::string &message)
Definition: protocol.cpp:54
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:36
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool isNull() const
Definition: univalue.h:79
Config::Pair_type Pair