From 04222b08c0acffb3ccca719ce043248d57989ce4 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Mon, 6 May 2024 18:44:16 +0900 Subject: [PATCH] feat(launchpad): escalate to sudo mode if required automatically This adds a check for sudo/admin priviliges, bailing if we cannot automatically escalate. --- Cargo.lock | 19 ++++++++++++++++ sn_node_launchpad/Cargo.toml | 1 + sn_node_launchpad/src/bin/tui/main.rs | 32 ++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 198b23a739..c2ddc61d3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -957,6 +957,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "chacha20" version = "0.9.1" @@ -4585,6 +4591,18 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" +dependencies = [ + "bitflags 2.5.0", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -7151,6 +7169,7 @@ dependencies = [ "lazy_static", "libc", "log", + "nix 0.28.0", "pretty_assertions", "ratatui", "serde", diff --git a/sn_node_launchpad/Cargo.toml b/sn_node_launchpad/Cargo.toml index b5a6df85c6..740c4a22cd 100644 --- a/sn_node_launchpad/Cargo.toml +++ b/sn_node_launchpad/Cargo.toml @@ -58,6 +58,7 @@ tracing-error = "0.2.0" tracing-subscriber = { version = "0.3.17", features = ["env-filter", "serde"] } tui-input = "0.8.0" which = "6.0.1" +nix = { version = "0.28.0", features = ["user"] } [build-dependencies] vergen = { version = "8.2.6", features = ["build", "git", "gitoxide", "cargo"] } diff --git a/sn_node_launchpad/src/bin/tui/main.rs b/sn_node_launchpad/src/bin/tui/main.rs index 6f10236c16..c177dfb02c 100644 --- a/sn_node_launchpad/src/bin/tui/main.rs +++ b/sn_node_launchpad/src/bin/tui/main.rs @@ -7,7 +7,9 @@ // permissions and limitations relating to use of the SAFE Network Software. use clap::Parser; -use color_eyre::eyre::Result; +use color_eyre::eyre::{bail, Result}; +use std::env; +use std::process::Command; use tokio::task::LocalSet; use sn_node_launchpad::{ @@ -49,8 +51,36 @@ async fn tokio_main() -> Result<()> { Ok(()) } +#[cfg(not(windows))] +fn is_running_root() -> bool { + use nix::unistd::geteuid; + geteuid().is_root() +} + +#[cfg(windows)] +fn is_running_root() -> bool { + // Example: Attempt to read from a typically restricted system directory + std::fs::read_dir("C:\\Windows\\System32\\config").is_ok() +} + #[tokio::main] async fn main() -> Result<()> { + if !is_running_root() { + #[cfg(windows)] + { + bail!("Admin privilges required to run"); + } + + // Attempt to elevate privileges using sudo + let exe = env::current_exe()?; + let status = Command::new("sudo").arg(exe).status()?; + + if !status.success() { + bail!("Failed to gain root privileges."); + } + return Ok(()); + } + // Construct a local task set that can run `!Send` futures. let local = LocalSet::new(); local