From 6b696b19230d4589b08afb743dfb5ca9a3908444 Mon Sep 17 00:00:00 2001 From: James Hodgkinson Date: Tue, 15 Jun 2021 11:54:04 +1000 Subject: [PATCH] unixd will now bail if startup tests fail (#476) --- kanidm_client/src/lib.rs | 5 ++- kanidm_unix_int/src/daemon.rs | 60 +++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/kanidm_client/src/lib.rs b/kanidm_client/src/lib.rs index 45f86eef4..b57d82966 100644 --- a/kanidm_client/src/lib.rs +++ b/kanidm_client/src/lib.rs @@ -132,7 +132,10 @@ impl KanidmClientBuilder { // Process and apply all our options if they exist. let address = match kcc.uri { Some(uri) => Some(uri), - None => address, + None => { + debug!("No URI in supplied config"); + address + } }; let verify_ca = kcc.verify_ca.unwrap_or(verify_ca); let verify_hostnames = kcc.verify_hostnames.unwrap_or(verify_hostnames); diff --git a/kanidm_unix_int/src/daemon.rs b/kanidm_unix_int/src/daemon.rs index 7bf7b1378..dfe6ffd87 100644 --- a/kanidm_unix_int/src/daemon.rs +++ b/kanidm_unix_int/src/daemon.rs @@ -121,9 +121,17 @@ impl TaskCodec { } fn rm_if_exist(p: &str) { - let _ = std::fs::remove_file(p).map_err(|e| { - warn!("attempting to remove {:?} -> {:?}", p, e); - }); + if Path::new(p).exists() { + debug!("Removing requested file {:?}", p); + let _ = std::fs::remove_file(p).map_err(|e| { + error!( + "Failure while attempting to attempting to remove {:?} -> {:?}", + p, e + ); + }); + } else { + debug!("Path {:?} doesn't exist, not attempting to remove.", p); + } } async fn handle_task_client( @@ -381,6 +389,15 @@ async fn main() { std::process::exit(1); } }; + if !cfg_path.exists() { + // there's no point trying to start up if we can't read a usable config! + error!( + "Client config missing from {} - cannot start up. Quitting.", + cfg_path_str + ); + std::process::exit(1); + } + if cfg_path.exists() { let cfg_meta = match metadata(&cfg_path) { Ok(v) => v, @@ -410,7 +427,14 @@ async fn main() { std::process::exit(1); } }; - if unixd_path.exists() { + if !unixd_path.exists() { + // there's no point trying to start up if we can't read a usable config! + error!( + "unixd config missing from {} - cannot start up. Quitting.", + unixd_path_str + ); + std::process::exit(1); + } else { let unixd_meta = match metadata(&unixd_path) { Ok(v) => v, Err(e) => { @@ -447,6 +471,7 @@ async fn main() { } }; + debug!("🧹 Cleaning up sockets from previous invocations"); rm_if_exist(cfg.sock_path.as_str()); rm_if_exist(cfg.task_sock_path.as_str()); @@ -463,8 +488,7 @@ async fn main() { // Check the pb path will be okay. if cfg.db_path != "" { let db_path = PathBuf::from(cfg.db_path.as_str()); - // We only need to check the parent folder path permissions as the db itself may not - // exist yet. + // We only need to check the parent folder path permissions as the db itself may not exist yet. if let Some(db_parent_path) = db_path.parent() { if !db_parent_path.exists() { error!( @@ -513,6 +537,30 @@ async fn main() { ); } } + + // check to see if the db's already there + if db_path.exists() { + if !db_path.is_file() { + error!( + "Refusing to run - DB path {} already exists and is not a file.", + db_path.to_str().unwrap_or_else(|| "") + ); + std::process::exit(1); + }; + + match metadata(&db_path) { + Ok(v) => v, + Err(e) => { + error!( + "Unable to read metadata for {} - {:?}", + db_path.to_str().unwrap_or_else(|| ""), + e + ); + std::process::exit(1); + } + }; + // TODO: permissions dance to enumerate the user's ability to write to the file? ref #456 - r2d2 will happily keep trying to do things without bailing. + }; } let cl_inner = match CacheLayer::new(