Skip to content

Commit 39cbd4f

Browse files
committed
args: Support -norpccookiefile for bitcoind and bitcoin-cli
Replaces belt & suspenders check for initialization in RPCAuthorized() with not allowing empty passwords further down.
1 parent e82ad88 commit 39cbd4f

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/httprpc.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ static bool multiUserAuthorized(std::string strUserPass)
134134

135135
static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUsernameOut)
136136
{
137-
if (strRPCUserColonPass.empty()) // Belt-and-suspenders measure if InitRPCAuthentication was not called
138-
return false;
139137
if (strAuth.substr(0, 6) != "Basic ")
140138
return false;
141139
std::string_view strUserPass64 = TrimStringView(std::string_view{strAuth}.substr(6));
@@ -147,8 +145,9 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
147145
if (strUserPass.find(':') != std::string::npos)
148146
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(':'));
149147

150-
//Check if authorized under single-user field
151-
if (TimingResistantEqual(strUserPass, strRPCUserColonPass)) {
148+
// Check if authorized under single-user field.
149+
// (strRPCUserColonPass is empty when -norpccookiefile is specified).
150+
if (!strRPCUserColonPass.empty() && TimingResistantEqual(strUserPass, strRPCUserColonPass)) {
152151
return true;
153152
}
154153
return multiUserAuthorized(strUserPass);
@@ -294,8 +293,6 @@ static bool InitRPCAuthentication()
294293
{
295294
if (gArgs.GetArg("-rpcpassword", "") == "")
296295
{
297-
LogInfo("Using random cookie authentication.\n");
298-
299296
std::optional<fs::perms> cookie_perms{std::nullopt};
300297
auto cookie_perms_arg{gArgs.GetArg("-rpccookieperms")};
301298
if (cookie_perms_arg) {
@@ -307,9 +304,15 @@ static bool InitRPCAuthentication()
307304
cookie_perms = *perm_opt;
308305
}
309306

307+
assert(strRPCUserColonPass.empty()); // Only support initializing once
310308
if (!GenerateAuthCookie(&strRPCUserColonPass, cookie_perms)) {
311309
return false;
312310
}
311+
if (strRPCUserColonPass.empty()) {
312+
LogInfo("RPC authentication cookie file generation is disabled.");
313+
} else {
314+
LogInfo("Using random cookie authentication.");
315+
}
313316
} else {
314317
LogPrintf("Config options rpcuser and rpcpassword will soon be deprecated. Locally-run instances may remove rpcuser to use cookie-based auth, or may be replaced with rpcauth. Please see share/rpcauth for rpcauth auth generation.\n");
315318
strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + gArgs.GetArg("-rpcpassword", "");

src/rpc/request.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ static const char* const COOKIEAUTH_FILE = ".cookie";
8686
static fs::path GetAuthCookieFile(bool temp=false)
8787
{
8888
fs::path arg = gArgs.GetPathArg("-rpccookiefile", COOKIEAUTH_FILE);
89+
if (arg.empty()) {
90+
return {}; // -norpccookiefile was specified
91+
}
8992
if (temp) {
9093
arg += ".tmp";
9194
}
@@ -106,6 +109,9 @@ bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie
106109
*/
107110
std::ofstream file;
108111
fs::path filepath_tmp = GetAuthCookieFile(true);
112+
if (filepath_tmp.empty()) {
113+
return true; // -norpccookiefile
114+
}
109115
file.open(filepath_tmp);
110116
if (!file.is_open()) {
111117
LogWarning("Unable to open cookie authentication file %s for writing", fs::PathToString(filepath_tmp));
@@ -142,6 +148,9 @@ bool GetAuthCookie(std::string *cookie_out)
142148
std::ifstream file;
143149
std::string cookie;
144150
fs::path filepath = GetAuthCookieFile();
151+
if (filepath.empty()) {
152+
return true; // -norpccookiefile
153+
}
145154
file.open(filepath);
146155
if (!file.is_open())
147156
return false;

0 commit comments

Comments
 (0)