]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Provide no auth callbacks for bash completer
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 14 May 2026 14:32:30 +0000 (16:32 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 18 May 2026 07:58:55 +0000 (09:58 +0200)
Our bash completion script parses (partially incomplete) command
line and looks for two arguments: --readonly and --connect
because in the next step it executes virsh with those two
arguments like this:

  virsh --readonly --connect $URI complete -- "text to complete"

Now, whenever virsh sees connection URI specified on its cmd line
it connects to it right away (before executing any command). This
happens inside virshConnect(). Here, virConnectOpenAuth() is
called with the default auth callback (virConnectAuthPtrDefault).
In majority of the cases this is desirable, as it might ask user
for credentials (password for example). But in case of bash
completion this is not desired because bash completion script
must not expect users to input anything (that's why we even
close stdin in cmdComplete()).

Therefore, when connecting from virsh that's executed by the bash
completion script provide no auth callbacks to prevent virsh from
asking for credentials.

Resolves: https://gitlab.com/libvirt/libvirt/-/work_items/879
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Tested-by: Richrad W.M. Jones <rjones@redhat.com>
tools/virsh.c

index fdce3220b3fc1d1785edb61af826b8df1c1194c8..72d233f98d0b3b006f3aa5c30fd6e605b251342a 100644 (file)
@@ -124,8 +124,16 @@ virshConnect(vshControl *ctl, const char *uri, bool readonly)
 
     do {
         virErrorPtr err;
+        virConnectAuthPtr auth = virConnectAuthPtrDefault;
 
-        if ((c = virConnectOpenAuth(uri, virConnectAuthPtrDefault,
+        if (ctl->cmd->def->handler == cmdComplete) {
+            /* When running from a bash completer we need to avoid any kind of
+             * keyboard input (e.g. ssh asking for a password). To achieve
+             * this, provide no authentication callbacks. */
+            auth = NULL;
+        }
+
+        if ((c = virConnectOpenAuth(uri, auth,
                                     readonly ? VIR_CONNECT_RO : 0)))
             break;