]> code.delx.au - gnu-emacs/blob - lisp/net/network-stream.el
* lisp/net/tramp.el (tramp-get-debug-buffer): Ensure outline.el is not
[gnu-emacs] / lisp / net / network-stream.el
1 ;;; network-stream.el --- open network processes, possibly with encryption
2
3 ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: network
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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This library provides the function `open-network-stream', which provides a
26 ;; higher-level interface for opening TCP network processes than the built-in
27 ;; function `make-network-process'. In addition to plain connections, it
28 ;; supports TLS/SSL and STARTTLS connections.
29
30 ;; Usage example:
31
32 ;; (open-network-stream
33 ;; "*nnimap*" buffer address port
34 ;; :type 'network
35 ;; :capability-command "1 CAPABILITY\r\n"
36 ;; :success " OK "
37 ;; :starttls-function
38 ;; (lambda (capabilities)
39 ;; (if (not (string-match "STARTTLS" capabilities))
40 ;; nil
41 ;; "1 STARTTLS\r\n")))
42
43 ;;; Code:
44
45 (require 'tls)
46 (require 'starttls)
47 (require 'auth-source)
48
49 (autoload 'gnutls-negotiate "gnutls")
50 (autoload 'open-gnutls-stream "gnutls")
51
52 ;;;###autoload
53 (defun open-network-stream (name buffer host service &rest parameters)
54 "Open a TCP connection to HOST, optionally with encryption.
55 Normally, return a network process object; with a non-nil
56 :return-list parameter, return a list instead (see below).
57 Input and output work as for subprocesses; `delete-process'
58 closes it.
59
60 NAME is the name for the process. It is modified if necessary to
61 make it unique.
62 BUFFER is a buffer or buffer name to associate with the process.
63 Process output goes at end of that buffer. BUFFER may be nil,
64 meaning that the process is not associated with any buffer.
65 HOST is the name or IP address of the host to connect to.
66 SERVICE is the name of the service desired, or an integer specifying
67 a port number to connect to.
68
69 The remaining PARAMETERS should be a sequence of keywords and
70 values:
71
72 :type specifies the connection type, one of the following:
73 nil or `network'
74 -- Begin with an ordinary network connection, and if
75 the parameters :success and :capability-command
76 are also supplied, try to upgrade to an encrypted
77 connection via STARTTLS. Even if that
78 fails (e.g. if HOST does not support TLS), retain
79 an unencrypted connection.
80 `plain' -- An ordinary, unencrypted network connection.
81 `starttls' -- Begin with an ordinary connection, and try
82 upgrading via STARTTLS. If that fails for any
83 reason, drop the connection; in that case the
84 returned object is a killed process.
85 `tls' -- A TLS connection.
86 `ssl' -- Equivalent to `tls'.
87 `shell' -- A shell connection.
88
89 :return-list specifies this function's return value.
90 If omitted or nil, return a process object. A non-nil means to
91 return (PROC . PROPS), where PROC is a process object and PROPS
92 is a plist of connection properties, with these keywords:
93 :greeting -- the greeting returned by HOST (a string), or nil.
94 :capabilities -- a string representing HOST's capabilities,
95 or nil if none could be found.
96 :type -- the resulting connection type; `plain' (unencrypted)
97 or `tls' (TLS-encrypted).
98
99 :end-of-command specifies a regexp matching the end of a command.
100
101 :end-of-capability specifies a regexp matching the end of the
102 response to the command specified for :capability-command.
103 It defaults to the regexp specified for :end-of-command.
104
105 :success specifies a regexp matching a message indicating a
106 successful STARTTLS negotiation. For instance, the default
107 should be \"^3\" for an NNTP connection.
108
109 :capability-command specifies a command used to query the HOST
110 for its capabilities. For instance, for IMAP this should be
111 \"1 CAPABILITY\\r\\n\".
112
113 :starttls-function specifies a function for handling STARTTLS.
114 This function should take one parameter, the response to the
115 capability command, and should return the command to switch on
116 STARTTLS if the server supports STARTTLS, and nil otherwise.
117
118 :always-query-capabilities says whether to query the server for
119 capabilities, even if we're doing a `plain' network connection.
120
121 :client-certificate should either be a list where the first
122 element is the certificate key file name, and the second
123 element is the certificate file name itself, or `t', which
124 means that `auth-source' will be queried for the key and the
125 certificate. This parameter will only be used when doing TLS
126 or STARTTLS connections.
127
128 :use-starttls-if-possible is a boolean that says to do opportunistic
129 STARTTLS upgrades even if Emacs doesn't have built-in TLS functionality.
130
131 :nowait is a boolean that says the connection should be made
132 asynchronously, if possible."
133 (unless (featurep 'make-network-process)
134 (error "Emacs was compiled without networking support"))
135 (let ((type (plist-get parameters :type))
136 (return-list (plist-get parameters :return-list)))
137 (if (and (not return-list)
138 (or (eq type 'plain)
139 (and (memq type '(nil network))
140 (not (and (plist-get parameters :success)
141 (plist-get parameters :capability-command))))))
142 ;; The simplest case: wrapper around `make-network-process'.
143 (make-network-process :name name :buffer buffer
144 :host host :service service
145 :nowait (plist-get parameters :nowait))
146 (let ((work-buffer (or buffer
147 (generate-new-buffer " *stream buffer*")))
148 (fun (cond ((and (eq type 'plain)
149 (not (plist-get parameters
150 :always-query-capabilities)))
151 'network-stream-open-plain)
152 ((memq type '(nil network starttls plain))
153 'network-stream-open-starttls)
154 ((memq type '(tls ssl)) 'network-stream-open-tls)
155 ((eq type 'shell) 'network-stream-open-shell)
156 (t (error "Invalid connection type %s" type))))
157 result)
158 (unwind-protect
159 (setq result (funcall fun name work-buffer host service parameters))
160 (unless buffer
161 (and (processp (car result))
162 (set-process-buffer (car result) nil))
163 (kill-buffer work-buffer)))
164 (if return-list
165 (list (car result)
166 :greeting (nth 1 result)
167 :capabilities (nth 2 result)
168 :type (nth 3 result)
169 :error (nth 4 result))
170 (car result))))))
171
172 (defun network-stream-certificate (host service parameters)
173 (let ((spec (plist-get :client-certificate parameters)))
174 (cond
175 ((listp spec)
176 ;; Either nil or a list with a key/certificate pair.
177 spec)
178 ((eq spec t)
179 (let* ((auth-info
180 (car (auth-source-search :max 1
181 :host host
182 :port service)))
183 (key (plist-get auth-info :key))
184 (cert (plist-get auth-info :cert)))
185 (and key cert
186 (list key cert)))))))
187
188 ;;;###autoload
189 (defalias 'open-protocol-stream 'open-network-stream)
190
191 (defun network-stream-open-plain (name buffer host service parameters)
192 (let ((start (with-current-buffer buffer (point)))
193 (stream (make-network-process :name name :buffer buffer
194 :host host :service service
195 :nowait (plist-get parameters :nowait))))
196 (list stream
197 (network-stream-get-response stream start
198 (plist-get parameters :end-of-command))
199 nil
200 'plain)))
201
202 (defun network-stream-open-starttls (name buffer host service parameters)
203 (let* ((start (with-current-buffer buffer (point)))
204 (require-tls (eq (plist-get parameters :type) 'starttls))
205 (starttls-function (plist-get parameters :starttls-function))
206 (success-string (plist-get parameters :success))
207 (capability-command (plist-get parameters :capability-command))
208 (eoc (plist-get parameters :end-of-command))
209 (eo-capa (or (plist-get parameters :end-of-capability)
210 eoc))
211 ;; Return (STREAM GREETING CAPABILITIES RESULTING-TYPE)
212 (stream (make-network-process :name name :buffer buffer
213 :host host :service service))
214 (greeting (network-stream-get-response stream start eoc))
215 (capabilities (network-stream-command stream capability-command
216 eo-capa))
217 (resulting-type 'plain)
218 (builtin-starttls (and (fboundp 'gnutls-available-p)
219 (gnutls-available-p)))
220 starttls-available starttls-command error)
221
222 ;; First check whether the server supports STARTTLS at all.
223 (when (and capabilities success-string starttls-function)
224 (setq starttls-command
225 (funcall starttls-function capabilities)))
226 ;; If we have built-in STARTTLS support, try to upgrade the
227 ;; connection.
228 (when (and starttls-command
229 (setq starttls-available
230 (or builtin-starttls
231 (and (or require-tls
232 (plist-get parameters :use-starttls-if-possible))
233 (starttls-available-p))))
234 (not (eq (plist-get parameters :type) 'plain)))
235 ;; If using external STARTTLS, drop this connection and start
236 ;; anew with `starttls-open-stream'.
237 (unless builtin-starttls
238 (delete-process stream)
239 (setq start (with-current-buffer buffer (point-max)))
240 (let* ((starttls-extra-arguments
241 (if require-tls
242 starttls-extra-arguments
243 ;; For opportunistic TLS upgrades, we don't really
244 ;; care about the identity of the peer.
245 (cons "--insecure" starttls-extra-arguments)))
246 (starttls-extra-args starttls-extra-args)
247 (cert (network-stream-certificate host service parameters)))
248 ;; There are client certificates requested, so add them to
249 ;; the command line.
250 (when cert
251 (setq starttls-extra-arguments
252 (nconc (list "--x509keyfile" (expand-file-name (nth 0 cert))
253 "--x509certfile" (expand-file-name (nth 1 cert)))
254 starttls-extra-arguments)
255 starttls-extra-args
256 (nconc (list "--key-file" (expand-file-name (nth 0 cert))
257 "--cert-file" (expand-file-name (nth 1 cert)))
258 starttls-extra-args)))
259 (setq stream (starttls-open-stream name buffer host service)))
260 (network-stream-get-response stream start eoc)
261 ;; Requery capabilities for protocols that require it; i.e.,
262 ;; EHLO for SMTP.
263 (when (plist-get parameters :always-query-capabilities)
264 (network-stream-command stream capability-command eo-capa)))
265 (when (let ((response
266 (network-stream-command stream starttls-command eoc)))
267 (and response (string-match success-string response)))
268 ;; The server said it was OK to begin STARTTLS negotiations.
269 (if builtin-starttls
270 (let ((cert (network-stream-certificate host service parameters)))
271 (condition-case nil
272 (gnutls-negotiate :process stream :hostname host
273 :keylist (and cert (list cert)))
274 ;; If we get a gnutls-specific error (for instance if
275 ;; the certificate the server gives us is completely
276 ;; syntactically invalid), then close the connection
277 ;; and possibly (further down) try to create a
278 ;; non-encrypted connection.
279 (gnutls-error
280 (delete-process stream))))
281 (unless (starttls-negotiate stream)
282 (delete-process stream)))
283 (if (memq (process-status stream) '(open run))
284 (setq resulting-type 'tls)
285 ;; We didn't successfully negotiate STARTTLS; if TLS
286 ;; isn't demanded, reopen an unencrypted connection.
287 (unless require-tls
288 (setq stream
289 (make-network-process :name name :buffer buffer
290 :host host :service service))
291 (network-stream-get-response stream start eoc)))
292 ;; Re-get the capabilities, which may have now changed.
293 (setq capabilities
294 (network-stream-command stream capability-command eo-capa))))
295
296 ;; If TLS is mandatory, close the connection if it's unencrypted.
297 (when (and require-tls
298 ;; ... but Emacs wasn't able to -- either no built-in
299 ;; support, or no gnutls-cli installed.
300 (eq resulting-type 'plain))
301 (setq error
302 (if (or (null starttls-command)
303 starttls-available)
304 "Server does not support TLS"
305 ;; See `starttls-available-p'. If this predicate
306 ;; changes to allow running under Windows, the error
307 ;; message below should be amended.
308 (if (memq system-type '(windows-nt ms-dos))
309 (concat "Emacs does not support TLS")
310 (concat "Emacs does not support TLS, and no external `"
311 (if starttls-use-gnutls
312 starttls-gnutls-program
313 starttls-program)
314 "' program was found"))))
315 (delete-process stream)
316 (setq stream nil))
317 ;; Return value:
318 (list stream greeting capabilities resulting-type error)))
319
320 (defun network-stream-command (stream command eoc)
321 (when command
322 (let ((start (with-current-buffer (process-buffer stream) (point-max))))
323 (process-send-string stream command)
324 (network-stream-get-response stream start eoc))))
325
326 (defun network-stream-get-response (stream start end-of-command)
327 (when end-of-command
328 (with-current-buffer (process-buffer stream)
329 (save-excursion
330 (goto-char start)
331 (while (and (memq (process-status stream) '(open run))
332 (not (re-search-forward end-of-command nil t)))
333 (accept-process-output stream 0 50)
334 (goto-char start))
335 ;; Return the data we got back, or nil if the process died.
336 (unless (= start (point))
337 (buffer-substring start (point)))))))
338
339 (defun network-stream-open-tls (name buffer host service parameters)
340 (with-current-buffer buffer
341 (let* ((start (point-max))
342 (use-builtin-gnutls (and (fboundp 'gnutls-available-p)
343 (gnutls-available-p)))
344 (stream
345 (funcall (if use-builtin-gnutls
346 'open-gnutls-stream
347 'open-tls-stream)
348 name buffer host service))
349 (eoc (plist-get parameters :end-of-command)))
350 (if (null stream)
351 (list nil nil nil 'plain)
352 ;; If we're using tls.el, we have to delete the output from
353 ;; openssl/gnutls-cli.
354 (when (and (null use-builtin-gnutls)
355 eoc)
356 (network-stream-get-response stream start eoc)
357 (goto-char (point-min))
358 (when (re-search-forward eoc nil t)
359 (goto-char (match-beginning 0))
360 (delete-region (point-min) (line-beginning-position))))
361 (let* ((capability-command (plist-get parameters :capability-command)))
362 (list stream
363 (network-stream-get-response stream start eoc)
364 (network-stream-command stream capability-command eoc)
365 'tls))))))
366
367 (defun network-stream-open-shell (name buffer host service parameters)
368 (require 'format-spec)
369 (let* ((capability-command (plist-get parameters :capability-command))
370 (eoc (plist-get parameters :end-of-command))
371 (start (with-current-buffer buffer (point)))
372 (stream (let ((process-connection-type nil))
373 (start-process name buffer shell-file-name
374 shell-command-switch
375 (format-spec
376 (plist-get parameters :shell-command)
377 (format-spec-make
378 ?s host
379 ?p service))))))
380 (list stream
381 (network-stream-get-response stream start eoc)
382 (network-stream-command stream capability-command
383 (or (plist-get parameters :end-of-capability)
384 eoc))
385 'plain)))
386
387 (provide 'network-stream)
388
389 ;;; network-stream.el ends here