1 ;;; url-gw.el --- Gateway munging for URL loading
3 ;; Copyright (C) 1997-1998, 2004-2013 Free Software Foundation, Inc.
5 ;; Author: Bill Perry <wmperry@gnu.org>
6 ;; Keywords: comm, data, processes
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Fixme: support SSH explicitly or via a url-gateway-rlogin-program?
29 (autoload 'socks-open-network-stream
"socks")
31 (defgroup url-gateway nil
32 "URL gateway variables."
35 (defcustom url-gateway-local-host-regexp nil
36 "A regular expression specifying local hostnames/machines."
37 :type
'(choice (const nil
) regexp
)
40 (defcustom url-gateway-prompt-pattern
41 "^[^#$%>;]*[#$%>;] *" ;; "bash\\|\$ *\r?$\\|> *\r?"
42 "A regular expression matching a shell prompt."
46 (defcustom url-gateway-rlogin-host nil
47 "What hostname to actually rlog into before doing a telnet."
48 :type
'(choice (const nil
) string
)
51 (defcustom url-gateway-rlogin-user-name nil
52 "Username to log into the remote machine with when using rlogin."
53 :type
'(choice (const nil
) string
)
56 (defcustom url-gateway-rlogin-parameters
'("telnet" "-8")
57 "Parameters to `url-open-rlogin'.
58 This list will be used as the parameter list given to rsh."
59 :type
'(repeat string
)
62 (defcustom url-gateway-telnet-host nil
63 "What hostname to actually login to before doing a telnet."
64 :type
'(choice (const nil
) string
)
67 (defcustom url-gateway-telnet-parameters
'("exec" "telnet" "-8")
68 "Parameters to `url-open-telnet'.
69 This list will be executed as a command after logging in via telnet."
70 :type
'(repeat string
)
73 (defcustom url-gateway-telnet-login-prompt
"^\r*.?login:"
74 "Prompt that tells us we should send our username when logging in w/telnet."
78 (defcustom url-gateway-telnet-password-prompt
"^\r*.?password:"
79 "Prompt that tells us we should send our password when logging in w/telnet."
83 (defcustom url-gateway-telnet-user-name nil
84 "User name to log in via telnet with."
85 :type
'(choice (const nil
) string
)
88 (defcustom url-gateway-telnet-password nil
89 "Password to use to log in via telnet with."
90 :type
'(choice (const nil
) string
)
93 (defcustom url-gateway-broken-resolution nil
94 "Whether to use nslookup to resolve hostnames.
95 This should be used when your version of Emacs cannot correctly use DNS,
96 but your machine can. This usually happens if you are running a statically
97 linked Emacs under SunOS 4.x."
101 (defcustom url-gateway-nslookup-program
"nslookup"
102 "If non-nil then a string naming nslookup program."
103 :type
'(choice (const :tag
"None" :value nil
) string
)
106 ;; Stolen from ange-ftp
108 (defun url-gateway-nslookup-host (host)
109 "Attempt to resolve the given HOST using nslookup if possible."
110 (interactive "sHost: ")
111 (if url-gateway-nslookup-program
112 (let ((proc (start-process " *nslookup*" " *nslookup*"
113 url-gateway-nslookup-program host
))
115 (set-process-query-on-exit-flag proc nil
)
116 (with-current-buffer (process-buffer proc
)
117 (while (memq (process-status proc
) '(run open
))
118 (accept-process-output proc
))
119 (goto-char (point-min))
120 (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t
)
121 (setq res
(buffer-substring (match-beginning 1)
123 (kill-buffer (current-buffer)))
127 ;; Stolen from red gnus nntp.el
128 (defun url-wait-for-string (regexp proc
)
129 "Wait until string matching REGEXP arrives in process PROC's buffer."
130 (let ((buf (current-buffer)))
131 (goto-char (point-min))
132 (while (not (re-search-forward regexp nil t
))
133 (accept-process-output proc
)
135 (goto-char (point-min)))))
137 ;; Stolen from red gnus nntp.el
138 (defun url-open-rlogin (name buffer host service
)
139 "Open a connection using rsh."
140 (if (not (stringp service
))
141 (setq service
(int-to-string service
)))
142 (let ((proc (if url-gateway-rlogin-user-name
145 url-gateway-rlogin-host
"-l" url-gateway-rlogin-user-name
147 (append url-gateway-rlogin-parameters
148 (list host service
)) " "))
150 name buffer
"rsh" url-gateway-rlogin-host
152 (append url-gateway-rlogin-parameters
156 (url-wait-for-string "^\r*200" proc
)
158 (delete-region (point-min) (point))
161 ;; Stolen from red gnus nntp.el
162 (defun url-open-telnet (name buffer host service
)
163 (if (not (stringp service
))
164 (setq service
(int-to-string service
)))
165 (with-current-buffer (get-buffer-create buffer
)
167 (let ((proc (start-process name buffer
"telnet" "-8"))
168 (case-fold-search t
))
169 (when (memq (process-status proc
) '(open run
))
170 (process-send-string proc
"set escape \^X\n")
171 (process-send-string proc
(concat
172 "open " url-gateway-telnet-host
"\n"))
173 (url-wait-for-string url-gateway-telnet-login-prompt proc
)
176 (or url-gateway-telnet-user-name
177 (setq url-gateway-telnet-user-name
(read-string "login: ")))
179 (url-wait-for-string url-gateway-telnet-password-prompt proc
)
182 (or url-gateway-telnet-password
183 (setq url-gateway-telnet-password
184 (read-passwd "Password: ")))
187 (url-wait-for-string url-gateway-prompt-pattern proc
)
189 proc
(concat (mapconcat 'identity
190 (append url-gateway-telnet-parameters
191 (list host service
)) " ") "\n"))
192 (url-wait-for-string "^\r*Escape character.*\r*\n+" proc
)
193 (delete-region (point-min) (match-end 0))
194 (process-send-string proc
"\^]\n")
195 (url-wait-for-string "^telnet" proc
)
196 (process-send-string proc
"mode character\n")
197 (accept-process-output proc
1)
199 (goto-char (point-min))
201 (delete-region (point) (point-max)))
205 (defun url-open-stream (name buffer host service
)
206 "Open a stream to HOST, possibly via a gateway.
207 Args per `open-network-stream'.
208 Will not make a connection if `url-gateway-unplugged' is non-nil.
209 Might do a non-blocking connection; use `process-status' to check."
210 (unless url-gateway-unplugged
211 (let ((gw-method (if (and url-gateway-local-host-regexp
212 (not (eq 'tls url-gateway-method
))
213 (not (eq 'ssl url-gateway-method
))
215 url-gateway-local-host-regexp
219 ;; An attempt to deal with denied connections, and attempt
226 ;; If the user told us to do DNS for them, do it.
227 (if url-gateway-broken-resolution
228 (setq host
(url-gateway-nslookup-host host
)))
230 (condition-case errobj
231 ;; This is a clean way to ensure the new process inherits the
232 ;; right coding systems in both Emacs and XEmacs.
233 (let ((coding-system-for-read 'binary
)
234 (coding-system-for-write 'binary
))
235 (setq conn
(pcase gw-method
236 ((or `tls
`ssl
`native
)
237 (if (eq gw-method
'native
)
238 (setq gw-method
'plain
))
240 name buffer host service
242 ;; Use non-blocking socket if we can.
243 :nowait
(featurep 'make-network-process
246 (socks-open-network-stream name buffer host service
))
248 (url-open-telnet name buffer host service
))
250 (url-open-rlogin name buffer host service
))
252 (error "Bad setting of url-gateway-method: %s"
253 url-gateway-method
))))))
258 ;;; url-gw.el ends here