2020-04-16 07:18:56 +00:00
|
|
|
//! Cached user info for fast ACL permission checks
|
|
|
|
|
2020-04-18 06:49:20 +00:00
|
|
|
use std::sync::{RwLock, Arc};
|
2020-04-16 07:18:56 +00:00
|
|
|
|
2020-04-18 06:09:34 +00:00
|
|
|
use anyhow::{Error, bail};
|
2020-04-16 07:18:56 +00:00
|
|
|
|
|
|
|
use proxmox::api::section_config::SectionConfigData;
|
2020-04-18 06:49:20 +00:00
|
|
|
use lazy_static::lazy_static;
|
2020-04-16 07:18:56 +00:00
|
|
|
use proxmox::api::UserInformation;
|
|
|
|
|
2020-04-27 09:21:34 +00:00
|
|
|
use super::acl::{AclTree, ROLE_NAMES, ROLE_ADMIN};
|
2020-10-23 11:33:21 +00:00
|
|
|
use super::user::{ApiToken, User};
|
|
|
|
use crate::api2::types::{Authid, Userid};
|
2020-04-16 07:18:56 +00:00
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
/// Cache User/Group/Token/Acl configuration data for fast permission tests
|
2020-04-16 07:18:56 +00:00
|
|
|
pub struct CachedUserInfo {
|
|
|
|
user_cfg: Arc<SectionConfigData>,
|
|
|
|
acl_tree: Arc<AclTree>,
|
|
|
|
}
|
|
|
|
|
2020-04-18 06:49:20 +00:00
|
|
|
fn now() -> i64 { unsafe { libc::time(std::ptr::null_mut()) } }
|
|
|
|
|
|
|
|
struct ConfigCache {
|
|
|
|
data: Option<Arc<CachedUserInfo>>,
|
|
|
|
last_update: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref CACHED_CONFIG: RwLock<ConfigCache> = RwLock::new(
|
|
|
|
ConfigCache { data: None, last_update: 0 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:18:56 +00:00
|
|
|
impl CachedUserInfo {
|
|
|
|
|
2020-04-18 06:49:20 +00:00
|
|
|
/// Returns a cached instance (up to 5 seconds old).
|
|
|
|
pub fn new() -> Result<Arc<Self>, Error> {
|
|
|
|
let now = now();
|
|
|
|
{ // limit scope
|
|
|
|
let cache = CACHED_CONFIG.read().unwrap();
|
|
|
|
if (now - cache.last_update) < 5 {
|
|
|
|
if let Some(ref config) = cache.data {
|
|
|
|
return Ok(config.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let config = Arc::new(CachedUserInfo {
|
2020-04-16 07:18:56 +00:00
|
|
|
user_cfg: super::user::cached_config()?,
|
|
|
|
acl_tree: super::acl::cached_config()?,
|
2020-04-18 06:49:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut cache = CACHED_CONFIG.write().unwrap();
|
|
|
|
cache.last_update = now;
|
|
|
|
cache.data = Some(config.clone());
|
|
|
|
|
|
|
|
Ok(config)
|
2020-04-16 07:18:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:48:09 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn test_new(user_cfg: SectionConfigData, acl_tree: AclTree) -> Self {
|
|
|
|
Self {
|
|
|
|
user_cfg: Arc::new(user_cfg),
|
|
|
|
acl_tree: Arc::new(acl_tree),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
/// Test if a authentication id is enabled and not expired
|
|
|
|
pub fn is_active_auth_id(&self, auth_id: &Authid) -> bool {
|
|
|
|
let userid = auth_id.user();
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
if let Ok(info) = self.user_cfg.lookup::<User>("user", userid.as_str()) {
|
2020-04-16 07:18:56 +00:00
|
|
|
if !info.enable.unwrap_or(true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if let Some(expire) = info.expire {
|
2020-10-14 12:22:38 +00:00
|
|
|
if expire > 0 && expire <= now() {
|
|
|
|
return false;
|
2020-04-16 07:18:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-23 11:33:21 +00:00
|
|
|
|
|
|
|
if auth_id.is_token() {
|
|
|
|
if let Ok(info) = self.user_cfg.lookup::<ApiToken>("token", &auth_id.to_string()) {
|
|
|
|
if !info.enable.unwrap_or(true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if let Some(expire) = info.expire {
|
|
|
|
if expire > 0 && expire <= now() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-04-16 07:18:56 +00:00
|
|
|
}
|
2020-04-18 06:09:34 +00:00
|
|
|
|
|
|
|
pub fn check_privs(
|
|
|
|
&self,
|
2020-10-23 11:33:21 +00:00
|
|
|
auth_id: &Authid,
|
2020-04-18 06:09:34 +00:00
|
|
|
path: &[&str],
|
|
|
|
required_privs: u64,
|
|
|
|
partial: bool,
|
|
|
|
) -> Result<(), Error> {
|
2020-10-23 11:33:21 +00:00
|
|
|
let privs = self.lookup_privs(&auth_id, path);
|
2020-04-18 06:09:34 +00:00
|
|
|
let allowed = if partial {
|
2020-10-23 11:33:21 +00:00
|
|
|
(privs & required_privs) != 0
|
2020-04-18 06:09:34 +00:00
|
|
|
} else {
|
2020-10-23 11:33:21 +00:00
|
|
|
(privs & required_privs) == required_privs
|
2020-04-18 06:09:34 +00:00
|
|
|
};
|
|
|
|
if !allowed {
|
2020-07-15 06:33:22 +00:00
|
|
|
// printing the path doesn't leaks any information as long as we
|
|
|
|
// always check privilege before resource existence
|
|
|
|
bail!("no permissions on '/{}'", path.join("/"));
|
2020-04-18 06:09:34 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-16 07:18:56 +00:00
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
pub fn is_superuser(&self, auth_id: &Authid) -> bool {
|
|
|
|
!auth_id.is_token() && auth_id.user() == "root@pam"
|
2020-04-16 07:18:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
pub fn is_group_member(&self, _userid: &Userid, _group: &str) -> bool {
|
2020-04-16 07:18:56 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
pub fn lookup_privs(&self, auth_id: &Authid, path: &[&str]) -> u64 {
|
2020-10-08 08:34:07 +00:00
|
|
|
let (privs, _) = self.lookup_privs_details(auth_id, path);
|
|
|
|
privs
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lookup_privs_details(&self, auth_id: &Authid, path: &[&str]) -> (u64, u64) {
|
2020-10-23 11:33:21 +00:00
|
|
|
if self.is_superuser(auth_id) {
|
2020-10-08 08:34:07 +00:00
|
|
|
return (ROLE_ADMIN, ROLE_ADMIN);
|
2020-08-06 13:46:01 +00:00
|
|
|
}
|
2020-04-27 09:21:34 +00:00
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
let roles = self.acl_tree.roles(auth_id, path);
|
2020-04-16 07:18:56 +00:00
|
|
|
let mut privs: u64 = 0;
|
2020-10-08 08:34:07 +00:00
|
|
|
let mut propagated_privs: u64 = 0;
|
|
|
|
for (role, propagate) in roles {
|
2020-04-17 12:03:24 +00:00
|
|
|
if let Some((role_privs, _)) = ROLE_NAMES.get(role.as_str()) {
|
2020-10-08 08:34:07 +00:00
|
|
|
if propagate {
|
|
|
|
propagated_privs |= role_privs;
|
|
|
|
}
|
2020-04-16 07:18:56 +00:00
|
|
|
privs |= role_privs;
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 11:33:21 +00:00
|
|
|
|
|
|
|
if auth_id.is_token() {
|
|
|
|
// limit privs to that of owning user
|
|
|
|
let user_auth_id = Authid::from(auth_id.user().clone());
|
|
|
|
privs &= self.lookup_privs(&user_auth_id, path);
|
2020-10-08 08:34:07 +00:00
|
|
|
let (owner_privs, owner_propagated_privs) = self.lookup_privs_details(&user_auth_id, path);
|
|
|
|
privs &= owner_privs;
|
|
|
|
propagated_privs &= owner_propagated_privs;
|
2020-10-23 11:33:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 08:34:07 +00:00
|
|
|
(privs, propagated_privs)
|
2020-04-16 07:18:56 +00:00
|
|
|
}
|
2020-10-08 08:34:07 +00:00
|
|
|
|
2020-04-16 07:18:56 +00:00
|
|
|
}
|
2020-08-06 13:46:01 +00:00
|
|
|
|
|
|
|
impl UserInformation for CachedUserInfo {
|
|
|
|
fn is_superuser(&self, userid: &str) -> bool {
|
|
|
|
userid == "root@pam"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_group_member(&self, _userid: &str, _group: &str) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
fn lookup_privs(&self, auth_id: &str, path: &[&str]) -> u64 {
|
|
|
|
match auth_id.parse::<Authid>() {
|
|
|
|
Ok(auth_id) => Self::lookup_privs(self, &auth_id, path),
|
2020-08-06 13:46:01 +00:00
|
|
|
Err(_) => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|