remote.c 120.1 KB
Newer Older
1 2 3
/*
 * remote.c: code handling remote requests (from remote_internal.c)
 *
4
 * Copyright (C) 2007, 2008 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Richard W.M. Jones <rjones@redhat.com>
 */

#include <config.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <paths.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/poll.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <pwd.h>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
44
#include <fnmatch.h>
45

46 47 48 49 50
#ifdef HAVE_POLKIT
#include <polkit/polkit.h>
#include <polkit-dbus/polkit-dbus.h>
#endif

51 52
#include "libvirt_internal.h"
#include "datatypes.h"
53
#include "qemud.h"
54
#include "memory.h"
55

56 57
#define REMOTE_DEBUG(fmt,...) qemudDebug("REMOTE: " fmt, __VA_ARGS__)

58 59
static void remoteDispatchError (struct qemud_client *client,
                                 remote_message_header *req,
60 61
                                 const char *fmt, ...)
    ATTRIBUTE_FORMAT(printf, 3, 4);
62 63
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
64 65
static virStoragePoolPtr get_nonnull_storage_pool (virConnectPtr conn, remote_nonnull_storage_pool pool);
static virStorageVolPtr get_nonnull_storage_vol (virConnectPtr conn, remote_nonnull_storage_vol vol);
66 67
static void make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src);
static void make_nonnull_network (remote_nonnull_network *net_dst, virNetworkPtr net_src);
68 69
static void make_nonnull_storage_pool (remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src);
static void make_nonnull_storage_vol (remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src);
70 71 72

#include "remote_dispatch_prototypes.h"

73 74 75 76 77
typedef int (*dispatch_fn) (struct qemud_server *server,
                            struct qemud_client *client,
                            remote_message_header *req,
                            char *args,
                            char *ret);
78

79 80 81 82 83 84
/* Prototypes */
static void
remoteDispatchDomainEventSend (struct qemud_client *client,
                               virDomainPtr dom,
                               virDomainEventType event);

85 86 87 88 89
/* This function gets called from qemud when it detects an incoming
 * remote protocol message.  At this point, client->buffer contains
 * the full call message (including length word which we skip).
 */
void
90
remoteDispatchClientRequest (struct qemud_server *server,
91 92 93 94 95 96 97 98 99 100 101 102 103
                             struct qemud_client *client)
{
    XDR xdr;
    remote_message_header req, rep;
    dispatch_fn fn;
    xdrproc_t args_filter = (xdrproc_t) xdr_void;
    xdrproc_t ret_filter = (xdrproc_t) xdr_void;
    char *args = NULL, *ret = NULL;
    int rv, len;

#include "remote_dispatch_localvars.h"

    /* Parse the header. */
104
    xdrmem_create (&xdr, client->buffer, client->bufferLength, XDR_DECODE);
105 106

    if (!xdr_remote_message_header (&xdr, &req)) {
107
        remoteDispatchError (client, NULL, "%s", _("xdr_remote_message_header"));
108 109 110 111 112 113
        xdr_destroy (&xdr);
        return;
    }

    /* Check version, etc. */
    if (req.prog != REMOTE_PROGRAM) {
114 115
        remoteDispatchError (client, &req,
                             _("program mismatch (actual %x, expected %x)"),
116 117 118 119 120
                             req.prog, REMOTE_PROGRAM);
        xdr_destroy (&xdr);
        return;
    }
    if (req.vers != REMOTE_PROTOCOL_VERSION) {
121 122
        remoteDispatchError (client, &req,
                             _("version mismatch (actual %x, expected %x)"),
123 124 125 126 127
                             req.vers, REMOTE_PROTOCOL_VERSION);
        xdr_destroy (&xdr);
        return;
    }
    if (req.direction != REMOTE_CALL) {
128
        remoteDispatchError (client, &req, _("direction (%d) != REMOTE_CALL"),
129 130 131 132 133
                             (int) req.direction);
        xdr_destroy (&xdr);
        return;
    }
    if (req.status != REMOTE_OK) {
134
        remoteDispatchError (client, &req, _("status (%d) != REMOTE_OK"),
135 136 137 138 139
                             (int) req.status);
        xdr_destroy (&xdr);
        return;
    }

140 141 142 143 144 145 146
    /* If client is marked as needing auth, don't allow any RPC ops,
     * except for authentication ones
     */
    if (client->auth) {
        if (req.proc != REMOTE_PROC_AUTH_LIST &&
            req.proc != REMOTE_PROC_AUTH_SASL_INIT &&
            req.proc != REMOTE_PROC_AUTH_SASL_START &&
147 148
            req.proc != REMOTE_PROC_AUTH_SASL_STEP &&
            req.proc != REMOTE_PROC_AUTH_POLKIT
149
            ) {
150
            remoteDispatchError (client, &req, "%s", _("authentication required"));
151 152 153 154 155
            xdr_destroy (&xdr);
            return;
        }
    }

156 157 158 159 160 161 162
    /* Based on the procedure number, dispatch.  In future we may base
     * this on the version number as well.
     */
    switch (req.proc) {
#include "remote_dispatch_proc_switch.h"

    default:
163
        remoteDispatchError (client, &req, _("unknown procedure: %d"),
164 165 166 167 168 169 170
                             req.proc);
        xdr_destroy (&xdr);
        return;
    }

    /* Parse args. */
    if (!(*args_filter) (&xdr, args)) {
171
        remoteDispatchError (client, &req, "%s", _("parse args failed"));
172 173 174 175 176 177 178
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Call function. */
179
    rv = fn (server, client, &req, args, ret);
180 181 182 183 184 185
    xdr_free (args_filter, args);

    /* Dispatch function must return -2, -1 or 0.  Anything else is
     * an internal error.
     */
    if (rv < -2 || rv > 0) {
186 187
        remoteDispatchError (client, &req,
                             _("internal error - dispatch function returned invalid code %d"), rv);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        return;
    }

    /* Dispatch error?  If so then the function has already set up the
     * return buffer, so just return immediately.
     */
    if (rv == -2) return;

    /* Return header. */
    rep.prog = req.prog;
    rep.vers = req.vers;
    rep.proc = req.proc;
    rep.direction = REMOTE_REPLY;
    rep.serial = req.serial;
    rep.status = rv == 0 ? REMOTE_OK : REMOTE_ERROR;

    /* Serialise the return header. */
    xdrmem_create (&xdr, client->buffer, sizeof client->buffer, XDR_ENCODE);

    len = 0; /* We'll come back and write this later. */
    if (!xdr_int (&xdr, &len)) {
209
        remoteDispatchError (client, &req, "%s", _("dummy length"));
210 211 212 213 214 215
        xdr_destroy (&xdr);
        if (rv == 0) xdr_free (ret_filter, ret);
        return;
    }

    if (!xdr_remote_message_header (&xdr, &rep)) {
216
        remoteDispatchError (client, &req, "%s", _("serialise reply header"));
217 218 219 220 221 222 223 224
        xdr_destroy (&xdr);
        if (rv == 0) xdr_free (ret_filter, ret);
        return;
    }

    /* If OK, serialise return structure, if error serialise error. */
    if (rv == 0) {
        if (!(*ret_filter) (&xdr, ret)) {
225
            remoteDispatchError (client, &req, "%s", _("serialise return struct"));
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
            xdr_destroy (&xdr);
            return;
        }
        xdr_free (ret_filter, ret);
    } else /* error */ {
        virErrorPtr verr;
        remote_error error;
        remote_nonnull_domain dom;
        remote_nonnull_network net;

        verr = client->conn
            ? virConnGetLastError (client->conn)
            : virGetLastError ();

        if (verr) {
            error.code = verr->code;
            error.domain = verr->domain;
            error.message = verr->message ? &verr->message : NULL;
            error.level = verr->level;
            if (verr->dom) {
                dom.name = verr->dom->name;
                memcpy (dom.uuid, verr->dom->uuid, VIR_UUID_BUFLEN);
                dom.id = verr->dom->id;
            }
            error.dom = verr->dom ? &dom : NULL;
            error.str1 = verr->str1 ? &verr->str1 : NULL;
            error.str2 = verr->str2 ? &verr->str2 : NULL;
            error.str3 = verr->str3 ? &verr->str3 : NULL;
            error.int1 = verr->int1;
            error.int2 = verr->int2;
            if (verr->net) {
                net.name = verr->net->name;
                memcpy (net.uuid, verr->net->uuid, VIR_UUID_BUFLEN);
            }
            error.net = verr->net ? &net : NULL;
        } else {
            /* Error was NULL so synthesize an error. */
            char msgbuf[] = "remoteDispatchClientRequest: internal error: library function returned error but did not set virterror";
            char *msg = msgbuf;

            error.code = VIR_ERR_RPC;
            error.domain = VIR_FROM_REMOTE;
            error.message = &msg;
            error.level = VIR_ERR_ERROR;
            error.dom = NULL;
            error.str1 = &msg;
            error.str2 = NULL;
            error.str3 = NULL;
            error.int1 = 0;
            error.int2 = 0;
            error.net = NULL;
        }

        if (!xdr_remote_error (&xdr, &error)) {
280
            remoteDispatchError (client, &req, "%s", _("serialise return error"));
281 282 283 284 285 286 287 288
            xdr_destroy (&xdr);
            return;
        }
    }

    /* Write the length word. */
    len = xdr_getpos (&xdr);
    if (xdr_setpos (&xdr, 0) == 0) {
289
        remoteDispatchError (client, &req, "%s", _("xdr_setpos"));
290 291 292 293 294
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_int (&xdr, &len)) {
295
        remoteDispatchError (client, &req, "%s", _("serialise return length"));
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Set up the output buffer. */
    client->mode = QEMUD_MODE_TX_PACKET;
    client->bufferLength = len;
    client->bufferOffset = 0;
}

/* An error occurred during the dispatching process itself (ie. not
 * an error from the function being called).  We return an error
 * reply.
 */
static void
313 314 315
remoteDispatchSendError (struct qemud_client *client,
                         remote_message_header *req,
                         int code, const char *msg)
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
{
    remote_message_header rep;
    remote_error error;
    XDR xdr;
    int len;

    /* Future versions of the protocol may use different vers or prog.  Try
     * our hardest to send back a message that such clients could see.
     */
    if (req) {
        rep.prog = req->prog;
        rep.vers = req->vers;
        rep.proc = req->proc;
        rep.direction = REMOTE_REPLY;
        rep.serial = req->serial;
        rep.status = REMOTE_ERROR;
    } else {
        rep.prog = REMOTE_PROGRAM;
        rep.vers = REMOTE_PROTOCOL_VERSION;
        rep.proc = REMOTE_PROC_OPEN;
        rep.direction = REMOTE_REPLY;
        rep.serial = 1;
        rep.status = REMOTE_ERROR;
    }

    /* Construct the error. */
342
    error.code = code;
343
    error.domain = VIR_FROM_REMOTE;
344
    error.message = (char**)&msg;
345 346
    error.level = VIR_ERR_ERROR;
    error.dom = NULL;
347
    error.str1 = (char**)&msg;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    error.str2 = NULL;
    error.str3 = NULL;
    error.int1 = 0;
    error.int2 = 0;
    error.net = NULL;

    /* Serialise the return header and error. */
    xdrmem_create (&xdr, client->buffer, sizeof client->buffer, XDR_ENCODE);

    len = 0; /* We'll come back and write this later. */
    if (!xdr_int (&xdr, &len)) {
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_remote_message_header (&xdr, &rep)) {
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_remote_error (&xdr, &error)) {
        xdr_destroy (&xdr);
        return;
    }

    len = xdr_getpos (&xdr);
    if (xdr_setpos (&xdr, 0) == 0) {
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_int (&xdr, &len)) {
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Send it. */
    client->mode = QEMUD_MODE_TX_PACKET;
    client->bufferLength = len;
    client->bufferOffset = 0;
}

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
static void
remoteDispatchFailAuth (struct qemud_client *client,
                        remote_message_header *req)
{
    remoteDispatchSendError (client, req, VIR_ERR_AUTH_FAILED, "authentication failed");
}

static void
remoteDispatchError (struct qemud_client *client,
                     remote_message_header *req,
                     const char *fmt, ...)
{
    va_list args;
    char msgbuf[1024];
    char *msg = msgbuf;

    va_start (args, fmt);
    vsnprintf (msgbuf, sizeof msgbuf, fmt, args);
    va_end (args);

    remoteDispatchSendError (client, req, VIR_ERR_RPC, msg);
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428
int remoteRelayDomainEvent (virConnectPtr conn ATTRIBUTE_UNUSED,
                                   virDomainPtr dom,
                                   int event,
                                   void *opaque)
{
    struct qemud_client *client = opaque;
    REMOTE_DEBUG("Relaying domain event %d", event);

    if(client) {
        remoteDispatchDomainEventSend (client, dom, event);
        qemudDispatchClientWrite(client->server,client);
    }
    return 0;
}
429 430


431 432 433
/*----- Functions. -----*/

static int
434 435
remoteDispatchOpen (struct qemud_server *server ATTRIBUTE_UNUSED,
                    struct qemud_client *client, remote_message_header *req,
436 437 438 439 440 441 442
                    struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
{
    const char *name;
    int flags;

    /* Already opened? */
    if (client->conn) {
443
        remoteDispatchError (client, req, "%s", _("connection already open"));
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        return -2;
    }

    name = args->name ? *args->name : NULL;

    /* If this connection arrived on a readonly socket, force
     * the connection to be readonly.
     */
    flags = args->flags;
    if (client->readonly) flags |= VIR_CONNECT_RO;

    client->conn =
        flags & VIR_CONNECT_RO
        ? virConnectOpenReadOnly (name)
        : virConnectOpen (name);

    return client->conn ? 0 : -1;
}

#define CHECK_CONN(client)                      \
    if (!client->conn) {                        \
465
        remoteDispatchError (client, req, "%s", _("connection not open"));   \
466 467 468 469
        return -2;                                                  \
    }

static int
470 471
remoteDispatchClose (struct qemud_server *server ATTRIBUTE_UNUSED,
                     struct qemud_client *client, remote_message_header *req,
472 473 474 475 476 477 478 479 480 481 482
                     void *args ATTRIBUTE_UNUSED, void *ret ATTRIBUTE_UNUSED)
{
    int rv;
    CHECK_CONN(client);

    rv = virConnectClose (client->conn);
    if (rv == 0) client->conn = NULL;

    return rv;
}

483
static int
484 485
remoteDispatchSupportsFeature (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client, remote_message_header *req,
486 487 488 489 490 491 492 493 494 495
                               remote_supports_feature_args *args, remote_supports_feature_ret *ret)
{
    CHECK_CONN(client);

    ret->supported = __virDrvSupportsFeature (client->conn, args->feature);
    if (ret->supported == -1) return -1;

    return 0;
}

496
static int
497 498
remoteDispatchGetType (struct qemud_server *server ATTRIBUTE_UNUSED,
                       struct qemud_client *client, remote_message_header *req,
499 500 501 502 503 504 505 506 507 508 509 510 511
                       void *args ATTRIBUTE_UNUSED, remote_get_type_ret *ret)
{
    const char *type;
    CHECK_CONN(client);

    type = virConnectGetType (client->conn);
    if (type == NULL) return -1;

    /* We have to strdup because remoteDispatchClientRequest will
     * free this string after it's been serialised.
     */
    ret->type = strdup (type);
    if (!ret->type) {
512
        remoteDispatchError (client, req, "%s", _("out of memory in strdup"));
513 514 515 516 517 518 519
        return -2;
    }

    return 0;
}

static int
520 521
remoteDispatchGetVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client,
522 523 524 525 526 527 528 529 530 531 532 533 534 535
                          remote_message_header *req,
                          void *args ATTRIBUTE_UNUSED,
                          remote_get_version_ret *ret)
{
    unsigned long hvVer;
    CHECK_CONN(client);

    if (virConnectGetVersion (client->conn, &hvVer) == -1)
        return -1;

    ret->hv_ver = hvVer;
    return 0;
}

536
static int
537 538
remoteDispatchGetHostname (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client,
539 540 541 542 543 544 545 546 547 548 549 550 551 552
                           remote_message_header *req,
                           void *args ATTRIBUTE_UNUSED,
                           remote_get_hostname_ret *ret)
{
    char *hostname;
    CHECK_CONN(client);

    hostname = virConnectGetHostname (client->conn);
    if (hostname == NULL) return -1;

    ret->hostname = hostname;
    return 0;
}

553
static int
554 555
remoteDispatchGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client,
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
                           remote_message_header *req,
                           remote_get_max_vcpus_args *args,
                           remote_get_max_vcpus_ret *ret)
{
    char *type;
    CHECK_CONN(client);

    type = args->type ? *args->type : NULL;
    ret->max_vcpus = virConnectGetMaxVcpus (client->conn, type);
    if (ret->max_vcpus == -1) return -1;

    return 0;
}

static int
571 572
remoteDispatchNodeGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client,
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
                           remote_message_header *req,
                           void *args ATTRIBUTE_UNUSED,
                           remote_node_get_info_ret *ret)
{
    virNodeInfo info;
    CHECK_CONN(client);

    if (virNodeGetInfo (client->conn, &info) == -1)
        return -1;

    memcpy (ret->model, info.model, sizeof ret->model);
    ret->memory = info.memory;
    ret->cpus = info.cpus;
    ret->mhz = info.mhz;
    ret->nodes = info.nodes;
    ret->sockets = info.sockets;
    ret->cores = info.cores;
    ret->threads = info.threads;

    return 0;
}

static int
596 597
remoteDispatchGetCapabilities (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
598 599 600 601 602 603 604 605 606 607 608 609 610 611
                               remote_message_header *req,
                               void *args ATTRIBUTE_UNUSED,
                               remote_get_capabilities_ret *ret)
{
    char *caps;
    CHECK_CONN(client);

    caps = virConnectGetCapabilities (client->conn);
    if (caps == NULL) return -1;

    ret->capabilities = caps;
    return 0;
}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
static int
remoteDispatchNodeGetCellsFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client,
                                      remote_message_header *req,
                                      remote_node_get_cells_free_memory_args *args,
                                      remote_node_get_cells_free_memory_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxCells > REMOTE_NODE_MAX_CELLS) {
        remoteDispatchError (client, req,
                             "%s", _("maxCells > REMOTE_NODE_MAX_CELLS"));
        return -2;
    }

    /* Allocate return buffer. */
628 629 630 631
    if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
632 633 634 635 636

    ret->freeMems.freeMems_len = virNodeGetCellsFreeMemory(client->conn,
                                                           (unsigned long long *)ret->freeMems.freeMems_val,
                                                           args->startCell,
                                                           args->maxCells);
637 638
    if (ret->freeMems.freeMems_len == 0) {
        VIR_FREE(ret->freeMems.freeMems_val);
639
        return -1;
640
    }
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

    return 0;
}


static int
remoteDispatchNodeGetFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
                                 remote_message_header *req,
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_node_get_free_memory_ret *ret)
{
    unsigned long long freeMem;
    CHECK_CONN(client);

    freeMem = virNodeGetFreeMemory(client->conn);
    if (freeMem == 0) return -1;

    ret->freeMem = freeMem;
    return 0;
}


664
static int
665 666
remoteDispatchDomainGetSchedulerType (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client,
667 668 669 670 671 672 673 674 675 676 677
                                      remote_message_header *req,
                                      remote_domain_get_scheduler_type_args *args,
                                      remote_domain_get_scheduler_type_ret *ret)
{
    virDomainPtr dom;
    char *type;
    int nparams;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
678
        remoteDispatchError (client, req, "%s", _("domain not found"));
679 680 681 682
        return -2;
    }

    type = virDomainGetSchedulerType (dom, &nparams);
683 684 685 686
    if (type == NULL) {
        virDomainFree(dom);
        return -1;
    }
687 688 689

    ret->type = type;
    ret->nparams = nparams;
690
    virDomainFree(dom);
691 692 693 694
    return 0;
}

static int
695 696
remoteDispatchDomainGetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
                                            struct qemud_client *client,
697 698 699 700 701 702 703 704 705 706 707 708
                                            remote_message_header *req,
                                            remote_domain_get_scheduler_parameters_args *args,
                                            remote_domain_get_scheduler_parameters_ret *ret)
{
    virDomainPtr dom;
    virSchedParameterPtr params;
    int i, r, nparams;
    CHECK_CONN(client);

    nparams = args->nparams;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
709
        remoteDispatchError (client, req, "%s", _("nparams too large"));
710 711
        return -2;
    }
712 713
    if (VIR_ALLOC_N(params, nparams) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
714 715 716 717 718
        return -2;
    }

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
719
        VIR_FREE(params);
720
        remoteDispatchError (client, req, "%s", _("domain not found"));
721 722 723 724 725
        return -2;
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
726
        virDomainFree(dom);
727
        VIR_FREE(params);
728 729 730 731 732
        return -1;
    }

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
733 734
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto oom;
735 736 737 738

    for (i = 0; i < nparams; ++i) {
        // remoteDispatchClientRequest will free this:
        ret->params.params_val[i].field = strdup (params[i].field);
739 740 741
        if (ret->params.params_val[i].field == NULL)
            goto oom;

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
        ret->params.params_val[i].value.type = params[i].type;
        switch (params[i].type) {
        case VIR_DOMAIN_SCHED_FIELD_INT:
            ret->params.params_val[i].value.remote_sched_param_value_u.i = params[i].value.i; break;
        case VIR_DOMAIN_SCHED_FIELD_UINT:
            ret->params.params_val[i].value.remote_sched_param_value_u.ui = params[i].value.ui; break;
        case VIR_DOMAIN_SCHED_FIELD_LLONG:
            ret->params.params_val[i].value.remote_sched_param_value_u.l = params[i].value.l; break;
        case VIR_DOMAIN_SCHED_FIELD_ULLONG:
            ret->params.params_val[i].value.remote_sched_param_value_u.ul = params[i].value.ul; break;
        case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
            ret->params.params_val[i].value.remote_sched_param_value_u.d = params[i].value.d; break;
        case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
            ret->params.params_val[i].value.remote_sched_param_value_u.b = params[i].value.b; break;
        default:
757
            remoteDispatchError (client, req, "%s", _("unknown type"));
758
            goto cleanup;
759 760
        }
    }
761
    virDomainFree(dom);
762
    VIR_FREE(params);
763 764

    return 0;
765 766 767 768 769 770 771 772 773

oom:
    remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
cleanup:
    virDomainFree(dom);
    for (i = 0 ; i < nparams ; i++)
        VIR_FREE(ret->params.params_val[i].field);
    VIR_FREE(params);
    return -2;
774 775 776
}

static int
777 778
remoteDispatchDomainSetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
                                            struct qemud_client *client,
779 780 781 782 783 784 785 786 787 788 789 790
                                            remote_message_header *req,
                                            remote_domain_set_scheduler_parameters_args *args,
                                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int i, r, nparams;
    virSchedParameterPtr params;
    CHECK_CONN(client);

    nparams = args->params.params_len;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
791
        remoteDispatchError (client, req, "%s", _("nparams too large"));
792 793
        return -2;
    }
794 795
    if (VIR_ALLOC_N(params, nparams)) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
        return -2;
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
        strncpy (params[i].field, args->params.params_val[i].field,
                 VIR_DOMAIN_SCHED_FIELD_LENGTH);
        params[i].field[VIR_DOMAIN_SCHED_FIELD_LENGTH-1] = '\0';
        params[i].type = args->params.params_val[i].value.type;
        switch (params[i].type) {
        case VIR_DOMAIN_SCHED_FIELD_INT:
            params[i].value.i = args->params.params_val[i].value.remote_sched_param_value_u.i; break;
        case VIR_DOMAIN_SCHED_FIELD_UINT:
            params[i].value.ui = args->params.params_val[i].value.remote_sched_param_value_u.ui; break;
        case VIR_DOMAIN_SCHED_FIELD_LLONG:
            params[i].value.l = args->params.params_val[i].value.remote_sched_param_value_u.l; break;
        case VIR_DOMAIN_SCHED_FIELD_ULLONG:
            params[i].value.ul = args->params.params_val[i].value.remote_sched_param_value_u.ul; break;
        case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
            params[i].value.d = args->params.params_val[i].value.remote_sched_param_value_u.d; break;
        case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
            params[i].value.b = args->params.params_val[i].value.remote_sched_param_value_u.b; break;
        }
    }

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
823
        VIR_FREE(params);
824
        remoteDispatchError (client, req, "%s", _("domain not found"));
825 826 827 828
        return -2;
    }

    r = virDomainSetSchedulerParameters (dom, params, nparams);
829
    virDomainFree(dom);
830
    VIR_FREE(params);
831 832 833 834 835
    if (r == -1) return -1;

    return 0;
}

836
static int
837 838
remoteDispatchDomainBlockStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
839 840 841 842 843 844 845 846 847 848 849
                                remote_message_header *req,
                                remote_domain_block_stats_args *args,
                                remote_domain_block_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainBlockStats stats;
    CHECK_CONN (client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
850
        remoteDispatchError (client, req, "%s", _("domain not found"));
851 852 853 854
        return -2;
    }
    path = args->path;

D
Daniel P. Berrange 已提交
855 856
    if (virDomainBlockStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
857
        return -1;
D
Daniel P. Berrange 已提交
858 859
    }
    virDomainFree (dom);
860 861 862 863 864 865 866 867 868 869 870

    ret->rd_req = stats.rd_req;
    ret->rd_bytes = stats.rd_bytes;
    ret->wr_req = stats.wr_req;
    ret->wr_bytes = stats.wr_bytes;
    ret->errs = stats.errs;

    return 0;
}

static int
871 872
remoteDispatchDomainInterfaceStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
873 874 875 876 877 878 879 880 881 882 883
                                    remote_message_header *req,
                                    remote_domain_interface_stats_args *args,
                                    remote_domain_interface_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainInterfaceStats stats;
    CHECK_CONN (client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
884
        remoteDispatchError (client, req, "%s", _("domain not found"));
885 886 887 888
        return -2;
    }
    path = args->path;

D
Daniel P. Berrange 已提交
889 890
    if (virDomainInterfaceStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
891
        return -1;
D
Daniel P. Berrange 已提交
892 893
    }
    virDomainFree (dom);
894 895 896 897 898 899 900 901 902 903 904 905 906

    ret->rx_bytes = stats.rx_bytes;
    ret->rx_packets = stats.rx_packets;
    ret->rx_errs = stats.rx_errs;
    ret->rx_drop = stats.rx_drop;
    ret->tx_bytes = stats.tx_bytes;
    ret->tx_packets = stats.tx_packets;
    ret->tx_errs = stats.tx_errs;
    ret->tx_drop = stats.tx_drop;

    return 0;
}

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
static int
remoteDispatchDomainBlockPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
                               remote_message_header *req,
                               remote_domain_block_peek_args *args,
                               remote_domain_block_peek_ret *ret)
{
    virDomainPtr dom;
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
    CHECK_CONN (client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "%s", _("domain not found"));
        return -2;
    }
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
932
        virDomainFree (dom);
933 934 935 936 937 938
        remoteDispatchError (client, req,
                             "%s", _("size > maximum buffer size"));
        return -2;
    }

    ret->buffer.buffer_len = size;
939 940
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
        virDomainFree (dom);
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
        remoteDispatchError (client, req, "%s", strerror (errno));
        return -2;
    }

    if (virDomainBlockPeek (dom, path, offset, size,
                            ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

R
Richard W.M. Jones 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
static int
remoteDispatchDomainMemoryPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
                                remote_message_header *req,
                                remote_domain_memory_peek_args *args,
                                remote_domain_memory_peek_ret *ret)
{
    virDomainPtr dom;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
    CHECK_CONN (client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "%s", _("domain not found"));
        return -2;
    }
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
        remoteDispatchError (client, req,
                             "%s", _("size > maximum buffer size"));
        virDomainFree (dom);
        return -2;
    }

    ret->buffer.buffer_len = size;
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
        remoteDispatchError (client, req, "%s", strerror (errno));
        virDomainFree (dom);
        return -2;
    }

    if (virDomainMemoryPeek (dom, offset, size,
                             ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

1003
static int
1004 1005
remoteDispatchDomainAttachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1006 1007 1008 1009 1010 1011 1012 1013 1014
                                  remote_message_header *req,
                                  remote_domain_attach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1015
        remoteDispatchError (client, req, "%s", _("domain not found"));
1016 1017 1018
        return -2;
    }

1019 1020
    if (virDomainAttachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
1021
        return -1;
1022 1023
    }
    virDomainFree(dom);
1024 1025 1026 1027
    return 0;
}

static int
1028 1029
remoteDispatchDomainCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
1030 1031 1032 1033 1034 1035 1036 1037 1038
                            remote_message_header *req,
                            remote_domain_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1039
        remoteDispatchError (client, req, "%s", _("domain not found"));
1040 1041 1042
        return -2;
    }

1043 1044
    if (virDomainCreate (dom) == -1) {
        virDomainFree(dom);
1045
        return -1;
1046 1047
    }
    virDomainFree(dom);
1048 1049 1050 1051
    return 0;
}

static int
1052
remoteDispatchDomainCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1053
                                 struct qemud_client *client,
1054
                                 remote_message_header *req,
1055 1056
                                 remote_domain_create_xml_args *args,
                                 remote_domain_create_xml_ret *ret)
1057 1058 1059 1060
{
    virDomainPtr dom;
    CHECK_CONN(client);

1061
    dom = virDomainCreateXML (client->conn, args->xml_desc, args->flags);
1062 1063 1064
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
1065
    virDomainFree(dom);
1066 1067 1068 1069 1070

    return 0;
}

static int
1071 1072
remoteDispatchDomainDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
                               remote_message_header *req,
                               remote_domain_define_xml_args *args,
                               remote_domain_define_xml_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainDefineXML (client->conn, args->xml);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
1084
    virDomainFree(dom);
1085 1086 1087 1088 1089

    return 0;
}

static int
1090 1091
remoteDispatchDomainDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1092 1093 1094 1095 1096 1097 1098 1099 1100
                             remote_message_header *req,
                             remote_domain_destroy_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1101
        remoteDispatchError (client, req, "%s", _("domain not found"));
1102 1103 1104
        return -2;
    }

1105 1106
    if (virDomainDestroy (dom) == -1) {
        virDomainFree(dom);
1107
        return -1;
1108 1109
    }
    virDomainFree(dom);
1110 1111 1112 1113
    return 0;
}

static int
1114 1115
remoteDispatchDomainDetachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1116 1117 1118 1119 1120 1121 1122 1123 1124
                                  remote_message_header *req,
                                  remote_domain_detach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1125
        remoteDispatchError (client, req, "%s", _("domain not found"));
1126 1127 1128
        return -2;
    }

1129 1130
    if (virDomainDetachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
1131
        return -1;
1132
    }
1133

1134
    virDomainFree(dom);
1135 1136 1137 1138
    return 0;
}

static int
1139 1140
remoteDispatchDomainDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1141 1142 1143 1144 1145 1146 1147 1148 1149
                             remote_message_header *req,
                             remote_domain_dump_xml_args *args,
                             remote_domain_dump_xml_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1150
        remoteDispatchError (client, req, "%s", _("domain not found"));
1151 1152 1153 1154 1155
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
1156 1157 1158 1159 1160
    if (!ret->xml) {
            virDomainFree(dom);
            return -1;
    }
    virDomainFree(dom);
1161 1162 1163 1164
    return 0;
}

static int
1165 1166
remoteDispatchDomainGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1167 1168 1169 1170 1171 1172 1173 1174 1175
                                  remote_message_header *req,
                                  remote_domain_get_autostart_args *args,
                                  remote_domain_get_autostart_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1176
        remoteDispatchError (client, req, "%s", _("domain not found"));
1177 1178 1179
        return -2;
    }

1180 1181
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
        virDomainFree(dom);
1182
        return -1;
1183 1184
    }
    virDomainFree(dom);
1185 1186 1187 1188
    return 0;
}

static int
1189 1190
remoteDispatchDomainGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
                             remote_message_header *req,
                             remote_domain_get_info_args *args,
                             remote_domain_get_info_ret *ret)
{
    virDomainPtr dom;
    virDomainInfo info;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1201
        remoteDispatchError (client, req, "%s", _("domain not found"));
1202 1203 1204
        return -2;
    }

1205 1206
    if (virDomainGetInfo (dom, &info) == -1) {
        virDomainFree(dom);
1207
        return -1;
1208
    }
1209 1210 1211 1212 1213 1214 1215

    ret->state = info.state;
    ret->max_mem = info.maxMem;
    ret->memory = info.memory;
    ret->nr_virt_cpu = info.nrVirtCpu;
    ret->cpu_time = info.cpuTime;

1216 1217
    virDomainFree(dom);

1218 1219 1220 1221
    return 0;
}

static int
1222 1223
remoteDispatchDomainGetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1224 1225 1226 1227 1228 1229 1230 1231 1232
                                  remote_message_header *req,
                                  remote_domain_get_max_memory_args *args,
                                  remote_domain_get_max_memory_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1233
        remoteDispatchError (client, req, "%s", _("domain not found"));
1234 1235 1236 1237
        return -2;
    }

    ret->memory = virDomainGetMaxMemory (dom);
1238 1239 1240 1241 1242
    if (ret->memory == 0) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
1243 1244 1245 1246
    return 0;
}

static int
1247 1248
remoteDispatchDomainGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
1249 1250 1251 1252 1253 1254 1255 1256 1257
                                 remote_message_header *req,
                                 remote_domain_get_max_vcpus_args *args,
                                 remote_domain_get_max_vcpus_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1258
        remoteDispatchError (client, req, "%s", _("domain not found"));
1259 1260 1261 1262
        return -2;
    }

    ret->num = virDomainGetMaxVcpus (dom);
1263 1264 1265 1266 1267
    if (ret->num == -1) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
1268 1269 1270 1271
    return 0;
}

static int
1272 1273
remoteDispatchDomainGetOsType (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
1274 1275 1276 1277 1278 1279 1280 1281 1282
                               remote_message_header *req,
                               remote_domain_get_os_type_args *args,
                               remote_domain_get_os_type_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1283
        remoteDispatchError (client, req, "%s", _("domain not found"));
1284 1285 1286 1287 1288
        return -2;
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
1289 1290 1291 1292 1293
    if (ret->type == NULL) {
            virDomainFree(dom);
            return -1;
    }
    virDomainFree(dom);
1294 1295 1296 1297
    return 0;
}

static int
1298 1299
remoteDispatchDomainGetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1300 1301 1302 1303
                              remote_message_header *req,
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
1304 1305 1306
    virDomainPtr dom = NULL;
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1307 1308 1309 1310 1311
    int info_len, i;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1312
        remoteDispatchError (client, req, "%s", _("domain not found"));
1313 1314 1315 1316
        return -2;
    }

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1317
        virDomainFree(dom);
1318
        remoteDispatchError (client, req, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1319 1320 1321
        return -2;
    }

1322
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1323
        virDomainFree(dom);
1324
        remoteDispatchError (client, req, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1325 1326 1327 1328
        return -2;
    }

    /* Allocate buffers to take the results. */
1329 1330 1331 1332
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto oom;
    if (VIR_ALLOC_N(cpumaps, args->maxinfo) < 0)
        goto oom;
1333 1334 1335 1336

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1337
    if (info_len == -1) {
1338 1339
        VIR_FREE(info);
        VIR_FREE(cpumaps);
1340 1341 1342
        virDomainFree(dom);
        return -1;
    }
1343 1344 1345

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
1346 1347
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto oom;
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

    for (i = 0; i < info_len; ++i) {
        ret->info.info_val[i].number = info[i].number;
        ret->info.info_val[i].state = info[i].state;
        ret->info.info_val[i].cpu_time = info[i].cpuTime;
        ret->info.info_val[i].cpu = info[i].cpu;
    }

    /* Don't need to allocate/copy the cpumaps if we make the reasonable
     * assumption that unsigned char and char are the same size.
     * Note that remoteDispatchClientRequest will free.
     */
1360
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1361 1362
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1363
    VIR_FREE(info);
1364
    virDomainFree(dom);
1365
    return 0;
1366 1367 1368 1369 1370 1371 1372

oom:
    VIR_FREE(info);
    VIR_FREE(cpumaps);
    virDomainFree(dom);
    remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
    return -2;
1373 1374
}

1375
static int
1376 1377
remoteDispatchDomainMigratePrepare (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
                                    remote_message_header *req,
                                    remote_domain_migrate_prepare_args *args,
                                    remote_domain_migrate_prepare_ret *ret)
{
    int r;
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
    CHECK_CONN (client);

    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
1394 1395 1396 1397
    if (VIR_ALLOC(uri_out) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
1398 1399 1400 1401

    r = __virDomainMigratePrepare (client->conn, &cookie, &cookielen,
                                   uri_in, uri_out,
                                   args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1402
    if (r == -1) {
1403
        VIR_FREE(uri_out);
D
Daniel P. Berrange 已提交
1404 1405
        return -1;
    }
1406 1407 1408 1409 1410 1411

    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie.cookie_len = cookielen;
    ret->cookie.cookie_val = cookie;
D
Daniel P. Berrange 已提交
1412 1413
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
1414
        VIR_FREE(uri_out);
D
Daniel P. Berrange 已提交
1415 1416 1417
    } else {
        ret->uri_out = uri_out;
    }
1418 1419 1420 1421 1422

    return 0;
}

static int
1423 1424
remoteDispatchDomainMigratePerform (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
                                    remote_message_header *req,
                                    remote_domain_migrate_perform_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
    int r;
    virDomainPtr dom;
    char *dname;
    CHECK_CONN (client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1436
        remoteDispatchError (client, req, "%s", _("domain not found"));
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
        return -2;
    }

    dname = args->dname == NULL ? NULL : *args->dname;

    r = __virDomainMigratePerform (dom,
                                   args->cookie.cookie_val,
                                   args->cookie.cookie_len,
                                   args->uri,
                                   args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1447
    virDomainFree (dom);
1448 1449 1450 1451 1452 1453
    if (r == -1) return -1;

    return 0;
}

static int
1454 1455
remoteDispatchDomainMigrateFinish (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
                                   remote_message_header *req,
                                   remote_domain_migrate_finish_args *args,
                                   remote_domain_migrate_finish_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

    ddom = __virDomainMigrateFinish (client->conn, args->dname,
                                     args->cookie.cookie_val,
                                     args->cookie.cookie_len,
                                     args->uri,
                                     args->flags);
    if (ddom == NULL) return -1;

    make_nonnull_domain (&ret->ddom, ddom);
D
Daniel P. Berrange 已提交
1471
    virDomainFree (ddom);
1472 1473 1474
    return 0;
}

1475
static int
1476 1477
remoteDispatchListDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1478 1479 1480 1481 1482 1483 1484 1485
                                  remote_message_header *req,
                                  remote_list_defined_domains_args *args,
                                  remote_list_defined_domains_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
1486
                             "%s", _("maxnames > REMOTE_DOMAIN_NAME_LIST_MAX"));
1487 1488 1489 1490
        return -2;
    }

    /* Allocate return buffer. */
1491 1492 1493 1494
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
1495 1496 1497 1498

    ret->names.names_len =
        virConnectListDefinedDomains (client->conn,
                                      ret->names.names_val, args->maxnames);
1499 1500 1501 1502
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
        return -1;
    }
1503 1504 1505 1506 1507

    return 0;
}

static int
1508 1509
remoteDispatchDomainLookupById (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
                                remote_message_header *req,
                                remote_domain_lookup_by_id_args *args,
                                remote_domain_lookup_by_id_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainLookupByID (client->conn, args->id);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
1521
    virDomainFree(dom);
1522 1523 1524 1525
    return 0;
}

static int
1526 1527
remoteDispatchDomainLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
                                  remote_message_header *req,
                                  remote_domain_lookup_by_name_args *args,
                                  remote_domain_lookup_by_name_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainLookupByName (client->conn, args->name);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
1539
    virDomainFree(dom);
1540 1541 1542 1543
    return 0;
}

static int
1544 1545
remoteDispatchDomainLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
                                  remote_message_header *req,
                                  remote_domain_lookup_by_uuid_args *args,
                                  remote_domain_lookup_by_uuid_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainLookupByUUID (client->conn, (unsigned char *) args->uuid);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
1557
    virDomainFree(dom);
1558 1559 1560 1561
    return 0;
}

static int
1562 1563
remoteDispatchNumOfDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
                                   remote_message_header *req,
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_num_of_defined_domains_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfDefinedDomains (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

static int
1577 1578
remoteDispatchDomainPinVcpu (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
                             remote_message_header *req,
                             remote_domain_pin_vcpu_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int rv;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1589
        remoteDispatchError (client, req, "%s", _("domain not found"));
1590 1591 1592 1593
        return -2;
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
1594
        virDomainFree(dom);
1595
        remoteDispatchError (client, req, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
1596 1597 1598 1599 1600 1601
        return -2;
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
1602 1603 1604 1605 1606
    if (rv == -1) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
1607 1608 1609 1610
    return 0;
}

static int
1611 1612
remoteDispatchDomainReboot (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
1613 1614 1615 1616 1617 1618 1619 1620 1621
                            remote_message_header *req,
                            remote_domain_reboot_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1622
        remoteDispatchError (client, req, "%s", _("domain not found"));
1623 1624 1625
        return -2;
    }

1626 1627
    if (virDomainReboot (dom, args->flags) == -1) {
        virDomainFree(dom);
1628
        return -1;
1629 1630
    }
    virDomainFree(dom);
1631 1632 1633 1634
    return 0;
}

static int
1635 1636
remoteDispatchDomainRestore (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
                             remote_message_header *req,
                             remote_domain_restore_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

    if (virDomainRestore (client->conn, args->from) == -1)
        return -1;

    return 0;
}

static int
1650 1651
remoteDispatchDomainResume (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
1652 1653 1654 1655 1656 1657 1658 1659 1660
                            remote_message_header *req,
                            remote_domain_resume_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1661
        remoteDispatchError (client, req, "%s", _("domain not found"));
1662 1663 1664
        return -2;
    }

1665 1666
    if (virDomainResume (dom) == -1) {
        virDomainFree(dom);
1667
        return -1;
1668 1669
    }
    virDomainFree(dom);
1670 1671 1672 1673
    return 0;
}

static int
1674 1675
remoteDispatchDomainSave (struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client,
1676 1677 1678 1679 1680 1681 1682 1683 1684
                          remote_message_header *req,
                          remote_domain_save_args *args,
                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1685
        remoteDispatchError (client, req, "%s", _("domain not found"));
1686 1687 1688
        return -2;
    }

1689 1690
    if (virDomainSave (dom, args->to) == -1) {
        virDomainFree(dom);
1691
        return -1;
1692 1693
    }
    virDomainFree(dom);
1694 1695 1696 1697
    return 0;
}

static int
1698 1699
remoteDispatchDomainCoreDump (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1700 1701 1702 1703 1704 1705 1706 1707 1708
                              remote_message_header *req,
                              remote_domain_core_dump_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1709
        remoteDispatchError (client, req, "%s", _("domain not found"));
1710 1711 1712
        return -2;
    }

1713 1714
    if (virDomainCoreDump (dom, args->to, args->flags) == -1) {
        virDomainFree(dom);
1715
        return -1;
1716 1717
    }
    virDomainFree(dom);
1718 1719 1720 1721
    return 0;
}

static int
1722 1723
remoteDispatchDomainSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1724 1725 1726 1727 1728 1729 1730 1731 1732
                                  remote_message_header *req,
                                  remote_domain_set_autostart_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1733
        remoteDispatchError (client, req, "%s", _("domain not found"));
1734 1735 1736
        return -2;
    }

1737 1738
    if (virDomainSetAutostart (dom, args->autostart) == -1) {
        virDomainFree(dom);
1739
        return -1;
1740 1741
    }
    virDomainFree(dom);
1742 1743 1744 1745
    return 0;
}

static int
1746 1747
remoteDispatchDomainSetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1748 1749 1750 1751 1752 1753 1754 1755 1756
                                  remote_message_header *req,
                                  remote_domain_set_max_memory_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1757
        remoteDispatchError (client, req, "%s", _("domain not found"));
1758 1759 1760
        return -2;
    }

1761 1762
    if (virDomainSetMaxMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1763
        return -1;
1764 1765
    }
    virDomainFree(dom);
1766 1767 1768 1769
    return 0;
}

static int
1770 1771
remoteDispatchDomainSetMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
1772 1773 1774 1775 1776 1777 1778 1779 1780
                               remote_message_header *req,
                               remote_domain_set_memory_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1781
        remoteDispatchError (client, req, "%s", _("domain not found"));
1782 1783 1784
        return -2;
    }

1785 1786
    if (virDomainSetMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1787
        return -1;
1788 1789
    }
    virDomainFree(dom);
1790 1791 1792 1793
    return 0;
}

static int
1794 1795
remoteDispatchDomainSetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1796 1797 1798 1799 1800 1801 1802 1803 1804
                              remote_message_header *req,
                              remote_domain_set_vcpus_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1805
        remoteDispatchError (client, req, "%s", _("domain not found"));
1806 1807 1808
        return -2;
    }

1809 1810
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
        virDomainFree(dom);
1811
        return -1;
1812 1813
    }
    virDomainFree(dom);
1814 1815 1816 1817
    return 0;
}

static int
1818 1819
remoteDispatchDomainShutdown (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1820 1821 1822 1823 1824 1825 1826 1827 1828
                              remote_message_header *req,
                              remote_domain_shutdown_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1829
        remoteDispatchError (client, req, "%s", _("domain not found"));
1830 1831 1832
        return -2;
    }

1833 1834
    if (virDomainShutdown (dom) == -1) {
        virDomainFree(dom);
1835
        return -1;
1836 1837
    }
    virDomainFree(dom);
1838 1839 1840 1841
    return 0;
}

static int
1842 1843
remoteDispatchDomainSuspend (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1844 1845 1846 1847 1848 1849 1850 1851 1852
                             remote_message_header *req,
                             remote_domain_suspend_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1853
        remoteDispatchError (client, req, "%s", _("domain not found"));
1854 1855 1856
        return -2;
    }

1857 1858
    if (virDomainSuspend (dom) == -1) {
        virDomainFree(dom);
1859
        return -1;
1860 1861
    }
    virDomainFree(dom);
1862 1863 1864 1865
    return 0;
}

static int
1866 1867
remoteDispatchDomainUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1868 1869 1870 1871 1872 1873 1874 1875 1876
                              remote_message_header *req,
                              remote_domain_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
1877
        remoteDispatchError (client, req, "%s", _("domain not found"));
1878 1879 1880
        return -2;
    }

1881 1882
    if (virDomainUndefine (dom) == -1) {
        virDomainFree(dom);
1883
        return -1;
1884 1885
    }
    virDomainFree(dom);
1886 1887 1888 1889
    return 0;
}

static int
1890 1891
remoteDispatchListDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1892 1893 1894 1895 1896 1897 1898 1899
                                   remote_message_header *req,
                                   remote_list_defined_networks_args *args,
                                   remote_list_defined_networks_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
1900
                             "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
1901 1902 1903 1904
        return -2;
    }

    /* Allocate return buffer. */
1905 1906 1907 1908
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
1909 1910 1911 1912

    ret->names.names_len =
        virConnectListDefinedNetworks (client->conn,
                                       ret->names.names_val, args->maxnames);
1913 1914 1915 1916
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
        return -1;
    }
1917 1918 1919 1920 1921

    return 0;
}

static int
1922 1923
remoteDispatchListDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client,
1924 1925 1926 1927 1928 1929 1930 1931
                           remote_message_header *req,
                           remote_list_domains_args *args,
                           remote_list_domains_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
        remoteDispatchError (client, req,
1932
                             "%s", _("maxids > REMOTE_DOMAIN_ID_LIST_MAX"));
1933 1934 1935 1936
        return -2;
    }

    /* Allocate return buffer. */
1937 1938 1939 1940
    if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
1941 1942 1943

    ret->ids.ids_len = virConnectListDomains (client->conn,
                                              ret->ids.ids_val, args->maxids);
1944 1945 1946 1947
    if (ret->ids.ids_len == -1) {
        VIR_FREE(ret->ids.ids_val);
        return -1;
    }
1948 1949 1950 1951 1952

    return 0;
}

static int
1953 1954
remoteDispatchListNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
1955 1956 1957 1958 1959 1960 1961 1962
                            remote_message_header *req,
                            remote_list_networks_args *args,
                            remote_list_networks_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
1963
                             "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
1964 1965 1966 1967
        return -2;
    }

    /* Allocate return buffer. */
1968 1969 1970 1971
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
1972 1973 1974 1975

    ret->names.names_len =
        virConnectListNetworks (client->conn,
                                ret->names.names_val, args->maxnames);
1976 1977 1978 1979
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
        return -1;
    }
1980 1981 1982 1983 1984

    return 0;
}

static int
1985 1986
remoteDispatchNetworkCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1987 1988 1989 1990 1991 1992 1993 1994 1995
                             remote_message_header *req,
                             remote_network_create_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
1996
        remoteDispatchError (client, req, "%s", _("network not found"));
1997 1998 1999
        return -2;
    }

2000 2001
    if (virNetworkCreate (net) == -1) {
        virNetworkFree(net);
2002
        return -1;
2003 2004
    }
    virNetworkFree(net);
2005 2006 2007 2008
    return 0;
}

static int
2009 2010
remoteDispatchNetworkCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
                                remote_message_header *req,
                                remote_network_create_xml_args *args,
                                remote_network_create_xml_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = virNetworkCreateXML (client->conn, args->xml);
    if (net == NULL) return -1;

    make_nonnull_network (&ret->net, net);
2022
    virNetworkFree(net);
2023 2024 2025 2026
    return 0;
}

static int
2027 2028
remoteDispatchNetworkDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
                                remote_message_header *req,
                                remote_network_define_xml_args *args,
                                remote_network_define_xml_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = virNetworkDefineXML (client->conn, args->xml);
    if (net == NULL) return -1;

    make_nonnull_network (&ret->net, net);
2040
    virNetworkFree(net);
2041 2042 2043 2044
    return 0;
}

static int
2045 2046
remoteDispatchNetworkDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
2047 2048 2049 2050 2051 2052 2053 2054 2055
                              remote_message_header *req,
                              remote_network_destroy_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
2056
        remoteDispatchError (client, req, "%s", _("network not found"));
2057 2058 2059
        return -2;
    }

2060 2061
    if (virNetworkDestroy (net) == -1) {
        virNetworkFree(net);
2062
        return -1;
2063 2064
    }
    virNetworkFree(net);
2065 2066 2067 2068
    return 0;
}

static int
2069 2070
remoteDispatchNetworkDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
2071 2072 2073 2074 2075 2076 2077 2078 2079
                              remote_message_header *req,
                              remote_network_dump_xml_args *args,
                              remote_network_dump_xml_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
2080
        remoteDispatchError (client, req, "%s", _("network not found"));
2081 2082 2083 2084 2085
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
2086 2087 2088 2089 2090
    if (!ret->xml) {
        virNetworkFree(net);
        return -1;
    }
    virNetworkFree(net);
2091 2092 2093 2094
    return 0;
}

static int
2095 2096
remoteDispatchNetworkGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
2097 2098 2099 2100 2101 2102 2103 2104 2105
                                   remote_message_header *req,
                                   remote_network_get_autostart_args *args,
                                   remote_network_get_autostart_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
2106
        remoteDispatchError (client, req, "%s", _("network not found"));
2107 2108 2109
        return -2;
    }

2110 2111
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
        virNetworkFree(net);
2112
        return -1;
2113 2114
    }
    virNetworkFree(net);
2115 2116 2117 2118
    return 0;
}

static int
2119 2120
remoteDispatchNetworkGetBridgeName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
2121 2122 2123 2124 2125 2126 2127 2128 2129
                                    remote_message_header *req,
                                    remote_network_get_bridge_name_args *args,
                                    remote_network_get_bridge_name_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
2130
        remoteDispatchError (client, req, "%s", _("network not found"));
2131 2132 2133 2134 2135
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
2136 2137 2138 2139 2140
    if (!ret->name) {
        virNetworkFree(net);
        return -1;
    }
    virNetworkFree(net);
2141 2142 2143 2144
    return 0;
}

static int
2145 2146
remoteDispatchNetworkLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
                                   remote_message_header *req,
                                   remote_network_lookup_by_name_args *args,
                                   remote_network_lookup_by_name_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = virNetworkLookupByName (client->conn, args->name);
    if (net == NULL) return -1;

    make_nonnull_network (&ret->net, net);
2158
    virNetworkFree(net);
2159 2160 2161 2162
    return 0;
}

static int
2163 2164
remoteDispatchNetworkLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
                                   remote_message_header *req,
                                   remote_network_lookup_by_uuid_args *args,
                                   remote_network_lookup_by_uuid_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = virNetworkLookupByUUID (client->conn, (unsigned char *) args->uuid);
    if (net == NULL) return -1;

    make_nonnull_network (&ret->net, net);
2176
    virNetworkFree(net);
2177 2178 2179 2180
    return 0;
}

static int
2181 2182
remoteDispatchNetworkSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
2183 2184 2185 2186 2187 2188 2189 2190 2191
                                   remote_message_header *req,
                                   remote_network_set_autostart_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
2192
        remoteDispatchError (client, req, "%s", _("network not found"));
2193 2194 2195
        return -2;
    }

2196 2197
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
        virNetworkFree(net);
2198
        return -1;
2199 2200
    }
    virNetworkFree(net);
2201 2202 2203 2204
    return 0;
}

static int
2205 2206
remoteDispatchNetworkUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
2207 2208 2209 2210 2211 2212 2213 2214 2215
                               remote_message_header *req,
                               remote_network_undefine_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
2216
        remoteDispatchError (client, req, "%s", _("network not found"));
2217 2218 2219
        return -2;
    }

2220 2221
    if (virNetworkUndefine (net) == -1) {
        virNetworkFree(net);
2222
        return -1;
2223 2224
    }
    virNetworkFree(net);
2225 2226 2227 2228
    return 0;
}

static int
2229 2230
remoteDispatchNumOfDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
                                    remote_message_header *req,
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_num_of_defined_networks_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfDefinedNetworks (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

static int
2244 2245
remoteDispatchNumOfDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
                            remote_message_header *req,
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_domains_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfDomains (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

static int
2259 2260
remoteDispatchNumOfNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
                             remote_message_header *req,
                             void *args ATTRIBUTE_UNUSED,
                             remote_num_of_networks_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfNetworks (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

2273 2274

static int
2275 2276
remoteDispatchAuthList (struct qemud_server *server ATTRIBUTE_UNUSED,
                        struct qemud_client *client,
2277 2278 2279 2280 2281
                        remote_message_header *req ATTRIBUTE_UNUSED,
                        void *args ATTRIBUTE_UNUSED,
                        remote_auth_list_ret *ret)
{
    ret->types.types_len = 1;
2282 2283
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
        return -2;
    }
    ret->types.types_val[0] = client->auth;
    return 0;
}


#if HAVE_SASL
/*
 * NB, keep in sync with similar method in src/remote_internal.c
 */
static char *addrToString(struct qemud_client *client,
                          remote_message_header *req,
                          struct sockaddr_storage *sa, socklen_t salen) {
    char host[1024], port[20];
    char *addr;
    int err;

    if ((err = getnameinfo((struct sockaddr *)sa, salen,
                           host, sizeof(host),
                           port, sizeof(port),
                           NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
        remoteDispatchError(client, req,
2307 2308
                            _("Cannot resolve address %d: %s"),
                            err, gai_strerror(err));
2309 2310 2311
        return NULL;
    }

2312 2313
    if (VIR_ALLOC_N(addr, strlen(host) + 1 + strlen(port) + 1) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
        return NULL;
    }

    strcpy(addr, host);
    strcat(addr, ";");
    strcat(addr, port);
    return addr;
}


/*
 * Initializes the SASL session in prepare for authentication
2326
 * and gives the client a list of allowed mechanisms to choose
2327 2328 2329 2330
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
2331 2332
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
2333 2334 2335 2336 2337
                            remote_message_header *req,
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret)
{
    const char *mechlist = NULL;
2338
    sasl_security_properties_t secprops;
2339 2340 2341 2342 2343 2344 2345 2346
    int err;
    struct sockaddr_storage sa;
    socklen_t salen;
    char *localAddr, *remoteAddr;

    REMOTE_DEBUG("Initialize SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
J
Jim Meyering 已提交
2347
        qemudLog(QEMUD_ERR, "%s", _("client tried invalid SASL init request"));
2348 2349 2350 2351 2352 2353 2354
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    /* Get local address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getsockname(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
2355 2356
        remoteDispatchError(client, req,
                            _("failed to get sock address %d (%s)"),
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
                            errno, strerror(errno));
        return -2;
    }
    if ((localAddr = addrToString(client, req, &sa, salen)) == NULL) {
        return -2;
    }

    /* Get remote address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getpeername(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
2367
        remoteDispatchError(client, req, _("failed to get peer address %d (%s)"),
2368
                            errno, strerror(errno));
2369
        VIR_FREE(localAddr);
2370 2371 2372
        return -2;
    }
    if ((remoteAddr = addrToString(client, req, &sa, salen)) == NULL) {
2373
        VIR_FREE(localAddr);
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
        return -2;
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
2385 2386
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
2387
    if (err != SASL_OK) {
2388
        qemudLog(QEMUD_ERR, _("sasl context setup failed %d (%s)"),
2389 2390 2391 2392 2393 2394
                 err, sasl_errstring(err, NULL, NULL));
        remoteDispatchFailAuth(client, req);
        client->saslconn = NULL;
        return -2;
    }

2395 2396 2397 2398 2399 2400 2401
    /* Inform SASL that we've got an external SSF layer from TLS */
    if (client->type == QEMUD_SOCK_TYPE_TLS) {
        gnutls_cipher_algorithm_t cipher;
        sasl_ssf_t ssf;

        cipher = gnutls_cipher_get(client->tlssession);
        if (!(ssf = (sasl_ssf_t)gnutls_cipher_get_key_size(cipher))) {
J
Jim Meyering 已提交
2402
            qemudLog(QEMUD_ERR, "%s", _("cannot TLS get cipher size"));
2403 2404 2405 2406 2407 2408 2409 2410 2411
            remoteDispatchFailAuth(client, req);
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
            return -2;
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
2412
            qemudLog(QEMUD_ERR, _("cannot set SASL external SSF %d (%s)"),
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
                     err, sasl_errstring(err, NULL, NULL));
            remoteDispatchFailAuth(client, req);
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
            return -2;
        }
    }

    memset (&secprops, 0, sizeof secprops);
    if (client->type == QEMUD_SOCK_TYPE_TLS ||
        client->type == QEMUD_SOCK_TYPE_UNIX) {
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
        secprops.min_ssf = 0;
        secprops.max_ssf = 0;
        secprops.maxbufsize = 8192;
        secprops.security_flags = 0;
    } else {
        /* Plain TCP, better get an SSF layer */
        secprops.min_ssf = 56; /* Good enough to require kerberos */
        secprops.max_ssf = 100000; /* Arbitrary big number */
        secprops.maxbufsize = 8192;
        /* Forbid any anonymous or trivially crackable auth */
        secprops.security_flags =
            SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;
    }

    err = sasl_setprop(client->saslconn, SASL_SEC_PROPS, &secprops);
    if (err != SASL_OK) {
2441
        qemudLog(QEMUD_ERR, _("cannot set SASL security props %d (%s)"),
2442 2443 2444 2445 2446 2447 2448
                 err, sasl_errstring(err, NULL, NULL));
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -2;
    }

2449 2450 2451 2452 2453 2454 2455 2456 2457
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
2458
        qemudLog(QEMUD_ERR, _("cannot list SASL mechanisms %d (%s)"),
2459 2460 2461 2462 2463 2464 2465 2466 2467
                 err, sasl_errdetail(client->saslconn));
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -2;
    }
    REMOTE_DEBUG("Available mechanisms for client: '%s'", mechlist);
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
J
Jim Meyering 已提交
2468
        qemudLog(QEMUD_ERR, "%s", _("cannot allocate mechlist"));
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -2;
    }

    return 0;
}


2479
/* We asked for an SSF layer, so sanity check that we actually
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
 * got what we asked for */
static int
remoteSASLCheckSSF (struct qemud_client *client,
                    remote_message_header *req) {
    const void *val;
    int err, ssf;

    if (client->type == QEMUD_SOCK_TYPE_TLS ||
        client->type == QEMUD_SOCK_TYPE_UNIX)
        return 0; /* TLS or UNIX domain sockets trivially OK */

    err = sasl_getprop(client->saslconn, SASL_SSF, &val);
    if (err != SASL_OK) {
2493
        qemudLog(QEMUD_ERR, _("cannot query SASL ssf on connection %d (%s)"),
2494 2495 2496 2497 2498 2499 2500 2501 2502
                 err, sasl_errstring(err, NULL, NULL));
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    ssf = *(const int *)val;
    REMOTE_DEBUG("negotiated an SSF of %d", ssf);
    if (ssf < 56) { /* 56 is good for Kerberos */
2503
        qemudLog(QEMUD_ERR, _("negotiated SSF %d was not strong enough"), ssf);
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }

    /* Only setup for read initially, because we're about to send an RPC
     * reply which must be in plain text. When the next incoming RPC
     * arrives, we'll switch on writes too
     *
     * cf qemudClientReadSASL  in qemud.c
     */
    client->saslSSF = QEMUD_SASL_SSF_READ;

    /* We have a SSF !*/
    return 0;
}

2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
static int
remoteSASLCheckAccess (struct qemud_server *server,
                       struct qemud_client *client,
                       remote_message_header *req) {
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
2532 2533
        qemudLog(QEMUD_ERR,
                 _("cannot query SASL username on connection %d (%s)"),
2534 2535 2536 2537 2538 2539 2540
                 err, sasl_errstring(err, NULL, NULL));
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
J
Jim Meyering 已提交
2541
        qemudLog(QEMUD_ERR, "%s", _("no client username was found"));
2542 2543 2544 2545 2546 2547 2548 2549 2550
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    REMOTE_DEBUG("SASL client username %s", (const char *)val);

    client->saslUsername = strdup((const char*)val);
    if (client->saslUsername == NULL) {
J
Jim Meyering 已提交
2551
        qemudLog(QEMUD_ERR, "%s", _("out of memory copying username"));
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }

    /* If the list is not set, allow any DN. */
    wildcards = server->saslUsernameWhitelist;
    if (!wildcards)
        return 0; /* No ACL, allow all */

    while (*wildcards) {
        if (fnmatch (*wildcards, client->saslUsername, 0) == 0)
            return 0; /* Allowed */
        wildcards++;
    }

    /* Denied */
2570 2571
    qemudLog(QEMUD_ERR, _("SASL client %s not allowed in whitelist"),
             client->saslUsername);
2572 2573 2574 2575 2576 2577 2578
    remoteDispatchFailAuth(client, req);
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
    return -1;
}


2579 2580 2581 2582
/*
 * This starts the SASL authentication negotiation.
 */
static int
2583 2584
remoteDispatchAuthSaslStart (struct qemud_server *server,
                             struct qemud_client *client,
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
                             remote_message_header *req,
                             remote_auth_sasl_start_args *args,
                             remote_auth_sasl_start_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

    REMOTE_DEBUG("Start SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
J
Jim Meyering 已提交
2596
        qemudLog(QEMUD_ERR, "%s", _("client tried invalid SASL start request"));
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    REMOTE_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
                 args->mech, args->data.data_len, args->nil);
    err = sasl_server_start(client->saslconn,
                            args->mech,
                            /* NB, distinction of NULL vs "" is *critical* in SASL */
                            args->nil ? NULL : args->data.data_val,
                            args->data.data_len,
                            &serverout,
                            &serveroutlen);
    if (err != SASL_OK &&
        err != SASL_CONTINUE) {
2612
        qemudLog(QEMUD_ERR, _("sasl start failed %d (%s)"),
2613 2614 2615 2616 2617 2618 2619
                 err, sasl_errdetail(client->saslconn));
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        remoteDispatchFailAuth(client, req);
        return -2;
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2620 2621
        qemudLog(QEMUD_ERR, _("sasl start reply data too long %d"),
                 serveroutlen);
2622 2623 2624 2625 2626 2627 2628 2629
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2630 2631
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
            remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
            return -2;
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

    REMOTE_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
2645 2646 2647
        if (remoteSASLCheckSSF(client, req) < 0)
            return -2;

2648 2649 2650 2651
        /* Check username whitelist ACL */
        if (remoteSASLCheckAccess(server, client, req) < 0)
            return -2;

2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

    return 0;
}


static int
2662 2663
remoteDispatchAuthSaslStep (struct qemud_server *server,
                            struct qemud_client *client,
2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
                            remote_message_header *req,
                            remote_auth_sasl_step_args *args,
                            remote_auth_sasl_step_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

    REMOTE_DEBUG("Step SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
J
Jim Meyering 已提交
2675
        qemudLog(QEMUD_ERR, "%s", _("client tried invalid SASL start request"));
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    REMOTE_DEBUG("Using SASL Data %d bytes, nil: %d",
                 args->data.data_len, args->nil);
    err = sasl_server_step(client->saslconn,
                           /* NB, distinction of NULL vs "" is *critical* in SASL */
                           args->nil ? NULL : args->data.data_val,
                           args->data.data_len,
                           &serverout,
                           &serveroutlen);
    if (err != SASL_OK &&
        err != SASL_CONTINUE) {
2690
        qemudLog(QEMUD_ERR, _("sasl step failed %d (%s)"),
2691 2692 2693 2694 2695 2696 2697 2698
                 err, sasl_errdetail(client->saslconn));
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2699 2700
        qemudLog(QEMUD_ERR, _("sasl step reply data too long %d"),
                 serveroutlen);
2701 2702 2703 2704 2705 2706 2707 2708
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2709 2710
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
            remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
            return -2;
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

    REMOTE_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
2724 2725 2726
        if (remoteSASLCheckSSF(client, req) < 0)
            return -2;

2727 2728 2729 2730
        /* Check username whitelist ACL */
        if (remoteSASLCheckAccess(server, client, req) < 0)
            return -2;

2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

    return 0;
}


#else /* HAVE_SASL */
static int
2742 2743
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
2744 2745 2746 2747
                            remote_message_header *req,
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
J
Jim Meyering 已提交
2748
    qemudLog(QEMUD_ERR, "%s", _("client tried unsupported SASL init request"));
2749 2750 2751 2752 2753
    remoteDispatchFailAuth(client, req);
    return -1;
}

static int
2754 2755
remoteDispatchAuthSaslStart (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
2756 2757 2758 2759
                             remote_message_header *req,
                             remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                             remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
J
Jim Meyering 已提交
2760
    qemudLog(QEMUD_ERR, "%s", _("client tried unsupported SASL start request"));
2761 2762 2763 2764 2765
    remoteDispatchFailAuth(client, req);
    return -1;
}

static int
2766 2767
remoteDispatchAuthSaslStep (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
2768 2769 2770 2771
                            remote_message_header *req,
                            remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
J
Jim Meyering 已提交
2772
    qemudLog(QEMUD_ERR, "%s", _("client tried unsupported SASL step request"));
2773 2774 2775 2776 2777 2778
    remoteDispatchFailAuth(client, req);
    return -1;
}
#endif /* HAVE_SASL */


2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
#if HAVE_POLKIT
static int
remoteDispatchAuthPolkit (struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client,
                          remote_message_header *req,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
2789 2790 2791 2792 2793 2794 2795 2796 2797
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
    const char *action = client->readonly ?
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
2798 2799 2800

    REMOTE_DEBUG("Start PolicyKit auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_POLKIT) {
J
Jim Meyering 已提交
2801 2802
        qemudLog(QEMUD_ERR,
                 "%s", _("client tried invalid PolicyKit init request"));
2803 2804 2805 2806 2807
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    if (qemudGetSocketIdentity(client->fd, &callerUid, &callerPid) < 0) {
J
Jim Meyering 已提交
2808
        qemudLog(QEMUD_ERR, "%s", _("cannot get peer socket identity"));
2809 2810 2811 2812
        remoteDispatchFailAuth(client, req);
        return -2;
    }

2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
    qemudLog(QEMUD_INFO, _("Checking PID %d running as %d"),
             callerPid, callerUid);
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
        qemudLog(QEMUD_ERR, _("Failed to lookup policy kit caller: %s"),
                 err.message);
        dbus_error_free(&err);
        remoteDispatchFailAuth(client, req);
        return -2;
    }
2824

2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
    if (!(pkaction = polkit_action_new())) {
        qemudLog(QEMUD_ERR, _("Failed to create polkit action %s\n"),
                 strerror(errno));
        polkit_caller_unref(pkcaller);
        remoteDispatchFailAuth(client, req);
        return -2;
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
        qemudLog(QEMUD_ERR, _("Failed to create polkit context %s\n"),
                 (pkerr ? polkit_error_get_error_message(pkerr)
                  : strerror(errno)));
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
        remoteDispatchFailAuth(client, req);
        return -2;
    }
2847

2848
#if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
        qemudLog(QEMUD_ERR,
                 _("Policy kit failed to check authorization %d %s"),
                 polkit_error_get_error_code(pkerr),
                 polkit_error_get_error_message(pkerr));
        remoteDispatchFailAuth(client, req);
        return -2;
    }
2862
#else
2863 2864 2865
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
2866
#endif
2867 2868 2869 2870 2871 2872 2873
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
        qemudLog(QEMUD_ERR,
                 _("Policy kit denied action %s from pid %d, uid %d,"
                   " result: %s\n"),
2874 2875
                 action, callerPid, callerUid,
                 polkit_result_to_string_representation(pkresult));
2876 2877
        remoteDispatchFailAuth(client, req);
        return -2;
2878
    }
2879 2880 2881 2882 2883 2884
    qemudLog(QEMUD_INFO,
             _("Policy allowed action %s from pid %d, uid %d, result %s"),
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
2885 2886 2887 2888 2889 2890 2891

    return 0;
}

#else /* HAVE_POLKIT */

static int
2892
remoteDispatchAuthPolkit (struct qemud_server *server ATTRIBUTE_UNUSED,
2893 2894 2895 2896 2897
                              struct qemud_client *client,
                              remote_message_header *req,
                              void *args ATTRIBUTE_UNUSED,
                              remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
{
J
Jim Meyering 已提交
2898 2899
    qemudLog(QEMUD_ERR,
             "%s", _("client tried unsupported PolicyKit init request"));
2900 2901 2902 2903 2904
    remoteDispatchFailAuth(client, req);
    return -1;
}
#endif /* HAVE_POLKIT */

2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921

/***************************************************************
 *     STORAGE POOL APIS
 ***************************************************************/


static int
remoteDispatchListDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client,
                                       remote_message_header *req,
                                       remote_list_defined_storage_pools_args *args,
                                       remote_list_defined_storage_pools_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
2922
                             "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
2923 2924 2925 2926
        return -2;
    }

    /* Allocate return buffer. */
2927 2928 2929 2930
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
2931 2932 2933

    ret->names.names_len =
        virConnectListDefinedStoragePools (client->conn,
2934 2935 2936 2937 2938
                                           ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
        return -1;
    }
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953

    return 0;
}

static int
remoteDispatchListStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
                                remote_message_header *req,
                                remote_list_storage_pools_args *args,
                                remote_list_storage_pools_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
2954
                             "%s", _("maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX"));
2955 2956 2957 2958
        return -2;
    }

    /* Allocate return buffer. */
2959 2960 2961 2962
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
2963 2964 2965 2966

    ret->names.names_len =
        virConnectListStoragePools (client->conn,
                                ret->names.names_val, args->maxnames);
2967 2968 2969 2970
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
        return -1;
    }
2971 2972 2973 2974

    return 0;
}

2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
static int
remoteDispatchFindStoragePoolSources (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client,
                                      remote_message_header *req,
                                      remote_find_storage_pool_sources_args *args,
                                      remote_find_storage_pool_sources_ret *ret)
{
    CHECK_CONN(client);

    ret->xml =
        virConnectFindStoragePoolSources (client->conn,
                                          args->type,
                                          args->srcSpec ? *args->srcSpec : NULL,
                                          args->flags);
    if (ret->xml == NULL)
        return -1;

    return 0;
}


2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007
static int
remoteDispatchStoragePoolCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
                                 remote_message_header *req,
                                 remote_storage_pool_create_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3008
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
        return -2;
    }

    if (virStoragePoolCreate (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
                                    remote_message_header *req,
                                    remote_storage_pool_create_xml_args *args,
                                    remote_storage_pool_create_xml_ret *ret)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = virStoragePoolCreateXML (client->conn, args->xml, args->flags);
    if (pool == NULL) return -1;

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
                                    remote_message_header *req,
                                    remote_storage_pool_define_xml_args *args,
                                    remote_storage_pool_define_xml_ret *ret)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = virStoragePoolDefineXML (client->conn, args->xml, args->flags);
    if (pool == NULL) return -1;

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolBuild (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
                                 remote_message_header *req,
                                 remote_storage_pool_build_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3068
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
        return -2;
    }

    if (virStoragePoolBuild (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_storage_pool_destroy_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3093
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116
        return -2;
    }

    if (virStoragePoolDestroy (pool) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
                                 remote_message_header *req,
                                 remote_storage_pool_delete_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3117
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
        return -2;
    }

    if (virStoragePoolDelete (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolRefresh (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_storage_pool_refresh_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3141
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
        return -2;
    }

    if (virStoragePoolRefresh (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_storage_pool_get_info_args *args,
                                  remote_storage_pool_get_info_ret *ret)
{
    virStoragePoolPtr pool;
    virStoragePoolInfo info;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3166
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196
        return -2;
    }

    if (virStoragePoolGetInfo (pool, &info) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }

    ret->state = info.state;
    ret->capacity = info.capacity;
    ret->allocation = info.allocation;
    ret->available = info.available;

    virStoragePoolFree(pool);

    return 0;
}

static int
remoteDispatchStoragePoolDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_storage_pool_dump_xml_args *args,
                                  remote_storage_pool_dump_xml_ret *ret)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3197
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStoragePoolGetXMLDesc (pool, args->flags);
    if (!ret->xml) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client,
                                       remote_message_header *req,
                                       remote_storage_pool_get_autostart_args *args,
                                       remote_storage_pool_get_autostart_ret *ret)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3223
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
        return -2;
    }

    if (virStoragePoolGetAutostart (pool, &ret->autostart) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client,
                                       remote_message_header *req,
                                       remote_storage_pool_lookup_by_name_args *args,
                                       remote_storage_pool_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = virStoragePoolLookupByName (client->conn, args->name);
    if (pool == NULL) return -1;

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client,
                                       remote_message_header *req,
                                       remote_storage_pool_lookup_by_uuid_args *args,
                                       remote_storage_pool_lookup_by_uuid_ret *ret)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = virStoragePoolLookupByUUID (client->conn, (unsigned char *) args->uuid);
    if (pool == NULL) return -1;

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolLookupByVolume (struct qemud_server *server ATTRIBUTE_UNUSED,
                                         struct qemud_client *client,
                                         remote_message_header *req,
                                         remote_storage_pool_lookup_by_volume_args *args,
                                         remote_storage_pool_lookup_by_volume_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;
    CHECK_CONN(client);

    vol = get_nonnull_storage_vol (client->conn, args->vol);

    pool = virStoragePoolLookupByVolume (vol);
    virStorageVolFree(vol);
    if (pool == NULL) return -1;

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client,
                                       remote_message_header *req,
                                       remote_storage_pool_set_autostart_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3306
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329
        return -2;
    }

    if (virStoragePoolSetAutostart (pool, args->autostart) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
                                   remote_message_header *req,
                                   remote_storage_pool_undefine_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3330
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
        return -2;
    }

    if (virStoragePoolUndefine (pool) == -1) {
        virStoragePoolFree(pool);
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchNumOfStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
                                 remote_message_header *req,
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_num_of_storage_pools_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfStoragePools (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

static int
remoteDispatchNumOfDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
                                        struct qemud_client *client,
                                        remote_message_header *req,
                                        void *args ATTRIBUTE_UNUSED,
                                        remote_num_of_defined_storage_pools_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfDefinedStoragePools (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

static int
remoteDispatchStoragePoolListVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client,
                                      remote_message_header *req,
                                      remote_storage_pool_list_volumes_args *args,
                                      remote_storage_pool_list_volumes_ret *ret)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
3384
                             "%s", _("maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX"));
3385 3386 3387 3388 3389
        return -2;
    }

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3390
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3391 3392 3393 3394
        return -2;
    }

    /* Allocate return buffer. */
3395 3396 3397 3398 3399
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        virStoragePoolFree(pool);
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
        return -2;
    }
3400 3401 3402 3403 3404

    ret->names.names_len =
        virStoragePoolListVolumes (pool,
                                   ret->names.names_val, args->maxnames);
    virStoragePoolFree(pool);
3405 3406 3407 3408
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
        return -1;
    }
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425

    return 0;
}


static int
remoteDispatchStoragePoolNumOfVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client,
                                       remote_message_header *req,
                                       remote_storage_pool_num_of_volumes_args *args,
                                       remote_storage_pool_num_of_volumes_ret *ret)
{
    virStoragePoolPtr pool;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3426
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
        return -2;
    }

    ret->num = virStoragePoolNumOfVolumes (pool);
    virStoragePoolFree(pool);
    if (ret->num == -1) return -1;

    return 0;
}


/***************************************************************
 *     STORAGE VOL APIS
 ***************************************************************/



static int
remoteDispatchStorageVolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
                                   remote_message_header *req,
                                   remote_storage_vol_create_xml_args *args,
                                   remote_storage_vol_create_xml_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3457
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482
        return -2;
    }

    vol = virStorageVolCreateXML (pool, args->xml, args->flags);
    virStoragePoolFree(pool);
    if (vol == NULL) return -1;

    make_nonnull_storage_vol (&ret->vol, vol);
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
                                remote_message_header *req,
                                remote_storage_vol_delete_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virStorageVolPtr vol;
    CHECK_CONN(client);

    vol = get_nonnull_storage_vol (client->conn, args->vol);
    if (vol == NULL) {
3483
        remoteDispatchError (client, req, "%s", _("storage_vol not found"));
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
        return -2;
    }

    if (virStorageVolDelete (vol, args->flags) == -1) {
        virStorageVolFree(vol);
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}

static int
remoteDispatchStorageVolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
                                 remote_message_header *req,
                                 remote_storage_vol_get_info_args *args,
                                 remote_storage_vol_get_info_ret *ret)
{
    virStorageVolPtr vol;
    virStorageVolInfo info;
    CHECK_CONN(client);

    vol = get_nonnull_storage_vol (client->conn, args->vol);
    if (vol == NULL) {
3508
        remoteDispatchError (client, req, "%s", _("storage_vol not found"));
3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537
        return -2;
    }

    if (virStorageVolGetInfo (vol, &info) == -1) {
        virStorageVolFree(vol);
        return -1;
    }

    ret->type = info.type;
    ret->capacity = info.capacity;
    ret->allocation = info.allocation;

    virStorageVolFree(vol);

    return 0;
}

static int
remoteDispatchStorageVolDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
                                 remote_message_header *req,
                                 remote_storage_vol_dump_xml_args *args,
                                 remote_storage_vol_dump_xml_ret *ret)
{
    virStorageVolPtr vol;
    CHECK_CONN(client);

    vol = get_nonnull_storage_vol (client->conn, args->vol);
    if (vol == NULL) {
3538
        remoteDispatchError (client, req, "%s", _("storage_vol not found"));
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStorageVolGetXMLDesc (vol, args->flags);
    if (!ret->xml) {
        virStorageVolFree(vol);
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolGetPath (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
                                 remote_message_header *req,
                                 remote_storage_vol_get_path_args *args,
                                 remote_storage_vol_get_path_ret *ret)
{
    virStorageVolPtr vol;
    CHECK_CONN(client);

    vol = get_nonnull_storage_vol (client->conn, args->vol);
    if (vol == NULL) {
3565
        remoteDispatchError (client, req, "%s", _("storage_vol not found"));
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virStorageVolGetPath (vol);
    if (!ret->name) {
        virStorageVolFree(vol);
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client,
                                      remote_message_header *req,
                                      remote_storage_vol_lookup_by_name_args *args,
                                      remote_storage_vol_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;
    CHECK_CONN(client);

    pool = get_nonnull_storage_pool (client->conn, args->pool);
    if (pool == NULL) {
3593
        remoteDispatchError (client, req, "%s", _("storage_pool not found"));
3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
        return -2;
    }

    vol = virStorageVolLookupByName (pool, args->name);
    virStoragePoolFree(pool);
    if (vol == NULL) return -1;

    make_nonnull_storage_vol (&ret->vol, vol);
    virStorageVolFree(vol);
    return 0;
}

static int
remoteDispatchStorageVolLookupByKey (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client,
                                     remote_message_header *req,
                                     remote_storage_vol_lookup_by_key_args *args,
                                     remote_storage_vol_lookup_by_key_ret *ret)
{
    virStorageVolPtr vol;
    CHECK_CONN(client);

    vol = virStorageVolLookupByKey (client->conn, args->key);
    if (vol == NULL) return -1;

    make_nonnull_storage_vol (&ret->vol, vol);
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByPath (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client,
                                      remote_message_header *req,
                                      remote_storage_vol_lookup_by_path_args *args,
                                      remote_storage_vol_lookup_by_path_ret *ret)
{
    virStorageVolPtr vol;
    CHECK_CONN(client);

    vol = virStorageVolLookupByPath (client->conn, args->path);
    if (vol == NULL) return -1;

    make_nonnull_storage_vol (&ret->vol, vol);
    virStorageVolFree(vol);
    return 0;
}


3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766
/**************************
 * Async Events
 **************************/
static int
remoteDispatchDomainEvent (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           remote_message_header *req ATTRIBUTE_UNUSED,
                           void *args ATTRIBUTE_UNUSED,
                           remote_domain_event_ret *ret ATTRIBUTE_UNUSED)
{
    /* This call gets dispatched from a client call.
     * This does not make sense, as this should not be intiated
     * from the client side in generated code.
     */
     return -1;
}

/***************************
 * Register / deregister events
 ***************************/
static int
remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
                                    remote_message_header *req ATTRIBUTE_UNUSED,
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

    /* Register event delivery callback */
    REMOTE_DEBUG("%s","Registering to relay remote events");
    virConnectDomainEventRegister(client->conn, remoteRelayDomainEvent, client);

    if(ret)
        ret->cb_registered = 1;
    return 0;
}

static int
remoteDispatchDomainEventsDeregister (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client,
                                      remote_message_header *req ATTRIBUTE_UNUSED,
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

    /* Deregister event delivery callback */
    REMOTE_DEBUG("%s","Deregistering to relay remote events");
    virConnectDomainEventDeregister(client->conn, remoteRelayDomainEvent);

    if(ret)
        ret->cb_registered = 0;
    return 0;
}

static void
remoteDispatchDomainEventSend (struct qemud_client *client,
                         virDomainPtr dom,
                         virDomainEventType event)
{
    remote_message_header rep;
    XDR xdr;
    int len;
    remote_domain_event_ret data;

    if(!client) {
        remoteDispatchError (client, NULL, "%s", _("Invalid Client"));
        return;
    }

    rep.prog = REMOTE_PROGRAM;
    rep.vers = REMOTE_PROTOCOL_VERSION;
    rep.proc = REMOTE_PROC_DOMAIN_EVENT;
    rep.direction = REMOTE_MESSAGE;
    rep.serial = 1;
    rep.status = REMOTE_OK;

    /* Serialise the return header and event. */
    xdrmem_create (&xdr, client->buffer, sizeof client->buffer, XDR_ENCODE);

    len = 0; /* We'll come back and write this later. */
    if (!xdr_int (&xdr, &len)) {
        remoteDispatchError (client, NULL, "%s", _("xdr_int failed (1)"));
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_remote_message_header (&xdr, &rep)) {
        xdr_destroy (&xdr);
        return;
    }

    /* build return data */
    make_nonnull_domain (&data.dom, dom);
    data.event = (int) event;

    if (!xdr_remote_domain_event_ret(&xdr, &data)) {
        remoteDispatchError (client, NULL, "%s", _("serialise return struct"));
        xdr_destroy (&xdr);
        return;
    }

    len = xdr_getpos (&xdr);
    if (xdr_setpos (&xdr, 0) == 0) {
        remoteDispatchError (client, NULL, "%s", _("xdr_setpos failed"));
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_int (&xdr, &len)) {
        remoteDispatchError (client, NULL, "%s", _("xdr_int failed (2)"));
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Send it. */
    client->mode = QEMUD_MODE_TX_PACKET;
    client->bufferLength = len;
    client->bufferOffset = 0;
}
3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792
/*----- Helpers. -----*/

/* get_nonnull_domain and get_nonnull_network turn an on-wire
 * (name, uuid) pair into virDomainPtr or virNetworkPtr object.
 * virDomainPtr or virNetworkPtr cannot be NULL.
 *
 * NB. If these return NULL then the caller must return an error.
 */
static virDomainPtr
get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain)
{
    virDomainPtr dom;
    dom = virGetDomain (conn, domain.name, BAD_CAST domain.uuid);
    /* Should we believe the domain.id sent by the client?  Maybe
     * this should be a check rather than an assignment? XXX
     */
    if (dom) dom->id = domain.id;
    return dom;
}

static virNetworkPtr
get_nonnull_network (virConnectPtr conn, remote_nonnull_network network)
{
    return virGetNetwork (conn, network.name, BAD_CAST network.uuid);
}

3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806
static virStoragePoolPtr
get_nonnull_storage_pool (virConnectPtr conn, remote_nonnull_storage_pool pool)
{
    return virGetStoragePool (conn, pool.name, BAD_CAST pool.uuid);
}

static virStorageVolPtr
get_nonnull_storage_vol (virConnectPtr conn, remote_nonnull_storage_vol vol)
{
    virStorageVolPtr ret;
    ret = virGetStorageVol (conn, vol.pool, vol.name, vol.key);
    return ret;
}

3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
{
    dom_dst->id = dom_src->id;
    dom_dst->name = strdup (dom_src->name);
    memcpy (dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
}

static void
make_nonnull_network (remote_nonnull_network *net_dst, virNetworkPtr net_src)
{
    net_dst->name = strdup (net_src->name);
    memcpy (net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
}

3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836
static void
make_nonnull_storage_pool (remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
{
    pool_dst->name = strdup (pool_src->name);
    memcpy (pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
}

static void
make_nonnull_storage_vol (remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
{
    vol_dst->pool = strdup (vol_src->pool);
    vol_dst->name = strdup (vol_src->name);
    vol_dst->key = strdup (vol_src->key);
}