Skip to content

Commit 7147ed9

Browse files
nyxlj3954
andauthored
implement a root check menu (#927)
* implement a root check menu * code needs to be readable * rephrase it a lil bit * disregard escalation tool variable if found as root * refactor: Call root check from within AppState constructor (#7) * remove duplicate check * add comment back --------- Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com>
1 parent fab3415 commit 7147ed9

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

Cargo.lock

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tabs/common-script.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ checkAURHelper() {
8686
checkEscalationTool() {
8787
## Check for escalation tools.
8888
if [ -z "$ESCALATION_TOOL_CHECKED" ]; then
89+
if [ "$(id -u)" = "0" ]; then
90+
ESCALATION_TOOL="eval"
91+
ESCALATION_TOOL_CHECKED=true
92+
printf "%b\n" "${CYAN}Running as root, no escalation needed${RC}"
93+
return 0
94+
fi
95+
8996
ESCALATION_TOOLS='sudo doas'
9097
for tool in ${ESCALATION_TOOLS}; do
9198
if command_exists "${tool}"; then

tui/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ textwrap = { version = "0.16.1", default-features = false }
3030
anstyle = { version = "1.0.8", default-features = false }
3131
ansi-to-tui = { version = "7.0.0", default-features = false }
3232
zips = "0.1.7"
33+
nix = { version = "0.29.0", features = [ "user" ] }
3334

3435
[[bin]]
3536
name = "linutil"

tui/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod filter;
33
mod float;
44
mod floating_text;
55
mod hint;
6+
mod root;
67
mod running_command;
78
pub mod state;
89
mod theme;

tui/src/root.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::floating_text::FloatingText;
2+
3+
#[cfg(unix)]
4+
use nix::unistd::Uid;
5+
6+
const ROOT_WARNING: &str = "WARNING: You are running this utility as root!\n
7+
This means you have full system access and commands can potentially damage your system if used incorrectly.\n
8+
Please proceed with caution and make sure you understand what each script does before executing it.";
9+
10+
#[cfg(unix)]
11+
pub fn check_root_status() -> Option<FloatingText> {
12+
(Uid::effective().is_root()).then_some(FloatingText::new(
13+
ROOT_WARNING.into(),
14+
"Root User Warning",
15+
true,
16+
))
17+
}

tui/src/state.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
float::{Float, FloatContent},
55
floating_text::FloatingText,
66
hint::{create_shortcut_list, Shortcut},
7+
root::check_root_status,
78
running_command::RunningCommand,
89
theme::Theme,
910
};
@@ -124,6 +125,11 @@ impl AppState {
124125
skip_confirmation,
125126
};
126127

128+
#[cfg(unix)]
129+
if let Some(root_warning) = check_root_status() {
130+
state.spawn_float(root_warning, 60, 40);
131+
}
132+
127133
state.update_items();
128134
if let Some(auto_execute_commands) = auto_execute_commands {
129135
state.handle_initial_auto_execute(&auto_execute_commands);

0 commit comments

Comments
 (0)