]>
Commit | Line | Data |
---|---|---|
0a12ec87 RJ |
1 | /* |
2 | * Secure Shell (ssh) backend for QEMU. | |
3 | * | |
4 | * Copyright (C) 2013 Red Hat Inc., Richard W.M. Jones <[email protected]> | |
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 | ||
80c71a24 | 25 | #include "qemu/osdep.h" |
0a12ec87 | 26 | |
b10d49d7 PT |
27 | #include <libssh/libssh.h> |
28 | #include <libssh/sftp.h> | |
0a12ec87 RJ |
29 | |
30 | #include "block/block_int.h" | |
609f45ea | 31 | #include "block/qdict.h" |
da34e65c | 32 | #include "qapi/error.h" |
d49b6836 | 33 | #include "qemu/error-report.h" |
0b8fa32f | 34 | #include "qemu/module.h" |
922a01a0 | 35 | #include "qemu/option.h" |
856dfd8a | 36 | #include "qemu/ctype.h" |
0da5b8ef | 37 | #include "qemu/cutils.h" |
0a12ec87 RJ |
38 | #include "qemu/sockets.h" |
39 | #include "qemu/uri.h" | |
9af23989 | 40 | #include "qapi/qapi-visit-sockets.h" |
16e4bdb1 | 41 | #include "qapi/qapi-visit-block-core.h" |
452fcdbc | 42 | #include "qapi/qmp/qdict.h" |
d49b6836 | 43 | #include "qapi/qmp/qstring.h" |
0da5b8ef AA |
44 | #include "qapi/qobject-input-visitor.h" |
45 | #include "qapi/qobject-output-visitor.h" | |
023908a2 | 46 | #include "trace.h" |
0a12ec87 | 47 | |
023908a2 | 48 | /* |
b10d49d7 PT |
49 | * TRACE_LIBSSH=<level> enables tracing in libssh itself. |
50 | * The meaning of <level> is described here: | |
51 | * http://api.libssh.org/master/group__libssh__log.html | |
0a12ec87 | 52 | */ |
b10d49d7 | 53 | #define TRACE_LIBSSH 0 /* see: SSH_LOG_* */ |
0a12ec87 | 54 | |
0a12ec87 RJ |
55 | typedef struct BDRVSSHState { |
56 | /* Coroutine. */ | |
57 | CoMutex lock; | |
58 | ||
59 | /* SSH connection. */ | |
60 | int sock; /* socket */ | |
b10d49d7 PT |
61 | ssh_session session; /* ssh session */ |
62 | sftp_session sftp; /* sftp session */ | |
63 | sftp_file sftp_handle; /* sftp remote file handle */ | |
0a12ec87 | 64 | |
b10d49d7 PT |
65 | /* |
66 | * File attributes at open. We try to keep the .size field | |
0a12ec87 RJ |
67 | * updated if it changes (eg by writing at the end of the file). |
68 | */ | |
b10d49d7 | 69 | sftp_attributes attrs; |
9a2d462e | 70 | |
0da5b8ef AA |
71 | InetSocketAddress *inet; |
72 | ||
9a2d462e | 73 | /* Used to warn if 'flush' is not supported. */ |
9a2d462e | 74 | bool unsafe_flush_warning; |
b8c1f901 HR |
75 | |
76 | /* | |
77 | * Store the user name for ssh_refresh_filename() because the | |
78 | * default depends on the system you are on -- therefore, when we | |
79 | * generate a filename, it should always contain the user name we | |
80 | * are actually using. | |
81 | */ | |
82 | char *user; | |
0a12ec87 RJ |
83 | } BDRVSSHState; |
84 | ||
85 | static void ssh_state_init(BDRVSSHState *s) | |
86 | { | |
87 | memset(s, 0, sizeof *s); | |
88 | s->sock = -1; | |
0a12ec87 RJ |
89 | qemu_co_mutex_init(&s->lock); |
90 | } | |
91 | ||
92 | static void ssh_state_free(BDRVSSHState *s) | |
93 | { | |
b8c1f901 HR |
94 | g_free(s->user); |
95 | ||
b10d49d7 PT |
96 | if (s->attrs) { |
97 | sftp_attributes_free(s->attrs); | |
98 | } | |
0a12ec87 | 99 | if (s->sftp_handle) { |
b10d49d7 | 100 | sftp_close(s->sftp_handle); |
0a12ec87 RJ |
101 | } |
102 | if (s->sftp) { | |
b10d49d7 | 103 | sftp_free(s->sftp); |
0a12ec87 RJ |
104 | } |
105 | if (s->session) { | |
b10d49d7 PT |
106 | ssh_disconnect(s->session); |
107 | ssh_free(s->session); /* This frees s->sock */ | |
0a12ec87 RJ |
108 | } |
109 | } | |
110 | ||
01c2b265 MA |
111 | static void GCC_FMT_ATTR(3, 4) |
112 | session_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...) | |
113 | { | |
114 | va_list args; | |
115 | char *msg; | |
116 | ||
117 | va_start(args, fs); | |
118 | msg = g_strdup_vprintf(fs, args); | |
119 | va_end(args); | |
120 | ||
121 | if (s->session) { | |
b10d49d7 | 122 | const char *ssh_err; |
01c2b265 MA |
123 | int ssh_err_code; |
124 | ||
b10d49d7 PT |
125 | /* This is not an errno. See <libssh/libssh.h>. */ |
126 | ssh_err = ssh_get_error(s->session); | |
127 | ssh_err_code = ssh_get_error_code(s->session); | |
128 | error_setg(errp, "%s: %s (libssh error code: %d)", | |
01c2b265 MA |
129 | msg, ssh_err, ssh_err_code); |
130 | } else { | |
131 | error_setg(errp, "%s", msg); | |
132 | } | |
133 | g_free(msg); | |
134 | } | |
135 | ||
5496fb1a MA |
136 | static void GCC_FMT_ATTR(3, 4) |
137 | sftp_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...) | |
0a12ec87 RJ |
138 | { |
139 | va_list args; | |
5496fb1a | 140 | char *msg; |
0a12ec87 RJ |
141 | |
142 | va_start(args, fs); | |
5496fb1a MA |
143 | msg = g_strdup_vprintf(fs, args); |
144 | va_end(args); | |
0a12ec87 | 145 | |
5496fb1a | 146 | if (s->sftp) { |
b10d49d7 | 147 | const char *ssh_err; |
0a12ec87 | 148 | int ssh_err_code; |
b10d49d7 | 149 | int sftp_err_code; |
0a12ec87 | 150 | |
b10d49d7 PT |
151 | /* This is not an errno. See <libssh/libssh.h>. */ |
152 | ssh_err = ssh_get_error(s->session); | |
153 | ssh_err_code = ssh_get_error_code(s->session); | |
154 | /* See <libssh/sftp.h>. */ | |
155 | sftp_err_code = sftp_get_error(s->sftp); | |
0a12ec87 | 156 | |
5496fb1a | 157 | error_setg(errp, |
b10d49d7 | 158 | "%s: %s (libssh error code: %d, sftp error code: %d)", |
5496fb1a MA |
159 | msg, ssh_err, ssh_err_code, sftp_err_code); |
160 | } else { | |
161 | error_setg(errp, "%s", msg); | |
162 | } | |
163 | g_free(msg); | |
0a12ec87 RJ |
164 | } |
165 | ||
6b3048ce | 166 | static void sftp_error_trace(BDRVSSHState *s, const char *op) |
0a12ec87 | 167 | { |
b10d49d7 | 168 | const char *ssh_err; |
6b3048ce | 169 | int ssh_err_code; |
b10d49d7 | 170 | int sftp_err_code; |
0a12ec87 | 171 | |
b10d49d7 PT |
172 | /* This is not an errno. See <libssh/libssh.h>. */ |
173 | ssh_err = ssh_get_error(s->session); | |
174 | ssh_err_code = ssh_get_error_code(s->session); | |
175 | /* See <libssh/sftp.h>. */ | |
176 | sftp_err_code = sftp_get_error(s->sftp); | |
0a12ec87 | 177 | |
6b3048ce | 178 | trace_sftp_error(op, ssh_err, ssh_err_code, sftp_err_code); |
0a12ec87 RJ |
179 | } |
180 | ||
181 | static int parse_uri(const char *filename, QDict *options, Error **errp) | |
182 | { | |
183 | URI *uri = NULL; | |
eab2ac9d | 184 | QueryParams *qp; |
1059f1bb | 185 | char *port_str; |
0a12ec87 RJ |
186 | int i; |
187 | ||
188 | uri = uri_parse(filename); | |
189 | if (!uri) { | |
190 | return -EINVAL; | |
191 | } | |
192 | ||
f69165a8 | 193 | if (g_strcmp0(uri->scheme, "ssh") != 0) { |
0a12ec87 RJ |
194 | error_setg(errp, "URI scheme must be 'ssh'"); |
195 | goto err; | |
196 | } | |
197 | ||
198 | if (!uri->server || strcmp(uri->server, "") == 0) { | |
199 | error_setg(errp, "missing hostname in URI"); | |
200 | goto err; | |
201 | } | |
202 | ||
203 | if (!uri->path || strcmp(uri->path, "") == 0) { | |
204 | error_setg(errp, "missing remote path in URI"); | |
205 | goto err; | |
206 | } | |
207 | ||
208 | qp = query_params_parse(uri->query); | |
209 | if (!qp) { | |
210 | error_setg(errp, "could not parse query parameters"); | |
211 | goto err; | |
212 | } | |
213 | ||
214 | if(uri->user && strcmp(uri->user, "") != 0) { | |
46f5ac20 | 215 | qdict_put_str(options, "user", uri->user); |
0a12ec87 RJ |
216 | } |
217 | ||
46f5ac20 | 218 | qdict_put_str(options, "server.host", uri->server); |
0a12ec87 | 219 | |
1059f1bb | 220 | port_str = g_strdup_printf("%d", uri->port ?: 22); |
46f5ac20 | 221 | qdict_put_str(options, "server.port", port_str); |
1059f1bb | 222 | g_free(port_str); |
0a12ec87 | 223 | |
46f5ac20 | 224 | qdict_put_str(options, "path", uri->path); |
0a12ec87 RJ |
225 | |
226 | /* Pick out any query parameters that we understand, and ignore | |
227 | * the rest. | |
228 | */ | |
229 | for (i = 0; i < qp->n; ++i) { | |
230 | if (strcmp(qp->p[i].name, "host_key_check") == 0) { | |
46f5ac20 | 231 | qdict_put_str(options, "host_key_check", qp->p[i].value); |
0a12ec87 RJ |
232 | } |
233 | } | |
234 | ||
235 | query_params_free(qp); | |
236 | uri_free(uri); | |
237 | return 0; | |
238 | ||
239 | err: | |
0a12ec87 RJ |
240 | if (uri) { |
241 | uri_free(uri); | |
242 | } | |
243 | return -EINVAL; | |
244 | } | |
245 | ||
89dbe180 AA |
246 | static bool ssh_has_filename_options_conflict(QDict *options, Error **errp) |
247 | { | |
248 | const QDictEntry *qe; | |
249 | ||
250 | for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) { | |
251 | if (!strcmp(qe->key, "host") || | |
252 | !strcmp(qe->key, "port") || | |
253 | !strcmp(qe->key, "path") || | |
254 | !strcmp(qe->key, "user") || | |
0da5b8ef AA |
255 | !strcmp(qe->key, "host_key_check") || |
256 | strstart(qe->key, "server.", NULL)) | |
89dbe180 AA |
257 | { |
258 | error_setg(errp, "Option '%s' cannot be used with a file name", | |
259 | qe->key); | |
260 | return true; | |
261 | } | |
262 | } | |
263 | ||
264 | return false; | |
265 | } | |
266 | ||
0a12ec87 RJ |
267 | static void ssh_parse_filename(const char *filename, QDict *options, |
268 | Error **errp) | |
269 | { | |
89dbe180 | 270 | if (ssh_has_filename_options_conflict(options, errp)) { |
0a12ec87 RJ |
271 | return; |
272 | } | |
273 | ||
274 | parse_uri(filename, options, errp); | |
275 | } | |
276 | ||
b10d49d7 | 277 | static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp) |
0a12ec87 | 278 | { |
b10d49d7 PT |
279 | int ret; |
280 | #ifdef HAVE_LIBSSH_0_8 | |
281 | enum ssh_known_hosts_e state; | |
282 | int r; | |
283 | ssh_key pubkey; | |
284 | enum ssh_keytypes_e pubkey_type; | |
285 | unsigned char *server_hash = NULL; | |
286 | size_t server_hash_len; | |
287 | char *fingerprint = NULL; | |
288 | ||
289 | state = ssh_session_is_known_server(s->session); | |
290 | trace_ssh_server_status(state); | |
291 | ||
292 | switch (state) { | |
293 | case SSH_KNOWN_HOSTS_OK: | |
294 | /* OK */ | |
295 | trace_ssh_check_host_key_knownhosts(); | |
296 | break; | |
297 | case SSH_KNOWN_HOSTS_CHANGED: | |
0a12ec87 | 298 | ret = -EINVAL; |
b10d49d7 PT |
299 | r = ssh_get_server_publickey(s->session, &pubkey); |
300 | if (r == 0) { | |
301 | r = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256, | |
302 | &server_hash, &server_hash_len); | |
303 | pubkey_type = ssh_key_type(pubkey); | |
304 | ssh_key_free(pubkey); | |
305 | } | |
306 | if (r == 0) { | |
307 | fingerprint = ssh_get_fingerprint_hash(SSH_PUBLICKEY_HASH_SHA256, | |
308 | server_hash, | |
309 | server_hash_len); | |
310 | ssh_clean_pubkey_hash(&server_hash); | |
311 | } | |
312 | if (fingerprint) { | |
313 | error_setg(errp, | |
314 | "host key (%s key with fingerprint %s) does not match " | |
315 | "the one in known_hosts; this may be a possible attack", | |
316 | ssh_key_type_to_char(pubkey_type), fingerprint); | |
317 | ssh_string_free_char(fingerprint); | |
318 | } else { | |
319 | error_setg(errp, | |
320 | "host key does not match the one in known_hosts; this " | |
321 | "may be a possible attack"); | |
322 | } | |
0a12ec87 | 323 | goto out; |
b10d49d7 | 324 | case SSH_KNOWN_HOSTS_OTHER: |
0a12ec87 | 325 | ret = -EINVAL; |
b10d49d7 PT |
326 | error_setg(errp, |
327 | "host key for this server not found, another type exists"); | |
328 | goto out; | |
329 | case SSH_KNOWN_HOSTS_UNKNOWN: | |
330 | ret = -EINVAL; | |
331 | error_setg(errp, "no host key was found in known_hosts"); | |
332 | goto out; | |
333 | case SSH_KNOWN_HOSTS_NOT_FOUND: | |
334 | ret = -ENOENT; | |
335 | error_setg(errp, "known_hosts file not found"); | |
336 | goto out; | |
337 | case SSH_KNOWN_HOSTS_ERROR: | |
338 | ret = -EINVAL; | |
339 | error_setg(errp, "error while checking the host"); | |
340 | goto out; | |
341 | default: | |
342 | ret = -EINVAL; | |
343 | error_setg(errp, "error while checking for known server (%d)", state); | |
0a12ec87 RJ |
344 | goto out; |
345 | } | |
b10d49d7 PT |
346 | #else /* !HAVE_LIBSSH_0_8 */ |
347 | int state; | |
0a12ec87 | 348 | |
b10d49d7 PT |
349 | state = ssh_is_server_known(s->session); |
350 | trace_ssh_server_status(state); | |
0a12ec87 | 351 | |
b10d49d7 PT |
352 | switch (state) { |
353 | case SSH_SERVER_KNOWN_OK: | |
0a12ec87 | 354 | /* OK */ |
b10d49d7 | 355 | trace_ssh_check_host_key_knownhosts(); |
0a12ec87 | 356 | break; |
b10d49d7 | 357 | case SSH_SERVER_KNOWN_CHANGED: |
0a12ec87 | 358 | ret = -EINVAL; |
b10d49d7 PT |
359 | error_setg(errp, |
360 | "host key does not match the one in known_hosts; this " | |
361 | "may be a possible attack"); | |
0a12ec87 | 362 | goto out; |
b10d49d7 | 363 | case SSH_SERVER_FOUND_OTHER: |
0a12ec87 | 364 | ret = -EINVAL; |
b10d49d7 PT |
365 | error_setg(errp, |
366 | "host key for this server not found, another type exists"); | |
367 | goto out; | |
368 | case SSH_SERVER_FILE_NOT_FOUND: | |
369 | ret = -ENOENT; | |
370 | error_setg(errp, "known_hosts file not found"); | |
0a12ec87 | 371 | goto out; |
b10d49d7 | 372 | case SSH_SERVER_NOT_KNOWN: |
0a12ec87 | 373 | ret = -EINVAL; |
b10d49d7 PT |
374 | error_setg(errp, "no host key was found in known_hosts"); |
375 | goto out; | |
376 | case SSH_SERVER_ERROR: | |
377 | ret = -EINVAL; | |
378 | error_setg(errp, "server error"); | |
0a12ec87 RJ |
379 | goto out; |
380 | default: | |
381 | ret = -EINVAL; | |
b10d49d7 | 382 | error_setg(errp, "error while checking for known server (%d)", state); |
0a12ec87 RJ |
383 | goto out; |
384 | } | |
b10d49d7 | 385 | #endif /* !HAVE_LIBSSH_0_8 */ |
0a12ec87 RJ |
386 | |
387 | /* known_hosts checking successful. */ | |
388 | ret = 0; | |
389 | ||
390 | out: | |
0a12ec87 RJ |
391 | return ret; |
392 | } | |
393 | ||
394 | static unsigned hex2decimal(char ch) | |
395 | { | |
396 | if (ch >= '0' && ch <= '9') { | |
397 | return (ch - '0'); | |
398 | } else if (ch >= 'a' && ch <= 'f') { | |
399 | return 10 + (ch - 'a'); | |
400 | } else if (ch >= 'A' && ch <= 'F') { | |
401 | return 10 + (ch - 'A'); | |
402 | } | |
403 | ||
404 | return -1; | |
405 | } | |
406 | ||
407 | /* Compare the binary fingerprint (hash of host key) with the | |
408 | * host_key_check parameter. | |
409 | */ | |
410 | static int compare_fingerprint(const unsigned char *fingerprint, size_t len, | |
411 | const char *host_key_check) | |
412 | { | |
413 | unsigned c; | |
414 | ||
415 | while (len > 0) { | |
416 | while (*host_key_check == ':') | |
417 | host_key_check++; | |
418 | if (!qemu_isxdigit(host_key_check[0]) || | |
419 | !qemu_isxdigit(host_key_check[1])) | |
420 | return 1; | |
421 | c = hex2decimal(host_key_check[0]) * 16 + | |
422 | hex2decimal(host_key_check[1]); | |
423 | if (c - *fingerprint != 0) | |
424 | return c - *fingerprint; | |
425 | fingerprint++; | |
426 | len--; | |
427 | host_key_check += 2; | |
428 | } | |
429 | return *host_key_check - '\0'; | |
430 | } | |
431 | ||
432 | static int | |
433 | check_host_key_hash(BDRVSSHState *s, const char *hash, | |
b10d49d7 | 434 | enum ssh_publickey_hash_type type, Error **errp) |
0a12ec87 | 435 | { |
b10d49d7 PT |
436 | int r; |
437 | ssh_key pubkey; | |
438 | unsigned char *server_hash; | |
439 | size_t server_hash_len; | |
440 | ||
441 | #ifdef HAVE_LIBSSH_0_8 | |
442 | r = ssh_get_server_publickey(s->session, &pubkey); | |
443 | #else | |
444 | r = ssh_get_publickey(s->session, &pubkey); | |
445 | #endif | |
446 | if (r != SSH_OK) { | |
01c2b265 | 447 | session_error_setg(errp, s, "failed to read remote host key"); |
0a12ec87 RJ |
448 | return -EINVAL; |
449 | } | |
450 | ||
b10d49d7 PT |
451 | r = ssh_get_publickey_hash(pubkey, type, &server_hash, &server_hash_len); |
452 | ssh_key_free(pubkey); | |
453 | if (r != 0) { | |
454 | session_error_setg(errp, s, | |
455 | "failed reading the hash of the server SSH key"); | |
456 | return -EINVAL; | |
457 | } | |
458 | ||
459 | r = compare_fingerprint(server_hash, server_hash_len, hash); | |
460 | ssh_clean_pubkey_hash(&server_hash); | |
461 | if (r != 0) { | |
01c2b265 MA |
462 | error_setg(errp, "remote host key does not match host_key_check '%s'", |
463 | hash); | |
0a12ec87 RJ |
464 | return -EPERM; |
465 | } | |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
b10d49d7 | 470 | static int check_host_key(BDRVSSHState *s, SshHostKeyCheck *hkc, Error **errp) |
0a12ec87 | 471 | { |
ec2f5418 | 472 | SshHostKeyCheckMode mode; |
0a12ec87 | 473 | |
ec2f5418 KW |
474 | if (hkc) { |
475 | mode = hkc->mode; | |
476 | } else { | |
477 | mode = SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS; | |
0a12ec87 RJ |
478 | } |
479 | ||
ec2f5418 KW |
480 | switch (mode) { |
481 | case SSH_HOST_KEY_CHECK_MODE_NONE: | |
482 | return 0; | |
483 | case SSH_HOST_KEY_CHECK_MODE_HASH: | |
484 | if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_MD5) { | |
485 | return check_host_key_hash(s, hkc->u.hash.hash, | |
b10d49d7 | 486 | SSH_PUBLICKEY_HASH_MD5, errp); |
ec2f5418 KW |
487 | } else if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_SHA1) { |
488 | return check_host_key_hash(s, hkc->u.hash.hash, | |
b10d49d7 | 489 | SSH_PUBLICKEY_HASH_SHA1, errp); |
ec2f5418 KW |
490 | } |
491 | g_assert_not_reached(); | |
492 | break; | |
493 | case SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS: | |
b10d49d7 | 494 | return check_host_key_knownhosts(s, errp); |
ec2f5418 KW |
495 | default: |
496 | g_assert_not_reached(); | |
0a12ec87 RJ |
497 | } |
498 | ||
0a12ec87 RJ |
499 | return -EINVAL; |
500 | } | |
501 | ||
b10d49d7 | 502 | static int authenticate(BDRVSSHState *s, Error **errp) |
0a12ec87 RJ |
503 | { |
504 | int r, ret; | |
b10d49d7 | 505 | int method; |
0a12ec87 | 506 | |
b10d49d7 PT |
507 | /* Try to authenticate with the "none" method. */ |
508 | r = ssh_userauth_none(s->session, NULL); | |
509 | if (r == SSH_AUTH_ERROR) { | |
0a12ec87 | 510 | ret = -EPERM; |
b10d49d7 PT |
511 | session_error_setg(errp, s, "failed to authenticate using none " |
512 | "authentication"); | |
0a12ec87 | 513 | goto out; |
b10d49d7 PT |
514 | } else if (r == SSH_AUTH_SUCCESS) { |
515 | /* Authenticated! */ | |
516 | ret = 0; | |
0a12ec87 RJ |
517 | goto out; |
518 | } | |
519 | ||
b10d49d7 PT |
520 | method = ssh_userauth_list(s->session, NULL); |
521 | trace_ssh_auth_methods(method); | |
522 | ||
523 | /* | |
524 | * Try to authenticate with publickey, using the ssh-agent | |
525 | * if available. | |
526 | */ | |
527 | if (method & SSH_AUTH_METHOD_PUBLICKEY) { | |
528 | r = ssh_userauth_publickey_auto(s->session, NULL, NULL); | |
529 | if (r == SSH_AUTH_ERROR) { | |
0a12ec87 | 530 | ret = -EINVAL; |
b10d49d7 PT |
531 | session_error_setg(errp, s, "failed to authenticate using " |
532 | "publickey authentication"); | |
0a12ec87 | 533 | goto out; |
b10d49d7 | 534 | } else if (r == SSH_AUTH_SUCCESS) { |
0a12ec87 RJ |
535 | /* Authenticated! */ |
536 | ret = 0; | |
537 | goto out; | |
538 | } | |
0a12ec87 RJ |
539 | } |
540 | ||
541 | ret = -EPERM; | |
4618e658 MA |
542 | error_setg(errp, "failed to authenticate using publickey authentication " |
543 | "and the identities held by your ssh-agent"); | |
0a12ec87 RJ |
544 | |
545 | out: | |
0a12ec87 RJ |
546 | return ret; |
547 | } | |
548 | ||
8a6a8089 HR |
549 | static QemuOptsList ssh_runtime_opts = { |
550 | .name = "ssh", | |
551 | .head = QTAILQ_HEAD_INITIALIZER(ssh_runtime_opts.head), | |
552 | .desc = { | |
553 | { | |
554 | .name = "host", | |
555 | .type = QEMU_OPT_STRING, | |
556 | .help = "Host to connect to", | |
557 | }, | |
558 | { | |
559 | .name = "port", | |
560 | .type = QEMU_OPT_NUMBER, | |
561 | .help = "Port to connect to", | |
562 | }, | |
ec2f5418 KW |
563 | { |
564 | .name = "host_key_check", | |
565 | .type = QEMU_OPT_STRING, | |
566 | .help = "Defines how and what to check the host key against", | |
567 | }, | |
fbd5c4c0 | 568 | { /* end of list */ } |
8a6a8089 HR |
569 | }, |
570 | }; | |
571 | ||
ec2f5418 KW |
572 | static bool ssh_process_legacy_options(QDict *output_opts, |
573 | QemuOpts *legacy_opts, | |
574 | Error **errp) | |
0da5b8ef AA |
575 | { |
576 | const char *host = qemu_opt_get(legacy_opts, "host"); | |
577 | const char *port = qemu_opt_get(legacy_opts, "port"); | |
ec2f5418 | 578 | const char *host_key_check = qemu_opt_get(legacy_opts, "host_key_check"); |
0da5b8ef AA |
579 | |
580 | if (!host && port) { | |
581 | error_setg(errp, "port may not be used without host"); | |
582 | return false; | |
583 | } | |
584 | ||
585 | if (host) { | |
46f5ac20 EB |
586 | qdict_put_str(output_opts, "server.host", host); |
587 | qdict_put_str(output_opts, "server.port", port ?: stringify(22)); | |
0da5b8ef AA |
588 | } |
589 | ||
ec2f5418 KW |
590 | if (host_key_check) { |
591 | if (strcmp(host_key_check, "no") == 0) { | |
592 | qdict_put_str(output_opts, "host-key-check.mode", "none"); | |
593 | } else if (strncmp(host_key_check, "md5:", 4) == 0) { | |
594 | qdict_put_str(output_opts, "host-key-check.mode", "hash"); | |
595 | qdict_put_str(output_opts, "host-key-check.type", "md5"); | |
596 | qdict_put_str(output_opts, "host-key-check.hash", | |
597 | &host_key_check[4]); | |
598 | } else if (strncmp(host_key_check, "sha1:", 5) == 0) { | |
599 | qdict_put_str(output_opts, "host-key-check.mode", "hash"); | |
600 | qdict_put_str(output_opts, "host-key-check.type", "sha1"); | |
601 | qdict_put_str(output_opts, "host-key-check.hash", | |
602 | &host_key_check[5]); | |
603 | } else if (strcmp(host_key_check, "yes") == 0) { | |
604 | qdict_put_str(output_opts, "host-key-check.mode", "known_hosts"); | |
605 | } else { | |
606 | error_setg(errp, "unknown host_key_check setting (%s)", | |
607 | host_key_check); | |
608 | return false; | |
609 | } | |
610 | } | |
611 | ||
0da5b8ef AA |
612 | return true; |
613 | } | |
614 | ||
16e4bdb1 | 615 | static BlockdevOptionsSsh *ssh_parse_options(QDict *options, Error **errp) |
0da5b8ef | 616 | { |
16e4bdb1 KW |
617 | BlockdevOptionsSsh *result = NULL; |
618 | QemuOpts *opts = NULL; | |
16e4bdb1 KW |
619 | const QDictEntry *e; |
620 | Visitor *v; | |
621 | ||
622 | /* Translate legacy options */ | |
623 | opts = qemu_opts_create(&ssh_runtime_opts, NULL, 0, &error_abort); | |
af175e85 | 624 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
16e4bdb1 | 625 | goto fail; |
0da5b8ef AA |
626 | } |
627 | ||
ec2f5418 | 628 | if (!ssh_process_legacy_options(options, opts, errp)) { |
16e4bdb1 KW |
629 | goto fail; |
630 | } | |
631 | ||
632 | /* Create the QAPI object */ | |
af91062e MA |
633 | v = qobject_input_visitor_new_flat_confused(options, errp); |
634 | if (!v) { | |
16e4bdb1 | 635 | goto fail; |
0da5b8ef AA |
636 | } |
637 | ||
b11a093c | 638 | visit_type_BlockdevOptionsSsh(v, NULL, &result, errp); |
16e4bdb1 | 639 | visit_free(v); |
b11a093c | 640 | if (!result) { |
16e4bdb1 | 641 | goto fail; |
0da5b8ef AA |
642 | } |
643 | ||
16e4bdb1 KW |
644 | /* Remove the processed options from the QDict (the visitor processes |
645 | * _all_ options in the QDict) */ | |
646 | while ((e = qdict_first(options))) { | |
647 | qdict_del(options, e->key); | |
648 | } | |
649 | ||
650 | fail: | |
651 | qemu_opts_del(opts); | |
652 | return result; | |
0da5b8ef AA |
653 | } |
654 | ||
375f0b92 | 655 | static int connect_to_ssh(BDRVSSHState *s, BlockdevOptionsSsh *opts, |
5f0c39e5 | 656 | int ssh_flags, int creat_mode, Error **errp) |
0a12ec87 RJ |
657 | { |
658 | int r, ret; | |
b10d49d7 PT |
659 | unsigned int port = 0; |
660 | int new_sock = -1; | |
0a12ec87 | 661 | |
16e4bdb1 | 662 | if (opts->has_user) { |
b8c1f901 | 663 | s->user = g_strdup(opts->user); |
16e4bdb1 | 664 | } else { |
b8c1f901 HR |
665 | s->user = g_strdup(g_get_user_name()); |
666 | if (!s->user) { | |
5f0c39e5 | 667 | error_setg_errno(errp, errno, "Can't get user name"); |
0a12ec87 RJ |
668 | ret = -errno; |
669 | goto err; | |
670 | } | |
671 | } | |
672 | ||
0da5b8ef | 673 | /* Pop the config into our state object, Exit if invalid */ |
16e4bdb1 KW |
674 | s->inet = opts->server; |
675 | opts->server = NULL; | |
0da5b8ef | 676 | |
b10d49d7 | 677 | if (qemu_strtoui(s->inet->port, NULL, 10, &port) < 0) { |
0da5b8ef AA |
678 | error_setg(errp, "Use only numeric port value"); |
679 | ret = -EINVAL; | |
680 | goto err; | |
681 | } | |
9a2d462e | 682 | |
0a12ec87 | 683 | /* Open the socket and connect. */ |
b10d49d7 PT |
684 | new_sock = inet_connect_saddr(s->inet, errp); |
685 | if (new_sock < 0) { | |
325e3904 | 686 | ret = -EIO; |
0a12ec87 RJ |
687 | goto err; |
688 | } | |
689 | ||
b10d49d7 PT |
690 | /* |
691 | * Try to disable the Nagle algorithm on TCP sockets to reduce latency, | |
692 | * but do not fail if it cannot be disabled. | |
693 | */ | |
694 | r = socket_set_nodelay(new_sock); | |
695 | if (r < 0) { | |
696 | warn_report("can't set TCP_NODELAY for the ssh server %s: %s", | |
697 | s->inet->host, strerror(errno)); | |
698 | } | |
699 | ||
0a12ec87 | 700 | /* Create SSH session. */ |
b10d49d7 | 701 | s->session = ssh_new(); |
0a12ec87 RJ |
702 | if (!s->session) { |
703 | ret = -EINVAL; | |
b10d49d7 | 704 | session_error_setg(errp, s, "failed to initialize libssh session"); |
0a12ec87 RJ |
705 | goto err; |
706 | } | |
707 | ||
b10d49d7 PT |
708 | /* |
709 | * Make sure we are in blocking mode during the connection and | |
710 | * authentication phases. | |
711 | */ | |
712 | ssh_set_blocking(s->session, 1); | |
0a12ec87 | 713 | |
b10d49d7 PT |
714 | r = ssh_options_set(s->session, SSH_OPTIONS_USER, s->user); |
715 | if (r < 0) { | |
716 | ret = -EINVAL; | |
717 | session_error_setg(errp, s, | |
718 | "failed to set the user in the libssh session"); | |
719 | goto err; | |
720 | } | |
721 | ||
722 | r = ssh_options_set(s->session, SSH_OPTIONS_HOST, s->inet->host); | |
723 | if (r < 0) { | |
724 | ret = -EINVAL; | |
725 | session_error_setg(errp, s, | |
726 | "failed to set the host in the libssh session"); | |
727 | goto err; | |
728 | } | |
729 | ||
730 | if (port > 0) { | |
731 | r = ssh_options_set(s->session, SSH_OPTIONS_PORT, &port); | |
732 | if (r < 0) { | |
733 | ret = -EINVAL; | |
734 | session_error_setg(errp, s, | |
735 | "failed to set the port in the libssh session"); | |
736 | goto err; | |
737 | } | |
738 | } | |
739 | ||
740 | r = ssh_options_set(s->session, SSH_OPTIONS_COMPRESSION, "none"); | |
741 | if (r < 0) { | |
742 | ret = -EINVAL; | |
743 | session_error_setg(errp, s, | |
744 | "failed to disable the compression in the libssh " | |
745 | "session"); | |
746 | goto err; | |
747 | } | |
748 | ||
749 | /* Read ~/.ssh/config. */ | |
750 | r = ssh_options_parse_config(s->session, NULL); | |
751 | if (r < 0) { | |
752 | ret = -EINVAL; | |
753 | session_error_setg(errp, s, "failed to parse ~/.ssh/config"); | |
754 | goto err; | |
755 | } | |
756 | ||
757 | r = ssh_options_set(s->session, SSH_OPTIONS_FD, &new_sock); | |
758 | if (r < 0) { | |
759 | ret = -EINVAL; | |
760 | session_error_setg(errp, s, | |
761 | "failed to set the socket in the libssh session"); | |
762 | goto err; | |
763 | } | |
764 | /* libssh took ownership of the socket. */ | |
765 | s->sock = new_sock; | |
766 | new_sock = -1; | |
767 | ||
768 | /* Connect. */ | |
769 | r = ssh_connect(s->session); | |
770 | if (r != SSH_OK) { | |
0a12ec87 | 771 | ret = -EINVAL; |
5f0c39e5 | 772 | session_error_setg(errp, s, "failed to establish SSH session"); |
0a12ec87 RJ |
773 | goto err; |
774 | } | |
775 | ||
776 | /* Check the remote host's key against known_hosts. */ | |
b10d49d7 | 777 | ret = check_host_key(s, opts->host_key_check, errp); |
0a12ec87 RJ |
778 | if (ret < 0) { |
779 | goto err; | |
780 | } | |
781 | ||
782 | /* Authenticate. */ | |
b10d49d7 | 783 | ret = authenticate(s, errp); |
0a12ec87 RJ |
784 | if (ret < 0) { |
785 | goto err; | |
786 | } | |
787 | ||
788 | /* Start SFTP. */ | |
b10d49d7 | 789 | s->sftp = sftp_new(s->session); |
0a12ec87 | 790 | if (!s->sftp) { |
b10d49d7 PT |
791 | session_error_setg(errp, s, "failed to create sftp handle"); |
792 | ret = -EINVAL; | |
793 | goto err; | |
794 | } | |
795 | ||
796 | r = sftp_init(s->sftp); | |
797 | if (r < 0) { | |
798 | sftp_error_setg(errp, s, "failed to initialize sftp handle"); | |
0a12ec87 RJ |
799 | ret = -EINVAL; |
800 | goto err; | |
801 | } | |
802 | ||
803 | /* Open the remote file. */ | |
023908a2 | 804 | trace_ssh_connect_to_ssh(opts->path, ssh_flags, creat_mode); |
b10d49d7 | 805 | s->sftp_handle = sftp_open(s->sftp, opts->path, ssh_flags, creat_mode); |
0a12ec87 | 806 | if (!s->sftp_handle) { |
b10d49d7 PT |
807 | sftp_error_setg(errp, s, "failed to open remote file '%s'", |
808 | opts->path); | |
0a12ec87 RJ |
809 | ret = -EINVAL; |
810 | goto err; | |
811 | } | |
812 | ||
b10d49d7 PT |
813 | /* Make sure the SFTP file is handled in blocking mode. */ |
814 | sftp_file_set_blocking(s->sftp_handle); | |
815 | ||
816 | s->attrs = sftp_fstat(s->sftp_handle); | |
817 | if (!s->attrs) { | |
5496fb1a | 818 | sftp_error_setg(errp, s, "failed to read file attributes"); |
0a12ec87 RJ |
819 | return -EINVAL; |
820 | } | |
821 | ||
0a12ec87 RJ |
822 | return 0; |
823 | ||
824 | err: | |
b10d49d7 PT |
825 | if (s->attrs) { |
826 | sftp_attributes_free(s->attrs); | |
827 | } | |
828 | s->attrs = NULL; | |
0a12ec87 | 829 | if (s->sftp_handle) { |
b10d49d7 | 830 | sftp_close(s->sftp_handle); |
0a12ec87 RJ |
831 | } |
832 | s->sftp_handle = NULL; | |
833 | if (s->sftp) { | |
b10d49d7 | 834 | sftp_free(s->sftp); |
0a12ec87 RJ |
835 | } |
836 | s->sftp = NULL; | |
837 | if (s->session) { | |
b10d49d7 PT |
838 | ssh_disconnect(s->session); |
839 | ssh_free(s->session); | |
0a12ec87 RJ |
840 | } |
841 | s->session = NULL; | |
b10d49d7 PT |
842 | s->sock = -1; |
843 | if (new_sock >= 0) { | |
844 | close(new_sock); | |
845 | } | |
0a12ec87 RJ |
846 | |
847 | return ret; | |
848 | } | |
849 | ||
015a1036 HR |
850 | static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags, |
851 | Error **errp) | |
0a12ec87 RJ |
852 | { |
853 | BDRVSSHState *s = bs->opaque; | |
375f0b92 | 854 | BlockdevOptionsSsh *opts; |
0a12ec87 RJ |
855 | int ret; |
856 | int ssh_flags; | |
857 | ||
858 | ssh_state_init(s); | |
859 | ||
b10d49d7 | 860 | ssh_flags = 0; |
0a12ec87 | 861 | if (bdrv_flags & BDRV_O_RDWR) { |
b10d49d7 PT |
862 | ssh_flags |= O_RDWR; |
863 | } else { | |
864 | ssh_flags |= O_RDONLY; | |
0a12ec87 RJ |
865 | } |
866 | ||
375f0b92 KW |
867 | opts = ssh_parse_options(options, errp); |
868 | if (opts == NULL) { | |
869 | return -EINVAL; | |
870 | } | |
871 | ||
0a12ec87 | 872 | /* Start up SSH. */ |
375f0b92 | 873 | ret = connect_to_ssh(s, opts, ssh_flags, 0, errp); |
0a12ec87 RJ |
874 | if (ret < 0) { |
875 | goto err; | |
876 | } | |
877 | ||
878 | /* Go non-blocking. */ | |
b10d49d7 | 879 | ssh_set_blocking(s->session, 0); |
0a12ec87 | 880 | |
be9c9404 EB |
881 | if (s->attrs->type == SSH_FILEXFER_TYPE_REGULAR) { |
882 | bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; | |
883 | } | |
884 | ||
375f0b92 KW |
885 | qapi_free_BlockdevOptionsSsh(opts); |
886 | ||
0a12ec87 RJ |
887 | return 0; |
888 | ||
889 | err: | |
375f0b92 KW |
890 | qapi_free_BlockdevOptionsSsh(opts); |
891 | ||
0a12ec87 RJ |
892 | return ret; |
893 | } | |
894 | ||
bd8e0e32 | 895 | /* Note: This is a blocking operation */ |
2b12a756 HR |
896 | static int ssh_grow_file(BDRVSSHState *s, int64_t offset, Error **errp) |
897 | { | |
898 | ssize_t ret; | |
899 | char c[1] = { '\0' }; | |
b10d49d7 | 900 | int was_blocking = ssh_is_blocking(s->session); |
2b12a756 HR |
901 | |
902 | /* offset must be strictly greater than the current size so we do | |
903 | * not overwrite anything */ | |
b10d49d7 | 904 | assert(offset > 0 && offset > s->attrs->size); |
2b12a756 | 905 | |
b10d49d7 | 906 | ssh_set_blocking(s->session, 1); |
bd8e0e32 | 907 | |
b10d49d7 PT |
908 | sftp_seek64(s->sftp_handle, offset - 1); |
909 | ret = sftp_write(s->sftp_handle, c, 1); | |
bd8e0e32 | 910 | |
b10d49d7 | 911 | ssh_set_blocking(s->session, was_blocking); |
bd8e0e32 | 912 | |
2b12a756 HR |
913 | if (ret < 0) { |
914 | sftp_error_setg(errp, s, "Failed to grow file"); | |
915 | return -EIO; | |
916 | } | |
917 | ||
b10d49d7 | 918 | s->attrs->size = offset; |
2b12a756 HR |
919 | return 0; |
920 | } | |
921 | ||
766181fe CL |
922 | static QemuOptsList ssh_create_opts = { |
923 | .name = "ssh-create-opts", | |
924 | .head = QTAILQ_HEAD_INITIALIZER(ssh_create_opts.head), | |
925 | .desc = { | |
926 | { | |
927 | .name = BLOCK_OPT_SIZE, | |
928 | .type = QEMU_OPT_SIZE, | |
929 | .help = "Virtual disk size" | |
930 | }, | |
931 | { /* end of list */ } | |
932 | } | |
0a12ec87 RJ |
933 | }; |
934 | ||
4906da7e KW |
935 | static int ssh_co_create(BlockdevCreateOptions *options, Error **errp) |
936 | { | |
937 | BlockdevCreateOptionsSsh *opts = &options->u.ssh; | |
938 | BDRVSSHState s; | |
939 | int ret; | |
940 | ||
941 | assert(options->driver == BLOCKDEV_DRIVER_SSH); | |
942 | ||
943 | ssh_state_init(&s); | |
944 | ||
945 | ret = connect_to_ssh(&s, opts->location, | |
b10d49d7 | 946 | O_RDWR | O_CREAT | O_TRUNC, |
4906da7e KW |
947 | 0644, errp); |
948 | if (ret < 0) { | |
949 | goto fail; | |
950 | } | |
951 | ||
952 | if (opts->size > 0) { | |
953 | ret = ssh_grow_file(&s, opts->size, errp); | |
954 | if (ret < 0) { | |
955 | goto fail; | |
956 | } | |
957 | } | |
958 | ||
959 | ret = 0; | |
960 | fail: | |
961 | ssh_state_free(&s); | |
962 | return ret; | |
963 | } | |
964 | ||
b92902df ML |
965 | static int coroutine_fn ssh_co_create_opts(BlockDriver *drv, |
966 | const char *filename, | |
967 | QemuOpts *opts, | |
efc75e2a | 968 | Error **errp) |
0a12ec87 | 969 | { |
4906da7e KW |
970 | BlockdevCreateOptions *create_options; |
971 | BlockdevCreateOptionsSsh *ssh_opts; | |
972 | int ret; | |
0a12ec87 | 973 | QDict *uri_options = NULL; |
0a12ec87 | 974 | |
4906da7e KW |
975 | create_options = g_new0(BlockdevCreateOptions, 1); |
976 | create_options->driver = BLOCKDEV_DRIVER_SSH; | |
977 | ssh_opts = &create_options->u.ssh; | |
0a12ec87 RJ |
978 | |
979 | /* Get desired file size. */ | |
4906da7e KW |
980 | ssh_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
981 | BDRV_SECTOR_SIZE); | |
023908a2 | 982 | trace_ssh_co_create_opts(ssh_opts->size); |
0a12ec87 RJ |
983 | |
984 | uri_options = qdict_new(); | |
4906da7e KW |
985 | ret = parse_uri(filename, uri_options, errp); |
986 | if (ret < 0) { | |
0a12ec87 RJ |
987 | goto out; |
988 | } | |
989 | ||
4906da7e KW |
990 | ssh_opts->location = ssh_parse_options(uri_options, errp); |
991 | if (ssh_opts->location == NULL) { | |
375f0b92 KW |
992 | ret = -EINVAL; |
993 | goto out; | |
994 | } | |
995 | ||
4906da7e | 996 | ret = ssh_co_create(create_options, errp); |
0a12ec87 RJ |
997 | |
998 | out: | |
cb3e7f08 | 999 | qobject_unref(uri_options); |
4906da7e | 1000 | qapi_free_BlockdevCreateOptions(create_options); |
0a12ec87 RJ |
1001 | return ret; |
1002 | } | |
1003 | ||
1004 | static void ssh_close(BlockDriverState *bs) | |
1005 | { | |
1006 | BDRVSSHState *s = bs->opaque; | |
1007 | ||
1008 | ssh_state_free(s); | |
1009 | } | |
1010 | ||
0b3f21e6 RJ |
1011 | static int ssh_has_zero_init(BlockDriverState *bs) |
1012 | { | |
1013 | BDRVSSHState *s = bs->opaque; | |
1014 | /* Assume false, unless we can positively prove it's true. */ | |
1015 | int has_zero_init = 0; | |
1016 | ||
b10d49d7 PT |
1017 | if (s->attrs->type == SSH_FILEXFER_TYPE_REGULAR) { |
1018 | has_zero_init = 1; | |
0b3f21e6 RJ |
1019 | } |
1020 | ||
1021 | return has_zero_init; | |
1022 | } | |
1023 | ||
5aca18a4 PB |
1024 | typedef struct BDRVSSHRestart { |
1025 | BlockDriverState *bs; | |
1026 | Coroutine *co; | |
1027 | } BDRVSSHRestart; | |
1028 | ||
0a12ec87 RJ |
1029 | static void restart_coroutine(void *opaque) |
1030 | { | |
5aca18a4 PB |
1031 | BDRVSSHRestart *restart = opaque; |
1032 | BlockDriverState *bs = restart->bs; | |
1033 | BDRVSSHState *s = bs->opaque; | |
1034 | AioContext *ctx = bdrv_get_aio_context(bs); | |
0a12ec87 | 1035 | |
023908a2 | 1036 | trace_ssh_restart_coroutine(restart->co); |
5aca18a4 | 1037 | aio_set_fd_handler(ctx, s->sock, false, NULL, NULL, NULL, NULL); |
0a12ec87 | 1038 | |
5aca18a4 | 1039 | aio_co_wake(restart->co); |
0a12ec87 RJ |
1040 | } |
1041 | ||
9d456654 PB |
1042 | /* A non-blocking call returned EAGAIN, so yield, ensuring the |
1043 | * handlers are set up so that we'll be rescheduled when there is an | |
1044 | * interesting event on the socket. | |
1045 | */ | |
1046 | static coroutine_fn void co_yield(BDRVSSHState *s, BlockDriverState *bs) | |
0a12ec87 RJ |
1047 | { |
1048 | int r; | |
1049 | IOHandler *rd_handler = NULL, *wr_handler = NULL; | |
5aca18a4 PB |
1050 | BDRVSSHRestart restart = { |
1051 | .bs = bs, | |
1052 | .co = qemu_coroutine_self() | |
1053 | }; | |
0a12ec87 | 1054 | |
b10d49d7 | 1055 | r = ssh_get_poll_flags(s->session); |
0a12ec87 | 1056 | |
b10d49d7 | 1057 | if (r & SSH_READ_PENDING) { |
0a12ec87 RJ |
1058 | rd_handler = restart_coroutine; |
1059 | } | |
b10d49d7 | 1060 | if (r & SSH_WRITE_PENDING) { |
0a12ec87 RJ |
1061 | wr_handler = restart_coroutine; |
1062 | } | |
1063 | ||
023908a2 | 1064 | trace_ssh_co_yield(s->sock, rd_handler, wr_handler); |
0a12ec87 | 1065 | |
2af0b200 | 1066 | aio_set_fd_handler(bdrv_get_aio_context(bs), s->sock, |
5aca18a4 | 1067 | false, rd_handler, wr_handler, NULL, &restart); |
0a12ec87 | 1068 | qemu_coroutine_yield(); |
023908a2 | 1069 | trace_ssh_co_yield_back(s->sock); |
0a12ec87 RJ |
1070 | } |
1071 | ||
2af0b200 | 1072 | static coroutine_fn int ssh_read(BDRVSSHState *s, BlockDriverState *bs, |
0a12ec87 RJ |
1073 | int64_t offset, size_t size, |
1074 | QEMUIOVector *qiov) | |
1075 | { | |
1076 | ssize_t r; | |
1077 | size_t got; | |
1078 | char *buf, *end_of_vec; | |
1079 | struct iovec *i; | |
1080 | ||
023908a2 | 1081 | trace_ssh_read(offset, size); |
0a12ec87 | 1082 | |
b10d49d7 PT |
1083 | trace_ssh_seek(offset); |
1084 | sftp_seek64(s->sftp_handle, offset); | |
0a12ec87 RJ |
1085 | |
1086 | /* This keeps track of the current iovec element ('i'), where we | |
1087 | * will write to next ('buf'), and the end of the current iovec | |
1088 | * ('end_of_vec'). | |
1089 | */ | |
1090 | i = &qiov->iov[0]; | |
1091 | buf = i->iov_base; | |
1092 | end_of_vec = i->iov_base + i->iov_len; | |
1093 | ||
0a12ec87 | 1094 | for (got = 0; got < size; ) { |
b10d49d7 | 1095 | size_t request_read_size; |
0a12ec87 | 1096 | again: |
b10d49d7 PT |
1097 | /* |
1098 | * The size of SFTP packets is limited to 32K bytes, so limit | |
1099 | * the amount of data requested to 16K, as libssh currently | |
1100 | * does not handle multiple requests on its own. | |
1101 | */ | |
1102 | request_read_size = MIN(end_of_vec - buf, 16384); | |
1103 | trace_ssh_read_buf(buf, end_of_vec - buf, request_read_size); | |
1104 | r = sftp_read(s->sftp_handle, buf, request_read_size); | |
1105 | trace_ssh_read_return(r, sftp_get_error(s->sftp)); | |
0a12ec87 | 1106 | |
b10d49d7 | 1107 | if (r == SSH_AGAIN) { |
2af0b200 | 1108 | co_yield(s, bs); |
0a12ec87 RJ |
1109 | goto again; |
1110 | } | |
b10d49d7 | 1111 | if (r == SSH_EOF || (r == 0 && sftp_get_error(s->sftp) == SSH_FX_EOF)) { |
0a12ec87 RJ |
1112 | /* EOF: Short read so pad the buffer with zeroes and return it. */ |
1113 | qemu_iovec_memset(qiov, got, 0, size - got); | |
1114 | return 0; | |
1115 | } | |
b10d49d7 PT |
1116 | if (r <= 0) { |
1117 | sftp_error_trace(s, "read"); | |
1118 | return -EIO; | |
1119 | } | |
0a12ec87 RJ |
1120 | |
1121 | got += r; | |
1122 | buf += r; | |
0a12ec87 RJ |
1123 | if (buf >= end_of_vec && got < size) { |
1124 | i++; | |
1125 | buf = i->iov_base; | |
1126 | end_of_vec = i->iov_base + i->iov_len; | |
1127 | } | |
1128 | } | |
1129 | ||
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | static coroutine_fn int ssh_co_readv(BlockDriverState *bs, | |
1134 | int64_t sector_num, | |
1135 | int nb_sectors, QEMUIOVector *qiov) | |
1136 | { | |
1137 | BDRVSSHState *s = bs->opaque; | |
1138 | int ret; | |
1139 | ||
1140 | qemu_co_mutex_lock(&s->lock); | |
2af0b200 | 1141 | ret = ssh_read(s, bs, sector_num * BDRV_SECTOR_SIZE, |
0a12ec87 RJ |
1142 | nb_sectors * BDRV_SECTOR_SIZE, qiov); |
1143 | qemu_co_mutex_unlock(&s->lock); | |
1144 | ||
1145 | return ret; | |
1146 | } | |
1147 | ||
2af0b200 | 1148 | static int ssh_write(BDRVSSHState *s, BlockDriverState *bs, |
0a12ec87 RJ |
1149 | int64_t offset, size_t size, |
1150 | QEMUIOVector *qiov) | |
1151 | { | |
1152 | ssize_t r; | |
1153 | size_t written; | |
1154 | char *buf, *end_of_vec; | |
1155 | struct iovec *i; | |
1156 | ||
023908a2 | 1157 | trace_ssh_write(offset, size); |
0a12ec87 | 1158 | |
b10d49d7 PT |
1159 | trace_ssh_seek(offset); |
1160 | sftp_seek64(s->sftp_handle, offset); | |
0a12ec87 RJ |
1161 | |
1162 | /* This keeps track of the current iovec element ('i'), where we | |
1163 | * will read from next ('buf'), and the end of the current iovec | |
1164 | * ('end_of_vec'). | |
1165 | */ | |
1166 | i = &qiov->iov[0]; | |
1167 | buf = i->iov_base; | |
1168 | end_of_vec = i->iov_base + i->iov_len; | |
1169 | ||
1170 | for (written = 0; written < size; ) { | |
b10d49d7 | 1171 | size_t request_write_size; |
0a12ec87 | 1172 | again: |
b10d49d7 PT |
1173 | /* |
1174 | * Avoid too large data packets, as libssh currently does not | |
1175 | * handle multiple requests on its own. | |
1176 | */ | |
1177 | request_write_size = MIN(end_of_vec - buf, 131072); | |
1178 | trace_ssh_write_buf(buf, end_of_vec - buf, request_write_size); | |
1179 | r = sftp_write(s->sftp_handle, buf, request_write_size); | |
1180 | trace_ssh_write_return(r, sftp_get_error(s->sftp)); | |
0a12ec87 | 1181 | |
b10d49d7 | 1182 | if (r == SSH_AGAIN) { |
2af0b200 | 1183 | co_yield(s, bs); |
0a12ec87 RJ |
1184 | goto again; |
1185 | } | |
1186 | if (r < 0) { | |
6b3048ce | 1187 | sftp_error_trace(s, "write"); |
0a12ec87 RJ |
1188 | return -EIO; |
1189 | } | |
0a12ec87 RJ |
1190 | |
1191 | written += r; | |
1192 | buf += r; | |
0a12ec87 RJ |
1193 | if (buf >= end_of_vec && written < size) { |
1194 | i++; | |
1195 | buf = i->iov_base; | |
1196 | end_of_vec = i->iov_base + i->iov_len; | |
1197 | } | |
1198 | ||
b10d49d7 PT |
1199 | if (offset + written > s->attrs->size) { |
1200 | s->attrs->size = offset + written; | |
1201 | } | |
0a12ec87 RJ |
1202 | } |
1203 | ||
1204 | return 0; | |
1205 | } | |
1206 | ||
1207 | static coroutine_fn int ssh_co_writev(BlockDriverState *bs, | |
1208 | int64_t sector_num, | |
e18a58b4 EB |
1209 | int nb_sectors, QEMUIOVector *qiov, |
1210 | int flags) | |
0a12ec87 RJ |
1211 | { |
1212 | BDRVSSHState *s = bs->opaque; | |
1213 | int ret; | |
1214 | ||
e18a58b4 | 1215 | assert(!flags); |
0a12ec87 | 1216 | qemu_co_mutex_lock(&s->lock); |
2af0b200 | 1217 | ret = ssh_write(s, bs, sector_num * BDRV_SECTOR_SIZE, |
0a12ec87 RJ |
1218 | nb_sectors * BDRV_SECTOR_SIZE, qiov); |
1219 | qemu_co_mutex_unlock(&s->lock); | |
1220 | ||
1221 | return ret; | |
1222 | } | |
1223 | ||
9a2d462e RJ |
1224 | static void unsafe_flush_warning(BDRVSSHState *s, const char *what) |
1225 | { | |
1226 | if (!s->unsafe_flush_warning) { | |
3dc6f869 AF |
1227 | warn_report("ssh server %s does not support fsync", |
1228 | s->inet->host); | |
9a2d462e RJ |
1229 | if (what) { |
1230 | error_report("to support fsync, you need %s", what); | |
1231 | } | |
1232 | s->unsafe_flush_warning = true; | |
1233 | } | |
1234 | } | |
1235 | ||
b10d49d7 | 1236 | #ifdef HAVE_LIBSSH_0_8 |
9a2d462e | 1237 | |
2af0b200 | 1238 | static coroutine_fn int ssh_flush(BDRVSSHState *s, BlockDriverState *bs) |
9a2d462e RJ |
1239 | { |
1240 | int r; | |
1241 | ||
023908a2 | 1242 | trace_ssh_flush(); |
b10d49d7 PT |
1243 | |
1244 | if (!sftp_extension_supported(s->sftp, "[email protected]", "1")) { | |
1245 | unsafe_flush_warning(s, "OpenSSH >= 6.3"); | |
1246 | return 0; | |
1247 | } | |
9a2d462e | 1248 | again: |
b10d49d7 PT |
1249 | r = sftp_fsync(s->sftp_handle); |
1250 | if (r == SSH_AGAIN) { | |
2af0b200 | 1251 | co_yield(s, bs); |
9a2d462e RJ |
1252 | goto again; |
1253 | } | |
9a2d462e | 1254 | if (r < 0) { |
6b3048ce | 1255 | sftp_error_trace(s, "fsync"); |
9a2d462e RJ |
1256 | return -EIO; |
1257 | } | |
1258 | ||
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | static coroutine_fn int ssh_co_flush(BlockDriverState *bs) | |
1263 | { | |
1264 | BDRVSSHState *s = bs->opaque; | |
1265 | int ret; | |
1266 | ||
1267 | qemu_co_mutex_lock(&s->lock); | |
2af0b200 | 1268 | ret = ssh_flush(s, bs); |
9a2d462e RJ |
1269 | qemu_co_mutex_unlock(&s->lock); |
1270 | ||
1271 | return ret; | |
1272 | } | |
1273 | ||
b10d49d7 | 1274 | #else /* !HAVE_LIBSSH_0_8 */ |
9a2d462e RJ |
1275 | |
1276 | static coroutine_fn int ssh_co_flush(BlockDriverState *bs) | |
1277 | { | |
1278 | BDRVSSHState *s = bs->opaque; | |
1279 | ||
b10d49d7 | 1280 | unsafe_flush_warning(s, "libssh >= 0.8.0"); |
9a2d462e RJ |
1281 | return 0; |
1282 | } | |
1283 | ||
b10d49d7 | 1284 | #endif /* !HAVE_LIBSSH_0_8 */ |
9a2d462e | 1285 | |
0a12ec87 RJ |
1286 | static int64_t ssh_getlength(BlockDriverState *bs) |
1287 | { | |
1288 | BDRVSSHState *s = bs->opaque; | |
1289 | int64_t length; | |
1290 | ||
b10d49d7 PT |
1291 | /* Note we cannot make a libssh call here. */ |
1292 | length = (int64_t) s->attrs->size; | |
023908a2 | 1293 | trace_ssh_getlength(length); |
0a12ec87 RJ |
1294 | |
1295 | return length; | |
1296 | } | |
1297 | ||
061ca8a3 | 1298 | static int coroutine_fn ssh_co_truncate(BlockDriverState *bs, int64_t offset, |
c80d8b06 | 1299 | bool exact, PreallocMode prealloc, |
92b92799 | 1300 | BdrvRequestFlags flags, Error **errp) |
624f3006 HR |
1301 | { |
1302 | BDRVSSHState *s = bs->opaque; | |
1303 | ||
1304 | if (prealloc != PREALLOC_MODE_OFF) { | |
1305 | error_setg(errp, "Unsupported preallocation mode '%s'", | |
1306 | PreallocMode_str(prealloc)); | |
1307 | return -ENOTSUP; | |
1308 | } | |
1309 | ||
b10d49d7 | 1310 | if (offset < s->attrs->size) { |
624f3006 HR |
1311 | error_setg(errp, "ssh driver does not support shrinking files"); |
1312 | return -ENOTSUP; | |
1313 | } | |
1314 | ||
b10d49d7 | 1315 | if (offset == s->attrs->size) { |
624f3006 HR |
1316 | return 0; |
1317 | } | |
1318 | ||
1319 | return ssh_grow_file(s, offset, errp); | |
1320 | } | |
1321 | ||
b8c1f901 HR |
1322 | static void ssh_refresh_filename(BlockDriverState *bs) |
1323 | { | |
1324 | BDRVSSHState *s = bs->opaque; | |
1325 | const char *path, *host_key_check; | |
1326 | int ret; | |
1327 | ||
1328 | /* | |
1329 | * None of these options can be represented in a plain "host:port" | |
1330 | * format, so if any was given, we have to abort. | |
1331 | */ | |
1332 | if (s->inet->has_ipv4 || s->inet->has_ipv6 || s->inet->has_to || | |
1333 | s->inet->has_numeric) | |
1334 | { | |
1335 | return; | |
1336 | } | |
1337 | ||
1338 | path = qdict_get_try_str(bs->full_open_options, "path"); | |
1339 | assert(path); /* mandatory option */ | |
1340 | ||
1341 | host_key_check = qdict_get_try_str(bs->full_open_options, "host_key_check"); | |
1342 | ||
1343 | ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
1344 | "ssh://%s@%s:%s%s%s%s", | |
1345 | s->user, s->inet->host, s->inet->port, path, | |
1346 | host_key_check ? "?host_key_check=" : "", | |
1347 | host_key_check ?: ""); | |
1348 | if (ret >= sizeof(bs->exact_filename)) { | |
1349 | /* An overflow makes the filename unusable, so do not report any */ | |
1350 | bs->exact_filename[0] = '\0'; | |
1351 | } | |
1352 | } | |
1353 | ||
21205c7c HR |
1354 | static char *ssh_bdrv_dirname(BlockDriverState *bs, Error **errp) |
1355 | { | |
1356 | if (qdict_haskey(bs->full_open_options, "host_key_check")) { | |
1357 | /* | |
1358 | * We cannot generate a simple prefix if we would have to | |
1359 | * append a query string. | |
1360 | */ | |
1361 | error_setg(errp, | |
1362 | "Cannot generate a base directory with host_key_check set"); | |
1363 | return NULL; | |
1364 | } | |
1365 | ||
1366 | if (bs->exact_filename[0] == '\0') { | |
1367 | error_setg(errp, "Cannot generate a base directory for this ssh node"); | |
1368 | return NULL; | |
1369 | } | |
1370 | ||
1371 | return path_combine(bs->exact_filename, ""); | |
1372 | } | |
1373 | ||
2654267c HR |
1374 | static const char *const ssh_strong_runtime_opts[] = { |
1375 | "host", | |
1376 | "port", | |
1377 | "path", | |
1378 | "user", | |
1379 | "host_key_check", | |
1380 | "server.", | |
1381 | ||
1382 | NULL | |
1383 | }; | |
1384 | ||
0a12ec87 RJ |
1385 | static BlockDriver bdrv_ssh = { |
1386 | .format_name = "ssh", | |
1387 | .protocol_name = "ssh", | |
1388 | .instance_size = sizeof(BDRVSSHState), | |
1389 | .bdrv_parse_filename = ssh_parse_filename, | |
1390 | .bdrv_file_open = ssh_file_open, | |
4906da7e | 1391 | .bdrv_co_create = ssh_co_create, |
efc75e2a | 1392 | .bdrv_co_create_opts = ssh_co_create_opts, |
0a12ec87 | 1393 | .bdrv_close = ssh_close, |
0b3f21e6 | 1394 | .bdrv_has_zero_init = ssh_has_zero_init, |
0a12ec87 RJ |
1395 | .bdrv_co_readv = ssh_co_readv, |
1396 | .bdrv_co_writev = ssh_co_writev, | |
1397 | .bdrv_getlength = ssh_getlength, | |
061ca8a3 | 1398 | .bdrv_co_truncate = ssh_co_truncate, |
9a2d462e | 1399 | .bdrv_co_flush_to_disk = ssh_co_flush, |
b8c1f901 | 1400 | .bdrv_refresh_filename = ssh_refresh_filename, |
21205c7c | 1401 | .bdrv_dirname = ssh_bdrv_dirname, |
766181fe | 1402 | .create_opts = &ssh_create_opts, |
2654267c | 1403 | .strong_runtime_opts = ssh_strong_runtime_opts, |
0a12ec87 RJ |
1404 | }; |
1405 | ||
1406 | static void bdrv_ssh_init(void) | |
1407 | { | |
1408 | int r; | |
1409 | ||
b10d49d7 | 1410 | r = ssh_init(); |
0a12ec87 | 1411 | if (r != 0) { |
b10d49d7 | 1412 | fprintf(stderr, "libssh initialization failed, %d\n", r); |
0a12ec87 RJ |
1413 | exit(EXIT_FAILURE); |
1414 | } | |
1415 | ||
b10d49d7 PT |
1416 | #if TRACE_LIBSSH != 0 |
1417 | ssh_set_log_level(TRACE_LIBSSH); | |
1418 | #endif | |
1419 | ||
0a12ec87 RJ |
1420 | bdrv_register(&bdrv_ssh); |
1421 | } | |
1422 | ||
1423 | block_init(bdrv_ssh_init); |