comparison lisp/gnus/proto-stream.el @ 111789:f97704487fb3

Merge changes made in Gnus trunk. nnir.el: Batch header retrieval. proto-stream.el: New library to provide protocol-specific TLS/STARTTLS connections for IMAP, NNTP, SMTP, POP3 and similar protocols. nnimap.el (nnimap-open-connection): Use it. proto-stream.el (open-proto-stream): Complete the documentation. nnimap.el (nnimap-open-connection): Check for "OK" from the greeting. nntp.el: Use proto-streams for the relevant connections types. nntp.el (nntp-open-connection): Switch on STARTTLS on supported servers. proto-stream.el (open-proto-stream): Add a way to specify what the end of a command is. proto-stream.el (proto-stream-open-tls): Delete output from openssl if we're using tls.el. proto-stream.el (proto-stream-open-network): If we don't have gnutls-cli or gnutls built in, then don't try to establish a STARTTLS connection. color.el (color-lab->srgb): Fix function call name. proto-stream.el: Fix the syntax in the comment. nntp.el (nntp-open-connection): Fix the STARTTLS command syntax. proto-stream.el (proto-stream-open-starttls): Actually implement the starttls.el STARTTLS. proto-stream.el (proto-stream-always-use-starttls): New variable. proto-stream.el (proto-stream-open-starttls): De-duplicate the starttls code. proto-stream.el (proto-stream-open-starttls): Folded back into the main function. proto-stream.el (proto-stream-command): Refactor out. nnimap.el (nnimap-stream): Change default to `undecided'. nnimap.el (nnimap-open-connection): If `nnimap-stream' is `undecided', try ssl first, and then network. nnimap.el (nnimap-open-connection-1): Respect nnimap-server-port. nnimap.el (nnimap-open-connection): Be more backwards-compatible. proto-stream.el (open-protocol-stream): Renamed from open-proto-stream. proto-stream.el (proto-stream-open-network): When doing opportunistic TLS upgrades we don't really care about the identity of the peer. gnus.texi (Customizing the IMAP Connection): Note the new defaults. gnus.texi (Direct Functions): Note the STARTTLS upgrade. proto-stream.el (proto-stream-open-network): Force starttls.el to use gnutls-cli, since that what we've checked for. proto-stream.el (proto-stream-always-use-starttls): Only default to t if open-gnutls-stream exists. proto-stream.el (proto-stream-open-network): If STARTTLS failed, then just open a normal connection. proto-stream.el (proto-stream-open-network): Wait until the greeting before doing STARTTLS. nnimap.el (nnimap-open-connection-1): Always upgrade to STARTTLS (for backwards compatibility). nnimap.el (nnimap-open-connection-1): Really respect nnimap-server-port. nntp.el (nntp-open-connection): Provide a :success condition. nnimap.el (nnimap-open-connection-1): Ditto. proto-stream.el (proto-stream-open-network): See what the response to the STARTTLS command is. proto-stream.el (proto-stream-open-network): Add some comments. proto-stream.el: Fix example. proto-stream.el (open-protocol-stream): Actually mention the STARTTLS upgrade. nnir.el (nnir-get-active): Skip nnir-ignored-newsgroups when searching. nnir.el (nnir-ignore-newsgroups): Fix default value. nnir.el (nnir-run-gmane): Use mm-delete-duplicates instead of delete-dups that is not available in XEmacs 21.4. mm-util.el (mm-delete-duplicates): Add comment. gnus-sum.el (gnus-summary-delete-article): If delete fails don't change the registry. nnimap.el (nnimap-open-connection-1): w32 open-network-stream doesn't seem to accept strings-with-numbers as port numbers. color.el: fix docstring to use English rather than math notation for intervals. shr.el (shr-find-fill-point): Don't break before apostrophes. nnir.el (nnir-request-move-article): Bail out if no move support in group. color.el (color-rgb->hsv): Fix docstring. nnir.el (nnir-get-active): Improve active list retrieval. shr.el (shr-find-fill-point): Work better for kinsoku chars and apostrophes. gnus-gravatar.el (gnus-gravatar-size): Set gnus-gravatar-size to nil. nnimap.el (nnimap-open-connection-1): Use gnus-string-match-p. nnimap.el (nnimap-open-connection-1): Fix PREAUTH. proto-stream.el (open-protocol-stream): All starttls connections are handled by the network handler. gnus-gravatar.el (gnus-gravatar-insert): Delete unnecessary binding to t of inhibit-read-only since it is inside gnus-with-article-headers. gnus-gravatar.el (gnus-gravatar-transform-address): Use mail-extract-address-components that supports non-ASCII names rather than mail-header-parse-addresses. shr.el (shr-find-fill-point): Don't break line between kinsoku-bol characters. gnus-gravatar.el (gnus-gravatar-insert): Allow LWSP in the middle of names. nnmaildir.el (nnmaildir-request-set-mark): Add article to add-mark funcall. gnus-msg.el: Remove nastygram thing. message.el (message-from-style): Fix comment. message.el (message-user-organization): Do not use gnus-local-organization. gnus.el: Remove gnus-local-organization. rtree.el: New file to handle range trees. nnir.el, gnus-sum.el: Redo the way nnir handles registry updates. rtree.el (rtree-extract): Simplify. gnus-win.el (gnus-configure-windows): Remove Gnus 3.x setting support. gnus-msg.el: Mark gnus-outgoing-message-group as obsolete. gnus.texi (Archived Messages): Remove gnus-outgoing-message-group. gnus-win.el (gnus-configure-frame): Remove old compatibility code. rtree.el (rtree-memq): Rewrite it as a non-recursive function. rtree.el (rtree-add, rtree-delq, rtree-length): Implement. rtree.el (rtree-add): Make code slightly faster. nnir.el: Allow modified summary-line-format in nnir summary buffers.
author Katsumi Yamaoka <yamaoka@jpl.org>
date Thu, 02 Dec 2010 22:21:31 +0000
parents
children 5deef9141286
comparison
equal deleted inserted replaced
111788:8e746f396237 111789:f97704487fb3
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 (cond
100 ((eq type 'starttls)
101 (setq type 'network))
102 ((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