Subject: 28.2; tramp: Remote host name not always accounted when computing multi-hops
From: Jani Juhani Sinervo <[email-masked]>
To: [email-masked]
Date: Mon, 02 Jan 2023 13:57:29 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

When using a custom TRAMP method with a program that expects the host
name to be passed in as an argument to a command-line switch, the
natural way of writing the `tramp-login-args' like (("--switch" "%h"))
does not work, since `tramp-compute-multi-hops' only considers the host
name to have been used within the command template, if it is in its own
list, as in (("--switch") ("%h")).

This is a problem, because there are tools, like Toolbox[0], which
allow running the command within a specific container. For example with
Toolbox, it would look something like the following:

`toolbox run -c container-name-here command'

This can be turned into a TRAMP method relatively easily, and the most
natural way to write the `tramp-login-args' would be the following:

(tramp-login-args (("run") ("-c" "%h") ("sh")))

This however will not work for the aforementioned bug in
`tramp-compute-multi-hops'. While splitting the "-c" and "%h" into
their own lists works, and would work for the usecase of
`C-x C-f /toolbox:container-name-here:', there is a reasonable
interpretation for `C-x C-f /toolbox::', entering the default
container, in which case splitting the optional "-c"-flag from the
"host name" would not be the valid thing to do.

[0]: https://containertoolbx.org/

Steps to reproduce from `emacs -Q':

1. Evaluate the following Lisp in the scratch buffer:

(require 'tramp)
(add-to-list 'tramp-methods
             (cons "ssh4"
                   '((tramp-login-program "ssh")
                     (tramp-login-args (("-l" "%u")
                                        ("-p" "%p")
                                        ("%c")
                                        ("-e" "none")
                                        ("-t" "-t")
                                        ("-o" "RemoteCommand=\"%l\"")
                                        ("-4" "%h")))
                     (tramp-remote-shell "/bin/sh")
                     (tramp-remote-shell-login
                      ("-l"))
                     (tramp-remote-shell-args
                      ("-c")))))

2. Try to connect somewhere with this method, for example by doing:

`C-x C-f /ssh4:example.org: TAB'

Expected behavior:

TRAMP should try to connect to the host according to the method
definition.

Actual behavior:

TRAMP gives the error message:

Host name ‘example.org’ does not match
‘\`\(127\.0\.0\.1\|::1\|hostname\|localhost6?\)\'’

The code responsible can be found in `lisp/net/tramp.el' and within the
function `tramp-compute-multi-hops'. Excerpt:

;; Some methods ("su", "sg", "sudo", "doas", "ksu") do not use the
;; host name in their command template.  In this case, the remote
;; file name must use either a local host name (first hop), or a
;; host name matching the previous hop.
(let ((previous-host (or tramp-local-host-regexp "")))
  (setq choices target-alist)
  (while (setq item (pop choices))
    (let ((host (tramp-file-name-host item)))
      (unless
          (or
           ;; The host name is used for the remote shell command.
           (member
            '("%h") (tramp-get-method-parameter item 'tramp-login-
args))
           ;; The host name must match previous hop.
           (string-match-p previous-host host))
        (setq tramp-default-proxies-alist saved-tdpa)
        (tramp-user-error
         vec "Host name `%s' does not match `%s'" host previous-host))
      (setq previous-host (concat "^" (regexp-quote host) "$")))))

We notice that the culprit here is the call to `member', where it
erroneously checks for the host name being used by the command template
by only checking whether the literal list '("%h") is within the
`tramp-login-args'.

My proposed fix would be the go through the `tramp-login-args' method
parameter, and go through each of the list-of-lists to check whether
there is a string "%h" there.

In GNU Emacs 28.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.35,
cairo version 1.16.0)
 of 2022-12-09 built on cncf-x86-3
Windowing system distributor 'The X.Org Foundation', version
11.0.12201007
System Description: Freedesktop.org SDK 22.08 (Flatpak runtime)

Configured using:
 'configure --prefix=/app --with-gnutls 'CFLAGS=-O2 -g -pipe
 -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions
 -fstack-protector-strong -grecord-gcc-switches
 -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection
 -fno-omit-frame-pointer ' 'LDFLAGS=-L/app/lib -Wl,-z,relro,-z,now
 -Wl,--as-needed ''

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG
JSON
LCMS2 LIBSELINUX LIBSYSTEMD LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PNG
RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS X11 XDBE XIM XPM
GTK3 ZLIB

Important settings:
  value of $LANG: en_GB.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Fundamental

Minor modes in effect:
  electric-pair-mode: t
  icomplete-mode: t
  yas-global-mode: t
  global-git-commit-mode: t
  magit-auto-revert-mode: t
  shell-dirtrack-mode: t
  global-corfu-mode: t
  override-global-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  column-number-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t

Load-path shadows:
/var/home/jani/.emacs.d/elpa/transient-20221202.1727/transient hides
/app/share/emacs/28.2/lisp/transient

Features:
(shadow sort mail-extr emacsbug sendmail checkdoc lisp-mnt
rainbow-delimiters elec-pair cus-edit cus-load wid-edit icomplete
geiser
slime-tramp docker-tramp tramp-cache tramp tramp-loaddefs trampver
tramp-integration files-x tramp-compat parse-time iso8601 ls-lisp
slime-asdf grep slime-fancy slime-indentation slime-cl-indent cl-indent
slime-trace-dialog slime-fontifying-fu slime-package-fu slime-
references
slime-compiler-notes-tree advice slime-scratch slime-presentations
bridge slime-macrostep macrostep slime-mdot-fu slime-enclosing-context
slime-fuzzy slime-fancy-trace slime-fancy-inspector slime-c-p-c
slime-editing-commands slime-autodoc slime-repl slime-parse slime
arc-mode archive-mode pp hyperspec ibuffer ibuffer-loaddefs elixir-mode
elixir-format elixir-smie pass f f-shortdoc shortdoc s password-store
auth-source-pass adoc-mode tempo markup-faces markdown-mode
edit-indirect color haskell-mode haskell-cabal haskell-utils
haskell-font-lock haskell-indentation haskell-string
haskell-sort-imports haskell-lexeme haskell-align-imports
haskell-complete-module haskell-ghc-support noutline outline etags
fileloop generator xref dabbrev haskell-customize typescript-mode
cc-mode cc-fonts cc-guess cc-menus cc-cmds cc-styles cc-align cc-engine
cc-vars cc-defs irony irony-iotask meson-mode smie tango-theme
yasnippet
magit-submodule magit-obsolete magit-blame magit-stash magit-reflog
magit-bisect magit-push magit-pull magit-fetch magit-clone magit-remote
magit-commit magit-sequence magit-notes magit-worktree magit-tag
magit-merge magit-branch magit-reset magit-files magit-refs magit-
status
magit magit-repos magit-apply magit-wip magit-log which-func imenu
magit-diff smerge-mode diff diff-mode git-commit rx log-edit message
rmc
puny dired dired-loaddefs rfc822 mml mml-sec epa derived epg rfc6068
epg-config gnus-util rmail rmail-loaddefs time-date mm-decode mm-bodies
mm-encode mail-parse rfc2231 rfc2047 rfc2045 mm-util ietf-drums
mail-prsvr mailabbrev mail-utils gmm-utils mailheader pcvs-util add-log
magit-core magit-autorevert autorevert filenotify magit-margin
magit-transient magit-process with-editor shell pcomplete server
magit-mode transient edmacro kmacro magit-git magit-base magit-section
format-spec crm dash compat-27 compat-26 compat compat-macs flymake-
proc
flymake project compile text-property-search comint ansi-color ring
warnings thingatpt corfu cl-extra help-mode use-package-bind-key
bind-key easy-mmode use-package-ensure use-package-core tex-site
slime-autoloads info package browse-url url url-proxy url-privacy
url-expand url-methods url-history url-cookie url-domsuf url-util
mailcap url-handlers url-parse auth-source cl-seq eieio eieio-core
cl-macs eieio-loaddefs password-cache json subr-x map url-vars seq
byte-opt gv bytecomp byte-compile cconv cl-loaddefs cl-lib iso-transl
tooltip eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode mwheel term/x-win x-win term/common-win x-
dnd
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj
charscript
charprop case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
cairo move-toolbar gtk x-toolkit x multi-tty make-network-process
emacs)

Memory information:
((conses 16 262115 24837)
 (symbols 48 27853 4)
 (strings 32 93904 4769)
 (string-bytes 1 3030693)
 (vectors 16 50616)
 (vector-slots 8 589420 28184)
 (floats 8 264 120)
 (intervals 56 490 79)
 (buffers 992 13))