(proto-stream-open-starttls): Actually implement the starttls.el STARTTLS.
[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-proto-stream
40 ;;  "*nnimap*" buffer address port
41 ;;  :type 'network
42 ;;  :capability-command "1 CAPABILITY\r\n"
43 ;;  :starttls-function
44 ;;  (lambda (capabilities)
45 ;;    (if (not (string-match "STARTTLS" capabilities))
46 ;;        nil
47 ;;      "1 STARTTLS\r\n")))
48
49 ;;; Code:
50
51 (eval-when-compile
52   (require 'cl))
53 (require 'tls)
54 (require 'starttls)
55 (require 'format-spec)
56
57 (declare-function gnutls-negotiate "gnutls"
58                   (proc type &optional priority-string trustfiles keyfiles))
59
60 ;;;###autoload
61 (defun open-proto-stream (name buffer host service &rest parameters)
62   "Open a network stream to HOST.
63 The first four parameters have the same meaning as in
64 `open-network-stream'.  The function returns a list where the
65 first element is the stream, the second element is the greeting
66 the server replied with after connecting, and the third element
67 is a string representing the capabilities of the server (if any).
68
69 The PARAMETERS is a keyword list that can have the following
70 values:
71
72 :type -- either `network', `tls', `shell' or `starttls'.  If
73 omitted, the default is `network'.
74
75 :end-of-command -- a regexp saying what the end of a command is.
76 This defaults to \"\\n\".
77
78 :capability-command -- a string representing the command used to
79 query server for capabilities.  For instance, for IMAP this is
80 \"1 CAPABILITY\\r\\n\".
81
82 :starttls-function -- a function that takes one parameter, which
83 is the response to the capaibility command.  It should return nil
84 if it turns out that the server doesn't support STARTTLS, or the
85 command to switch on STARTTLS otherwise."
86   (let ((type (or (cadr (memq :type parameters)) 'network)))
87     (when (and (eq type 'starttls)
88                (fboundp 'open-gnutls-stream))
89       (setq type 'network))
90     (when (eq type 'ssl)
91       (setq type 'tls))
92     (destructuring-bind (stream greeting capabilities)
93         (funcall (intern (format "proto-stream-open-%s" type) obarray)
94                  name buffer host service parameters)
95       (list (and stream
96                  (memq (process-status stream)
97                        '(open run))
98                  stream)
99             greeting capabilities))))
100
101 (defun proto-stream-open-network (name buffer host service parameters)
102   (let* ((start (with-current-buffer buffer (point)))
103          (stream (open-network-stream name buffer host service))
104          (capability-command (cadr (memq :capability-command parameters)))
105          (greeting (proto-stream-get-response
106                     stream start (proto-stream-eoc parameters))))
107     (if (not capability-command)
108         (list stream greeting nil)
109       (let* ((capabilities
110               (proto-stream-capabilities stream capability-command
111                                          (proto-stream-eoc parameters)))
112              (starttls-command
113               (funcall (cadr (memq :starttls-function parameters))
114                        capabilities)))
115         (cond
116          ((not starttls-command)
117           ;; If this server doesn't support STARTTLS, but we have
118           ;; requested it explicitly, then close the connection and
119           ;; return nil.
120           (if (eq (cadr (memq :type parameters)) 'starttls)
121               (progn
122                 (delete-process stream)
123                 nil)
124             ;; Otherwise, just return this plain network connection.
125             (list stream greeting capabilities)))
126          ((fboundp 'open-gnutls-stream)
127           (setq start (with-current-buffer buffer (point-max)))
128           (process-send-string stream starttls-command)
129           (proto-stream-get-response stream start (proto-stream-eoc parameters))
130           (gnutls-negotiate stream nil)
131           ;; Re-get the capabilities, since they may have changed
132           ;; after switching to TLS.
133           (setq start (with-current-buffer buffer (point-max)))
134           (process-send-string stream capability-command)
135           (list stream greeting (proto-stream-get-response
136                                  stream start (proto-stream-eoc parameters))))
137          ((executable-find "gnutls-cli")
138           (delete-process stream)
139           (proto-stream-open-starttls name buffer host service parameters))
140          ((eq (cadr (memq :type parameters)) 'starttls)
141           (delete-process stream)
142           nil)
143          (t
144           (list stream greeting capabilities)))))))
145
146 (defun proto-stream-capabilities (stream command end-of-command)
147   (let ((start (with-current-buffer (process-buffer stream) (point-max))))
148     (process-send-string stream command)
149     (proto-stream-get-response stream start end-of-command)))
150
151 (defun proto-stream-open-starttls (name buffer host service parameters)
152   (let* ((start (with-current-buffer buffer (point-max)))
153          (stream (starttls-open-stream name buffer host service))
154          (greeting (proto-stream-get-response
155                     stream start (proto-stream-eoc parameters)))
156          (capabilities
157           (proto-stream-capabilities
158            stream
159            (cadr (memq :capability-command parameters))
160            (proto-stream-eoc parameters)))
161          (starttls-command
162           (funcall (cadr (memq :starttls-function parameters))
163                    capabilities)))
164     (setq start (with-current-buffer buffer (point-max)))
165     (process-send-string stream starttls-command)
166     (proto-stream-get-response stream start (proto-stream-eoc parameters))
167     (starttls-negotiate stream)
168     ;; Re-get the capabilities, since they may have changed
169     ;; after switching to TLS.
170     (setq start (with-current-buffer buffer (point-max)))
171     (process-send-string stream capability-command)
172     (list stream greeting (proto-stream-get-response
173                            stream start (proto-stream-eoc parameters)))))
174
175 (defun proto-stream-get-response (stream start end-of-command)
176   (with-current-buffer (process-buffer stream)
177     (save-excursion
178       (goto-char start)
179       (while (and (memq (process-status stream)
180                         '(open run))
181                   (not (re-search-forward end-of-command nil t)))
182         (accept-process-output stream 0 50)
183         (goto-char start))
184       (if (= start (point))
185           ;; The process died; return nil.
186           nil
187         ;; Return the data we got back.
188         (buffer-substring start (point))))))
189
190 (defun proto-stream-open-tls (name buffer host service parameters)
191   (with-current-buffer buffer
192     (let ((start (point-max))
193           (stream
194            (funcall (if (fboundp 'open-gnutls-stream)
195                         'open-gnutls-stream
196                       'open-tls-stream)
197                     name buffer host service)))
198       ;; If we're using tls.el, we have to delete the output from
199       ;; openssl/gnutls-cli.
200       (unless (fboundp 'open-gnutls-stream)
201         (proto-stream-get-response
202          stream start (proto-stream-eoc parameters))
203         (goto-char (point-min))
204         (when (re-search-forward (proto-stream-eoc parameters) nil t)
205           (goto-char (match-beginning 0))
206           (delete-region (point-min) (line-beginning-position))))
207       (proto-stream-capability-open start stream parameters))))
208
209 (defun proto-stream-open-shell (name buffer host service parameters)
210   (proto-stream-capability-open
211    (with-current-buffer buffer (point))
212    (let ((process-connection-type nil))
213      (start-process name buffer shell-file-name
214                     shell-command-switch
215                     (format-spec
216                      (cadr (memq :shell-command parameters))
217                      (format-spec-make
218                       ?s host
219                       ?p service))))
220    parameters))
221
222 (defun proto-stream-capability-open (start stream parameters)
223   (let ((capability-command (cadr (memq :capability-command parameters)))
224         (greeting (proto-stream-get-response
225                    stream start (proto-stream-eoc parameters))))
226     (list stream greeting
227           (and capability-command
228                (proto-stream-capabilities
229                 stream capability-command
230                 (proto-stream-eoc parameters))))))
231
232 (defun proto-stream-eoc (parameters)
233   (or (cadr (memq :end-of-command parameters))
234       "\r\n"))
235
236 (provide 'proto-stream)
237
238 ;;; proto-stream.el ends here