remote.c 165.3 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2007-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24
 *
 * Author: Richard W.M. Jones <rjones@redhat.com>
 */

#include <config.h>

25
#include "virerror.h"
26

27
#if WITH_POLKIT0
28 29
# include <polkit/polkit.h>
# include <polkit-dbus/polkit-dbus.h>
30 31
#endif

32
#include "remote.h"
33
#include "libvirtd.h"
34 35
#include "libvirt_internal.h"
#include "datatypes.h"
36
#include "viralloc.h"
37
#include "virlog.h"
C
Chris Lalancette 已提交
38
#include "stream.h"
39
#include "viruuid.h"
40
#include "vircommand.h"
41
#include "intprops.h"
42
#include "virnetserverservice.h"
J
Jim Fehlig 已提交
43
#include "virnetserver.h"
44
#include "virfile.h"
45
#include "virtypedparam.h"
46
#include "virdbus.h"
47
#include "virprocess.h"
48 49
#include "remote_protocol.h"
#include "qemu_protocol.h"
50
#include "lxc_protocol.h"
51
#include "virstring.h"
52 53

#define VIR_FROM_THIS VIR_FROM_RPC
54

55
#if SIZEOF_LONG < 8
E
Eric Blake 已提交
56 57 58
# define HYPER_TO_TYPE(_type, _to, _from)                               \
    do {                                                                \
        if ((_from) != (_type)(_from)) {                                \
59 60 61
            virReportError(VIR_ERR_OVERFLOW,                            \
                           _("conversion from hyper to %s overflowed"), \
                           #_type);                                     \
E
Eric Blake 已提交
62 63 64
            goto cleanup;                                               \
        }                                                               \
        (_to) = (_from);                                                \
65 66 67 68 69 70 71 72 73
    } 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

74 75 76 77 78 79 80
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);
81
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
82 83 84 85 86 87 88 89 90
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);
91

92 93 94 95 96
static virTypedParameterPtr
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
                                 int limit,
                                 int *nparams);
97

98 99 100 101 102 103
static int
remoteSerializeDomainDiskErrors(virDomainDiskErrorPtr errors,
                                int nerrors,
                                remote_domain_disk_error **ret_errors_val,
                                u_int *ret_errors_len);

104 105
#include "remote_dispatch.h"
#include "qemu_dispatch.h"
106
#include "lxc_dispatch.h"
C
Chris Lalancette 已提交
107 108


109 110
/* Prototypes */
static void
111 112
remoteDispatchDomainEventSend(virNetServerClientPtr client,
                              virNetServerProgramPtr program,
113 114 115
                              int procnr,
                              xdrproc_t proc,
                              void *data);
116

117 118 119 120 121
static int remoteRelayDomainEventLifecycle(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           int event,
                                           int detail,
                                           void *opaque)
122
{
123
    virNetServerClientPtr client = opaque;
124
    remote_domain_event_lifecycle_msg data;
125

126 127 128
    if (!client)
        return -1;

129
    VIR_DEBUG("Relaying domain lifecycle event %d %d", event, detail);
130

131
    /* build return data */
132
    memset(&data, 0, sizeof(data));
133
    make_nonnull_domain(&data.dom, dom);
134 135
    data.event = event;
    data.detail = detail;
136

137
    remoteDispatchDomainEventSend(client, remoteProgram,
138 139
                                  REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_domain_event_lifecycle_msg, &data);
140

141 142
    return 0;
}
143

144 145 146 147
static int remoteRelayDomainEventReboot(virConnectPtr conn ATTRIBUTE_UNUSED,
                                        virDomainPtr dom,
                                        void *opaque)
{
148
    virNetServerClientPtr client = opaque;
149 150 151 152 153
    remote_domain_event_reboot_msg data;

    if (!client)
        return -1;

154
    VIR_DEBUG("Relaying domain reboot event %s %d", dom->name, dom->id);
155 156

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

160
    remoteDispatchDomainEventSend(client, remoteProgram,
161 162
                                  REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                  (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);
163 164 165 166

    return 0;
}

167

168 169 170 171 172
static int remoteRelayDomainEventRTCChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           long long offset,
                                           void *opaque)
{
173
    virNetServerClientPtr client = opaque;
174 175 176 177 178
    remote_domain_event_rtc_change_msg data;

    if (!client)
        return -1;

179
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld", dom->name, dom->id, offset);
180 181

    /* build return data */
182
    memset(&data, 0, sizeof(data));
183
    make_nonnull_domain(&data.dom, dom);
184 185
    data.offset = offset;

186
    remoteDispatchDomainEventSend(client, remoteProgram,
187 188
                                  REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);
189 190 191 192 193

    return 0;
}


194 195 196 197 198
static int remoteRelayDomainEventWatchdog(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          int action,
                                          void *opaque)
{
199
    virNetServerClientPtr client = opaque;
200 201 202 203 204
    remote_domain_event_watchdog_msg data;

    if (!client)
        return -1;

205
    VIR_DEBUG("Relaying domain watchdog event %s %d %d", dom->name, dom->id, action);
206 207

    /* build return data */
208
    memset(&data, 0, sizeof(data));
209
    make_nonnull_domain(&data.dom, dom);
210 211
    data.action = action;

212
    remoteDispatchDomainEventSend(client, remoteProgram,
213 214
                                  REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                  (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);
215 216 217 218 219

    return 0;
}


220 221 222 223 224 225 226
static int remoteRelayDomainEventIOError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                         virDomainPtr dom,
                                         const char *srcPath,
                                         const char *devAlias,
                                         int action,
                                         void *opaque)
{
227
    virNetServerClientPtr client = opaque;
228 229 230 231 232
    remote_domain_event_io_error_msg data;

    if (!client)
        return -1;

233
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d", dom->name, dom->id, srcPath, devAlias, action);
234 235

    /* build return data */
236
    memset(&data, 0, sizeof(data));
237 238 239
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
240
    make_nonnull_domain(&data.dom, dom);
241 242
    data.action = action;

243
    remoteDispatchDomainEventSend(client, remoteProgram,
244 245
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);
246 247

    return 0;
248
error:
E
Eric Blake 已提交
249 250
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
251
    return -1;
252 253 254
}


255 256 257 258 259 260 261 262
static int remoteRelayDomainEventIOErrorReason(virConnectPtr conn ATTRIBUTE_UNUSED,
                                               virDomainPtr dom,
                                               const char *srcPath,
                                               const char *devAlias,
                                               int action,
                                               const char *reason,
                                               void *opaque)
{
263
    virNetServerClientPtr client = opaque;
264 265 266 267 268
    remote_domain_event_io_error_reason_msg data;

    if (!client)
        return -1;

269 270
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s",
              dom->name, dom->id, srcPath, devAlias, action, reason);
271 272

    /* build return data */
273
    memset(&data, 0, sizeof(data));
274 275 276 277
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0 ||
        VIR_STRDUP(data.reason, reason) < 0)
        goto error;
278
    data.action = action;
279 280

    make_nonnull_domain(&data.dom, dom);
281

282
    remoteDispatchDomainEventSend(client, remoteProgram,
283 284
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);
285 286

    return 0;
287

288
error:
E
Eric Blake 已提交
289 290 291
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
    VIR_FREE(data.reason);
292
    return -1;
293 294 295
}


296 297 298 299 300 301 302 303 304
static int remoteRelayDomainEventGraphics(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          int phase,
                                          virDomainEventGraphicsAddressPtr local,
                                          virDomainEventGraphicsAddressPtr remote,
                                          const char *authScheme,
                                          virDomainEventGraphicsSubjectPtr subject,
                                          void *opaque)
{
305
    virNetServerClientPtr client = opaque;
306
    remote_domain_event_graphics_msg data;
307
    size_t i;
308 309 310 311

    if (!client)
        return -1;

312 313 314 315
    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);
316

317
    VIR_DEBUG("Subject %d", subject->nidentity);
318
    for (i = 0; i < subject->nidentity; i++) {
319
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
320 321 322
    }

    /* build return data */
323
    memset(&data, 0, sizeof(data));
324 325 326
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
327 328 329 330 331 332
    if (VIR_STRDUP(data.authScheme, authScheme) < 0 ||
        VIR_STRDUP(data.local.node, local->node) < 0 ||
        VIR_STRDUP(data.local.service, local->service) < 0 ||
        VIR_STRDUP(data.remote.node, remote->node) < 0 ||
        VIR_STRDUP(data.remote.service, remote->service) < 0)
        goto error;
333 334

    data.subject.subject_len = subject->nidentity;
335
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
336
        goto error;
337

338
    for (i = 0; i < data.subject.subject_len; i++) {
339 340 341
        if (VIR_STRDUP(data.subject.subject_val[i].type, subject->identities[i].type) < 0 ||
            VIR_STRDUP(data.subject.subject_val[i].name, subject->identities[i].name) < 0)
            goto error;
342
    }
343
    make_nonnull_domain(&data.dom, dom);
344

345
    remoteDispatchDomainEventSend(client, remoteProgram,
346 347
                                  REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                  (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
348 349

    return 0;
350

351
error:
E
Eric Blake 已提交
352 353 354 355 356
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
357
    if (data.subject.subject_val != NULL) {
358
        for (i = 0; i < data.subject.subject_len; i++) {
E
Eric Blake 已提交
359 360
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
361
        }
E
Eric Blake 已提交
362
        VIR_FREE(data.subject.subject_val);
363 364
    }
    return -1;
365 366
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
static int remoteRelayDomainEventBlockJob(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          const char *path,
                                          int type,
                                          int status,
                                          void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_block_job_msg data;

    if (!client)
        return -1;

    VIR_DEBUG("Relaying domain block job event %s %d %s %i, %i",
              dom->name, dom->id, path, type, status);

    /* build return data */
384
    memset(&data, 0, sizeof(data));
385 386
    if (VIR_STRDUP(data.path, path) < 0)
        goto error;
387 388
    data.type = type;
    data.status = status;
389
    make_nonnull_domain(&data.dom, dom);
390 391 392 393 394 395

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
                                  (xdrproc_t)xdr_remote_domain_event_block_job_msg, &data);

    return 0;
396
error:
E
Eric Blake 已提交
397
    VIR_FREE(data.path);
398
    return -1;
399 400
}

401

402 403 404 405
static int remoteRelayDomainEventControlError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                              virDomainPtr dom,
                                              void *opaque)
{
406
    virNetServerClientPtr client = opaque;
407 408 409 410 411 412 413 414
    remote_domain_event_control_error_msg data;

    if (!client)
        return -1;

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

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

418
    remoteDispatchDomainEventSend(client, remoteProgram,
419 420 421 422 423 424 425
                                  REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_control_error_msg, &data);

    return 0;
}


426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
static int remoteRelayDomainEventDiskChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                            virDomainPtr dom,
                                            const char *oldSrcPath,
                                            const char *newSrcPath,
                                            const char *devAlias,
                                            int reason,
                                            void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_disk_change_msg data;
    char **oldSrcPath_p = NULL, **newSrcPath_p = NULL;

    if (!client)
        return -1;

    VIR_DEBUG("Relaying domain %s %d disk change %s %s %s %d",
              dom->name, dom->id, oldSrcPath, newSrcPath, devAlias, reason);

    /* build return data */
445
    memset(&data, 0, sizeof(data));
446 447
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
448
         VIR_STRDUP(*oldSrcPath_p, oldSrcPath) < 0))
449
        goto error;
450 451 452

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
453
         VIR_STRDUP(*newSrcPath_p, newSrcPath) < 0))
454
        goto error;
455 456 457

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
458 459
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
460 461 462 463 464 465 466 467 468 469
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_disk_change_msg, &data);

    return 0;

470
error:
M
Michal Privoznik 已提交
471 472
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
473 474 475 476
    return -1;
}


477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
static int remoteRelayDomainEventTrayChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                            virDomainPtr dom,
                                            const char *devAlias,
                                            int reason,
                                            void *opaque) {
    virNetServerClientPtr client = opaque;
    remote_domain_event_tray_change_msg data;

    if (!client)
        return -1;

    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d",
              dom->name, dom->id, devAlias, reason);

    /* build return data */
492
    memset(&data, 0, sizeof(data));
493

494
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
495 496 497 498 499 500 501 502 503 504 505 506
        return -1;
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_TRAY_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_tray_change_msg, &data);

    return 0;
}

O
Osier Yang 已提交
507 508
static int remoteRelayDomainEventPMWakeup(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
509
                                          int reason ATTRIBUTE_UNUSED,
O
Osier Yang 已提交
510 511 512 513 514 515 516 517 518 519
                                          void *opaque) {
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmwakeup_msg data;

    if (!client)
        return -1;

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

    /* build return data */
520
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
521 522 523 524 525 526 527 528 529
    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
                                  (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg, &data);

    return 0;
}

O
Osier Yang 已提交
530 531
static int remoteRelayDomainEventPMSuspend(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
532
                                           int reason ATTRIBUTE_UNUSED,
O
Osier Yang 已提交
533 534 535 536 537 538 539 540 541 542
                                           void *opaque) {
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmsuspend_msg data;

    if (!client)
        return -1;

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

    /* build return data */
543
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
544 545 546 547 548 549 550 551 552
    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
                                  (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg, &data);

    return 0;
}

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
static int
remoteRelayDomainEventBalloonChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_balloon_change_msg data;

    if (!client)
        return -1;

    VIR_DEBUG("Relaying domain balloon change event %s %d %lld", dom->name, dom->id, actual);

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

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_balloon_change_msg, &data);

    return 0;
}


580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
static int remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn ATTRIBUTE_UNUSED,
                                               virDomainPtr dom,
                                               int reason ATTRIBUTE_UNUSED,
                                               void *opaque) {
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmsuspend_disk_msg data;

    if (!client)
        return -1;

    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk", dom->name, dom->id);

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

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
                                  (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg, &data);

    return 0;
}

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
static int
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn ATTRIBUTE_UNUSED,
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_device_removed_msg data;

    if (!client)
        return -1;

    VIR_DEBUG("Relaying domain device removed event %s %d %s",
              dom->name, dom->id, devAlias);

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

    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        return -1;

    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
                                  (xdrproc_t)xdr_remote_domain_event_device_removed_msg,
                                  &data);

    return 0;
}

634

635
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
636
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
637
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
638
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
639
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
640
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
641
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
642
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
643
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
644
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
645
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
646
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
647
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
648
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
649
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
650
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
651
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
652 653 654 655
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

656 657 658 659 660 661 662
/*
 * You must hold lock for at least the client
 * We don't free stuff here, merely disconnect the client's
 * network socket & resources.
 * We keep the libvirt connection open until any async
 * jobs have finished, then clean it up elsewhere
 */
663
void remoteClientFreeFunc(void *data)
664 665 666 667 668
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
669
        virIdentityPtr sysident = virIdentityGetSystem();
670
        size_t i;
671

672 673
        virIdentitySetCurrent(sysident);

674
        for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) {
675
            if (priv->domainEventCallbackID[i] != -1) {
676
                VIR_DEBUG("Deregistering to relay remote events %zu", i);
677 678 679 680 681 682 683
                virConnectDomainEventDeregisterAny(priv->conn,
                                                   priv->domainEventCallbackID[i]);
            }
            priv->domainEventCallbackID[i] = -1;
        }

        virConnectClose(priv->conn);
684 685 686

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
687 688 689 690 691 692
    }

    VIR_FREE(priv);
}


693 694 695 696 697 698 699
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

700

701 702
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
703 704
{
    struct daemonClientPrivate *priv;
705
    size_t i;
706

707
    if (VIR_ALLOC(priv) < 0)
708
        return NULL;
709 710 711

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

716
    for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++)
717 718
        priv->domainEventCallbackID[i] = -1;

719
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
720
    return priv;
721 722
}

723 724 725
/*----- Functions. -----*/

static int
726 727 728 729 730
remoteDispatchConnectOpen(virNetServerPtr server,
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
731 732
{
    const char *name;
733
    unsigned int flags;
734
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
735
    int rv = -1;
736

737 738 739 740
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
741
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
742 743 744
        goto cleanup;
    }

745
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
746 747
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
748 749 750
        goto cleanup;
    }

751 752 753 754 755 756
    name = args->name ? *args->name : NULL;

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

760
    priv->conn =
761
        flags & VIR_CONNECT_RO
762 763
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
764

765
    if (priv->conn == NULL)
766 767 768
        goto cleanup;

    rv = 0;
769

770 771
cleanup:
    if (rv < 0)
772 773
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
774
    return rv;
775 776 777 778
}


static int
779 780 781 782
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
783
{
784
    virNetServerClientDelayedClose(client);
785
    return 0;
786 787
}

788

789
static int
790 791
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
792
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
793
                                     virNetMessageErrorPtr rerr,
794 795
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
796
{
797
    virDomainPtr dom = NULL;
798 799
    char *type;
    int nparams;
800
    int rv = -1;
801 802
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
803

804
    if (!priv->conn) {
805
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
806
        goto cleanup;
807 808
    }

809
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
810
        goto cleanup;
811

812
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
813
        goto cleanup;
814 815 816

    ret->type = type;
    ret->nparams = nparams;
817 818 819 820
    rv = 0;

cleanup:
    if (rv < 0)
821
        virNetMessageSaveError(rerr);
822 823 824
    if (dom)
        virDomainFree(dom);
    return rv;
825 826
}

827 828
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
829 830 831
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
832
                               remote_typed_param **ret_params_val,
833 834
                               u_int *ret_params_len,
                               unsigned int flags)
835
{
836 837
    size_t i;
    size_t j;
838 839 840 841
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
842
    if (VIR_ALLOC_N(val, nparams) < 0)
843 844
        goto cleanup;

845
    for (i = 0, j = 0; i < nparams; ++i) {
846 847 848 849 850
        /* virDomainGetCPUStats can return a sparse array; also, we
         * can't pass back strings to older clients.  */
        if (!params[i].type ||
            (!(flags & VIR_TYPED_PARAM_STRING_OKAY) &&
             params[i].type == VIR_TYPED_PARAM_STRING)) {
851 852 853 854
            --*ret_params_len;
            continue;
        }

855
        /* remoteDispatchClientRequest will free this: */
856
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
857
            goto cleanup;
858
        val[j].value.type = params[i].type;
859
        switch (params[i].type) {
860
        case VIR_TYPED_PARAM_INT:
861
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
862 863
            break;
        case VIR_TYPED_PARAM_UINT:
864
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
865 866
            break;
        case VIR_TYPED_PARAM_LLONG:
867
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
868 869
            break;
        case VIR_TYPED_PARAM_ULLONG:
870
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
871 872
            break;
        case VIR_TYPED_PARAM_DOUBLE:
873
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
874 875
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
876 877 878
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
879
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
880
                goto cleanup;
881 882
            break;
        default:
883 884
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
885 886
            goto cleanup;
        }
887
        j++;
888 889 890 891 892 893 894 895
    }

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

cleanup:
    if (val) {
896
        for (i = 0; i < nparams; i++) {
897
            VIR_FREE(val[i].field);
898
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
899 900
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
901 902 903 904 905 906 907
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
908 909
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
910 911 912
                                 int limit,
                                 int *nparams)
{
913
    size_t i = 0;
914 915 916 917
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
918
    if (limit && args_params_len > limit) {
919
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
920 921
        goto cleanup;
    }
922
    if (VIR_ALLOC_N(params, args_params_len) < 0)
923 924 925 926 927 928 929 930
        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) {
931 932 933
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
            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;
962
        case VIR_TYPED_PARAM_STRING:
963 964
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
965 966
                goto cleanup;
            break;
967
        default:
968 969
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
970 971 972 973 974 975 976
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
977
    if (rv < 0) {
978 979
        virTypedParamsFree(params, i);
        params = NULL;
980
    }
981 982 983
    return params;
}

984
static int
985 986
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
987
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
988
                                           virNetMessageErrorPtr rerr,
989 990
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
991
{
992
    virDomainPtr dom = NULL;
993
    virTypedParameterPtr params = NULL;
994
    int nparams = 0;
995
    int rv = -1;
996 997
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
998

999
    if (!priv->conn) {
1000
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1001
        goto cleanup;
1002 1003
    }

1004
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1005
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1006
        goto cleanup;
1007
    }
1008
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1009
        goto cleanup;
1010
    nparams = args->nparams;
1011

1012
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1013
        goto cleanup;
1014

1015
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1016
        goto cleanup;
1017

1018
    if (remoteSerializeTypedParameters(params, nparams,
1019
                                       &ret->params.params_val,
1020 1021
                                       &ret->params.params_len,
                                       0) < 0)
1022 1023 1024 1025 1026 1027
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
1028
        virNetMessageSaveError(rerr);
1029
    virTypedParamsFree(params, nparams);
1030 1031 1032 1033 1034
    if (dom)
        virDomainFree(dom);
    return rv;
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
static int
remoteDispatchConnectListAllDomains(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_domains_args *args,
                                    remote_connect_list_all_domains_ret *ret)
{
    virDomainPtr *doms = NULL;
    int ndomains = 0;
1045
    size_t i;
1046 1047 1048 1049
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1050
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1051 1052 1053 1054 1055 1056 1057 1058 1059
        goto cleanup;
    }

    if ((ndomains = virConnectListAllDomains(priv->conn,
                                             args->need_results ? &doms : NULL,
                                             args->flags)) < 0)
        goto cleanup;

    if (doms && ndomains) {
1060
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0)
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
            goto cleanup;

        ret->domains.domains_len = ndomains;

        for (i = 0; i < ndomains; i++)
            make_nonnull_domain(ret->domains.domains_val + i, doms[i]);
    } else {
        ret->domains.domains_len = 0;
        ret->domains.domains_val = NULL;
    }

    ret->ret = ndomains;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (doms) {
        for (i = 0; i < ndomains; i++)
            virDomainFree(doms[i]);
        VIR_FREE(doms);
    }
    return rv;
}

1087
static int
1088 1089
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1090
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1091
                                                virNetMessageErrorPtr rerr,
1092 1093 1094 1095 1096
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1097
    int nparams = 0;
1098
    int rv = -1;
1099 1100
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1101

1102
    if (!priv->conn) {
1103
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1104 1105 1106
        goto cleanup;
    }

1107
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1108
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1109 1110
        goto cleanup;
    }
1111
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1112
        goto cleanup;
1113
    nparams = args->nparams;
1114

1115
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1116 1117 1118 1119 1120 1121 1122
        goto cleanup;

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

    if (remoteSerializeTypedParameters(params, nparams,
1123
                                       &ret->params.params_val,
1124 1125
                                       &ret->params.params_len,
                                       args->flags) < 0)
1126
        goto cleanup;
1127

1128
    rv = 0;
1129 1130

cleanup:
1131
    if (rv < 0)
1132
        virNetMessageSaveError(rerr);
1133
    virTypedParamsFree(params, nparams);
1134 1135 1136
    if (dom)
        virDomainFree(dom);
    return rv;
1137 1138
}

1139
static int
1140 1141
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1142
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1143
                                virNetMessageErrorPtr rerr,
1144 1145
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1146
{
1147
    virDomainPtr dom = NULL;
1148
    struct _virDomainMemoryStat *stats = NULL;
1149 1150
    int nr_stats;
    size_t i;
1151
    int rv = -1;
1152 1153
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1154

1155
    if (!priv->conn) {
1156
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1157
        goto cleanup;
1158 1159
    }

1160
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1161 1162
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1163
        goto cleanup;
1164 1165
    }

1166
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1167
        goto cleanup;
1168 1169

    /* Allocate stats array for making dispatch call */
1170
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1171
        goto cleanup;
1172

1173
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1174
    if (nr_stats < 0)
1175
        goto cleanup;
1176 1177

    /* Allocate return buffer */
1178
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1179
        goto cleanup;
1180 1181 1182 1183 1184 1185 1186

    /* 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;
1187 1188 1189 1190
    rv = 0;

cleanup:
    if (rv < 0)
1191
        virNetMessageSaveError(rerr);
1192 1193
    if (dom)
        virDomainFree(dom);
1194
    VIR_FREE(stats);
1195
    return rv;
1196 1197
}

1198
static int
1199 1200
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1201
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1202
                              virNetMessageErrorPtr rerr,
1203 1204
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1205
{
1206
    virDomainPtr dom = NULL;
1207 1208 1209 1210
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1211
    int rv = -1;
1212 1213
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1214

1215
    if (!priv->conn) {
1216
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1217
        goto cleanup;
1218 1219
    }

1220
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1221
        goto cleanup;
1222 1223 1224 1225 1226 1227
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1228 1229
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1230
        goto cleanup;
1231 1232 1233
    }

    ret->buffer.buffer_len = size;
1234
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1235
        goto cleanup;
1236

1237
    if (virDomainBlockPeek(dom, path, offset, size,
1238
                           ret->buffer.buffer_val, flags) < 0)
1239
        goto cleanup;
1240

1241 1242 1243 1244
    rv = 0;

cleanup:
    if (rv < 0) {
1245
        virNetMessageSaveError(rerr);
1246 1247 1248 1249 1250
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1251 1252
}

1253 1254 1255
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1256
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1257 1258 1259 1260 1261 1262 1263
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_block_stats_flags_args *args,
                                    remote_domain_block_stats_flags_ret *ret)
{
    virTypedParameterPtr params = NULL;
    virDomainPtr dom = NULL;
    const char *path = args->path;
1264
    int nparams = 0;
1265 1266 1267 1268 1269 1270
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1271
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1272 1273 1274 1275 1276 1277 1278
        goto cleanup;
    }

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

1279
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1280
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1281 1282
        goto cleanup;
    }
1283
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1284
        goto cleanup;
1285
    nparams = args->nparams;
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

    if (virDomainBlockStatsFlags(dom, path, 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 block stats. */
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
1301 1302
                                       &ret->params.params_len,
                                       args->flags) < 0)
1303 1304 1305 1306 1307 1308
        goto cleanup;

success:
    rv = 0;

cleanup:
1309
    if (rv < 0)
1310
        virNetMessageSaveError(rerr);
1311
    virTypedParamsFree(params, nparams);
A
ajia@redhat.com 已提交
1312 1313
    if (dom)
        virDomainFree(dom);
1314 1315 1316
    return rv;
}

R
Richard W.M. Jones 已提交
1317
static int
1318 1319
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1320
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1321
                               virNetMessageErrorPtr rerr,
1322 1323
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1324
{
1325
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1326 1327 1328
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1329
    int rv = -1;
1330 1331
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1332

1333
    if (!priv->conn) {
1334
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1335
        goto cleanup;
1336 1337
    }

1338
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1339
        goto cleanup;
R
Richard W.M. Jones 已提交
1340 1341 1342 1343 1344
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1345 1346
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1347
        goto cleanup;
R
Richard W.M. Jones 已提交
1348 1349 1350
    }

    ret->buffer.buffer_len = size;
1351
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1352
        goto cleanup;
R
Richard W.M. Jones 已提交
1353

1354
    if (virDomainMemoryPeek(dom, offset, size,
1355
                            ret->buffer.buffer_val, flags) < 0)
1356
        goto cleanup;
R
Richard W.M. Jones 已提交
1357

1358 1359 1360 1361
    rv = 0;

cleanup:
    if (rv < 0) {
1362
        virNetMessageSaveError(rerr);
1363 1364 1365 1366 1367
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1368 1369
}

1370
static int
1371 1372
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1373
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1374
                                     virNetMessageErrorPtr rerr,
1375 1376
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1377
{
1378 1379
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1380
    int rv = -1;
1381 1382
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1383

1384
    if (!priv->conn) {
1385
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1386
        goto cleanup;
1387 1388
    }

1389
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1390 1391
        goto cleanup;

1392
    if (VIR_ALLOC(seclabel) < 0)
1393 1394 1395 1396 1397 1398
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1399
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1400
        goto cleanup;
1401 1402
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1403

1404 1405 1406 1407
    rv = 0;

cleanup:
    if (rv < 0)
1408
        virNetMessageSaveError(rerr);
1409 1410 1411
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1412
    return rv;
1413 1414
}

M
Marcelo Cerri 已提交
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
static int
remoteDispatchDomainGetSecurityLabelList(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_get_security_label_list_args *args,
                                         remote_domain_get_security_label_list_ret *ret)
{
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabels = NULL;
1425 1426
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

    if ((len = virDomainGetSecurityLabelList(dom, &seclabels)) < 0) {
        ret->ret = len;
        ret->labels.labels_len = 0;
        ret->labels.labels_val = NULL;
        goto done;
    }

1445
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
1446 1447 1448 1449 1450
        goto cleanup;

    for (i = 0; i < len; i++) {
        size_t label_len = strlen(seclabels[i].label) + 1;
        remote_domain_get_security_label_ret *cur = &ret->labels.labels_val[i];
1451
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
            goto cleanup;
        if (virStrcpy(cur->label.label_val, seclabels[i].label, label_len) == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to copy security label"));
            goto cleanup;
        }
        cur->label.label_len = label_len;
        cur->enforcing = seclabels[i].enforcing;
    }
    ret->labels.labels_len = ret->ret = len;

done:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabels);
    return rv;
}

1475
static int
1476 1477
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1478
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1479
                                   virNetMessageErrorPtr rerr,
1480
                                   remote_node_get_security_model_ret *ret)
1481
{
1482
    virSecurityModel secmodel;
1483
    int rv = -1;
1484 1485
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1486

1487
    if (!priv->conn) {
1488
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1489
        goto cleanup;
1490 1491
    }

1492
    memset(&secmodel, 0, sizeof(secmodel));
1493
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1494 1495 1496
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
1497
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
1498 1499 1500 1501
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
1502
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
1503
        goto cleanup;
1504
    strcpy(ret->doi.doi_val, secmodel.doi);
1505

1506 1507 1508 1509
    rv = 0;

cleanup:
    if (rv < 0)
1510
        virNetMessageSaveError(rerr);
1511
    return rv;
1512 1513
}

1514
static int
1515 1516
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1517
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1518
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1519 1520
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1521 1522 1523 1524 1525
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1526 1527
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1528

1529
    if (!priv->conn) {
1530
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1531 1532 1533
        goto cleanup;
    }

1534
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1535 1536 1537
        goto cleanup;

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
1538
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
1539 1540 1541 1542 1543
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
1544
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1545 1546 1547 1548 1549 1550
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->ncpumaps * args->maplen) < 0)
1551
        goto cleanup;
1552

E
Eric Blake 已提交
1553
    if ((num = virDomainGetVcpuPinInfo(dom,
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
                                       args->ncpumaps,
                                       cpumaps,
                                       args->maplen,
                                       args->flags)) < 0)
        goto cleanup;

    ret->num = num;
    /* 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->ncpumaps * args->maplen;
    ret->cpumaps.cpumaps_val = (char *) cpumaps;
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 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
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

static int
remoteDispatchDomainPinEmulator(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client,
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_pin_emulator_args *args)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

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

    rv = 0;

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


static int
remoteDispatchDomainGetEmulatorPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_emulator_pin_info_args *args,
                                       remote_domain_get_emulator_pin_info_ret *ret)
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int r;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

    /* Allocate buffers to take the results */
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maplen) < 0)
1643
        goto cleanup;
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653

    if ((r = virDomainGetEmulatorPinInfo(dom,
                                         cpumaps,
                                         args->maplen,
                                         args->flags)) < 0)
        goto cleanup;

    ret->ret = r;
    ret->cpumaps.cpumaps_len = args->maplen;
    ret->cpumaps.cpumaps_val = (char *) cpumaps;
1654 1655 1656 1657 1658 1659
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
1660
        virNetMessageSaveError(rerr);
1661 1662 1663 1664 1665 1666
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

1667
static int
1668 1669
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
1670
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
1671
                             virNetMessageErrorPtr rerr,
1672 1673
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1674
{
1675
    virDomainPtr dom = NULL;
1676 1677
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1678 1679
    int info_len;
    size_t i;
1680
    int rv = -1;
1681 1682
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1683

1684
    if (!priv->conn) {
1685
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1686
        goto cleanup;
1687 1688
    }

1689
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1690
        goto cleanup;
1691

1692
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1693
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1694
        goto cleanup;
1695
    }
1696

1697 1698
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1699
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1700 1701 1702 1703 1704
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
1705
        goto cleanup;
1706 1707
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
1708
        goto cleanup;
1709 1710 1711 1712 1713 1714 1715 1716 1717

    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)
1718
        goto cleanup;
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735

    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;
1736 1737

cleanup:
1738
    if (rv < 0) {
1739
        virNetMessageSaveError(rerr);
1740 1741 1742 1743
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
1744 1745 1746
    if (dom)
        virDomainFree(dom);
    return rv;
1747 1748 1749
}

static int
1750 1751
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1752
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1753
                                   virNetMessageErrorPtr rerr,
1754 1755
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
1756
{
1757 1758 1759 1760 1761
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1762
    int rv = -1;
1763 1764
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1765

1766
    if (!priv->conn) {
1767
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1768
        goto cleanup;
1769 1770
    }

1771 1772 1773 1774
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
1775
    if (VIR_ALLOC(uri_out) < 0)
1776
        goto cleanup;
1777

1778
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
1779 1780
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
1781
        goto cleanup;
1782

1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
    /* 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;
    }
1794

1795
    rv = 0;
1796

1797 1798
cleanup:
    if (rv < 0)
1799
        virNetMessageSaveError(rerr);
1800
    VIR_FREE(uri_out);
1801
    return rv;
1802 1803
}

1804
static int
1805 1806
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1807
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1808
                                    virNetMessageErrorPtr rerr,
1809 1810
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
1811
{
1812 1813 1814 1815 1816
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1817
    int rv = -1;
1818 1819
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1820

1821
    if (!priv->conn) {
1822
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1823
        goto cleanup;
1824 1825
    }

1826 1827
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
1828

1829
    /* Wacky world of XDR ... */
1830
    if (VIR_ALLOC(uri_out) < 0)
1831
        goto cleanup;
1832

1833
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
1834 1835 1836
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
1837
        goto cleanup;
1838

1839 1840 1841 1842 1843 1844
    /* 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;
1845

1846 1847 1848 1849
    rv = 0;

cleanup:
    if (rv < 0)
1850
        virNetMessageSaveError(rerr);
1851
    return rv;
1852 1853
}

C
Chris Lalancette 已提交
1854
static int
1855 1856
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
1857
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
1858 1859 1860
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
1861
{
1862
    virDomainPtr dom = NULL;
1863
    virTypedParameterPtr params = NULL;
1864
    int nparams = 0;
1865
    unsigned int flags;
1866
    int rv = -1;
1867 1868
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1869

1870
    if (!priv->conn) {
1871
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1872
        goto cleanup;
1873
    }
C
Chris Lalancette 已提交
1874

1875
    flags = args->flags;
C
Chris Lalancette 已提交
1876

1877
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
1878
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1879 1880
        goto cleanup;
    }
1881
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1882
        goto cleanup;
1883
    nparams = args->nparams;
C
Chris Lalancette 已提交
1884

1885
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1886
        goto cleanup;
C
Chris Lalancette 已提交
1887

1888
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
1889
        goto cleanup;
C
Chris Lalancette 已提交
1890

1891 1892 1893 1894 1895 1896
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
1897 1898
    }

1899
    if (remoteSerializeTypedParameters(params, nparams,
1900
                                       &ret->params.params_val,
1901 1902
                                       &ret->params.params_len,
                                       args->flags) < 0)
1903
        goto cleanup;
1904

1905
success:
1906 1907 1908
    rv = 0;

cleanup:
1909
    if (rv < 0)
1910
        virNetMessageSaveError(rerr);
1911
    virTypedParamsFree(params, nparams);
1912 1913 1914
    if (dom)
        virDomainFree(dom);
    return rv;
1915 1916
}

1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
static int
remoteDispatchDomainGetNumaParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_domain_get_numa_parameters_args *args,
                                      remote_domain_get_numa_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1927
    int nparams = 0;
1928 1929 1930 1931 1932 1933
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1934
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1935 1936 1937 1938 1939
        goto cleanup;
    }

    flags = args->flags;

1940
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
1941
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1942 1943
        goto cleanup;
    }
1944
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1945
        goto cleanup;
1946
    nparams = args->nparams;
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973

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

    if (virDomainGetNumaParameters(dom, 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;
    }

    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       flags) < 0)
        goto cleanup;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
1974
    virTypedParamsFree(params, nparams);
1975 1976 1977 1978 1979
    if (dom)
        virDomainFree(dom);
    return rv;
}

1980
static int
1981 1982
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
1983
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
1984 1985 1986
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
1987
{
1988
    virDomainPtr dom = NULL;
1989
    virTypedParameterPtr params = NULL;
1990
    int nparams = 0;
1991
    unsigned int flags;
1992
    int rv = -1;
1993 1994
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1995

1996
    if (!priv->conn) {
1997
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1998
        goto cleanup;
1999 2000
    }

2001 2002
    flags = args->flags;

2003
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2004
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2005
        goto cleanup;
2006
    }
2007
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2008
        goto cleanup;
2009
    nparams = args->nparams;
2010

2011
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2012
        goto cleanup;
2013

2014
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2015
        goto cleanup;
2016

2017 2018 2019 2020 2021 2022 2023 2024
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2025
    if (remoteSerializeTypedParameters(params, nparams,
2026
                                       &ret->params.params_val,
2027 2028
                                       &ret->params.params_len,
                                       args->flags) < 0)
2029
        goto cleanup;
2030

2031 2032
success:
    rv = 0;
2033

2034
cleanup:
2035
    if (rv < 0)
2036
        virNetMessageSaveError(rerr);
2037
    virTypedParamsFree(params, nparams);
2038 2039 2040
    if (dom)
        virDomainFree(dom);
    return rv;
2041 2042
}

2043
static int
2044 2045
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2046
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2047 2048 2049
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2050
{
2051
    virNodeCPUStatsPtr params = NULL;
2052
    size_t i;
2053
    int cpuNum = args->cpuNum;
2054
    int nparams = 0;
2055 2056
    unsigned int flags;
    int rv = -1;
2057 2058
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2059

2060
    if (!priv->conn) {
2061
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2062 2063 2064 2065 2066
        goto cleanup;
    }

    flags = args->flags;

2067
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2068
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2069 2070
        goto cleanup;
    }
2071
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2072
        goto cleanup;
2073
    nparams = args->nparams;
2074

2075
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
        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)
2089
        goto cleanup;
2090 2091 2092

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2093 2094
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2095 2096 2097 2098 2099 2100 2101

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

success:
    rv = 0;

2102 2103
cleanup:
    if (rv < 0) {
2104
        virNetMessageSaveError(rerr);
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
        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;
}

static int
2116 2117
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2118
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2119 2120 2121
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2122
{
2123
    virNodeMemoryStatsPtr params = NULL;
2124
    size_t i;
2125
    int cellNum = args->cellNum;
2126
    int nparams = 0;
2127 2128
    unsigned int flags;
    int rv = -1;
2129 2130
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2131

2132
    if (!priv->conn) {
2133
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2134 2135 2136 2137 2138
        goto cleanup;
    }

    flags = args->flags;

2139
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2140
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2141 2142
        goto cleanup;
    }
2143
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2144
        goto cleanup;
2145
    nparams = args->nparams;
2146

2147
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
        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)
2161
        goto cleanup;
2162 2163 2164

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2165 2166
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2167 2168 2169 2170 2171 2172 2173

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

success:
    rv = 0;

2174 2175
cleanup:
    if (rv < 0) {
2176
        virNetMessageSaveError(rerr);
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
        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;
}

2187 2188 2189
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2190
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_get_block_job_info_args *args,
                                    remote_domain_get_block_job_info_ret *ret)
{
    virDomainPtr dom = NULL;
    virDomainBlockJobInfo tmp;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2202
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
        goto cleanup;
    }

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

    rv = virDomainGetBlockJobInfo(dom, args->path, &tmp, args->flags);
    if (rv <= 0)
        goto cleanup;

    ret->type = tmp.type;
    ret->bandwidth = tmp.bandwidth;
    ret->cur = tmp.cur;
    ret->end = tmp.end;
    ret->found = 1;
    rv = 0;

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

2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
static int
remoteDispatchDomainGetBlockIoTune(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                   virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_get_block_io_tune_args *args,
                                   remote_domain_get_block_io_tune_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    virTypedParameterPtr params = NULL;
2239
    int nparams = 0;
2240 2241 2242 2243
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2244
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2245 2246 2247
        goto cleanup;
    }

2248
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2249
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2250 2251 2252
        goto cleanup;
    }

2253
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2254
        goto cleanup;
2255
    nparams = args->nparams;
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284

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

    if (virDomainGetBlockIoTune(dom, args->disk ? *args->disk : NULL,
                                params, &nparams, args->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 block I/O tuning parameters. */
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       args->flags) < 0)
        goto cleanup;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
2285
    virTypedParamsFree(params, nparams);
2286 2287 2288 2289
    if (dom)
        virDomainFree(dom);
    return rv;
}
2290

D
Daniel Veillard 已提交
2291 2292
/*-------------------------------------------------------------*/

2293
static int
2294 2295
remoteDispatchAuthList(virNetServerPtr server ATTRIBUTE_UNUSED,
                       virNetServerClientPtr client,
2296
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2297
                       virNetMessageErrorPtr rerr,
2298
                       remote_auth_list_ret *ret)
2299
{
2300
    int rv = -1;
2301 2302
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2303
    gid_t callerGid;
2304
    pid_t callerPid;
2305
    unsigned long long timestamp;
2306 2307 2308 2309 2310 2311

    /* If the client is root then we want to bypass the
     * policykit auth to avoid root being denied if
     * some piece of polkit isn't present/running
     */
    if (auth == VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2312
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2313
                                              &callerPid, &timestamp) < 0) {
2314 2315 2316 2317
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2318 2319
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2320
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2321 2322
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2323 2324
            virNetServerClientSetAuth(client, 0);
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2325
            VIR_FREE(ident);
2326 2327
        }
    }
2328

2329
    ret->types.types_len = 1;
2330
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2331
        goto cleanup;
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345

    switch (auth) {
    case VIR_NET_SERVER_SERVICE_AUTH_NONE:
        ret->types.types_val[0] = REMOTE_AUTH_NONE;
        break;
    case VIR_NET_SERVER_SERVICE_AUTH_POLKIT:
        ret->types.types_val[0] = REMOTE_AUTH_POLKIT;
        break;
    case VIR_NET_SERVER_SERVICE_AUTH_SASL:
        ret->types.types_val[0] = REMOTE_AUTH_SASL;
        break;
    default:
        ret->types.types_val[0] = REMOTE_AUTH_NONE;
    }
2346

2347 2348 2349 2350
    rv = 0;

cleanup:
    if (rv < 0)
2351
        virNetMessageSaveError(rerr);
2352
    return rv;
2353 2354 2355
}


2356
#ifdef WITH_SASL
2357 2358
/*
 * Initializes the SASL session in prepare for authentication
2359
 * and gives the client a list of allowed mechanisms to choose
2360 2361
 */
static int
2362 2363
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2364
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2365
                           virNetMessageErrorPtr rerr,
2366
                           remote_auth_sasl_init_ret *ret)
2367
{
2368 2369 2370
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2371

2372
    virMutexLock(&priv->lock);
2373

2374 2375 2376
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2377
        VIR_ERROR(_("client tried invalid SASL init request"));
2378
        goto authfail;
2379 2380
    }

2381 2382 2383 2384 2385
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2386
        goto authfail;
2387

2388
# if WITH_GNUTLS
2389
    /* Inform SASL that we've got an external SSF layer from TLS */
2390 2391 2392 2393
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2394
            goto authfail;
2395 2396 2397 2398 2399

        ssf *= 8; /* key size is bytes, sasl wants bits */

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2400
            goto authfail;
2401
    }
2402
# endif
2403

2404
    if (virNetServerClientIsSecure(client))
2405
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2406 2407
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2408
        /* Plain TCP, better get an SSF layer */
2409 2410 2411 2412
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2413

2414
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2415
        goto authfail;
2416
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2417

2418 2419
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2420
    return 0;
2421 2422

authfail:
2423
    virResetLastError();
2424 2425
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2426
    virNetMessageSaveError(rerr);
2427 2428 2429
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2430
    virObjectUnref(sasl);
2431
    virMutexUnlock(&priv->lock);
2432
    return -1;
2433 2434
}

2435
/*
2436 2437
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2438
static int
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
remoteSASLFinish(virNetServerClientPtr client)
{
    const char *identity;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    int ssf;

    /* TLS or UNIX domain sockets trivially OK */
    if (!virNetServerClientIsSecure(client)) {
        if ((ssf = virNetSASLSessionGetKeySize(priv->sasl)) < 0)
            goto error;

        VIR_DEBUG("negotiated an SSF of %d", ssf);
        if (ssf < 56) { /* 56 is good for Kerberos */
            VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
            return -2;
        }
2455
    }
2456 2457

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2458
        return -2;
2459

2460 2461
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2462

2463
    virNetServerClientSetAuth(client, 0);
2464
    virNetServerClientSetSASLSession(client, priv->sasl);
2465

2466
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2467

2468 2469 2470
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2471

2472
    virObjectUnref(priv->sasl);
2473
    priv->sasl = NULL;
2474

2475
    return 0;
2476

2477 2478 2479
error:
    return -1;
}
2480

2481 2482 2483 2484
/*
 * This starts the SASL authentication negotiation.
 */
static int
2485 2486
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
2487
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2488
                            virNetMessageErrorPtr rerr,
2489 2490
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2491 2492
{
    const char *serverout;
2493
    size_t serveroutlen;
2494
    int err;
2495 2496 2497
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2498
    const char *identity;
2499

2500
    virMutexLock(&priv->lock);
2501

2502 2503 2504
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2505
        VIR_ERROR(_("client tried invalid SASL start request"));
2506
        goto authfail;
2507 2508
    }

2509 2510
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2511 2512 2513 2514 2515 2516 2517 2518 2519
    err = virNetSASLSessionServerStart(priv->sasl,
                                       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 != VIR_NET_SASL_COMPLETE &&
        err != VIR_NET_SASL_CONTINUE)
2520
        goto authfail;
2521

2522
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2523
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2524
        goto authfail;
2525 2526 2527 2528
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2529 2530
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2531 2532 2533 2534 2535 2536 2537
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2538
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2539
    if (err == VIR_NET_SASL_CONTINUE) {
2540 2541
        ret->complete = 0;
    } else {
2542
        /* Check username whitelist ACL */
2543
        if ((err = remoteSASLFinish(client)) < 0) {
2544 2545 2546 2547 2548
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2549

2550 2551 2552
        ret->complete = 1;
    }

2553
    virMutexUnlock(&priv->lock);
2554
    return 0;
2555 2556

authfail:
2557 2558 2559
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2560 2561 2562
    goto error;

authdeny:
2563
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2564 2565 2566
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2567 2568
    goto error;

2569
error:
2570
    virObjectUnref(priv->sasl);
2571 2572
    priv->sasl = NULL;
    virResetLastError();
2573 2574
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2575 2576 2577
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2578
    return -1;
2579 2580 2581 2582
}


static int
2583 2584
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2585
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2586
                           virNetMessageErrorPtr rerr,
2587 2588
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2589 2590
{
    const char *serverout;
2591
    size_t serveroutlen;
2592
    int err;
2593 2594 2595
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2596
    const char *identity;
2597

2598 2599 2600 2601 2602
    virMutexLock(&priv->lock);

    VIR_DEBUG("Step SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2603
        VIR_ERROR(_("client tried invalid SASL start request"));
2604
        goto authfail;
2605 2606
    }

2607
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
2608
              args->data.data_len, args->nil);
2609 2610 2611 2612 2613 2614 2615 2616
    err = virNetSASLSessionServerStep(priv->sasl,
                                      /* NB, distinction of NULL vs "" is *critical* in SASL */
                                      args->nil ? NULL : args->data.data_val,
                                      args->data.data_len,
                                      &serverout,
                                      &serveroutlen);
    if (err != VIR_NET_SASL_COMPLETE &&
        err != VIR_NET_SASL_CONTINUE)
2617
        goto authfail;
2618 2619

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2620
        VIR_ERROR(_("sasl step reply data too long %d"),
2621
                  (int)serveroutlen);
2622
        goto authfail;
2623 2624 2625 2626
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2627 2628
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2629 2630 2631 2632 2633 2634 2635
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2636
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2637
    if (err == VIR_NET_SASL_CONTINUE) {
2638 2639
        ret->complete = 0;
    } else {
2640
        /* Check username whitelist ACL */
2641
        if ((err = remoteSASLFinish(client)) < 0) {
2642 2643 2644 2645 2646
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2647

2648 2649 2650
        ret->complete = 1;
    }

2651
    virMutexUnlock(&priv->lock);
2652
    return 0;
2653 2654

authfail:
2655 2656 2657
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2658 2659 2660
    goto error;

authdeny:
2661
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2662 2663 2664
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2665 2666
    goto error;

2667
error:
2668
    virObjectUnref(priv->sasl);
2669 2670
    priv->sasl = NULL;
    virResetLastError();
2671 2672
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2673 2674 2675
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2676 2677
    return -1;
}
2678 2679 2680 2681
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2682
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2683 2684 2685 2686
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
2687 2688
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2689 2690 2691 2692 2693 2694
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
2695
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2696 2697 2698 2699 2700
                            virNetMessageErrorPtr rerr,
                            remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
2701 2702
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2703 2704 2705 2706 2707 2708
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2709
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2710 2711 2712 2713 2714
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
2715 2716
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2717 2718 2719 2720
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
2721 2722 2723



2724
#if WITH_POLKIT1
2725
static int
2726 2727
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
                         virNetServerClientPtr client,
2728
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2729
                         virNetMessageErrorPtr rerr,
2730
                         remote_auth_polkit_ret *ret)
2731
{
2732
    pid_t callerPid = -1;
2733
    gid_t callerGid = -1;
2734
    uid_t callerUid = -1;
2735
    unsigned long long timestamp;
2736 2737
    const char *action;
    int status = -1;
2738
    char *ident = NULL;
2739
    bool authdismissed = 0;
2740
    bool supportsuid = false;
2741
    char *pkout = NULL;
2742 2743
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2744
    virCommandPtr cmd = NULL;
2745
    static bool polkitInsecureWarned;
2746

2747 2748
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
2749 2750 2751
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

2752
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
2753
    virCommandSetOutputBuffer(cmd, &pkout);
2754
    virCommandSetErrorBuffer(cmd, &pkout);
2755

2756 2757
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2758
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2759 2760 2761
        goto authfail;
    }

2762
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2763
                                          &callerPid, &timestamp) < 0) {
2764 2765 2766
        goto authfail;
    }

2767 2768 2769 2770 2771 2772
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

2773 2774
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2775

2776
    virCommandAddArg(cmd, "--process");
2777 2778 2779 2780 2781 2782
# ifdef PKCHECK_SUPPORTS_UID
    supportsuid = true;
# endif
    if (supportsuid) {
        virCommandAddArgFormat(cmd, "%lld,%llu,%lu",
                               (long long) callerPid, timestamp, (unsigned long) callerUid);
2783
    } else {
2784 2785 2786 2787 2788
        if (!polkitInsecureWarned) {
            VIR_WARN("No support for caller UID with pkcheck. This deployment is known to be insecure.");
            polkitInsecureWarned = true;
        }
        virCommandAddArgFormat(cmd, "%lld,%llu", (long long) callerPid, timestamp);
2789
    }
2790
    virCommandAddArg(cmd, "--allow-user-interaction");
2791

2792
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
2793
                    (long long) callerPid, callerUid) < 0)
2794 2795
        goto authfail;

2796
    if (virCommandRun(cmd, &status) < 0)
2797
        goto authfail;
2798

2799
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
2800
    if (status != 0) {
2801
        char *tmp = virProcessTranslateStatus(status);
2802 2803
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d: %s"),
                  action, (long long) callerPid, callerUid, NULLSTR(tmp));
2804
        VIR_FREE(tmp);
2805
        goto authdeny;
2806
    }
2807 2808 2809
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2810 2811
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
2812 2813
    ret->complete = 1;

2814
    virNetServerClientSetAuth(client, 0);
2815
    virMutexUnlock(&priv->lock);
2816
    virCommandFree(cmd);
E
Eric Blake 已提交
2817
    VIR_FREE(pkout);
2818
    VIR_FREE(ident);
2819

2820 2821
    return 0;

2822
error:
2823 2824
    virCommandFree(cmd);
    VIR_FREE(ident);
2825
    virResetLastError();
2826

2827
    if (authdismissed) {
2828 2829
        virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
                       _("authentication cancelled by user"));
2830 2831
    } else if (pkout && *pkout) {
        virReportError(VIR_ERR_AUTH_FAILED, _("polkit: %s"), pkout);
2832
    } else {
2833
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed"));
2834
    }
2835 2836

    VIR_FREE(pkout);
2837 2838 2839 2840
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

2841
authfail:
2842 2843 2844
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2845 2846 2847
    goto error;

authdeny:
2848 2849
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
2850
          client, REMOTE_AUTH_POLKIT, ident);
2851
    goto error;
2852
}
2853
#elif WITH_POLKIT0
2854
static int
2855
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
2856
                         virNetServerClientPtr client,
2857
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2858
                         virNetMessageErrorPtr rerr,
2859
                         remote_auth_polkit_ret *ret)
2860 2861
{
    pid_t callerPid;
2862
    gid_t callerGid;
2863
    uid_t callerUid;
2864 2865 2866 2867 2868 2869
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
2870
    const char *action;
2871
    char *ident = NULL;
2872 2873
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2874
    DBusConnection *sysbus;
J
Jim Fehlig 已提交
2875
    unsigned long long timestamp;
2876

J
Jim Fehlig 已提交
2877
    virMutexLock(&priv->lock);
2878

J
Jim Fehlig 已提交
2879
    action = virNetServerClientGetReadonly(client) ?
2880 2881
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
2882

2883
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
2884
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2885
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2886
        goto authfail;
2887 2888
    }

2889
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
J
Jim Fehlig 已提交
2890
                                          &callerPid, &timestamp) < 0) {
2891
        VIR_ERROR(_("cannot get peer socket identity"));
2892
        goto authfail;
2893 2894
    }

2895
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
2896
                    (long long) callerPid, callerUid) < 0)
2897 2898
        goto authfail;

2899 2900 2901
    if (!(sysbus = virDBusGetSystemBus()))
        goto authfail;

2902 2903
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2904
    dbus_error_init(&err);
2905
    if (!(pkcaller = polkit_caller_new_from_pid(sysbus,
2906
                                                callerPid, &err))) {
2907
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
2908
        dbus_error_free(&err);
2909
        goto authfail;
2910
    }
2911

2912
    if (!(pkaction = polkit_action_new())) {
2913
        char ebuf[1024];
2914
        VIR_ERROR(_("Failed to create polkit action %s"),
2915
                  virStrerror(errno, ebuf, sizeof(ebuf)));
2916
        polkit_caller_unref(pkcaller);
2917
        goto authfail;
2918 2919 2920 2921 2922
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
2923
        char ebuf[1024];
2924
        VIR_ERROR(_("Failed to create polkit context %s"),
2925
                  (pkerr ? polkit_error_get_error_message(pkerr)
2926
                   : virStrerror(errno, ebuf, sizeof(ebuf))));
2927 2928 2929 2930 2931
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
2932
        goto authfail;
2933
    }
2934

2935
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
2936 2937 2938 2939 2940 2941
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
2942 2943 2944
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
2945
        goto authfail;
2946
    }
2947
# else
2948 2949 2950
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
2951
# endif
2952 2953 2954 2955
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
2956 2957
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
2958
                  polkit_result_to_string_representation(pkresult));
2959
        goto authdeny;
2960
    }
2961 2962 2963
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2964 2965
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
2966 2967
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
2968

2969
    virNetServerClientSetAuth(client, 0);
J
Jim Fehlig 已提交
2970
    virMutexUnlock(&priv->lock);
2971
    VIR_FREE(ident);
2972
    return 0;
2973

2974
error:
2975
    VIR_FREE(ident);
2976
    virResetLastError();
2977 2978
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2979
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
2980
    virMutexUnlock(&priv->lock);
2981 2982
    return -1;

2983
authfail:
2984 2985 2986
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2987
    goto error;
2988

2989
authdeny:
2990 2991 2992
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2993
    goto error;
2994 2995
}

2996
#else /* !WITH_POLKIT0 & !HAVE_POLKIT1*/
2997 2998

static int
2999
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
3000
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
3001
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3002
                         virNetMessageErrorPtr rerr,
3003
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3004
{
3005
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
3006 3007
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3008
    virNetMessageSaveError(rerr);
3009 3010
    return -1;
}
3011
#endif /* WITH_POLKIT1 */
3012 3013


3014 3015 3016
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3017

3018
static int
3019 3020
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3021
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3022
                                  virNetMessageErrorPtr rerr,
3023 3024
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3025
{
3026 3027
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3028
    int rv = -1;
3029 3030
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3031

3032
    if (!priv->conn) {
3033
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3034
        goto cleanup;
3035 3036
    }

3037
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3038 3039
        goto cleanup;

3040 3041 3042 3043 3044 3045 3046
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3047
        if (VIR_ALLOC(parent_p) < 0)
3048
            goto cleanup;
3049
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3050 3051 3052 3053 3054 3055
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3056 3057 3058 3059
    rv = 0;

cleanup:
    if (rv < 0)
3060
        virNetMessageSaveError(rerr);
3061 3062
    if (dev)
        virNodeDeviceFree(dev);
3063
    return rv;
3064 3065
}

3066 3067 3068 3069 3070

/***************************
 * Register / deregister events
 ***************************/
static int
3071 3072 3073 3074 3075
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3076
{
3077
    int callbackID;
3078
    int rv = -1;
3079 3080
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3081

3082
    if (!priv->conn) {
3083
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3084
        goto cleanup;
3085 3086
    }

3087 3088 3089
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] != -1) {
3090
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
3091
        goto cleanup;
3092
    }
O
Osier Yang 已提交
3093

3094
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3095 3096 3097 3098
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0)
3099
        goto cleanup;
O
Osier Yang 已提交
3100

3101
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
3102

3103 3104 3105 3106
    rv = 0;

cleanup:
    if (rv < 0)
3107 3108
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3109
    return rv;
O
Osier Yang 已提交
3110 3111
}

3112
static int
3113 3114 3115 3116 3117
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3118
{
3119
    int rv = -1;
3120 3121
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3122

3123
    if (!priv->conn) {
3124
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3125
        goto cleanup;
3126 3127
    }

3128 3129 3130
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] < 0) {
3131
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
3132
        goto cleanup;
3133
    }
3134

3135 3136
    if (virConnectDomainEventDeregisterAny(priv->conn,
                                           priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0)
3137
        goto cleanup;
3138

3139 3140
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;

3141 3142 3143
    rv = 0;

cleanup:
3144
    if (rv < 0)
3145 3146
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3147 3148 3149 3150
    return rv;
}

static void
3151 3152
remoteDispatchDomainEventSend(virNetServerClientPtr client,
                              virNetServerProgramPtr program,
3153 3154 3155 3156
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3157
    virNetMessagePtr msg;
3158

3159
    if (!(msg = virNetMessageNew(false)))
3160
        goto cleanup;
3161

3162 3163 3164 3165 3166 3167
    msg->header.prog = virNetServerProgramGetID(program);
    msg->header.vers = virNetServerProgramGetVersion(program);
    msg->header.proc = procnr;
    msg->header.type = VIR_NET_MESSAGE;
    msg->header.serial = 1;
    msg->header.status = VIR_NET_OK;
3168

3169
    if (virNetMessageEncodeHeader(msg) < 0)
3170 3171
        goto cleanup;

3172 3173
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3174

3175 3176
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3177

3178
    xdr_free(proc, data);
3179 3180 3181
    return;

cleanup:
3182
    virNetMessageFree(msg);
3183
    xdr_free(proc, data);
3184 3185
}

3186
static int
3187 3188
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3189
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3190
                             virNetMessageErrorPtr rerr,
3191 3192
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3193
{
3194 3195 3196
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3197
    int rv = -1;
3198 3199
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3200

3201
    if (!priv->conn) {
3202
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3203
        goto cleanup;
3204 3205
    }

3206
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3207
        goto cleanup;
3208

3209
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3210
        goto cleanup;
3211

3212 3213 3214
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3215 3216 3217 3218
    rv = 0;

cleanup:
    if (rv < 0)
3219
        virNetMessageSaveError(rerr);
3220 3221
    if (secret)
        virSecretFree(secret);
3222
    return rv;
3223 3224
}

3225
static int
3226 3227
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3228
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3229
                             virNetMessageErrorPtr rerr,
3230 3231 3232 3233 3234
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3235 3236
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3237

3238
    if (!priv->conn) {
3239
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3240 3241 3242
        goto cleanup;
    }

3243
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3244 3245 3246 3247 3248 3249 3250 3251 3252
        goto cleanup;

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

    rv = 0;

cleanup:
    if (rv < 0)
3253
        virNetMessageSaveError(rerr);
3254 3255 3256 3257 3258
    if (dom)
        virDomainFree(dom);
    return rv;
}

3259
static int
3260 3261 3262 3263 3264
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3265 3266
{
    int callbackID;
3267
    int rv = -1;
3268 3269
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3270

3271
    if (!priv->conn) {
3272
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3273
        goto cleanup;
3274 3275
    }

3276 3277
    virMutexLock(&priv->lock);

3278 3279
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3280
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
3281
        goto cleanup;
3282 3283
    }

3284
    if (priv->domainEventCallbackID[args->eventID] != -1)  {
3285
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), args->eventID);
3286
        goto cleanup;
3287 3288
    }

3289
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3290 3291 3292
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3293
                                                       client, NULL)) < 0)
3294
        goto cleanup;
3295

3296
    priv->domainEventCallbackID[args->eventID] = callbackID;
3297

3298 3299 3300 3301
    rv = 0;

cleanup:
    if (rv < 0)
3302 3303
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3304
    return rv;
3305 3306 3307 3308
}


static int
3309 3310 3311 3312 3313
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3314 3315
{
    int callbackID = -1;
3316
    int rv = -1;
3317 3318
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3319

3320
    if (!priv->conn) {
3321
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3322
        goto cleanup;
3323 3324
    }

3325 3326
    virMutexLock(&priv->lock);

3327 3328
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3329
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
3330
        goto cleanup;
3331 3332
    }

3333 3334
    callbackID = priv->domainEventCallbackID[args->eventID];
    if (callbackID < 0) {
3335
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), args->eventID);
3336
        goto cleanup;
3337 3338
    }

3339
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3340
        goto cleanup;
3341

3342 3343
    priv->domainEventCallbackID[args->eventID] = -1;

3344 3345 3346 3347
    rv = 0;

cleanup:
    if (rv < 0)
3348 3349
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3350
    return rv;
3351 3352
}

C
Chris Lalancette 已提交
3353
static int
3354 3355 3356 3357 3358 3359
qemuDispatchDomainMonitorCommand(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                 virNetMessageErrorPtr rerr,
                                 qemu_domain_monitor_command_args *args,
                                 qemu_domain_monitor_command_ret *ret)
C
Chris Lalancette 已提交
3360
{
3361
    virDomainPtr dom = NULL;
3362
    int rv = -1;
3363 3364
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3365

3366
    if (!priv->conn) {
3367
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3368
        goto cleanup;
3369 3370
    }

3371
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3372
        goto cleanup;
C
Chris Lalancette 已提交
3373

3374
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3375
                                    args->flags) < 0)
3376
        goto cleanup;
C
Chris Lalancette 已提交
3377

3378
    rv = 0;
C
Chris Lalancette 已提交
3379

3380 3381
cleanup:
    if (rv < 0)
3382
        virNetMessageSaveError(rerr);
3383 3384
    if (dom)
        virDomainFree(dom);
3385
    return rv;
C
Chris Lalancette 已提交
3386 3387
}

3388

3389
static int
3390 3391
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3392
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3393
                                  virNetMessageErrorPtr rerr,
3394 3395 3396 3397 3398 3399
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3400
    char *xmlin;
3401 3402 3403
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3404 3405
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3406

3407
    if (!priv->conn) {
3408
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3409 3410 3411
        goto cleanup;
    }

3412
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3413 3414
        goto cleanup;

3415
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3416 3417
    dname = args->dname == NULL ? NULL : *args->dname;

3418
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
                                       &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)
3434
        virNetMessageSaveError(rerr);
3435 3436 3437 3438 3439 3440 3441
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3442 3443
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3444
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3445
                                    virNetMessageErrorPtr rerr,
3446 3447 3448 3449 3450 3451 3452 3453 3454
                                    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;
3455 3456
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3457

3458
    if (!priv->conn) {
3459
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3460 3461 3462 3463 3464 3465 3466
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
3467
    if (VIR_ALLOC(uri_out) < 0)
3468 3469
        goto cleanup;

3470
    if (virDomainMigratePrepare3(priv->conn,
3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489
                                 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) {
3490
        virNetMessageSaveError(rerr);
3491 3492 3493 3494 3495
        VIR_FREE(uri_out);
    }
    return rv;
}

3496

3497
static int
3498 3499
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3500
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3501
                                    virNetMessageErrorPtr rerr,
3502 3503 3504 3505
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
3506
    char *xmlin;
3507
    char *dname;
3508 3509
    char *uri;
    char *dconnuri;
3510 3511 3512
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3513 3514
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3515

3516
    if (!priv->conn) {
3517
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3518 3519 3520
        goto cleanup;
    }

3521
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3522 3523
        goto cleanup;

3524
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3525
    dname = args->dname == NULL ? NULL : *args->dname;
3526 3527
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
3528

3529
    if (virDomainMigratePerform3(dom, xmlin,
3530 3531 3532
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
3533
                                 dconnuri, uri,
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545
                                 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)
3546
        virNetMessageSaveError(rerr);
3547 3548 3549 3550 3551 3552 3553
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3554 3555
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
3556
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
3557
                                   virNetMessageErrorPtr rerr,
3558 3559 3560 3561 3562 3563
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
3564 3565
    char *uri;
    char *dconnuri;
3566
    int rv = -1;
3567 3568
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3569

3570
    if (!priv->conn) {
3571
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3572 3573 3574
        goto cleanup;
    }

3575 3576 3577
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

3578
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
3579 3580 3581 3582 3583 3584
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
3585 3586
        goto cleanup;

3587
    make_nonnull_domain(&ret->dom, dom);
3588 3589 3590 3591 3592 3593 3594 3595 3596 3597

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

    rv = 0;

cleanup:
    if (rv < 0) {
3598
        virNetMessageSaveError(rerr);
3599 3600 3601 3602 3603 3604 3605 3606 3607
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3608 3609
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3610
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3611 3612
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
3613 3614 3615
{
    virDomainPtr dom = NULL;
    int rv = -1;
3616 3617
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3618

3619
    if (!priv->conn) {
3620
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3621 3622 3623
        goto cleanup;
    }

3624
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
        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)
3637
        virNetMessageSaveError(rerr);
3638 3639 3640 3641 3642 3643
    if (dom)
        virDomainFree(dom);
    return rv;
}


3644 3645 3646 3647 3648 3649
static int remoteDispatchConnectSupportsFeature(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                virNetMessageErrorPtr rerr,
                                                remote_connect_supports_feature_args *args,
                                                remote_connect_supports_feature_ret *ret)
3650 3651 3652 3653 3654 3655
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

3656 3657 3658 3659 3660 3661 3662 3663 3664 3665
    /* This feature is checked before opening the connection, thus we must
     * check it first.
     */
    if (args->feature == VIR_DRV_FEATURE_PROGRAM_KEEPALIVE) {
        if (virNetServerClientStartKeepAlive(client) < 0)
            goto cleanup;
        supported = 1;
        goto done;
    }

3666
    if (!priv->conn) {
3667
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3668 3669 3670 3671 3672 3673 3674 3675 3676
        goto cleanup;
    }

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
        supported = 1;
        break;

    default:
3677
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
3678 3679 3680 3681
            goto cleanup;
        break;
    }

3682
done:
3683 3684 3685 3686 3687 3688 3689 3690 3691 3692
    ret->supported = supported;
    rv = 0;

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


3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706
static int
remoteDispatchDomainOpenGraphics(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                 virNetMessagePtr msg,
                                 virNetMessageErrorPtr rerr,
                                 remote_domain_open_graphics_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3707
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733
        goto cleanup;
    }

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

    if ((fd = virNetMessageDupFD(msg, 0)) < 0)
        goto cleanup;

    if (virDomainOpenGraphics(dom,
                              args->idx,
                              fd,
                              args->flags) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744
static int
remoteDispatchDomainGetInterfaceParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr,
                                           remote_domain_get_interface_parameters_args *args,
                                           remote_domain_get_interface_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    const char *device = args->device;
3745
    int nparams = 0;
3746 3747 3748 3749 3750 3751
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3752
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3753 3754 3755 3756 3757
        goto cleanup;
    }

    flags = args->flags;

3758
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
3759
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3760 3761
        goto cleanup;
    }
3762
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3763
        goto cleanup;
3764
    nparams = args->nparams;
3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791

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

    if (virDomainGetInterfaceParameters(dom, device, 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;
    }

    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       flags) < 0)
        goto cleanup;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
3792
    virTypedParamsFree(params, nparams);
3793 3794 3795 3796
    if (dom)
        virDomainFree(dom);
    return rv;
}
3797

3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813
static int
remoteDispatchDomainGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_cpu_stats_args *args,
                                remote_domain_get_cpu_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv;
    virTypedParameterPtr params = NULL;
    int rv = -1;
    int percpu_len = 0;

    priv = virNetServerClientGetPrivateData(client);
    if (!priv->conn) {
3814
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3815 3816 3817 3818
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
3819
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3820 3821 3822
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
3823
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
3824 3825 3826 3827
        goto cleanup;
    }

    if (args->nparams > 0 &&
3828
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851
        goto cleanup;

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

    percpu_len = virDomainGetCPUStats(dom, params, args->nparams,
                                      args->start_cpu, args->ncpus,
                                      args->flags);
    if (percpu_len < 0)
        goto cleanup;
    /* If nparams == 0, the function returns a single value */
    if (args->nparams == 0)
        goto success;

    if (remoteSerializeTypedParameters(params, args->nparams * args->ncpus,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       args->flags) < 0)
        goto cleanup;

success:
    rv = 0;
    ret->nparams = percpu_len;
3852
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
3853
        size_t i;
3854 3855 3856 3857 3858 3859

        for (i = 0; i < percpu_len; i++) {
            if (params[i].type == VIR_TYPED_PARAM_STRING)
                ret->nparams--;
        }
    }
3860 3861 3862 3863

cleanup:
    if (rv < 0)
         virNetMessageSaveError(rerr);
3864
    virTypedParamsFree(params, args->ncpus * args->nparams);
3865 3866 3867 3868 3869
    if (dom)
        virDomainFree(dom);
    return rv;
}

3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880
static int remoteDispatchDomainGetDiskErrors(
    virNetServerPtr server ATTRIBUTE_UNUSED,
    virNetServerClientPtr client,
    virNetMessagePtr msg ATTRIBUTE_UNUSED,
    virNetMessageErrorPtr rerr,
    remote_domain_get_disk_errors_args *args,
    remote_domain_get_disk_errors_ret *ret)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
3881
    int len = 0;
3882 3883 3884 3885
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3886
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3887 3888 3889 3890 3891 3892 3893
        goto cleanup;
    }

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
3894 3895
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
3896 3897 3898 3899
        goto cleanup;
    }

    if (args->maxerrors &&
3900
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922
        goto cleanup;

    if ((len = virDomainGetDiskErrors(dom, errors,
                                      args->maxerrors,
                                      args->flags)) < 0)
        goto cleanup;

    ret->nerrors = len;
    if (errors &&
        remoteSerializeDomainDiskErrors(errors, len,
                                        &ret->errors.errors_val,
                                        &ret->errors.errors_len) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (errors) {
3923
        size_t i;
3924 3925 3926 3927 3928 3929 3930
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

3931 3932 3933 3934 3935 3936 3937 3938 3939 3940
static int
remoteDispatchDomainListAllSnapshots(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_domain_list_all_snapshots_args *args,
                                     remote_domain_list_all_snapshots_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
3941
    size_t i;
3942 3943 3944 3945 3946
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
3947
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
        goto cleanup;
    }

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

    if ((nsnaps = virDomainListAllSnapshots(dom,
                                            args->need_results ? &snaps : NULL,
                                            args->flags)) < 0)
        goto cleanup;

    if (snaps && nsnaps) {
3960
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998
            goto cleanup;

        ret->snapshots.snapshots_len = nsnaps;

        for (i = 0; i < nsnaps; i++)
            make_nonnull_domain_snapshot(ret->snapshots.snapshots_val + i,
                                         snaps[i]);
    } else {
        ret->snapshots.snapshots_len = 0;
        ret->snapshots.snapshots_val = NULL;
    }

    ret->ret = nsnaps;
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (snaps) {
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

static int
remoteDispatchDomainSnapshotListAllChildren(virNetServerPtr server ATTRIBUTE_UNUSED,
                                            virNetServerClientPtr client,
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr,
                                            remote_domain_snapshot_list_all_children_args *args,
                                            remote_domain_snapshot_list_all_children_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
3999
    size_t i;
4000 4001 4002 4003 4004 4005
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4006
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
        goto cleanup;
    }

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

    if (!(snapshot = get_nonnull_domain_snapshot(dom, args->snapshot)))
        goto cleanup;

    if ((nsnaps = virDomainSnapshotListAllChildren(snapshot,
                                                   args->need_results ? &snaps : NULL,
                                                   args->flags)) < 0)
        goto cleanup;

    if (snaps && nsnaps) {
4022
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052
            goto cleanup;

        ret->snapshots.snapshots_len = nsnaps;

        for (i = 0; i < nsnaps; i++)
            make_nonnull_domain_snapshot(ret->snapshots.snapshots_val + i,
                                         snaps[i]);
    } else {
        ret->snapshots.snapshots_len = 0;
        ret->snapshots.snapshots_val = NULL;
    }

    ret->ret = nsnaps;
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (snapshot)
        virDomainSnapshotFree(snapshot);
    if (dom)
        virDomainFree(dom);
    if (snaps) {
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

4053 4054 4055 4056 4057 4058 4059 4060 4061 4062
static int
remoteDispatchConnectListAllStoragePools(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_connect_list_all_storage_pools_args *args,
                                         remote_connect_list_all_storage_pools_ret *ret)
{
    virStoragePoolPtr *pools = NULL;
    int npools = 0;
4063
    size_t i;
4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((npools = virConnectListAllStoragePools(priv->conn,
                                                args->need_results ? &pools : NULL,
                                                args->flags)) < 0)
        goto cleanup;

    if (pools && npools) {
4078
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0)
4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104
            goto cleanup;

        ret->pools.pools_len = npools;

        for (i = 0; i < npools; i++)
            make_nonnull_storage_pool(ret->pools.pools_val + i, pools[i]);
    } else {
        ret->pools.pools_len = 0;
        ret->pools.pools_val = NULL;
    }

    ret->ret = npools;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (pools) {
        for (i = 0; i < npools; i++)
            virStoragePoolFree(pools[i]);
        VIR_FREE(pools);
    }
    return rv;
}

4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115
static int
remoteDispatchStoragePoolListAllVolumes(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_storage_pool_list_all_volumes_args *args,
                                        remote_storage_pool_list_all_volumes_ret *ret)
{
    virStorageVolPtr *vols = NULL;
    virStoragePoolPtr pool = NULL;
    int nvols = 0;
4116
    size_t i;
4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(pool = get_nonnull_storage_pool(priv->conn, args->pool)))
        goto cleanup;

    if ((nvols = virStoragePoolListAllVolumes(pool,
                                              args->need_results ? &vols : NULL,
                                              args->flags)) < 0)
        goto cleanup;

    if (vols && nvols) {
4134
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0)
4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157
            goto cleanup;

        ret->vols.vols_len = nvols;

        for (i = 0; i < nvols; i++)
            make_nonnull_storage_vol(ret->vols.vols_val + i, vols[i]);
    } else {
        ret->vols.vols_len = 0;
        ret->vols.vols_val = NULL;
    }

    ret->ret = nvols;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (vols) {
        for (i = 0; i < nvols; i++)
            virStorageVolFree(vols[i]);
        VIR_FREE(vols);
    }
4158 4159
    if (pool)
        virStoragePoolFree(pool);
4160 4161 4162
    return rv;
}

4163 4164 4165 4166 4167 4168 4169 4170 4171 4172
static int
remoteDispatchConnectListAllNetworks(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_connect_list_all_networks_args *args,
                                     remote_connect_list_all_networks_ret *ret)
{
    virNetworkPtr *nets = NULL;
    int nnets = 0;
4173
    size_t i;
4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((nnets = virConnectListAllNetworks(priv->conn,
                                           args->need_results ? &nets : NULL,
                                           args->flags)) < 0)
        goto cleanup;

    if (nets && nnets) {
4188
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0)
4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214
            goto cleanup;

        ret->nets.nets_len = nnets;

        for (i = 0; i < nnets; i++)
            make_nonnull_network(ret->nets.nets_val + i, nets[i]);
    } else {
        ret->nets.nets_len = 0;
        ret->nets.nets_val = NULL;
    }

    ret->ret = nnets;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (nets) {
        for (i = 0; i < nnets; i++)
            virNetworkFree(nets[i]);
        VIR_FREE(nets);
    }
    return rv;
}

4215 4216 4217 4218 4219 4220 4221 4222 4223 4224
static int
remoteDispatchConnectListAllInterfaces(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_list_all_interfaces_args *args,
                                       remote_connect_list_all_interfaces_ret *ret)
{
    virInterfacePtr *ifaces = NULL;
    int nifaces = 0;
4225
    size_t i;
4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((nifaces = virConnectListAllInterfaces(priv->conn,
                                               args->need_results ? &ifaces : NULL,
                                               args->flags)) < 0)
        goto cleanup;

    if (ifaces && nifaces) {
4240
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0)
4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266
            goto cleanup;

        ret->ifaces.ifaces_len = nifaces;

        for (i = 0; i < nifaces; i++)
            make_nonnull_interface(ret->ifaces.ifaces_val + i, ifaces[i]);
    } else {
        ret->ifaces.ifaces_len = 0;
        ret->ifaces.ifaces_val = NULL;
    }

    ret->ret = nifaces;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (ifaces) {
        for (i = 0; i < nifaces; i++)
            virInterfaceFree(ifaces[i]);
        VIR_FREE(ifaces);
    }
    return rv;
}

4267 4268 4269 4270 4271 4272 4273 4274 4275 4276
static int
remoteDispatchConnectListAllNodeDevices(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_connect_list_all_node_devices_args *args,
                                        remote_connect_list_all_node_devices_ret *ret)
{
    virNodeDevicePtr *devices = NULL;
    int ndevices = 0;
4277
    size_t i;
4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((ndevices = virConnectListAllNodeDevices(priv->conn,
                                                 args->need_results ? &devices : NULL,
                                                 args->flags)) < 0)
        goto cleanup;

    if (devices && ndevices) {
4292
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0)
4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317
            goto cleanup;

        ret->devices.devices_len = ndevices;

        for (i = 0; i < ndevices; i++)
            make_nonnull_node_device(ret->devices.devices_val + i, devices[i]);
    } else {
        ret->devices.devices_len = 0;
        ret->devices.devices_val = NULL;
    }

    ret->ret = ndevices;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (devices) {
        for (i = 0; i < ndevices; i++)
            virNodeDeviceFree(devices[i]);
        VIR_FREE(devices);
    }
    return rv;
}
4318

4319 4320 4321 4322 4323 4324 4325 4326 4327 4328
static int
remoteDispatchConnectListAllNWFilters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_connect_list_all_nwfilters_args *args,
                                      remote_connect_list_all_nwfilters_ret *ret)
{
    virNWFilterPtr *filters = NULL;
    int nfilters = 0;
4329
    size_t i;
4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((nfilters = virConnectListAllNWFilters(priv->conn,
                                               args->need_results ? &filters : NULL,
                                               args->flags)) < 0)
        goto cleanup;

    if (filters && nfilters) {
4344
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0)
4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370
            goto cleanup;

        ret->filters.filters_len = nfilters;

        for (i = 0; i < nfilters; i++)
            make_nonnull_nwfilter(ret->filters.filters_val + i, filters[i]);
    } else {
        ret->filters.filters_len = 0;
        ret->filters.filters_val = NULL;
    }

    ret->ret = nfilters;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (filters) {
        for (i = 0; i < nfilters; i++)
            virNWFilterFree(filters[i]);
        VIR_FREE(filters);
    }
    return rv;
}

4371 4372 4373 4374 4375 4376 4377 4378 4379 4380
static int
remoteDispatchConnectListAllSecrets(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_secrets_args *args,
                                    remote_connect_list_all_secrets_ret *ret)
{
    virSecretPtr *secrets = NULL;
    int nsecrets = 0;
4381
    size_t i;
4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((nsecrets = virConnectListAllSecrets(priv->conn,
                                             args->need_results ? &secrets : NULL,
                                             args->flags)) < 0)
        goto cleanup;

    if (secrets && nsecrets) {
4396
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0)
4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422
            goto cleanup;

        ret->secrets.secrets_len = nsecrets;

        for (i = 0; i < nsecrets; i++)
            make_nonnull_secret(ret->secrets.secrets_val + i, secrets[i]);
    } else {
        ret->secrets.secrets_len = 0;
        ret->secrets.secrets_val = NULL;
    }

    ret->ret = nsecrets;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (secrets) {
        for (i = 0; i < nsecrets; i++)
            virSecretFree(secrets[i]);
        VIR_FREE(secrets);
    }
    return rv;
}

4423 4424 4425 4426 4427 4428 4429 4430 4431
static int
remoteDispatchNodeGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_node_get_memory_parameters_args *args,
                                      remote_node_get_memory_parameters_ret *ret)
{
    virTypedParameterPtr params = NULL;
4432
    int nparams = 0;
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

4445
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
4446 4447 4448
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
4449
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4450
        goto cleanup;
4451
    nparams = args->nparams;
4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475

    if (virNodeGetMemoryParameters(priv->conn, 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;
    }

    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       args->flags) < 0)
        goto cleanup;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
4476
    virTypedParamsFree(params, nparams);
4477 4478 4479
    return rv;
}

4480 4481 4482 4483 4484 4485 4486 4487 4488
static int
remoteDispatchNodeGetCPUMap(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                            virNetMessageErrorPtr rerr,
                            remote_node_get_cpu_map_args *args,
                            remote_node_get_cpu_map_ret *ret)
{
    unsigned char *cpumap = NULL;
4489
    unsigned int online = 0;
4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502
    unsigned int flags;
    int cpunum;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

4503 4504
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
4505 4506 4507 4508
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
4509
    if (args->need_map) {
4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

    ret->online = online;
    ret->ret = cpunum;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559
static int
lxcDispatchDomainOpenNamespace(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                               virNetMessageErrorPtr rerr,
                               lxc_domain_open_namespace_args *args)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *fdlist = NULL;
    int ret;
    virDomainPtr dom = NULL;
    size_t i;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

    ret = virDomainLxcOpenNamespace(dom,
                                    &fdlist,
                                    args->flags);
    if (ret < 0)
        goto cleanup;

    /* We shouldn't have received any from the client,
     * but in case they're playing games with us, prevent
     * a resource leak
     */
4560
    for (i = 0; i < msg->nfds; i++)
4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

    msg->fds = fdlist;
    msg->nfds = ret;

    rv = 1;

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

4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620
static int
remoteDispatchDomainGetJobStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client,
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_job_stats_args *args,
                                remote_domain_get_job_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

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

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

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
    if (dom)
        virDomainFree(dom);
    return rv;
}

4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644
static int
remoteDispatchDomainMigrateBegin3Params(
        virNetServerPtr server ATTRIBUTE_UNUSED,
        virNetServerClientPtr client ATTRIBUTE_UNUSED,
        virNetMessagePtr msg ATTRIBUTE_UNUSED,
        virNetMessageErrorPtr rerr,
        remote_domain_migrate_begin3_params_args *args,
        remote_domain_migrate_begin3_params_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

4645 4646 4647 4648 4649 4650 4651
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    if (!(xml = virDomainMigrateBegin3Params(dom, params, nparams,
                                             &cookieout, &cookieoutlen,
                                             args->flags)))
        goto cleanup;

    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;
    ret->xml = xml;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

static int
remoteDispatchDomainMigratePrepare3Params(
        virNetServerPtr server ATTRIBUTE_UNUSED,
        virNetServerClientPtr client ATTRIBUTE_UNUSED,
        virNetMessagePtr msg ATTRIBUTE_UNUSED,
        virNetMessageErrorPtr rerr,
        remote_domain_migrate_prepare3_params_args *args,
        remote_domain_migrate_prepare3_params_ret *ret)
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char **uri_out;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

4703 4704 4705 4706 4707 4708 4709
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

4710 4711 4712 4713 4714 4715
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    /* Wacky world of XDR ... */
4716
    if (VIR_ALLOC(uri_out) < 0)
4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764
        goto cleanup;

    if (virDomainMigratePrepare3Params(priv->conn, params, nparams,
                                       args->cookie_in.cookie_in_val,
                                       args->cookie_in.cookie_in_len,
                                       &cookieout, &cookieoutlen,
                                       uri_out, args->flags) < 0)
        goto cleanup;

    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;
    ret->uri_out = !*uri_out ? NULL : uri_out;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
remoteDispatchDomainMigratePrepareTunnel3Params(
        virNetServerPtr server ATTRIBUTE_UNUSED,
        virNetServerClientPtr client,
        virNetMessagePtr msg,
        virNetMessageErrorPtr rerr,
        remote_domain_migrate_prepare_tunnel3_params_args *args,
        remote_domain_migrate_prepare_tunnel3_params_ret *ret)
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virStreamPtr st = NULL;
    daemonClientStreamPtr stream = NULL;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

4765 4766 4767 4768 4769 4770 4771
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    if (!(st = virStreamNew(priv->conn, VIR_STREAM_NONBLOCK)) ||
        !(stream = daemonCreateClientStream(client, st, remoteProgram,
                                            &msg->header)))
        goto cleanup;

    if (virDomainMigratePrepareTunnel3Params(priv->conn, st, params, nparams,
                                             args->cookie_in.cookie_in_val,
                                             args->cookie_in.cookie_in_len,
                                             &cookieout, &cookieoutlen,
                                             args->flags) < 0)
        goto cleanup;

    if (daemonAddClientStream(client, stream, false) < 0)
        goto cleanup;

    ret->cookie_out.cookie_out_val = cookieout;
    ret->cookie_out.cookie_out_len = cookieoutlen;
    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
            virStreamFree(st);
        }
    }
    return rv;
}


static int
remoteDispatchDomainMigratePerform3Params(
        virNetServerPtr server ATTRIBUTE_UNUSED,
        virNetServerClientPtr client ATTRIBUTE_UNUSED,
        virNetMessagePtr msg ATTRIBUTE_UNUSED,
        virNetMessageErrorPtr rerr,
        remote_domain_migrate_perform3_params_args *args,
        remote_domain_migrate_perform3_params_ret *ret)
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char *dconnuri;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

4836 4837 4838 4839 4840 4841 4842
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

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

    if (virDomainMigratePerform3Params(dom, dconnuri, params, nparams,
                                       args->cookie_in.cookie_in_val,
                                       args->cookie_in.cookie_in_len,
                                       &cookieout, &cookieoutlen,
                                       args->flags) < 0)
        goto cleanup;

    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
remoteDispatchDomainMigrateFinish3Params(
        virNetServerPtr server ATTRIBUTE_UNUSED,
        virNetServerClientPtr client ATTRIBUTE_UNUSED,
        virNetMessagePtr msg ATTRIBUTE_UNUSED,
        virNetMessageErrorPtr rerr,
        remote_domain_migrate_finish3_params_args *args,
        remote_domain_migrate_finish3_params_ret *ret)
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

4898 4899 4900 4901 4902 4903 4904
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    dom = virDomainMigrateFinish3Params(priv->conn, params, nparams,
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        args->flags, args->cancelled);
    if (!dom)
        goto cleanup;

    make_nonnull_domain(&ret->dom, dom);

    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
remoteDispatchDomainMigrateConfirm3Params(
        virNetServerPtr server ATTRIBUTE_UNUSED,
        virNetServerClientPtr client ATTRIBUTE_UNUSED,
        virNetMessagePtr msg ATTRIBUTE_UNUSED,
        virNetMessageErrorPtr rerr,
        remote_domain_migrate_confirm3_params_args *args)
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

4957 4958 4959 4960 4961 4962 4963
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

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

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093
static int remoteDispatchDomainCreateXMLWithFiles(
    virNetServerPtr server ATTRIBUTE_UNUSED,
    virNetServerClientPtr client,
    virNetMessagePtr msg ATTRIBUTE_UNUSED,
    virNetMessageErrorPtr rerr,
    remote_domain_create_xml_with_files_args *args,
    remote_domain_create_xml_with_files_ret *ret)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (VIR_ALLOC_N(files, msg->nfds) < 0)
        goto cleanup;
    for (i = 0; i < msg->nfds; i++) {
        if ((files[i] = virNetMessageDupFD(msg, i)) < 0)
            goto cleanup;
        nfiles++;
    }

    if ((dom = virDomainCreateXMLWithFiles(priv->conn, args->xml_desc,
                                           nfiles, files,
                                           args->flags)) == NULL)
        goto cleanup;

    make_nonnull_domain(&ret->dom, dom);
    rv = 0;

cleanup:
    for (i = 0; i < nfiles; i++) {
        VIR_FORCE_CLOSE(files[i]);
    }
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int remoteDispatchDomainCreateWithFiles(
    virNetServerPtr server ATTRIBUTE_UNUSED,
    virNetServerClientPtr client,
    virNetMessagePtr msg ATTRIBUTE_UNUSED,
    virNetMessageErrorPtr rerr,
    remote_domain_create_with_files_args *args,
    remote_domain_create_with_files_ret *ret)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (VIR_ALLOC_N(files, msg->nfds) < 0)
        goto cleanup;
    for (i = 0; i < msg->nfds; i++) {
        if ((files[i] = virNetMessageDupFD(msg, i)) < 0)
            goto cleanup;
        nfiles++;
    }

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

    if (virDomainCreateWithFiles(dom,
                                 nfiles, files,
                                 args->flags) < 0)
        goto cleanup;

    make_nonnull_domain(&ret->dom, dom);
    rv = 0;

cleanup:
    for (i = 0; i < nfiles; i++) {
        VIR_FORCE_CLOSE(files[i]);
    }
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}



5094 5095 5096 5097 5098 5099 5100 5101 5102
/*----- 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
5103
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
5104 5105
{
    virDomainPtr dom;
5106
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
5107 5108 5109 5110 5111 5112 5113 5114
    /* 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
5115
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
5116
{
5117
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
5118 5119
}

D
Daniel Veillard 已提交
5120
static virInterfacePtr
5121
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
5122
{
5123
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
5124 5125
}

5126
static virStoragePoolPtr
5127
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
5128
{
5129 5130
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
5131 5132 5133
}

static virStorageVolPtr
5134
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
5135 5136
{
    virStorageVolPtr ret;
5137 5138
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
5139 5140 5141
    return ret;
}

5142
static virSecretPtr
5143
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
5144
{
5145
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
5146 5147
}

5148
static virNWFilterPtr
5149
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
5150
{
5151
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
5152 5153
}

C
Chris Lalancette 已提交
5154
static virDomainSnapshotPtr
5155
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
5156
{
5157
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
5158 5159
}

5160 5161
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
5162
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
5163 5164
{
    dom_dst->id = dom_src->id;
5165
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
5166
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
5167 5168 5169
}

static void
5170
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
5171
{
5172
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
5173
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
5174 5175
}

D
Daniel Veillard 已提交
5176
static void
5177 5178
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
5179
{
5180 5181
    ignore_value(VIR_STRDUP_QUIET(interface_dst->name, interface_src->name));
    ignore_value(VIR_STRDUP_QUIET(interface_dst->mac, interface_src->mac));
D
Daniel Veillard 已提交
5182 5183
}

5184
static void
5185
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
5186
{
5187
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
5188
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
5189 5190 5191
}

static void
5192
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
5193
{
5194 5195 5196
    ignore_value(VIR_STRDUP_QUIET(vol_dst->pool, vol_src->pool));
    ignore_value(VIR_STRDUP_QUIET(vol_dst->name, vol_src->name));
    ignore_value(VIR_STRDUP_QUIET(vol_dst->key, vol_src->key));
5197
}
5198 5199

static void
5200
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
5201
{
5202
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
5203
}
5204 5205

static void
5206
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
5207
{
5208
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
5209
    secret_dst->usageType = secret_src->usageType;
5210
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
5211
}
5212 5213

static void
5214
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
5215
{
5216
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
5217
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
5218
}
C
Chris Lalancette 已提交
5219 5220

static void
5221
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
5222
{
5223
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
5224
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
5225
}
5226 5227 5228 5229 5230 5231 5232 5233

static int
remoteSerializeDomainDiskErrors(virDomainDiskErrorPtr errors,
                                int nerrors,
                                remote_domain_disk_error **ret_errors_val,
                                u_int *ret_errors_len)
{
    remote_domain_disk_error *val = NULL;
5234
    size_t i = 0;
5235

5236
    if (VIR_ALLOC_N(val, nerrors) < 0)
5237
        goto error;
5238 5239

    for (i = 0; i < nerrors; i++) {
5240 5241
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
5242 5243 5244 5245 5246 5247 5248 5249
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

5250
error:
5251
    if (val) {
5252
        size_t j;
5253 5254 5255 5256 5257 5258
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}