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