Commit | Line | Data |
---|---|---|
8c8b8430 | 1 | ;;; url-gw.el --- Gateway munging for URL loading |
00eef4de | 2 | |
ba318903 | 3 | ;; Copyright (C) 1997-1998, 2004-2014 Free Software Foundation, Inc. |
00eef4de | 4 | |
8c8b8430 | 5 | ;; Author: Bill Perry <wmperry@gnu.org> |
93a583ee | 6 | ;; Maintainer: emacs-devel@gnu.org |
8c8b8430 SM |
7 | ;; Keywords: comm, data, processes |
8 | ||
00eef4de LH |
9 | ;; This file is part of GNU Emacs. |
10 | ||
4936186e | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
00eef4de | 12 | ;; it under the terms of the GNU General Public License as published by |
4936186e GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
00eef4de LH |
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 | |
4936186e | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
00eef4de LH |
23 | |
24 | ;;; Code: | |
25 | ||
8c8b8430 SM |
26 | (require 'url-vars) |
27 | ||
28 | ;; Fixme: support SSH explicitly or via a url-gateway-rlogin-program? | |
29 | ||
30 | (autoload 'socks-open-network-stream "socks") | |
8c8b8430 SM |
31 | |
32 | (defgroup url-gateway nil | |
383168ec | 33 | "URL gateway variables." |
8c8b8430 SM |
34 | :group 'url) |
35 | ||
36 | (defcustom url-gateway-local-host-regexp nil | |
a5cda60e | 37 | "A regular expression specifying local hostnames/machines." |
8c8b8430 SM |
38 | :type '(choice (const nil) regexp) |
39 | :group 'url-gateway) | |
40 | ||
41 | (defcustom url-gateway-prompt-pattern | |
42 | "^[^#$%>;]*[#$%>;] *" ;; "bash\\|\$ *\r?$\\|> *\r?" | |
a5cda60e | 43 | "A regular expression matching a shell prompt." |
8c8b8430 SM |
44 | :type 'regexp |
45 | :group 'url-gateway) | |
46 | ||
47 | (defcustom url-gateway-rlogin-host nil | |
a5cda60e | 48 | "What hostname to actually rlog into before doing a telnet." |
8c8b8430 SM |
49 | :type '(choice (const nil) string) |
50 | :group 'url-gateway) | |
51 | ||
52 | (defcustom url-gateway-rlogin-user-name nil | |
a5cda60e | 53 | "Username to log into the remote machine with when using rlogin." |
8c8b8430 SM |
54 | :type '(choice (const nil) string) |
55 | :group 'url-gateway) | |
56 | ||
57 | (defcustom url-gateway-rlogin-parameters '("telnet" "-8") | |
a5cda60e | 58 | "Parameters to `url-open-rlogin'. |
8c8b8430 SM |
59 | This list will be used as the parameter list given to rsh." |
60 | :type '(repeat string) | |
61 | :group 'url-gateway) | |
62 | ||
63 | (defcustom url-gateway-telnet-host nil | |
a5cda60e | 64 | "What hostname to actually login to before doing a telnet." |
8c8b8430 SM |
65 | :type '(choice (const nil) string) |
66 | :group 'url-gateway) | |
67 | ||
68 | (defcustom url-gateway-telnet-parameters '("exec" "telnet" "-8") | |
a5cda60e | 69 | "Parameters to `url-open-telnet'. |
8c8b8430 SM |
70 | This list will be executed as a command after logging in via telnet." |
71 | :type '(repeat string) | |
72 | :group 'url-gateway) | |
73 | ||
74 | (defcustom url-gateway-telnet-login-prompt "^\r*.?login:" | |
c80e3b4a | 75 | "Prompt that tells us we should send our username when logging in w/telnet." |
8c8b8430 SM |
76 | :type 'regexp |
77 | :group 'url-gateway) | |
78 | ||
79 | (defcustom url-gateway-telnet-password-prompt "^\r*.?password:" | |
c80e3b4a | 80 | "Prompt that tells us we should send our password when logging in w/telnet." |
8c8b8430 SM |
81 | :type 'regexp |
82 | :group 'url-gateway) | |
83 | ||
84 | (defcustom url-gateway-telnet-user-name nil | |
85 | "User name to log in via telnet with." | |
86 | :type '(choice (const nil) string) | |
87 | :group 'url-gateway) | |
88 | ||
89 | (defcustom url-gateway-telnet-password nil | |
90 | "Password to use to log in via telnet with." | |
91 | :type '(choice (const nil) string) | |
92 | :group 'url-gateway) | |
93 | ||
94 | (defcustom url-gateway-broken-resolution nil | |
a5cda60e | 95 | "Whether to use nslookup to resolve hostnames. |
8c8b8430 SM |
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 | |
d1ce47b0 | 98 | linked Emacs under SunOS 4.x." |
8c8b8430 SM |
99 | :type 'boolean |
100 | :group 'url-gateway) | |
101 | ||
102 | (defcustom url-gateway-nslookup-program "nslookup" | |
a5cda60e | 103 | "If non-nil then a string naming nslookup program." |
8c8b8430 SM |
104 | :type '(choice (const :tag "None" :value nil) string) |
105 | :group 'url-gateway) | |
106 | ||
107 | ;; Stolen from ange-ftp | |
108 | ;;;###autoload | |
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)) | |
115 | (res host)) | |
ed87225a | 116 | (set-process-query-on-exit-flag proc nil) |
351b838f | 117 | (with-current-buffer (process-buffer proc) |
8c8b8430 SM |
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) | |
123 | (match-end 1)))) | |
124 | (kill-buffer (current-buffer))) | |
125 | res) | |
126 | host)) | |
127 | ||
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) | |
135 | (set-buffer buf) | |
136 | (goto-char (point-min))))) | |
137 | ||
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 | |
144 | (start-process | |
145 | name buffer "rsh" | |
146 | url-gateway-rlogin-host "-l" url-gateway-rlogin-user-name | |
147 | (mapconcat 'identity | |
148 | (append url-gateway-rlogin-parameters | |
149 | (list host service)) " ")) | |
150 | (start-process | |
151 | name buffer "rsh" url-gateway-rlogin-host | |
152 | (mapconcat 'identity | |
153 | (append url-gateway-rlogin-parameters | |
154 | (list host service)) | |
155 | " "))))) | |
156 | (set-buffer buffer) | |
157 | (url-wait-for-string "^\r*200" proc) | |
158 | (beginning-of-line) | |
159 | (delete-region (point-min) (point)) | |
160 | proc)) | |
161 | ||
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))) | |
351b838f | 166 | (with-current-buffer (get-buffer-create buffer) |
8c8b8430 SM |
167 | (erase-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) | |
175 | (process-send-string | |
176 | proc (concat | |
177 | (or url-gateway-telnet-user-name | |
178 | (setq url-gateway-telnet-user-name (read-string "login: "))) | |
179 | "\n")) | |
180 | (url-wait-for-string url-gateway-telnet-password-prompt proc) | |
181 | (process-send-string | |
182 | proc (concat | |
183 | (or url-gateway-telnet-password | |
184 | (setq url-gateway-telnet-password | |
af3e7f78 | 185 | (read-passwd "Password: "))) |
8c8b8430 SM |
186 | "\n")) |
187 | (erase-buffer) | |
188 | (url-wait-for-string url-gateway-prompt-pattern proc) | |
189 | (process-send-string | |
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) | |
199 | (sit-for 1) | |
200 | (goto-char (point-min)) | |
201 | (forward-line 1) | |
202 | (delete-region (point) (point-max))) | |
203 | proc))) | |
204 | ||
205 | ;;;###autoload | |
206 | (defun url-open-stream (name buffer host service) | |
207 | "Open a stream to HOST, possibly via a gateway. | |
208 | Args per `open-network-stream'. | |
5695d1dd CY |
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." | |
8c8b8430 SM |
211 | (unless url-gateway-unplugged |
212 | (let ((gw-method (if (and url-gateway-local-host-regexp | |
5bbb0eb9 | 213 | (not (eq 'tls url-gateway-method)) |
8c8b8430 SM |
214 | (not (eq 'ssl url-gateway-method)) |
215 | (string-match | |
216 | url-gateway-local-host-regexp | |
217 | host)) | |
218 | 'native | |
219 | url-gateway-method)) | |
8c8b8430 SM |
220 | ;; An attempt to deal with denied connections, and attempt |
221 | ;; to reconnect | |
222 | (cur-retries 0) | |
223 | (retry t) | |
224 | (errobj nil) | |
225 | (conn nil)) | |
226 | ||
227 | ;; If the user told us to do DNS for them, do it. | |
228 | (if url-gateway-broken-resolution | |
229 | (setq host (url-gateway-nslookup-host host))) | |
230 | ||
231 | (condition-case errobj | |
232 | ;; This is a clean way to ensure the new process inherits the | |
233 | ;; right coding systems in both Emacs and XEmacs. | |
234 | (let ((coding-system-for-read 'binary) | |
235 | (coding-system-for-write 'binary)) | |
a464a6c7 SM |
236 | (setq conn (pcase gw-method |
237 | ((or `tls `ssl `native) | |
da91b5f2 CY |
238 | (if (eq gw-method 'native) |
239 | (setq gw-method 'plain)) | |
240 | (open-network-stream | |
241 | name buffer host service | |
242 | :type gw-method | |
243 | ;; Use non-blocking socket if we can. | |
244 | :nowait (featurep 'make-network-process | |
245 | '(:nowait t)))) | |
a464a6c7 | 246 | (`socks |
8c8b8430 | 247 | (socks-open-network-stream name buffer host service)) |
a464a6c7 | 248 | (`telnet |
8c8b8430 | 249 | (url-open-telnet name buffer host service)) |
a464a6c7 | 250 | (`rlogin |
8c8b8430 | 251 | (url-open-rlogin name buffer host service)) |
a464a6c7 | 252 | (_ |
8c8b8430 | 253 | (error "Bad setting of url-gateway-method: %s" |
da91b5f2 | 254 | url-gateway-method)))))) |
8c8b8430 SM |
255 | conn))) |
256 | ||
257 | (provide 'url-gw) | |
e5566bd5 | 258 | |
00eef4de | 259 | ;;; url-gw.el ends here |