| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <poll.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "bus-util.h" |
| 8 |
#include "exec-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "io-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "path-util.h" |
| 13 |
#include "pidref.h" |
| 14 |
#include "polkit-agent.h" |
| 15 |
#include "stdio-util.h" |
| 16 |
|
| 17 |
#if ENABLE_POLKIT |
| 18 |
static PidRef agent_pidref = PIDREF_NULL; |
| 19 |
|
| 20 |
int polkit_agent_open(void) { |
| 21 |
_cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR; |
| 22 |
char notify_fd[DECIMAL_STR_MAX(int) + 1]; |
| 23 |
int r; |
| 24 |
|
| 25 |
if (pidref_is_set(&agent_pidref)) |
| 26 |
return 0; |
| 27 |
|
| 28 |
/* Clients that run as root don't need to activate/query polkit */ |
| 29 |
if (geteuid() == 0) |
| 30 |
return 0; |
| 31 |
|
| 32 |
r = shall_fork_agent(); |
| 33 |
if (r <= 0) |
| 34 |
return r; |
| 35 |
|
| 36 |
_cleanup_free_ char *pkttyagent = NULL; |
| 37 |
r = find_executable("pkttyagent", &pkttyagent); |
| 38 |
if (r == -ENOENT) { |
| 39 |
log_debug("pkttyagent binary not available, ignoring."); |
| 40 |
return 0; |
| 41 |
} |
| 42 |
if (r < 0) |
| 43 |
return log_error_errno(r, "Failed to determine whether pkttyagent binary exists: %m"); |
| 44 |
|
| 45 |
if (pipe2(pipe_fd, 0) < 0) |
| 46 |
return -errno; |
| 47 |
|
| 48 |
xsprintf(notify_fd, "%i", pipe_fd[1]); |
| 49 |
|
| 50 |
r = fork_agent("(polkit-agent)", |
| 51 |
&pipe_fd[1], |
| 52 |
1, |
| 53 |
&agent_pidref, |
| 54 |
pkttyagent, |
| 55 |
"--notify-fd", notify_fd, |
| 56 |
"--fallback"); |
| 57 |
if (r < 0) |
| 58 |
return log_error_errno(r, "Failed to fork polkit agent: %m"); |
| 59 |
|
| 60 |
/* Close the writing side, because that's the one for the agent */ |
| 61 |
pipe_fd[1] = safe_close(pipe_fd[1]); |
| 62 |
|
| 63 |
/* Wait until the agent closes the fd */ |
| 64 |
(void) fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY); |
| 65 |
|
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
void polkit_agent_close(void) { |
| 70 |
/* Inform agent that we are done */ |
| 71 |
pidref_done_sigterm_wait(&agent_pidref); |
| 72 |
} |
| 73 |
|
| 74 |
#else |
| 75 |
|
| 76 |
int polkit_agent_open(void) { |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
void polkit_agent_close(void) { |
| 81 |
} |
| 82 |
|
| 83 |
#endif |
| 84 |
|
| 85 |
int polkit_agent_open_if_enabled(BusTransport transport, bool ask_password) { |
| 86 |
|
| 87 |
/* Open the polkit agent as a child process if necessary */ |
| 88 |
|
| 89 |
if (transport != BUS_TRANSPORT_LOCAL) |
| 90 |
return 0; |
| 91 |
|
| 92 |
if (!ask_password) |
| 93 |
return 0; |
| 94 |
|
| 95 |
return polkit_agent_open(); |
| 96 |
} |