karen
Escalate to your manager and get root access!
This is an extended fork of the sudo and elevate crates, which is a simple library to restart your process with sudo to escalate privileges.
This fork is a refactor of the original version, with the following changes:
- A builder pattern for the
Elevatestruct - An ability to use
pkexecorpolkitas an alternative tosudoby setting the wrapper from the builder
The API is a superset of the original sudo crate, so you can use it as a drop-in replacement, but you can also use the new builder pattern to set your own options (currently only wrapper is supported)
The original sudo crate can be found on GitLab (crates.io).
Detect if you are running as root, restart self with sudo if needed or setup uid zero when running with the SUID flag set.
Requirements
- Unix-like operating system
- The intended wrapper (sudo, pkexec, polkit) must be installed and in the PATH. The default is
sudo. - Linux or Mac OS X tested
- It should work on *BSD. You may want to use
doasinstead ofsudoon OpenBSD using the new builder pattern.
- It should work on *BSD. You may want to use
Example:
First, add karen to your Cargo.toml:
karen = "0.6.1"
In your main.rs:
If you are using logging based on the log infrastructure you will get timestamped and formatted output.
Passing RUST_BACKTRACE
The crate will automatically keep the setting of RUST_BACKTRACE intact if it is set to one of the following values:
- `` <- empty string means no pass-through
1ortrue<- standard tracefull<- full trace
Keeping part of the environment
You can keep parts of your environment across the sudo barrier. This enables more configuration options often used in daemons or cloud environments:
// keeping all environment variables starting with "EXAMPLE_" or "CARGO"
with_env.expect;
Warning: This may introduce security problems to your application if untrusted users are able to set these variables.
; ;
; ;
Run a program with SUID
; ;
uid=1000()) groups=1000()))
; ;
uid=0()) groups=0()
; ;
uid=0()) groups=0()
Then give the file to root and add the suid flag.
Now run the program again:
; ;
uid=1000()) euid=0())))
)
; ;
uid=0()) groups=1000()))