admin_server_dispatch.c 15.8 KB
Newer Older
1
/*
2
 * admin_server_dispatch.c: handlers for admin RPC method calls
3
 *
4
 * Copyright (C) 2014-2016 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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>

#include "internal.h"
#include "libvirt_internal.h"

26
#include "admin_server_dispatch.h"
27
#include "admin_server.h"
28 29 30 31
#include "datatypes.h"
#include "viralloc.h"
#include "virerror.h"
#include "virlog.h"
32 33
#include "rpc/virnetdaemon.h"
#include "rpc/virnetserver.h"
34 35
#include "virstring.h"
#include "virthreadjob.h"
36
#include "virtypedparam.h"
37
#include "virutil.h"
38 39 40 41 42

#define VIR_FROM_THIS VIR_FROM_ADMIN

VIR_LOG_INIT("daemon.admin");

43 44 45 46 47 48 49 50 51
typedef struct daemonAdmClientPrivate daemonAdmClientPrivate;
typedef daemonAdmClientPrivate *daemonAdmClientPrivatePtr;
/* Separate private data for admin connection */
struct daemonAdmClientPrivate {
    /* Just a placeholder, not that there is anything to be locked */
    virMutex lock;

    virNetDaemonPtr dmn;
};
52 53

void
54
remoteAdmClientFree(void *data)
55 56 57 58 59 60 61 62 63
{
    struct daemonAdmClientPrivate *priv = data;

    virMutexDestroy(&priv->lock);
    virObjectUnref(priv->dmn);
    VIR_FREE(priv);
}

void *
J
Ján Tomko 已提交
64
remoteAdmClientNew(virNetServerClientPtr client G_GNUC_UNUSED,
65
                   void *opaque)
66 67
{
    struct daemonAdmClientPrivate *priv;
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    uid_t clientuid;
    gid_t clientgid;
    pid_t clientpid;
    unsigned long long timestamp;

    if (virNetServerClientGetUNIXIdentity(client,
                                          &clientuid,
                                          &clientgid,
                                          &clientpid,
                                          &timestamp) < 0)
        return NULL;

    VIR_DEBUG("New client pid %lld uid %lld",
              (long long)clientpid,
              (long long)clientuid);

    if (geteuid() != clientuid) {
        virReportRestrictedError(_("Disallowing client %lld with uid %lld"),
                                 (long long)clientpid,
                                 (long long)clientuid);
        return NULL;
    }
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    if (VIR_ALLOC(priv) < 0)
        return NULL;

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
        virReportSystemError(errno, "%s", _("unable to init mutex"));
        return NULL;
    }

    /*
     * We don't necessarily need to ref this object right now as there
     * must be one ref being held throughout the life of the daemon,
     * but let's just be safe for future.
     */
    priv->dmn = virObjectRef(opaque);

    return priv;
}

110
void *remoteAdmClientNewPostExecRestart(virNetServerClientPtr client,
J
Ján Tomko 已提交
111
                                        virJSONValuePtr object G_GNUC_UNUSED,
112 113 114 115 116
                                        void *opaque)
{
    return remoteAdmClientNew(client, opaque);
}

J
Ján Tomko 已提交
117 118
virJSONValuePtr remoteAdmClientPreExecRestart(virNetServerClientPtr client G_GNUC_UNUSED,
                                              void *data G_GNUC_UNUSED)
119 120 121 122 123 124 125 126 127 128 129 130
{
    virJSONValuePtr object = virJSONValueNewObject();

    if (!object)
        return NULL;

    /* No content to add at this time - just need empty object */

    return object;
}


131 132
/* Helpers */

E
Erik Skultety 已提交
133 134 135 136 137 138
static virNetServerPtr
get_nonnull_server(virNetDaemonPtr dmn, admin_nonnull_server srv)
{
    return virNetDaemonGetServer(dmn, srv.name);
}

139
static void
140
make_nonnull_server(admin_nonnull_server *srv_dst,
141
                    virNetServerPtr srv_src)
142
{
143
    srv_dst->name = g_strdup(virNetServerGetName(srv_src));
144 145
}

146 147 148 149 150 151
static virNetServerClientPtr
get_nonnull_client(virNetServerPtr srv, admin_nonnull_client clnt)
{
    return virNetServerGetClient(srv, clnt.id);
}

152
static void
E
Erik Skultety 已提交
153 154 155 156 157 158 159 160
make_nonnull_client(admin_nonnull_client *clt_dst,
                    virNetServerClientPtr clt_src)
{
    clt_dst->id = virNetServerClientGetID(clt_src);
    clt_dst->timestamp = virNetServerClientGetTimestamp(clt_src);
    clt_dst->transport = virNetServerClientGetTransport(clt_src);
}

161 162
/* Functions */
static int
J
Ján Tomko 已提交
163
adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED,
164
                         virNetServerClientPtr client,
J
Ján Tomko 已提交
165
                         virNetMessagePtr msg G_GNUC_UNUSED,
166 167
                         virNetMessageErrorPtr rerr,
                         struct admin_connect_open_args *args)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
    unsigned int flags;
    struct daemonAdmClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int ret = -1;

    VIR_DEBUG("priv=%p dmn=%p", priv, priv->dmn);
    virMutexLock(&priv->lock);

    flags = args->flags;
    virCheckFlagsGoto(0, cleanup);

    ret = 0;
 cleanup:
    if (ret < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return ret;
}

static int
J
Ján Tomko 已提交
189
adminDispatchConnectClose(virNetServerPtr server G_GNUC_UNUSED,
190
                          virNetServerClientPtr client,
J
Ján Tomko 已提交
191 192
                          virNetMessagePtr msg G_GNUC_UNUSED,
                          virNetMessageErrorPtr rerr G_GNUC_UNUSED)
193 194 195 196 197
{
    virNetServerClientDelayedClose(client);
    return 0;
}

198
static int
J
Ján Tomko 已提交
199
adminConnectGetLibVersion(virNetDaemonPtr dmn G_GNUC_UNUSED,
200
                          unsigned long long *libVer)
201 202 203 204 205 206
{
    if (libVer)
        *libVer = LIBVIR_VERSION_NUMBER;
    return 0;
}

207 208 209 210 211 212 213 214 215
static virNetDaemonPtr
adminGetConn(virNetServerClientPtr client)
{
    struct daemonAdmClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    return priv->dmn;
}

216
static int
J
Ján Tomko 已提交
217
adminDispatchServerGetThreadpoolParameters(virNetServerPtr server G_GNUC_UNUSED,
218
                                           virNetServerClientPtr client,
J
Ján Tomko 已提交
219
                                           virNetMessagePtr msg G_GNUC_UNUSED,
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
                                           virNetMessageErrorPtr rerr,
                                           struct admin_server_get_threadpool_parameters_args *args,
                                           struct admin_server_get_threadpool_parameters_ret *ret)
{
    int rv = -1;
    virNetServerPtr srv = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    struct daemonAdmClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!(srv = virNetDaemonGetServer(priv->dmn, args->srv.name)))
        goto cleanup;

    if (adminServerGetThreadPoolParameters(srv, &params, &nparams,
                                           args->flags) < 0)
        goto cleanup;

    if (virTypedParamsSerialize(params, nparams,
239
                                ADMIN_SERVER_THREADPOOL_PARAMETERS_MAX,
240 241 242 243 244 245 246 247 248 249 250 251 252
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len, 0) < 0)
        goto cleanup;

    rv = 0;
 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);

    virTypedParamsFree(params, nparams);
    virObjectUnref(srv);
    return rv;
}
253 254

static int
J
Ján Tomko 已提交
255
adminDispatchServerSetThreadpoolParameters(virNetServerPtr server G_GNUC_UNUSED,
256
                                           virNetServerClientPtr client,
J
Ján Tomko 已提交
257
                                           virNetMessagePtr msg G_GNUC_UNUSED,
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
                                           virNetMessageErrorPtr rerr,
                                           struct admin_server_set_threadpool_parameters_args *args)
{
    int rv = -1;
    virNetServerPtr srv = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    struct daemonAdmClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!(srv = virNetDaemonGetServer(priv->dmn, args->srv.name))) {
        virReportError(VIR_ERR_NO_SERVER,
                       _("no server with matching name '%s' found"),
                       args->srv.name);
        goto cleanup;
    }

    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  ADMIN_SERVER_THREADPOOL_PARAMETERS_MAX,
                                  &params,
                                  &nparams) < 0)
        goto cleanup;


    if (adminServerSetThreadPoolParameters(srv, params,
                                           nparams, args->flags) < 0)
        goto cleanup;

    rv = 0;
 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);

    virTypedParamsFree(params, nparams);
    virObjectUnref(srv);
    return rv;
}
296 297

static int
J
Ján Tomko 已提交
298
adminDispatchClientGetInfo(virNetServerPtr server G_GNUC_UNUSED,
299
                           virNetServerClientPtr client,
J
Ján Tomko 已提交
300
                           virNetMessagePtr msg G_GNUC_UNUSED,
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
                           virNetMessageErrorPtr rerr,
                           struct admin_client_get_info_args *args,
                           struct admin_client_get_info_ret *ret)
{
    int rv = -1;
    virNetServerPtr srv = NULL;
    virNetServerClientPtr clnt = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    struct daemonAdmClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!(srv = virNetDaemonGetServer(priv->dmn, args->clnt.srv.name))) {
        virReportError(VIR_ERR_NO_SERVER,
                       _("no server with matching name '%s' found"),
                       args->clnt.srv.name);
        goto cleanup;
    }

    if (!(clnt = virNetServerGetClient(srv, args->clnt.id))) {
        virReportError(VIR_ERR_NO_CLIENT,
E
Eric Blake 已提交
322 323
                       _("no client with matching id '%llu' found"),
                       (unsigned long long) args->clnt.id);
324 325 326 327 328 329 330
        goto cleanup;
    }

    if (adminClientGetInfo(clnt, &params, &nparams, args->flags) < 0)
        goto cleanup;

    if (virTypedParamsSerialize(params, nparams,
331
                                ADMIN_CLIENT_INFO_PARAMETERS_MAX,
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0)
        goto cleanup;

    rv = 0;

 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);

    virTypedParamsFree(params, nparams);
    virObjectUnref(clnt);
    virObjectUnref(srv);
    return rv;
}
348 349

static int
J
Ján Tomko 已提交
350
adminDispatchServerGetClientLimits(virNetServerPtr server G_GNUC_UNUSED,
351
                                   virNetServerClientPtr client,
J
Ján Tomko 已提交
352 353
                                   virNetMessagePtr msg G_GNUC_UNUSED,
                                   virNetMessageErrorPtr rerr G_GNUC_UNUSED,
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
                                   admin_server_get_client_limits_args *args,
                                   admin_server_get_client_limits_ret *ret)
{
    int rv = -1;
    virNetServerPtr srv = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    struct daemonAdmClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!(srv = virNetDaemonGetServer(priv->dmn, args->srv.name)))
        goto cleanup;

    if (adminServerGetClientLimits(srv, &params, &nparams, args->flags) < 0)
        goto cleanup;

    if (virTypedParamsSerialize(params, nparams,
371
                                ADMIN_SERVER_CLIENT_LIMITS_MAX,
372 373 374 375 376 377 378 379 380 381 382 383 384
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len, 0) < 0)
        goto cleanup;

    rv = 0;
 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);

    virTypedParamsFree(params, nparams);
    virObjectUnref(srv);
    return rv;
}
385 386

static int
J
Ján Tomko 已提交
387
adminDispatchServerSetClientLimits(virNetServerPtr server G_GNUC_UNUSED,
388
                                   virNetServerClientPtr client,
J
Ján Tomko 已提交
389 390
                                   virNetMessagePtr msg G_GNUC_UNUSED,
                                   virNetMessageErrorPtr rerr G_GNUC_UNUSED,
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
                                   admin_server_set_client_limits_args *args)
{
    int rv = -1;
    virNetServerPtr srv = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    struct daemonAdmClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!(srv = virNetDaemonGetServer(priv->dmn, args->srv.name))) {
        virReportError(VIR_ERR_NO_SERVER,
                       _("no server with matching name '%s' found"),
                       args->srv.name);
        goto cleanup;
    }

    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
        args->params.params_len,
        ADMIN_SERVER_CLIENT_LIMITS_MAX, &params, &nparams) < 0)
        goto cleanup;

    if (adminServerSetClientLimits(srv, params, nparams, args->flags) < 0)
        goto cleanup;

    rv = 0;
 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
    virObjectUnref(srv);
    return rv;
}
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

/* Returns the number of outputs stored in @outputs */
static int
adminConnectGetLoggingOutputs(char **outputs, unsigned int flags)
{
    char *tmp = NULL;

    virCheckFlags(0, -1);

    if (!(tmp = virLogGetOutputs()))
        return -1;

    *outputs = tmp;
    return virLogGetNbOutputs();
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
/* Returns the number of defined filters or -1 in case of an error */
static int
adminConnectGetLoggingFilters(char **filters, unsigned int flags)
{
    char *tmp = NULL;
    int ret = 0;

    virCheckFlags(0, -1);

    if ((ret = virLogGetNbFilters()) > 0 && !(tmp = virLogGetFilters()))
        return -1;

    *filters = tmp;
    return ret;
}

455
static int
J
Ján Tomko 已提交
456
adminConnectSetLoggingOutputs(virNetDaemonPtr dmn G_GNUC_UNUSED,
457 458 459 460 461 462 463 464
                              const char *outputs,
                              unsigned int flags)
{
    virCheckFlags(0, -1);

    return virLogSetOutputs(outputs);
}

465
static int
J
Ján Tomko 已提交
466
adminConnectSetLoggingFilters(virNetDaemonPtr dmn G_GNUC_UNUSED,
467 468 469 470 471 472 473 474
                              const char *filters,
                              unsigned int flags)
{
    virCheckFlags(0, -1);

    return virLogSetFilters(filters);
}

475
static int
J
Ján Tomko 已提交
476 477 478
adminDispatchConnectGetLoggingOutputs(virNetServerPtr server G_GNUC_UNUSED,
                                      virNetServerClientPtr client G_GNUC_UNUSED,
                                      virNetMessagePtr msg G_GNUC_UNUSED,
479 480 481 482 483 484 485
                                      virNetMessageErrorPtr rerr,
                                      admin_connect_get_logging_outputs_args *args,
                                      admin_connect_get_logging_outputs_ret *ret)
{
    char *outputs = NULL;
    int noutputs = 0;

486
    if ((noutputs = adminConnectGetLoggingOutputs(&outputs, args->flags)) < 0) {
487 488 489 490
        virNetMessageSaveError(rerr);
        return -1;
    }

491
    ret->outputs = g_steal_pointer(&outputs);
492 493 494 495
    ret->noutputs = noutputs;

    return 0;
}
496 497

static int
J
Ján Tomko 已提交
498 499 500
adminDispatchConnectGetLoggingFilters(virNetServerPtr server G_GNUC_UNUSED,
                                      virNetServerClientPtr client G_GNUC_UNUSED,
                                      virNetMessagePtr msg G_GNUC_UNUSED,
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
                                      virNetMessageErrorPtr rerr,
                                      admin_connect_get_logging_filters_args *args,
                                      admin_connect_get_logging_filters_ret *ret)
{
    char *filters = NULL;
    int nfilters = 0;

    if ((nfilters = adminConnectGetLoggingFilters(&filters, args->flags)) < 0) {
        virNetMessageSaveError(rerr);
        return -1;
    }

    if (nfilters == 0) {
        ret->filters = NULL;
    } else {
        char **ret_filters = NULL;
        if (VIR_ALLOC(ret_filters) < 0)
            return -1;

        *ret_filters = filters;
        ret->filters = ret_filters;
    }
    ret->nfilters = nfilters;

    return 0;
}
527
#include "admin_server_dispatch_stubs.h"