From 7147ed93e96b4ddeeb43ce55be431cea0a30c5e2 Mon Sep 17 00:00:00 2001 From: nyx Date: Mon, 11 Nov 2024 16:27:30 +0000 Subject: [PATCH] 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> --- Cargo.lock | 21 ++++++++++++++++++++- core/tabs/common-script.sh | 7 +++++++ tui/Cargo.toml | 1 + tui/src/main.rs | 1 + tui/src/root.rs | 17 +++++++++++++++++ tui/src/state.rs | 6 ++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tui/src/root.rs diff --git a/Cargo.lock b/Cargo.lock index 6108bfe3..07123221 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,6 +113,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "clap" version = "4.5.20" @@ -393,6 +399,7 @@ dependencies = [ "anstyle", "clap", "linutil_core", + "nix 0.29.0", "oneshot", "portable-pty", "rand", @@ -486,6 +493,18 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags 2.6.0", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -571,7 +590,7 @@ dependencies = [ "lazy_static", "libc", "log", - "nix", + "nix 0.25.1", "serial", "shared_library", "shell-words", diff --git a/core/tabs/common-script.sh b/core/tabs/common-script.sh index f8188ac3..c3472c08 100644 --- a/core/tabs/common-script.sh +++ b/core/tabs/common-script.sh @@ -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 diff --git a/tui/Cargo.toml b/tui/Cargo.toml index 401edc08..5094b815 100644 --- a/tui/Cargo.toml +++ b/tui/Cargo.toml @@ -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" diff --git a/tui/src/main.rs b/tui/src/main.rs index a47819ca..d391f9e6 100644 --- a/tui/src/main.rs +++ b/tui/src/main.rs @@ -3,6 +3,7 @@ mod filter; mod float; mod floating_text; mod hint; +mod root; mod running_command; pub mod state; mod theme; diff --git a/tui/src/root.rs b/tui/src/root.rs new file mode 100644 index 00000000..1b02b938 --- /dev/null +++ b/tui/src/root.rs @@ -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 { + (Uid::effective().is_root()).then_some(FloatingText::new( + ROOT_WARNING.into(), + "Root User Warning", + true, + )) +} diff --git a/tui/src/state.rs b/tui/src/state.rs index dbd13a1d..7f96aee9 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -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, }; @@ -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);