2020-02-29 05:02:14 +01:00
|
|
|
use crate::constants::{
|
2020-06-21 13:57:48 +02:00
|
|
|
DEFAULT_CACHE_TIMEOUT, DEFAULT_CONN_TIMEOUT, DEFAULT_DB_PATH, DEFAULT_GID_ATTR_MAP,
|
2021-03-13 03:33:15 +01:00
|
|
|
DEFAULT_HOME_ALIAS, DEFAULT_HOME_ATTR, DEFAULT_HOME_PREFIX, DEFAULT_SHELL, DEFAULT_SOCK_PATH,
|
|
|
|
DEFAULT_TASK_SOCK_PATH, DEFAULT_UID_ATTR_MAP,
|
2020-02-29 05:02:14 +01:00
|
|
|
};
|
2021-12-31 00:11:20 +01:00
|
|
|
use serde::Deserialize;
|
2020-02-29 05:02:14 +01:00
|
|
|
use std::fs::File;
|
2021-05-21 07:19:36 +02:00
|
|
|
use std::io::{ErrorKind, Read};
|
2020-02-29 05:02:14 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct ConfigInt {
|
|
|
|
db_path: Option<String>,
|
|
|
|
sock_path: Option<String>,
|
2021-03-13 03:33:15 +01:00
|
|
|
task_sock_path: Option<String>,
|
2020-02-29 05:02:14 +01:00
|
|
|
conn_timeout: Option<u64>,
|
|
|
|
cache_timeout: Option<u64>,
|
|
|
|
pam_allowed_login_groups: Option<Vec<String>>,
|
2020-06-21 13:57:48 +02:00
|
|
|
default_shell: Option<String>,
|
|
|
|
home_prefix: Option<String>,
|
|
|
|
home_attr: Option<String>,
|
2021-03-13 03:33:15 +01:00
|
|
|
home_alias: Option<String>,
|
2020-06-21 13:57:48 +02:00
|
|
|
uid_attr_map: Option<String>,
|
|
|
|
gid_attr_map: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum HomeAttr {
|
|
|
|
Uuid,
|
|
|
|
Spn,
|
|
|
|
Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum UidAttr {
|
|
|
|
Name,
|
|
|
|
Spn,
|
2020-02-29 05:02:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct KanidmUnixdConfig {
|
|
|
|
pub db_path: String,
|
|
|
|
pub sock_path: String,
|
2021-03-13 03:33:15 +01:00
|
|
|
pub task_sock_path: String,
|
2020-02-29 05:02:14 +01:00
|
|
|
pub conn_timeout: u64,
|
|
|
|
pub cache_timeout: u64,
|
|
|
|
pub pam_allowed_login_groups: Vec<String>,
|
2020-06-21 13:57:48 +02:00
|
|
|
pub default_shell: String,
|
|
|
|
pub home_prefix: String,
|
|
|
|
pub home_attr: HomeAttr,
|
2021-03-13 03:33:15 +01:00
|
|
|
pub home_alias: Option<HomeAttr>,
|
2020-06-21 13:57:48 +02:00
|
|
|
pub uid_attr_map: UidAttr,
|
|
|
|
pub gid_attr_map: UidAttr,
|
2020-02-29 05:02:14 +01:00
|
|
|
}
|
|
|
|
|
2020-06-18 02:30:42 +02:00
|
|
|
impl Default for KanidmUnixdConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
KanidmUnixdConfig::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 05:02:14 +01:00
|
|
|
impl KanidmUnixdConfig {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
KanidmUnixdConfig {
|
|
|
|
db_path: DEFAULT_DB_PATH.to_string(),
|
|
|
|
sock_path: DEFAULT_SOCK_PATH.to_string(),
|
2021-03-13 03:33:15 +01:00
|
|
|
task_sock_path: DEFAULT_TASK_SOCK_PATH.to_string(),
|
2020-02-29 05:02:14 +01:00
|
|
|
conn_timeout: DEFAULT_CONN_TIMEOUT,
|
|
|
|
cache_timeout: DEFAULT_CACHE_TIMEOUT,
|
|
|
|
pam_allowed_login_groups: Vec::new(),
|
2020-06-21 13:57:48 +02:00
|
|
|
default_shell: DEFAULT_SHELL.to_string(),
|
|
|
|
home_prefix: DEFAULT_HOME_PREFIX.to_string(),
|
|
|
|
home_attr: DEFAULT_HOME_ATTR,
|
2021-03-13 03:33:15 +01:00
|
|
|
home_alias: DEFAULT_HOME_ALIAS,
|
2020-06-21 13:57:48 +02:00
|
|
|
uid_attr_map: DEFAULT_UID_ATTR_MAP,
|
|
|
|
gid_attr_map: DEFAULT_GID_ATTR_MAP,
|
2020-02-29 05:02:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 00:54:28 +02:00
|
|
|
pub fn read_options_from_optional_config<P: AsRef<Path> + std::fmt::Debug>(
|
2020-02-29 05:02:14 +01:00
|
|
|
self,
|
|
|
|
config_path: P,
|
|
|
|
) -> Result<Self, ()> {
|
2021-05-21 07:19:36 +02:00
|
|
|
debug!("Attempting to load configuration from {:#?}", &config_path);
|
2021-04-15 00:54:28 +02:00
|
|
|
let mut f = match File::open(&config_path) {
|
2021-05-21 07:19:36 +02:00
|
|
|
Ok(f) => {
|
|
|
|
debug!("Successfully opened configuration file {:#?}", &config_path);
|
|
|
|
f
|
|
|
|
}
|
2020-02-29 05:02:14 +01:00
|
|
|
Err(e) => {
|
2021-05-21 07:19:36 +02:00
|
|
|
match e.kind() {
|
|
|
|
ErrorKind::NotFound => {
|
|
|
|
debug!(
|
|
|
|
"Configuration file {:#?} not found, skipping.",
|
|
|
|
&config_path
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ErrorKind::PermissionDenied => {
|
|
|
|
warn!(
|
|
|
|
"Permission denied loading configuration file {:#?}, skipping.",
|
|
|
|
&config_path
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
debug!(
|
|
|
|
"Unable to open config file {:#?} [{:?}], skipping ...",
|
|
|
|
&config_path, e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2020-02-29 05:02:14 +01:00
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
f.read_to_string(&mut contents)
|
|
|
|
.map_err(|e| eprintln!("{:?}", e))?;
|
|
|
|
|
|
|
|
let config: ConfigInt =
|
|
|
|
toml::from_str(contents.as_str()).map_err(|e| eprintln!("{:?}", e))?;
|
|
|
|
|
|
|
|
// Now map the values into our config.
|
|
|
|
Ok(KanidmUnixdConfig {
|
|
|
|
db_path: config.db_path.unwrap_or(self.db_path),
|
|
|
|
sock_path: config.sock_path.unwrap_or(self.sock_path),
|
2021-03-13 03:33:15 +01:00
|
|
|
task_sock_path: config.task_sock_path.unwrap_or(self.task_sock_path),
|
2020-02-29 05:02:14 +01:00
|
|
|
conn_timeout: config.conn_timeout.unwrap_or(self.conn_timeout),
|
|
|
|
cache_timeout: config.cache_timeout.unwrap_or(self.cache_timeout),
|
|
|
|
pam_allowed_login_groups: config
|
|
|
|
.pam_allowed_login_groups
|
|
|
|
.unwrap_or(self.pam_allowed_login_groups),
|
2020-06-21 13:57:48 +02:00
|
|
|
default_shell: config.default_shell.unwrap_or(self.default_shell),
|
|
|
|
home_prefix: config.home_prefix.unwrap_or(self.home_prefix),
|
|
|
|
home_attr: config
|
|
|
|
.home_attr
|
|
|
|
.and_then(|v| match v.as_str() {
|
|
|
|
"uuid" => Some(HomeAttr::Uuid),
|
|
|
|
"spn" => Some(HomeAttr::Spn),
|
|
|
|
"name" => Some(HomeAttr::Name),
|
|
|
|
_ => {
|
|
|
|
warn!("Invalid home_attr configured, using default ...");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(self.home_attr),
|
2021-03-13 03:33:15 +01:00
|
|
|
home_alias: config
|
|
|
|
.home_alias
|
|
|
|
.and_then(|v| match v.as_str() {
|
|
|
|
"none" => Some(None),
|
|
|
|
"uuid" => Some(Some(HomeAttr::Uuid)),
|
|
|
|
"spn" => Some(Some(HomeAttr::Spn)),
|
|
|
|
"name" => Some(Some(HomeAttr::Name)),
|
|
|
|
_ => {
|
|
|
|
warn!("Invalid home_alias configured, using default ...");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(self.home_alias),
|
2020-06-21 13:57:48 +02:00
|
|
|
uid_attr_map: config
|
|
|
|
.uid_attr_map
|
|
|
|
.and_then(|v| match v.as_str() {
|
|
|
|
"spn" => Some(UidAttr::Spn),
|
|
|
|
"name" => Some(UidAttr::Name),
|
|
|
|
_ => {
|
|
|
|
warn!("Invalid uid_attr_map configured, using default ...");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(self.uid_attr_map),
|
|
|
|
gid_attr_map: config
|
|
|
|
.gid_attr_map
|
|
|
|
.and_then(|v| match v.as_str() {
|
|
|
|
"spn" => Some(UidAttr::Spn),
|
|
|
|
"name" => Some(UidAttr::Name),
|
|
|
|
_ => {
|
|
|
|
warn!("Invalid gid_attr_map configured, using default ...");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(self.gid_attr_map),
|
2020-02-29 05:02:14 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|