Skip to content

Commit

Permalink
implement a root check menu (#927)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
nnyyxxxx and lj3954 authored Nov 11, 2024
1 parent fab3415 commit 7147ed9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions core/tabs/common-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ checkAURHelper() {
checkEscalationTool() {
## Check for escalation tools.
if [ -z "$ESCALATION_TOOL_CHECKED" ]; then
if [ "$(id -u)" = "0" ]; then
ESCALATION_TOOL="eval"
ESCALATION_TOOL_CHECKED=true
printf "%b\n" "${CYAN}Running as root, no escalation needed${RC}"
return 0
fi

ESCALATION_TOOLS='sudo doas'
for tool in ${ESCALATION_TOOLS}; do
if command_exists "${tool}"; then
Expand Down
1 change: 1 addition & 0 deletions tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ textwrap = { version = "0.16.1", default-features = false }
anstyle = { version = "1.0.8", default-features = false }
ansi-to-tui = { version = "7.0.0", default-features = false }
zips = "0.1.7"
nix = { version = "0.29.0", features = [ "user" ] }

[[bin]]
name = "linutil"
Expand Down
1 change: 1 addition & 0 deletions tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod filter;
mod float;
mod floating_text;
mod hint;
mod root;
mod running_command;
pub mod state;
mod theme;
Expand Down
17 changes: 17 additions & 0 deletions tui/src/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::floating_text::FloatingText;

#[cfg(unix)]
use nix::unistd::Uid;

const ROOT_WARNING: &str = "WARNING: You are running this utility as root!\n
This means you have full system access and commands can potentially damage your system if used incorrectly.\n
Please proceed with caution and make sure you understand what each script does before executing it.";

#[cfg(unix)]
pub fn check_root_status() -> Option<FloatingText> {
(Uid::effective().is_root()).then_some(FloatingText::new(
ROOT_WARNING.into(),
"Root User Warning",
true,
))
}
6 changes: 6 additions & 0 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
float::{Float, FloatContent},
floating_text::FloatingText,
hint::{create_shortcut_list, Shortcut},
root::check_root_status,
running_command::RunningCommand,
theme::Theme,
};
Expand Down Expand Up @@ -124,6 +125,11 @@ impl AppState {
skip_confirmation,
};

#[cfg(unix)]
if let Some(root_warning) = check_root_status() {
state.spawn_float(root_warning, 60, 40);
}

state.update_items();
if let Some(auto_execute_commands) = auto_execute_commands {
state.handle_initial_auto_execute(&auto_execute_commands);
Expand Down

0 comments on commit 7147ed9

Please sign in to comment.