Mercurial > crates > nonstick
annotate src/libpam/items.rs @ 180:a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 30 Jul 2025 18:22:16 -0400 |
parents | a75a66cb4181 |
children |
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::LibPamHandle; |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 use crate::libpam::memory; |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 use std::ffi::{c_int, OsStr, OsString}; |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 use std::ptr; |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
8 |
180
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
9 memory::num_enum! { |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
10 /// Identifies what is being gotten or set with `pam_get_item` |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
11 /// or `pam_set_item`. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
12 #[non_exhaustive] |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
13 pub enum ItemType(i32) { |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
14 /// The PAM service name. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
15 Service = libpam_sys::PAM_SERVICE, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
16 /// The user's login name. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
17 User = libpam_sys::PAM_USER, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
18 /// The TTY name. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
19 Tty = libpam_sys::PAM_TTY, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
20 /// The remote host (if applicable). |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
21 RemoteHost = libpam_sys::PAM_RHOST, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
22 /// The conversation struct (not a CStr-based item). |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
23 Conversation = libpam_sys::PAM_CONV, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
24 /// The authentication token (password). |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
25 AuthTok = libpam_sys::PAM_AUTHTOK, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
26 /// The old authentication token (when changing passwords). |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
27 OldAuthTok = libpam_sys::PAM_OLDAUTHTOK, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
28 /// The remote user's name. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
29 RemoteUser = libpam_sys::PAM_RUSER, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
30 /// The prompt shown when requesting a username. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
31 UserPrompt = libpam_sys::PAM_USER_PROMPT, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
32 #[cfg(feature = "linux-pam-ext")] |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
33 /// App-supplied function to override failure delays. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
34 FailDelay = libpam_sys::PAM_FAIL_DELAY, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
35 #[cfg(feature = "linux-pam-ext")] |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
36 /// X display name. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
37 XDisplay = libpam_sys::PAM_XDISPLAY, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
38 #[cfg(feature = "linux-pam-ext")] |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
39 /// X server authentication data. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
40 XAuthData = libpam_sys::PAM_XAUTHDATA, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
41 #[cfg(feature = "linux-pam-ext")] |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
42 /// The type of `pam_get_authtok`. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
43 AuthTokType = libpam_sys::PAM_AUTHTOK_TYPE, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
44 } |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
45 } |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
46 |
146
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
47 pub struct LibPamItems<'a>(pub &'a LibPamHandle); |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 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
|
49 |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 /// 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
|
51 macro_rules! cstr_item { |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
52 (get = $getter:ident, item = $item_type:path) => { |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
53 fn $getter(&self) -> Result<Option<OsString>> { |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
54 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
|
55 } |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 }; |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 (set = $setter:ident, item = $item_type:path) => { |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 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
|
59 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
|
60 } |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 }; |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 } |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 impl Items<'_> for LibPamItems<'_> { |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 cstr_item!(get = user, item = ItemType::User); |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 cstr_item!(get = service, item = ItemType::Service); |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 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
|
71 } |
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 impl Items<'_> for LibPamItemsMut<'_> { |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
74 cstr_item!(get = user, item = ItemType::User); |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 cstr_item!(get = service, item = ItemType::Service); |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 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
|
80 } |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
81 |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
82 impl ItemsMut<'_> for LibPamItemsMut<'_> { |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 } |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
92 |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
93 /// Gets a C string item. |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
94 /// |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
95 /// # Safety |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 /// |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 /// 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
|
98 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
|
99 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
|
100 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
|
101 ErrorCode::result_from(ret)?; |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
102 Ok(memory::copy_pam_string(output.cast())) |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
103 } |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
104 |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
105 /// Sets a C string item. |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
106 /// |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
107 /// # Safety |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
108 /// |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
109 /// 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
|
110 pub unsafe fn set_cstr_item( |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
111 hdl: &mut LibPamHandle, |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
112 item_type: ItemType, |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
113 data: Option<&OsStr>, |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
146
diff
changeset
|
114 ) -> Result<()> { |
146
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
115 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
|
116 let ret = unsafe { |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
117 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
|
118 hdl.inner_mut(), |
146
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
119 item_type as c_int, |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
120 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
|
121 ) |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
122 }; |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
123 ErrorCode::result_from(ret) |
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
124 } |