Skip to main content

pam/
constants.rs

1use libc::{c_int, c_uint};
2
3// TODO: Import constants from C header file at compile time.
4
5pub type PamFlag = c_uint;
6pub type PamItemType = c_int;
7pub type PamMessageStyle = c_int;
8
9// The Linux-PAM flags
10// see /usr/include/security/_pam_types.h
11pub const PAM_SILENT: PamFlag = 0x8000;
12pub const PAM_DISALLOW_NULL_AUTHTOK: PamFlag = 0x0001;
13pub const PAM_ESTABLISH_CRED: PamFlag = 0x0002;
14pub const PAM_DELETE_CRED: PamFlag = 0x0004;
15pub const PAM_REINITIALIZE_CRED: PamFlag = 0x0008;
16pub const PAM_REFRESH_CRED: PamFlag = 0x0010;
17pub const PAM_CHANGE_EXPIRED_AUTHTOK: PamFlag = 0x0020;
18
19// Message styles
20pub const PAM_PROMPT_ECHO_OFF: PamMessageStyle = 1;
21pub const PAM_PROMPT_ECHO_ON: PamMessageStyle = 2;
22pub const PAM_ERROR_MSG: PamMessageStyle = 3;
23pub const PAM_TEXT_INFO: PamMessageStyle = 4;
24pub const PAM_RADIO_TYPE: PamMessageStyle = 5;
25// Intentionally not public: we don't yet have a send API for this style.
26#[allow(dead_code)]
27pub(crate) const PAM_BINARY_PROMPT: PamMessageStyle = 7;
28
29// The Linux-PAM return values
30// see /usr/include/security/_pam_types.h
31#[allow(non_camel_case_types, dead_code)]
32#[derive(Debug, PartialEq, Eq)]
33#[repr(C)]
34#[non_exhaustive]
35pub enum PamResultCode {
36    PAM_SUCCESS = 0,
37    PAM_OPEN_ERR = 1,
38    PAM_SYMBOL_ERR = 2,
39    PAM_SERVICE_ERR = 3,
40    PAM_SYSTEM_ERR = 4,
41    PAM_BUF_ERR = 5,
42    PAM_PERM_DENIED = 6,
43    PAM_AUTH_ERR = 7,
44    PAM_CRED_INSUFFICIENT = 8,
45    PAM_AUTHINFO_UNAVAIL = 9,
46    PAM_USER_UNKNOWN = 10,
47    PAM_MAXTRIES = 11,
48    PAM_NEW_AUTHTOK_REQD = 12,
49    PAM_ACCT_EXPIRED = 13,
50    PAM_SESSION_ERR = 14,
51    PAM_CRED_UNAVAIL = 15,
52    PAM_CRED_EXPIRED = 16,
53    PAM_CRED_ERR = 17,
54    PAM_NO_MODULE_DATA = 18,
55    PAM_CONV_ERR = 19,
56    PAM_AUTHTOK_ERR = 20,
57    PAM_AUTHTOK_RECOVERY_ERR = 21,
58    PAM_AUTHTOK_LOCK_BUSY = 22,
59    PAM_AUTHTOK_DISABLE_AGING = 23,
60    PAM_TRY_AGAIN = 24,
61    PAM_IGNORE = 25,
62    PAM_ABORT = 26,
63    PAM_AUTHTOK_EXPIRED = 27,
64    PAM_MODULE_UNKNOWN = 28,
65    PAM_BAD_ITEM = 29,
66    PAM_CONV_AGAIN = 30,
67    PAM_INCOMPLETE = 31,
68}
69
70impl TryFrom<c_int> for PamResultCode {
71    /// The original value is returned when it does not name a known result code.
72    type Error = c_int;
73
74    /// Map a raw [`c_int`] to a [`PamResultCode`].
75    fn try_from(value: c_int) -> Result<Self, Self::Error> {
76        Ok(match value {
77            0 => Self::PAM_SUCCESS,
78            1 => Self::PAM_OPEN_ERR,
79            2 => Self::PAM_SYMBOL_ERR,
80            3 => Self::PAM_SERVICE_ERR,
81            4 => Self::PAM_SYSTEM_ERR,
82            5 => Self::PAM_BUF_ERR,
83            6 => Self::PAM_PERM_DENIED,
84            7 => Self::PAM_AUTH_ERR,
85            8 => Self::PAM_CRED_INSUFFICIENT,
86            9 => Self::PAM_AUTHINFO_UNAVAIL,
87            10 => Self::PAM_USER_UNKNOWN,
88            11 => Self::PAM_MAXTRIES,
89            12 => Self::PAM_NEW_AUTHTOK_REQD,
90            13 => Self::PAM_ACCT_EXPIRED,
91            14 => Self::PAM_SESSION_ERR,
92            15 => Self::PAM_CRED_UNAVAIL,
93            16 => Self::PAM_CRED_EXPIRED,
94            17 => Self::PAM_CRED_ERR,
95            18 => Self::PAM_NO_MODULE_DATA,
96            19 => Self::PAM_CONV_ERR,
97            20 => Self::PAM_AUTHTOK_ERR,
98            21 => Self::PAM_AUTHTOK_RECOVERY_ERR,
99            22 => Self::PAM_AUTHTOK_LOCK_BUSY,
100            23 => Self::PAM_AUTHTOK_DISABLE_AGING,
101            24 => Self::PAM_TRY_AGAIN,
102            25 => Self::PAM_IGNORE,
103            26 => Self::PAM_ABORT,
104            27 => Self::PAM_AUTHTOK_EXPIRED,
105            28 => Self::PAM_MODULE_UNKNOWN,
106            29 => Self::PAM_BAD_ITEM,
107            30 => Self::PAM_CONV_AGAIN,
108            31 => Self::PAM_INCOMPLETE,
109            other => return Err(other),
110        })
111    }
112}
113
114impl PamResultCode {
115    /// Map a [`c_int`] to a [`PamResultCode`].
116    /// Unknown values are mapped to [`PamResultCode::PAM_SYSTEM_ERR`].
117    pub(crate) fn from_raw(value: c_int) -> Self {
118        Self::try_from(value).unwrap_or(Self::PAM_SYSTEM_ERR)
119    }
120}