Actually mention the STARTTLS upgrade.
[gnus] / lisp / proto-stream.el
1 ;;; proto-stream.el --- negotiating TLS, STARTTLS and other connections
2 ;; Copyright (C) 2010 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: network
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
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.
29
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.
36
37 ;; Usage example:
38
39 ;; (open-protocol-stream
40 ;;  "*nnimap*" buffer address port
41 ;;  :type 'network
42 ;;  :capability-command "1 CAPABILITY\r\n"
43 ;;  :success " OK "
44 ;;  :starttls-function
45 ;;  (lambda (capabilities)
46 ;;    (if (not (string-match "STARTTLS" capabilities))
47 ;;        nil
48 ;;      "1 STARTTLS\r\n")))
49
50 ;;; Code:
51
52 (eval-when-compile
53   (require 'cl))
54 (require 'tls)
55 (require 'starttls)
56 (require 'format-spec)
57
58 (defcustom proto-stream-always-use-starttls (fboundp 'open-gnutls-stream)
59   "If non-nil, always try to upgrade network connections with STARTTLS."
60   :version "24.1"
61   :type 'boolean
62   :group 'comm)
63
64 (declare-function gnutls-negotiate "gnutls"
65                   (proc type &optional priority-string trustfiles keyfiles))
66
67 ;;;###autoload
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).
75
76 The PARAMETERS is a keyword list that can have the following
77 values:
78
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
82 Emacs supports it.
83
84 :end-of-command -- a regexp saying what the end of a command is.
85 This defaults to \"\\n\".
86
87 :success -- a regexp saying whether the STARTTLS command was
88 successful or not.  For instance, for NNTP this is \"^3\".
89
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\".
93
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))
102     (when (eq type 'ssl)
103       (setq type 'tls))
104     (destructuring-bind (stream greeting capabilities)
105         (funcall (intern (format "proto-stream-open-%s" type) obarray)
106                  name buffer host service parameters)
107       (list (and stream
108                  (memq (process-status stream)
109                        '(open run))
110                  stream)
111             greeting capabilities))))
112
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))
120          success)
121     (if (not capability-command)
122         (list stream greeting nil)
123       (let* ((capabilities
124               (proto-stream-command stream capability-command eoc))
125              (starttls-command
126               (funcall (cadr (memq :starttls-function parameters))
127                        capabilities)))
128         (cond
129           ;; If this server doesn't support STARTTLS, but we have
130           ;; requested it explicitly, then close the connection and
131           ;; return nil.
132          ((or (not starttls-command)
133               (and (not (eq type 'starttls))
134                    (not proto-stream-always-use-starttls)))
135           (if (eq type 'starttls)
136               (progn
137                 (delete-process stream)
138                 nil)
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
153                         ;; peer.
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))
158           (if (not
159                (string-match
160                 (cadr (memq :success parameters))
161                 (proto-stream-command stream starttls-command eoc)))
162               ;; We got an error back from the STARTTLS command.
163               (progn
164                 (if (eq type 'starttls)
165                     (progn
166                       (delete-process stream)
167                       nil)
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)
174                 (setq stream nil)))
175             (when (or (null stream)
176                       (not (memq (process-status stream)
177                                  '(open run))))
178               ;; It didn't successfully negotiate STARTTLS, so we reopen
179               ;; the connection.
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)
190           nil)
191          ;; Fall back on using a plain network stream.
192          (t
193           (list stream greeting capabilities)))))))
194
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)))
199
200 (defun proto-stream-get-response (stream start end-of-command)
201   (with-current-buffer (process-buffer stream)
202     (save-excursion
203       (goto-char start)
204       (while (and (memq (process-status stream)
205                         '(open run))
206                   (not (re-search-forward end-of-command nil t)))
207         (accept-process-output stream 0 50)
208         (goto-char start))
209       (if (= start (point))
210           ;; The process died; return nil.
211           nil
212         ;; Return the data we got back.
213         (buffer-substring start (point))))))
214
215 (defun proto-stream-open-tls (name buffer host service parameters)
216   (with-current-buffer buffer
217     (let ((start (point-max))
218           (stream
219            (funcall (if (fboundp 'open-gnutls-stream)
220                         'open-gnutls-stream
221                       'open-tls-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))))
233
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
239                     shell-command-switch
240                     (format-spec
241                      (cadr (memq :shell-command parameters))
242                      (format-spec-make
243                       ?s host
244                       ?p service))))
245    parameters))
246
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))))))
255
256 (defun proto-stream-eoc (parameters)
257   (or (cadr (memq :end-of-command parameters))
258       "\r\n"))
259
260 (provide 'proto-stream)
261
262 ;;; proto-stream.el ends here