diff src/libpam/handle.rs @ 171:e27c5c667a5a

Create full new types for return code and flags, separate end to end. This plumbs the ReturnCode and RawFlags types through the places where we call into or are called from PAM. Also adds Sun documentation to the project.
author Paul Fisher <paul@pfish.zone>
date Fri, 25 Jul 2025 20:52:14 -0400
parents 77470e45e397
children 9e4ce1631bd3
line wrap: on
line diff
--- a/src/libpam/handle.rs	Wed Jul 16 18:45:20 2025 -0400
+++ b/src/libpam/handle.rs	Fri Jul 25 20:52:14 2025 -0400
@@ -1,6 +1,6 @@
 use super::conversation::{OwnedConversation, PamConv};
 use crate::_doc::{guide, linklist, man7, stdlinks};
-use crate::constants::{ErrorCode, Result};
+use crate::constants::{ErrorCode, RawFlags, Result, ReturnCode};
 use crate::conv::Exchange;
 use crate::environ::EnvironMapMut;
 use crate::handle::PamShared;
@@ -10,7 +10,6 @@
 use crate::libpam::{items, memory};
 use crate::logging::{Level, Location, Logger};
 use crate::{AuthnFlags, AuthtokFlags, Conversation, EnvironMap, ModuleClient, Transaction};
-use libpam_sys_consts::constants;
 use num_enum::{IntoPrimitive, TryFromPrimitive};
 use std::any::TypeId;
 use std::cell::Cell;
@@ -146,7 +145,8 @@
 
     /// Internal "end" function, which binary-ORs the status with `or_with`.
     fn end_internal(&mut self, or_with: i32) {
-        let result = ErrorCode::result_to_c(self.last_return.get()) | or_with;
+        let last: i32 = ReturnCode::from(self.last_return.get()).into();
+        let result = last | or_with;
         unsafe { libpam_sys::pam_end(self.handle.cast(), result) };
     }
 }
@@ -154,8 +154,9 @@
 macro_rules! wrap {
     (fn $name:ident($ftype:ident) { $pam_func:ident }) => {
         fn $name(&mut self, flags: $ftype) -> Result<()> {
+            let flags: RawFlags = flags.into();
             ErrorCode::result_from(unsafe {
-                libpam_sys::$pam_func((self as *mut Self).cast(), flags.bits())
+                libpam_sys::$pam_func((self as *mut Self).cast(), flags.into())
             })
         }
     };
@@ -265,7 +266,8 @@
     #[doc = guide!(adg: "adg-interface-by-app-expected.html#adg-pam_end")]
     #[doc = stdlinks!(3 pam_end)]
     pub fn end(&mut self, result: Result<()>) {
-        unsafe { libpam_sys::pam_end(self.inner_mut(), ErrorCode::result_to_c(result)) };
+        let code: ReturnCode = result.into();
+        unsafe { libpam_sys::pam_end(self.inner_mut(), code.into()) };
     }
 
     #[cfg_attr(
@@ -290,7 +292,7 @@
     #[doc = guide!(adg: "adg-interface-by-app-expected.html#adg-pam_end")]
     #[doc = stdlinks!(3 pam_end)]
     pub fn end_silent(&mut self, result: Result<()>) {
-        let result = ErrorCode::result_to_c(result);
+        let result: i32 = ReturnCode::from(result).into();
         #[cfg(pam_impl = "LinuxPam")]
         let result = result | libpam_sys::PAM_DATA_SILENT;
         unsafe {
@@ -517,33 +519,33 @@
 #[non_exhaustive] // because C could give us anything!
 pub enum ItemType {
     /// The PAM service name.
-    Service = constants::PAM_SERVICE,
+    Service = libpam_sys::PAM_SERVICE,
     /// The user's login name.
-    User = constants::PAM_USER,
+    User = libpam_sys::PAM_USER,
     /// The TTY name.
-    Tty = constants::PAM_TTY,
+    Tty = libpam_sys::PAM_TTY,
     /// The remote host (if applicable).
-    RemoteHost = constants::PAM_RHOST,
+    RemoteHost = libpam_sys::PAM_RHOST,
     /// The conversation struct (not a CStr-based item).
-    Conversation = constants::PAM_CONV,
+    Conversation = libpam_sys::PAM_CONV,
     /// The authentication token (password).
-    AuthTok = constants::PAM_AUTHTOK,
+    AuthTok = libpam_sys::PAM_AUTHTOK,
     /// The old authentication token (when changing passwords).
-    OldAuthTok = constants::PAM_OLDAUTHTOK,
+    OldAuthTok = libpam_sys::PAM_OLDAUTHTOK,
     /// The remote user's name.
-    RemoteUser = constants::PAM_RUSER,
+    RemoteUser = libpam_sys::PAM_RUSER,
     /// The prompt shown when requesting a username.
-    UserPrompt = constants::PAM_USER_PROMPT,
+    UserPrompt = libpam_sys::PAM_USER_PROMPT,
     #[cfg(feature = "linux-pam-ext")]
     /// App-supplied function to override failure delays.
-    FailDelay = constants::PAM_FAIL_DELAY,
+    FailDelay = libpam_sys::PAM_FAIL_DELAY,
     #[cfg(feature = "linux-pam-ext")]
     /// X display name.
-    XDisplay = constants::PAM_XDISPLAY,
+    XDisplay = libpam_sys::PAM_XDISPLAY,
     #[cfg(feature = "linux-pam-ext")]
     /// X server authentication data.
-    XAuthData = constants::PAM_XAUTHDATA,
+    XAuthData = libpam_sys::PAM_XAUTHDATA,
     #[cfg(feature = "linux-pam-ext")]
     /// The type of `pam_get_authtok`.
-    AuthTokType = constants::PAM_AUTHTOK_TYPE,
+    AuthTokType = libpam_sys::PAM_AUTHTOK_TYPE,
 }