<commit_before>/*
 * remote_sockets.c: helpers for getting remote driver socket paths
 *
 * Copyright (C) 2007-2019 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "remote_sockets.h"
#include "virerror.h"
#include "virlog.h"
#include "virfile.h"
#include "virutil.h"
#include "configmake.h"

#define VIR_FROM_THIS VIR_FROM_REMOTE

VIR_LOG_INIT("remote.remote_sockets");

VIR_ENUM_IMPL(remoteDriverTransport,
              REMOTE_DRIVER_TRANSPORT_LAST,
              "tls",
              "unix",
              "ssh",
              "libssh2",
              "ext",
              "tcp",
              "libssh");

VIR_ENUM_IMPL(remoteDriverMode,
              REMOTE_DRIVER_MODE_LAST,
              "auto",
              "legacy",
              "direct");

#ifndef WIN32
static const char *
remoteGetDaemonPathEnv(void)
{
    /* We prefer a VIRTD_PATH env var to use for all daemons,
     * but if it is not set we will fallback to LIBVIRTD_PATH
     * for previous behaviour
     */
    if (getenv("VIRTD_PATH") != NULL) {
        return "VIRTD_PATH";
    } else {
        return "LIBVIRTD_PATH";
    }
}
#endif /* WIN32 */


int
remoteSplitURIScheme(virURI *uri,
                     char **driver,
                     remoteDriverTransport *transport)
{
    char *p = NULL;

    if (!uri->scheme) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("missing scheme for URI"));
        return -1;
    }

    p = strchr(uri->scheme, '+');

    if (p)
        *driver = g_strndup(uri->scheme, p - uri->scheme);
    else
        *driver = g_strdup(uri->scheme);

    if (p) {
        g_autofree char *tmp = g_strdup(p + 1);
        int val;

        p = tmp;
        while (*p) {
            *p = g_ascii_tolower(*p);
            p++;
        }

        if ((val = remoteDriverTransportTypeFromString(tmp)) < 0) {
            virReportError(VIR_ERR_INVALID_ARG, "%s",
                           _("remote_open: transport in URL not recognised "
                             "(should be tls|unix|ssh|ext|tcp|libssh2|libssh)"));
            return -1;
        }

        if (val == REMOTE_DRIVER_TRANSPORT_UNIX &&
            uri->server) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("using unix socket and remote "
                             "server '%s' is not supported."),
                           uri->server);
            return -1;
        }

        *transport = val;
    } else {
        if (uri->server)
            *transport = REMOTE_DRIVER_TRANSPORT_TLS;
        else
            *transport = REMOTE_DRIVER_TRANSPORT_UNIX;
    }

    return 0;
}


static char *
remoteGetUNIXSocketHelper(remoteDriverTransport transport,
                          const char *sock_prefix,
                          unsigned int flags)
{
    char *sockname = NULL;
    g_autofree char *userdir = NULL;

    if (flags & REMOTE_DRIVER_OPEN_USER) {
        userdir = virGetUserRuntimeDirectory();

        sockname = g_strdup_printf("%s/%s-sock", userdir, sock_prefix);
    } else {
        /* Intentionally do *NOT* use RUNSTATEDIR here. We might
         * be connecting to a remote machine, and cannot assume
         * the remote host has /run. The converse is ok though,
         * any machine with /run will have a /var/run symlink.
         * The portable option is to thus use $LOCALSTATEDIR/run
         */
        sockname = g_strdup_printf("%s/run/libvirt/%s-%s", LOCALSTATEDIR,
                                   sock_prefix,
                                   flags & REMOTE_DRIVER_OPEN_RO ?
                                   "sock-ro" : "sock");
    }

    VIR_DEBUG("Built UNIX sockname=%s for transport=%s "
              "prefix=%s flags=0x%x",
              sockname, remoteDriverTransportTypeToString(transport),
              sock_prefix, flags);
    return sockname;
}

/*
 * Determine which driver is probably usable based on
 * which modular daemon binaries are installed.
 */
int
remoteProbeSessionDriverFromBinary(char **driver)
{
    /* Order these the same as virDriverLoadModule
     * calls in daemonInitialize, so we replicate
     * probing order that virConnectOpen would use
     * if running inside libvirtd */
    const char *drivers[] = {
#ifdef WITH_QEMU
        "qemu",
#endif
#ifdef WITH_VBOX
        "vbox",
#endif
    };
    ssize_t i;

    VIR_DEBUG("Probing for driver from daemon binaries");

    *driver = NULL;

    for (i = 0; i < (ssize_t) G_N_ELEMENTS(drivers); i++) {
        g_autofree char *daemonname = NULL;
        g_autofree char *daemonpath = NULL;

        daemonname = g_strdup_printf("virt%sd", drivers[i]);
        VIR_DEBUG("Probing driver '%s' via daemon %s", drivers[i], daemonpath);

        if (!(daemonpath = virFileFindResource(daemonname,
                                               abs_top_builddir "/src",
                                               SBINDIR)))<commit_msg>remote: fix null string specifier argument in remoteProbeSessionDriverFromBinary<commit_after>/*
 * remote_sockets.c: helpers for getting remote driver socket paths
 *
 * Copyright (C) 2007-2019 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "remote_sockets.h"
#include "virerror.h"
#include "virlog.h"
#include "virfile.h"
#include "virutil.h"
#include "configmake.h"

#define VIR_FROM_THIS VIR_FROM_REMOTE

VIR_LOG_INIT("remote.remote_sockets");

VIR_ENUM_IMPL(remoteDriverTransport,
              REMOTE_DRIVER_TRANSPORT_LAST,
              "tls",
              "unix",
              "ssh",
              "libssh2",
              "ext",
              "tcp",
              "libssh");

VIR_ENUM_IMPL(remoteDriverMode,
              REMOTE_DRIVER_MODE_LAST,
              "auto",
              "legacy",
              "direct");

#ifndef WIN32
static const char *
remoteGetDaemonPathEnv(void)
{
    /* We prefer a VIRTD_PATH env var to use for all daemons,
     * but if it is not set we will fallback to LIBVIRTD_PATH
     * for previous behaviour
     */
    if (getenv("VIRTD_PATH") != NULL) {
        return "VIRTD_PATH";
    } else {
        return "LIBVIRTD_PATH";
    }
}
#endif /* WIN32 */


int
remoteSplitURIScheme(virURI *uri,
                     char **driver,
                     remoteDriverTransport *transport)
{
    char *p = NULL;

    if (!uri->scheme) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("missing scheme for URI"));
        return -1;
    }

    p = strchr(uri->scheme, '+');

    if (p)
        *driver = g_strndup(uri->scheme, p - uri->scheme);
    else
        *driver = g_strdup(uri->scheme);

    if (p) {
        g_autofree char *tmp = g_strdup(p + 1);
        int val;

        p = tmp;
        while (*p) {
            *p = g_ascii_tolower(*p);
            p++;
        }

        if ((val = remoteDriverTransportTypeFromString(tmp)) < 0) {
            virReportError(VIR_ERR_INVALID_ARG, "%s",
                           _("remote_open: transport in URL not recognised "
                             "(should be tls|unix|ssh|ext|tcp|libssh2|libssh)"));
            return -1;
        }

        if (val == REMOTE_DRIVER_TRANSPORT_UNIX &&
            uri->server) {
            virReportError(VIR_ERR_INVALID_ARG,
                           _("using unix socket and remote "
                             "server '%s' is not supported."),
                           uri->server);
            return -1;
        }

        *transport = val;
    } else {
        if (uri->server)
            *transport = REMOTE_DRIVER_TRANSPORT_TLS;
        else
            *transport = REMOTE_DRIVER_TRANSPORT_UNIX;
    }

    return 0;
}


static char *
remoteGetUNIXSocketHelper(remoteDriverTransport transport,
                          const char *sock_prefix,
                          unsigned int flags)
{
    char *sockname = NULL;
    g_autofree char *userdir = NULL;

    if (flags & REMOTE_DRIVER_OPEN_USER) {
        userdir = virGetUserRuntimeDirectory();

        sockname = g_strdup_printf("%s/%s-sock", userdir, sock_prefix);
    } else {
        /* Intentionally do *NOT* use RUNSTATEDIR here. We might
         * be connecting to a remote machine, and cannot assume
         * the remote host has /run. The converse is ok though,
         * any machine with /run will have a /var/run symlink.
         * The portable option is to thus use $LOCALSTATEDIR/run
         */
        sockname = g_strdup_printf("%s/run/libvirt/%s-%s", LOCALSTATEDIR,
                                   sock_prefix,
                                   flags & REMOTE_DRIVER_OPEN_RO ?
                                   "sock-ro" : "sock");
    }

    VIR_DEBUG("Built UNIX sockname=%s for transport=%s "
              "prefix=%s flags=0x%x",
              sockname, remoteDriverTransportTypeToString(transport),
              sock_prefix, flags);
    return sockname;
}

/*
 * Determine which driver is probably usable based on
 * which modular daemon binaries are installed.
 */
int
remoteProbeSessionDriverFromBinary(char **driver)
{
    /* Order these the same as virDriverLoadModule
     * calls in daemonInitialize, so we replicate
     * probing order that virConnectOpen would use
     * if running inside libvirtd */
    const char *drivers[] = {
#ifdef WITH_QEMU
        "qemu",
#endif
#ifdef WITH_VBOX
        "vbox",
#endif
    };
    ssize_t i;

    VIR_DEBUG("Probing for driver from daemon binaries");

    *driver = NULL;

    for (i = 0; i < (ssize_t) G_N_ELEMENTS(drivers); i++) {
        g_autofree char *daemonname = NULL;
        g_autofree char *daemonpath = NULL;

        daemonname = g_strdup_printf("virt%sd", drivers[i]);
        VIR_DEBUG("Probing driver '%s' via daemon %s", drivers[i], daemonname);

        if (!(daemonpath = virFileFindResource(daemonname,
                                               abs_top_builddir "/src",
                                               SBINDIR)))
{'fraction_non_alphanumeric': 0.0963887650468123, 'fraction_numerical': 0.004279982166740972, 'mean_word_length': 1.9671957671957672, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1977010', 'n_tokens_mistral': 3406, 'n_tokens_neox': 3259, 'n_words': 944}