1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-25 10:47:00 +00:00
emacs/lisp/net/tramp-gw.el
Michael Albinus 2fe4b1254d Sync with Tramp 2.2.6-pre.
* net/tramp-cache.el (tramp-dump-connection-properties): Let-bind
  `print-length' and `print-level' to nil, in order to avoid
  truncation.  Reported by Christopher Schmidt
  <christopher@ristopher.com>.

* net/tramp-cmds.el (tramp-cleanup-connection): Delete also
process.

* net/tramp-compat.el (tramp-compat-condition-case-unless-debug):
New defmacro.
(tramp-compat-copy-directory): Add optional argument
COPY-CONTENTS.  It is not handled yet.

* net/tramp-ftp.el (tramp-disable-ange-ftp): Fix docstring.
(tramp-ftp-file-name-p): Simplify.

* net/tramp-gvfs.el (tramp-gvfs-handle-expand-file-name):
* net/tramp-gw.el (tramp-gw-open-connection): Add hop to
connection vector.

* net/tramp-sh.el (tramp-copy-size-limit): Fix docstring.
(tramp-methods): Do not use `tramp-password-end-of-line'.
(tramp-completion-function-alist-putty): Handle
UNIX case.
(tramp-remote-path): Add "/opt/bin", "/opt/sbin"
and "/opt/local/bin".
(tramp-do-file-attributes-with-stat)
(tramp-do-directory-files-and-attributes-with-stat)
Return uid and gid as real numbers.  They could run out of
integer range on cygwin.
(tramp-do-copy-or-rename-file-out-of-band): Better
trace format.
(tramp-sh-handle-expand-file-name): Handle hops.
(tramp-open-connection-setup-interactive-shell):
Use `tramp-cleanup'.  Move check for busyboxes ...
(tramp-find-shell): ... here.  Simplify
implementation.  Set "remote-shell" property also for alternative
shells.
(tramp-remote-coding-commands): Check "test -c
/dev/stdout".  If failing, a regular file would be written
otherwise.  Reported by
Dmitry Kurochkin <dmitry.kurochkin@gmail.com>.
(tramp-find-inline-encoding): Cache the coding
commands in the process cache.  Apply test command on the remote
side, if defined.
(tramp-find-inline-compress): Cache the compress
commands in the process cache.
(tramp-compute-multi-hops): Save
`tramp-default-proxies-alist'
when requested.  Handle hops.
(tramp-current-connection): New defvar.
(tramp-maybe-open-connection): Use
`tramp-cleanup'.  Throw
`suppress', if there was a failed connection
shortly before.  Handle user interrupt.  (Bug#10187)
(tramp-get-inline-compress,
tramp-get-inline-coding): Read
connection properties from the process cache.

* net/tramp-smb.el (tramp-smb-server-version)
(tramp-smb-wrong-passwd-regexp,
tramp-smb-actions-with-tar): New defconsts.
(tramp-smb-prompt): Extend for powershell prompt.
(tramp-smb-file-name-handler-alist): Add handlers for
`process-file', `shell-command' and
`start-file-process'.
(tramp-smb-winexe-program, tramp-smb-winexe-shell-command)
(tramp-smb-winexe-shell-command-switch): New
defcustoms.
(tramp-smb-file-name-p): Simplify.
(tramp-smb-action-with-tar,
tramp-smb-handle-process-file)
(tramp-smb-kill-winexe-function, tramp-smb-call-winexe)
(tramp-smb-shell-quote-argument): New defuns.
(tramp-smb-handle-copy-directory): Add
COPY-CONTENTS argument.
Implement using "tar".  By this, time-stamps are
preserved.
(tramp-smb-handle-copy-file): Handle also the case
of directories.
(tramp-smb-do-file-attributes-with-stat)
(tramp-smb-get-file-entries,
tramp-smb-get-cifs-capabilities): Use
`tramp-get-connection-buffer').
(tramp-smb-handle-rename-file): Use "rename", when source and
target are on the same share.
(tramp-smb-maybe-open-connection): Handle wrong passwords.  Use
`tramp-smb-server-version'.
(tramp-smb-wait-for-output): Remove prompt.

* net/tramp.el (top): Require 'cl.
(tramp-methods, tramp-rsh-end-of-line): Remove
`tramp-password-end-of-line' from docstring.
(tramp-save-ad-hoc-proxies): New defcustom.
(tramp-completion-function-alist): Adapt docstring.
(tramp-default-password-end-of-line): Remove defcustom.
(tramp-shell-prompt-pattern): Allow "[]" style
prompts.  (Bug#11065)
(tramp-user-regexp, tramp-file-name-regexp-unified)
(tramp-file-name-regexp-url): Extend regexp by hop
separator.
(tramp-postfix-hop-format,
tramp-postfix-hop-regexp) 
(tramp-remote-file-name-spec-regexp): New defconst.
(tramp-file-name-structure): Extend structure for
hops.
(tramp-get-method-parameter): Move up.
(tramp-file-name-p, tramp-dissect-file-name)
(with-parsed-tramp-file-name): Handle hops.
(tramp-file-name-hop): New defun.
(tramp-make-tramp-file-name): New optional arg HOP.
(tramp-message-show-progress-reporter-message):
New defvar.
(tramp-with-progress-reporter): Use it.  We cannot use
`tramp-message-show-message' here, because this
suppresses also error buffers.
(tramp-error-with-buffer): Suppress buffer view, if
`tramp-message-show-message' is nil.  Use
`tramp-get-connection-buffer'.
(tramp-cleanup): New defun.
(tramp-rfn-eshadow-update-overlay): Let-bind
`non-essential' to `t'.
(tramp-file-name-handler): If `debug-on-error' is
set, propagate an error unchanged.
(tramp-completion-handle-file-name-all-completions):
Handle hops.  Fix an error when called from ido.
(tramp-completion-dissect-file-name): Use better
local variable name.  Add hop to the vector.
(tramp-handle-insert-file-contents): Use
progress-reporter for the whole scenario.
(tramp-action-password): Let-bind
`enable-recursive-minibuffers' to `t'.
(tramp-check-for-regexp): Simplify search.
(tramp-enter-password): Remove it.  Move
implementation ...
(tramp-action-password): ... here.
(tramp-mode-string-to-int, tramp-local-host-p)
(tramp-make-tramp-temp-file, tramp-read-passwd)
(tramp-clear-passwd, tramp-time-less-p,
tramp-time-diff): Set tramp-autoload cookie.

* net/trampver.el: Update release number.

* net/tramp.el (tramp-set-completion-function): Fix
docstring.
(tramp-parse-group, tramp-parse-file)
(tramp-parse-shostkeys-sknownhosts): New defuns.
(tramp-parse-rhosts, tramp-parse-rhosts-group, tramp-parse-shosts)
(tramp-parse-shosts-group, tramp-parse-sconfig)
(tramp-parse-sconfig-group, tramp-parse-shostkeys)
(tramp-parse-sknownhosts, tramp-parse-hosts)
(tramp-parse-hosts-group, tramp-parse-passwd,
tramp-parse-netrc): Use them.
(tramp-parse-passwd-group, tramp-parse-netrc-group)
(tramp-parse-putty-group): Don't narrow.
(tramp-parse-putty): Make a loop.
(tramp-file-name-handler): Catch the `suppress'
signal.
2012-06-11 12:30:07 +02:00

334 lines
12 KiB
EmacsLisp

;;; tramp-gw.el --- Tramp utility functions for HTTP tunnels and SOCKS gateways
;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
;; Author: Michael Albinus <michael.albinus@gmx.de>
;; Keywords: comm, processes
;; Package: tramp
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Access functions for HTTP tunnels and SOCKS gateways from Tramp.
;; SOCKS functionality is implemented by socks.el from the w3 package.
;; HTTP tunnels are partly implemented in socks.el and url-http.el;
;; both implementations are not complete. Therefore, it is
;; implemented in this package.
;;; Code:
(require 'tramp)
;; Pacify byte-compiler
(eval-when-compile
(require 'cl)
(require 'custom))
;; Avoid byte-compiler warnings if the byte-compiler supports this.
;; Currently, XEmacs supports this.
(eval-when-compile
(when (featurep 'xemacs)
(byte-compiler-options (warnings (- unused-vars)))))
;; We don't add the following methods to `tramp-methods', in order to
;; exclude them from file name completion.
;; Define HTTP tunnel method ...
;;;###tramp-autoload
(defconst tramp-gw-tunnel-method "tunnel"
"Method to connect HTTP gateways.")
;; ... and port.
(defconst tramp-gw-default-tunnel-port 8080
"Default port for HTTP gateways.")
;; Define SOCKS method ...
;;;###tramp-autoload
(defconst tramp-gw-socks-method "socks"
"Method to connect SOCKS servers.")
;; ... and port.
(defconst tramp-gw-default-socks-port 1080
"Default port for SOCKS servers.")
;; Autoload the socks library. It is used only when we access a SOCKS server.
(autoload 'socks-open-network-stream "socks")
(defvar socks-username (user-login-name))
(defvar socks-server
(list "Default server" "socks" tramp-gw-default-socks-port 5))
;; Add a default for `tramp-default-user-alist'. Default is the local user.
;;;###tramp-autoload
(add-to-list
'tramp-default-user-alist
(list (concat "\\`"
(regexp-opt (list tramp-gw-tunnel-method tramp-gw-socks-method))
"\\'")
nil (user-login-name)))
;; Internal file name functions and variables.
(defvar tramp-gw-vector nil
"Keeps the remote host identification. Needed for Tramp messages.")
(defvar tramp-gw-gw-vector nil
"Current gateway identification vector.")
(defvar tramp-gw-gw-proc nil
"Current gateway process.")
;; This variable keeps the listening process, in order to reuse it for
;; new processes.
(defvar tramp-gw-aux-proc nil
"Process listening on local port, as mediation between SSH and the gateway.")
(defun tramp-gw-gw-proc-sentinel (proc event)
"Delete auxiliary process when we are deleted."
(unless (memq (process-status proc) '(run open))
(tramp-message
tramp-gw-vector 4 "Deleting auxiliary process `%s'" tramp-gw-gw-proc)
(let* (tramp-verbose
(p (tramp-get-connection-property proc "process" nil)))
(when (processp p) (delete-process p)))))
(defun tramp-gw-aux-proc-sentinel (proc event)
"Activate the different filters for involved gateway and auxiliary processes."
(when (memq (process-status proc) '(run open))
;; A new process has been spawned from `tramp-gw-aux-proc'.
(tramp-message
tramp-gw-vector 4
"Opening auxiliary process `%s', speaking with process `%s'"
proc tramp-gw-gw-proc)
(tramp-compat-set-process-query-on-exit-flag proc nil)
;; We don't want debug messages, because the corresponding debug
;; buffer might be undecided.
(let (tramp-verbose)
(tramp-set-connection-property tramp-gw-gw-proc "process" proc)
(tramp-set-connection-property proc "process" tramp-gw-gw-proc))
;; Set the process-filter functions for both processes.
(set-process-filter proc 'tramp-gw-process-filter)
(set-process-filter tramp-gw-gw-proc 'tramp-gw-process-filter)
;; There might be already some output from the gateway process.
(with-current-buffer (process-buffer tramp-gw-gw-proc)
(unless (= (point-min) (point-max))
(let ((s (buffer-string)))
(delete-region (point) (point-max))
(tramp-gw-process-filter tramp-gw-gw-proc s))))))
(defun tramp-gw-process-filter (proc string)
(let (tramp-verbose)
(process-send-string
(tramp-get-connection-property proc "process" nil) string)))
;;;###tramp-autoload
(defun tramp-gw-open-connection (vec gw-vec target-vec)
"Open a remote connection to VEC (see `tramp-file-name' structure).
Take GW-VEC as SOCKS or HTTP gateway, i.e. its method must be a
gateway method. TARGET-VEC identifies where to connect to via
the gateway, it can be different from VEC when there are more
hops to be applied.
It returns a string like \"localhost#port\", which must be used
instead of the host name declared in TARGET-VEC."
;; Remember vectors for property retrieval.
(setq tramp-gw-vector vec
tramp-gw-gw-vector gw-vec)
;; Start listening auxiliary process.
(unless (and (processp tramp-gw-aux-proc)
(memq (process-status tramp-gw-aux-proc) '(listen)))
(let ((aux-vec
(vector "aux" (tramp-file-name-user gw-vec)
(tramp-file-name-host gw-vec) nil nil)))
(setq tramp-gw-aux-proc
(make-network-process
:name (tramp-buffer-name aux-vec) :buffer nil :host 'local
:server t :noquery t :service t :coding 'binary))
(set-process-sentinel tramp-gw-aux-proc 'tramp-gw-aux-proc-sentinel)
(tramp-compat-set-process-query-on-exit-flag tramp-gw-aux-proc nil)
(tramp-message
vec 4 "Opening auxiliary process `%s', listening on port %d"
tramp-gw-aux-proc (process-contact tramp-gw-aux-proc :service))))
(let* ((gw-method
(intern
(tramp-find-method
(tramp-file-name-method gw-vec)
(tramp-file-name-user gw-vec)
(tramp-file-name-host gw-vec))))
(socks-username
(tramp-find-user
(tramp-file-name-method gw-vec)
(tramp-file-name-user gw-vec)
(tramp-file-name-host gw-vec)))
;; Declare the SOCKS server to be used.
(socks-server
(list "Tramp temporary socks server list"
;; Host name.
(tramp-file-name-real-host gw-vec)
;; Port number.
(or (tramp-file-name-port gw-vec)
(case gw-method
(tunnel tramp-gw-default-tunnel-port)
(socks tramp-gw-default-socks-port)))
;; Type. We support only http and socks5, NO socks4.
;; 'http could be used when HTTP tunnel works in socks.el.
5))
;; The function to be called.
(socks-function
(case gw-method
(tunnel 'tramp-gw-open-network-stream)
(socks 'socks-open-network-stream)))
socks-noproxy)
;; Open SOCKS process.
(setq tramp-gw-gw-proc
(funcall
socks-function
(tramp-get-connection-name gw-vec)
(tramp-get-connection-buffer gw-vec)
(tramp-file-name-real-host target-vec)
(tramp-file-name-port target-vec)))
(set-process-sentinel tramp-gw-gw-proc 'tramp-gw-gw-proc-sentinel)
(tramp-compat-set-process-query-on-exit-flag tramp-gw-gw-proc nil)
(tramp-message
vec 4 "Opened %s process `%s'"
(case gw-method ('tunnel "HTTP tunnel") ('socks "SOCKS"))
tramp-gw-gw-proc)
;; Return the new host for gateway access.
(format "localhost#%d" (process-contact tramp-gw-aux-proc :service))))
(defun tramp-gw-open-network-stream (name buffer host service)
"Open stream to proxy server HOST:SERVICE.
Resulting process has name NAME and buffer BUFFER. If
authentication is requested from proxy server, provide it."
(let ((command (format (concat
"CONNECT %s:%d HTTP/1.1\r\n"
"Host: %s:%d\r\n"
"Connection: keep-alive\r\n"
"User-Agent: Tramp/%s\r\n")
host service host service tramp-version))
(authentication "")
(first t)
found proc)
(while (not found)
;; Clean up.
(when (processp proc) (delete-process proc))
(with-current-buffer buffer (erase-buffer))
;; Open network stream.
(setq proc (open-network-stream
name buffer (nth 1 socks-server) (nth 2 socks-server)))
(set-process-coding-system proc 'binary 'binary)
(tramp-compat-set-process-query-on-exit-flag proc nil)
;; Send CONNECT command.
(process-send-string proc (format "%s%s\r\n" command authentication))
(tramp-message
tramp-gw-vector 6 "\n%s"
(format
"%s%s\r\n" command
(replace-regexp-in-string ;; no password in trace!
"Basic [^\r\n]+" "Basic xxxxx" authentication t)))
(with-current-buffer buffer
;; Trap errors to be traced in the right trace buffer. Often,
;; proxies have a timeout of 60". We wait 65" in order to
;; receive an answer this case.
(ignore-errors
(let (tramp-verbose)
(tramp-wait-for-regexp proc 65 "\r?\n\r?\n")))
;; Check return code.
(goto-char (point-min))
(narrow-to-region
(point-min)
(or (search-forward-regexp "\r?\n\r?\n" nil t) (point-max)))
(tramp-message tramp-gw-vector 6 "\n%s" (buffer-string))
(goto-char (point-min))
(search-forward-regexp "^HTTP/[1-9]\\.[0-9]" nil t)
(case (condition-case nil (read (current-buffer)) (error))
;; Connected.
(200 (setq found t))
;; We need basic authentication.
(401 (setq authentication (tramp-gw-basic-authentication nil first)))
;; Target host not found.
(404 (tramp-error-with-buffer
(current-buffer) tramp-gw-vector 'file-error
"Host %s not found." host))
;; We need basic proxy authentication.
(407 (setq authentication (tramp-gw-basic-authentication t first)))
;; Connection failed.
(503 (tramp-error-with-buffer
(current-buffer) tramp-gw-vector 'file-error
"Connection to %s:%d failed." host service))
;; That doesn't work at all.
(t (tramp-error-with-buffer
(current-buffer) tramp-gw-vector 'file-error
"Access to HTTP server %s:%d failed."
(nth 1 socks-server) (nth 2 socks-server))))
;; Remove HTTP headers.
(delete-region (point-min) (point-max))
(widen)
(setq first nil)))
;; Return the process.
proc))
(defun tramp-gw-basic-authentication (proxy pw-cache)
"Return authentication header for CONNECT, based on server request.
PROXY is an indication whether we need a Proxy-Authorization header
or an Authorization header. If PW-CACHE is non-nil, check for
password in password cache. This is done for the first try only."
;; `tramp-current-*' must be set for `tramp-read-passwd'.
(let ((tramp-current-method (tramp-file-name-method tramp-gw-gw-vector))
(tramp-current-user (tramp-file-name-user tramp-gw-gw-vector))
(tramp-current-host (tramp-file-name-host tramp-gw-gw-vector)))
(unless pw-cache (tramp-clear-passwd tramp-gw-gw-vector))
;; We are already in the right buffer.
(tramp-message
tramp-gw-vector 5 "%s required"
(if proxy "Proxy authentication" "Authentication"))
;; Search for request header. We accept only basic authentication.
(goto-char (point-min))
(search-forward-regexp
"^\\(Proxy\\|WWW\\)-Authenticate:\\s-*Basic\\s-+realm=")
;; Return authentication string.
(format
"%s: Basic %s\r\n"
(if proxy "Proxy-Authorization" "Authorization")
(base64-encode-string
(format
"%s:%s"
socks-username
(tramp-read-passwd
nil
(format
"Password for %s@[%s]: " socks-username (read (current-buffer)))))))))
(add-hook 'tramp-unload-hook
(lambda ()
(unload-feature 'tramp-gw 'force)))
(provide 'tramp-gw)
;;; TODO:
;; * Provide descriptive Commentary.
;; * Enable it for several gateway processes in parallel.
;;; tramp-gw.el ends here