remote.c 102.6 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *
 * 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 <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>
43
#include <fnmatch.h>
44
#include <arpa/inet.h>
45
#include "virterror_internal.h"
46

47
#if HAVE_POLKIT0
48 49
# include <polkit/polkit.h>
# include <polkit-dbus/polkit-dbus.h>
50 51
#endif

52 53 54
#include "remote.h"
#include "dispatch.h"

55 56
#include "libvirt_internal.h"
#include "datatypes.h"
57
#include "memory.h"
58
#include "util.h"
C
Chris Lalancette 已提交
59
#include "stream.h"
60
#include "uuid.h"
61
#include "network.h"
C
Chris Lalancette 已提交
62
#include "libvirt/libvirt-qemu.h"
63
#include "command.h"
64

65
#define VIR_FROM_THIS VIR_FROM_REMOTE
66

67 68 69 70
#define virNetError(code, ...)                                    \
    virReportErrorHelper(VIR_FROM_THIS, code, __FILE__,           \
                         __FUNCTION__, __LINE__, __VA_ARGS__)

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#if SIZEOF_LONG < 8
# define HYPER_TO_TYPE(_type, _to, _from)                                     \
    do {                                                                      \
        if ((_from) != (_type)(_from)) {                                      \
            virNetError(VIR_ERR_INTERNAL_ERROR,                               \
                        _("conversion from hyper to %s overflowed"), #_type); \
            goto cleanup;                                                     \
        }                                                                     \
        (_to) = (_from);                                                      \
    } while (0)

# define HYPER_TO_LONG(_to, _from) HYPER_TO_TYPE(long, _to, _from)
# define HYPER_TO_ULONG(_to, _from) HYPER_TO_TYPE(unsigned long, _to, _from)
#else
# define HYPER_TO_LONG(_to, _from) (_to) = (_from)
# define HYPER_TO_ULONG(_to, _from) (_to) = (_from)
#endif

89 90 91 92 93 94 95
static virDomainPtr get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network(virConnectPtr conn, remote_nonnull_network network);
static virInterfacePtr get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface);
static virStoragePoolPtr get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool);
static virStorageVolPtr get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol);
static virSecretPtr get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret);
static virNWFilterPtr get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
96
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
97 98 99 100 101 102 103 104 105
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);
static void make_nonnull_interface(remote_nonnull_interface *interface_dst, virInterfacePtr interface_src);
static void make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src);
static void make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src);
static void make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src);
static void make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src);
static void make_nonnull_nwfilter(remote_nonnull_nwfilter *net_dst, virNWFilterPtr nwfilter_src);
static void make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
106

107

108
#include "remote_dispatch_prototypes.h"
C
Chris Lalancette 已提交
109
#include "qemu_dispatch_prototypes.h"
110 111 112 113

static const dispatch_data const dispatch_table[] = {
#include "remote_dispatch_table.h"
};
114

C
Chris Lalancette 已提交
115 116 117 118
static const dispatch_data const qemu_dispatch_table[] = {
#include "qemu_dispatch_table.h"
};

119 120 121 122 123 124 125 126 127 128
const dispatch_data const *remoteGetDispatchData(int proc)
{
    if (proc >= ARRAY_CARDINALITY(dispatch_table) ||
        dispatch_table[proc].fn == NULL) {
        return NULL;
    }

    return &(dispatch_table[proc]);
}

C
Chris Lalancette 已提交
129 130 131 132 133 134 135 136 137 138
const dispatch_data const *qemuGetDispatchData(int proc)
{
    if (proc >= ARRAY_CARDINALITY(qemu_dispatch_table) ||
        qemu_dispatch_table[proc].fn == NULL) {
        return NULL;
    }

    return &(qemu_dispatch_table[proc]);
}

139 140
/* Prototypes */
static void
141 142 143 144
remoteDispatchDomainEventSend(struct qemud_client *client,
                              int procnr,
                              xdrproc_t proc,
                              void *data);
145

146 147 148 149 150
static int remoteRelayDomainEventLifecycle(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           int event,
                                           int detail,
                                           void *opaque)
151 152
{
    struct qemud_client *client = opaque;
153
    remote_domain_event_lifecycle_msg data;
154

155 156 157
    if (!client)
        return -1;

158
    VIR_DEBUG("Relaying domain lifecycle event %d %d", event, detail);
159

160
    virMutexLock(&client->lock);
161

162 163
    /* build return data */
    memset(&data, 0, sizeof data);
164
    make_nonnull_domain(&data.dom, dom);
165 166
    data.event = event;
    data.detail = detail;
167

168 169 170
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_domain_event_lifecycle_msg, &data);
171 172

    virMutexUnlock(&client->lock);
173

174 175
    return 0;
}
176

177 178 179 180 181 182 183 184 185 186
static int remoteRelayDomainEventReboot(virConnectPtr conn ATTRIBUTE_UNUSED,
                                        virDomainPtr dom,
                                        void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_reboot_msg data;

    if (!client)
        return -1;

187
    VIR_DEBUG("Relaying domain reboot event %s %d", dom->name, dom->id);
188 189 190 191 192

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
193
    make_nonnull_domain(&data.dom, dom);
194

195 196 197
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                  (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);
198 199 200 201 202 203

    virMutexUnlock(&client->lock);

    return 0;
}

204

205 206 207 208 209 210 211 212 213 214 215
static int remoteRelayDomainEventRTCChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           long long offset,
                                           void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_rtc_change_msg data;

    if (!client)
        return -1;

216
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld", dom->name, dom->id, offset);
217 218 219 220 221

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
222
    make_nonnull_domain(&data.dom, dom);
223 224
    data.offset = offset;

225 226 227
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);
228 229 230 231 232 233 234

    virMutexUnlock(&client->lock);

    return 0;
}


235 236 237 238 239 240 241 242 243 244 245
static int remoteRelayDomainEventWatchdog(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          int action,
                                          void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_watchdog_msg data;

    if (!client)
        return -1;

246
    VIR_DEBUG("Relaying domain watchdog event %s %d %d", dom->name, dom->id, action);
247 248 249 250 251

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
252
    make_nonnull_domain(&data.dom, dom);
253 254
    data.action = action;

255 256 257
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                  (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);
258 259 260 261 262 263 264

    virMutexUnlock(&client->lock);

    return 0;
}


265 266 267 268 269 270 271 272 273 274 275 276 277
static int remoteRelayDomainEventIOError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                         virDomainPtr dom,
                                         const char *srcPath,
                                         const char *devAlias,
                                         int action,
                                         void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_io_error_msg data;

    if (!client)
        return -1;

278
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d", dom->name, dom->id, srcPath, devAlias, action);
279 280 281 282 283

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
284
    make_nonnull_domain(&data.dom, dom);
285 286 287 288
    data.srcPath = (char*)srcPath;
    data.devAlias = (char*)devAlias;
    data.action = action;

289 290 291
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);
292 293 294 295 296 297 298

    virMutexUnlock(&client->lock);

    return 0;
}


299 300 301 302 303 304 305 306 307 308 309 310 311 312
static int remoteRelayDomainEventIOErrorReason(virConnectPtr conn ATTRIBUTE_UNUSED,
                                               virDomainPtr dom,
                                               const char *srcPath,
                                               const char *devAlias,
                                               int action,
                                               const char *reason,
                                               void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_io_error_reason_msg data;

    if (!client)
        return -1;

313 314
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s",
              dom->name, dom->id, srcPath, devAlias, action, reason);
315 316 317 318 319

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
320
    make_nonnull_domain(&data.dom, dom);
321 322 323 324 325
    data.srcPath = (char*)srcPath;
    data.devAlias = (char*)devAlias;
    data.action = action;
    data.reason = (char*)reason;

326 327 328
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);
329 330 331 332 333 334 335

    virMutexUnlock(&client->lock);

    return 0;
}


336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
static int remoteRelayDomainEventGraphics(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          int phase,
                                          virDomainEventGraphicsAddressPtr local,
                                          virDomainEventGraphicsAddressPtr remote,
                                          const char *authScheme,
                                          virDomainEventGraphicsSubjectPtr subject,
                                          void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_graphics_msg data;
    int i;

    if (!client)
        return -1;

352 353 354 355
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s", dom->name, dom->id, phase,
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
              authScheme);
356

357
    VIR_DEBUG("Subject %d", subject->nidentity);
358
    for (i = 0 ; i < subject->nidentity ; i++) {
359
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
360 361 362 363 364 365
    }

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
366
    make_nonnull_domain(&data.dom, dom);
367 368 369 370 371 372 373 374 375 376 377 378 379
    data.phase = phase;
    data.authScheme = (char*)authScheme;

    data.local.family = local->family;
    data.local.node = (char *)local->node;
    data.local.service = (char *)local->service;

    data.remote.family = remote->family;
    data.remote.node = (char*)remote->node;
    data.remote.service = (char*)remote->service;

    data.subject.subject_len = subject->nidentity;
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0) {
380
        VIR_WARN("cannot allocate memory for graphics event subject");
381 382 383 384 385 386 387
        return -1;
    }
    for (i = 0 ; i < data.subject.subject_len ; i++) {
        data.subject.subject_val[i].type = (char*)subject->identities[i].type;
        data.subject.subject_val[i].name = (char*)subject->identities[i].name;
    }

388 389 390
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                  (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
391 392 393 394 395 396 397 398 399

    VIR_FREE(data.subject.subject_val);

    virMutexUnlock(&client->lock);

    return 0;
}


400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
static int remoteRelayDomainEventControlError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                              virDomainPtr dom,
                                              void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_control_error_msg data;

    if (!client)
        return -1;

    VIR_DEBUG("Relaying domain control error %s %d", dom->name, dom->id);

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_control_error_msg, &data);

    virMutexUnlock(&client->lock);

    return 0;
}


428
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
429
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
430
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
431
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
432
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
433
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
434
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
435
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
436
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
437 438 439 440
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

441 442 443
/*----- Functions. -----*/

static int
444 445 446 447 448 449
remoteDispatchOpen(struct qemud_server *server,
                   struct qemud_client *client,
                   virConnectPtr conn,
                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                   remote_error *rerr,
                   struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
450 451
{
    const char *name;
452 453
    int flags;
    int rv = -1;
454

455 456 457
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
458

459 460 461 462 463
    if (conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
        goto cleanup;
    }

464 465 466 467 468 469 470 471 472 473
    name = args->name ? *args->name : NULL;

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

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

477
    if (client->conn == NULL)
478 479 480
        goto cleanup;

    rv = 0;
481

482 483 484
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
485
    virMutexUnlock(&client->lock);
486
    return rv;
487 488 489 490
}


static int
491 492 493 494 495 496
remoteDispatchClose(struct qemud_server *server ATTRIBUTE_UNUSED,
                    struct qemud_client *client ATTRIBUTE_UNUSED,
                    virConnectPtr conn ATTRIBUTE_UNUSED,
                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                    remote_error *rerr ATTRIBUTE_UNUSED,
                    void *args ATTRIBUTE_UNUSED, void *ret ATTRIBUTE_UNUSED)
497
{
498 499 500
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
501

502
    client->closing = 1;
503

504
    virMutexUnlock(&client->lock);
505
    return 0;
506 507
}

508
static int
509 510 511 512 513 514 515
remoteDispatchDomainGetSchedulerType(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr,
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
516
{
517
    virDomainPtr dom = NULL;
518 519
    char *type;
    int nparams;
520
    int rv = -1;
521

522
    if (!conn) {
523 524
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
525 526
    }

527
    if (!(dom = get_nonnull_domain(conn, args->dom)))
528
        goto cleanup;
529

530
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
531
        goto cleanup;
532 533 534

    ret->type = type;
    ret->nparams = nparams;
535 536 537 538 539 540 541 542
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
543 544
}

545 546 547 548
/* Helper to serialize typed parameters. */
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
549 550
                               remote_typed_param **ret_params_val,
                               u_int *ret_params_len)
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
{
    int i;
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
    if (VIR_ALLOC_N(val, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
        val[i].field = strdup (params[i].field);
        if (val[i].field == NULL) {
            virReportOOMError();
            goto cleanup;
        }
        val[i].value.type = params[i].type;
        switch (params[i].type) {
        case VIR_TYPED_PARAM_INT:
            val[i].value.remote_typed_param_value_u.i = params[i].value.i;
            break;
        case VIR_TYPED_PARAM_UINT:
            val[i].value.remote_typed_param_value_u.ui = params[i].value.ui;
            break;
        case VIR_TYPED_PARAM_LLONG:
            val[i].value.remote_typed_param_value_u.l = params[i].value.l;
            break;
        case VIR_TYPED_PARAM_ULLONG:
            val[i].value.remote_typed_param_value_u.ul = params[i].value.ul;
            break;
        case VIR_TYPED_PARAM_DOUBLE:
            val[i].value.remote_typed_param_value_u.d = params[i].value.d;
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
            val[i].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        default:
            virNetError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                        params[i].type);
            goto cleanup;
        }
    }

    *ret_params_val = val;
    val = NULL;
    rv = 0;

cleanup:
    if (val) {
        for (i = 0; i < nparams; i++)
            VIR_FREE(val[i].field);
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
611 612
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
613 614 615 616 617 618 619 620 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 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
                                 int limit,
                                 int *nparams)
{
    int i;
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
    if (args_params_len > limit) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, args_params_len) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    *nparams = args_params_len;

    /* Deserialise the result. */
    for (i = 0; i < args_params_len; ++i) {
        if (virStrcpyStatic(params[i].field,
                            args_params_val[i].field) == NULL) {
            virNetError(VIR_ERR_INTERNAL_ERROR,
                        _("Parameter %s too big for destination"),
                        args_params_val[i].field);
            goto cleanup;
        }
        params[i].type = args_params_val[i].value.type;
        switch (params[i].type) {
        case VIR_TYPED_PARAM_INT:
            params[i].value.i =
                args_params_val[i].value.remote_typed_param_value_u.i;
            break;
        case VIR_TYPED_PARAM_UINT:
            params[i].value.ui =
                args_params_val[i].value.remote_typed_param_value_u.ui;
            break;
        case VIR_TYPED_PARAM_LLONG:
            params[i].value.l =
                args_params_val[i].value.remote_typed_param_value_u.l;
            break;
        case VIR_TYPED_PARAM_ULLONG:
            params[i].value.ul =
                args_params_val[i].value.remote_typed_param_value_u.ul;
            break;
        case VIR_TYPED_PARAM_DOUBLE:
            params[i].value.d =
                args_params_val[i].value.remote_typed_param_value_u.d;
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
            params[i].value.b =
                args_params_val[i].value.remote_typed_param_value_u.b;
            break;
        default:
            virNetError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                        params[i].type);
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
    if (rv < 0)
        VIR_FREE(params);
    return params;
}

682
static int
683 684 685 686 687 688 689
remoteDispatchDomainGetSchedulerParameters(struct qemud_server *server ATTRIBUTE_UNUSED,
                                           struct qemud_client *client ATTRIBUTE_UNUSED,
                                           virConnectPtr conn,
                                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                                           remote_error *rerr,
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
690
{
691
    virDomainPtr dom = NULL;
692
    virTypedParameterPtr params = NULL;
693
    int nparams = args->nparams;
694
    int rv = -1;
695

696
    if (!conn) {
697 698
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
699 700
    }

701
    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
702 703
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
704
    }
705 706
    if (VIR_ALLOC_N(params, nparams) < 0)
        goto no_memory;
707

708
    if (!(dom = get_nonnull_domain(conn, args->dom)))
709
        goto cleanup;
710

711
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
712
        goto cleanup;
713

714
    if (remoteSerializeTypedParameters(params, nparams,
715 716
                                       &ret->params.params_val,
                                       &ret->params.params_len) < 0)
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 759 760 761 762 763 764 765 766 767
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    VIR_FREE(params);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

static int
remoteDispatchDomainGetSchedulerParametersFlags(struct qemud_server *server ATTRIBUTE_UNUSED,
                                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                                virConnectPtr conn,
                                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                                remote_error *rerr,
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = args->nparams;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0)
        goto no_memory;

    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

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

    if (remoteSerializeTypedParameters(params, nparams,
768 769
                                       &ret->params.params_val,
                                       &ret->params.params_len) < 0)
770
        goto cleanup;
771

772
    rv = 0;
773 774

cleanup:
775
    if (rv < 0)
776 777 778
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
779
    VIR_FREE(params);
780 781 782 783 784
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
785 786
}

787
static int
788 789 790 791 792 793 794
remoteDispatchDomainMemoryStats(struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *rerr,
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
795
{
796
    virDomainPtr dom = NULL;
797
    struct _virDomainMemoryStat *stats;
798
    int nr_stats, i;
799
    int rv = -1;
800

801
    if (!conn) {
802 803
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
804 805
    }

806
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
807 808 809
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
        goto cleanup;
810 811
    }

812
    if (!(dom = get_nonnull_domain(conn, args->dom)))
813
        goto cleanup;
814 815 816

    /* Allocate stats array for making dispatch call */
    if (VIR_ALLOC_N(stats, args->maxStats) < 0) {
817 818
        virReportOOMError();
        goto cleanup;
819
    }
820

821
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, 0);
822
    if (nr_stats < 0)
823
        goto cleanup;
824 825 826

    /* Allocate return buffer */
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0) {
827 828
        virReportOOMError();
        goto cleanup;
829 830 831 832 833 834 835 836
    }

    /* Copy the stats into the xdr return structure */
    for (i = 0; i < nr_stats; i++) {
        ret->stats.stats_val[i].tag = stats[i].tag;
        ret->stats.stats_val[i].val = stats[i].val;
    }
    ret->stats.stats_len = nr_stats;
837 838 839 840 841 842 843
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
844
    VIR_FREE(stats);
845
    return rv;
846 847
}

848
static int
849 850 851 852 853 854 855
remoteDispatchDomainBlockPeek(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
856
{
857
    virDomainPtr dom = NULL;
858 859 860 861
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
862
    int rv = -1;
863

864
    if (!conn) {
865 866
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
867 868
    }

869
    if (!(dom = get_nonnull_domain(conn, args->dom)))
870
        goto cleanup;
871 872 873 874 875 876
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
877 878 879
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("size > maximum buffer size"));
        goto cleanup;
880 881 882
    }

    ret->buffer.buffer_len = size;
883
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
884 885
        virReportOOMError();
        goto cleanup;
886 887
    }

888
    if (virDomainBlockPeek(dom, path, offset, size,
889
                           ret->buffer.buffer_val, flags) < 0)
890
        goto cleanup;
891

892 893 894 895 896 897 898 899 900 901
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
902 903
}

R
Richard W.M. Jones 已提交
904
static int
905 906 907 908 909 910 911
remoteDispatchDomainMemoryPeek(struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *rerr,
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
912
{
913
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
914 915 916
    unsigned long long offset;
    size_t size;
    unsigned int flags;
917
    int rv = -1;
R
Richard W.M. Jones 已提交
918

919
    if (!conn) {
920 921
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
922 923
    }

924
    if (!(dom = get_nonnull_domain(conn, args->dom)))
925
        goto cleanup;
R
Richard W.M. Jones 已提交
926 927 928 929 930
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
931 932 933
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("size > maximum buffer size"));
        goto cleanup;
R
Richard W.M. Jones 已提交
934 935 936
    }

    ret->buffer.buffer_len = size;
937
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
938 939
        virReportOOMError();
        goto cleanup;
R
Richard W.M. Jones 已提交
940 941
    }

942
    if (virDomainMemoryPeek(dom, offset, size,
943
                            ret->buffer.buffer_val, flags) < 0)
944
        goto cleanup;
R
Richard W.M. Jones 已提交
945

946 947 948 949 950 951 952 953 954 955
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
956 957
}

958
static int
959 960 961 962 963 964 965
remoteDispatchDomainGetSecurityLabel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr,
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
966
{
967 968
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
969 970
    int rv = -1;

971
    if (!conn) {
972 973
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
974 975
    }

976 977 978 979 980 981 982 983 984 985 986 987 988 989
    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

    if (VIR_ALLOC(seclabel) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    if (virDomainGetSecurityLabel(dom, seclabel) < 0)
        goto cleanup;

    ret->label.label_len = strlen(seclabel->label) + 1;
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0) {
        virReportOOMError();
990
        goto cleanup;
991 992 993
    }
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
994

995 996 997 998 999
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
1000 1001 1002
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1003
    return rv;
1004 1005 1006
}

static int
1007 1008 1009 1010 1011 1012 1013
remoteDispatchNodeGetSecurityModel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *rerr,
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_node_get_security_model_ret *ret)
1014
{
1015
    virSecurityModel secmodel;
1016 1017
    int rv = -1;

1018
    if (!conn) {
1019 1020
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1021 1022
    }

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    memset(&secmodel, 0, sizeof secmodel);
    if (virNodeGetSecurityModel(conn, &secmodel) < 0)
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0) {
        virReportOOMError();
        goto cleanup;
    }
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0) {
        virReportOOMError();
1037
        goto cleanup;
1038 1039
    }
    strcpy(ret->doi.doi_val, secmodel.doi);
1040

1041 1042 1043 1044 1045 1046
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
1047 1048
}

1049
static int
1050 1051 1052 1053 1054 1055 1056
remoteDispatchDomainGetVcpus(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1057
{
1058
    virDomainPtr dom = NULL;
1059 1060 1061
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
    int info_len, i;
1062
    int rv = -1;
1063

1064
    if (!conn) {
1065 1066
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1067 1068
    }

1069
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1070
        goto cleanup;
1071

1072 1073
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1074
        goto cleanup;
1075
    }
1076

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto no_memory;
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
        goto no_memory;

    if ((info_len = virDomainGetVcpus(dom,
                                      info, args->maxinfo,
                                      cpumaps, args->maplen)) < 0)
        goto cleanup;

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto no_memory;

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

    rv = 0;
1115 1116

cleanup:
1117
    if (rv < 0) {
1118
        remoteDispatchError(rerr);
1119 1120 1121 1122
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
1123 1124 1125
    if (dom)
        virDomainFree(dom);
    return rv;
1126 1127 1128 1129

no_memory:
    virReportOOMError();
    goto cleanup;
1130 1131 1132
}

static int
1133 1134 1135 1136 1137 1138 1139
remoteDispatchDomainMigratePrepare(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *rerr,
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
1140
{
1141 1142 1143 1144 1145
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1146
    int rv = -1;
1147

1148
    if (!conn) {
1149 1150
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1151 1152
    }

1153 1154 1155 1156 1157 1158
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
        virReportOOMError();
1159
        goto cleanup;
1160
    }
1161

1162 1163 1164
    if (virDomainMigratePrepare(conn, &cookie, &cookielen,
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
1165
        goto cleanup;
1166

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie.cookie_len = cookielen;
    ret->cookie.cookie_val = cookie;
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
    } else {
        ret->uri_out = uri_out;
        uri_out = NULL;
    }
1178

1179
    rv = 0;
1180

1181 1182 1183
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
1184
    VIR_FREE(uri_out);
1185
    return rv;
1186 1187
}

1188
static int
1189 1190 1191 1192 1193 1194 1195
remoteDispatchDomainMigratePrepare2(struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
1196
{
1197 1198 1199 1200 1201
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1202
    int rv = -1;
1203

1204
    if (!conn) {
1205 1206
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1207 1208
    }

1209 1210
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
1211

1212 1213
    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
1214 1215
        virReportOOMError();
        goto cleanup;
1216 1217
    }

1218 1219 1220 1221
    if (virDomainMigratePrepare2(conn, &cookie, &cookielen,
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
1222
        goto cleanup;
1223

1224 1225 1226 1227 1228 1229
    /* 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;
1230

1231 1232 1233 1234 1235 1236
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
1237 1238
}

1239
static int
1240 1241 1242 1243 1244 1245 1246
remoteDispatchDomainPinVcpu(struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *rerr,
                            remote_domain_pin_vcpu_args *args,
                            void *ret ATTRIBUTE_UNUSED)
1247
{
1248
    virDomainPtr dom = NULL;
1249
    int rv = -1;
1250 1251

    if (!conn) {
1252 1253
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1254
    }
1255

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        goto cleanup;
    }

    if (virDomainPinVcpu(dom, args->vcpu,
                         (unsigned char *) args->cpumap.cpumap_val,
                         args->cpumap.cpumap_len) < 0)
1267
        goto cleanup;
1268

1269 1270 1271 1272 1273
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
1274 1275
    if (dom)
        virDomainFree(dom);
1276
    return rv;
1277 1278
}

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
static int
remoteDispatchDomainPinVcpuFlags(struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_pin_vcpu_flags_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom = NULL;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        goto cleanup;
    }

    if (virDomainPinVcpuFlags(dom,
                              args->vcpu,
                              (unsigned char *) args->cpumap.cpumap_val,
                              args->cpumap.cpumap_len,
                              args->flags) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

C
Chris Lalancette 已提交
1321
static int
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
remoteDispatchDomainGetMemoryParameters(struct qemud_server *server
                                        ATTRIBUTE_UNUSED,
                                        struct qemud_client *client
                                        ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *
                                        hdr ATTRIBUTE_UNUSED,
                                        remote_error * rerr,
                                        remote_domain_get_memory_parameters_args
                                        * args,
                                        remote_domain_get_memory_parameters_ret
                                        * ret)
C
Chris Lalancette 已提交
1334
{
1335
    virDomainPtr dom = NULL;
1336
    virTypedParameterPtr params = NULL;
1337 1338
    int nparams = args->nparams;
    unsigned int flags;
1339
    int rv = -1;
1340 1341

    if (!conn) {
1342 1343
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1344
    }
C
Chris Lalancette 已提交
1345

1346
    flags = args->flags;
C
Chris Lalancette 已提交
1347

1348 1349 1350 1351 1352
    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
1353 1354
        virReportOOMError();
        goto cleanup;
C
Chris Lalancette 已提交
1355 1356
    }

1357
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1358
        goto cleanup;
C
Chris Lalancette 已提交
1359

1360
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
1361
        goto cleanup;
C
Chris Lalancette 已提交
1362

1363 1364 1365 1366 1367 1368
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
1369 1370
    }

1371
    if (remoteSerializeTypedParameters(params, nparams,
1372 1373
                                       &ret->params.params_val,
                                       &ret->params.params_len) < 0)
1374
        goto cleanup;
1375

1376
success:
1377 1378 1379
    rv = 0;

cleanup:
1380
    if (rv < 0)
1381 1382 1383
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
1384
    VIR_FREE(params);
1385
    return rv;
1386 1387
}

1388
static int
1389
remoteDispatchDomainGetBlkioParameters(struct qemud_server *server
1390 1391 1392 1393 1394 1395 1396
                                        ATTRIBUTE_UNUSED,
                                        struct qemud_client *client
                                        ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *
                                        hdr ATTRIBUTE_UNUSED,
                                        remote_error * rerr,
1397
                                        remote_domain_get_blkio_parameters_args
1398
                                        * args,
1399
                                        remote_domain_get_blkio_parameters_ret
1400 1401
                                        * ret)
{
1402
    virDomainPtr dom = NULL;
1403
    virTypedParameterPtr params = NULL;
1404
    int nparams = args->nparams;
1405
    unsigned int flags;
1406
    int rv = -1;
1407

1408
    if (!conn) {
1409 1410
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1411 1412
    }

1413 1414
    flags = args->flags;

1415
    if (nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
1416 1417
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
1418 1419
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
1420 1421
        virReportOOMError();
        goto cleanup;
1422 1423
    }

1424
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1425
        goto cleanup;
1426

1427
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
1428
        goto cleanup;
1429

1430 1431 1432 1433 1434 1435 1436 1437
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

1438
    if (remoteSerializeTypedParameters(params, nparams,
1439 1440
                                       &ret->params.params_val,
                                       &ret->params.params_len) < 0)
1441
        goto cleanup;
1442

1443 1444
success:
    rv = 0;
1445

1446
cleanup:
1447
    if (rv < 0)
1448
        remoteDispatchError(rerr);
1449
    VIR_FREE(params);
1450 1451 1452
    if (dom)
        virDomainFree(dom);
    return rv;
1453 1454
}

1455
static int
1456 1457 1458 1459 1460 1461 1462
remoteDispatchDomainScreenshot(struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client,
                               virConnectPtr conn,
                               remote_message_header *hdr,
                               remote_error *rerr,
                               remote_domain_screenshot_args *args,
                               remote_domain_screenshot_ret *ret)
1463 1464 1465
{
    int rv = -1;
    struct qemud_client_stream *stream = NULL;
1466
    virDomainPtr dom = NULL;
1467 1468
    char *mime, **mime_p;

1469 1470 1471 1472 1473
    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

1474 1475
    ret->mime = NULL;

1476 1477
    if (!(dom = get_nonnull_domain (conn, args->dom)))
        goto cleanup;
1478

1479 1480
    if (!(stream = remoteCreateClientStream(conn, hdr)))
        goto cleanup;
1481

1482 1483
    if (!(mime = virDomainScreenshot(dom, stream->st, args->screen, args->flags)))
        goto cleanup;
1484 1485 1486

    if (remoteAddClientStream(client, stream, 1) < 0) {
        virStreamAbort(stream->st);
1487
        goto cleanup;
1488 1489 1490
    }

    if (VIR_ALLOC(mime_p) < 0) {
1491
        virReportOOMError();
1492 1493 1494 1495 1496
        goto cleanup;
    }

    *mime_p = strdup(mime);
    if (*mime_p == NULL) {
1497
        virReportOOMError();
E
Eric Blake 已提交
1498
        VIR_FREE(mime_p);
1499 1500 1501 1502
        goto cleanup;
    }

    ret->mime = mime_p;
1503

1504 1505
    rv = 0;

1506
cleanup:
1507 1508
    if (rv < 0)
        remoteDispatchError(rerr);
1509
    VIR_FREE(mime);
1510 1511 1512 1513
    if (dom)
        virDomainFree(dom);
    if (stream && rv != 0) {
        virStreamAbort(stream->st);
1514
        remoteFreeClientStream(client, stream);
1515
    }
1516 1517 1518
    return rv;
}

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
static int
remoteDispatchNodeGetCPUStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *rerr,
                               remote_node_get_cpu_stats_args *args,
                               remote_node_get_cpu_stats_ret *ret)
{
    virCPUStatsPtr params = NULL;
    int i;
    int cpuNum = args->cpuNum;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

    if (nparams > REMOTE_NODE_CPU_STATS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    if (virNodeGetCPUStats(conn, cpuNum, params, &nparams, flags) < 0)
        goto cleanup;

    /* In this case, we need to send back the number of stats
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    /* Serialise the memory parameters. */
    ret->params.params_len = nparams;
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto no_memory;

    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)
            goto no_memory;

        ret->params.params_val[i].value = params[i].value;
    }

success:
    rv = 0;

1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        if (ret->params.params_val) {
            for (i = 0; i < nparams; i++)
                VIR_FREE(ret->params.params_val[i].field);
            VIR_FREE(ret->params.params_val);
        }
    }
    VIR_FREE(params);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

static int
remoteDispatchNodeGetMemoryStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_node_get_memory_stats_args *args,
                                  remote_node_get_memory_stats_ret *ret)
{
    virMemoryStatsPtr params = NULL;
    int i;
    int cellNum = args->cellNum;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

    if (nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    if (virNodeGetMemoryStats(conn, cellNum, params, &nparams, flags) < 0)
        goto cleanup;

    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    /* Serialise the memory parameters. */
    ret->params.params_len = nparams;
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto no_memory;

    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)
            goto no_memory;

        ret->params.params_val[i].value = params[i].value;
    }

success:
    rv = 0;

1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        if (ret->params.params_val) {
            for (i = 0; i < nparams; i++)
                VIR_FREE(ret->params.params_val[i].field);
            VIR_FREE(ret->params.params_val);
        }
    }
    VIR_FREE(params);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
static int
remoteDispatchDomainBlockPull(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error * rerr,
                              remote_domain_block_pull_args *args,
                              remote_domain_block_pull_ret *ret)
{
    virDomainPtr dom = NULL;
    virDomainBlockPullInfo tmp;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

    if (virDomainBlockPull(dom, args->path, &tmp, args->flags) < 0)
        goto cleanup;
    ret->cur = tmp.cur;
    ret->end = tmp.end;
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

static int
remoteDispatchDomainGetBlockPullInfo(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error * rerr,
                                     remote_domain_get_block_pull_info_args *args,
                                     remote_domain_get_block_pull_info_ret *ret)
{
    virDomainPtr dom = NULL;
    virDomainBlockPullInfo tmp;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

    if (virDomainGetBlockPullInfo(dom, args->path, &tmp, args->flags) < 0)
        goto cleanup;
    ret->cur = tmp.cur;
    ret->end = tmp.end;
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


D
Daniel Veillard 已提交
1744 1745
/*-------------------------------------------------------------*/

1746
static int
1747 1748 1749 1750 1751 1752 1753
remoteDispatchAuthList(struct qemud_server *server,
                       struct qemud_client *client,
                       virConnectPtr conn ATTRIBUTE_UNUSED,
                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                       remote_error *rerr,
                       void *args ATTRIBUTE_UNUSED,
                       remote_auth_list_ret *ret)
1754
{
1755 1756
    int rv = -1;

1757
    ret->types.types_len = 1;
1758
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
1759 1760
        virReportOOMError();
        goto cleanup;
1761
    }
1762 1763 1764
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
1765
    ret->types.types_val[0] = client->auth;
1766
    virMutexUnlock(&client->lock);
1767

1768 1769 1770 1771 1772 1773
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
1774 1775 1776 1777 1778 1779
}


#if HAVE_SASL
/*
 * Initializes the SASL session in prepare for authentication
1780
 * and gives the client a list of allowed mechanisms to choose
1781 1782 1783 1784
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
1785 1786
remoteDispatchAuthSaslInit(struct qemud_server *server,
                           struct qemud_client *client,
1787
                           virConnectPtr conn ATTRIBUTE_UNUSED,
1788 1789 1790 1791
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           void *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_init_ret *ret)
1792 1793
{
    const char *mechlist = NULL;
1794
    sasl_security_properties_t secprops;
1795
    int err;
1796
    virSocketAddr sa;
1797 1798
    char *localAddr, *remoteAddr;

1799 1800 1801
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
1802

1803
    VIR_DEBUG("Initialize SASL auth %d", client->fd);
1804 1805
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
1806
        VIR_ERROR(_("client tried invalid SASL init request"));
1807
        goto authfail;
1808 1809 1810
    }

    /* Get local address in form  IPADDR:PORT */
1811 1812
    sa.len = sizeof(sa.data.stor);
    if (getsockname(client->fd, &sa.data.sa, &sa.len) < 0) {
1813
        char ebuf[1024];
1814 1815 1816
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    _("failed to get sock address: %s"),
                    virStrerror(errno, ebuf, sizeof ebuf));
1817
        goto error;
1818
    }
1819
    if ((localAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL)
1820
        goto error;
1821 1822

    /* Get remote address in form  IPADDR:PORT */
1823 1824
    sa.len = sizeof(sa.data.stor);
    if (getpeername(client->fd, &sa.data.sa, &sa.len) < 0) {
1825
        char ebuf[1024];
1826 1827
        virNetError(VIR_ERR_INTERNAL_ERROR, _("failed to get peer address: %s"),
                    virStrerror(errno, ebuf, sizeof ebuf));
1828
        VIR_FREE(localAddr);
1829
        goto error;
1830
    }
1831
    if ((remoteAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL) {
1832
        VIR_FREE(localAddr);
1833
        goto error;
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
1844 1845
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
1846
    if (err != SASL_OK) {
1847 1848
        VIR_ERROR(_("sasl context setup failed %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
1849
        client->saslconn = NULL;
1850
        goto authfail;
1851 1852
    }

1853 1854 1855 1856 1857 1858 1859
    /* 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))) {
1860
            VIR_ERROR(_("cannot get TLS cipher size"));
1861 1862
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
1863
            goto authfail;
1864 1865 1866 1867 1868
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
1869 1870
            VIR_ERROR(_("cannot set SASL external SSF %d (%s)"),
                      err, sasl_errstring(err, NULL, NULL));
1871 1872
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
1873
            goto authfail;
1874 1875 1876
        }
    }

1877
    memset(&secprops, 0, sizeof secprops);
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
    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) {
1897 1898
        VIR_ERROR(_("cannot set SASL security props %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
1899 1900
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
1901
        goto authfail;
1902 1903
    }

1904 1905 1906 1907 1908 1909 1910 1911 1912
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
1913 1914
        VIR_ERROR(_("cannot list SASL mechanisms %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
1915 1916
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
1917
        goto authfail;
1918
    }
1919
    VIR_DEBUG("Available mechanisms for client: '%s'", mechlist);
1920 1921
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
1922
        VIR_ERROR(_("cannot allocate mechlist"));
1923 1924
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
1925
        goto authfail;
1926 1927
    }

1928
    virMutexUnlock(&client->lock);
1929
    return 0;
1930 1931 1932 1933

authfail:
    remoteDispatchAuthError(rerr);
error:
1934
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
1935
    virMutexUnlock(&client->lock);
1936
    return -1;
1937 1938 1939
}


1940
/* We asked for an SSF layer, so sanity check that we actually
1941 1942 1943
 * got what we asked for
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
1944
static int
1945 1946
remoteSASLCheckSSF(struct qemud_client *client,
                   remote_error *rerr) {
1947 1948 1949 1950 1951 1952 1953 1954 1955
    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) {
1956 1957
        VIR_ERROR(_("cannot query SASL ssf on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
1958
        remoteDispatchAuthError(rerr);
1959 1960 1961 1962 1963
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    ssf = *(const int *)val;
1964
    VIR_DEBUG("negotiated an SSF of %d", ssf);
1965
    if (ssf < 56) { /* 56 is good for Kerberos */
1966
        VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
1967
        remoteDispatchAuthError(rerr);
1968 1969
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
1970
        return -2;
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
    }

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

1985 1986 1987
/*
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
1988
static int
1989 1990 1991
remoteSASLCheckAccess(struct qemud_server *server,
                      struct qemud_client *client,
                      remote_error *rerr) {
1992 1993 1994 1995 1996 1997
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
1998 1999
        VIR_ERROR(_("cannot query SASL username on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
2000
        remoteDispatchAuthError(rerr);
2001 2002 2003 2004 2005
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
2006
        VIR_ERROR(_("no client username was found"));
2007
        remoteDispatchAuthError(rerr);
2008 2009 2010 2011
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
2012
    VIR_DEBUG("SASL client username %s", (const char *)val);
2013 2014 2015

    client->saslUsername = strdup((const char*)val);
    if (client->saslUsername == NULL) {
2016
        VIR_ERROR(_("out of memory copying username"));
2017
        remoteDispatchAuthError(rerr);
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
        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) {
2029
        if (fnmatch(*wildcards, client->saslUsername, 0) == 0)
2030 2031 2032 2033 2034
            return 0; /* Allowed */
        wildcards++;
    }

    /* Denied */
2035
    VIR_ERROR(_("SASL client %s not allowed in whitelist"), client->saslUsername);
2036
    remoteDispatchAuthError(rerr);
2037 2038
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
2039
    return -2;
2040 2041 2042
}


2043 2044 2045 2046
/*
 * This starts the SASL authentication negotiation.
 */
static int
2047 2048 2049 2050 2051 2052 2053
remoteDispatchAuthSaslStart(struct qemud_server *server,
                            struct qemud_client *client,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *rerr,
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2054 2055 2056 2057 2058
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

2059 2060 2061
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
2062

2063
    VIR_DEBUG("Start SASL auth %d", client->fd);
2064 2065
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
2066
        VIR_ERROR(_("client tried invalid SASL start request"));
2067
        goto authfail;
2068 2069
    }

2070 2071
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2072 2073 2074 2075 2076 2077 2078 2079 2080
    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) {
2081 2082
        VIR_ERROR(_("sasl start failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
2083 2084
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2085
        goto authfail;
2086 2087
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2088
        VIR_ERROR(_("sasl start reply data too long %d"), serveroutlen);
2089 2090
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2091
        goto authfail;
2092 2093 2094 2095
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2096
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
2097 2098
            virReportOOMError();
            remoteDispatchError(rerr);
2099
            goto error;
2100 2101 2102 2103 2104 2105 2106 2107
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2108
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2109 2110 2111
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
2112
        /* Check username whitelist ACL */
2113 2114 2115 2116 2117 2118 2119
        if ((err = remoteSASLCheckAccess(server, client, rerr)) < 0 ||
            (err = remoteSASLCheckSSF(client, rerr)) < 0) {
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2120

2121
        VIR_DEBUG("Authentication successful %d", client->fd);
2122 2123
        PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
              client->fd, REMOTE_AUTH_SASL, client->saslUsername);
2124 2125 2126 2127
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

2128
    virMutexUnlock(&client->lock);
2129
    return 0;
2130 2131

authfail:
2132
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
2133
    remoteDispatchAuthError(rerr);
2134 2135 2136
    goto error;

authdeny:
2137 2138
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_SASL, client->saslUsername);
2139 2140
    goto error;

2141
error:
2142
    virMutexUnlock(&client->lock);
2143
    return -1;
2144 2145 2146 2147
}


static int
2148 2149 2150 2151 2152 2153 2154
remoteDispatchAuthSaslStep(struct qemud_server *server,
                           struct qemud_client *client,
                           virConnectPtr conn ATTRIBUTE_UNUSED,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2155 2156 2157 2158 2159
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

2160 2161 2162
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
2163

2164
    VIR_DEBUG("Step SASL auth %d", client->fd);
2165 2166
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
2167
        VIR_ERROR(_("client tried invalid SASL start request"));
2168
        goto authfail;
2169 2170
    }

2171 2172
    VIR_DEBUG("Using SASL Data %d bytes, nil: %d",
              args->data.data_len, args->nil);
2173 2174 2175 2176 2177 2178 2179 2180
    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) {
2181 2182
        VIR_ERROR(_("sasl step failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
2183 2184
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2185
        goto authfail;
2186 2187 2188
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2189 2190
        VIR_ERROR(_("sasl step reply data too long %d"),
                  serveroutlen);
2191 2192
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2193
        goto authfail;
2194 2195 2196 2197
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2198
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
2199 2200
            virReportOOMError();
            remoteDispatchError(rerr);
2201
            goto error;
2202 2203 2204 2205 2206 2207 2208 2209
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2210
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2211 2212 2213
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
2214
        /* Check username whitelist ACL */
2215 2216 2217 2218 2219 2220 2221
        if ((err = remoteSASLCheckAccess(server, client, rerr)) < 0 ||
            (err = remoteSASLCheckSSF(client, rerr)) < 0) {
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2222

2223
        VIR_DEBUG("Authentication successful %d", client->fd);
2224 2225
        PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
              client->fd, REMOTE_AUTH_SASL, client->saslUsername);
2226 2227 2228 2229
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

2230
    virMutexUnlock(&client->lock);
2231
    return 0;
2232 2233

authfail:
2234
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
2235
    remoteDispatchAuthError(rerr);
2236 2237 2238
    goto error;

authdeny:
2239 2240
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_SASL, client->saslUsername);
2241 2242
    goto error;

2243
error:
2244
    virMutexUnlock(&client->lock);
2245
    return -1;
2246 2247 2248 2249 2250
}


#else /* HAVE_SASL */
static int
2251 2252 2253 2254 2255 2256 2257
remoteDispatchAuthSaslInit(struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn ATTRIBUTE_UNUSED,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           void *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
2258
{
2259
    VIR_ERROR(_("client tried unsupported SASL init request"));
2260
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
2261
    remoteDispatchAuthError(rerr);
2262 2263 2264 2265
    return -1;
}

static int
2266 2267 2268 2269 2270 2271 2272
remoteDispatchAuthSaslStart(struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *rerr,
                            remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
2273
{
2274
    VIR_ERROR(_("client tried unsupported SASL start request"));
2275
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
2276
    remoteDispatchAuthError(rerr);
2277 2278 2279 2280
    return -1;
}

static int
2281 2282 2283 2284 2285 2286 2287
remoteDispatchAuthSaslStep(struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn ATTRIBUTE_UNUSED,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
2288
{
2289
    VIR_ERROR(_("client tried unsupported SASL step request"));
2290
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
2291
    remoteDispatchAuthError(rerr);
2292 2293 2294 2295 2296
    return -1;
}
#endif /* HAVE_SASL */


2297 2298
#if HAVE_POLKIT1
static int
2299 2300 2301 2302 2303 2304 2305
remoteDispatchAuthPolkit(struct qemud_server *server,
                         struct qemud_client *client,
                         virConnectPtr conn ATTRIBUTE_UNUSED,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *rerr,
                         void *args ATTRIBUTE_UNUSED,
                         remote_auth_polkit_ret *ret)
2306
{
2307 2308
    pid_t callerPid = -1;
    uid_t callerUid = -1;
2309 2310 2311
    const char *action;
    int status = -1;
    char pidbuf[50];
2312
    char ident[100];
2313 2314
    int rv;

2315 2316
    memset(ident, 0, sizeof ident);

2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);

    action = client->readonly ?
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

    const char * const pkcheck [] = {
      PKCHECK_PATH,
      "--action-id", action,
      "--process", pidbuf,
      "--allow-user-interaction",
      NULL
    };

2333
    VIR_DEBUG("Start PolicyKit auth %d", client->fd);
2334
    if (client->auth != REMOTE_AUTH_POLKIT) {
2335
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2336 2337 2338 2339
        goto authfail;
    }

    if (qemudGetSocketIdentity(client->fd, &callerUid, &callerPid) < 0) {
2340
        VIR_ERROR(_("cannot get peer socket identity"));
2341 2342 2343
        goto authfail;
    }

2344
    VIR_INFO("Checking PID %d running as %d", callerPid, callerUid);
2345 2346 2347 2348

    rv = snprintf(pidbuf, sizeof pidbuf, "%d", callerPid);
    if (rv < 0 || rv >= sizeof pidbuf) {
        VIR_ERROR(_("Caller PID was too large %d"), callerPid);
2349
        goto authfail;
2350 2351
    }

2352 2353 2354 2355 2356 2357
    rv = snprintf(ident, sizeof ident, "pid:%d,uid:%d", callerPid, callerUid);
    if (rv < 0 || rv >= sizeof ident) {
        VIR_ERROR(_("Caller identity was too large %d:%d"), callerPid, callerUid);
        goto authfail;
    }

2358
    if (virRun(pkcheck, &status) < 0) {
2359
        VIR_ERROR(_("Cannot invoke %s"), PKCHECK_PATH);
2360
        goto authfail;
2361 2362
    }
    if (status != 0) {
2363 2364 2365 2366
        char *tmp = virCommandTranslateStatus(status);
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d: %s"),
                  action, callerPid, callerUid, NULLSTR(tmp));
        VIR_FREE(tmp);
2367
        goto authdeny;
2368
    }
2369
    PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
M
Matthias Bolte 已提交
2370
          client->fd, REMOTE_AUTH_POLKIT, (char *)ident);
2371
    VIR_INFO("Policy allowed action %s from pid %d, uid %d",
2372 2373 2374 2375 2376 2377 2378 2379
             action, callerPid, callerUid);
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;

    virMutexUnlock(&client->lock);
    return 0;

authfail:
2380
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_POLKIT);
2381 2382 2383
    goto error;

authdeny:
2384
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
M
Matthias Bolte 已提交
2385
          client->fd, REMOTE_AUTH_POLKIT, (char *)ident);
2386 2387 2388
    goto error;

error:
2389 2390 2391 2392 2393
    remoteDispatchAuthError(rerr);
    virMutexUnlock(&client->lock);
    return -1;
}
#elif HAVE_POLKIT0
2394
static int
2395 2396 2397 2398 2399 2400 2401
remoteDispatchAuthPolkit(struct qemud_server *server,
                         struct qemud_client *client,
                         virConnectPtr conn ATTRIBUTE_UNUSED,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *rerr,
                         void *args ATTRIBUTE_UNUSED,
                         remote_auth_polkit_ret *ret)
2402 2403 2404
{
    pid_t callerPid;
    uid_t callerUid;
2405 2406 2407 2408 2409 2410
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
2411
    const char *action;
2412
    char ident[100];
J
Jim Fehlig 已提交
2413
    int rv;
2414 2415

    memset(ident, 0, sizeof ident);
2416

2417 2418 2419
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
2420 2421

    action = client->readonly ?
2422 2423
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
2424

2425
    VIR_DEBUG("Start PolicyKit auth %d", client->fd);
2426
    if (client->auth != REMOTE_AUTH_POLKIT) {
2427
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2428
        goto authfail;
2429 2430 2431
    }

    if (qemudGetSocketIdentity(client->fd, &callerUid, &callerPid) < 0) {
2432
        VIR_ERROR(_("cannot get peer socket identity"));
2433
        goto authfail;
2434 2435
    }

2436 2437 2438 2439 2440 2441
    rv = snprintf(ident, sizeof ident, "pid:%d,uid:%d", callerPid, callerUid);
    if (rv < 0 || rv >= sizeof ident) {
        VIR_ERROR(_("Caller identity was too large %d:%d"), callerPid, callerUid);
        goto authfail;
    }

2442
    VIR_INFO("Checking PID %d running as %d", callerPid, callerUid);
2443 2444 2445
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
2446
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
2447
        dbus_error_free(&err);
2448
        goto authfail;
2449
    }
2450

2451
    if (!(pkaction = polkit_action_new())) {
2452
        char ebuf[1024];
2453
        VIR_ERROR(_("Failed to create polkit action %s"),
2454
                  virStrerror(errno, ebuf, sizeof ebuf));
2455
        polkit_caller_unref(pkcaller);
2456
        goto authfail;
2457 2458 2459 2460 2461
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
2462
        char ebuf[1024];
2463
        VIR_ERROR(_("Failed to create polkit context %s"),
2464
                  (pkerr ? polkit_error_get_error_message(pkerr)
2465
                   : virStrerror(errno, ebuf, sizeof ebuf)));
2466 2467 2468 2469 2470
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
2471
        goto authfail;
2472
    }
2473

2474
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
2475 2476 2477 2478 2479 2480
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
2481 2482 2483
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
2484
        goto authfail;
2485
    }
2486
# else
2487 2488 2489
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
2490
# endif
2491 2492 2493 2494
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
2495
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %s"),
2496 2497
                  action, callerPid, callerUid,
                  polkit_result_to_string_representation(pkresult));
2498
        goto authdeny;
2499
    }
2500 2501
    PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_POLKIT, ident);
2502
    VIR_INFO("Policy allowed action %s from pid %d, uid %d, result %s",
2503 2504 2505 2506
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
2507

2508 2509
    virMutexUnlock(&client->lock);
    return 0;
2510

2511 2512 2513
authfail:
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_POLKIT);
    goto error;
2514

2515 2516 2517 2518
authdeny:
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_POLKIT, ident);
    goto error;
2519

2520 2521 2522 2523
error:
    remoteDispatchAuthError(rerr);
    virMutexUnlock(&client->lock);
    return -1;
2524 2525
}

2526
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
2527 2528

static int
2529 2530 2531 2532 2533 2534 2535
remoteDispatchAuthPolkit(struct qemud_server *server ATTRIBUTE_UNUSED,
                         struct qemud_client *client ATTRIBUTE_UNUSED,
                         virConnectPtr conn ATTRIBUTE_UNUSED,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *rerr,
                         void *args ATTRIBUTE_UNUSED,
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
2536
{
2537
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
2538 2539 2540 2541
    remoteDispatchAuthError(rerr);
    return -1;
}
#endif /* HAVE_POLKIT1 */
2542 2543


2544 2545 2546
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
2547

2548
static int
2549 2550 2551 2552 2553 2554 2555
remoteDispatchNodeDeviceGetParent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
2556
{
2557 2558
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
2559
    int rv = -1;
2560

2561
    if (!conn) {
2562 2563
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2564 2565
    }

2566
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
2567 2568
        goto cleanup;

2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
            virReportOOMError();
            goto cleanup;
        }
        if (!(*parent_p = strdup(parent))) {
            VIR_FREE(parent_p);
            virReportOOMError();
            goto cleanup;
        }
        ret->parent = parent_p;
    }

2588 2589 2590 2591 2592
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
2593 2594
    if (dev)
        virNodeDeviceFree(dev);
2595
    return rv;
2596 2597
}

2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609

/***************************
 * Register / deregister events
 ***************************/
static int
remoteDispatchDomainEventsRegister(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *rerr ATTRIBUTE_UNUSED,
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
2610
{
2611
    int callbackID;
2612
    int rv = -1;
O
Osier Yang 已提交
2613

2614
    if (!conn) {
2615 2616
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2617 2618
    }

2619 2620
    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] != -1) {
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
2621
        goto cleanup;
2622
    }
O
Osier Yang 已提交
2623

2624 2625 2626 2627 2628
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0)
2629
        goto cleanup;
O
Osier Yang 已提交
2630

2631 2632
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;

2633 2634 2635 2636 2637 2638
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
O
Osier Yang 已提交
2639 2640
}

2641 2642 2643 2644 2645 2646 2647 2648
static int
remoteDispatchDomainEventsDeregister(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr ATTRIBUTE_UNUSED,
                                     void *args ATTRIBUTE_UNUSED,
                                     remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
2649
{
2650
    int rv = -1;
2651

2652
    if (!conn) {
2653 2654
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2655 2656
    }

2657 2658
    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] < 0) {
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
2659
        goto cleanup;
2660
    }
2661

2662 2663
    if (virConnectDomainEventDeregisterAny(conn,
                                           client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0)
2664
        goto cleanup;
2665

2666
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;
2667 2668 2669
    rv = 0;

cleanup:
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
}

static void
remoteDispatchDomainEventSend(struct qemud_client *client,
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
    struct qemud_client_message *msg = NULL;
    XDR xdr;
    unsigned int len;

    if (VIR_ALLOC(msg) < 0)
        return;

    msg->hdr.prog = REMOTE_PROGRAM;
    msg->hdr.vers = REMOTE_PROTOCOL_VERSION;
    msg->hdr.proc = procnr;
    msg->hdr.type = REMOTE_MESSAGE;
    msg->hdr.serial = 1;
    msg->hdr.status = REMOTE_OK;

    if (remoteEncodeClientMessageHeader(msg) < 0)
        goto cleanup;

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

    /* Skip over the header we just wrote */
    if (xdr_setpos(&xdr, msg->bufferOffset) == 0)
        goto xdr_cleanup;

    if (!(proc)(&xdr, data)) {
        VIR_WARN("Failed to serialize domain event %d", procnr);
        goto xdr_cleanup;
    }

    /* Update length word to include payload*/
    len = msg->bufferOffset = xdr_getpos(&xdr);
    if (xdr_setpos(&xdr, 0) == 0)
        goto xdr_cleanup;

    if (!xdr_u_int(&xdr, &len))
        goto xdr_cleanup;

    /* Send it. */
    msg->async = 1;
    msg->bufferLength = len;
    msg->bufferOffset = 0;

    VIR_DEBUG("Queue event %d %d", procnr, msg->bufferLength);
    qemudClientMessageQueuePush(&client->tx, msg);
    qemudUpdateClientEvent(client);

    xdr_destroy(&xdr);
    return;

xdr_cleanup:
    xdr_destroy(&xdr);
cleanup:
    VIR_FREE(msg);
2737 2738
}

2739 2740 2741 2742 2743 2744 2745 2746
static int
remoteDispatchSecretGetValue(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
2747
{
2748 2749 2750
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
2751
    int rv = -1;
2752

2753
    if (!conn) {
2754 2755
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2756 2757
    }

2758
    if (!(secret = get_nonnull_secret(conn, args->secret)))
2759
        goto cleanup;
2760

2761
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
2762
        goto cleanup;
2763

2764 2765 2766
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

2767 2768 2769 2770 2771
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
2772 2773
    if (secret)
        virSecretFree(secret);
2774
    return rv;
2775 2776
}

2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
static int
remoteDispatchDomainGetState(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

    if (virDomainGetState(dom, &ret->state, &ret->reason, args->flags) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

2810
static int
2811 2812 2813 2814 2815 2816 2817
remoteDispatchDomainEventsRegisterAny(struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
                                      remote_error *rerr ATTRIBUTE_UNUSED,
                                      remote_domain_events_register_any_args *args,
                                      void *ret ATTRIBUTE_UNUSED)
2818 2819
{
    int callbackID;
2820
    int rv = -1;
2821

2822
    if (!conn) {
2823 2824
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2825 2826
    }

2827 2828
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
2829 2830
        virNetError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
        goto cleanup;
2831 2832 2833
    }

    if (client->domainEventCallbackID[args->eventID] != -1)  {
2834 2835
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), args->eventID);
        goto cleanup;
2836 2837 2838 2839 2840 2841
    }

    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
2842
                                                       client, NULL)) < 0)
2843
        goto cleanup;
2844 2845 2846

    client->domainEventCallbackID[args->eventID] = callbackID;

2847 2848 2849 2850 2851 2852
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
2853 2854 2855 2856
}


static int
2857 2858 2859 2860 2861 2862 2863
remoteDispatchDomainEventsDeregisterAny(struct qemud_server *server ATTRIBUTE_UNUSED,
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
                                        remote_error *rerr ATTRIBUTE_UNUSED,
                                        remote_domain_events_deregister_any_args *args,
                                        void *ret ATTRIBUTE_UNUSED)
2864 2865
{
    int callbackID = -1;
2866
    int rv = -1;
2867

2868
    if (!conn) {
2869 2870
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2871 2872
    }

2873 2874
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
2875 2876
        virNetError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
        goto cleanup;
2877 2878
    }

2879
    if ((callbackID = client->domainEventCallbackID[args->eventID]) < 0) {
2880 2881
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), args->eventID);
        goto cleanup;
2882 2883
    }

2884
    if (virConnectDomainEventDeregisterAny(conn, callbackID) < 0)
2885
        goto cleanup;
2886 2887

    client->domainEventCallbackID[args->eventID] = -1;
2888 2889 2890 2891 2892 2893
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
2894 2895
}

C
Chris Lalancette 已提交
2896
static int
2897 2898 2899 2900 2901 2902 2903
qemuDispatchMonitorCommand(struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           qemu_monitor_command_args *args,
                           qemu_monitor_command_ret *ret)
C
Chris Lalancette 已提交
2904
{
2905
    virDomainPtr dom = NULL;
2906
    int rv = -1;
C
Chris Lalancette 已提交
2907

2908
    if (!conn) {
2909 2910
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2911 2912
    }

2913
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2914
        goto cleanup;
C
Chris Lalancette 已提交
2915

2916
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
2917
                                    args->flags) < 0)
2918
        goto cleanup;
C
Chris Lalancette 已提交
2919

2920
    rv = 0;
C
Chris Lalancette 已提交
2921

2922 2923 2924
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
2925 2926
    if (dom)
        virDomainFree(dom);
2927
    return rv;
C
Chris Lalancette 已提交
2928 2929
}

2930

2931 2932
#include "remote_dispatch_bodies.h"
#include "qemu_dispatch_bodies.h"
2933

2934

2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
static int
remoteDispatchDomainMigrateBegin3(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
2947
    char *xmlin;
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

2960
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
2961 2962
    dname = args->dname == NULL ? NULL : *args->dname;

2963
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
                                       &cookieout, &cookieoutlen,
                                       args->flags, dname, args->resource)))
        goto cleanup;

    /* remoteDispatchClientRequest will free cookie and
     * the xml string if there is one.
     */
    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;
    ret->xml = xml;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
remoteDispatchDomainMigratePrepare3(struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_domain_migrate_prepare3_args *args,
                                    remote_domain_migrate_prepare3_ret *ret)
{
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    if (virDomainMigratePrepare3(conn,
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
        goto cleanup;

    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;
    ret->uri_out = *uri_out == NULL ? NULL : uri_out;

    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
remoteDispatchDomainMigratePerform3(struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
3052
    char *xmlin;
3053
    char *dname;
3054 3055
    char *uri;
    char *dconnuri;
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

3068
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3069
    dname = args->dname == NULL ? NULL : *args->dname;
3070 3071
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
3072

3073
    if (virDomainMigratePerform3(dom, xmlin,
3074 3075 3076
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
3077
                                 dconnuri, uri,
3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108
                                 args->flags, dname, args->resource) < 0)
        goto cleanup;

    /* remoteDispatchClientRequest will free cookie
     */
    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
remoteDispatchDomainMigrateFinish3(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *rerr,
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
3109 3110
    char *uri;
    char *dconnuri;
3111 3112 3113 3114 3115 3116 3117
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

3118 3119 3120
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

3121 3122 3123 3124 3125 3126 3127
    if (!(dom = virDomainMigrateFinish3(conn, args->dname,
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
3128 3129
        goto cleanup;

3130
    make_nonnull_domain(&ret->dom, dom);
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186

    /* remoteDispatchClientRequest will free cookie
     */
    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;

    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
remoteDispatchDomainMigrateConfirm3(struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_domain_migrate_confirm3_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom = NULL;
    int rv = -1;

    if (!conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(conn, args->dom)))
        goto cleanup;

    if (virDomainMigrateConfirm3(dom,
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 args->flags, args->cancelled) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


3187 3188 3189 3190 3191 3192 3193 3194 3195
/*----- 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
3196
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
3197 3198
{
    virDomainPtr dom;
3199
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
3200 3201 3202 3203 3204 3205 3206 3207
    /* 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
3208
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
3209
{
3210
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
3211 3212
}

D
Daniel Veillard 已提交
3213
static virInterfacePtr
3214
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
3215
{
3216
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
3217 3218
}

3219
static virStoragePoolPtr
3220
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
3221
{
3222
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid);
3223 3224 3225
}

static virStorageVolPtr
3226
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
3227 3228
{
    virStorageVolPtr ret;
3229
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key);
3230 3231 3232
    return ret;
}

3233
static virSecretPtr
3234
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
3235
{
3236
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
3237 3238
}

3239
static virNWFilterPtr
3240
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
3241
{
3242
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
3243 3244
}

C
Chris Lalancette 已提交
3245
static virDomainSnapshotPtr
3246
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
3247
{
3248
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
3249 3250
}

3251 3252
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
3253
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
3254 3255
{
    dom_dst->id = dom_src->id;
3256 3257
    dom_dst->name = strdup(dom_src->name);
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
3258 3259 3260
}

static void
3261
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
3262
{
3263 3264
    net_dst->name = strdup(net_src->name);
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
3265 3266
}

D
Daniel Veillard 已提交
3267
static void
3268 3269
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
3270
{
3271 3272
    interface_dst->name = strdup(interface_src->name);
    interface_dst->mac = strdup(interface_src->mac);
D
Daniel Veillard 已提交
3273 3274
}

3275
static void
3276
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
3277
{
3278 3279
    pool_dst->name = strdup(pool_src->name);
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
3280 3281 3282
}

static void
3283
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
3284
{
3285 3286 3287
    vol_dst->pool = strdup(vol_src->pool);
    vol_dst->name = strdup(vol_src->name);
    vol_dst->key = strdup(vol_src->key);
3288
}
3289 3290

static void
3291
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
3292 3293 3294
{
    dev_dst->name = strdup(dev_src->name);
}
3295 3296

static void
3297
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
3298
{
3299
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
3300
    secret_dst->usageType = secret_src->usageType;
3301
    secret_dst->usageID = strdup(secret_src->usageID);
3302
}
3303 3304

static void
3305
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
3306
{
3307 3308
    nwfilter_dst->name = strdup(nwfilter_src->name);
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
3309
}
C
Chris Lalancette 已提交
3310 3311

static void
3312
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
3313 3314
{
    snapshot_dst->name = strdup(snapshot_src->name);
3315
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
3316
}