Skip to content

Commit

Permalink
fix(launchpad): exit if not run as admin on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin authored and joshuef committed Jun 6, 2024
1 parent e06d7e1 commit eaa7b72
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 60 deletions.
19 changes: 0 additions & 19 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion node-launchpad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ json5 = "0.4.1"
lazy_static = "1.4.0"
libc = "0.2.148"
log = "0.4.20"
nix = { version = "0.28.0", features = ["user"] }
pretty_assertions = "1.4.0"
prometheus-parse = "0.2.5"
rand = "0.8.5"
Expand Down
25 changes: 21 additions & 4 deletions node-launchpad/src/bin/tui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@

mod terminal;

#[macro_use]
extern crate tracing;

use clap::Parser;
use color_eyre::eyre::Result;
use node_launchpad::{
app::App,
config::configure_winsw,
utils::{initialize_logging, initialize_panic_handler, version},
};
#[cfg(target_os = "windows")]
use sn_node_manager::config::is_running_as_root;
use sn_peers_acquisition::PeersArgs;
use std::{env, path::PathBuf};
use tokio::task::LocalSet;
Expand Down Expand Up @@ -51,12 +56,10 @@ pub struct Cli {
}

async fn tokio_main() -> Result<()> {
initialize_logging()?;

initialize_panic_handler()?;

let args = Cli::parse();

info!("Starting app with args: {args:?}");
let mut app = App::new(
args.tick_rate,
args.frame_rate,
Expand All @@ -74,14 +77,28 @@ fn is_running_in_terminal() -> bool {

#[tokio::main]
async fn main() -> Result<()> {
initialize_logging()?;
configure_winsw().await?;

if !is_running_in_terminal() {
info!("Running in non-terminal mode. Launching terminal.");
// If we weren't already running in a terminal, this process returns early, having spawned
// a new process that launches a terminal.
let terminal_type = terminal::detect_and_setup_terminal()?;
terminal::launch_terminal(&terminal_type)?;
terminal::launch_terminal(&terminal_type)
.inspect_err(|err| error!("Error while launching terminal: {err:?}"))?;
return Ok(());
} else {
// Windows spawns the terminal directly, so the check for root has to happen here as well.
debug!("Running inside a terminal!");
#[cfg(target_os = "windows")]
if !is_running_as_root() {
{
// todo: There is no terminal to show this error message when double clicking on the exe.
error!("Admin privileges required to run on Windows. Exiting.");
color_eyre::eyre::bail!("Admin privileges required to run on Windows. Exiting.");
}
}
}

// Construct a local task set that can run `!Send` futures.
Expand Down
21 changes: 6 additions & 15 deletions node-launchpad/src/bin/tui/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use clap::Parser;
use color_eyre::eyre::{eyre, Result};
#[cfg(target_os = "windows")]
use sn_node_manager::config::is_running_as_root;
use std::{path::PathBuf, process::Command};
use which::which;

Expand All @@ -24,24 +26,12 @@ pub(crate) enum TerminalType {
WindowsTerminal(PathBuf),
Xterm(PathBuf),
}

#[cfg(not(windows))]
pub(crate) fn is_running_root() -> bool {
use nix::unistd::geteuid;
geteuid().is_root()
}

#[cfg(windows)]
pub(crate) 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()
}

pub(crate) fn detect_and_setup_terminal() -> Result<TerminalType> {
if !is_running_root() {
#[cfg(target_os = "windows")]
#[cfg(target_os = "windows")]
if !is_running_as_root() {
{
// todo: There is no terminal to show this error message when double clicking on the exe.
error!("Admin privileges required to run on Windows. Exiting.");
color_eyre::eyre::bail!("Admin privileges required to run");
}
}
Expand Down Expand Up @@ -86,6 +76,7 @@ fn get_available_linux_terminal() -> Result<TerminalType> {
}

pub(crate) fn launch_terminal(terminal_type: &TerminalType) -> Result<()> {
info!("Launching terminal: {terminal_type:?}");
let launchpad_path = std::env::current_exe()?;

match terminal_type {
Expand Down
4 changes: 2 additions & 2 deletions sn_node_manager/src/cmd/auditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::{download_and_get_upgrade_bin_path, is_running_as_root, print_upgrade_summary};
use super::{download_and_get_upgrade_bin_path, print_upgrade_summary};
use crate::{
add_services::{add_auditor, config::AddAuditorServiceOptions},
config,
config::{self, is_running_as_root},
helpers::{download_and_extract_release, get_bin_version},
print_banner, ServiceManager, VerbosityLevel,
};
Expand Down
3 changes: 1 addition & 2 deletions sn_node_manager/src/cmd/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::is_running_as_root;
use crate::{
add_services::{add_daemon, config::AddDaemonServiceOptions},
config,
config::{self, is_running_as_root},
helpers::{download_and_extract_release, get_bin_version},
print_banner, ServiceManager, VerbosityLevel,
};
Expand Down
4 changes: 2 additions & 2 deletions sn_node_manager/src/cmd/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::{download_and_get_upgrade_bin_path, is_running_as_root, print_upgrade_summary};
use super::{download_and_get_upgrade_bin_path, print_upgrade_summary};
use crate::{
add_services::{add_faucet, config::AddFaucetServiceOptions},
config,
config::{self, is_running_as_root},
helpers::{download_and_extract_release, get_bin_version},
print_banner, ServiceManager, VerbosityLevel,
};
Expand Down
10 changes: 0 additions & 10 deletions sn_node_manager/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ use std::{
process::{Command, Stdio},
};

#[cfg(unix)]
pub fn is_running_as_root() -> bool {
users::get_effective_uid() == 0
}

#[cfg(windows)]
pub fn is_running_as_root() -> bool {
true
}

pub async fn download_and_get_upgrade_bin_path(
custom_bin_path: Option<PathBuf>,
release_type: ReleaseType,
Expand Down
4 changes: 2 additions & 2 deletions sn_node_manager/src/cmd/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

#![allow(clippy::too_many_arguments)]

use super::{download_and_get_upgrade_bin_path, is_running_as_root, print_upgrade_summary};
use super::{download_and_get_upgrade_bin_path, print_upgrade_summary};
use crate::{
add_services::{
add_node,
config::{AddNodeServiceOptions, PortRange},
},
config,
config::{self, is_running_as_root},
helpers::{download_and_extract_release, get_bin_version},
print_banner, refresh_node_registry, status_report, ServiceManager, VerbosityLevel,
};
Expand Down
7 changes: 4 additions & 3 deletions sn_node_manager/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,14 @@ pub fn create_owned_dir(path: PathBuf, _owner: &str) -> Result<()> {

#[cfg(unix)]
pub fn is_running_as_root() -> bool {
users::get_effective_uid() == 0
use nix::unistd::geteuid;
geteuid().is_root()
}

#[cfg(windows)]
pub fn is_running_as_root() -> bool {
// The Windows implementation for this will be much more complex.
true
// Example: Attempt to read from a typically restricted system directory
std::fs::read_dir("C:\\Windows\\System32\\config").is_ok()
}

pub fn get_user_safenode_data_dir() -> Result<PathBuf> {
Expand Down

0 comments on commit eaa7b72

Please sign in to comment.