changeset 175:e30775c80b49

Separate #[cfg(feature = "link")] from other features.
author Paul Fisher <paul@pfish.zone>
date Wed, 30 Jul 2025 14:57:12 -0400
parents 9e4ce1631bd3
children 0730f5f2ee2a
files Cargo.toml src/constants.rs src/lib.rs src/libpam/handle.rs
diffstat 4 files changed, 41 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/Cargo.toml	Tue Jul 29 18:58:27 2025 -0400
+++ b/Cargo.toml	Wed Jul 30 14:57:12 2025 -0400
@@ -48,6 +48,7 @@
 num_enum = "0.7.3"
 libpam-sys = { optional = true, path = "libpam-sys" }
 libpam-sys-helpers = { path = "libpam-sys/libpam-sys-helpers" }
+libpam-sys-consts = { path = "libpam-sys/libpam-sys-consts" }
 
 [build-dependencies]
 libpam-sys-consts = { path = "libpam-sys/libpam-sys-consts" }
--- a/src/constants.rs	Tue Jul 29 18:58:27 2025 -0400
+++ b/src/constants.rs	Wed Jul 30 14:57:12 2025 -0400
@@ -3,9 +3,7 @@
 use crate::_doc::{linklist, man7, manbsd, mansun, xsso};
 use bitflags::bitflags;
 use std::error::Error;
-use std::ffi::c_int;
 use std::fmt;
-use std::fmt::{Display, Formatter};
 use std::result::Result as StdResult;
 
 macro_rules! wrapper {
@@ -33,11 +31,11 @@
 
 wrapper! {
     /// Type of the flags that PAM passes to us (or that we pass to PAM).
-    pub RawFlags(c_int);
+    pub RawFlags(i32);
 }
 wrapper! {
     /// The error code that we return to PAM.
-    pub ReturnCode(c_int);
+    pub ReturnCode(i32);
 }
 
 impl ReturnCode {
@@ -70,7 +68,7 @@
         impl From<RawFlags> for $name {
             #[allow(unused_doc_comments)]
             fn from(value: RawFlags) -> Self {
-                let value: c_int = value.into();
+                let value: i32 = value.into();
                 let result = Self::empty();
                 $(
                     $(#[$m_ident $($m_arg)*])*
@@ -128,7 +126,7 @@
         const CHANGE_EXPIRED_AUTHTOK = (link = libpam_sys::PAM_CHANGE_EXPIRED_AUTHTOK, else = 0b10);
 
         /// Don't check if the password is any good (Sun only).
-        #[cfg(pam_impl = "Sun")]
+        #[cfg(feature = "sun-ext")]
         const NO_AUTHTOK_CHECK = (link = libpam_sys::PAM_NO_AUTHTOK_CHECK, else = 0b100);
     }
 }
@@ -141,11 +139,6 @@
     }
 }
 
-#[cfg(feature = "openpam-ext")]
-const BAD_CONST: ErrorCode = ErrorCode::BadConstant;
-#[cfg(not(feature = "openpam-ext"))]
-const BAD_CONST: ErrorCode = ErrorCode::SystemError;
-
 macro_rules! flag_enum {
     (
         $(#[$m:meta])*
@@ -173,7 +166,7 @@
                     $(
                         $item_value => Ok(Self::$item_name),
                     )*
-                    _ => Err(BAD_CONST),
+                    _ => Err(ErrorCode::BAD_CONST),
                 }
             }
         }
@@ -198,7 +191,7 @@
                 let them = (value.0 & !Self::ALL_VALUES).into();
                 let me = match RawFlags(me) {
                     RawFlags(0) => None,
-                    other => Some(Self::try_from(other).map_err(|_| BAD_CONST)?),
+                    other => Some(Self::try_from(other).map_err(|_| ErrorCode::BAD_CONST)?),
                 };
                 Ok((me, them))
             }
@@ -254,7 +247,7 @@
     pub(crate) fn extract(value: RawFlags) -> Result<(Self, AuthtokFlags)> {
         match Self::split(value)? {
             (Some(act), rest) => Ok((act, AuthtokFlags::from(rest))),
-            (None, _) => Err(BAD_CONST),
+            (None, _) => Err(ErrorCode::BAD_CONST),
         }
     }
 }
@@ -288,7 +281,7 @@
                         $(#[$im])*
                         $value => Ok(Self::$key),
                     )*
-                    _ => Err(BAD_CONST),
+                    _ => Err(ErrorCode::BAD_CONST),
                 }
             }
         }
@@ -380,30 +373,32 @@
 /// A PAM-specific Result type with an [ErrorCode] error.
 pub type Result<T> = StdResult<T, ErrorCode>;
 
-impl Display for ErrorCode {
-    #[cfg(all(
-        feature = "link",
-        any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun")
-    ))]
-    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+#[cfg(feature = "link")]
+impl fmt::Display for ErrorCode {
+    #[cfg(any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun"))]
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         use std::ffi::CStr;
         use std::ptr;
         // SAFETY: PAM impls don't care about the PAM handle and always return
         // static strings.
-        let got = unsafe { libpam_sys::pam_strerror(ptr::null(), *self as c_int) };
+        let got = unsafe { libpam_sys::pam_strerror(ptr::null(), *self as i32) };
         if got.is_null() {
             // This shouldn't happen.
-            write!(f, "PAM error: {self:?} ({:?})", *self as c_int)
+            write!(f, "PAM error: {self:?} ({:?})", *self as i32)
         } else {
             // SAFETY: We just got this back from PAM and we checked if it's null.
             f.write_str(&unsafe { CStr::from_ptr(got) }.to_string_lossy())
         }
     }
-    #[cfg(not(all(
-        feature = "link",
-        any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun")
-    )))]
-    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+    #[cfg(not(any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun")))]
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Debug::fmt(self, f)
+    }
+}
+
+#[cfg(not(feature = "link"))]
+impl fmt::Display for ErrorCode {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt::Debug::fmt(self, f)
     }
 }
@@ -412,14 +407,21 @@
 
 #[cfg(feature = "link")]
 impl ErrorCode {
-    pub(crate) fn result_from(ret: c_int) -> Result<()> {
+    #[cfg(feature = "openpam-ext")]
+    const BAD_CONST: ErrorCode = ErrorCode::BadConstant;
+    #[cfg(not(feature = "openpam-ext"))]
+    const BAD_CONST: ErrorCode = ErrorCode::SystemError;
+
+
+    pub(crate) fn result_from(ret: i32) -> Result<()> {
         match ret {
             0 => Ok(()),
-            value => Err(ReturnCode(value).try_into().unwrap_or(BAD_CONST)),
+            value => Err(ReturnCode(value).try_into().unwrap_or(Self::BAD_CONST)),
         }
     }
 }
 
+#[cfg(feature = "link")]
 impl<T> From<Result<T>> for ReturnCode {
     fn from(value: Result<T>) -> Self {
         match value {
@@ -444,7 +446,7 @@
             Result::<()>::Err(ErrorCode::Abort),
             ErrorCode::result_from(libpam_sys::PAM_ABORT)
         );
-        assert_eq!(Err(BAD_CONST), ErrorCode::result_from(423));
+        assert_eq!(Err(ErrorCode::BAD_CONST), ErrorCode::result_from(423));
     }
 
     #[test]
--- a/src/lib.rs	Tue Jul 29 18:58:27 2025 -0400
+++ b/src/lib.rs	Wed Jul 30 14:57:12 2025 -0400
@@ -218,4 +218,4 @@
     handle::{ModuleClient, PamShared, Transaction},
     module::PamModule,
 };
-use libpam_sys::pam_impl_name;
+use libpam_sys_consts::pam_impl_name;
--- a/src/libpam/handle.rs	Tue Jul 29 18:58:27 2025 -0400
+++ b/src/libpam/handle.rs	Wed Jul 30 14:57:12 2025 -0400
@@ -504,6 +504,12 @@
         unsafe { items::get_cstr_item(self, item_type) }?.ok_or(ErrorCode::ConversationError)
     }
 
+    #[cfg(not(any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun")))]
+    fn get_authtok(&mut self, _: Option<&OsStr>, _: ItemType) -> Result<OsString> {
+        // We don't have an authtok implementation for other PAM implementations.
+        Err(ErrorCode::ConversationError)
+    }
+
     /// Gets the `PAM_CONV` item from the handle.
     fn conversation_item(&self) -> Result<&PamConv> {
         let mut output: *const c_void = ptr::null();