Mercurial > crates > nonstick
diff src/libpam/handle.rs @ 95:51c9d7e8261a
Return owned strings rather than borrowed strings.
It's going to be irritating to have to work with strings borrowed from the
PAM handle rather than just using your own. They're cheap enough to copy.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Mon, 23 Jun 2025 14:03:44 -0400 |
parents | 5ddbcada30f2 |
children | f3e260f9ddcb |
line wrap: on
line diff
--- a/src/libpam/handle.rs Mon Jun 23 13:04:27 2025 -0400 +++ b/src/libpam/handle.rs Mon Jun 23 14:03:44 2025 -0400 @@ -65,7 +65,7 @@ /// Macro to implement getting/setting a CStr-based item. macro_rules! cstr_item { (get = $getter:ident, item = $item_type:path) => { - fn $getter(&self) -> Result<Option<&str>> { + fn $getter(&self) -> Result<Option<String>> { unsafe { self.get_cstr_item($item_type) } } }; @@ -98,14 +98,14 @@ } } - fn username(&mut self, prompt: Option<&str>) -> Result<&str> { + fn username(&mut self, prompt: Option<&str>) -> Result<String> { let prompt = memory::option_cstr(prompt)?; let mut output: *const c_char = ptr::null(); let ret = unsafe { pam_ffi::pam_get_user(self, &mut output, memory::prompt_ptr(prompt.as_ref())) }; ErrorCode::result_from(ret)?; - unsafe { memory::wrap_string(output) } + unsafe { memory::copy_pam_string(output) } .transpose() .unwrap_or(Err(ErrorCode::ConversationError)) } @@ -140,7 +140,7 @@ } impl PamHandleModule for LibPamHandle { - fn authtok(&mut self, prompt: Option<&str>) -> Result<&str> { + fn authtok(&mut self, prompt: Option<&str>) -> Result<String> { let prompt = memory::option_cstr(prompt)?; let mut output: *const c_char = ptr::null_mut(); // SAFETY: We're calling this with known-good values. @@ -154,7 +154,7 @@ }; ErrorCode::result_from(res)?; // SAFETY: We got this string from PAM. - unsafe { memory::wrap_string(output) } + unsafe { memory::copy_pam_string(output) } .transpose() .unwrap_or(Err(ErrorCode::ConversationError)) } @@ -179,11 +179,11 @@ /// # Safety /// /// You better be requesting an item which is a C string. - unsafe fn get_cstr_item(&self, item_type: ItemType) -> Result<Option<&str>> { + unsafe fn get_cstr_item(&self, item_type: ItemType) -> Result<Option<String>> { let mut output = ptr::null(); let ret = unsafe { pam_ffi::pam_get_item(self, item_type as c_int, &mut output) }; ErrorCode::result_from(ret)?; - memory::wrap_string(output.cast()) + memory::copy_pam_string(output.cast()) } /// Sets a C string item. @@ -235,7 +235,7 @@ } }; (get = $get:ident$(, set = $set:ident)?) => { - delegate!(fn $get(&self) -> Result<Option<&str>>); + delegate!(fn $get(&self) -> Result<Option<String>>); $(delegate!(set = $set);)? }; (set = $set:ident) => { @@ -251,7 +251,7 @@ fn log(&self, level: Level, entry: &str) { self.handle.log(level, entry) } - delegate!(fn username(&mut self, prompt: Option<&str>) -> Result<&str>); + delegate!(fn username(&mut self, prompt: Option<&str>) -> Result<String>); delegate!(get = user_item, set = set_user_item); delegate!(get = service, set = set_service); delegate!(get = user_prompt, set = set_user_prompt);