From 3abe7bfe306706a95ca8dc404c7645073c949507 Mon Sep 17 00:00:00 2001 From: Michael Albinus Date: Tue, 6 Nov 2018 15:48:05 +0100 Subject: [PATCH] Handle also port and domain in Tramp proxy definitions * doc/misc/tramp.texi (Multi-hops): Exclude ports and domains from pattern expansion. * lisp/net/tramp-cmds.el (tramp-cleanup-all-connections): Remove ad-hoc proxies. * lisp/net/tramp-sh.el (tramp-compute-multi-hops): Handle also port and domain in the proxy. Propertize ad-hoc proxies. * lisp/net/tramp.el (tramp-default-proxies-alist): Adapt docstring. * test/lisp/net/tramp-tests.el (tramp-test02-file-name-dissect) (tramp-test02-file-name-dissect-simplified) (tramp-test02-file-name-dissect-separate): Extend tests. --- doc/misc/tramp.texi | 3 ++- lisp/net/tramp-cmds.el | 13 ++++++++++++ lisp/net/tramp-sh.el | 20 ++++++++++-------- lisp/net/tramp.el | 16 ++++++++++----- test/lisp/net/tramp-tests.el | 40 +++++++++++++++++++++--------------- 5 files changed, 60 insertions(+), 32 deletions(-) diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi index f68205519f3..a0b65d58b55 100644 --- a/doc/misc/tramp.texi +++ b/doc/misc/tramp.texi @@ -1374,7 +1374,8 @@ connect to @samp{bastion.your.domain}, then: @end lisp @var{proxy} can take patterns @code{%h} or @code{%u} for @var{host} or -@var{user} respectively. +@var{user} respectively. Ports or domains, if they are part of +a hop file name, are not expanded by those patterns. To login as @samp{root} on remote hosts in the domain @samp{your.domain}, but login as @samp{root} is disabled for non-local diff --git a/lisp/net/tramp-cmds.el b/lisp/net/tramp-cmds.el index b05f475f2fd..456300e7662 100644 --- a/lisp/net/tramp-cmds.el +++ b/lisp/net/tramp-cmds.el @@ -147,6 +147,19 @@ This includes password cache, file cache, connection cache, buffers." (when (bound-and-true-p tramp-archive-enabled) (tramp-archive-cleanup-hash)) + ;; Remove ad-hoc proxies. + (let ((proxies tramp-default-proxies-alist)) + (while proxies + (if (ignore-errors + (get-text-property 0 'tramp-ad-hoc (nth 2 (car proxies)))) + (setq tramp-default-proxies-alist + (delete (car proxies) tramp-default-proxies-alist) + proxies tramp-default-proxies-alist) + (setq proxies (cdr proxies))))) + (when (and tramp-default-proxies-alist tramp-save-ad-hoc-proxies) + (customize-save-variable + 'tramp-default-proxies-alist tramp-default-proxies-alist)) + ;; Remove buffers. (dolist (name (tramp-list-tramp-buffers)) (when (bufferp (get-buffer name)) (kill-buffer name)))) diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el index 11ee0639988..4fb011b3423 100644 --- a/lisp/net/tramp-sh.el +++ b/lisp/net/tramp-sh.el @@ -4571,21 +4571,21 @@ Goes through the list `tramp-inline-compress-commands'." ;; Ad-hoc proxy definitions. (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit))) - (let ((user (tramp-file-name-user item)) - (host (tramp-file-name-host item)) + (let ((user-domain (tramp-file-name-user-domain item)) + (host-port (tramp-file-name-host-port item)) (proxy (concat tramp-prefix-format proxy tramp-postfix-host-format))) (tramp-message vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")" - (and (stringp host) (regexp-quote host)) - (and (stringp user) (regexp-quote user)) + (and (stringp host-port) (regexp-quote host-port)) + (and (stringp user-domain) (regexp-quote user-domain)) proxy) ;; Add the hop. (add-to-list 'tramp-default-proxies-alist - (list (and (stringp host) (regexp-quote host)) - (and (stringp user) (regexp-quote user)) - proxy)) + (list (and (stringp host-port) (regexp-quote host-port)) + (and (stringp user-domain) (regexp-quote user-domain)) + (propertize proxy 'tramp-ad-hoc t))) (setq item (tramp-dissect-file-name proxy)))) ;; Save the new value. (when (and hops tramp-save-ad-hoc-proxies) @@ -4600,10 +4600,12 @@ Goes through the list `tramp-inline-compress-commands'." (when (and ;; Host. (string-match (or (eval (nth 0 item)) "") - (or (tramp-file-name-host (car target-alist)) "")) + (or (tramp-file-name-host-port (car target-alist)) + "")) ;; User. (string-match (or (eval (nth 1 item)) "") - (or (tramp-file-name-user (car target-alist)) ""))) + (or (tramp-file-name-user-domain (car target-alist)) + ""))) (if (null proxy) ;; No more hops needed. (setq choices nil) diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el index 13c3b5f939c..6d8e720563f 100644 --- a/lisp/net/tramp.el +++ b/lisp/net/tramp.el @@ -379,11 +379,17 @@ empty string for the method name." This is an alist of items (HOST USER PROXY). The first matching item specifies the proxy to be passed for a file name located on a remote target matching USER@HOST. HOST and USER are regular -expressions. PROXY must be a Tramp filename without a localname -part. Method and user name on PROXY are optional, which is -interpreted with the default values. PROXY can contain the -patterns %h and %u, which are replaced by the strings matching -HOST or USER, respectively. +expressions, which could also cover a domain (USER%DOMAIN) or +port (HOST#PORT). PROXY must be a Tramp filename without a +localname part. Method and user name on PROXY are optional, +which is interpreted with the default values. + +PROXY can contain the patterns %h and %u, which are replaced by +the strings matching HOST or USER (without DOMAIN and PORT parts), +respectively. + +If an entry is added while parsing ad-hoc hop definitions, PROXY +carries the non-nil text property `tramp-ad-hoc'. HOST, USER or PROXY could also be Lisp forms, which will be evaluated. The result must be a string or nil, which is diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index ceda70947c8..4016ece94d3 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -819,12 +819,14 @@ handled properly. BODY shall not contain a timeout." (file-remote-p (concat "/method1:%u@%h" - "|method2:%u@%h" - "|method3:user3@host3:/path/to/file")) - (format "/%s:%s@%s|%s:%s@%s|%s:%s@%s:" - "method1" "user3" "host3" - "method2" "user3" "host3" - "method3" "user3" "host3"))))) + "|method2:user2@host2" + "|method3:%u@%h" + "|method4:user4%domain4@host4#1234:/path/to/file")) + (format "/%s:%s@%s|%s:%s@%s|%s:%s@%s|%s:%s@%s:" + "method1" "user2" "host2" + "method2" "user2" "host2" + "method3" "user4" "host4" + "method4" "user4%domain4" "host4#1234"))))) (ert-deftest tramp-test02-file-name-dissect-simplified () "Check simplified file name components." @@ -1134,12 +1136,14 @@ handled properly. BODY shall not contain a timeout." (file-remote-p (concat "/%u@%h" + "|user2@host2" "|%u@%h" - "|user3@host3:/path/to/file")) - (format "/%s@%s|%s@%s|%s@%s:" - "user3" "host3" - "user3" "host3" - "user3" "host3")))) + "|user4%domain4@host4#1234:/path/to/file")) + (format "/%s@%s|%s@%s|%s@%s|%s@%s:" + "user2" "host2" + "user2" "host2" + "user4" "host4" + "user4%domain4" "host4#1234")))) ;; Exit. (tramp-change-syntax syntax)))) @@ -1780,12 +1784,14 @@ handled properly. BODY shall not contain a timeout." (file-remote-p (concat "/[method1/%u@%h" - "|method2/%u@%h" - "|method3/user3@host3]/path/to/file")) - (format "/[%s/%s@%s|%s/%s@%s|%s/%s@%s]" - "method1" "user3" "host3" - "method2" "user3" "host3" - "method3" "user3" "host3")))) + "|method2/user2@host2" + "|method3/%u@%h" + "|method4/user4%domain4@host4#1234]/path/to/file")) + (format "/[%s/%s@%s|%s/%s@%s|%s/%s@%s|%s/%s@%s]" + "method1" "user2" "host2" + "method2" "user2" "host2" + "method3" "user4" "host4" + "method4" "user4%domain4" "host4#1234")))) ;; Exit. (tramp-change-syntax syntax)))) -- 2.39.5