-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
845 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
enum class AmIRoot { | ||
YES, | ||
NO, | ||
MAYBE, | ||
}; | ||
|
||
// Checks if effective UID or effective GID is equal to root UID / GID. Works in linux namespaces. | ||
AmIRoot am_i_root(); |
8 changes: 8 additions & 0 deletions
8
subprojects/simlib/include/simlib/escape_bytes_to_utf8_str.hh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
std::string escape_bytes_to_utf8_str( | ||
const std::string_view& prefix, const std::string_view& bytes, const std::string_view& suffix | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <simlib/am_i_root.hh> | ||
#include <simlib/errmsg.hh> | ||
#include <simlib/macros/throw.hh> | ||
#include <sys/stat.h> | ||
#include <sys/sysmacros.h> | ||
#include <unistd.h> | ||
|
||
AmIRoot am_i_root() { | ||
struct stat st; | ||
if (stat("/dev/null", &st)) { | ||
THROW("stat()", errmsg()); | ||
} | ||
bool dev_null_is_dev_null = S_ISCHR(st.st_mode) && st.st_rdev == makedev(1, 3); | ||
auto root_real_uid = st.st_uid; | ||
auto root_real_gid = st.st_gid; | ||
|
||
auto euid = geteuid(); | ||
auto egid = getegid(); | ||
|
||
if (euid == 0 || egid == 0) { | ||
return AmIRoot::YES; | ||
} | ||
if (!dev_null_is_dev_null) { | ||
return AmIRoot::MAYBE; | ||
} | ||
if (euid == root_real_uid || egid == root_real_gid) { | ||
return AmIRoot::YES; | ||
} | ||
return AmIRoot::NO; | ||
} |
Oops, something went wrong.