comparison src/constants.rs @ 113:178310336596

Fix up more constants, make things i32 rather than u32.
author Paul Fisher <paul@pfish.zone>
date Sun, 29 Jun 2025 03:11:33 -0400
parents e97534be35e3
children
comparison
equal deleted inserted replaced
112:82995b4dccee 113:178310336596
27 27
28 macro_rules! define { 28 macro_rules! define {
29 ($(#[$attr:meta])* $($name:ident = $value:expr),+) => { 29 ($(#[$attr:meta])* $($name:ident = $value:expr),+) => {
30 define!( 30 define!(
31 @meta { $(#[$attr])* } 31 @meta { $(#[$attr])* }
32 $(pub const $name: u32 = $value;)+ 32 $(pub const $name: i32 = $value;)+
33 ); 33 );
34 }; 34 };
35 (@meta $m:tt $($i:item)+) => { define!(@expand $($m $i)+); }; 35 (@meta $m:tt $($i:item)+) => { define!(@expand $($m $i)+); };
36 (@expand $({ $(#[$m:meta])* } $i:item)+) => {$($(#[$m])* $i)+}; 36 (@expand $({ $(#[$m:meta])* } $i:item)+) => {$($(#[$m])* $i)+};
37 } 37 }
38 const fn bit(n: u8) -> u32 { 38 const fn bit(n: u8) -> i32 {
39 1 << n 39 1 << n
40 } 40 }
41 define!( 41 define!(
42 PAM_SILENT = bit(13), 42 PAM_SILENT = bit(13),
43 PAM_DISALLOW_NULL_AUTHTOK = bit(14), 43 PAM_DISALLOW_NULL_AUTHTOK = bit(14),
96 /// 96 ///
97 /// See `/usr/include/security/_pam_types.h` and 97 /// See `/usr/include/security/_pam_types.h` and
98 /// See `/usr/include/security/pam_modules.h` for more details. 98 /// See `/usr/include/security/pam_modules.h` for more details.
99 #[derive(Debug, Default, PartialEq)] 99 #[derive(Debug, Default, PartialEq)]
100 #[repr(transparent)] 100 #[repr(transparent)]
101 pub struct Flags: c_uint { 101 pub struct Flags: c_int {
102 /// The module should not generate any messages. 102 /// The module should not generate any messages.
103 const SILENT = pam_ffi::PAM_SILENT as u32; 103 const SILENT = pam_ffi::PAM_SILENT;
104 104
105 /// The module should return [ErrorCode::AuthError] 105 /// The module should return [ErrorCode::AuthError]
106 /// if the user has an empty authentication token 106 /// if the user has an empty authentication token
107 /// rather than immediately accepting them. 107 /// rather than immediately accepting them.
108 const DISALLOW_NULL_AUTHTOK = pam_ffi::PAM_DISALLOW_NULL_AUTHTOK as u32; 108 const DISALLOW_NULL_AUTHTOK = pam_ffi::PAM_DISALLOW_NULL_AUTHTOK;
109 109
110 // Flag used for `set_credentials`. 110 // Flag used for `set_credentials`.
111 111
112 /// Set user credentials for an authentication service. 112 /// Set user credentials for an authentication service.
113 const ESTABLISH_CREDENTIALS = pam_ffi::PAM_ESTABLISH_CRED as u32; 113 const ESTABLISH_CREDENTIALS = pam_ffi::PAM_ESTABLISH_CRED;
114 /// Delete user credentials associated with 114 /// Delete user credentials associated with
115 /// an authentication service. 115 /// an authentication service.
116 const DELETE_CREDENTIALS = pam_ffi::PAM_DELETE_CRED as u32; 116 const DELETE_CREDENTIALS = pam_ffi::PAM_DELETE_CRED;
117 /// Reinitialize user credentials. 117 /// Reinitialize user credentials.
118 const REINITIALIZE_CREDENTIALS = pam_ffi::PAM_REINITIALIZE_CRED as u32; 118 const REINITIALIZE_CREDENTIALS = pam_ffi::PAM_REINITIALIZE_CRED;
119 /// Extend the lifetime of user credentials. 119 /// Extend the lifetime of user credentials.
120 const REFRESH_CREDENTIALS = pam_ffi::PAM_REFRESH_CRED as u32; 120 const REFRESH_CREDENTIALS = pam_ffi::PAM_REFRESH_CRED;
121 121
122 // Flags used for password changing. 122 // Flags used for password changing.
123 123
124 /// The password service should only update those passwords 124 /// The password service should only update those passwords
125 /// that have aged. If this flag is _not_ passed, 125 /// that have aged. If this flag is _not_ passed,
126 /// the password service should update all passwords. 126 /// the password service should update all passwords.
127 /// 127 ///
128 /// This flag is only used by `change_authtok`. 128 /// This flag is only used by `change_authtok`.
129 const CHANGE_EXPIRED_AUTHTOK = pam_ffi::PAM_CHANGE_EXPIRED_AUTHTOK as u32; 129 const CHANGE_EXPIRED_AUTHTOK = pam_ffi::PAM_CHANGE_EXPIRED_AUTHTOK;
130 /// This is a preliminary check for password changing. 130 /// This is a preliminary check for password changing.
131 /// The password should not be changed. 131 /// The password should not be changed.
132 /// 132 ///
133 /// This is only used between PAM and a module. 133 /// This is only used between PAM and a module.
134 /// Applications may not use this flag. 134 /// Applications may not use this flag.
135 /// 135 ///
136 /// This flag is only used by `change_authtok`. 136 /// This flag is only used by `change_authtok`.
137 const PRELIMINARY_CHECK = pam_ffi::PAM_PRELIM_CHECK as u32; 137 const PRELIMINARY_CHECK = pam_ffi::PAM_PRELIM_CHECK;
138 /// The password should actuallyPR be updated. 138 /// The password should actuallyPR be updated.
139 /// This and [Self::PRELIMINARY_CHECK] are mutually exclusive. 139 /// This and [Self::PRELIMINARY_CHECK] are mutually exclusive.
140 /// 140 ///
141 /// This is only used between PAM and a module. 141 /// This is only used between PAM and a module.
142 /// Applications may not use this flag. 142 /// Applications may not use this flag.
143 /// 143 ///
144 /// This flag is only used by `change_authtok`. 144 /// This flag is only used by `change_authtok`.
145 const UPDATE_AUTHTOK = pam_ffi::PAM_UPDATE_AUTHTOK as u32; 145 const UPDATE_AUTHTOK = pam_ffi::PAM_UPDATE_AUTHTOK;
146 } 146 }
147 } 147 }
148 148
149 /// The PAM error return codes. 149 /// The PAM error return codes.
150 /// 150 ///
161 #[doc = _manbsd!(3 pam "RETURN%20VALUES")] 161 #[doc = _manbsd!(3 pam "RETURN%20VALUES")]
162 #[doc = _xsso!("chap5.htm#tagcjh_06_02")] 162 #[doc = _xsso!("chap5.htm#tagcjh_06_02")]
163 #[allow(non_camel_case_types, dead_code)] 163 #[allow(non_camel_case_types, dead_code)]
164 #[derive(Copy, Clone, Debug, PartialEq, TryFromPrimitive, IntoPrimitive)] 164 #[derive(Copy, Clone, Debug, PartialEq, TryFromPrimitive, IntoPrimitive)]
165 #[non_exhaustive] // C might give us anything! 165 #[non_exhaustive] // C might give us anything!
166 #[repr(u32)] 166 #[repr(i32)]
167 pub enum ErrorCode { 167 pub enum ErrorCode {
168 OpenError = pam_ffi::PAM_OPEN_ERR, 168 OpenError = pam_ffi::PAM_OPEN_ERR,
169 SymbolError = pam_ffi::PAM_SYMBOL_ERR, 169 SymbolError = pam_ffi::PAM_SYMBOL_ERR,
170 ServiceError = pam_ffi::PAM_SERVICE_ERR, 170 ServiceError = pam_ffi::PAM_SERVICE_ERR,
171 SystemError = pam_ffi::PAM_SYSTEM_ERR, 171 SystemError = pam_ffi::PAM_SYSTEM_ERR,
219 impl ErrorCode { 219 impl ErrorCode {
220 /// Converts this [Result] into a C-compatible result code. 220 /// Converts this [Result] into a C-compatible result code.
221 pub fn result_to_c<T>(value: Result<T>) -> c_int { 221 pub fn result_to_c<T>(value: Result<T>) -> c_int {
222 match value { 222 match value {
223 Ok(_) => 0, // PAM_SUCCESS 223 Ok(_) => 0, // PAM_SUCCESS
224 Err(otherwise) => u32::from(otherwise) as i32, 224 Err(otherwise) => otherwise.into(),
225 } 225 }
226 } 226 }
227 227
228 /// Converts a C result code into a [Result], with success as Ok. 228 /// Converts a C result code into a [Result], with success as Ok.
229 /// Invalid values are returned as a [Self::SystemError]. 229 /// Invalid values are returned as a [Self::SystemError].
230 pub fn result_from(value: c_int) -> Result<()> { 230 pub fn result_from(value: c_int) -> Result<()> {
231 match value { 231 match value {
232 0 => Ok(()), 232 0 => Ok(()),
233 value => Err((value as u32).try_into().unwrap_or(Self::SystemError)), 233 value => Err(value.try_into().unwrap_or(Self::SystemError)),
234 } 234 }
235 } 235 }
236 236
237 /// A basic Display implementation for if we don't link against PAM. 237 /// A basic Display implementation for if we don't link against PAM.
238 fn fmt_internal(self, f: &mut Formatter<'_>) -> fmt::Result { 238 fn fmt_internal(self, f: &mut Formatter<'_>) -> fmt::Result {