virpolkit.c 6.7 KB
Newer Older
1 2 3
/*
 * virpolkit.c: helpers for using polkit APIs
 *
4
 * Copyright (C) 2013, 2014, 2016 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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>
23
#include <poll.h>
24 25 26 27 28 29 30 31

#include "virpolkit.h"
#include "virerror.h"
#include "virlog.h"
#include "virstring.h"
#include "virprocess.h"
#include "viralloc.h"
#include "virdbus.h"
32
#include "virfile.h"
33 34 35 36 37

#define VIR_FROM_THIS VIR_FROM_POLKIT

VIR_LOG_INIT("util.polkit");

J
Ján Tomko 已提交
38
#if WITH_POLKIT
39 40 41 42 43

struct _virPolkitAgent {
    virCommandPtr cmd;
};

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/*
 * 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)
{
64 65 66 67
    DBusConnection *sysbus;
    DBusMessage *reply = NULL;
    char **retdetails = NULL;
    size_t nretdetails = 0;
68 69
    bool is_authorized;
    bool is_challenge;
70 71
    bool is_dismissed = false;
    size_t i;
72 73
    int ret = -1;

74 75
    if (!(sysbus = virDBusGetSystemBus()))
        goto cleanup;
76

77 78
    VIR_INFO("Checking PID %lld running as %d",
             (long long) pid, uid);
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93
    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,
94
                          virStringListLength(details) / 2,
95 96 97
                          details,
                          allowInteraction,
                          "" /* cancellation ID */) < 0)
98 99
        goto cleanup;

100 101 102 103 104 105
    if (virDBusMessageDecode(reply,
                             "(bba&{ss})",
                             &is_authorized,
                             &is_challenge,
                             &nretdetails,
                             &retdetails) < 0)
106 107
        goto cleanup;

108 109 110 111 112
    for (i = 0; i < (nretdetails / 2); i++) {
        if (STREQ(retdetails[(i * 2)], "polkit.dismissed") &&
            STREQ(retdetails[(i * 2) + 1], "true"))
            is_dismissed = true;
    }
113

114 115
    VIR_DEBUG("is auth %d  is challenge %d",
              is_authorized, is_challenge);
116

117 118 119 120 121
    if (is_authorized) {
        ret = 0;
    } else {
        ret = -2;
        if (is_dismissed)
122
            virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
123 124
                           _("user cancelled authentication process"));
        else if (is_challenge)
125 126 127 128
            virReportError(VIR_ERR_AUTH_UNAVAILABLE,
                           _("no polkit agent available to authenticate "
                             "action '%s'"),
                           actionid);
129 130 131
        else
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("access denied by policy"));
132 133
    }

134
 cleanup:
135
    virStringListFreeCount(retdetails, nretdetails);
136
    virDBusMessageUnref(reply);
137 138 139 140
    return ret;
}


141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/* 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)
{
165
    virPolkitAgentPtr agent = NULL;
166 167 168 169 170 171 172 173 174 175 176 177 178
    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;
179 180 181 182 183 184 185 186 187 188 189

    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);
190
    pipe_fd[1] = -1;
191
    if (virCommandRunAsync(agent->cmd, NULL) < 0)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        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;
}


J
Ján Tomko 已提交
210
#else /* ! WITH_POLKIT */
211

P
Pavel Hrdina 已提交
212 213 214 215 216 217
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)
218 219 220 221 222 223 224 225
{
    VIR_ERROR(_("Polkit auth attempted, even though polkit is not available"));
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
    return -1;
}


226
void
J
Jiri Denemark 已提交
227
virPolkitAgentDestroy(virPolkitAgentPtr agent ATTRIBUTE_UNUSED)
228 229 230 231 232
{
    return; /* do nothing */
}


J
Jiri Denemark 已提交
233
virPolkitAgentPtr
234 235 236 237 238 239
virPolkitAgentCreate(void)
{
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("polkit text authentication agent unavailable"));
    return NULL;
}
J
Ján Tomko 已提交
240
#endif /* WITH_POLKIT */