From 6d084c671a48391401a56e2048b8ae211fd8e3a6 Mon Sep 17 00:00:00 2001 From: Neshura Date: Wed, 10 Apr 2024 22:50:25 +0200 Subject: [PATCH] Fix ownership issues caused by confy when interacting with other users' config directory. Closes #1 --- src/main.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index a73b061..a874d2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use std::error::Error; -use std::fs; +use std::{fs, io}; use std::net::{IpAddr, Ipv6Addr}; +use std::os::unix; +use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; use std::sync::RwLock; use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest}; @@ -158,8 +160,16 @@ impl Config { fn load_user_config_directory(path: PathBuf) -> (UserConfig, Option) { let config_path = format!("{}/.config/{}", path.display(), env!("CARGO_PKG_NAME")); - match confy::load_path(config_path.clone() + "/domains.toml") { + match confy::load_path::(config_path.clone() + "/domains.toml") { Ok(data) => { + if data.domain_configs.is_empty() { + match Self::fix_path_ownership(path, vec![".config", env!("CARGO_PKG_NAME"), "domains.toml"]) { + Ok(_) => (), + Err(e) => { + error!(e); + } + }; + } let msg = format!("Using {config_path}/domains.toml"); info!(msg); (data, Some(config_path)) @@ -185,6 +195,35 @@ impl Config { } } } + + fn fix_path_ownership(root: PathBuf, paths: Vec<&str>) -> io::Result<()> { + let root_metadata = fs::metadata(&root)?; + let uid = root_metadata.uid(); + let gid = root_metadata.gid(); + println!("uid: {uid}, gid: {gid}"); + match paths.len() { + 1 => { + let new_root = root.join(paths[0]); + println!("{}", &new_root.display()); + unix::fs::chown(new_root, Some(uid), Some(gid)) + }, + _ => { + let new_root = root.join(paths[0]); + println!("{}", &new_root.display()); + let ret = unix::fs::chown(&new_root, Some(uid), Some(gid)); + + let mut new_paths = paths.clone(); + new_paths.remove(0); + match Self::fix_path_ownership(new_root, new_paths) { + Ok(_) => ret, + Err(e) => { + error!(e); + Err(e) + } + } + } + } + } } #[actix_web::main] @@ -238,7 +277,7 @@ async fn main() -> notify::Result<()> { } } })?; - + for directory in directories.iter() { watcher.watch(directory, RecursiveMode::NonRecursive)?; }