Skip to content

Commit

Permalink
feat(launchpad): escalate to sudo mode if required automatically
Browse files Browse the repository at this point in the history
This adds a check for sudo/admin priviliges, bailing if we cannot automatically escalate.
  • Loading branch information
joshuef committed May 6, 2024
1 parent bbcb5a3 commit 04222b0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions sn_node_launchpad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
32 changes: 31 additions & 1 deletion sn_node_launchpad/src/bin/tui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 04222b0

Please sign in to comment.