| 1 | use std::ffi::{NulError, c_int}; |
| 2 | use std::fmt; |
| 3 | use std::path::PathBuf; |
| 4 | use std::str::Utf8Error; |
| 5 | |
| 6 | use crate::cutils::string_from_ptr; |
| 7 | |
| 8 | use super::sys::*; |
| 9 | |
| 10 | pub type PamResult<T, E = PamError> = Result<T, E>; |
| 11 | |
| 12 | // TODO: add missing doc-comments |
| 13 | #[derive(PartialEq, Eq, Debug)] |
| 14 | pub enum PamErrorType { |
| 15 | /// There was no error running the PAM command |
| 16 | Success, |
| 17 | OpenError, |
| 18 | SymbolError, |
| 19 | ServiceError, |
| 20 | SystemError, |
| 21 | BufferError, |
| 22 | ConversationError, |
| 23 | PermissionDenied, |
| 24 | /// The maximum number of authentication attempts was reached and no more |
| 25 | /// attempts should be made. |
| 26 | MaxTries, |
| 27 | /// The user failed to authenticate correctly. |
| 28 | AuthError, |
| 29 | NewAuthTokenRequired, |
| 30 | /// The application does not have enough credentials to authenticate the |
| 31 | /// user. This can for example happen if we wanted to update the user |
| 32 | /// password from a non-root process, which we cannot do. |
| 33 | CredentialsInsufficient, |
| 34 | /// PAM modules were unable to access the authentication information (for |
| 35 | /// example due to a network error). |
| 36 | AuthInfoUnavailable, |
| 37 | /// The specified user is unknown to an authentication service. |
| 38 | UserUnknown, |
| 39 | /// Failed to retrieve the credentials (i.e. password) for a user. |
| 40 | CredentialsUnavailable, |
| 41 | /// The credentials (i.e. password) for this user were expired. |
| 42 | CredentialsExpired, |
| 43 | /// There was an error setting the user credentials. |
| 44 | CredentialsError, |
| 45 | /// The user account is expired and can no longer be used. |
| 46 | AccountExpired, |
| 47 | AuthTokenExpired, |
| 48 | SessionError, |
| 49 | AuthTokenError, |
| 50 | AuthTokenRecoveryError, |
| 51 | AuthTokenLockBusy, |
| 52 | AuthTokenDisableAging, |
| 53 | NoModuleData, |
| 54 | Ignore, |
| 55 | /// The application should exit immediately. |
| 56 | Abort, |
| 57 | TryAgain, |
| 58 | ModuleUnknown, |
| 59 | /// The application tried to set/delete an undefined or inaccessible item. |
| 60 | BadItem, // Extension in OpenPAM and LinuxPAM |
| 61 | // DomainUnknown, // OpenPAM only |
| 62 | // BadHandle // OpenPAM only |
| 63 | // BadFeature // OpenPAM only |
| 64 | // BadConstant // OpenPAM only |
| 65 | // ConverseAgain // LinuxPAM only |
| 66 | // Incomplete // LinuxPAM only |
| 67 | UnknownErrorType(i32), |
| 68 | } |
| 69 | |
| 70 | impl PamErrorType { |
| 71 | pub(super) fn from_int(errno: c_int) -> PamErrorType { |
| 72 | use PamErrorType::*; |
| 73 | |
| 74 | match errno as _ { |
| 75 | PAM_SUCCESS => Success, |
| 76 | PAM_OPEN_ERR => OpenError, |
| 77 | PAM_SYMBOL_ERR => SymbolError, |
| 78 | PAM_SERVICE_ERR => ServiceError, |
| 79 | PAM_SYSTEM_ERR => SystemError, |
| 80 | PAM_BUF_ERR => BufferError, |
| 81 | PAM_CONV_ERR => ConversationError, |
| 82 | PAM_PERM_DENIED => PermissionDenied, |
| 83 | PAM_MAXTRIES => MaxTries, |
| 84 | PAM_AUTH_ERR => AuthError, |
| 85 | PAM_NEW_AUTHTOK_REQD => NewAuthTokenRequired, |
| 86 | PAM_CRED_INSUFFICIENT => CredentialsInsufficient, |
| 87 | PAM_AUTHINFO_UNAVAIL => AuthInfoUnavailable, |
| 88 | PAM_USER_UNKNOWN => UserUnknown, |
| 89 | PAM_CRED_UNAVAIL => CredentialsUnavailable, |
| 90 | PAM_CRED_EXPIRED => CredentialsExpired, |
| 91 | PAM_CRED_ERR => CredentialsError, |
| 92 | PAM_ACCT_EXPIRED => AccountExpired, |
| 93 | PAM_AUTHTOK_EXPIRED => AuthTokenExpired, |
| 94 | PAM_SESSION_ERR => SessionError, |
| 95 | PAM_AUTHTOK_ERR => AuthTokenError, |
| 96 | PAM_AUTHTOK_RECOVERY_ERR => AuthTokenRecoveryError, |
| 97 | PAM_AUTHTOK_LOCK_BUSY => AuthTokenLockBusy, |
| 98 | PAM_AUTHTOK_DISABLE_AGING => AuthTokenDisableAging, |
| 99 | PAM_NO_MODULE_DATA => NoModuleData, |
| 100 | PAM_IGNORE => Ignore, |
| 101 | PAM_ABORT => Abort, |
| 102 | PAM_TRY_AGAIN => TryAgain, |
| 103 | PAM_MODULE_UNKNOWN => ModuleUnknown, |
| 104 | PAM_BAD_ITEM => BadItem, |
| 105 | // PAM_DOMAIN_UNKNOWN => DomainUnknown, |
| 106 | // PAM_BAD_HANDLE => BadHandle, |
| 107 | // PAM_BAD_FEATURE => BadFeature, |
| 108 | // PAM_BAD_CONSTANT => BadConstant, |
| 109 | // PAM_CONV_AGAIN => ConverseAgain, |
| 110 | // PAM_INCOMPLETE => Incomplete, |
| 111 | _ => UnknownErrorType(errno), |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | pub fn as_int(&self) -> c_int { |
| 116 | use PamErrorType::*; |
| 117 | |
| 118 | match self { |
| 119 | Success => PAM_SUCCESS as c_int, |
| 120 | OpenError => PAM_OPEN_ERR as c_int, |
| 121 | SymbolError => PAM_SYMBOL_ERR as c_int, |
| 122 | ServiceError => PAM_SERVICE_ERR as c_int, |
| 123 | SystemError => PAM_SYSTEM_ERR as c_int, |
| 124 | BufferError => PAM_BUF_ERR as c_int, |
| 125 | ConversationError => PAM_CONV_ERR as c_int, |
| 126 | PermissionDenied => PAM_PERM_DENIED as c_int, |
| 127 | MaxTries => PAM_MAXTRIES as c_int, |
| 128 | AuthError => PAM_AUTH_ERR as c_int, |
| 129 | NewAuthTokenRequired => PAM_NEW_AUTHTOK_REQD as c_int, |
| 130 | CredentialsInsufficient => PAM_CRED_INSUFFICIENT as c_int, |
| 131 | AuthInfoUnavailable => PAM_AUTHINFO_UNAVAIL as c_int, |
| 132 | UserUnknown => PAM_USER_UNKNOWN as c_int, |
| 133 | CredentialsUnavailable => PAM_CRED_UNAVAIL as c_int, |
| 134 | CredentialsExpired => PAM_CRED_EXPIRED as c_int, |
| 135 | CredentialsError => PAM_CRED_ERR as c_int, |
| 136 | AccountExpired => PAM_ACCT_EXPIRED as c_int, |
| 137 | AuthTokenExpired => PAM_AUTHTOK_EXPIRED as c_int, |
| 138 | SessionError => PAM_SESSION_ERR as c_int, |
| 139 | AuthTokenError => PAM_AUTHTOK_ERR as c_int, |
| 140 | AuthTokenRecoveryError => PAM_AUTHTOK_RECOVERY_ERR as c_int, |
| 141 | AuthTokenLockBusy => PAM_AUTHTOK_LOCK_BUSY as c_int, |
| 142 | AuthTokenDisableAging => PAM_AUTHTOK_DISABLE_AGING as c_int, |
| 143 | NoModuleData => PAM_NO_MODULE_DATA as c_int, |
| 144 | Ignore => PAM_IGNORE as c_int, |
| 145 | Abort => PAM_ABORT as c_int, |
| 146 | TryAgain => PAM_TRY_AGAIN as c_int, |
| 147 | ModuleUnknown => PAM_MODULE_UNKNOWN as c_int, |
| 148 | BadItem => PAM_BAD_ITEM as c_int, |
| 149 | // DomainUnknown => PAM_DOMAIN_UNKNOWN as c_int, |
| 150 | // BadHandle => PAM_BAD_HANDLE as c_int, |
| 151 | // BadFeature => PAM_BAD_FEATURE as c_int, |
| 152 | // BadConstant => PAM_BAD_CONSTANT as c_int, |
| 153 | // ConverseAgain => PAM_CONV_AGAIN as c_int, |
| 154 | // Incomplete => PAM_INCOMPLETE as c_int, |
| 155 | UnknownErrorType(e) => *e, |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | fn get_err_msg(&self) -> String { |
| 160 | // SAFETY: pam_strerror technically takes a pam handle as the first argument, |
| 161 | // but we do not know of any implementation that actually uses the pamh |
| 162 | // argument. See also the netbsd man page for `pam_strerror`. |
| 163 | let data = unsafe { pam_strerror(std::ptr::null_mut(), self.as_int()) }; |
| 164 | if data.is_null() { |
| 165 | String::from("Error unresolved by PAM") |
| 166 | } else { |
| 167 | // SAFETY: pam_strerror returns a pointer to a null-terminated string |
| 168 | unsafe { string_from_ptr(data) } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | #[derive(Debug)] |
| 174 | pub enum PamError { |
| 175 | UnexpectedNulByte(NulError), |
| 176 | Utf8Error(Utf8Error), |
| 177 | Pam(PamErrorType), |
| 178 | IoError(std::io::Error), |
| 179 | TtyRequired, |
| 180 | EnvListFailure, |
| 181 | InteractionRequired, |
| 182 | NoPasswordProvided, |
| 183 | IncorrectPasswordAttempt, |
| 184 | TimedOut, |
| 185 | InvalidUser(String, String), |
| 186 | NoAskpassProgram, |
| 187 | InvalidAskpassProgram(PathBuf), |
| 188 | } |
| 189 | |
| 190 | impl From<std::io::Error> for PamError { |
| 191 | fn from(err: std::io::Error) -> Self { |
| 192 | PamError::IoError(err) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | impl From<NulError> for PamError { |
| 197 | fn from(err: NulError) -> Self { |
| 198 | PamError::UnexpectedNulByte(err) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | impl From<Utf8Error> for PamError { |
| 203 | fn from(err: Utf8Error) -> Self { |
| 204 | PamError::Utf8Error(err) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | impl fmt::Display for PamError { |
| 209 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 210 | match self { |
| 211 | PamError::UnexpectedNulByte(_) => xlat_write!(f, "Unexpected null character in input"), |
| 212 | PamError::Utf8Error(_) => xlat_write!(f, "Could not read input data as UTF-8 string"), |
| 213 | PamError::Pam(PamErrorType::AuthError) => { |
| 214 | xlat_write!(f, "Account validation failure, is your account locked?") |
| 215 | } |
| 216 | PamError::Pam(PamErrorType::NewAuthTokenRequired) => { |
| 217 | xlat_write!( |
| 218 | f, |
| 219 | "Account or password is expired, reset your password and try again" |
| 220 | ) |
| 221 | } |
| 222 | PamError::Pam(PamErrorType::AuthTokenExpired) => { |
| 223 | xlat_write!(f, "Password expired, contact your system administrator") |
| 224 | } |
| 225 | PamError::Pam(tp) => xlat_write!(f, "PAM error: {error}", error = tp.get_err_msg()), |
| 226 | PamError::IoError(e) => xlat_write!(f, "IO error: {error}", error = e), |
| 227 | PamError::TtyRequired => xlat_write!(f, "A terminal is required to authenticate"), |
| 228 | PamError::EnvListFailure => { |
| 229 | xlat_write!( |
| 230 | f, |
| 231 | "It was not possible to get a list of environment variables" |
| 232 | ) |
| 233 | } |
| 234 | PamError::InteractionRequired => xlat_write!(f, "Interaction is required"), |
| 235 | PamError::NoPasswordProvided => { |
| 236 | xlat_write!(f, "Authentication required but not attempted") |
| 237 | } |
| 238 | PamError::IncorrectPasswordAttempt => { |
| 239 | xlat_write!(f, "Incorrect authentication attempt") |
| 240 | } |
| 241 | PamError::TimedOut => xlat_write!(f, "timed out"), |
| 242 | PamError::InvalidUser(username, other_user) => { |
| 243 | xlat_write!( |
| 244 | f, |
| 245 | "Sorry, user {user} is not allowed to authenticate as {other_user}.", |
| 246 | user = username, |
| 247 | other_user = other_user, |
| 248 | ) |
| 249 | } |
| 250 | PamError::NoAskpassProgram => { |
| 251 | xlat_write!(f, "No askpass program specified in SUDO_ASKPASS") |
| 252 | } |
| 253 | PamError::InvalidAskpassProgram(program) => { |
| 254 | xlat_write!( |
| 255 | f, |
| 256 | "Askpass program '{path}' is not an absolute path", |
| 257 | path = program.display() |
| 258 | ) |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | impl PamError { |
| 265 | /// Create a new PamError based on the error number from pam. |
| 266 | pub(super) fn from_pam(errno: c_int) -> PamError { |
| 267 | let tp = PamErrorType::from_int(errno); |
| 268 | PamError::Pam(tp) |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /// Returns `Ok(())` if the error code is `PAM_SUCCESS` or a `PamError` in other cases |
| 273 | pub(super) fn pam_err(err: c_int) -> Result<(), PamError> { |
| 274 | if err == PAM_SUCCESS as c_int { |
| 275 | Ok(()) |
| 276 | } else { |
| 277 | Err(PamError::from_pam(err)) |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | #[cfg(test)] |
| 282 | mod test { |
| 283 | use super::PamErrorType; |
| 284 | |
| 285 | #[test] |
| 286 | fn isomorphy() { |
| 287 | for i in -100..100 { |
| 288 | let pam = PamErrorType::from_int(i); |
| 289 | assert_eq!(pam.as_int(), i); |
| 290 | assert_eq!(PamErrorType::from_int(pam.as_int()), pam); |
| 291 | } |
| 292 | } |
| 293 | } |