From 7ea14c76e75d73ada183b472fabdf6ad2436c39c Mon Sep 17 00:00:00 2001 From: Michael Albinus Date: Wed, 8 May 2024 13:44:15 +0200 Subject: [PATCH] More Tramp optional methods * doc/misc/tramp.texi (Inline methods) : (External methods) : These are optional methods. * etc/NEWS: Mention more optional Tramp methods. * lisp/net/tramp-androidsu.el (tramp-enable-androidsu-method): Use proper regexp for `tramp-default-user-alist'. * lisp/net/tramp-sh.el (tramp-enable-nc-method) (tramp-enable-ksu-method, tramp-enable-krlogin-method) (tramp-enable-fcp-method): New defuns. Move respective configurations there. * lisp/net/tramp.el (tramp-enable-method): Implement completion for interactive use. * test/lisp/net/tramp-tests.el (tramp-test03-file-name-defaults) (tramp-test03-file-name-host-rules): Extend tests. (cherry picked from commit 339b8349f436b3738cfa02b236e05c3da4c5a8a5) --- doc/misc/tramp.texi | 9 ++- etc/NEWS | 3 +- lisp/net/tramp-androidsu.el | 3 +- lisp/net/tramp-sh.el | 133 +++++++++++++++++++++-------------- lisp/net/tramp.el | 22 +++++- test/lisp/net/tramp-tests.el | 45 ++++++------ 6 files changed, 140 insertions(+), 75 deletions(-) diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi index aa628e55f82..76d8136d196 100644 --- a/doc/misc/tramp.texi +++ b/doc/misc/tramp.texi @@ -923,12 +923,15 @@ missing shell prompts that confuses @value{tramp}. This method is also similar to @option{ssh}. It uses the @command{krlogin -x} command only for remote host login. +This method is an optional method, @ref{Optional methods}. + @item @option{ksu} @cindex method @option{ksu} @cindex @option{ksu} method @cindex kerberos (with @option{ksu} method) -This is another method from the Kerberos suite. It behaves like @option{su}. +This is another method from the Kerberos suite. It behaves like +@option{su}. It is an optional method, @ref{Optional methods}. @item @option{plink} @cindex method @option{plink} @@ -1151,6 +1154,8 @@ The command used for this connection is: @samp{fsh @var{host} -l not useful for @value{tramp}. @command{fsh} connects to remote host and @value{tramp} keeps that one connection open. +This is an optional method, @ref{Optional methods}. + @item @option{nc} @cindex method @option{nc} @cindex @option{nc} method @@ -1162,6 +1167,8 @@ NAS hosts. These dumb devices have severely restricted local shells, such as the @command{busybox} and do not host any other encode or decode programs. +This is an optional method, @ref{Optional methods}. + @item @option{sudoedit} @cindex method @option{sudoedit} @cindex @option{sudoedit} method diff --git a/etc/NEWS b/etc/NEWS index 7c58d01444e..ec98255c011 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1151,7 +1151,8 @@ mode line. 'header' will display in the header line; +++ *** Tramp methods can be optional. An optional connection method is not enabled by default. The user must -enable it explicitly by the 'tramp-enable-method' command. +enable it explicitly by the 'tramp-enable-method' command. The existing +methods "fcp", "krlogin", " ksu" and "nc" are optional now. +++ *** New optional connection method "androidsu". diff --git a/lisp/net/tramp-androidsu.el b/lisp/net/tramp-androidsu.el index 09dcd4d7bed..953f75ad9f3 100644 --- a/lisp/net/tramp-androidsu.el +++ b/lisp/net/tramp-androidsu.el @@ -91,7 +91,8 @@ may edit files belonging to any and all applications." (tramp-shell-name ,tramp-androidsu-local-shell-name))) (add-to-list 'tramp-default-user-alist - `(,tramp-androidsu-method nil ,tramp-root-id-string))) + `(,(rx bos (literal tramp-androidsu-method) eos) + nil ,tramp-root-id-string))) ;;;###tramp-autoload (tramp--with-startup diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el index 5af1d723bb9..bb136d6e6b5 100644 --- a/lisp/net/tramp-sh.el +++ b/lisp/net/tramp-sh.el @@ -272,22 +272,6 @@ The string is used in `tramp-methods'.") (tramp-remote-shell ,tramp-default-remote-shell) (tramp-remote-shell-login ("-l")) (tramp-remote-shell-args ("-c")))) - (add-to-list 'tramp-methods - `("nc" - (tramp-login-program "telnet") - (tramp-login-args (("%h") ("%p") ("%n"))) - (tramp-remote-shell ,tramp-default-remote-shell) - (tramp-remote-shell-login ("-l")) - (tramp-remote-shell-args ("-c")) - (tramp-copy-program "nc") - ;; We use "-v" for better error tracking. - (tramp-copy-args (("-w" "1") ("-v") ("%h") ("%r"))) - (tramp-copy-file-name (("%f"))) - (tramp-remote-copy-program "nc") - ;; We use "-p" as required for newer busyboxes. For older - ;; busybox/nc versions, the value must be (("-l") ("%r")). This - ;; can be achieved by tweaking `tramp-connection-properties'. - (tramp-remote-copy-args (("-l") ("-p" "%r") ("%n"))))) (add-to-list 'tramp-methods `("su" (tramp-login-program "su") @@ -328,21 +312,6 @@ The string is used in `tramp-methods'.") (tramp-connection-timeout 10) (tramp-session-timeout 300) (tramp-password-previous-hop t))) - (add-to-list 'tramp-methods - `("ksu" - (tramp-login-program "ksu") - (tramp-login-args (("%u") ("-q"))) - (tramp-remote-shell ,tramp-default-remote-shell) - (tramp-remote-shell-login ("-l")) - (tramp-remote-shell-args ("-c")) - (tramp-connection-timeout 10))) - (add-to-list 'tramp-methods - `("krlogin" - (tramp-login-program "krlogin") - (tramp-login-args (("%h") ("-l" "%u") ("-x"))) - (tramp-remote-shell ,tramp-default-remote-shell) - (tramp-remote-shell-login ("-l")) - (tramp-remote-shell-args ("-c")))) (add-to-list 'tramp-methods `("plink" (tramp-login-program "plink") @@ -403,30 +372,18 @@ The string is used in `tramp-methods'.") (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k"))) (tramp-copy-keep-date t))) - (add-to-list 'tramp-methods - `("fcp" - (tramp-login-program "fsh") - (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i"))) - (tramp-remote-shell ,tramp-default-remote-shell) - (tramp-remote-shell-login ("-l")) - (tramp-remote-shell-args ("-i") ("-c")) - (tramp-copy-program "fcp") - (tramp-copy-args (("-p" "%k"))) - (tramp-copy-keep-date t))) (add-to-list 'tramp-default-method-alist `(,tramp-local-host-regexp ,(rx bos (literal tramp-root-id-string) eos) "su")) (add-to-list 'tramp-default-user-alist - `(,(rx bos (| "su" "sudo" "doas" "ksu") eos) + `(,(rx bos (| "su" "sudo" "doas") eos) nil ,tramp-root-id-string)) ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored. ;; Do not add "plink" based methods, they ask interactively for the user. (add-to-list 'tramp-default-user-alist - `(,(rx bos - (| "rcp" "remcp" "rsh" "telnet" "nc" "krlogin" "fcp") - eos) + `(,(rx bos (| "rcp" "remcp" "rsh" "telnet") eos) nil ,(user-login-name)))) (defconst tramp-default-copy-file-name '(("%u" "@") ("%h" ":") ("%f")) @@ -508,20 +465,94 @@ The string is used in `tramp-methods'.") (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh) (tramp-set-completion-function "telnet" tramp-completion-function-alist-telnet) - (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet) (tramp-set-completion-function "su" tramp-completion-function-alist-su) (tramp-set-completion-function "sudo" tramp-completion-function-alist-su) (tramp-set-completion-function "doas" tramp-completion-function-alist-su) - (tramp-set-completion-function "ksu" tramp-completion-function-alist-su) (tramp-set-completion-function "sg" tramp-completion-function-alist-sg) - (tramp-set-completion-function - "krlogin" tramp-completion-function-alist-rsh) (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh) (tramp-set-completion-function "plinkx" tramp-completion-function-alist-putty) (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh) - (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh) - (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)) + (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh)) + +;;;###tramp-autoload +(defun tramp-enable-nc-method () + "Enable \"ksu\" method." + (add-to-list 'tramp-methods + `("nc" + (tramp-login-program "telnet") + (tramp-login-args (("%h") ("%p") ("%n"))) + (tramp-remote-shell ,tramp-default-remote-shell) + (tramp-remote-shell-login ("-l")) + (tramp-remote-shell-args ("-c")) + (tramp-copy-program "nc") + ;; We use "-v" for better error tracking. + (tramp-copy-args (("-w" "1") ("-v") ("%h") ("%r"))) + (tramp-copy-file-name (("%f"))) + (tramp-remote-copy-program "nc") + ;; We use "-p" as required for newer busyboxes. For + ;; older busybox/nc versions, the value must be + ;; (("-l") ("%r")). This can be achieved by tweaking + ;; `tramp-connection-properties'. + (tramp-remote-copy-args (("-l") ("-p" "%r") ("%n"))))) + + (add-to-list 'tramp-default-user-alist + `(,(rx bos "nc" eos) nil ,(user-login-name))) + + (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet)) + +;;;###tramp-autoload +(defun tramp-enable-ksu-method () + "Enable \"ksu\" method." + (add-to-list 'tramp-methods + `("ksu" + (tramp-login-program "ksu") + (tramp-login-args (("%u") ("-q"))) + (tramp-remote-shell ,tramp-default-remote-shell) + (tramp-remote-shell-login ("-l")) + (tramp-remote-shell-args ("-c")) + (tramp-connection-timeout 10))) + + (add-to-list 'tramp-default-user-alist + `(,(rx bos "ksu" eos) nil ,tramp-root-id-string)) + + (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)) + +;;;###tramp-autoload +(defun tramp-enable-krlogin-method () + "Enable \"krlogin\" method." + (add-to-list 'tramp-methods + `("krlogin" + (tramp-login-program "krlogin") + (tramp-login-args (("%h") ("-l" "%u") ("-x"))) + (tramp-remote-shell ,tramp-default-remote-shell) + (tramp-remote-shell-login ("-l")) + (tramp-remote-shell-args ("-c")))) + + (add-to-list 'tramp-default-user-alist + `(,(rx bos "krlogin" eos) nil ,(user-login-name))) + + (tramp-set-completion-function + "krlogin" tramp-completion-function-alist-rsh)) + +;;;###tramp-autoload +(defun tramp-enable-fcp-method () + "Enable \"fcp\" method." + (add-to-list 'tramp-methods + `("fcp" + (tramp-login-program "fsh") + (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i"))) + (tramp-remote-shell ,tramp-default-remote-shell) + (tramp-remote-shell-login ("-l")) + (tramp-remote-shell-args ("-i") ("-c")) + (tramp-copy-program "fcp") + (tramp-copy-args (("-p" "%k"))) + (tramp-copy-keep-date t))) + + (add-to-list 'tramp-default-user-alist + `(,(rx bos "fcp" eos) nil ,(user-login-name))) + + (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)) (defcustom tramp-sh-extra-args `((,(rx (| bos "/") "bash" eos) . "-noediting -norc -noprofile") diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el index 6b002366c17..f2f25e60cc2 100644 --- a/lisp/net/tramp.el +++ b/lisp/net/tramp.el @@ -1462,7 +1462,27 @@ calling HANDLER.") (defun tramp-enable-method (method) "Enable optional METHOD if possible." - (interactive "Mmethod: ") + (interactive + (list + (completing-read + "method: " + (seq-keep + (lambda (x) + (when-let ((name (symbol-name x)) + ;; It must match `tramp-enable-METHOD-method'. + ((string-match + (rx "tramp-enable-" + (group (regexp tramp-method-regexp)) + "-method") + name)) + (method (match-string 1 name)) + ;; It must not be enabled yet. + ((not (assoc method tramp-methods)))) + method)) + ;; All method enabling functions. + (mapcar + #'intern (all-completions "tramp-enable-" obarray #'functionp)))))) + (when-let (((not (assoc method tramp-methods))) (fn (intern (format "tramp-enable-%s-method" method))) ((functionp fn))) diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index 88d6c13331d..931bc25ef43 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -2103,14 +2103,18 @@ is greater than 10. (string-equal (file-remote-p (format "/-:%s@:" u) 'method) "ftp")))) ;; Default values in tramp-sh.el and tramp-sudoedit.el. (when (assoc "su" tramp-methods) - (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name))) + (dolist + (h `("127.0.0.1" "[::1]" "localhost" "localhost4" "localhost6" + "ip6-localhost" "ip6-loopback" ,(system-name))) (should - (string-equal (file-remote-p (format "/-:root@%s:" h) 'method) "su"))) - (dolist (m '("su" "sudo" "ksu" "doas" "sudoedit")) + (string-equal (file-remote-p (format "/-:root@%s:" h) 'method) "su")))) + (dolist (m '("su" "sudo" "ksu" "doas" "sudoedit")) + (when (assoc m tramp-methods) (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")) (should - (string-equal (file-remote-p (format "/%s::" m) 'host) (system-name)))) - (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp" "nc")) + (string-equal (file-remote-p (format "/%s::" m) 'host) (system-name))))) + (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp" "nc")) + (when (assoc m tramp-methods) (should (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))) @@ -2128,21 +2132,22 @@ is greater than 10. ;; Host names must match rules in case the command template of a ;; method doesn't use them. (dolist (m '("su" "sg" "sudo" "doas" "ksu")) - (let (tramp-connection-properties tramp-default-proxies-alist) - (ignore-errors - (tramp-cleanup-connection tramp-test-vec nil 'keep-password)) - ;; Single hop. The host name must match `tramp-local-host-regexp'. - (should-error - (find-file (format "/%s:foo:" m)) - :type 'user-error) - ;; Multi hop. The host name must match the previous hop. - (should-error - (find-file - (format - "%s|%s:foo:" - (substring (file-remote-p ert-remote-temporary-file-directory) 0 -1) - m)) - :type 'user-error)))) + (when (assoc m tramp-methods) + (let (tramp-connection-properties tramp-default-proxies-alist) + (ignore-errors + (tramp-cleanup-connection tramp-test-vec nil 'keep-password)) + ;; Single hop. The host name must match `tramp-local-host-regexp'. + (should-error + (find-file (format "/%s:foo:" m)) + :type 'user-error) + ;; Multi hop. The host name must match the previous hop. + (should-error + (find-file + (format + "%s|%s:foo:" + (substring (file-remote-p ert-remote-temporary-file-directory) 0 -1) + m)) + :type 'user-error))))) (ert-deftest tramp-test03-file-name-method-rules () "Check file name rules for some methods." -- 2.39.5