1 ;;; proto-stream.el --- negotiating TLS, STARTTLS and other connections
2 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
26 ;; This library is meant to provide the glue between modules that want
27 ;; to establish a network connection to a server for protocols such as
28 ;; IMAP, NNTP, SMTP and POP3.
30 ;; The main problem is that there's more than a couple of interfaces
31 ;; towards doing this. You have normal, plain connections, which are
32 ;; no trouble at all, but you also have TLS/SSL connections, and you
33 ;; have STARTTLS. Negotiating this for each protocol can be rather
34 ;; tedious, so this library provides a single entry point, and hides
35 ;; much of the ugliness.
39 ;; (open-protocol-stream
40 ;; "*nnimap*" buffer address port
42 ;; :capability-command "1 CAPABILITY\r\n"
45 ;; (lambda (capabilities)
46 ;; (if (not (string-match "STARTTLS" capabilities))
48 ;; "1 STARTTLS\r\n")))
56 (require 'format-spec)
58 (defcustom proto-stream-always-use-starttls (fboundp 'open-gnutls-stream)
59 "If non-nil, always try to upgrade network connections with STARTTLS."
64 (declare-function gnutls-negotiate "gnutls"
65 (proc type &optional priority-string trustfiles keyfiles))
68 (defun open-protocol-stream (name buffer host service &rest parameters)
69 "Open a network stream to HOST, upgrading to STARTTLS if possible.
70 The first four parameters have the same meaning as in
71 `open-network-stream'. The function returns a list where the
72 first element is the stream, the second element is the greeting
73 the server replied with after connecting, and the third element
74 is a string representing the capabilities of the server (if any).
76 The PARAMETERS is a keyword list that can have the following
79 :type -- either `network', `tls', `shell' or `starttls'. If
80 omitted, the default is `network'. `network' will be
81 opportunistically upgraded to STARTTLS if both the server and
84 :end-of-command -- a regexp saying what the end of a command is.
85 This defaults to \"\\n\".
87 :success -- a regexp saying whether the STARTTLS command was
88 successful or not. For instance, for NNTP this is \"^3\".
90 :capability-command -- a string representing the command used to
91 query server for capabilities. For instance, for IMAP this is
92 \"1 CAPABILITY\\r\\n\".
94 :starttls-function -- a function that takes one parameter, which
95 is the response to the capaibility command. It should return nil
96 if it turns out that the server doesn't support STARTTLS, or the
97 command to switch on STARTTLS otherwise."
98 (let ((type (or (cadr (memq :type parameters)) 'network)))
99 (when (and (eq type 'starttls)
100 (fboundp 'open-gnutls-stream))
101 (setq type 'network))
104 (destructuring-bind (stream greeting capabilities)
105 (funcall (intern (format "proto-stream-open-%s" type) obarray)
106 name buffer host service parameters)
108 (memq (process-status stream)
111 greeting capabilities))))
113 (defun proto-stream-open-network (name buffer host service parameters)
114 (let* ((start (with-current-buffer buffer (point)))
115 (stream (open-network-stream name buffer host service))
116 (capability-command (cadr (memq :capability-command parameters)))
117 (eoc (proto-stream-eoc parameters))
118 (type (cadr (memq :type parameters)))
119 (greeting (proto-stream-get-response stream start eoc))
121 (if (not capability-command)
122 (list stream greeting nil)
124 (proto-stream-command stream capability-command eoc))
126 (funcall (cadr (memq :starttls-function parameters))
129 ;; If this server doesn't support STARTTLS, but we have
130 ;; requested it explicitly, then close the connection and
132 ((or (not starttls-command)
133 (and (not (eq type 'starttls))
134 (not proto-stream-always-use-starttls)))
135 (if (eq type 'starttls)
137 (delete-process stream)
139 ;; Otherwise, just return this plain network connection.
140 (list stream greeting capabilities)))
141 ;; We have some kind of STARTTLS support, so we try to
142 ;; upgrade the connection opportunistically.
143 ((or (fboundp 'open-gnutls-stream)
144 (executable-find "gnutls-cli"))
145 (unless (fboundp 'open-gnutls-stream)
146 (delete-process stream)
147 (setq start (with-current-buffer buffer (point-max)))
148 (let* ((starttls-use-gnutls t)
149 (starttls-extra-arguments
150 (if (not (eq type 'starttls))
151 ;; When doing opportunistic TLS upgrades we
152 ;; don't really care about the identity of the
154 (cons "--insecure" starttls-extra-arguments)
155 starttls-extra-arguments)))
156 (setq stream (starttls-open-stream name buffer host service)))
157 (proto-stream-get-response stream start eoc))
160 (cadr (memq :success parameters))
161 (proto-stream-command stream starttls-command eoc)))
162 ;; We got an error back from the STARTTLS command.
164 (if (eq type 'starttls)
166 (delete-process stream)
168 (list stream greeting capabilities)))
169 ;; The server said it was OK to start doing STARTTLS negotiations.
170 (if (fboundp 'open-gnutls-stream)
171 (gnutls-negotiate stream nil)
172 (unless (starttls-negotiate stream)
173 (delete-process stream)
175 (when (or (null stream)
176 (not (memq (process-status stream)
178 ;; It didn't successfully negotiate STARTTLS, so we reopen
180 (setq stream (open-network-stream name buffer host service))
181 (proto-stream-get-response stream start eoc))
182 ;; Re-get the capabilities, since they may have changed
183 ;; after switching to TLS.
184 (list stream greeting
185 (proto-stream-command stream capability-command eoc))))
186 ;; We don't have STARTTLS support available, but the caller
187 ;; requested a STARTTLS connection, so we give up.
188 ((eq (cadr (memq :type parameters)) 'starttls)
189 (delete-process stream)
191 ;; Fall back on using a plain network stream.
193 (list stream greeting capabilities)))))))
195 (defun proto-stream-command (stream command eoc)
196 (let ((start (with-current-buffer (process-buffer stream) (point-max))))
197 (process-send-string stream command)
198 (proto-stream-get-response stream start eoc)))
200 (defun proto-stream-get-response (stream start end-of-command)
201 (with-current-buffer (process-buffer stream)
204 (while (and (memq (process-status stream)
206 (not (re-search-forward end-of-command nil t)))
207 (accept-process-output stream 0 50)
209 (if (= start (point))
210 ;; The process died; return nil.
212 ;; Return the data we got back.
213 (buffer-substring start (point))))))
215 (defun proto-stream-open-tls (name buffer host service parameters)
216 (with-current-buffer buffer
217 (let ((start (point-max))
219 (funcall (if (fboundp 'open-gnutls-stream)
222 name buffer host service)))
223 ;; If we're using tls.el, we have to delete the output from
224 ;; openssl/gnutls-cli.
225 (unless (fboundp 'open-gnutls-stream)
226 (proto-stream-get-response
227 stream start (proto-stream-eoc parameters))
228 (goto-char (point-min))
229 (when (re-search-forward (proto-stream-eoc parameters) nil t)
230 (goto-char (match-beginning 0))
231 (delete-region (point-min) (line-beginning-position))))
232 (proto-stream-capability-open start stream parameters))))
234 (defun proto-stream-open-shell (name buffer host service parameters)
235 (proto-stream-capability-open
236 (with-current-buffer buffer (point))
237 (let ((process-connection-type nil))
238 (start-process name buffer shell-file-name
241 (cadr (memq :shell-command parameters))
247 (defun proto-stream-capability-open (start stream parameters)
248 (let ((capability-command (cadr (memq :capability-command parameters)))
249 (greeting (proto-stream-get-response
250 stream start (proto-stream-eoc parameters))))
251 (list stream greeting
252 (and capability-command
253 (proto-stream-command
254 stream capability-command (proto-stream-eoc parameters))))))
256 (defun proto-stream-eoc (parameters)
257 (or (cadr (memq :end-of-command parameters))
260 (provide 'proto-stream)
262 ;;; proto-stream.el ends here