remote.c 150.9 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2012 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 307 308 309 310 311
    remote_domain_event_graphics_msg data;
    int i;

    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 336 337 338
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0) {
        virReportOOMError();
        goto error;
    }
339

340
    for (i = 0; i < data.subject.subject_len; i++) {
341 342 343
        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;
344
    }
345
    make_nonnull_domain(&data.dom, dom);
346

347
    remoteDispatchDomainEventSend(client, remoteProgram,
348 349
                                  REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                  (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
350 351

    return 0;
352

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

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
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 */
386
    memset(&data, 0, sizeof(data));
387 388
    if (VIR_STRDUP(data.path, path) < 0)
        goto error;
389 390
    data.type = type;
    data.status = status;
391
    make_nonnull_domain(&data.dom, dom);
392 393 394 395 396 397

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

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

403

404 405 406 407
static int remoteRelayDomainEventControlError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                              virDomainPtr dom,
                                              void *opaque)
{
408
    virNetServerClientPtr client = opaque;
409 410 411 412 413 414 415 416
    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 */
417
    memset(&data, 0, sizeof(data));
418 419
    make_nonnull_domain(&data.dom, dom);

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

    return 0;
}


428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
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 */
447
    memset(&data, 0, sizeof(data));
448 449
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
450
         VIR_STRDUP(*oldSrcPath_p, oldSrcPath) < 0))
451 452 453 454
        goto mem_error;

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
455
         VIR_STRDUP(*newSrcPath_p, newSrcPath) < 0))
456 457 458 459
        goto mem_error;

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
460 461
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
462 463 464 465 466 467 468 469 470 471 472
    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;

mem_error:
473 474
    virReportOOMError();
error:
M
Michal Privoznik 已提交
475 476
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
477 478 479 480
    return -1;
}


481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
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 */
496
    memset(&data, 0, sizeof(data));
497

498
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
499 500 501 502 503 504 505 506 507 508 509 510
        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 已提交
511 512
static int remoteRelayDomainEventPMWakeup(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
513
                                          int reason ATTRIBUTE_UNUSED,
O
Osier Yang 已提交
514 515 516 517 518 519 520 521 522 523
                                          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 */
524
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
525 526 527 528 529 530 531 532 533
    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 已提交
534 535
static int remoteRelayDomainEventPMSuspend(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
536
                                           int reason ATTRIBUTE_UNUSED,
O
Osier Yang 已提交
537 538 539 540 541 542 543 544 545 546
                                           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 */
547
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
548 549 550 551 552 553 554 555 556
    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;
}

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
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;
}


584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
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;
}


608
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
609
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
610
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
611
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
612
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
613
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
614
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
615
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
616
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
617
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
618
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
619
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
620
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
621
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
622
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
623
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
624 625 626 627
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

628 629 630 631 632 633 634
/*
 * 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
 */
635
void remoteClientFreeFunc(void *data)
636 637 638 639 640 641 642
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
        int i;

643
        for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) {
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
            if (priv->domainEventCallbackID[i] != -1) {
                VIR_DEBUG("Deregistering to relay remote events %d", i);
                virConnectDomainEventDeregisterAny(priv->conn,
                                                   priv->domainEventCallbackID[i]);
            }
            priv->domainEventCallbackID[i] = -1;
        }

        virConnectClose(priv->conn);
    }

    VIR_FREE(priv);
}


659 660 661 662 663 664 665
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

666

667 668
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
669 670 671 672 673 674
{
    struct daemonClientPrivate *priv;
    int i;

    if (VIR_ALLOC(priv) < 0) {
        virReportOOMError();
675
        return NULL;
676 677 678 679 680
    }

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
        virReportOOMError();
681
        return NULL;
682 683
    }

684
    for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++)
685 686
        priv->domainEventCallbackID[i] = -1;

687
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
688
    return priv;
689 690
}

691 692 693
/*----- Functions. -----*/

static int
694 695 696 697 698
remoteDispatchConnectOpen(virNetServerPtr server,
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
699 700
{
    const char *name;
701
    unsigned int flags;
702
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
703
    int rv = -1;
704

705 706 707 708
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
709
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
710 711 712
        goto cleanup;
    }

713
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
714 715
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
716 717 718
        goto cleanup;
    }

719 720 721 722 723 724
    name = args->name ? *args->name : NULL;

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

728
    priv->conn =
729
        flags & VIR_CONNECT_RO
730 731
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
732

733
    if (priv->conn == NULL)
734 735 736
        goto cleanup;

    rv = 0;
737

738 739
cleanup:
    if (rv < 0)
740 741
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
742
    return rv;
743 744 745 746
}


static int
747 748 749 750
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
751
{
752
    virNetServerClientDelayedClose(client);
753
    return 0;
754 755
}

756

757
static int
758 759
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
760
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
761
                                     virNetMessageErrorPtr rerr,
762 763
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
764
{
765
    virDomainPtr dom = NULL;
766 767
    char *type;
    int nparams;
768
    int rv = -1;
769 770
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
771

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

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

780
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
781
        goto cleanup;
782 783 784

    ret->type = type;
    ret->nparams = nparams;
785 786 787 788
    rv = 0;

cleanup:
    if (rv < 0)
789
        virNetMessageSaveError(rerr);
790 791 792
    if (dom)
        virDomainFree(dom);
    return rv;
793 794
}

795 796
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
797 798 799
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
800
                               remote_typed_param **ret_params_val,
801 802
                               u_int *ret_params_len,
                               unsigned int flags)
803 804
{
    int i;
805
    int j;
806 807 808 809 810 811 812 813 814
    int rv = -1;
    remote_typed_param *val;

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

815
    for (i = 0, j = 0; i < nparams; ++i) {
816 817 818 819 820
        /* 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)) {
821 822 823 824
            --*ret_params_len;
            continue;
        }

825
        /* remoteDispatchClientRequest will free this: */
826
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
827
            goto cleanup;
828
        val[j].value.type = params[i].type;
829
        switch (params[i].type) {
830
        case VIR_TYPED_PARAM_INT:
831
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
832 833
            break;
        case VIR_TYPED_PARAM_UINT:
834
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
835 836
            break;
        case VIR_TYPED_PARAM_LLONG:
837
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
838 839
            break;
        case VIR_TYPED_PARAM_ULLONG:
840
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
841 842
            break;
        case VIR_TYPED_PARAM_DOUBLE:
843
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
844 845
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
846 847 848
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
849
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
850
                goto cleanup;
851 852
            break;
        default:
853 854
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
855 856
            goto cleanup;
        }
857
        j++;
858 859 860 861 862 863 864 865
    }

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

cleanup:
    if (val) {
866
        for (i = 0; i < nparams; i++) {
867
            VIR_FREE(val[i].field);
868
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
869 870
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
871 872 873 874 875 876 877
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
878 879
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
880 881 882
                                 int limit,
                                 int *nparams)
{
883
    int i = 0;
884 885 886 887 888
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
    if (args_params_len > limit) {
889
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
890 891 892 893 894 895 896 897 898 899 900 901 902
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, args_params_len) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    *nparams = args_params_len;

    /* Deserialise the result. */
    for (i = 0; i < args_params_len; ++i) {
        if (virStrcpyStatic(params[i].field,
                            args_params_val[i].field) == NULL) {
903 904 905
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
            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;
934
        case VIR_TYPED_PARAM_STRING:
935 936
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
937 938
                goto cleanup;
            break;
939
        default:
940 941
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
942 943 944 945 946 947 948
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
949
    if (rv < 0) {
950 951
        virTypedParamsFree(params, i);
        params = NULL;
952
    }
953 954 955
    return params;
}

956
static int
957 958
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
959
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
960
                                           virNetMessageErrorPtr rerr,
961 962
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
963
{
964
    virDomainPtr dom = NULL;
965
    virTypedParameterPtr params = NULL;
966
    int nparams = 0;
967
    int rv = -1;
968 969
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
970

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

976
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
977
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
978
        goto cleanup;
979
    }
980
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
981
        goto no_memory;
982
    nparams = args->nparams;
983

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

987
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
988
        goto cleanup;
989

990
    if (remoteSerializeTypedParameters(params, nparams,
991
                                       &ret->params.params_val,
992 993
                                       &ret->params.params_len,
                                       0) < 0)
994 995 996 997 998 999
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
1000
        virNetMessageSaveError(rerr);
1001
    virTypedParamsFree(params, nparams);
1002 1003 1004 1005 1006 1007 1008 1009 1010
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
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;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1026
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
        goto cleanup;
    }

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

    if (doms && ndomains) {
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0) {
            virReportOOMError();
            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;
}

1065
static int
1066 1067
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1068
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1069
                                                virNetMessageErrorPtr rerr,
1070 1071 1072 1073 1074
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1075
    int nparams = 0;
1076
    int rv = -1;
1077 1078
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1079

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

1085
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1086
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1087 1088
        goto cleanup;
    }
1089
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1090
        goto no_memory;
1091
    nparams = args->nparams;
1092

1093
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1094 1095 1096 1097 1098 1099 1100
        goto cleanup;

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

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

1106
    rv = 0;
1107 1108

cleanup:
1109
    if (rv < 0)
1110
        virNetMessageSaveError(rerr);
1111
    virTypedParamsFree(params, nparams);
1112 1113 1114 1115 1116 1117 1118
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
1119 1120
}

1121
static int
1122 1123
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1124
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1125
                                virNetMessageErrorPtr rerr,
1126 1127
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1128
{
1129
    virDomainPtr dom = NULL;
1130
    struct _virDomainMemoryStat *stats;
1131
    int nr_stats, i;
1132
    int rv = -1;
1133 1134
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1135

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

1141
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1142 1143
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1144
        goto cleanup;
1145 1146
    }

1147
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1148
        goto cleanup;
1149 1150 1151

    /* Allocate stats array for making dispatch call */
    if (VIR_ALLOC_N(stats, args->maxStats) < 0) {
1152 1153
        virReportOOMError();
        goto cleanup;
1154
    }
1155

1156
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1157
    if (nr_stats < 0)
1158
        goto cleanup;
1159 1160 1161

    /* Allocate return buffer */
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0) {
1162 1163
        virReportOOMError();
        goto cleanup;
1164 1165 1166 1167 1168 1169 1170 1171
    }

    /* 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;
1172 1173 1174 1175
    rv = 0;

cleanup:
    if (rv < 0)
1176
        virNetMessageSaveError(rerr);
1177 1178
    if (dom)
        virDomainFree(dom);
1179
    VIR_FREE(stats);
1180
    return rv;
1181 1182
}

1183
static int
1184 1185
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1186
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1187
                              virNetMessageErrorPtr rerr,
1188 1189
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1190
{
1191
    virDomainPtr dom = NULL;
1192 1193 1194 1195
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1196
    int rv = -1;
1197 1198
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1199

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

1205
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1206
        goto cleanup;
1207 1208 1209 1210 1211 1212
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1213 1214
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1215
        goto cleanup;
1216 1217 1218
    }

    ret->buffer.buffer_len = size;
1219
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1220 1221
        virReportOOMError();
        goto cleanup;
1222 1223
    }

1224
    if (virDomainBlockPeek(dom, path, offset, size,
1225
                           ret->buffer.buffer_val, flags) < 0)
1226
        goto cleanup;
1227

1228 1229 1230 1231
    rv = 0;

cleanup:
    if (rv < 0) {
1232
        virNetMessageSaveError(rerr);
1233 1234 1235 1236 1237
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1238 1239
}

1240 1241 1242
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1243
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1244 1245 1246 1247 1248 1249 1250
                                    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;
1251
    int nparams = 0;
1252 1253 1254 1255 1256 1257
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1258
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1259 1260 1261 1262 1263 1264 1265
        goto cleanup;
    }

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

1266
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1267
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1268 1269
        goto cleanup;
    }
1270
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
1271 1272 1273
        virReportOOMError();
        goto cleanup;
    }
1274
    nparams = args->nparams;
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289

    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,
1290 1291
                                       &ret->params.params_len,
                                       args->flags) < 0)
1292 1293 1294 1295 1296 1297
        goto cleanup;

success:
    rv = 0;

cleanup:
1298
    if (rv < 0)
1299
        virNetMessageSaveError(rerr);
1300
    virTypedParamsFree(params, nparams);
A
ajia@redhat.com 已提交
1301 1302
    if (dom)
        virDomainFree(dom);
1303 1304 1305
    return rv;
}

R
Richard W.M. Jones 已提交
1306
static int
1307 1308
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1309
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1310
                               virNetMessageErrorPtr rerr,
1311 1312
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1313
{
1314
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1315 1316 1317
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1318
    int rv = -1;
1319 1320
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1321

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

1327
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1328
        goto cleanup;
R
Richard W.M. Jones 已提交
1329 1330 1331 1332 1333
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1334 1335
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1336
        goto cleanup;
R
Richard W.M. Jones 已提交
1337 1338 1339
    }

    ret->buffer.buffer_len = size;
1340
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1341 1342
        virReportOOMError();
        goto cleanup;
R
Richard W.M. Jones 已提交
1343 1344
    }

1345
    if (virDomainMemoryPeek(dom, offset, size,
1346
                            ret->buffer.buffer_val, flags) < 0)
1347
        goto cleanup;
R
Richard W.M. Jones 已提交
1348

1349 1350 1351 1352
    rv = 0;

cleanup:
    if (rv < 0) {
1353
        virNetMessageSaveError(rerr);
1354 1355 1356 1357 1358
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1359 1360
}

1361
static int
1362 1363
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1364
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1365
                                     virNetMessageErrorPtr rerr,
1366 1367
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1368
{
1369 1370
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1371
    int rv = -1;
1372 1373
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1374

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

1380
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
        goto cleanup;

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

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

    ret->label.label_len = strlen(seclabel->label) + 1;
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0) {
        virReportOOMError();
1394
        goto cleanup;
1395 1396 1397
    }
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1398

1399 1400 1401 1402
    rv = 0;

cleanup:
    if (rv < 0)
1403
        virNetMessageSaveError(rerr);
1404 1405 1406
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1407
    return rv;
1408 1409
}

M
Marcelo Cerri 已提交
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
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;
    int i, len, 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 ((len = virDomainGetSecurityLabelList(dom, &seclabels)) < 0) {
        ret->ret = len;
        ret->labels.labels_len = 0;
        ret->labels.labels_val = NULL;
        goto done;
    }

    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0) {
        virReportOOMError();
        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];
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0) {
            virReportOOMError();
            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;
}

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

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

1490
    memset(&secmodel, 0, sizeof(secmodel));
1491
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
        goto cleanup;

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

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0) {
        virReportOOMError();
1504
        goto cleanup;
1505 1506
    }
    strcpy(ret->doi.doi_val, secmodel.doi);
1507

1508 1509 1510 1511
    rv = 0;

cleanup:
    if (rv < 0)
1512
        virNetMessageSaveError(rerr);
1513
    return rv;
1514 1515
}

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

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

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

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

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

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

E
Eric Blake 已提交
1555
    if ((num = virDomainGetVcpuPinInfo(dom,
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
                                       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;
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 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
    cpumaps = NULL;

    rv = 0;

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

no_memory:
    virReportOOMError();
    goto cleanup;
}

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

    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;
1660 1661 1662 1663 1664 1665
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
1666
        virNetMessageSaveError(rerr);
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1677
static int
1678 1679
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
1680
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
1681
                             virNetMessageErrorPtr rerr,
1682 1683
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1684
{
1685
    virDomainPtr dom = NULL;
1686 1687 1688
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
    int info_len, i;
1689
    int rv = -1;
1690 1691
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1692

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

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

1701
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1702
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1703
        goto cleanup;
1704
    }
1705

1706 1707
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1708
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
        goto cleanup;
    }

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

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

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

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

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

    rv = 0;
1745 1746

cleanup:
1747
    if (rv < 0) {
1748
        virNetMessageSaveError(rerr);
1749 1750 1751 1752
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
1753 1754 1755
    if (dom)
        virDomainFree(dom);
    return rv;
1756 1757 1758 1759

no_memory:
    virReportOOMError();
    goto cleanup;
1760 1761 1762
}

static int
1763 1764
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1765
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1766
                                   virNetMessageErrorPtr rerr,
1767 1768
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
1769
{
1770 1771 1772 1773 1774
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1775
    int rv = -1;
1776 1777
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1778

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

1784 1785 1786 1787 1788 1789
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

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

1793
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
1794 1795
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
1796
        goto cleanup;
1797

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
    /* 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;
    }
1809

1810
    rv = 0;
1811

1812 1813
cleanup:
    if (rv < 0)
1814
        virNetMessageSaveError(rerr);
1815
    VIR_FREE(uri_out);
1816
    return rv;
1817 1818
}

1819
static int
1820 1821
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1822
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1823
                                    virNetMessageErrorPtr rerr,
1824 1825
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
1826
{
1827 1828 1829 1830 1831
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1832
    int rv = -1;
1833 1834
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1835

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

1841 1842
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
1843

1844 1845
    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
1846 1847
        virReportOOMError();
        goto cleanup;
1848 1849
    }

1850
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
1851 1852 1853
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
1854
        goto cleanup;
1855

1856 1857 1858 1859 1860 1861
    /* 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;
1862

1863 1864 1865 1866
    rv = 0;

cleanup:
    if (rv < 0)
1867
        virNetMessageSaveError(rerr);
1868
    return rv;
1869 1870
}

C
Chris Lalancette 已提交
1871
static int
1872 1873
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
1874
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
1875 1876 1877
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
1878
{
1879
    virDomainPtr dom = NULL;
1880
    virTypedParameterPtr params = NULL;
1881
    int nparams = 0;
1882
    unsigned int flags;
1883
    int rv = -1;
1884 1885
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1886

1887
    if (!priv->conn) {
1888
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1889
        goto cleanup;
1890
    }
C
Chris Lalancette 已提交
1891

1892
    flags = args->flags;
C
Chris Lalancette 已提交
1893

1894
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
1895
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1896 1897
        goto cleanup;
    }
1898
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
1899 1900
        virReportOOMError();
        goto cleanup;
C
Chris Lalancette 已提交
1901
    }
1902
    nparams = args->nparams;
C
Chris Lalancette 已提交
1903

1904
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1905
        goto cleanup;
C
Chris Lalancette 已提交
1906

1907
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
1908
        goto cleanup;
C
Chris Lalancette 已提交
1909

1910 1911 1912 1913 1914 1915
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
1916 1917
    }

1918
    if (remoteSerializeTypedParameters(params, nparams,
1919
                                       &ret->params.params_val,
1920 1921
                                       &ret->params.params_len,
                                       args->flags) < 0)
1922
        goto cleanup;
1923

1924
success:
1925 1926 1927
    rv = 0;

cleanup:
1928
    if (rv < 0)
1929
        virNetMessageSaveError(rerr);
1930
    virTypedParamsFree(params, nparams);
1931 1932 1933
    if (dom)
        virDomainFree(dom);
    return rv;
1934 1935
}

1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
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;
1946
    int nparams = 0;
1947 1948 1949 1950 1951 1952
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1953
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1954 1955 1956 1957 1958
        goto cleanup;
    }

    flags = args->flags;

1959
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
1960
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1961 1962
        goto cleanup;
    }
1963
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
1964 1965 1966
        virReportOOMError();
        goto cleanup;
    }
1967
    nparams = args->nparams;
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994

    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);
1995
    virTypedParamsFree(params, nparams);
1996 1997 1998 1999 2000
    if (dom)
        virDomainFree(dom);
    return rv;
}

2001
static int
2002 2003
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2004
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2005 2006 2007
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2008
{
2009
    virDomainPtr dom = NULL;
2010
    virTypedParameterPtr params = NULL;
2011
    int nparams = 0;
2012
    unsigned int flags;
2013
    int rv = -1;
2014 2015
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2016

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

2022 2023
    flags = args->flags;

2024
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2025
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2026
        goto cleanup;
2027
    }
2028
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
2029 2030
        virReportOOMError();
        goto cleanup;
2031
    }
2032
    nparams = args->nparams;
2033

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

2037
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2038
        goto cleanup;
2039

2040 2041 2042 2043 2044 2045 2046 2047
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2048
    if (remoteSerializeTypedParameters(params, nparams,
2049
                                       &ret->params.params_val,
2050 2051
                                       &ret->params.params_len,
                                       args->flags) < 0)
2052
        goto cleanup;
2053

2054 2055
success:
    rv = 0;
2056

2057
cleanup:
2058
    if (rv < 0)
2059
        virNetMessageSaveError(rerr);
2060
    virTypedParamsFree(params, nparams);
2061 2062 2063
    if (dom)
        virDomainFree(dom);
    return rv;
2064 2065
}

2066
static int
2067 2068
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2069
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2070 2071 2072
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2073
{
2074
    virNodeCPUStatsPtr params = NULL;
2075 2076
    int i;
    int cpuNum = args->cpuNum;
2077
    int nparams = 0;
2078 2079
    unsigned int flags;
    int rv = -1;
2080 2081
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2082

2083
    if (!priv->conn) {
2084
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2085 2086 2087 2088 2089
        goto cleanup;
    }

    flags = args->flags;

2090
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2091
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2092 2093
        goto cleanup;
    }
2094
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
2095 2096 2097
        virReportOOMError();
        goto cleanup;
    }
2098
    nparams = args->nparams;
2099

2100
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
        goto cleanup;

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

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

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2118 2119
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2120 2121 2122 2123 2124 2125 2126

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

success:
    rv = 0;

2127 2128
cleanup:
    if (rv < 0) {
2129
        virNetMessageSaveError(rerr);
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
        if (ret->params.params_val) {
            for (i = 0; i < nparams; i++)
                VIR_FREE(ret->params.params_val[i].field);
            VIR_FREE(ret->params.params_val);
        }
    }
    VIR_FREE(params);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

static int
2145 2146
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2147
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2148 2149 2150
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2151
{
2152
    virNodeMemoryStatsPtr params = NULL;
2153 2154
    int i;
    int cellNum = args->cellNum;
2155
    int nparams = 0;
2156 2157
    unsigned int flags;
    int rv = -1;
2158 2159
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2160

2161
    if (!priv->conn) {
2162
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2163 2164 2165 2166 2167
        goto cleanup;
    }

    flags = args->flags;

2168
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2169
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2170 2171
        goto cleanup;
    }
2172
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
2173 2174 2175
        virReportOOMError();
        goto cleanup;
    }
2176
    nparams = args->nparams;
2177

2178
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
        goto cleanup;

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

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

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2196 2197
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2198 2199 2200 2201 2202 2203 2204

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

success:
    rv = 0;

2205 2206
cleanup:
    if (rv < 0) {
2207
        virNetMessageSaveError(rerr);
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
        if (ret->params.params_val) {
            for (i = 0; i < nparams; i++)
                VIR_FREE(ret->params.params_val[i].field);
            VIR_FREE(ret->params.params_val);
        }
    }
    VIR_FREE(params);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

2222 2223 2224
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2225
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
                                    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) {
2237
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
        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;
}

2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
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;
2274
    int nparams = 0;
2275 2276 2277 2278
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

2283
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2284
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2285 2286 2287
        goto cleanup;
    }

2288
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
2289 2290 2291
        virReportOOMError();
        goto cleanup;
    }
2292
    nparams = args->nparams;
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321

    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);
2322
    virTypedParamsFree(params, nparams);
2323 2324 2325 2326
    if (dom)
        virDomainFree(dom);
    return rv;
}
2327

D
Daniel Veillard 已提交
2328 2329
/*-------------------------------------------------------------*/

2330
static int
2331 2332
remoteDispatchAuthList(virNetServerPtr server ATTRIBUTE_UNUSED,
                       virNetServerClientPtr client,
2333
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2334
                       virNetMessageErrorPtr rerr,
2335
                       remote_auth_list_ret *ret)
2336
{
2337
    int rv = -1;
2338 2339
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2340
    gid_t callerGid;
2341
    pid_t callerPid;
2342
    unsigned long long timestamp;
2343 2344 2345 2346 2347 2348

    /* 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) {
2349
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2350
                                              &callerPid, &timestamp) < 0) {
2351 2352 2353 2354
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2355 2356
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
J
Jim Fehlig 已提交
2357 2358 2359
                            (long long) callerPid, callerUid) < 0) {
                virReportOOMError();
                goto cleanup;
2360
            }
J
Jim Fehlig 已提交
2361
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2362 2363
            virNetServerClientSetAuth(client, 0);
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2364
            VIR_FREE(ident);
2365 2366
        }
    }
2367

2368
    ret->types.types_len = 1;
2369
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
2370 2371
        virReportOOMError();
        goto cleanup;
2372
    }
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386

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

2388 2389 2390 2391
    rv = 0;

cleanup:
    if (rv < 0)
2392
        virNetMessageSaveError(rerr);
2393
    return rv;
2394 2395 2396
}


2397
#ifdef WITH_SASL
2398 2399
/*
 * Initializes the SASL session in prepare for authentication
2400
 * and gives the client a list of allowed mechanisms to choose
2401 2402
 */
static int
2403 2404
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2405
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2406
                           virNetMessageErrorPtr rerr,
2407
                           remote_auth_sasl_init_ret *ret)
2408
{
2409 2410 2411
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2412

2413
    virMutexLock(&priv->lock);
2414

2415 2416 2417
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2418
        VIR_ERROR(_("client tried invalid SASL init request"));
2419
        goto authfail;
2420 2421
    }

2422 2423 2424 2425 2426
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2427
        goto authfail;
2428

2429
# if WITH_GNUTLS
2430
    /* Inform SASL that we've got an external SSF layer from TLS */
2431 2432 2433 2434
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2435
            goto authfail;
2436 2437 2438 2439 2440

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2441
            goto authfail;
2442
    }
2443
# endif
2444

2445
    if (virNetServerClientIsSecure(client))
2446
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2447 2448
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2449
        /* Plain TCP, better get an SSF layer */
2450 2451 2452 2453
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2454

2455
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2456
        goto authfail;
2457
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2458

2459 2460
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2461
    return 0;
2462 2463

authfail:
2464
    virResetLastError();
2465 2466
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2467
    virNetMessageSaveError(rerr);
2468 2469 2470
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2471
    virObjectUnref(sasl);
2472
    virMutexUnlock(&priv->lock);
2473
    return -1;
2474 2475
}

2476
/*
2477 2478
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2479
static int
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
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;
        }
2496
    }
2497 2498

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2499
        return -2;
2500

2501 2502
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2503

2504
    virNetServerClientSetAuth(client, 0);
2505
    virNetServerClientSetSASLSession(client, priv->sasl);
2506

2507
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2508

2509 2510 2511
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2512

2513
    virObjectUnref(priv->sasl);
2514
    priv->sasl = NULL;
2515

2516
    return 0;
2517

2518 2519 2520
error:
    return -1;
}
2521

2522 2523 2524 2525
/*
 * This starts the SASL authentication negotiation.
 */
static int
2526 2527
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
2528
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2529
                            virNetMessageErrorPtr rerr,
2530 2531
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2532 2533
{
    const char *serverout;
2534
    size_t serveroutlen;
2535
    int err;
2536 2537 2538
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2539
    const char *identity;
2540

2541
    virMutexLock(&priv->lock);
2542

2543 2544 2545
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2546
        VIR_ERROR(_("client tried invalid SASL start request"));
2547
        goto authfail;
2548 2549
    }

2550 2551
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2552 2553 2554 2555 2556 2557 2558 2559 2560
    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)
2561
        goto authfail;
2562

2563
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2564
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2565
        goto authfail;
2566 2567 2568 2569
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2570 2571
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2572 2573 2574 2575 2576 2577 2578
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2579
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2580
    if (err == VIR_NET_SASL_CONTINUE) {
2581 2582
        ret->complete = 0;
    } else {
2583
        /* Check username whitelist ACL */
2584
        if ((err = remoteSASLFinish(client)) < 0) {
2585 2586 2587 2588 2589
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2590

2591 2592 2593
        ret->complete = 1;
    }

2594
    virMutexUnlock(&priv->lock);
2595
    return 0;
2596 2597

authfail:
2598 2599 2600
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2601 2602 2603
    goto error;

authdeny:
2604
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2605 2606 2607
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2608 2609
    goto error;

2610
error:
2611
    virObjectUnref(priv->sasl);
2612 2613
    priv->sasl = NULL;
    virResetLastError();
2614 2615
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2616 2617 2618
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2619
    return -1;
2620 2621 2622 2623
}


static int
2624 2625
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2626
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2627
                           virNetMessageErrorPtr rerr,
2628 2629
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2630 2631
{
    const char *serverout;
2632
    size_t serveroutlen;
2633
    int err;
2634 2635 2636
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2637
    const char *identity;
2638

2639 2640 2641 2642 2643
    virMutexLock(&priv->lock);

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

2648
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
2649
              args->data.data_len, args->nil);
2650 2651 2652 2653 2654 2655 2656 2657
    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)
2658
        goto authfail;
2659 2660

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2661
        VIR_ERROR(_("sasl step reply data too long %d"),
2662
                  (int)serveroutlen);
2663
        goto authfail;
2664 2665 2666 2667
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2668 2669
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2670 2671 2672 2673 2674 2675 2676
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2677
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2678
    if (err == VIR_NET_SASL_CONTINUE) {
2679 2680
        ret->complete = 0;
    } else {
2681
        /* Check username whitelist ACL */
2682
        if ((err = remoteSASLFinish(client)) < 0) {
2683 2684 2685 2686 2687
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2688

2689 2690 2691
        ret->complete = 1;
    }

2692
    virMutexUnlock(&priv->lock);
2693
    return 0;
2694 2695

authfail:
2696 2697 2698
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2699 2700 2701
    goto error;

authdeny:
2702
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2703 2704 2705
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2706 2707
    goto error;

2708
error:
2709
    virObjectUnref(priv->sasl);
2710 2711
    priv->sasl = NULL;
    virResetLastError();
2712 2713
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2714 2715 2716
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2717 2718
    return -1;
}
2719 2720 2721 2722
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2723
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2724 2725 2726 2727
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
2728 2729
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2730 2731 2732 2733 2734 2735
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
2736
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2737 2738 2739 2740 2741
                            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");
2742 2743
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2744 2745 2746 2747 2748 2749
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2750
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2751 2752 2753 2754 2755
                           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");
2756 2757
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2758 2759 2760 2761
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
2762 2763 2764



2765
#if WITH_POLKIT1
2766
static int
2767 2768
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
                         virNetServerClientPtr client,
2769
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2770
                         virNetMessageErrorPtr rerr,
2771
                         remote_auth_polkit_ret *ret)
2772
{
2773
    pid_t callerPid = -1;
2774
    gid_t callerGid = -1;
2775
    uid_t callerUid = -1;
2776
    unsigned long long timestamp;
2777 2778
    const char *action;
    int status = -1;
2779
    char *ident = NULL;
2780
    bool authdismissed = 0;
2781
    char *pkout = NULL;
2782 2783
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2784
    virCommandPtr cmd = NULL;
2785

2786 2787
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
2788 2789 2790
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

2791
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
2792
    virCommandSetOutputBuffer(cmd, &pkout);
2793
    virCommandSetErrorBuffer(cmd, &pkout);
2794

2795 2796
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2797
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2798 2799 2800
        goto authfail;
    }

2801
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2802
                                          &callerPid, &timestamp) < 0) {
2803 2804 2805
        goto authfail;
    }

2806 2807
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2808

2809
    virCommandAddArg(cmd, "--process");
2810 2811 2812 2813 2814
    if (timestamp != 0) {
        virCommandAddArgFormat(cmd, "%lld,%llu", (long long) callerPid, timestamp);
    } else {
        virCommandAddArgFormat(cmd, "%lld", (long long) callerPid);
    }
2815
    virCommandAddArg(cmd, "--allow-user-interaction");
2816

2817 2818
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
                    (long long) callerPid, callerUid) < 0) {
2819
        virReportOOMError();
2820 2821 2822
        goto authfail;
    }

2823
    if (virCommandRun(cmd, &status) < 0)
2824
        goto authfail;
2825

2826
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
2827
    if (status != 0) {
2828
        char *tmp = virProcessTranslateStatus(status);
2829 2830
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d: %s"),
                  action, (long long) callerPid, callerUid, NULLSTR(tmp));
2831
        VIR_FREE(tmp);
2832
        goto authdeny;
2833
    }
2834 2835 2836
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2837 2838
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
2839 2840
    ret->complete = 1;

2841
    virNetServerClientSetAuth(client, 0);
2842
    virMutexUnlock(&priv->lock);
2843
    virCommandFree(cmd);
E
Eric Blake 已提交
2844
    VIR_FREE(pkout);
2845
    VIR_FREE(ident);
2846

2847 2848
    return 0;

2849
error:
2850 2851
    virCommandFree(cmd);
    VIR_FREE(ident);
2852
    virResetLastError();
2853

2854
    if (authdismissed) {
2855 2856
        virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
                       _("authentication cancelled by user"));
2857 2858
    } else if (pkout && *pkout) {
        virReportError(VIR_ERR_AUTH_FAILED, _("polkit: %s"), pkout);
2859
    } else {
2860
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed"));
2861
    }
2862 2863

    VIR_FREE(pkout);
2864 2865 2866 2867
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

2868
authfail:
2869 2870 2871
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2872 2873 2874
    goto error;

authdeny:
2875 2876
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
2877
          client, REMOTE_AUTH_POLKIT, ident);
2878
    goto error;
2879
}
2880
#elif WITH_POLKIT0
2881
static int
2882
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
2883
                         virNetServerClientPtr client,
2884
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2885
                         virNetMessageErrorPtr rerr,
2886
                         remote_auth_polkit_ret *ret)
2887 2888
{
    pid_t callerPid;
2889
    gid_t callerGid;
2890
    uid_t callerUid;
2891 2892 2893 2894 2895 2896
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
2897
    const char *action;
2898
    char *ident = NULL;
2899 2900
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2901
    DBusConnection *sysbus;
J
Jim Fehlig 已提交
2902
    unsigned long long timestamp;
2903

J
Jim Fehlig 已提交
2904
    virMutexLock(&priv->lock);
2905

J
Jim Fehlig 已提交
2906
    action = virNetServerClientGetReadonly(client) ?
2907 2908
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
2909

2910
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
2911
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2912
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2913
        goto authfail;
2914 2915
    }

2916
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
J
Jim Fehlig 已提交
2917
                                          &callerPid, &timestamp) < 0) {
2918
        VIR_ERROR(_("cannot get peer socket identity"));
2919
        goto authfail;
2920 2921
    }

2922 2923 2924
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
                    (long long) callerPid, callerUid) < 0) {
        virReportOOMError();
2925 2926 2927
        goto authfail;
    }

2928 2929 2930
    if (!(sysbus = virDBusGetSystemBus()))
        goto authfail;

2931 2932
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2933
    dbus_error_init(&err);
2934
    if (!(pkcaller = polkit_caller_new_from_pid(sysbus,
2935
                                                callerPid, &err))) {
2936
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
2937
        dbus_error_free(&err);
2938
        goto authfail;
2939
    }
2940

2941
    if (!(pkaction = polkit_action_new())) {
2942
        char ebuf[1024];
2943
        VIR_ERROR(_("Failed to create polkit action %s"),
2944
                  virStrerror(errno, ebuf, sizeof(ebuf)));
2945
        polkit_caller_unref(pkcaller);
2946
        goto authfail;
2947 2948 2949 2950 2951
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
2952
        char ebuf[1024];
2953
        VIR_ERROR(_("Failed to create polkit context %s"),
2954
                  (pkerr ? polkit_error_get_error_message(pkerr)
2955
                   : virStrerror(errno, ebuf, sizeof(ebuf))));
2956 2957 2958 2959 2960
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
2961
        goto authfail;
2962
    }
2963

2964
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
2965 2966 2967 2968 2969 2970
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
2971 2972 2973
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
2974
        goto authfail;
2975
    }
2976
# else
2977 2978 2979
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
2980
# endif
2981 2982 2983 2984
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
2985 2986
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
2987
                  polkit_result_to_string_representation(pkresult));
2988
        goto authdeny;
2989
    }
2990 2991 2992
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2993 2994
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
2995 2996
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
2997

2998
    virNetServerClientSetAuth(client, 0);
J
Jim Fehlig 已提交
2999
    virMutexUnlock(&priv->lock);
3000
    VIR_FREE(ident);
3001
    return 0;
3002

3003
error:
3004
    VIR_FREE(ident);
3005
    virResetLastError();
3006 3007
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3008
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3009
    virMutexUnlock(&priv->lock);
3010 3011
    return -1;

3012
authfail:
3013 3014 3015
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3016
    goto error;
3017

3018
authdeny:
3019 3020 3021
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3022
    goto error;
3023 3024
}

3025
#else /* !WITH_POLKIT0 & !HAVE_POLKIT1*/
3026 3027

static int
3028
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
3029
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
3030
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3031
                         virNetMessageErrorPtr rerr,
3032
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3033
{
3034
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
3035 3036
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3037
    virNetMessageSaveError(rerr);
3038 3039
    return -1;
}
3040
#endif /* WITH_POLKIT1 */
3041 3042


3043 3044 3045
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3046

3047
static int
3048 3049
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3050
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3051
                                  virNetMessageErrorPtr rerr,
3052 3053
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3054
{
3055 3056
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3057
    int rv = -1;
3058 3059
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3060

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

3066
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3067 3068
        goto cleanup;

3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
            virReportOOMError();
            goto cleanup;
        }
3080
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3081 3082 3083 3084 3085 3086
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3087 3088 3089 3090
    rv = 0;

cleanup:
    if (rv < 0)
3091
        virNetMessageSaveError(rerr);
3092 3093
    if (dev)
        virNodeDeviceFree(dev);
3094
    return rv;
3095 3096
}

3097 3098 3099 3100 3101

/***************************
 * Register / deregister events
 ***************************/
static int
3102 3103 3104 3105 3106
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 已提交
3107
{
3108
    int callbackID;
3109
    int rv = -1;
3110 3111
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3112

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

3118 3119 3120
    virMutexLock(&priv->lock);

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

3125
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3126 3127 3128 3129
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0)
3130
        goto cleanup;
O
Osier Yang 已提交
3131

3132
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
3133

3134 3135 3136 3137
    rv = 0;

cleanup:
    if (rv < 0)
3138 3139
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3140
    return rv;
O
Osier Yang 已提交
3141 3142
}

3143
static int
3144 3145 3146 3147 3148
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)
3149
{
3150
    int rv = -1;
3151 3152
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3153

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

3159 3160 3161
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] < 0) {
3162
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
3163
        goto cleanup;
3164
    }
3165

3166 3167
    if (virConnectDomainEventDeregisterAny(priv->conn,
                                           priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0)
3168
        goto cleanup;
3169

3170 3171
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;

3172 3173 3174
    rv = 0;

cleanup:
3175
    if (rv < 0)
3176 3177
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3178 3179 3180 3181
    return rv;
}

static void
3182 3183
remoteDispatchDomainEventSend(virNetServerClientPtr client,
                              virNetServerProgramPtr program,
3184 3185 3186 3187
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3188
    virNetMessagePtr msg;
3189

3190
    if (!(msg = virNetMessageNew(false)))
3191
        goto cleanup;
3192

3193 3194 3195 3196 3197 3198
    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;
3199

3200
    if (virNetMessageEncodeHeader(msg) < 0)
3201 3202
        goto cleanup;

3203 3204
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3205

3206 3207
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3208

3209
    xdr_free(proc, data);
3210 3211 3212
    return;

cleanup:
3213
    virNetMessageFree(msg);
3214
    xdr_free(proc, data);
3215 3216
}

3217
static int
3218 3219
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3220
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3221
                             virNetMessageErrorPtr rerr,
3222 3223
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3224
{
3225 3226 3227
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3228
    int rv = -1;
3229 3230
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3231

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

3237
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3238
        goto cleanup;
3239

3240
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3241
        goto cleanup;
3242

3243 3244 3245
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3246 3247 3248 3249
    rv = 0;

cleanup:
    if (rv < 0)
3250
        virNetMessageSaveError(rerr);
3251 3252
    if (secret)
        virSecretFree(secret);
3253
    return rv;
3254 3255
}

3256
static int
3257 3258
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3259
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3260
                             virNetMessageErrorPtr rerr,
3261 3262 3263 3264 3265
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3266 3267
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3268

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

3274
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3275 3276 3277 3278 3279 3280 3281 3282 3283
        goto cleanup;

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

    rv = 0;

cleanup:
    if (rv < 0)
3284
        virNetMessageSaveError(rerr);
3285 3286 3287 3288 3289
    if (dom)
        virDomainFree(dom);
    return rv;
}

3290
static int
3291 3292 3293 3294 3295
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)
3296 3297
{
    int callbackID;
3298
    int rv = -1;
3299 3300
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3301

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

3307 3308
    virMutexLock(&priv->lock);

3309 3310
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3311
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
3312
        goto cleanup;
3313 3314
    }

3315
    if (priv->domainEventCallbackID[args->eventID] != -1)  {
3316
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), args->eventID);
3317
        goto cleanup;
3318 3319
    }

3320
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3321 3322 3323
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3324
                                                       client, NULL)) < 0)
3325
        goto cleanup;
3326

3327
    priv->domainEventCallbackID[args->eventID] = callbackID;
3328

3329 3330 3331 3332
    rv = 0;

cleanup:
    if (rv < 0)
3333 3334
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3335
    return rv;
3336 3337 3338 3339
}


static int
3340 3341 3342 3343 3344
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)
3345 3346
{
    int callbackID = -1;
3347
    int rv = -1;
3348 3349
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3350

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

3356 3357
    virMutexLock(&priv->lock);

3358 3359
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3360
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
3361
        goto cleanup;
3362 3363
    }

3364 3365
    callbackID = priv->domainEventCallbackID[args->eventID];
    if (callbackID < 0) {
3366
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), args->eventID);
3367
        goto cleanup;
3368 3369
    }

3370
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3371
        goto cleanup;
3372

3373 3374
    priv->domainEventCallbackID[args->eventID] = -1;

3375 3376 3377 3378
    rv = 0;

cleanup:
    if (rv < 0)
3379 3380
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3381
    return rv;
3382 3383
}

C
Chris Lalancette 已提交
3384
static int
3385 3386 3387 3388 3389 3390
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 已提交
3391
{
3392
    virDomainPtr dom = NULL;
3393
    int rv = -1;
3394 3395
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3396

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

3402
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3403
        goto cleanup;
C
Chris Lalancette 已提交
3404

3405
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3406
                                    args->flags) < 0)
3407
        goto cleanup;
C
Chris Lalancette 已提交
3408

3409
    rv = 0;
C
Chris Lalancette 已提交
3410

3411 3412
cleanup:
    if (rv < 0)
3413
        virNetMessageSaveError(rerr);
3414 3415
    if (dom)
        virDomainFree(dom);
3416
    return rv;
C
Chris Lalancette 已提交
3417 3418
}

3419

3420
static int
3421 3422
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3423
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3424
                                  virNetMessageErrorPtr rerr,
3425 3426 3427 3428 3429 3430
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3431
    char *xmlin;
3432 3433 3434
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3435 3436
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3437

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

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

3446
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3447 3448
    dname = args->dname == NULL ? NULL : *args->dname;

3449
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
                                       &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)
3465
        virNetMessageSaveError(rerr);
3466 3467 3468 3469 3470 3471 3472
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3473 3474
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3475
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3476
                                    virNetMessageErrorPtr rerr,
3477 3478 3479 3480 3481 3482 3483 3484 3485
                                    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;
3486 3487
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3488

3489
    if (!priv->conn) {
3490
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502
        goto cleanup;
    }

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

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

3503
    if (virDomainMigratePrepare3(priv->conn,
3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
                                 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) {
3523
        virNetMessageSaveError(rerr);
3524 3525 3526 3527 3528
        VIR_FREE(uri_out);
    }
    return rv;
}

3529

3530
static int
3531 3532
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3533
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3534
                                    virNetMessageErrorPtr rerr,
3535 3536 3537 3538
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
3539
    char *xmlin;
3540
    char *dname;
3541 3542
    char *uri;
    char *dconnuri;
3543 3544 3545
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3546 3547
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3548

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

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

3557
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3558
    dname = args->dname == NULL ? NULL : *args->dname;
3559 3560
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
3561

3562
    if (virDomainMigratePerform3(dom, xmlin,
3563 3564 3565
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
3566
                                 dconnuri, uri,
3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
                                 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)
3579
        virNetMessageSaveError(rerr);
3580 3581 3582 3583 3584 3585 3586
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3587 3588
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
3589
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
3590
                                   virNetMessageErrorPtr rerr,
3591 3592 3593 3594 3595 3596
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
3597 3598
    char *uri;
    char *dconnuri;
3599
    int rv = -1;
3600 3601
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3602

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

3608 3609 3610
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

3611
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
3612 3613 3614 3615 3616 3617
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
3618 3619
        goto cleanup;

3620
    make_nonnull_domain(&ret->dom, dom);
3621 3622 3623 3624 3625 3626 3627 3628 3629 3630

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

    rv = 0;

cleanup:
    if (rv < 0) {
3631
        virNetMessageSaveError(rerr);
3632 3633 3634 3635 3636 3637 3638 3639 3640
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3641 3642
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3643
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3644 3645
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
3646 3647 3648
{
    virDomainPtr dom = NULL;
    int rv = -1;
3649 3650
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3651

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

3657
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669
        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)
3670
        virNetMessageSaveError(rerr);
3671 3672 3673 3674 3675 3676
    if (dom)
        virDomainFree(dom);
    return rv;
}


3677 3678 3679 3680 3681 3682
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)
3683 3684 3685 3686 3687 3688
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
    /* 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;
    }

3699
    if (!priv->conn) {
3700
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3701 3702 3703 3704 3705 3706 3707 3708 3709
        goto cleanup;
    }

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

    default:
3710
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
3711 3712 3713 3714
            goto cleanup;
        break;
    }

3715
done:
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725
    ret->supported = supported;
    rv = 0;

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


3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739
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) {
3740
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766
        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;
}

3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777
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;
3778
    int nparams = 0;
3779 3780 3781 3782 3783 3784
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3785
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3786 3787 3788 3789 3790
        goto cleanup;
    }

    flags = args->flags;

3791
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
3792
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3793 3794
        goto cleanup;
    }
3795
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
3796 3797 3798
        virReportOOMError();
        goto cleanup;
    }
3799
    nparams = args->nparams;
3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826

    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);
3827
    virTypedParamsFree(params, nparams);
3828 3829 3830 3831
    if (dom)
        virDomainFree(dom);
    return rv;
}
3832

3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848
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) {
3849
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3850 3851 3852 3853
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
3854
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3855 3856 3857
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
3858
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888
        goto cleanup;
    }

    if (args->nparams > 0 &&
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0) {
        virReportOOMError();
        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;
3889 3890 3891 3892 3893 3894 3895 3896
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
        int i;

        for (i = 0; i < percpu_len; i++) {
            if (params[i].type == VIR_TYPED_PARAM_STRING)
                ret->nparams--;
        }
    }
3897 3898 3899 3900

cleanup:
    if (rv < 0)
         virNetMessageSaveError(rerr);
3901
    virTypedParamsFree(params, args->ncpus * args->nparams);
3902 3903 3904 3905 3906
    if (dom)
        virDomainFree(dom);
    return rv;
}

3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917
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;
3918
    int len = 0;
3919 3920 3921 3922
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3923
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3924 3925 3926 3927 3928 3929 3930
        goto cleanup;
    }

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
3931 3932
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969
        goto cleanup;
    }

    if (args->maxerrors &&
        VIR_ALLOC_N(errors, args->maxerrors) < 0) {
        virReportOOMError();
        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) {
        int i;
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985
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;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
3986
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046
        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) {
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0) {
            virReportOOMError();
            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;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4047
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095
        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) {
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0) {
            virReportOOMError();
            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;
}

4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149
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;
    int i;
    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) {
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0) {
            virReportOOMError();
            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;
}

4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204
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;
    int i;
    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) {
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0) {
            virReportOOMError();
            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);
    }
4205 4206
    if (pool)
        virStoragePoolFree(pool);
4207 4208 4209
    return rv;
}

4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263
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;
    int i;
    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) {
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0) {
            virReportOOMError();
            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;
}

4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 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
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;
    int i;
    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) {
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0) {
            virReportOOMError();
            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;
}

4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 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
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;
    int i;
    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) {
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0) {
            virReportOOMError();
            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;
}
4371

4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 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 4423 4424 4425
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;
    int i;
    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) {
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0) {
            virReportOOMError();
            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;
}

4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479
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;
    int i;
    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) {
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0) {
            virReportOOMError();
            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;
}

4480 4481 4482 4483 4484 4485 4486 4487 4488
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;
4489
    int nparams = 0;
4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501
    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;

4502
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
4503 4504 4505
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
4506
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0) {
4507 4508 4509
        virReportOOMError();
        goto cleanup;
    }
4510
    nparams = args->nparams;
4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534

    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);
4535
    virTypedParamsFree(params, nparams);
4536 4537 4538
    return rv;
}

4539 4540 4541 4542 4543 4544 4545 4546 4547
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;
4548
    unsigned int online = 0;
4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561
    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;

4562 4563
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
4564 4565 4566 4567
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
4568
    if (args->need_map) {
4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585
        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;
}

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
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
     */
4619
    for (i = 0; i < msg->nfds; i++)
4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635
        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;
}

4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 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
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;
}

4680 4681 4682 4683 4684 4685 4686 4687 4688
/*----- 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
4689
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
4690 4691
{
    virDomainPtr dom;
4692
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
4693 4694 4695 4696 4697 4698 4699 4700
    /* 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
4701
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
4702
{
4703
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
4704 4705
}

D
Daniel Veillard 已提交
4706
static virInterfacePtr
4707
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
4708
{
4709
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
4710 4711
}

4712
static virStoragePoolPtr
4713
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
4714
{
4715 4716
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
4717 4718 4719
}

static virStorageVolPtr
4720
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
4721 4722
{
    virStorageVolPtr ret;
4723 4724
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
4725 4726 4727
    return ret;
}

4728
static virSecretPtr
4729
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
4730
{
4731
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
4732 4733
}

4734
static virNWFilterPtr
4735
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
4736
{
4737
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
4738 4739
}

C
Chris Lalancette 已提交
4740
static virDomainSnapshotPtr
4741
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
4742
{
4743
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
4744 4745
}

4746 4747
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
4748
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
4749 4750
{
    dom_dst->id = dom_src->id;
4751
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
4752
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
4753 4754 4755
}

static void
4756
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
4757
{
4758
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
4759
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
4760 4761
}

D
Daniel Veillard 已提交
4762
static void
4763 4764
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
4765
{
4766 4767
    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 已提交
4768 4769
}

4770
static void
4771
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
4772
{
4773
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
4774
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
4775 4776 4777
}

static void
4778
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
4779
{
4780 4781 4782
    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));
4783
}
4784 4785

static void
4786
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
4787
{
4788
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
4789
}
4790 4791

static void
4792
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
4793
{
4794
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
4795
    secret_dst->usageType = secret_src->usageType;
4796
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
4797
}
4798 4799

static void
4800
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
4801
{
4802
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
4803
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
4804
}
C
Chris Lalancette 已提交
4805 4806

static void
4807
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
4808
{
4809
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
4810
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
4811
}
4812 4813 4814 4815 4816 4817 4818 4819 4820 4821

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;
    int i = 0;

4822 4823 4824 4825
    if (VIR_ALLOC_N(val, nerrors) < 0) {
        virReportOOMError();
        goto error;
    }
4826 4827

    for (i = 0; i < nerrors; i++) {
4828 4829
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
4830 4831 4832 4833 4834 4835 4836 4837
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

4838
error:
4839 4840 4841 4842 4843 4844 4845 4846
    if (val) {
        int j;
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}