Mercurial > crates > nonstick
diff pam/src/module.rs @ 34:ec70822cbdef
Overhaul
author | Andy Caldwell <andrew.caldwell@metaswitch.com> |
---|---|
date | Sun, 24 Apr 2022 03:42:11 +0100 |
parents | 4263c1d83d5b |
children | 50371046c61a |
line wrap: on
line diff
--- a/pam/src/module.rs Thu Jul 11 19:13:19 2019 -0700 +++ b/pam/src/module.rs Sun Apr 24 03:42:11 2022 +0100 @@ -1,93 +1,87 @@ //! Functions for use in pam modules. use libc::c_char; -use std::{mem, ptr}; use std::ffi::{CStr, CString}; -use constants::{PamResultCode, PamItemType, PamFlag}; +use constants::{PamFlag, PamResultCode}; /// Opaque type, used as a pointer when making pam API calls. /// /// A module is invoked via an external function such as `pam_sm_authenticate`. /// Such a call provides a pam handle pointer. The same pointer should be given /// as an argument when making API calls. -#[allow(missing_copy_implementations)] -pub enum PamHandle {} - -#[allow(missing_copy_implementations)] -enum PamItemT {} - -#[allow(missing_copy_implementations)] -pub enum PamDataT {} +#[repr(C)] +pub struct PamHandle { + _data: [u8; 0], +} #[link(name = "pam")] extern "C" { - fn pam_get_data(pamh: *const PamHandle, - module_data_name: *const c_char, - data: &mut *const PamDataT) - -> PamResultCode; + fn pam_get_data( + pamh: *const PamHandle, + module_data_name: *const c_char, + data: &mut *const libc::c_void, + ) -> PamResultCode; - fn pam_set_data(pamh: *const PamHandle, - module_data_name: *const c_char, - data: Box<PamDataT>, - cleanup: extern "C" fn(pamh: *const PamHandle, - data: Box<PamDataT>, - error_status: PamResultCode)) - -> PamResultCode; + fn pam_set_data( + pamh: *const PamHandle, + module_data_name: *const c_char, + data: *mut libc::c_void, + cleanup: extern "C" fn( + pamh: *const PamHandle, + data: *mut libc::c_void, + error_status: PamResultCode, + ), + ) -> PamResultCode; - fn pam_get_item(pamh: *const PamHandle, - item_type: PamItemType, - item: &mut *const PamItemT) - -> PamResultCode; + fn pam_get_item( + pamh: *const PamHandle, + item_type: crate::items::ItemType, + item: &mut *const libc::c_void, + ) -> PamResultCode; - fn pam_set_item(pamh: *mut PamHandle, - item_type: PamItemType, - item: &PamItemT) - -> PamResultCode; + fn pam_set_item( + pamh: *mut PamHandle, + item_type: crate::items::ItemType, + item: *const libc::c_void, + ) -> PamResultCode; - fn pam_get_user(pamh: *const PamHandle, - user: &*mut c_char, - prompt: *const c_char) - -> PamResultCode; + fn pam_get_user( + pamh: *const PamHandle, + user: &*mut c_char, + prompt: *const c_char, + ) -> PamResultCode; } -#[no_mangle] -pub extern "C" fn cleanup<T>(_: *const PamHandle, c_data: Box<PamDataT>, _: PamResultCode) { +pub extern "C" fn cleanup<T>(_: *const PamHandle, c_data: *mut libc::c_void, _: PamResultCode) { unsafe { - let data: Box<T> = mem::transmute(c_data); - mem::drop(data); + let _data: Box<T> = Box::from_raw(c_data.cast::<T>()); } } pub type PamResult<T> = Result<T, PamResultCode>; -/// Type-level mapping for safely retrieving values with `get_item`. -/// -/// See `pam_get_item` in -/// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html -pub trait PamItem { - /// Maps a Rust type to a pam constant. - /// - /// For example, the type PamConv maps to the constant PAM_CONV. The pam - /// API contract specifies that when the API function `pam_get_item` is - /// called with the constant PAM_CONV, it will return a value of type - /// `PamConv`. - fn item_type() -> PamItemType; -} - - impl PamHandle { /// Gets some value, identified by `key`, that has been set by the module /// previously. /// /// See `pam_get_data` in /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html + /// + /// # Errors + /// + /// Returns an error if the underlying PAM function call fails. + /// + /// # Safety + /// + /// The data stored under the provided key must be of type `T` otherwise the + /// behaviour of this funtion is undefined. pub unsafe fn get_data<'a, T>(&'a self, key: &str) -> PamResult<&'a T> { - let c_key = CString::new(key).unwrap().as_ptr(); - let mut ptr: *const PamDataT = ptr::null(); - let res = pam_get_data(self, c_key, &mut ptr); + let c_key = CString::new(key).unwrap(); + let mut ptr: *const libc::c_void = std::ptr::null(); + let res = pam_get_data(self, c_key.as_ptr(), &mut ptr); if PamResultCode::PAM_SUCCESS == res && !ptr.is_null() { - let typed_ptr: *const T = mem::transmute(ptr); + let typed_ptr = ptr.cast::<T>(); let data: &T = &*typed_ptr; Ok(data) } else { @@ -100,11 +94,19 @@ /// /// See `pam_set_data` in /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html + /// + /// # Errors + /// + /// Returns an error if the underlying PAM function call fails. pub fn set_data<T>(&self, key: &str, data: Box<T>) -> PamResult<()> { - let c_key = CString::new(key).unwrap().as_ptr(); + let c_key = CString::new(key).unwrap(); let res = unsafe { - let c_data: Box<PamDataT> = mem::transmute(data); - pam_set_data(self, c_key, c_data, cleanup::<T>) + pam_set_data( + self, + c_key.as_ptr(), + Box::into_raw(data).cast::<libc::c_void>(), + cleanup::<T>, + ) }; if PamResultCode::PAM_SUCCESS == res { Ok(()) @@ -113,19 +115,25 @@ } } - - /// Retrieves a value that has been set, possibly by the pam client. This is /// particularly useful for getting a `PamConv` reference. /// /// See `pam_get_item` in /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html - pub fn get_item<'a, T: PamItem>(&self) -> PamResult<&'a T> { - let mut ptr: *const PamItemT = ptr::null(); + /// + /// # Errors + /// + /// Returns an error if the underlying PAM function call fails. + pub fn get_item<T: crate::items::Item>(&self) -> PamResult<Option<T>> { + let mut ptr: *const libc::c_void = std::ptr::null(); let (res, item) = unsafe { - let r = pam_get_item(self, T::item_type(), &mut ptr); - let typed_ptr: *const T = mem::transmute(ptr); - let t: &T = &*typed_ptr; + let r = pam_get_item(self, T::type_id(), &mut ptr); + let typed_ptr = ptr.cast::<T::Raw>(); + let t = if typed_ptr.is_null() { + None + } else { + Some(T::from_raw(typed_ptr)) + }; (r, t) }; if PamResultCode::PAM_SUCCESS == res { @@ -142,17 +150,17 @@ /// /// See `pam_set_item` in /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html - pub fn set_item_str<T: PamItem>(&mut self, item: &str) -> PamResult<()> { - let c_item = CString::new(item).unwrap().as_ptr(); - - let res = unsafe { - pam_set_item(self, - T::item_type(), - - // unwrapping is okay here, as c_item will not be a NULL - // pointer - (c_item as *const PamItemT).as_ref().unwrap()) - }; + /// + /// # Errors + /// + /// Returns an error if the underlying PAM function call fails. + /// + /// # Panics + /// + /// Panics if the provided item key contains a nul byte + pub fn set_item_str<T: crate::items::Item>(&mut self, item: T) -> PamResult<()> { + let res = + unsafe { pam_set_item(self, T::type_id(), item.into_raw().cast::<libc::c_void>())}; if PamResultCode::PAM_SUCCESS == res { Ok(()) } else { @@ -166,11 +174,23 @@ /// /// See `pam_get_user` in /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html + /// + /// # Errors + /// + /// Returns an error if the underlying PAM function call fails. + /// + /// # Panics + /// + /// Panics if the provided prompt string contains a nul byte pub fn get_user(&self, prompt: Option<&str>) -> PamResult<String> { - let ptr: *mut c_char = ptr::null_mut(); + let ptr: *mut c_char = std::ptr::null_mut(); + let prompt_string; let c_prompt = match prompt { - Some(p) => CString::new(p).unwrap().as_ptr(), - None => ptr::null(), + Some(p) => { + prompt_string = CString::new(p).unwrap(); + prompt_string.as_ptr() + } + None => std::ptr::null(), }; let res = unsafe { pam_get_user(self, &ptr, c_prompt) }; if PamResultCode::PAM_SUCCESS == res && !ptr.is_null() { @@ -195,41 +215,41 @@ /// authentication module. This function checks for other things. Such things might be: the time of /// day or the date, the terminal line, remote hostname, etc. This function may also determine /// things like the expiration on passwords, and respond that the user change it before continuing. - fn acct_mgmt(pamh: &PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { - PamResultCode::PAM_IGNORE - } + fn acct_mgmt(pamh: &mut PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { + PamResultCode::PAM_IGNORE + } /// This function performs the task of authenticating the user. - fn sm_authenticate(pamh: &PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { - PamResultCode::PAM_IGNORE - } + fn sm_authenticate(pamh: &mut PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { + PamResultCode::PAM_IGNORE + } - /// This function is used to (re-)set the authentication token of the user. - /// - /// The PAM library calls this function twice in succession. The first time with - /// PAM_PRELIM_CHECK and then, if the module does not return PAM_TRY_AGAIN, subsequently with - /// PAM_UPDATE_AUTHTOK. It is only on the second call that the authorization token is - /// (possibly) changed. - fn sm_chauthtok(pamh: &PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { - PamResultCode::PAM_IGNORE - } + /// This function is used to (re-)set the authentication token of the user. + /// + /// The PAM library calls this function twice in succession. The first time with + /// `PAM_PRELIM_CHECK` and then, if the module does not return `PAM_TRY_AGAIN`, subsequently with + /// `PAM_UPDATE_AUTHTOK`. It is only on the second call that the authorization token is + /// (possibly) changed. + fn sm_chauthtok(pamh: &mut PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { + PamResultCode::PAM_IGNORE + } - /// This function is called to terminate a session. - fn sm_close_session(pamh: &PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { - PamResultCode::PAM_IGNORE - } + /// This function is called to terminate a session. + fn sm_close_session(pamh: &mut PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { + PamResultCode::PAM_IGNORE + } - /// This function is called to commence a session. - fn sm_open_session(pamh: &PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { - PamResultCode::PAM_IGNORE - } + /// This function is called to commence a session. + fn sm_open_session(pamh: &mut PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { + PamResultCode::PAM_IGNORE + } /// This function performs the task of altering the credentials of the user with respect to the /// corresponding authorization scheme. Generally, an authentication module may have access to more /// information about a user than their authentication token. This function is used to make such /// information available to the application. It should only be called after the user has been /// authenticated but before a session has been established. - fn sm_setcred(pamh: &PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { - PamResultCode::PAM_IGNORE - } -} \ No newline at end of file + fn sm_setcred(pamh: &mut PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { + PamResultCode::PAM_IGNORE + } +}