diff --git a/src/rpc/protocol.cpp b/src/rpc/protocol.cpp index fe0ed8127..dcd116ba6 100644 --- a/src/rpc/protocol.cpp +++ b/src/rpc/protocol.cpp @@ -1,148 +1,160 @@ // Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include /** * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility, but * uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were * unspecified (HTTP errors and contents of 'error'). * * 1.0 spec: http://json-rpc.org/wiki/specification * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html */ UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue ¶ms, const UniValue &id) { UniValue request(UniValue::VOBJ); request.pushKV("method", strMethod); request.pushKV("params", params); request.pushKV("id", id); return request; } UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id) { UniValue reply(UniValue::VOBJ); if (!error.isNull()) { reply.pushKV("result", NullUniValue); } else { reply.pushKV("result", result); } reply.pushKV("error", error); reply.pushKV("id", id); return reply; } std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id) { UniValue reply = JSONRPCReplyObj(result, error, id); return reply.write() + "\n"; } UniValue JSONRPCError(int code, const std::string &message) { UniValue error(UniValue::VOBJ); error.pushKV("code", code); error.pushKV("message", message); return error; } /** Username used when cookie authentication is in use (arbitrary, only for * recognizability in debugging/logging purposes) */ static const std::string COOKIEAUTH_USER = "__cookie__"; /** Default name for auth cookie file */ static const std::string COOKIEAUTH_FILE = ".cookie"; -fs::path GetAuthCookieFile() { - fs::path path(gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE)); +/** Get name of RPC authentication cookie file */ +static fs::path GetAuthCookieFile(bool temp = false) { + std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE); + if (temp) { + arg += ".tmp"; + } + fs::path path(arg); if (!path.is_complete()) { path = GetDataDir() / path; } return path; } bool GenerateAuthCookie(std::string *cookie_out) { const size_t COOKIE_SIZE = 32; uint8_t rand_pwd[COOKIE_SIZE]; GetRandBytes(rand_pwd, COOKIE_SIZE); std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd + COOKIE_SIZE); /** the umask determines what permissions are used to create this file - * these are set to 077 in init.cpp unless overridden with -sysperms. */ std::ofstream file; - fs::path filepath = GetAuthCookieFile(); - file.open(filepath.string().c_str()); + fs::path filepath_tmp = GetAuthCookieFile(true); + file.open(filepath_tmp.string().c_str()); if (!file.is_open()) { LogPrintf("Unable to open cookie authentication file %s for writing\n", - filepath.string()); + filepath_tmp.string()); return false; } file << cookie; file.close(); + + fs::path filepath = GetAuthCookieFile(false); + if (!RenameOver(filepath_tmp, filepath)) { + LogPrintf("Unable to rename cookie authentication file %s to %s\n", + filepath_tmp.string(), filepath.string()); + return false; + } LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); if (cookie_out) { *cookie_out = cookie; } return true; } bool GetAuthCookie(std::string *cookie_out) { std::ifstream file; std::string cookie; fs::path filepath = GetAuthCookieFile(); file.open(filepath.string().c_str()); if (!file.is_open()) { return false; } std::getline(file, cookie); file.close(); if (cookie_out) { *cookie_out = cookie; } return true; } void DeleteAuthCookie() { try { fs::remove(GetAuthCookieFile()); } catch (const fs::filesystem_error &e) { LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what()); } } std::vector JSONRPCProcessBatchReply(const UniValue &in, size_t num) { if (!in.isArray()) { throw std::runtime_error("Batch must be an array"); } std::vector batch(num); for (size_t i = 0; i < in.size(); ++i) { const UniValue &rec = in[i]; if (!rec.isObject()) { throw std::runtime_error("Batch member must be object"); } size_t id = rec["id"].get_int(); if (id >= num) { throw std::runtime_error("Batch member id larger than size"); } batch[id] = rec; } return batch; } diff --git a/src/rpc/protocol.h b/src/rpc/protocol.h index a028ed697..22df14de0 100644 --- a/src/rpc/protocol.h +++ b/src/rpc/protocol.h @@ -1,144 +1,142 @@ // Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_RPC_PROTOCOL_H #define BITCOIN_RPC_PROTOCOL_H #include #include #include #include #include #include //! HTTP status codes enum HTTPStatusCode { HTTP_OK = 200, HTTP_BAD_REQUEST = 400, HTTP_UNAUTHORIZED = 401, HTTP_FORBIDDEN = 403, HTTP_NOT_FOUND = 404, HTTP_BAD_METHOD = 405, HTTP_INTERNAL_SERVER_ERROR = 500, HTTP_SERVICE_UNAVAILABLE = 503, }; //! Bitcoin RPC error codes enum RPCErrorCode { //! Standard JSON-RPC 2.0 errors // RPC_INVALID_REQUEST is internally mapped to HTTP_BAD_REQUEST (400). // It should not be used for application-layer errors. RPC_INVALID_REQUEST = -32600, // RPC_METHOD_NOT_FOUND is internally mapped to HTTP_NOT_FOUND (404). // It should not be used for application-layer errors. RPC_METHOD_NOT_FOUND = -32601, RPC_INVALID_PARAMS = -32602, // RPC_INTERNAL_ERROR should only be used for genuine errors in bitcoind // (for example datadir corruption). RPC_INTERNAL_ERROR = -32603, RPC_PARSE_ERROR = -32700, //! General application defined errors //!< std::exception thrown in command handling RPC_MISC_ERROR = -1, //!< Unexpected type was passed as parameter RPC_TYPE_ERROR = -3, //!< Invalid address or key RPC_INVALID_ADDRESS_OR_KEY = -5, //!< Ran out of memory during operation RPC_OUT_OF_MEMORY = -7, //!< Invalid, missing or duplicate parameter RPC_INVALID_PARAMETER = -8, //!< Database error RPC_DATABASE_ERROR = -20, //!< Error parsing or validating structure in raw format RPC_DESERIALIZATION_ERROR = -22, //!< General error during transaction or block submission RPC_VERIFY_ERROR = -25, //!< Transaction or block was rejected by network rules RPC_VERIFY_REJECTED = -26, //!< Transaction already in chain RPC_VERIFY_ALREADY_IN_CHAIN = -27, //!< Client still warming up RPC_IN_WARMUP = -28, //!< RPC method is deprecated RPC_METHOD_DEPRECATED = -32, //! Aliases for backward compatibility RPC_TRANSACTION_ERROR = RPC_VERIFY_ERROR, RPC_TRANSACTION_REJECTED = RPC_VERIFY_REJECTED, RPC_TRANSACTION_ALREADY_IN_CHAIN = RPC_VERIFY_ALREADY_IN_CHAIN, //! P2P client errors //!< Bitcoin is not connected RPC_CLIENT_NOT_CONNECTED = -9, //!< Still downloading initial blocks RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, //!< Node is already added RPC_CLIENT_NODE_ALREADY_ADDED = -23, //!< Node has not been added before RPC_CLIENT_NODE_NOT_ADDED = -24, //!< Node to disconnect not found in connected nodes RPC_CLIENT_NODE_NOT_CONNECTED = -29, //!< Invalid IP/Subnet RPC_CLIENT_INVALID_IP_OR_SUBNET = -30, //!< No valid connection manager instance found RPC_CLIENT_P2P_DISABLED = -31, //! Wallet errors //!< Unspecified problem with wallet (key not found etc.) RPC_WALLET_ERROR = -4, //!< Not enough funds in wallet or account RPC_WALLET_INSUFFICIENT_FUNDS = -6, //!< Invalid label name RPC_WALLET_INVALID_LABEL_NAME = -11, //!< Keypool ran out, call keypoolrefill first RPC_WALLET_KEYPOOL_RAN_OUT = -12, //!< Enter the wallet passphrase with walletpassphrase first RPC_WALLET_UNLOCK_NEEDED = -13, //!< The wallet passphrase entered was incorrect RPC_WALLET_PASSPHRASE_INCORRECT = -14, //!< Command given in wrong wallet encryption state (encrypting an encrypted //! wallet etc.) RPC_WALLET_WRONG_ENC_STATE = -15, //!< Failed to encrypt the wallet RPC_WALLET_ENCRYPTION_FAILED = -16, //!< Wallet is already unlocked RPC_WALLET_ALREADY_UNLOCKED = -17, //!< Invalid wallet specified RPC_WALLET_NOT_FOUND = -18, //!< No wallet specified (error when there are multiple wallets loaded) RPC_WALLET_NOT_SPECIFIED = -19, //!< Backwards compatible aliases RPC_WALLET_INVALID_ACCOUNT_NAME = RPC_WALLET_INVALID_LABEL_NAME, //! Unused reserved codes, kept around for backwards compatibility. Do not //! reuse. //!< Server is in safe mode, and command is not allowed in safe mode RPC_FORBIDDEN_BY_SAFE_MODE = -2, }; UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue ¶ms, const UniValue &id); UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id); std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id); UniValue JSONRPCError(int code, const std::string &message); -/** Get name of RPC authentication cookie file */ -fs::path GetAuthCookieFile(); /** Generate a new RPC authentication cookie and write it to disk */ bool GenerateAuthCookie(std::string *cookie_out); /** Read the RPC authentication cookie from disk */ bool GetAuthCookie(std::string *cookie_out); /** Delete RPC authentication cookie from disk */ void DeleteAuthCookie(); /** Parse JSON-RPC batch reply into a vector */ std::vector JSONRPCProcessBatchReply(const UniValue &in, size_t num); #endif // BITCOIN_RPC_PROTOCOL_H