Mercurial > crates > nonstick
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 55:676675c3d434 | 56:daa2cde64601 |
|---|---|
| 1 use std::ffi::CStr; | 1 use num_derive::{FromPrimitive, ToPrimitive}; |
| 2 use num_traits::{FromPrimitive, ToPrimitive}; | |
| 3 use std::ffi::{c_int, CStr}; | |
| 2 | 4 |
| 3 #[repr(u32)] | 5 #[derive(FromPrimitive, ToPrimitive)] |
| 6 #[repr(i32)] | |
| 4 pub enum ItemType { | 7 pub enum ItemType { |
| 8 /// Unset. This should never be used. | |
| 9 Unset = 0, | |
| 5 /// The service name | 10 /// The service name |
| 6 Service = 1, | 11 Service = 1, |
| 7 /// The user name | 12 /// The user name |
| 8 User = 2, | 13 User = 2, |
| 9 /// The tty name | 14 /// The tty name |
| 10 Tty = 3, | 15 Tty = 3, |
| 11 /// The remote host name | 16 /// The remote host name |
| 12 RHost = 4, | 17 RemoteHost = 4, |
| 13 /// The pam_conv structure | 18 /// The pam_conv structure |
| 14 Conv = 5, | 19 Conversation = 5, |
| 15 /// The authentication token (password) | 20 /// The authentication token (password) |
| 16 AuthTok = 6, | 21 AuthTok = 6, |
| 17 /// The old authentication token | 22 /// The old authentication token |
| 18 OldAuthTok = 7, | 23 OldAuthTok = 7, |
| 19 /// The remote user name | 24 /// The remote user name |
| 20 RUser = 8, | 25 RemoteUser = 8, |
| 21 /// the prompt for getting a username | 26 /// the prompt for getting a username |
| 22 UserPrompt = 9, | 27 UserPrompt = 9, |
| 23 /// app supplied function to override failure delays | 28 /// app supplied function to override failure delays |
| 24 FailDelay = 10, | 29 FailDelay = 10, |
| 25 /// X :display name | 30 /// X :display name |
| 26 XDisplay = 11, | 31 XDisplay = 11, |
| 27 /// X :server authentication data | 32 /// X :server authentication data |
| 28 XAuthData = 12, | 33 XAuthData = 12, |
| 29 /// The type for pam_get_authtok | 34 /// The type for pam_get_authtok |
| 30 AuthTokType = 13, | 35 AuthTokType = 13, |
| 36 } | |
| 37 | |
| 38 impl From<c_int> for ItemType { | |
| 39 fn from(value: c_int) -> Self { | |
| 40 Self::from_i32(value).unwrap_or(Self::Unset) | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 impl From<ItemType> for c_int { | |
| 45 fn from(val: ItemType) -> Self { | |
| 46 val.to_i32().unwrap_or(0) | |
| 47 } | |
| 31 } | 48 } |
| 32 | 49 |
| 33 // A type that can be requested by `pam::Handle::get_item`. | 50 // A type that can be requested by `pam::Handle::get_item`. |
| 34 pub trait Item { | 51 pub trait Item { |
| 35 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. | 52 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. |
| 81 } | 98 } |
| 82 | 99 |
| 83 cstr_item!(Service); | 100 cstr_item!(Service); |
| 84 cstr_item!(User); | 101 cstr_item!(User); |
| 85 cstr_item!(Tty); | 102 cstr_item!(Tty); |
| 86 cstr_item!(RHost); | 103 cstr_item!(RemoteHost); |
| 87 // Conv | 104 // Conv |
| 88 cstr_item!(AuthTok); | 105 cstr_item!(AuthTok); |
| 89 cstr_item!(OldAuthTok); | 106 cstr_item!(OldAuthTok); |
| 90 cstr_item!(RUser); | 107 cstr_item!(RemoteUser); |
| 91 cstr_item!(UserPrompt); | 108 cstr_item!(UserPrompt); |
