1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* For given domain and port create a VSOCK socket and pass it onto STDOUT.
*/
#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/vm_sockets.h>
#include "internal.h"
#include "virsocket.h"
#include "virstring.h"
#include "virfile.h"
#include "datatypes.h"
#include "virgettext.h"
#include "virxml.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#define SYS_ERROR(...) \
do { \
int err = errno; \
fprintf(stderr, "ERROR %s:%d : ", __FUNCTION__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, " : %s\n", g_strerror(err)); \
fprintf(stderr, "\n"); \
} while (0)
#define ERROR(...) \
do { \
fprintf(stderr, "ERROR %s:%d : ", __FUNCTION__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)
#define HOSTNAME_PREFIX "qemu"
#define QEMU_SYSTEM_URI "qemu:///system"
#define QEMU_SESSION_URI "qemu:///session"
static void
dummyErrorHandler(void *opaque G_GNUC_UNUSED,
virErrorPtr error G_GNUC_UNUSED)
{
}
static void
printUsage(const char *argv0)
{
const char *progname;
if (!(progname = strrchr(argv0, '/')))
progname = argv0;
else
progname++;
printf(_("\n"
"Usage:\n"
"%1$s hostname port\n"
"\n"
"Hostname should be in one of the following forms:\n"
"\n"
" qemu:system/$domname\t\tfor domains under qemu:///system\n"
" qemu:session/$domname\t\tfor domains under qemu:///session\n"
" qemu/$domname\t\t\ttries looking up $domname under system followed by session URI\n"),
progname);
}
static int
parseArgs(int argc,
char *argv[],
const char **uriRet,
const char **domname,
unsigned int *port)
{
const char *uri = NULL;
/* Accepted URIs are:
*
* qemu/virtualMachine
* qemu:system/virtualMachine
* qemu:session/virtualMachine
*
* The last two result in system or session connection URIs passed to
* virConnectOpen(), the first one tries to find the machine under system
* connection first, followed by session connection.
*/
if (argc != 3 ||
!(uri = STRSKIP(argv[1], HOSTNAME_PREFIX))) {
ERROR(_("Bad usage"));
printUsage(argv[0]);
return -1;
}
if (*uri == ':') {
const char *tmp = NULL;
uri++;
if ((tmp = STRSKIP(uri, "system"))) {
*uriRet = QEMU_SYSTEM_URI;
} else if ((tmp = STRSKIP(uri, "session"))) {
*uriRet = QEMU_SESSION_URI;
} else {
ERROR(_("Unknown connection URI: '%1$s'"), uri);
printUsage(argv[0]);
return -1;
}
uri = tmp;
} else {
*uriRet = NULL;
}
if (!(*domname = STRSKIP(uri, "/")) ||
**domname == '\0') {
ERROR(_("Bad usage"));
printUsage(argv[0]);
return -1;
}
if (virStrToLong_ui(argv[2], NULL, 10, port) < 0) {
ERROR(_("Unable to parse port: %1$s"), argv[2]);
printUsage(argv[0]);
return -1;
}
return 0;
}
#define VSOCK_CID_XPATH "/domain/devices/vsock/cid"
static int
extractCID(virDomainPtr dom,
unsigned long long *cidRet)
{
g_autofree char *domxml = NULL;
g_autoptr(xmlDoc) doc = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL;
g_autofree xmlNodePtr *nodes = NULL;
int nnodes = 0;
size_t i;
if (!(domxml = virDomainGetXMLDesc(dom, 0)))
return -1;
doc = virXMLParseStringCtxtWithIndent(domxml, "domain", &ctxt);
if (!doc)
return -1;
if ((nnodes = virXPathNodeSet(VSOCK_CID_XPATH, ctxt, &nodes)) < 0) {
return -1;
}
for (i = 0; i < nnodes; i++) {
unsigned long long cid;
if (virXMLPropULongLong(nodes[i], "address", 10, 0, &cid) > 0) {
*cidRet = cid;
return 0;
}
}
return -1;
}
#undef VSOCK_CID_XPATH
static int
lookupDomainAndFetchCID(const char *uri,
const char *domname,
unsigned long long *cid)
{
g_autoptr(virConnect) conn = NULL;
g_autoptr(virDomain) dom = NULL;
if (!(conn = virConnectOpenReadOnly(uri)))
return -1;
dom = virDomainLookupByName(conn, domname);
if (!dom)
dom = virDomainLookupByUUIDString(conn, domname);
if (!dom) {
int id;
if (virStrToLong_i(domname, NULL, 10, &id) >= 0)
dom = virDomainLookupByID(conn, id);
}
/* If no domain is found, return an error. Similarly, inactive domain may
* contain CID of another (running) domain, yielding misleading results. */
if (!dom || virDomainIsActive(dom) <= 0)
return -1;
return extractCID(dom, cid);
}
static int
findDomain(const char *domname,
unsigned long long *cid)
{
const char *uris[] = {QEMU_SYSTEM_URI, QEMU_SESSION_URI};
const uid_t userid = geteuid();
size_t i;
for (i = 0; i < G_N_ELEMENTS(uris); i++) {
if (userid == 0 &&
STREQ(uris[i], "qemu:///session")) {
continue;
}
if (lookupDomainAndFetchCID(uris[i], domname, cid) >= 0)
return 0;
}
return -1;
}
static int
processVsock(const char *uri,
const char *domname,
unsigned int port)
{
struct sockaddr_vm sa = {
.svm_family = AF_VSOCK,
.svm_port = port,
};
VIR_AUTOCLOSE fd = -1;
unsigned long long cid = -1;
if (uri) {
lookupDomainAndFetchCID(uri, domname, &cid);
} else {
findDomain(domname, &cid);
}
if (cid == -1) {
ERROR(_("No usable vsock found"));
return -1;
}
sa.svm_cid = cid;
fd = socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd < 0) {
SYS_ERROR(_("Failed to allocate AF_VSOCK socket"));
return -1;
}
if (connect(fd, (const struct sockaddr *)&sa, sizeof(sa)) < 0) {
SYS_ERROR(_("Failed to connect to vsock (cid=%1$llu port=%2$u)"),
cid, port);
return -1;
}
/* OpenSSH wants us to send a single byte along with the file descriptor,
* hence do so. */
if (virSocketSendFD(STDOUT_FILENO, fd) < 0) {
SYS_ERROR(_("Failed to send file descriptor %1$d"), fd);
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
const char *uri = NULL;
const char *domname = NULL;
unsigned int port;
if (virGettextInitialize() < 0)
return EXIT_FAILURE;
if (virInitialize() < 0) {
ERROR(_("Failed to initialize libvirt"));
return EXIT_FAILURE;
}
virSetErrorFunc(NULL, dummyErrorHandler);
if (parseArgs(argc, argv, &uri, &domname, &port) < 0)
return EXIT_FAILURE;
if (processVsock(uri, domname, port) < 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
|