avoid running as root, compare underlying configs

This commit is contained in:
Bryson Steck 2025-03-02 15:47:21 -07:00
parent d7a84d5471
commit a50c9bffdb
Signed by: bryson
SSH key fingerprint: SHA256:XpKABw/nP4z8UVaH+weLaBnEOD86+cVwif+QjuYLGT4
4 changed files with 30 additions and 2 deletions

11
Cargo.lock generated
View file

@ -1649,6 +1649,7 @@ dependencies = [
"serde", "serde",
"serde_derive", "serde_derive",
"toml", "toml",
"users",
] ]
[[package]] [[package]]
@ -1948,6 +1949,16 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "users"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"
dependencies = [
"libc",
"log",
]
[[package]] [[package]]
name = "utf16_iter" name = "utf16_iter"
version = "1.0.5" version = "1.0.5"

View file

@ -9,3 +9,4 @@ gix = "0.70.0"
serde = "1.0.217" serde = "1.0.217"
serde_derive = "1.0.217" serde_derive = "1.0.217"
toml = "0.8.20" toml = "0.8.20"
users = "0.11.0"

View file

@ -82,6 +82,7 @@ impl fmt::Display for ConfigFile {
} }
} }
#[derive(PartialEq)]
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Config { pub struct Config {
from: String, from: String,
@ -92,11 +93,13 @@ pub struct Config {
schedule: Schedule schedule: Schedule
} }
#[derive(PartialEq)]
#[derive(Deserialize)] #[derive(Deserialize)]
struct Git { struct Git {
ssh_identity_file: String, ssh_identity_file: String,
} }
#[derive(PartialEq)]
#[derive(Deserialize)] #[derive(Deserialize)]
struct Schedule { struct Schedule {
enabled: bool, enabled: bool,
@ -135,13 +138,14 @@ pub fn read_config(paths: Vec<PathBuf>, refractr: &common::Refractr) -> Vec<Conf
eprintln!("refractr: warning: skipping config file \"{}\" as it was already read", path.as_path().display()); eprintln!("refractr: warning: skipping config file \"{}\" as it was already read", path.as_path().display());
dup = true; dup = true;
break; break;
} else if i.config == config_file.config {
eprintln!("refractr: warning: config files \"{}\" and \"{}\" appear to have the same config", i.path, config_file.path);
} }
} }
if !dup { if !dup {
config_files.push(config_file); config_files.push(config_file);
} }
} }
return config_files; return config_files;

View file

@ -4,6 +4,7 @@ mod config;
use clap::Parser; use clap::Parser;
use std::path::PathBuf; use std::path::PathBuf;
use std::process; use std::process;
use users;
#[derive(Parser)] #[derive(Parser)]
#[command(name = "refractor")] #[command(name = "refractor")]
@ -29,7 +30,7 @@ fn get_config_default() -> &'static str {
} }
} }
fn main() { fn main() -> std::io::Result<()> {
let args = Args::parse(); let args = Args::parse();
let refractr = common::Refractr { let refractr = common::Refractr {
verbose: args.verbose, verbose: args.verbose,
@ -44,6 +45,15 @@ fn main() {
let example = include_str!("example/config.toml"); let example = include_str!("example/config.toml");
println!("{}", example); println!("{}", example);
} else { } else {
// warn to avoid root/admin
if refractr.unix {
if users::get_current_uid() == 0 {
eprintln!("refractr: warning: this program should not ran as root")
}
} else {
// TODO: print message for Windows
}
let cfgs = config::read_config(args.config, &refractr); let cfgs = config::read_config(args.config, &refractr);
if refractr.verbose >= 2 { if refractr.verbose >= 2 {
// no need to loop over configs if verbose is not at the correct level // no need to loop over configs if verbose is not at the correct level
@ -54,4 +64,6 @@ fn main() {
common::verbose(refractr.verbose, 1, format!("Config file(s) read successfully")); common::verbose(refractr.verbose, 1, format!("Config file(s) read successfully"));
} }
Ok(())
} }