rpc: Avoid join-split roundtrip for user:pass for auth credentials
This commit is contained in:
1 parent
98ff38a6f1
commit
e49a7274a2
3 files changed
+42
-27
No files matched your search
+11
-15
@@ -278,7 +278,8 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
|
|||||||
|
|
||||||
static bool InitRPCAuthentication()
|
static bool InitRPCAuthentication()
|
||||||
{
|
{
|
||||||
std::string user_colon_pass;
|
std::string user;
|
||||||
|
std::string pass;
|
||||||
|
|
||||||
if (gArgs.GetArg("-rpcpassword", "") == "")
|
if (gArgs.GetArg("-rpcpassword", "") == "")
|
||||||
{
|
{
|
||||||
@@ -293,30 +294,25 @@ static bool InitRPCAuthentication()
|
|||||||
cookie_perms = *perm_opt;
|
cookie_perms = *perm_opt;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!GenerateAuthCookie(&user_colon_pass, cookie_perms)) {
|
switch (GenerateAuthCookie(cookie_perms, user, pass)) {
|
||||||
|
case GenerateAuthCookieResult::ERR:
|
||||||
return false;
|
return false;
|
||||||
}
|
case GenerateAuthCookieResult::DISABLED:
|
||||||
if (user_colon_pass.empty()) {
|
|
||||||
LogInfo("RPC authentication cookie file generation is disabled.");
|
LogInfo("RPC authentication cookie file generation is disabled.");
|
||||||
} else {
|
break;
|
||||||
|
case GenerateAuthCookieResult::OK:
|
||||||
LogInfo("Using random cookie authentication.");
|
LogInfo("Using random cookie authentication.");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LogInfo("Using rpcuser/rpcpassword authentication.");
|
LogInfo("Using rpcuser/rpcpassword authentication.");
|
||||||
LogWarning("The use of rpcuser/rpcpassword is less secure, because credentials are configured in plain text. It is recommended that locally-run instances switch to cookie-based auth, or otherwise to use hashed rpcauth credentials. See share/rpcauth in the source directory for more information.");
|
LogWarning("The use of rpcuser/rpcpassword is less secure, because credentials are configured in plain text. It is recommended that locally-run instances switch to cookie-based auth, or otherwise to use hashed rpcauth credentials. See share/rpcauth in the source directory for more information.");
|
||||||
user_colon_pass = gArgs.GetArg("-rpcuser", "") + ":" + gArgs.GetArg("-rpcpassword", "");
|
user = gArgs.GetArg("-rpcuser", "");
|
||||||
|
pass = gArgs.GetArg("-rpcpassword", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is a plaintext credential, hash it with a random salt before storage.
|
// If there is a plaintext credential, hash it with a random salt before storage.
|
||||||
if (!user_colon_pass.empty()) {
|
if (!user.empty() || !pass.empty()) {
|
||||||
std::vector<std::string> fields{SplitString(user_colon_pass, ':')};
|
|
||||||
if (fields.size() != 2) {
|
|
||||||
LogError("Unable to parse RPC credentials. The configured rpcuser or rpcpassword cannot contain a \":\".");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const std::string& user = fields[0];
|
|
||||||
const std::string& pass = fields[1];
|
|
||||||
|
|
||||||
// Generate a random 16 byte hex salt.
|
// Generate a random 16 byte hex salt.
|
||||||
std::array<unsigned char, 16> raw_salt;
|
std::array<unsigned char, 16> raw_salt;
|
||||||
GetStrongRandBytes(raw_salt);
|
GetStrongRandBytes(raw_salt);
|
||||||
|
|||||||
+12
-10
@@ -97,12 +97,14 @@ static fs::path GetAuthCookieFile(bool temp=false)
|
|||||||
|
|
||||||
static bool g_generated_cookie = false;
|
static bool g_generated_cookie = false;
|
||||||
|
|
||||||
bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie_perms)
|
GenerateAuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cookie_perms,
|
||||||
|
std::string& user,
|
||||||
|
std::string& pass)
|
||||||
{
|
{
|
||||||
const size_t COOKIE_SIZE = 32;
|
const size_t COOKIE_SIZE = 32;
|
||||||
unsigned char rand_pwd[COOKIE_SIZE];
|
unsigned char rand_pwd[COOKIE_SIZE];
|
||||||
GetRandBytes(rand_pwd);
|
GetRandBytes(rand_pwd);
|
||||||
std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
|
const std::string rand_pwd_hex{HexStr(rand_pwd)};
|
||||||
|
|
||||||
/** 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 0077 in common/system.cpp.
|
* these are set to 0077 in common/system.cpp.
|
||||||
@@ -110,27 +112,27 @@ bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie
|
|||||||
std::ofstream file;
|
std::ofstream file;
|
||||||
fs::path filepath_tmp = GetAuthCookieFile(true);
|
fs::path filepath_tmp = GetAuthCookieFile(true);
|
||||||
if (filepath_tmp.empty()) {
|
if (filepath_tmp.empty()) {
|
||||||
return true; // -norpccookiefile
|
return GenerateAuthCookieResult::DISABLED; // -norpccookiefile
|
||||||
}
|
}
|
||||||
file.open(filepath_tmp);
|
file.open(filepath_tmp);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
LogWarning("Unable to open cookie authentication file %s for writing", fs::PathToString(filepath_tmp));
|
LogWarning("Unable to open cookie authentication file %s for writing", fs::PathToString(filepath_tmp));
|
||||||
return false;
|
return GenerateAuthCookieResult::ERR;
|
||||||
}
|
}
|
||||||
file << cookie;
|
file << COOKIEAUTH_USER << ":" << rand_pwd_hex;
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
fs::path filepath = GetAuthCookieFile(false);
|
fs::path filepath = GetAuthCookieFile(false);
|
||||||
if (!RenameOver(filepath_tmp, filepath)) {
|
if (!RenameOver(filepath_tmp, filepath)) {
|
||||||
LogWarning("Unable to rename cookie authentication file %s to %s", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
|
LogWarning("Unable to rename cookie authentication file %s to %s", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
|
||||||
return false;
|
return GenerateAuthCookieResult::ERR;
|
||||||
}
|
}
|
||||||
if (cookie_perms) {
|
if (cookie_perms) {
|
||||||
std::error_code code;
|
std::error_code code;
|
||||||
fs::permissions(filepath, cookie_perms.value(), fs::perm_options::replace, code);
|
fs::permissions(filepath, cookie_perms.value(), fs::perm_options::replace, code);
|
||||||
if (code) {
|
if (code) {
|
||||||
LogWarning("Unable to set permissions on cookie authentication file %s", fs::PathToString(filepath));
|
LogWarning("Unable to set permissions on cookie authentication file %s", fs::PathToString(filepath));
|
||||||
return false;
|
return GenerateAuthCookieResult::ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,9 +140,9 @@ bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie
|
|||||||
LogInfo("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
|
LogInfo("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
|
||||||
LogInfo("Permissions used for cookie: %s\n", PermsToSymbolicString(fs::status(filepath).permissions()));
|
LogInfo("Permissions used for cookie: %s\n", PermsToSymbolicString(fs::status(filepath).permissions()));
|
||||||
|
|
||||||
if (cookie_out)
|
user = COOKIEAUTH_USER;
|
||||||
*cookie_out = cookie;
|
pass = rand_pwd_hex;
|
||||||
return true;
|
return GenerateAuthCookieResult::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GetAuthCookie(std::string *cookie_out)
|
bool GetAuthCookie(std::string *cookie_out)
|
||||||
|
|||||||
+19
-2
@@ -23,8 +23,25 @@ UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params,
|
|||||||
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version);
|
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version);
|
||||||
UniValue JSONRPCError(int code, const std::string& message);
|
UniValue JSONRPCError(int code, const std::string& message);
|
||||||
|
|
||||||
/** Generate a new RPC authentication cookie and write it to disk */
|
enum class GenerateAuthCookieResult : uint8_t {
|
||||||
bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie_perms=std::nullopt);
|
DISABLED, // -norpccookiefile
|
||||||
|
ERR,
|
||||||
|
OK,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a new RPC authentication cookie and write it to disk
|
||||||
|
* @param[in] cookie_perms Filesystem permissions to use for the cookie file.
|
||||||
|
* @param[out] user Generated username, only set if `OK` is returned.
|
||||||
|
* @param[out] pass Generated password, only set if `OK` is returned.
|
||||||
|
* @retval GenerateAuthCookieResult::DISABLED Authentication via cookie is disabled.
|
||||||
|
* @retval GenerateAuthCookieResult::ERROR Error occurred, auth data could not be saved to disk.
|
||||||
|
* @retval GenerateAuthCookieResult::OK Auth data was generated, saved to disk and in `user` and `pass`.
|
||||||
|
*/
|
||||||
|
GenerateAuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cookie_perms,
|
||||||
|
std::string& user,
|
||||||
|
std::string& pass);
|
||||||
|
|
||||||
/** Read the RPC authentication cookie from disk */
|
/** Read the RPC authentication cookie from disk */
|
||||||
bool GetAuthCookie(std::string *cookie_out);
|
bool GetAuthCookie(std::string *cookie_out);
|
||||||
/** Delete RPC authentication cookie from disk */
|
/** Delete RPC authentication cookie from disk */
|
||||||
|
|||||||
Reference in new issue
Block a user