Fix ownership issues caused by confy when interacting with other users' config directory. Closes #1
All checks were successful
Run Tests on Code / run-tests (push) Successful in 19s

This commit is contained in:
Neshura 2024-04-10 22:50:25 +02:00
parent d0eb4b793c
commit 6d084c671a
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C

View file

@ -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<String>) {
let config_path = format!("{}/.config/{}", path.display(), env!("CARGO_PKG_NAME"));
match confy::load_path(config_path.clone() + "/domains.toml") {
match confy::load_path::<UserConfig>(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]