comparison src/items.rs @ 45:ce47901aab7a

Rename to “nonstick”, move to root, update docs and license. - Renames the crate to “nonstick”. - Moves the main library to the root of the repository. - Removes the example PAM modules. - Updates copyright information in LICENSE file. - Updates the README.
author Paul Fisher <paul@pfish.zone>
date Tue, 15 Apr 2025 00:50:23 -0400
parents pam/src/items.rs@ec70822cbdef
children
comparison
equal deleted inserted replaced
44:50371046c61a 45:ce47901aab7a
1 #[repr(u32)]
2 pub enum ItemType {
3 /// The service name
4 Service = 1,
5 /// The user name
6 User = 2,
7 /// The tty name
8 Tty = 3,
9 /// The remote host name
10 RHost = 4,
11 /// The pam_conv structure
12 Conv = 5,
13 /// The authentication token (password)
14 AuthTok = 6,
15 /// The old authentication token
16 OldAuthTok = 7,
17 /// The remote user name
18 RUser = 8,
19 /// the prompt for getting a username
20 UserPrompt = 9,
21 /// app supplied function to override failure delays
22 FailDelay = 10,
23 /// X :display name
24 XDisplay = 11,
25 /// X :server authentication data
26 XAuthData = 12,
27 /// The type for pam_get_authtok
28 AuthTokType = 13,
29 }
30
31 // A type that can be requested by `pam::Handle::get_item`.
32 pub trait Item {
33 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function.
34 type Raw;
35
36 /// The `ItemType` for this type
37 fn type_id() -> ItemType;
38
39 /// The function to convert from the pointer to the C-representation to this safer wrapper type
40 ///
41 /// # Safety
42 ///
43 /// This function can assume the pointer is a valid pointer to a `Self::Raw` instance.
44 unsafe fn from_raw(raw: *const Self::Raw) -> Self;
45
46 /// The function to convert from this wrapper type to a C-compatible pointer.
47 fn into_raw(self) -> *const Self::Raw;
48 }
49
50 macro_rules! cstr_item {
51 ($name:ident) => {
52 #[derive(Debug)]
53 pub struct $name<'s>(pub &'s std::ffi::CStr);
54
55 impl<'s> std::ops::Deref for $name<'s> {
56 type Target = &'s std::ffi::CStr;
57 fn deref(&self) -> &Self::Target {
58 &self.0
59 }
60 }
61
62 impl<'s> Item for $name<'s> {
63 type Raw = libc::c_char;
64
65 fn type_id() -> ItemType {
66 ItemType::$name
67 }
68
69 unsafe fn from_raw(raw: *const Self::Raw) -> Self {
70 Self(std::ffi::CStr::from_ptr(raw))
71 }
72
73 fn into_raw(self) -> *const Self::Raw {
74 self.0.as_ptr()
75 }
76 }
77 };
78 }
79
80 cstr_item!(Service);
81 cstr_item!(User);
82 cstr_item!(Tty);
83 cstr_item!(RHost);
84 // Conv
85 cstr_item!(AuthTok);
86 cstr_item!(OldAuthTok);
87 cstr_item!(RUser);
88 cstr_item!(UserPrompt);