All of lore.kernel.org
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
	"Richard W . M . Jones" <rjones@redhat.com>,
	Jeff Cody <jcody@redhat.com>, Kevin Wolf <kwolf@redhat.com>
Subject: [Qemu-devel] [PATCH 1/2] block/ssh: Implement .bdrv_refresh_filename()
Date: Mon,  5 Feb 2018 21:22:31 +0100	[thread overview]
Message-ID: <20180205202232.19254-2-mreitz@redhat.com> (raw)
In-Reply-To: <20180205202232.19254-1-mreitz@redhat.com>

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 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 {
 
     /* 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;
 
 static void ssh_state_init(BDRVSSHState *s)
@@ -86,6 +92,8 @@ static void ssh_state_init(BDRVSSHState *s)
 
 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 *options,
     int r, ret;
     QemuOpts *opts = NULL;
     Error *local_err = NULL;
-    const char *user, *path, *host_key_check;
+    const char *path, *host_key_check;
     long port = 0;
     unsigned long portU = 0;
     int new_sock = -1;
@@ -594,10 +602,10 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
         goto err;
     }
 
-    user = qemu_opt_get(opts, "user");
-    if (!user) {
-        user = g_get_user_name();
-        if (!user) {
+    s->user = g_strdup(qemu_opt_get(opts, "user"));
+    if (!s->user) {
+        s->user = g_strdup(g_get_user_name());
+        if (!s->user) {
             error_setg_errno(errp, errno, "Can't get user name");
             ret = -errno;
             goto err;
@@ -642,7 +650,7 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
      */
     ssh_set_blocking(s->session, 1);
 
-    r = ssh_options_set(s->session, SSH_OPTIONS_USER, user);
+    r = ssh_options_set(s->session, SSH_OPTIONS_USER, s->user);
     if (r < 0) {
         ret = -EINVAL;
         session_error_setg(errp, s,
@@ -704,7 +712,7 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
     }
 
     /* Authenticate. */
-    ret = authenticate(s, user, errp);
+    ret = authenticate(s, s->user, errp);
     if (ret < 0) {
         goto err;
     }
@@ -1156,6 +1164,36 @@ static int64_t ssh_getlength(BlockDriverState *bs)
     return length;
 }
 
+static void ssh_refresh_filename(BlockDriverState *bs)
+{
+    BDRVSSHState *s = 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 = qdict_get_try_str(bs->full_open_options, "path");
+    assert(path); /* mandatory option */
+
+    host_key_check = qdict_get_try_str(bs->full_open_options, "host_key_check");
+
+    ret = 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=" : "",
+                   host_key_check ?: "");
+    if (ret >= sizeof(bs->exact_filename)) {
+        /* An overflow makes the filename unusable, so do not report any */
+        bs->exact_filename[0] = '\0';
+    }
+}
+
 static const char *const ssh_sgfnt_runtime_opts[] = {
     "host",
     "port",
@@ -1180,6 +1218,7 @@ static BlockDriver bdrv_ssh = {
     .bdrv_co_writev               = ssh_co_writev,
     .bdrv_getlength               = ssh_getlength,
     .bdrv_co_flush_to_disk        = ssh_co_flush,
+    .bdrv_refresh_filename        = ssh_refresh_filename,
     .create_opts                  = &ssh_create_opts,
     .sgfnt_runtime_opts           = ssh_sgfnt_runtime_opts,
 };
-- 
2.14.3

  reply	other threads:[~2018-02-05 20:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-05 20:22 [Qemu-devel] [PATCH 0/2] block/ssh: Implement .bdrv_refresh_filename() Max Reitz
2018-02-05 20:22 ` Max Reitz [this message]
2018-02-05 20:22 ` [Qemu-devel] [PATCH 2/2] block/ssh: Implement .bdrv_dirname() Max Reitz
2018-02-05 20:45 ` [Qemu-devel] [PATCH 0/2] block/ssh: Implement .bdrv_refresh_filename() Richard W.M. Jones
2018-02-05 20:46 ` Richard W.M. Jones
2018-02-05 20:56   ` Max Reitz
2018-03-06 21:51 ` [Qemu-devel] [Qemu-block] " John Snow
2018-03-07 10:04   ` Kevin Wolf
2018-03-07 17:50     ` John Snow
2018-03-08  2:12       ` Fam Zheng
2018-03-12 15:47         ` John Snow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180205202232.19254-2-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=jcody@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rjones@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.