annotate src/libpam/items.rs @ 163:a75a66cb4181

Add end-to-end tests; fix issues found by tests. - Create tests and installer/remover shell script - Fix Pointer/pointee problems - Add Debug formatting - Misc cleanup
author Paul Fisher <paul@pfish.zone>
date Mon, 14 Jul 2025 17:40:11 -0400
parents 1bc52025156b
children a1bb1d013567
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
146
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 use crate::constants::ErrorCode;
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2 use crate::constants::Result;
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 use crate::items::{Items, ItemsMut};
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4 use crate::libpam::handle::ItemType;
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 use crate::libpam::handle::LibPamHandle;
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 use crate::libpam::memory;
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 use std::ffi::{c_int, OsStr, OsString};
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 use std::ptr;
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 pub struct LibPamItems<'a>(pub &'a LibPamHandle);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 pub struct LibPamItemsMut<'a>(pub &'a mut LibPamHandle);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13 /// Macro to implement getting/setting a CStr-based item.
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 macro_rules! cstr_item {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 (get = $getter:ident, item = $item_type:path) => {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 fn $getter(&self) -> Result<Option<OsString>> {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 unsafe { get_cstr_item(&self.0, $item_type) }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 };
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 (set = $setter:ident, item = $item_type:path) => {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 fn $setter(&mut self, value: Option<&OsStr>) -> Result<()> {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 unsafe { set_cstr_item(&mut self.0, $item_type, value) }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 };
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 impl Items<'_> for LibPamItems<'_> {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 cstr_item!(get = user, item = ItemType::User);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 cstr_item!(get = service, item = ItemType::Service);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 cstr_item!(get = user_prompt, item = ItemType::UserPrompt);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31 cstr_item!(get = tty_name, item = ItemType::Tty);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 cstr_item!(get = remote_user, item = ItemType::RemoteUser);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 cstr_item!(get = remote_host, item = ItemType::RemoteHost);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 impl Items<'_> for LibPamItemsMut<'_> {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37 cstr_item!(get = user, item = ItemType::User);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 cstr_item!(get = service, item = ItemType::Service);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 cstr_item!(get = user_prompt, item = ItemType::UserPrompt);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 cstr_item!(get = tty_name, item = ItemType::Tty);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
41 cstr_item!(get = remote_user, item = ItemType::RemoteUser);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42 cstr_item!(get = remote_host, item = ItemType::RemoteHost);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
44
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45 impl ItemsMut<'_> for LibPamItemsMut<'_> {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 cstr_item!(set = set_user, item = ItemType::User);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 cstr_item!(set = set_service, item = ItemType::Service);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 cstr_item!(set = set_user_prompt, item = ItemType::UserPrompt);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 cstr_item!(set = set_tty_name, item = ItemType::Tty);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 cstr_item!(set = set_remote_user, item = ItemType::RemoteUser);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 cstr_item!(set = set_remote_host, item = ItemType::RemoteHost);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 cstr_item!(set = set_authtok, item = ItemType::AuthTok);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
53 cstr_item!(set = set_old_authtok, item = ItemType::OldAuthTok);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
56 /// Gets a C string item.
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 ///
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 /// # Safety
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 ///
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 /// You better be requesting an item which is a C string.
163
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 146
diff changeset
61 pub unsafe fn get_cstr_item(hdl: &LibPamHandle, item_type: ItemType) -> Result<Option<OsString>> {
146
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 let mut output = ptr::null();
163
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 146
diff changeset
63 let ret = unsafe { libpam_sys::pam_get_item(hdl.inner(), item_type as c_int, &mut output) };
146
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 ErrorCode::result_from(ret)?;
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 Ok(memory::copy_pam_string(output.cast()))
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 }
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 /// Sets a C string item.
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69 ///
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 /// # Safety
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 ///
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 /// You better be setting an item which is a C string.
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73 pub unsafe fn set_cstr_item(
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
74 hdl: &mut LibPamHandle,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
75 item_type: ItemType,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
76 data: Option<&OsStr>,
163
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 146
diff changeset
77 ) -> Result<()> {
146
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
78 let data_str = memory::option_cstr_os(data);
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
79 let ret = unsafe {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80 libpam_sys::pam_set_item(
163
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 146
diff changeset
81 hdl.inner_mut(),
146
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82 item_type as c_int,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 memory::prompt_ptr(data_str.as_deref()).cast(),
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 )
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 };
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 ErrorCode::result_from(ret)
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
87 }