2013-08-22 13:27:19 +00:00
|
|
|
/*
|
|
|
|
* virpolkit.c: helpers for using polkit APIs
|
|
|
|
*
|
2016-01-14 19:34:28 +00:00
|
|
|
* Copyright (C) 2013, 2014, 2016 Red Hat, Inc.
|
2013-08-22 13:27:19 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2016-02-09 15:09:44 +00:00
|
|
|
#include <poll.h>
|
2013-08-22 13:27:19 +00:00
|
|
|
|
|
|
|
#include "virpolkit.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
#include "virprocess.h"
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virdbus.h"
|
2016-02-09 15:09:44 +00:00
|
|
|
#include "virfile.h"
|
2013-08-22 13:27:19 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_POLKIT
|
|
|
|
|
|
|
|
VIR_LOG_INIT("util.polkit");
|
|
|
|
|
2018-03-07 09:14:23 +00:00
|
|
|
#if WITH_POLKIT
|
2016-02-09 15:09:44 +00:00
|
|
|
|
|
|
|
struct _virPolkitAgent {
|
|
|
|
virCommandPtr cmd;
|
|
|
|
};
|
|
|
|
|
2013-08-22 13:27:19 +00:00
|
|
|
/*
|
|
|
|
* virPolkitCheckAuth:
|
|
|
|
* @actionid: permission to check
|
|
|
|
* @pid: client process ID
|
|
|
|
* @startTime: process start time, or 0
|
|
|
|
* @uid: client process user ID
|
|
|
|
* @details: NULL terminated (key, value) pair list
|
|
|
|
* @allowInteraction: true if auth prompts are allowed
|
|
|
|
*
|
|
|
|
* Check if a client is authenticated with polkit
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure, -2 on auth denied
|
|
|
|
*/
|
|
|
|
int virPolkitCheckAuth(const char *actionid,
|
|
|
|
pid_t pid,
|
|
|
|
unsigned long long startTime,
|
|
|
|
uid_t uid,
|
|
|
|
const char **details,
|
|
|
|
bool allowInteraction)
|
|
|
|
{
|
2014-09-10 13:52:48 +00:00
|
|
|
DBusConnection *sysbus;
|
|
|
|
DBusMessage *reply = NULL;
|
|
|
|
char **retdetails = NULL;
|
|
|
|
size_t nretdetails = 0;
|
2014-11-18 00:18:58 +00:00
|
|
|
bool is_authorized;
|
|
|
|
bool is_challenge;
|
2014-09-10 13:52:48 +00:00
|
|
|
bool is_dismissed = false;
|
|
|
|
size_t i;
|
2013-08-22 13:27:19 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2014-09-10 13:52:48 +00:00
|
|
|
if (!(sysbus = virDBusGetSystemBus()))
|
|
|
|
goto cleanup;
|
2013-08-22 13:27:19 +00:00
|
|
|
|
2014-09-10 13:52:48 +00:00
|
|
|
VIR_INFO("Checking PID %lld running as %d",
|
|
|
|
(long long) pid, uid);
|
2013-08-22 13:27:19 +00:00
|
|
|
|
2014-09-10 13:52:48 +00:00
|
|
|
if (virDBusCallMethod(sysbus,
|
|
|
|
&reply,
|
|
|
|
NULL,
|
|
|
|
"org.freedesktop.PolicyKit1",
|
|
|
|
"/org/freedesktop/PolicyKit1/Authority",
|
|
|
|
"org.freedesktop.PolicyKit1.Authority",
|
|
|
|
"CheckAuthorization",
|
|
|
|
"(sa{sv})sa&{ss}us",
|
|
|
|
"unix-process",
|
|
|
|
3,
|
|
|
|
"pid", "u", (unsigned int)pid,
|
|
|
|
"start-time", "t", startTime,
|
|
|
|
"uid", "i", (int)uid,
|
|
|
|
actionid,
|
2016-02-09 15:01:33 +00:00
|
|
|
virStringListLength(details) / 2,
|
2014-09-10 13:52:48 +00:00
|
|
|
details,
|
|
|
|
allowInteraction,
|
|
|
|
"" /* cancellation ID */) < 0)
|
2013-08-22 13:27:19 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2019-04-12 16:58:01 +00:00
|
|
|
if (virDBusMessageDecode(reply,
|
|
|
|
"(bba&{ss})",
|
|
|
|
&is_authorized,
|
|
|
|
&is_challenge,
|
|
|
|
&nretdetails,
|
|
|
|
&retdetails) < 0)
|
2013-08-22 13:27:19 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2014-09-10 13:52:48 +00:00
|
|
|
for (i = 0; i < (nretdetails / 2); i++) {
|
|
|
|
if (STREQ(retdetails[(i * 2)], "polkit.dismissed") &&
|
|
|
|
STREQ(retdetails[(i * 2) + 1], "true"))
|
|
|
|
is_dismissed = true;
|
|
|
|
}
|
2013-08-22 13:27:19 +00:00
|
|
|
|
2014-09-10 13:52:48 +00:00
|
|
|
VIR_DEBUG("is auth %d is challenge %d",
|
|
|
|
is_authorized, is_challenge);
|
2013-08-22 13:27:19 +00:00
|
|
|
|
2014-09-10 13:52:48 +00:00
|
|
|
if (is_authorized) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = -2;
|
|
|
|
if (is_dismissed)
|
2013-08-22 13:27:19 +00:00
|
|
|
virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
|
2014-09-10 13:52:48 +00:00
|
|
|
_("user cancelled authentication process"));
|
|
|
|
else if (is_challenge)
|
2016-01-14 19:34:28 +00:00
|
|
|
virReportError(VIR_ERR_AUTH_UNAVAILABLE,
|
|
|
|
_("no polkit agent available to authenticate "
|
|
|
|
"action '%s'"),
|
|
|
|
actionid);
|
2014-09-10 13:52:48 +00:00
|
|
|
else
|
|
|
|
virReportError(VIR_ERR_AUTH_FAILED, "%s",
|
|
|
|
_("access denied by policy"));
|
2013-08-22 13:27:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 13:52:48 +00:00
|
|
|
cleanup:
|
2016-11-25 08:18:35 +00:00
|
|
|
virStringListFreeCount(retdetails, nretdetails);
|
2017-10-04 08:38:45 +00:00
|
|
|
virDBusMessageUnref(reply);
|
2013-08-22 13:27:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-09 15:09:44 +00:00
|
|
|
/* virPolkitAgentDestroy:
|
|
|
|
* @cmd: Pointer to the virCommandPtr created during virPolkitAgentCreate
|
|
|
|
*
|
|
|
|
* Destroy resources used by Polkit Agent
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virPolkitAgentDestroy(virPolkitAgentPtr agent)
|
|
|
|
{
|
|
|
|
if (!agent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virCommandFree(agent->cmd);
|
|
|
|
VIR_FREE(agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virPolkitAgentCreate:
|
|
|
|
*
|
|
|
|
* Allocate and setup a polkit agent
|
|
|
|
*
|
|
|
|
* Returns a virCommandPtr on success and NULL on failure
|
|
|
|
*/
|
|
|
|
virPolkitAgentPtr
|
|
|
|
virPolkitAgentCreate(void)
|
|
|
|
{
|
2016-03-02 00:36:37 +00:00
|
|
|
virPolkitAgentPtr agent = NULL;
|
2016-02-09 15:09:44 +00:00
|
|
|
int pipe_fd[2] = {-1, -1};
|
|
|
|
struct pollfd pollfd;
|
|
|
|
int outfd = STDOUT_FILENO;
|
|
|
|
int errfd = STDERR_FILENO;
|
|
|
|
|
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (pipe2(pipe_fd, 0) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(agent) < 0)
|
|
|
|
goto error;
|
2016-05-09 09:02:51 +00:00
|
|
|
|
|
|
|
agent->cmd = virCommandNewArgList(PKTTYAGENT, "--process", NULL);
|
|
|
|
|
|
|
|
virCommandAddArgFormat(agent->cmd, "%lld", (long long int) getpid());
|
|
|
|
virCommandAddArg(agent->cmd, "--notify-fd");
|
|
|
|
virCommandAddArgFormat(agent->cmd, "%d", pipe_fd[1]);
|
|
|
|
virCommandAddArg(agent->cmd, "--fallback");
|
|
|
|
virCommandSetInputFD(agent->cmd, STDIN_FILENO);
|
|
|
|
virCommandSetOutputFD(agent->cmd, &outfd);
|
|
|
|
virCommandSetErrorFD(agent->cmd, &errfd);
|
|
|
|
virCommandPassFD(agent->cmd, pipe_fd[1], VIR_COMMAND_PASS_FD_CLOSE_PARENT);
|
2019-04-30 09:17:22 +00:00
|
|
|
pipe_fd[1] = -1;
|
2016-05-09 09:02:51 +00:00
|
|
|
if (virCommandRunAsync(agent->cmd, NULL) < 0)
|
2016-02-09 15:09:44 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
pollfd.fd = pipe_fd[0];
|
|
|
|
pollfd.events = POLLHUP;
|
|
|
|
|
|
|
|
if (poll(&pollfd, 1, -1) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return agent;
|
|
|
|
|
|
|
|
error:
|
|
|
|
VIR_FORCE_CLOSE(pipe_fd[0]);
|
|
|
|
VIR_FORCE_CLOSE(pipe_fd[1]);
|
|
|
|
virPolkitAgentDestroy(agent);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 09:14:23 +00:00
|
|
|
#else /* ! WITH_POLKIT */
|
2013-08-22 13:27:19 +00:00
|
|
|
|
2014-09-25 08:57:24 +00:00
|
|
|
int virPolkitCheckAuth(const char *actionid ATTRIBUTE_UNUSED,
|
|
|
|
pid_t pid ATTRIBUTE_UNUSED,
|
|
|
|
unsigned long long startTime ATTRIBUTE_UNUSED,
|
|
|
|
uid_t uid ATTRIBUTE_UNUSED,
|
|
|
|
const char **details ATTRIBUTE_UNUSED,
|
|
|
|
bool allowInteraction ATTRIBUTE_UNUSED)
|
2013-08-22 13:27:19 +00:00
|
|
|
{
|
|
|
|
VIR_ERROR(_("Polkit auth attempted, even though polkit is not available"));
|
|
|
|
virReportError(VIR_ERR_AUTH_FAILED, "%s",
|
|
|
|
_("authentication failed"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-09 15:09:44 +00:00
|
|
|
void
|
2016-03-01 14:51:37 +00:00
|
|
|
virPolkitAgentDestroy(virPolkitAgentPtr agent ATTRIBUTE_UNUSED)
|
2016-02-09 15:09:44 +00:00
|
|
|
{
|
|
|
|
return; /* do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-01 14:51:37 +00:00
|
|
|
virPolkitAgentPtr
|
2016-02-09 15:09:44 +00:00
|
|
|
virPolkitAgentCreate(void)
|
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_AUTH_FAILED, "%s",
|
|
|
|
_("polkit text authentication agent unavailable"));
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-07 09:14:23 +00:00
|
|
|
#endif /* WITH_POLKIT */
|