Skip to content

Commit a888155

Browse files
aureleoulesluke-jr
authored andcommitted
httprpc: allow specifying rpccookie permissions in octal
Co-authored-by: Will Clark <[email protected]> Github-Pull: bitcoin#28167 Rebased-From: 7456c3a
1 parent 07f39b7 commit a888155

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/util/fs_helpers.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,23 @@ std::string PermsToSymbolicString(fs::perms p)
294294
return perm_str;
295295
}
296296

297+
static std::optional<unsigned> StringToOctal(const std::string& str)
298+
{
299+
unsigned ret = 0;
300+
for (char c : str) {
301+
if (c < '0' || c > '7') return std::nullopt;
302+
ret = (ret << 3) | (c - '0');
303+
}
304+
return ret;
305+
}
306+
307+
static auto ConvertPermsToOctal(const std::string& str) noexcept -> std::optional<unsigned>
308+
{
309+
// Don't permit setting special bits as they're not relevant to cookie files
310+
if (str.length() == 3) return StringToOctal(str);
311+
return std::nullopt;
312+
}
313+
297314
std::optional<fs::perms> InterpretPermString(const std::string& s)
298315
{
299316
if (s == "owner") {
@@ -305,6 +322,8 @@ std::optional<fs::perms> InterpretPermString(const std::string& s)
305322
return fs::perms::owner_read | fs::perms::owner_write |
306323
fs::perms::group_read |
307324
fs::perms::others_read;
325+
} else if (auto octal_perms = ConvertPermsToOctal(s)) {
326+
return static_cast<fs::perms>(*octal_perms);
308327
} else {
309328
return std::nullopt;
310329
}

0 commit comments

Comments
 (0)