use colored::*; use std::path::Path; use std::process::Command; use crate::config::Config; const REPOS_DIR: &str = "/var/ror/packages"; fn enforce_repo_https(url: &str) -> Result<(), String> { if url.starts_with("http://") { return Err(format!( "Insecure HTTP URL rejected: {}. Only HTTPS is allowed for repositories.", url )); } Ok(()) } fn check_repo_permissions() { let repo_path = Path::new(REPOS_DIR); if !repo_path.exists() { return; } match std::fs::metadata(repo_path) { Ok(meta) => { use std::os::unix::fs::MetadataExt; let uid = meta.uid(); if uid != 0 { eprintln!( "{} WARNING: Repository directory '{}' is owned by UID {} (not root). \ This is a security risk — any user can modify packages.", "[ror]".yellow().bold(), REPOS_DIR, uid ); } let mode = meta.mode() & 0o7777; if mode & 0o002 != 0 { eprintln!( "{} WARNING: Repository directory '{}' is world-writable (mode {:o}). \ This is a security risk — any user can inject malicious packages.", "[ror]".yellow().bold(), REPOS_DIR, mode ); } } Err(_) => {} } } pub fn run_sync(cfg: &Config) { if cfg.repositories.is_empty() { eprintln!("{} No repositories configured.", "[ror]".red().bold()); return; } check_repo_permissions(); for (name, repo) in &cfg.repositories { if let Err(e) = enforce_repo_https(&repo.url) { eprintln!("{} {}: {}", "[ror]".red().bold(), name, e); continue; } println!("{} Syncing repository '{}'...", "[ror]".blue().bold(), name); let repo_path = Path::new(REPOS_DIR).join(name); if repo_path.exists() { println!("{} Updating {}...", ">>>".green(), name); if let Err(e) = pull_repo(&repo_path) { eprintln!("{} {}", "[ror]".red(), e); } else { println!("{} Repository updated.", ">>>".green()); } } else { println!("{} Cloning {}...", ">>>".green(), name); if let Err(e) = clone_repo(name, &repo.url) { eprintln!("{} {}", "[ror]".red(), e); } else { println!("{} Repository cloned.", ">>>".green()); } } } } fn clone_repo(name: &str, primary: &str) -> Result<(), String> { let path = Path::new(REPOS_DIR).join(name); if let Some(parent) = path.parent() { std::fs::create_dir_all(parent).map_err(|e| format!("Failed to create repos dir: {}", e))?; } let status = Command::new("git") .args(["clone", primary, path.to_str().unwrap()]) .status() .map_err(|e| format!("Failed to run git: {}", e))?; if status.success() { return Ok(()); } Err(format!("Failed to clone repository '{}'", name)) } fn pull_repo(repo_path: &Path) -> Result<(), String> { let path_str = repo_path.to_str().ok_or("Invalid repo path")?; let fetch = Command::new("git") .args(["-C", path_str, "fetch", "origin"]) .status() .map_err(|e| format!("Failed to run git fetch: {}", e))?; if !fetch.success() { return Err(format!("git fetch failed for '{}'", path_str)); } let _ = Command::new("git") .args(["-C", path_str, "clean", "-fd"]) .status(); let reset = Command::new("git") .args(["-C", path_str, "reset", "--hard", "FETCH_HEAD"]) .status() .map_err(|e| format!("Failed to run git reset: {}", e))?; if reset.success() { return Ok(()); } Err(format!("Failed to update repository at '{}'", path_str)) }