Merge Gnus' changes.
[emacs.git] / lisp / gnus / proto-stream.el
blob45cc974e7a92decc826fe61bc469ed4a02c0d0d6
1 ;;; proto-stream.el --- negotiating TLS, STARTTLS and other connections
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: network
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/>.
23 ;;; Commentary:
25 ;; This library is meant to provide the glue between modules that want
26 ;; to establish a network connection to a server for protocols such as
27 ;; IMAP, NNTP, SMTP and POP3.
29 ;; The main problem is that there's more than a couple of interfaces
30 ;; towards doing this. You have normal, plain connections, which are
31 ;; no trouble at all, but you also have TLS/SSL connections, and you
32 ;; have STARTTLS. Negotiating this for each protocol can be rather
33 ;; tedious, so this library provides a single entry point, and hides
34 ;; much of the ugliness.
36 ;; Usage example:
38 ;; (open-protocol-stream
39 ;; "*nnimap*" buffer address port
40 ;; :type 'network
41 ;; :capability-command "1 CAPABILITY\r\n"
42 ;; :success " OK "
43 ;; :starttls-function
44 ;; (lambda (capabilities)
45 ;; (if (not (string-match "STARTTLS" capabilities))
46 ;; nil
47 ;; "1 STARTTLS\r\n")))
49 ;;; Code:
51 (require 'tls)
52 (require 'starttls)
54 (declare-function gnutls-negotiate "gnutls"
55 (proc type &optional priority-string trustfiles keyfiles))
57 ;;;###autoload
58 (defun open-protocol-stream (name buffer host service &rest parameters)
59 "Open a network stream to HOST, possibly with encryption.
60 Normally, return a network process object; with a non-nil
61 :return-list parameter, return a list instead (see below).
63 The first four parameters, NAME, BUFFER, HOST, and SERVICE, have
64 the same meanings as in `open-network-stream'. The remaining
65 PARAMETERS should be a sequence of keywords and values:
67 :type specifies the connection type, one of the following:
68 nil or `network'
69 -- Begin with an ordinary network connection, and if
70 the parameters :success and :capability-command
71 are also supplied, try to upgrade to an encrypted
72 connection via STARTTLS. Even if that
73 fails (e.g. if HOST does not support TLS), retain
74 an unencrypted connection.
75 `plain' -- An ordinary, unencrypted network connection.
76 `starttls' -- Begin with an ordinary connection, and try
77 upgrading via STARTTLS. If that fails for any
78 reason, drop the connection; in that case the
79 returned object is a killed process.
80 `tls' -- A TLS connection.
81 `ssl' -- Equivalent to `tls'.
82 `shell' -- A shell connection.
84 :return-list specifies this function's return value.
85 If omitted or nil, return a process object. A non-nil means to
86 return (PROC . PROPS), where PROC is a process object and PROPS
87 is a plist of connection properties, with these keywords:
88 :greeting -- the greeting returned by HOST (a string), or nil.
89 :capabilities -- a string representing HOST's capabilities,
90 or nil if none could be found.
91 :type -- the resulting connection type; `plain' (unencrypted)
92 or `tls' (TLS-encrypted).
94 :end-of-command specifies a regexp matching the end of a command.
95 If non-nil, it defaults to \"\\n\".
97 :success specifies a regexp matching a message indicating a
98 successful STARTTLS negotiation. For instance, the default
99 should be \"^3\" for an NNTP connection.
101 :capability-command specifies a command used to query the HOST
102 for its capabilities. For instance, for IMAP this should be
103 \"1 CAPABILITY\\r\\n\".
105 :starttls-function specifies a function for handling STARTTLS.
106 This function should take one parameter, the response to the
107 capability command, and should return the command to switch on
108 STARTTLS if the server supports STARTTLS, and nil otherwise."
109 (let ((type (plist-get parameters :type))
110 (return-list (plist-get parameters :return-list)))
111 (if (and (not return-list)
112 (or (eq type 'plain)
113 (and (memq type '(nil network))
114 (not (and (plist-get parameters :success)
115 (plist-get parameters :capability-command))))))
116 ;; The simplest case is equivalent to `open-network-stream'.
117 (open-network-stream name buffer host service)
118 ;; For everything else, refer to proto-stream-open-*.
119 (unless (plist-get parameters :end-of-command)
120 (setq parameters (append '(:end-of-command "\r\n") parameters)))
121 (let* ((connection-function
122 (cond
123 ((eq type 'plain) 'proto-stream-open-plain)
124 ((memq type '(nil network starttls))
125 'proto-stream-open-starttls)
126 ((memq type '(tls ssl)) 'proto-stream-open-tls)
127 ((eq type 'shell) 'proto-stream-open-shell)
128 (t (error "Invalid connection type %s" type))))
129 (result (funcall connection-function
130 name buffer host service parameters)))
131 (if return-list
132 (list (car result)
133 :greeting (nth 1 result)
134 :capabilities (nth 2 result)
135 :type (nth 3 result))
136 (car result))))))
138 (defun proto-stream-open-plain (name buffer host service parameters)
139 (let ((start (with-current-buffer buffer (point)))
140 (stream (open-network-stream name buffer host service)))
141 (list stream
142 (proto-stream-get-response stream start
143 (plist-get parameters :end-of-command))
145 'plain)))
147 (defun proto-stream-open-starttls (name buffer host service parameters)
148 (let* ((start (with-current-buffer buffer (point)))
149 (require-tls (eq (plist-get parameters :type) 'starttls))
150 (starttls-function (plist-get parameters :starttls-function))
151 (success-string (plist-get parameters :success))
152 (capability-command (plist-get parameters :capability-command))
153 (eoc (plist-get parameters :end-of-command))
154 ;; Return (STREAM GREETING CAPABILITIES RESULTING-TYPE)
155 (stream (open-network-stream name buffer host service))
156 (greeting (proto-stream-get-response stream start eoc))
157 (capabilities (when capability-command
158 (proto-stream-command stream
159 capability-command eoc)))
160 (resulting-type 'plain)
161 starttls-command)
163 ;; If we have STARTTLS support, try to upgrade the connection.
164 (when (and (or (fboundp 'open-gnutls-stream)
165 (executable-find "gnutls-cli"))
166 capabilities success-string starttls-function
167 (setq starttls-command
168 (funcall starttls-function capabilities)))
169 ;; If using external STARTTLS, drop this connection and start
170 ;; anew with `starttls-open-stream'.
171 (unless (fboundp 'open-gnutls-stream)
172 (delete-process stream)
173 (setq start (with-current-buffer buffer (point-max)))
174 (let* ((starttls-use-gnutls t)
175 (starttls-extra-arguments
176 (if require-tls
177 starttls-extra-arguments
178 ;; For opportunistic TLS upgrades, we don't really
179 ;; care about the identity of the peer.
180 (cons "--insecure" starttls-extra-arguments))))
181 (setq stream (starttls-open-stream name buffer host service)))
182 (proto-stream-get-response stream start eoc))
183 (when (string-match success-string
184 (proto-stream-command stream starttls-command eoc))
185 ;; The server said it was OK to begin STARTTLS negotiations.
186 (if (fboundp 'open-gnutls-stream)
187 (gnutls-negotiate stream nil)
188 (unless (starttls-negotiate stream)
189 (delete-process stream)))
190 (if (memq (process-status stream) '(open run))
191 (setq resulting-type 'tls)
192 ;; We didn't successfully negotiate STARTTLS; if TLS
193 ;; isn't demanded, reopen an unencrypted connection.
194 (unless require-tls
195 (setq stream (open-network-stream name buffer host service))
196 (proto-stream-get-response stream start eoc)))
197 ;; Re-get the capabilities, which may have now changed.
198 (setq capabilities
199 (proto-stream-command stream capability-command eoc))))
201 ;; If TLS is mandatory, close the connection if it's unencrypted.
202 (and require-tls
203 (eq resulting-type 'plain)
204 (delete-process stream))
205 ;; Return value:
206 (list stream greeting capabilities resulting-type)))
208 (defun proto-stream-command (stream command eoc)
209 (let ((start (with-current-buffer (process-buffer stream) (point-max))))
210 (process-send-string stream command)
211 (proto-stream-get-response stream start eoc)))
213 (defun proto-stream-get-response (stream start end-of-command)
214 (with-current-buffer (process-buffer stream)
215 (save-excursion
216 (goto-char start)
217 (while (and (memq (process-status stream)
218 '(open run))
219 (not (re-search-forward end-of-command nil t)))
220 (accept-process-output stream 0 50)
221 (goto-char start))
222 (if (= start (point))
223 ;; The process died; return nil.
225 ;; Return the data we got back.
226 (buffer-substring start (point))))))
228 (defun proto-stream-open-tls (name buffer host service parameters)
229 (with-current-buffer buffer
230 (let ((start (point-max))
231 (stream
232 (funcall (if (fboundp 'open-gnutls-stream)
233 'open-gnutls-stream
234 'open-tls-stream)
235 name buffer host service))
236 (eoc (plist-get parameters :end-of-command)))
237 (if (null stream)
238 (list nil nil nil 'plain)
239 ;; If we're using tls.el, we have to delete the output from
240 ;; openssl/gnutls-cli.
241 (unless (fboundp 'open-gnutls-stream)
242 (proto-stream-get-response stream start eoc)
243 (goto-char (point-min))
244 (when (re-search-forward eoc nil t)
245 (goto-char (match-beginning 0))
246 (delete-region (point-min) (line-beginning-position))))
247 (proto-stream-capability-open start stream parameters 'tls)))))
249 (defun proto-stream-open-shell (name buffer host service parameters)
250 (require 'format-spec)
251 (proto-stream-capability-open
252 (with-current-buffer buffer (point))
253 (let ((process-connection-type nil))
254 (start-process name buffer shell-file-name
255 shell-command-switch
256 (format-spec
257 (plist-get parameters :shell-command)
258 (format-spec-make
259 ?s host
260 ?p service))))
261 parameters 'plain))
263 (defun proto-stream-capability-open (start stream parameters stream-type)
264 (let* ((capability-command (plist-get parameters :capability-command))
265 (eoc (plist-get parameters :end-of-command))
266 (greeting (proto-stream-get-response stream start eoc)))
267 (list stream greeting
268 (and capability-command
269 (proto-stream-command stream capability-command eoc))
270 stream-type)))
272 (provide 'proto-stream)
274 ;;; proto-stream.el ends here