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