2013-04-09 22:30:53 +08:00
|
|
|
/*
|
|
|
|
* Secure Shell (ssh) backend for QEMU.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Red Hat Inc., Richard W.M. Jones <rjones@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-19 02:01:42 +08:00
|
|
|
#include "qemu/osdep.h"
|
2013-04-09 22:30:53 +08:00
|
|
|
|
|
|
|
#include <libssh2.h>
|
|
|
|
#include <libssh2_sftp.h>
|
|
|
|
|
|
|
|
#include "block/block_int.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2015-03-18 01:29:20 +08:00
|
|
|
#include "qemu/error-report.h"
|
2016-10-25 21:03:59 +08:00
|
|
|
#include "qemu/cutils.h"
|
2013-04-09 22:30:53 +08:00
|
|
|
#include "qemu/sockets.h"
|
|
|
|
#include "qemu/uri.h"
|
2016-10-25 21:03:59 +08:00
|
|
|
#include "qapi-visit.h"
|
2013-04-09 22:30:53 +08:00
|
|
|
#include "qapi/qmp/qint.h"
|
2015-03-18 01:29:20 +08:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2016-10-25 21:03:59 +08:00
|
|
|
#include "qapi/qobject-input-visitor.h"
|
|
|
|
#include "qapi/qobject-output-visitor.h"
|
2013-04-09 22:30:53 +08:00
|
|
|
|
|
|
|
/* DEBUG_SSH=1 enables the DPRINTF (debugging printf) statements in
|
|
|
|
* this block driver code.
|
|
|
|
*
|
|
|
|
* TRACE_LIBSSH2=<bitmask> enables tracing in libssh2 itself. Note
|
|
|
|
* that this requires that libssh2 was specially compiled with the
|
|
|
|
* `./configure --enable-debug' option, so most likely you will have
|
|
|
|
* to compile it yourself. The meaning of <bitmask> is described
|
|
|
|
* here: http://www.libssh2.org/libssh2_trace.html
|
|
|
|
*/
|
|
|
|
#define DEBUG_SSH 0
|
|
|
|
#define TRACE_LIBSSH2 0 /* or try: LIBSSH2_TRACE_SFTP */
|
|
|
|
|
|
|
|
#define DPRINTF(fmt, ...) \
|
|
|
|
do { \
|
|
|
|
if (DEBUG_SSH) { \
|
|
|
|
fprintf(stderr, "ssh: %-15s " fmt "\n", \
|
|
|
|
__func__, ##__VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
typedef struct BDRVSSHState {
|
|
|
|
/* Coroutine. */
|
|
|
|
CoMutex lock;
|
|
|
|
|
|
|
|
/* SSH connection. */
|
|
|
|
int sock; /* socket */
|
|
|
|
LIBSSH2_SESSION *session; /* ssh session */
|
|
|
|
LIBSSH2_SFTP *sftp; /* sftp session */
|
|
|
|
LIBSSH2_SFTP_HANDLE *sftp_handle; /* sftp remote file handle */
|
|
|
|
|
|
|
|
/* See ssh_seek() function below. */
|
|
|
|
int64_t offset;
|
|
|
|
bool offset_op_read;
|
|
|
|
|
|
|
|
/* File attributes at open. We try to keep the .filesize field
|
|
|
|
* updated if it changes (eg by writing at the end of the file).
|
|
|
|
*/
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs;
|
2013-04-09 22:30:54 +08:00
|
|
|
|
2016-10-25 21:03:59 +08:00
|
|
|
InetSocketAddress *inet;
|
|
|
|
|
2013-04-09 22:30:54 +08:00
|
|
|
/* Used to warn if 'flush' is not supported. */
|
|
|
|
bool unsafe_flush_warning;
|
2013-04-09 22:30:53 +08:00
|
|
|
} BDRVSSHState;
|
|
|
|
|
|
|
|
static void ssh_state_init(BDRVSSHState *s)
|
|
|
|
{
|
|
|
|
memset(s, 0, sizeof *s);
|
|
|
|
s->sock = -1;
|
|
|
|
s->offset = -1;
|
|
|
|
qemu_co_mutex_init(&s->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ssh_state_free(BDRVSSHState *s)
|
|
|
|
{
|
|
|
|
if (s->sftp_handle) {
|
|
|
|
libssh2_sftp_close(s->sftp_handle);
|
|
|
|
}
|
|
|
|
if (s->sftp) {
|
|
|
|
libssh2_sftp_shutdown(s->sftp);
|
|
|
|
}
|
|
|
|
if (s->session) {
|
|
|
|
libssh2_session_disconnect(s->session,
|
|
|
|
"from qemu ssh client: "
|
|
|
|
"user closed the connection");
|
|
|
|
libssh2_session_free(s->session);
|
|
|
|
}
|
|
|
|
if (s->sock >= 0) {
|
|
|
|
close(s->sock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 17:00:13 +08:00
|
|
|
static void GCC_FMT_ATTR(3, 4)
|
|
|
|
session_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *msg;
|
|
|
|
|
|
|
|
va_start(args, fs);
|
|
|
|
msg = g_strdup_vprintf(fs, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (s->session) {
|
|
|
|
char *ssh_err;
|
|
|
|
int ssh_err_code;
|
|
|
|
|
|
|
|
/* This is not an errno. See <libssh2.h>. */
|
|
|
|
ssh_err_code = libssh2_session_last_error(s->session,
|
|
|
|
&ssh_err, NULL, 0);
|
|
|
|
error_setg(errp, "%s: %s (libssh2 error code: %d)",
|
|
|
|
msg, ssh_err, ssh_err_code);
|
|
|
|
} else {
|
|
|
|
error_setg(errp, "%s", msg);
|
|
|
|
}
|
|
|
|
g_free(msg);
|
|
|
|
}
|
|
|
|
|
2014-05-16 17:00:16 +08:00
|
|
|
static void GCC_FMT_ATTR(3, 4)
|
|
|
|
sftp_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
2014-05-16 17:00:16 +08:00
|
|
|
char *msg;
|
2013-04-09 22:30:53 +08:00
|
|
|
|
|
|
|
va_start(args, fs);
|
2014-05-16 17:00:16 +08:00
|
|
|
msg = g_strdup_vprintf(fs, args);
|
|
|
|
va_end(args);
|
2013-04-09 22:30:53 +08:00
|
|
|
|
2014-05-16 17:00:16 +08:00
|
|
|
if (s->sftp) {
|
2013-04-09 22:30:53 +08:00
|
|
|
char *ssh_err;
|
|
|
|
int ssh_err_code;
|
2014-05-16 17:00:16 +08:00
|
|
|
unsigned long sftp_err_code;
|
2013-04-09 22:30:53 +08:00
|
|
|
|
|
|
|
/* This is not an errno. See <libssh2.h>. */
|
2014-05-16 17:00:12 +08:00
|
|
|
ssh_err_code = libssh2_session_last_error(s->session,
|
|
|
|
&ssh_err, NULL, 0);
|
2014-05-16 17:00:16 +08:00
|
|
|
/* See <libssh2_sftp.h>. */
|
|
|
|
sftp_err_code = libssh2_sftp_last_error((s)->sftp);
|
2013-04-09 22:30:53 +08:00
|
|
|
|
2014-05-16 17:00:16 +08:00
|
|
|
error_setg(errp,
|
|
|
|
"%s: %s (libssh2 error code: %d, sftp error code: %lu)",
|
|
|
|
msg, ssh_err, ssh_err_code, sftp_err_code);
|
|
|
|
} else {
|
|
|
|
error_setg(errp, "%s", msg);
|
|
|
|
}
|
|
|
|
g_free(msg);
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
2013-04-19 04:21:05 +08:00
|
|
|
static void GCC_FMT_ATTR(2, 3)
|
2013-04-09 22:30:53 +08:00
|
|
|
sftp_error_report(BDRVSSHState *s, const char *fs, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fs);
|
|
|
|
error_vprintf(fs, args);
|
|
|
|
|
|
|
|
if ((s)->sftp) {
|
|
|
|
char *ssh_err;
|
|
|
|
int ssh_err_code;
|
|
|
|
unsigned long sftp_err_code;
|
|
|
|
|
|
|
|
/* This is not an errno. See <libssh2.h>. */
|
2014-05-16 17:00:12 +08:00
|
|
|
ssh_err_code = libssh2_session_last_error(s->session,
|
|
|
|
&ssh_err, NULL, 0);
|
2013-04-09 22:30:53 +08:00
|
|
|
/* See <libssh2_sftp.h>. */
|
|
|
|
sftp_err_code = libssh2_sftp_last_error((s)->sftp);
|
|
|
|
|
|
|
|
error_printf(": %s (libssh2 error code: %d, sftp error code: %lu)",
|
|
|
|
ssh_err, ssh_err_code, sftp_err_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
error_printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_uri(const char *filename, QDict *options, Error **errp)
|
|
|
|
{
|
|
|
|
URI *uri = NULL;
|
2015-09-14 19:12:34 +08:00
|
|
|
QueryParams *qp;
|
2016-10-25 21:04:00 +08:00
|
|
|
char *port_str;
|
2013-04-09 22:30:53 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
uri = uri_parse(filename);
|
|
|
|
if (!uri) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(uri->scheme, "ssh") != 0) {
|
|
|
|
error_setg(errp, "URI scheme must be 'ssh'");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uri->server || strcmp(uri->server, "") == 0) {
|
|
|
|
error_setg(errp, "missing hostname in URI");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uri->path || strcmp(uri->path, "") == 0) {
|
|
|
|
error_setg(errp, "missing remote path in URI");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
qp = query_params_parse(uri->query);
|
|
|
|
if (!qp) {
|
|
|
|
error_setg(errp, "could not parse query parameters");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(uri->user && strcmp(uri->user, "") != 0) {
|
|
|
|
qdict_put(options, "user", qstring_from_str(uri->user));
|
|
|
|
}
|
|
|
|
|
2016-10-25 21:04:00 +08:00
|
|
|
qdict_put(options, "server.host", qstring_from_str(uri->server));
|
2013-04-09 22:30:53 +08:00
|
|
|
|
2016-10-25 21:04:00 +08:00
|
|
|
port_str = g_strdup_printf("%d", uri->port ?: 22);
|
|
|
|
qdict_put(options, "server.port", qstring_from_str(port_str));
|
|
|
|
g_free(port_str);
|
2013-04-09 22:30:53 +08:00
|
|
|
|
|
|
|
qdict_put(options, "path", qstring_from_str(uri->path));
|
|
|
|
|
|
|
|
/* Pick out any query parameters that we understand, and ignore
|
|
|
|
* the rest.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < qp->n; ++i) {
|
|
|
|
if (strcmp(qp->p[i].name, "host_key_check") == 0) {
|
|
|
|
qdict_put(options, "host_key_check",
|
|
|
|
qstring_from_str(qp->p[i].value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query_params_free(qp);
|
|
|
|
uri_free(uri);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (uri) {
|
|
|
|
uri_free(uri);
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-10-25 21:03:57 +08:00
|
|
|
static bool ssh_has_filename_options_conflict(QDict *options, Error **errp)
|
|
|
|
{
|
|
|
|
const QDictEntry *qe;
|
|
|
|
|
|
|
|
for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
|
|
|
|
if (!strcmp(qe->key, "host") ||
|
|
|
|
!strcmp(qe->key, "port") ||
|
|
|
|
!strcmp(qe->key, "path") ||
|
|
|
|
!strcmp(qe->key, "user") ||
|
2016-10-25 21:03:59 +08:00
|
|
|
!strcmp(qe->key, "host_key_check") ||
|
|
|
|
strstart(qe->key, "server.", NULL))
|
2016-10-25 21:03:57 +08:00
|
|
|
{
|
|
|
|
error_setg(errp, "Option '%s' cannot be used with a file name",
|
|
|
|
qe->key);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-09 22:30:53 +08:00
|
|
|
static void ssh_parse_filename(const char *filename, QDict *options,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2016-10-25 21:03:57 +08:00
|
|
|
if (ssh_has_filename_options_conflict(options, errp)) {
|
2013-04-09 22:30:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_uri(filename, options, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_host_key_knownhosts(BDRVSSHState *s,
|
2014-05-16 17:00:13 +08:00
|
|
|
const char *host, int port, Error **errp)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
const char *home;
|
|
|
|
char *knh_file = NULL;
|
|
|
|
LIBSSH2_KNOWNHOSTS *knh = NULL;
|
|
|
|
struct libssh2_knownhost *found;
|
|
|
|
int ret, r;
|
|
|
|
const char *hostkey;
|
|
|
|
size_t len;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
hostkey = libssh2_session_hostkey(s->session, &len, &type);
|
|
|
|
if (!hostkey) {
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:13 +08:00
|
|
|
session_error_setg(errp, s, "failed to read remote host key");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
knh = libssh2_knownhost_init(s->session);
|
|
|
|
if (!knh) {
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:13 +08:00
|
|
|
session_error_setg(errp, s,
|
|
|
|
"failed to initialize known hosts support");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
home = getenv("HOME");
|
|
|
|
if (home) {
|
|
|
|
knh_file = g_strdup_printf("%s/.ssh/known_hosts", home);
|
|
|
|
} else {
|
|
|
|
knh_file = g_strdup_printf("/root/.ssh/known_hosts");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read all known hosts from OpenSSH-style known_hosts file. */
|
|
|
|
libssh2_knownhost_readfile(knh, knh_file, LIBSSH2_KNOWNHOST_FILE_OPENSSH);
|
|
|
|
|
|
|
|
r = libssh2_knownhost_checkp(knh, host, port, hostkey, len,
|
|
|
|
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
|
|
|
|
LIBSSH2_KNOWNHOST_KEYENC_RAW,
|
|
|
|
&found);
|
|
|
|
switch (r) {
|
|
|
|
case LIBSSH2_KNOWNHOST_CHECK_MATCH:
|
|
|
|
/* OK */
|
|
|
|
DPRINTF("host key OK: %s", found->key);
|
|
|
|
break;
|
|
|
|
case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:13 +08:00
|
|
|
session_error_setg(errp, s,
|
|
|
|
"host key does not match the one in known_hosts"
|
|
|
|
" (found key %s)", found->key);
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:13 +08:00
|
|
|
session_error_setg(errp, s, "no host key was found in known_hosts");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
case LIBSSH2_KNOWNHOST_CHECK_FAILURE:
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:13 +08:00
|
|
|
session_error_setg(errp, s,
|
|
|
|
"failure matching the host key with known_hosts");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:13 +08:00
|
|
|
session_error_setg(errp, s, "unknown error matching the host key"
|
|
|
|
" with known_hosts (%d)", r);
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* known_hosts checking successful. */
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (knh != NULL) {
|
|
|
|
libssh2_knownhost_free(knh);
|
|
|
|
}
|
|
|
|
g_free(knh_file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned hex2decimal(char ch)
|
|
|
|
{
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
return (ch - '0');
|
|
|
|
} else if (ch >= 'a' && ch <= 'f') {
|
|
|
|
return 10 + (ch - 'a');
|
|
|
|
} else if (ch >= 'A' && ch <= 'F') {
|
|
|
|
return 10 + (ch - 'A');
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare the binary fingerprint (hash of host key) with the
|
|
|
|
* host_key_check parameter.
|
|
|
|
*/
|
|
|
|
static int compare_fingerprint(const unsigned char *fingerprint, size_t len,
|
|
|
|
const char *host_key_check)
|
|
|
|
{
|
|
|
|
unsigned c;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
while (*host_key_check == ':')
|
|
|
|
host_key_check++;
|
|
|
|
if (!qemu_isxdigit(host_key_check[0]) ||
|
|
|
|
!qemu_isxdigit(host_key_check[1]))
|
|
|
|
return 1;
|
|
|
|
c = hex2decimal(host_key_check[0]) * 16 +
|
|
|
|
hex2decimal(host_key_check[1]);
|
|
|
|
if (c - *fingerprint != 0)
|
|
|
|
return c - *fingerprint;
|
|
|
|
fingerprint++;
|
|
|
|
len--;
|
|
|
|
host_key_check += 2;
|
|
|
|
}
|
|
|
|
return *host_key_check - '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_host_key_hash(BDRVSSHState *s, const char *hash,
|
2014-05-16 17:00:13 +08:00
|
|
|
int hash_type, size_t fingerprint_len, Error **errp)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
const char *fingerprint;
|
|
|
|
|
|
|
|
fingerprint = libssh2_hostkey_hash(s->session, hash_type);
|
|
|
|
if (!fingerprint) {
|
2014-05-16 17:00:13 +08:00
|
|
|
session_error_setg(errp, s, "failed to read remote host key");
|
2013-04-09 22:30:53 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(compare_fingerprint((unsigned char *) fingerprint, fingerprint_len,
|
|
|
|
hash) != 0) {
|
2014-05-16 17:00:13 +08:00
|
|
|
error_setg(errp, "remote host key does not match host_key_check '%s'",
|
|
|
|
hash);
|
2013-04-09 22:30:53 +08:00
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_host_key(BDRVSSHState *s, const char *host, int port,
|
2014-05-16 17:00:13 +08:00
|
|
|
const char *host_key_check, Error **errp)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
/* host_key_check=no */
|
|
|
|
if (strcmp(host_key_check, "no") == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* host_key_check=md5:xx:yy:zz:... */
|
2013-04-19 05:09:33 +08:00
|
|
|
if (strncmp(host_key_check, "md5:", 4) == 0) {
|
2013-04-09 22:30:53 +08:00
|
|
|
return check_host_key_hash(s, &host_key_check[4],
|
2014-05-16 17:00:13 +08:00
|
|
|
LIBSSH2_HOSTKEY_HASH_MD5, 16, errp);
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* host_key_check=sha1:xx:yy:zz:... */
|
2013-04-19 05:09:33 +08:00
|
|
|
if (strncmp(host_key_check, "sha1:", 5) == 0) {
|
2013-04-09 22:30:53 +08:00
|
|
|
return check_host_key_hash(s, &host_key_check[5],
|
2014-05-16 17:00:13 +08:00
|
|
|
LIBSSH2_HOSTKEY_HASH_SHA1, 20, errp);
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* host_key_check=yes */
|
|
|
|
if (strcmp(host_key_check, "yes") == 0) {
|
2014-05-16 17:00:13 +08:00
|
|
|
return check_host_key_knownhosts(s, host, port, errp);
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
2014-05-16 17:00:13 +08:00
|
|
|
error_setg(errp, "unknown host_key_check setting (%s)", host_key_check);
|
2013-04-09 22:30:53 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-05-16 17:00:14 +08:00
|
|
|
static int authenticate(BDRVSSHState *s, const char *user, Error **errp)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
int r, ret;
|
|
|
|
const char *userauthlist;
|
|
|
|
LIBSSH2_AGENT *agent = NULL;
|
|
|
|
struct libssh2_agent_publickey *identity;
|
|
|
|
struct libssh2_agent_publickey *prev_identity = NULL;
|
|
|
|
|
|
|
|
userauthlist = libssh2_userauth_list(s->session, user, strlen(user));
|
|
|
|
if (strstr(userauthlist, "publickey") == NULL) {
|
|
|
|
ret = -EPERM;
|
2014-05-16 17:00:14 +08:00
|
|
|
error_setg(errp,
|
|
|
|
"remote server does not support \"publickey\" authentication");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to ssh-agent and try each identity in turn. */
|
|
|
|
agent = libssh2_agent_init(s->session);
|
|
|
|
if (!agent) {
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:14 +08:00
|
|
|
session_error_setg(errp, s, "failed to initialize ssh-agent support");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (libssh2_agent_connect(agent)) {
|
|
|
|
ret = -ECONNREFUSED;
|
2014-05-16 17:00:14 +08:00
|
|
|
session_error_setg(errp, s, "failed to connect to ssh-agent");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (libssh2_agent_list_identities(agent)) {
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:14 +08:00
|
|
|
session_error_setg(errp, s,
|
|
|
|
"failed requesting identities from ssh-agent");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
r = libssh2_agent_get_identity(agent, &identity, prev_identity);
|
|
|
|
if (r == 1) { /* end of list */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r < 0) {
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:14 +08:00
|
|
|
session_error_setg(errp, s,
|
|
|
|
"failed to obtain identity from ssh-agent");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
r = libssh2_agent_userauth(agent, user, identity);
|
|
|
|
if (r == 0) {
|
|
|
|
/* Authenticated! */
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Failed to authenticate with this identity, try the next one. */
|
|
|
|
prev_identity = identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = -EPERM;
|
2014-05-16 17:00:14 +08:00
|
|
|
error_setg(errp, "failed to authenticate using publickey authentication "
|
|
|
|
"and the identities held by your ssh-agent");
|
2013-04-09 22:30:53 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (agent != NULL) {
|
|
|
|
/* Note: libssh2 implementation implicitly calls
|
|
|
|
* libssh2_agent_disconnect if necessary.
|
|
|
|
*/
|
|
|
|
libssh2_agent_free(agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:29:23 +08:00
|
|
|
static QemuOptsList ssh_runtime_opts = {
|
|
|
|
.name = "ssh",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(ssh_runtime_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = "host",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "Host to connect to",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "port",
|
|
|
|
.type = QEMU_OPT_NUMBER,
|
|
|
|
.help = "Port to connect to",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "path",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "Path of the image on the host",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "user",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "User as which to connect",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "host_key_check",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "Defines how and what to check the host key against",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-10-25 21:03:59 +08:00
|
|
|
static bool ssh_process_legacy_socket_options(QDict *output_opts,
|
|
|
|
QemuOpts *legacy_opts,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
const char *host = qemu_opt_get(legacy_opts, "host");
|
|
|
|
const char *port = qemu_opt_get(legacy_opts, "port");
|
|
|
|
|
|
|
|
if (!host && port) {
|
|
|
|
error_setg(errp, "port may not be used without host");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host) {
|
|
|
|
qdict_put(output_opts, "server.host", qstring_from_str(host));
|
|
|
|
qdict_put(output_opts, "server.port",
|
|
|
|
qstring_from_str(port ?: stringify(22)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static InetSocketAddress *ssh_config(BDRVSSHState *s, QDict *options,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
InetSocketAddress *inet = NULL;
|
|
|
|
QDict *addr = NULL;
|
|
|
|
QObject *crumpled_addr = NULL;
|
|
|
|
Visitor *iv = NULL;
|
|
|
|
Error *local_error = NULL;
|
|
|
|
|
|
|
|
qdict_extract_subqdict(options, &addr, "server.");
|
|
|
|
if (!qdict_size(addr)) {
|
|
|
|
error_setg(errp, "SSH server address missing");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
crumpled_addr = qdict_crumple(addr, errp);
|
|
|
|
if (!crumpled_addr) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
iv = qobject_input_visitor_new(crumpled_addr, true);
|
|
|
|
visit_type_InetSocketAddress(iv, NULL, &inet, &local_error);
|
|
|
|
if (local_error) {
|
|
|
|
error_propagate(errp, local_error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
QDECREF(addr);
|
|
|
|
qobject_decref(crumpled_addr);
|
|
|
|
visit_free(iv);
|
|
|
|
return inet;
|
|
|
|
}
|
|
|
|
|
2013-04-09 22:30:53 +08:00
|
|
|
static int connect_to_ssh(BDRVSSHState *s, QDict *options,
|
2014-05-16 17:00:15 +08:00
|
|
|
int ssh_flags, int creat_mode, Error **errp)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
int r, ret;
|
2016-08-15 21:29:23 +08:00
|
|
|
QemuOpts *opts = NULL;
|
|
|
|
Error *local_err = NULL;
|
2016-10-25 21:03:59 +08:00
|
|
|
const char *user, *path, *host_key_check;
|
|
|
|
long port = 0;
|
2013-04-09 22:30:53 +08:00
|
|
|
|
2016-08-15 21:29:23 +08:00
|
|
|
opts = qemu_opts_create(&ssh_runtime_opts, NULL, 0, &error_abort);
|
|
|
|
qemu_opts_absorb_qdict(opts, options, &local_err);
|
|
|
|
if (local_err) {
|
2014-09-29 16:06:22 +08:00
|
|
|
ret = -EINVAL;
|
2016-08-15 21:29:23 +08:00
|
|
|
error_propagate(errp, local_err);
|
2014-09-29 16:06:22 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2013-04-09 22:30:53 +08:00
|
|
|
|
2016-10-25 21:03:59 +08:00
|
|
|
if (!ssh_process_legacy_socket_options(options, opts, errp)) {
|
2016-08-15 21:29:23 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 21:29:23 +08:00
|
|
|
path = qemu_opt_get(opts, "path");
|
|
|
|
if (!path) {
|
2014-09-29 16:06:22 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
error_setg(errp, "No path was specified");
|
|
|
|
goto err;
|
|
|
|
}
|
2013-04-09 22:30:53 +08:00
|
|
|
|
2016-08-15 21:29:23 +08:00
|
|
|
user = qemu_opt_get(opts, "user");
|
|
|
|
if (!user) {
|
2013-04-09 22:30:53 +08:00
|
|
|
user = g_get_user_name();
|
|
|
|
if (!user) {
|
2014-05-16 17:00:15 +08:00
|
|
|
error_setg_errno(errp, errno, "Can't get user name");
|
2013-04-09 22:30:53 +08:00
|
|
|
ret = -errno;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:29:23 +08:00
|
|
|
host_key_check = qemu_opt_get(opts, "host_key_check");
|
|
|
|
if (!host_key_check) {
|
2013-04-09 22:30:53 +08:00
|
|
|
host_key_check = "yes";
|
|
|
|
}
|
|
|
|
|
2016-10-25 21:03:59 +08:00
|
|
|
/* Pop the config into our state object, Exit if invalid */
|
|
|
|
s->inet = ssh_config(s, options, errp);
|
|
|
|
if (!s->inet) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemu_strtol(s->inet->port, NULL, 10, &port) < 0) {
|
|
|
|
error_setg(errp, "Use only numeric port value");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
2013-04-09 22:30:54 +08:00
|
|
|
|
2013-04-09 22:30:53 +08:00
|
|
|
/* Open the socket and connect. */
|
2016-10-25 21:03:59 +08:00
|
|
|
s->sock = inet_connect_saddr(s->inet, errp, NULL, NULL);
|
2014-05-16 17:00:15 +08:00
|
|
|
if (s->sock < 0) {
|
block/ssh: Avoid segfault if inet_connect doesn't set errno.
On some (but not all) systems:
$ qemu-img create -f qcow2 overlay -b ssh://xen/
Segmentation fault
It turns out this happens when inet_connect returns -1 in the
following code, but errno == 0.
s->sock = inet_connect(s->hostport, errp);
if (s->sock < 0) {
ret = -errno;
goto err;
}
In the test case above, no host called "xen" exists, so getaddrinfo fails.
On Fedora 22, getaddrinfo happens to set errno = ENOENT (although it
is *not* documented to do that), so it doesn't segfault.
On RHEL 7, errno is not set by the failing getaddrinfo, so ret =
-errno = 0, so the caller doesn't know there was an error and
continues with a half-initialized BDRVSSHState struct, and everything
goes south from there, eventually resulting in a segfault.
Fix this by setting ret to -EIO (same as block/nbd.c and
block/sheepdog.c). The real error is saved in the Error** errp
struct, so it is printed correctly:
$ ./qemu-img create -f qcow2 overlay -b ssh://xen/
qemu-img: overlay: address resolution failed for xen:22: No address associated with hostname
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
Reported-by: Jun Li
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1147343
Signed-off-by: Jeff Cody <jcody@redhat.com>
2015-07-22 21:27:47 +08:00
|
|
|
ret = -EIO;
|
2013-04-09 22:30:53 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create SSH session. */
|
|
|
|
s->session = libssh2_session_init();
|
|
|
|
if (!s->session) {
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:15 +08:00
|
|
|
session_error_setg(errp, s, "failed to initialize libssh2 session");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TRACE_LIBSSH2 != 0
|
|
|
|
libssh2_trace(s->session, TRACE_LIBSSH2);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
r = libssh2_session_handshake(s->session, s->sock);
|
|
|
|
if (r != 0) {
|
|
|
|
ret = -EINVAL;
|
2014-05-16 17:00:15 +08:00
|
|
|
session_error_setg(errp, s, "failed to establish SSH session");
|
2013-04-09 22:30:53 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the remote host's key against known_hosts. */
|
2016-10-25 21:03:59 +08:00
|
|
|
ret = check_host_key(s, s->inet->host, port, host_key_check,
|
|
|
|
errp);
|
2013-04-09 22:30:53 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Authenticate. */
|
2014-05-16 17:00:15 +08:00
|
|
|
ret = authenticate(s, user, errp);
|
2013-04-09 22:30:53 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start SFTP. */
|
|
|
|
s->sftp = libssh2_sftp_init(s->session);
|
|
|
|
if (!s->sftp) {
|
2014-05-16 17:00:15 +08:00
|
|
|
session_error_setg(errp, s, "failed to initialize sftp handle");
|
2013-04-09 22:30:53 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the remote file. */
|
|
|
|
DPRINTF("opening file %s flags=0x%x creat_mode=0%o",
|
|
|
|
path, ssh_flags, creat_mode);
|
|
|
|
s->sftp_handle = libssh2_sftp_open(s->sftp, path, ssh_flags, creat_mode);
|
|
|
|
if (!s->sftp_handle) {
|
2014-05-16 17:00:16 +08:00
|
|
|
session_error_setg(errp, s, "failed to open remote file '%s'", path);
|
2013-04-09 22:30:53 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:29:23 +08:00
|
|
|
qemu_opts_del(opts);
|
|
|
|
|
2013-04-09 22:30:53 +08:00
|
|
|
r = libssh2_sftp_fstat(s->sftp_handle, &s->attrs);
|
|
|
|
if (r < 0) {
|
2014-05-16 17:00:16 +08:00
|
|
|
sftp_error_setg(errp, s, "failed to read file attributes");
|
2013-04-09 22:30:53 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (s->sftp_handle) {
|
|
|
|
libssh2_sftp_close(s->sftp_handle);
|
|
|
|
}
|
|
|
|
s->sftp_handle = NULL;
|
|
|
|
if (s->sftp) {
|
|
|
|
libssh2_sftp_shutdown(s->sftp);
|
|
|
|
}
|
|
|
|
s->sftp = NULL;
|
|
|
|
if (s->session) {
|
|
|
|
libssh2_session_disconnect(s->session,
|
|
|
|
"from qemu ssh client: "
|
|
|
|
"error opening connection");
|
|
|
|
libssh2_session_free(s->session);
|
|
|
|
}
|
|
|
|
s->session = NULL;
|
|
|
|
|
2016-08-15 21:29:23 +08:00
|
|
|
qemu_opts_del(opts);
|
|
|
|
|
2013-04-09 22:30:53 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-05 20:22:29 +08:00
|
|
|
static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags,
|
|
|
|
Error **errp)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
BDRVSSHState *s = bs->opaque;
|
|
|
|
int ret;
|
|
|
|
int ssh_flags;
|
|
|
|
|
|
|
|
ssh_state_init(s);
|
|
|
|
|
|
|
|
ssh_flags = LIBSSH2_FXF_READ;
|
|
|
|
if (bdrv_flags & BDRV_O_RDWR) {
|
|
|
|
ssh_flags |= LIBSSH2_FXF_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start up SSH. */
|
2014-05-16 17:00:16 +08:00
|
|
|
ret = connect_to_ssh(s, options, ssh_flags, 0, errp);
|
2013-04-09 22:30:53 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go non-blocking. */
|
|
|
|
libssh2_session_set_blocking(s->session, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (s->sock >= 0) {
|
|
|
|
close(s->sock);
|
|
|
|
}
|
|
|
|
s->sock = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-05 17:21:06 +08:00
|
|
|
static QemuOptsList ssh_create_opts = {
|
|
|
|
.name = "ssh-create-opts",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(ssh_create_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_SIZE,
|
|
|
|
.type = QEMU_OPT_SIZE,
|
|
|
|
.help = "Virtual disk size"
|
|
|
|
},
|
|
|
|
{ /* end of list */ }
|
|
|
|
}
|
2013-04-09 22:30:53 +08:00
|
|
|
};
|
|
|
|
|
2014-06-05 17:21:06 +08:00
|
|
|
static int ssh_create(const char *filename, QemuOpts *opts, Error **errp)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
int r, ret;
|
|
|
|
int64_t total_size = 0;
|
|
|
|
QDict *uri_options = NULL;
|
|
|
|
BDRVSSHState s;
|
|
|
|
ssize_t r2;
|
|
|
|
char c[1] = { '\0' };
|
|
|
|
|
|
|
|
ssh_state_init(&s);
|
|
|
|
|
|
|
|
/* Get desired file size. */
|
2014-09-10 17:05:45 +08:00
|
|
|
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
|
|
|
BDRV_SECTOR_SIZE);
|
2013-04-09 22:30:53 +08:00
|
|
|
DPRINTF("total_size=%" PRIi64, total_size);
|
|
|
|
|
|
|
|
uri_options = qdict_new();
|
2014-05-16 17:00:16 +08:00
|
|
|
r = parse_uri(filename, uri_options, errp);
|
2013-04-09 22:30:53 +08:00
|
|
|
if (r < 0) {
|
|
|
|
ret = r;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = connect_to_ssh(&s, uri_options,
|
|
|
|
LIBSSH2_FXF_READ|LIBSSH2_FXF_WRITE|
|
2014-05-16 17:00:15 +08:00
|
|
|
LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
|
2014-05-16 17:00:16 +08:00
|
|
|
0644, errp);
|
2013-04-09 22:30:53 +08:00
|
|
|
if (r < 0) {
|
|
|
|
ret = r;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (total_size > 0) {
|
|
|
|
libssh2_sftp_seek64(s.sftp_handle, total_size-1);
|
|
|
|
r2 = libssh2_sftp_write(s.sftp_handle, c, 1);
|
|
|
|
if (r2 < 0) {
|
2014-05-16 17:00:16 +08:00
|
|
|
sftp_error_setg(errp, &s, "truncate failed");
|
2013-04-09 22:30:53 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
s.attrs.filesize = total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
ssh_state_free(&s);
|
|
|
|
if (uri_options != NULL) {
|
|
|
|
QDECREF(uri_options);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ssh_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSSHState *s = bs->opaque;
|
|
|
|
|
|
|
|
ssh_state_free(s);
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:15:18 +08:00
|
|
|
static int ssh_has_zero_init(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSSHState *s = bs->opaque;
|
|
|
|
/* Assume false, unless we can positively prove it's true. */
|
|
|
|
int has_zero_init = 0;
|
|
|
|
|
|
|
|
if (s->attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
|
|
|
|
if (s->attrs.permissions & LIBSSH2_SFTP_S_IFREG) {
|
|
|
|
has_zero_init = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_zero_init;
|
|
|
|
}
|
|
|
|
|
2013-04-09 22:30:53 +08:00
|
|
|
static void restart_coroutine(void *opaque)
|
|
|
|
{
|
|
|
|
Coroutine *co = opaque;
|
|
|
|
|
|
|
|
DPRINTF("co=%p", co);
|
|
|
|
|
coroutine: move entry argument to qemu_coroutine_create
In practice the entry argument is always known at creation time, and
it is confusing that sometimes qemu_coroutine_enter is used with a
non-NULL argument to re-enter a coroutine (this happens in
block/sheepdog.c and tests/test-coroutine.c). So pass the opaque value
at creation time, for consistency with e.g. aio_bh_new.
Mostly done with the following semantic patch:
@ entry1 @
expression entry, arg, co;
@@
- co = qemu_coroutine_create(entry);
+ co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry2 @
expression entry, arg;
identifier co;
@@
- Coroutine *co = qemu_coroutine_create(entry);
+ Coroutine *co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry3 @
expression entry, arg;
@@
- qemu_coroutine_enter(qemu_coroutine_create(entry), arg);
+ qemu_coroutine_enter(qemu_coroutine_create(entry, arg));
@ reentry @
expression co;
@@
- qemu_coroutine_enter(co, NULL);
+ qemu_coroutine_enter(co);
except for the aforementioned few places where the semantic patch
stumbled (as expected) and for test_co_queue, which would otherwise
produce an uninitialized variable warning.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2016-07-05 01:10:01 +08:00
|
|
|
qemu_coroutine_enter(co);
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 22:34:53 +08:00
|
|
|
static coroutine_fn void set_fd_handler(BDRVSSHState *s, BlockDriverState *bs)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
IOHandler *rd_handler = NULL, *wr_handler = NULL;
|
|
|
|
Coroutine *co = qemu_coroutine_self();
|
|
|
|
|
|
|
|
r = libssh2_session_block_directions(s->session);
|
|
|
|
|
|
|
|
if (r & LIBSSH2_SESSION_BLOCK_INBOUND) {
|
|
|
|
rd_handler = restart_coroutine;
|
|
|
|
}
|
|
|
|
if (r & LIBSSH2_SESSION_BLOCK_OUTBOUND) {
|
|
|
|
wr_handler = restart_coroutine;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF("s->sock=%d rd_handler=%p wr_handler=%p", s->sock,
|
|
|
|
rd_handler, wr_handler);
|
|
|
|
|
2014-05-08 22:34:53 +08:00
|
|
|
aio_set_fd_handler(bdrv_get_aio_context(bs), s->sock,
|
2015-10-23 11:08:05 +08:00
|
|
|
false, rd_handler, wr_handler, co);
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 22:34:53 +08:00
|
|
|
static coroutine_fn void clear_fd_handler(BDRVSSHState *s,
|
|
|
|
BlockDriverState *bs)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
|
|
|
DPRINTF("s->sock=%d", s->sock);
|
2015-10-23 11:08:05 +08:00
|
|
|
aio_set_fd_handler(bdrv_get_aio_context(bs), s->sock,
|
|
|
|
false, NULL, NULL, NULL);
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A non-blocking call returned EAGAIN, so yield, ensuring the
|
|
|
|
* handlers are set up so that we'll be rescheduled when there is an
|
|
|
|
* interesting event on the socket.
|
|
|
|
*/
|
2014-05-08 22:34:53 +08:00
|
|
|
static coroutine_fn void co_yield(BDRVSSHState *s, BlockDriverState *bs)
|
2013-04-09 22:30:53 +08:00
|
|
|
{
|
2014-05-08 22:34:53 +08:00
|
|
|
set_fd_handler(s, bs);
|
2013-04-09 22:30:53 +08:00
|
|
|
qemu_coroutine_yield();
|
2014-05-08 22:34:53 +08:00
|
|
|
clear_fd_handler(s, bs);
|
2013-04-09 22:30:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SFTP has a function `libssh2_sftp_seek64' which seeks to a position
|
|
|
|
* in the remote file. Notice that it just updates a field in the
|
|
|
|
* sftp_handle structure, so there is no network traffic and it cannot
|
|
|
|
* fail.
|
|
|
|
*
|
|
|
|
* However, `libssh2_sftp_seek64' does have a catastrophic effect on
|
|
|
|
* performance since it causes the handle to throw away all in-flight
|
|
|
|
* reads and buffered readahead data. Therefore this function tries
|
|
|
|
* to be intelligent about when to call the underlying libssh2 function.
|
|
|
|
*/
|
|
|
|
#define SSH_SEEK_WRITE 0
|
|
|
|
#define SSH_SEEK_READ 1
|
|
|
|
#define SSH_SEEK_FORCE 2
|
|
|
|
|
|
|
|
static void ssh_seek(BDRVSSHState *s, int64_t offset, int flags)
|
|
|
|
{
|
|
|
|
bool op_read = (flags & SSH_SEEK_READ) != 0;
|
|
|
|
bool force = (flags & SSH_SEEK_FORCE) != 0;
|
|
|
|
|
|
|
|
if (force || op_read != s->offset_op_read || offset != s->offset) {
|
|
|
|
DPRINTF("seeking to offset=%" PRIi64, offset);
|
|
|
|
libssh2_sftp_seek64(s->sftp_handle, offset);
|
|
|
|
s->offset = offset;
|
|
|
|
s->offset_op_read = op_read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 22:34:53 +08:00
|
|
|
static coroutine_fn int ssh_read(BDRVSSHState *s, BlockDriverState *bs,
|
2013-04-09 22:30:53 +08:00
|
|
|
int64_t offset, size_t size,
|
|
|
|
QEMUIOVector *qiov)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
size_t got;
|
|
|
|
char *buf, *end_of_vec;
|
|
|
|
struct iovec *i;
|
|
|
|
|
|
|
|
DPRINTF("offset=%" PRIi64 " size=%zu", offset, size);
|
|
|
|
|
|
|
|
ssh_seek(s, offset, SSH_SEEK_READ);
|
|
|
|
|
|
|
|
/* This keeps track of the current iovec element ('i'), where we
|
|
|
|
* will write to next ('buf'), and the end of the current iovec
|
|
|
|
* ('end_of_vec').
|
|
|
|
*/
|
|
|
|
i = &qiov->iov[0];
|
|
|
|
buf = i->iov_base;
|
|
|
|
end_of_vec = i->iov_base + i->iov_len;
|
|
|
|
|
|
|
|
/* libssh2 has a hard-coded limit of 2000 bytes per request,
|
|
|
|
* although it will also do readahead behind our backs. Therefore
|
|
|
|
* we may have to do repeated reads here until we have read 'size'
|
|
|
|
* bytes.
|
|
|
|
*/
|
|
|
|
for (got = 0; got < size; ) {
|
|
|
|
again:
|
|
|
|
DPRINTF("sftp_read buf=%p size=%zu", buf, end_of_vec - buf);
|
|
|
|
r = libssh2_sftp_read(s->sftp_handle, buf, end_of_vec - buf);
|
|
|
|
DPRINTF("sftp_read returned %zd", r);
|
|
|
|
|
|
|
|
if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
|
2014-05-08 22:34:53 +08:00
|
|
|
co_yield(s, bs);
|
2013-04-09 22:30:53 +08:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
if (r < 0) {
|
|
|
|
sftp_error_report(s, "read failed");
|
|
|
|
s->offset = -1;
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
|
|
|
/* EOF: Short read so pad the buffer with zeroes and return it. */
|
|
|
|
qemu_iovec_memset(qiov, got, 0, size - got);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
got += r;
|
|
|
|
buf += r;
|
|
|
|
s->offset += r;
|
|
|
|
if (buf >= end_of_vec && got < size) {
|
|
|
|
i++;
|
|
|
|
buf = i->iov_base;
|
|
|
|
end_of_vec = i->iov_base + i->iov_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static coroutine_fn int ssh_co_readv(BlockDriverState *bs,
|
|
|
|
int64_t sector_num,
|
|
|
|
int nb_sectors, QEMUIOVector *qiov)
|
|
|
|
{
|
|
|
|
BDRVSSHState *s = bs->opaque;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
qemu_co_mutex_lock(&s->lock);
|
2014-05-08 22:34:53 +08:00
|
|
|
ret = ssh_read(s, bs, sector_num * BDRV_SECTOR_SIZE,
|
2013-04-09 22:30:53 +08:00
|
|
|
nb_sectors * BDRV_SECTOR_SIZE, qiov);
|
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-08 22:34:53 +08:00
|
|
|
static int ssh_write(BDRVSSHState *s, BlockDriverState *bs,
|
2013-04-09 22:30:53 +08:00
|
|
|
int64_t offset, size_t size,
|
|
|
|
QEMUIOVector *qiov)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
size_t written;
|
|
|
|
char *buf, *end_of_vec;
|
|
|
|
struct iovec *i;
|
|
|
|
|
|
|
|
DPRINTF("offset=%" PRIi64 " size=%zu", offset, size);
|
|
|
|
|
|
|
|
ssh_seek(s, offset, SSH_SEEK_WRITE);
|
|
|
|
|
|
|
|
/* This keeps track of the current iovec element ('i'), where we
|
|
|
|
* will read from next ('buf'), and the end of the current iovec
|
|
|
|
* ('end_of_vec').
|
|
|
|
*/
|
|
|
|
i = &qiov->iov[0];
|
|
|
|
buf = i->iov_base;
|
|
|
|
end_of_vec = i->iov_base + i->iov_len;
|
|
|
|
|
|
|
|
for (written = 0; written < size; ) {
|
|
|
|
again:
|
|
|
|
DPRINTF("sftp_write buf=%p size=%zu", buf, end_of_vec - buf);
|
|
|
|
r = libssh2_sftp_write(s->sftp_handle, buf, end_of_vec - buf);
|
|
|
|
DPRINTF("sftp_write returned %zd", r);
|
|
|
|
|
|
|
|
if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
|
2014-05-08 22:34:53 +08:00
|
|
|
co_yield(s, bs);
|
2013-04-09 22:30:53 +08:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
if (r < 0) {
|
|
|
|
sftp_error_report(s, "write failed");
|
|
|
|
s->offset = -1;
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
/* The libssh2 API is very unclear about this. A comment in
|
|
|
|
* the code says "nothing was acked, and no EAGAIN was
|
|
|
|
* received!" which apparently means that no data got sent
|
|
|
|
* out, and the underlying channel didn't return any EAGAIN
|
|
|
|
* indication. I think this is a bug in either libssh2 or
|
|
|
|
* OpenSSH (server-side). In any case, forcing a seek (to
|
|
|
|
* discard libssh2 internal buffers), and then trying again
|
|
|
|
* works for me.
|
|
|
|
*/
|
|
|
|
if (r == 0) {
|
|
|
|
ssh_seek(s, offset + written, SSH_SEEK_WRITE|SSH_SEEK_FORCE);
|
2014-05-08 22:34:53 +08:00
|
|
|
co_yield(s, bs);
|
2013-04-09 22:30:53 +08:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
written += r;
|
|
|
|
buf += r;
|
|
|
|
s->offset += r;
|
|
|
|
if (buf >= end_of_vec && written < size) {
|
|
|
|
i++;
|
|
|
|
buf = i->iov_base;
|
|
|
|
end_of_vec = i->iov_base + i->iov_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset + written > s->attrs.filesize)
|
|
|
|
s->attrs.filesize = offset + written;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static coroutine_fn int ssh_co_writev(BlockDriverState *bs,
|
|
|
|
int64_t sector_num,
|
|
|
|
int nb_sectors, QEMUIOVector *qiov)
|
|
|
|
{
|
|
|
|
BDRVSSHState *s = bs->opaque;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
qemu_co_mutex_lock(&s->lock);
|
2014-05-08 22:34:53 +08:00
|
|
|
ret = ssh_write(s, bs, sector_num * BDRV_SECTOR_SIZE,
|
2013-04-09 22:30:53 +08:00
|
|
|
nb_sectors * BDRV_SECTOR_SIZE, qiov);
|
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-09 22:30:54 +08:00
|
|
|
static void unsafe_flush_warning(BDRVSSHState *s, const char *what)
|
|
|
|
{
|
|
|
|
if (!s->unsafe_flush_warning) {
|
|
|
|
error_report("warning: ssh server %s does not support fsync",
|
2016-10-25 21:03:59 +08:00
|
|
|
s->inet->host);
|
2013-04-09 22:30:54 +08:00
|
|
|
if (what) {
|
|
|
|
error_report("to support fsync, you need %s", what);
|
|
|
|
}
|
|
|
|
s->unsafe_flush_warning = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_LIBSSH2_SFTP_FSYNC
|
|
|
|
|
2014-05-08 22:34:53 +08:00
|
|
|
static coroutine_fn int ssh_flush(BDRVSSHState *s, BlockDriverState *bs)
|
2013-04-09 22:30:54 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
DPRINTF("fsync");
|
|
|
|
again:
|
|
|
|
r = libssh2_sftp_fsync(s->sftp_handle);
|
|
|
|
if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
|
2014-05-08 22:34:53 +08:00
|
|
|
co_yield(s, bs);
|
2013-04-09 22:30:54 +08:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
if (r == LIBSSH2_ERROR_SFTP_PROTOCOL &&
|
|
|
|
libssh2_sftp_last_error(s->sftp) == LIBSSH2_FX_OP_UNSUPPORTED) {
|
|
|
|
unsafe_flush_warning(s, "OpenSSH >= 6.3");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (r < 0) {
|
|
|
|
sftp_error_report(s, "fsync failed");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSSHState *s = bs->opaque;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
qemu_co_mutex_lock(&s->lock);
|
2014-05-08 22:34:53 +08:00
|
|
|
ret = ssh_flush(s, bs);
|
2013-04-09 22:30:54 +08:00
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !HAS_LIBSSH2_SFTP_FSYNC */
|
|
|
|
|
|
|
|
static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSSHState *s = bs->opaque;
|
|
|
|
|
|
|
|
unsafe_flush_warning(s, "libssh2 >= 1.4.4");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !HAS_LIBSSH2_SFTP_FSYNC */
|
|
|
|
|
2013-04-09 22:30:53 +08:00
|
|
|
static int64_t ssh_getlength(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVSSHState *s = bs->opaque;
|
|
|
|
int64_t length;
|
|
|
|
|
|
|
|
/* Note we cannot make a libssh2 call here. */
|
|
|
|
length = (int64_t) s->attrs.filesize;
|
|
|
|
DPRINTF("length=%" PRIi64, length);
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BlockDriver bdrv_ssh = {
|
|
|
|
.format_name = "ssh",
|
|
|
|
.protocol_name = "ssh",
|
|
|
|
.instance_size = sizeof(BDRVSSHState),
|
|
|
|
.bdrv_parse_filename = ssh_parse_filename,
|
|
|
|
.bdrv_file_open = ssh_file_open,
|
2014-06-05 17:21:11 +08:00
|
|
|
.bdrv_create = ssh_create,
|
2013-04-09 22:30:53 +08:00
|
|
|
.bdrv_close = ssh_close,
|
2013-06-26 01:15:18 +08:00
|
|
|
.bdrv_has_zero_init = ssh_has_zero_init,
|
2013-04-09 22:30:53 +08:00
|
|
|
.bdrv_co_readv = ssh_co_readv,
|
|
|
|
.bdrv_co_writev = ssh_co_writev,
|
|
|
|
.bdrv_getlength = ssh_getlength,
|
2013-04-09 22:30:54 +08:00
|
|
|
.bdrv_co_flush_to_disk = ssh_co_flush,
|
2014-06-05 17:21:06 +08:00
|
|
|
.create_opts = &ssh_create_opts,
|
2013-04-09 22:30:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_ssh_init(void)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = libssh2_init(0);
|
|
|
|
if (r != 0) {
|
|
|
|
fprintf(stderr, "libssh2 initialization failed, %d\n", r);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bdrv_register(&bdrv_ssh);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_ssh_init);
|