Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/protocol.cpp
Show First 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | |||||
/** Username used when cookie authentication is in use (arbitrary, only for | /** Username used when cookie authentication is in use (arbitrary, only for | ||||
* recognizability in debugging/logging purposes) | * recognizability in debugging/logging purposes) | ||||
*/ | */ | ||||
static const std::string COOKIEAUTH_USER = "__cookie__"; | static const std::string COOKIEAUTH_USER = "__cookie__"; | ||||
/** Default name for auth cookie file */ | /** Default name for auth cookie file */ | ||||
static const std::string COOKIEAUTH_FILE = ".cookie"; | static const std::string COOKIEAUTH_FILE = ".cookie"; | ||||
boost::filesystem::path GetAuthCookieFile() { | fs::path GetAuthCookieFile() { | ||||
boost::filesystem::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE)); | fs::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE)); | ||||
if (!path.is_complete()) path = GetDataDir() / path; | if (!path.is_complete()) path = GetDataDir() / path; | ||||
return path; | return path; | ||||
} | } | ||||
bool GenerateAuthCookie(std::string *cookie_out) { | bool GenerateAuthCookie(std::string *cookie_out) { | ||||
const size_t COOKIE_SIZE = 32; | const size_t COOKIE_SIZE = 32; | ||||
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; | std::ofstream file; | ||||
boost::filesystem::path filepath = GetAuthCookieFile(); | fs::path filepath = GetAuthCookieFile(); | ||||
file.open(filepath.string().c_str()); | file.open(filepath.string().c_str()); | ||||
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.string()); | filepath.string()); | ||||
return false; | return false; | ||||
} | } | ||||
file << cookie; | file << cookie; | ||||
file.close(); | file.close(); | ||||
LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); | LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); | ||||
if (cookie_out) *cookie_out = cookie; | if (cookie_out) *cookie_out = cookie; | ||||
return true; | return true; | ||||
} | } | ||||
bool GetAuthCookie(std::string *cookie_out) { | bool GetAuthCookie(std::string *cookie_out) { | ||||
std::ifstream file; | std::ifstream file; | ||||
std::string cookie; | std::string cookie; | ||||
boost::filesystem::path filepath = GetAuthCookieFile(); | fs::path filepath = GetAuthCookieFile(); | ||||
file.open(filepath.string().c_str()); | file.open(filepath.string().c_str()); | ||||
if (!file.is_open()) return false; | if (!file.is_open()) return false; | ||||
std::getline(file, cookie); | std::getline(file, cookie); | ||||
file.close(); | file.close(); | ||||
if (cookie_out) *cookie_out = cookie; | if (cookie_out) *cookie_out = cookie; | ||||
return true; | return true; | ||||
} | } | ||||
void DeleteAuthCookie() { | void DeleteAuthCookie() { | ||||
try { | try { | ||||
boost::filesystem::remove(GetAuthCookieFile()); | fs::remove(GetAuthCookieFile()); | ||||
} catch (const boost::filesystem::filesystem_error &e) { | } catch (const fs::filesystem_error &e) { | ||||
LogPrintf("%s: Unable to remove random auth cookie file: %s\n", | LogPrintf("%s: Unable to remove random auth cookie file: %s\n", | ||||
__func__, e.what()); | __func__, e.what()); | ||||
} | } | ||||
} | } |