annotate src/libpam/items.rs @ 146:1bc52025156b

Split PAM items into their own separate struct. To trim down the number of methods on `PamShared`, this puts all the Items into their own struct(s). This also makes the split between authtok/authtok_item easier to understand.
author Paul Fisher <paul@pfish.zone>
date Sun, 06 Jul 2025 19:10:26 -0400
parents
children
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.
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 pub unsafe fn get_cstr_item(
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 hdl: &LibPamHandle,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 item_type: ItemType,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 ) -> crate::Result<Option<OsString>> {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 let mut output = ptr::null();
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 let ret = unsafe { libpam_sys::pam_get_item(hdl.raw_ref(), item_type as c_int, &mut output) };
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 ErrorCode::result_from(ret)?;
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 Ok(memory::copy_pam_string(output.cast()))
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
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 /// Sets a C string item.
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 ///
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73 /// # Safety
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
74 ///
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
75 /// 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
76 pub unsafe fn set_cstr_item(
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77 hdl: &mut LibPamHandle,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
78 item_type: ItemType,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
79 data: Option<&OsStr>,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80 ) -> crate::Result<()> {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81 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
82 let ret = unsafe {
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 libpam_sys::pam_set_item(
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 hdl.raw_mut(),
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 item_type as c_int,
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 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
87 )
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
88 };
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
89 ErrorCode::result_from(ret)
1bc52025156b Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
90 }