#sudo #root #running #uid #flags #restart #suid

elevate

检测是否以root身份运行,如需则使用sudo重启自身,或在设置SUID标志时设置uid为零

1个不稳定版本

0.6.1 2023年5月28日

#919开发工具

Download history 4/week @ 2024-03-13 12/week @ 2024-03-20 38/week @ 2024-03-27 118/week @ 2024-04-03 23/week @ 2024-04-10 4/week @ 2024-04-17 6/week @ 2024-04-24 47/week @ 2024-05-01 45/week @ 2024-05-08 22/week @ 2024-05-15 34/week @ 2024-05-22 57/week @ 2024-05-29 59/week @ 2024-06-05 70/week @ 2024-06-12 416/week @ 2024-06-19 66/week @ 2024-06-26

640每月下载量

MIT/Apache

11KB
98

sudo

crates.io docs.rs

检测是否以root身份运行,如需则使用sudo重启自身,或在设置SUID标志时设置uid为零。

要求

  • 需要在目标系统上安装并正确设置sudo程序。
  • 已测试Linux或Mac OS X
    • 它应该在*BSD上运行。但是,尚未测试。

示例

首先,将sudo添加到您的Cargo.toml

[dependencies]
sudo = "0.6"

在您的main.rs

fn main() -> Result<(), Box<dyn Error>> {
    sudo::escalate_if_needed()?;
    println!("Hello, Root-World!");
    Ok( () )
}

如果您使用的是基于log基础设施的日志,您将获得带时间戳和格式的输出。

传递RUST_BACKTRACE

如果RUST_BACKTRACE设置为以下值之一,则该包将自动保留设置

  • `` <- 空字符串表示不传递
  • 1true <- 标准跟踪
  • full <- 完整跟踪
$ RUST_BACKTRACE=full cargo run --example backtrace
2020-07-05 18:10:31,544 TRACE [sudo] Running as User
2020-07-05 18:10:31,544 DEBUG [sudo] Escalating privileges
2020-07-05 18:10:31,544 TRACE [sudo] relaying RUST_BACKTRACE=full
[sudo] Passwort für user:
2020-07-05 18:10:39,238 TRACE [sudo] Running as Root
2020-07-05 18:10:39,238 TRACE [sudo] already running as Root
2020-07-05 18:10:39,238 INFO  [backtrace] entering failing_function
thread 'main' panicked at 'now you see me fail', examples/backtrace.rs:16:5

保留环境的一部分

您可以在sudo屏障之间保留您环境的一部分。这允许更多的配置选项,通常用于守护进程或云环境

    // keeping all environment variables starting with "EXAMPLE_" or "CARGO"
    sudo::with_env(&["EXAMPLE_", "CARGO"]).expect("sudo failed");

警告:如果不受信任的用户能够设置这些变量,这可能会向您的应用程序引入安全问题。

$ EXAMPLE_EXEC='$(ls)' EXAMPLE_BTICKS='`ls`' cargo run --example environment
2020-07-07 16:32:11,261 INFO  [environment] ① uid: 1000; euid: 1000;

...

declare -x EXAMPLE_BTICKS="\`ls\`"
declare -x EXAMPLE_EXEC="\$(ls)"
...

[sudo] password for user:

2020-07-07 16:32:11,285 TRACE [sudo] Running as Root
2020-07-07 16:32:11,285 TRACE [sudo] already running as Root
2020-07-07 16:32:11,285 INFO  [environment] ② uid: 0; euid: 0;

...

declare -x EXAMPLE_BTICKS="\`ls\`"
declare -x EXAMPLE_EXEC="\$(ls)"

以SUID运行程序

$ cargo run --example suid
2020-04-17 15:13:49,450 INFO  [suid] ① uid: 1000; euid: 1000;
uid=1000(user) gid=1000(user) groups=1000(user),4(adm),27(sudo)
2020-04-17 15:13:49,453 TRACE [sudo] Running as User
2020-04-17 15:13:49,453 DEBUG [sudo] Escalating privileges
[sudo] password for user: 
2020-04-17 15:13:53,529 INFO  [suid] ① uid: 0; euid: 0;
uid=0(root) gid=0(root) groups=0(root)
2020-04-17 15:13:53,532 TRACE [sudo] Running as Root
2020-04-17 15:13:53,532 TRACE [sudo] already running as Root
2020-04-17 15:13:53,532 INFO  [suid] ② uid: 0; euid: 0;
uid=0(root) gid=0(root) groups=0(root)

然后将文件给root并添加suid标志。

$ sudo chown root target/debug/examples/suid
$ sudo chmod 4755 target/debug/examples/suid

现在再次运行程序

$ target/debug/examples/suid
2020-04-17 15:14:37,199 INFO  [suid] ① uid: 1000; euid: 0;
uid=1000(user) gid=1000(user) euid=0(root) groups=1000(user),4(adm),27(sudo)
2020-04-17 15:14:37,202 TRACE [sudo] Running as Suid
2020-04-17 15:14:37,202 TRACE [sudo] setuid(0)
2020-04-17 15:14:37,202 INFO  [suid] ② uid: 0; euid: 0;
uid=0(root) gid=1000(user) groups=1000(user),4(adm),27(sudo)

依赖项

~130KB