| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/::{FromRawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/FromRawFd#, OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#}; |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::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/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/process/::CommandExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/process/CommandExt#; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/}; |
| 6 | |
| 7 | use libcrust-analyzer cargo libc 0.2.186 crate/::O_CLOEXECrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/O_CLOEXEC.; |
| 8 | |
| 9 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::cutilsrust-analyzer cargo sudo-rs 0.2.14 cutils/::cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().; |
| 10 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::logrust-analyzer cargo sudo-rs 0.2.14 log/::user_errorrust-analyzer cargo sudo-rs 0.2.14 log/user_error!; |
| 11 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::interfacerust-analyzer cargo sudo-rs 0.2.14 system/interface/::ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#; |
| 12 | use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::{ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#, auditrust-analyzer cargo sudo-rs 0.2.14 system/audit/, forkrust-analyzer cargo sudo-rs 0.2.14 system/fork()., mark_fds_as_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/mark_fds_as_cloexec().}; |
| 13 | |
| 14 | pub(superrust-analyzer cargo sudo-rs 0.2.14 pam/) fn spawn_askpassrust-analyzer cargo sudo-rs 0.2.14 pam/askpass/spawn_askpass().(program: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, prompt: &str) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<(ProcessIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/ProcessId#, OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#)> { |
| 15 | // Create socket |
| 16 | let mut pipes = [-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1]; |
| 17 | // SAFETY: A valid pointer to a mutable array of 2 fds is passed in. |
| 18 | unsafe { |
| 19 | cerrrust-analyzer cargo sudo-rs 0.2.14 cutils/cerr().(libcrust-analyzer cargo libc 0.2.186 crate/::pipe2rust-analyzer cargo libc 0.2.186 pipe2().(pipes.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().(), O_CLOEXECrust-analyzer cargo libc 0.2.186 unix/linux_like/linux/gnu/b64/x86_64/O_CLOEXEC.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 20 | } |
| 21 | // SAFETY: pipe2 created two owned pipe fds |
| 22 | let (pipe_read, pipe_write) = unsafe { |
| 23 | ( |
| 24 | OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#::from_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[OwnedFd][FromRawFd]from_raw_fd().(pipes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().), |
| 25 | OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#::from_raw_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[OwnedFd][FromRawFd]from_raw_fd().(pipes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().), |
| 26 | ) |
| 27 | }; |
| 28 | |
| 29 | // Spawn child |
| 30 | // SAFETY: There should be no other threads at this point. |
| 31 | let ForkResultrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#::Parentrust-analyzer cargo sudo-rs 0.2.14 system/ForkResult#Parent#(command_pid) = unsafe { forkrust-analyzer cargo sudo-rs 0.2.14 system/fork().() }.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() else { |
| 32 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(pipe_read); |
| 33 | handle_childrust-analyzer cargo sudo-rs 0.2.14 pam/askpass/handle_child().(program, prompt, pipe_write) |
| 34 | }; |
| 35 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(pipe_write); |
| 36 | |
| 37 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((command_pid, pipe_read)) |
| 38 | } |
| 39 | |
| 40 | fn handle_childrust-analyzer cargo sudo-rs 0.2.14 pam/askpass/handle_child().(program: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, prompt: &str, stdout: OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#) -> ! { |
| 41 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) = mark_fds_as_cloexecrust-analyzer cargo sudo-rs 0.2.14 system/mark_fds_as_cloexec().() { |
| 42 | eprintln_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/eprintln_ignore_io_error!!("Failed to mark fds as CLOEXEC: {e}"); |
| 43 | processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1); |
| 44 | }; |
| 45 | |
| 46 | // root privileges are dangerous after this point, since we are about to |
| 47 | // execute a command under control of the user, so drop them |
| 48 | auditrust-analyzer cargo sudo-rs 0.2.14 system/audit/::irrevocably_drop_privilegesrust-analyzer cargo sudo-rs 0.2.14 system/audit/irrevocably_drop_privileges().(); |
| 49 | |
| 50 | // Exec askpass program |
| 51 | let error = Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().(program).argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().(prompt).stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stdout().(stdout).execrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/process/impl#[`process::Command`][CommandExt]exec().(); |
| 52 | user_errorrust-analyzer cargo sudo-rs 0.2.14 log/user_error!!( |
| 53 | "Failed to run askpass program {path}: {error}", |
| 54 | path = program.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), |
| 55 | error = error |
| 56 | ); |
| 57 | processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1); |
| 58 | } |