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 /// Datastore.Admin can do anything on the datastore.
37 pub const ROLE_DATASTORE_ADMIN
: u64 =
38 PRIV_DATASTORE_AUDIT
|
39 PRIV_DATASTORE_MODIFY
|
41 PRIV_DATASTORE_BACKUP
|
44 /// Datastore.Reader can read datastore content an do restore
45 pub const ROLE_DATASTORE_READER
: u64 =
46 PRIV_DATASTORE_AUDIT
|
49 /// Datastore.Backup can do backup and restore, but no prune.
50 pub const ROLE_DATASTORE_BACKUP
: u64 =
51 PRIV_DATASTORE_BACKUP
;
53 /// Datastore.PowerUser can do backup, restore, and prune.
54 pub const ROLE_DATASTORE_POWERUSER
: u64 =
55 PRIV_DATASTORE_PRUNE
|
56 PRIV_DATASTORE_BACKUP
;
58 /// Datastore.Audit can audit the datastore.
59 pub const ROLE_DATASTORE_AUDIT
: u64 =
62 pub const ROLE_NAME_NO_ACCESS
: &str ="NoAccess";
65 pub static ref ROLE_NAMES
: HashMap
<&'
static str, (u64, &'
static str)> = {
66 let mut map
= HashMap
::new();
76 map
.insert(ROLE_NAME_NO_ACCESS
, (
81 map
.insert("Datastore.Admin", (
83 "Datastore Administrator",
85 map
.insert("Datastore.Reader", (
86 ROLE_DATASTORE_READER
,
87 "Datastore Reader (inspect datastore content and do restores)",
89 map
.insert("Datastore.Backup", (
90 ROLE_DATASTORE_BACKUP
,
91 "Datastore Backup (backup and restore owned backups)",
93 map
.insert("Datastore.PowerUser", (
94 ROLE_DATASTORE_POWERUSER
,
95 "Datastore PowerUser (backup, restore and prune owned backup)",
97 map
.insert("Datastore.Audit", (
106 pub fn split_acl_path(path
: &str) -> Vec
<&str> {
108 let items
= path
.split('
/'
);
110 let mut components
= vec
![];
113 if name
.is_empty() { continue; }
114 components
.push(name
);
121 pub root
: AclTreeNode
,
124 pub struct AclTreeNode
{
125 pub users
: HashMap
<String
, HashMap
<String
, bool
>>,
126 pub groups
: HashMap
<String
, HashMap
<String
, bool
>>,
127 pub children
: BTreeMap
<String
, AclTreeNode
>,
132 pub fn new() -> Self {
134 users
: HashMap
::new(),
135 groups
: HashMap
::new(),
136 children
: BTreeMap
::new(),
140 pub fn extract_roles(&self, user
: &str, all
: bool
) -> HashSet
<String
> {
141 let user_roles
= self.extract_user_roles(user
, all
);
142 if !user_roles
.is_empty() {
143 // user privs always override group privs
147 self.extract_group_roles(user
, all
)
150 pub fn extract_user_roles(&self, user
: &str, all
: bool
) -> HashSet
<String
> {
152 let mut set
= HashSet
::new();
154 let roles
= match self.users
.get(user
) {
159 for (role
, propagate
) in roles
{
160 if *propagate
|| all
{
161 if role
== ROLE_NAME_NO_ACCESS
{
162 // return a set with a single role 'NoAccess'
163 let mut set
= HashSet
::new();
164 set
.insert(role
.to_string());
167 set
.insert(role
.to_string());
174 pub fn extract_group_roles(&self, _user
: &str, all
: bool
) -> HashSet
<String
> {
176 let mut set
= HashSet
::new();
178 for (_group
, roles
) in &self.groups
{
179 let is_member
= false; // fixme: check if user is member of the group
180 if !is_member { continue; }
182 for (role
, propagate
) in roles
{
183 if *propagate
|| all
{
184 if role
== ROLE_NAME_NO_ACCESS
{
185 // return a set with a single role 'NoAccess'
186 let mut set
= HashSet
::new();
187 set
.insert(role
.to_string());
190 set
.insert(role
.to_string());
198 pub fn delete_group_role(&mut self, group
: &str, role
: &str) {
199 let roles
= match self.groups
.get_mut(group
) {
206 pub fn delete_user_role(&mut self, userid
: &str, role
: &str) {
207 let roles
= match self.users
.get_mut(userid
) {
214 pub fn insert_group_role(&mut self, group
: String
, role
: String
, propagate
: bool
) {
215 let map
= self.groups
.entry(group
).or_insert_with(|| HashMap
::new());
216 if role
== ROLE_NAME_NO_ACCESS
{
218 map
.insert(role
, propagate
);
220 map
.remove(ROLE_NAME_NO_ACCESS
);
221 map
.insert(role
, propagate
);
225 pub fn insert_user_role(&mut self, user
: String
, role
: String
, propagate
: bool
) {
226 let map
= self.users
.entry(user
).or_insert_with(|| HashMap
::new());
227 if role
== ROLE_NAME_NO_ACCESS
{
229 map
.insert(role
, propagate
);
231 map
.remove(ROLE_NAME_NO_ACCESS
);
232 map
.insert(role
, propagate
);
239 pub fn new() -> Self {
240 Self { root: AclTreeNode::new() }
243 fn get_node(&mut self, path
: &[&str]) -> Option
<&mut AclTreeNode
> {
244 let mut node
= &mut self.root
;
246 node
= match node
.children
.get_mut(*comp
) {
254 fn get_or_insert_node(&mut self, path
: &[&str]) -> &mut AclTreeNode
{
255 let mut node
= &mut self.root
;
257 node
= node
.children
.entry(String
::from(*comp
))
258 .or_insert_with(|| AclTreeNode
::new());
263 pub fn delete_group_role(&mut self, path
: &str, group
: &str, role
: &str) {
264 let path
= split_acl_path(path
);
265 let node
= match self.get_node(&path
) {
269 node
.delete_group_role(group
, role
);
272 pub fn delete_user_role(&mut self, path
: &str, userid
: &str, role
: &str) {
273 let path
= split_acl_path(path
);
274 let node
= match self.get_node(&path
) {
278 node
.delete_user_role(userid
, role
);
281 pub fn insert_group_role(&mut self, path
: &str, group
: &str, role
: &str, propagate
: bool
) {
282 let path
= split_acl_path(path
);
283 let node
= self.get_or_insert_node(&path
);
284 node
.insert_group_role(group
.to_string(), role
.to_string(), propagate
);
287 pub fn insert_user_role(&mut self, path
: &str, user
: &str, role
: &str, propagate
: bool
) {
288 let path
= split_acl_path(path
);
289 let node
= self.get_or_insert_node(&path
);
290 node
.insert_user_role(user
.to_string(), role
.to_string(), propagate
);
293 fn write_node_config(
297 ) -> Result
<(), Error
> {
299 let mut role_ug_map0
= HashMap
::new();
300 let mut role_ug_map1
= HashMap
::new();
302 for (user
, roles
) in &node
.users
{
303 // no need to save, because root is always 'Administrator'
304 if user
== "root@pam" { continue; }
305 for (role
, propagate
) in roles
{
306 let role
= role
.as_str();
307 let user
= user
.to_string();
309 role_ug_map1
.entry(role
).or_insert_with(|| BTreeSet
::new())
312 role_ug_map0
.entry(role
).or_insert_with(|| BTreeSet
::new())
318 for (group
, roles
) in &node
.groups
{
319 for (role
, propagate
) in roles
{
320 let group
= format
!("@{}", group
);
322 role_ug_map1
.entry(role
).or_insert_with(|| BTreeSet
::new())
325 role_ug_map0
.entry(role
).or_insert_with(|| BTreeSet
::new())
331 fn group_by_property_list(
332 item_property_map
: &HashMap
<&str, BTreeSet
<String
>>,
333 ) -> BTreeMap
<String
, BTreeSet
<String
>> {
334 let mut result_map
= BTreeMap
::new();
335 for (item
, property_map
) in item_property_map
{
336 let item_list
= property_map
.iter().fold(String
::new(), |mut acc
, v
| {
337 if !acc
.is_empty() { acc.push(','); }
341 result_map
.entry(item_list
).or_insert_with(|| BTreeSet
::new())
342 .insert(item
.to_string());
347 let uglist_role_map0
= group_by_property_list(&role_ug_map0
);
348 let uglist_role_map1
= group_by_property_list(&role_ug_map1
);
350 fn role_list(roles
: &BTreeSet
<String
>) -> String
{
351 if roles
.contains(ROLE_NAME_NO_ACCESS
) { return String::from(ROLE_NAME_NO_ACCESS); }
352 roles
.iter().fold(String
::new(), |mut acc
, v
| {
353 if !acc
.is_empty() { acc.push(','); }
359 for (uglist
, roles
) in &uglist_role_map0
{
360 let role_list
= role_list(roles
);
361 writeln
!(w
, "acl:0:{}:{}:{}", if path
.is_empty() { "/" }
else { path }
, uglist
, role_list
)?
;
364 for (uglist
, roles
) in &uglist_role_map1
{
365 let role_list
= role_list(roles
);
366 writeln
!(w
, "acl:1:{}:{}:{}", if path
.is_empty() { "/" }
else { path }
, uglist
, role_list
)?
;
369 for (name
, child
) in node
.children
.iter() {
370 let child_path
= format
!("{}/{}", path
, name
);
371 Self::write_node_config(child
, &child_path
, w
)?
;
377 pub fn write_config(&self, w
: &mut dyn Write
) -> Result
<(), Error
> {
378 Self::write_node_config(&self.root
, "", w
)
381 fn parse_acl_line(&mut self, line
: &str) -> Result
<(), Error
> {
383 let items
: Vec
<&str> = line
.split('
:'
).collect();
385 if items
.len() != 5 {
386 bail
!("wrong number of items.");
389 if items
[0] != "acl" {
390 bail
!("line does not start with 'acl'.");
393 let propagate
= if items
[1] == "0" {
395 } else if items
[1] == "1" {
398 bail
!("expected '0' or '1' for propagate flag.");
401 let path
= split_acl_path(items
[2]);
402 let node
= self.get_or_insert_node(&path
);
404 let uglist
: Vec
<&str> = items
[3].split('
,'
).map(|v
| v
.trim()).collect();
406 let rolelist
: Vec
<&str> = items
[4].split('
,'
).map(|v
| v
.trim()).collect();
408 for user_or_group
in &uglist
{
409 for role
in &rolelist
{
410 if !ROLE_NAMES
.contains_key(role
) {
411 bail
!("unknown role '{}'", role
);
413 if user_or_group
.starts_with('@'
) {
414 let group
= &user_or_group
[1..];
415 node
.insert_group_role(group
.to_string(), role
.to_string(), propagate
);
417 node
.insert_user_role(user_or_group
.to_string(), role
.to_string(), propagate
);
425 pub fn load(filename
: &Path
) -> Result
<(Self, [u8;32]), Error
> {
426 let mut tree
= Self::new();
428 let raw
= match std
::fs
::read_to_string(filename
) {
431 if err
.kind() == std
::io
::ErrorKind
::NotFound
{
434 bail
!("unable to read acl config {:?} - {}", filename
, err
);
439 let digest
= openssl
::sha
::sha256(raw
.as_bytes());
441 for (linenr
, line
) in raw
.lines().enumerate() {
442 let line
= line
.trim();
443 if line
.is_empty() { continue; }
444 if let Err(err
) = tree
.parse_acl_line(line
) {
445 bail
!("unable to parse acl config {:?}, line {} - {}",
446 filename
, linenr
+1, err
);
453 pub fn from_raw(raw
: &str) -> Result
<Self, Error
> {
454 let mut tree
= Self::new();
455 for (linenr
, line
) in raw
.lines().enumerate() {
456 let line
= line
.trim();
457 if line
.is_empty() { continue; }
458 if let Err(err
) = tree
.parse_acl_line(line
) {
459 bail
!("unable to parse acl config data, line {} - {}", linenr
+1, err
);
465 pub fn roles(&self, userid
: &str, path
: &[&str]) -> HashSet
<String
> {
467 let mut node
= &self.root
;
468 let mut role_set
= node
.extract_roles(userid
, path
.is_empty());
470 for (pos
, comp
) in path
.iter().enumerate() {
471 let last_comp
= (pos
+ 1) == path
.len();
472 node
= match node
.children
.get(*comp
) {
474 None
=> return role_set
, // path not found
476 let new_set
= node
.extract_roles(userid
, last_comp
);
477 if !new_set
.is_empty() {
478 // overwrite previous settings
487 pub const ACL_CFG_FILENAME
: &str = "/etc/proxmox-backup/acl.cfg";
488 pub const ACL_CFG_LOCKFILE
: &str = "/etc/proxmox-backup/.acl.lck";
490 pub fn config() -> Result
<(AclTree
, [u8; 32]), Error
> {
491 let path
= PathBuf
::from(ACL_CFG_FILENAME
);
495 pub fn cached_config() -> Result
<Arc
<AclTree
>, Error
> {
498 data
: Option
<Arc
<AclTree
>>,
500 last_mtime_nsec
: i64,
504 static ref CACHED_CONFIG
: RwLock
<ConfigCache
> = RwLock
::new(
505 ConfigCache { data: None, last_mtime: 0, last_mtime_nsec: 0 }
);
508 let stat
= nix
::sys
::stat
::stat(ACL_CFG_FILENAME
)?
;
511 let cache
= CACHED_CONFIG
.read().unwrap();
512 if stat
.st_mtime
== cache
.last_mtime
&& stat
.st_mtime_nsec
== cache
.last_mtime_nsec
{
513 if let Some(ref config
) = cache
.data
{
514 return Ok(config
.clone());
519 let (config
, _digest
) = config()?
;
520 let config
= Arc
::new(config
);
522 let mut cache
= CACHED_CONFIG
.write().unwrap();
523 cache
.last_mtime
= stat
.st_mtime
;
524 cache
.last_mtime_nsec
= stat
.st_mtime_nsec
;
525 cache
.data
= Some(config
.clone());
530 pub fn save_config(acl
: &AclTree
) -> Result
<(), Error
> {
531 let mut raw
: Vec
<u8> = Vec
::new();
533 acl
.write_config(&mut raw
)?
;
535 let backup_user
= crate::backup
::backup_user()?
;
536 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0640);
537 // set the correct owner/group/permissions while saving file
538 // owner(rw) = root, group(r)= backup
539 let options
= CreateOptions
::new()
541 .owner(nix
::unistd
::ROOT
)
542 .group(backup_user
.gid
);
544 replace_file(ACL_CFG_FILENAME
, &raw
, options
)?
;
559 expected_roles
: &str,
562 let path_vec
= super::split_acl_path(path
);
563 let mut roles
= tree
.roles(user
, &path_vec
)
564 .iter().map(|v
| v
.clone()).collect
::<Vec
<String
>>();
566 let roles
= roles
.join(",");
568 assert_eq
!(roles
, expected_roles
, "\nat check_roles for '{}' on '{}'", user
, path
);
572 fn test_acl_line_compression() -> Result
<(), Error
> {
574 let tree
= AclTree
::from_raw(r
###"
575 acl:0:/store/store2:user1:Admin
576 acl:0:/store/store2:user2:Admin
577 acl:0:/store/store2:user1:Datastore.Backup
578 acl:0:/store/store2:user2:Datastore.Backup
581 let mut raw
: Vec
<u8> = Vec
::new();
582 tree
.write_config(&mut raw
)?
;
583 let raw
= std
::str::from_utf8(&raw
)?
;
585 assert_eq
!(raw
, "acl:0:/store/store2:user1,user2:Admin,Datastore.Backup\n");
591 fn test_roles_1() -> Result
<(), Error
> {
593 let tree
= AclTree
::from_raw(r
###"
594 acl:1:/storage:user1@pbs:Admin
595 acl:1:/storage/store1:user1@pbs:Datastore.Backup
596 acl:1:/storage/store2:user2@pbs:Datastore.Backup
598 check_roles(&tree
, "user1@pbs", "/", "");
599 check_roles(&tree
, "user1@pbs", "/storage", "Admin");
600 check_roles(&tree
, "user1@pbs", "/storage/store1", "Datastore.Backup");
601 check_roles(&tree
, "user1@pbs", "/storage/store2", "Admin");
603 check_roles(&tree
, "user2@pbs", "/", "");
604 check_roles(&tree
, "user2@pbs", "/storage", "");
605 check_roles(&tree
, "user2@pbs", "/storage/store1", "");
606 check_roles(&tree
, "user2@pbs", "/storage/store2", "Datastore.Backup");
612 fn test_role_no_access() -> Result
<(), Error
> {
614 let tree
= AclTree
::from_raw(r
###"
615 acl:1:/:user1@pbs:Admin
616 acl:1:/storage:user1@pbs:NoAccess
617 acl:1:/storage/store1:user1@pbs:Datastore.Backup
619 check_roles(&tree
, "user1@pbs", "/", "Admin");
620 check_roles(&tree
, "user1@pbs", "/storage", "NoAccess");
621 check_roles(&tree
, "user1@pbs", "/storage/store1", "Datastore.Backup");
622 check_roles(&tree
, "user1@pbs", "/storage/store2", "NoAccess");
623 check_roles(&tree
, "user1@pbs", "/system", "Admin");
625 let tree
= AclTree
::from_raw(r
###"
626 acl:1:/:user1@pbs:Admin
627 acl:0:/storage:user1@pbs:NoAccess
628 acl:1:/storage/store1:user1@pbs:Datastore.Backup
630 check_roles(&tree
, "user1@pbs", "/", "Admin");
631 check_roles(&tree
, "user1@pbs", "/storage", "NoAccess");
632 check_roles(&tree
, "user1@pbs", "/storage/store1", "Datastore.Backup");
633 check_roles(&tree
, "user1@pbs", "/storage/store2", "Admin");
634 check_roles(&tree
, "user1@pbs", "/system", "Admin");
640 fn test_role_add_delete() -> Result
<(), Error
> {
642 let mut tree
= AclTree
::new();
644 tree
.insert_user_role("/", "user1@pbs", "Admin", true);
645 tree
.insert_user_role("/", "user1@pbs", "Audit", true);
647 check_roles(&tree
, "user1@pbs", "/", "Admin,Audit");
649 tree
.insert_user_role("/", "user1@pbs", "NoAccess", true);
650 check_roles(&tree
, "user1@pbs", "/", "NoAccess");
652 let mut raw
: Vec
<u8> = Vec
::new();
653 tree
.write_config(&mut raw
)?
;
654 let raw
= std
::str::from_utf8(&raw
)?
;
656 assert_eq
!(raw
, "acl:1:/:user1@pbs:NoAccess\n");
662 fn test_no_access_overwrite() -> Result
<(), Error
> {
664 let mut tree
= AclTree
::new();
666 tree
.insert_user_role("/storage", "user1@pbs", "NoAccess", true);
668 check_roles(&tree
, "user1@pbs", "/storage", "NoAccess");
670 tree
.insert_user_role("/storage", "user1@pbs", "Admin", true);
671 tree
.insert_user_role("/storage", "user1@pbs", "Audit", true);
673 check_roles(&tree
, "user1@pbs", "/storage", "Admin,Audit");
675 tree
.insert_user_role("/storage", "user1@pbs", "NoAccess", true);
677 check_roles(&tree
, "user1@pbs", "/storage", "NoAccess");