PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/net/tramp-gw.el

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