2020-04-14 11:45:45 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2020-04-06 10:09:27 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_schema::*;
|
|
|
|
use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
|
2020-04-06 10:09:27 +00:00
|
|
|
|
2021-09-09 11:14:28 +00:00
|
|
|
use pbs_api_types::{
|
|
|
|
Authid, Userid, ApiToken, User,
|
2021-07-19 08:50:18 +00:00
|
|
|
};
|
|
|
|
|
2021-11-12 17:44:28 +00:00
|
|
|
use crate::ConfigVersionCache;
|
2021-09-10 04:53:53 +00:00
|
|
|
|
|
|
|
use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
|
2020-04-06 10:09:27 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2021-02-10 09:27:40 +00:00
|
|
|
pub static ref CONFIG: SectionConfig = init();
|
2020-04-06 10:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn init() -> SectionConfig {
|
2020-10-23 11:33:21 +00:00
|
|
|
let mut config = SectionConfig::new(&Authid::API_SCHEMA);
|
|
|
|
|
|
|
|
let user_schema = match User::API_SCHEMA {
|
|
|
|
Schema::Object(ref user_schema) => user_schema,
|
2020-04-06 10:09:27 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-10-23 11:33:21 +00:00
|
|
|
let user_plugin = SectionConfigPlugin::new("user".to_string(), Some("userid".to_string()), user_schema);
|
|
|
|
config.register_plugin(user_plugin);
|
2020-04-06 10:09:27 +00:00
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
let token_schema = match ApiToken::API_SCHEMA {
|
|
|
|
Schema::Object(ref token_schema) => token_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let token_plugin = SectionConfigPlugin::new("token".to_string(), Some("tokenid".to_string()), token_schema);
|
|
|
|
config.register_plugin(token_plugin);
|
2020-04-06 10:09:27 +00:00
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const USER_CFG_FILENAME: &str = "/etc/proxmox-backup/user.cfg";
|
|
|
|
pub const USER_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.user.lck";
|
|
|
|
|
2021-09-09 11:14:28 +00:00
|
|
|
/// Get exclusive lock
|
|
|
|
pub fn lock_config() -> Result<BackupLockGuard, Error> {
|
|
|
|
open_backup_lockfile(USER_CFG_LOCKFILE, None, true)
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:09:27 +00:00
|
|
|
pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
|
2020-05-28 08:06:44 +00:00
|
|
|
|
2021-01-19 13:04:46 +00:00
|
|
|
let content = proxmox::tools::fs::file_read_optional_string(USER_CFG_FILENAME)?
|
|
|
|
.unwrap_or_else(|| "".to_string());
|
2020-04-06 10:09:27 +00:00
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
let mut data = CONFIG.parse(USER_CFG_FILENAME, &content)?;
|
|
|
|
|
|
|
|
if data.sections.get("root@pam").is_none() {
|
2020-05-19 14:24:54 +00:00
|
|
|
let user: User = User {
|
2020-08-06 13:46:01 +00:00
|
|
|
userid: Userid::root_userid().clone(),
|
2020-05-19 14:24:54 +00:00
|
|
|
comment: Some("Superuser".to_string()),
|
|
|
|
enable: None,
|
|
|
|
expire: None,
|
|
|
|
firstname: None,
|
|
|
|
lastname: None,
|
|
|
|
email: None,
|
|
|
|
};
|
2020-04-06 10:09:27 +00:00
|
|
|
data.set_data("root@pam", "user", &user).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((data, digest))
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:35:57 +00:00
|
|
|
pub fn cached_config() -> Result<Arc<SectionConfigData>, Error> {
|
2020-04-14 11:45:45 +00:00
|
|
|
|
|
|
|
struct ConfigCache {
|
2020-04-15 09:35:57 +00:00
|
|
|
data: Option<Arc<SectionConfigData>>,
|
2020-04-14 11:45:45 +00:00
|
|
|
last_mtime: i64,
|
|
|
|
last_mtime_nsec: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref CACHED_CONFIG: RwLock<ConfigCache> = RwLock::new(
|
|
|
|
ConfigCache { data: None, last_mtime: 0, last_mtime_nsec: 0 });
|
|
|
|
}
|
|
|
|
|
2020-04-29 08:40:42 +00:00
|
|
|
let stat = match nix::sys::stat::stat(USER_CFG_FILENAME) {
|
|
|
|
Ok(stat) => Some(stat),
|
|
|
|
Err(nix::Error::Sys(nix::errno::Errno::ENOENT)) => None,
|
|
|
|
Err(err) => bail!("unable to stat '{}' - {}", USER_CFG_FILENAME, err),
|
|
|
|
};
|
2020-04-14 11:45:45 +00:00
|
|
|
|
2020-04-30 05:04:23 +00:00
|
|
|
{ // limit scope
|
2020-04-14 11:45:45 +00:00
|
|
|
let cache = CACHED_CONFIG.read().unwrap();
|
2020-04-30 05:04:23 +00:00
|
|
|
if let Some(ref config) = cache.data {
|
|
|
|
if let Some(stat) = stat {
|
|
|
|
if stat.st_mtime == cache.last_mtime && stat.st_mtime_nsec == cache.last_mtime_nsec {
|
|
|
|
return Ok(config.clone());
|
|
|
|
}
|
|
|
|
} else if cache.last_mtime == 0 && cache.last_mtime_nsec == 0 {
|
2020-04-15 09:35:57 +00:00
|
|
|
return Ok(config.clone());
|
2020-04-14 11:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:35:57 +00:00
|
|
|
let (config, _digest) = config()?;
|
2020-04-14 11:45:45 +00:00
|
|
|
let config = Arc::new(config);
|
|
|
|
|
|
|
|
let mut cache = CACHED_CONFIG.write().unwrap();
|
2020-04-29 08:40:42 +00:00
|
|
|
if let Some(stat) = stat {
|
|
|
|
cache.last_mtime = stat.st_mtime;
|
|
|
|
cache.last_mtime_nsec = stat.st_mtime_nsec;
|
|
|
|
}
|
2020-04-15 09:35:57 +00:00
|
|
|
cache.data = Some(config.clone());
|
2020-04-14 11:45:45 +00:00
|
|
|
|
2020-04-15 09:35:57 +00:00
|
|
|
Ok(config)
|
2020-04-14 11:45:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:09:27 +00:00
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
|
|
|
let raw = CONFIG.write(USER_CFG_FILENAME, &config)?;
|
2021-09-09 11:14:28 +00:00
|
|
|
replace_backup_config(USER_CFG_FILENAME, raw.as_bytes())?;
|
2020-04-06 10:09:27 +00:00
|
|
|
|
2021-11-12 17:44:28 +00:00
|
|
|
// increase user version
|
2021-06-25 08:52:38 +00:00
|
|
|
// We use this in CachedUserInfo
|
2021-11-12 17:44:28 +00:00
|
|
|
let version_cache = ConfigVersionCache::new()?;
|
|
|
|
version_cache.increase_user_cache_generation();
|
2021-06-25 08:52:38 +00:00
|
|
|
|
2020-04-06 10:09:27 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-10 04:53:53 +00:00
|
|
|
/// Only exposed for testing
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn test_cfg_from_str(raw: &str) -> Result<(SectionConfigData, [u8;32]), Error> {
|
2020-11-02 10:48:09 +00:00
|
|
|
let cfg = init();
|
|
|
|
let parsed = cfg.parse("test_user_cfg", raw)?;
|
|
|
|
|
|
|
|
Ok((parsed, [0;32]))
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:09:27 +00:00
|
|
|
// shell completion helper
|
2020-10-23 11:33:21 +00:00
|
|
|
pub fn complete_userid(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
2020-04-06 10:09:27 +00:00
|
|
|
match config() {
|
2020-10-23 11:33:21 +00:00
|
|
|
Ok((data, _digest)) => {
|
|
|
|
data.sections.iter()
|
|
|
|
.filter_map(|(id, (section_type, _))| {
|
|
|
|
if section_type == "user" {
|
|
|
|
Some(id.to_string())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
},
|
2020-04-06 10:09:27 +00:00
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 11:33:21 +00:00
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
pub fn complete_authid(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
|
|
|
|
Err(_) => vec![],
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 12:49:04 +00:00
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
pub fn complete_token_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
let data = match config() {
|
|
|
|
Ok((data, _digest)) => data,
|
|
|
|
Err(_) => return Vec::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
match param.get("userid") {
|
|
|
|
Some(userid) => {
|
|
|
|
let user = data.lookup::<User>("user", userid);
|
|
|
|
let tokens = data.convert_to_typed_array("token");
|
|
|
|
match (user, tokens) {
|
|
|
|
(Ok(_), Ok(tokens)) => {
|
|
|
|
tokens
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|token: ApiToken| {
|
|
|
|
let tokenid = token.tokenid;
|
|
|
|
if tokenid.is_token() && tokenid.user() == userid {
|
|
|
|
Some(tokenid.tokenname().unwrap().as_str().to_string())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
},
|
|
|
|
_ => vec![],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => vec![],
|
|
|
|
}
|
|
|
|
}
|