virnetsocket.c 50.9 KB
Newer Older
1 2 3
/*
 * virnetsocket.c: generic network socket handling
 *
4
 * Copyright (C) 2006-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <sys/stat.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/wait.h>
30
#include <signal.h>
31
#include <fcntl.h>
32
#include <netdb.h>
33 34 35 36 37

#ifdef HAVE_NETINET_TCP_H
# include <netinet/tcp.h>
#endif

38 39 40 41
#ifdef HAVE_SYS_UCRED_H
# include <sys/ucred.h>
#endif

42
#include "c-ctype.h"
43
#ifdef WITH_SELINUX
44 45 46
# include <selinux/selinux.h>
#endif

47
#include "virnetsocket.h"
48
#include "virutil.h"
49
#include "viralloc.h"
50
#include "virerror.h"
51
#include "virlog.h"
E
Eric Blake 已提交
52
#include "virfile.h"
53
#include "virthread.h"
54
#include "virprobe.h"
55
#include "virprocess.h"
56
#include "virstring.h"
57 58
#include "passfd.h"

59
#if WITH_SSH2
60 61 62
# include "virnetsshsession.h"
#endif

63 64
#define VIR_FROM_THIS VIR_FROM_RPC

65
VIR_LOG_INIT("rpc.netsocket");
66 67

struct _virNetSocket {
68
    virObjectLockable parent;
69

70 71 72 73 74
    int fd;
    int watch;
    pid_t pid;
    int errfd;
    bool client;
75 76

    /* Event callback fields */
77 78
    virNetSocketIOFunc func;
    void *opaque;
79 80
    virFreeCallback ff;

81 82 83 84
    virSocketAddr localAddr;
    virSocketAddr remoteAddr;
    char *localAddrStr;
    char *remoteAddrStr;
85

86
#if WITH_GNUTLS
87
    virNetTLSSessionPtr tlsSession;
88
#endif
89
#if WITH_SASL
90 91 92 93 94 95 96 97 98 99
    virNetSASLSessionPtr saslSession;

    const char *saslDecoded;
    size_t saslDecodedLength;
    size_t saslDecodedOffset;

    const char *saslEncoded;
    size_t saslEncodedLength;
    size_t saslEncodedOffset;
#endif
100
#if WITH_SSH2
101 102
    virNetSSHSessionPtr sshSession;
#endif
103 104 105
};


106 107 108 109 110
static virClassPtr virNetSocketClass;
static void virNetSocketDispose(void *obj);

static int virNetSocketOnceInit(void)
{
111
    if (!(virNetSocketClass = virClassNew(virClassForObjectLockable(),
112
                                          "virNetSocket",
113 114 115 116 117 118 119 120 121 122
                                          sizeof(virNetSocket),
                                          virNetSocketDispose)))
        return -1;

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virNetSocket)


123 124 125 126 127 128 129 130 131
#ifndef WIN32
static int virNetSocketForkDaemon(const char *binary)
{
    int ret;
    virCommandPtr cmd = virCommandNewArgList(binary,
                                             "--timeout=30",
                                             NULL);

    virCommandAddEnvPassCommon(cmd);
132 133 134
    virCommandAddEnvPassBlockSUID(cmd, "XDG_CACHE_HOME", NULL);
    virCommandAddEnvPassBlockSUID(cmd, "XDG_CONFIG_HOME", NULL);
    virCommandAddEnvPassBlockSUID(cmd, "XDG_RUNTIME_DIR", NULL);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    virCommandClearCaps(cmd);
    virCommandDaemonize(cmd);
    ret = virCommandRun(cmd, NULL);
    virCommandFree(cmd);
    return ret;
}
#endif


static virNetSocketPtr virNetSocketNew(virSocketAddrPtr localAddr,
                                       virSocketAddrPtr remoteAddr,
                                       bool isClient,
                                       int fd, int errfd, pid_t pid)
{
    virNetSocketPtr sock;
    int no_slow_start = 1;

152 153 154
    if (virNetSocketInitialize() < 0)
        return NULL;

E
Eric Blake 已提交
155
    VIR_DEBUG("localAddr=%p remoteAddr=%p fd=%d errfd=%d pid=%lld",
156
              localAddr, remoteAddr,
E
Eric Blake 已提交
157
              fd, errfd, (long long) pid);
158 159 160 161 162 163 164 165 166 167 168 169

    if (virSetCloseExec(fd) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to set close-on-exec flag"));
       return NULL;
    }
    if (virSetNonBlock(fd) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to enable non-blocking flag"));
        return NULL;
    }

170
    if (!(sock = virObjectLockableNew(virNetSocketClass)))
171 172
        return NULL;

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    if (localAddr)
        sock->localAddr = *localAddr;
    if (remoteAddr)
        sock->remoteAddr = *remoteAddr;
    sock->fd = fd;
    sock->errfd = errfd;
    sock->pid = pid;

    /* Disable nagle for TCP sockets */
    if (sock->localAddr.data.sa.sa_family == AF_INET ||
        sock->localAddr.data.sa.sa_family == AF_INET6) {
        if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
                       &no_slow_start,
                       sizeof(no_slow_start)) < 0) {
            virReportSystemError(errno, "%s",
                                 _("Unable to disable nagle algorithm"));
            goto error;
        }
    }


    if (localAddr &&
195
        !(sock->localAddrStr = virSocketAddrFormatFull(localAddr, true, ";")))
196 197 198
        goto error;

    if (remoteAddr &&
199
        !(sock->remoteAddrStr = virSocketAddrFormatFull(remoteAddr, true, ";")))
200 201 202 203
        goto error;

    sock->client = isClient;

204
    PROBE(RPC_SOCKET_NEW,
205 206
          "sock=%p fd=%d errfd=%d pid=%lld localAddr=%s, remoteAddr=%s",
          sock, fd, errfd, (long long) pid,
E
Eric Blake 已提交
207
          NULLSTR(sock->localAddrStr), NULLSTR(sock->remoteAddrStr));
208 209 210 211 212

    return sock;

error:
    sock->fd = sock->errfd = -1; /* Caller owns fd/errfd on failure */
213
    virObjectUnref(sock);
214 215 216 217 218 219 220 221 222 223 224 225 226 227
    return NULL;
}


int virNetSocketNewListenTCP(const char *nodename,
                             const char *service,
                             virNetSocketPtr **retsocks,
                             size_t *nretsocks)
{
    virNetSocketPtr *socks = NULL;
    size_t nsocks = 0;
    struct addrinfo *ai = NULL;
    struct addrinfo hints;
    int fd = -1;
228
    size_t i;
229
    int addrInUse = false;
230 231 232 233

    *retsocks = NULL;
    *nretsocks = 0;

234
    memset(&hints, 0, sizeof(hints));
235 236 237 238 239
    hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
    hints.ai_socktype = SOCK_STREAM;

    int e = getaddrinfo(nodename, service, &hints, &ai);
    if (e != 0) {
240 241 242
        virReportError(VIR_ERR_SYSTEM_ERROR,
                       _("Unable to resolve address '%s' service '%s': %s"),
                       nodename, service, gai_strerror(e));
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        return -1;
    }

    struct addrinfo *runp = ai;
    while (runp) {
        virSocketAddr addr;

        memset(&addr, 0, sizeof(addr));

        if ((fd = socket(runp->ai_family, runp->ai_socktype,
                         runp->ai_protocol)) < 0) {
            virReportSystemError(errno, "%s", _("Unable to create socket"));
            goto error;
        }

        int opt = 1;
259
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
            virReportSystemError(errno, "%s", _("Unable to enable port reuse"));
            goto error;
        }

#ifdef IPV6_V6ONLY
        if (runp->ai_family == PF_INET6) {
            int on = 1;
            /*
             * Normally on Linux an INET6 socket will bind to the INET4
             * address too. If getaddrinfo returns results with INET4
             * first though, this will result in INET6 binding failing.
             * We can trivially cope with multiple server sockets, so
             * we force it to only listen on IPv6
             */
            if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
275
                           (void*)&on, sizeof(on)) < 0) {
276 277 278 279 280 281 282 283 284 285 286 287
                virReportSystemError(errno, "%s",
                                     _("Unable to force bind to IPv6 only"));
                goto error;
            }
        }
#endif

        if (bind(fd, runp->ai_addr, runp->ai_addrlen) < 0) {
            if (errno != EADDRINUSE) {
                virReportSystemError(errno, "%s", _("Unable to bind to port"));
                goto error;
            }
288
            addrInUse = true;
289
            VIR_FORCE_CLOSE(fd);
290
            runp = runp->ai_next;
291 292 293 294 295 296 297 298 299 300 301
            continue;
        }

        addr.len = sizeof(addr.data);
        if (getsockname(fd, &addr.data.sa, &addr.len) < 0) {
            virReportSystemError(errno, "%s", _("Unable to get local socket name"));
            goto error;
        }

        VIR_DEBUG("%p f=%d f=%d", &addr, runp->ai_family, addr.data.sa.sa_family);

302
        if (VIR_EXPAND_N(socks, nsocks, 1) < 0)
303 304 305 306 307 308 309 310
            goto error;

        if (!(socks[nsocks-1] = virNetSocketNew(&addr, NULL, false, fd, -1, 0)))
            goto error;
        runp = runp->ai_next;
        fd = -1;
    }

311 312 313 314 315 316
    if (nsocks == 0 &&
        addrInUse) {
        virReportSystemError(EADDRINUSE, "%s", _("Unable to bind to port"));
        goto error;
    }

317 318 319 320 321 322 323
    freeaddrinfo(ai);

    *retsocks = socks;
    *nretsocks = nsocks;
    return 0;

error:
324
    for (i = 0; i < nsocks; i++)
325
        virObjectUnref(socks[i]);
326 327 328 329 330 331 332 333 334 335
    VIR_FREE(socks);
    freeaddrinfo(ai);
    VIR_FORCE_CLOSE(fd);
    return -1;
}


#if HAVE_SYS_UN_H
int virNetSocketNewListenUNIX(const char *path,
                              mode_t mask,
336
                              uid_t user,
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                              gid_t grp,
                              virNetSocketPtr *retsock)
{
    virSocketAddr addr;
    mode_t oldmask;
    int fd;

    *retsock = NULL;

    memset(&addr, 0, sizeof(addr));

    addr.len = sizeof(addr.data.un);

    if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
        virReportSystemError(errno, "%s", _("Failed to create socket"));
        goto error;
    }

    addr.data.un.sun_family = AF_UNIX;
    if (virStrcpyStatic(addr.data.un.sun_path, path) == NULL) {
357 358
        virReportSystemError(ENAMETOOLONG,
                             _("Path %s too long for unix socket"), path);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        goto error;
    }
    if (addr.data.un.sun_path[0] == '@')
        addr.data.un.sun_path[0] = '\0';
    else
        unlink(addr.data.un.sun_path);

    oldmask = umask(~mask);

    if (bind(fd, &addr.data.sa, addr.len) < 0) {
        umask(oldmask);
        virReportSystemError(errno,
                             _("Failed to bind socket to '%s'"),
                             path);
        goto error;
    }
    umask(oldmask);

    /* chown() doesn't work for abstract sockets but we use them only
     * if libvirtd runs unprivileged
     */
380
    if (grp != 0 && chown(path, user, grp)) {
381
        virReportSystemError(errno,
382 383
                             _("Failed to change ownership of '%s' to %d:%d"),
                             path, (int) user, (int) grp);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
        goto error;
    }

    if (!(*retsock = virNetSocketNew(&addr, NULL, false, fd, -1, 0)))
        goto error;

    return 0;

error:
    if (path[0] != '@')
        unlink(path);
    VIR_FORCE_CLOSE(fd);
    return -1;
}
#else
int virNetSocketNewListenUNIX(const char *path ATTRIBUTE_UNUSED,
                              mode_t mask ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
401
                              uid_t user ATTRIBUTE_UNUSED,
402 403 404 405 406 407 408 409 410
                              gid_t grp ATTRIBUTE_UNUSED,
                              virNetSocketPtr *retsock ATTRIBUTE_UNUSED)
{
    virReportSystemError(ENOSYS, "%s",
                         _("UNIX sockets are not supported on this platform"));
    return -1;
}
#endif

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
int virNetSocketNewListenFD(int fd,
                            virNetSocketPtr *retsock)
{
    virSocketAddr addr;
    *retsock = NULL;

    memset(&addr, 0, sizeof(addr));

    addr.len = sizeof(addr.data);
    if (getsockname(fd, &addr.data.sa, &addr.len) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get local socket name"));
        return -1;
    }

    if (!(*retsock = virNetSocketNew(&addr, NULL, false, fd, -1, 0)))
        return -1;

    return 0;
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

int virNetSocketNewConnectTCP(const char *nodename,
                              const char *service,
                              virNetSocketPtr *retsock)
{
    struct addrinfo *ai = NULL;
    struct addrinfo hints;
    int fd = -1;
    virSocketAddr localAddr;
    virSocketAddr remoteAddr;
    struct addrinfo *runp;
    int savedErrno = ENOENT;

    *retsock = NULL;

    memset(&localAddr, 0, sizeof(localAddr));
    memset(&remoteAddr, 0, sizeof(remoteAddr));

449
    memset(&hints, 0, sizeof(hints));
450 451 452 453 454
    hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
    hints.ai_socktype = SOCK_STREAM;

    int e = getaddrinfo(nodename, service, &hints, &ai);
    if (e != 0) {
455 456
        virReportError(VIR_ERR_SYSTEM_ERROR,
                       _("Unable to resolve address '%s' service '%s': %s"),
457
                       nodename, service, gai_strerror(e));
458 459 460 461 462 463 464 465 466 467 468 469 470
        return -1;
    }

    runp = ai;
    while (runp) {
        int opt = 1;

        if ((fd = socket(runp->ai_family, runp->ai_socktype,
                         runp->ai_protocol)) < 0) {
            virReportSystemError(errno, "%s", _("Unable to create socket"));
            goto error;
        }

471 472 473
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
            VIR_WARN("Unable to enable port reuse");
        }
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

        if (connect(fd, runp->ai_addr, runp->ai_addrlen) >= 0)
            break;

        savedErrno = errno;
        VIR_FORCE_CLOSE(fd);
        runp = runp->ai_next;
    }

    if (fd == -1) {
        virReportSystemError(savedErrno,
                             _("unable to connect to server at '%s:%s'"),
                             nodename, service);
        goto error;
    }

    localAddr.len = sizeof(localAddr.data);
    if (getsockname(fd, &localAddr.data.sa, &localAddr.len) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get local socket name"));
        goto error;
    }

    remoteAddr.len = sizeof(remoteAddr.data);
    if (getpeername(fd, &remoteAddr.data.sa, &remoteAddr.len) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get remote socket name"));
        goto error;
    }

    if (!(*retsock = virNetSocketNew(&localAddr, &remoteAddr, true, fd, -1, 0)))
        goto error;

    freeaddrinfo(ai);

    return 0;

error:
    freeaddrinfo(ai);
    VIR_FORCE_CLOSE(fd);
    return -1;
}


516
#ifdef HAVE_SYS_UN_H
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
int virNetSocketNewConnectUNIX(const char *path,
                               bool spawnDaemon,
                               const char *binary,
                               virNetSocketPtr *retsock)
{
    virSocketAddr localAddr;
    virSocketAddr remoteAddr;
    int fd;
    int retries = 0;

    memset(&localAddr, 0, sizeof(localAddr));
    memset(&remoteAddr, 0, sizeof(remoteAddr));

    remoteAddr.len = sizeof(remoteAddr.data.un);

532
    if (spawnDaemon && !binary) {
533
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
534
                       _("Auto-spawn of daemon requested, but no binary specified"));
535 536 537
        return -1;
    }

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
        virReportSystemError(errno, "%s", _("Failed to create socket"));
        goto error;
    }

    remoteAddr.data.un.sun_family = AF_UNIX;
    if (virStrcpyStatic(remoteAddr.data.un.sun_path, path) == NULL) {
        virReportSystemError(ENOMEM, _("Path %s too long for unix socket"), path);
        goto error;
    }
    if (remoteAddr.data.un.sun_path[0] == '@')
        remoteAddr.data.un.sun_path[0] = '\0';

retry:
    if (connect(fd, &remoteAddr.data.sa, remoteAddr.len) < 0) {
553 554 555 556 557
        if ((errno == ECONNREFUSED ||
             errno == ENOENT) &&
            spawnDaemon && retries < 20) {
            VIR_DEBUG("Connection refused for %s, trying to spawn %s",
                      path, binary);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
            if (retries == 0 &&
                virNetSocketForkDaemon(binary) < 0)
                goto error;

            retries++;
            usleep(1000 * 100 * retries);
            goto retry;
        }

        virReportSystemError(errno,
                             _("Failed to connect socket to '%s'"),
                             path);
        goto error;
    }

    localAddr.len = sizeof(localAddr.data);
    if (getsockname(fd, &localAddr.data.sa, &localAddr.len) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get local socket name"));
        goto error;
    }

    if (!(*retsock = virNetSocketNew(&localAddr, &remoteAddr, true, fd, -1, 0)))
        goto error;

    return 0;

error:
    VIR_FORCE_CLOSE(fd);
    return -1;
}
#else
int virNetSocketNewConnectUNIX(const char *path ATTRIBUTE_UNUSED,
                               bool spawnDaemon ATTRIBUTE_UNUSED,
                               const char *binary ATTRIBUTE_UNUSED,
                               virNetSocketPtr *retsock ATTRIBUTE_UNUSED)
{
    virReportSystemError(ENOSYS, "%s",
                         _("UNIX sockets are not supported on this platform"));
    return -1;
}
#endif


#ifndef WIN32
int virNetSocketNewConnectCommand(virCommandPtr cmd,
                                  virNetSocketPtr *retsock)
{
    pid_t pid = 0;
606 607
    int sv[2] = { -1, -1 };
    int errfd[2] = { -1, -1 };
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

    *retsock = NULL;

    /* Fork off the external process.  Use socketpair to create a private
     * (unnamed) Unix domain socket to the child process so we don't have
     * to faff around with two file descriptors (a la 'pipe(2)').
     */
    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
        virReportSystemError(errno, "%s",
                             _("unable to create socket pair"));
        goto error;
    }

    if (pipe(errfd) < 0) {
        virReportSystemError(errno, "%s",
                             _("unable to create socket pair"));
        goto error;
    }

    virCommandSetInputFD(cmd, sv[1]);
    virCommandSetOutputFD(cmd, &sv[1]);
    virCommandSetErrorFD(cmd, &errfd[1]);

    if (virCommandRunAsync(cmd, &pid) < 0)
        goto error;

    /* Parent continues here. */
    VIR_FORCE_CLOSE(sv[1]);
    VIR_FORCE_CLOSE(errfd[1]);

    if (!(*retsock = virNetSocketNew(NULL, NULL, true, sv[0], errfd[0], pid)))
        goto error;

    virCommandFree(cmd);

    return 0;

error:
    VIR_FORCE_CLOSE(sv[0]);
    VIR_FORCE_CLOSE(sv[1]);
    VIR_FORCE_CLOSE(errfd[0]);
    VIR_FORCE_CLOSE(errfd[1]);

    virCommandAbort(cmd);
    virCommandFree(cmd);

    return -1;
}
#else
int virNetSocketNewConnectCommand(virCommandPtr cmd ATTRIBUTE_UNUSED,
                                  virNetSocketPtr *retsock ATTRIBUTE_UNUSED)
{
    virReportSystemError(errno, "%s",
                         _("Tunnelling sockets not supported on this platform"));
    return -1;
}
#endif

int virNetSocketNewConnectSSH(const char *nodename,
                              const char *service,
                              const char *binary,
                              const char *username,
                              bool noTTY,
671
                              bool noVerify,
672
                              const char *netcat,
673
                              const char *keyfile,
674 675 676
                              const char *path,
                              virNetSocketPtr *retsock)
{
677
    char *quoted;
678
    virCommandPtr cmd;
679 680
    virBuffer buf = VIR_BUFFER_INITIALIZER;

681 682 683 684
    *retsock = NULL;

    cmd = virCommandNew(binary ? binary : "ssh");
    virCommandAddEnvPassCommon(cmd);
685 686 687 688 689
    virCommandAddEnvPassBlockSUID(cmd, "KRB5CCNAME", NULL);
    virCommandAddEnvPassBlockSUID(cmd, "SSH_AUTH_SOCK", NULL);
    virCommandAddEnvPassBlockSUID(cmd, "SSH_ASKPASS", NULL);
    virCommandAddEnvPassBlockSUID(cmd, "DISPLAY", NULL);
    virCommandAddEnvPassBlockSUID(cmd, "XAUTHORITY", NULL);
690 691 692 693 694 695
    virCommandClearCaps(cmd);

    if (service)
        virCommandAddArgList(cmd, "-p", service, NULL);
    if (username)
        virCommandAddArgList(cmd, "-l", username, NULL);
696 697
    if (keyfile)
        virCommandAddArgList(cmd, "-i", keyfile, NULL);
698 699 700
    if (noTTY)
        virCommandAddArgList(cmd, "-T", "-o", "BatchMode=yes",
                             "-e", "none", NULL);
701 702
    if (noVerify)
        virCommandAddArgList(cmd, "-o", "StrictHostKeyChecking=no", NULL);
703 704 705 706 707

    if (!netcat)
        netcat = "nc";

    virCommandAddArgList(cmd, nodename, "sh", "-c", NULL);
708 709 710

    virBufferEscapeShell(&buf, netcat);
    if (virBufferError(&buf)) {
711
        virCommandFree(cmd);
712 713 714 715 716
        virBufferFreeAndReset(&buf);
        virReportOOMError();
        return -1;
    }
    quoted = virBufferContentAndReset(&buf);
717 718 719 720 721 722 723 724 725 726 727
    /*
     * This ugly thing is a shell script to detect availability of
     * the -q option for 'nc': debian and suse based distros need this
     * flag to ensure the remote nc will exit on EOF, so it will go away
     * when we close the connection tunnel. If it doesn't go away, subsequent
     * connection attempts will hang.
     *
     * Fedora's 'nc' doesn't have this option, and defaults to the desired
     * behavior.
     */
    virCommandAddArgFormat(cmd,
728
         "'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
729 730 731 732
             "ARG=-q0;"
         "else "
             "ARG=;"
         "fi;"
733 734
         "'%s' $ARG -U %s'",
         quoted, quoted, path);
735

736
    VIR_FREE(quoted);
737 738 739
    return virNetSocketNewConnectCommand(cmd, retsock);
}

740
#if WITH_SSH2
741 742 743 744 745 746 747 748 749 750
int
virNetSocketNewConnectLibSSH2(const char *host,
                              const char *port,
                              const char *username,
                              const char *privkey,
                              const char *knownHosts,
                              const char *knownHostsVerify,
                              const char *authMethods,
                              const char *command,
                              virConnectAuthPtr auth,
751
                              virURIPtr uri,
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
                              virNetSocketPtr *retsock)
{
    virNetSocketPtr sock = NULL;
    virNetSSHSessionPtr sess = NULL;
    unsigned int verify;
    int ret = -1;
    int portN;

    char *authMethodNext = NULL;
    char *authMethodsCopy = NULL;
    char *authMethod;

    /* port number will be verified while opening the socket */
    if (virStrToLong_i(port, NULL, 10, &portN) < 0) {
        virReportError(VIR_ERR_SSH, "%s",
                       _("Failed to parse port number"));
        goto error;
    }

    /* create ssh session context */
    if (!(sess = virNetSSHSessionNew()))
        goto error;

    /* set ssh session parameters */
    if (virNetSSHSessionAuthSetCallback(sess, auth) != 0)
        goto error;

    if (STRCASEEQ("auto", knownHostsVerify))
        verify = VIR_NET_SSH_HOSTKEY_VERIFY_AUTO_ADD;
    else if (STRCASEEQ("ignore", knownHostsVerify))
        verify = VIR_NET_SSH_HOSTKEY_VERIFY_IGNORE;
    else if (STRCASEEQ("normal", knownHostsVerify))
        verify = VIR_NET_SSH_HOSTKEY_VERIFY_NORMAL;
    else {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Invalid host key verification method: '%s'"),
                       knownHostsVerify);
        goto error;
    }

    if (virNetSSHSessionSetHostKeyVerification(sess,
                                               host,
                                               portN,
                                               knownHosts,
796 797
                                               verify,
                                               VIR_NET_SSH_HOSTKEY_FILE_CREATE) != 0)
798 799 800 801 802
        goto error;

    if (virNetSSHSessionSetChannelCommand(sess, command) != 0)
        goto error;

803
    if (VIR_STRDUP(authMethodsCopy, authMethods) < 0)
804
        goto error;
805 806

    authMethodNext = authMethodsCopy;
807 808 809 810 811 812

    while ((authMethod = strsep(&authMethodNext, ","))) {
        if (STRCASEEQ(authMethod, "keyboard-interactive"))
            ret = virNetSSHSessionAuthAddKeyboardAuth(sess, username, -1);
        else if (STRCASEEQ(authMethod, "password"))
            ret = virNetSSHSessionAuthAddPasswordAuth(sess,
813 814
                                                      uri,
                                                      username);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
        else if (STRCASEEQ(authMethod, "privkey"))
            ret = virNetSSHSessionAuthAddPrivKeyAuth(sess,
                                                     username,
                                                     privkey,
                                                     NULL);
        else if (STRCASEEQ(authMethod, "agent"))
            ret = virNetSSHSessionAuthAddAgentAuth(sess, username);
        else {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("Invalid authentication method: '%s'"),
                           authMethod);
            ret = -1;
            goto error;
        }

        if (ret != 0)
            goto error;
    }

    /* connect to remote server */
    if ((ret = virNetSocketNewConnectTCP(host, port, &sock)) < 0)
        goto error;

    /* connect to the host using ssh */
    if ((ret = virNetSSHSessionConnect(sess, virNetSocketGetFD(sock))) != 0)
        goto error;

    sock->sshSession = sess;
    *retsock = sock;

    VIR_FREE(authMethodsCopy);
    return 0;

error:
    virObjectUnref(sock);
    virObjectUnref(sess);
    VIR_FREE(authMethodsCopy);
    return ret;
}
#else
int
virNetSocketNewConnectLibSSH2(const char *host ATTRIBUTE_UNUSED,
                              const char *port ATTRIBUTE_UNUSED,
                              const char *username ATTRIBUTE_UNUSED,
                              const char *privkey ATTRIBUTE_UNUSED,
                              const char *knownHosts ATTRIBUTE_UNUSED,
                              const char *knownHostsVerify ATTRIBUTE_UNUSED,
                              const char *authMethods ATTRIBUTE_UNUSED,
                              const char *command ATTRIBUTE_UNUSED,
                              virConnectAuthPtr auth ATTRIBUTE_UNUSED,
865
                              virURIPtr uri ATTRIBUTE_UNUSED,
866 867 868 869 870 871
                              virNetSocketPtr *retsock ATTRIBUTE_UNUSED)
{
    virReportSystemError(ENOSYS, "%s",
                         _("libssh2 transport support was not enabled"));
    return -1;
}
872
#endif /* WITH_SSH2 */
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

int virNetSocketNewConnectExternal(const char **cmdargv,
                                   virNetSocketPtr *retsock)
{
    virCommandPtr cmd;

    *retsock = NULL;

    cmd = virCommandNewArgs(cmdargv);
    virCommandAddEnvPassCommon(cmd);
    virCommandClearCaps(cmd);

    return virNetSocketNewConnectCommand(cmd, retsock);
}


889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
int virNetSocketNewConnectSockFD(int sockfd,
                                 virNetSocketPtr *retsock)
{
    virSocketAddr localAddr;

    localAddr.len = sizeof(localAddr.data);
    if (getsockname(sockfd, &localAddr.data.sa, &localAddr.len) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get local socket name"));
        return -1;
    }

    if (!(*retsock = virNetSocketNew(&localAddr, NULL, true, sockfd, -1, -1)))
        return -1;

    return 0;
}


907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
virNetSocketPtr virNetSocketNewPostExecRestart(virJSONValuePtr object)
{
    virSocketAddr localAddr;
    virSocketAddr remoteAddr;
    int fd, thepid, errfd;
    bool isClient;

    if (virJSONValueObjectGetNumberInt(object, "fd", &fd) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing fd data in JSON document"));
        return NULL;
    }

    if (virJSONValueObjectGetNumberInt(object, "pid", &thepid) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing pid data in JSON document"));
        return NULL;
    }

    if (virJSONValueObjectGetNumberInt(object, "errfd", &errfd) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing errfd data in JSON document"));
        return NULL;
    }
    if (virJSONValueObjectGetBoolean(object, "isClient", &isClient) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing isClient data in JSON document"));
        return NULL;
    }

    memset(&localAddr, 0, sizeof(localAddr));
    memset(&remoteAddr, 0, sizeof(remoteAddr));

    remoteAddr.len = sizeof(remoteAddr.data.stor);
    if (getsockname(fd, &remoteAddr.data.sa, &remoteAddr.len) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get peer socket name"));
        return NULL;
    }

    localAddr.len = sizeof(localAddr.data.stor);
    if (getsockname(fd, &localAddr.data.sa, &localAddr.len) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get local socket name"));
        return NULL;
    }

    return virNetSocketNew(&localAddr, &remoteAddr,
                           isClient, fd, errfd, thepid);
}


virJSONValuePtr virNetSocketPreExecRestart(virNetSocketPtr sock)
{
    virJSONValuePtr object = NULL;

961
    virObjectLock(sock);
962

963
#if WITH_SASL
964 965 966 967 968 969
    if (sock->saslSession) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Unable to save socket state when SASL session is active"));
        goto error;
    }
#endif
970
#if WITH_GNUTLS
971 972 973 974 975
    if (sock->tlsSession) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("Unable to save socket state when TLS session is active"));
        goto error;
    }
976
#endif
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

    if (!(object = virJSONValueNewObject()))
        goto error;

    if (virJSONValueObjectAppendNumberInt(object, "fd", sock->fd) < 0)
        goto error;

    if (virJSONValueObjectAppendNumberInt(object, "errfd", sock->errfd) < 0)
        goto error;

    if (virJSONValueObjectAppendNumberInt(object, "pid", sock->pid) < 0)
        goto error;

    if (virJSONValueObjectAppendBoolean(object, "isClient", sock->client) < 0)
        goto error;

    if (virSetInherit(sock->fd, true) < 0) {
        virReportSystemError(errno,
                             _("Cannot disable close-on-exec flag on socket %d"),
                             sock->fd);
        goto error;
    }
    if (sock->errfd != -1 &&
        virSetInherit(sock->errfd, true) < 0) {
        virReportSystemError(errno,
                             _("Cannot disable close-on-exec flag on pipe %d"),
                             sock->errfd);
        goto error;
    }

1007
    virObjectUnlock(sock);
1008 1009 1010
    return object;

error:
1011
    virObjectUnlock(sock);
1012 1013 1014 1015 1016
    virJSONValueFree(object);
    return NULL;
}


1017
void virNetSocketDispose(void *obj)
1018
{
1019
    virNetSocketPtr sock = obj;
1020

1021 1022 1023
    PROBE(RPC_SOCKET_DISPOSE,
          "sock=%p", sock);

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    if (sock->watch > 0) {
        virEventRemoveHandle(sock->watch);
        sock->watch = -1;
    }

#ifdef HAVE_SYS_UN_H
    /* If a server socket, then unlink UNIX path */
    if (!sock->client &&
        sock->localAddr.data.sa.sa_family == AF_UNIX &&
        sock->localAddr.data.un.sun_path[0] != '\0')
        unlink(sock->localAddr.data.un.sun_path);
#endif

1037
#if WITH_GNUTLS
1038 1039 1040
    /* Make sure it can't send any more I/O during shutdown */
    if (sock->tlsSession)
        virNetTLSSessionSetIOCallbacks(sock->tlsSession, NULL, NULL, NULL);
1041
    virObjectUnref(sock->tlsSession);
1042
#endif
1043
#if WITH_SASL
1044
    virObjectUnref(sock->saslSession);
1045 1046
#endif

1047
#if WITH_SSH2
1048 1049 1050
    virObjectUnref(sock->sshSession);
#endif

1051 1052 1053
    VIR_FORCE_CLOSE(sock->fd);
    VIR_FORCE_CLOSE(sock->errfd);

1054
    virProcessAbort(sock->pid);
1055 1056 1057 1058 1059 1060 1061 1062

    VIR_FREE(sock->localAddrStr);
    VIR_FREE(sock->remoteAddrStr);
}


int virNetSocketGetFD(virNetSocketPtr sock)
{
1063
    int fd;
1064
    virObjectLock(sock);
1065
    fd = sock->fd;
1066
    virObjectUnlock(sock);
1067
    return fd;
1068 1069 1070
}


1071 1072 1073 1074 1075
int virNetSocketDupFD(virNetSocketPtr sock, bool cloexec)
{
    int fd;

    if (cloexec)
1076
        fd = fcntl(sock->fd, F_DUPFD_CLOEXEC, 0);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    else
        fd = dup(sock->fd);
    if (fd < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to copy socket file handle"));
        return -1;
    }
    return fd;
}


1088 1089
bool virNetSocketIsLocal(virNetSocketPtr sock)
{
1090
    bool isLocal = false;
1091
    virObjectLock(sock);
1092
    if (sock->localAddr.data.sa.sa_family == AF_UNIX)
1093
        isLocal = true;
1094
    virObjectUnlock(sock);
1095
    return isLocal;
1096 1097 1098
}


1099 1100 1101
bool virNetSocketHasPassFD(virNetSocketPtr sock)
{
    bool hasPassFD = false;
1102
    virObjectLock(sock);
1103 1104
    if (sock->localAddr.data.sa.sa_family == AF_UNIX)
        hasPassFD = true;
1105
    virObjectUnlock(sock);
1106 1107 1108 1109
    return hasPassFD;
}


1110 1111
int virNetSocketGetPort(virNetSocketPtr sock)
{
1112
    int port;
1113
    virObjectLock(sock);
1114
    port = virSocketAddrGetPort(&sock->localAddr);
1115
    virObjectUnlock(sock);
1116
    return port;
1117 1118 1119
}


1120
#if defined(SO_PEERCRED)
1121 1122 1123
int virNetSocketGetUNIXIdentity(virNetSocketPtr sock,
                                uid_t *uid,
                                gid_t *gid,
1124 1125
                                pid_t *pid,
                                unsigned long long *timestamp)
1126 1127
{
    struct ucred cr;
1128
    socklen_t cr_len = sizeof(cr);
1129 1130
    int ret = -1;

1131
    virObjectLock(sock);
1132 1133 1134 1135

    if (getsockopt(sock->fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) < 0) {
        virReportSystemError(errno, "%s",
                             _("Failed to get client socket identity"));
1136
        goto cleanup;
1137 1138
    }

1139 1140 1141
    if (virProcessGetStartTime(cr.pid, timestamp) < 0)
        goto cleanup;

1142 1143
    *pid = cr.pid;
    *uid = cr.uid;
1144
    *gid = cr.gid;
1145

1146 1147 1148
    ret = 0;

cleanup:
1149
    virObjectUnlock(sock);
1150
    return ret;
1151
}
1152
#elif defined(LOCAL_PEERCRED)
1153

1154 1155 1156
/* VIR_SOL_PEERCRED - the value needed to let getsockopt() work with
 * LOCAL_PEERCRED
 */
1157 1158 1159 1160

/* Mac OS X 10.8 provides SOL_LOCAL for LOCAL_PEERCRED */
# ifdef SOL_LOCAL
#  define VIR_SOL_PEERCRED SOL_LOCAL
1161
# else
1162 1163 1164 1165 1166 1167
/* FreeBSD and Mac OS X prior to 10.7, SOL_LOCAL is not defined and
 * users are expected to supply 0 as the second value for getsockopt()
 * when using LOCAL_PEERCRED. NB SOL_SOCKET cannot be used instead
 * of SOL_LOCAL
 */
#  define VIR_SOL_PEERCRED 0
1168 1169
# endif

1170 1171 1172
int virNetSocketGetUNIXIdentity(virNetSocketPtr sock,
                                uid_t *uid,
                                gid_t *gid,
1173
                                pid_t *pid,
1174
                                unsigned long long *timestamp)
1175 1176 1177
{
    struct xucred cr;
    socklen_t cr_len = sizeof(cr);
1178 1179
    int ret = -1;

1180
    virObjectLock(sock);
1181

1182
    cr.cr_ngroups = -1;
1183
    if (getsockopt(sock->fd, VIR_SOL_PEERCRED, LOCAL_PEERCRED, &cr, &cr_len) < 0) {
1184 1185
        virReportSystemError(errno, "%s",
                             _("Failed to get client socket identity"));
1186
        goto cleanup;
1187 1188
    }

1189 1190 1191
    if (cr.cr_version != XUCRED_VERSION) {
        virReportError(VIR_ERR_SYSTEM_ERROR, "%s",
                       _("Failed to get valid client socket identity"));
1192
        goto cleanup;
1193 1194
    }

1195
    if (cr.cr_ngroups <= 0 || cr.cr_ngroups > NGROUPS) {
1196 1197
        virReportError(VIR_ERR_SYSTEM_ERROR, "%s",
                       _("Failed to get valid client socket identity groups"));
1198
        goto cleanup;
1199 1200
    }

1201 1202 1203
    /* PID and process creation time are not supported on BSDs by
     * LOCAL_PEERCRED.
     */
1204
    *pid = -1;
1205
    *timestamp = -1;
1206 1207 1208
    *uid = cr.cr_uid;
    *gid = cr.cr_gid;

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
# ifdef LOCAL_PEERPID
    /* Exists on Mac OS X 10.8 for retrieving the peer's PID */
    cr_len = sizeof(*pid);

    if (getsockopt(sock->fd, VIR_SOL_PEERCRED, LOCAL_PEERPID, pid, &cr_len) < 0) {
        /* Ensure this is set to something sane as there are no guarantees
         * as to what its set to now.
         */
        *pid = -1;

        /* If this was built on a system with LOCAL_PEERPID defined but
         * the kernel doesn't support it we'll get back EOPNOTSUPP so
         * treat all errors but EOPNOTSUPP as fatal
         */
        if (errno != EOPNOTSUPP) {
            virReportSystemError(errno, "%s",
                    _("Failed to get client socket PID"));
            goto cleanup;
        }
    }
# endif

1231 1232 1233
    ret = 0;

cleanup:
1234
    virObjectUnlock(sock);
1235
    return ret;
1236
}
1237
#else
1238 1239 1240
int virNetSocketGetUNIXIdentity(virNetSocketPtr sock ATTRIBUTE_UNUSED,
                                uid_t *uid ATTRIBUTE_UNUSED,
                                gid_t *gid ATTRIBUTE_UNUSED,
1241 1242
                                pid_t *pid ATTRIBUTE_UNUSED,
                                unsigned long long *timestamp ATTRIBUTE_UNUSED)
1243 1244 1245 1246 1247 1248 1249 1250
{
    /* XXX Many more OS support UNIX socket credentials we could port to. See dbus ....*/
    virReportSystemError(ENOSYS, "%s",
                         _("Client socket identity not available"));
    return -1;
}
#endif

1251
#ifdef WITH_SELINUX
1252 1253
int virNetSocketGetSELinuxContext(virNetSocketPtr sock,
                                  char **context)
1254 1255 1256 1257 1258 1259
{
    security_context_t seccon = NULL;
    int ret = -1;

    *context = NULL;

1260
    virObjectLock(sock);
1261
    if (getpeercon(sock->fd, &seccon) < 0) {
1262
        if (errno == ENOSYS || errno == ENOPROTOOPT) {
1263 1264 1265 1266 1267 1268 1269 1270
            ret = 0;
            goto cleanup;
        }
        virReportSystemError(errno, "%s",
                             _("Unable to query peer security context"));
        goto cleanup;
    }

1271
    if (VIR_STRDUP(*context, seccon) < 0)
1272 1273 1274 1275 1276
        goto cleanup;

    ret = 0;
cleanup:
    freecon(seccon);
1277
    virObjectUnlock(sock);
1278 1279 1280
    return ret;
}
#else
1281 1282
int virNetSocketGetSELinuxContext(virNetSocketPtr sock ATTRIBUTE_UNUSED,
                                  char **context)
1283 1284 1285 1286 1287 1288
{
    *context = NULL;
    return 0;
}
#endif

1289 1290 1291 1292

int virNetSocketSetBlocking(virNetSocketPtr sock,
                            bool blocking)
{
1293
    int ret;
1294
    virObjectLock(sock);
1295
    ret = virSetBlocking(sock->fd, blocking);
1296
    virObjectUnlock(sock);
1297
    return ret;
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
}


const char *virNetSocketLocalAddrString(virNetSocketPtr sock)
{
    return sock->localAddrStr;
}

const char *virNetSocketRemoteAddrString(virNetSocketPtr sock)
{
    return sock->remoteAddrStr;
}

1311

1312
#if WITH_GNUTLS
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
static ssize_t virNetSocketTLSSessionWrite(const char *buf,
                                           size_t len,
                                           void *opaque)
{
    virNetSocketPtr sock = opaque;
    return write(sock->fd, buf, len);
}


static ssize_t virNetSocketTLSSessionRead(char *buf,
                                          size_t len,
                                          void *opaque)
{
    virNetSocketPtr sock = opaque;
    return read(sock->fd, buf, len);
}


void virNetSocketSetTLSSession(virNetSocketPtr sock,
                               virNetTLSSessionPtr sess)
{
1334
    virObjectLock(sock);
1335 1336
    virObjectUnref(sock->tlsSession);
    sock->tlsSession = virObjectRef(sess);
1337 1338 1339 1340
    virNetTLSSessionSetIOCallbacks(sess,
                                   virNetSocketTLSSessionWrite,
                                   virNetSocketTLSSessionRead,
                                   sock);
1341
    virObjectUnlock(sock);
1342
}
1343
#endif
1344

1345
#if WITH_SASL
1346 1347 1348
void virNetSocketSetSASLSession(virNetSocketPtr sock,
                                virNetSASLSessionPtr sess)
{
1349
    virObjectLock(sock);
1350 1351
    virObjectUnref(sock->saslSession);
    sock->saslSession = virObjectRef(sess);
1352
    virObjectUnlock(sock);
1353 1354 1355 1356 1357 1358
}
#endif


bool virNetSocketHasCachedData(virNetSocketPtr sock ATTRIBUTE_UNUSED)
{
1359
    bool hasCached = false;
1360
    virObjectLock(sock);
1361

1362
#if WITH_SSH2
1363 1364 1365 1366
    if (virNetSSHSessionHasCachedData(sock->sshSession))
        hasCached = true;
#endif

1367
#if WITH_SASL
1368
    if (sock->saslDecoded)
1369
        hasCached = true;
1370
#endif
1371
    virObjectUnlock(sock);
1372
    return hasCached;
1373 1374
}

1375
#if WITH_SSH2
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
static ssize_t virNetSocketLibSSH2Read(virNetSocketPtr sock,
                                       char *buf,
                                       size_t len)
{
    return virNetSSHChannelRead(sock->sshSession, buf, len);
}

static ssize_t virNetSocketLibSSH2Write(virNetSocketPtr sock,
                                        const char *buf,
                                        size_t len)
{
    return virNetSSHChannelWrite(sock->sshSession, buf, len);
}
#endif
1390

1391 1392 1393
bool virNetSocketHasPendingData(virNetSocketPtr sock ATTRIBUTE_UNUSED)
{
    bool hasPending = false;
1394
    virObjectLock(sock);
1395
#if WITH_SASL
1396 1397 1398
    if (sock->saslEncoded)
        hasPending = true;
#endif
1399
    virObjectUnlock(sock);
1400 1401 1402 1403
    return hasPending;
}


1404
static ssize_t virNetSocketReadWire(virNetSocketPtr sock, char *buf, size_t len)
1405 1406 1407
{
    char *errout = NULL;
    ssize_t ret;
1408

1409
#if WITH_SSH2
1410 1411 1412 1413
    if (sock->sshSession)
        return virNetSocketLibSSH2Read(sock, buf, len);
#endif

1414
reread:
1415
#if WITH_GNUTLS
1416 1417 1418 1419 1420
    if (sock->tlsSession &&
        virNetTLSSessionGetHandshakeStatus(sock->tlsSession) ==
        VIR_NET_TLS_HANDSHAKE_COMPLETE) {
        ret = virNetTLSSessionRead(sock->tlsSession, buf, len);
    } else {
1421
#endif
1422
        ret = read(sock->fd, buf, len);
1423
#if WITH_GNUTLS
1424
    }
1425
#endif
1426 1427 1428 1429 1430

    if ((ret < 0) && (errno == EINTR))
        goto reread;
    if ((ret < 0) && (errno == EAGAIN))
        return 0;
1431

1432 1433 1434 1435 1436
    if (ret <= 0 &&
        sock->errfd != -1 &&
        virFileReadLimFD(sock->errfd, 1024, &errout) >= 0 &&
        errout != NULL) {
        size_t elen = strlen(errout);
1437 1438 1439
        /* remove trailing whitespace */
        while (elen && c_isspace(errout[elen - 1]))
            errout[--elen] = '\0';
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    }

    if (ret < 0) {
        if (errout)
            virReportSystemError(errno,
                                 _("Cannot recv data: %s"), errout);
        else
            virReportSystemError(errno, "%s",
                                 _("Cannot recv data"));
        ret = -1;
    } else if (ret == 0) {
        if (errout)
            virReportSystemError(EIO,
                                 _("End of file while reading data: %s"), errout);
        else
            virReportSystemError(EIO, "%s",
                                 _("End of file while reading data"));
        ret = -1;
    }

    VIR_FREE(errout);
    return ret;
}

1464
static ssize_t virNetSocketWriteWire(virNetSocketPtr sock, const char *buf, size_t len)
1465 1466
{
    ssize_t ret;
1467

1468
#if WITH_SSH2
1469 1470 1471 1472
    if (sock->sshSession)
        return virNetSocketLibSSH2Write(sock, buf, len);
#endif

1473
rewrite:
1474
#if WITH_GNUTLS
1475 1476 1477 1478 1479
    if (sock->tlsSession &&
        virNetTLSSessionGetHandshakeStatus(sock->tlsSession) ==
        VIR_NET_TLS_HANDSHAKE_COMPLETE) {
        ret = virNetTLSSessionWrite(sock->tlsSession, buf, len);
    } else {
1480
#endif
1481
        ret = write(sock->fd, buf, len);
1482
#if WITH_GNUTLS
1483
    }
1484
#endif
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505

    if (ret < 0) {
        if (errno == EINTR)
            goto rewrite;
        if (errno == EAGAIN)
            return 0;

        virReportSystemError(errno, "%s",
                             _("Cannot write data"));
        return -1;
    }
    if (ret == 0) {
        virReportSystemError(EIO, "%s",
                             _("End of file while writing data"));
        return -1;
    }

    return ret;
}


1506
#if WITH_SASL
1507 1508 1509 1510 1511 1512 1513 1514
static ssize_t virNetSocketReadSASL(virNetSocketPtr sock, char *buf, size_t len)
{
    ssize_t got;

    /* Need to read some more data off the wire */
    if (sock->saslDecoded == NULL) {
        ssize_t encodedLen = virNetSASLSessionGetMaxBufSize(sock->saslSession);
        char *encoded;
1515
        if (VIR_ALLOC_N(encoded, encodedLen) < 0)
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 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
            return -1;
        encodedLen = virNetSocketReadWire(sock, encoded, encodedLen);

        if (encodedLen <= 0) {
            VIR_FREE(encoded);
            return encodedLen;
        }

        if (virNetSASLSessionDecode(sock->saslSession,
                                    encoded, encodedLen,
                                    &sock->saslDecoded, &sock->saslDecodedLength) < 0) {
            VIR_FREE(encoded);
            return -1;
        }
        VIR_FREE(encoded);

        sock->saslDecodedOffset = 0;
    }

    /* Some buffered decoded data to return now */
    got = sock->saslDecodedLength - sock->saslDecodedOffset;

    if (len > got)
        len = got;

    memcpy(buf, sock->saslDecoded + sock->saslDecodedOffset, len);
    sock->saslDecodedOffset += len;

    if (sock->saslDecodedOffset == sock->saslDecodedLength) {
        sock->saslDecoded = NULL;
        sock->saslDecodedOffset = sock->saslDecodedLength = 0;
    }

    return len;
}


static ssize_t virNetSocketWriteSASL(virNetSocketPtr sock, const char *buf, size_t len)
{
    int ret;
    size_t tosend = virNetSASLSessionGetMaxBufSize(sock->saslSession);

    /* SASL doesn't necessarily let us send the whole
       buffer at once */
    if (tosend > len)
        tosend = len;

    /* Not got any pending encoded data, so we need to encode raw stuff */
    if (sock->saslEncoded == NULL) {
        if (virNetSASLSessionEncode(sock->saslSession,
                                    buf, tosend,
                                    &sock->saslEncoded,
                                    &sock->saslEncodedLength) < 0)
            return -1;

        sock->saslEncodedOffset = 0;
    }

    /* Send some of the encoded stuff out on the wire */
    ret = virNetSocketWriteWire(sock,
                                sock->saslEncoded + sock->saslEncodedOffset,
                                sock->saslEncodedLength - sock->saslEncodedOffset);

    if (ret <= 0)
        return ret; /* -1 error, 0 == egain */

    /* Note how much we sent */
    sock->saslEncodedOffset += ret;

    /* Sent all encoded, so update raw buffer to indicate completion */
    if (sock->saslEncodedOffset == sock->saslEncodedLength) {
        sock->saslEncoded = NULL;
        sock->saslEncodedOffset = sock->saslEncodedLength = 0;

        /* Mark as complete, so caller detects completion */
        return tosend;
    } else {
        /* Still have stuff pending in saslEncoded buffer.
         * Pretend to caller that we didn't send any yet.
         * The caller will then retry with same buffer
         * shortly, which lets us finish saslEncoded.
         */
        return 0;
    }
}
#endif

ssize_t virNetSocketRead(virNetSocketPtr sock, char *buf, size_t len)
{
1605
    ssize_t ret;
1606
    virObjectLock(sock);
1607
#if WITH_SASL
1608
    if (sock->saslSession)
1609
        ret = virNetSocketReadSASL(sock, buf, len);
1610 1611
    else
#endif
1612
        ret = virNetSocketReadWire(sock, buf, len);
1613
    virObjectUnlock(sock);
1614
    return ret;
1615 1616 1617 1618
}

ssize_t virNetSocketWrite(virNetSocketPtr sock, const char *buf, size_t len)
{
1619 1620
    ssize_t ret;

1621
    virObjectLock(sock);
1622
#if WITH_SASL
1623
    if (sock->saslSession)
1624
        ret = virNetSocketWriteSASL(sock, buf, len);
1625 1626
    else
#endif
1627
        ret = virNetSocketWriteWire(sock, buf, len);
1628
    virObjectUnlock(sock);
1629
    return ret;
1630 1631 1632
}


1633 1634 1635
/*
 * Returns 1 if an FD was sent, 0 if it would block, -1 on error
 */
1636 1637 1638 1639
int virNetSocketSendFD(virNetSocketPtr sock, int fd)
{
    int ret = -1;
    if (!virNetSocketHasPassFD(sock)) {
1640
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1641
                       _("Sending file descriptors is not supported on this socket"));
1642 1643
        return -1;
    }
1644
    virObjectLock(sock);
1645 1646 1647
    PROBE(RPC_SOCKET_SEND_FD,
          "sock=%p fd=%d", sock, fd);
    if (sendfd(sock->fd, fd) < 0) {
1648 1649 1650 1651 1652 1653
        if (errno == EAGAIN)
            ret = 0;
        else
            virReportSystemError(errno,
                                 _("Failed to send file descriptor %d"),
                                 fd);
1654 1655
        goto cleanup;
    }
1656
    ret = 1;
1657 1658

cleanup:
1659
    virObjectUnlock(sock);
1660 1661 1662 1663
    return ret;
}


1664 1665 1666 1667
/*
 * Returns 1 if an FD was read, 0 if it would block, -1 on error
 */
int virNetSocketRecvFD(virNetSocketPtr sock, int *fd)
1668 1669
{
    int ret = -1;
1670 1671 1672

    *fd = -1;

1673
    if (!virNetSocketHasPassFD(sock)) {
1674
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1675
                       _("Receiving file descriptors is not supported on this socket"));
1676 1677
        return -1;
    }
1678
    virObjectLock(sock);
1679

1680 1681 1682 1683 1684 1685
    if ((*fd = recvfd(sock->fd, O_CLOEXEC)) < 0) {
        if (errno == EAGAIN)
            ret = 0;
        else
            virReportSystemError(errno, "%s",
                                 _("Failed to recv file descriptor"));
1686 1687 1688
        goto cleanup;
    }
    PROBE(RPC_SOCKET_RECV_FD,
1689 1690
          "sock=%p fd=%d", sock, *fd);
    ret = 1;
1691 1692

cleanup:
1693
    virObjectUnlock(sock);
1694 1695 1696 1697
    return ret;
}


1698
int virNetSocketListen(virNetSocketPtr sock, int backlog)
1699
{
1700
    virObjectLock(sock);
1701
    if (listen(sock->fd, backlog > 0 ? backlog : 30) < 0) {
1702
        virReportSystemError(errno, "%s", _("Unable to listen on socket"));
1703
        virObjectUnlock(sock);
1704 1705
        return -1;
    }
1706
    virObjectUnlock(sock);
1707 1708 1709 1710 1711
    return 0;
}

int virNetSocketAccept(virNetSocketPtr sock, virNetSocketPtr *clientsock)
{
1712
    int fd = -1;
1713 1714
    virSocketAddr localAddr;
    virSocketAddr remoteAddr;
1715 1716
    int ret = -1;

1717
    virObjectLock(sock);
1718 1719 1720 1721 1722 1723 1724 1725 1726

    *clientsock = NULL;

    memset(&localAddr, 0, sizeof(localAddr));
    memset(&remoteAddr, 0, sizeof(remoteAddr));

    remoteAddr.len = sizeof(remoteAddr.data.stor);
    if ((fd = accept(sock->fd, &remoteAddr.data.sa, &remoteAddr.len)) < 0) {
        if (errno == ECONNABORTED ||
1727 1728 1729 1730
            errno == EAGAIN) {
            ret = 0;
            goto cleanup;
        }
1731 1732 1733

        virReportSystemError(errno, "%s",
                             _("Unable to accept client"));
1734
        goto cleanup;
1735 1736 1737 1738 1739
    }

    localAddr.len = sizeof(localAddr.data);
    if (getsockname(fd, &localAddr.data.sa, &localAddr.len) < 0) {
        virReportSystemError(errno, "%s", _("Unable to get local socket name"));
1740
        goto cleanup;
1741 1742 1743 1744 1745
    }

    if (!(*clientsock = virNetSocketNew(&localAddr,
                                        &remoteAddr,
                                        true,
1746 1747
                                        fd, -1, 0)))
        goto cleanup;
1748

1749 1750 1751 1752 1753
    fd = -1;
    ret = 0;

cleanup:
    VIR_FORCE_CLOSE(fd);
1754
    virObjectUnlock(sock);
1755
    return ret;
1756 1757 1758
}


W
Wen Congyang 已提交
1759 1760
static void virNetSocketEventHandle(int watch ATTRIBUTE_UNUSED,
                                    int fd ATTRIBUTE_UNUSED,
1761 1762 1763 1764
                                    int events,
                                    void *opaque)
{
    virNetSocketPtr sock = opaque;
1765 1766
    virNetSocketIOFunc func;
    void *eopaque;
1767

1768
    virObjectLock(sock);
1769 1770
    func = sock->func;
    eopaque = sock->opaque;
1771
    virObjectUnlock(sock);
1772 1773 1774

    if (func)
        func(sock, events, eopaque);
1775 1776
}

1777

1778 1779 1780 1781 1782 1783
static void virNetSocketEventFree(void *opaque)
{
    virNetSocketPtr sock = opaque;
    virFreeCallback ff;
    void *eopaque;

1784
    virObjectLock(sock);
1785 1786 1787 1788 1789
    ff = sock->ff;
    eopaque = sock->opaque;
    sock->func = NULL;
    sock->ff = NULL;
    sock->opaque = NULL;
1790
    virObjectUnlock(sock);
1791 1792 1793 1794

    if (ff)
        ff(eopaque);

1795
    virObjectUnref(sock);
1796 1797
}

1798 1799 1800
int virNetSocketAddIOCallback(virNetSocketPtr sock,
                              int events,
                              virNetSocketIOFunc func,
1801 1802
                              void *opaque,
                              virFreeCallback ff)
1803
{
1804 1805
    int ret = -1;

1806
    virObjectRef(sock);
1807
    virObjectLock(sock);
1808 1809
    if (sock->watch > 0) {
        VIR_DEBUG("Watch already registered on socket %p", sock);
1810
        goto cleanup;
1811 1812 1813 1814 1815 1816
    }

    if ((sock->watch = virEventAddHandle(sock->fd,
                                         events,
                                         virNetSocketEventHandle,
                                         sock,
1817
                                         virNetSocketEventFree)) < 0) {
1818
        VIR_DEBUG("Failed to register watch on socket %p", sock);
1819
        goto cleanup;
1820 1821 1822
    }
    sock->func = func;
    sock->opaque = opaque;
1823
    sock->ff = ff;
1824

1825 1826 1827
    ret = 0;

cleanup:
1828
    virObjectUnlock(sock);
1829
    if (ret != 0)
1830
        virObjectUnref(sock);
1831
    return ret;
1832 1833 1834 1835 1836
}

void virNetSocketUpdateIOCallback(virNetSocketPtr sock,
                                  int events)
{
1837
    virObjectLock(sock);
1838 1839
    if (sock->watch <= 0) {
        VIR_DEBUG("Watch not registered on socket %p", sock);
1840
        virObjectUnlock(sock);
1841 1842 1843 1844
        return;
    }

    virEventUpdateHandle(sock->watch, events);
1845

1846
    virObjectUnlock(sock);
1847 1848 1849 1850
}

void virNetSocketRemoveIOCallback(virNetSocketPtr sock)
{
1851
    virObjectLock(sock);
1852

1853 1854
    if (sock->watch <= 0) {
        VIR_DEBUG("Watch not registered on socket %p", sock);
1855
        virObjectUnlock(sock);
1856 1857 1858 1859
        return;
    }

    virEventRemoveHandle(sock->watch);
1860

1861
    virObjectUnlock(sock);
1862
}
1863 1864 1865 1866 1867 1868

void virNetSocketClose(virNetSocketPtr sock)
{
    if (!sock)
        return;

1869
    virObjectLock(sock);
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882

    VIR_FORCE_CLOSE(sock->fd);

#ifdef HAVE_SYS_UN_H
    /* If a server socket, then unlink UNIX path */
    if (!sock->client &&
        sock->localAddr.data.sa.sa_family == AF_UNIX &&
        sock->localAddr.data.un.sun_path[0] != '\0') {
        if (unlink(sock->localAddr.data.un.sun_path) == 0)
            sock->localAddr.data.un.sun_path[0] = '\0';
    }
#endif

1883
    virObjectUnlock(sock);
1884
}