| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 2 | ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#, OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#, c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#}, |
| 3 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, |
| 4 | osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::rawrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/::c_charrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/c_char#, |
| 5 | osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::preluderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/prelude/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#, |
| 6 | ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#, |
| 7 | timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, |
| 8 | }; |
| 9 | |
| 10 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::signalrust-analyzer cargo sudo-rs 0.2.14 system/signal/::{selfrust-analyzer cargo sudo-rs 0.2.14 system/signal/, SignalSetrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/SignalSet#}; |
| 11 | |
| 12 | use converserust-analyzer cargo sudo-rs 0.2.14 pam/converse/::ConverserDatarust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#; |
| 13 | use errorrust-analyzer cargo sudo-rs 0.2.14 pam/error/::pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().; |
| 14 | pub use errorrust-analyzer cargo sudo-rs 0.2.14 pam/error/::{PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#, PamErrorTyperust-analyzer cargo sudo-rs 0.2.14 pam/error/PamErrorType#, PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#}; |
| 15 | use sysrust-analyzer cargo sudo-rs 0.2.14 pam/sys/::*; |
| 16 | |
| 17 | mod askpassrust-analyzer cargo sudo-rs 0.2.14 pam/askpass/; |
| 18 | mod converserust-analyzer cargo sudo-rs 0.2.14 pam/converse/; |
| 19 | mod errorrust-analyzer cargo sudo-rs 0.2.14 pam/error/; |
| 20 | mod rpasswordrust-analyzer cargo sudo-rs 0.2.14 pam/rpassword/; |
| 21 | mod securememrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/; |
| 22 | |
| 23 | #[cfg_attr(target_os = "linux", path = "sys_linuxpam.rs")] |
| 24 | #[cfg_attr(target_os = "freebsd", path = "sys_openpam.rs")] |
| 25 | #[allow(nonstandard_style)] |
| 26 | pub mod sysrust-analyzer cargo sudo-rs 0.2.14 pam/sys/; |
| 27 | |
| 28 | #[link(name = "pam")] |
| 29 | unsafe extern "C" {} |
| 30 | |
| 31 | #[cfg(target_os = "freebsd")] |
| 32 | const PAM_DATA_SILENT: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int# = 0; |
| 33 | |
| 34 | pub use converserust-analyzer cargo sudo-rs 0.2.14 pam/converse/::CLIConverserrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#; |
| 35 | |
| 36 | pub struct PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext# { |
| 37 | data_ptrrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#data_ptr.: *mut ConverserDatarust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#<CLIConverserrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#>, |
| 38 | pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh.: *mut pam_handle_trust-analyzer cargo sudo-rs 0.2.14 pam/sys/pam_handle_t#, |
| 39 | silentrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#silent.: bool, |
| 40 | allow_null_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#allow_null_auth_token.: bool, |
| 41 | last_pam_statusrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#last_pam_status.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#>, |
| 42 | session_startedrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#session_started.: bool, |
| 43 | } |
| 44 | |
| 45 | impl PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext# { |
| 46 | /// Build the PamContext with the CLI conversation function. |
| 47 | /// |
| 48 | /// The target user is optional and may also be set after the context was |
| 49 | /// constructed or not set at all in which case PAM will ask for a |
| 50 | /// username. |
| 51 | /// |
| 52 | /// This function will error when initialization of the PAM session somehow failed. |
| 53 | #[allow(clippy::too_many_arguments)] |
| 54 | pub fn new_clirust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]new_cli().( |
| 55 | converser_name: &str, |
| 56 | service_name: &str, |
| 57 | use_askpass: bool, |
| 58 | use_stdin: bool, |
| 59 | bell: bool, |
| 60 | no_interact: bool, |
| 61 | password_feedback: bool, |
| 62 | password_timeout: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#>, |
| 63 | target_user: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, |
| 64 | ) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#> { |
| 65 | let converser = CLIConverserrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser# { |
| 66 | bellrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#bell.: bell.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 67 | namerust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#name.: converser_name.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 68 | use_askpassrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#use_askpass., |
| 69 | use_stdinrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#use_stdin., |
| 70 | password_feedbackrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#password_feedback., |
| 71 | password_timeoutrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#password_timeout., |
| 72 | }; |
| 73 | |
| 74 | let c_service_name = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(service_name)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 75 | let c_user = target_user.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().).transposerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<Result<T, E>>`]transpose().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 76 | let c_user_ptr = match c_user { |
| 77 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref c) => c.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), |
| 78 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null().(), |
| 79 | }; |
| 80 | |
| 81 | // this will be de-allocated explicitly in this type's drop method |
| 82 | let data_ptr = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::into_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]into_raw().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(ConverserDatarust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData# { |
| 83 | converserrust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#converser., |
| 84 | converser_namerust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#converser_name.: converser_name.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 85 | no_interactrust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#no_interact., |
| 86 | auth_promptrust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#auth_prompt.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(xlatrust-analyzer cargo sudo-rs 0.2.14 gettext/xlat!!("authenticate").to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()), |
| 87 | errorrust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#error.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 88 | panickedrust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#panicked.: false, |
| 89 | })); |
| 90 | |
| 91 | let mut pamh = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().(); |
| 92 | // SAFETY: we are passing the required fields to `pam_start`; in particular, the value |
| 93 | // of `pamh` set above is not used, but will be overwritten by `pam_start`. |
| 94 | let res = unsafe { |
| 95 | pam_startrust-analyzer cargo sudo-rs 0.2.14 pam_start().( |
| 96 | c_service_name.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().(), |
| 97 | c_user_ptr, |
| 98 | &pam_convrust-analyzer cargo sudo-rs 0.2.14 pam/sys/pam_conv# { |
| 99 | convrust-analyzer cargo sudo-rs 0.2.14 pam/sys/pam_conv#conv.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(converserust-analyzer cargo sudo-rs 0.2.14 pam/converse/::converserust-analyzer cargo sudo-rs 0.2.14 pam/converse/converse().::<CLIConverserrust-analyzer cargo sudo-rs 0.2.14 pam/converse/CLIConverser#>), |
| 100 | appdata_ptrrust-analyzer cargo sudo-rs 0.2.14 pam/sys/pam_conv#appdata_ptr.: data_ptr as *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#, |
| 101 | }, |
| 102 | &mut pamh, |
| 103 | ) |
| 104 | }; |
| 105 | |
| 106 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(res)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 107 | |
| 108 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pamh.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().()); |
| 109 | |
| 110 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext# { |
| 111 | data_ptrrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#data_ptr., |
| 112 | pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., |
| 113 | silentrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#silent.: false, |
| 114 | allow_null_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#allow_null_auth_token.: true, |
| 115 | last_pam_statusrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#last_pam_status.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 116 | session_startedrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#session_started.: false, |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | pub fn set_auth_promptrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]set_auth_prompt().(&mut self, prompt: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) { |
| 121 | // SAFETY: self.data_ptr was created by Box::into_raw |
| 122 | unsafe { |
| 123 | (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.data_ptrrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#data_ptr.).auth_promptrust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#auth_prompt. = prompt; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// Set whether output of pam calls should be silent or not, by default |
| 128 | /// PAM calls are not silent. |
| 129 | pub fn mark_silentrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]mark_silent().(&mut self, silent: bool) { |
| 130 | self.silentrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#silent. = silent; |
| 131 | } |
| 132 | |
| 133 | /// Set whether or not to allow empty authentication tokens, by default such |
| 134 | /// tokens are allowed. |
| 135 | pub fn mark_allow_null_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]mark_allow_null_auth_token().(&mut self, allow: bool) { |
| 136 | self.allow_null_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#allow_null_auth_token. = allow; |
| 137 | } |
| 138 | |
| 139 | /// Get the PAM flag value for the silent flag |
| 140 | fn silent_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]silent_flag().(&self) -> i32 { |
| 141 | if self.silentrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#silent. { PAM_SILENTrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_SILENT. as _ } else { 0 } |
| 142 | } |
| 143 | |
| 144 | /// Get the PAM flag value for the disallow_null_authtok flag |
| 145 | fn disallow_null_auth_token_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]disallow_null_auth_token_flag().(&self) -> i32 { |
| 146 | if self.allow_null_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#allow_null_auth_token. { |
| 147 | 0 |
| 148 | } else { |
| 149 | PAM_DISALLOW_NULL_AUTHTOKrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_DISALLOW_NULL_AUTHTOK. as _ |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /// Run authentication for the account |
| 154 | pub fn authenticaterust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]authenticate().(&mut self, for_user: &str) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 155 | let mut flags = 0; |
| 156 | flags rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().self.silent_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]silent_flag().(); |
| 157 | flags rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().self.disallow_null_auth_token_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]disallow_null_auth_token_flag().(); |
| 158 | |
| 159 | // Temporarily mask SIGINT and SIGQUIT. |
| 160 | let cur_signals = SignalSetrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/SignalSet#::emptyrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/impl#[SignalSet]empty().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|mut set| { |
| 161 | set.addrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/impl#[SignalSet]add().(signalrust-analyzer cargo sudo-rs 0.2.14 system/signal/::constsrust-analyzer cargo sudo-rs 0.2.14 system/signal/consts/::SIGINTrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGINT.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 162 | set.addrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/impl#[SignalSet]add().(signalrust-analyzer cargo sudo-rs 0.2.14 system/signal/::constsrust-analyzer cargo sudo-rs 0.2.14 system/signal/consts/::SIGQUITrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGQUIT.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 163 | set.blockrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/impl#[SignalSet]block().() |
| 164 | }); |
| 165 | |
| 166 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`) |
| 167 | let auth_res = pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_authenticaterust-analyzer cargo sudo-rs 0.2.14 pam_authenticate().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., flags) }); |
| 168 | |
| 169 | // Restore signals |
| 170 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(set) = cur_signals { |
| 171 | set.set_maskrust-analyzer cargo sudo-rs 0.2.14 system/signal/set/impl#[SignalSet]set_mask().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#::IoErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#IoError#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 172 | } |
| 173 | |
| 174 | if self.has_panickedrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]has_panicked().() { |
| 175 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Panic during pam authentication"); |
| 176 | } |
| 177 | |
| 178 | // SAFETY: self.data_ptr was created by Box::into_raw |
| 179 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(error) = unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.data_ptrrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#data_ptr.).errorrust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#error..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().() } { |
| 180 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error); |
| 181 | } |
| 182 | |
| 183 | #[allow(clippy::question_mark)] |
| 184 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = auth_res { |
| 185 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err); |
| 186 | } |
| 187 | |
| 188 | // Check that no PAM module changed the user. |
| 189 | match self.get_userrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]get_user().() { |
| 190 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pam_user) => { |
| 191 | if pam_user rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]ne().!=rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]ne(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]ne().for_user { |
| 192 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#::InvalidUserrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#InvalidUser#(pam_user, for_user.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())); |
| 193 | } |
| 194 | } |
| 195 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => { |
| 196 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 201 | } |
| 202 | |
| 203 | /// Check that the account is valid |
| 204 | pub fn validate_accountrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]validate_account().(&mut self) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 205 | let mut flags = 0; |
| 206 | flags rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().self.silent_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]silent_flag().(); |
| 207 | flags rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().self.disallow_null_auth_token_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]disallow_null_auth_token_flag().(); |
| 208 | |
| 209 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`) |
| 210 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_acct_mgmtrust-analyzer cargo sudo-rs 0.2.14 pam_acct_mgmt().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., flags) }) |
| 211 | } |
| 212 | |
| 213 | /// Attempt to validate the account, if that fails because the authentication |
| 214 | /// token is outdated, then an update of the authentication token is requested. |
| 215 | pub fn validate_account_or_change_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]validate_account_or_change_auth_token().(&mut self) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 216 | let check_val = self.validate_accountrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]validate_account().(); |
| 217 | match check_val { |
| 218 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 219 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#::Pamrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#Pam#(PamErrorTyperust-analyzer cargo sudo-rs 0.2.14 pam/error/PamErrorType#::NewAuthTokenRequiredrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamErrorType#NewAuthTokenRequired#)) => { |
| 220 | self.change_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]change_auth_token().(true)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 221 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 222 | } |
| 223 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /// Set the user that will be authenticated. |
| 228 | pub fn set_userrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]set_user().(&mut self, user: &str) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 229 | let c_user = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(user)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 230 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`); furthermore, |
| 231 | // `c_user.as_ptr()` will point to a correct null-terminated string. |
| 232 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_set_itemrust-analyzer cargo sudo-rs 0.2.14 pam_set_item().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., PAM_USERrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_USER. as _, c_user.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().() as *const c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#) }) |
| 233 | } |
| 234 | |
| 235 | /// Get the user that is currently active in the PAM handle |
| 236 | pub fn get_userrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]get_user().(&mut self) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 237 | let mut data = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null().(); |
| 238 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`) |
| 239 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_get_itemrust-analyzer cargo sudo-rs 0.2.14 pam_get_item().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., PAM_USERrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_USER. as _, &mut data) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 240 | |
| 241 | // safety check to make sure that we were not passed a null pointer by PAM, |
| 242 | // or that in fact PAM did not write to `data` at all. |
| 243 | if data.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]is_null().() { |
| 244 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#::IoErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#IoError#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 245 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#, |
| 246 | "PAM didn't return username", |
| 247 | ))); |
| 248 | } |
| 249 | |
| 250 | // SAFETY: the contract for `pam_get_item` ensures that if `data` was touched by |
| 251 | // `pam_get_item`, it will point to a valid null-terminated string. |
| 252 | let cstr = unsafe { CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_ptr().(data as *const c_charrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/c_char#) }; |
| 253 | |
| 254 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(cstr.to_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]to_str().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 255 | } |
| 256 | |
| 257 | /// Set the TTY path for the current TTY that this PAM session started from. |
| 258 | pub fn set_ttyrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]set_tty().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#>>(&mut self, tty_path: P) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 259 | let data = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(tty_path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().as_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 260 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`); furthermore, |
| 261 | // `data.as_ptr()` will point to a correct null-terminated string. |
| 262 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_set_itemrust-analyzer cargo sudo-rs 0.2.14 pam_set_item().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., PAM_TTYrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_TTY. as _, data.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().() as *const c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#) }) |
| 263 | } |
| 264 | |
| 265 | // Set the user that requested the actions in this PAM instance. |
| 266 | pub fn set_requesting_userrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]set_requesting_user().(&mut self, user: &str) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 267 | let data = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(user.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 268 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`); furthermore, |
| 269 | // `data.as_ptr()` will point to a correct null-terminated string. |
| 270 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_set_itemrust-analyzer cargo sudo-rs 0.2.14 pam_set_item().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., PAM_RUSERrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_RUSER. as _, data.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().() as *const c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#) }) |
| 271 | } |
| 272 | |
| 273 | /// Re-initialize the credentials stored in PAM |
| 274 | pub fn credentials_reinitializerust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]credentials_reinitialize().(&mut self) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 275 | self.credentialsrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]credentials().(PAM_REINITIALIZE_CREDrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_REINITIALIZE_CRED. as c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) |
| 276 | } |
| 277 | |
| 278 | /// Updates to the credentials stored in PAM |
| 279 | fn credentialsrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]credentials().(&mut self, action: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 280 | let mut flags = action; |
| 281 | flags rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().self.silent_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]silent_flag().(); |
| 282 | |
| 283 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`). |
| 284 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_setcredrust-analyzer cargo sudo-rs 0.2.14 pam_setcred().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., flags) }) |
| 285 | } |
| 286 | |
| 287 | /// Ask the user to change the authentication token (password). |
| 288 | /// |
| 289 | /// If `expired_only` is set to true, only expired authentication tokens |
| 290 | /// will be asked to be replaced, otherwise a replacement will always be |
| 291 | /// requested. |
| 292 | pub fn change_auth_tokenrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]change_auth_token().(&mut self, expired_only: bool) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 293 | let mut flags = 0; |
| 294 | flags rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().self.silent_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]silent_flag().(); |
| 295 | if expired_only { |
| 296 | flags rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOrAssign<Self>`]bitor_assign().PAM_CHANGE_EXPIRED_AUTHTOKrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_CHANGE_EXPIRED_AUTHTOK. as c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#; |
| 297 | } |
| 298 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`). |
| 299 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_chauthtokrust-analyzer cargo sudo-rs 0.2.14 pam_chauthtok().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., flags) }) |
| 300 | } |
| 301 | |
| 302 | /// Start a user session for the authenticated user. |
| 303 | pub fn open_sessionrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]open_session().(&mut self) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<()> { |
| 304 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.session_startedrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#session_started.); |
| 305 | |
| 306 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`). |
| 307 | pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_open_sessionrust-analyzer cargo sudo-rs 0.2.14 pam_open_session().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., self.silent_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]silent_flag().()) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 308 | self.session_startedrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#session_started. = true; |
| 309 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 310 | } |
| 311 | |
| 312 | /// End the user session. |
| 313 | pub fn close_sessionrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]close_session().(&mut self) { |
| 314 | // closing the pam session is best effort, if any error occurs we cannot |
| 315 | // do anything with it |
| 316 | if self.session_startedrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#session_started. { |
| 317 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`). |
| 318 | let _ = pam_errrust-analyzer cargo sudo-rs 0.2.14 pam/error/pam_err().(unsafe { pam_close_sessionrust-analyzer cargo sudo-rs 0.2.14 pam_close_session().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., self.silent_flagrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]silent_flag().()) }); |
| 319 | self.session_startedrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#session_started. = false; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /// Get a full listing of the current PAM environment |
| 324 | pub fn envrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]env().(&mut self) -> PamResultrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamResult#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#)>> { |
| 325 | let mut res = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 326 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`). |
| 327 | // The man page for pam_getenvlist states that: |
| 328 | // The format of the memory is a malloc()'d array of char pointers, the last element |
| 329 | // of which is set to NULL. Each of the non-NULL entries in this array point to a |
| 330 | // NUL terminated and malloc()'d char string of the form: "name=value". |
| 331 | // |
| 332 | // The pam_getenvlist function returns NULL on failure. |
| 333 | let envs = unsafe { pam_getenvlistrust-analyzer cargo sudo-rs 0.2.14 pam_getenvlist().(self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh.) }; |
| 334 | if envs.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() { |
| 335 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PamErrorrust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#::EnvListFailurerust-analyzer cargo sudo-rs 0.2.14 pam/error/PamError#EnvListFailure#); |
| 336 | } |
| 337 | let mut curr_env = envs; |
| 338 | // SAFETY: the loop invariant is as follows: |
| 339 | // - `curr_env` itself is always a valid pointer to an array of valid (possibly NULL) pointers |
| 340 | // - if `curr_env` points to a pointer that is not-null, that data is a c-string allocated by malloc() |
| 341 | // - `curr_env` points to NULL if and only if it is the final element in the array |
| 342 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(curr_str) = NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]new().(unsafe { curr_env.readrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]read().() }) { |
| 343 | let data = { |
| 344 | // SAFETY: `curr_str` points to a valid null-terminated string per the above |
| 345 | let cstr = unsafe { CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_ptr().(curr_str.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().()) }; |
| 346 | let bytes = cstr.to_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]to_bytes().(); |
| 347 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pos) = bytes.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|b| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'=') { |
| 348 | let key = OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::from_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]from_bytes().(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).to_ownedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr][ToOwned]to_owned().(); |
| 349 | let value = OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::from_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]from_bytes().(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).to_ownedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr][ToOwned]to_owned().(); |
| 350 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((key, value)) |
| 351 | } else { |
| 352 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 353 | } |
| 354 | }; |
| 355 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((k, v)) = data { |
| 356 | res.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((k, v)); |
| 357 | } |
| 358 | |
| 359 | // SAFETY: curr_str was obtained via libc::malloc() so we are responsible for freeing it. |
| 360 | // At this point, curr_str is also the only remaining pointer/reference to that allocated data |
| 361 | // (the data was copied above), so it can be deallocated without risk of use-after-free errors. |
| 362 | unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::freerust-analyzer cargo libc 0.2.186 free().(curr_str.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().()) }; |
| 363 | // SAFETY: curr_env was not NULL, so it was not the last element in the list and so PAM |
| 364 | // ensures that the next offset also is a valid pointer, and points to valid data. |
| 365 | curr_env = unsafe { curr_env.offsetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]offset().(1) }; |
| 366 | } |
| 367 | |
| 368 | // SAFETY: `envs` itself was obtained by malloc(), so we are responsible for freeing it. |
| 369 | unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::freerust-analyzer cargo libc 0.2.186 free().(envs.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().()) }; |
| 370 | |
| 371 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res) |
| 372 | } |
| 373 | |
| 374 | /// Check if anything panicked since the last call. |
| 375 | pub fn has_panickedrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]has_panicked().(&self) -> bool { |
| 376 | // SAFETY: self.data_ptr was created by Box::into_raw |
| 377 | unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().self.data_ptrrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#data_ptr.).panickedrust-analyzer cargo sudo-rs 0.2.14 pam/converse/ConverserData#panicked. } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for PamContextrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext# { |
| 382 | fn droprust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext][Drop]drop().(&mut self) { |
| 383 | // data_ptr's pointee is de-allocated in this scope |
| 384 | // SAFETY: self.data_ptr was created by Box::into_raw |
| 385 | let _data = unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(self.data_ptrrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#data_ptr.) }; |
| 386 | self.close_sessionrust-analyzer cargo sudo-rs 0.2.14 pam/impl#[PamContext]close_session().(); |
| 387 | |
| 388 | // It looks like PAM_DATA_SILENT is important to set for our sudo context, but |
| 389 | // it is unclear what it really does and does not do, other than the vague |
| 390 | // documentation description to 'not take the call to seriously' |
| 391 | // Also see https://github.com/systemd/systemd/issues/22318 |
| 392 | // SAFETY: `self.pamh` contains a correct handle (obtained from `pam_start`) |
| 393 | unsafe { |
| 394 | pam_endrust-analyzer cargo sudo-rs 0.2.14 pam_end().( |
| 395 | self.pamhrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#pamh., |
| 396 | self.last_pam_statusrust-analyzer cargo sudo-rs 0.2.14 pam/PamContext#last_pam_status..unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(PAM_SUCCESSrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_SUCCESS. as c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`BitOr<Self>`]bitor().PAM_DATA_SILENTrust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_DATA_SILENT. as c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, |
| 397 | ) |
| 398 | }; |
| 399 | } |
| 400 | } |