Mercurial > crates > nonstick
annotate src/items.rs @ 56:daa2cde64601
Big big refactor. Probably should have been multiple changes.
- Makes FFI safer by explicitly specifying c_int in calls.
- Uses ToPrimitive/FromPrimitive to make this easier.
- Pulls PamFlag variables into a bitflags! struct.
- Pulls PamMessageStyle variables into an enum.
- Renames ResultCode to ErrorCode.
- Switches from PAM_SUCCESS to using a Result<(), ErrorCode>.
- Uses thiserror to make ErrorCode into an Error.
- Gets rid of pam_try! because now we have Results.
- Expands some names (e.g. Conv to Conversation).
- Adds more doc comments.
- Returns passwords as a SecureString, to avoid unnecessarily
keeping it around in memory.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sun, 04 May 2025 02:56:55 -0400 |
| parents | 9d1160b02d2c |
| children | 3f4a77aa88be |
| rev | line source |
|---|---|
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
1 use num_derive::{FromPrimitive, ToPrimitive}; |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
2 use num_traits::{FromPrimitive, ToPrimitive}; |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
3 use std::ffi::{c_int, CStr}; |
| 51 | 4 |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
5 #[derive(FromPrimitive, ToPrimitive)] |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
6 #[repr(i32)] |
| 34 | 7 pub enum ItemType { |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
8 /// Unset. This should never be used. |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
9 Unset = 0, |
| 34 | 10 /// The service name |
| 11 Service = 1, | |
| 12 /// The user name | |
| 13 User = 2, | |
| 14 /// The tty name | |
| 15 Tty = 3, | |
| 16 /// The remote host name | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
17 RemoteHost = 4, |
| 34 | 18 /// The pam_conv structure |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
19 Conversation = 5, |
| 34 | 20 /// The authentication token (password) |
| 21 AuthTok = 6, | |
| 22 /// The old authentication token | |
| 23 OldAuthTok = 7, | |
| 24 /// The remote user name | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
25 RemoteUser = 8, |
| 34 | 26 /// the prompt for getting a username |
| 27 UserPrompt = 9, | |
| 28 /// app supplied function to override failure delays | |
| 29 FailDelay = 10, | |
| 30 /// X :display name | |
| 31 XDisplay = 11, | |
| 32 /// X :server authentication data | |
| 33 XAuthData = 12, | |
| 34 /// The type for pam_get_authtok | |
| 35 AuthTokType = 13, | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
36 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
37 |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
38 impl From<c_int> for ItemType { |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
39 fn from(value: c_int) -> Self { |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
40 Self::from_i32(value).unwrap_or(Self::Unset) |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
41 } |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
42 } |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
43 |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
44 impl From<ItemType> for c_int { |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
45 fn from(val: ItemType) -> Self { |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
46 val.to_i32().unwrap_or(0) |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
47 } |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
48 } |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
49 |
| 34 | 50 // A type that can be requested by `pam::Handle::get_item`. |
| 51 pub trait Item { | |
| 52 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. | |
| 53 type Raw; | |
| 54 | |
| 55 /// The `ItemType` for this type | |
| 56 fn type_id() -> ItemType; | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
57 |
| 51 | 58 /// The function to convert from the pointer to the C-representation to this safer wrapper type. |
| 34 | 59 /// |
| 60 /// # Safety | |
| 61 /// | |
| 51 | 62 /// This function assumes the pointer is a valid pointer to a `Self::Raw` instance. |
| 34 | 63 unsafe fn from_raw(raw: *const Self::Raw) -> Self; |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
64 |
| 34 | 65 /// The function to convert from this wrapper type to a C-compatible pointer. |
| 66 fn into_raw(self) -> *const Self::Raw; | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
67 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
68 |
| 34 | 69 macro_rules! cstr_item { |
| 70 ($name:ident) => { | |
| 51 | 71 ///A `CStr`-based item from a PAM conversation. |
| 34 | 72 #[derive(Debug)] |
| 51 | 73 pub struct $name<'s>(pub &'s CStr); |
| 34 | 74 |
| 75 impl<'s> std::ops::Deref for $name<'s> { | |
| 51 | 76 type Target = &'s CStr; |
| 34 | 77 fn deref(&self) -> &Self::Target { |
| 78 &self.0 | |
| 79 } | |
| 80 } | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
81 |
| 34 | 82 impl<'s> Item for $name<'s> { |
| 83 type Raw = libc::c_char; | |
| 84 | |
| 85 fn type_id() -> ItemType { | |
| 86 ItemType::$name | |
| 87 } | |
| 88 | |
| 89 unsafe fn from_raw(raw: *const Self::Raw) -> Self { | |
| 90 Self(std::ffi::CStr::from_ptr(raw)) | |
| 91 } | |
| 92 | |
| 93 fn into_raw(self) -> *const Self::Raw { | |
| 94 self.0.as_ptr() | |
| 95 } | |
| 96 } | |
| 97 }; | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
98 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
99 |
| 34 | 100 cstr_item!(Service); |
| 101 cstr_item!(User); | |
| 102 cstr_item!(Tty); | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
103 cstr_item!(RemoteHost); |
| 34 | 104 // Conv |
| 105 cstr_item!(AuthTok); | |
| 106 cstr_item!(OldAuthTok); | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
107 cstr_item!(RemoteUser); |
| 34 | 108 cstr_item!(UserPrompt); |
