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 <common/args.h>
9 : : #include <logging.h>
10 : : #include <random.h>
11 : : #include <rpc/protocol.h>
12 : : #include <util/fs.h>
13 : : #include <util/fs_helpers.h>
14 : : #include <util/strencodings.h>
15 : :
16 : : #include <fstream>
17 : : #include <stdexcept>
18 : : #include <string>
19 : : #include <vector>
20 : :
21 : : /**
22 : : * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility,
23 : : * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
24 : : * unspecified (HTTP errors and contents of 'error').
25 : : *
26 : : * 1.0 spec: http://json-rpc.org/wiki/specification
27 : : * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
28 : : *
29 : : * If the server receives a request with the JSON-RPC 2.0 marker `{"jsonrpc": "2.0"}`
30 : : * then Bitcoin will respond with a strictly specified response.
31 : : * It will only return an HTTP error code if an actual HTTP error is encountered
32 : : * such as the endpoint is not found (404) or the request is not formatted correctly (500).
33 : : * Otherwise the HTTP code is always OK (200) and RPC errors will be included in the
34 : : * response body.
35 : : *
36 : : * 2.0 spec: https://www.jsonrpc.org/specification
37 : : *
38 : : * Also see http://www.simple-is-better.org/rpc/#differences-between-1-0-and-2-0
39 : : */
40 : :
41 : 792 : UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
42 : : {
43 : 792 : UniValue request(UniValue::VOBJ);
44 [ + - + - : 1584 : request.pushKV("method", strMethod);
+ - ]
45 [ + - + - : 1584 : request.pushKV("params", params);
+ - ]
46 [ + - + - : 1584 : request.pushKV("id", id);
+ - ]
47 [ + - + - : 1584 : request.pushKV("jsonrpc", "2.0");
+ - ]
48 : 792 : return request;
49 : 0 : }
50 : :
51 : 199144 : UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version)
52 : : {
53 : 199144 : UniValue reply(UniValue::VOBJ);
54 : : // Add JSON-RPC version number field in v2 only.
55 [ + + + - : 398130 : if (jsonrpc_version == JSONRPCVersion::V2) reply.pushKV("jsonrpc", "2.0");
+ - + - ]
56 : :
57 : : // Add both result and error fields in v1, even though one will be null.
58 : : // Omit the null field in v2.
59 [ + + ]: 199144 : if (error.isNull()) {
60 [ + - + - ]: 385584 : reply.pushKV("result", std::move(result));
61 [ + + + - : 192930 : if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("error", NullUniValue);
+ - + - ]
62 : : } else {
63 [ + + + - : 6372 : if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("result", NullUniValue);
+ - + - ]
64 [ + - + - ]: 12704 : reply.pushKV("error", std::move(error));
65 : : }
66 [ + + + - : 398248 : if (id.has_value()) reply.pushKV("id", std::move(id.value()));
+ - ]
67 : 199144 : return reply;
68 : 0 : }
69 : :
70 : 6416 : UniValue JSONRPCError(int code, const std::string& message)
71 : : {
72 : 6416 : UniValue error(UniValue::VOBJ);
73 [ + - + - : 12832 : error.pushKV("code", code);
+ - ]
74 [ + - + - : 12832 : error.pushKV("message", message);
+ - ]
75 : 6416 : return error;
76 : 0 : }
77 : :
78 : : /** Username used when cookie authentication is in use (arbitrary, only for
79 : : * recognizability in debugging/logging purposes)
80 : : */
81 : : static const std::string COOKIEAUTH_USER = "__cookie__";
82 : : /** Default name for auth cookie file */
83 : : static const char* const COOKIEAUTH_FILE = ".cookie";
84 : :
85 : : /** Get name of RPC authentication cookie file */
86 : 3816 : static fs::path GetAuthCookieFile(bool temp=false)
87 : : {
88 [ + - + - ]: 7632 : fs::path arg = gArgs.GetPathArg("-rpccookiefile", COOKIEAUTH_FILE);
89 [ + + ]: 3816 : if (arg.empty()) {
90 : 1 : return {}; // -norpccookiefile was specified
91 : : }
92 [ + + ]: 3815 : if (temp) {
93 [ + - ]: 1024 : arg += ".tmp";
94 : : }
95 [ + - ]: 3815 : return AbsPathForConfigVal(gArgs, arg);
96 : 3816 : }
97 : :
98 : : static bool g_generated_cookie = false;
99 : :
100 : 1025 : GenerateAuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cookie_perms,
101 : : std::string& user,
102 : : std::string& pass)
103 : : {
104 : 1025 : const size_t COOKIE_SIZE = 32;
105 : 1025 : unsigned char rand_pwd[COOKIE_SIZE];
106 : 1025 : GetRandBytes(rand_pwd);
107 : 1025 : const std::string rand_pwd_hex{HexStr(rand_pwd)};
108 : :
109 : : /** the umask determines what permissions are used to create this file -
110 : : * these are set to 0077 in common/system.cpp.
111 : : */
112 [ + - ]: 1025 : std::ofstream file;
113 [ + - ]: 1025 : fs::path filepath_tmp = GetAuthCookieFile(true);
114 [ + + ]: 1025 : if (filepath_tmp.empty()) {
115 : : return GenerateAuthCookieResult::DISABLED; // -norpccookiefile
116 : : }
117 [ + - ]: 1024 : file.open(filepath_tmp);
118 [ + + ]: 1024 : if (!file.is_open()) {
119 [ + - + - ]: 2 : LogWarning("Unable to open cookie authentication file %s for writing", fs::PathToString(filepath_tmp));
120 : 1 : return GenerateAuthCookieResult::ERR;
121 : : }
122 [ + - + - : 1023 : file << COOKIEAUTH_USER << ":" << rand_pwd_hex;
+ - ]
123 [ + - ]: 1023 : file.close();
124 : :
125 [ + - ]: 1023 : fs::path filepath = GetAuthCookieFile(false);
126 [ + - + - : 3069 : if (!RenameOver(filepath_tmp, filepath)) {
+ - - + ]
127 [ # # # # : 0 : LogWarning("Unable to rename cookie authentication file %s to %s", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
# # ]
128 : 0 : return GenerateAuthCookieResult::ERR;
129 : : }
130 [ + + ]: 1023 : if (cookie_perms) {
131 [ + - ]: 3 : std::error_code code;
132 [ + - ]: 3 : fs::permissions(filepath, cookie_perms.value(), fs::perm_options::replace, code);
133 [ - + ]: 3 : if (code) {
134 [ # # # # ]: 0 : LogWarning("Unable to set permissions on cookie authentication file %s", fs::PathToString(filepath));
135 : 0 : return GenerateAuthCookieResult::ERR;
136 : : }
137 : : }
138 : :
139 : 1023 : g_generated_cookie = true;
140 [ + - + - : 3069 : LogInfo("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
+ - ]
141 [ + - + - : 1023 : LogInfo("Permissions used for cookie: %s\n", PermsToSymbolicString(fs::status(filepath).permissions()));
+ - ]
142 : :
143 [ + - ]: 1023 : user = COOKIEAUTH_USER;
144 [ + - ]: 2046 : pass = rand_pwd_hex;
145 : : return GenerateAuthCookieResult::OK;
146 : 2048 : }
147 : :
148 : 745 : bool GetAuthCookie(std::string *cookie_out)
149 : : {
150 : 745 : std::ifstream file;
151 [ + - ]: 745 : std::string cookie;
152 [ + - ]: 745 : fs::path filepath = GetAuthCookieFile();
153 [ + - ]: 745 : if (filepath.empty()) {
154 : : return true; // -norpccookiefile
155 : : }
156 [ + - ]: 745 : file.open(filepath);
157 [ + + ]: 745 : if (!file.is_open())
158 : : return false;
159 [ + - ]: 738 : std::getline(file, cookie);
160 [ + - ]: 738 : file.close();
161 : :
162 [ + - ]: 738 : if (cookie_out)
163 [ + - ]: 1483 : *cookie_out = cookie;
164 : : return true;
165 : 745 : }
166 : :
167 : 1064 : void DeleteAuthCookie()
168 : : {
169 : 1064 : try {
170 [ + + ]: 1064 : if (g_generated_cookie) {
171 : : // Delete the cookie file if it was generated by this process
172 [ + - + - ]: 2046 : fs::remove(GetAuthCookieFile());
173 : : }
174 [ - - ]: 0 : } catch (const fs::filesystem_error& e) {
175 [ - - - - : 0 : LogWarning("Unable to remove random auth cookie file %s: %s\n", fs::PathToString(e.path1()), e.code().message());
- - - - ]
176 : 0 : }
177 : 1064 : }
178 : :
179 : 15 : std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in)
180 : : {
181 [ - + ]: 15 : if (!in.isArray()) {
182 [ # # ]: 0 : throw std::runtime_error("Batch must be an array");
183 : : }
184 : 15 : const size_t num {in.size()};
185 : 15 : std::vector<UniValue> batch(num);
186 [ + - + + ]: 75 : for (const UniValue& rec : in.getValues()) {
187 [ - + ]: 60 : if (!rec.isObject()) {
188 [ # # ]: 0 : throw std::runtime_error("Batch member must be an object");
189 : : }
190 [ + - + - : 60 : size_t id = rec["id"].getInt<int>();
+ - ]
191 [ - + ]: 60 : if (id >= num) {
192 [ # # ]: 0 : throw std::runtime_error("Batch member id is larger than batch size");
193 : : }
194 [ + - ]: 60 : batch[id] = rec;
195 : : }
196 : 15 : return batch;
197 : 0 : }
198 : :
199 : 199131 : void JSONRPCRequest::parse(const UniValue& valRequest)
200 : : {
201 : : // Parse request
202 [ - + ]: 199131 : if (!valRequest.isObject())
203 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
204 : 199131 : const UniValue& request = valRequest.get_obj();
205 : :
206 : : // Parse id now so errors from here on will have the id
207 [ + + ]: 398262 : if (request.exists("id")) {
208 : 199080 : id = request.find_value("id");
209 : : } else {
210 : 51 : id = std::nullopt;
211 : : }
212 : :
213 : : // Check for JSON-RPC 2.0 (default 1.1)
214 : 199131 : m_json_version = JSONRPCVersion::V1_LEGACY;
215 : 199131 : const UniValue& jsonrpc_version = request.find_value("jsonrpc");
216 [ + + ]: 199131 : if (!jsonrpc_version.isNull()) {
217 [ + + ]: 198974 : if (!jsonrpc_version.isStr()) {
218 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_REQUEST, "jsonrpc field must be a string");
219 : : }
220 : : // The "jsonrpc" key was added in the 2.0 spec, but some older documentation
221 : : // incorrectly included {"jsonrpc":"1.0"} in a request object, so we
222 : : // maintain that for backwards compatibility.
223 [ + + ]: 198973 : if (jsonrpc_version.get_str() == "1.0") {
224 : 4 : m_json_version = JSONRPCVersion::V1_LEGACY;
225 [ + + ]: 198969 : } else if (jsonrpc_version.get_str() == "2.0") {
226 : 198964 : m_json_version = JSONRPCVersion::V2;
227 : : } else {
228 [ + - + - ]: 10 : throw JSONRPCError(RPC_INVALID_REQUEST, "JSON-RPC version not supported");
229 : : }
230 : : }
231 : :
232 : : // Parse method
233 : 199125 : const UniValue& valMethod{request.find_value("method")};
234 [ + + ]: 199125 : if (valMethod.isNull())
235 [ + - + - ]: 16 : throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
236 [ - + ]: 199117 : if (!valMethod.isStr())
237 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
238 : 199117 : strMethod = valMethod.get_str();
239 [ + + ]: 199117 : if (fLogIPs)
240 [ + - + - ]: 56 : LogDebug(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod),
241 : : this->authUser, this->peerAddr);
242 : : else
243 [ + - + - ]: 398178 : LogDebug(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
244 : :
245 : : // Parse params
246 : 199117 : const UniValue& valParams{request.find_value("params")};
247 [ + + + + ]: 199117 : if (valParams.isArray() || valParams.isObject())
248 : 199002 : params = valParams;
249 [ + - ]: 115 : else if (valParams.isNull())
250 : 115 : params = UniValue(UniValue::VARR);
251 : : else
252 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object");
253 : 199117 : }
|