diff 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
line wrap: on
line diff
--- a/src/items.rs	Sun May 04 00:58:04 2025 -0400
+++ b/src/items.rs	Sun May 04 02:56:55 2025 -0400
@@ -1,7 +1,12 @@
-use std::ffi::CStr;
+use num_derive::{FromPrimitive, ToPrimitive};
+use num_traits::{FromPrimitive, ToPrimitive};
+use std::ffi::{c_int, CStr};
 
-#[repr(u32)]
+#[derive(FromPrimitive, ToPrimitive)]
+#[repr(i32)]
 pub enum ItemType {
+    /// Unset. This should never be used.
+    Unset = 0,
     /// The service name
     Service = 1,
     /// The user name
@@ -9,15 +14,15 @@
     /// The tty name
     Tty = 3,
     /// The remote host name
-    RHost = 4,
+    RemoteHost = 4,
     /// The pam_conv structure
-    Conv = 5,
+    Conversation = 5,
     /// The authentication token (password)
     AuthTok = 6,
     /// The old authentication token
     OldAuthTok = 7,
     /// The remote user name
-    RUser = 8,
+    RemoteUser = 8,
     /// the prompt for getting a username
     UserPrompt = 9,
     /// app supplied function to override failure delays
@@ -30,6 +35,18 @@
     AuthTokType = 13,
 }
 
+impl From<c_int> for ItemType {
+    fn from(value: c_int) -> Self {
+        Self::from_i32(value).unwrap_or(Self::Unset)
+    }
+}
+
+impl From<ItemType> for c_int {
+    fn from(val: ItemType) -> Self {
+        val.to_i32().unwrap_or(0)
+    }
+}
+
 // A type that can be requested by `pam::Handle::get_item`.
 pub trait Item {
     /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function.
@@ -83,9 +100,9 @@
 cstr_item!(Service);
 cstr_item!(User);
 cstr_item!(Tty);
-cstr_item!(RHost);
+cstr_item!(RemoteHost);
 // Conv
 cstr_item!(AuthTok);
 cstr_item!(OldAuthTok);
-cstr_item!(RUser);
+cstr_item!(RemoteUser);
 cstr_item!(UserPrompt);