Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/protocol.cpp
// Copyright (c) 2010 Satoshi Nakamoto | // Copyright (c) 2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <rpc/protocol.h> | #include <rpc/protocol.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <tinyformat.h> | #include <tinyformat.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <version.h> | #include <version.h> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <fstream> | |||||
/** | /** | ||||
* JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility, but | * 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 | * uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were | ||||
* unspecified (HTTP errors and contents of 'error'). | * unspecified (HTTP errors and contents of 'error'). | ||||
* | * | ||||
* 1.0 spec: http://json-rpc.org/wiki/specification | * 1.0 spec: http://json-rpc.org/wiki/specification | ||||
* 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html | * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | bool GenerateAuthCookie(std::string *cookie_out) { | ||||
uint8_t rand_pwd[COOKIE_SIZE]; | uint8_t rand_pwd[COOKIE_SIZE]; | ||||
GetRandBytes(rand_pwd, COOKIE_SIZE); | GetRandBytes(rand_pwd, COOKIE_SIZE); | ||||
std::string cookie = | std::string cookie = | ||||
COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd + COOKIE_SIZE); | COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd + COOKIE_SIZE); | ||||
/** the umask determines what permissions are used to create this file - | /** the umask determines what permissions are used to create this file - | ||||
* these are set to 077 in init.cpp unless overridden with -sysperms. | * these are set to 077 in init.cpp unless overridden with -sysperms. | ||||
*/ | */ | ||||
std::ofstream file; | fsbridge::ofstream file; | ||||
fs::path filepath_tmp = GetAuthCookieFile(true); | fs::path filepath_tmp = GetAuthCookieFile(true); | ||||
file.open(filepath_tmp.string().c_str()); | file.open(filepath_tmp); | ||||
if (!file.is_open()) { | if (!file.is_open()) { | ||||
LogPrintf("Unable to open cookie authentication file %s for writing\n", | LogPrintf("Unable to open cookie authentication file %s for writing\n", | ||||
filepath_tmp.string()); | filepath_tmp.string()); | ||||
return false; | return false; | ||||
} | } | ||||
file << cookie; | file << cookie; | ||||
file.close(); | file.close(); | ||||
fs::path filepath = GetAuthCookieFile(false); | fs::path filepath = GetAuthCookieFile(false); | ||||
if (!RenameOver(filepath_tmp, filepath)) { | if (!RenameOver(filepath_tmp, filepath)) { | ||||
LogPrintf("Unable to rename cookie authentication file %s to %s\n", | LogPrintf("Unable to rename cookie authentication file %s to %s\n", | ||||
filepath_tmp.string(), filepath.string()); | filepath_tmp.string(), filepath.string()); | ||||
return false; | return false; | ||||
} | } | ||||
LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); | LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); | ||||
if (cookie_out) { | if (cookie_out) { | ||||
*cookie_out = cookie; | *cookie_out = cookie; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool GetAuthCookie(std::string *cookie_out) { | bool GetAuthCookie(std::string *cookie_out) { | ||||
std::ifstream file; | fsbridge::ifstream file; | ||||
std::string cookie; | std::string cookie; | ||||
fs::path filepath = GetAuthCookieFile(); | fs::path filepath = GetAuthCookieFile(); | ||||
file.open(filepath.string().c_str()); | file.open(filepath); | ||||
if (!file.is_open()) { | if (!file.is_open()) { | ||||
return false; | return false; | ||||
} | } | ||||
std::getline(file, cookie); | std::getline(file, cookie); | ||||
file.close(); | file.close(); | ||||
if (cookie_out) { | if (cookie_out) { | ||||
*cookie_out = cookie; | *cookie_out = cookie; | ||||
Show All 31 Lines |