From nobody Fri May 17 02:41:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551121817902913.7934082425575; Mon, 25 Feb 2019 11:10:17 -0800 (PST) Received: from localhost ([127.0.0.1]:43033 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLdh-0003j5-LM for importer@patchew.org; Mon, 25 Feb 2019 14:10:09 -0500 Received: from eggs.gnu.org ([209.51.188.92]:38418) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLcI-0002ze-Fx for qemu-devel@nongnu.org; Mon, 25 Feb 2019 14:08:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyLcH-0001eA-4H for qemu-devel@nongnu.org; Mon, 25 Feb 2019 14:08:42 -0500 Received: from mx1.redhat.com ([209.132.183.28]:32820) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyLcD-0001ax-UA; Mon, 25 Feb 2019 14:08:38 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 203EC3082144; Mon, 25 Feb 2019 19:08:35 +0000 (UTC) Received: from localhost (ovpn-117-37.ams2.redhat.com [10.36.117.37]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 636CF608A5; Mon, 25 Feb 2019 19:08:34 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 25 Feb 2019 20:08:27 +0100 Message-Id: <20190225190828.17726-2-mreitz@redhat.com> In-Reply-To: <20190225190828.17726-1-mreitz@redhat.com> References: <20190225190828.17726-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Mon, 25 Feb 2019 19:08:35 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/2] block/ssh: Implement .bdrv_refresh_filename() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , "Richard W . M . Jones" , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" This requires some changes to keep iotests 104 and 207 working. qemu-img info in 104 will now return a filename including the user name and the port, which need to be filtered by adjusting REMOTE_TEST_DIR in common.rc. This additional information has to be marked optional, however (which is simple as REMOTE_TEST_DIR is a regex), because otherwise 197 and 215 would fail: They use it (indirectly) to filter qemu-img create output which contains a backing filename they have passed to it -- which probably does not contain a user name or port number. The problem in 207 is a nice one to have: qemu-img info used to return json:{} filenames, but with this patch it returns nice plain ones. We now need to adjust the filtering to hide the user name (and port number while we are at it). The simplest way to do this is to include both in iotests.remote_filename() so that bdrv_refresh_filename() will not change it, and then iotests.img_info_log() will filter it correctly automatically. Signed-off-by: Max Reitz Tested-by: Richard W.M. Jones --- block/ssh.c | 52 +++++++++++++++++++++++++++++++---- tests/qemu-iotests/207 | 10 +++---- tests/qemu-iotests/207.out | 10 +++---- tests/qemu-iotests/common.rc | 2 +- tests/qemu-iotests/iotests.py | 2 +- 5 files changed, 59 insertions(+), 17 deletions(-) diff --git a/block/ssh.c b/block/ssh.c index 190ef95300..88401bbd31 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -75,6 +75,14 @@ typedef struct BDRVSSHState { =20 /* Used to warn if 'flush' is not supported. */ bool unsafe_flush_warning; + + /* + * Store the user name for ssh_refresh_filename() because the + * default depends on the system you are on -- therefore, when we + * generate a filename, it should always contain the user name we + * are actually using. + */ + char *user; } BDRVSSHState; =20 static void ssh_state_init(BDRVSSHState *s) @@ -87,6 +95,8 @@ static void ssh_state_init(BDRVSSHState *s) =20 static void ssh_state_free(BDRVSSHState *s) { + g_free(s->user); + if (s->sftp_handle) { libssh2_sftp_close(s->sftp_handle); } @@ -640,14 +650,13 @@ static int connect_to_ssh(BDRVSSHState *s, BlockdevOp= tionsSsh *opts, int ssh_flags, int creat_mode, Error **errp) { int r, ret; - const char *user; long port =3D 0; =20 if (opts->has_user) { - user =3D opts->user; + s->user =3D g_strdup(opts->user); } else { - user =3D g_get_user_name(); - if (!user) { + s->user =3D g_strdup(g_get_user_name()); + if (!s->user) { error_setg_errno(errp, errno, "Can't get user name"); ret =3D -errno; goto err; @@ -697,7 +706,7 @@ static int connect_to_ssh(BDRVSSHState *s, BlockdevOpti= onsSsh *opts, } =20 /* Authenticate. */ - ret =3D authenticate(s, user, errp); + ret =3D authenticate(s, s->user, errp); if (ret < 0) { goto err; } @@ -1254,6 +1263,38 @@ static int coroutine_fn ssh_co_truncate(BlockDriverS= tate *bs, int64_t offset, return ssh_grow_file(s, offset, errp); } =20 +static void ssh_refresh_filename(BlockDriverState *bs) +{ + BDRVSSHState *s =3D bs->opaque; + const char *path, *host_key_check; + int ret; + + /* + * None of these options can be represented in a plain "host:port" + * format, so if any was given, we have to abort. + */ + if (s->inet->has_ipv4 || s->inet->has_ipv6 || s->inet->has_to || + s->inet->has_numeric) + { + return; + } + + path =3D qdict_get_try_str(bs->full_open_options, "path"); + assert(path); /* mandatory option */ + + host_key_check =3D qdict_get_try_str(bs->full_open_options, "host_key_= check"); + + ret =3D snprintf(bs->exact_filename, sizeof(bs->exact_filename), + "ssh://%s@%s:%s%s%s%s", + s->user, s->inet->host, s->inet->port, path, + host_key_check ? "?host_key_check=3D" : "", + host_key_check ?: ""); + if (ret >=3D sizeof(bs->exact_filename)) { + /* An overflow makes the filename unusable, so do not report any */ + bs->exact_filename[0] =3D '\0'; + } +} + static const char *const ssh_strong_runtime_opts[] =3D { "host", "port", @@ -1280,6 +1321,7 @@ static BlockDriver bdrv_ssh =3D { .bdrv_getlength =3D ssh_getlength, .bdrv_co_truncate =3D ssh_co_truncate, .bdrv_co_flush_to_disk =3D ssh_co_flush, + .bdrv_refresh_filename =3D ssh_refresh_filename, .create_opts =3D &ssh_create_opts, .strong_runtime_opts =3D ssh_strong_runtime_opts, }; diff --git a/tests/qemu-iotests/207 b/tests/qemu-iotests/207 index dfd3c51bd1..b3816136f7 100755 --- a/tests/qemu-iotests/207 +++ b/tests/qemu-iotests/207 @@ -66,7 +66,7 @@ with iotests.FilePath('t.img') as disk_path, \ 'size': 4194304 }) vm.shutdown() =20 - iotests.img_info_log(remote_path, filter_path=3Ddisk_path) + iotests.img_info_log(remote_path) iotests.log("") iotests.img_info_log(disk_path) =20 @@ -91,7 +91,7 @@ with iotests.FilePath('t.img') as disk_path, \ 'size': 8388608 }) vm.shutdown() =20 - iotests.img_info_log(remote_path, filter_path=3Ddisk_path) + iotests.img_info_log(remote_path) =20 vm.launch() blockdev_create(vm, { 'driver': 'ssh', @@ -108,7 +108,7 @@ with iotests.FilePath('t.img') as disk_path, \ 'size': 4194304 }) vm.shutdown() =20 - iotests.img_info_log(remote_path, filter_path=3Ddisk_path) + iotests.img_info_log(remote_path) =20 md5_key =3D subprocess.check_output( 'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + @@ -146,7 +146,7 @@ with iotests.FilePath('t.img') as disk_path, \ 'size': 8388608 }) vm.shutdown() =20 - iotests.img_info_log(remote_path, filter_path=3Ddisk_path) + iotests.img_info_log(remote_path) =20 sha1_key =3D subprocess.check_output( 'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + @@ -184,7 +184,7 @@ with iotests.FilePath('t.img') as disk_path, \ 'size': 4194304 }) vm.shutdown() =20 - iotests.img_info_log(remote_path, filter_path=3Ddisk_path) + iotests.img_info_log(remote_path) =20 # # Invalid path and user diff --git a/tests/qemu-iotests/207.out b/tests/qemu-iotests/207.out index 568e8619d0..8b8c28c7e1 100644 --- a/tests/qemu-iotests/207.out +++ b/tests/qemu-iotests/207.out @@ -5,7 +5,7 @@ {"execute": "job-dismiss", "arguments": {"id": "job0"}} {"return": {}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: TEST_IMG file format: IMGFMT virtual size: 4.0M (4194304 bytes) =20 @@ -21,7 +21,7 @@ virtual size: 4.0M (4194304 bytes) {"execute": "job-dismiss", "arguments": {"id": "job0"}} {"return": {}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: TEST_IMG file format: IMGFMT virtual size: 8.0M (8388608 bytes) =20 @@ -30,7 +30,7 @@ virtual size: 8.0M (8388608 bytes) {"execute": "job-dismiss", "arguments": {"id": "job0"}} {"return": {}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: TEST_IMG file format: IMGFMT virtual size: 4.0M (4194304 bytes) =20 @@ -45,7 +45,7 @@ Job failed: remote host key does not match host_key_check= 'wrong' {"execute": "job-dismiss", "arguments": {"id": "job0"}} {"return": {}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: TEST_IMG file format: IMGFMT virtual size: 8.0M (8388608 bytes) =20 @@ -60,7 +60,7 @@ Job failed: remote host key does not match host_key_check= 'wrong' {"execute": "job-dismiss", "arguments": {"id": "job0"}} {"return": {}} =20 -image: json:{"driver": "IMGFMT", "file": {"server.host": "127.0.0.1", "ser= ver.port": "22", "driver": "ssh", "path": "TEST_IMG"}} +image: TEST_IMG file format: IMGFMT virtual size: 4.0M (4194304 bytes) =20 diff --git a/tests/qemu-iotests/common.rc b/tests/qemu-iotests/common.rc index 09a27f02d0..f323fa2925 100644 --- a/tests/qemu-iotests/common.rc +++ b/tests/qemu-iotests/common.rc @@ -145,7 +145,7 @@ else TEST_IMG=3D"nbd:127.0.0.1:10810" elif [ "$IMGPROTO" =3D "ssh" ]; then TEST_IMG_FILE=3D$TEST_DIR/t.$IMGFMT - REMOTE_TEST_DIR=3D"ssh://127.0.0.1$TEST_DIR" + REMOTE_TEST_DIR=3D"ssh://\\($USER@\\)\\?127.0.0.1\\(:[0-9]\\+\\)\\= ?$TEST_DIR" TEST_IMG=3D"ssh://127.0.0.1$TEST_IMG_FILE" elif [ "$IMGPROTO" =3D "nfs" ]; then TEST_IMG_FILE=3D$TEST_DIR/t.$IMGFMT diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index 4910fb2005..aa16d6cbdd 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -411,7 +411,7 @@ def remote_filename(path): if imgproto =3D=3D 'file': return path elif imgproto =3D=3D 'ssh': - return "ssh://127.0.0.1%s" % (path) + return "ssh://%s@127.0.0.1:22%s" % (os.environ.get('USER'), path) else: raise Exception("Protocol %s not supported" % (imgproto)) =20 --=20 2.20.1 From nobody Fri May 17 02:41:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551121903472520.696277168435; Mon, 25 Feb 2019 11:11:43 -0800 (PST) Received: from localhost ([127.0.0.1]:43073 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLf9-0004hR-Gs for importer@patchew.org; Mon, 25 Feb 2019 14:11:39 -0500 Received: from eggs.gnu.org ([209.51.188.92]:38429) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyLcJ-00030c-9c for qemu-devel@nongnu.org; Mon, 25 Feb 2019 14:08:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyLcI-0001eu-KU for qemu-devel@nongnu.org; Mon, 25 Feb 2019 14:08:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:31194) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gyLcG-0001dV-QM; Mon, 25 Feb 2019 14:08:40 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 227DA301A642; Mon, 25 Feb 2019 19:08:40 +0000 (UTC) Received: from localhost (ovpn-117-37.ams2.redhat.com [10.36.117.37]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B77CC608AB; Mon, 25 Feb 2019 19:08:37 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 25 Feb 2019 20:08:28 +0100 Message-Id: <20190225190828.17726-3-mreitz@redhat.com> In-Reply-To: <20190225190828.17726-1-mreitz@redhat.com> References: <20190225190828.17726-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Mon, 25 Feb 2019 19:08:40 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 2/2] block/ssh: Implement .bdrv_dirname() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , "Richard W . M . Jones" , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" ssh_bdrv_dirname() is basically the generic bdrv_dirname(), except it takes care not to silently chop off any query string (i.e., host_key_check). Signed-off-by: Max Reitz Tested-by: Richard W.M. Jones --- block/ssh.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/block/ssh.c b/block/ssh.c index 88401bbd31..b519c00869 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -1295,6 +1295,26 @@ static void ssh_refresh_filename(BlockDriverState *b= s) } } =20 +static char *ssh_bdrv_dirname(BlockDriverState *bs, Error **errp) +{ + if (qdict_haskey(bs->full_open_options, "host_key_check")) { + /* + * We cannot generate a simple prefix if we would have to + * append a query string. + */ + error_setg(errp, + "Cannot generate a base directory with host_key_check s= et"); + return NULL; + } + + if (bs->exact_filename[0] =3D=3D '\0') { + error_setg(errp, "Cannot generate a base directory for this ssh no= de"); + return NULL; + } + + return path_combine(bs->exact_filename, ""); +} + static const char *const ssh_strong_runtime_opts[] =3D { "host", "port", @@ -1322,6 +1342,7 @@ static BlockDriver bdrv_ssh =3D { .bdrv_co_truncate =3D ssh_co_truncate, .bdrv_co_flush_to_disk =3D ssh_co_flush, .bdrv_refresh_filename =3D ssh_refresh_filename, + .bdrv_dirname =3D ssh_bdrv_dirname, .create_opts =3D &ssh_create_opts, .strong_runtime_opts =3D ssh_strong_runtime_opts, }; --=20 2.20.1