comparison src/items.rs @ 60:05cc2c27334f

The Big Refactor: clean up docs and exports. - Brings the most important symbols in the library to the root with `pub use` statements. - Expands and updates documentation. - Rearranges things extensively to make the external interface nicer and make the structure easier to understand. - Renames a few things (e.g. `Result`).
author Paul Fisher <paul@pfish.zone>
date Wed, 21 May 2025 19:00:51 -0400
parents 3f4a77aa88be
children d83623951070
comparison
equal deleted inserted replaced
59:3f4a77aa88be 60:05cc2c27334f
1 //! Things that can be gotten with the `pam_get_item` function.
2
1 use crate::constants::InvalidEnum; 3 use crate::constants::InvalidEnum;
2 use num_derive::FromPrimitive; 4 use num_derive::FromPrimitive;
3 use num_traits::FromPrimitive; 5 use num_traits::FromPrimitive;
4 use std::ffi::{c_int, CStr}; 6 use std::ffi::{c_int, CStr};
5 7
8 /// Enum identifying what a `pam_get_item` return is.
9 ///
10 /// Generally, you shouldn’t have to worry about this, and instead
11 /// just use the various [Item] implementations.
6 #[derive(FromPrimitive)] 12 #[derive(FromPrimitive)]
7 #[repr(i32)] 13 #[repr(i32)]
8 #[non_exhaustive] // because C could give us anything! 14 #[non_exhaustive] // because C could give us anything!
9 pub enum ItemType { 15 pub enum ItemType {
10 /// The service name 16 /// The PAM service name.
11 Service = 1, 17 Service = 1,
12 /// The user name 18 /// The user's login name.
13 User = 2, 19 User = 2,
14 /// The tty name 20 /// The TTY name.
15 Tty = 3, 21 Tty = 3,
16 /// The remote host name 22 /// The remote host (if applicable).
17 RemoteHost = 4, 23 RemoteHost = 4,
18 /// The pam_conv structure 24 /// The [crate::Conversation] structure.
19 Conversation = 5, 25 Conversation = 5,
20 /// The authentication token (password) 26 /// The authentication token (password).
21 AuthTok = 6, 27 AuthTok = 6,
22 /// The old authentication token 28 /// The old authentication token (when changing passwords).
23 OldAuthTok = 7, 29 OldAuthTok = 7,
24 /// The remote user name 30 /// The remote user's name.
25 RemoteUser = 8, 31 RemoteUser = 8,
26 /// the prompt for getting a username 32 /// The prompt shown when requesting a username.
27 UserPrompt = 9, 33 UserPrompt = 9,
28 /// app supplied function to override failure delays 34 /// App-supplied function to override failure delays.
29 FailDelay = 10, 35 FailDelay = 10,
30 /// X :display name 36 /// X display name.
31 XDisplay = 11, 37 XDisplay = 11,
32 /// X :server authentication data 38 /// X server authentication data.
33 XAuthData = 12, 39 XAuthData = 12,
34 /// The type for pam_get_authtok 40 /// The type of `pam_get_authtok`.
35 AuthTokType = 13, 41 AuthTokType = 13,
36 } 42 }
37 43
38 impl TryFrom<c_int> for ItemType { 44 impl TryFrom<c_int> for ItemType {
39 type Error = InvalidEnum<Self>; 45 type Error = InvalidEnum<Self>;
46 fn from(val: ItemType) -> Self { 52 fn from(val: ItemType) -> Self {
47 val as Self 53 val as Self
48 } 54 }
49 } 55 }
50 56
51 /// A type that can be requested by [crate::Handle::get_item]. 57 /// A type that can be requested by [crate::PamHandle::get_item].
52 pub trait Item { 58 pub trait Item {
53 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. 59 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function.
54 type Raw; 60 type Raw;
55 61
56 /// The `ItemType` for this type 62 /// The `ItemType` for this type
67 fn into_raw(self) -> *const Self::Raw; 73 fn into_raw(self) -> *const Self::Raw;
68 } 74 }
69 75
70 macro_rules! cstr_item { 76 macro_rules! cstr_item {
71 ($name:ident) => { 77 ($name:ident) => {
72 ///A `CStr`-based item from a PAM conversation. 78 #[doc = concat!("The [ItemType::", stringify!($name), "]")]
79 #[doc = " item, represented as a [CStr]."]
73 #[derive(Debug)] 80 #[derive(Debug)]
74 pub struct $name<'s>(pub &'s CStr); 81 pub struct $name<'s>(pub &'s CStr);
75 82
76 impl<'s> std::ops::Deref for $name<'s> { 83 impl<'s> std::ops::Deref for $name<'s> {
77 type Target = &'s CStr; 84 type Target = &'s CStr;
96 } 103 }
97 } 104 }
98 }; 105 };
99 } 106 }
100 107
108 // Conversation is not included here since it's special.
109
101 cstr_item!(Service); 110 cstr_item!(Service);
102 cstr_item!(User); 111 cstr_item!(User);
103 cstr_item!(Tty); 112 cstr_item!(Tty);
104 cstr_item!(RemoteHost); 113 cstr_item!(RemoteHost);
105 // Conversation is not included here since it's special.
106 cstr_item!(AuthTok); 114 cstr_item!(AuthTok);
107 cstr_item!(OldAuthTok); 115 cstr_item!(OldAuthTok);
108 cstr_item!(RemoteUser); 116 cstr_item!(RemoteUser);
109 cstr_item!(UserPrompt); 117 cstr_item!(UserPrompt);