Debian Codegraph @

examples/clear_permitted.rs

raw symbols SCIP
1 //! A simple example showing how to manipulate capabilities.
2 //!
3 //! It clears Permitted set to show its interaction
4 //! with Effective set.
5 //!
6 //! This is an example ONLY: do NOT panic/unwrap/assert
7 //! in production code!
8
9 type ExResult<T> = Result<T, Box<dyn std::error::Error + 'static>>;
10
11 fn main() -> ExResult<()> {
12 use caps::{CapSet, Capability};
13
14 // Check if `CAP_CHOWN` was originally available.
15 let cur = caps::read(None, CapSet::Permitted)?;
16 println!("-> Current permitted caps: {:?}.", cur);
17 let cur = caps::read(None, CapSet::Effective)?;
18 println!("-> Current effective caps: {:?}.", cur);
19 let perm_chown = caps::has_cap(None, CapSet::Permitted, Capability::CAP_CHOWN);
20 assert!(perm_chown.is_ok());
21 if !perm_chown? {
22 return Err(
23 "Try running this again as root/sudo or with CAP_CHOWN file capability!".into(),
24 );
25 }
26
27 // Clear all effective caps.
28 let r = caps::clear(None, CapSet::Effective);
29 assert!(r.is_ok());
30 println!("Cleared effective caps.");
31 let cur = caps::read(None, CapSet::Effective)?;
32 println!("-> Current effective caps: {:?}.", cur);
33
34 // Since `CAP_CHOWN` is still in permitted, it can be raised again.
35 let r = caps::raise(None, CapSet::Effective, Capability::CAP_CHOWN);
36 assert!(r.is_ok());
37 println!("Raised CAP_CHOWN in effective set.");
38 let cur = caps::read(None, CapSet::Effective)?;
39 println!("-> Current effective caps: {:?}.", cur);
40
41 // Clearing Permitted also impacts effective.
42 let r = caps::clear(None, CapSet::Permitted);
43 assert!(r.is_ok());
44 println!("Cleared permitted caps.");
45 let cur = caps::read(None, CapSet::Permitted)?;
46 println!("-> Current permitted caps: {:?}.", cur);
47 let cur = caps::read(None, CapSet::Effective)?;
48 println!("-> Current effective caps: {:?}.", cur);
49
50 // Trying to raise `CAP_CHOWN` now fails.
51 let r = caps::raise(None, CapSet::Effective, Capability::CAP_CHOWN);
52 assert!(r.is_err());
53 println!("Tried to raise CAP_CHOWN but failed.");
54 let cur = caps::read(None, CapSet::Effective)?;
55 println!("-> Current effective caps: {:?}.", cur);
56
57 Ok(())
58 }