comparison src/libpam/handle.rs @ 169:77470e45e397

Set up stuff to work the way Sun expects it to. This sets up PAM to use pam_authtok_get.so on Sun machines.
author Paul Fisher <paul@pfish.zone>
date Tue, 15 Jul 2025 01:32:21 -0400
parents 2f5913131295
children e27c5c667a5a
comparison
equal deleted inserted replaced
168:6642e89d29a2 169:77470e45e397
13 use libpam_sys_consts::constants; 13 use libpam_sys_consts::constants;
14 use num_enum::{IntoPrimitive, TryFromPrimitive}; 14 use num_enum::{IntoPrimitive, TryFromPrimitive};
15 use std::any::TypeId; 15 use std::any::TypeId;
16 use std::cell::Cell; 16 use std::cell::Cell;
17 use std::ffi::{c_char, c_int, c_void, CString, OsStr, OsString}; 17 use std::ffi::{c_char, c_int, c_void, CString, OsStr, OsString};
18 use std::mem::ManuallyDrop;
19 use std::os::unix::ffi::OsStrExt; 18 use std::os::unix::ffi::OsStrExt;
20 use std::ptr::NonNull; 19 use std::ptr::NonNull;
21 use std::{any, fmt, ptr}; 20 use std::{any, fmt, ptr};
22 21
23 /// An owned PAM handle. 22 /// An owned PAM handle.
137 /// 136 ///
138 #[doc = man7!(3 pam_end)] 137 #[doc = man7!(3 pam_end)]
139 pub fn end_silent(self) { 138 pub fn end_silent(self) {
140 #[cfg(pam_impl = "LinuxPam")] 139 #[cfg(pam_impl = "LinuxPam")]
141 { 140 {
142 let mut me = ManuallyDrop::new(self); 141 let mut me = std::mem::ManuallyDrop::new(self);
143 me.end_internal(libpam_sys::PAM_DATA_SILENT); 142 me.end_internal(libpam_sys::PAM_DATA_SILENT);
144 } 143 }
145 // If it's not LinuxPam, we just drop normally. 144 // If it's not LinuxPam, we just drop normally.
146 } 145 }
147 146
492 // SAFETY: We got this string from PAM. 491 // SAFETY: We got this string from PAM.
493 unsafe { memory::copy_pam_string(output) }.ok_or(ErrorCode::ConversationError) 492 unsafe { memory::copy_pam_string(output) }.ok_or(ErrorCode::ConversationError)
494 } 493 }
495 494
496 #[cfg(pam_impl = "Sun")] 495 #[cfg(pam_impl = "Sun")]
497 fn get_authtok(&mut self, prompt: Option<&OsStr>, item_type: ItemType) -> Result<OsString> { 496 fn get_authtok(&mut self, _prompt: Option<&OsStr>, item_type: ItemType) -> Result<OsString> {
498 use crate::libpam::memory::CHeapString; 497 unsafe { items::get_cstr_item(self, item_type) }?.ok_or(ErrorCode::ConversationError)
499 use std::os::unix::ffi::OsStringExt;
500 // Sun's __pam_get_authtok function is a little weird and requires
501 // that you specify where you want the authtok to come from.
502 // First we see if there's an authtok already set.
503 let mut output: *mut c_char = ptr::null_mut();
504 let result = unsafe {
505 libpam_sys::__pam_get_authtok(
506 self.inner_mut(),
507 libpam_sys::PAM_HANDLE,
508 item_type.into(),
509 ptr::null(),
510 &mut output,
511 )
512 };
513 let output = unsafe { CHeapString::from_ptr(output) };
514 if result == libpam_sys::PAM_SUCCESS {
515 if let Some(output) = output {
516 return Ok(OsString::from_vec(output.to_bytes().into()));
517 }
518 }
519 drop(output);
520 let mut output: *mut c_char = ptr::null_mut();
521 let prompt = memory::option_cstr_os(prompt);
522 let result = unsafe {
523 libpam_sys::__pam_get_authtok(
524 self.inner_mut(),
525 libpam_sys::PAM_PROMPT,
526 item_type.into(),
527 memory::prompt_ptr(prompt.as_deref()),
528 &mut output,
529 )
530 };
531 let output = unsafe { CHeapString::from_ptr(output) };
532 ErrorCode::result_from(result)?;
533 output
534 .map(|s| OsString::from_vec(s.to_bytes().into()))
535 .ok_or(ErrorCode::ConversationError)
536 } 498 }
537 499
538 /// Gets the `PAM_CONV` item from the handle. 500 /// Gets the `PAM_CONV` item from the handle.
539 fn conversation_item(&self) -> Result<&PamConv> { 501 fn conversation_item(&self) -> Result<&PamConv> {
540 let mut output: *const c_void = ptr::null(); 502 let mut output: *const c_void = ptr::null();