sudo2
Changelogs
Following changes were made to the crate karen
which is a fork of sudo
and
elevate
.
- Adds support for wildcard. It is possible to select all environment variables
with using
sudo2::with_env_wildcards(&["*"])
(mimicssudo -E
). - Adds a few internal functions.
sudo2::running_as_root
returntrue
if process already running asroot
. - Adds
rustfmt.toml
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
Elevate
struct - An ability to use
pkexec
orpolkit
as an alternative tosudo
by 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
doas
instead ofsudo
on OpenBSD using the new builder pattern.
- It should work on *BSD. You may want to use
Example:
First, add sudo to your Cargo.toml
:
sudo = "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
1
ortrue
<- 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() ) )