use std::io;
use std::path::PathBuf;
use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("{operation} requires root privileges (effective uid is {euid})")]
NeedsRoot {
operation: &'static str,
euid: u32,
},
#[error(
"This command requires root privileges, but automatic escalation was disabled by --no-escalate (effective uid is {euid})"
)]
EscalationDisabled {
euid: u32,
},
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("Failed to read config file '{path}': {source}")]
ConfigRead { path: PathBuf, source: io::Error },
#[error("Failed to parse config: {0}")]
ConfigParse(#[from] toml::de::Error),
#[error("Config validation failed: {0}")]
ConfigValidation(String),
#[error("Unknown dependency '{0}' - jail not defined")]
UnknownDependency(String),
#[error("Jail '{0}' not found")]
JailNotFound(String),
#[error("Jail '{0}' is already running")]
JailAlreadyRunning(String),
#[error("Jail '{0}' is not running")]
JailNotRunning(String),
#[error("Jail '{0}' already exists")]
JailExists(String),
#[error("Jail creation failed: {0}")]
JailCreationFailed(String),
#[error("Jail path does not exist: {0}")]
JailPathNotFound(PathBuf),
#[error("Jail operation failed: {0}")]
JailOperation(String),
#[error("jail_get syscall failed: {0}")]
JailGet(String),
#[error("jail_set syscall failed: {0}")]
JailSet(String),
#[error("jail_remove syscall failed")]
JailRemoveFailed,
#[error("Failed to attach to jail with JID {0}")]
JailAttachFailed(i32),
#[error("Command execution failed in jail: {0}")]
JailExecFailed(String),
#[error("Failed to create C string: {0}")]
CString(#[from] std::ffi::NulError),
#[error("ZFS operation failed: {0}")]
Zfs(String),
#[error("ZFS not enabled but required for operation")]
ZfsNotEnabled,
#[error("Failed to download: {0}")]
DownloadFailed(String),
#[error("Checksum mismatch for {file}: expected {expected}, got {actual}")]
ChecksumMismatch {
file: String,
expected: String,
actual: String,
},
#[error("Release '{0}' not found")]
ReleaseNotFound(String),
#[error("Release '{0}' already exists")]
ReleaseAlreadyExists(String),
#[error("Failed to extract archive: {0}")]
ExtractionFailed(String),
#[error("Unsupported architecture: {0}")]
UnsupportedArch(String),
#[error("Feature '{feature}' requires FreeBSD {minimum}+, but running {current}")]
UnsupportedOsVersion {
feature: String,
minimum: String,
current: String,
},
#[error("Invalid version format: {0}")]
InvalidVersion(String),
#[error("Command '{command}' failed: {message}")]
CommandFailed { command: String, message: String },
#[error("State error: {0}")]
State(String),
#[error("Network error: {0}")]
Network(String),
#[error("Network '{0}' already exists")]
NetworkAlreadyExists(String),
#[error("Network '{0}' not found")]
NetworkNotFound(String),
#[error("Interface '{0}' not found")]
InterfaceNotFound(String),
#[error("Bridge '{0}' already exists")]
BridgeAlreadyExists(String),
#[error("Hook failed at phase '{phase}': {command} - {message}")]
HookFailed {
phase: String,
command: String,
message: String,
},
#[error("Hook timeout after {0} seconds")]
HookTimeout(u64),
#[error("Jail command timeout after {0} seconds")]
JailTimeout(u64),
#[error("Health check failed for jail '{jail}' ({check}): {message}")]
HealthCheckFailed {
jail: String,
check: String,
message: String,
},
#[error("Template parse failed: {0}")]
TemplateParseFailed(String),
#[error("Build failed at step '{step}': {message}")]
BuildFailed { step: String, message: String },
#[error("Copy failed: {0}")]
CopyFailed(String),
#[error("RCTL error: {0}")]
Rctl(String),
#[error("Invalid argument: {0}")]
InvalidArgument(String),
#[error("{0}")]
CliParse(String),
#[error("The requested output format is not supported for this command")]
UnsupportedOutputFormat,
#[error("Failed to serialize JSON output: {0}")]
JsonOutput(String),
#[error("{0}")]
User(String),
#[error("{source}")]
WithOutput {
source: Box<Self>,
output: Vec<String>,
},
}
impl Error {
#[must_use]
pub fn output(&self) -> &[String] {
match self {
Self::WithOutput { output, .. } => output,
_ => &[],
}
}
#[must_use]
pub fn code(&self) -> &'static str {
match self {
Self::WithOutput { source, .. } => source.code(),
Self::Io(_) => "io_error",
Self::ConfigRead { .. } => "config_read_failed",
Self::ConfigParse(_) => "config_parse_failed",
Self::ConfigValidation(_) => "config_validation_failed",
Self::UnknownDependency(_) => "unknown_dependency",
Self::JailNotFound(_) => "jail_not_found",
Self::JailAlreadyRunning(_) => "jail_already_running",
Self::JailNotRunning(_) => "jail_not_running",
Self::JailExists(_) => "jail_already_exists",
Self::JailCreationFailed(_) => "jail_creation_failed",
Self::JailPathNotFound(_) => "jail_path_not_found",
Self::JailOperation(_) => "jail_operation_failed",
Self::JailGet(_) => "jail_get_failed",
Self::JailSet(_) => "jail_set_failed",
Self::JailRemoveFailed => "jail_remove_failed",
Self::JailAttachFailed(_) => "jail_attach_failed",
Self::JailExecFailed(_) => "jail_exec_failed",
Self::CString(_) => "invalid_c_string",
Self::Zfs(_) => "zfs_operation_failed",
Self::ZfsNotEnabled => "zfs_not_enabled",
Self::DownloadFailed(_) => "download_failed",
Self::ChecksumMismatch { .. } => "checksum_mismatch",
Self::ReleaseNotFound(_) => "release_not_found",
Self::ReleaseAlreadyExists(_) => "release_already_exists",
Self::ExtractionFailed(_) => "extraction_failed",
Self::UnsupportedArch(_) => "unsupported_architecture",
Self::UnsupportedOsVersion { .. } => "unsupported_os_version",
Self::InvalidVersion(_) => "invalid_version",
Self::CommandFailed { .. } => "command_failed",
Self::State(_) => "state_error",
Self::Network(_) => "network_error",
Self::NetworkAlreadyExists(_) => "network_already_exists",
Self::NetworkNotFound(_) => "network_not_found",
Self::InterfaceNotFound(_) => "interface_not_found",
Self::BridgeAlreadyExists(_) => "bridge_already_exists",
Self::HookFailed { .. } => "hook_failed",
Self::HookTimeout(_) => "hook_timeout",
Self::JailTimeout(_) => "jail_timeout",
Self::HealthCheckFailed { .. } => "health_check_failed",
Self::TemplateParseFailed(_) => "template_parse_failed",
Self::BuildFailed { .. } => "build_failed",
Self::CopyFailed(_) => "copy_failed",
Self::Rctl(_) => "rctl_error",
Self::InvalidArgument(_) => "invalid_argument",
Self::CliParse(_) => "cli_parse_failed",
Self::UnsupportedOutputFormat => "unsupported_output_format",
Self::JsonOutput(_) => "json_output_failed",
Self::NeedsRoot { .. } => "needs_root",
Self::EscalationDisabled { .. } => "escalation_disabled",
Self::User(_) => "user_error",
}
}
#[must_use]
pub fn migrated(jail: &str, fence: &str) -> Self {
let (destination, at) = fence.rsplit_once('@').unwrap_or((fence, "an unknown time"));
Self::JailOperation(format!(
"Jail '{jail}' was migrated to {destination} (at unix time {at}); starting it here would run it on two hosts. \
Run 'blackship unfence {jail}' to undo the migration"
))
}
pub(crate) fn require_root(operation: &'static str) -> Result<()> {
let euid = nix::unistd::geteuid().as_raw();
if euid == 0 {
Ok(())
} else {
Err(Self::NeedsRoot { operation, euid })
}
}
}
pub type Result<T> = std::result::Result<T, Error>;