diff --git a/src/pam/error.rs b/src/pam/error.rs index e850cba05..ba468cbe5 100644 --- a/src/pam/error.rs +++ b/src/pam/error.rs @@ -6,23 +6,17 @@ use super::sys::*; pub type PamResult = Result; +// TODO: add missing doc-comments #[derive(PartialEq, Eq, Debug)] pub enum PamErrorType { /// There was no error running the PAM command Success, - /// OpenError, - /// SymbolError, - /// ServiceError, - /// SystemError, - /// BufferError, - /// ConversationError, - /// PermissionDenied, /// The maximum number of authentication attempts was reached and no more /// attempts should be made. @@ -47,27 +41,17 @@ pub enum PamErrorType { CredentialsError, /// The user account is expired and can no longer be used. AccountExpired, - /// AuthTokenExpired, - /// SessionError, - /// AuthTokenError, - /// AuthTokenRecoveryError, - /// AuthTokenLockBusy, - /// AuthTokenDisableAging, - /// NoModuleData, - /// Ignore, /// The application should exit immediately. Abort, - /// TryAgain, - /// ModuleUnknown, /// The application tried to set/delete an undefined or inaccessible item. BadItem, // Extension in OpenPAM and LinuxPAM diff --git a/src/pam/sys.rs b/src/pam/sys.rs index d222b0467..2d8029d02 100644 --- a/src/pam/sys.rs +++ b/src/pam/sys.rs @@ -240,409 +240,6 @@ extern "C" { extern "C" { pub fn pam_chauthtok(pamh: *mut pam_handle_t, flags: libc::c_int) -> libc::c_int; } -pub type __uid_t = libc::c_uint; -pub type __gid_t = libc::c_uint; -pub type gid_t = __gid_t; -pub type uid_t = __uid_t; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct passwd { - pub pw_name: *mut libc::c_char, - pub pw_passwd: *mut libc::c_char, - pub pw_uid: __uid_t, - pub pw_gid: __gid_t, - pub pw_gecos: *mut libc::c_char, - pub pw_dir: *mut libc::c_char, - pub pw_shell: *mut libc::c_char, -} -#[test] -fn bindgen_test_layout_passwd() { - const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::align_of::(), - ::std::mem::align_of::<*mut libc::c_char>(), - concat!("Alignment of ", stringify!(passwd)) - ); - let mut offset: usize = 0; - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pw_name) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(passwd), - "::", - stringify!(pw_name) - ) - ); - offset = - aligned_offset::<*mut libc::c_char>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pw_passwd) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(passwd), - "::", - stringify!(pw_passwd) - ) - ); - offset = aligned_offset::<__uid_t>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pw_uid) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(passwd), - "::", - stringify!(pw_uid) - ) - ); - offset = aligned_offset::<__gid_t>(offset + ::std::mem::size_of::<__uid_t>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pw_gid) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(passwd), - "::", - stringify!(pw_gid) - ) - ); - offset = aligned_offset::<*mut libc::c_char>(offset + ::std::mem::size_of::<__gid_t>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pw_gecos) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(passwd), - "::", - stringify!(pw_gecos) - ) - ); - offset = - aligned_offset::<*mut libc::c_char>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pw_dir) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(passwd), - "::", - stringify!(pw_dir) - ) - ); - offset = - aligned_offset::<*mut libc::c_char>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pw_shell) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(passwd), - "::", - stringify!(pw_shell) - ) - ); - offset = - aligned_offset::<*mut libc::c_void>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - ::std::mem::size_of::(), - offset, - concat!("Size of: ", stringify!(passwd)) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct group { - pub gr_name: *mut libc::c_char, - pub gr_passwd: *mut libc::c_char, - pub gr_gid: __gid_t, - pub gr_mem: *mut *mut libc::c_char, -} -#[test] -fn bindgen_test_layout_group() { - const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::align_of::(), - ::std::mem::align_of::<*mut libc::c_char>(), - concat!("Alignment of ", stringify!(group)) - ); - let mut offset: usize = 0; - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).gr_name) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(group), - "::", - stringify!(gr_name) - ) - ); - offset = - aligned_offset::<*mut libc::c_char>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).gr_passwd) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(group), - "::", - stringify!(gr_passwd) - ) - ); - offset = aligned_offset::<__gid_t>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).gr_gid) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(group), - "::", - stringify!(gr_gid) - ) - ); - offset = aligned_offset::<*mut libc::c_char>(offset + ::std::mem::size_of::<__gid_t>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).gr_mem) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(group), - "::", - stringify!(gr_mem) - ) - ); - offset = - aligned_offset::<*mut libc::c_void>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - ::std::mem::size_of::(), - offset, - concat!("Size of: ", stringify!(group)) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct spwd { - pub sp_namp: *mut libc::c_char, - pub sp_pwdp: *mut libc::c_char, - pub sp_lstchg: libc::c_long, - pub sp_min: libc::c_long, - pub sp_max: libc::c_long, - pub sp_warn: libc::c_long, - pub sp_inact: libc::c_long, - pub sp_expire: libc::c_long, - pub sp_flag: libc::c_ulong, -} -#[test] -fn bindgen_test_layout_spwd() { - const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::align_of::(), - ::std::mem::align_of::<*mut libc::c_char>(), - concat!("Alignment of ", stringify!(spwd)) - ); - let mut offset: usize = 0; - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_namp) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_namp) - ) - ); - offset = - aligned_offset::<*mut libc::c_char>(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_pwdp) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_pwdp) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::<*mut libc::c_char>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_lstchg) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_lstchg) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_min) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_min) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_max) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_max) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_warn) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_warn) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_inact) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_inact) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_expire) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_expire) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).sp_flag) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(spwd), - "::", - stringify!(sp_flag) - ) - ); - offset = aligned_offset::<*mut libc::c_void>(offset + ::std::mem::size_of::()); - assert_eq!( - ::std::mem::size_of::(), - offset, - concat!("Size of: ", stringify!(spwd)) - ); -} -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct pam_modutil_privs { - pub grplist: *mut gid_t, - pub number_of_groups: libc::c_int, - pub allocated: libc::c_int, - pub old_gid: gid_t, - pub old_uid: uid_t, - pub is_dropped: libc::c_int, -} -#[test] -fn bindgen_test_layout_pam_modutil_privs() { - const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::align_of::(), - ::std::mem::align_of::<*mut gid_t>(), - concat!("Alignment of ", stringify!(pam_modutil_privs)) - ); - let mut offset: usize = 0; - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).grplist) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(pam_modutil_privs), - "::", - stringify!(grplist) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::<*mut gid_t>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).number_of_groups) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(pam_modutil_privs), - "::", - stringify!(number_of_groups) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).allocated) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(pam_modutil_privs), - "::", - stringify!(allocated) - ) - ); - offset = aligned_offset::<__gid_t>(offset + ::std::mem::size_of::()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).old_gid) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(pam_modutil_privs), - "::", - stringify!(old_gid) - ) - ); - offset = aligned_offset::<__uid_t>(offset + ::std::mem::size_of::<__gid_t>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).old_uid) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(pam_modutil_privs), - "::", - stringify!(old_uid) - ) - ); - offset = aligned_offset::(offset + ::std::mem::size_of::<__uid_t>()); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).is_dropped) as usize - ptr as usize }, - offset, - concat!( - "Offset of field: ", - stringify!(pam_modutil_privs), - "::", - stringify!(is_dropped) - ) - ); - offset = aligned_offset::<*mut libc::c_void>(offset + ::std::mem::size_of::()); - assert_eq!( - ::std::mem::size_of::(), - offset, - concat!("Size of: ", stringify!(pam_modutil_privs)) - ); -} #[cfg(test)] fn aligned_offset(offset: usize) -> usize { diff --git a/src/sudoers/basic_parser.rs b/src/sudoers/basic_parser.rs index 46899b317..8b4364833 100644 --- a/src/sudoers/basic_parser.rs +++ b/src/sudoers/basic_parser.rs @@ -73,7 +73,6 @@ pub fn maybe(status: Parsed) -> Parsed> { pub use super::char_stream::CharStream; /// All implementations of the Parse trait must satisfy this contract: -/// /// If the `parse` method of this trait returns None, the iterator is not advanced; otherwise it is /// advanced beyond the accepted part of the input. i.e. if some input is consumed the method /// *MUST* be producing a `Some` value. @@ -274,6 +273,7 @@ impl Parse for Option { } /// Parsing method for lists of items separated by a given character; this adheres to the contract of the [Parse] trait. +#[allow(clippy::multiple_bound_locations)] pub(super) fn parse_list( sep_by: char, max: usize, diff --git a/src/system/timestamp.rs b/src/system/timestamp.rs index c5d5542de..603c5c860 100644 --- a/src/system/timestamp.rs +++ b/src/system/timestamp.rs @@ -17,20 +17,6 @@ use super::{ Process, WithProcess, }; -/// Truncates or extends the underlying data -pub trait SetLength { - /// After this is called, the underlying data will either be truncated - /// up to new_len bytes, or it will have been extended by zero bytes up to - /// new_len. - fn set_len(&mut self, new_len: usize) -> io::Result<()>; -} - -impl SetLength for File { - fn set_len(&mut self, new_len: usize) -> io::Result<()> { - File::set_len(self, new_len as u64) - } -} - type BoolStorage = u8; const SIZE_OF_TS: i64 = std::mem::size_of::() as i64; @@ -584,26 +570,6 @@ mod tests { const TEST_USER_ID: UserId = 1000; - impl SetLength for Cursor> { - fn set_len(&mut self, new_len: usize) -> io::Result<()> { - self.get_mut().truncate(new_len); - while self.get_mut().len() < new_len { - self.get_mut().push(0); - } - Ok(()) - } - } - - impl SetLength for Cursor<&mut Vec> { - fn set_len(&mut self, new_len: usize) -> io::Result<()> { - self.get_mut().truncate(new_len); - while self.get_mut().len() < new_len { - self.get_mut().push(0); - } - Ok(()) - } - } - #[test] fn can_encode_and_decode() { let tty_sample = SessionRecord::new( diff --git a/test-framework/sudo-compliance-tests/src/sudo/sudo_ps1.rs b/test-framework/sudo-compliance-tests/src/sudo/sudo_ps1.rs index a6a3b2c09..b502c32f3 100644 --- a/test-framework/sudo-compliance-tests/src/sudo/sudo_ps1.rs +++ b/test-framework/sudo-compliance-tests/src/sudo/sudo_ps1.rs @@ -22,7 +22,7 @@ fn ps1_env_var_is_set_when_sudo_ps1_is_set() -> Result<()> { let sudo_env = helpers::parse_env_output(&stdout)?; assert_eq!(Some(ps1), sudo_env.get("PS1").copied()); - assert!(sudo_env.get("SUDO_PS1").is_none()); + assert!(!sudo_env.contains_key("SUDO_PS1")); Ok(()) } @@ -43,8 +43,8 @@ fn ps1_env_var_is_not_set_when_sudo_ps1_is_set_and_flag_login_is_used() -> Resul .stdout()?; let sudo_env = helpers::parse_env_output(&stdout)?; - assert!(sudo_env.get("PS1").is_none()); - assert!(sudo_env.get("SUDO_PS1").is_none()); + assert!(!sudo_env.contains_key("PS1")); + assert!(!sudo_env.contains_key("SUDO_PS1")); Ok(()) } @@ -68,7 +68,7 @@ fn can_start_with_parentheses() -> Result<()> { let sudo_env = helpers::parse_env_output(&stdout)?; assert_eq!(Some(ps1), sudo_env.get("PS1").copied()); - assert!(sudo_env.get("SUDO_PS1").is_none()); + assert!(!sudo_env.contains_key("SUDO_PS1")); Ok(()) } diff --git a/test-framework/sudo-compliance-tests/src/sudo/sudoers/env.rs b/test-framework/sudo-compliance-tests/src/sudo/sudoers/env.rs index 1a83be706..847945f08 100644 --- a/test-framework/sudo-compliance-tests/src/sudo/sudoers/env.rs +++ b/test-framework/sudo-compliance-tests/src/sudo/sudoers/env.rs @@ -220,7 +220,7 @@ fn equal_overrides(env_list: EnvList) -> Result<()> { .stdout()?; let sudo_env = helpers::parse_env_output(&stdout)?; - assert!(sudo_env.get(env_name2).is_none()); + assert!(!sudo_env.contains_key(env_name2)); assert_eq!(Some(env_val1), sudo_env.get(env_name1).copied()); Ok(()) @@ -412,7 +412,7 @@ fn if_value_starts_with_parentheses_variable_is_removed(env_list: EnvList) -> Re .stdout()?; let sudo_env = helpers::parse_env_output(&stdout)?; - assert!(sudo_env.get(env_name).is_none()); + assert!(!sudo_env.contains_key(env_name)); Ok(()) } @@ -525,7 +525,7 @@ fn minus_equal_removes(env_list: EnvList) -> Result<()> { let sudo_env = helpers::parse_env_output(&stdout)?; assert_eq!(Some(env_val1), sudo_env.get(env_name1).copied()); - assert!(sudo_env.get(env_name2).is_none()); + assert!(!sudo_env.contains_key(env_name2)); Ok(()) } @@ -550,7 +550,7 @@ fn minus_equal_an_element_not_in_the_list_is_not_an_error(env_list: EnvList) -> let stdout = output.stdout()?; let sudo_env = helpers::parse_env_output(&stdout)?; - assert!(sudo_env.get(env_name).is_none()); + assert!(!sudo_env.contains_key(env_name)); Ok(()) } @@ -576,8 +576,8 @@ fn bang_clears_the_whole_list(env_list: EnvList) -> Result<()> { let sudo_env = helpers::parse_env_output(&stdout)?; - assert!(sudo_env.get(env_name1).is_none()); - assert!(sudo_env.get(env_name1).is_none()); + assert!(!sudo_env.contains_key(env_name1)); + assert!(!sudo_env.contains_key(env_name1)); Ok(()) } @@ -604,7 +604,7 @@ fn can_append_after_bang(env_list: EnvList) -> Result<()> { let sudo_env = helpers::parse_env_output(&stdout)?; - assert!(sudo_env.get(env_name1).is_none()); + assert!(!sudo_env.contains_key(env_name1)); assert_eq!(Some(env_val2), sudo_env.get(env_name2).copied()); Ok(()) @@ -632,7 +632,7 @@ fn can_override_after_bang(env_list: EnvList) -> Result<()> { let sudo_env = helpers::parse_env_output(&stdout)?; - assert!(sudo_env.get(env_name1).is_none()); + assert!(!sudo_env.contains_key(env_name1)); assert_eq!(Some(env_val2), sudo_env.get(env_name2).copied()); Ok(()) diff --git a/test-framework/sudo-compliance-tests/src/sudo/sudoers/env/keep.rs b/test-framework/sudo-compliance-tests/src/sudo/sudoers/env/keep.rs index d494efc78..dcfa89372 100644 --- a/test-framework/sudo-compliance-tests/src/sudo/sudoers/env/keep.rs +++ b/test-framework/sudo-compliance-tests/src/sudo/sudoers/env/keep.rs @@ -158,7 +158,7 @@ fn equal_can_disable_preservation_of_vars_display_path_but_not_term() -> Result< let sudo_env = helpers::parse_env_output(&stdout)?; // can be disabled - assert!(sudo_env.get("DISPLAY").is_none()); + assert!(!sudo_env.contains_key("DISPLAY")); assert_eq!(Some(SUDO_ENV_DEFAULT_PATH), sudo_env.get("PATH").copied()); // cannot be disabled @@ -190,7 +190,7 @@ fn minus_equal_can_disable_preservation_of_vars_display_path_but_not_term() -> R let sudo_env = helpers::parse_env_output(&stdout)?; // can be disabled - assert!(sudo_env.get("DISPLAY").is_none()); + assert!(!sudo_env.contains_key("DISPLAY")); assert_eq!(Some(SUDO_ENV_DEFAULT_PATH), sudo_env.get("PATH").copied()); // cannot be disabled @@ -218,7 +218,7 @@ fn bang_can_disable_preservation_of_vars_display_path_but_not_term() -> Result<( let sudo_env = helpers::parse_env_output(&stdout)?; // can be disabled - assert!(sudo_env.get("DISPLAY").is_none()); + assert!(!sudo_env.contains_key("DISPLAY")); assert_eq!(Some(SUDO_ENV_DEFAULT_PATH), sudo_env.get("PATH").copied()); // cannot be disabled