2020-03-04 17:39:48 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2020-07-08 17:02:50 +00:00
|
|
|
#include "virerror.h"
|
2020-03-04 17:45:55 +00:00
|
|
|
#include "virlog.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virutil.h"
|
|
|
|
#include "configmake.h"
|
2020-07-08 17:02:50 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_REMOTE
|
2020-03-04 17:39:48 +00:00
|
|
|
|
2020-03-04 17:45:55 +00:00
|
|
|
VIR_LOG_INIT("remote.remote_sockets");
|
|
|
|
|
2020-03-04 17:39:48 +00:00
|
|
|
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");
|
2020-03-04 17:45:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
remoteSplitURIScheme(virURIPtr uri,
|
|
|
|
char **driver,
|
2020-07-08 17:02:50 +00:00
|
|
|
remoteDriverTransport *transport)
|
2020-03-04 17:45:55 +00:00
|
|
|
{
|
|
|
|
char *p = strchr(uri->scheme, '+');
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
*driver = g_strndup(uri->scheme, p - uri->scheme);
|
|
|
|
else
|
|
|
|
*driver = g_strdup(uri->scheme);
|
|
|
|
|
|
|
|
if (p) {
|
2020-07-08 17:02:50 +00:00
|
|
|
g_autofree char *tmp = g_strdup(p + 1);
|
|
|
|
int val;
|
2020-03-04 17:45:55 +00:00
|
|
|
|
2020-07-08 17:02:50 +00:00
|
|
|
p = tmp;
|
2020-03-04 17:45:55 +00:00
|
|
|
while (*p) {
|
|
|
|
*p = g_ascii_tolower(*p);
|
|
|
|
p++;
|
|
|
|
}
|
2020-07-08 17:02:50 +00:00
|
|
|
|
|
|
|
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;
|
2020-03-04 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-04 17:45:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
remoteGetUNIXSocketHelper(remoteDriverTransport transport,
|
|
|
|
const char *sock_prefix,
|
|
|
|
bool ro,
|
|
|
|
bool session)
|
|
|
|
{
|
|
|
|
char *sockname = NULL;
|
|
|
|
g_autofree char *userdir = NULL;
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
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,
|
|
|
|
ro ? "sock-ro" : "sock");
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Built UNIX sockname=%s for transport=%s "
|
|
|
|
"prefix=%s ro=%d session=%d",
|
|
|
|
sockname, remoteDriverTransportTypeToString(transport),
|
|
|
|
sock_prefix, ro, session);
|
|
|
|
return sockname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
remoteGetUNIXSocket(remoteDriverTransport transport,
|
|
|
|
remoteDriverMode mode,
|
|
|
|
const char *driver,
|
|
|
|
bool ro,
|
|
|
|
bool session,
|
|
|
|
char **daemon)
|
|
|
|
{
|
|
|
|
char *sock_name = NULL;
|
|
|
|
g_autofree char *direct_daemon = NULL;
|
|
|
|
g_autofree char *legacy_daemon = NULL;
|
|
|
|
g_autofree char *direct_sock_name = NULL;
|
|
|
|
g_autofree char *legacy_sock_name = NULL;
|
|
|
|
|
2020-09-16 13:51:47 +00:00
|
|
|
VIR_DEBUG("Choosing remote socket for transport=%s mode=%s driver=%s ro=%d session=%d",
|
|
|
|
remoteDriverTransportTypeToString(transport),
|
|
|
|
remoteDriverModeTypeToString(mode),
|
|
|
|
driver, ro, session);
|
|
|
|
|
2020-03-04 17:45:55 +00:00
|
|
|
if (driver)
|
|
|
|
direct_daemon = g_strdup_printf("virt%sd", driver);
|
|
|
|
|
|
|
|
legacy_daemon = g_strdup("libvirtd");
|
|
|
|
|
|
|
|
if (driver &&
|
|
|
|
!(direct_sock_name = remoteGetUNIXSocketHelper(transport, direct_daemon, ro, session)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(legacy_sock_name = remoteGetUNIXSocketHelper(transport, "libvirt", ro, session)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (mode == REMOTE_DRIVER_MODE_AUTO) {
|
|
|
|
if (transport == REMOTE_DRIVER_TRANSPORT_UNIX) {
|
|
|
|
if (direct_sock_name && virFileExists(direct_sock_name)) {
|
|
|
|
mode = REMOTE_DRIVER_MODE_DIRECT;
|
|
|
|
} else if (virFileExists(legacy_sock_name)) {
|
|
|
|
mode = REMOTE_DRIVER_MODE_LEGACY;
|
|
|
|
} else if (driver) {
|
|
|
|
/*
|
|
|
|
* This constant comes from the configure script and
|
|
|
|
* maps to either the direct or legacy mode constant
|
|
|
|
*/
|
|
|
|
mode = REMOTE_DRIVER_MODE_DEFAULT;
|
|
|
|
} else {
|
|
|
|
mode = REMOTE_DRIVER_MODE_LEGACY;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mode = REMOTE_DRIVER_MODE_LEGACY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ((remoteDriverMode)mode) {
|
|
|
|
case REMOTE_DRIVER_MODE_LEGACY:
|
|
|
|
sock_name = g_steal_pointer(&legacy_sock_name);
|
|
|
|
*daemon = g_steal_pointer(&legacy_daemon);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REMOTE_DRIVER_MODE_DIRECT:
|
|
|
|
if (transport != REMOTE_DRIVER_TRANSPORT_UNIX) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
|
|
|
|
_("Cannot use direct socket mode for %s transport"),
|
|
|
|
remoteDriverTransportTypeToString(transport));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!direct_sock_name) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
|
|
|
|
_("Cannot use direct socket mode if no URI is set"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_name = g_steal_pointer(&direct_sock_name);
|
|
|
|
*daemon = g_steal_pointer(&direct_daemon);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REMOTE_DRIVER_MODE_AUTO:
|
|
|
|
case REMOTE_DRIVER_MODE_LAST:
|
|
|
|
default:
|
|
|
|
virReportEnumRangeError(remoteDriverMode, mode);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-16 13:51:47 +00:00
|
|
|
VIR_DEBUG("Chosen UNIX sockname=%s daemon=%s with mode=%s",
|
2020-03-04 17:45:55 +00:00
|
|
|
sock_name, NULLSTR(*daemon),
|
2020-09-16 13:51:47 +00:00
|
|
|
remoteDriverModeTypeToString(mode));
|
2020-03-04 17:45:55 +00:00
|
|
|
return sock_name;
|
|
|
|
}
|
2019-07-08 14:30:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
remoteGetURIDaemonInfo(virURIPtr uri,
|
|
|
|
remoteDriverTransport transport,
|
|
|
|
bool *session,
|
|
|
|
bool *autostart)
|
|
|
|
{
|
|
|
|
const char *autostart_str = getenv("LIBVIRT_AUTOSTART");
|
|
|
|
|
|
|
|
*session = false;
|
|
|
|
*autostart = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User session daemon is used for
|
|
|
|
*
|
|
|
|
* - Any URI with /session suffix
|
|
|
|
* - Test driver, if a protocol is given
|
|
|
|
*
|
|
|
|
* provided we are running non-root
|
|
|
|
*/
|
|
|
|
if (uri &&
|
|
|
|
uri->path &&
|
|
|
|
uri->scheme &&
|
|
|
|
(STREQ(uri->path, "/session") ||
|
|
|
|
STRPREFIX(uri->scheme, "test+")) &&
|
|
|
|
geteuid() > 0) {
|
|
|
|
VIR_DEBUG("User session daemon required");
|
|
|
|
*session = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Furthermore if no servername is given,
|
|
|
|
* and the transport is unix,
|
|
|
|
* and uid is unprivileged then auto-spawn a daemon.
|
|
|
|
*/
|
|
|
|
if (!uri->server &&
|
|
|
|
(transport == REMOTE_DRIVER_TRANSPORT_UNIX) &&
|
|
|
|
(!autostart_str ||
|
|
|
|
STRNEQ(autostart_str, "0"))) {
|
|
|
|
VIR_DEBUG("Try daemon autostart");
|
|
|
|
*autostart = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If URI is NULL, then do a UNIX connection possibly auto-spawning
|
|
|
|
* unprivileged server and probe remote server for URI.
|
|
|
|
*/
|
|
|
|
if (!uri) {
|
|
|
|
VIR_DEBUG("Auto-probe remote URI");
|
|
|
|
if (geteuid() > 0) {
|
|
|
|
VIR_DEBUG("Auto-spawn user daemon instance");
|
|
|
|
*session = true;
|
|
|
|
if (!autostart_str ||
|
|
|
|
STRNEQ(autostart_str, "0"))
|
|
|
|
*autostart = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|