Mercurial > crates > nonstick
diff src/module.rs @ 59:3f4a77aa88be
Fix string copyting and improve error situation.
This change is too big and includes several things:
- Fix copying strings from PAM by fixing const and mut on pam funcs.
- Improve error enums by simplifying conversions and removing
unnecessary and ambiguous "success" variants.
- Make a bunch of casts nicer.
- Assorted other cleanup.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 21 May 2025 00:27:18 -0400 |
parents | daa2cde64601 |
children | 05cc2c27334f |
line wrap: on
line diff
--- a/src/module.rs Mon May 05 00:16:04 2025 -0400 +++ b/src/module.rs Wed May 21 00:27:18 2025 -0400 @@ -1,10 +1,10 @@ //! Functions for use in pam modules. -use crate::constants::{Flags, PamResult, ErrorCode}; +use crate::constants::{ErrorCode, Flags, PamResult}; use crate::items::{Item, ItemType}; use libc::c_char; +use secure_string::SecureString; use std::ffi::{c_int, CStr, CString}; -use secure_string::SecureString; /// Opaque type, used as a pointer when making pam API calls. /// @@ -27,7 +27,7 @@ fn pam_set_data( pamh: *const PamHandle, module_data_name: *const c_char, - data: *mut libc::c_void, + data: *const libc::c_void, cleanup: extern "C" fn( pamh: *const PamHandle, data: *mut libc::c_void, @@ -43,12 +43,16 @@ fn pam_set_item(pamh: *mut PamHandle, item_type: c_int, item: *const libc::c_void) -> c_int; - fn pam_get_user(pamh: *const PamHandle, user: &*mut c_char, prompt: *const c_char) -> c_int; + fn pam_get_user( + pamh: *const PamHandle, + user: &mut *const c_char, + prompt: *const c_char, + ) -> c_int; fn pam_get_authtok( pamh: *const PamHandle, item_type: c_int, - data: &*mut c_char, + data: &mut *const c_char, prompt: *const c_char, ) -> c_int; @@ -60,7 +64,7 @@ /// You should never call this yourself. extern "C" fn cleanup<T>(_: *const PamHandle, c_data: *mut libc::c_void, _: c_int) { unsafe { - let _data: Box<T> = Box::from_raw(c_data.cast::<T>()); + let _data: Box<T> = Box::from_raw(c_data.cast()); } } @@ -88,7 +92,7 @@ match ptr.is_null() { true => Ok(None), false => { - let typed_ptr = ptr.cast::<T>(); + let typed_ptr = ptr.cast(); Ok(Some(&*typed_ptr)) } } @@ -109,7 +113,7 @@ pam_set_data( self, c_key.as_ptr(), - Box::into_raw(data).cast::<libc::c_void>(), + Box::into_raw(data).cast(), cleanup::<T>, ) }; @@ -133,7 +137,7 @@ let out = unsafe { let ret = pam_get_item(self, T::type_id().into(), &mut ptr); ErrorCode::result_from(ret)?; - let typed_ptr = ptr.cast::<T::Raw>(); + let typed_ptr: *const T::Raw = ptr.cast(); match typed_ptr.is_null() { true => None, false => Some(T::from_raw(typed_ptr)), @@ -151,8 +155,7 @@ /// /// Returns an error if the underlying PAM function call fails. pub fn set_item<T: Item>(&mut self, item: T) -> PamResult<()> { - let ret = - unsafe { pam_set_item(self, T::type_id().into(), item.into_raw().cast::<libc::c_void>()) }; + let ret = unsafe { pam_set_item(self, T::type_id().into(), item.into_raw().cast()) }; ErrorCode::result_from(ret) } @@ -168,8 +171,8 @@ /// Returns an error if the underlying PAM function call fails. pub fn get_user(&self, prompt: Option<&str>) -> PamResult<String> { let prompt = option_cstr(prompt)?; - let output: *mut c_char = std::ptr::null_mut(); - let ret = unsafe { pam_get_user(self, &output, prompt_ptr(prompt.as_ref())) }; + let mut output: *const c_char = std::ptr::null_mut(); + let ret = unsafe { pam_get_user(self, &mut output, prompt_ptr(prompt.as_ref())) }; ErrorCode::result_from(ret)?; copy_pam_string(output) } @@ -186,12 +189,12 @@ /// Returns an error if the underlying PAM function call fails. pub fn get_authtok(&self, prompt: Option<&str>) -> PamResult<SecureString> { let prompt = option_cstr(prompt)?; - let output: *mut c_char = std::ptr::null_mut(); + let mut output: *const c_char = std::ptr::null_mut(); let res = unsafe { pam_get_authtok( self, ItemType::AuthTok.into(), - &output, + &mut output, prompt_ptr(prompt.as_ref()), ) };