Debian Codegraph @

src/pam/askpass.rs

raw symbols SCIP
1 use std::os::fd::{FromRawFd, OwnedFd};
2 use std::os::unix::process::CommandExt;
3 use std::path::Path;
4 use std::process::Command;
5 use std::{io, process};
6
7 use libc::O_CLOEXEC;
8
9 use crate::cutils::cerr;
10 use crate::log::user_error;
11 use crate::system::interface::ProcessId;
12 use crate::system::{ForkResult, audit, fork, mark_fds_as_cloexec};
13
14 pub(super) fn spawn_askpass(program: &Path, prompt: &str) -> io::Result<(ProcessId, OwnedFd)> {
15 // Create socket
16 let mut pipes = [-1, -1];
17 // SAFETY: A valid pointer to a mutable array of 2 fds is passed in.
18 unsafe {
19 cerr(libc::pipe2(pipes.as_mut_ptr(), O_CLOEXEC))?;
20 }
21 // SAFETY: pipe2 created two owned pipe fds
22 let (pipe_read, pipe_write) = unsafe {
23 (
24 OwnedFd::from_raw_fd(pipes[0]),
25 OwnedFd::from_raw_fd(pipes[1]),
26 )
27 };
28
29 // Spawn child
30 // SAFETY: There should be no other threads at this point.
31 let ForkResult::Parent(command_pid) = unsafe { fork() }.unwrap() else {
32 drop(pipe_read);
33 handle_child(program, prompt, pipe_write)
34 };
35 drop(pipe_write);
36
37 Ok((command_pid, pipe_read))
38 }
39
40 fn handle_child(program: &Path, prompt: &str, stdout: OwnedFd) -> ! {
41 if let Err(e) = mark_fds_as_cloexec() {
42 eprintln_ignore_io_error!("Failed to mark fds as CLOEXEC: {e}");
43 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 audit::irrevocably_drop_privileges();
49
50 // Exec askpass program
51 let error = Command::new(program).arg(prompt).stdout(stdout).exec();
52 user_error!(
53 "Failed to run askpass program {path}: {error}",
54 path = program.display(),
55 error = error
56 );
57 process::exit(1);
58 }