| 1 | use std::ffi::OsString; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use crate::common::context::LaunchType; |
| 5 | use crate::common::error::Error; |
| 6 | use crate::log::{dev_info, user_warn}; |
| 7 | use crate::pam::{PamContext, PamError, PamErrorType, PamResult}; |
| 8 | use crate::system::term::current_tty_name; |
| 9 | |
| 10 | pub(super) struct InitPamArgs<'a> { |
| 11 | pub(super) launch: LaunchType, |
| 12 | pub(super) use_askpass: bool, |
| 13 | pub(super) use_stdin: bool, |
| 14 | pub(super) bell: bool, |
| 15 | pub(super) non_interactive: bool, |
| 16 | pub(super) password_feedback: bool, |
| 17 | pub(super) password_timeout: Option<Duration>, |
| 18 | pub(super) auth_prompt: Option<String>, |
| 19 | pub(super) auth_user: &'a str, |
| 20 | pub(super) requesting_user: &'a str, |
| 21 | pub(super) target_user: &'a str, |
| 22 | pub(super) hostname: &'a str, |
| 23 | } |
| 24 | |
| 25 | pub(super) fn init_pam( |
| 26 | InitPamArgs { |
| 27 | launch, |
| 28 | use_askpass, |
| 29 | use_stdin, |
| 30 | bell, |
| 31 | non_interactive, |
| 32 | password_feedback, |
| 33 | password_timeout, |
| 34 | auth_prompt, |
| 35 | auth_user, |
| 36 | requesting_user, |
| 37 | target_user, |
| 38 | hostname, |
| 39 | }: InitPamArgs, |
| 40 | ) -> PamResult<PamContext> { |
| 41 | let service_name = match launch { |
| 42 | LaunchType::Login if cfg!(feature = "pam-login") => "sudo-i", |
| 43 | LaunchType::Login | LaunchType::Shell | LaunchType::Direct => "sudo", |
| 44 | }; |
| 45 | let mut pam = PamContext::new_cli( |
| 46 | "sudo", |
| 47 | service_name, |
| 48 | use_askpass, |
| 49 | use_stdin, |
| 50 | bell, |
| 51 | non_interactive, |
| 52 | password_feedback, |
| 53 | password_timeout, |
| 54 | Some(auth_user), |
| 55 | )?; |
| 56 | pam.mark_silent(matches!(launch, LaunchType::Direct)); |
| 57 | pam.mark_allow_null_auth_token(false); |
| 58 | pam.set_requesting_user(requesting_user)?; |
| 59 | |
| 60 | match auth_prompt.as_deref() { |
| 61 | None => {} |
| 62 | Some("") => pam.set_auth_prompt(None), |
| 63 | Some(auth_prompt) => { |
| 64 | let mut final_prompt = String::new(); |
| 65 | let mut chars = auth_prompt.chars(); |
| 66 | while let Some(c) = chars.next() { |
| 67 | if c != '%' { |
| 68 | final_prompt.push(c); |
| 69 | continue; |
| 70 | } |
| 71 | match chars.next() { |
| 72 | Some('H') => final_prompt.push_str(hostname), |
| 73 | Some('h') => final_prompt |
| 74 | .push_str(hostname.split_once('.').map(|x| x.0).unwrap_or(hostname)), |
| 75 | Some('p') => final_prompt.push_str(auth_user), |
| 76 | Some('U') => final_prompt.push_str(target_user), |
| 77 | Some('u') => final_prompt.push_str(requesting_user), |
| 78 | Some('%') | None => final_prompt.push('%'), |
| 79 | Some(c) => { |
| 80 | final_prompt.push('%'); |
| 81 | final_prompt.push(c); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | pam.set_auth_prompt(Some(final_prompt)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // attempt to set the TTY this session is communicating on |
| 90 | if let Ok(pam_tty) = current_tty_name() { |
| 91 | pam.set_tty(&pam_tty)?; |
| 92 | } |
| 93 | |
| 94 | Ok(pam) |
| 95 | } |
| 96 | |
| 97 | pub(super) fn attempt_authenticate( |
| 98 | pam: &mut PamContext, |
| 99 | auth_user: &str, |
| 100 | non_interactive: bool, |
| 101 | max_tries: u16, |
| 102 | ) -> Result<(), Error> { |
| 103 | // Reject zero upfront so we don't ask for a password once when max_tries is 0. |
| 104 | if max_tries == 0 { |
| 105 | return Err(Error::MaxAuthAttempts(0)); |
| 106 | } |
| 107 | |
| 108 | let mut current_try = 0; |
| 109 | loop { |
| 110 | current_try += 1; |
| 111 | match pam.authenticate(auth_user) { |
| 112 | // there was no error, so authentication succeeded |
| 113 | Ok(_) => break, |
| 114 | |
| 115 | // maxtries was reached, pam does not allow any more tries |
| 116 | Err(PamError::Pam(PamErrorType::MaxTries)) => { |
| 117 | return Err(Error::MaxAuthAttempts(current_try)); |
| 118 | } |
| 119 | |
| 120 | // there was an authentication error, we can retry |
| 121 | Err(PamError::Pam(PamErrorType::AuthError | PamErrorType::ConversationError)) => { |
| 122 | if current_try >= max_tries { |
| 123 | return Err(Error::MaxAuthAttempts(current_try)); |
| 124 | } else if non_interactive { |
| 125 | return Err(Error::InteractionRequired); |
| 126 | } else { |
| 127 | user_warn!("Authentication failed, try again."); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // there was another pam error, return the error |
| 132 | Err(e) => { |
| 133 | return Err(e.into()); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | Ok(()) |
| 139 | } |
| 140 | |
| 141 | pub(super) fn pre_exec( |
| 142 | pam: &mut PamContext, |
| 143 | target_user: &str, |
| 144 | ) -> Result<Vec<(OsString, OsString)>, Error> { |
| 145 | // check what the current user in PAM is |
| 146 | let user = pam.get_user()?; |
| 147 | if user != target_user { |
| 148 | // switch pam over to the target user |
| 149 | pam.set_user(target_user)?; |
| 150 | |
| 151 | // make sure that credentials are loaded for the target user |
| 152 | // errors are ignored because not all modules support this functionality |
| 153 | if let Err(e) = pam.credentials_reinitialize() { |
| 154 | dev_info!( |
| 155 | "PAM gave an error while trying to re-initialize credentials: {:?}", |
| 156 | e |
| 157 | ); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | pam.open_session()?; |
| 162 | |
| 163 | let env_vars = pam.env()?; |
| 164 | |
| 165 | Ok(env_vars) |
| 166 | } |