Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/protocol.cpp
Show First 20 Lines • Show All 60 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"; | ||||
fs::path GetAuthCookieFile() { | /** Get name of RPC authentication cookie file */ | ||||
fs::path path(gArgs.GetArg("-rpccookiefile", COOKIEAUTH_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()) { | if (!path.is_complete()) { | ||||
path = GetDataDir() / path; | 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; | ||||
fs::path filepath = GetAuthCookieFile(); | fs::path filepath_tmp = GetAuthCookieFile(true); | ||||
file.open(filepath.string().c_str()); | file.open(filepath_tmp.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_tmp.string()); | ||||
return false; | return false; | ||||
} | } | ||||
file << cookie; | file << cookie; | ||||
file.close(); | 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()); | 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; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |