libpam_sys/
constants.rs

1//! All of `libpam`'s constants.
2//!
3//! These constants are tested on a per-platform basis by `libpam-sys-test`'s
4//! `test_constants.rs`.
5
6#![allow(non_camel_case_types)]
7
8/// Macro to make defining a bunch of constants way easier.
9macro_rules! define {
10    ($(#[$attr:meta])* $($name:ident = $value:expr);+$(;)?) => {
11        define!(
12            @meta { $(#[$attr])* }
13            $(pub const $name: i32 = $value;)+
14        );
15    };
16    (@meta $m:tt $($i:item)+) => { define!(@expand $($m $i)+); };
17    (@expand $({ $(#[$m:meta])* } $i:item)+) => {$($(#[$m])* $i)+};
18}
19
20/// Macro to make defining C-style enums way easier.
21macro_rules! c_enum {
22    ($(#[$attr:meta])* $($name:ident $(= $value:expr)?,)*) => {
23        c_enum!(
24            (0)
25            $(#[$attr])*
26            $($name $(= $value)?,)*
27        );
28    };
29    (($n:expr) $(#[$attr:meta])* $name:ident, $($rest:ident $(= $rv:expr)?,)*) => {
30        $(#[$attr])* pub const $name: i32 = $n;
31        c_enum!(($n + 1) $(#[$attr])* $($rest $(= $rv)?,)*);
32    };
33    (($n:expr) $(#[$attr:meta])* $name:ident = $value:expr, $($rest:ident $(= $rv:expr)?,)*) => {
34        $(#[$attr])* pub const $name: i32 = $value;
35        c_enum!(($value + 1) $(#[$attr])* $($rest $(= $rv)?,)*);
36    };
37    (($n:expr) $(#[$attr:meta])*) => {};
38}
39
40// There are a few truly universal constants.
41// They are defined here directly.
42/// The successful return code.
43pub const PAM_SUCCESS: i32 = 0;
44
45c_enum!(
46    /// An item type.
47    PAM_SERVICE = 1,
48    PAM_USER,
49    PAM_TTY,
50    PAM_RHOST,
51    PAM_CONV,
52    PAM_AUTHTOK,
53    PAM_OLDAUTHTOK,
54    PAM_RUSER,
55    PAM_USER_PROMPT,
56);
57
58c_enum!(
59    /// A message style.
60    PAM_PROMPT_ECHO_OFF = 1,
61    PAM_PROMPT_ECHO_ON,
62    PAM_ERROR_MSG,
63    PAM_TEXT_INFO,
64);
65
66define!(
67    /// Maximum size of PAM conversation elements (suggested).
68    PAM_MAX_NUM_MSG = 32;
69    PAM_MAX_MSG_SIZE = 512;
70    PAM_MAX_RESP_SIZE = 512;
71);
72
73/// A flag for `pam_authenticate`.
74pub const PAM_DISALLOW_NULL_AUTHTOK: i32 = 0x1;
75
76#[cfg(pam_impl = "LinuxPam")]
77pub use linux_pam::*;
78#[cfg(pam_impl = "LinuxPam")]
79mod linux_pam {
80    c_enum!(
81        /// An error return code.
82        PAM_OPEN_ERR = 1,
83        PAM_SYMBOL_ERR,
84        PAM_SERVICE_ERR,
85        PAM_SYSTEM_ERR,
86        PAM_BUF_ERR,
87        PAM_PERM_DENIED,
88        PAM_AUTH_ERR,
89        PAM_CRED_INSUFFICIENT,
90        PAM_AUTHINFO_UNAVAIL,
91        PAM_USER_UNKNOWN,
92        PAM_MAXTRIES,
93        PAM_NEW_AUTHTOK_REQD,
94        PAM_ACCT_EXPIRED,
95        PAM_SESSION_ERR,
96        PAM_CRED_UNAVAIL,
97        PAM_CRED_EXPIRED,
98        PAM_CRED_ERR,
99        PAM_NO_MODULE_DATA,
100        PAM_CONV_ERR,
101        PAM_AUTHTOK_ERR,
102        PAM_AUTHTOK_RECOVERY_ERR,
103        PAM_AUTHTOK_LOCK_BUSY,
104        PAM_AUTHTOK_DISABLE_AGING,
105        PAM_TRY_AGAIN,
106        PAM_IGNORE,
107        PAM_ABORT,
108        PAM_AUTHTOK_EXPIRED,
109        PAM_MODULE_UNKNOWN,
110        PAM_BAD_ITEM,
111        PAM_CONV_AGAIN,
112        PAM_INCOMPLETE,
113        _PAM_RETURN_VALUES,
114    );
115
116    define!(
117        /// A flag value.
118        PAM_SILENT = 0x8000;
119        PAM_ESTABLISH_CRED = 0x0002;
120        PAM_DELETE_CRED = 0x0004;
121        PAM_REINITIALIZE_CRED = 0x0008;
122        PAM_REFRESH_CRED = 0x0010;
123
124        PAM_CHANGE_EXPIRED_AUTHTOK = 0x0020;
125
126        PAM_PRELIM_CHECK = 0x4000;
127        PAM_UPDATE_AUTHTOK = 0x2000;
128        PAM_DATA_REPLACE = 0x20000000;
129    );
130
131    c_enum!(
132        /// An item type (Linux-only).
133        PAM_FAIL_DELAY = 10,
134        PAM_XDISPLAY,
135        PAM_XAUTHDATA,
136        PAM_AUTHTOK_TYPE,
137    );
138
139    /// To suppress messages in the item cleanup function.
140    pub const PAM_DATA_SILENT: i32 = 0x40000000;
141
142    // Message styles
143    define!(
144        /// A message style.
145        PAM_RADIO_TYPE = 5;
146        PAM_BINARY_PROMPT = 7;
147    );
148
149    pub const PAM_MODUTIL_NGROUPS: i32 = 64;
150
151    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
152    #[repr(i32)]
153    pub enum pam_modutil_redirect_fd {
154        PAM_MODUTIL_IGNORE_FD,
155        PAM_MODUTIL_PIPE_FD,
156        PAM_MODUTIL_NULL_FD,
157    }
158
159    impl From<pam_modutil_redirect_fd> for i32 {
160        fn from(value: pam_modutil_redirect_fd) -> Self {
161            value as Self
162        }
163    }
164
165    impl TryFrom<i32> for pam_modutil_redirect_fd {
166        type Error = i32;
167        fn try_from(value: i32) -> Result<Self, Self::Error> {
168            match value {
169                0..=2 => Ok(unsafe { *(&value as *const i32).cast() }),
170                other => Err(other),
171            }
172        }
173    }
174
175    #[doc(inline)]
176    pub use pam_modutil_redirect_fd::*;
177}
178
179#[cfg(any(pam_impl = "OpenPam", pam_impl = "Sun", pam_impl = "XSso"))]
180pub use xsso_shared::*;
181#[cfg(any(pam_impl = "OpenPam", pam_impl = "Sun", pam_impl = "XSso"))]
182mod xsso_shared {
183    c_enum!(
184        /// An error return code.
185        PAM_OPEN_ERR = 1,
186        PAM_SYMBOL_ERR,
187        PAM_SERVICE_ERR,
188        PAM_SYSTEM_ERR,
189        PAM_BUF_ERR,
190        PAM_CONV_ERR,
191        PAM_PERM_DENIED,
192        PAM_MAXTRIES,
193        PAM_AUTH_ERR,
194        PAM_NEW_AUTHTOK_REQD,
195        PAM_CRED_INSUFFICIENT,
196        PAM_AUTHINFO_UNAVAIL,
197        PAM_USER_UNKNOWN,
198        PAM_CRED_UNAVAIL,
199        PAM_CRED_EXPIRED,
200        PAM_CRED_ERR,
201        PAM_ACCT_EXPIRED,
202        PAM_AUTHTOK_EXPIRED,
203        PAM_SESSION_ERR,
204        PAM_AUTHTOK_ERR,
205        PAM_AUTHTOK_RECOVERY_ERR,
206        PAM_AUTHTOK_LOCK_BUSY,
207        PAM_AUTHTOK_DISABLE_AGING,
208        PAM_NO_MODULE_DATA,
209        PAM_IGNORE,
210        PAM_ABORT,
211        PAM_TRY_AGAIN,
212    );
213    // While `PAM_MODULE_UNKNOWN` and `PAM_DOMAIN_UNKNOWN` are in X/SSO,
214    // Sun doesn't use them so we're omitting them here.
215
216    /// A general flag for PAM operations.
217    pub const PAM_SILENT: i32 = 0x80000000u32 as i32;
218
219    define!(
220        /// A flag for `pam_setcred`.
221        PAM_ESTABLISH_CRED = 0b0001;
222        PAM_DELETE_CRED = 0b0010;
223        PAM_REINITIALIZE_CRED = 0b0100;
224        PAM_REFRESH_CRED = 0b1000;
225    );
226
227    define!(
228        /// A flag for `pam_sm_chauthtok`.
229        PAM_PRELIM_CHECK = 0b0001;
230        PAM_UPDATE_AUTHTOK = 0b0010;
231        PAM_CHANGE_EXPIRED_AUTHTOK = 0b0100;
232    );
233}
234
235#[cfg(pam_impl = "OpenPam")]
236pub use openpam::*;
237#[cfg(pam_impl = "OpenPam")]
238mod openpam {
239    c_enum!(
240        /// An error return code.
241        PAM_MODULE_UNKNOWN = 28,
242        PAM_DOMAIN_UNKNOWN,
243        PAM_BAD_HANDLE,
244        PAM_BAD_ITEM,
245        PAM_BAD_FEATURE,
246        PAM_BAD_CONSTANT,
247    );
248    /// The total number of PAM error codes (including success).
249    pub const PAM_NUM_ERRORS: i32 = 34;
250
251    c_enum!(
252        /// An item type.
253        PAM_REPOSITORY = 10,
254        PAM_AUTHTOK_PROMPT,
255        PAM_OLDAUTHTOK_PROMPT,
256        PAM_HOST,
257    );
258    /// The total number of PAM items.
259    pub const PAM_NUM_ITEMS: i32 = 14;
260
261    c_enum!(
262        /// An optional OpenPAM feature.
263        OPENPAM_RESTRICT_SERVICE_NAME,
264        OPENPAM_VERIFY_POLICY_FILE,
265        OPENPAM_RESTRICT_MODULE_NAME,
266        OPENPAM_VERIFY_MODULE_FILE,
267        OPENPAM_FALLBACK_TO_OTHER,
268    );
269    /// The number of optional OpenPAM features.
270    pub const OPENPAM_NUM_FEATURES: i32 = 5;
271
272    c_enum!(
273        /// Log level.
274        PAM_LOG_LIBDEBUG = -1,
275        PAM_LOG_DEBUG,
276        PAM_LOG_VERBOSE,
277        PAM_LOG_NOTICE,
278        PAM_LOG_ERROR,
279    );
280
281    c_enum!(
282        /// PAM primitives.
283        PAM_SM_AUTHENTICATE,
284        PAM_SM_SETCRED,
285        PAM_SM_ACCT_MGMT,
286        PAM_SM_OPEN_SESSION,
287        PAM_SM_CLOSE_SESSION,
288        PAM_SM_CHAUTHTOK,
289    );
290    /// The number of PAM primitives.
291    pub const PAM_NUM_PRIMITIVES: i32 = 6;
292}
293
294/// Constants exclusive to Illumos.
295#[cfg(pam_impl = "Sun")]
296pub use sun::*;
297#[cfg(pam_impl = "Sun")]
298mod sun {
299    /// The total number of PAM error codes.
300    pub const PAM_TOTAL_ERRNUM: i32 = 28;
301
302    c_enum!(
303        /// An item type.
304        PAM_REPOSITORY = 10,
305        PAM_RESOURCE,
306        PAM_AUSER,
307    );
308
309    /// A flag for `pam_chauthtok`.
310    pub const PAM_NO_AUTHTOK_CHECK: i32 = 0b1000;
311
312    define!(
313        /// A flag for `__pam_get_authtok`.
314        PAM_PROMPT = 1;
315        PAM_HANDLE = 2;
316    );
317}