use std::ffi::OsString;
use std::path::PathBuf;
use zeroize::Zeroize;
#[cfg_attr(not(unix), allow(dead_code))]
pub(crate) mod macos;
pub(crate) mod plan;
#[cfg(unix)]
#[path = "elevation/posix.rs"]
pub(crate) mod posix;
pub(crate) mod sanitize;
#[cfg(windows)]
#[path = "elevation/windows.rs"]
pub(crate) mod windows;
#[cfg(unix)]
#[doc(hidden)]
pub use posix::controlling_terminal_present;
pub use sanitize::EnvSanitizer;
pub fn is_elevated() -> bool {
#[cfg(unix)]
{
posix::is_elevated()
}
#[cfg(windows)]
{
windows::is_elevated()
}
#[cfg(not(any(unix, windows)))]
{
false
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Backend {
#[default]
Auto,
Run0,
Sudo,
Doas,
Pkexec,
}
#[derive(Debug, Clone, Default)]
pub enum Auth {
#[default]
Interactive,
NonInteractive,
Askpass(PathBuf),
Stdin(Secret),
Gui,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ElevatedStdio {
Passthrough,
OwnConsole,
StdinConsumed,
OsascriptRelay,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ElevatedVia {
Wrapped(Backend),
WindowsUac,
MacosOsascript,
AlreadyElevated,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Privilege {
Unprivileged,
Elevated,
}
#[derive(Debug, Clone)]
pub struct ElevationReport {
pub via: ElevatedVia,
pub stripped_env: Vec<OsString>,
pub stdio: ElevatedStdio,
}
#[derive(Debug)]
pub(crate) struct ElevationRequest {
pub enabled: bool,
pub backend: Backend,
pub auth: Auth,
pub sanitizer: EnvSanitizer,
}
impl Default for ElevationRequest {
fn default() -> ElevationRequest {
ElevationRequest {
enabled: false,
backend: Backend::Auto,
auth: Auth::Interactive,
sanitizer: EnvSanitizer::default(),
}
}
}
#[cfg_attr(not(windows), allow(dead_code))]
pub(crate) fn already_elevated_report(stdio: ElevatedStdio) -> ElevationReport {
ElevationReport {
via: ElevatedVia::AlreadyElevated,
stripped_env: Vec::new(),
stdio,
}
}
pub(crate) fn map_elevated_kill_error(err: std::io::Error, elevated_wrapper: bool) -> crate::error::Error {
use crate::error::{ElevationErrorKind, Error};
if elevated_wrapper && err.kind() == std::io::ErrorKind::PermissionDenied {
Error::Elevation {
kind: ElevationErrorKind::Unkillable,
detail: format!("could not signal the elevated child: {err}"),
}
} else {
Error::Io(err)
}
}
#[cfg(unix)]
fn backend_unusable(backend_path: &std::path::Path) -> bool {
use std::os::unix::ffi::OsStrExt;
let Ok(cpath) = std::ffi::CString::new(backend_path.as_os_str().as_bytes()) else {
return true; };
unsafe { libc::faccessat(libc::AT_FDCWD, cpath.as_ptr(), libc::X_OK, libc::AT_EACCESS) != 0 }
}
#[allow(dead_code)]
#[cfg(not(unix))]
fn backend_unusable(backend_path: &std::path::Path) -> bool {
!backend_path.exists()
}
#[cfg_attr(not(unix), allow(dead_code))]
pub(crate) fn remap_derived_spawn_error(
err: crate::error::Error,
backend_path: &std::path::Path,
) -> crate::error::Error {
use crate::error::{ElevationErrorKind, Error};
match err {
Error::Io(e)
if matches!(
e.kind(),
std::io::ErrorKind::NotFound | std::io::ErrorKind::PermissionDenied
) && backend_unusable(backend_path) =>
{
Error::Elevation {
kind: ElevationErrorKind::BackendUnavailable,
detail: format!(
"elevation backend {} could not be executed: {e}",
backend_path.display()
),
}
}
other => other,
}
}
#[derive(Clone)]
pub struct Secret(Vec<u8>);
impl Secret {
pub fn new(bytes: impl Into<Vec<u8>>) -> Secret {
Secret(bytes.into())
}
pub fn expose(&self) -> &[u8] {
&self.0
}
}
impl std::fmt::Debug for Secret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Secret(<redacted>)")
}
}
impl Drop for Secret {
fn drop(&mut self) {
self.0.zeroize();
}
}
#[cfg(test)]
#[path = "elevation_tests.rs"]
mod elevation_tests;