Commit | Line | Data |
---|---|---|
8c8b8430 SM |
1 | ;;; url-gw.el --- Gateway munging for URL loading |
2 | ;; Author: Bill Perry <wmperry@gnu.org> | |
8c8b8430 SM |
3 | ;; Keywords: comm, data, processes |
4 | ||
5 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
6 | ;;; Copyright (c) 1997, 1998 Free Software Foundation, Inc. | |
7 | ;;; | |
8 | ;;; This file is part of GNU Emacs. | |
9 | ;;; | |
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 2, or (at your option) | |
13 | ;;; any later version. | |
14 | ;;; | |
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. | |
19 | ;;; | |
20 | ;;; You should have received a copy of the GNU General Public License | |
21 | ;;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;;; Boston, MA 02111-1307, USA. | |
24 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
25 | (eval-when-compile (require 'cl)) | |
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") | |
31 | (autoload 'open-ssl-stream "ssl") | |
32 | ||
33 | (defgroup url-gateway nil | |
34 | "URL gateway variables" | |
35 | :group 'url) | |
36 | ||
37 | (defcustom url-gateway-local-host-regexp nil | |
38 | "*A regular expression specifying local hostnames/machines." | |
39 | :type '(choice (const nil) regexp) | |
40 | :group 'url-gateway) | |
41 | ||
42 | (defcustom url-gateway-prompt-pattern | |
43 | "^[^#$%>;]*[#$%>;] *" ;; "bash\\|\$ *\r?$\\|> *\r?" | |
44 | "*A regular expression matching a shell prompt." | |
45 | :type 'regexp | |
46 | :group 'url-gateway) | |
47 | ||
48 | (defcustom url-gateway-rlogin-host nil | |
49 | "*What hostname to actually rlog into before doing a telnet." | |
50 | :type '(choice (const nil) string) | |
51 | :group 'url-gateway) | |
52 | ||
53 | (defcustom url-gateway-rlogin-user-name nil | |
54 | "*Username to log into the remote machine with when using rlogin." | |
55 | :type '(choice (const nil) string) | |
56 | :group 'url-gateway) | |
57 | ||
58 | (defcustom url-gateway-rlogin-parameters '("telnet" "-8") | |
59 | "*Parameters to `url-open-rlogin'. | |
60 | This list will be used as the parameter list given to rsh." | |
61 | :type '(repeat string) | |
62 | :group 'url-gateway) | |
63 | ||
64 | (defcustom url-gateway-telnet-host nil | |
65 | "*What hostname to actually login to before doing a telnet." | |
66 | :type '(choice (const nil) string) | |
67 | :group 'url-gateway) | |
68 | ||
69 | (defcustom url-gateway-telnet-parameters '("exec" "telnet" "-8") | |
70 | "*Parameters to `url-open-telnet'. | |
71 | This list will be executed as a command after logging in via telnet." | |
72 | :type '(repeat string) | |
73 | :group 'url-gateway) | |
74 | ||
75 | (defcustom url-gateway-telnet-login-prompt "^\r*.?login:" | |
76 | "*Prompt that tells us we should send our username when loggin in w/telnet." | |
77 | :type 'regexp | |
78 | :group 'url-gateway) | |
79 | ||
80 | (defcustom url-gateway-telnet-password-prompt "^\r*.?password:" | |
81 | "*Prompt that tells us we should send our password when loggin in w/telnet." | |
82 | :type 'regexp | |
83 | :group 'url-gateway) | |
84 | ||
85 | (defcustom url-gateway-telnet-user-name nil | |
86 | "User name to log in via telnet with." | |
87 | :type '(choice (const nil) string) | |
88 | :group 'url-gateway) | |
89 | ||
90 | (defcustom url-gateway-telnet-password nil | |
91 | "Password to use to log in via telnet with." | |
92 | :type '(choice (const nil) string) | |
93 | :group 'url-gateway) | |
94 | ||
95 | (defcustom url-gateway-broken-resolution nil | |
96 | "*Whether to use nslookup to resolve hostnames. | |
97 | This should be used when your version of Emacs cannot correctly use DNS, | |
98 | but your machine can. This usually happens if you are running a statically | |
99 | linked Emacs under SunOS 4.x" | |
100 | :type 'boolean | |
101 | :group 'url-gateway) | |
102 | ||
103 | (defcustom url-gateway-nslookup-program "nslookup" | |
104 | "*If non-NIL then a string naming nslookup program." | |
105 | :type '(choice (const :tag "None" :value nil) string) | |
106 | :group 'url-gateway) | |
107 | ||
108 | ;; Stolen from ange-ftp | |
109 | ;;;###autoload | |
110 | (defun url-gateway-nslookup-host (host) | |
111 | "Attempt to resolve the given HOST using nslookup if possible." | |
112 | (interactive "sHost: ") | |
113 | (if url-gateway-nslookup-program | |
114 | (let ((proc (start-process " *nslookup*" " *nslookup*" | |
115 | url-gateway-nslookup-program host)) | |
116 | (res host)) | |
117 | (process-kill-without-query proc) | |
118 | (save-excursion | |
119 | (set-buffer (process-buffer proc)) | |
120 | (while (memq (process-status proc) '(run open)) | |
121 | (accept-process-output proc)) | |
122 | (goto-char (point-min)) | |
123 | (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t) | |
124 | (setq res (buffer-substring (match-beginning 1) | |
125 | (match-end 1)))) | |
126 | (kill-buffer (current-buffer))) | |
127 | res) | |
128 | host)) | |
129 | ||
130 | ;; Stolen from red gnus nntp.el | |
131 | (defun url-wait-for-string (regexp proc) | |
132 | "Wait until string matching REGEXP arrives in process PROC's buffer." | |
133 | (let ((buf (current-buffer))) | |
134 | (goto-char (point-min)) | |
135 | (while (not (re-search-forward regexp nil t)) | |
136 | (accept-process-output proc) | |
137 | (set-buffer buf) | |
138 | (goto-char (point-min))))) | |
139 | ||
140 | ;; Stolen from red gnus nntp.el | |
141 | (defun url-open-rlogin (name buffer host service) | |
142 | "Open a connection using rsh." | |
143 | (if (not (stringp service)) | |
144 | (setq service (int-to-string service))) | |
145 | (let ((proc (if url-gateway-rlogin-user-name | |
146 | (start-process | |
147 | name buffer "rsh" | |
148 | url-gateway-rlogin-host "-l" url-gateway-rlogin-user-name | |
149 | (mapconcat 'identity | |
150 | (append url-gateway-rlogin-parameters | |
151 | (list host service)) " ")) | |
152 | (start-process | |
153 | name buffer "rsh" url-gateway-rlogin-host | |
154 | (mapconcat 'identity | |
155 | (append url-gateway-rlogin-parameters | |
156 | (list host service)) | |
157 | " "))))) | |
158 | (set-buffer buffer) | |
159 | (url-wait-for-string "^\r*200" proc) | |
160 | (beginning-of-line) | |
161 | (delete-region (point-min) (point)) | |
162 | proc)) | |
163 | ||
164 | ;; Stolen from red gnus nntp.el | |
165 | (defun url-open-telnet (name buffer host service) | |
166 | (if (not (stringp service)) | |
167 | (setq service (int-to-string service))) | |
168 | (save-excursion | |
169 | (set-buffer (get-buffer-create buffer)) | |
170 | (erase-buffer) | |
171 | (let ((proc (start-process name buffer "telnet" "-8")) | |
172 | (case-fold-search t)) | |
173 | (when (memq (process-status proc) '(open run)) | |
174 | (process-send-string proc "set escape \^X\n") | |
175 | (process-send-string proc (concat | |
176 | "open " url-gateway-telnet-host "\n")) | |
177 | (url-wait-for-string url-gateway-telnet-login-prompt proc) | |
178 | (process-send-string | |
179 | proc (concat | |
180 | (or url-gateway-telnet-user-name | |
181 | (setq url-gateway-telnet-user-name (read-string "login: "))) | |
182 | "\n")) | |
183 | (url-wait-for-string url-gateway-telnet-password-prompt proc) | |
184 | (process-send-string | |
185 | proc (concat | |
186 | (or url-gateway-telnet-password | |
187 | (setq url-gateway-telnet-password | |
188 | (funcall url-passwd-entry-func "Password: "))) | |
189 | "\n")) | |
190 | (erase-buffer) | |
191 | (url-wait-for-string url-gateway-prompt-pattern proc) | |
192 | (process-send-string | |
193 | proc (concat (mapconcat 'identity | |
194 | (append url-gateway-telnet-parameters | |
195 | (list host service)) " ") "\n")) | |
196 | (url-wait-for-string "^\r*Escape character.*\r*\n+" proc) | |
197 | (delete-region (point-min) (match-end 0)) | |
198 | (process-send-string proc "\^]\n") | |
199 | (url-wait-for-string "^telnet" proc) | |
200 | (process-send-string proc "mode character\n") | |
201 | (accept-process-output proc 1) | |
202 | (sit-for 1) | |
203 | (goto-char (point-min)) | |
204 | (forward-line 1) | |
205 | (delete-region (point) (point-max))) | |
206 | proc))) | |
207 | ||
208 | ;;;###autoload | |
209 | (defun url-open-stream (name buffer host service) | |
210 | "Open a stream to HOST, possibly via a gateway. | |
211 | Args per `open-network-stream'. | |
212 | Will not make a connexion if `url-gateway-unplugged' is non-nil." | |
213 | (unless url-gateway-unplugged | |
214 | (let ((gw-method (if (and url-gateway-local-host-regexp | |
215 | (not (eq 'ssl url-gateway-method)) | |
216 | (string-match | |
217 | url-gateway-local-host-regexp | |
218 | host)) | |
219 | 'native | |
220 | url-gateway-method)) | |
221 | ;;; ;; This hack is for OS/2 Emacs so that it will not do bogus CRLF | |
222 | ;;; ;; conversions while trying to be 'helpful' | |
223 | ;;; (tcp-binary-process-output-services (if (stringp service) | |
224 | ;;; (list service) | |
225 | ;;; (list service | |
226 | ;;; (int-to-string service)))) | |
227 | ||
228 | ;; An attempt to deal with denied connections, and attempt | |
229 | ;; to reconnect | |
230 | (cur-retries 0) | |
231 | (retry t) | |
232 | (errobj nil) | |
233 | (conn nil)) | |
234 | ||
235 | ;; If the user told us to do DNS for them, do it. | |
236 | (if url-gateway-broken-resolution | |
237 | (setq host (url-gateway-nslookup-host host))) | |
238 | ||
239 | (condition-case errobj | |
240 | ;; This is a clean way to ensure the new process inherits the | |
241 | ;; right coding systems in both Emacs and XEmacs. | |
242 | (let ((coding-system-for-read 'binary) | |
243 | (coding-system-for-write 'binary)) | |
244 | (setq conn (case gw-method | |
245 | (ssl | |
246 | (open-ssl-stream name buffer host service)) | |
247 | ((native) | |
248 | (open-network-stream name buffer host service)) | |
249 | (socks | |
250 | (socks-open-network-stream name buffer host service)) | |
251 | (telnet | |
252 | (url-open-telnet name buffer host service)) | |
253 | (rlogin | |
254 | (url-open-rlogin name buffer host service)) | |
255 | (otherwise | |
256 | (error "Bad setting of url-gateway-method: %s" | |
257 | url-gateway-method))))) | |
258 | (error | |
259 | (setq conn nil))) | |
260 | conn))) | |
261 | ||
262 | (provide 'url-gw) | |
e5566bd5 MB |
263 | |
264 | ;;; arch-tag: 1c4c0317-6d03-45b8-b3f3-838bd8f9d838 |