1 ;;; network-stream.el --- open network processes, possibly with encryption
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library provides the function `open-network-stream', which provides a
26 ;; higher-level interface for opening TCP network processes than the built-in
27 ;; function `make-network-process'. In addition to plain connections, it
28 ;; supports TLS/SSL and STARTTLS connections.
32 ;; (open-network-stream
33 ;; "*nnimap*" buffer address port
35 ;; :capability-command "1 CAPABILITY\r\n"
38 ;; (lambda (capabilities)
39 ;; (if (not (string-match "STARTTLS" capabilities))
41 ;; "1 STARTTLS\r\n")))
48 (declare-function gnutls-negotiate
"gnutls" t t
) ; defun*
51 (defun open-network-stream (name buffer host service
&rest parameters
)
52 "Open a TCP connection to HOST, optionally with encryption.
53 Normally, return a network process object; with a non-nil
54 :return-list parameter, return a list instead (see below).
55 Input and output work as for subprocesses; `delete-process'
58 NAME is the name for the process. It is modified if necessary to
60 BUFFER is a buffer or buffer name to associate with the process.
61 Process output goes at end of that buffer. BUFFER may be nil,
62 meaning that the process is not associated with any buffer.
63 HOST is the name or IP address of the host to connect to.
64 SERVICE is the name of the service desired, or an integer specifying
65 a port number to connect to.
67 The remaining PARAMETERS should be a sequence of keywords and
70 :type specifies the connection type, one of the following:
72 -- Begin with an ordinary network connection, and if
73 the parameters :success and :capability-command
74 are also supplied, try to upgrade to an encrypted
75 connection via STARTTLS. Even if that
76 fails (e.g. if HOST does not support TLS), retain
77 an unencrypted connection.
78 `plain' -- An ordinary, unencrypted network connection.
79 `starttls' -- Begin with an ordinary connection, and try
80 upgrading via STARTTLS. If that fails for any
81 reason, drop the connection; in that case the
82 returned object is a killed process.
83 `tls' -- A TLS connection.
84 `ssl' -- Equivalent to `tls'.
85 `shell' -- A shell connection.
87 :return-list specifies this function's return value.
88 If omitted or nil, return a process object. A non-nil means to
89 return (PROC . PROPS), where PROC is a process object and PROPS
90 is a plist of connection properties, with these keywords:
91 :greeting -- the greeting returned by HOST (a string), or nil.
92 :capabilities -- a string representing HOST's capabilities,
93 or nil if none could be found.
94 :type -- the resulting connection type; `plain' (unencrypted)
95 or `tls' (TLS-encrypted).
97 :end-of-command specifies a regexp matching the end of a command.
99 :success specifies a regexp matching a message indicating a
100 successful STARTTLS negotiation. For instance, the default
101 should be \"^3\" for an NNTP connection.
103 :capability-command specifies a command used to query the HOST
104 for its capabilities. For instance, for IMAP this should be
105 \"1 CAPABILITY\\r\\n\".
107 :starttls-function specifies a function for handling STARTTLS.
108 This function should take one parameter, the response to the
109 capability command, and should return the command to switch on
110 STARTTLS if the server supports STARTTLS, and nil otherwise.
112 :always-query-capabilies says whether to query the server for
113 capabilities, even if we're doing a `plain' network connection.
115 :nowait is a boolean that says the connection should be made
116 asynchronously, if possible."
117 (unless (featurep 'make-network-process
)
118 (error "Emacs was compiled without networking support"))
119 (let ((type (plist-get parameters
:type
))
120 (return-list (plist-get parameters
:return-list
)))
121 (if (and (not return-list
)
123 (and (memq type
'(nil network
))
124 (not (and (plist-get parameters
:success
)
125 (plist-get parameters
:capability-command
))))))
126 ;; The simplest case: wrapper around `make-network-process'.
127 (make-network-process :name name
:buffer buffer
128 :host host
:service service
129 :nowait
(plist-get parameters
:nowait
))
130 (let ((work-buffer (or buffer
131 (generate-new-buffer " *stream buffer*")))
132 (fun (cond ((and (eq type
'plain
)
133 (not (plist-get parameters
134 :always-query-capabilities
)))
135 'network-stream-open-plain
)
136 ((memq type
'(nil network starttls plain
))
137 'network-stream-open-starttls
)
138 ((memq type
'(tls ssl
)) 'network-stream-open-tls
)
139 ((eq type
'shell
) 'network-stream-open-shell
)
140 (t (error "Invalid connection type %s" type
))))
143 (setq result
(funcall fun name work-buffer host service parameters
))
145 (and (processp (car result
))
146 (set-process-buffer (car result
) nil
))
147 (kill-buffer work-buffer
)))
150 :greeting
(nth 1 result
)
151 :capabilities
(nth 2 result
)
152 :type
(nth 3 result
))
156 (defalias 'open-protocol-stream
'open-network-stream
)
158 (defun network-stream-open-plain (name buffer host service parameters
)
159 (let ((start (with-current-buffer buffer
(point)))
160 (stream (make-network-process :name name
:buffer buffer
161 :host host
:service service
162 :nowait
(plist-get parameters
:nowait
))))
164 (network-stream-get-response stream start
165 (plist-get parameters
:end-of-command
))
169 (defun network-stream-open-starttls (name buffer host service parameters
)
170 (let* ((start (with-current-buffer buffer
(point)))
171 (require-tls (eq (plist-get parameters
:type
) 'starttls
))
172 (starttls-function (plist-get parameters
:starttls-function
))
173 (success-string (plist-get parameters
:success
))
174 (capability-command (plist-get parameters
:capability-command
))
175 (eoc (plist-get parameters
:end-of-command
))
176 ;; Return (STREAM GREETING CAPABILITIES RESULTING-TYPE)
177 (stream (make-network-process :name name
:buffer buffer
178 :host host
:service service
))
179 (greeting (network-stream-get-response stream start eoc
))
180 (capabilities (network-stream-command stream capability-command eoc
))
181 (resulting-type 'plain
)
184 ;; If we have built-in STARTTLS support, try to upgrade the
186 (when (and (or (fboundp 'open-gnutls-stream
)
188 (executable-find "gnutls-cli")))
189 capabilities success-string starttls-function
190 (setq starttls-command
191 (funcall starttls-function capabilities
))
192 (not (eq (plist-get parameters
:type
) 'plain
)))
193 ;; If using external STARTTLS, drop this connection and start
194 ;; anew with `starttls-open-stream'.
195 (unless (fboundp 'open-gnutls-stream
)
196 (delete-process stream
)
197 (setq start
(with-current-buffer buffer
(point-max)))
198 (let* ((starttls-use-gnutls t
)
199 (starttls-extra-arguments
201 starttls-extra-arguments
202 ;; For opportunistic TLS upgrades, we don't really
203 ;; care about the identity of the peer.
204 (cons "--insecure" starttls-extra-arguments
))))
205 (setq stream
(starttls-open-stream name buffer host service
)))
206 (network-stream-get-response stream start eoc
))
207 (when (string-match success-string
208 (network-stream-command stream starttls-command eoc
))
209 ;; The server said it was OK to begin STARTTLS negotiations.
210 (if (fboundp 'open-gnutls-stream
)
211 (gnutls-negotiate :process stream
:hostname host
)
212 (unless (starttls-negotiate stream
)
213 (delete-process stream
)))
214 (if (memq (process-status stream
) '(open run
))
215 (setq resulting-type
'tls
)
216 ;; We didn't successfully negotiate STARTTLS; if TLS
217 ;; isn't demanded, reopen an unencrypted connection.
220 (make-network-process :name name
:buffer buffer
221 :host host
:service service
))
222 (network-stream-get-response stream start eoc
)))
223 ;; Re-get the capabilities, which may have now changed.
225 (network-stream-command stream capability-command eoc
))))
227 ;; If TLS is mandatory, close the connection if it's unencrypted.
229 (eq resulting-type
'plain
)
230 (delete-process stream
))
232 (list stream greeting capabilities resulting-type
)))
234 (defun network-stream-command (stream command eoc
)
236 (let ((start (with-current-buffer (process-buffer stream
) (point-max))))
237 (process-send-string stream command
)
238 (network-stream-get-response stream start eoc
))))
240 (defun network-stream-get-response (stream start end-of-command
)
242 (with-current-buffer (process-buffer stream
)
245 (while (and (memq (process-status stream
) '(open run
))
246 (not (re-search-forward end-of-command nil t
)))
247 (accept-process-output stream
0 50)
249 ;; Return the data we got back, or nil if the process died.
250 (unless (= start
(point))
251 (buffer-substring start
(point)))))))
253 (defun network-stream-open-tls (name buffer host service parameters
)
254 (with-current-buffer buffer
255 (let* ((start (point-max))
256 (use-builtin-gnutls (fboundp 'open-gnutls-stream
))
258 (funcall (if use-builtin-gnutls
261 name buffer host service
))
262 (eoc (plist-get parameters
:end-of-command
)))
264 (list nil nil nil
'plain
)
265 ;; If we're using tls.el, we have to delete the output from
266 ;; openssl/gnutls-cli.
267 (when (and (null use-builtin-gnutls
) eoc
)
268 (network-stream-get-response stream start eoc
)
269 (goto-char (point-min))
270 (when (re-search-forward eoc nil t
)
271 (goto-char (match-beginning 0))
272 (delete-region (point-min) (line-beginning-position))))
273 (let* ((capability-command (plist-get parameters
:capability-command
)))
275 (network-stream-get-response stream start eoc
)
276 (network-stream-command stream capability-command eoc
)
279 (defun network-stream-open-shell (name buffer host service parameters
)
280 (require 'format-spec
)
281 (let* ((capability-command (plist-get parameters
:capability-command
))
282 (eoc (plist-get parameters
:end-of-command
))
283 (start (with-current-buffer buffer
(point)))
284 (stream (let ((process-connection-type nil
))
285 (start-process name buffer shell-file-name
288 (plist-get parameters
:shell-command
)
293 (network-stream-get-response stream start eoc
)
294 (network-stream-command stream capability-command eoc
)
297 (provide 'network-stream
)
299 ;;; network-stream.el ends here