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.
40 ;; "*nnimap*" buffer address port
42 ;; :capability-command "1 CAPABILITY\r\n"
44 ;; (lambda (capabilities)
45 ;; (if (not (string-match "STARTTLS" capabilities))
47 ;; "1 STARTTLS\r\n")))
55 (require 'format-spec)
57 (defcustom proto-stream-always-use-starttls t
58 "If non-nil, always try to upgrade network connections with STARTTLS."
63 (declare-function gnutls-negotiate "gnutls"
64 (proc type &optional priority-string trustfiles keyfiles))
67 (defun open-proto-stream (name buffer host service &rest parameters)
68 "Open a network stream to HOST.
69 The first four parameters have the same meaning as in
70 `open-network-stream'. The function returns a list where the
71 first element is the stream, the second element is the greeting
72 the server replied with after connecting, and the third element
73 is a string representing the capabilities of the server (if any).
75 The PARAMETERS is a keyword list that can have the following
78 :type -- either `network', `tls', `shell' or `starttls'. If
79 omitted, the default is `network'.
81 :end-of-command -- a regexp saying what the end of a command is.
82 This defaults to \"\\n\".
84 :capability-command -- a string representing the command used to
85 query server for capabilities. For instance, for IMAP this is
86 \"1 CAPABILITY\\r\\n\".
88 :starttls-function -- a function that takes one parameter, which
89 is the response to the capaibility command. It should return nil
90 if it turns out that the server doesn't support STARTTLS, or the
91 command to switch on STARTTLS otherwise."
92 (let ((type (or (cadr (memq :type parameters)) 'network)))
93 (when (and (eq type 'starttls)
94 (fboundp 'open-gnutls-stream))
98 (destructuring-bind (stream greeting capabilities)
99 (funcall (intern (format "proto-stream-open-%s" type) obarray)
100 name buffer host service parameters)
102 (memq (process-status stream)
105 greeting capabilities))))
107 (defun proto-stream-open-network (name buffer host service parameters)
108 (let* ((start (with-current-buffer buffer (point)))
109 (stream (open-network-stream name buffer host service))
110 (capability-command (cadr (memq :capability-command parameters)))
111 (greeting (proto-stream-get-response
112 stream start (proto-stream-eoc parameters))))
113 (if (not capability-command)
114 (list stream greeting nil)
116 (proto-stream-capabilities stream capability-command
117 (proto-stream-eoc parameters)))
119 (funcall (cadr (memq :starttls-function parameters))
122 ((or (not starttls-command)
123 (not proto-stream-always-use-starttls))
124 ;; If this server doesn't support STARTTLS, but we have
125 ;; requested it explicitly, then close the connection and
127 (if (eq (cadr (memq :type parameters)) 'starttls)
129 (delete-process stream)
131 ;; Otherwise, just return this plain network connection.
132 (list stream greeting capabilities)))
133 ((or (fboundp 'open-gnutls-stream)
134 (executable-find "gnutls-cli"))
135 (unless (fboundp 'open-gnutls-stream)
136 (delete-process stream)
138 (proto-stream-open-starttls name buffer host service
139 stream capabilities starttls-command
140 greeting (proto-stream-eoc parameters)))
141 ((eq (cadr (memq :type parameters)) 'starttls)
142 (delete-process stream)
145 (list stream greeting capabilities)))))))
147 (defun proto-stream-capabilities (stream command end-of-command)
148 (let ((start (with-current-buffer (process-buffer stream) (point-max))))
149 (process-send-string stream command)
150 (proto-stream-get-response stream start end-of-command)))
152 (defun proto-stream-open-starttls (name buffer host service stream
153 capabilities starttls-command
155 (let ((start (with-current-buffer buffer (point-max))))
157 (setq stream (starttls-open-stream name buffer host service))
158 (proto-stream-get-response stream start eoc)
159 (setq start (with-current-buffer buffer (point-max))))
160 (process-send-string stream starttls-command)
161 (proto-stream-get-response stream start eoc)
162 (if (fboundp 'open-gnutls-stream)
163 (gnutls-negotiate stream nil)
164 (starttls-negotiate stream))
165 ;; Re-get the capabilities, since they may have changed
166 ;; after switching to TLS.
167 (setq start (with-current-buffer buffer (point-max)))
168 (process-send-string stream capability-command)
169 (list stream greeting (proto-stream-get-response stream start eoc))))
171 (defun proto-stream-get-response (stream start end-of-command)
172 (with-current-buffer (process-buffer stream)
175 (while (and (memq (process-status stream)
177 (not (re-search-forward end-of-command nil t)))
178 (accept-process-output stream 0 50)
180 (if (= start (point))
181 ;; The process died; return nil.
183 ;; Return the data we got back.
184 (buffer-substring start (point))))))
186 (defun proto-stream-open-tls (name buffer host service parameters)
187 (with-current-buffer buffer
188 (let ((start (point-max))
190 (funcall (if (fboundp 'open-gnutls-stream)
193 name buffer host service)))
194 ;; If we're using tls.el, we have to delete the output from
195 ;; openssl/gnutls-cli.
196 (unless (fboundp 'open-gnutls-stream)
197 (proto-stream-get-response
198 stream start (proto-stream-eoc parameters))
199 (goto-char (point-min))
200 (when (re-search-forward (proto-stream-eoc parameters) nil t)
201 (goto-char (match-beginning 0))
202 (delete-region (point-min) (line-beginning-position))))
203 (proto-stream-capability-open start stream parameters))))
205 (defun proto-stream-open-shell (name buffer host service parameters)
206 (proto-stream-capability-open
207 (with-current-buffer buffer (point))
208 (let ((process-connection-type nil))
209 (start-process name buffer shell-file-name
212 (cadr (memq :shell-command parameters))
218 (defun proto-stream-capability-open (start stream parameters)
219 (let ((capability-command (cadr (memq :capability-command parameters)))
220 (greeting (proto-stream-get-response
221 stream start (proto-stream-eoc parameters))))
222 (list stream greeting
223 (and capability-command
224 (proto-stream-capabilities
225 stream capability-command
226 (proto-stream-eoc parameters))))))
228 (defun proto-stream-eoc (parameters)
229 (or (cadr (memq :end-of-command parameters))
232 (provide 'proto-stream)
234 ;;; proto-stream.el ends here