1 ;;; url-gw.el --- Gateway munging for URL loading
3 ;; Copyright (C) 1997-1998, 2004-2016 Free Software Foundation, Inc.
5 ;; Author: Bill Perry <wmperry@gnu.org>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: comm, data, processes
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
28 ;; Fixme: support SSH explicitly or via a url-gateway-rlogin-program?
30 (autoload 'socks-open-network-stream
"socks")
32 (defgroup url-gateway nil
33 "URL gateway variables."
36 (defcustom url-gateway-local-host-regexp nil
37 "A regular expression specifying local hostnames/machines."
38 :type
'(choice (const nil
) regexp
)
41 (defcustom url-gateway-prompt-pattern
42 "^[^#$%>;]*[#$%>;] *" ;; "bash\\|[$>] *\r?$"
43 "A regular expression matching a shell prompt."
47 (defcustom url-gateway-rlogin-host nil
48 "What hostname to actually rlog into before doing a telnet."
49 :type
'(choice (const nil
) string
)
52 (defcustom url-gateway-rlogin-user-name nil
53 "Username to log into the remote machine with when using rlogin."
54 :type
'(choice (const nil
) string
)
57 (defcustom url-gateway-rlogin-parameters
'("telnet" "-8")
58 "Parameters to `url-open-rlogin'.
59 This list will be used as the parameter list given to rsh."
60 :type
'(repeat string
)
63 (defcustom url-gateway-telnet-host nil
64 "What hostname to actually login to before doing a telnet."
65 :type
'(choice (const nil
) string
)
68 (defcustom url-gateway-telnet-parameters
'("exec" "telnet" "-8")
69 "Parameters to `url-open-telnet'.
70 This list will be executed as a command after logging in via telnet."
71 :type
'(repeat string
)
74 (defcustom url-gateway-telnet-login-prompt
"^\r*.?login:"
75 "Prompt that tells us we should send our username when logging in w/telnet."
79 (defcustom url-gateway-telnet-password-prompt
"^\r*.?password:"
80 "Prompt that tells us we should send our password when logging in w/telnet."
84 (defcustom url-gateway-telnet-user-name nil
85 "User name to log in via telnet with."
86 :type
'(choice (const nil
) string
)
89 (defcustom url-gateway-telnet-password nil
90 "Password to use to log in via telnet with."
91 :type
'(choice (const nil
) string
)
94 (defcustom url-gateway-broken-resolution nil
95 "Whether to use nslookup to resolve hostnames.
96 This should be used when your version of Emacs cannot correctly use DNS,
97 but your machine can. This usually happens if you are running a statically
98 linked Emacs under SunOS 4.x."
102 (defcustom url-gateway-nslookup-program
"nslookup"
103 "If non-nil then a string naming nslookup program."
104 :type
'(choice (const :tag
"None" :value nil
) string
)
107 ;; Stolen from ange-ftp
109 (defun url-gateway-nslookup-host (host)
110 "Attempt to resolve the given HOST using nslookup if possible."
111 (interactive "sHost: ")
112 (if url-gateway-nslookup-program
113 (let ((proc (start-process " *nslookup*" " *nslookup*"
114 url-gateway-nslookup-program host
))
116 (set-process-query-on-exit-flag proc nil
)
117 (with-current-buffer (process-buffer proc
)
118 (while (memq (process-status proc
) '(run open
))
119 (accept-process-output proc
))
120 (goto-char (point-min))
121 (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t
)
122 (setq res
(buffer-substring (match-beginning 1)
124 (kill-buffer (current-buffer)))
128 ;; Stolen from red gnus nntp.el
129 (defun url-wait-for-string (regexp proc
)
130 "Wait until string matching REGEXP arrives in process PROC's buffer."
131 (let ((buf (current-buffer)))
132 (goto-char (point-min))
133 (while (not (re-search-forward regexp nil t
))
134 (accept-process-output proc
)
136 (goto-char (point-min)))))
138 ;; Stolen from red gnus nntp.el
139 (defun url-open-rlogin (name buffer host service
)
140 "Open a connection using rsh."
141 (if (not (stringp service
))
142 (setq service
(int-to-string service
)))
143 (let ((proc (if url-gateway-rlogin-user-name
146 url-gateway-rlogin-host
"-l" url-gateway-rlogin-user-name
148 (append url-gateway-rlogin-parameters
149 (list host service
)) " "))
151 name buffer
"rsh" url-gateway-rlogin-host
153 (append url-gateway-rlogin-parameters
157 (url-wait-for-string "^\r*200" proc
)
159 (delete-region (point-min) (point))
162 ;; Stolen from red gnus nntp.el
163 (defun url-open-telnet (name buffer host service
)
164 (if (not (stringp service
))
165 (setq service
(int-to-string service
)))
166 (with-current-buffer (get-buffer-create buffer
)
168 (let ((proc (start-process name buffer
"telnet" "-8"))
169 (case-fold-search t
))
170 (when (memq (process-status proc
) '(open run
))
171 (process-send-string proc
"set escape \^X\n")
172 (process-send-string proc
(concat
173 "open " url-gateway-telnet-host
"\n"))
174 (url-wait-for-string url-gateway-telnet-login-prompt proc
)
177 (or url-gateway-telnet-user-name
178 (setq url-gateway-telnet-user-name
(read-string "login: ")))
180 (url-wait-for-string url-gateway-telnet-password-prompt proc
)
183 (or url-gateway-telnet-password
184 (setq url-gateway-telnet-password
185 (read-passwd "Password: ")))
188 (url-wait-for-string url-gateway-prompt-pattern proc
)
190 proc
(concat (mapconcat 'identity
191 (append url-gateway-telnet-parameters
192 (list host service
)) " ") "\n"))
193 (url-wait-for-string "^\r*Escape character.*\r*\n+" proc
)
194 (delete-region (point-min) (match-end 0))
195 (process-send-string proc
"\^]\n")
196 (url-wait-for-string "^telnet" proc
)
197 (process-send-string proc
"mode character\n")
198 (accept-process-output proc
1)
200 (goto-char (point-min))
202 (delete-region (point) (point-max)))
206 (defun url-open-stream (name buffer host service
&optional gateway-method
)
207 "Open a stream to HOST, possibly via a gateway.
208 Args per `open-network-stream'.
209 Will not make a connection if `url-gateway-unplugged' is non-nil.
210 Might do a non-blocking connection; use `process-status' to check.
212 Optional arg GATEWAY-METHOD specifies the gateway to be used,
213 overriding the value of `url-gateway-method'."
214 (unless url-gateway-unplugged
215 (let* ((gwm (or gateway-method url-gateway-method
))
216 (gw-method (if (and url-gateway-local-host-regexp
220 url-gateway-local-host-regexp
224 ;; An attempt to deal with denied connections, and attempt
231 ;; If the user told us to do DNS for them, do it.
232 (if url-gateway-broken-resolution
233 (setq host
(url-gateway-nslookup-host host
)))
235 (condition-case errobj
236 ;; This is a clean way to ensure the new process inherits the
237 ;; right coding systems in both Emacs and XEmacs.
238 (let ((coding-system-for-read 'binary
)
239 (coding-system-for-write 'binary
))
240 (setq conn
(pcase gw-method
241 ((or `tls
`ssl
`native
)
242 (if (eq gw-method
'native
)
243 (setq gw-method
'plain
))
245 name buffer host service
247 ;; Use non-blocking socket if we can.
248 :nowait
(featurep 'make-network-process
251 (socks-open-network-stream name buffer host service
))
253 (url-open-telnet name buffer host service
))
255 (url-open-rlogin name buffer host service
))
257 (error "Bad setting of url-gateway-method: %s"
258 url-gateway-method
))))))
263 ;;; url-gw.el ends here