#capabilities #posix #linux #setcap #getcap

caps

一个用于操作 Linux 能力的纯 Rust 库

18 次发布

0.5.5 2022 年 11 月 2 日
0.5.3 2021 年 9 月 27 日
0.5.2 2021 年 5 月 10 日
0.5.1 2021 年 2 月 15 日
0.0.1 2017 年 2 月 7 日

#11 in Unix APIs

Download history 40550/week @ 2024-04-22 33702/week @ 2024-04-29 32609/week @ 2024-05-06 35156/week @ 2024-05-13 31223/week @ 2024-05-20 32706/week @ 2024-05-27 36965/week @ 2024-06-03 38215/week @ 2024-06-10 35084/week @ 2024-06-17 38118/week @ 2024-06-24 33144/week @ 2024-07-01 36236/week @ 2024-07-08 38004/week @ 2024-07-15 39826/week @ 2024-07-22 41332/week @ 2024-07-29 55664/week @ 2024-08-05

每月下载量 176,803
546 个crate中使用 (直接使用 30)

MIT/Apache

40KB
782 行代码 (不含注释)

caps

Build Status crates.io Documentation

一个用于操作 Linux 能力的纯 Rust 库。

caps 提供了对现代 Linux 内核中可用的能力的支持。它支持传统的 POSIX 集合(有效、可继承、允许)以及 Linux 特有的环境能力和边界能力集合。

caps 提供了一个简单且符合 Rust 习惯的接口来处理 Linux 上的能力。有关更多详细信息,请参阅 capabilities(7)

动机

这个库试图实现以下目标:

  • 全面支持现代内核,包括最新的能力和集合
  • 提供符合习惯的接口
  • 在静态目标中使用,无需外部 C 库

示例

type ExResult<T> = Result<T, Box<dyn std::error::Error + 'static>>;

fn manipulate_caps() -> ExResult<()> {
    use caps::{Capability, CapSet};

    // Retrieve permitted set.
    let cur = caps::read(None, CapSet::Permitted)?;
    println!("Current permitted caps: {:?}.", cur);
    
    // Retrieve effective set.
    let cur = caps::read(None, CapSet::Effective)?;
    println!("Current effective caps: {:?}.", cur);
    
    // Check if CAP_CHOWN is in permitted set.
    let perm_chown = caps::has_cap(None, CapSet::Permitted, Capability::CAP_CHOWN)?;
    if !perm_chown {
        return Err("Try running this as root!".into());
    }

    // Clear all effective caps.
    caps::clear(None, CapSet::Effective)?;
    println!("Cleared effective caps.");
    let cur = caps::read(None, CapSet::Effective)?;
    println!("Current effective caps: {:?}.", cur);

    // Since `CAP_CHOWN` is still in permitted, it can be raised again.
    caps::raise(None, CapSet::Effective, Capability::CAP_CHOWN)?;
    println!("Raised CAP_CHOWN in effective set.");
    let cur = caps::read(None, CapSet::Effective)?;
    println!("Current effective caps: {:?}.", cur);

    Ok(())
}

更多示例可在 示例 下找到。

许可证

许可方式任选以下之一:

根据您的选择。

依赖项

~0.3–0.8MB
~19K SLoC