Makefile: Let the 'clean' rule remove qemu-ga.exe on Windows hosts
[qemu/ar7.git] / block / ssh.c
blob859249113d3f234d6e8cb8ae36074a8cec881b81
1 /*
2 * Secure Shell (ssh) backend for QEMU.
4 * Copyright (C) 2013 Red Hat Inc., Richard W.M. Jones <rjones@redhat.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
27 #include <libssh2.h>
28 #include <libssh2_sftp.h>
30 #include "block/block_int.h"
31 #include "block/qdict.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
34 #include "qemu/option.h"
35 #include "qemu/cutils.h"
36 #include "qemu/sockets.h"
37 #include "qemu/uri.h"
38 #include "qapi/qapi-visit-sockets.h"
39 #include "qapi/qapi-visit-block-core.h"
40 #include "qapi/qmp/qdict.h"
41 #include "qapi/qmp/qstring.h"
42 #include "qapi/qobject-input-visitor.h"
43 #include "qapi/qobject-output-visitor.h"
44 #include "trace.h"
47 * TRACE_LIBSSH2=<bitmask> enables tracing in libssh2 itself. Note
48 * that this requires that libssh2 was specially compiled with the
49 * `./configure --enable-debug' option, so most likely you will have
50 * to compile it yourself. The meaning of <bitmask> is described
51 * here: http://www.libssh2.org/libssh2_trace.html
53 #define TRACE_LIBSSH2 0 /* or try: LIBSSH2_TRACE_SFTP */
55 typedef struct BDRVSSHState {
56 /* Coroutine. */
57 CoMutex lock;
59 /* SSH connection. */
60 int sock; /* socket */
61 LIBSSH2_SESSION *session; /* ssh session */
62 LIBSSH2_SFTP *sftp; /* sftp session */
63 LIBSSH2_SFTP_HANDLE *sftp_handle; /* sftp remote file handle */
65 /* See ssh_seek() function below. */
66 int64_t offset;
67 bool offset_op_read;
69 /* File attributes at open. We try to keep the .filesize field
70 * updated if it changes (eg by writing at the end of the file).
72 LIBSSH2_SFTP_ATTRIBUTES attrs;
74 InetSocketAddress *inet;
76 /* Used to warn if 'flush' is not supported. */
77 bool unsafe_flush_warning;
78 } BDRVSSHState;
80 static void ssh_state_init(BDRVSSHState *s)
82 memset(s, 0, sizeof *s);
83 s->sock = -1;
84 s->offset = -1;
85 qemu_co_mutex_init(&s->lock);
88 static void ssh_state_free(BDRVSSHState *s)
90 if (s->sftp_handle) {
91 libssh2_sftp_close(s->sftp_handle);
93 if (s->sftp) {
94 libssh2_sftp_shutdown(s->sftp);
96 if (s->session) {
97 libssh2_session_disconnect(s->session,
98 "from qemu ssh client: "
99 "user closed the connection");
100 libssh2_session_free(s->session);
102 if (s->sock >= 0) {
103 close(s->sock);
107 static void GCC_FMT_ATTR(3, 4)
108 session_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...)
110 va_list args;
111 char *msg;
113 va_start(args, fs);
114 msg = g_strdup_vprintf(fs, args);
115 va_end(args);
117 if (s->session) {
118 char *ssh_err;
119 int ssh_err_code;
121 /* This is not an errno. See <libssh2.h>. */
122 ssh_err_code = libssh2_session_last_error(s->session,
123 &ssh_err, NULL, 0);
124 error_setg(errp, "%s: %s (libssh2 error code: %d)",
125 msg, ssh_err, ssh_err_code);
126 } else {
127 error_setg(errp, "%s", msg);
129 g_free(msg);
132 static void GCC_FMT_ATTR(3, 4)
133 sftp_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...)
135 va_list args;
136 char *msg;
138 va_start(args, fs);
139 msg = g_strdup_vprintf(fs, args);
140 va_end(args);
142 if (s->sftp) {
143 char *ssh_err;
144 int ssh_err_code;
145 unsigned long sftp_err_code;
147 /* This is not an errno. See <libssh2.h>. */
148 ssh_err_code = libssh2_session_last_error(s->session,
149 &ssh_err, NULL, 0);
150 /* See <libssh2_sftp.h>. */
151 sftp_err_code = libssh2_sftp_last_error((s)->sftp);
153 error_setg(errp,
154 "%s: %s (libssh2 error code: %d, sftp error code: %lu)",
155 msg, ssh_err, ssh_err_code, sftp_err_code);
156 } else {
157 error_setg(errp, "%s", msg);
159 g_free(msg);
162 static void sftp_error_trace(BDRVSSHState *s, const char *op)
164 char *ssh_err;
165 int ssh_err_code;
166 unsigned long sftp_err_code;
168 /* This is not an errno. See <libssh2.h>. */
169 ssh_err_code = libssh2_session_last_error(s->session,
170 &ssh_err, NULL, 0);
171 /* See <libssh2_sftp.h>. */
172 sftp_err_code = libssh2_sftp_last_error((s)->sftp);
174 trace_sftp_error(op, ssh_err, ssh_err_code, sftp_err_code);
177 static int parse_uri(const char *filename, QDict *options, Error **errp)
179 URI *uri = NULL;
180 QueryParams *qp;
181 char *port_str;
182 int i;
184 uri = uri_parse(filename);
185 if (!uri) {
186 return -EINVAL;
189 if (g_strcmp0(uri->scheme, "ssh") != 0) {
190 error_setg(errp, "URI scheme must be 'ssh'");
191 goto err;
194 if (!uri->server || strcmp(uri->server, "") == 0) {
195 error_setg(errp, "missing hostname in URI");
196 goto err;
199 if (!uri->path || strcmp(uri->path, "") == 0) {
200 error_setg(errp, "missing remote path in URI");
201 goto err;
204 qp = query_params_parse(uri->query);
205 if (!qp) {
206 error_setg(errp, "could not parse query parameters");
207 goto err;
210 if(uri->user && strcmp(uri->user, "") != 0) {
211 qdict_put_str(options, "user", uri->user);
214 qdict_put_str(options, "server.host", uri->server);
216 port_str = g_strdup_printf("%d", uri->port ?: 22);
217 qdict_put_str(options, "server.port", port_str);
218 g_free(port_str);
220 qdict_put_str(options, "path", uri->path);
222 /* Pick out any query parameters that we understand, and ignore
223 * the rest.
225 for (i = 0; i < qp->n; ++i) {
226 if (strcmp(qp->p[i].name, "host_key_check") == 0) {
227 qdict_put_str(options, "host_key_check", qp->p[i].value);
231 query_params_free(qp);
232 uri_free(uri);
233 return 0;
235 err:
236 if (uri) {
237 uri_free(uri);
239 return -EINVAL;
242 static bool ssh_has_filename_options_conflict(QDict *options, Error **errp)
244 const QDictEntry *qe;
246 for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
247 if (!strcmp(qe->key, "host") ||
248 !strcmp(qe->key, "port") ||
249 !strcmp(qe->key, "path") ||
250 !strcmp(qe->key, "user") ||
251 !strcmp(qe->key, "host_key_check") ||
252 strstart(qe->key, "server.", NULL))
254 error_setg(errp, "Option '%s' cannot be used with a file name",
255 qe->key);
256 return true;
260 return false;
263 static void ssh_parse_filename(const char *filename, QDict *options,
264 Error **errp)
266 if (ssh_has_filename_options_conflict(options, errp)) {
267 return;
270 parse_uri(filename, options, errp);
273 static int check_host_key_knownhosts(BDRVSSHState *s,
274 const char *host, int port, Error **errp)
276 const char *home;
277 char *knh_file = NULL;
278 LIBSSH2_KNOWNHOSTS *knh = NULL;
279 struct libssh2_knownhost *found;
280 int ret, r;
281 const char *hostkey;
282 size_t len;
283 int type;
285 hostkey = libssh2_session_hostkey(s->session, &len, &type);
286 if (!hostkey) {
287 ret = -EINVAL;
288 session_error_setg(errp, s, "failed to read remote host key");
289 goto out;
292 knh = libssh2_knownhost_init(s->session);
293 if (!knh) {
294 ret = -EINVAL;
295 session_error_setg(errp, s,
296 "failed to initialize known hosts support");
297 goto out;
300 home = getenv("HOME");
301 if (home) {
302 knh_file = g_strdup_printf("%s/.ssh/known_hosts", home);
303 } else {
304 knh_file = g_strdup_printf("/root/.ssh/known_hosts");
307 /* Read all known hosts from OpenSSH-style known_hosts file. */
308 libssh2_knownhost_readfile(knh, knh_file, LIBSSH2_KNOWNHOST_FILE_OPENSSH);
310 r = libssh2_knownhost_checkp(knh, host, port, hostkey, len,
311 LIBSSH2_KNOWNHOST_TYPE_PLAIN|
312 LIBSSH2_KNOWNHOST_KEYENC_RAW,
313 &found);
314 switch (r) {
315 case LIBSSH2_KNOWNHOST_CHECK_MATCH:
316 /* OK */
317 trace_ssh_check_host_key_knownhosts(found->key);
318 break;
319 case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:
320 ret = -EINVAL;
321 session_error_setg(errp, s,
322 "host key does not match the one in known_hosts"
323 " (found key %s)", found->key);
324 goto out;
325 case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:
326 ret = -EINVAL;
327 session_error_setg(errp, s, "no host key was found in known_hosts");
328 goto out;
329 case LIBSSH2_KNOWNHOST_CHECK_FAILURE:
330 ret = -EINVAL;
331 session_error_setg(errp, s,
332 "failure matching the host key with known_hosts");
333 goto out;
334 default:
335 ret = -EINVAL;
336 session_error_setg(errp, s, "unknown error matching the host key"
337 " with known_hosts (%d)", r);
338 goto out;
341 /* known_hosts checking successful. */
342 ret = 0;
344 out:
345 if (knh != NULL) {
346 libssh2_knownhost_free(knh);
348 g_free(knh_file);
349 return ret;
352 static unsigned hex2decimal(char ch)
354 if (ch >= '0' && ch <= '9') {
355 return (ch - '0');
356 } else if (ch >= 'a' && ch <= 'f') {
357 return 10 + (ch - 'a');
358 } else if (ch >= 'A' && ch <= 'F') {
359 return 10 + (ch - 'A');
362 return -1;
365 /* Compare the binary fingerprint (hash of host key) with the
366 * host_key_check parameter.
368 static int compare_fingerprint(const unsigned char *fingerprint, size_t len,
369 const char *host_key_check)
371 unsigned c;
373 while (len > 0) {
374 while (*host_key_check == ':')
375 host_key_check++;
376 if (!qemu_isxdigit(host_key_check[0]) ||
377 !qemu_isxdigit(host_key_check[1]))
378 return 1;
379 c = hex2decimal(host_key_check[0]) * 16 +
380 hex2decimal(host_key_check[1]);
381 if (c - *fingerprint != 0)
382 return c - *fingerprint;
383 fingerprint++;
384 len--;
385 host_key_check += 2;
387 return *host_key_check - '\0';
390 static int
391 check_host_key_hash(BDRVSSHState *s, const char *hash,
392 int hash_type, size_t fingerprint_len, Error **errp)
394 const char *fingerprint;
396 fingerprint = libssh2_hostkey_hash(s->session, hash_type);
397 if (!fingerprint) {
398 session_error_setg(errp, s, "failed to read remote host key");
399 return -EINVAL;
402 if(compare_fingerprint((unsigned char *) fingerprint, fingerprint_len,
403 hash) != 0) {
404 error_setg(errp, "remote host key does not match host_key_check '%s'",
405 hash);
406 return -EPERM;
409 return 0;
412 static int check_host_key(BDRVSSHState *s, const char *host, int port,
413 SshHostKeyCheck *hkc, Error **errp)
415 SshHostKeyCheckMode mode;
417 if (hkc) {
418 mode = hkc->mode;
419 } else {
420 mode = SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS;
423 switch (mode) {
424 case SSH_HOST_KEY_CHECK_MODE_NONE:
425 return 0;
426 case SSH_HOST_KEY_CHECK_MODE_HASH:
427 if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_MD5) {
428 return check_host_key_hash(s, hkc->u.hash.hash,
429 LIBSSH2_HOSTKEY_HASH_MD5, 16, errp);
430 } else if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_SHA1) {
431 return check_host_key_hash(s, hkc->u.hash.hash,
432 LIBSSH2_HOSTKEY_HASH_SHA1, 20, errp);
434 g_assert_not_reached();
435 break;
436 case SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS:
437 return check_host_key_knownhosts(s, host, port, errp);
438 default:
439 g_assert_not_reached();
442 return -EINVAL;
445 static int authenticate(BDRVSSHState *s, const char *user, Error **errp)
447 int r, ret;
448 const char *userauthlist;
449 LIBSSH2_AGENT *agent = NULL;
450 struct libssh2_agent_publickey *identity;
451 struct libssh2_agent_publickey *prev_identity = NULL;
453 userauthlist = libssh2_userauth_list(s->session, user, strlen(user));
454 if (strstr(userauthlist, "publickey") == NULL) {
455 ret = -EPERM;
456 error_setg(errp,
457 "remote server does not support \"publickey\" authentication");
458 goto out;
461 /* Connect to ssh-agent and try each identity in turn. */
462 agent = libssh2_agent_init(s->session);
463 if (!agent) {
464 ret = -EINVAL;
465 session_error_setg(errp, s, "failed to initialize ssh-agent support");
466 goto out;
468 if (libssh2_agent_connect(agent)) {
469 ret = -ECONNREFUSED;
470 session_error_setg(errp, s, "failed to connect to ssh-agent");
471 goto out;
473 if (libssh2_agent_list_identities(agent)) {
474 ret = -EINVAL;
475 session_error_setg(errp, s,
476 "failed requesting identities from ssh-agent");
477 goto out;
480 for(;;) {
481 r = libssh2_agent_get_identity(agent, &identity, prev_identity);
482 if (r == 1) { /* end of list */
483 break;
485 if (r < 0) {
486 ret = -EINVAL;
487 session_error_setg(errp, s,
488 "failed to obtain identity from ssh-agent");
489 goto out;
491 r = libssh2_agent_userauth(agent, user, identity);
492 if (r == 0) {
493 /* Authenticated! */
494 ret = 0;
495 goto out;
497 /* Failed to authenticate with this identity, try the next one. */
498 prev_identity = identity;
501 ret = -EPERM;
502 error_setg(errp, "failed to authenticate using publickey authentication "
503 "and the identities held by your ssh-agent");
505 out:
506 if (agent != NULL) {
507 /* Note: libssh2 implementation implicitly calls
508 * libssh2_agent_disconnect if necessary.
510 libssh2_agent_free(agent);
513 return ret;
516 static QemuOptsList ssh_runtime_opts = {
517 .name = "ssh",
518 .head = QTAILQ_HEAD_INITIALIZER(ssh_runtime_opts.head),
519 .desc = {
521 .name = "host",
522 .type = QEMU_OPT_STRING,
523 .help = "Host to connect to",
526 .name = "port",
527 .type = QEMU_OPT_NUMBER,
528 .help = "Port to connect to",
531 .name = "host_key_check",
532 .type = QEMU_OPT_STRING,
533 .help = "Defines how and what to check the host key against",
535 { /* end of list */ }
539 static bool ssh_process_legacy_options(QDict *output_opts,
540 QemuOpts *legacy_opts,
541 Error **errp)
543 const char *host = qemu_opt_get(legacy_opts, "host");
544 const char *port = qemu_opt_get(legacy_opts, "port");
545 const char *host_key_check = qemu_opt_get(legacy_opts, "host_key_check");
547 if (!host && port) {
548 error_setg(errp, "port may not be used without host");
549 return false;
552 if (host) {
553 qdict_put_str(output_opts, "server.host", host);
554 qdict_put_str(output_opts, "server.port", port ?: stringify(22));
557 if (host_key_check) {
558 if (strcmp(host_key_check, "no") == 0) {
559 qdict_put_str(output_opts, "host-key-check.mode", "none");
560 } else if (strncmp(host_key_check, "md5:", 4) == 0) {
561 qdict_put_str(output_opts, "host-key-check.mode", "hash");
562 qdict_put_str(output_opts, "host-key-check.type", "md5");
563 qdict_put_str(output_opts, "host-key-check.hash",
564 &host_key_check[4]);
565 } else if (strncmp(host_key_check, "sha1:", 5) == 0) {
566 qdict_put_str(output_opts, "host-key-check.mode", "hash");
567 qdict_put_str(output_opts, "host-key-check.type", "sha1");
568 qdict_put_str(output_opts, "host-key-check.hash",
569 &host_key_check[5]);
570 } else if (strcmp(host_key_check, "yes") == 0) {
571 qdict_put_str(output_opts, "host-key-check.mode", "known_hosts");
572 } else {
573 error_setg(errp, "unknown host_key_check setting (%s)",
574 host_key_check);
575 return false;
579 return true;
582 static BlockdevOptionsSsh *ssh_parse_options(QDict *options, Error **errp)
584 BlockdevOptionsSsh *result = NULL;
585 QemuOpts *opts = NULL;
586 Error *local_err = NULL;
587 const QDictEntry *e;
588 Visitor *v;
590 /* Translate legacy options */
591 opts = qemu_opts_create(&ssh_runtime_opts, NULL, 0, &error_abort);
592 qemu_opts_absorb_qdict(opts, options, &local_err);
593 if (local_err) {
594 error_propagate(errp, local_err);
595 goto fail;
598 if (!ssh_process_legacy_options(options, opts, errp)) {
599 goto fail;
602 /* Create the QAPI object */
603 v = qobject_input_visitor_new_flat_confused(options, errp);
604 if (!v) {
605 goto fail;
608 visit_type_BlockdevOptionsSsh(v, NULL, &result, &local_err);
609 visit_free(v);
611 if (local_err) {
612 error_propagate(errp, local_err);
613 goto fail;
616 /* Remove the processed options from the QDict (the visitor processes
617 * _all_ options in the QDict) */
618 while ((e = qdict_first(options))) {
619 qdict_del(options, e->key);
622 fail:
623 qemu_opts_del(opts);
624 return result;
627 static int connect_to_ssh(BDRVSSHState *s, BlockdevOptionsSsh *opts,
628 int ssh_flags, int creat_mode, Error **errp)
630 int r, ret;
631 const char *user;
632 long port = 0;
634 if (opts->has_user) {
635 user = opts->user;
636 } else {
637 user = g_get_user_name();
638 if (!user) {
639 error_setg_errno(errp, errno, "Can't get user name");
640 ret = -errno;
641 goto err;
645 /* Pop the config into our state object, Exit if invalid */
646 s->inet = opts->server;
647 opts->server = NULL;
649 if (qemu_strtol(s->inet->port, NULL, 10, &port) < 0) {
650 error_setg(errp, "Use only numeric port value");
651 ret = -EINVAL;
652 goto err;
655 /* Open the socket and connect. */
656 s->sock = inet_connect_saddr(s->inet, errp);
657 if (s->sock < 0) {
658 ret = -EIO;
659 goto err;
662 /* Create SSH session. */
663 s->session = libssh2_session_init();
664 if (!s->session) {
665 ret = -EINVAL;
666 session_error_setg(errp, s, "failed to initialize libssh2 session");
667 goto err;
670 #if TRACE_LIBSSH2 != 0
671 libssh2_trace(s->session, TRACE_LIBSSH2);
672 #endif
674 r = libssh2_session_handshake(s->session, s->sock);
675 if (r != 0) {
676 ret = -EINVAL;
677 session_error_setg(errp, s, "failed to establish SSH session");
678 goto err;
681 /* Check the remote host's key against known_hosts. */
682 ret = check_host_key(s, s->inet->host, port, opts->host_key_check, errp);
683 if (ret < 0) {
684 goto err;
687 /* Authenticate. */
688 ret = authenticate(s, user, errp);
689 if (ret < 0) {
690 goto err;
693 /* Start SFTP. */
694 s->sftp = libssh2_sftp_init(s->session);
695 if (!s->sftp) {
696 session_error_setg(errp, s, "failed to initialize sftp handle");
697 ret = -EINVAL;
698 goto err;
701 /* Open the remote file. */
702 trace_ssh_connect_to_ssh(opts->path, ssh_flags, creat_mode);
703 s->sftp_handle = libssh2_sftp_open(s->sftp, opts->path, ssh_flags,
704 creat_mode);
705 if (!s->sftp_handle) {
706 session_error_setg(errp, s, "failed to open remote file '%s'",
707 opts->path);
708 ret = -EINVAL;
709 goto err;
712 r = libssh2_sftp_fstat(s->sftp_handle, &s->attrs);
713 if (r < 0) {
714 sftp_error_setg(errp, s, "failed to read file attributes");
715 return -EINVAL;
718 return 0;
720 err:
721 if (s->sftp_handle) {
722 libssh2_sftp_close(s->sftp_handle);
724 s->sftp_handle = NULL;
725 if (s->sftp) {
726 libssh2_sftp_shutdown(s->sftp);
728 s->sftp = NULL;
729 if (s->session) {
730 libssh2_session_disconnect(s->session,
731 "from qemu ssh client: "
732 "error opening connection");
733 libssh2_session_free(s->session);
735 s->session = NULL;
737 return ret;
740 static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags,
741 Error **errp)
743 BDRVSSHState *s = bs->opaque;
744 BlockdevOptionsSsh *opts;
745 int ret;
746 int ssh_flags;
748 ssh_state_init(s);
750 ssh_flags = LIBSSH2_FXF_READ;
751 if (bdrv_flags & BDRV_O_RDWR) {
752 ssh_flags |= LIBSSH2_FXF_WRITE;
755 opts = ssh_parse_options(options, errp);
756 if (opts == NULL) {
757 return -EINVAL;
760 /* Start up SSH. */
761 ret = connect_to_ssh(s, opts, ssh_flags, 0, errp);
762 if (ret < 0) {
763 goto err;
766 /* Go non-blocking. */
767 libssh2_session_set_blocking(s->session, 0);
769 qapi_free_BlockdevOptionsSsh(opts);
771 return 0;
773 err:
774 if (s->sock >= 0) {
775 close(s->sock);
777 s->sock = -1;
779 qapi_free_BlockdevOptionsSsh(opts);
781 return ret;
784 /* Note: This is a blocking operation */
785 static int ssh_grow_file(BDRVSSHState *s, int64_t offset, Error **errp)
787 ssize_t ret;
788 char c[1] = { '\0' };
789 int was_blocking = libssh2_session_get_blocking(s->session);
791 /* offset must be strictly greater than the current size so we do
792 * not overwrite anything */
793 assert(offset > 0 && offset > s->attrs.filesize);
795 libssh2_session_set_blocking(s->session, 1);
797 libssh2_sftp_seek64(s->sftp_handle, offset - 1);
798 ret = libssh2_sftp_write(s->sftp_handle, c, 1);
800 libssh2_session_set_blocking(s->session, was_blocking);
802 if (ret < 0) {
803 sftp_error_setg(errp, s, "Failed to grow file");
804 return -EIO;
807 s->attrs.filesize = offset;
808 return 0;
811 static QemuOptsList ssh_create_opts = {
812 .name = "ssh-create-opts",
813 .head = QTAILQ_HEAD_INITIALIZER(ssh_create_opts.head),
814 .desc = {
816 .name = BLOCK_OPT_SIZE,
817 .type = QEMU_OPT_SIZE,
818 .help = "Virtual disk size"
820 { /* end of list */ }
824 static int ssh_co_create(BlockdevCreateOptions *options, Error **errp)
826 BlockdevCreateOptionsSsh *opts = &options->u.ssh;
827 BDRVSSHState s;
828 int ret;
830 assert(options->driver == BLOCKDEV_DRIVER_SSH);
832 ssh_state_init(&s);
834 ret = connect_to_ssh(&s, opts->location,
835 LIBSSH2_FXF_READ|LIBSSH2_FXF_WRITE|
836 LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
837 0644, errp);
838 if (ret < 0) {
839 goto fail;
842 if (opts->size > 0) {
843 ret = ssh_grow_file(&s, opts->size, errp);
844 if (ret < 0) {
845 goto fail;
849 ret = 0;
850 fail:
851 ssh_state_free(&s);
852 return ret;
855 static int coroutine_fn ssh_co_create_opts(const char *filename, QemuOpts *opts,
856 Error **errp)
858 BlockdevCreateOptions *create_options;
859 BlockdevCreateOptionsSsh *ssh_opts;
860 int ret;
861 QDict *uri_options = NULL;
863 create_options = g_new0(BlockdevCreateOptions, 1);
864 create_options->driver = BLOCKDEV_DRIVER_SSH;
865 ssh_opts = &create_options->u.ssh;
867 /* Get desired file size. */
868 ssh_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
869 BDRV_SECTOR_SIZE);
870 trace_ssh_co_create_opts(ssh_opts->size);
872 uri_options = qdict_new();
873 ret = parse_uri(filename, uri_options, errp);
874 if (ret < 0) {
875 goto out;
878 ssh_opts->location = ssh_parse_options(uri_options, errp);
879 if (ssh_opts->location == NULL) {
880 ret = -EINVAL;
881 goto out;
884 ret = ssh_co_create(create_options, errp);
886 out:
887 qobject_unref(uri_options);
888 qapi_free_BlockdevCreateOptions(create_options);
889 return ret;
892 static void ssh_close(BlockDriverState *bs)
894 BDRVSSHState *s = bs->opaque;
896 ssh_state_free(s);
899 static int ssh_has_zero_init(BlockDriverState *bs)
901 BDRVSSHState *s = bs->opaque;
902 /* Assume false, unless we can positively prove it's true. */
903 int has_zero_init = 0;
905 if (s->attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
906 if (s->attrs.permissions & LIBSSH2_SFTP_S_IFREG) {
907 has_zero_init = 1;
911 return has_zero_init;
914 typedef struct BDRVSSHRestart {
915 BlockDriverState *bs;
916 Coroutine *co;
917 } BDRVSSHRestart;
919 static void restart_coroutine(void *opaque)
921 BDRVSSHRestart *restart = opaque;
922 BlockDriverState *bs = restart->bs;
923 BDRVSSHState *s = bs->opaque;
924 AioContext *ctx = bdrv_get_aio_context(bs);
926 trace_ssh_restart_coroutine(restart->co);
927 aio_set_fd_handler(ctx, s->sock, false, NULL, NULL, NULL, NULL);
929 aio_co_wake(restart->co);
932 /* A non-blocking call returned EAGAIN, so yield, ensuring the
933 * handlers are set up so that we'll be rescheduled when there is an
934 * interesting event on the socket.
936 static coroutine_fn void co_yield(BDRVSSHState *s, BlockDriverState *bs)
938 int r;
939 IOHandler *rd_handler = NULL, *wr_handler = NULL;
940 BDRVSSHRestart restart = {
941 .bs = bs,
942 .co = qemu_coroutine_self()
945 r = libssh2_session_block_directions(s->session);
947 if (r & LIBSSH2_SESSION_BLOCK_INBOUND) {
948 rd_handler = restart_coroutine;
950 if (r & LIBSSH2_SESSION_BLOCK_OUTBOUND) {
951 wr_handler = restart_coroutine;
954 trace_ssh_co_yield(s->sock, rd_handler, wr_handler);
956 aio_set_fd_handler(bdrv_get_aio_context(bs), s->sock,
957 false, rd_handler, wr_handler, NULL, &restart);
958 qemu_coroutine_yield();
959 trace_ssh_co_yield_back(s->sock);
962 /* SFTP has a function `libssh2_sftp_seek64' which seeks to a position
963 * in the remote file. Notice that it just updates a field in the
964 * sftp_handle structure, so there is no network traffic and it cannot
965 * fail.
967 * However, `libssh2_sftp_seek64' does have a catastrophic effect on
968 * performance since it causes the handle to throw away all in-flight
969 * reads and buffered readahead data. Therefore this function tries
970 * to be intelligent about when to call the underlying libssh2 function.
972 #define SSH_SEEK_WRITE 0
973 #define SSH_SEEK_READ 1
974 #define SSH_SEEK_FORCE 2
976 static void ssh_seek(BDRVSSHState *s, int64_t offset, int flags)
978 bool op_read = (flags & SSH_SEEK_READ) != 0;
979 bool force = (flags & SSH_SEEK_FORCE) != 0;
981 if (force || op_read != s->offset_op_read || offset != s->offset) {
982 trace_ssh_seek(offset);
983 libssh2_sftp_seek64(s->sftp_handle, offset);
984 s->offset = offset;
985 s->offset_op_read = op_read;
989 static coroutine_fn int ssh_read(BDRVSSHState *s, BlockDriverState *bs,
990 int64_t offset, size_t size,
991 QEMUIOVector *qiov)
993 ssize_t r;
994 size_t got;
995 char *buf, *end_of_vec;
996 struct iovec *i;
998 trace_ssh_read(offset, size);
1000 ssh_seek(s, offset, SSH_SEEK_READ);
1002 /* This keeps track of the current iovec element ('i'), where we
1003 * will write to next ('buf'), and the end of the current iovec
1004 * ('end_of_vec').
1006 i = &qiov->iov[0];
1007 buf = i->iov_base;
1008 end_of_vec = i->iov_base + i->iov_len;
1010 /* libssh2 has a hard-coded limit of 2000 bytes per request,
1011 * although it will also do readahead behind our backs. Therefore
1012 * we may have to do repeated reads here until we have read 'size'
1013 * bytes.
1015 for (got = 0; got < size; ) {
1016 again:
1017 trace_ssh_read_buf(buf, end_of_vec - buf);
1018 r = libssh2_sftp_read(s->sftp_handle, buf, end_of_vec - buf);
1019 trace_ssh_read_return(r);
1021 if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
1022 co_yield(s, bs);
1023 goto again;
1025 if (r < 0) {
1026 sftp_error_trace(s, "read");
1027 s->offset = -1;
1028 return -EIO;
1030 if (r == 0) {
1031 /* EOF: Short read so pad the buffer with zeroes and return it. */
1032 qemu_iovec_memset(qiov, got, 0, size - got);
1033 return 0;
1036 got += r;
1037 buf += r;
1038 s->offset += r;
1039 if (buf >= end_of_vec && got < size) {
1040 i++;
1041 buf = i->iov_base;
1042 end_of_vec = i->iov_base + i->iov_len;
1046 return 0;
1049 static coroutine_fn int ssh_co_readv(BlockDriverState *bs,
1050 int64_t sector_num,
1051 int nb_sectors, QEMUIOVector *qiov)
1053 BDRVSSHState *s = bs->opaque;
1054 int ret;
1056 qemu_co_mutex_lock(&s->lock);
1057 ret = ssh_read(s, bs, sector_num * BDRV_SECTOR_SIZE,
1058 nb_sectors * BDRV_SECTOR_SIZE, qiov);
1059 qemu_co_mutex_unlock(&s->lock);
1061 return ret;
1064 static int ssh_write(BDRVSSHState *s, BlockDriverState *bs,
1065 int64_t offset, size_t size,
1066 QEMUIOVector *qiov)
1068 ssize_t r;
1069 size_t written;
1070 char *buf, *end_of_vec;
1071 struct iovec *i;
1073 trace_ssh_write(offset, size);
1075 ssh_seek(s, offset, SSH_SEEK_WRITE);
1077 /* This keeps track of the current iovec element ('i'), where we
1078 * will read from next ('buf'), and the end of the current iovec
1079 * ('end_of_vec').
1081 i = &qiov->iov[0];
1082 buf = i->iov_base;
1083 end_of_vec = i->iov_base + i->iov_len;
1085 for (written = 0; written < size; ) {
1086 again:
1087 trace_ssh_write_buf(buf, end_of_vec - buf);
1088 r = libssh2_sftp_write(s->sftp_handle, buf, end_of_vec - buf);
1089 trace_ssh_write_return(r);
1091 if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
1092 co_yield(s, bs);
1093 goto again;
1095 if (r < 0) {
1096 sftp_error_trace(s, "write");
1097 s->offset = -1;
1098 return -EIO;
1100 /* The libssh2 API is very unclear about this. A comment in
1101 * the code says "nothing was acked, and no EAGAIN was
1102 * received!" which apparently means that no data got sent
1103 * out, and the underlying channel didn't return any EAGAIN
1104 * indication. I think this is a bug in either libssh2 or
1105 * OpenSSH (server-side). In any case, forcing a seek (to
1106 * discard libssh2 internal buffers), and then trying again
1107 * works for me.
1109 if (r == 0) {
1110 ssh_seek(s, offset + written, SSH_SEEK_WRITE|SSH_SEEK_FORCE);
1111 co_yield(s, bs);
1112 goto again;
1115 written += r;
1116 buf += r;
1117 s->offset += r;
1118 if (buf >= end_of_vec && written < size) {
1119 i++;
1120 buf = i->iov_base;
1121 end_of_vec = i->iov_base + i->iov_len;
1124 if (offset + written > s->attrs.filesize)
1125 s->attrs.filesize = offset + written;
1128 return 0;
1131 static coroutine_fn int ssh_co_writev(BlockDriverState *bs,
1132 int64_t sector_num,
1133 int nb_sectors, QEMUIOVector *qiov,
1134 int flags)
1136 BDRVSSHState *s = bs->opaque;
1137 int ret;
1139 assert(!flags);
1140 qemu_co_mutex_lock(&s->lock);
1141 ret = ssh_write(s, bs, sector_num * BDRV_SECTOR_SIZE,
1142 nb_sectors * BDRV_SECTOR_SIZE, qiov);
1143 qemu_co_mutex_unlock(&s->lock);
1145 return ret;
1148 static void unsafe_flush_warning(BDRVSSHState *s, const char *what)
1150 if (!s->unsafe_flush_warning) {
1151 warn_report("ssh server %s does not support fsync",
1152 s->inet->host);
1153 if (what) {
1154 error_report("to support fsync, you need %s", what);
1156 s->unsafe_flush_warning = true;
1160 #ifdef HAS_LIBSSH2_SFTP_FSYNC
1162 static coroutine_fn int ssh_flush(BDRVSSHState *s, BlockDriverState *bs)
1164 int r;
1166 trace_ssh_flush();
1167 again:
1168 r = libssh2_sftp_fsync(s->sftp_handle);
1169 if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
1170 co_yield(s, bs);
1171 goto again;
1173 if (r == LIBSSH2_ERROR_SFTP_PROTOCOL &&
1174 libssh2_sftp_last_error(s->sftp) == LIBSSH2_FX_OP_UNSUPPORTED) {
1175 unsafe_flush_warning(s, "OpenSSH >= 6.3");
1176 return 0;
1178 if (r < 0) {
1179 sftp_error_trace(s, "fsync");
1180 return -EIO;
1183 return 0;
1186 static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
1188 BDRVSSHState *s = bs->opaque;
1189 int ret;
1191 qemu_co_mutex_lock(&s->lock);
1192 ret = ssh_flush(s, bs);
1193 qemu_co_mutex_unlock(&s->lock);
1195 return ret;
1198 #else /* !HAS_LIBSSH2_SFTP_FSYNC */
1200 static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
1202 BDRVSSHState *s = bs->opaque;
1204 unsafe_flush_warning(s, "libssh2 >= 1.4.4");
1205 return 0;
1208 #endif /* !HAS_LIBSSH2_SFTP_FSYNC */
1210 static int64_t ssh_getlength(BlockDriverState *bs)
1212 BDRVSSHState *s = bs->opaque;
1213 int64_t length;
1215 /* Note we cannot make a libssh2 call here. */
1216 length = (int64_t) s->attrs.filesize;
1217 trace_ssh_getlength(length);
1219 return length;
1222 static int coroutine_fn ssh_co_truncate(BlockDriverState *bs, int64_t offset,
1223 PreallocMode prealloc, Error **errp)
1225 BDRVSSHState *s = bs->opaque;
1227 if (prealloc != PREALLOC_MODE_OFF) {
1228 error_setg(errp, "Unsupported preallocation mode '%s'",
1229 PreallocMode_str(prealloc));
1230 return -ENOTSUP;
1233 if (offset < s->attrs.filesize) {
1234 error_setg(errp, "ssh driver does not support shrinking files");
1235 return -ENOTSUP;
1238 if (offset == s->attrs.filesize) {
1239 return 0;
1242 return ssh_grow_file(s, offset, errp);
1245 static const char *const ssh_strong_runtime_opts[] = {
1246 "host",
1247 "port",
1248 "path",
1249 "user",
1250 "host_key_check",
1251 "server.",
1253 NULL
1256 static BlockDriver bdrv_ssh = {
1257 .format_name = "ssh",
1258 .protocol_name = "ssh",
1259 .instance_size = sizeof(BDRVSSHState),
1260 .bdrv_parse_filename = ssh_parse_filename,
1261 .bdrv_file_open = ssh_file_open,
1262 .bdrv_co_create = ssh_co_create,
1263 .bdrv_co_create_opts = ssh_co_create_opts,
1264 .bdrv_close = ssh_close,
1265 .bdrv_has_zero_init = ssh_has_zero_init,
1266 .bdrv_co_readv = ssh_co_readv,
1267 .bdrv_co_writev = ssh_co_writev,
1268 .bdrv_getlength = ssh_getlength,
1269 .bdrv_co_truncate = ssh_co_truncate,
1270 .bdrv_co_flush_to_disk = ssh_co_flush,
1271 .create_opts = &ssh_create_opts,
1272 .strong_runtime_opts = ssh_strong_runtime_opts,
1275 static void bdrv_ssh_init(void)
1277 int r;
1279 r = libssh2_init(0);
1280 if (r != 0) {
1281 fprintf(stderr, "libssh2 initialization failed, %d\n", r);
1282 exit(EXIT_FAILURE);
1285 bdrv_register(&bdrv_ssh);
1288 block_init(bdrv_ssh_init);