Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 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 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 26 | |
| 27 | #include <libssh2.h> |
| 28 | #include <libssh2_sftp.h> |
| 29 | |
| 30 | #include "block/block_int.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 31 | #include "qapi/error.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 32 | #include "qemu/error-report.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 33 | #include "qemu/option.h" |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 34 | #include "qemu/cutils.h" |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 35 | #include "qemu/sockets.h" |
| 36 | #include "qemu/uri.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 37 | #include "qapi/qapi-visit-sockets.h" |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 38 | #include "qapi/qapi-visit-block-core.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 39 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 40 | #include "qapi/qmp/qstring.h" |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 41 | #include "qapi/qobject-input-visitor.h" |
| 42 | #include "qapi/qobject-output-visitor.h" |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 43 | |
| 44 | /* DEBUG_SSH=1 enables the DPRINTF (debugging printf) statements in |
| 45 | * this block driver code. |
| 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 DEBUG_SSH 0 |
| 54 | #define TRACE_LIBSSH2 0 /* or try: LIBSSH2_TRACE_SFTP */ |
| 55 | |
| 56 | #define DPRINTF(fmt, ...) \ |
| 57 | do { \ |
| 58 | if (DEBUG_SSH) { \ |
| 59 | fprintf(stderr, "ssh: %-15s " fmt "\n", \ |
| 60 | __func__, ##__VA_ARGS__); \ |
| 61 | } \ |
| 62 | } while (0) |
| 63 | |
| 64 | typedef struct BDRVSSHState { |
| 65 | /* Coroutine. */ |
| 66 | CoMutex lock; |
| 67 | |
| 68 | /* SSH connection. */ |
| 69 | int sock; /* socket */ |
| 70 | LIBSSH2_SESSION *session; /* ssh session */ |
| 71 | LIBSSH2_SFTP *sftp; /* sftp session */ |
| 72 | LIBSSH2_SFTP_HANDLE *sftp_handle; /* sftp remote file handle */ |
| 73 | |
| 74 | /* See ssh_seek() function below. */ |
| 75 | int64_t offset; |
| 76 | bool offset_op_read; |
| 77 | |
| 78 | /* File attributes at open. We try to keep the .filesize field |
| 79 | * updated if it changes (eg by writing at the end of the file). |
| 80 | */ |
| 81 | LIBSSH2_SFTP_ATTRIBUTES attrs; |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 82 | |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 83 | InetSocketAddress *inet; |
| 84 | |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 85 | /* Used to warn if 'flush' is not supported. */ |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 86 | bool unsafe_flush_warning; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 87 | } BDRVSSHState; |
| 88 | |
| 89 | static void ssh_state_init(BDRVSSHState *s) |
| 90 | { |
| 91 | memset(s, 0, sizeof *s); |
| 92 | s->sock = -1; |
| 93 | s->offset = -1; |
| 94 | qemu_co_mutex_init(&s->lock); |
| 95 | } |
| 96 | |
| 97 | static void ssh_state_free(BDRVSSHState *s) |
| 98 | { |
| 99 | if (s->sftp_handle) { |
| 100 | libssh2_sftp_close(s->sftp_handle); |
| 101 | } |
| 102 | if (s->sftp) { |
| 103 | libssh2_sftp_shutdown(s->sftp); |
| 104 | } |
| 105 | if (s->session) { |
| 106 | libssh2_session_disconnect(s->session, |
| 107 | "from qemu ssh client: " |
| 108 | "user closed the connection"); |
| 109 | libssh2_session_free(s->session); |
| 110 | } |
| 111 | if (s->sock >= 0) { |
| 112 | close(s->sock); |
| 113 | } |
| 114 | } |
| 115 | |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 116 | static void GCC_FMT_ATTR(3, 4) |
| 117 | session_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...) |
| 118 | { |
| 119 | va_list args; |
| 120 | char *msg; |
| 121 | |
| 122 | va_start(args, fs); |
| 123 | msg = g_strdup_vprintf(fs, args); |
| 124 | va_end(args); |
| 125 | |
| 126 | if (s->session) { |
| 127 | char *ssh_err; |
| 128 | int ssh_err_code; |
| 129 | |
| 130 | /* This is not an errno. See <libssh2.h>. */ |
| 131 | ssh_err_code = libssh2_session_last_error(s->session, |
| 132 | &ssh_err, NULL, 0); |
| 133 | error_setg(errp, "%s: %s (libssh2 error code: %d)", |
| 134 | msg, ssh_err, ssh_err_code); |
| 135 | } else { |
| 136 | error_setg(errp, "%s", msg); |
| 137 | } |
| 138 | g_free(msg); |
| 139 | } |
| 140 | |
Markus Armbruster | 5496fb1 | 2014-05-16 11:00:16 +0200 | [diff] [blame] | 141 | static void GCC_FMT_ATTR(3, 4) |
| 142 | sftp_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 143 | { |
| 144 | va_list args; |
Markus Armbruster | 5496fb1 | 2014-05-16 11:00:16 +0200 | [diff] [blame] | 145 | char *msg; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 146 | |
| 147 | va_start(args, fs); |
Markus Armbruster | 5496fb1 | 2014-05-16 11:00:16 +0200 | [diff] [blame] | 148 | msg = g_strdup_vprintf(fs, args); |
| 149 | va_end(args); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 150 | |
Markus Armbruster | 5496fb1 | 2014-05-16 11:00:16 +0200 | [diff] [blame] | 151 | if (s->sftp) { |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 152 | char *ssh_err; |
| 153 | int ssh_err_code; |
Markus Armbruster | 5496fb1 | 2014-05-16 11:00:16 +0200 | [diff] [blame] | 154 | unsigned long sftp_err_code; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 155 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 156 | /* This is not an errno. See <libssh2.h>. */ |
Markus Armbruster | 04bc7c0 | 2014-05-16 11:00:12 +0200 | [diff] [blame] | 157 | ssh_err_code = libssh2_session_last_error(s->session, |
| 158 | &ssh_err, NULL, 0); |
Markus Armbruster | 5496fb1 | 2014-05-16 11:00:16 +0200 | [diff] [blame] | 159 | /* See <libssh2_sftp.h>. */ |
| 160 | sftp_err_code = libssh2_sftp_last_error((s)->sftp); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 161 | |
Markus Armbruster | 5496fb1 | 2014-05-16 11:00:16 +0200 | [diff] [blame] | 162 | error_setg(errp, |
| 163 | "%s: %s (libssh2 error code: %d, sftp error code: %lu)", |
| 164 | msg, ssh_err, ssh_err_code, sftp_err_code); |
| 165 | } else { |
| 166 | error_setg(errp, "%s", msg); |
| 167 | } |
| 168 | g_free(msg); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Stefan Weil | 6ae7d66 | 2013-04-18 22:21:05 +0200 | [diff] [blame] | 171 | static void GCC_FMT_ATTR(2, 3) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 172 | sftp_error_report(BDRVSSHState *s, const char *fs, ...) |
| 173 | { |
| 174 | va_list args; |
| 175 | |
| 176 | va_start(args, fs); |
| 177 | error_vprintf(fs, args); |
| 178 | |
| 179 | if ((s)->sftp) { |
| 180 | char *ssh_err; |
| 181 | int ssh_err_code; |
| 182 | unsigned long sftp_err_code; |
| 183 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 184 | /* This is not an errno. See <libssh2.h>. */ |
Markus Armbruster | 04bc7c0 | 2014-05-16 11:00:12 +0200 | [diff] [blame] | 185 | ssh_err_code = libssh2_session_last_error(s->session, |
| 186 | &ssh_err, NULL, 0); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 187 | /* See <libssh2_sftp.h>. */ |
| 188 | sftp_err_code = libssh2_sftp_last_error((s)->sftp); |
| 189 | |
| 190 | error_printf(": %s (libssh2 error code: %d, sftp error code: %lu)", |
| 191 | ssh_err, ssh_err_code, sftp_err_code); |
| 192 | } |
| 193 | |
| 194 | va_end(args); |
| 195 | error_printf("\n"); |
| 196 | } |
| 197 | |
| 198 | static int parse_uri(const char *filename, QDict *options, Error **errp) |
| 199 | { |
| 200 | URI *uri = NULL; |
Paolo Bonzini | eab2ac9 | 2015-09-14 13:12:34 +0200 | [diff] [blame] | 201 | QueryParams *qp; |
Ashijeet Acharya | 1059f1b | 2016-10-25 18:34:00 +0530 | [diff] [blame] | 202 | char *port_str; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 203 | int i; |
| 204 | |
| 205 | uri = uri_parse(filename); |
| 206 | if (!uri) { |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
Max Reitz | f69165a | 2017-06-13 22:57:26 +0200 | [diff] [blame] | 210 | if (g_strcmp0(uri->scheme, "ssh") != 0) { |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 211 | error_setg(errp, "URI scheme must be 'ssh'"); |
| 212 | goto err; |
| 213 | } |
| 214 | |
| 215 | if (!uri->server || strcmp(uri->server, "") == 0) { |
| 216 | error_setg(errp, "missing hostname in URI"); |
| 217 | goto err; |
| 218 | } |
| 219 | |
| 220 | if (!uri->path || strcmp(uri->path, "") == 0) { |
| 221 | error_setg(errp, "missing remote path in URI"); |
| 222 | goto err; |
| 223 | } |
| 224 | |
| 225 | qp = query_params_parse(uri->query); |
| 226 | if (!qp) { |
| 227 | error_setg(errp, "could not parse query parameters"); |
| 228 | goto err; |
| 229 | } |
| 230 | |
| 231 | if(uri->user && strcmp(uri->user, "") != 0) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 232 | qdict_put_str(options, "user", uri->user); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 235 | qdict_put_str(options, "server.host", uri->server); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 236 | |
Ashijeet Acharya | 1059f1b | 2016-10-25 18:34:00 +0530 | [diff] [blame] | 237 | port_str = g_strdup_printf("%d", uri->port ?: 22); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 238 | qdict_put_str(options, "server.port", port_str); |
Ashijeet Acharya | 1059f1b | 2016-10-25 18:34:00 +0530 | [diff] [blame] | 239 | g_free(port_str); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 240 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 241 | qdict_put_str(options, "path", uri->path); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 242 | |
| 243 | /* Pick out any query parameters that we understand, and ignore |
| 244 | * the rest. |
| 245 | */ |
| 246 | for (i = 0; i < qp->n; ++i) { |
| 247 | if (strcmp(qp->p[i].name, "host_key_check") == 0) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 248 | qdict_put_str(options, "host_key_check", qp->p[i].value); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | query_params_free(qp); |
| 253 | uri_free(uri); |
| 254 | return 0; |
| 255 | |
| 256 | err: |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 257 | if (uri) { |
| 258 | uri_free(uri); |
| 259 | } |
| 260 | return -EINVAL; |
| 261 | } |
| 262 | |
Ashijeet Acharya | 89dbe18 | 2016-10-25 18:33:57 +0530 | [diff] [blame] | 263 | static bool ssh_has_filename_options_conflict(QDict *options, Error **errp) |
| 264 | { |
| 265 | const QDictEntry *qe; |
| 266 | |
| 267 | for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) { |
| 268 | if (!strcmp(qe->key, "host") || |
| 269 | !strcmp(qe->key, "port") || |
| 270 | !strcmp(qe->key, "path") || |
| 271 | !strcmp(qe->key, "user") || |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 272 | !strcmp(qe->key, "host_key_check") || |
| 273 | strstart(qe->key, "server.", NULL)) |
Ashijeet Acharya | 89dbe18 | 2016-10-25 18:33:57 +0530 | [diff] [blame] | 274 | { |
| 275 | error_setg(errp, "Option '%s' cannot be used with a file name", |
| 276 | qe->key); |
| 277 | return true; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 284 | static void ssh_parse_filename(const char *filename, QDict *options, |
| 285 | Error **errp) |
| 286 | { |
Ashijeet Acharya | 89dbe18 | 2016-10-25 18:33:57 +0530 | [diff] [blame] | 287 | if (ssh_has_filename_options_conflict(options, errp)) { |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | parse_uri(filename, options, errp); |
| 292 | } |
| 293 | |
| 294 | static int check_host_key_knownhosts(BDRVSSHState *s, |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 295 | const char *host, int port, Error **errp) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 296 | { |
| 297 | const char *home; |
| 298 | char *knh_file = NULL; |
| 299 | LIBSSH2_KNOWNHOSTS *knh = NULL; |
| 300 | struct libssh2_knownhost *found; |
| 301 | int ret, r; |
| 302 | const char *hostkey; |
| 303 | size_t len; |
| 304 | int type; |
| 305 | |
| 306 | hostkey = libssh2_session_hostkey(s->session, &len, &type); |
| 307 | if (!hostkey) { |
| 308 | ret = -EINVAL; |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 309 | session_error_setg(errp, s, "failed to read remote host key"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 310 | goto out; |
| 311 | } |
| 312 | |
| 313 | knh = libssh2_knownhost_init(s->session); |
| 314 | if (!knh) { |
| 315 | ret = -EINVAL; |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 316 | session_error_setg(errp, s, |
| 317 | "failed to initialize known hosts support"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 318 | goto out; |
| 319 | } |
| 320 | |
| 321 | home = getenv("HOME"); |
| 322 | if (home) { |
| 323 | knh_file = g_strdup_printf("%s/.ssh/known_hosts", home); |
| 324 | } else { |
| 325 | knh_file = g_strdup_printf("/root/.ssh/known_hosts"); |
| 326 | } |
| 327 | |
| 328 | /* Read all known hosts from OpenSSH-style known_hosts file. */ |
| 329 | libssh2_knownhost_readfile(knh, knh_file, LIBSSH2_KNOWNHOST_FILE_OPENSSH); |
| 330 | |
| 331 | r = libssh2_knownhost_checkp(knh, host, port, hostkey, len, |
| 332 | LIBSSH2_KNOWNHOST_TYPE_PLAIN| |
| 333 | LIBSSH2_KNOWNHOST_KEYENC_RAW, |
| 334 | &found); |
| 335 | switch (r) { |
| 336 | case LIBSSH2_KNOWNHOST_CHECK_MATCH: |
| 337 | /* OK */ |
| 338 | DPRINTF("host key OK: %s", found->key); |
| 339 | break; |
| 340 | case LIBSSH2_KNOWNHOST_CHECK_MISMATCH: |
| 341 | ret = -EINVAL; |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 342 | session_error_setg(errp, s, |
| 343 | "host key does not match the one in known_hosts" |
| 344 | " (found key %s)", found->key); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 345 | goto out; |
| 346 | case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND: |
| 347 | ret = -EINVAL; |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 348 | session_error_setg(errp, s, "no host key was found in known_hosts"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 349 | goto out; |
| 350 | case LIBSSH2_KNOWNHOST_CHECK_FAILURE: |
| 351 | ret = -EINVAL; |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 352 | session_error_setg(errp, s, |
| 353 | "failure matching the host key with known_hosts"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 354 | goto out; |
| 355 | default: |
| 356 | ret = -EINVAL; |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 357 | session_error_setg(errp, s, "unknown error matching the host key" |
| 358 | " with known_hosts (%d)", r); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 359 | goto out; |
| 360 | } |
| 361 | |
| 362 | /* known_hosts checking successful. */ |
| 363 | ret = 0; |
| 364 | |
| 365 | out: |
| 366 | if (knh != NULL) { |
| 367 | libssh2_knownhost_free(knh); |
| 368 | } |
| 369 | g_free(knh_file); |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | static unsigned hex2decimal(char ch) |
| 374 | { |
| 375 | if (ch >= '0' && ch <= '9') { |
| 376 | return (ch - '0'); |
| 377 | } else if (ch >= 'a' && ch <= 'f') { |
| 378 | return 10 + (ch - 'a'); |
| 379 | } else if (ch >= 'A' && ch <= 'F') { |
| 380 | return 10 + (ch - 'A'); |
| 381 | } |
| 382 | |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | /* Compare the binary fingerprint (hash of host key) with the |
| 387 | * host_key_check parameter. |
| 388 | */ |
| 389 | static int compare_fingerprint(const unsigned char *fingerprint, size_t len, |
| 390 | const char *host_key_check) |
| 391 | { |
| 392 | unsigned c; |
| 393 | |
| 394 | while (len > 0) { |
| 395 | while (*host_key_check == ':') |
| 396 | host_key_check++; |
| 397 | if (!qemu_isxdigit(host_key_check[0]) || |
| 398 | !qemu_isxdigit(host_key_check[1])) |
| 399 | return 1; |
| 400 | c = hex2decimal(host_key_check[0]) * 16 + |
| 401 | hex2decimal(host_key_check[1]); |
| 402 | if (c - *fingerprint != 0) |
| 403 | return c - *fingerprint; |
| 404 | fingerprint++; |
| 405 | len--; |
| 406 | host_key_check += 2; |
| 407 | } |
| 408 | return *host_key_check - '\0'; |
| 409 | } |
| 410 | |
| 411 | static int |
| 412 | check_host_key_hash(BDRVSSHState *s, const char *hash, |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 413 | int hash_type, size_t fingerprint_len, Error **errp) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 414 | { |
| 415 | const char *fingerprint; |
| 416 | |
| 417 | fingerprint = libssh2_hostkey_hash(s->session, hash_type); |
| 418 | if (!fingerprint) { |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 419 | session_error_setg(errp, s, "failed to read remote host key"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 420 | return -EINVAL; |
| 421 | } |
| 422 | |
| 423 | if(compare_fingerprint((unsigned char *) fingerprint, fingerprint_len, |
| 424 | hash) != 0) { |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 425 | error_setg(errp, "remote host key does not match host_key_check '%s'", |
| 426 | hash); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 427 | return -EPERM; |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int check_host_key(BDRVSSHState *s, const char *host, int port, |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 434 | SshHostKeyCheck *hkc, Error **errp) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 435 | { |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 436 | SshHostKeyCheckMode mode; |
| 437 | |
| 438 | if (hkc) { |
| 439 | mode = hkc->mode; |
| 440 | } else { |
| 441 | mode = SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS; |
| 442 | } |
| 443 | |
| 444 | switch (mode) { |
| 445 | case SSH_HOST_KEY_CHECK_MODE_NONE: |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 446 | return 0; |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 447 | case SSH_HOST_KEY_CHECK_MODE_HASH: |
| 448 | if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_MD5) { |
| 449 | return check_host_key_hash(s, hkc->u.hash.hash, |
| 450 | LIBSSH2_HOSTKEY_HASH_MD5, 16, errp); |
| 451 | } else if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_SHA1) { |
| 452 | return check_host_key_hash(s, hkc->u.hash.hash, |
| 453 | LIBSSH2_HOSTKEY_HASH_SHA1, 20, errp); |
| 454 | } |
| 455 | g_assert_not_reached(); |
| 456 | break; |
| 457 | case SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS: |
Markus Armbruster | 01c2b26 | 2014-05-16 11:00:13 +0200 | [diff] [blame] | 458 | return check_host_key_knownhosts(s, host, port, errp); |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 459 | default: |
| 460 | g_assert_not_reached(); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 463 | return -EINVAL; |
| 464 | } |
| 465 | |
Markus Armbruster | 4618e65 | 2014-05-16 11:00:14 +0200 | [diff] [blame] | 466 | static int authenticate(BDRVSSHState *s, const char *user, Error **errp) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 467 | { |
| 468 | int r, ret; |
| 469 | const char *userauthlist; |
| 470 | LIBSSH2_AGENT *agent = NULL; |
| 471 | struct libssh2_agent_publickey *identity; |
| 472 | struct libssh2_agent_publickey *prev_identity = NULL; |
| 473 | |
| 474 | userauthlist = libssh2_userauth_list(s->session, user, strlen(user)); |
| 475 | if (strstr(userauthlist, "publickey") == NULL) { |
| 476 | ret = -EPERM; |
Markus Armbruster | 4618e65 | 2014-05-16 11:00:14 +0200 | [diff] [blame] | 477 | error_setg(errp, |
| 478 | "remote server does not support \"publickey\" authentication"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 479 | goto out; |
| 480 | } |
| 481 | |
| 482 | /* Connect to ssh-agent and try each identity in turn. */ |
| 483 | agent = libssh2_agent_init(s->session); |
| 484 | if (!agent) { |
| 485 | ret = -EINVAL; |
Markus Armbruster | 4618e65 | 2014-05-16 11:00:14 +0200 | [diff] [blame] | 486 | session_error_setg(errp, s, "failed to initialize ssh-agent support"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 487 | goto out; |
| 488 | } |
| 489 | if (libssh2_agent_connect(agent)) { |
| 490 | ret = -ECONNREFUSED; |
Markus Armbruster | 4618e65 | 2014-05-16 11:00:14 +0200 | [diff] [blame] | 491 | session_error_setg(errp, s, "failed to connect to ssh-agent"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 492 | goto out; |
| 493 | } |
| 494 | if (libssh2_agent_list_identities(agent)) { |
| 495 | ret = -EINVAL; |
Markus Armbruster | 4618e65 | 2014-05-16 11:00:14 +0200 | [diff] [blame] | 496 | session_error_setg(errp, s, |
| 497 | "failed requesting identities from ssh-agent"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 498 | goto out; |
| 499 | } |
| 500 | |
| 501 | for(;;) { |
| 502 | r = libssh2_agent_get_identity(agent, &identity, prev_identity); |
| 503 | if (r == 1) { /* end of list */ |
| 504 | break; |
| 505 | } |
| 506 | if (r < 0) { |
| 507 | ret = -EINVAL; |
Markus Armbruster | 4618e65 | 2014-05-16 11:00:14 +0200 | [diff] [blame] | 508 | session_error_setg(errp, s, |
| 509 | "failed to obtain identity from ssh-agent"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 510 | goto out; |
| 511 | } |
| 512 | r = libssh2_agent_userauth(agent, user, identity); |
| 513 | if (r == 0) { |
| 514 | /* Authenticated! */ |
| 515 | ret = 0; |
| 516 | goto out; |
| 517 | } |
| 518 | /* Failed to authenticate with this identity, try the next one. */ |
| 519 | prev_identity = identity; |
| 520 | } |
| 521 | |
| 522 | ret = -EPERM; |
Markus Armbruster | 4618e65 | 2014-05-16 11:00:14 +0200 | [diff] [blame] | 523 | error_setg(errp, "failed to authenticate using publickey authentication " |
| 524 | "and the identities held by your ssh-agent"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 525 | |
| 526 | out: |
| 527 | if (agent != NULL) { |
| 528 | /* Note: libssh2 implementation implicitly calls |
| 529 | * libssh2_agent_disconnect if necessary. |
| 530 | */ |
| 531 | libssh2_agent_free(agent); |
| 532 | } |
| 533 | |
| 534 | return ret; |
| 535 | } |
| 536 | |
Max Reitz | 8a6a808 | 2016-08-15 15:29:23 +0200 | [diff] [blame] | 537 | static QemuOptsList ssh_runtime_opts = { |
| 538 | .name = "ssh", |
| 539 | .head = QTAILQ_HEAD_INITIALIZER(ssh_runtime_opts.head), |
| 540 | .desc = { |
| 541 | { |
| 542 | .name = "host", |
| 543 | .type = QEMU_OPT_STRING, |
| 544 | .help = "Host to connect to", |
| 545 | }, |
| 546 | { |
| 547 | .name = "port", |
| 548 | .type = QEMU_OPT_NUMBER, |
| 549 | .help = "Port to connect to", |
| 550 | }, |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 551 | { |
| 552 | .name = "host_key_check", |
| 553 | .type = QEMU_OPT_STRING, |
| 554 | .help = "Defines how and what to check the host key against", |
| 555 | }, |
Murilo Opsfelder Araujo | fbd5c4c | 2018-01-05 12:44:40 -0200 | [diff] [blame] | 556 | { /* end of list */ } |
Max Reitz | 8a6a808 | 2016-08-15 15:29:23 +0200 | [diff] [blame] | 557 | }, |
| 558 | }; |
| 559 | |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 560 | static bool ssh_process_legacy_options(QDict *output_opts, |
| 561 | QemuOpts *legacy_opts, |
| 562 | Error **errp) |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 563 | { |
| 564 | const char *host = qemu_opt_get(legacy_opts, "host"); |
| 565 | const char *port = qemu_opt_get(legacy_opts, "port"); |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 566 | const char *host_key_check = qemu_opt_get(legacy_opts, "host_key_check"); |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 567 | |
| 568 | if (!host && port) { |
| 569 | error_setg(errp, "port may not be used without host"); |
| 570 | return false; |
| 571 | } |
| 572 | |
| 573 | if (host) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 574 | qdict_put_str(output_opts, "server.host", host); |
| 575 | qdict_put_str(output_opts, "server.port", port ?: stringify(22)); |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 576 | } |
| 577 | |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 578 | if (host_key_check) { |
| 579 | if (strcmp(host_key_check, "no") == 0) { |
| 580 | qdict_put_str(output_opts, "host-key-check.mode", "none"); |
| 581 | } else if (strncmp(host_key_check, "md5:", 4) == 0) { |
| 582 | qdict_put_str(output_opts, "host-key-check.mode", "hash"); |
| 583 | qdict_put_str(output_opts, "host-key-check.type", "md5"); |
| 584 | qdict_put_str(output_opts, "host-key-check.hash", |
| 585 | &host_key_check[4]); |
| 586 | } else if (strncmp(host_key_check, "sha1:", 5) == 0) { |
| 587 | qdict_put_str(output_opts, "host-key-check.mode", "hash"); |
| 588 | qdict_put_str(output_opts, "host-key-check.type", "sha1"); |
| 589 | qdict_put_str(output_opts, "host-key-check.hash", |
| 590 | &host_key_check[5]); |
| 591 | } else if (strcmp(host_key_check, "yes") == 0) { |
| 592 | qdict_put_str(output_opts, "host-key-check.mode", "known_hosts"); |
| 593 | } else { |
| 594 | error_setg(errp, "unknown host_key_check setting (%s)", |
| 595 | host_key_check); |
| 596 | return false; |
| 597 | } |
| 598 | } |
| 599 | |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 600 | return true; |
| 601 | } |
| 602 | |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 603 | static BlockdevOptionsSsh *ssh_parse_options(QDict *options, Error **errp) |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 604 | { |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 605 | BlockdevOptionsSsh *result = NULL; |
| 606 | QemuOpts *opts = NULL; |
| 607 | Error *local_err = NULL; |
| 608 | QObject *crumpled; |
| 609 | const QDictEntry *e; |
| 610 | Visitor *v; |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 611 | |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 612 | /* Translate legacy options */ |
| 613 | opts = qemu_opts_create(&ssh_runtime_opts, NULL, 0, &error_abort); |
| 614 | qemu_opts_absorb_qdict(opts, options, &local_err); |
| 615 | if (local_err) { |
| 616 | error_propagate(errp, local_err); |
| 617 | goto fail; |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 618 | } |
| 619 | |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 620 | if (!ssh_process_legacy_options(options, opts, errp)) { |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 621 | goto fail; |
| 622 | } |
| 623 | |
| 624 | /* Create the QAPI object */ |
| 625 | crumpled = qdict_crumple(options, errp); |
| 626 | if (crumpled == NULL) { |
| 627 | goto fail; |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 628 | } |
| 629 | |
Markus Armbruster | 129c7d1 | 2017-03-30 19:43:12 +0200 | [diff] [blame] | 630 | /* |
| 631 | * FIXME .numeric, .to, .ipv4 or .ipv6 don't work with -drive. |
| 632 | * .to doesn't matter, it's ignored anyway. |
| 633 | * That's because when @options come from -blockdev or |
| 634 | * blockdev_add, members are typed according to the QAPI schema, |
| 635 | * but when they come from -drive, they're all QString. The |
| 636 | * visitor expects the former. |
| 637 | */ |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 638 | v = qobject_input_visitor_new(crumpled); |
| 639 | visit_type_BlockdevOptionsSsh(v, NULL, &result, &local_err); |
| 640 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 641 | qobject_unref(crumpled); |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 642 | |
| 643 | if (local_err) { |
| 644 | error_propagate(errp, local_err); |
| 645 | goto fail; |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 646 | } |
| 647 | |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 648 | /* Remove the processed options from the QDict (the visitor processes |
| 649 | * _all_ options in the QDict) */ |
| 650 | while ((e = qdict_first(options))) { |
| 651 | qdict_del(options, e->key); |
| 652 | } |
| 653 | |
| 654 | fail: |
| 655 | qemu_opts_del(opts); |
| 656 | return result; |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 657 | } |
| 658 | |
Kevin Wolf | 375f0b9 | 2018-02-05 15:28:14 +0100 | [diff] [blame] | 659 | static int connect_to_ssh(BDRVSSHState *s, BlockdevOptionsSsh *opts, |
Markus Armbruster | 5f0c39e | 2014-05-16 11:00:15 +0200 | [diff] [blame] | 660 | int ssh_flags, int creat_mode, Error **errp) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 661 | { |
| 662 | int r, ret; |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 663 | const char *user; |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 664 | long port = 0; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 665 | |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 666 | if (opts->has_user) { |
| 667 | user = opts->user; |
| 668 | } else { |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 669 | user = g_get_user_name(); |
| 670 | if (!user) { |
Markus Armbruster | 5f0c39e | 2014-05-16 11:00:15 +0200 | [diff] [blame] | 671 | error_setg_errno(errp, errno, "Can't get user name"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 672 | ret = -errno; |
| 673 | goto err; |
| 674 | } |
| 675 | } |
| 676 | |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 677 | /* Pop the config into our state object, Exit if invalid */ |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 678 | s->inet = opts->server; |
| 679 | opts->server = NULL; |
Ashijeet Acharya | 0da5b8e | 2016-10-25 18:33:59 +0530 | [diff] [blame] | 680 | |
| 681 | if (qemu_strtol(s->inet->port, NULL, 10, &port) < 0) { |
| 682 | error_setg(errp, "Use only numeric port value"); |
| 683 | ret = -EINVAL; |
| 684 | goto err; |
| 685 | } |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 686 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 687 | /* Open the socket and connect. */ |
Cao jin | b258793 | 2017-06-16 16:54:45 +0800 | [diff] [blame] | 688 | s->sock = inet_connect_saddr(s->inet, errp); |
Markus Armbruster | 5f0c39e | 2014-05-16 11:00:15 +0200 | [diff] [blame] | 689 | if (s->sock < 0) { |
Richard W.M. Jones | 325e390 | 2015-07-22 14:27:47 +0100 | [diff] [blame] | 690 | ret = -EIO; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 691 | goto err; |
| 692 | } |
| 693 | |
| 694 | /* Create SSH session. */ |
| 695 | s->session = libssh2_session_init(); |
| 696 | if (!s->session) { |
| 697 | ret = -EINVAL; |
Markus Armbruster | 5f0c39e | 2014-05-16 11:00:15 +0200 | [diff] [blame] | 698 | session_error_setg(errp, s, "failed to initialize libssh2 session"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 699 | goto err; |
| 700 | } |
| 701 | |
| 702 | #if TRACE_LIBSSH2 != 0 |
| 703 | libssh2_trace(s->session, TRACE_LIBSSH2); |
| 704 | #endif |
| 705 | |
| 706 | r = libssh2_session_handshake(s->session, s->sock); |
| 707 | if (r != 0) { |
| 708 | ret = -EINVAL; |
Markus Armbruster | 5f0c39e | 2014-05-16 11:00:15 +0200 | [diff] [blame] | 709 | session_error_setg(errp, s, "failed to establish SSH session"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 710 | goto err; |
| 711 | } |
| 712 | |
| 713 | /* Check the remote host's key against known_hosts. */ |
Kevin Wolf | ec2f541 | 2018-02-05 14:59:05 +0100 | [diff] [blame] | 714 | ret = check_host_key(s, s->inet->host, port, opts->host_key_check, errp); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 715 | if (ret < 0) { |
| 716 | goto err; |
| 717 | } |
| 718 | |
| 719 | /* Authenticate. */ |
Markus Armbruster | 5f0c39e | 2014-05-16 11:00:15 +0200 | [diff] [blame] | 720 | ret = authenticate(s, user, errp); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 721 | if (ret < 0) { |
| 722 | goto err; |
| 723 | } |
| 724 | |
| 725 | /* Start SFTP. */ |
| 726 | s->sftp = libssh2_sftp_init(s->session); |
| 727 | if (!s->sftp) { |
Markus Armbruster | 5f0c39e | 2014-05-16 11:00:15 +0200 | [diff] [blame] | 728 | session_error_setg(errp, s, "failed to initialize sftp handle"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 729 | ret = -EINVAL; |
| 730 | goto err; |
| 731 | } |
| 732 | |
| 733 | /* Open the remote file. */ |
| 734 | DPRINTF("opening file %s flags=0x%x creat_mode=0%o", |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 735 | opts->path, ssh_flags, creat_mode); |
| 736 | s->sftp_handle = libssh2_sftp_open(s->sftp, opts->path, ssh_flags, |
| 737 | creat_mode); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 738 | if (!s->sftp_handle) { |
Kevin Wolf | 16e4bdb | 2018-02-02 16:12:18 +0100 | [diff] [blame] | 739 | session_error_setg(errp, s, "failed to open remote file '%s'", |
| 740 | opts->path); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 741 | ret = -EINVAL; |
| 742 | goto err; |
| 743 | } |
| 744 | |
| 745 | r = libssh2_sftp_fstat(s->sftp_handle, &s->attrs); |
| 746 | if (r < 0) { |
Markus Armbruster | 5496fb1 | 2014-05-16 11:00:16 +0200 | [diff] [blame] | 747 | sftp_error_setg(errp, s, "failed to read file attributes"); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 748 | return -EINVAL; |
| 749 | } |
| 750 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 751 | return 0; |
| 752 | |
| 753 | err: |
| 754 | if (s->sftp_handle) { |
| 755 | libssh2_sftp_close(s->sftp_handle); |
| 756 | } |
| 757 | s->sftp_handle = NULL; |
| 758 | if (s->sftp) { |
| 759 | libssh2_sftp_shutdown(s->sftp); |
| 760 | } |
| 761 | s->sftp = NULL; |
| 762 | if (s->session) { |
| 763 | libssh2_session_disconnect(s->session, |
| 764 | "from qemu ssh client: " |
| 765 | "error opening connection"); |
| 766 | libssh2_session_free(s->session); |
| 767 | } |
| 768 | s->session = NULL; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 769 | |
| 770 | return ret; |
| 771 | } |
| 772 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 773 | static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags, |
| 774 | Error **errp) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 775 | { |
| 776 | BDRVSSHState *s = bs->opaque; |
Kevin Wolf | 375f0b9 | 2018-02-05 15:28:14 +0100 | [diff] [blame] | 777 | BlockdevOptionsSsh *opts; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 778 | int ret; |
| 779 | int ssh_flags; |
| 780 | |
| 781 | ssh_state_init(s); |
| 782 | |
| 783 | ssh_flags = LIBSSH2_FXF_READ; |
| 784 | if (bdrv_flags & BDRV_O_RDWR) { |
| 785 | ssh_flags |= LIBSSH2_FXF_WRITE; |
| 786 | } |
| 787 | |
Kevin Wolf | 375f0b9 | 2018-02-05 15:28:14 +0100 | [diff] [blame] | 788 | opts = ssh_parse_options(options, errp); |
| 789 | if (opts == NULL) { |
| 790 | return -EINVAL; |
| 791 | } |
| 792 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 793 | /* Start up SSH. */ |
Kevin Wolf | 375f0b9 | 2018-02-05 15:28:14 +0100 | [diff] [blame] | 794 | ret = connect_to_ssh(s, opts, ssh_flags, 0, errp); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 795 | if (ret < 0) { |
| 796 | goto err; |
| 797 | } |
| 798 | |
| 799 | /* Go non-blocking. */ |
| 800 | libssh2_session_set_blocking(s->session, 0); |
| 801 | |
Kevin Wolf | 375f0b9 | 2018-02-05 15:28:14 +0100 | [diff] [blame] | 802 | qapi_free_BlockdevOptionsSsh(opts); |
| 803 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 804 | return 0; |
| 805 | |
| 806 | err: |
| 807 | if (s->sock >= 0) { |
| 808 | close(s->sock); |
| 809 | } |
| 810 | s->sock = -1; |
| 811 | |
Kevin Wolf | 375f0b9 | 2018-02-05 15:28:14 +0100 | [diff] [blame] | 812 | qapi_free_BlockdevOptionsSsh(opts); |
| 813 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 814 | return ret; |
| 815 | } |
| 816 | |
Max Reitz | bd8e0e3 | 2018-02-14 21:49:14 +0100 | [diff] [blame] | 817 | /* Note: This is a blocking operation */ |
Max Reitz | 2b12a75 | 2018-02-14 21:49:13 +0100 | [diff] [blame] | 818 | static int ssh_grow_file(BDRVSSHState *s, int64_t offset, Error **errp) |
| 819 | { |
| 820 | ssize_t ret; |
| 821 | char c[1] = { '\0' }; |
Max Reitz | bd8e0e3 | 2018-02-14 21:49:14 +0100 | [diff] [blame] | 822 | int was_blocking = libssh2_session_get_blocking(s->session); |
Max Reitz | 2b12a75 | 2018-02-14 21:49:13 +0100 | [diff] [blame] | 823 | |
| 824 | /* offset must be strictly greater than the current size so we do |
| 825 | * not overwrite anything */ |
| 826 | assert(offset > 0 && offset > s->attrs.filesize); |
| 827 | |
Max Reitz | bd8e0e3 | 2018-02-14 21:49:14 +0100 | [diff] [blame] | 828 | libssh2_session_set_blocking(s->session, 1); |
| 829 | |
Max Reitz | 2b12a75 | 2018-02-14 21:49:13 +0100 | [diff] [blame] | 830 | libssh2_sftp_seek64(s->sftp_handle, offset - 1); |
| 831 | ret = libssh2_sftp_write(s->sftp_handle, c, 1); |
Max Reitz | bd8e0e3 | 2018-02-14 21:49:14 +0100 | [diff] [blame] | 832 | |
| 833 | libssh2_session_set_blocking(s->session, was_blocking); |
| 834 | |
Max Reitz | 2b12a75 | 2018-02-14 21:49:13 +0100 | [diff] [blame] | 835 | if (ret < 0) { |
| 836 | sftp_error_setg(errp, s, "Failed to grow file"); |
| 837 | return -EIO; |
| 838 | } |
| 839 | |
| 840 | s->attrs.filesize = offset; |
| 841 | return 0; |
| 842 | } |
| 843 | |
Chunyan Liu | 766181f | 2014-06-05 17:21:06 +0800 | [diff] [blame] | 844 | static QemuOptsList ssh_create_opts = { |
| 845 | .name = "ssh-create-opts", |
| 846 | .head = QTAILQ_HEAD_INITIALIZER(ssh_create_opts.head), |
| 847 | .desc = { |
| 848 | { |
| 849 | .name = BLOCK_OPT_SIZE, |
| 850 | .type = QEMU_OPT_SIZE, |
| 851 | .help = "Virtual disk size" |
| 852 | }, |
| 853 | { /* end of list */ } |
| 854 | } |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 855 | }; |
| 856 | |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 857 | static int ssh_co_create(BlockdevCreateOptions *options, Error **errp) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 858 | { |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 859 | BlockdevCreateOptionsSsh *opts = &options->u.ssh; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 860 | BDRVSSHState s; |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 861 | int ret; |
| 862 | |
| 863 | assert(options->driver == BLOCKDEV_DRIVER_SSH); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 864 | |
| 865 | ssh_state_init(&s); |
| 866 | |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 867 | ret = connect_to_ssh(&s, opts->location, |
| 868 | LIBSSH2_FXF_READ|LIBSSH2_FXF_WRITE| |
| 869 | LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, |
| 870 | 0644, errp); |
| 871 | if (ret < 0) { |
| 872 | goto fail; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 875 | if (opts->size > 0) { |
| 876 | ret = ssh_grow_file(&s, opts->size, errp); |
Max Reitz | 2b12a75 | 2018-02-14 21:49:13 +0100 | [diff] [blame] | 877 | if (ret < 0) { |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 878 | goto fail; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 879 | } |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | ret = 0; |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 883 | fail: |
| 884 | ssh_state_free(&s); |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | static int coroutine_fn ssh_co_create_opts(const char *filename, QemuOpts *opts, |
| 889 | Error **errp) |
| 890 | { |
| 891 | BlockdevCreateOptions *create_options; |
| 892 | BlockdevCreateOptionsSsh *ssh_opts; |
| 893 | int ret; |
| 894 | QDict *uri_options = NULL; |
| 895 | |
| 896 | create_options = g_new0(BlockdevCreateOptions, 1); |
| 897 | create_options->driver = BLOCKDEV_DRIVER_SSH; |
| 898 | ssh_opts = &create_options->u.ssh; |
| 899 | |
| 900 | /* Get desired file size. */ |
| 901 | ssh_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 902 | BDRV_SECTOR_SIZE); |
| 903 | DPRINTF("total_size=%" PRIi64, ssh_opts->size); |
| 904 | |
| 905 | uri_options = qdict_new(); |
| 906 | ret = parse_uri(filename, uri_options, errp); |
| 907 | if (ret < 0) { |
| 908 | goto out; |
| 909 | } |
| 910 | |
| 911 | ssh_opts->location = ssh_parse_options(uri_options, errp); |
| 912 | if (ssh_opts->location == NULL) { |
| 913 | ret = -EINVAL; |
| 914 | goto out; |
| 915 | } |
| 916 | |
| 917 | ret = ssh_co_create(create_options, errp); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 918 | |
| 919 | out: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 920 | qobject_unref(uri_options); |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 921 | qapi_free_BlockdevCreateOptions(create_options); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | static void ssh_close(BlockDriverState *bs) |
| 926 | { |
| 927 | BDRVSSHState *s = bs->opaque; |
| 928 | |
| 929 | ssh_state_free(s); |
| 930 | } |
| 931 | |
Richard W.M. Jones | 0b3f21e | 2013-06-25 18:15:18 +0100 | [diff] [blame] | 932 | static int ssh_has_zero_init(BlockDriverState *bs) |
| 933 | { |
| 934 | BDRVSSHState *s = bs->opaque; |
| 935 | /* Assume false, unless we can positively prove it's true. */ |
| 936 | int has_zero_init = 0; |
| 937 | |
| 938 | if (s->attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { |
| 939 | if (s->attrs.permissions & LIBSSH2_SFTP_S_IFREG) { |
| 940 | has_zero_init = 1; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | return has_zero_init; |
| 945 | } |
| 946 | |
Paolo Bonzini | 5aca18a | 2017-06-29 15:27:49 +0200 | [diff] [blame] | 947 | typedef struct BDRVSSHRestart { |
| 948 | BlockDriverState *bs; |
| 949 | Coroutine *co; |
| 950 | } BDRVSSHRestart; |
| 951 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 952 | static void restart_coroutine(void *opaque) |
| 953 | { |
Paolo Bonzini | 5aca18a | 2017-06-29 15:27:49 +0200 | [diff] [blame] | 954 | BDRVSSHRestart *restart = opaque; |
| 955 | BlockDriverState *bs = restart->bs; |
| 956 | BDRVSSHState *s = bs->opaque; |
| 957 | AioContext *ctx = bdrv_get_aio_context(bs); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 958 | |
Paolo Bonzini | 5aca18a | 2017-06-29 15:27:49 +0200 | [diff] [blame] | 959 | DPRINTF("co=%p", restart->co); |
| 960 | aio_set_fd_handler(ctx, s->sock, false, NULL, NULL, NULL, NULL); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 961 | |
Paolo Bonzini | 5aca18a | 2017-06-29 15:27:49 +0200 | [diff] [blame] | 962 | aio_co_wake(restart->co); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 965 | /* A non-blocking call returned EAGAIN, so yield, ensuring the |
| 966 | * handlers are set up so that we'll be rescheduled when there is an |
| 967 | * interesting event on the socket. |
| 968 | */ |
| 969 | static coroutine_fn void co_yield(BDRVSSHState *s, BlockDriverState *bs) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 970 | { |
| 971 | int r; |
| 972 | IOHandler *rd_handler = NULL, *wr_handler = NULL; |
Paolo Bonzini | 5aca18a | 2017-06-29 15:27:49 +0200 | [diff] [blame] | 973 | BDRVSSHRestart restart = { |
| 974 | .bs = bs, |
| 975 | .co = qemu_coroutine_self() |
| 976 | }; |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 977 | |
| 978 | r = libssh2_session_block_directions(s->session); |
| 979 | |
| 980 | if (r & LIBSSH2_SESSION_BLOCK_INBOUND) { |
| 981 | rd_handler = restart_coroutine; |
| 982 | } |
| 983 | if (r & LIBSSH2_SESSION_BLOCK_OUTBOUND) { |
| 984 | wr_handler = restart_coroutine; |
| 985 | } |
| 986 | |
| 987 | DPRINTF("s->sock=%d rd_handler=%p wr_handler=%p", s->sock, |
| 988 | rd_handler, wr_handler); |
| 989 | |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 990 | aio_set_fd_handler(bdrv_get_aio_context(bs), s->sock, |
Paolo Bonzini | 5aca18a | 2017-06-29 15:27:49 +0200 | [diff] [blame] | 991 | false, rd_handler, wr_handler, NULL, &restart); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 992 | qemu_coroutine_yield(); |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 993 | DPRINTF("s->sock=%d - back", s->sock); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* SFTP has a function `libssh2_sftp_seek64' which seeks to a position |
| 997 | * in the remote file. Notice that it just updates a field in the |
| 998 | * sftp_handle structure, so there is no network traffic and it cannot |
| 999 | * fail. |
| 1000 | * |
| 1001 | * However, `libssh2_sftp_seek64' does have a catastrophic effect on |
| 1002 | * performance since it causes the handle to throw away all in-flight |
| 1003 | * reads and buffered readahead data. Therefore this function tries |
| 1004 | * to be intelligent about when to call the underlying libssh2 function. |
| 1005 | */ |
| 1006 | #define SSH_SEEK_WRITE 0 |
| 1007 | #define SSH_SEEK_READ 1 |
| 1008 | #define SSH_SEEK_FORCE 2 |
| 1009 | |
| 1010 | static void ssh_seek(BDRVSSHState *s, int64_t offset, int flags) |
| 1011 | { |
| 1012 | bool op_read = (flags & SSH_SEEK_READ) != 0; |
| 1013 | bool force = (flags & SSH_SEEK_FORCE) != 0; |
| 1014 | |
| 1015 | if (force || op_read != s->offset_op_read || offset != s->offset) { |
| 1016 | DPRINTF("seeking to offset=%" PRIi64, offset); |
| 1017 | libssh2_sftp_seek64(s->sftp_handle, offset); |
| 1018 | s->offset = offset; |
| 1019 | s->offset_op_read = op_read; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1023 | static coroutine_fn int ssh_read(BDRVSSHState *s, BlockDriverState *bs, |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1024 | int64_t offset, size_t size, |
| 1025 | QEMUIOVector *qiov) |
| 1026 | { |
| 1027 | ssize_t r; |
| 1028 | size_t got; |
| 1029 | char *buf, *end_of_vec; |
| 1030 | struct iovec *i; |
| 1031 | |
| 1032 | DPRINTF("offset=%" PRIi64 " size=%zu", offset, size); |
| 1033 | |
| 1034 | ssh_seek(s, offset, SSH_SEEK_READ); |
| 1035 | |
| 1036 | /* This keeps track of the current iovec element ('i'), where we |
| 1037 | * will write to next ('buf'), and the end of the current iovec |
| 1038 | * ('end_of_vec'). |
| 1039 | */ |
| 1040 | i = &qiov->iov[0]; |
| 1041 | buf = i->iov_base; |
| 1042 | end_of_vec = i->iov_base + i->iov_len; |
| 1043 | |
| 1044 | /* libssh2 has a hard-coded limit of 2000 bytes per request, |
| 1045 | * although it will also do readahead behind our backs. Therefore |
| 1046 | * we may have to do repeated reads here until we have read 'size' |
| 1047 | * bytes. |
| 1048 | */ |
| 1049 | for (got = 0; got < size; ) { |
| 1050 | again: |
| 1051 | DPRINTF("sftp_read buf=%p size=%zu", buf, end_of_vec - buf); |
| 1052 | r = libssh2_sftp_read(s->sftp_handle, buf, end_of_vec - buf); |
| 1053 | DPRINTF("sftp_read returned %zd", r); |
| 1054 | |
| 1055 | if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) { |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1056 | co_yield(s, bs); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1057 | goto again; |
| 1058 | } |
| 1059 | if (r < 0) { |
| 1060 | sftp_error_report(s, "read failed"); |
| 1061 | s->offset = -1; |
| 1062 | return -EIO; |
| 1063 | } |
| 1064 | if (r == 0) { |
| 1065 | /* EOF: Short read so pad the buffer with zeroes and return it. */ |
| 1066 | qemu_iovec_memset(qiov, got, 0, size - got); |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | got += r; |
| 1071 | buf += r; |
| 1072 | s->offset += r; |
| 1073 | if (buf >= end_of_vec && got < size) { |
| 1074 | i++; |
| 1075 | buf = i->iov_base; |
| 1076 | end_of_vec = i->iov_base + i->iov_len; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | static coroutine_fn int ssh_co_readv(BlockDriverState *bs, |
| 1084 | int64_t sector_num, |
| 1085 | int nb_sectors, QEMUIOVector *qiov) |
| 1086 | { |
| 1087 | BDRVSSHState *s = bs->opaque; |
| 1088 | int ret; |
| 1089 | |
| 1090 | qemu_co_mutex_lock(&s->lock); |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1091 | ret = ssh_read(s, bs, sector_num * BDRV_SECTOR_SIZE, |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1092 | nb_sectors * BDRV_SECTOR_SIZE, qiov); |
| 1093 | qemu_co_mutex_unlock(&s->lock); |
| 1094 | |
| 1095 | return ret; |
| 1096 | } |
| 1097 | |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1098 | static int ssh_write(BDRVSSHState *s, BlockDriverState *bs, |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1099 | int64_t offset, size_t size, |
| 1100 | QEMUIOVector *qiov) |
| 1101 | { |
| 1102 | ssize_t r; |
| 1103 | size_t written; |
| 1104 | char *buf, *end_of_vec; |
| 1105 | struct iovec *i; |
| 1106 | |
| 1107 | DPRINTF("offset=%" PRIi64 " size=%zu", offset, size); |
| 1108 | |
| 1109 | ssh_seek(s, offset, SSH_SEEK_WRITE); |
| 1110 | |
| 1111 | /* This keeps track of the current iovec element ('i'), where we |
| 1112 | * will read from next ('buf'), and the end of the current iovec |
| 1113 | * ('end_of_vec'). |
| 1114 | */ |
| 1115 | i = &qiov->iov[0]; |
| 1116 | buf = i->iov_base; |
| 1117 | end_of_vec = i->iov_base + i->iov_len; |
| 1118 | |
| 1119 | for (written = 0; written < size; ) { |
| 1120 | again: |
| 1121 | DPRINTF("sftp_write buf=%p size=%zu", buf, end_of_vec - buf); |
| 1122 | r = libssh2_sftp_write(s->sftp_handle, buf, end_of_vec - buf); |
| 1123 | DPRINTF("sftp_write returned %zd", r); |
| 1124 | |
| 1125 | if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) { |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1126 | co_yield(s, bs); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1127 | goto again; |
| 1128 | } |
| 1129 | if (r < 0) { |
| 1130 | sftp_error_report(s, "write failed"); |
| 1131 | s->offset = -1; |
| 1132 | return -EIO; |
| 1133 | } |
| 1134 | /* The libssh2 API is very unclear about this. A comment in |
| 1135 | * the code says "nothing was acked, and no EAGAIN was |
| 1136 | * received!" which apparently means that no data got sent |
| 1137 | * out, and the underlying channel didn't return any EAGAIN |
| 1138 | * indication. I think this is a bug in either libssh2 or |
| 1139 | * OpenSSH (server-side). In any case, forcing a seek (to |
| 1140 | * discard libssh2 internal buffers), and then trying again |
| 1141 | * works for me. |
| 1142 | */ |
| 1143 | if (r == 0) { |
| 1144 | ssh_seek(s, offset + written, SSH_SEEK_WRITE|SSH_SEEK_FORCE); |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1145 | co_yield(s, bs); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1146 | goto again; |
| 1147 | } |
| 1148 | |
| 1149 | written += r; |
| 1150 | buf += r; |
| 1151 | s->offset += r; |
| 1152 | if (buf >= end_of_vec && written < size) { |
| 1153 | i++; |
| 1154 | buf = i->iov_base; |
| 1155 | end_of_vec = i->iov_base + i->iov_len; |
| 1156 | } |
| 1157 | |
| 1158 | if (offset + written > s->attrs.filesize) |
| 1159 | s->attrs.filesize = offset + written; |
| 1160 | } |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static coroutine_fn int ssh_co_writev(BlockDriverState *bs, |
| 1166 | int64_t sector_num, |
Eric Blake | e18a58b | 2018-04-24 17:01:57 -0500 | [diff] [blame] | 1167 | int nb_sectors, QEMUIOVector *qiov, |
| 1168 | int flags) |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1169 | { |
| 1170 | BDRVSSHState *s = bs->opaque; |
| 1171 | int ret; |
| 1172 | |
Eric Blake | e18a58b | 2018-04-24 17:01:57 -0500 | [diff] [blame] | 1173 | assert(!flags); |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1174 | qemu_co_mutex_lock(&s->lock); |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1175 | ret = ssh_write(s, bs, sector_num * BDRV_SECTOR_SIZE, |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1176 | nb_sectors * BDRV_SECTOR_SIZE, qiov); |
| 1177 | qemu_co_mutex_unlock(&s->lock); |
| 1178 | |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 1182 | static void unsafe_flush_warning(BDRVSSHState *s, const char *what) |
| 1183 | { |
| 1184 | if (!s->unsafe_flush_warning) { |
Alistair Francis | 3dc6f86 | 2017-07-12 06:57:41 -0700 | [diff] [blame] | 1185 | warn_report("ssh server %s does not support fsync", |
| 1186 | s->inet->host); |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 1187 | if (what) { |
| 1188 | error_report("to support fsync, you need %s", what); |
| 1189 | } |
| 1190 | s->unsafe_flush_warning = true; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | #ifdef HAS_LIBSSH2_SFTP_FSYNC |
| 1195 | |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1196 | static coroutine_fn int ssh_flush(BDRVSSHState *s, BlockDriverState *bs) |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 1197 | { |
| 1198 | int r; |
| 1199 | |
| 1200 | DPRINTF("fsync"); |
| 1201 | again: |
| 1202 | r = libssh2_sftp_fsync(s->sftp_handle); |
| 1203 | if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) { |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1204 | co_yield(s, bs); |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 1205 | goto again; |
| 1206 | } |
| 1207 | if (r == LIBSSH2_ERROR_SFTP_PROTOCOL && |
| 1208 | libssh2_sftp_last_error(s->sftp) == LIBSSH2_FX_OP_UNSUPPORTED) { |
| 1209 | unsafe_flush_warning(s, "OpenSSH >= 6.3"); |
| 1210 | return 0; |
| 1211 | } |
| 1212 | if (r < 0) { |
| 1213 | sftp_error_report(s, "fsync failed"); |
| 1214 | return -EIO; |
| 1215 | } |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | static coroutine_fn int ssh_co_flush(BlockDriverState *bs) |
| 1221 | { |
| 1222 | BDRVSSHState *s = bs->opaque; |
| 1223 | int ret; |
| 1224 | |
| 1225 | qemu_co_mutex_lock(&s->lock); |
Stefan Hajnoczi | 2af0b20 | 2014-05-08 16:34:53 +0200 | [diff] [blame] | 1226 | ret = ssh_flush(s, bs); |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 1227 | qemu_co_mutex_unlock(&s->lock); |
| 1228 | |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | #else /* !HAS_LIBSSH2_SFTP_FSYNC */ |
| 1233 | |
| 1234 | static coroutine_fn int ssh_co_flush(BlockDriverState *bs) |
| 1235 | { |
| 1236 | BDRVSSHState *s = bs->opaque; |
| 1237 | |
| 1238 | unsafe_flush_warning(s, "libssh2 >= 1.4.4"); |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | #endif /* !HAS_LIBSSH2_SFTP_FSYNC */ |
| 1243 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1244 | static int64_t ssh_getlength(BlockDriverState *bs) |
| 1245 | { |
| 1246 | BDRVSSHState *s = bs->opaque; |
| 1247 | int64_t length; |
| 1248 | |
| 1249 | /* Note we cannot make a libssh2 call here. */ |
| 1250 | length = (int64_t) s->attrs.filesize; |
| 1251 | DPRINTF("length=%" PRIi64, length); |
| 1252 | |
| 1253 | return length; |
| 1254 | } |
| 1255 | |
Max Reitz | 624f300 | 2018-02-14 21:49:15 +0100 | [diff] [blame] | 1256 | static int ssh_truncate(BlockDriverState *bs, int64_t offset, |
| 1257 | PreallocMode prealloc, Error **errp) |
| 1258 | { |
| 1259 | BDRVSSHState *s = bs->opaque; |
| 1260 | |
| 1261 | if (prealloc != PREALLOC_MODE_OFF) { |
| 1262 | error_setg(errp, "Unsupported preallocation mode '%s'", |
| 1263 | PreallocMode_str(prealloc)); |
| 1264 | return -ENOTSUP; |
| 1265 | } |
| 1266 | |
| 1267 | if (offset < s->attrs.filesize) { |
| 1268 | error_setg(errp, "ssh driver does not support shrinking files"); |
| 1269 | return -ENOTSUP; |
| 1270 | } |
| 1271 | |
| 1272 | if (offset == s->attrs.filesize) { |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | return ssh_grow_file(s, offset, errp); |
| 1277 | } |
| 1278 | |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1279 | static BlockDriver bdrv_ssh = { |
| 1280 | .format_name = "ssh", |
| 1281 | .protocol_name = "ssh", |
| 1282 | .instance_size = sizeof(BDRVSSHState), |
| 1283 | .bdrv_parse_filename = ssh_parse_filename, |
| 1284 | .bdrv_file_open = ssh_file_open, |
Kevin Wolf | 4906da7 | 2018-02-05 16:24:32 +0100 | [diff] [blame] | 1285 | .bdrv_co_create = ssh_co_create, |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 1286 | .bdrv_co_create_opts = ssh_co_create_opts, |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1287 | .bdrv_close = ssh_close, |
Richard W.M. Jones | 0b3f21e | 2013-06-25 18:15:18 +0100 | [diff] [blame] | 1288 | .bdrv_has_zero_init = ssh_has_zero_init, |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1289 | .bdrv_co_readv = ssh_co_readv, |
| 1290 | .bdrv_co_writev = ssh_co_writev, |
| 1291 | .bdrv_getlength = ssh_getlength, |
Max Reitz | 624f300 | 2018-02-14 21:49:15 +0100 | [diff] [blame] | 1292 | .bdrv_truncate = ssh_truncate, |
Richard W.M. Jones | 9a2d462 | 2013-04-09 15:30:54 +0100 | [diff] [blame] | 1293 | .bdrv_co_flush_to_disk = ssh_co_flush, |
Chunyan Liu | 766181f | 2014-06-05 17:21:06 +0800 | [diff] [blame] | 1294 | .create_opts = &ssh_create_opts, |
Richard W.M. Jones | 0a12ec8 | 2013-04-09 15:30:53 +0100 | [diff] [blame] | 1295 | }; |
| 1296 | |
| 1297 | static void bdrv_ssh_init(void) |
| 1298 | { |
| 1299 | int r; |
| 1300 | |
| 1301 | r = libssh2_init(0); |
| 1302 | if (r != 0) { |
| 1303 | fprintf(stderr, "libssh2 initialization failed, %d\n", r); |
| 1304 | exit(EXIT_FAILURE); |
| 1305 | } |
| 1306 | |
| 1307 | bdrv_register(&bdrv_ssh); |
| 1308 | } |
| 1309 | |
| 1310 | block_init(bdrv_ssh_init); |