2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-04-11 18:50:16 +02:00
|
|
|
|
2012-04-11 22:37:13 +02:00
|
|
|
#include <errno.h>
|
2015-02-12 14:06:32 +01:00
|
|
|
#include <poll.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-04-11 18:50:16 +02:00
|
|
|
|
2015-10-25 14:08:25 +01:00
|
|
|
#include "fd-util.h"
|
|
|
|
#include "io-util.h"
|
2012-04-11 18:50:16 +02:00
|
|
|
#include "log.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "macro.h"
|
2015-04-10 19:10:00 +02:00
|
|
|
#include "process-util.h"
|
2012-04-11 18:50:16 +02:00
|
|
|
#include "spawn-polkit-agent.h"
|
2015-10-27 01:26:31 +01:00
|
|
|
#include "stdio-util.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "time-util.h"
|
2015-10-25 14:08:25 +01:00
|
|
|
#include "util.h"
|
2012-04-11 18:50:16 +02:00
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if ENABLE_POLKIT
|
2012-04-11 18:50:16 +02:00
|
|
|
static pid_t agent_pid = 0;
|
|
|
|
|
|
|
|
int polkit_agent_open(void) {
|
2015-01-27 14:00:11 +01:00
|
|
|
char notify_fd[DECIMAL_STR_MAX(int) + 1];
|
2017-12-22 15:07:22 +01:00
|
|
|
int pipe_fd[2], r;
|
2012-04-11 18:50:16 +02:00
|
|
|
|
|
|
|
if (agent_pid > 0)
|
|
|
|
return 0;
|
|
|
|
|
2016-04-05 10:30:45 +02:00
|
|
|
/* Clients that run as root don't need to activate/query polkit */
|
|
|
|
if (geteuid() == 0)
|
|
|
|
return 0;
|
|
|
|
|
2017-12-22 15:07:22 +01:00
|
|
|
/* We check STDIN here, not STDOUT, since this is about input, not output */
|
2012-04-11 18:50:16 +02:00
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
return 0;
|
|
|
|
|
2017-12-29 18:13:38 +01:00
|
|
|
if (!is_main_thread())
|
|
|
|
return -EPERM;
|
|
|
|
|
2012-04-11 22:37:13 +02:00
|
|
|
if (pipe2(pipe_fd, 0) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(notify_fd, "%i", pipe_fd[1]);
|
2012-04-11 22:37:13 +02:00
|
|
|
|
2017-12-22 15:07:22 +01:00
|
|
|
r = fork_agent("(polkit-agent)",
|
2012-04-11 22:37:13 +02:00
|
|
|
&pipe_fd[1], 1,
|
2017-12-22 15:07:22 +01:00
|
|
|
&agent_pid,
|
2012-04-11 22:37:13 +02:00
|
|
|
POLKIT_AGENT_BINARY_PATH,
|
2012-04-13 21:52:25 +02:00
|
|
|
POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
|
2012-04-11 22:37:13 +02:00
|
|
|
|
|
|
|
/* Close the writing side, because that's the one for the agent */
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(pipe_fd[1]);
|
2012-04-11 22:37:13 +02:00
|
|
|
|
2012-04-11 18:50:16 +02:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to fork TTY ask password agent: %m");
|
2012-04-11 22:37:13 +02:00
|
|
|
else
|
|
|
|
/* Wait until the agent closes the fd */
|
2014-07-29 12:23:31 +02:00
|
|
|
fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY);
|
2012-04-11 22:37:13 +02:00
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(pipe_fd[0]);
|
2012-04-11 18:50:16 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void polkit_agent_close(void) {
|
|
|
|
|
|
|
|
if (agent_pid <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Inform agent that we are done */
|
2017-12-22 15:10:56 +01:00
|
|
|
(void) kill_and_sigcont(agent_pid, SIGTERM);
|
2014-11-09 15:51:04 +01:00
|
|
|
(void) wait_for_terminate(agent_pid, NULL);
|
2012-04-11 18:50:16 +02:00
|
|
|
agent_pid = 0;
|
|
|
|
}
|
2013-02-13 22:56:43 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int polkit_agent_open(void) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void polkit_agent_close(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2019-11-15 11:11:10 +01:00
|
|
|
|
|
|
|
int polkit_agent_open_if_enabled(BusTransport transport, bool ask_password) {
|
|
|
|
|
|
|
|
/* Open the polkit agent as a child process if necessary */
|
|
|
|
|
|
|
|
if (transport != BUS_TRANSPORT_LOCAL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!ask_password)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return polkit_agent_open();
|
|
|
|
}
|