Skip to content

Commit ec34bd8

Browse files
committed
Merge rpccookieperms_log_improvements-28+k
2 parents 57d68d7 + 20aab08 commit ec34bd8

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

src/httprpc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <optional>
2626
#include <set>
2727
#include <string>
28+
#include <utility>
2829
#include <vector>
2930

3031
using util::SplitString;
@@ -308,7 +309,7 @@ static bool InitRPCAuthentication()
308309
}
309310

310311
assert(strRPCUserColonPass.empty()); // Only support initializing once
311-
if (!GenerateAuthCookie(&strRPCUserColonPass, cookie_perms)) {
312+
if (!GenerateAuthCookie(&strRPCUserColonPass, std::make_pair(cookie_perms, bool(cookie_perms_arg)))) {
312313
return false;
313314
}
314315
if (strRPCUserColonPass.empty()) {

src/rpc/request.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fstream>
1717
#include <stdexcept>
1818
#include <string>
19+
#include <utility>
1920
#include <vector>
2021

2122
/**
@@ -97,7 +98,7 @@ static fs::path GetAuthCookieFile(bool temp=false)
9798

9899
static std::optional<std::string> g_generated_cookie;
99100

100-
bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie_perms)
101+
bool GenerateAuthCookie(std::string* cookie_out, const std::pair<std::optional<fs::perms>, bool>& cookie_perms)
101102
{
102103
const size_t COOKIE_SIZE = 32;
103104
unsigned char rand_pwd[COOKIE_SIZE];
@@ -118,9 +119,9 @@ bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie
118119
return false;
119120
}
120121

121-
if (cookie_perms) {
122+
if (cookie_perms.first) {
122123
std::error_code code;
123-
fs::permissions(filepath_tmp, cookie_perms.value(), fs::perm_options::replace, code);
124+
fs::permissions(filepath_tmp, cookie_perms.first.value(), fs::perm_options::replace, code);
124125
if (code) {
125126
LogWarning("Unable to set permissions on cookie authentication file %s", fs::PathToString(filepath_tmp));
126127
return false;
@@ -138,7 +139,9 @@ bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie
138139

139140
g_generated_cookie = cookie;
140141
LogInfo("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
141-
LogInfo("Permissions used for cookie: %s\n", PermsToSymbolicString(fs::status(filepath).permissions()));
142+
LogInfo("Permissions used for cookie%s: %s\n",
143+
(cookie_perms.first && cookie_perms.second) ? " (set by -rpccookieperms)" : "",
144+
PermsToSymbolicString(fs::status(filepath).permissions()));
142145

143146
if (cookie_out)
144147
*cookie_out = cookie;

src/rpc/request.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <any>
1010
#include <optional>
1111
#include <string>
12+
#include <utility>
1213

1314
#include <univalue.h>
1415
#include <util/fs.h>
@@ -24,7 +25,7 @@ UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue
2425
UniValue JSONRPCError(int code, const std::string& message);
2526

2627
/** Generate a new RPC authentication cookie and write it to disk */
27-
bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie_perms=std::nullopt);
28+
bool GenerateAuthCookie(std::string* cookie_out, const std::pair<std::optional<fs::perms>, bool>& cookie_perms);
2829
/** Read the RPC authentication cookie from disk */
2930
bool GetAuthCookie(std::string *cookie_out);
3031
/** Delete RPC authentication cookie from disk */

src/util/fs_helpers.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,37 @@ std::string PermsToSymbolicString(fs::perms p)
325325
{
326326
std::string perm_str(9, '-');
327327

328-
auto set_perm = [&](size_t pos, fs::perms required_perm, char letter) {
328+
auto set_perm = [&](size_t pos, fs::perms required_perm, char letter, char else_letter = '\0') {
329329
if ((p & required_perm) != fs::perms::none) {
330330
perm_str[pos] = letter;
331+
} else if (else_letter) {
332+
perm_str[pos] = else_letter;
331333
}
332334
};
333335

334336
set_perm(0, fs::perms::owner_read, 'r');
335337
set_perm(1, fs::perms::owner_write, 'w');
336-
set_perm(2, fs::perms::owner_exec, 'x');
338+
if ((p & fs::perms::owner_exec) != fs::perms::none) {
339+
set_perm(2, fs::perms::set_uid, 's', 'x');
340+
} else {
341+
set_perm(2, fs::perms::set_uid, 'S');
342+
}
343+
337344
set_perm(3, fs::perms::group_read, 'r');
338345
set_perm(4, fs::perms::group_write, 'w');
339-
set_perm(5, fs::perms::group_exec, 'x');
346+
if ((p & fs::perms::group_exec) != fs::perms::none) {
347+
set_perm(5, fs::perms::set_gid, 's', 'x');
348+
} else {
349+
set_perm(5, fs::perms::set_gid, 'S');
350+
}
351+
340352
set_perm(6, fs::perms::others_read, 'r');
341353
set_perm(7, fs::perms::others_write, 'w');
342-
set_perm(8, fs::perms::others_exec, 'x');
354+
if ((p & fs::perms::others_exec) != fs::perms::none) {
355+
set_perm(8, fs::perms::sticky_bit, 't', 'x');
356+
} else {
357+
set_perm(8, fs::perms::sticky_bit, 'T');
358+
}
343359

344360
return perm_str;
345361
}

0 commit comments

Comments
 (0)