2 use std
::collections
::{HashMap, HashSet, BTreeMap, BTreeSet}
;
3 use std
::path
::{PathBuf, Path}
;
4 use std
::sync
::{Arc, RwLock}
;
6 use anyhow
::{bail, Error}
;
8 use lazy_static
::lazy_static
;
10 use proxmox
::tools
::{fs::replace_file, fs::CreateOptions}
;
12 // define Privilege bitfield
14 pub const PRIV_SYS_AUDIT
: u64 = 1 << 0;
15 pub const PRIV_SYS_MODIFY
: u64 = 1 << 1;
16 pub const PRIV_SYS_POWER_MANAGEMENT
: u64 = 1 << 2;
18 pub const PRIV_DATASTORE_AUDIT
: u64 = 1 << 3;
19 pub const PRIV_DATASTORE_MODIFY
: u64 = 1 << 4;
20 pub const PRIV_DATASTORE_READ
: u64 = 1 << 5;
22 /// Datastore.Backup also requires backup ownership
23 pub const PRIV_DATASTORE_BACKUP
: u64 = 1 << 6;
24 /// Datastore.Prune also requires backup ownership
25 pub const PRIV_DATASTORE_PRUNE
: u64 = 1 << 7;
27 pub const PRIV_PERMISSIONS_MODIFY
: u64 = 1 << 8;
29 pub const ROLE_ADMIN
: u64 = std
::u64::MAX
;
30 pub const ROLE_NO_ACCESS
: u64 = 0;
32 pub const ROLE_AUDIT
: u64 =
36 pub const ROLE_DATASTORE_ADMIN
: u64 =
37 PRIV_DATASTORE_AUDIT
|
38 PRIV_DATASTORE_MODIFY
|
40 PRIV_DATASTORE_BACKUP
|
43 pub const ROLE_DATASTORE_USER
: u64 =
44 PRIV_DATASTORE_BACKUP
;
46 pub const ROLE_DATASTORE_AUDIT
: u64 =
49 pub const ROLE_NAME_NO_ACCESS
: &str ="NoAccess";
52 pub static ref ROLE_NAMES
: HashMap
<&'
static str, (u64, &'
static str)> = {
53 let mut map
= HashMap
::new();
63 map
.insert(ROLE_NAME_NO_ACCESS
, (
68 map
.insert("Datastore.Admin", (
70 "Datastore Administrator",
72 map
.insert("Datastore.User", (
76 map
.insert("Datastore.Audit", (
85 pub fn split_acl_path(path
: &str) -> Vec
<&str> {
87 let items
= path
.split('
/'
);
89 let mut components
= vec
![];
92 if name
.is_empty() { continue; }
93 components
.push(name
);
100 pub root
: AclTreeNode
,
103 pub struct AclTreeNode
{
104 pub users
: HashMap
<String
, HashMap
<String
, bool
>>,
105 pub groups
: HashMap
<String
, HashMap
<String
, bool
>>,
106 pub children
: BTreeMap
<String
, AclTreeNode
>,
111 pub fn new() -> Self {
113 users
: HashMap
::new(),
114 groups
: HashMap
::new(),
115 children
: BTreeMap
::new(),
119 pub fn extract_roles(&self, user
: &str, all
: bool
) -> HashSet
<String
> {
120 let user_roles
= self.extract_user_roles(user
, all
);
121 if !user_roles
.is_empty() {
122 // user privs always override group privs
126 self.extract_group_roles(user
, all
)
129 pub fn extract_user_roles(&self, user
: &str, all
: bool
) -> HashSet
<String
> {
131 let mut set
= HashSet
::new();
133 let roles
= match self.users
.get(user
) {
138 for (role
, propagate
) in roles
{
139 if *propagate
|| all
{
140 if role
== ROLE_NAME_NO_ACCESS
{
141 // return a set with a single role 'NoAccess'
142 let mut set
= HashSet
::new();
143 set
.insert(role
.to_string());
146 set
.insert(role
.to_string());
153 pub fn extract_group_roles(&self, _user
: &str, all
: bool
) -> HashSet
<String
> {
155 let mut set
= HashSet
::new();
157 for (_group
, roles
) in &self.groups
{
158 let is_member
= false; // fixme: check if user is member of the group
159 if !is_member { continue; }
161 for (role
, propagate
) in roles
{
162 if *propagate
|| all
{
163 if role
== ROLE_NAME_NO_ACCESS
{
164 // return a set with a single role 'NoAccess'
165 let mut set
= HashSet
::new();
166 set
.insert(role
.to_string());
169 set
.insert(role
.to_string());
177 pub fn delete_group_role(&mut self, group
: &str, role
: &str) {
178 let roles
= match self.groups
.get_mut(group
) {
185 pub fn delete_user_role(&mut self, userid
: &str, role
: &str) {
186 let roles
= match self.users
.get_mut(userid
) {
193 pub fn insert_group_role(&mut self, group
: String
, role
: String
, propagate
: bool
) {
194 let map
= self.groups
.entry(group
).or_insert_with(|| HashMap
::new());
195 if role
== ROLE_NAME_NO_ACCESS
{
197 map
.insert(role
, propagate
);
199 map
.remove(ROLE_NAME_NO_ACCESS
);
200 map
.insert(role
, propagate
);
204 pub fn insert_user_role(&mut self, user
: String
, role
: String
, propagate
: bool
) {
205 let map
= self.users
.entry(user
).or_insert_with(|| HashMap
::new());
206 if role
== ROLE_NAME_NO_ACCESS
{
208 map
.insert(role
, propagate
);
210 map
.remove(ROLE_NAME_NO_ACCESS
);
211 map
.insert(role
, propagate
);
218 pub fn new() -> Self {
219 Self { root: AclTreeNode::new() }
222 fn get_node(&mut self, path
: &[&str]) -> Option
<&mut AclTreeNode
> {
223 let mut node
= &mut self.root
;
225 node
= match node
.children
.get_mut(*comp
) {
233 fn get_or_insert_node(&mut self, path
: &[&str]) -> &mut AclTreeNode
{
234 let mut node
= &mut self.root
;
236 node
= node
.children
.entry(String
::from(*comp
))
237 .or_insert_with(|| AclTreeNode
::new());
242 pub fn delete_group_role(&mut self, path
: &str, group
: &str, role
: &str) {
243 let path
= split_acl_path(path
);
244 let node
= match self.get_node(&path
) {
248 node
.delete_group_role(group
, role
);
251 pub fn delete_user_role(&mut self, path
: &str, userid
: &str, role
: &str) {
252 let path
= split_acl_path(path
);
253 let node
= match self.get_node(&path
) {
257 node
.delete_user_role(userid
, role
);
260 pub fn insert_group_role(&mut self, path
: &str, group
: &str, role
: &str, propagate
: bool
) {
261 let path
= split_acl_path(path
);
262 let node
= self.get_or_insert_node(&path
);
263 node
.insert_group_role(group
.to_string(), role
.to_string(), propagate
);
266 pub fn insert_user_role(&mut self, path
: &str, user
: &str, role
: &str, propagate
: bool
) {
267 let path
= split_acl_path(path
);
268 let node
= self.get_or_insert_node(&path
);
269 node
.insert_user_role(user
.to_string(), role
.to_string(), propagate
);
272 fn write_node_config(
276 ) -> Result
<(), Error
> {
278 let mut role_ug_map0
= HashMap
::new();
279 let mut role_ug_map1
= HashMap
::new();
281 for (user
, roles
) in &node
.users
{
282 // no need to save, because root is always 'Administrator'
283 if user
== "root@pam" { continue; }
284 for (role
, propagate
) in roles
{
285 let role
= role
.as_str();
286 let user
= user
.to_string();
288 role_ug_map1
.entry(role
).or_insert_with(|| BTreeSet
::new())
291 role_ug_map0
.entry(role
).or_insert_with(|| BTreeSet
::new())
297 for (group
, roles
) in &node
.groups
{
298 for (role
, propagate
) in roles
{
299 let group
= format
!("@{}", group
);
301 role_ug_map1
.entry(role
).or_insert_with(|| BTreeSet
::new())
304 role_ug_map0
.entry(role
).or_insert_with(|| BTreeSet
::new())
310 fn group_by_property_list(
311 item_property_map
: &HashMap
<&str, BTreeSet
<String
>>,
312 ) -> BTreeMap
<String
, BTreeSet
<String
>> {
313 let mut result_map
= BTreeMap
::new();
314 for (item
, property_map
) in item_property_map
{
315 let item_list
= property_map
.iter().fold(String
::new(), |mut acc
, v
| {
316 if !acc
.is_empty() { acc.push(','); }
320 result_map
.entry(item_list
).or_insert_with(|| BTreeSet
::new())
321 .insert(item
.to_string());
326 let uglist_role_map0
= group_by_property_list(&role_ug_map0
);
327 let uglist_role_map1
= group_by_property_list(&role_ug_map1
);
329 fn role_list(roles
: &BTreeSet
<String
>) -> String
{
330 if roles
.contains(ROLE_NAME_NO_ACCESS
) { return String::from(ROLE_NAME_NO_ACCESS); }
331 roles
.iter().fold(String
::new(), |mut acc
, v
| {
332 if !acc
.is_empty() { acc.push(','); }
338 for (uglist
, roles
) in &uglist_role_map0
{
339 let role_list
= role_list(roles
);
340 writeln
!(w
, "acl:0:{}:{}:{}", if path
.is_empty() { "/" }
else { path }
, uglist
, role_list
)?
;
343 for (uglist
, roles
) in &uglist_role_map1
{
344 let role_list
= role_list(roles
);
345 writeln
!(w
, "acl:1:{}:{}:{}", if path
.is_empty() { "/" }
else { path }
, uglist
, role_list
)?
;
348 for (name
, child
) in node
.children
.iter() {
349 let child_path
= format
!("{}/{}", path
, name
);
350 Self::write_node_config(child
, &child_path
, w
)?
;
356 pub fn write_config(&self, w
: &mut dyn Write
) -> Result
<(), Error
> {
357 Self::write_node_config(&self.root
, "", w
)
360 fn parse_acl_line(&mut self, line
: &str) -> Result
<(), Error
> {
362 let items
: Vec
<&str> = line
.split('
:'
).collect();
364 if items
.len() != 5 {
365 bail
!("wrong number of items.");
368 if items
[0] != "acl" {
369 bail
!("line does not start with 'acl'.");
372 let propagate
= if items
[1] == "0" {
374 } else if items
[1] == "1" {
377 bail
!("expected '0' or '1' for propagate flag.");
380 let path
= split_acl_path(items
[2]);
381 let node
= self.get_or_insert_node(&path
);
383 let uglist
: Vec
<&str> = items
[3].split('
,'
).map(|v
| v
.trim()).collect();
385 let rolelist
: Vec
<&str> = items
[4].split('
,'
).map(|v
| v
.trim()).collect();
387 for user_or_group
in &uglist
{
388 for role
in &rolelist
{
389 if !ROLE_NAMES
.contains_key(role
) {
390 bail
!("unknown role '{}'", role
);
392 if user_or_group
.starts_with('@'
) {
393 let group
= &user_or_group
[1..];
394 node
.insert_group_role(group
.to_string(), role
.to_string(), propagate
);
396 node
.insert_user_role(user_or_group
.to_string(), role
.to_string(), propagate
);
404 pub fn load(filename
: &Path
) -> Result
<(Self, [u8;32]), Error
> {
405 let mut tree
= Self::new();
407 let raw
= match std
::fs
::read_to_string(filename
) {
410 if err
.kind() == std
::io
::ErrorKind
::NotFound
{
413 bail
!("unable to read acl config {:?} - {}", filename
, err
);
418 let digest
= openssl
::sha
::sha256(raw
.as_bytes());
420 for (linenr
, line
) in raw
.lines().enumerate() {
421 let line
= line
.trim();
422 if line
.is_empty() { continue; }
423 if let Err(err
) = tree
.parse_acl_line(line
) {
424 bail
!("unable to parse acl config {:?}, line {} - {}",
425 filename
, linenr
+1, err
);
432 pub fn from_raw(raw
: &str) -> Result
<Self, Error
> {
433 let mut tree
= Self::new();
434 for (linenr
, line
) in raw
.lines().enumerate() {
435 let line
= line
.trim();
436 if line
.is_empty() { continue; }
437 if let Err(err
) = tree
.parse_acl_line(line
) {
438 bail
!("unable to parse acl config data, line {} - {}", linenr
+1, err
);
444 pub fn roles(&self, userid
: &str, path
: &[&str]) -> HashSet
<String
> {
446 let mut node
= &self.root
;
447 let mut role_set
= node
.extract_roles(userid
, path
.is_empty());
449 for (pos
, comp
) in path
.iter().enumerate() {
450 let last_comp
= (pos
+ 1) == path
.len();
451 node
= match node
.children
.get(*comp
) {
453 None
=> return role_set
, // path not found
455 let new_set
= node
.extract_roles(userid
, last_comp
);
456 if !new_set
.is_empty() {
457 // overwrite previous settings
466 pub const ACL_CFG_FILENAME
: &str = "/etc/proxmox-backup/acl.cfg";
467 pub const ACL_CFG_LOCKFILE
: &str = "/etc/proxmox-backup/.acl.lck";
469 pub fn config() -> Result
<(AclTree
, [u8; 32]), Error
> {
470 let path
= PathBuf
::from(ACL_CFG_FILENAME
);
474 pub fn cached_config() -> Result
<Arc
<AclTree
>, Error
> {
477 data
: Option
<Arc
<AclTree
>>,
479 last_mtime_nsec
: i64,
483 static ref CACHED_CONFIG
: RwLock
<ConfigCache
> = RwLock
::new(
484 ConfigCache { data: None, last_mtime: 0, last_mtime_nsec: 0 }
);
487 let stat
= nix
::sys
::stat
::stat(ACL_CFG_FILENAME
)?
;
490 let cache
= CACHED_CONFIG
.read().unwrap();
491 if stat
.st_mtime
== cache
.last_mtime
&& stat
.st_mtime_nsec
== cache
.last_mtime_nsec
{
492 if let Some(ref config
) = cache
.data
{
493 return Ok(config
.clone());
498 let (config
, _digest
) = config()?
;
499 let config
= Arc
::new(config
);
501 let mut cache
= CACHED_CONFIG
.write().unwrap();
502 cache
.last_mtime
= stat
.st_mtime
;
503 cache
.last_mtime_nsec
= stat
.st_mtime_nsec
;
504 cache
.data
= Some(config
.clone());
509 pub fn save_config(acl
: &AclTree
) -> Result
<(), Error
> {
510 let mut raw
: Vec
<u8> = Vec
::new();
512 acl
.write_config(&mut raw
)?
;
514 let backup_user
= crate::backup
::backup_user()?
;
515 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0640);
516 // set the correct owner/group/permissions while saving file
517 // owner(rw) = root, group(r)= backup
518 let options
= CreateOptions
::new()
520 .owner(nix
::unistd
::ROOT
)
521 .group(backup_user
.gid
);
523 replace_file(ACL_CFG_FILENAME
, &raw
, options
)?
;
538 expected_roles
: &str,
541 let path_vec
= super::split_acl_path(path
);
542 let mut roles
= tree
.roles(user
, &path_vec
)
543 .iter().map(|v
| v
.clone()).collect
::<Vec
<String
>>();
545 let roles
= roles
.join(",");
547 assert_eq
!(roles
, expected_roles
, "\nat check_roles for '{}' on '{}'", user
, path
);
551 fn test_acl_line_compression() -> Result
<(), Error
> {
553 let tree
= AclTree
::from_raw(r
###"
554 acl:0:/store/store2:user1:Admin
555 acl:0:/store/store2:user2:Admin
556 acl:0:/store/store2:user1:Datastore.User
557 acl:0:/store/store2:user2:Datastore.User
560 let mut raw
: Vec
<u8> = Vec
::new();
561 tree
.write_config(&mut raw
)?
;
562 let raw
= std
::str::from_utf8(&raw
)?
;
564 assert_eq
!(raw
, "acl:0:/store/store2:user1,user2:Admin,Datastore.User\n");
570 fn test_roles_1() -> Result
<(), Error
> {
572 let tree
= AclTree
::from_raw(r
###"
573 acl:1:/storage:user1@pbs:Admin
574 acl:1:/storage/store1:user1@pbs:Datastore.User
575 acl:1:/storage/store2:user2@pbs:Datastore.User
577 check_roles(&tree
, "user1@pbs", "/", "");
578 check_roles(&tree
, "user1@pbs", "/storage", "Admin");
579 check_roles(&tree
, "user1@pbs", "/storage/store1", "Datastore.User");
580 check_roles(&tree
, "user1@pbs", "/storage/store2", "Admin");
582 check_roles(&tree
, "user2@pbs", "/", "");
583 check_roles(&tree
, "user2@pbs", "/storage", "");
584 check_roles(&tree
, "user2@pbs", "/storage/store1", "");
585 check_roles(&tree
, "user2@pbs", "/storage/store2", "Datastore.User");
591 fn test_role_no_access() -> Result
<(), Error
> {
593 let tree
= AclTree
::from_raw(r
###"
594 acl:1:/:user1@pbs:Admin
595 acl:1:/storage:user1@pbs:NoAccess
596 acl:1:/storage/store1:user1@pbs:Datastore.User
598 check_roles(&tree
, "user1@pbs", "/", "Admin");
599 check_roles(&tree
, "user1@pbs", "/storage", "NoAccess");
600 check_roles(&tree
, "user1@pbs", "/storage/store1", "Datastore.User");
601 check_roles(&tree
, "user1@pbs", "/storage/store2", "NoAccess");
602 check_roles(&tree
, "user1@pbs", "/system", "Admin");
604 let tree
= AclTree
::from_raw(r
###"
605 acl:1:/:user1@pbs:Admin
606 acl:0:/storage:user1@pbs:NoAccess
607 acl:1:/storage/store1:user1@pbs:Datastore.User
609 check_roles(&tree
, "user1@pbs", "/", "Admin");
610 check_roles(&tree
, "user1@pbs", "/storage", "NoAccess");
611 check_roles(&tree
, "user1@pbs", "/storage/store1", "Datastore.User");
612 check_roles(&tree
, "user1@pbs", "/storage/store2", "Admin");
613 check_roles(&tree
, "user1@pbs", "/system", "Admin");
619 fn test_role_add_delete() -> Result
<(), Error
> {
621 let mut tree
= AclTree
::new();
623 tree
.insert_user_role("/", "user1@pbs", "Admin", true);
624 tree
.insert_user_role("/", "user1@pbs", "Audit", true);
626 check_roles(&tree
, "user1@pbs", "/", "Admin,Audit");
628 tree
.insert_user_role("/", "user1@pbs", "NoAccess", true);
629 check_roles(&tree
, "user1@pbs", "/", "NoAccess");
631 let mut raw
: Vec
<u8> = Vec
::new();
632 tree
.write_config(&mut raw
)?
;
633 let raw
= std
::str::from_utf8(&raw
)?
;
635 assert_eq
!(raw
, "acl:1:/:user1@pbs:NoAccess\n");
641 fn test_no_access_overwrite() -> Result
<(), Error
> {
643 let mut tree
= AclTree
::new();
645 tree
.insert_user_role("/storage", "user1@pbs", "NoAccess", true);
647 check_roles(&tree
, "user1@pbs", "/storage", "NoAccess");
649 tree
.insert_user_role("/storage", "user1@pbs", "Admin", true);
650 tree
.insert_user_role("/storage", "user1@pbs", "Audit", true);
652 check_roles(&tree
, "user1@pbs", "/storage", "Admin,Audit");
654 tree
.insert_user_role("/storage", "user1@pbs", "NoAccess", true);
656 check_roles(&tree
, "user1@pbs", "/storage", "NoAccess");