Skip to content

Commit

Permalink
prevent running khost as a root
Browse files Browse the repository at this point in the history
fix status errors when nginx is not installed
fix network selection during full install
  • Loading branch information
aspect committed Jul 22, 2024
1 parent 8939b50 commit a4e61cf
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "khost"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
authors = ["Kaspa developers"]
license = "MIT OR Apache-2.0"
Expand Down
6 changes: 3 additions & 3 deletions src/kaspad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Config {
}

pub fn disable(&mut self) {
self.enabled = true;
self.enabled = false;
}

pub fn set_origin(&mut self, origin: Origin) {
Expand Down Expand Up @@ -394,7 +394,7 @@ pub fn reconfigure(ctx: &Context, force: bool) -> Result<()> {
for config in inactive_configs(ctx) {
let service_name = config.service_name();
if systemd::exists(config) {
if systemd::is_active(config)? {
if systemd::is_active(&config.service_name())? {
step(format!("Bringing down '{}'", service_name), || {
systemd::stop(config)
})?;
Expand Down Expand Up @@ -536,7 +536,7 @@ pub fn find_config_by_service_detail<'a>(
}

pub fn select_networks(ctx: &mut Context) -> Result<()> {
if ctx.system.total_memory < 17 * 1024 * 1024 * 1024 {
if ctx.system.total_memory < 15 * 1024 * 1024 * 1024 {
log::warning(format!(
"Detected RAM is {}, minimum required for multiple networks is 32 Gb.",
as_gb(ctx.system.total_memory as f64, false, false)
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ fn main() {
std::process::exit(1);
}

if is_root() {
let _ = log::error("kHOST should not be run as root");
let _ = outro("Exiting...");
println!();
std::process::exit(2);
}

// Check for updates
khost::update().ok();

Expand Down
5 changes: 4 additions & 1 deletion src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ pub fn reconfigure(ctx: &mut Context, _force: bool) -> Result<()> {

if !resolver::is_installed(ctx) {
resolver::install(ctx)?;
} else if config.enabled() && systemd::is_enabled(config)? && systemd::is_active(config)? {
} else if config.enabled()
&& systemd::is_enabled(&config.service_name())?
&& systemd::is_active(&config.service_name())?
{
restart(ctx)?;
} else {
step("Configuring 'kaspa-resolver'", || {
Expand Down
15 changes: 13 additions & 2 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ pub fn detect(ctx: &Context) -> Status {
.active_services()
.into_iter()
.map(|service| {
let status = systemd::unit_state(&service.name);
(service, status)
if service.kind == ServiceKind::Nginx {
match systemd::is_enabled(&service.name) {
Ok(true) => {
let status = systemd::unit_state(&service.name);
(service, status)
}
Ok(false) => (service, Err("n/a".to_string())),
Err(e) => (service, Err(e.to_string())),
}
} else {
let status = systemd::unit_state(&service.name);
(service, status)
}
})
.collect();

Expand Down
8 changes: 4 additions & 4 deletions src/systemd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ pub fn status<S: Service>(service: &S) -> Result<String> {
sudo!("systemctl", "status", service.service_name()).read()
}

pub fn is_active<S: Service>(service: &S) -> Result<bool> {
let output = sudo!("systemctl", "is-active", service.service_name())
pub fn is_active<S: Display>(service: &S) -> Result<bool> {
let output = sudo!("systemctl", "is-active", service.to_string())
.unchecked()
.read()?;
Ok(output.trim() == "active")
}

pub fn is_enabled<S: Service>(service: &S) -> Result<bool> {
let output = sudo!("systemctl", "is-enabled", service.service_name())
pub fn is_enabled<S: Display>(service: &S) -> Result<bool> {
let output = sudo!("systemctl", "is-enabled", service.to_string())
.unchecked()
.read()?;
Ok(output.trim() == "enabled")
Expand Down

0 comments on commit a4e61cf

Please sign in to comment.