Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / net / tramp-gw.el
1 ;;; tramp-gw.el --- Tramp utility functions for HTTP tunnels and SOCKS gateways
2
3 ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Access functions for HTTP tunnels and SOCKS gateways from Tramp.
27 ;; SOCKS functionality is implemented by socks.el from the w3 package.
28 ;; HTTP tunnels are partly implemented in socks.el and url-http.el;
29 ;; both implementations are not complete. Therefore, it is
30 ;; implemented in this package.
31
32 ;;; Code:
33
34 (require 'tramp)
35
36 ;; Pacify byte-compiler
37 (eval-when-compile
38 (require 'cl)
39 (require 'custom))
40
41 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
42 ;; Currently, XEmacs supports this.
43 (eval-when-compile
44 (when (featurep 'xemacs)
45 (byte-compiler-options (warnings (- unused-vars)))))
46
47 ;; Define HTTP tunnel method ...
48 ;;;###tramp-autoload
49 (defconst tramp-gw-tunnel-method "tunnel"
50 "*Method to connect HTTP gateways.")
51
52 ;; ... and port.
53 (defconst tramp-gw-default-tunnel-port 8080
54 "*Default port for HTTP gateways.")
55
56 ;; Define SOCKS method ...
57 ;;;###tramp-autoload
58 (defconst tramp-gw-socks-method "socks"
59 "*Method to connect SOCKS servers.")
60
61 ;; ... and port.
62 (defconst tramp-gw-default-socks-port 1080
63 "*Default port for SOCKS servers.")
64
65 ;; Autoload the socks library. It is used only when we access a SOCKS server.
66 (autoload 'socks-open-network-stream "socks")
67 (defvar socks-username (user-login-name))
68 (defvar socks-server
69 (list "Default server" "socks" tramp-gw-default-socks-port 5))
70
71 ;; Add a default for `tramp-default-user-alist'. Default is the local user.
72 (add-to-list 'tramp-default-user-alist
73 `(,tramp-gw-tunnel-method nil ,(user-login-name)))
74 (add-to-list 'tramp-default-user-alist
75 `(,tramp-gw-socks-method nil ,(user-login-name)))
76
77 ;; Internal file name functions and variables.
78
79 (defvar tramp-gw-vector nil
80 "Keeps the remote host identification. Needed for Tramp messages.")
81
82 (defvar tramp-gw-gw-vector nil
83 "Current gateway identification vector.")
84
85 (defvar tramp-gw-gw-proc nil
86 "Current gateway process.")
87
88 ;; This variable keeps the listening process, in order to reuse it for
89 ;; new processes.
90 (defvar tramp-gw-aux-proc nil
91 "Process listening on local port, as mediation between SSH and the gateway.")
92
93 (defun tramp-gw-gw-proc-sentinel (proc event)
94 "Delete auxiliary process when we are deleted."
95 (unless (memq (process-status proc) '(run open))
96 (tramp-message
97 tramp-gw-vector 4 "Deleting auxiliary process `%s'" tramp-gw-gw-proc)
98 (let* (tramp-verbose
99 (p (tramp-get-connection-property proc "process" nil)))
100 (when (processp p) (delete-process p)))))
101
102 (defun tramp-gw-aux-proc-sentinel (proc event)
103 "Activate the different filters for involved gateway and auxiliary processes."
104 (when (memq (process-status proc) '(run open))
105 ;; A new process has been spawned from `tramp-gw-aux-proc'.
106 (tramp-message
107 tramp-gw-vector 4
108 "Opening auxiliary process `%s', speaking with process `%s'"
109 proc tramp-gw-gw-proc)
110 (tramp-compat-set-process-query-on-exit-flag proc nil)
111 ;; We don't want debug messages, because the corresponding debug
112 ;; buffer might be undecided.
113 (let (tramp-verbose)
114 (tramp-set-connection-property tramp-gw-gw-proc "process" proc)
115 (tramp-set-connection-property proc "process" tramp-gw-gw-proc))
116 ;; Set the process-filter functions for both processes.
117 (set-process-filter proc 'tramp-gw-process-filter)
118 (set-process-filter tramp-gw-gw-proc 'tramp-gw-process-filter)
119 ;; There might be already some output from the gateway process.
120 (with-current-buffer (process-buffer tramp-gw-gw-proc)
121 (unless (= (point-min) (point-max))
122 (let ((s (buffer-string)))
123 (delete-region (point) (point-max))
124 (tramp-gw-process-filter tramp-gw-gw-proc s))))))
125
126 (defun tramp-gw-process-filter (proc string)
127 (let (tramp-verbose)
128 (process-send-string
129 (tramp-get-connection-property proc "process" nil) string)))
130
131 ;;;###tramp-autoload
132 (defun tramp-gw-open-connection (vec gw-vec target-vec)
133 "Open a remote connection to VEC (see `tramp-file-name' structure).
134 Take GW-VEC as SOCKS or HTTP gateway, i.e. its method must be a
135 gateway method. TARGET-VEC identifies where to connect to via
136 the gateway, it can be different from VEC when there are more
137 hops to be applied.
138
139 It returns a string like \"localhost#port\", which must be used
140 instead of the host name declared in TARGET-VEC."
141
142 ;; Remember vectors for property retrieval.
143 (setq tramp-gw-vector vec
144 tramp-gw-gw-vector gw-vec)
145
146 ;; Start listening auxiliary process.
147 (unless (and (processp tramp-gw-aux-proc)
148 (memq (process-status tramp-gw-aux-proc) '(listen)))
149 (let ((aux-vec
150 (vector "aux" (tramp-file-name-user gw-vec)
151 (tramp-file-name-host gw-vec) nil)))
152 (setq tramp-gw-aux-proc
153 (make-network-process
154 :name (tramp-buffer-name aux-vec) :buffer nil :host 'local
155 :server t :noquery t :service t :coding 'binary))
156 (set-process-sentinel tramp-gw-aux-proc 'tramp-gw-aux-proc-sentinel)
157 (tramp-compat-set-process-query-on-exit-flag tramp-gw-aux-proc nil)
158 (tramp-message
159 vec 4 "Opening auxiliary process `%s', listening on port %d"
160 tramp-gw-aux-proc (process-contact tramp-gw-aux-proc :service))))
161
162 (let* ((gw-method
163 (intern
164 (tramp-find-method
165 (tramp-file-name-method gw-vec)
166 (tramp-file-name-user gw-vec)
167 (tramp-file-name-host gw-vec))))
168 (socks-username
169 (tramp-find-user
170 (tramp-file-name-method gw-vec)
171 (tramp-file-name-user gw-vec)
172 (tramp-file-name-host gw-vec)))
173 ;; Declare the SOCKS server to be used.
174 (socks-server
175 (list "Tramp tempory socks server list"
176 ;; Host name.
177 (tramp-file-name-real-host gw-vec)
178 ;; Port number.
179 (or (tramp-file-name-port gw-vec)
180 (case gw-method
181 (tunnel tramp-gw-default-tunnel-port)
182 (socks tramp-gw-default-socks-port)))
183 ;; Type. We support only http and socks5, NO socks4.
184 ;; 'http could be used when HTTP tunnel works in socks.el.
185 5))
186 ;; The function to be called.
187 (socks-function
188 (case gw-method
189 (tunnel 'tramp-gw-open-network-stream)
190 (socks 'socks-open-network-stream)))
191 socks-noproxy)
192
193 ;; Open SOCKS process.
194 (setq tramp-gw-gw-proc
195 (funcall
196 socks-function
197 (tramp-buffer-name gw-vec)
198 (tramp-get-buffer gw-vec)
199 (tramp-file-name-real-host target-vec)
200 (tramp-file-name-port target-vec)))
201 (set-process-sentinel tramp-gw-gw-proc 'tramp-gw-gw-proc-sentinel)
202 (tramp-compat-set-process-query-on-exit-flag tramp-gw-gw-proc nil)
203 (tramp-message
204 vec 4 "Opened %s process `%s'"
205 (case gw-method ('tunnel "HTTP tunnel") ('socks "SOCKS"))
206 tramp-gw-gw-proc)
207
208 ;; Return the new host for gateway access.
209 (format "localhost#%d" (process-contact tramp-gw-aux-proc :service))))
210
211 (defun tramp-gw-open-network-stream (name buffer host service)
212 "Open stream to proxy server HOST:SERVICE.
213 Resulting process has name NAME and buffer BUFFER. If
214 authentication is requested from proxy server, provide it."
215 (let ((command (format (concat
216 "CONNECT %s:%d HTTP/1.1\r\n"
217 "Host: %s:%d\r\n"
218 "Connection: keep-alive\r\n"
219 "User-Agent: Tramp/%s\r\n")
220 host service host service tramp-version))
221 (authentication "")
222 (first t)
223 found proc)
224
225 (while (not found)
226 ;; Clean up.
227 (when (processp proc) (delete-process proc))
228 (with-current-buffer buffer (erase-buffer))
229 ;; Open network stream.
230 (setq proc (open-network-stream
231 name buffer (nth 1 socks-server) (nth 2 socks-server)))
232 (set-process-coding-system proc 'binary 'binary)
233 (tramp-compat-set-process-query-on-exit-flag proc nil)
234 ;; Send CONNECT command.
235 (process-send-string proc (format "%s%s\r\n" command authentication))
236 (tramp-message
237 tramp-gw-vector 6 "\n%s"
238 (format
239 "%s%s\r\n" command
240 (replace-regexp-in-string ;; no password in trace!
241 "Basic [^\r\n]+" "Basic xxxxx" authentication t)))
242 (with-current-buffer buffer
243 ;; Trap errors to be traced in the right trace buffer. Often,
244 ;; proxies have a timeout of 60". We wait 65" in order to
245 ;; receive an answer this case.
246 (ignore-errors
247 (let (tramp-verbose)
248 (tramp-wait-for-regexp proc 65 "\r?\n\r?\n")))
249 ;; Check return code.
250 (goto-char (point-min))
251 (narrow-to-region
252 (point-min)
253 (or (search-forward-regexp "\r?\n\r?\n" nil t) (point-max)))
254 (tramp-message tramp-gw-vector 6 "\n%s" (buffer-string))
255 (goto-char (point-min))
256 (search-forward-regexp "^HTTP/[1-9]\\.[0-9]" nil t)
257 (case (condition-case nil (read (current-buffer)) (error))
258 ;; Connected.
259 (200 (setq found t))
260 ;; We need basic authentication.
261 (401 (setq authentication (tramp-gw-basic-authentication nil first)))
262 ;; Target host not found.
263 (404 (tramp-error-with-buffer
264 (current-buffer) tramp-gw-vector 'file-error
265 "Host %s not found." host))
266 ;; We need basic proxy authentication.
267 (407 (setq authentication (tramp-gw-basic-authentication t first)))
268 ;; Connection failed.
269 (503 (tramp-error-with-buffer
270 (current-buffer) tramp-gw-vector 'file-error
271 "Connection to %s:%d failed." host service))
272 ;; That doesn't work at all.
273 (t (tramp-error-with-buffer
274 (current-buffer) tramp-gw-vector 'file-error
275 "Access to HTTP server %s:%d failed."
276 (nth 1 socks-server) (nth 2 socks-server))))
277 ;; Remove HTTP headers.
278 (delete-region (point-min) (point-max))
279 (widen)
280 (setq first nil)))
281 ;; Return the process.
282 proc))
283
284 (defun tramp-gw-basic-authentication (proxy pw-cache)
285 "Return authentication header for CONNECT, based on server request.
286 PROXY is an indication whether we need a Proxy-Authorization header
287 or an Authorization header. If PW-CACHE is non-nil, check for
288 password in password cache. This is done for the first try only."
289
290 ;; `tramp-current-*' must be set for `tramp-read-passwd'.
291 (let ((tramp-current-method (tramp-file-name-method tramp-gw-gw-vector))
292 (tramp-current-user (tramp-file-name-user tramp-gw-gw-vector))
293 (tramp-current-host (tramp-file-name-host tramp-gw-gw-vector)))
294 (unless pw-cache (tramp-clear-passwd tramp-gw-gw-vector))
295 ;; We are already in the right buffer.
296 (tramp-message
297 tramp-gw-vector 5 "%s required"
298 (if proxy "Proxy authentication" "Authentication"))
299 ;; Search for request header. We accept only basic authentication.
300 (goto-char (point-min))
301 (search-forward-regexp
302 "^\\(Proxy\\|WWW\\)-Authenticate:\\s-*Basic\\s-+realm=")
303 ;; Return authentication string.
304 (format
305 "%s: Basic %s\r\n"
306 (if proxy "Proxy-Authorization" "Authorization")
307 (base64-encode-string
308 (format
309 "%s:%s"
310 socks-username
311 (tramp-read-passwd
312 nil
313 (format
314 "Password for %s@[%s]: " socks-username (read (current-buffer)))))))))
315
316 (add-hook 'tramp-unload-hook
317 (lambda ()
318 (unload-feature 'tramp-gw 'force)))
319
320 (provide 'tramp-gw)
321
322 ;;; TODO:
323
324 ;; * Provide descriptive Commentary.
325 ;; * Enable it for several gateway processes in parallel.
326
327 ;; arch-tag: 277e3a81-fdee-40cf-9e6b-59626292a5e0
328 ;;; tramp-gw.el ends here