@@ -16,8 +16,8 @@ pub enum AuthState { |
| 16 | 16 | Idle, |
| 17 | 17 | /// Waiting for password after CreateSession |
| 18 | 18 | AwaitingPassword { username: String }, |
| 19 | | - /// Authentication succeeded |
| 20 | | - Authenticated { username: String }, |
| 19 | + /// Authentication succeeded - stores password for session PAM |
| 20 | + Authenticated { username: String, password: String }, |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | impl Default for AuthState { |
@@ -73,6 +73,9 @@ impl AuthSession { |
| 73 | 73 | /// Attempt authentication with provided password |
| 74 | 74 | /// |
| 75 | 75 | /// This runs PAM authentication in a blocking thread since PAM is synchronous. |
| 76 | + /// The password is stored temporarily for use by the session spawner, which |
| 77 | + /// will re-authenticate with PAM in the child process to properly register |
| 78 | + /// the session with logind. |
| 76 | 79 | pub async fn authenticate(&mut self, password: &str) -> AuthResponse { |
| 77 | 80 | let username = match &self.state { |
| 78 | 81 | AuthState::AwaitingPassword { username } => username.clone(), |
@@ -83,18 +86,24 @@ impl AuthSession { |
| 83 | 86 | } |
| 84 | 87 | }; |
| 85 | 88 | |
| 86 | | - // Run PAM in blocking thread |
| 87 | | - let password = password.to_string(); |
| 89 | + // Run PAM in blocking thread - this just verifies credentials |
| 90 | + let password_str = password.to_string(); |
| 88 | 91 | let username_for_pam = username.clone(); |
| 92 | + tracing::debug!("Spawning PAM authentication task"); |
| 89 | 93 | let result = tokio::task::spawn_blocking(move || { |
| 90 | | - pam_authenticate(&username_for_pam, &password) |
| 94 | + pam_verify_only(&username_for_pam, &password_str) |
| 91 | 95 | }) |
| 92 | 96 | .await; |
| 97 | + tracing::debug!(?result, "PAM task completed"); |
| 93 | 98 | |
| 94 | 99 | match result { |
| 95 | 100 | Ok(Ok(())) => { |
| 96 | 101 | tracing::info!(%username, "Authentication succeeded"); |
| 97 | | - self.state = AuthState::Authenticated { username }; |
| 102 | + // Store password for session spawner to use with PAM open_session |
| 103 | + self.state = AuthState::Authenticated { |
| 104 | + username, |
| 105 | + password: password.to_string(), |
| 106 | + }; |
| 98 | 107 | AuthResponse::Success |
| 99 | 108 | } |
| 100 | 109 | Ok(Err(e)) => { |
@@ -116,7 +125,7 @@ impl AuthSession { |
| 116 | 125 | |
| 117 | 126 | /// Cancel the current authentication session |
| 118 | 127 | pub fn cancel(&mut self) { |
| 119 | | - if let AuthState::AwaitingPassword { username } | AuthState::Authenticated { username } = |
| 128 | + if let AuthState::AwaitingPassword { username } | AuthState::Authenticated { username, .. } = |
| 120 | 129 | &self.state |
| 121 | 130 | { |
| 122 | 131 | tracing::info!(username, "Session cancelled"); |
@@ -127,26 +136,29 @@ impl AuthSession { |
| 127 | 136 | /// Check if user is authenticated and ready to start session |
| 128 | 137 | pub fn is_authenticated(&self) -> Option<&str> { |
| 129 | 138 | match &self.state { |
| 130 | | - AuthState::Authenticated { username } => Some(username), |
| 139 | + AuthState::Authenticated { username, .. } => Some(username), |
| 131 | 140 | _ => None, |
| 132 | 141 | } |
| 133 | 142 | } |
| 134 | 143 | |
| 135 | | - /// Consume the authenticated state and return the username |
| 136 | | - pub fn take_authenticated(&mut self) -> Option<String> { |
| 144 | + /// Consume the authenticated state and return (username, password) |
| 145 | + /// The password is needed for the session spawner to re-authenticate with PAM |
| 146 | + pub fn take_authenticated(&mut self) -> Option<(String, String)> { |
| 137 | 147 | if matches!(self.state, AuthState::Authenticated { .. }) { |
| 138 | 148 | let old = std::mem::replace(&mut self.state, AuthState::Idle); |
| 139 | | - if let AuthState::Authenticated { username } = old { |
| 140 | | - return Some(username); |
| 149 | + if let AuthState::Authenticated { username, password } = old { |
| 150 | + return Some((username, password)); |
| 141 | 151 | } |
| 142 | 152 | } |
| 143 | 153 | None |
| 144 | 154 | } |
| 145 | 155 | } |
| 146 | 156 | |
| 147 | | -/// Perform PAM authentication (blocking) |
| 148 | | -fn pam_authenticate(username: &str, password: &str) -> Result<()> { |
| 149 | | - tracing::debug!(%username, password_len = password.len(), "Starting PAM authentication"); |
| 157 | +/// Verify PAM credentials only (no open_session) |
| 158 | +/// This is used for quick credential verification. The actual session opening |
| 159 | +/// happens in the spawned child process. |
| 160 | +fn pam_verify_only(username: &str, password: &str) -> Result<()> { |
| 161 | + tracing::debug!(%username, password_len = password.len(), "Verifying PAM credentials"); |
| 150 | 162 | |
| 151 | 163 | // Create client with PasswordConv (non-interactive, uses provided password) |
| 152 | 164 | let mut client = Client::with_password(PAM_SERVICE) |
@@ -159,22 +171,19 @@ fn pam_authenticate(username: &str, password: &str) -> Result<()> { |
| 159 | 171 | |
| 160 | 172 | tracing::debug!("PAM client created, calling authenticate"); |
| 161 | 173 | |
| 162 | | - // Authenticate |
| 174 | + // Authenticate only - don't open session here |
| 175 | + // open_session will be called in the spawned child process |
| 163 | 176 | client |
| 164 | 177 | .authenticate() |
| 165 | 178 | .map_err(|e| anyhow!("PAM authentication failed: {:?}", e))?; |
| 166 | 179 | |
| 167 | | - tracing::debug!("PAM authenticate succeeded, opening session"); |
| 168 | | - |
| 169 | | - // Open session (also does account validation) |
| 170 | | - client |
| 171 | | - .open_session() |
| 172 | | - .map_err(|e| anyhow!("Failed to open PAM session: {:?}", e))?; |
| 173 | | - |
| 174 | | - tracing::debug!("PAM session opened successfully"); |
| 180 | + tracing::debug!("PAM credential verification complete"); |
| 175 | 181 | Ok(()) |
| 176 | 182 | } |
| 177 | 183 | |
| 184 | +/// Service name for PAM - exported for use by session module |
| 185 | +pub const PAM_SERVICE_NAME: &str = PAM_SERVICE; |
| 186 | + |
| 178 | 187 | #[cfg(test)] |
| 179 | 188 | mod tests { |
| 180 | 189 | use super::*; |