From nobody Thu May 2 01:47:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.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 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517862518805613.0918732380882; Mon, 5 Feb 2018 12:28:38 -0800 (PST) Received: from localhost ([::1]:60443 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1einNT-0002c4-VN for importer@patchew.org; Mon, 05 Feb 2018 15:28:36 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34722) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1einHp-0004qe-5n for qemu-devel@nongnu.org; Mon, 05 Feb 2018 15:22:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1einHn-0006z1-Mm for qemu-devel@nongnu.org; Mon, 05 Feb 2018 15:22:45 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52706) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1einHk-0006xP-Uq; Mon, 05 Feb 2018 15:22:41 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 440D51752B8; Mon, 5 Feb 2018 20:22:40 +0000 (UTC) Received: from localhost (unknown [10.40.205.49]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B9E8260E39; Mon, 5 Feb 2018 20:22:37 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 5 Feb 2018 21:22:31 +0100 Message-Id: <20180205202232.19254-2-mreitz@redhat.com> In-Reply-To: <20180205202232.19254-1-mreitz@redhat.com> References: <20180205202232.19254-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 05 Feb 2018 20:22:40 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 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" , Jeff Cody , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Max Reitz --- block/ssh.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/block/ssh.c b/block/ssh.c index c31b2686c3..a5b7e831a4 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -75,6 +75,12 @@ 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) @@ -86,6 +92,8 @@ static void ssh_state_init(BDRVSSHState *s) =20 static void ssh_state_free(BDRVSSHState *s) { + g_free(s->user); + if (s->attrs) { sftp_attributes_free(s->attrs); } @@ -569,7 +577,7 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *optio= ns, int r, ret; QemuOpts *opts =3D NULL; Error *local_err =3D NULL; - const char *user, *path, *host_key_check; + const char *path, *host_key_check; long port =3D 0; unsigned long portU =3D 0; int new_sock =3D -1; @@ -594,10 +602,10 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *opt= ions, goto err; } =20 - user =3D qemu_opt_get(opts, "user"); - if (!user) { - user =3D g_get_user_name(); - if (!user) { + s->user =3D g_strdup(qemu_opt_get(opts, "user")); + if (!s->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; @@ -642,7 +650,7 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *optio= ns, */ ssh_set_blocking(s->session, 1); =20 - r =3D ssh_options_set(s->session, SSH_OPTIONS_USER, user); + r =3D ssh_options_set(s->session, SSH_OPTIONS_USER, s->user); if (r < 0) { ret =3D -EINVAL; session_error_setg(errp, s, @@ -704,7 +712,7 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *optio= ns, } =20 /* Authenticate. */ - ret =3D authenticate(s, user, errp); + ret =3D authenticate(s, s->user, errp); if (ret < 0) { goto err; } @@ -1156,6 +1164,36 @@ static int64_t ssh_getlength(BlockDriverState *bs) return length; } =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_sgfnt_runtime_opts[] =3D { "host", "port", @@ -1180,6 +1218,7 @@ static BlockDriver bdrv_ssh =3D { .bdrv_co_writev =3D ssh_co_writev, .bdrv_getlength =3D ssh_getlength, .bdrv_co_flush_to_disk =3D ssh_co_flush, + .bdrv_refresh_filename =3D ssh_refresh_filename, .create_opts =3D &ssh_create_opts, .sgfnt_runtime_opts =3D ssh_sgfnt_runtime_opts, }; --=20 2.14.3 From nobody Thu May 2 01:47:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.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 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517862651462748.7743822479458; Mon, 5 Feb 2018 12:30:51 -0800 (PST) Received: from localhost ([::1]:32783 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1einPe-0005Et-Gr for importer@patchew.org; Mon, 05 Feb 2018 15:30:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34742) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1einHs-0004vB-Is for qemu-devel@nongnu.org; Mon, 05 Feb 2018 15:22:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1einHp-000709-Qq for qemu-devel@nongnu.org; Mon, 05 Feb 2018 15:22:48 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56926) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1einHn-0006yY-EE; Mon, 05 Feb 2018 15:22:43 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9DC9C15567; Mon, 5 Feb 2018 20:22:42 +0000 (UTC) Received: from localhost (unknown [10.40.205.49]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 312E65D6A2; Mon, 5 Feb 2018 20:22:42 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Mon, 5 Feb 2018 21:22:32 +0100 Message-Id: <20180205202232.19254-3-mreitz@redhat.com> In-Reply-To: <20180205202232.19254-1-mreitz@redhat.com> References: <20180205202232.19254-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 05 Feb 2018 20:22:42 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 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" , Jeff Cody , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 --- block/ssh.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/block/ssh.c b/block/ssh.c index a5b7e831a4..7dbe55cec1 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -1194,6 +1194,24 @@ 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_sgfnt_runtime_opts[] =3D { "host", "port", @@ -1219,6 +1237,7 @@ static BlockDriver bdrv_ssh =3D { .bdrv_getlength =3D ssh_getlength, .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, .sgfnt_runtime_opts =3D ssh_sgfnt_runtime_opts, }; --=20 2.14.3