blob: 8bd2a134c141deb3bc640a16ddc7fcebff115177 [file] [log] [blame]
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001/*
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 Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010026
Pino Toscanob10d49d2019-06-20 22:08:40 +020027#include <libssh/libssh.h>
28#include <libssh/sftp.h>
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010029
Markus Armbrustere2c1c342022-12-21 14:35:49 +010030#include "block/block-io.h"
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010031#include "block/block_int.h"
Max Reitz609f45e2018-06-14 21:14:28 +020032#include "block/qdict.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010033#include "qapi/error.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010034#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020035#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010036#include "qemu/option.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020037#include "qemu/ctype.h"
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +053038#include "qemu/cutils.h"
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010039#include "qemu/sockets.h"
40#include "qemu/uri.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010041#include "qapi/qapi-visit-sockets.h"
Kevin Wolf16e4bdb2018-02-02 16:12:18 +010042#include "qapi/qapi-visit-block-core.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010043#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010044#include "qapi/qmp/qstring.h"
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +053045#include "qapi/qobject-input-visitor.h"
46#include "qapi/qobject-output-visitor.h"
Laurent Vivier023908a2018-12-13 17:27:24 +010047#include "trace.h"
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010048
Laurent Vivier023908a2018-12-13 17:27:24 +010049/*
Pino Toscanob10d49d2019-06-20 22:08:40 +020050 * TRACE_LIBSSH=<level> enables tracing in libssh itself.
51 * The meaning of <level> is described here:
52 * http://api.libssh.org/master/group__libssh__log.html
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010053 */
Pino Toscanob10d49d2019-06-20 22:08:40 +020054#define TRACE_LIBSSH 0 /* see: SSH_LOG_* */
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010055
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010056typedef struct BDRVSSHState {
57 /* Coroutine. */
58 CoMutex lock;
59
60 /* SSH connection. */
61 int sock; /* socket */
Pino Toscanob10d49d2019-06-20 22:08:40 +020062 ssh_session session; /* ssh session */
63 sftp_session sftp; /* sftp session */
64 sftp_file sftp_handle; /* sftp remote file handle */
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010065
Pino Toscanob10d49d2019-06-20 22:08:40 +020066 /*
67 * File attributes at open. We try to keep the .size field
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010068 * updated if it changes (eg by writing at the end of the file).
69 */
Pino Toscanob10d49d2019-06-20 22:08:40 +020070 sftp_attributes attrs;
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +010071
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +053072 InetSocketAddress *inet;
73
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +010074 /* Used to warn if 'flush' is not supported. */
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +010075 bool unsafe_flush_warning;
Max Reitzb8c1f902019-02-25 20:08:27 +010076
77 /*
78 * Store the user name for ssh_refresh_filename() because the
79 * default depends on the system you are on -- therefore, when we
80 * generate a filename, it should always contain the user name we
81 * are actually using.
82 */
83 char *user;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010084} BDRVSSHState;
85
86static void ssh_state_init(BDRVSSHState *s)
87{
88 memset(s, 0, sizeof *s);
89 s->sock = -1;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +010090 qemu_co_mutex_init(&s->lock);
91}
92
93static void ssh_state_free(BDRVSSHState *s)
94{
Max Reitzb8c1f902019-02-25 20:08:27 +010095 g_free(s->user);
96
Pino Toscanob10d49d2019-06-20 22:08:40 +020097 if (s->attrs) {
98 sftp_attributes_free(s->attrs);
99 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100100 if (s->sftp_handle) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200101 sftp_close(s->sftp_handle);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100102 }
103 if (s->sftp) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200104 sftp_free(s->sftp);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100105 }
106 if (s->session) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200107 ssh_disconnect(s->session);
108 ssh_free(s->session); /* This frees s->sock */
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100109 }
110}
111
Marc-André Lureau9edc6312022-02-20 20:39:25 +0400112static void G_GNUC_PRINTF(3, 4)
Markus Armbruster01c2b262014-05-16 11:00:13 +0200113session_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...)
114{
115 va_list args;
116 char *msg;
117
118 va_start(args, fs);
119 msg = g_strdup_vprintf(fs, args);
120 va_end(args);
121
122 if (s->session) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200123 const char *ssh_err;
Markus Armbruster01c2b262014-05-16 11:00:13 +0200124 int ssh_err_code;
125
Pino Toscanob10d49d2019-06-20 22:08:40 +0200126 /* This is not an errno. See <libssh/libssh.h>. */
127 ssh_err = ssh_get_error(s->session);
128 ssh_err_code = ssh_get_error_code(s->session);
129 error_setg(errp, "%s: %s (libssh error code: %d)",
Markus Armbruster01c2b262014-05-16 11:00:13 +0200130 msg, ssh_err, ssh_err_code);
131 } else {
132 error_setg(errp, "%s", msg);
133 }
134 g_free(msg);
135}
136
Marc-André Lureau9edc6312022-02-20 20:39:25 +0400137static void G_GNUC_PRINTF(3, 4)
Markus Armbruster5496fb12014-05-16 11:00:16 +0200138sftp_error_setg(Error **errp, BDRVSSHState *s, const char *fs, ...)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100139{
140 va_list args;
Markus Armbruster5496fb12014-05-16 11:00:16 +0200141 char *msg;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100142
143 va_start(args, fs);
Markus Armbruster5496fb12014-05-16 11:00:16 +0200144 msg = g_strdup_vprintf(fs, args);
145 va_end(args);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100146
Markus Armbruster5496fb12014-05-16 11:00:16 +0200147 if (s->sftp) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200148 const char *ssh_err;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100149 int ssh_err_code;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200150 int sftp_err_code;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100151
Pino Toscanob10d49d2019-06-20 22:08:40 +0200152 /* This is not an errno. See <libssh/libssh.h>. */
153 ssh_err = ssh_get_error(s->session);
154 ssh_err_code = ssh_get_error_code(s->session);
155 /* See <libssh/sftp.h>. */
156 sftp_err_code = sftp_get_error(s->sftp);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100157
Markus Armbruster5496fb12014-05-16 11:00:16 +0200158 error_setg(errp,
Pino Toscanob10d49d2019-06-20 22:08:40 +0200159 "%s: %s (libssh error code: %d, sftp error code: %d)",
Markus Armbruster5496fb12014-05-16 11:00:16 +0200160 msg, ssh_err, ssh_err_code, sftp_err_code);
161 } else {
162 error_setg(errp, "%s", msg);
163 }
164 g_free(msg);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100165}
166
Markus Armbruster6b3048c2019-04-17 21:06:28 +0200167static void sftp_error_trace(BDRVSSHState *s, const char *op)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100168{
Pino Toscanob10d49d2019-06-20 22:08:40 +0200169 const char *ssh_err;
Markus Armbruster6b3048c2019-04-17 21:06:28 +0200170 int ssh_err_code;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200171 int sftp_err_code;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100172
Pino Toscanob10d49d2019-06-20 22:08:40 +0200173 /* This is not an errno. See <libssh/libssh.h>. */
174 ssh_err = ssh_get_error(s->session);
175 ssh_err_code = ssh_get_error_code(s->session);
176 /* See <libssh/sftp.h>. */
177 sftp_err_code = sftp_get_error(s->sftp);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100178
Markus Armbruster6b3048c2019-04-17 21:06:28 +0200179 trace_sftp_error(op, ssh_err, ssh_err_code, sftp_err_code);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100180}
181
182static int parse_uri(const char *filename, QDict *options, Error **errp)
183{
184 URI *uri = NULL;
Paolo Bonzinieab2ac92015-09-14 13:12:34 +0200185 QueryParams *qp;
Ashijeet Acharya1059f1b2016-10-25 18:34:00 +0530186 char *port_str;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100187 int i;
188
189 uri = uri_parse(filename);
190 if (!uri) {
191 return -EINVAL;
192 }
193
Max Reitzf69165a2017-06-13 22:57:26 +0200194 if (g_strcmp0(uri->scheme, "ssh") != 0) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100195 error_setg(errp, "URI scheme must be 'ssh'");
196 goto err;
197 }
198
199 if (!uri->server || strcmp(uri->server, "") == 0) {
200 error_setg(errp, "missing hostname in URI");
201 goto err;
202 }
203
204 if (!uri->path || strcmp(uri->path, "") == 0) {
205 error_setg(errp, "missing remote path in URI");
206 goto err;
207 }
208
209 qp = query_params_parse(uri->query);
210 if (!qp) {
211 error_setg(errp, "could not parse query parameters");
212 goto err;
213 }
214
215 if(uri->user && strcmp(uri->user, "") != 0) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500216 qdict_put_str(options, "user", uri->user);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100217 }
218
Eric Blake46f5ac22017-04-27 16:58:17 -0500219 qdict_put_str(options, "server.host", uri->server);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100220
Ashijeet Acharya1059f1b2016-10-25 18:34:00 +0530221 port_str = g_strdup_printf("%d", uri->port ?: 22);
Eric Blake46f5ac22017-04-27 16:58:17 -0500222 qdict_put_str(options, "server.port", port_str);
Ashijeet Acharya1059f1b2016-10-25 18:34:00 +0530223 g_free(port_str);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100224
Eric Blake46f5ac22017-04-27 16:58:17 -0500225 qdict_put_str(options, "path", uri->path);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100226
227 /* Pick out any query parameters that we understand, and ignore
228 * the rest.
229 */
230 for (i = 0; i < qp->n; ++i) {
231 if (strcmp(qp->p[i].name, "host_key_check") == 0) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500232 qdict_put_str(options, "host_key_check", qp->p[i].value);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100233 }
234 }
235
236 query_params_free(qp);
237 uri_free(uri);
238 return 0;
239
240 err:
Heinrich Schuchardtc2615bd2021-06-29 08:36:02 +0200241 uri_free(uri);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100242 return -EINVAL;
243}
244
Ashijeet Acharya89dbe182016-10-25 18:33:57 +0530245static bool ssh_has_filename_options_conflict(QDict *options, Error **errp)
246{
247 const QDictEntry *qe;
248
249 for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
250 if (!strcmp(qe->key, "host") ||
251 !strcmp(qe->key, "port") ||
252 !strcmp(qe->key, "path") ||
253 !strcmp(qe->key, "user") ||
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530254 !strcmp(qe->key, "host_key_check") ||
255 strstart(qe->key, "server.", NULL))
Ashijeet Acharya89dbe182016-10-25 18:33:57 +0530256 {
257 error_setg(errp, "Option '%s' cannot be used with a file name",
258 qe->key);
259 return true;
260 }
261 }
262
263 return false;
264}
265
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100266static void ssh_parse_filename(const char *filename, QDict *options,
267 Error **errp)
268{
Ashijeet Acharya89dbe182016-10-25 18:33:57 +0530269 if (ssh_has_filename_options_conflict(options, errp)) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100270 return;
271 }
272
273 parse_uri(filename, options, errp);
274}
275
Pino Toscanob10d49d2019-06-20 22:08:40 +0200276static int check_host_key_knownhosts(BDRVSSHState *s, Error **errp)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100277{
Pino Toscanob10d49d2019-06-20 22:08:40 +0200278 int ret;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200279 enum ssh_known_hosts_e state;
280 int r;
281 ssh_key pubkey;
282 enum ssh_keytypes_e pubkey_type;
283 unsigned char *server_hash = NULL;
284 size_t server_hash_len;
285 char *fingerprint = NULL;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100286
Pino Toscanob10d49d2019-06-20 22:08:40 +0200287 state = ssh_session_is_known_server(s->session);
288 trace_ssh_server_status(state);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100289
Pino Toscanob10d49d2019-06-20 22:08:40 +0200290 switch (state) {
291 case SSH_KNOWN_HOSTS_OK:
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100292 /* OK */
Pino Toscanob10d49d2019-06-20 22:08:40 +0200293 trace_ssh_check_host_key_knownhosts();
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100294 break;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200295 case SSH_KNOWN_HOSTS_CHANGED:
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100296 ret = -EINVAL;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200297 r = ssh_get_server_publickey(s->session, &pubkey);
298 if (r == 0) {
299 r = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256,
300 &server_hash, &server_hash_len);
301 pubkey_type = ssh_key_type(pubkey);
302 ssh_key_free(pubkey);
303 }
304 if (r == 0) {
305 fingerprint = ssh_get_fingerprint_hash(SSH_PUBLICKEY_HASH_SHA256,
306 server_hash,
307 server_hash_len);
308 ssh_clean_pubkey_hash(&server_hash);
309 }
310 if (fingerprint) {
311 error_setg(errp,
312 "host key (%s key with fingerprint %s) does not match "
313 "the one in known_hosts; this may be a possible attack",
314 ssh_key_type_to_char(pubkey_type), fingerprint);
315 ssh_string_free_char(fingerprint);
316 } else {
317 error_setg(errp,
318 "host key does not match the one in known_hosts; this "
319 "may be a possible attack");
320 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100321 goto out;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200322 case SSH_KNOWN_HOSTS_OTHER:
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100323 ret = -EINVAL;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200324 error_setg(errp,
325 "host key for this server not found, another type exists");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100326 goto out;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200327 case SSH_KNOWN_HOSTS_UNKNOWN:
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100328 ret = -EINVAL;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200329 error_setg(errp, "no host key was found in known_hosts");
330 goto out;
331 case SSH_KNOWN_HOSTS_NOT_FOUND:
332 ret = -ENOENT;
333 error_setg(errp, "known_hosts file not found");
334 goto out;
335 case SSH_KNOWN_HOSTS_ERROR:
336 ret = -EINVAL;
337 error_setg(errp, "error while checking the host");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100338 goto out;
339 default:
340 ret = -EINVAL;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200341 error_setg(errp, "error while checking for known server (%d)", state);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100342 goto out;
343 }
344
345 /* known_hosts checking successful. */
346 ret = 0;
347
348 out:
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100349 return ret;
350}
351
352static unsigned hex2decimal(char ch)
353{
354 if (ch >= '0' && ch <= '9') {
355 return (ch - '0');
356 } else if (ch >= 'a' && ch <= 'f') {
357 return 10 + (ch - 'a');
358 } else if (ch >= 'A' && ch <= 'F') {
359 return 10 + (ch - 'A');
360 }
361
362 return -1;
363}
364
365/* Compare the binary fingerprint (hash of host key) with the
366 * host_key_check parameter.
367 */
368static int compare_fingerprint(const unsigned char *fingerprint, size_t len,
369 const char *host_key_check)
370{
371 unsigned c;
372
373 while (len > 0) {
374 while (*host_key_check == ':')
375 host_key_check++;
376 if (!qemu_isxdigit(host_key_check[0]) ||
377 !qemu_isxdigit(host_key_check[1]))
378 return 1;
379 c = hex2decimal(host_key_check[0]) * 16 +
380 hex2decimal(host_key_check[1]);
381 if (c - *fingerprint != 0)
382 return c - *fingerprint;
383 fingerprint++;
384 len--;
385 host_key_check += 2;
386 }
387 return *host_key_check - '\0';
388}
389
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100390static char *format_fingerprint(const unsigned char *fingerprint, size_t len)
391{
392 static const char *hex = "0123456789abcdef";
393 char *ret = g_new0(char, (len * 2) + 1);
394 for (size_t i = 0; i < len; i++) {
395 ret[i * 2] = hex[((fingerprint[i] >> 4) & 0xf)];
396 ret[(i * 2) + 1] = hex[(fingerprint[i] & 0xf)];
397 }
398 ret[len * 2] = '\0';
399 return ret;
400}
401
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100402static int
403check_host_key_hash(BDRVSSHState *s, const char *hash,
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100404 enum ssh_publickey_hash_type type, const char *typestr,
405 Error **errp)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100406{
Pino Toscanob10d49d2019-06-20 22:08:40 +0200407 int r;
408 ssh_key pubkey;
409 unsigned char *server_hash;
410 size_t server_hash_len;
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100411 const char *keytype;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100412
Pino Toscanob10d49d2019-06-20 22:08:40 +0200413 r = ssh_get_server_publickey(s->session, &pubkey);
Pino Toscanob10d49d2019-06-20 22:08:40 +0200414 if (r != SSH_OK) {
Markus Armbruster01c2b262014-05-16 11:00:13 +0200415 session_error_setg(errp, s, "failed to read remote host key");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100416 return -EINVAL;
417 }
418
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100419 keytype = ssh_key_type_to_char(ssh_key_type(pubkey));
420
Pino Toscanob10d49d2019-06-20 22:08:40 +0200421 r = ssh_get_publickey_hash(pubkey, type, &server_hash, &server_hash_len);
422 ssh_key_free(pubkey);
423 if (r != 0) {
424 session_error_setg(errp, s,
425 "failed reading the hash of the server SSH key");
426 return -EINVAL;
427 }
428
429 r = compare_fingerprint(server_hash, server_hash_len, hash);
Pino Toscanob10d49d2019-06-20 22:08:40 +0200430 if (r != 0) {
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100431 g_autofree char *server_fp = format_fingerprint(server_hash,
432 server_hash_len);
433 error_setg(errp, "remote host %s key fingerprint '%s:%s' "
434 "does not match host_key_check '%s:%s'",
435 keytype, typestr, server_fp, typestr, hash);
436 ssh_clean_pubkey_hash(&server_hash);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100437 return -EPERM;
438 }
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100439 ssh_clean_pubkey_hash(&server_hash);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100440
441 return 0;
442}
443
Pino Toscanob10d49d2019-06-20 22:08:40 +0200444static int check_host_key(BDRVSSHState *s, SshHostKeyCheck *hkc, Error **errp)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100445{
Kevin Wolfec2f5412018-02-05 14:59:05 +0100446 SshHostKeyCheckMode mode;
447
448 if (hkc) {
449 mode = hkc->mode;
450 } else {
451 mode = SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS;
452 }
453
454 switch (mode) {
455 case SSH_HOST_KEY_CHECK_MODE_NONE:
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100456 return 0;
Kevin Wolfec2f5412018-02-05 14:59:05 +0100457 case SSH_HOST_KEY_CHECK_MODE_HASH:
458 if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_MD5) {
459 return check_host_key_hash(s, hkc->u.hash.hash,
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100460 SSH_PUBLICKEY_HASH_MD5, "md5",
461 errp);
Kevin Wolfec2f5412018-02-05 14:59:05 +0100462 } else if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_SHA1) {
463 return check_host_key_hash(s, hkc->u.hash.hash,
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100464 SSH_PUBLICKEY_HASH_SHA1, "sha1",
465 errp);
Daniel P. Berrangébf783262021-06-22 12:51:56 +0100466 } else if (hkc->u.hash.type == SSH_HOST_KEY_CHECK_HASH_TYPE_SHA256) {
467 return check_host_key_hash(s, hkc->u.hash.hash,
Daniel P. Berrangée3296cc2021-09-13 17:59:00 +0100468 SSH_PUBLICKEY_HASH_SHA256, "sha256",
469 errp);
Kevin Wolfec2f5412018-02-05 14:59:05 +0100470 }
471 g_assert_not_reached();
472 break;
473 case SSH_HOST_KEY_CHECK_MODE_KNOWN_HOSTS:
Pino Toscanob10d49d2019-06-20 22:08:40 +0200474 return check_host_key_knownhosts(s, errp);
Kevin Wolfec2f5412018-02-05 14:59:05 +0100475 default:
476 g_assert_not_reached();
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100477 }
478
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100479 return -EINVAL;
480}
481
Pino Toscanob10d49d2019-06-20 22:08:40 +0200482static int authenticate(BDRVSSHState *s, Error **errp)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100483{
484 int r, ret;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200485 int method;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100486
Pino Toscanob10d49d2019-06-20 22:08:40 +0200487 /* Try to authenticate with the "none" method. */
488 r = ssh_userauth_none(s->session, NULL);
489 if (r == SSH_AUTH_ERROR) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100490 ret = -EPERM;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200491 session_error_setg(errp, s, "failed to authenticate using none "
492 "authentication");
493 goto out;
494 } else if (r == SSH_AUTH_SUCCESS) {
495 /* Authenticated! */
496 ret = 0;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100497 goto out;
498 }
499
Pino Toscanob10d49d2019-06-20 22:08:40 +0200500 method = ssh_userauth_list(s->session, NULL);
501 trace_ssh_auth_methods(method);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100502
Pino Toscanob10d49d2019-06-20 22:08:40 +0200503 /*
504 * Try to authenticate with publickey, using the ssh-agent
505 * if available.
506 */
507 if (method & SSH_AUTH_METHOD_PUBLICKEY) {
508 r = ssh_userauth_publickey_auto(s->session, NULL, NULL);
509 if (r == SSH_AUTH_ERROR) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100510 ret = -EINVAL;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200511 session_error_setg(errp, s, "failed to authenticate using "
512 "publickey authentication");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100513 goto out;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200514 } else if (r == SSH_AUTH_SUCCESS) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100515 /* Authenticated! */
516 ret = 0;
517 goto out;
518 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100519 }
520
521 ret = -EPERM;
Markus Armbruster4618e652014-05-16 11:00:14 +0200522 error_setg(errp, "failed to authenticate using publickey authentication "
523 "and the identities held by your ssh-agent");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100524
525 out:
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100526 return ret;
527}
528
Max Reitz8a6a8082016-08-15 15:29:23 +0200529static QemuOptsList ssh_runtime_opts = {
530 .name = "ssh",
531 .head = QTAILQ_HEAD_INITIALIZER(ssh_runtime_opts.head),
532 .desc = {
533 {
534 .name = "host",
535 .type = QEMU_OPT_STRING,
536 .help = "Host to connect to",
537 },
538 {
539 .name = "port",
540 .type = QEMU_OPT_NUMBER,
541 .help = "Port to connect to",
542 },
Kevin Wolfec2f5412018-02-05 14:59:05 +0100543 {
544 .name = "host_key_check",
545 .type = QEMU_OPT_STRING,
546 .help = "Defines how and what to check the host key against",
547 },
Murilo Opsfelder Araujofbd5c4c2018-01-05 12:44:40 -0200548 { /* end of list */ }
Max Reitz8a6a8082016-08-15 15:29:23 +0200549 },
550};
551
Kevin Wolfec2f5412018-02-05 14:59:05 +0100552static bool ssh_process_legacy_options(QDict *output_opts,
553 QemuOpts *legacy_opts,
554 Error **errp)
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530555{
556 const char *host = qemu_opt_get(legacy_opts, "host");
557 const char *port = qemu_opt_get(legacy_opts, "port");
Kevin Wolfec2f5412018-02-05 14:59:05 +0100558 const char *host_key_check = qemu_opt_get(legacy_opts, "host_key_check");
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530559
560 if (!host && port) {
561 error_setg(errp, "port may not be used without host");
562 return false;
563 }
564
565 if (host) {
Eric Blake46f5ac22017-04-27 16:58:17 -0500566 qdict_put_str(output_opts, "server.host", host);
567 qdict_put_str(output_opts, "server.port", port ?: stringify(22));
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530568 }
569
Kevin Wolfec2f5412018-02-05 14:59:05 +0100570 if (host_key_check) {
571 if (strcmp(host_key_check, "no") == 0) {
572 qdict_put_str(output_opts, "host-key-check.mode", "none");
573 } else if (strncmp(host_key_check, "md5:", 4) == 0) {
574 qdict_put_str(output_opts, "host-key-check.mode", "hash");
575 qdict_put_str(output_opts, "host-key-check.type", "md5");
576 qdict_put_str(output_opts, "host-key-check.hash",
577 &host_key_check[4]);
578 } else if (strncmp(host_key_check, "sha1:", 5) == 0) {
579 qdict_put_str(output_opts, "host-key-check.mode", "hash");
580 qdict_put_str(output_opts, "host-key-check.type", "sha1");
581 qdict_put_str(output_opts, "host-key-check.hash",
582 &host_key_check[5]);
Daniel P. Berrangéea0f60e2021-09-13 17:57:34 +0100583 } else if (strncmp(host_key_check, "sha256:", 7) == 0) {
584 qdict_put_str(output_opts, "host-key-check.mode", "hash");
585 qdict_put_str(output_opts, "host-key-check.type", "sha256");
586 qdict_put_str(output_opts, "host-key-check.hash",
587 &host_key_check[7]);
Kevin Wolfec2f5412018-02-05 14:59:05 +0100588 } else if (strcmp(host_key_check, "yes") == 0) {
589 qdict_put_str(output_opts, "host-key-check.mode", "known_hosts");
590 } else {
591 error_setg(errp, "unknown host_key_check setting (%s)",
592 host_key_check);
593 return false;
594 }
595 }
596
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530597 return true;
598}
599
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100600static BlockdevOptionsSsh *ssh_parse_options(QDict *options, Error **errp)
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530601{
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100602 BlockdevOptionsSsh *result = NULL;
603 QemuOpts *opts = NULL;
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100604 const QDictEntry *e;
605 Visitor *v;
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530606
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100607 /* Translate legacy options */
608 opts = qemu_opts_create(&ssh_runtime_opts, NULL, 0, &error_abort);
Markus Armbrusteraf175e82020-07-07 18:06:03 +0200609 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100610 goto fail;
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530611 }
612
Kevin Wolfec2f5412018-02-05 14:59:05 +0100613 if (!ssh_process_legacy_options(options, opts, errp)) {
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100614 goto fail;
615 }
616
617 /* Create the QAPI object */
Markus Armbrusteraf910622018-06-14 21:14:33 +0200618 v = qobject_input_visitor_new_flat_confused(options, errp);
619 if (!v) {
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100620 goto fail;
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530621 }
622
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200623 visit_type_BlockdevOptionsSsh(v, NULL, &result, errp);
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100624 visit_free(v);
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200625 if (!result) {
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100626 goto fail;
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530627 }
628
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100629 /* Remove the processed options from the QDict (the visitor processes
630 * _all_ options in the QDict) */
631 while ((e = qdict_first(options))) {
632 qdict_del(options, e->key);
633 }
634
635fail:
636 qemu_opts_del(opts);
637 return result;
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530638}
639
Kevin Wolf375f0b92018-02-05 15:28:14 +0100640static int connect_to_ssh(BDRVSSHState *s, BlockdevOptionsSsh *opts,
Markus Armbruster5f0c39e2014-05-16 11:00:15 +0200641 int ssh_flags, int creat_mode, Error **errp)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100642{
643 int r, ret;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200644 unsigned int port = 0;
645 int new_sock = -1;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100646
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100647 if (opts->user) {
Max Reitzb8c1f902019-02-25 20:08:27 +0100648 s->user = g_strdup(opts->user);
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100649 } else {
Max Reitzb8c1f902019-02-25 20:08:27 +0100650 s->user = g_strdup(g_get_user_name());
651 if (!s->user) {
Markus Armbruster5f0c39e2014-05-16 11:00:15 +0200652 error_setg_errno(errp, errno, "Can't get user name");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100653 ret = -errno;
654 goto err;
655 }
656 }
657
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530658 /* Pop the config into our state object, Exit if invalid */
Kevin Wolf16e4bdb2018-02-02 16:12:18 +0100659 s->inet = opts->server;
660 opts->server = NULL;
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530661
Pino Toscanob10d49d2019-06-20 22:08:40 +0200662 if (qemu_strtoui(s->inet->port, NULL, 10, &port) < 0) {
Ashijeet Acharya0da5b8e2016-10-25 18:33:59 +0530663 error_setg(errp, "Use only numeric port value");
664 ret = -EINVAL;
665 goto err;
666 }
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +0100667
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100668 /* Open the socket and connect. */
Pino Toscanob10d49d2019-06-20 22:08:40 +0200669 new_sock = inet_connect_saddr(s->inet, errp);
670 if (new_sock < 0) {
Richard W.M. Jones325e3902015-07-22 14:27:47 +0100671 ret = -EIO;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100672 goto err;
673 }
674
Pino Toscanob10d49d2019-06-20 22:08:40 +0200675 /*
676 * Try to disable the Nagle algorithm on TCP sockets to reduce latency,
677 * but do not fail if it cannot be disabled.
678 */
679 r = socket_set_nodelay(new_sock);
680 if (r < 0) {
681 warn_report("can't set TCP_NODELAY for the ssh server %s: %s",
682 s->inet->host, strerror(errno));
683 }
684
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100685 /* Create SSH session. */
Pino Toscanob10d49d2019-06-20 22:08:40 +0200686 s->session = ssh_new();
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100687 if (!s->session) {
688 ret = -EINVAL;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200689 session_error_setg(errp, s, "failed to initialize libssh session");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100690 goto err;
691 }
692
Pino Toscanob10d49d2019-06-20 22:08:40 +0200693 /*
694 * Make sure we are in blocking mode during the connection and
695 * authentication phases.
696 */
697 ssh_set_blocking(s->session, 1);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100698
Pino Toscanob10d49d2019-06-20 22:08:40 +0200699 r = ssh_options_set(s->session, SSH_OPTIONS_USER, s->user);
700 if (r < 0) {
701 ret = -EINVAL;
702 session_error_setg(errp, s,
703 "failed to set the user in the libssh session");
704 goto err;
705 }
706
707 r = ssh_options_set(s->session, SSH_OPTIONS_HOST, s->inet->host);
708 if (r < 0) {
709 ret = -EINVAL;
710 session_error_setg(errp, s,
711 "failed to set the host in the libssh session");
712 goto err;
713 }
714
715 if (port > 0) {
716 r = ssh_options_set(s->session, SSH_OPTIONS_PORT, &port);
717 if (r < 0) {
718 ret = -EINVAL;
719 session_error_setg(errp, s,
720 "failed to set the port in the libssh session");
721 goto err;
722 }
723 }
724
725 r = ssh_options_set(s->session, SSH_OPTIONS_COMPRESSION, "none");
726 if (r < 0) {
727 ret = -EINVAL;
728 session_error_setg(errp, s,
729 "failed to disable the compression in the libssh "
730 "session");
731 goto err;
732 }
733
734 /* Read ~/.ssh/config. */
735 r = ssh_options_parse_config(s->session, NULL);
736 if (r < 0) {
737 ret = -EINVAL;
738 session_error_setg(errp, s, "failed to parse ~/.ssh/config");
739 goto err;
740 }
741
742 r = ssh_options_set(s->session, SSH_OPTIONS_FD, &new_sock);
743 if (r < 0) {
744 ret = -EINVAL;
745 session_error_setg(errp, s,
746 "failed to set the socket in the libssh session");
747 goto err;
748 }
749 /* libssh took ownership of the socket. */
750 s->sock = new_sock;
751 new_sock = -1;
752
753 /* Connect. */
754 r = ssh_connect(s->session);
755 if (r != SSH_OK) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100756 ret = -EINVAL;
Markus Armbruster5f0c39e2014-05-16 11:00:15 +0200757 session_error_setg(errp, s, "failed to establish SSH session");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100758 goto err;
759 }
760
761 /* Check the remote host's key against known_hosts. */
Pino Toscanob10d49d2019-06-20 22:08:40 +0200762 ret = check_host_key(s, opts->host_key_check, errp);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100763 if (ret < 0) {
764 goto err;
765 }
766
767 /* Authenticate. */
Pino Toscanob10d49d2019-06-20 22:08:40 +0200768 ret = authenticate(s, errp);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100769 if (ret < 0) {
770 goto err;
771 }
772
773 /* Start SFTP. */
Pino Toscanob10d49d2019-06-20 22:08:40 +0200774 s->sftp = sftp_new(s->session);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100775 if (!s->sftp) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200776 session_error_setg(errp, s, "failed to create sftp handle");
777 ret = -EINVAL;
778 goto err;
779 }
780
781 r = sftp_init(s->sftp);
782 if (r < 0) {
783 sftp_error_setg(errp, s, "failed to initialize sftp handle");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100784 ret = -EINVAL;
785 goto err;
786 }
787
788 /* Open the remote file. */
Laurent Vivier023908a2018-12-13 17:27:24 +0100789 trace_ssh_connect_to_ssh(opts->path, ssh_flags, creat_mode);
Pino Toscanob10d49d2019-06-20 22:08:40 +0200790 s->sftp_handle = sftp_open(s->sftp, opts->path, ssh_flags, creat_mode);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100791 if (!s->sftp_handle) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200792 sftp_error_setg(errp, s, "failed to open remote file '%s'",
793 opts->path);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100794 ret = -EINVAL;
795 goto err;
796 }
797
Pino Toscanob10d49d2019-06-20 22:08:40 +0200798 /* Make sure the SFTP file is handled in blocking mode. */
799 sftp_file_set_blocking(s->sftp_handle);
800
801 s->attrs = sftp_fstat(s->sftp_handle);
802 if (!s->attrs) {
Markus Armbruster5496fb12014-05-16 11:00:16 +0200803 sftp_error_setg(errp, s, "failed to read file attributes");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100804 return -EINVAL;
805 }
806
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100807 return 0;
808
809 err:
Pino Toscanob10d49d2019-06-20 22:08:40 +0200810 if (s->attrs) {
811 sftp_attributes_free(s->attrs);
812 }
813 s->attrs = NULL;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100814 if (s->sftp_handle) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200815 sftp_close(s->sftp_handle);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100816 }
817 s->sftp_handle = NULL;
818 if (s->sftp) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200819 sftp_free(s->sftp);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100820 }
821 s->sftp = NULL;
822 if (s->session) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200823 ssh_disconnect(s->session);
824 ssh_free(s->session);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100825 }
826 s->session = NULL;
Pino Toscanob10d49d2019-06-20 22:08:40 +0200827 s->sock = -1;
828 if (new_sock >= 0) {
829 close(new_sock);
830 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100831
832 return ret;
833}
834
Max Reitz015a1032013-09-05 14:22:29 +0200835static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags,
836 Error **errp)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100837{
838 BDRVSSHState *s = bs->opaque;
Kevin Wolf375f0b92018-02-05 15:28:14 +0100839 BlockdevOptionsSsh *opts;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100840 int ret;
841 int ssh_flags;
842
843 ssh_state_init(s);
844
Pino Toscanob10d49d2019-06-20 22:08:40 +0200845 ssh_flags = 0;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100846 if (bdrv_flags & BDRV_O_RDWR) {
Pino Toscanob10d49d2019-06-20 22:08:40 +0200847 ssh_flags |= O_RDWR;
848 } else {
849 ssh_flags |= O_RDONLY;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100850 }
851
Kevin Wolf375f0b92018-02-05 15:28:14 +0100852 opts = ssh_parse_options(options, errp);
853 if (opts == NULL) {
854 return -EINVAL;
855 }
856
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100857 /* Start up SSH. */
Kevin Wolf375f0b92018-02-05 15:28:14 +0100858 ret = connect_to_ssh(s, opts, ssh_flags, 0, errp);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100859 if (ret < 0) {
860 goto err;
861 }
862
863 /* Go non-blocking. */
Pino Toscanob10d49d2019-06-20 22:08:40 +0200864 ssh_set_blocking(s->session, 0);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100865
Eric Blakebe9c9402020-04-28 15:29:02 -0500866 if (s->attrs->type == SSH_FILEXFER_TYPE_REGULAR) {
867 bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
868 }
869
Kevin Wolf375f0b92018-02-05 15:28:14 +0100870 qapi_free_BlockdevOptionsSsh(opts);
871
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100872 return 0;
873
874 err:
Kevin Wolf375f0b92018-02-05 15:28:14 +0100875 qapi_free_BlockdevOptionsSsh(opts);
876
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100877 return ret;
878}
879
Max Reitzbd8e0e32018-02-14 21:49:14 +0100880/* Note: This is a blocking operation */
Max Reitz2b12a752018-02-14 21:49:13 +0100881static int ssh_grow_file(BDRVSSHState *s, int64_t offset, Error **errp)
882{
883 ssize_t ret;
884 char c[1] = { '\0' };
Pino Toscanob10d49d2019-06-20 22:08:40 +0200885 int was_blocking = ssh_is_blocking(s->session);
Max Reitz2b12a752018-02-14 21:49:13 +0100886
887 /* offset must be strictly greater than the current size so we do
888 * not overwrite anything */
Pino Toscanob10d49d2019-06-20 22:08:40 +0200889 assert(offset > 0 && offset > s->attrs->size);
Max Reitz2b12a752018-02-14 21:49:13 +0100890
Pino Toscanob10d49d2019-06-20 22:08:40 +0200891 ssh_set_blocking(s->session, 1);
Max Reitzbd8e0e32018-02-14 21:49:14 +0100892
Pino Toscanob10d49d2019-06-20 22:08:40 +0200893 sftp_seek64(s->sftp_handle, offset - 1);
894 ret = sftp_write(s->sftp_handle, c, 1);
Max Reitzbd8e0e32018-02-14 21:49:14 +0100895
Pino Toscanob10d49d2019-06-20 22:08:40 +0200896 ssh_set_blocking(s->session, was_blocking);
Max Reitzbd8e0e32018-02-14 21:49:14 +0100897
Max Reitz2b12a752018-02-14 21:49:13 +0100898 if (ret < 0) {
899 sftp_error_setg(errp, s, "Failed to grow file");
900 return -EIO;
901 }
902
Pino Toscanob10d49d2019-06-20 22:08:40 +0200903 s->attrs->size = offset;
Max Reitz2b12a752018-02-14 21:49:13 +0100904 return 0;
905}
906
Chunyan Liu766181f2014-06-05 17:21:06 +0800907static QemuOptsList ssh_create_opts = {
908 .name = "ssh-create-opts",
909 .head = QTAILQ_HEAD_INITIALIZER(ssh_create_opts.head),
910 .desc = {
911 {
912 .name = BLOCK_OPT_SIZE,
913 .type = QEMU_OPT_SIZE,
914 .help = "Virtual disk size"
915 },
916 { /* end of list */ }
917 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100918};
919
Kevin Wolf4906da72018-02-05 16:24:32 +0100920static int ssh_co_create(BlockdevCreateOptions *options, Error **errp)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100921{
Kevin Wolf4906da72018-02-05 16:24:32 +0100922 BlockdevCreateOptionsSsh *opts = &options->u.ssh;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100923 BDRVSSHState s;
Kevin Wolf4906da72018-02-05 16:24:32 +0100924 int ret;
925
926 assert(options->driver == BLOCKDEV_DRIVER_SSH);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100927
928 ssh_state_init(&s);
929
Kevin Wolf4906da72018-02-05 16:24:32 +0100930 ret = connect_to_ssh(&s, opts->location,
Pino Toscanob10d49d2019-06-20 22:08:40 +0200931 O_RDWR | O_CREAT | O_TRUNC,
Kevin Wolf4906da72018-02-05 16:24:32 +0100932 0644, errp);
933 if (ret < 0) {
934 goto fail;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100935 }
936
Kevin Wolf4906da72018-02-05 16:24:32 +0100937 if (opts->size > 0) {
938 ret = ssh_grow_file(&s, opts->size, errp);
Max Reitz2b12a752018-02-14 21:49:13 +0100939 if (ret < 0) {
Kevin Wolf4906da72018-02-05 16:24:32 +0100940 goto fail;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100941 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100942 }
943
944 ret = 0;
Kevin Wolf4906da72018-02-05 16:24:32 +0100945fail:
946 ssh_state_free(&s);
947 return ret;
948}
949
Maxim Levitskyb92902d2020-03-26 03:12:17 +0200950static int coroutine_fn ssh_co_create_opts(BlockDriver *drv,
951 const char *filename,
952 QemuOpts *opts,
Kevin Wolf4906da72018-02-05 16:24:32 +0100953 Error **errp)
954{
955 BlockdevCreateOptions *create_options;
956 BlockdevCreateOptionsSsh *ssh_opts;
957 int ret;
958 QDict *uri_options = NULL;
959
960 create_options = g_new0(BlockdevCreateOptions, 1);
961 create_options->driver = BLOCKDEV_DRIVER_SSH;
962 ssh_opts = &create_options->u.ssh;
963
964 /* Get desired file size. */
965 ssh_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
966 BDRV_SECTOR_SIZE);
Laurent Vivier023908a2018-12-13 17:27:24 +0100967 trace_ssh_co_create_opts(ssh_opts->size);
Kevin Wolf4906da72018-02-05 16:24:32 +0100968
969 uri_options = qdict_new();
970 ret = parse_uri(filename, uri_options, errp);
971 if (ret < 0) {
972 goto out;
973 }
974
975 ssh_opts->location = ssh_parse_options(uri_options, errp);
976 if (ssh_opts->location == NULL) {
977 ret = -EINVAL;
978 goto out;
979 }
980
981 ret = ssh_co_create(create_options, errp);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100982
983 out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200984 qobject_unref(uri_options);
Kevin Wolf4906da72018-02-05 16:24:32 +0100985 qapi_free_BlockdevCreateOptions(create_options);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +0100986 return ret;
987}
988
989static void ssh_close(BlockDriverState *bs)
990{
991 BDRVSSHState *s = bs->opaque;
992
993 ssh_state_free(s);
994}
995
Richard W.M. Jones0b3f21e2013-06-25 18:15:18 +0100996static int ssh_has_zero_init(BlockDriverState *bs)
997{
998 BDRVSSHState *s = bs->opaque;
999 /* Assume false, unless we can positively prove it's true. */
1000 int has_zero_init = 0;
1001
Pino Toscanob10d49d2019-06-20 22:08:40 +02001002 if (s->attrs->type == SSH_FILEXFER_TYPE_REGULAR) {
1003 has_zero_init = 1;
Richard W.M. Jones0b3f21e2013-06-25 18:15:18 +01001004 }
1005
1006 return has_zero_init;
1007}
1008
Paolo Bonzini5aca18a2017-06-29 15:27:49 +02001009typedef struct BDRVSSHRestart {
1010 BlockDriverState *bs;
1011 Coroutine *co;
1012} BDRVSSHRestart;
1013
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001014static void restart_coroutine(void *opaque)
1015{
Paolo Bonzini5aca18a2017-06-29 15:27:49 +02001016 BDRVSSHRestart *restart = opaque;
1017 BlockDriverState *bs = restart->bs;
1018 BDRVSSHState *s = bs->opaque;
1019 AioContext *ctx = bdrv_get_aio_context(bs);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001020
Laurent Vivier023908a2018-12-13 17:27:24 +01001021 trace_ssh_restart_coroutine(restart->co);
Stefan Hajnoczi826cc322021-12-07 13:23:31 +00001022 aio_set_fd_handler(ctx, s->sock, false, NULL, NULL, NULL, NULL, NULL);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001023
Paolo Bonzini5aca18a2017-06-29 15:27:49 +02001024 aio_co_wake(restart->co);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001025}
1026
Paolo Bonzini9d456652017-02-13 14:52:30 +01001027/* A non-blocking call returned EAGAIN, so yield, ensuring the
1028 * handlers are set up so that we'll be rescheduled when there is an
1029 * interesting event on the socket.
1030 */
1031static coroutine_fn void co_yield(BDRVSSHState *s, BlockDriverState *bs)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001032{
1033 int r;
1034 IOHandler *rd_handler = NULL, *wr_handler = NULL;
Paolo Bonzini5aca18a2017-06-29 15:27:49 +02001035 BDRVSSHRestart restart = {
1036 .bs = bs,
1037 .co = qemu_coroutine_self()
1038 };
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001039
Pino Toscanob10d49d2019-06-20 22:08:40 +02001040 r = ssh_get_poll_flags(s->session);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001041
Pino Toscanob10d49d2019-06-20 22:08:40 +02001042 if (r & SSH_READ_PENDING) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001043 rd_handler = restart_coroutine;
1044 }
Pino Toscanob10d49d2019-06-20 22:08:40 +02001045 if (r & SSH_WRITE_PENDING) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001046 wr_handler = restart_coroutine;
1047 }
1048
Laurent Vivier023908a2018-12-13 17:27:24 +01001049 trace_ssh_co_yield(s->sock, rd_handler, wr_handler);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001050
Stefan Hajnoczi2af0b202014-05-08 16:34:53 +02001051 aio_set_fd_handler(bdrv_get_aio_context(bs), s->sock,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +00001052 false, rd_handler, wr_handler, NULL, NULL, &restart);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001053 qemu_coroutine_yield();
Laurent Vivier023908a2018-12-13 17:27:24 +01001054 trace_ssh_co_yield_back(s->sock);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001055}
1056
Stefan Hajnoczi2af0b202014-05-08 16:34:53 +02001057static coroutine_fn int ssh_read(BDRVSSHState *s, BlockDriverState *bs,
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001058 int64_t offset, size_t size,
1059 QEMUIOVector *qiov)
1060{
1061 ssize_t r;
1062 size_t got;
1063 char *buf, *end_of_vec;
1064 struct iovec *i;
1065
Laurent Vivier023908a2018-12-13 17:27:24 +01001066 trace_ssh_read(offset, size);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001067
Pino Toscanob10d49d2019-06-20 22:08:40 +02001068 trace_ssh_seek(offset);
1069 sftp_seek64(s->sftp_handle, offset);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001070
1071 /* This keeps track of the current iovec element ('i'), where we
1072 * will write to next ('buf'), and the end of the current iovec
1073 * ('end_of_vec').
1074 */
1075 i = &qiov->iov[0];
1076 buf = i->iov_base;
1077 end_of_vec = i->iov_base + i->iov_len;
1078
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001079 for (got = 0; got < size; ) {
Pino Toscanob10d49d2019-06-20 22:08:40 +02001080 size_t request_read_size;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001081 again:
Pino Toscanob10d49d2019-06-20 22:08:40 +02001082 /*
1083 * The size of SFTP packets is limited to 32K bytes, so limit
1084 * the amount of data requested to 16K, as libssh currently
1085 * does not handle multiple requests on its own.
1086 */
1087 request_read_size = MIN(end_of_vec - buf, 16384);
1088 trace_ssh_read_buf(buf, end_of_vec - buf, request_read_size);
1089 r = sftp_read(s->sftp_handle, buf, request_read_size);
1090 trace_ssh_read_return(r, sftp_get_error(s->sftp));
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001091
Pino Toscanob10d49d2019-06-20 22:08:40 +02001092 if (r == SSH_AGAIN) {
Stefan Hajnoczi2af0b202014-05-08 16:34:53 +02001093 co_yield(s, bs);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001094 goto again;
1095 }
Pino Toscanob10d49d2019-06-20 22:08:40 +02001096 if (r == SSH_EOF || (r == 0 && sftp_get_error(s->sftp) == SSH_FX_EOF)) {
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001097 /* EOF: Short read so pad the buffer with zeroes and return it. */
1098 qemu_iovec_memset(qiov, got, 0, size - got);
1099 return 0;
1100 }
Pino Toscanob10d49d2019-06-20 22:08:40 +02001101 if (r <= 0) {
1102 sftp_error_trace(s, "read");
1103 return -EIO;
1104 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001105
1106 got += r;
1107 buf += r;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001108 if (buf >= end_of_vec && got < size) {
1109 i++;
1110 buf = i->iov_base;
1111 end_of_vec = i->iov_base + i->iov_len;
1112 }
1113 }
1114
1115 return 0;
1116}
1117
1118static coroutine_fn int ssh_co_readv(BlockDriverState *bs,
1119 int64_t sector_num,
1120 int nb_sectors, QEMUIOVector *qiov)
1121{
1122 BDRVSSHState *s = bs->opaque;
1123 int ret;
1124
1125 qemu_co_mutex_lock(&s->lock);
Stefan Hajnoczi2af0b202014-05-08 16:34:53 +02001126 ret = ssh_read(s, bs, sector_num * BDRV_SECTOR_SIZE,
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001127 nb_sectors * BDRV_SECTOR_SIZE, qiov);
1128 qemu_co_mutex_unlock(&s->lock);
1129
1130 return ret;
1131}
1132
Alberto Faria42f6ad72022-10-13 14:36:51 +02001133static coroutine_fn int ssh_write(BDRVSSHState *s, BlockDriverState *bs,
1134 int64_t offset, size_t size,
1135 QEMUIOVector *qiov)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001136{
1137 ssize_t r;
1138 size_t written;
1139 char *buf, *end_of_vec;
1140 struct iovec *i;
1141
Laurent Vivier023908a2018-12-13 17:27:24 +01001142 trace_ssh_write(offset, size);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001143
Pino Toscanob10d49d2019-06-20 22:08:40 +02001144 trace_ssh_seek(offset);
1145 sftp_seek64(s->sftp_handle, offset);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001146
1147 /* This keeps track of the current iovec element ('i'), where we
1148 * will read from next ('buf'), and the end of the current iovec
1149 * ('end_of_vec').
1150 */
1151 i = &qiov->iov[0];
1152 buf = i->iov_base;
1153 end_of_vec = i->iov_base + i->iov_len;
1154
1155 for (written = 0; written < size; ) {
Pino Toscanob10d49d2019-06-20 22:08:40 +02001156 size_t request_write_size;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001157 again:
Pino Toscanob10d49d2019-06-20 22:08:40 +02001158 /*
1159 * Avoid too large data packets, as libssh currently does not
1160 * handle multiple requests on its own.
1161 */
1162 request_write_size = MIN(end_of_vec - buf, 131072);
1163 trace_ssh_write_buf(buf, end_of_vec - buf, request_write_size);
1164 r = sftp_write(s->sftp_handle, buf, request_write_size);
1165 trace_ssh_write_return(r, sftp_get_error(s->sftp));
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001166
Pino Toscanob10d49d2019-06-20 22:08:40 +02001167 if (r == SSH_AGAIN) {
Stefan Hajnoczi2af0b202014-05-08 16:34:53 +02001168 co_yield(s, bs);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001169 goto again;
1170 }
1171 if (r < 0) {
Markus Armbruster6b3048c2019-04-17 21:06:28 +02001172 sftp_error_trace(s, "write");
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001173 return -EIO;
1174 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001175
1176 written += r;
1177 buf += r;
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001178 if (buf >= end_of_vec && written < size) {
1179 i++;
1180 buf = i->iov_base;
1181 end_of_vec = i->iov_base + i->iov_len;
1182 }
1183
Pino Toscanob10d49d2019-06-20 22:08:40 +02001184 if (offset + written > s->attrs->size) {
1185 s->attrs->size = offset + written;
1186 }
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001187 }
1188
1189 return 0;
1190}
1191
1192static coroutine_fn int ssh_co_writev(BlockDriverState *bs,
1193 int64_t sector_num,
Eric Blakee18a58b2018-04-24 17:01:57 -05001194 int nb_sectors, QEMUIOVector *qiov,
1195 int flags)
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001196{
1197 BDRVSSHState *s = bs->opaque;
1198 int ret;
1199
1200 qemu_co_mutex_lock(&s->lock);
Stefan Hajnoczi2af0b202014-05-08 16:34:53 +02001201 ret = ssh_write(s, bs, sector_num * BDRV_SECTOR_SIZE,
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001202 nb_sectors * BDRV_SECTOR_SIZE, qiov);
1203 qemu_co_mutex_unlock(&s->lock);
1204
1205 return ret;
1206}
1207
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +01001208static void unsafe_flush_warning(BDRVSSHState *s, const char *what)
1209{
1210 if (!s->unsafe_flush_warning) {
Alistair Francis3dc6f862017-07-12 06:57:41 -07001211 warn_report("ssh server %s does not support fsync",
1212 s->inet->host);
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +01001213 if (what) {
1214 error_report("to support fsync, you need %s", what);
1215 }
1216 s->unsafe_flush_warning = true;
1217 }
1218}
1219
Stefan Hajnoczi2af0b202014-05-08 16:34:53 +02001220static coroutine_fn int ssh_flush(BDRVSSHState *s, BlockDriverState *bs)
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +01001221{
1222 int r;
1223
Laurent Vivier023908a2018-12-13 17:27:24 +01001224 trace_ssh_flush();
Pino Toscanob10d49d2019-06-20 22:08:40 +02001225
1226 if (!sftp_extension_supported(s->sftp, "fsync@openssh.com", "1")) {
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +01001227 unsafe_flush_warning(s, "OpenSSH >= 6.3");
1228 return 0;
1229 }
Pino Toscanob10d49d2019-06-20 22:08:40 +02001230 again:
1231 r = sftp_fsync(s->sftp_handle);
1232 if (r == SSH_AGAIN) {
1233 co_yield(s, bs);
1234 goto again;
1235 }
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +01001236 if (r < 0) {
Markus Armbruster6b3048c2019-04-17 21:06:28 +02001237 sftp_error_trace(s, "fsync");
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +01001238 return -EIO;
1239 }
1240
1241 return 0;
1242}
1243
1244static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
1245{
1246 BDRVSSHState *s = bs->opaque;
1247 int ret;
1248
1249 qemu_co_mutex_lock(&s->lock);
Stefan Hajnoczi2af0b202014-05-08 16:34:53 +02001250 ret = ssh_flush(s, bs);
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +01001251 qemu_co_mutex_unlock(&s->lock);
1252
1253 return ret;
1254}
1255
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001256static int64_t ssh_getlength(BlockDriverState *bs)
1257{
1258 BDRVSSHState *s = bs->opaque;
1259 int64_t length;
1260
Pino Toscanob10d49d2019-06-20 22:08:40 +02001261 /* Note we cannot make a libssh call here. */
1262 length = (int64_t) s->attrs->size;
Laurent Vivier023908a2018-12-13 17:27:24 +01001263 trace_ssh_getlength(length);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001264
1265 return length;
1266}
1267
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001268static int coroutine_fn ssh_co_truncate(BlockDriverState *bs, int64_t offset,
Max Reitzc80d8b02019-09-18 11:51:40 +02001269 bool exact, PreallocMode prealloc,
Kevin Wolf92b92792020-04-24 14:54:39 +02001270 BdrvRequestFlags flags, Error **errp)
Max Reitz624f3002018-02-14 21:49:15 +01001271{
1272 BDRVSSHState *s = bs->opaque;
1273
1274 if (prealloc != PREALLOC_MODE_OFF) {
1275 error_setg(errp, "Unsupported preallocation mode '%s'",
1276 PreallocMode_str(prealloc));
1277 return -ENOTSUP;
1278 }
1279
Pino Toscanob10d49d2019-06-20 22:08:40 +02001280 if (offset < s->attrs->size) {
Max Reitz624f3002018-02-14 21:49:15 +01001281 error_setg(errp, "ssh driver does not support shrinking files");
1282 return -ENOTSUP;
1283 }
1284
Pino Toscanob10d49d2019-06-20 22:08:40 +02001285 if (offset == s->attrs->size) {
Max Reitz624f3002018-02-14 21:49:15 +01001286 return 0;
1287 }
1288
1289 return ssh_grow_file(s, offset, errp);
1290}
1291
Max Reitzb8c1f902019-02-25 20:08:27 +01001292static void ssh_refresh_filename(BlockDriverState *bs)
1293{
1294 BDRVSSHState *s = bs->opaque;
1295 const char *path, *host_key_check;
1296 int ret;
1297
1298 /*
1299 * None of these options can be represented in a plain "host:port"
1300 * format, so if any was given, we have to abort.
1301 */
1302 if (s->inet->has_ipv4 || s->inet->has_ipv6 || s->inet->has_to ||
1303 s->inet->has_numeric)
1304 {
1305 return;
1306 }
1307
1308 path = qdict_get_try_str(bs->full_open_options, "path");
1309 assert(path); /* mandatory option */
1310
1311 host_key_check = qdict_get_try_str(bs->full_open_options, "host_key_check");
1312
1313 ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1314 "ssh://%s@%s:%s%s%s%s",
1315 s->user, s->inet->host, s->inet->port, path,
1316 host_key_check ? "?host_key_check=" : "",
1317 host_key_check ?: "");
1318 if (ret >= sizeof(bs->exact_filename)) {
1319 /* An overflow makes the filename unusable, so do not report any */
1320 bs->exact_filename[0] = '\0';
1321 }
1322}
1323
Max Reitz21205c72019-02-25 20:08:28 +01001324static char *ssh_bdrv_dirname(BlockDriverState *bs, Error **errp)
1325{
1326 if (qdict_haskey(bs->full_open_options, "host_key_check")) {
1327 /*
1328 * We cannot generate a simple prefix if we would have to
1329 * append a query string.
1330 */
1331 error_setg(errp,
1332 "Cannot generate a base directory with host_key_check set");
1333 return NULL;
1334 }
1335
1336 if (bs->exact_filename[0] == '\0') {
1337 error_setg(errp, "Cannot generate a base directory for this ssh node");
1338 return NULL;
1339 }
1340
1341 return path_combine(bs->exact_filename, "");
1342}
1343
Max Reitz26542672019-02-01 20:29:25 +01001344static const char *const ssh_strong_runtime_opts[] = {
1345 "host",
1346 "port",
1347 "path",
1348 "user",
1349 "host_key_check",
1350 "server.",
1351
1352 NULL
1353};
1354
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001355static BlockDriver bdrv_ssh = {
1356 .format_name = "ssh",
1357 .protocol_name = "ssh",
1358 .instance_size = sizeof(BDRVSSHState),
1359 .bdrv_parse_filename = ssh_parse_filename,
1360 .bdrv_file_open = ssh_file_open,
Kevin Wolf4906da72018-02-05 16:24:32 +01001361 .bdrv_co_create = ssh_co_create,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01001362 .bdrv_co_create_opts = ssh_co_create_opts,
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001363 .bdrv_close = ssh_close,
Richard W.M. Jones0b3f21e2013-06-25 18:15:18 +01001364 .bdrv_has_zero_init = ssh_has_zero_init,
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001365 .bdrv_co_readv = ssh_co_readv,
1366 .bdrv_co_writev = ssh_co_writev,
1367 .bdrv_getlength = ssh_getlength,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02001368 .bdrv_co_truncate = ssh_co_truncate,
Richard W.M. Jones9a2d4622013-04-09 15:30:54 +01001369 .bdrv_co_flush_to_disk = ssh_co_flush,
Max Reitzb8c1f902019-02-25 20:08:27 +01001370 .bdrv_refresh_filename = ssh_refresh_filename,
Max Reitz21205c72019-02-25 20:08:28 +01001371 .bdrv_dirname = ssh_bdrv_dirname,
Chunyan Liu766181f2014-06-05 17:21:06 +08001372 .create_opts = &ssh_create_opts,
Max Reitz26542672019-02-01 20:29:25 +01001373 .strong_runtime_opts = ssh_strong_runtime_opts,
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001374};
1375
1376static void bdrv_ssh_init(void)
1377{
1378 int r;
1379
Pino Toscanob10d49d2019-06-20 22:08:40 +02001380 r = ssh_init();
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001381 if (r != 0) {
Pino Toscanob10d49d2019-06-20 22:08:40 +02001382 fprintf(stderr, "libssh initialization failed, %d\n", r);
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001383 exit(EXIT_FAILURE);
1384 }
1385
Pino Toscanob10d49d2019-06-20 22:08:40 +02001386#if TRACE_LIBSSH != 0
1387 ssh_set_log_level(TRACE_LIBSSH);
1388#endif
1389
Richard W.M. Jones0a12ec82013-04-09 15:30:53 +01001390 bdrv_register(&bdrv_ssh);
1391}
1392
1393block_init(bdrv_ssh_init);