remote.c 80.4 KB
Newer Older
1 2 3 4 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 44 45 46
/*
 * remote.c: code handling remote requests (from remote_internal.c)
 *
 * Copyright (C) 2007 Red Hat, Inc.
 *
 * 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>
#include <getopt.h>
#include <ctype.h>
#include <assert.h>
47
#include <fnmatch.h>
48

49
#include "libvirt/virterror.h"
50 51 52 53 54 55

#include "internal.h"
#include "../src/internal.h"

#define DEBUG 0

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 64 65 66 67 68
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
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);

#include "remote_dispatch_prototypes.h"

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

/* 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
80
remoteDispatchClientRequest (struct qemud_server *server,
81 82 83 84 85 86 87 88 89 90 91 92 93
                             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. */
94
    xdrmem_create (&xdr, client->buffer, client->bufferLength, XDR_DECODE);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    if (!xdr_remote_message_header (&xdr, &req)) {
        remoteDispatchError (client, NULL, "xdr_remote_message_header");
        xdr_destroy (&xdr);
        return;
    }

    /* Check version, etc. */
    if (req.prog != REMOTE_PROGRAM) {
        remoteDispatchError (client, &req, "program mismatch (actual %x, expected %x)",
                             req.prog, REMOTE_PROGRAM);
        xdr_destroy (&xdr);
        return;
    }
    if (req.vers != REMOTE_PROTOCOL_VERSION) {
        remoteDispatchError (client, &req, "version mismatch (actual %x, expected %x)",
                             req.vers, REMOTE_PROTOCOL_VERSION);
        xdr_destroy (&xdr);
        return;
    }
    if (req.direction != REMOTE_CALL) {
        remoteDispatchError (client, &req, "direction (%d) != REMOTE_CALL",
                             (int) req.direction);
        xdr_destroy (&xdr);
        return;
    }
    if (req.status != REMOTE_OK) {
        remoteDispatchError (client, &req, "status (%d) != REMOTE_OK",
                             (int) req.status);
        xdr_destroy (&xdr);
        return;
    }

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    /* 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 &&
            req.proc != REMOTE_PROC_AUTH_SASL_STEP
            ) {
            remoteDispatchError (client, &req, "authentication required");
            xdr_destroy (&xdr);
            return;
        }
    }

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    /* 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:
        remoteDispatchError (client, &req, "unknown procedure: %d",
                             req.proc);
        xdr_destroy (&xdr);
        return;
    }

    /* Parse args. */
    if (!(*args_filter) (&xdr, args)) {
        remoteDispatchError (client, &req, "parse args failed");
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Call function. */
166
    rv = fn (server, client, &req, args, ret);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    xdr_free (args_filter, args);

    /* Dispatch function must return -2, -1 or 0.  Anything else is
     * an internal error.
     */
    if (rv < -2 || rv > 0) {
        remoteDispatchError (client, &req, "internal error - dispatch function returned invalid code %d", rv);
        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)) {
        remoteDispatchError (client, &req, "dummy length");
        xdr_destroy (&xdr);
        if (rv == 0) xdr_free (ret_filter, ret);
        return;
    }

    if (!xdr_remote_message_header (&xdr, &rep)) {
        remoteDispatchError (client, &req, "serialise reply header");
        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)) {
            remoteDispatchError (client, &req, "serialise return struct");
            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)) {
            remoteDispatchError (client, &req, "serialise return error");
            xdr_destroy (&xdr);
            return;
        }
    }

    /* Write the length word. */
    len = xdr_getpos (&xdr);
    if (xdr_setpos (&xdr, 0) == 0) {
        remoteDispatchError (client, &req, "xdr_setpos");
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_int (&xdr, &len)) {
        remoteDispatchError (client, &req, "serialise return length");
        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
299 300 301
remoteDispatchSendError (struct qemud_client *client,
                         remote_message_header *req,
                         int code, const char *msg)
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
{
    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. */
328
    error.code = code;
329
    error.domain = VIR_FROM_REMOTE;
330
    error.message = (char**)&msg;
331 332
    error.level = VIR_ERR_ERROR;
    error.dom = NULL;
333
    error.str1 = (char**)&msg;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 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
    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;
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
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);
}



403 404 405
/*----- Functions. -----*/

static int
406 407
remoteDispatchOpen (struct qemud_server *server ATTRIBUTE_UNUSED,
                    struct qemud_client *client, remote_message_header *req,
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
                    struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
{
    const char *name;
    int flags;

    /* Already opened? */
    if (client->conn) {
        remoteDispatchError (client, req, "connection already open");
        return -2;
    }

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

#if DEBUG
    fprintf (stderr, "remoteDispatchOpen: name = %s\n", name);
#endif

    /* 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) {                        \
        remoteDispatchError (client, req, "connection not open");   \
        return -2;                                                  \
    }

static int
446 447
remoteDispatchClose (struct qemud_server *server ATTRIBUTE_UNUSED,
                     struct qemud_client *client, remote_message_header *req,
448 449 450 451 452 453 454 455 456 457 458
                     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;
}

459
static int
460 461
remoteDispatchSupportsFeature (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client, remote_message_header *req,
462 463 464 465 466 467 468 469 470 471
                               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;
}

472
static int
473 474
remoteDispatchGetType (struct qemud_server *server ATTRIBUTE_UNUSED,
                       struct qemud_client *client, remote_message_header *req,
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
                       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) {
        remoteDispatchError (client, req, "out of memory in strdup");
        return -2;
    }

    return 0;
}

static int
496 497
remoteDispatchGetVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client,
498 499 500 501 502 503 504 505 506 507 508 509 510 511
                          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;
}

512
static int
513 514
remoteDispatchGetHostname (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client,
515 516 517 518 519 520 521 522 523 524 525 526 527 528
                           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;
}

529
static int
530 531
remoteDispatchGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client,
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
                           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
547 548
remoteDispatchNodeGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client,
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
                           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
572 573
remoteDispatchGetCapabilities (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
574 575 576 577 578 579 580 581 582 583 584 585 586 587
                               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;
}

588
static int
589 590
remoteDispatchDomainGetSchedulerType (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client,
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
                                      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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    type = virDomainGetSchedulerType (dom, &nparams);
607 608 609 610
    if (type == NULL) {
        virDomainFree(dom);
        return -1;
    }
611 612 613

    ret->type = type;
    ret->nparams = nparams;
614
    virDomainFree(dom);
615 616 617 618
    return 0;
}

static int
619 620
remoteDispatchDomainGetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
                                            struct qemud_client *client,
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
                                            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) {
        remoteDispatchError (client, req, "nparams too large");
        return -2;
    }
    params = malloc (sizeof (virSchedParameter) * nparams);
    if (params == NULL) {
        remoteDispatchError (client, req, "out of memory allocating array");
        return -2;
    }

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        free (params);
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
651
        virDomainFree(dom);
652 653 654 655 656 657 658 659 660
        free (params);
        return -1;
    }

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
    ret->params.params_val = malloc (sizeof (struct remote_sched_param)
                                     * nparams);
    if (ret->params.params_val == NULL) {
661
        virDomainFree(dom);
662 663 664 665 666 667 668 669 670 671
        free (params);
        remoteDispatchError (client, req,
                             "out of memory allocating return array");
        return -2;
    }

    for (i = 0; i < nparams; ++i) {
        // remoteDispatchClientRequest will free this:
        ret->params.params_val[i].field = strdup (params[i].field);
        if (ret->params.params_val[i].field == NULL) {
672
            virDomainFree(dom);
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
            free (params);
            remoteDispatchError (client, req,
                                 "out of memory allocating return array");
            return -2;
        }
        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:
693
            virDomainFree(dom);
694 695 696 697 698
            free (params);
            remoteDispatchError (client, req, "unknown type");
            return -2;
        }
    }
699
    virDomainFree(dom);
700 701 702 703 704 705
    free (params);

    return 0;
}

static int
706 707
remoteDispatchDomainSetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
                                            struct qemud_client *client,
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
                                            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) {
        remoteDispatchError (client, req, "nparams too large");
        return -2;
    }
    params = malloc (sizeof (virSchedParameter) * nparams);
    if (params == NULL) {
        remoteDispatchError (client, req, "out of memory allocating array");
        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) {
        free (params);
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    r = virDomainSetSchedulerParameters (dom, params, nparams);
759
    virDomainFree(dom);
760 761 762 763 764 765
    free (params);
    if (r == -1) return -1;

    return 0;
}

766
static int
767 768
remoteDispatchDomainBlockStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
                                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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }
    path = args->path;

    if (virDomainBlockStats (dom, path, &stats, sizeof stats) == -1)
        return -1;

    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
798 799
remoteDispatchDomainInterfaceStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
                                    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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }
    path = args->path;

    if (virDomainInterfaceStats (dom, path, &stats, sizeof stats) == -1)
        return -1;

    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;
}

831
static int
832 833
remoteDispatchDomainAttachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
834 835 836 837 838 839 840 841 842 843 844 845 846
                                  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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

847 848
    if (virDomainAttachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
849
        return -1;
850 851
    }
    virDomainFree(dom);
852 853 854 855
    return 0;
}

static int
856 857
remoteDispatchDomainCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
858 859 860 861 862 863 864 865 866 867 868 869 870
                            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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

871 872
    if (virDomainCreate (dom) == -1) {
        virDomainFree(dom);
873
        return -1;
874 875
    }
    virDomainFree(dom);
876 877 878 879
    return 0;
}

static int
880 881
remoteDispatchDomainCreateLinux (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
882 883 884 885 886 887 888 889 890 891 892
                                 remote_message_header *req,
                                 remote_domain_create_linux_args *args,
                                 remote_domain_create_linux_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainCreateLinux (client->conn, args->xml_desc, args->flags);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
893
    virDomainFree(dom);
894 895 896 897 898

    return 0;
}

static int
899 900
remoteDispatchDomainDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
901 902 903 904 905 906 907 908 909 910 911
                               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);
912
    virDomainFree(dom);
913 914 915 916 917

    return 0;
}

static int
918 919
remoteDispatchDomainDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
                             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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    if (virDomainDestroy (dom) == -1)
        return -1;
935
    /* No need to free dom - destroy does it for us */
936 937 938 939
    return 0;
}

static int
940 941
remoteDispatchDomainDetachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
942 943 944 945 946 947 948 949 950 951 952 953 954
                                  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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

955 956
    if (virDomainDetachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
957
        return -1;
958
    }
959

960
    virDomainFree(dom);
961 962 963 964
    return 0;
}

static int
965 966
remoteDispatchDomainDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
                             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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
982 983 984 985 986
    if (!ret->xml) {
            virDomainFree(dom);
            return -1;
    }
    virDomainFree(dom);
987 988 989 990
    return 0;
}

static int
991 992
remoteDispatchDomainGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
                                  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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1006 1007
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
        virDomainFree(dom);
1008
        return -1;
1009 1010
    }
    virDomainFree(dom);
1011 1012 1013 1014
    return 0;
}

static int
1015 1016
remoteDispatchDomainGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
                             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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1031 1032
    if (virDomainGetInfo (dom, &info) == -1) {
        virDomainFree(dom);
1033
        return -1;
1034
    }
1035 1036 1037 1038 1039 1040 1041

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

1042 1043
    virDomainFree(dom);

1044 1045 1046 1047
    return 0;
}

static int
1048 1049
remoteDispatchDomainGetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
                                  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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    ret->memory = virDomainGetMaxMemory (dom);
1064 1065 1066 1067 1068
    if (ret->memory == 0) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
1069 1070 1071 1072
    return 0;
}

static int
1073 1074
remoteDispatchDomainGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client,
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
                                 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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    ret->num = virDomainGetMaxVcpus (dom);
1089 1090 1091 1092 1093
    if (ret->num == -1) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
1094 1095 1096 1097
    return 0;
}

static int
1098 1099
remoteDispatchDomainGetOsType (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
                               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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
1115 1116 1117 1118 1119
    if (ret->type == NULL) {
            virDomainFree(dom);
            return -1;
    }
    virDomainFree(dom);
1120 1121 1122 1123
    return 0;
}

static int
1124 1125
remoteDispatchDomainGetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
                              remote_message_header *req,
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
    virDomainPtr dom;
    virVcpuInfoPtr info;
    unsigned char *cpumaps;
    int info_len, i;
    CHECK_CONN(client);

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

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1143
        virDomainFree(dom);
1144 1145 1146 1147
        remoteDispatchError (client, req, "maxinfo > REMOTE_VCPUINFO_MAX");
        return -2;
    }

1148
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1149
        virDomainFree(dom);
1150
        remoteDispatchError (client, req, "maxinfo * maplen > REMOTE_CPUMAPS_MAX");
1151 1152 1153 1154 1155
        return -2;
    }

    /* Allocate buffers to take the results. */
    info = calloc (args->maxinfo, sizeof (virVcpuInfo));
1156
    cpumaps = calloc (args->maxinfo * args->maplen, sizeof (unsigned char));
1157 1158 1159 1160

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1161 1162 1163 1164
    if (info_len == -1) {
        virDomainFree(dom);
        return -1;
    }
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
    ret->info.info_val = calloc (info_len, sizeof (remote_vcpu_info));

    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.
     */
1181
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1182 1183
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1184
    virDomainFree(dom);
1185 1186 1187
    return 0;
}

1188
static int
1189 1190
remoteDispatchDomainMigratePrepare (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
                                    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 ... */
    uri_out = calloc (1, sizeof (char *));

    r = __virDomainMigratePrepare (client->conn, &cookie, &cookielen,
                                   uri_in, uri_out,
                                   args->flags, dname, args->resource);
    if (r == -1) return -1;

    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie.cookie_len = cookielen;
    ret->cookie.cookie_val = cookie;
    ret->uri_out = *uri_out == NULL ? NULL : uri_out;

    return 0;
}

static int
1225 1226
remoteDispatchDomainMigratePerform (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
                                    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) {
        remoteDispatchError (client, req, "domain not found");
        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);
    if (r == -1) return -1;

    return 0;
}

static int
1255 1256
remoteDispatchDomainMigrateFinish (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
                                   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);

    return 0;
}

1276
static int
1277 1278
remoteDispatchListDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
                                  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,
                             "maxnames > REMOTE_DOMAIN_NAME_LIST_MAX");
        return -2;
    }

    /* Allocate return buffer. */
    ret->names.names_val = calloc (args->maxnames, sizeof (char *));

    ret->names.names_len =
        virConnectListDefinedDomains (client->conn,
                                      ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) return -1;

    return 0;
}

static int
1303 1304
remoteDispatchDomainLookupById (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
                                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);
1316
    virDomainFree(dom);
1317 1318 1319 1320
    return 0;
}

static int
1321 1322
remoteDispatchDomainLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
                                  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);
1334
    virDomainFree(dom);
1335 1336 1337 1338
    return 0;
}

static int
1339 1340
remoteDispatchDomainLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
                                  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);
1352
    virDomainFree(dom);
1353 1354 1355 1356
    return 0;
}

static int
1357 1358
remoteDispatchNumOfDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
                                   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
1372 1373
remoteDispatchDomainPinVcpu (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
                             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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
1389
        virDomainFree(dom);
1390 1391 1392 1393 1394 1395 1396
        remoteDispatchError (client, req, "cpumap_len > REMOTE_CPUMAP_MAX");
        return -2;
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
1397 1398 1399 1400 1401
    if (rv == -1) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
1402 1403 1404 1405
    return 0;
}

static int
1406 1407
remoteDispatchDomainReboot (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
                            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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1421 1422
    if (virDomainReboot (dom, args->flags) == -1) {
        virDomainFree(dom);
1423
        return -1;
1424 1425
    }
    virDomainFree(dom);
1426 1427 1428 1429
    return 0;
}

static int
1430 1431
remoteDispatchDomainRestore (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
                             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
1445 1446
remoteDispatchDomainResume (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
                            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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1460 1461
    if (virDomainResume (dom) == -1) {
        virDomainFree(dom);
1462
        return -1;
1463 1464
    }
    virDomainFree(dom);
1465 1466 1467 1468
    return 0;
}

static int
1469 1470
remoteDispatchDomainSave (struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client,
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
                          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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1484 1485
    if (virDomainSave (dom, args->to) == -1) {
        virDomainFree(dom);
1486
        return -1;
1487 1488
    }
    virDomainFree(dom);
1489 1490 1491 1492
    return 0;
}

static int
1493 1494
remoteDispatchDomainCoreDump (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
                              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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1508 1509
    if (virDomainCoreDump (dom, args->to, args->flags) == -1) {
        virDomainFree(dom);
1510
        return -1;
1511 1512
    }
    virDomainFree(dom);
1513 1514 1515 1516
    return 0;
}

static int
1517 1518
remoteDispatchDomainSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
                                  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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1532 1533
    if (virDomainSetAutostart (dom, args->autostart) == -1) {
        virDomainFree(dom);
1534
        return -1;
1535 1536
    }
    virDomainFree(dom);
1537 1538 1539 1540
    return 0;
}

static int
1541 1542
remoteDispatchDomainSetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client,
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
                                  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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1556 1557
    if (virDomainSetMaxMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1558
        return -1;
1559 1560
    }
    virDomainFree(dom);
1561 1562 1563 1564
    return 0;
}

static int
1565 1566
remoteDispatchDomainSetMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
                               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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1580 1581
    if (virDomainSetMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1582
        return -1;
1583 1584
    }
    virDomainFree(dom);
1585 1586 1587 1588
    return 0;
}

static int
1589 1590
remoteDispatchDomainSetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
                              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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1604 1605
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
        virDomainFree(dom);
1606
        return -1;
1607 1608
    }
    virDomainFree(dom);
1609 1610 1611 1612
    return 0;
}

static int
1613 1614
remoteDispatchDomainShutdown (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
                              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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1628 1629
    if (virDomainShutdown (dom) == -1) {
        virDomainFree(dom);
1630
        return -1;
1631 1632
    }
    virDomainFree(dom);
1633 1634 1635 1636
    return 0;
}

static int
1637 1638
remoteDispatchDomainSuspend (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
                             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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1652 1653
    if (virDomainSuspend (dom) == -1) {
        virDomainFree(dom);
1654
        return -1;
1655 1656
    }
    virDomainFree(dom);
1657 1658 1659 1660
    return 0;
}

static int
1661 1662
remoteDispatchDomainUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
                              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) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1676 1677
    if (virDomainUndefine (dom) == -1) {
        virDomainFree(dom);
1678
        return -1;
1679 1680
    }
    virDomainFree(dom);
1681 1682 1683 1684
    return 0;
}

static int
1685 1686
remoteDispatchListDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
                                   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,
                             "maxnames > REMOTE_NETWORK_NAME_LIST_MAX");
        return -2;
    }

    /* Allocate return buffer. */
    ret->names.names_val = calloc (args->maxnames, sizeof (char *));

    ret->names.names_len =
        virConnectListDefinedNetworks (client->conn,
                                       ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) return -1;

    return 0;
}

static int
1711 1712
remoteDispatchListDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client,
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
                           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,
                             "maxids > REMOTE_DOMAIN_ID_LIST_MAX");
        return -2;
    }

    /* Allocate return buffer. */
    ret->ids.ids_val = calloc (args->maxids, sizeof (int));

    ret->ids.ids_len = virConnectListDomains (client->conn,
                                              ret->ids.ids_val, args->maxids);
    if (ret->ids.ids_len == -1) return -1;

    return 0;
}

static int
1736 1737
remoteDispatchListNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
                            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,
                             "maxnames > REMOTE_NETWORK_NAME_LIST_MAX");
        return -2;
    }

    /* Allocate return buffer. */
    ret->names.names_val = calloc (args->maxnames, sizeof (char *));

    ret->names.names_len =
        virConnectListNetworks (client->conn,
                                ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) return -1;

    return 0;
}

static int
1762 1763
remoteDispatchNetworkCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
                             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) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1777 1778
    if (virNetworkCreate (net) == -1) {
        virNetworkFree(net);
1779
        return -1;
1780 1781
    }
    virNetworkFree(net);
1782 1783 1784 1785
    return 0;
}

static int
1786 1787
remoteDispatchNetworkCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
                                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);
1799
    virNetworkFree(net);
1800 1801 1802 1803
    return 0;
}

static int
1804 1805
remoteDispatchNetworkDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
                                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);
1817
    virNetworkFree(net);
1818 1819 1820 1821
    return 0;
}

static int
1822 1823
remoteDispatchNetworkDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
                              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) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1837 1838
    if (virNetworkDestroy (net) == -1) {
        virNetworkFree(net);
1839
        return -1;
1840 1841
    }
    virNetworkFree(net);
1842 1843 1844 1845
    return 0;
}

static int
1846 1847
remoteDispatchNetworkDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client,
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
                              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) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
1863 1864 1865 1866 1867
    if (!ret->xml) {
        virNetworkFree(net);
        return -1;
    }
    virNetworkFree(net);
1868 1869 1870 1871
    return 0;
}

static int
1872 1873
remoteDispatchNetworkGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
                                   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) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1887 1888
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
        virNetworkFree(net);
1889
        return -1;
1890 1891
    }
    virNetworkFree(net);
1892 1893 1894 1895
    return 0;
}

static int
1896 1897
remoteDispatchNetworkGetBridgeName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
                                    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) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
1913 1914 1915 1916 1917
    if (!ret->name) {
        virNetworkFree(net);
        return -1;
    }
    virNetworkFree(net);
1918 1919 1920 1921
    return 0;
}

static int
1922 1923
remoteDispatchNetworkLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
                                   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);
1935
    virNetworkFree(net);
1936 1937 1938 1939
    return 0;
}

static int
1940 1941
remoteDispatchNetworkLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
                                   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);
1953
    virNetworkFree(net);
1954 1955 1956 1957
    return 0;
}

static int
1958 1959
remoteDispatchNetworkSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client,
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
                                   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) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1973 1974
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
        virNetworkFree(net);
1975
        return -1;
1976 1977
    }
    virNetworkFree(net);
1978 1979 1980 1981
    return 0;
}

static int
1982 1983
remoteDispatchNetworkUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
                               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) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1997 1998
    if (virNetworkUndefine (net) == -1) {
        virNetworkFree(net);
1999
        return -1;
2000 2001
    }
    virNetworkFree(net);
2002 2003 2004 2005
    return 0;
}

static int
2006 2007
remoteDispatchNumOfDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client,
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
                                    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
2021 2022
remoteDispatchNumOfDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
                            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
2036 2037
remoteDispatchNumOfNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
                             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;
}

2050 2051

static int
2052 2053
remoteDispatchAuthList (struct qemud_server *server ATTRIBUTE_UNUSED,
                        struct qemud_client *client,
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
                        remote_message_header *req ATTRIBUTE_UNUSED,
                        void *args ATTRIBUTE_UNUSED,
                        remote_auth_list_ret *ret)
{
    ret->types.types_len = 1;
    if ((ret->types.types_val = calloc (ret->types.types_len, sizeof (remote_auth_type))) == NULL) {
        remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, "auth types");
        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,
                            "Cannot resolve address %d: %s", err, gai_strerror(err));
        return NULL;
    }

    addr = malloc(strlen(host) + 1 + strlen(port) + 1);
    if (!addr) {
        remoteDispatchError(client, req, "cannot allocate address");
        return NULL;
    }

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


/*
 * Initializes the SASL session in prepare for authentication
 * and gives the client a list of allowed mechansims to choose
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
2108 2109
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
2110 2111 2112 2113 2114
                            remote_message_header *req,
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret)
{
    const char *mechlist = NULL;
2115
    sasl_security_properties_t secprops;
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
    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) {
        qemudLog(QEMUD_ERR, "client tried invalid SASL init request");
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    /* Get local address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getsockname(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
        remoteDispatchError(client, req, "failed to get sock address %d (%s)",
                            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) {
        remoteDispatchError(client, req, "failed to get peer address %d (%s)",
                            errno, strerror(errno));
        free(localAddr);
        return -2;
    }
    if ((remoteAddr = addrToString(client, req, &sa, salen)) == NULL) {
        free(localAddr);
        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);
    free(localAddr);
    free(remoteAddr);
    if (err != SASL_OK) {
        qemudLog(QEMUD_ERR, "sasl context setup failed %d (%s)",
                 err, sasl_errstring(err, NULL, NULL));
        remoteDispatchFailAuth(client, req);
        client->saslconn = NULL;
        return -2;
    }

2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
    /* 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))) {
            qemudLog(QEMUD_ERR, "cannot TLS get cipher size");
            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) {
            qemudLog(QEMUD_ERR, "cannot set SASL external SSF %d (%s)",
                     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) {
        qemudLog(QEMUD_ERR, "cannot set SASL security props %d (%s)",
                 err, sasl_errstring(err, NULL, NULL));
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -2;
    }

2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
        qemudLog(QEMUD_ERR, "cannot list SASL mechanisms %d (%s)",
                 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) {
        qemudLog(QEMUD_ERR, "cannot allocate mechlist");
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -2;
    }

    return 0;
}


2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
/* We asked for an SSF layer, so sanity check that we actaully
 * 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) {
        qemudLog(QEMUD_ERR, "cannot query SASL ssf on connection %d (%s)",
                 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 */
        qemudLog(QEMUD_ERR, "negotiated SSF %d was not strong enough", ssf);
        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;
}

2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
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) {
        qemudLog(QEMUD_ERR, "cannot query SASL username on connection %d (%s)",
                 err, sasl_errstring(err, NULL, NULL));
        remoteDispatchFailAuth(client, req);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
        qemudLog(QEMUD_ERR, "no client username was found");
        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) {
        qemudLog(QEMUD_ERR, "out of memory copying username");
        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 */
    qemudLog(QEMUD_ERR, "SASL client %s not allowed in whitelist", client->saslUsername);
    remoteDispatchFailAuth(client, req);
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
    return -1;
}


2353 2354 2355 2356
/*
 * This starts the SASL authentication negotiation.
 */
static int
2357 2358
remoteDispatchAuthSaslStart (struct qemud_server *server,
                             struct qemud_client *client,
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
                             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) {
        qemudLog(QEMUD_ERR, "client tried invalid SASL start request");
        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) {
        qemudLog(QEMUD_ERR, "sasl start failed %d (%s)",
                 err, sasl_errdetail(client->saslconn));
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        remoteDispatchFailAuth(client, req);
        return -2;
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
        qemudLog(QEMUD_ERR, "sasl start reply data too long %d", serveroutlen);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
        ret->data.data_val = malloc(serveroutlen);
        if (!ret->data.data_val) {
            remoteDispatchError (client, req, "out of memory allocating array");
            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 {
2419 2420 2421
        if (remoteSASLCheckSSF(client, req) < 0)
            return -2;

2422 2423 2424 2425
        /* Check username whitelist ACL */
        if (remoteSASLCheckAccess(server, client, req) < 0)
            return -2;

2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

    return 0;
}


static int
2436 2437
remoteDispatchAuthSaslStep (struct qemud_server *server,
                            struct qemud_client *client,
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
                            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) {
        qemudLog(QEMUD_ERR, "client tried invalid SASL start request");
        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) {
        qemudLog(QEMUD_ERR, "sasl step failed %d (%s)",
                 err, sasl_errdetail(client->saslconn));
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
        qemudLog(QEMUD_ERR, "sasl step reply data too long %d", serveroutlen);
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        remoteDispatchFailAuth(client, req);
        return -2;
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
        ret->data.data_val = malloc(serveroutlen);
        if (!ret->data.data_val) {
            remoteDispatchError (client, req, "out of memory allocating array");
            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 {
2498 2499 2500
        if (remoteSASLCheckSSF(client, req) < 0)
            return -2;

2501 2502 2503 2504
        /* Check username whitelist ACL */
        if (remoteSASLCheckAccess(server, client, req) < 0)
            return -2;

2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

    return 0;
}


#else /* HAVE_SASL */
static int
2516 2517
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
                            remote_message_header *req,
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    qemudLog(QEMUD_ERR, "client tried unsupported SASL init request");
    remoteDispatchFailAuth(client, req);
    return -1;
}

static int
2528 2529
remoteDispatchAuthSaslStart (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client,
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
                             remote_message_header *req,
                             remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                             remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
    qemudLog(QEMUD_ERR, "client tried unsupported SASL start request");
    remoteDispatchFailAuth(client, req);
    return -1;
}

static int
2540 2541
remoteDispatchAuthSaslStep (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client,
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
                            remote_message_header *req,
                            remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
    qemudLog(QEMUD_ERR, "client tried unsupported SASL step request");
    remoteDispatchFailAuth(client, req);
    return -1;
}
#endif /* HAVE_SASL */


2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
/*----- 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);
}

/* 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);
}

/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */