1 ;;; network-stream.el --- open network processes, possibly with encryption
3 ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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/>.
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.
32 ;; (open-network-stream
33 ;; "*nnimap*" buffer address port
35 ;; :capability-command "1 CAPABILITY\r\n"
38 ;; (lambda (capabilities)
39 ;; (if (not (string-match "STARTTLS" capabilities))
41 ;; "1 STARTTLS\r\n")))
47 (require 'auth-source
)
50 (autoload 'gnutls-negotiate
"gnutls")
51 (autoload 'open-gnutls-stream
"gnutls")
54 (defun open-network-stream (name buffer host service
&rest parameters
)
55 "Open a TCP connection to HOST, optionally with encryption.
56 Normally, return a network process object; with a non-nil
57 :return-list parameter, return a list instead (see below).
58 Input and output work as for subprocesses; `delete-process'
61 NAME is the name for the process. It is modified if necessary to
63 BUFFER is a buffer or buffer name to associate with the process.
64 Process output goes at end of that buffer. BUFFER may be nil,
65 meaning that the process is not associated with any buffer.
66 HOST is the name or IP address of the host to connect to.
67 SERVICE is the name of the service desired, or an integer specifying
68 a port number to connect to.
70 The remaining PARAMETERS should be a sequence of keywords and
73 :type specifies the connection type, one of the following:
75 -- Begin with an ordinary network connection, and if
76 the parameters :success and :capability-command
77 are also supplied, try to upgrade to an encrypted
78 connection via STARTTLS. Even if that
79 fails (e.g. if HOST does not support TLS), retain
80 an unencrypted connection.
81 `plain' -- An ordinary, unencrypted network connection.
82 `starttls' -- Begin with an ordinary connection, and try
83 upgrading via STARTTLS. If that fails for any
84 reason, drop the connection; in that case the
85 returned object is a killed process.
86 `tls' -- A TLS connection.
87 `ssl' -- Equivalent to `tls'.
88 `shell' -- A shell connection.
90 :return-list specifies this function's return value.
91 If omitted or nil, return a process object. A non-nil means to
92 return (PROC . PROPS), where PROC is a process object and PROPS
93 is a plist of connection properties, with these keywords:
94 :greeting -- the greeting returned by HOST (a string), or nil.
95 :capabilities -- a string representing HOST's capabilities,
96 or nil if none could be found.
97 :type -- the resulting connection type; `plain' (unencrypted)
98 or `tls' (TLS-encrypted).
100 :end-of-command specifies a regexp matching the end of a command.
102 :end-of-capability specifies a regexp matching the end of the
103 response to the command specified for :capability-command.
104 It defaults to the regexp specified for :end-of-command.
106 :success specifies a regexp matching a message indicating a
107 successful STARTTLS negotiation. For instance, the default
108 should be \"^3\" for an NNTP connection.
110 :capability-command specifies a command used to query the HOST
111 for its capabilities. For instance, for IMAP this should be
112 \"1 CAPABILITY\\r\\n\".
114 :starttls-function specifies a function for handling STARTTLS.
115 This function should take one parameter, the response to the
116 capability command, and should return the command to switch on
117 STARTTLS if the server supports STARTTLS, and nil otherwise.
119 :always-query-capabilities says whether to query the server for
120 capabilities, even if we're doing a `plain' network connection.
122 :client-certificate should either be a list where the first
123 element is the certificate key file name, and the second
124 element is the certificate file name itself, or `t', which
125 means that `auth-source' will be queried for the key and the
126 certificate. This parameter will only be used when doing TLS
127 or STARTTLS connections.
129 :use-starttls-if-possible is a boolean that says to do opportunistic
130 STARTTLS upgrades even if Emacs doesn't have built-in TLS functionality.
132 :warn-unless-encrypted is a boolean which, if :return-list is
133 non-nil, is used warn the user if the connection isn't encrypted.
135 :nogreeting is a boolean that can be used to inhibit waiting for
136 a greeting from the server.
138 :nowait is a boolean that says the connection should be made
139 asynchronously, if possible."
140 (unless (featurep 'make-network-process
)
141 (error "Emacs was compiled without networking support"))
142 (let ((type (plist-get parameters
:type
))
143 (return-list (plist-get parameters
:return-list
)))
144 (if (and (not return-list
)
146 (and (memq type
'(nil network
))
147 (not (and (plist-get parameters
:success
)
148 (plist-get parameters
:capability-command
))))))
149 ;; The simplest case: wrapper around `make-network-process'.
150 (make-network-process :name name
:buffer buffer
151 :host host
:service service
152 :nowait
(plist-get parameters
:nowait
))
153 (let ((work-buffer (or buffer
154 (generate-new-buffer " *stream buffer*")))
155 (fun (cond ((and (eq type
'plain
)
156 (not (plist-get parameters
157 :always-query-capabilities
)))
158 'network-stream-open-plain
)
159 ((memq type
'(nil network starttls plain
))
160 'network-stream-open-starttls
)
161 ((memq type
'(tls ssl
)) 'network-stream-open-tls
)
162 ((eq type
'shell
) 'network-stream-open-shell
)
163 (t (error "Invalid connection type %s" type
))))
166 (setq result
(funcall fun name work-buffer host service parameters
))
168 (and (processp (car result
))
169 (set-process-buffer (car result
) nil
))
170 (kill-buffer work-buffer
)))
173 :greeting
(nth 1 result
)
174 :capabilities
(nth 2 result
)
176 :error
(nth 4 result
))
179 (defun network-stream-certificate (host service parameters
)
180 (let ((spec (plist-get :client-certificate parameters
)))
183 ;; Either nil or a list with a key/certificate pair.
187 (car (auth-source-search :max
1
190 (key (plist-get auth-info
:key
))
191 (cert (plist-get auth-info
:cert
)))
193 (list key cert
)))))))
196 (defalias 'open-protocol-stream
'open-network-stream
)
198 (defun network-stream-open-plain (name buffer host service parameters
)
199 (let ((start (with-current-buffer buffer
(point)))
200 (stream (make-network-process :name name
:buffer buffer
201 :host host
:service service
202 :nowait
(plist-get parameters
:nowait
))))
203 (when (plist-get parameters
:warn-unless-encrypted
)
204 (setq stream
(nsm-verify-connection stream host service nil t
)))
206 (network-stream-get-response stream start
207 (plist-get parameters
:end-of-command
))
211 (defun network-stream-open-starttls (name buffer host service parameters
)
212 (let* ((start (with-current-buffer buffer
(point)))
213 (require-tls (eq (plist-get parameters
:type
) 'starttls
))
214 (starttls-function (plist-get parameters
:starttls-function
))
215 (success-string (plist-get parameters
:success
))
216 (capability-command (plist-get parameters
:capability-command
))
217 (eoc (plist-get parameters
:end-of-command
))
218 (eo-capa (or (plist-get parameters
:end-of-capability
)
220 ;; Return (STREAM GREETING CAPABILITIES RESULTING-TYPE)
221 (stream (make-network-process :name name
:buffer buffer
222 :host host
:service service
))
223 (greeting (and (not (plist-get parameters
:nogreeting
))
224 (network-stream-get-response stream start eoc
)))
225 (capabilities (network-stream-command stream capability-command
227 (resulting-type 'plain
)
228 starttls-available starttls-command error
)
230 ;; First check whether the server supports STARTTLS at all.
231 (when (and capabilities success-string starttls-function
)
232 (setq starttls-command
233 (funcall starttls-function capabilities
)))
234 ;; If we have built-in STARTTLS support, try to upgrade the
236 (when (and starttls-command
237 (setq starttls-available
238 (or (gnutls-available-p)
240 (plist-get parameters
:use-starttls-if-possible
))
241 (starttls-available-p))))
242 (not (eq (plist-get parameters
:type
) 'plain
)))
243 ;; If using external STARTTLS, drop this connection and start
244 ;; anew with `starttls-open-stream'.
245 (unless (gnutls-available-p)
246 (delete-process stream
)
247 (setq start
(with-current-buffer buffer
(point-max)))
248 (let* ((starttls-extra-arguments
250 (member "--insecure" starttls-extra-arguments
))
251 starttls-extra-arguments
252 ;; For opportunistic TLS upgrades, we don't really
253 ;; care about the identity of the peer.
254 (cons "--insecure" starttls-extra-arguments
)))
255 (starttls-extra-args starttls-extra-args
)
256 (cert (network-stream-certificate host service parameters
)))
257 ;; There are client certificates requested, so add them to
260 (setq starttls-extra-arguments
261 (nconc (list "--x509keyfile" (expand-file-name (nth 0 cert
))
262 "--x509certfile" (expand-file-name (nth 1 cert
)))
263 starttls-extra-arguments
)
265 (nconc (list "--key-file" (expand-file-name (nth 0 cert
))
266 "--cert-file" (expand-file-name (nth 1 cert
)))
267 starttls-extra-args
)))
268 (setq stream
(starttls-open-stream name buffer host service
)))
269 (network-stream-get-response stream start eoc
)
270 ;; Requery capabilities for protocols that require it; i.e.,
272 (when (plist-get parameters
:always-query-capabilities
)
273 (network-stream-command stream capability-command eo-capa
)))
274 (when (let ((response
275 (network-stream-command stream starttls-command eoc
)))
276 (and response
(string-match success-string response
)))
277 ;; The server said it was OK to begin STARTTLS negotiations.
278 (if (gnutls-available-p)
279 (let ((cert (network-stream-certificate host service parameters
)))
281 (gnutls-negotiate :process stream
:hostname host
282 :keylist
(and cert
(list cert
)))
283 ;; If we get a gnutls-specific error (for instance if
284 ;; the certificate the server gives us is completely
285 ;; syntactically invalid), then close the connection
286 ;; and possibly (further down) try to create a
287 ;; non-encrypted connection.
289 (delete-process stream
))))
290 (unless (starttls-negotiate stream
)
291 (delete-process stream
)))
292 (if (memq (process-status stream
) '(open run
))
293 (setq resulting-type
'tls
)
294 ;; We didn't successfully negotiate STARTTLS; if TLS
295 ;; isn't demanded, reopen an unencrypted connection.
298 (make-network-process :name name
:buffer buffer
299 :host host
:service service
))
300 (network-stream-get-response stream start eoc
)))
301 ;; Re-get the capabilities, which may have now changed.
303 (network-stream-command stream capability-command eo-capa
))))
305 ;; If TLS is mandatory, close the connection if it's unencrypted.
306 (when (and require-tls
307 ;; ... but Emacs wasn't able to -- either no built-in
308 ;; support, or no gnutls-cli installed.
309 (eq resulting-type
'plain
))
311 (if (or (null starttls-command
)
313 "Server does not support TLS"
314 ;; See `starttls-available-p'. If this predicate
315 ;; changes to allow running under Windows, the error
316 ;; message below should be amended.
317 (if (memq system-type
'(windows-nt ms-dos
))
318 (concat "Emacs does not support TLS")
319 (concat "Emacs does not support TLS, and no external `"
320 (if starttls-use-gnutls
321 starttls-gnutls-program
323 "' program was found"))))
324 (delete-process stream
)
326 ;; Check certificate validity etc.
327 (when (gnutls-available-p)
328 (setq stream
(nsm-verify-connection
330 (eq resulting-type
'tls
)
331 (plist-get parameters
:warn-unless-encrypted
))))
333 (list stream greeting capabilities resulting-type error
)))
335 (defun network-stream-command (stream command eoc
)
337 (let ((start (with-current-buffer (process-buffer stream
) (point-max))))
338 (process-send-string stream command
)
339 (network-stream-get-response stream start eoc
))))
341 (defun network-stream-get-response (stream start end-of-command
)
343 (with-current-buffer (process-buffer stream
)
346 (while (and (memq (process-status stream
) '(open run
))
347 (not (re-search-forward end-of-command nil t
)))
348 (accept-process-output stream
0 50)
350 ;; Return the data we got back, or nil if the process died.
351 (unless (= start
(point))
352 (buffer-substring start
(point)))))))
354 (defun network-stream-open-tls (name buffer host service parameters
)
355 (with-current-buffer buffer
356 (let* ((start (point-max))
358 (funcall (if (gnutls-available-p)
361 name buffer host service
))
362 (eoc (plist-get parameters
:end-of-command
)))
363 ;; Check certificate validity etc.
364 (when (and (gnutls-available-p) stream
)
365 (setq stream
(nsm-verify-connection stream host service
)))
367 (list nil nil nil
'plain
)
368 ;; If we're using tls.el, we have to delete the output from
369 ;; openssl/gnutls-cli.
370 (when (and (not (gnutls-available-p))
372 (network-stream-get-response stream start eoc
)
373 (goto-char (point-min))
374 (when (re-search-forward eoc nil t
)
375 (goto-char (match-beginning 0))
376 (delete-region (point-min) (line-beginning-position))))
377 (let* ((capability-command (plist-get parameters
:capability-command
)))
379 (network-stream-get-response stream start eoc
)
380 (network-stream-command stream capability-command eoc
)
383 (defun network-stream-open-shell (name buffer host service parameters
)
384 (require 'format-spec
)
385 (let* ((capability-command (plist-get parameters
:capability-command
))
386 (eoc (plist-get parameters
:end-of-command
))
387 (start (with-current-buffer buffer
(point)))
388 (stream (let ((process-connection-type nil
))
389 (start-process name buffer shell-file-name
392 (plist-get parameters
:shell-command
)
397 (network-stream-get-response stream start eoc
)
398 (network-stream-command stream capability-command
399 (or (plist-get parameters
:end-of-capability
)
403 (provide 'network-stream
)
405 ;;; network-stream.el ends here