view libpam-sys/src/constants.rs @ 108:e97534be35e3

Make some proc macros for doing cfg-like stuff for PAM impls.
author Paul Fisher <paul@pfish.zone>
date Sat, 28 Jun 2025 00:34:45 -0400
parents 49d9e2b5c189
children bb465393621f
line wrap: on
line source

//! All the constants.

// We have to enable these otherwise we get lit up with warnings
// during conditional compilation.
#![allow(dead_code, unused_imports)]

/// Macro to make defining a bunch of constants way easier.
macro_rules! define {
    ($(#[$attr:meta])* $($name:ident = $value:expr);+$(;)?) => {
        define!(
            @meta { $(#[$attr])* }
            $(pub const $name: u32 = $value;)+
        );
    };
    (@meta $m:tt $($i:item)+) => { define!(@expand $($m $i)+); };
    (@expand $({ $(#[$m:meta])* } $i:item)+) => {$($(#[$m])* $i)+};
}

#[cfg(feature = "use-system-headers")]
pub use system_headers::*;

#[cfg(not(feature = "use-system-headers"))]
pub use export::*;

mod export {
    // There are a few truly universal constants.
    // They are defined here directly.
    pub const PAM_SUCCESS: u32 = 0;

    define!(
        /// An item type.
        PAM_SERVICE = 1;
        PAM_USER = 2;
        PAM_TTY = 3;
        PAM_RHOST = 4;
        PAM_CONV = 5;
        PAM_AUTHTOK = 6;
        PAM_OLDAUTHTOK = 7;
        PAM_RUSER = 8;
    );

    define!(
        /// A message style.
        PAM_PROMPT_ECHO_OFF = 1;
        PAM_PROMPT_ECHO_ON = 2;
        PAM_ERROR_MSG = 3;
        PAM_TEXT_INFO = 4;
    );

    define!(
        /// Maximum size of PAM conversation elements (suggested).
        PAM_MAX_NUM_MSG = 32;
        PAM_MAX_MSG_SIZE = 512;
        PAM_MAX_RESP_SIZE = 512;
    );

    #[cfg(pam_impl = "linux-pam")]
    pub use super::linux_pam::*;

    #[cfg(not(pam_impl = "linux-pam"))]
    pub use super::illumos_openpam::*;

    #[cfg(pam_impl = "illumos")]
    pub use super::illumos::*;

    #[cfg(pam_impl = "openpam")]
    pub use super::openpam::*;
}

/// Constants extracted from PAM header files.
mod system_headers {
    // include!(concat!(env!("OUT_DIR"), "/constants.rs"));
}

/// Constants used by Linux-PAM.
mod linux_pam {
    define!(
        /// An error code.
        PAM_OPEN_ERR = 1;
        PAM_SYMBOL_ERR = 2;
        PAM_SERVICE_ERR = 3;
        PAM_SYSTEM_ERR = 4;
        PAM_BUF_ERR = 5;
        PAM_PERM_DENIED = 6;
        PAM_AUTH_ERR = 7;
        PAM_CRED_INSUFFICIENT = 8;
        PAM_AUTHINFO_UNAVAIL = 9;
        PAM_USER_UNKNOWN = 10;
        PAM_MAXTRIES = 11;
        PAM_NEW_AUTHTOK_REQD = 12;
        PAM_ACCT_EXPIRED = 13;
        PAM_SESSION_ERR = 14;
        PAM_CRED_UNAVAIL = 15;
        PAM_CRED_EXPIRED = 16;
        PAM_CRED_ERR = 17;
        PAM_NO_MODULE_DATA = 18;
        PAM_CONV_ERR = 19;
        PAM_AUTHTOK_ERR = 20;
        PAM_AUTHTOK_RECOVERY_ERR = 21;
        PAM_AUTHTOK_LOCK_BUSY = 22;
        PAM_AUTHTOK_DISABLE_AGING = 23;
        PAM_TRY_AGAIN = 24;
        PAM_IGNORE = 25;
        PAM_ABORT = 26;
        PAM_AUTHTOK_EXPIRED = 27;
        PAM_MODULE_UNKNOWN = 28;
        PAM_BAD_ITEM = 29;
        PAM_CONV_AGAIN = 30;
        PAM_INCOMPLETE = 31;
        _PAM_RETURN_VALUES = 32;
    );

    define!(
        /// A flag value.
        PAM_SILENT = 0x8000;
        PAM_DISALLOW_NULL_AUTHTOK = 0x0001;
        PAM_ESTABLISH_CRED = 0x0002;
        PAM_DELETE_CRED = 0x0004;
        PAM_REINITIALIZE_CRED = 0x0008;
        PAM_REFRESH_CRED = 0x0010;

        PAM_CHANGE_EXPIRED_AUTHTOK = 0x0020;

        PAM_PRELIM_CHECK = 0x4000;
        PAM_UPDATE_AUTHTOK = 0x2000;
        PAM_DATA_REPLACE = 0x20000000;
    );

    define!(
        PAM_USER_PROMPT = 9;
        PAM_FAIL_DELAY = 10;
        PAM_XDISPLAY = 11;
        PAM_XAUTHDATA = 12;
        PAM_AUTHTOKTYPE = 13;
    );

    /// To suppress messages in the item cleanup function.
    pub const PAM_DATA_SILENT: u32 = 0x40000000;

    // Message styles
    define!(
        /// A message style.
        PAM_RADIO_TYPE = 5;
        PAM_BINARY_PROMPT = 7;
    );
}

/// Constants shared between Illumos and OpenPAM.
mod illumos_openpam {
    define!(
        /// An error code.
        PAM_OPEN_ERR = 1;
        PAM_SYMBOL_ERR = 2;
        PAM_SERVICE_ERR = 3;
        PAM_SYSTEM_ERR = 4;
        PAM_BUF_ERR = 5;
        PAM_CONV_ERR = 6;
        PAM_PERM_DENIED = 7;
        PAM_MAXTRIES = 8;
        PAM_AUTH_ERR = 9;
        PAM_NEW_AUTHTOK_REQD = 10;
        PAM_CRED_INSUFFICIENT = 11;
        PAM_AUTHINFO_UNAVAIL = 12;
        PAM_USER_UNKNOWN = 13;
        PAM_CRED_UNAVAIL = 14;
        PAM_CRED_EXPIRED = 15;
        PAM_CRED_ERR = 16;
        PAM_ACCT_EXPIRED = 17;
        PAM_AUTHTOK_EXPIRED = 18;
        PAM_SESSION_ERR = 19;
        PAM_AUTHTOK_ERR = 20;
        PAM_AUTHTOK_RECOVERY_ERR = 21;
        PAM_AUTHTOK_LOCK_BUSY = 22;
        PAM_AUTHTOK_DISABLE_AGING = 23;
        PAM_NO_MODULE_DATA = 24;
        PAM_IGNORE = 25;
        PAM_ABORT = 26;
        PAM_TRY_AGAIN = 27;
    );

    define!(
        /// An item type.
        PAM_USER_PROMPT = 9;
        PAM_REPOSITORY = 10;
    );

    /// A general flag for PAM operations.
    pub const PAM_SILENT: u32 = 0x80000000;

    /// The password must be non-null.
    pub const PAM_DISALLOW_NULL_AUTHTOK: u32 = 0b1;

    define!(
        /// A flag for `pam_setcred`.
        PAM_ESTABLISH_CRED = 0b0001;
        PAM_DELETE_CRED = 0b0010;
        PAM_REINITIALIZE_CRED = 0b0100;
        PAM_REFRESH_CRED = 0b1000;
    );

    define!(
        /// A flag for `pam_chauthtok`.
        PAM_PRELIM_CHECK = 0b0001;
        PAM_UPDATE_AUTHTOK = 0b0010;
        PAM_CHANGE_EXPIRED_AUTHTOK = 0b0100;
    );
}

/// Constants exclusive to Illumos.
mod illumos {
    /// The total number of PAM error codes.
    pub const PAM_TOTAL_ERRNUM: u32 = 28;

    define!(
        /// An item type.
        PAM_RESOURCE = 11;
        PAM_AUSER = 12;
    );

    /// A flag for `pam_chauthtok`.
    pub const PAM_NO_AUTHTOK_CHECK: u32 = 0b1000;
}

/// Constants exclusive to OpenPAM.
mod openpam {
    define!(
        /// An error code.
        PAM_MODULE_UNKNOWN = 28;
        PAM_DOMAIN_UNKNOWN = 29;
        PAM_BAD_HANDLE = 30;
        PAM_BAD_ITEM = 31;
        PAM_BAD_FEATURE = 32;
        PAM_BAD_CONSTANT = 33;
    );
    /// The total number of PAM error codes.
    pub const PAM_NUM_ERRORS: i32 = 34;

    define!(
        /// An item type.
        PAM_AUTHTOK_PROMPT = 11;
        PAM_OLDAUTHTOK_PROMPT = 12;
        PAM_HOST = 13;
    );
    /// The total number of PAM items.
    pub const PAM_NUM_ITEMS: u32 = 14;
}

#[cfg(test)]
mod test {}