Skip to main content

coqui_access_acl/
options.rs

1//! Bounds and choices an evaluation runs with, and the non-default settings they amount to.
2
3use alloc::vec::Vec;
4
5/// Bounds on work and memory. Each is `Some(n)` by default and `None` lifts it; none is a
6/// security property.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct Limits {
10    /// The most entries one ACL may hold. Default 1 024.
11    pub entries: Option<usize>,
12    /// The most rights (bits) a schema may define. Default 1 024.
13    pub rights: Option<usize>,
14    /// The most levels a walk goes through, the item included. Default 64.
15    pub depth: Option<usize>,
16}
17
18impl Limits {
19    /// The default entries bound.
20    pub const DEFAULT_ENTRIES: usize = 1_024;
21    /// The default rights bound.
22    pub const DEFAULT_RIGHTS: usize = 1_024;
23    /// The default depth bound.
24    pub const DEFAULT_DEPTH: usize = 64;
25
26    /// No bound at all.
27    pub const fn unbounded() -> Self {
28        Self {
29            entries: None,
30            rights: None,
31            depth: None,
32        }
33    }
34
35    /// Sets [`entries`](Limits::entries).
36    #[must_use]
37    pub const fn entries(mut self, max: Option<usize>) -> Self {
38        self.entries = max;
39        self
40    }
41
42    /// Sets [`rights`](Limits::rights).
43    #[must_use]
44    pub const fn rights(mut self, max: Option<usize>) -> Self {
45        self.rights = max;
46        self
47    }
48
49    /// Sets [`depth`](Limits::depth).
50    #[must_use]
51    pub const fn depth(mut self, max: Option<usize>) -> Self {
52        self.depth = max;
53        self
54    }
55}
56
57impl Default for Limits {
58    fn default() -> Self {
59        Self {
60            entries: Some(Self::DEFAULT_ENTRIES),
61            rights: Some(Self::DEFAULT_RIGHTS),
62            depth: Some(Self::DEFAULT_DEPTH),
63        }
64    }
65}
66
67/// What an item with no ACL at all gets.
68#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
69pub enum OnMissing {
70    /// Nothing. The default.
71    #[default]
72    Deny,
73    /// Every right, as a Windows NULL DACL grants. A non-default setting ([`NonDefault::MissingAllowsAll`]).
74    AllowAll,
75}
76
77/// The choices one evaluation runs with.
78#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
79#[non_exhaustive]
80pub struct Options {
81    /// The bounds.
82    pub limits: Limits,
83    /// What an item with no ACL gets. Default [`OnMissing::Deny`].
84    pub on_missing: OnMissing,
85}
86
87impl Options {
88    /// The defaults.
89    pub fn new() -> Self {
90        Self::default()
91    }
92
93    /// Sets the bounds.
94    #[must_use]
95    pub fn limits(mut self, limits: Limits) -> Self {
96        self.limits = limits;
97        self
98    }
99
100    /// Sets what an item with no ACL gets.
101    #[must_use]
102    pub fn on_missing(mut self, on_missing: OnMissing) -> Self {
103        self.on_missing = on_missing;
104        self
105    }
106
107    /// What these choices set off the default, for the caller to report or refuse.
108    pub fn non_defaults(&self) -> Vec<NonDefault> {
109        match self.on_missing {
110            OnMissing::AllowAll => alloc::vec![NonDefault::MissingAllowsAll],
111            OnMissing::Deny => Vec::new(),
112        }
113    }
114}
115
116/// A choice that grants more than the default does, reported as data.
117#[derive(Debug, Clone, Copy, PartialEq, Eq)]
118#[non_exhaustive]
119pub enum NonDefault {
120    /// [`OnMissing::AllowAll`]: an item with no ACL grants every right.
121    MissingAllowsAll,
122    /// The entry at this index allows something to `Everyone`, signed in or not.
123    EveryoneAllowed {
124        /// The entry.
125        entry: usize,
126    },
127}