xref: /qemu/block/ssh.c (revision b097ba37)
1 /*
2  * Secure Shell (ssh) backend for QEMU.
3  *
4  * Copyright (C) 2013 Red Hat Inc., Richard W.M. Jones <rjones@redhat.com>
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 #include "qemu/osdep.h"
26 
27 #include <libssh2.h>
28 #include <libssh2_sftp.h>
29 
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"
45 
46 /*
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
52  */
53 #define TRACE_LIBSSH2 0 /* or try: LIBSSH2_TRACE_SFTP */
54 
55 typedef struct BDRVSSHState {
56     /* Coroutine. */
57     CoMutex lock;
58 
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 */
64 
65     /* See ssh_seek() function below. */
66     int64_t offset;
67     bool offset_op_read;
68 
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).
71      */
72     LIBSSH2_SFTP_ATTRIBUTES attrs;
73 
74     InetSocketAddress *inet;
75 
76     /* Used to warn if 'flush' is not supported. */
77     bool unsafe_flush_warning;
78 } BDRVSSHState;
79 
80 static void ssh_state_init(BDRVSSHState *s)
81 {
82     memset(s, 0, sizeof *s);
83     s->sock = -1;
84     s->offset = -1;
85     qemu_co_mutex_init(&s->lock);
86 }
87 
88 static void ssh_state_free(BDRVSSHState *s)
89 {
90     if (s->sftp_handle) {
91         libssh2_sftp_close(s->sftp_handle);
92     }
93     if (s->sftp) {
94         libssh2_sftp_shutdown(s->sftp);
95     }
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);
101     }
102     if (s->sock >= 0) {
103         close(s->sock);
104     }
105 }
106 
107 static void GCC_FMT_ATTR(3, 4)
108 session_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...)
109 {
110     va_list args;
111     char *msg;
112 
113     va_start(args, fs);
114     msg = g_strdup_vprintf(fs, args);
115     va_end(args);
116 
117     if (s->session) {
118         char *ssh_err;
119         int ssh_err_code;
120 
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);
128     }
129     g_free(msg);
130 }
131 
132 static void GCC_FMT_ATTR(3, 4)
133 sftp_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...)
134 {
135     va_list args;
136     char *msg;
137 
138     va_start(args, fs);
139     msg = g_strdup_vprintf(fs, args);
140     va_end(args);
141 
142     if (s->sftp) {
143         char *ssh_err;
144         int ssh_err_code;
145         unsigned long sftp_err_code;
146 
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);
152 
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);
158     }
159     g_free(msg);
160 }
161 
162 static void sftp_error_trace(BDRVSSHState *s, const char *op)
163 {
164     char *ssh_err;
165     int ssh_err_code;
166     unsigned long sftp_err_code;
167 
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);
173 
174     trace_sftp_error(op, ssh_err, ssh_err_code, sftp_err_code);
175 }
176 
177 static int parse_uri(const char *filename, QDict *options, Error **errp)
178 {
179     URI *uri = NULL;
180     QueryParams *qp;
181     char *port_str;
182     int i;
183 
184     uri = uri_parse(filename);
185     if (!uri) {
186         return -EINVAL;
187     }
188 
189     if (g_strcmp0(uri->scheme, "ssh") != 0) {
190         error_setg(errp, "URI scheme must be 'ssh'");
191         goto err;
192     }
193 
194     if (!uri->server || strcmp(uri->server, "") == 0) {
195         error_setg(errp, "missing hostname in URI");
196         goto err;
197     }
198 
199     if (!uri->path || strcmp(uri->path, "") == 0) {
200         error_setg(errp, "missing remote path in URI");
201         goto err;
202     }
203 
204     qp = query_params_parse(uri->query);
205     if (!qp) {
206         error_setg(errp, "could not parse query parameters");
207         goto err;
208     }
209 
210     if(uri->user && strcmp(uri->user, "") != 0) {
211         qdict_put_str(options, "user", uri->user);
212     }
213 
214     qdict_put_str(options, "server.host", uri->server);
215 
216     port_str = g_strdup_printf("%d", uri->port ?: 22);
217     qdict_put_str(options, "server.port", port_str);
218     g_free(port_str);
219 
220     qdict_put_str(options, "path", uri->path);
221 
222     /* Pick out any query parameters that we understand, and ignore
223      * the rest.
224      */
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);
228         }
229     }
230 
231     query_params_free(qp);
232     uri_free(uri);
233     return 0;
234 
235  err:
236     if (uri) {
237       uri_free(uri);
238     }
239     return -EINVAL;
240 }
241 
242 static bool ssh_has_filename_options_conflict(QDict *options, Error **errp)
243 {
244     const QDictEntry *qe;
245 
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))
253         {
254             error_setg(errp, "Option '%s' cannot be used with a file name",
255                        qe->key);
256             return true;
257         }
258     }
259 
260     return false;
261 }
262 
263 static void ssh_parse_filename(const char *filename, QDict *options,
264                                Error **errp)
265 {
266     if (ssh_has_filename_options_conflict(options, errp)) {
267         return;
268     }
269 
270     parse_uri(filename, options, errp);
271 }
272 
273 static int check_host_key_knownhosts(BDRVSSHState *s,
274                                      const char *host, int port, Error **errp)
275 {
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;
284 
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;
290     }
291 
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;
298     }
299 
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");
305     }
306 
307     /* Read all known hosts from OpenSSH-style known_hosts file. */
308     libssh2_knownhost_readfile(knh, knh_file, LIBSSH2_KNOWNHOST_FILE_OPENSSH);
309 
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;
339     }
340 
341     /* known_hosts checking successful. */
342     ret = 0;
343 
344  out:
345     if (knh != NULL) {
346         libssh2_knownhost_free(knh);
347     }
348     g_free(knh_file);
349     return ret;
350 }
351 
352 static unsigned hex2decimal(char ch)
353 {
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');
360     }
361 
362     return -1;
363 }
364 
365 /* Compare the binary fingerprint (hash of host key) with the
366  * host_key_check parameter.
367  */
368 static int compare_fingerprint(const unsigned char *fingerprint, size_t len,
369                                const char *host_key_check)
370 {
371     unsigned c;
372 
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;
386     }
387     return *host_key_check - '\0';
388 }
389 
390 static int
391 check_host_key_hash(BDRVSSHState *s, const char *hash,
392                     int hash_type, size_t fingerprint_len, Error **errp)
393 {
394     const char *fingerprint;
395 
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;
400     }
401 
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;
407     }
408 
409     return 0;
410 }
411 
412 static int check_host_key(BDRVSSHState *s, const char *host, int port,
413                           SshHostKeyCheck *hkc, Error **errp)
414 {
415     SshHostKeyCheckMode mode;
416 
417     if (hkc) {
418         mode = hkc->mode;
419     } else {
420         mode = SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS;
421     }
422 
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);
433         }
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();
440     }
441 
442     return -EINVAL;
443 }
444 
445 static int authenticate(BDRVSSHState *s, const char *user, Error **errp)
446 {
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;
452 
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;
459     }
460 
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;
467     }
468     if (libssh2_agent_connect(agent)) {
469         ret = -ECONNREFUSED;
470         session_error_setg(errp, s, "failed to connect to ssh-agent");
471         goto out;
472     }
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;
478     }
479 
480     for(;;) {
481         r = libssh2_agent_get_identity(agent, &identity, prev_identity);
482         if (r == 1) {           /* end of list */
483             break;
484         }
485         if (r < 0) {
486             ret = -EINVAL;
487             session_error_setg(errp, s,
488                                "failed to obtain identity from ssh-agent");
489             goto out;
490         }
491         r = libssh2_agent_userauth(agent, user, identity);
492         if (r == 0) {
493             /* Authenticated! */
494             ret = 0;
495             goto out;
496         }
497         /* Failed to authenticate with this identity, try the next one. */
498         prev_identity = identity;
499     }
500 
501     ret = -EPERM;
502     error_setg(errp, "failed to authenticate using publickey authentication "
503                "and the identities held by your ssh-agent");
504 
505  out:
506     if (agent != NULL) {
507         /* Note: libssh2 implementation implicitly calls
508          * libssh2_agent_disconnect if necessary.
509          */
510         libssh2_agent_free(agent);
511     }
512 
513     return ret;
514 }
515 
516 static QemuOptsList ssh_runtime_opts = {
517     .name = "ssh",
518     .head = QTAILQ_HEAD_INITIALIZER(ssh_runtime_opts.head),
519     .desc = {
520         {
521             .name = "host",
522             .type = QEMU_OPT_STRING,
523             .help = "Host to connect to",
524         },
525         {
526             .name = "port",
527             .type = QEMU_OPT_NUMBER,
528             .help = "Port to connect to",
529         },
530         {
531             .name = "host_key_check",
532             .type = QEMU_OPT_STRING,
533             .help = "Defines how and what to check the host key against",
534         },
535         { /* end of list */ }
536     },
537 };
538 
539 static bool ssh_process_legacy_options(QDict *output_opts,
540                                        QemuOpts *legacy_opts,
541                                        Error **errp)
542 {
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");
546 
547     if (!host && port) {
548         error_setg(errp, "port may not be used without host");
549         return false;
550     }
551 
552     if (host) {
553         qdict_put_str(output_opts, "server.host", host);
554         qdict_put_str(output_opts, "server.port", port ?: stringify(22));
555     }
556 
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;
576         }
577     }
578 
579     return true;
580 }
581 
582 static BlockdevOptionsSsh *ssh_parse_options(QDict *options, Error **errp)
583 {
584     BlockdevOptionsSsh *result = NULL;
585     QemuOpts *opts = NULL;
586     Error *local_err = NULL;
587     const QDictEntry *e;
588     Visitor *v;
589 
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;
596     }
597 
598     if (!ssh_process_legacy_options(options, opts, errp)) {
599         goto fail;
600     }
601 
602     /* Create the QAPI object */
603     v = qobject_input_visitor_new_flat_confused(options, errp);
604     if (!v) {
605         goto fail;
606     }
607 
608     visit_type_BlockdevOptionsSsh(v, NULL, &result, &local_err);
609     visit_free(v);
610 
611     if (local_err) {
612         error_propagate(errp, local_err);
613         goto fail;
614     }
615 
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);
620     }
621 
622 fail:
623     qemu_opts_del(opts);
624     return result;
625 }
626 
627 static int connect_to_ssh(BDRVSSHState *s, BlockdevOptionsSsh *opts,
628                           int ssh_flags, int creat_mode, Error **errp)
629 {
630     int r, ret;
631     const char *user;
632     long port = 0;
633 
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;
642         }
643     }
644 
645     /* Pop the config into our state object, Exit if invalid */
646     s->inet = opts->server;
647     opts->server = NULL;
648 
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;
653     }
654 
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;
660     }
661 
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;
668     }
669 
670 #if TRACE_LIBSSH2 != 0
671     libssh2_trace(s->session, TRACE_LIBSSH2);
672 #endif
673 
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;
679     }
680 
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;
685     }
686 
687     /* Authenticate. */
688     ret = authenticate(s, user, errp);
689     if (ret < 0) {
690         goto err;
691     }
692 
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;
699     }
700 
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;
710     }
711 
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;
716     }
717 
718     return 0;
719 
720  err:
721     if (s->sftp_handle) {
722         libssh2_sftp_close(s->sftp_handle);
723     }
724     s->sftp_handle = NULL;
725     if (s->sftp) {
726         libssh2_sftp_shutdown(s->sftp);
727     }
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);
734     }
735     s->session = NULL;
736 
737     return ret;
738 }
739 
740 static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags,
741                          Error **errp)
742 {
743     BDRVSSHState *s = bs->opaque;
744     BlockdevOptionsSsh *opts;
745     int ret;
746     int ssh_flags;
747 
748     ssh_state_init(s);
749 
750     ssh_flags = LIBSSH2_FXF_READ;
751     if (bdrv_flags & BDRV_O_RDWR) {
752         ssh_flags |= LIBSSH2_FXF_WRITE;
753     }
754 
755     opts = ssh_parse_options(options, errp);
756     if (opts == NULL) {
757         return -EINVAL;
758     }
759 
760     /* Start up SSH. */
761     ret = connect_to_ssh(s, opts, ssh_flags, 0, errp);
762     if (ret < 0) {
763         goto err;
764     }
765 
766     /* Go non-blocking. */
767     libssh2_session_set_blocking(s->session, 0);
768 
769     qapi_free_BlockdevOptionsSsh(opts);
770 
771     return 0;
772 
773  err:
774     if (s->sock >= 0) {
775         close(s->sock);
776     }
777     s->sock = -1;
778 
779     qapi_free_BlockdevOptionsSsh(opts);
780 
781     return ret;
782 }
783 
784 /* Note: This is a blocking operation */
785 static int ssh_grow_file(BDRVSSHState *s, int64_t offset, Error **errp)
786 {
787     ssize_t ret;
788     char c[1] = { '\0' };
789     int was_blocking = libssh2_session_get_blocking(s->session);
790 
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);
794 
795     libssh2_session_set_blocking(s->session, 1);
796 
797     libssh2_sftp_seek64(s->sftp_handle, offset - 1);
798     ret = libssh2_sftp_write(s->sftp_handle, c, 1);
799 
800     libssh2_session_set_blocking(s->session, was_blocking);
801 
802     if (ret < 0) {
803         sftp_error_setg(errp, s, "Failed to grow file");
804         return -EIO;
805     }
806 
807     s->attrs.filesize = offset;
808     return 0;
809 }
810 
811 static QemuOptsList ssh_create_opts = {
812     .name = "ssh-create-opts",
813     .head = QTAILQ_HEAD_INITIALIZER(ssh_create_opts.head),
814     .desc = {
815         {
816             .name = BLOCK_OPT_SIZE,
817             .type = QEMU_OPT_SIZE,
818             .help = "Virtual disk size"
819         },
820         { /* end of list */ }
821     }
822 };
823 
824 static int ssh_co_create(BlockdevCreateOptions *options, Error **errp)
825 {
826     BlockdevCreateOptionsSsh *opts = &options->u.ssh;
827     BDRVSSHState s;
828     int ret;
829 
830     assert(options->driver == BLOCKDEV_DRIVER_SSH);
831 
832     ssh_state_init(&s);
833 
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;
840     }
841 
842     if (opts->size > 0) {
843         ret = ssh_grow_file(&s, opts->size, errp);
844         if (ret < 0) {
845             goto fail;
846         }
847     }
848 
849     ret = 0;
850 fail:
851     ssh_state_free(&s);
852     return ret;
853 }
854 
855 static int coroutine_fn ssh_co_create_opts(const char *filename, QemuOpts *opts,
856                                            Error **errp)
857 {
858     BlockdevCreateOptions *create_options;
859     BlockdevCreateOptionsSsh *ssh_opts;
860     int ret;
861     QDict *uri_options = NULL;
862 
863     create_options = g_new0(BlockdevCreateOptions, 1);
864     create_options->driver = BLOCKDEV_DRIVER_SSH;
865     ssh_opts = &create_options->u.ssh;
866 
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);
871 
872     uri_options = qdict_new();
873     ret = parse_uri(filename, uri_options, errp);
874     if (ret < 0) {
875         goto out;
876     }
877 
878     ssh_opts->location = ssh_parse_options(uri_options, errp);
879     if (ssh_opts->location == NULL) {
880         ret = -EINVAL;
881         goto out;
882     }
883 
884     ret = ssh_co_create(create_options, errp);
885 
886  out:
887     qobject_unref(uri_options);
888     qapi_free_BlockdevCreateOptions(create_options);
889     return ret;
890 }
891 
892 static void ssh_close(BlockDriverState *bs)
893 {
894     BDRVSSHState *s = bs->opaque;
895 
896     ssh_state_free(s);
897 }
898 
899 static int ssh_has_zero_init(BlockDriverState *bs)
900 {
901     BDRVSSHState *s = bs->opaque;
902     /* Assume false, unless we can positively prove it's true. */
903     int has_zero_init = 0;
904 
905     if (s->attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
906         if (s->attrs.permissions & LIBSSH2_SFTP_S_IFREG) {
907             has_zero_init = 1;
908         }
909     }
910 
911     return has_zero_init;
912 }
913 
914 typedef struct BDRVSSHRestart {
915     BlockDriverState *bs;
916     Coroutine *co;
917 } BDRVSSHRestart;
918 
919 static void restart_coroutine(void *opaque)
920 {
921     BDRVSSHRestart *restart = opaque;
922     BlockDriverState *bs = restart->bs;
923     BDRVSSHState *s = bs->opaque;
924     AioContext *ctx = bdrv_get_aio_context(bs);
925 
926     trace_ssh_restart_coroutine(restart->co);
927     aio_set_fd_handler(ctx, s->sock, false, NULL, NULL, NULL, NULL);
928 
929     aio_co_wake(restart->co);
930 }
931 
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.
935  */
936 static coroutine_fn void co_yield(BDRVSSHState *s, BlockDriverState *bs)
937 {
938     int r;
939     IOHandler *rd_handler = NULL, *wr_handler = NULL;
940     BDRVSSHRestart restart = {
941         .bs = bs,
942         .co = qemu_coroutine_self()
943     };
944 
945     r = libssh2_session_block_directions(s->session);
946 
947     if (r & LIBSSH2_SESSION_BLOCK_INBOUND) {
948         rd_handler = restart_coroutine;
949     }
950     if (r & LIBSSH2_SESSION_BLOCK_OUTBOUND) {
951         wr_handler = restart_coroutine;
952     }
953 
954     trace_ssh_co_yield(s->sock, rd_handler, wr_handler);
955 
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);
960 }
961 
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.
966  *
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.
971  */
972 #define SSH_SEEK_WRITE 0
973 #define SSH_SEEK_READ  1
974 #define SSH_SEEK_FORCE 2
975 
976 static void ssh_seek(BDRVSSHState *s, int64_t offset, int flags)
977 {
978     bool op_read = (flags & SSH_SEEK_READ) != 0;
979     bool force = (flags & SSH_SEEK_FORCE) != 0;
980 
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;
986     }
987 }
988 
989 static coroutine_fn int ssh_read(BDRVSSHState *s, BlockDriverState *bs,
990                                  int64_t offset, size_t size,
991                                  QEMUIOVector *qiov)
992 {
993     ssize_t r;
994     size_t got;
995     char *buf, *end_of_vec;
996     struct iovec *i;
997 
998     trace_ssh_read(offset, size);
999 
1000     ssh_seek(s, offset, SSH_SEEK_READ);
1001 
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').
1005      */
1006     i = &qiov->iov[0];
1007     buf = i->iov_base;
1008     end_of_vec = i->iov_base + i->iov_len;
1009 
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.
1014      */
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);
1020 
1021         if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
1022             co_yield(s, bs);
1023             goto again;
1024         }
1025         if (r < 0) {
1026             sftp_error_trace(s, "read");
1027             s->offset = -1;
1028             return -EIO;
1029         }
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;
1034         }
1035 
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;
1043         }
1044     }
1045 
1046     return 0;
1047 }
1048 
1049 static coroutine_fn int ssh_co_readv(BlockDriverState *bs,
1050                                      int64_t sector_num,
1051                                      int nb_sectors, QEMUIOVector *qiov)
1052 {
1053     BDRVSSHState *s = bs->opaque;
1054     int ret;
1055 
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);
1060 
1061     return ret;
1062 }
1063 
1064 static int ssh_write(BDRVSSHState *s, BlockDriverState *bs,
1065                      int64_t offset, size_t size,
1066                      QEMUIOVector *qiov)
1067 {
1068     ssize_t r;
1069     size_t written;
1070     char *buf, *end_of_vec;
1071     struct iovec *i;
1072 
1073     trace_ssh_write(offset, size);
1074 
1075     ssh_seek(s, offset, SSH_SEEK_WRITE);
1076 
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').
1080      */
1081     i = &qiov->iov[0];
1082     buf = i->iov_base;
1083     end_of_vec = i->iov_base + i->iov_len;
1084 
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);
1090 
1091         if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
1092             co_yield(s, bs);
1093             goto again;
1094         }
1095         if (r < 0) {
1096             sftp_error_trace(s, "write");
1097             s->offset = -1;
1098             return -EIO;
1099         }
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.
1108          */
1109         if (r == 0) {
1110             ssh_seek(s, offset + written, SSH_SEEK_WRITE|SSH_SEEK_FORCE);
1111             co_yield(s, bs);
1112             goto again;
1113         }
1114 
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;
1122         }
1123 
1124         if (offset + written > s->attrs.filesize)
1125             s->attrs.filesize = offset + written;
1126     }
1127 
1128     return 0;
1129 }
1130 
1131 static coroutine_fn int ssh_co_writev(BlockDriverState *bs,
1132                                       int64_t sector_num,
1133                                       int nb_sectors, QEMUIOVector *qiov,
1134                                       int flags)
1135 {
1136     BDRVSSHState *s = bs->opaque;
1137     int ret;
1138 
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);
1144 
1145     return ret;
1146 }
1147 
1148 static void unsafe_flush_warning(BDRVSSHState *s, const char *what)
1149 {
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);
1155         }
1156         s->unsafe_flush_warning = true;
1157     }
1158 }
1159 
1160 #ifdef HAS_LIBSSH2_SFTP_FSYNC
1161 
1162 static coroutine_fn int ssh_flush(BDRVSSHState *s, BlockDriverState *bs)
1163 {
1164     int r;
1165 
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;
1172     }
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;
1177     }
1178     if (r < 0) {
1179         sftp_error_trace(s, "fsync");
1180         return -EIO;
1181     }
1182 
1183     return 0;
1184 }
1185 
1186 static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
1187 {
1188     BDRVSSHState *s = bs->opaque;
1189     int ret;
1190 
1191     qemu_co_mutex_lock(&s->lock);
1192     ret = ssh_flush(s, bs);
1193     qemu_co_mutex_unlock(&s->lock);
1194 
1195     return ret;
1196 }
1197 
1198 #else /* !HAS_LIBSSH2_SFTP_FSYNC */
1199 
1200 static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
1201 {
1202     BDRVSSHState *s = bs->opaque;
1203 
1204     unsafe_flush_warning(s, "libssh2 >= 1.4.4");
1205     return 0;
1206 }
1207 
1208 #endif /* !HAS_LIBSSH2_SFTP_FSYNC */
1209 
1210 static int64_t ssh_getlength(BlockDriverState *bs)
1211 {
1212     BDRVSSHState *s = bs->opaque;
1213     int64_t length;
1214 
1215     /* Note we cannot make a libssh2 call here. */
1216     length = (int64_t) s->attrs.filesize;
1217     trace_ssh_getlength(length);
1218 
1219     return length;
1220 }
1221 
1222 static int coroutine_fn ssh_co_truncate(BlockDriverState *bs, int64_t offset,
1223                                         PreallocMode prealloc, Error **errp)
1224 {
1225     BDRVSSHState *s = bs->opaque;
1226 
1227     if (prealloc != PREALLOC_MODE_OFF) {
1228         error_setg(errp, "Unsupported preallocation mode '%s'",
1229                    PreallocMode_str(prealloc));
1230         return -ENOTSUP;
1231     }
1232 
1233     if (offset < s->attrs.filesize) {
1234         error_setg(errp, "ssh driver does not support shrinking files");
1235         return -ENOTSUP;
1236     }
1237 
1238     if (offset == s->attrs.filesize) {
1239         return 0;
1240     }
1241 
1242     return ssh_grow_file(s, offset, errp);
1243 }
1244 
1245 static const char *const ssh_strong_runtime_opts[] = {
1246     "host",
1247     "port",
1248     "path",
1249     "user",
1250     "host_key_check",
1251     "server.",
1252 
1253     NULL
1254 };
1255 
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,
1273 };
1274 
1275 static void bdrv_ssh_init(void)
1276 {
1277     int r;
1278 
1279     r = libssh2_init(0);
1280     if (r != 0) {
1281         fprintf(stderr, "libssh2 initialization failed, %d\n", r);
1282         exit(EXIT_FAILURE);
1283     }
1284 
1285     bdrv_register(&bdrv_ssh);
1286 }
1287 
1288 block_init(bdrv_ssh_init);
1289