| 1 | use crate::{pam::PamError, system::Hostname}; |
| 2 | use std::{borrow::Cow, fmt, path::PathBuf}; |
| 3 | |
| 4 | use super::{SudoPath, SudoString}; |
| 5 | |
| 6 | #[derive(Debug)] |
| 7 | pub enum Error { |
| 8 | Silent, |
| 9 | NotAllowed { |
| 10 | username: SudoString, |
| 11 | command: Cow<'static, str>, |
| 12 | hostname: Hostname, |
| 13 | other_user: Option<SudoString>, |
| 14 | }, |
| 15 | SelfCheckSetuid, |
| 16 | SelfCheckNoNewPrivs, |
| 17 | CommandNotFound(PathBuf), |
| 18 | InvalidCommand(PathBuf), |
| 19 | ChDirNotAllowed { |
| 20 | chdir: SudoPath, |
| 21 | command: PathBuf, |
| 22 | }, |
| 23 | UserNotFound(String), |
| 24 | GroupNotFound(String), |
| 25 | Authorization(String), |
| 26 | InteractionRequired, |
| 27 | EnvironmentVar(Vec<String>), |
| 28 | Configuration(String), |
| 29 | Options(String), |
| 30 | Pam(PamError), |
| 31 | Io(Option<PathBuf>, std::io::Error), |
| 32 | MaxAuthAttempts(u16), |
| 33 | PathValidation(PathBuf), |
| 34 | StringValidation(String), |
| 35 | #[cfg(feature = "apparmor")] |
| 36 | AppArmor(String, std::io::Error), |
| 37 | } |
| 38 | |
| 39 | impl fmt::Display for Error { |
| 40 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 41 | match self { |
| 42 | Error::Silent => Ok(()), |
| 43 | Error::NotAllowed { |
| 44 | username, |
| 45 | command, |
| 46 | hostname, |
| 47 | other_user, |
| 48 | } => { |
| 49 | if let Some(other_user) = other_user { |
| 50 | xlat_write!( |
| 51 | f, |
| 52 | "Sorry, user {user} is not allowed to execute '{command}' as {other_user} on {hostname}.", |
| 53 | user = username, |
| 54 | command = command, |
| 55 | other_user = other_user, |
| 56 | hostname = hostname, |
| 57 | ) |
| 58 | } else { |
| 59 | xlat_write!( |
| 60 | f, |
| 61 | "Sorry, user {user} may not run {command} on {hostname}.", |
| 62 | user = username, |
| 63 | command = command, |
| 64 | hostname = hostname, |
| 65 | ) |
| 66 | } |
| 67 | } |
| 68 | Error::SelfCheckSetuid => { |
| 69 | xlat_write!(f, "sudo must be owned by uid 0 and have the setuid bit set") |
| 70 | } |
| 71 | Error::SelfCheckNoNewPrivs => { |
| 72 | xlat_write!( |
| 73 | f, |
| 74 | "The \"no new privileges\" flag is set, which prevents sudo from running as root.\n\ |
| 75 | If sudo is running in a container, you may need to adjust the container \ |
| 76 | configuration to disable the flag." |
| 77 | ) |
| 78 | } |
| 79 | Error::CommandNotFound(p) => { |
| 80 | xlat_write!(f, "'{path}': command not found", path = p.display()) |
| 81 | } |
| 82 | Error::InvalidCommand(p) => { |
| 83 | xlat_write!(f, "'{path}': invalid command", path = p.display()) |
| 84 | } |
| 85 | Error::UserNotFound(u) => xlat_write!(f, "user '{user}' not found", user = u), |
| 86 | Error::GroupNotFound(g) => xlat_write!(f, "group '{group}' not found", group = g), |
| 87 | Error::Authorization(u) => { |
| 88 | // TRANSLATORS: This is a well-known quote, try to preserve it in translation. |
| 89 | xlat_write!(f, "I'm sorry {user}. I'm afraid I can't do that", user = u) |
| 90 | } |
| 91 | Error::InteractionRequired => xlat_write!(f, "interactive authentication is required"), |
| 92 | Error::EnvironmentVar(vs) => { |
| 93 | xlat_write!( |
| 94 | f, |
| 95 | "you are not allowed to set the following environment variables:" |
| 96 | )?; |
| 97 | let mut sep = ""; |
| 98 | for v in vs { |
| 99 | write!(f, "{sep} {v}")?; |
| 100 | sep = ","; |
| 101 | } |
| 102 | Ok(()) |
| 103 | } |
| 104 | Error::Configuration(e) => write!(f, "{e}"), |
| 105 | Error::Options(e) => write!(f, "{e}"), |
| 106 | Error::Pam(e) => write!(f, "{e}"), |
| 107 | Error::Io(location, e) => { |
| 108 | if let Some(path) = location { |
| 109 | xlat_write!( |
| 110 | f, |
| 111 | "cannot execute '{path}': {error}", |
| 112 | path = path.display(), |
| 113 | error = e |
| 114 | ) |
| 115 | } else { |
| 116 | xlat_write!(f, "IO error: {error}", error = e) |
| 117 | } |
| 118 | } |
| 119 | Error::MaxAuthAttempts(num) => { |
| 120 | xlat_write!( |
| 121 | f, |
| 122 | "maximum {num} incorrect authentication attempts", |
| 123 | num = num |
| 124 | ) |
| 125 | } |
| 126 | Error::ChDirNotAllowed { chdir, command } => xlat_write!( |
| 127 | f, |
| 128 | "you are not allowed to use '--chdir {path}' with '{command}'", |
| 129 | path = chdir.display(), |
| 130 | command = command.display() |
| 131 | ), |
| 132 | Error::StringValidation(string) => { |
| 133 | write!( |
| 134 | f, |
| 135 | "{}: {string:?}", |
| 136 | xlat!("Unexpected null character in input") |
| 137 | ) |
| 138 | } |
| 139 | Error::PathValidation(path) => { |
| 140 | write!( |
| 141 | f, |
| 142 | "{}: {path:?}", |
| 143 | xlat!("Unexpected null character in input") |
| 144 | ) |
| 145 | } |
| 146 | #[cfg(feature = "apparmor")] |
| 147 | Error::AppArmor(profile, e) => { |
| 148 | xlat_write!( |
| 149 | f, |
| 150 | "unable to change AppArmor profile to {profile}: {error}", |
| 151 | profile = profile, |
| 152 | error = e |
| 153 | ) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | impl From<PamError> for Error { |
| 160 | fn from(err: PamError) -> Self { |
| 161 | Error::Pam(err) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | impl From<std::io::Error> for Error { |
| 166 | fn from(err: std::io::Error) -> Self { |
| 167 | Error::Io(None, err) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl Error { |
| 172 | /// Returns `true` if the error is [`Silent`]. |
| 173 | /// |
| 174 | /// [`Silent`]: Error::Silent |
| 175 | #[must_use] |
| 176 | pub fn is_silent(&self) -> bool { |
| 177 | matches!(self, Self::Silent) |
| 178 | } |
| 179 | } |