Skip to main content

coqui_access_acl/
entry.rs

1//! Entries, ACLs and the ACL of one item, as data a consumer stores.
2
3use alloc::string::String;
4use alloc::vec::Vec;
5
6use coqui_access::Subject;
7use coqui_builder::{Describe, Leaf};
8
9use crate::options::NonDefault;
10use crate::principal::Principal;
11
12/// Whether an entry grants or refuses its rights.
13#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Leaf)]
14#[map(rename_all = "lowercase")]
15pub enum Effect {
16    /// Grants. The default.
17    #[default]
18    Allow,
19    /// Refuses.
20    Deny,
21}
22
23/// How an entry passes to what an item contains, in Windows' terms (NFSv4 uses the same
24/// four flags): `items` is OBJECT_INHERIT, `containers` CONTAINER_INHERIT, `no_propagate`
25/// NO_PROPAGATE_INHERIT and `inherit_only` INHERIT_ONLY. In `MostSpecific` mode,
26/// `containers` is Proxmox's `propagate`.
27#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Describe)]
28#[map(deny_unknown_fields)]
29#[non_exhaustive]
30pub struct Inherit {
31    /// Passes to items (leaves) inside.
32    #[map(default)]
33    pub items: bool,
34    /// Passes to containers inside, and on through them.
35    #[map(default)]
36    pub containers: bool,
37    /// Passes one level only: the copy carries no inheritance of its own.
38    #[map(default)]
39    pub no_propagate: bool,
40    /// Does not apply to the item that holds it, only to what inherits it.
41    #[map(default)]
42    pub inherit_only: bool,
43}
44
45impl Inherit {
46    /// Passing to items, containers, both or neither.
47    pub const fn new(items: bool, containers: bool) -> Self {
48        Self {
49            items,
50            containers,
51            no_propagate: false,
52            inherit_only: false,
53        }
54    }
55
56    /// The same, passing one level only.
57    #[must_use]
58    pub const fn no_propagate(mut self) -> Self {
59        self.no_propagate = true;
60        self
61    }
62
63    /// The same, not applying to the item that holds it.
64    #[must_use]
65    pub const fn inherit_only(mut self) -> Self {
66        self.inherit_only = true;
67        self
68    }
69
70    /// Whether it passes anywhere.
71    pub const fn passes(&self) -> bool {
72        self.items || self.containers
73    }
74}
75
76/// One line of an ACL: who, allow or deny, which rights, and how it is inherited.
77///
78/// `rights` are names a [`RightsSchema`](crate::RightsSchema) defines (rights or aliases),
79/// compiled when the ACL is checked. As configuration the principal is its text:
80///
81/// ```json
82/// { "principal": "group:staff", "effect": "deny", "rights": ["write"],
83///   "inherit": { "items": true, "containers": true } }
84/// ```
85#[derive(Debug, Clone, PartialEq, Eq, Describe)]
86#[map(deny_unknown_fields)]
87#[non_exhaustive]
88pub struct Entry {
89    /// Who it applies to.
90    #[map(as_text)]
91    pub principal: Principal,
92    /// Allow (the default) or deny.
93    #[map(default)]
94    pub effect: Effect,
95    /// The rights and aliases it names.
96    pub rights: Vec<String>,
97    /// How it passes to what the item contains.
98    #[map(default)]
99    pub inherit: Inherit,
100    /// Whether it was copied from a parent ([`Acl::inherited_by`]) rather than set here.
101    #[map(default)]
102    pub inherited: bool,
103    /// A condition the consumer's [`Conditions`](crate::Conditions) decides; none means it
104    /// always applies.
105    pub condition: Option<String>,
106}
107
108impl Entry {
109    /// An entry allowing `rights` to `principal`, inherited by nothing.
110    pub fn allow(
111        principal: impl Into<Principal>,
112        rights: impl IntoIterator<Item = impl Into<String>>,
113    ) -> Self {
114        Self {
115            principal: principal.into(),
116            effect: Effect::Allow,
117            rights: rights.into_iter().map(Into::into).collect(),
118            inherit: Inherit::default(),
119            inherited: false,
120            condition: None,
121        }
122    }
123
124    /// An entry denying `rights` to `principal`, inherited by nothing.
125    pub fn deny(
126        principal: impl Into<Principal>,
127        rights: impl IntoIterator<Item = impl Into<String>>,
128    ) -> Self {
129        Self {
130            effect: Effect::Deny,
131            ..Self::allow(principal, rights)
132        }
133    }
134
135    /// Sets how it is inherited.
136    #[must_use]
137    pub fn inherit(mut self, inherit: Inherit) -> Self {
138        self.inherit = inherit;
139        self
140    }
141
142    /// Marks it as copied from a parent.
143    #[must_use]
144    pub fn inherited(mut self, inherited: bool) -> Self {
145        self.inherited = inherited;
146        self
147    }
148
149    /// Sets its condition.
150    #[must_use]
151    pub fn condition(mut self, condition: impl Into<String>) -> Self {
152        self.condition = Some(condition.into());
153        self
154    }
155}
156
157/// How an ACL's entries combine into a decision.
158#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Leaf)]
159#[map(rename_all = "snake_case")]
160#[non_exhaustive]
161pub enum Mode {
162    /// Windows and NFSv4: entries in order, each right decided by the first entry that
163    /// names it. An allow before a deny wins that right.
164    Ordered,
165    /// Order-free: any applicable deny of a right refuses it, else the union of the allows.
166    /// The default, since no order can be got wrong.
167    #[default]
168    DenyOverrides,
169    /// NetBox, TrueNAS RBAC: the union of the allows; a deny entry is refused.
170    Additive,
171    /// POSIX.1e: the requester's class decides (owner, then named user, then the owning
172    /// and named groups, capped by the mask, then everyone); no deny.
173    Posix,
174    /// Proxmox: a walk from the root down, a deeper level replacing the one above, a
175    /// level's subject entries beating its group entries; no deny, a cancelling name
176    /// refuses everything.
177    MostSpecific,
178}
179
180/// An access control list.
181#[derive(Debug, Clone, Default, PartialEq, Eq, Describe)]
182#[map(deny_unknown_fields)]
183#[non_exhaustive]
184pub struct Acl {
185    /// The entries, in order.
186    pub entries: Vec<Entry>,
187    /// Whether it takes nothing from a parent ([`Acl::inherited_by`]): a protected DACL.
188    #[map(default)]
189    pub protected: bool,
190    /// How the entries combine. Default `deny_overrides`.
191    #[map(default)]
192    pub mode: Mode,
193}
194
195impl Acl {
196    /// An empty ACL in `mode`: it grants nothing.
197    pub fn new(mode: Mode) -> Self {
198        Self {
199            entries: Vec::new(),
200            protected: false,
201            mode,
202        }
203    }
204
205    /// Adds an entry after the others.
206    #[must_use]
207    pub fn entry(mut self, entry: Entry) -> Self {
208        self.entries.push(entry);
209        self
210    }
211
212    /// Sets whether it takes nothing from a parent.
213    #[must_use]
214    pub fn protected(mut self, protected: bool) -> Self {
215        self.protected = protected;
216        self
217    }
218
219    /// Sorts the entries into Windows' canonical order: explicit denies, explicit allows,
220    /// then inherited entries in the order they were. The sort is stable.
221    pub fn canonicalize(&mut self) {
222        self.entries.sort_by_key(|entry| {
223            (
224                entry.inherited,
225                !entry.inherited && entry.effect == Effect::Allow,
226            )
227        });
228    }
229
230    /// What this ACL grants more widely than the default does, for the caller to
231    /// report or refuse: every allow entry naming `Everyone`.
232    pub fn non_defaults(&self) -> Vec<NonDefault> {
233        self.entries
234            .iter()
235            .enumerate()
236            .filter(|(_, entry)| {
237                entry.effect == Effect::Allow
238                    && entry.principal == Principal::Everyone
239                    && !entry.inherit.inherit_only
240            })
241            .map(|(entry, _)| NonDefault::EveryoneAllowed { entry })
242            .collect()
243    }
244}
245
246impl Extend<Entry> for Acl {
247    fn extend<I: IntoIterator<Item = Entry>>(&mut self, entries: I) {
248        self.entries.extend(entries);
249    }
250}
251
252/// The ACL of one item, with the owner and owning group its `Owner` and `OwnerGroup`
253/// entries resolve against. As configuration the subjects are their text.
254#[derive(Debug, Clone, Default, PartialEq, Eq, Describe)]
255#[map(deny_unknown_fields)]
256#[non_exhaustive]
257pub struct ItemAcl {
258    /// The ACL.
259    pub acl: Acl,
260    /// Who owns the item.
261    #[map(as_text)]
262    pub owner: Option<Subject>,
263    /// The item's owning group.
264    #[map(as_text)]
265    pub owner_group: Option<Subject>,
266}
267
268impl ItemAcl {
269    /// The item's ACL, with no owner.
270    pub fn new(acl: Acl) -> Self {
271        Self {
272            acl,
273            owner: None,
274            owner_group: None,
275        }
276    }
277
278    /// Sets the owner.
279    #[must_use]
280    pub fn owner(mut self, owner: Subject) -> Self {
281        self.owner = Some(owner);
282        self
283    }
284
285    /// Sets the owning group.
286    #[must_use]
287    pub fn owner_group(mut self, group: Subject) -> Self {
288        self.owner_group = Some(group);
289        self
290    }
291}