(proto-stream-open-network): Fix some typos.
[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")))
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 :capability-command -- a string representing the command used to
76 query server for capabilities.  For instance, for IMAP this is
77 \"1 CAPABILITY\\r\\n\".
78
79 :starttls-function -- a function that takes one parameter, which
80 is the response to the capaibility command.  It should return nil
81 if it turns out that the server doesn't support STARTTLS, or the
82 command to switch on STARTTLS otherwise."
83   (let ((type (or (cadr (memq :type parameters)) 'stream)))
84     (when (and (eq type 'starttls)
85                (fboundp 'open-gnutls-stream))
86       (setq type 'network))
87     (when (eq type 'ssl)
88       (setq type 'tls))
89     (destructuring-bind (stream greeting capabilities)
90         (funcall (intern (format "proto-stream-open-%s" type) obarray)
91                  name buffer host service parameters)
92       (list (and stream
93                  (memq (process-status stream)
94                        '(open run))
95                  stream)
96             greeting capabilities))))
97
98 (defun proto-stream-open-network (name buffer host service parameters)
99   (let* ((start (with-current-buffer buffer (point)))
100          (stream (open-network-stream name buffer host service))
101          (capability-command (cadr (memq :capability-command parameters)))
102          (greeting (proto-stream-get-response stream start)))
103     (if (not capability-command)
104         (list stream greeting nil)
105       (let* ((capabilities
106               (proto-stream-capabilities stream capability-command))
107              (starttls-command
108               (funcall (cadr (memq :starttls-function parameters))
109                        capabilities)))
110         (cond
111          ((not starttls-command)
112           ;; If this server doesn't support STARTTLS, but we have
113           ;; requested it explicitly, then close the connection and
114           ;; return nil.
115           (if (eq (cadr (memq :type parameters)) 'starttls)
116               (progn
117                 (delete-process stream)
118                 nil)
119             ;; Otherwise, just return this plain network connection.
120             (list stream greeting capabilities)))
121          ((fboundp 'open-gnutls-stream)
122           (setq start (with-current-buffer buffer (point-max)))
123           (process-send-string stream starttls-command)
124           (proto-stream-get-response stream start)
125           (gnutls-negotiate stream nil)
126           ;; Re-get the capabilities, since they may have changed
127           ;; after switching to TLS.
128           (setq start (with-current-buffer buffer (point-max)))
129           (process-send-string stream capability-command)
130           (list stream greeting (proto-stream-get-response stream start)))
131          (t
132           (delete-process stream)
133           (proto-stream-open-starttls name buffer host service parameters)))))))
134
135 (defun proto-stream-capabilities (stream command)
136   (let ((start (with-current-buffer (process-buffer stream) (point-max))))
137     (process-send-string stream command)
138     (proto-stream-get-response stream start)))
139
140 (defun proto-stream-open-starttls (name buffer host service parameters)
141   (proto-stream-capability-open
142    (with-current-buffer buffer (point))
143    (starttls-open-stream name buffer host service)
144    parameters))
145
146 (defun proto-stream-get-response (stream start)
147   (with-current-buffer (process-buffer stream)
148     (save-excursion
149       (goto-char start)
150       (while (and (memq (process-status stream)
151                         '(open run))
152                   (not (search-forward "\n" nil t)))
153         (accept-process-output stream 0 50)
154         (goto-char start))
155       (if (= start (point))
156           ;; The process died; return nil.
157           nil
158         ;; Return the data we got back.
159         (buffer-substring start (point))))))
160
161 (defun proto-stream-open-tls (name buffer host service parameters)
162   (proto-stream-capability-open
163    (with-current-buffer buffer (point))
164    (funcall (if (fboundp 'open-gnutls-stream)
165                 'open-gnutls-stream
166               'open-tls-stream)
167             name buffer host service)
168    parameters))
169
170 (defun proto-stream-open-shell (name buffer host service parameters)
171   (proto-stream-capability-open
172    (with-current-buffer buffer (point))
173    (let ((process-connection-type nil))
174      (start-process name buffer shell-file-name
175                     shell-command-switch
176                     (format-spec
177                      (cadr (memq :shell-command parameters))
178                      (format-spec-make
179                       ?s host
180                       ?p service))))
181    parameters))
182
183 (defun proto-stream-capability-open (start stream parameters)
184   (let ((capability-command (cadr (memq :capability-command parameters)))
185         (greeting (proto-stream-get-response stream start)))
186     (list stream greeting
187           (and capability-command
188                (proto-stream-capabilities stream capability-command)))))
189
190 (provide 'proto-stream)
191
192 ;;; proto-stream.el ends here