Mercurial > crates > nonstick
annotate src/items.rs @ 53:52f1102e0715 v0.0.3
Bump version to v0.0.3.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sat, 03 May 2025 18:42:07 -0400 |
| parents | 9d1160b02d2c |
| children | daa2cde64601 |
| rev | line source |
|---|---|
| 51 | 1 use std::ffi::CStr; |
| 2 | |
| 34 | 3 #[repr(u32)] |
| 4 pub enum ItemType { | |
| 5 /// The service name | |
| 6 Service = 1, | |
| 7 /// The user name | |
| 8 User = 2, | |
| 9 /// The tty name | |
| 10 Tty = 3, | |
| 11 /// The remote host name | |
| 12 RHost = 4, | |
| 13 /// The pam_conv structure | |
| 14 Conv = 5, | |
| 15 /// The authentication token (password) | |
| 16 AuthTok = 6, | |
| 17 /// The old authentication token | |
| 18 OldAuthTok = 7, | |
| 19 /// The remote user name | |
| 20 RUser = 8, | |
| 21 /// the prompt for getting a username | |
| 22 UserPrompt = 9, | |
| 23 /// app supplied function to override failure delays | |
| 24 FailDelay = 10, | |
| 25 /// X :display name | |
| 26 XDisplay = 11, | |
| 27 /// X :server authentication data | |
| 28 XAuthData = 12, | |
| 29 /// The type for pam_get_authtok | |
| 30 AuthTokType = 13, | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
31 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
32 |
| 34 | 33 // A type that can be requested by `pam::Handle::get_item`. |
| 34 pub trait Item { | |
| 35 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. | |
| 36 type Raw; | |
| 37 | |
| 38 /// The `ItemType` for this type | |
| 39 fn type_id() -> ItemType; | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
40 |
| 51 | 41 /// The function to convert from the pointer to the C-representation to this safer wrapper type. |
| 34 | 42 /// |
| 43 /// # Safety | |
| 44 /// | |
| 51 | 45 /// This function assumes the pointer is a valid pointer to a `Self::Raw` instance. |
| 34 | 46 unsafe fn from_raw(raw: *const Self::Raw) -> Self; |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
47 |
| 34 | 48 /// The function to convert from this wrapper type to a C-compatible pointer. |
| 49 fn into_raw(self) -> *const Self::Raw; | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
50 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
51 |
| 34 | 52 macro_rules! cstr_item { |
| 53 ($name:ident) => { | |
| 51 | 54 ///A `CStr`-based item from a PAM conversation. |
| 34 | 55 #[derive(Debug)] |
| 51 | 56 pub struct $name<'s>(pub &'s CStr); |
| 34 | 57 |
| 58 impl<'s> std::ops::Deref for $name<'s> { | |
| 51 | 59 type Target = &'s CStr; |
| 34 | 60 fn deref(&self) -> &Self::Target { |
| 61 &self.0 | |
| 62 } | |
| 63 } | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
64 |
| 34 | 65 impl<'s> Item for $name<'s> { |
| 66 type Raw = libc::c_char; | |
| 67 | |
| 68 fn type_id() -> ItemType { | |
| 69 ItemType::$name | |
| 70 } | |
| 71 | |
| 72 unsafe fn from_raw(raw: *const Self::Raw) -> Self { | |
| 73 Self(std::ffi::CStr::from_ptr(raw)) | |
| 74 } | |
| 75 | |
| 76 fn into_raw(self) -> *const Self::Raw { | |
| 77 self.0.as_ptr() | |
| 78 } | |
| 79 } | |
| 80 }; | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
81 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
82 |
| 34 | 83 cstr_item!(Service); |
| 84 cstr_item!(User); | |
| 85 cstr_item!(Tty); | |
| 86 cstr_item!(RHost); | |
| 87 // Conv | |
| 88 cstr_item!(AuthTok); | |
| 89 cstr_item!(OldAuthTok); | |
| 90 cstr_item!(RUser); | |
| 91 cstr_item!(UserPrompt); |
