Branch data Line data Source code
1 : : // Copyright (c) 2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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/request.h>
7 : :
8 : : #include <util/fs.h>
9 : :
10 : : #include <common/args.h>
11 : : #include <logging.h>
12 : : #include <random.h>
13 : : #include <rpc/protocol.h>
14 : : #include <util/fs_helpers.h>
15 : : #include <util/strencodings.h>
16 : :
17 : : #include <fstream>
18 : : #include <stdexcept>
19 : : #include <string>
20 : : #include <vector>
21 : :
22 : : /**
23 : : * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility,
24 : : * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
25 : : * unspecified (HTTP errors and contents of 'error').
26 : : *
27 : : * 1.0 spec: http://json-rpc.org/wiki/specification
28 : : * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
29 : : */
30 : :
31 : 0 : UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
32 : : {
33 [ # # ]: 0 : UniValue request(UniValue::VOBJ);
34 [ # # ][ # # ]: 0 : request.pushKV("method", strMethod);
[ # # ]
35 [ # # ][ # # ]: 0 : request.pushKV("params", params);
[ # # ]
36 [ # # ][ # # ]: 0 : request.pushKV("id", id);
[ # # ]
37 : 0 : return request;
38 [ # # ]: 0 : }
39 : :
40 : 0 : UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
41 : : {
42 [ # # ]: 0 : UniValue reply(UniValue::VOBJ);
43 [ # # ][ # # ]: 0 : if (!error.isNull())
44 [ # # ][ # # ]: 0 : reply.pushKV("result", NullUniValue);
[ # # ]
45 : : else
46 [ # # ][ # # ]: 0 : reply.pushKV("result", result);
[ # # ]
47 [ # # ][ # # ]: 0 : reply.pushKV("error", error);
[ # # ]
48 [ # # ][ # # ]: 0 : reply.pushKV("id", id);
[ # # ]
49 : 0 : return reply;
50 [ # # ]: 0 : }
51 : :
52 : 0 : std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
53 : : {
54 : 0 : UniValue reply = JSONRPCReplyObj(result, error, id);
55 [ # # ][ # # ]: 0 : return reply.write() + "\n";
56 : 0 : }
57 : :
58 : 0 : UniValue JSONRPCError(int code, const std::string& message)
59 : : {
60 [ # # ]: 0 : UniValue error(UniValue::VOBJ);
61 [ # # ][ # # ]: 0 : error.pushKV("code", code);
[ # # ]
62 [ # # ][ # # ]: 0 : error.pushKV("message", message);
[ # # ]
63 : 0 : return error;
64 [ # # ]: 0 : }
65 : :
66 : : /** Username used when cookie authentication is in use (arbitrary, only for
67 : : * recognizability in debugging/logging purposes)
68 : : */
69 : : static const std::string COOKIEAUTH_USER = "__cookie__";
70 : : /** Default name for auth cookie file */
71 : : static const char* const COOKIEAUTH_FILE = ".cookie";
72 : :
73 : : /** Get name of RPC authentication cookie file */
74 : 0 : static fs::path GetAuthCookieFile(bool temp=false)
75 : : {
76 [ # # ][ # # ]: 0 : fs::path arg = gArgs.GetPathArg("-rpccookiefile", COOKIEAUTH_FILE);
[ # # ]
77 [ # # ]: 0 : if (temp) {
78 [ # # ]: 0 : arg += ".tmp";
79 : 0 : }
80 [ # # ]: 0 : return AbsPathForConfigVal(gArgs, arg);
81 : 0 : }
82 : :
83 : : static bool g_generated_cookie = false;
84 : :
85 : 0 : bool GenerateAuthCookie(std::string *cookie_out)
86 : : {
87 : 0 : const size_t COOKIE_SIZE = 32;
88 : : unsigned char rand_pwd[COOKIE_SIZE];
89 : 0 : GetRandBytes(rand_pwd);
90 [ # # ][ # # ]: 0 : std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
91 : :
92 : : /** the umask determines what permissions are used to create this file -
93 : : * these are set to 0077 in common/system.cpp.
94 : : */
95 [ # # ]: 0 : std::ofstream file;
96 [ # # ]: 0 : fs::path filepath_tmp = GetAuthCookieFile(true);
97 [ # # ]: 0 : file.open(filepath_tmp);
98 [ # # ][ # # ]: 0 : if (!file.is_open()) {
99 [ # # ][ # # ]: 0 : LogPrintf("Unable to open cookie authentication file %s for writing\n", fs::PathToString(filepath_tmp));
[ # # ][ # # ]
100 : 0 : return false;
101 : : }
102 [ # # ]: 0 : file << cookie;
103 [ # # ]: 0 : file.close();
104 : :
105 [ # # ]: 0 : fs::path filepath = GetAuthCookieFile(false);
106 [ # # ][ # # ]: 0 : if (!RenameOver(filepath_tmp, filepath)) {
[ # # ][ # # ]
107 [ # # ][ # # ]: 0 : LogPrintf("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
[ # # ][ # # ]
[ # # ]
108 : 0 : return false;
109 : : }
110 : 0 : g_generated_cookie = true;
111 [ # # ][ # # ]: 0 : LogPrintf("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
[ # # ][ # # ]
112 : :
113 [ # # ]: 0 : if (cookie_out)
114 [ # # ]: 0 : *cookie_out = cookie;
115 : 0 : return true;
116 : 0 : }
117 : :
118 : 0 : bool GetAuthCookie(std::string *cookie_out)
119 : : {
120 : 0 : std::ifstream file;
121 : 0 : std::string cookie;
122 [ # # ]: 0 : fs::path filepath = GetAuthCookieFile();
123 [ # # ]: 0 : file.open(filepath);
124 [ # # ][ # # ]: 0 : if (!file.is_open())
125 : 0 : return false;
126 [ # # ]: 0 : std::getline(file, cookie);
127 [ # # ]: 0 : file.close();
128 : :
129 [ # # ]: 0 : if (cookie_out)
130 [ # # ]: 0 : *cookie_out = cookie;
131 : 0 : return true;
132 : 0 : }
133 : :
134 : 0 : void DeleteAuthCookie()
135 : : {
136 : : try {
137 [ # # ]: 0 : if (g_generated_cookie) {
138 : : // Delete the cookie file if it was generated by this process
139 [ # # ][ # # ]: 0 : fs::remove(GetAuthCookieFile());
140 : 0 : }
141 [ # # ]: 0 : } catch (const fs::filesystem_error& e) {
142 [ # # ][ # # ]: 0 : LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
[ # # ][ # # ]
143 [ # # ]: 0 : }
144 : 0 : }
145 : :
146 : 0 : std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in)
147 : : {
148 [ # # ]: 0 : if (!in.isArray()) {
149 [ # # ]: 0 : throw std::runtime_error("Batch must be an array");
150 : : }
151 : 0 : const size_t num {in.size()};
152 [ # # ]: 0 : std::vector<UniValue> batch(num);
153 [ # # ][ # # ]: 0 : for (const UniValue& rec : in.getValues()) {
154 [ # # ][ # # ]: 0 : if (!rec.isObject()) {
155 [ # # ]: 0 : throw std::runtime_error("Batch member must be an object");
156 : : }
157 [ # # ][ # # ]: 0 : size_t id = rec["id"].getInt<int>();
[ # # ]
158 [ # # ]: 0 : if (id >= num) {
159 [ # # ]: 0 : throw std::runtime_error("Batch member id is larger than batch size");
160 : : }
161 [ # # ]: 0 : batch[id] = rec;
162 : : }
163 : 0 : return batch;
164 [ # # ]: 0 : }
165 : :
166 : 0 : void JSONRPCRequest::parse(const UniValue& valRequest)
167 : : {
168 : : // Parse request
169 [ # # ]: 0 : if (!valRequest.isObject())
170 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
[ # # ]
171 : 0 : const UniValue& request = valRequest.get_obj();
172 : :
173 : : // Parse id now so errors from here on will have the id
174 : 0 : id = request.find_value("id");
175 : :
176 : : // Parse method
177 : 0 : const UniValue& valMethod{request.find_value("method")};
178 [ # # ]: 0 : if (valMethod.isNull())
179 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
[ # # ]
180 [ # # ]: 0 : if (!valMethod.isStr())
181 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
[ # # ]
182 : 0 : strMethod = valMethod.get_str();
183 [ # # ]: 0 : if (fLogIPs)
184 [ # # ][ # # ]: 0 : LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod),
[ # # ][ # # ]
[ # # ]
185 : : this->authUser, this->peerAddr);
186 : : else
187 [ # # ][ # # ]: 0 : LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
[ # # ][ # # ]
[ # # ]
188 : :
189 : : // Parse params
190 : 0 : const UniValue& valParams{request.find_value("params")};
191 [ # # ][ # # ]: 0 : if (valParams.isArray() || valParams.isObject())
192 : 0 : params = valParams;
193 [ # # ]: 0 : else if (valParams.isNull())
194 [ # # ]: 0 : params = UniValue(UniValue::VARR);
195 : : else
196 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object");
[ # # ]
197 : 0 : }
|