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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
|
/* NBD client library in userspace
* Copyright Red Hat
*
* 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 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#ifdef HAVE_LINUX_VM_SOCKETS_H
#include <linux/vm_sockets.h>
#elif HAVE_SYS_VSOCK_H
#include <sys/vsock.h>
#endif
#include "array-size.h"
#include "vector.h"
#include "internal.h"
#ifdef HAVE_LIBXML2
#include <libxml/uri.h>
/* Connect to an NBD URI. */
int
nbd_unlocked_connect_uri (struct nbd_handle *h, const char *uri)
{
if (nbd_unlocked_aio_connect_uri (h, uri) == -1)
return -1;
return nbd_internal_wait_until_connected (h);
}
struct uri_query {
char *name;
char *value;
};
DEFINE_VECTOR_TYPE (uri_query_list, struct uri_query);
static void
free_uri_query (struct uri_query q)
{
free (q.name);
free (q.value);
}
/* Parse the query_raw substring of a URI into a list of decoded queries.
* Return 0 on success or -1 on error.
*/
static int
parse_uri_queries (const char *query_raw, uri_query_list *list)
{
/* Borrows from libvirt's viruri.c:virURIParseParams() */
const char *end, *eq;
const char *query = query_raw;
size_t i;
if (!query || !*query)
return 0;
while (*query) {
struct uri_query q = {0};
/* Find the next separator, or end of the string. */
end = strchr (query, '&');
if (!end)
end = strchr (query, ';');
if (!end)
end = query + strlen (query);
/* Find the first '=' character between here and end. */
eq = strchr (query, '=');
if (eq && eq >= end) eq = NULL;
if (end == query) {
/* Empty section (eg. "&&"). */
goto next;
} else if (!eq) {
/* If there is no '=' character, then we have just "name"
* and consistent with CGI.pm we assume value is "".
*/
q.name = xmlURIUnescapeString (query, end - query, NULL);
if (!q.name) goto error;
} else if (eq+1 == end) {
/* Or if we have "name=" here (works around annoying
* problem when calling xmlURIUnescapeString with len = 0).
*/
q.name = xmlURIUnescapeString (query, eq - query, NULL);
if (!q.name) goto error;
} else if (query == eq) {
/* If the '=' character is at the beginning then we have
* "=value" and consistent with CGI.pm we _ignore_ this.
*/
goto next;
} else {
/* Otherwise it's "name=value". */
q.name = xmlURIUnescapeString (query, eq - query, NULL);
if (!q.name)
goto error;
q.value = xmlURIUnescapeString (eq+1, end - (eq+1), NULL);
if (!q.value) {
free (q.name);
goto error;
}
}
if (!q.value) {
q.value = strdup ("");
if (!q.value) {
free (q.name);
goto error;
}
}
/* Append to the list of queries. */
if (uri_query_list_append (list, q) == -1) {
free (q.name);
free (q.value);
goto error;
}
next:
query = end;
if (*query) query++; /* skip '&' separator */
}
return 0;
error:
for (i = 0; i < list->len; ++i) {
free (list->ptr[i].name);
free (list->ptr[i].value);
}
uri_query_list_reset (list);
return -1;
}
/* Similar to nbdkit_parse_bool */
static int
parse_bool (const char *param, const char *value)
{
if (!strcmp (value, "1") ||
!strcasecmp (value, "true") ||
!strcasecmp (value, "t") ||
!strcasecmp (value, "yes") ||
!strcasecmp (value, "y") ||
!strcasecmp (value, "on"))
return 1;
if (!strcmp (value, "0") ||
!strcasecmp (value, "false") ||
!strcasecmp (value, "f") ||
!strcasecmp (value, "no") ||
!strcasecmp (value, "n") ||
!strcasecmp (value, "off"))
return 0;
set_error (EINVAL, "could not parse %s parameter, expecting %s=true|false",
param, param);
return -1;
}
int
nbd_unlocked_aio_connect_uri (struct nbd_handle *h, const char *raw_uri)
{
xmlURIPtr uri = NULL;
enum { tcp, unix_sock, vsock, ssh } transport;
bool tls;
enum { none, optional, required } socket_param;
uri_query_list queries = empty_vector;
int i, r;
int ret = -1;
const char *unixsocket = NULL;
uri = xmlParseURI (raw_uri);
if (!uri) {
set_error (EINVAL, "unable to parse URI: %s", raw_uri);
goto cleanup;
}
if (parse_uri_queries (uri->query_raw, &queries) == -1) {
set_error (EINVAL, "unable to parse URI queries: %s", uri->query_raw);
goto cleanup;
}
/* Scheme. */
if (uri->scheme) {
if (strcasecmp (uri->scheme, "nbd") == 0) {
transport = tcp;
tls = false;
socket_param = none;
}
else if (strcasecmp (uri->scheme, "nbds") == 0) {
transport = tcp;
tls = true;
socket_param = none;
}
else if (strcasecmp (uri->scheme, "nbd+unix") == 0) {
transport = unix_sock;
tls = false;
socket_param = required;
}
else if (strcasecmp (uri->scheme, "nbds+unix") == 0) {
transport = unix_sock;
tls = true;
socket_param = required;
}
else if (strcasecmp (uri->scheme, "nbd+vsock") == 0) {
transport = vsock;
tls = false;
socket_param = none;
}
else if (strcasecmp (uri->scheme, "nbds+vsock") == 0) {
transport = vsock;
tls = true;
socket_param = none;
}
else if (strcasecmp (uri->scheme, "nbd+ssh") == 0) {
transport = ssh;
tls = false;
socket_param = optional;
}
else if (strcasecmp (uri->scheme, "nbds+ssh") == 0) {
transport = ssh;
tls = true;
socket_param = optional;
}
else {
set_error (EINVAL, "unknown NBD URI scheme: %s", uri->scheme);
goto cleanup;
}
}
else {
const char *explanation = NULL;
if (raw_uri[0] == '/' &&
(h->uri_allow_transports & LIBNBD_ALLOW_TRANSPORT_UNIX) != 0)
explanation = "to open a local socket use \"nbd+unix://?socket=PATH\"";
else if (strncasecmp (raw_uri, "localhost", 9) == 0 &&
(h->uri_allow_transports & LIBNBD_ALLOW_TRANSPORT_TCP) != 0)
explanation =
"to open a local port use \"nbd://localhost\" or "
"\"nbd://localhost:PORT\"";
set_error (EINVAL,
"NBD URI does not have a scheme: valid NBD URIs should "
"start with a scheme like nbd://, nbds:// or nbd+unix://"
"%s%s",
explanation ? ": " : "",
explanation ? explanation : "");
goto cleanup;
}
/* Insist on the scheme://[authority][/absname][?queries] form. */
if (strncmp (raw_uri + strlen (uri->scheme), "://", 3)) {
set_error (EINVAL, "URI must begin with '%s://'", uri->scheme);
goto cleanup;
}
/* Check the transport is allowed. */
if ((transport == tcp &&
(h->uri_allow_transports & LIBNBD_ALLOW_TRANSPORT_TCP) == 0) ||
(transport == unix_sock &&
(h->uri_allow_transports & LIBNBD_ALLOW_TRANSPORT_UNIX) == 0) ||
(transport == vsock &&
(h->uri_allow_transports & LIBNBD_ALLOW_TRANSPORT_VSOCK) == 0) ||
(transport == ssh &&
(h->uri_allow_transports & LIBNBD_ALLOW_TRANSPORT_SSH) == 0)) {
set_error (EPERM, "URI transport %s is not permitted", uri->scheme);
goto cleanup;
}
/* Check TLS is allowed. */
if ((tls && h->uri_allow_tls == LIBNBD_TLS_DISABLE) ||
(!tls && h->uri_allow_tls == LIBNBD_TLS_REQUIRE)) {
set_error (EPERM, "URI TLS setting %s is not permitted", uri->scheme);
goto cleanup;
}
/* Parse the socket parameter. */
for (i = 0; i < queries.len; i++) {
if (strcasecmp (queries.ptr[i].name, "socket") == 0)
unixsocket = queries.ptr[i].value;
}
switch (socket_param) {
case required:
if (!unixsocket) {
set_error (EINVAL, "cannot parse socket parameter from NBD URI "
"(did you mean to use \"%s:///?socket=...\"?)",
uri->scheme);
goto cleanup;
}
break;
case none:
if (unixsocket) {
set_error (EINVAL, "socket parameter is incompatible with \"%s:\" "
"(did you mean to use \"%s+unix:///?socket=...\"?)",
uri->scheme, !tls ? "nbd" : "nbds");
goto cleanup;
}
break;
case optional:
/* no check needed */ ;
}
/* TLS */
if (tls && nbd_unlocked_set_tls (h, LIBNBD_TLS_REQUIRE) == -1)
goto cleanup;
/* Look for some tls-* parameters. */
for (i = 0; i < queries.len; i++) {
if (strcasecmp (queries.ptr[i].name, "tls-certificates") == 0) {
if (! h->uri_allow_local_file) {
set_error (EPERM,
"local file access (tls-certificates) is not allowed, "
"call nbd_set_uri_allow_local_file to enable this");
goto cleanup;
}
if (nbd_unlocked_set_tls_certificates (h, queries.ptr[i].value) == -1)
goto cleanup;
}
else if (strcasecmp (queries.ptr[i].name, "tls-psk-file") == 0) {
if (! h->uri_allow_local_file) {
set_error (EPERM,
"local file access (tls-psk-file) is not allowed, "
"call nbd_set_uri_allow_local_file to enable this");
goto cleanup;
}
if (nbd_unlocked_set_tls_psk_file (h, queries.ptr[i].value) == -1)
goto cleanup;
}
else if (strcasecmp (queries.ptr[i].name, "tls-hostname") == 0) {
if (nbd_unlocked_set_tls_hostname (h, queries.ptr[i].value) == -1)
goto cleanup;
}
else if (strcasecmp (queries.ptr[i].name, "tls-verify-peer") == 0) {
int v = parse_bool ("tls-verify-peer", queries.ptr[i].value);
if (v == -1)
goto cleanup;
if (nbd_unlocked_set_tls_verify_peer (h, v) == -1)
goto cleanup;
}
}
/* Username. */
if (uri->user && nbd_unlocked_set_tls_username (h, uri->user) == -1)
goto cleanup;
/* Export name. */
if (uri->path) {
/* Since we require scheme://authority above, any path is absolute */
assert (uri->path[0] == '/');
r = nbd_unlocked_set_export_name (h, &uri->path[1]);
}
else
r = nbd_unlocked_set_export_name (h, "");
if (r == -1)
goto cleanup;
switch (transport) {
case tcp: { /* TCP */
char port_str[32];
char *server;
size_t server_len;
snprintf (port_str, sizeof port_str,
"%d", uri->port > 0 ? uri->port : 10809);
/* If the uri->server field is NULL, substitute "localhost". This
* would be unusual and probably doesn't happen in reality. XXX
*/
server = uri->server ? : "localhost";
server_len = strlen (server);
/* For a literal IPv6 address, the uri->server field will contain
* "[addr]" and we must remove the brackets before passing it to
* getaddrinfo in the state machine.
*/
if (server_len >= 2 && server[0] == '[' && server[server_len-1] == ']') {
server_len -= 2;
server++;
server[server_len] = '\0';
}
if (nbd_unlocked_aio_connect_tcp (h, server, port_str) == -1)
goto cleanup;
break;
}
case unix_sock: /* Unix domain socket */
if (nbd_unlocked_aio_connect_unix (h, unixsocket) == -1)
goto cleanup;
break;
case vsock: { /* AF_VSOCK */
uint32_t cid, svm_port;
/* Server, if present, must be the numeric CID. Else we
* assume the host (2).
*/
if (uri->server && strcmp (uri->server, "") != 0) {
/* This doesn't deal with overflow, but that seems unlikely to
* matter because you'll end up with a CID one way or another.
*/
if (sscanf (uri->server, "%" SCNu32, &cid) != 1) {
set_error (EINVAL, "cannot parse vsock CID from NBD URI: %s",
uri->server);
goto cleanup;
}
}
else
cid = 2;
/* For unknown reasons libxml2 sets uri->port = -1 if the
* authority field is not present at all. So we must check that
* uri->port > 0. This prevents us from using certain very large
* port numbers, but that's not an issue that matters in practice.
*/
svm_port = uri->port > 0 ? (uint32_t)uri->port : 10809;
if (nbd_unlocked_aio_connect_vsock (h, cid, svm_port) == -1)
goto cleanup;
break;
}
case ssh: { /* SSH */
char port_str[32];
const char *ssh_command[] = {
"ssh", "-p", port_str, "--", uri->server,
"nc",
NULL, /* [6] "-U" or "localhost" */
NULL, /* [7] socket or "10809" */
NULL,
};
if (!uri->server || strcmp (uri->server, "") == 0) {
set_error (EINVAL, "SSH transport requires a server name");
goto cleanup;
}
snprintf (port_str, sizeof port_str,
"%d", uri->port > 0 ? uri->port : 22);
if (unixsocket) {
ssh_command[6] = "-U";
ssh_command[7] = unixsocket;
}
else {
ssh_command[6] = "localhost";
ssh_command[7] = "10809"; /* XXX provide a way to configure this */
}
if (nbd_unlocked_aio_connect_command (h, (char **) ssh_command) == -1)
goto cleanup;
}
}
ret = 0;
cleanup:
uri_query_list_iter (&queries, free_uri_query);
uri_query_list_reset (&queries);
xmlFreeURI (uri);
return ret;
}
/* This is best effort. If we didn't save enough information when
* connecting then return NULL but try to set errno and the error
* string to something useful.
*/
static int append_query_params (char **query_params,
const char *key, const char *value)
LIBNBD_ATTRIBUTE_NONNULL (1, 2, 3);
char *
nbd_unlocked_get_uri (struct nbd_handle *h)
{
xmlURI uri = { 0 };
bool using_tls;
char *server = NULL;
char *query_params = NULL;
char *path = NULL;
char *ret = NULL;
if (h->tls == 2) /* TLS == require */
using_tls = true;
else if (h->tls_negotiated)
using_tls = true;
else
using_tls = false;
/* First set uri.scheme and uri.server. */
/* We have a defined hostname and/or port number (nbd_connect_tcp). */
if (h->hostname && h->port) {
int r;
uri.scheme = using_tls ? "nbds" : "nbd";
/* We must try to guess here if the hostname is really an IPv6
* numeric address. Regular hostnames or IPv4 addresses wouldn't
* contain ':'.
*/
if (strchr (h->hostname, ':') == NULL)
r = asprintf (&server, "%s:%s", h->hostname, h->port);
else
r = asprintf (&server, "[%s]:%s", h->hostname, h->port);
if (r == -1) {
set_error (errno, "asprintf");
goto out;
}
uri.server = server;
}
/* We have a sockaddr (nbd_connect, nbd_connect_unix, nbd_connect_vsock). */
else if (h->connaddrlen > 0) {
switch (h->connaddr.ss_family) {
case AF_INET:
case AF_INET6: {
int r, err;
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
uri.scheme = using_tls ? "nbds" : "nbd";
err = getnameinfo ((struct sockaddr *)&h->connaddr, h->connaddrlen,
host, sizeof host, serv, sizeof serv,
NI_NUMERICHOST | NI_NUMERICSERV);
if (err != 0) {
set_error (0, "getnameinfo: %s", gai_strerror (err));
goto out;
}
if (h->connaddr.ss_family == AF_INET)
r = asprintf (&server, "%s:%s", host, serv);
else /* AF_INET6 */
r = asprintf (&server, "[%s]:%s", host, serv);
if (r == -1) {
set_error (errno, "asprintf");
goto out;
}
uri.server = server;
break;
}
case AF_UNIX: {
struct sockaddr_un *sun = (struct sockaddr_un *)&h->connaddr;
if (sun->sun_path[0] == '\0') {
/* Unix domain sockets in the abstract namespace are in theory
* supported in NBD URIs, but libxml2 cannot handle them so
* libnbd cannot use them here or in nbd_connect_uri.
*/
set_error (EPROTONOSUPPORT, "Unix domain sockets in the "
"abstract namespace are not yet supported");
goto out;
}
uri.scheme = using_tls ? "nbds+unix" : "nbd+unix";
if (append_query_params (&query_params, "socket", sun->sun_path) == -1)
goto out;
/* You have to set this otherwise xmlSaveUri generates bogus
* URIs "nbd+unix:/?socket=..."
*/
uri.server = "";
break;
}
#if HAVE_STRUCT_SOCKADDR_VM
case AF_VSOCK: {
struct sockaddr_vm *svm = (struct sockaddr_vm *)&h->connaddr;
uri.scheme = using_tls ? "nbds+vsock" : "nbd+vsock";
if (asprintf (&server, "%u:%u", svm->svm_cid, svm->svm_port) == -1) {
set_error (errno, "asprintf");
goto out;
}
uri.server = server;
break;
}
#endif
default:
set_error (EAFNOSUPPORT,
"address family %d not supported", h->connaddr.ss_family);
goto out;
}
}
/* Not enough information (nbd_connect_socket). */
else {
set_error (EINVAL, "cannot construct a URI for this connection type");
goto out;
}
/* Set other uri.* fields. */
if (h->tls_username)
uri.user = h->tls_username;
if (h->export_name) {
if (asprintf (&path, "/%s", h->export_name) == -1) {
set_error (errno, "asprintf");
goto out;
}
uri.path = path;
}
if (h->tls_certificates) {
if (append_query_params (&query_params,
"tls-certificates", h->tls_certificates) == -1)
goto out;
}
if (h->tls_psk_file) {
if (append_query_params (&query_params,
"tls-psk-file", h->tls_psk_file) == -1)
goto out;
}
if (h->tls_hostname) {
if (append_query_params (&query_params,
"tls-hostname", h->tls_hostname) == -1)
goto out;
}
if (! h->tls_verify_peer) {
if (append_query_params (&query_params,
"tls-verify-peer", "false") == -1)
goto out;
}
uri.query_raw = query_params;
/* Construct the final URI and return it. */
ret = (char *)xmlSaveUri (&uri);
if (ret == NULL)
set_error (errno, "xmlSaveUri failed");
out:
free (server);
free (query_params);
free (path);
return ret;
}
static int
append_query_params (char **query_params, const char *key, const char *value)
{
char *old_query_params = *query_params;
if (asprintf (query_params, "%s%s%s=%s",
old_query_params ? : "",
old_query_params ? "&" : "",
key, value) == -1) {
set_error (errno, "asprintf");
return -1;
}
free (old_query_params);
return 0;
}
#else /* !HAVE_LIBXML2 */
#define NOT_SUPPORTED_ERROR \
"libnbd was compiled without libxml2 support, so we do not support NBD URI"
int
nbd_unlocked_connect_uri (struct nbd_handle *h, const char *uri)
{
set_error (ENOTSUP, NOT_SUPPORTED_ERROR);
return -1;
}
int
nbd_unlocked_aio_connect_uri (struct nbd_handle *h, const char *raw_uri)
{
set_error (ENOTSUP, NOT_SUPPORTED_ERROR);
return -1;
}
char *
nbd_unlocked_get_uri (struct nbd_handle *h)
{
set_error (ENOTSUP, NOT_SUPPORTED_ERROR);
return NULL;
}
#endif /* !HAVE_LIBXML2 */
/* nbd_is_uri is simple enough that it works even if we don't have URI
* support.
*/
int
nbd_unlocked_is_uri (struct nbd_handle *h, const char *uri)
{
static const char *prefixes[] = {
"nbd://",
"nbds://",
"nbd+unix://",
"nbds+unix://",
"nbd+vsock://",
"nbds+vsock://",
"nbd+ssh://",
"nbds+ssh://",
};
size_t i;
for (i = 0; i < ARRAY_SIZE (prefixes); ++i) {
if (strncasecmp (uri, prefixes[i], strlen (prefixes[i])) == 0)
return 1;
}
return 0;
}
|