Merge from origin/emacs-25
[emacs.git] / lisp / net / sieve-manage.el
blob695bbd860debef9a5ebcd7ecec6e88af5c779fc0
1 ;;; sieve-manage.el --- Implementation of the managesieve protocol in elisp
3 ;; Copyright (C) 2001-2016 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Albert Krewinkel <tarleb@moltkeplatz.de>
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/>.
23 ;;; Commentary:
25 ;; This library provides an elisp API for the managesieve network
26 ;; protocol.
28 ;; It uses the SASL library for authentication, which means it
29 ;; supports DIGEST-MD5, CRAM-MD5, SCRAM-MD5, NTLM, PLAIN and LOGIN
30 ;; methods. STARTTLS is not well tested, but should be easy to get to
31 ;; work if someone wants.
33 ;; The API should be fairly obvious for anyone familiar with the
34 ;; managesieve protocol, interface functions include:
36 ;; `sieve-manage-open'
37 ;; open connection to managesieve server, returning a buffer to be
38 ;; used by all other API functions.
40 ;; `sieve-manage-opened'
41 ;; check if a server is open or not
43 ;; `sieve-manage-close'
44 ;; close a server connection.
46 ;; `sieve-manage-listscripts'
47 ;; `sieve-manage-deletescript'
48 ;; `sieve-manage-getscript'
49 ;; performs managesieve protocol actions
51 ;; and that's it. Example of a managesieve session in *scratch*:
53 ;; (with-current-buffer (sieve-manage-open "mail.example.com")
54 ;; (sieve-manage-authenticate)
55 ;; (sieve-manage-listscripts))
57 ;; => ((active . "main") "vacation")
59 ;; References:
61 ;; draft-martin-managesieve-02.txt,
62 ;; "A Protocol for Remotely Managing Sieve Scripts",
63 ;; by Tim Martin.
65 ;; Release history:
67 ;; 2001-10-31 Committed to Oort Gnus.
68 ;; 2002-07-27 Added DELETESCRIPT. Suggested by Ned Ludd.
69 ;; 2002-08-03 Use SASL library.
70 ;; 2013-06-05 Enabled STARTTLS support, fixed bit rot.
72 ;;; Code:
74 (if (locate-library "password-cache")
75 (require 'password-cache)
76 (require 'password))
78 (eval-when-compile (require 'cl))
79 (require 'sasl)
80 (require 'starttls)
81 (autoload 'sasl-find-mechanism "sasl")
82 (autoload 'auth-source-search "auth-source")
84 ;; User customizable variables:
86 (defgroup sieve-manage nil
87 "Low-level Managesieve protocol issues."
88 :group 'mail
89 :prefix "sieve-")
91 (defcustom sieve-manage-log "*sieve-manage-log*"
92 "Name of buffer for managesieve session trace."
93 :type 'string
94 :group 'sieve-manage)
96 (defcustom sieve-manage-server-eol "\r\n"
97 "The EOL string sent from the server."
98 :type 'string
99 :group 'sieve-manage)
101 (defcustom sieve-manage-client-eol "\r\n"
102 "The EOL string we send to the server."
103 :type 'string
104 :group 'sieve-manage)
106 (defcustom sieve-manage-authenticators '(digest-md5
107 cram-md5
108 scram-md5
109 ntlm
110 plain
111 login)
112 "Priority of authenticators to consider when authenticating to server."
113 ;; FIXME Improve this. It's not `set'.
114 ;; It's like (repeat (choice (const ...))), where each choice can
115 ;; only appear once.
116 :type '(repeat symbol)
117 :group 'sieve-manage)
119 (defcustom sieve-manage-authenticator-alist
120 '((cram-md5 sieve-manage-cram-md5-p sieve-manage-cram-md5-auth)
121 (digest-md5 sieve-manage-digest-md5-p sieve-manage-digest-md5-auth)
122 (scram-md5 sieve-manage-scram-md5-p sieve-manage-scram-md5-auth)
123 (ntlm sieve-manage-ntlm-p sieve-manage-ntlm-auth)
124 (plain sieve-manage-plain-p sieve-manage-plain-auth)
125 (login sieve-manage-login-p sieve-manage-login-auth))
126 "Definition of authenticators.
128 \(NAME CHECK AUTHENTICATE)
130 NAME names the authenticator. CHECK is a function returning non-nil if
131 the server support the authenticator and AUTHENTICATE is a function
132 for doing the actual authentication."
133 :type '(repeat (list (symbol :tag "Name") (function :tag "Check function")
134 (function :tag "Authentication function")))
135 :group 'sieve-manage)
137 (defcustom sieve-manage-default-port "sieve"
138 "Default port number or service name for managesieve protocol."
139 :type '(choice integer string)
140 :version "24.4"
141 :group 'sieve-manage)
143 (defcustom sieve-manage-default-stream 'network
144 "Default stream type to use for `sieve-manage'."
145 :version "24.1"
146 :type 'symbol
147 :group 'sieve-manage)
149 ;; Internal variables:
151 (defconst sieve-manage-local-variables '(sieve-manage-server
152 sieve-manage-port
153 sieve-manage-auth
154 sieve-manage-stream
155 sieve-manage-process
156 sieve-manage-client-eol
157 sieve-manage-server-eol
158 sieve-manage-capability))
159 (defconst sieve-manage-coding-system-for-read 'binary)
160 (defconst sieve-manage-coding-system-for-write 'binary)
161 (defvar sieve-manage-stream nil)
162 (defvar sieve-manage-auth nil)
163 (defvar sieve-manage-server nil)
164 (defvar sieve-manage-port nil)
165 (defvar sieve-manage-state 'closed
166 "Managesieve state.
167 Valid states are `closed', `initial', `nonauth', and `auth'.")
168 (defvar sieve-manage-process nil)
169 (defvar sieve-manage-capability nil)
171 ;; Internal utility functions
172 (autoload 'mm-enable-multibyte "mm-util")
174 (defun sieve-manage-make-process-buffer ()
175 (with-current-buffer
176 (generate-new-buffer (format " *sieve %s:%s*"
177 sieve-manage-server
178 sieve-manage-port))
179 (mapc 'make-local-variable sieve-manage-local-variables)
180 (mm-enable-multibyte)
181 (buffer-disable-undo)
182 (current-buffer)))
184 (defun sieve-manage-erase (&optional p buffer)
185 (let ((buffer (or buffer (current-buffer))))
186 (and sieve-manage-log
187 (with-current-buffer (get-buffer-create sieve-manage-log)
188 (mm-enable-multibyte)
189 (buffer-disable-undo)
190 (goto-char (point-max))
191 (insert-buffer-substring buffer (with-current-buffer buffer
192 (point-min))
193 (or p (with-current-buffer buffer
194 (point-max)))))))
195 (delete-region (point-min) (or p (point-max))))
197 (defun sieve-manage-open-server (server port &optional stream buffer)
198 "Open network connection to SERVER on PORT.
199 Return the buffer associated with the connection."
200 (with-current-buffer buffer
201 (sieve-manage-erase)
202 (setq sieve-manage-state 'initial)
203 (destructuring-bind (proc . props)
204 (open-network-stream
205 "SIEVE" buffer server port
206 :type stream
207 :capability-command "CAPABILITY\r\n"
208 :end-of-command "^\\(OK\\|NO\\).*\n"
209 :success "^OK.*\n"
210 :return-list t
211 :starttls-function
212 (lambda (capabilities)
213 (when (string-match "\\bSTARTTLS\\b" capabilities)
214 "STARTTLS\r\n")))
215 (setq sieve-manage-process proc)
216 (setq sieve-manage-capability
217 (sieve-manage-parse-capability (plist-get props :capabilities)))
218 ;; Ignore new capabilities issues after successful STARTTLS
219 (when (and (memq stream '(nil network starttls))
220 (eq (plist-get props :type) 'tls))
221 (sieve-manage-drop-next-answer))
222 (current-buffer))))
224 ;; Authenticators
225 (defun sieve-sasl-auth (buffer mech)
226 "Login to server using the SASL MECH method."
227 (message "sieve: Authenticating using %s..." mech)
228 (with-current-buffer buffer
229 (let* ((auth-info (auth-source-search :host sieve-manage-server
230 :port "sieve"
231 :max 1
232 :create t))
233 (user-name (or (plist-get (nth 0 auth-info) :user) ""))
234 (user-password (or (plist-get (nth 0 auth-info) :secret) ""))
235 (user-password (if (functionp user-password)
236 (funcall user-password)
237 user-password))
238 (client (sasl-make-client (sasl-find-mechanism (list mech))
239 user-name "sieve" sieve-manage-server))
240 (sasl-read-passphrase
241 ;; We *need* to copy the password, because sasl will modify it
242 ;; somehow.
243 `(lambda (prompt) ,(copy-sequence user-password)))
244 (step (sasl-next-step client nil))
245 (tag (sieve-manage-send
246 (concat
247 "AUTHENTICATE \""
248 mech
249 "\""
250 (and (sasl-step-data step)
251 (concat
252 " \""
253 (base64-encode-string
254 (sasl-step-data step)
255 'no-line-break)
256 "\"")))))
257 data rsp)
258 (catch 'done
259 (while t
260 (setq rsp nil)
261 (goto-char (point-min))
262 (while (null (or (progn
263 (setq rsp (sieve-manage-is-string))
264 (if (not (and rsp (looking-at
265 sieve-manage-server-eol)))
266 (setq rsp nil)
267 (goto-char (match-end 0))
268 rsp))
269 (setq rsp (sieve-manage-is-okno))))
270 (accept-process-output sieve-manage-process 1)
271 (goto-char (point-min)))
272 (sieve-manage-erase)
273 (when (sieve-manage-ok-p rsp)
274 (when (and (cadr rsp)
275 (string-match "^SASL \"\\([^\"]+\\)\"" (cadr rsp)))
276 (sasl-step-set-data
277 step (base64-decode-string (match-string 1 (cadr rsp)))))
278 (if (and (setq step (sasl-next-step client step))
279 (setq data (sasl-step-data step)))
280 ;; We got data for server but it's finished
281 (error "Server not ready for SASL data: %s" data)
282 ;; The authentication process is finished.
283 (throw 'done t)))
284 (unless (stringp rsp)
285 (error "Server aborted SASL authentication: %s" (caddr rsp)))
286 (sasl-step-set-data step (base64-decode-string rsp))
287 (setq step (sasl-next-step client step))
288 (sieve-manage-send
289 (if (sasl-step-data step)
290 (concat "\""
291 (base64-encode-string (sasl-step-data step)
292 'no-line-break)
293 "\"")
294 ""))))
295 (message "sieve: Login using %s...done" mech))))
297 (defun sieve-manage-cram-md5-p (buffer)
298 (sieve-manage-capability "SASL" "CRAM-MD5" buffer))
300 (defun sieve-manage-cram-md5-auth (buffer)
301 "Login to managesieve server using the CRAM-MD5 SASL method."
302 (sieve-sasl-auth buffer "CRAM-MD5"))
304 (defun sieve-manage-digest-md5-p (buffer)
305 (sieve-manage-capability "SASL" "DIGEST-MD5" buffer))
307 (defun sieve-manage-digest-md5-auth (buffer)
308 "Login to managesieve server using the DIGEST-MD5 SASL method."
309 (sieve-sasl-auth buffer "DIGEST-MD5"))
311 (defun sieve-manage-scram-md5-p (buffer)
312 (sieve-manage-capability "SASL" "SCRAM-MD5" buffer))
314 (defun sieve-manage-scram-md5-auth (buffer)
315 "Login to managesieve server using the SCRAM-MD5 SASL method."
316 (sieve-sasl-auth buffer "SCRAM-MD5"))
318 (defun sieve-manage-ntlm-p (buffer)
319 (sieve-manage-capability "SASL" "NTLM" buffer))
321 (defun sieve-manage-ntlm-auth (buffer)
322 "Login to managesieve server using the NTLM SASL method."
323 (sieve-sasl-auth buffer "NTLM"))
325 (defun sieve-manage-plain-p (buffer)
326 (sieve-manage-capability "SASL" "PLAIN" buffer))
328 (defun sieve-manage-plain-auth (buffer)
329 "Login to managesieve server using the PLAIN SASL method."
330 (sieve-sasl-auth buffer "PLAIN"))
332 (defun sieve-manage-login-p (buffer)
333 (sieve-manage-capability "SASL" "LOGIN" buffer))
335 (defun sieve-manage-login-auth (buffer)
336 "Login to managesieve server using the LOGIN SASL method."
337 (sieve-sasl-auth buffer "LOGIN"))
339 ;; Managesieve API
341 (defun sieve-manage-open (server &optional port stream auth buffer)
342 "Open a network connection to a managesieve SERVER (string).
343 Optional argument PORT is port number (integer) on remote server.
344 Optional argument STREAM is any of `sieve-manage-streams' (a symbol).
345 Optional argument AUTH indicates authenticator to use, see
346 `sieve-manage-authenticators' for available authenticators.
347 If nil, chooses the best stream the server is capable of.
348 Optional argument BUFFER is buffer (buffer, or string naming buffer)
349 to work in."
350 (setq sieve-manage-port (or port sieve-manage-default-port))
351 (with-current-buffer (or buffer (sieve-manage-make-process-buffer))
352 (setq sieve-manage-server (or server
353 sieve-manage-server)
354 sieve-manage-stream (or stream
355 sieve-manage-stream
356 sieve-manage-default-stream)
357 sieve-manage-auth (or auth
358 sieve-manage-auth))
359 (message "sieve: Connecting to %s..." sieve-manage-server)
360 (sieve-manage-open-server sieve-manage-server
361 sieve-manage-port
362 sieve-manage-stream
363 (current-buffer))
364 (when (sieve-manage-opened (current-buffer))
365 ;; Choose authenticator
366 (when (and (null sieve-manage-auth)
367 (not (eq sieve-manage-state 'auth)))
368 (dolist (auth sieve-manage-authenticators)
369 (when (funcall (nth 1 (assq auth sieve-manage-authenticator-alist))
370 buffer)
371 (setq sieve-manage-auth auth)
372 (return)))
373 (unless sieve-manage-auth
374 (error "Couldn't figure out authenticator for server")))
375 (sieve-manage-erase)
376 (current-buffer))))
378 (defun sieve-manage-authenticate (&optional buffer)
379 "Authenticate on server in BUFFER.
380 Return `sieve-manage-state' value."
381 (with-current-buffer (or buffer (current-buffer))
382 (if (eq sieve-manage-state 'nonauth)
383 (when (funcall (nth 2 (assq sieve-manage-auth
384 sieve-manage-authenticator-alist))
385 (current-buffer))
386 (setq sieve-manage-state 'auth))
387 sieve-manage-state)))
389 (defun sieve-manage-opened (&optional buffer)
390 "Return non-nil if connection to managesieve server in BUFFER is open.
391 If BUFFER is nil then the current buffer is used."
392 (and (setq buffer (get-buffer (or buffer (current-buffer))))
393 (buffer-live-p buffer)
394 (with-current-buffer buffer
395 (and sieve-manage-process
396 (memq (process-status sieve-manage-process) '(open run))))))
398 (defun sieve-manage-close (&optional buffer)
399 "Close connection to managesieve server in BUFFER.
400 If BUFFER is nil, the current buffer is used."
401 (with-current-buffer (or buffer (current-buffer))
402 (when (sieve-manage-opened)
403 (sieve-manage-send "LOGOUT")
404 (sit-for 1))
405 (when (and sieve-manage-process
406 (memq (process-status sieve-manage-process) '(open run)))
407 (delete-process sieve-manage-process))
408 (setq sieve-manage-process nil)
409 (sieve-manage-erase)
412 (defun sieve-manage-capability (&optional name value buffer)
413 "Check if capability NAME of server BUFFER match VALUE.
414 If it does, return the server value of NAME. If not returns nil.
415 If VALUE is nil, do not check VALUE and return server value.
416 If NAME is nil, return the full server list of capabilities."
417 (with-current-buffer (or buffer (current-buffer))
418 (if (null name)
419 sieve-manage-capability
420 (let ((server-value (cadr (assoc name sieve-manage-capability))))
421 (when (or (null value)
422 (and server-value
423 (string-match value server-value)))
424 server-value)))))
426 (defun sieve-manage-listscripts (&optional buffer)
427 (with-current-buffer (or buffer (current-buffer))
428 (sieve-manage-send "LISTSCRIPTS")
429 (sieve-manage-parse-listscripts)))
431 (defun sieve-manage-havespace (name size &optional buffer)
432 (with-current-buffer (or buffer (current-buffer))
433 (sieve-manage-send (format "HAVESPACE \"%s\" %s" name size))
434 (sieve-manage-parse-okno)))
436 (defun sieve-manage-putscript (name content &optional buffer)
437 (with-current-buffer (or buffer (current-buffer))
438 (sieve-manage-send (format "PUTSCRIPT \"%s\" {%d+}%s%s" name
439 ;; Here we assume that the coding-system will
440 ;; replace each char with a single byte.
441 ;; This is always the case if `content' is
442 ;; a unibyte string.
443 (length content)
444 sieve-manage-client-eol content))
445 (sieve-manage-parse-okno)))
447 (defun sieve-manage-deletescript (name &optional buffer)
448 (with-current-buffer (or buffer (current-buffer))
449 (sieve-manage-send (format "DELETESCRIPT \"%s\"" name))
450 (sieve-manage-parse-okno)))
452 (defun sieve-manage-getscript (name output-buffer &optional buffer)
453 (with-current-buffer (or buffer (current-buffer))
454 (sieve-manage-send (format "GETSCRIPT \"%s\"" name))
455 (let ((script (sieve-manage-parse-string)))
456 (sieve-manage-parse-crlf)
457 (with-current-buffer output-buffer
458 (insert script))
459 (sieve-manage-parse-okno))))
461 (defun sieve-manage-setactive (name &optional buffer)
462 (with-current-buffer (or buffer (current-buffer))
463 (sieve-manage-send (format "SETACTIVE \"%s\"" name))
464 (sieve-manage-parse-okno)))
466 ;; Protocol parsing routines
468 (defun sieve-manage-wait-for-answer ()
469 (let ((pattern "^\\(OK\\|NO\\).*\n")
470 pos)
471 (while (not pos)
472 (setq pos (search-forward-regexp pattern nil t))
473 (goto-char (point-min))
474 (sleep-for 0 50))
475 pos))
477 (defun sieve-manage-drop-next-answer ()
478 (sieve-manage-wait-for-answer)
479 (sieve-manage-erase))
481 (defun sieve-manage-ok-p (rsp)
482 (string= (downcase (or (car-safe rsp) "")) "ok"))
484 (defun sieve-manage-is-okno ()
485 (when (looking-at (concat
486 "^\\(OK\\|NO\\)\\( (\\([^)]+\\))\\)?\\( \\(.*\\)\\)?"
487 sieve-manage-server-eol))
488 (let ((status (match-string 1))
489 (resp-code (match-string 3))
490 (response (match-string 5)))
491 (when response
492 (goto-char (match-beginning 5))
493 (setq response (sieve-manage-is-string)))
494 (list status resp-code response))))
496 (defun sieve-manage-parse-okno ()
497 (let (rsp)
498 (while (null rsp)
499 (accept-process-output (get-buffer-process (current-buffer)) 1)
500 (goto-char (point-min))
501 (setq rsp (sieve-manage-is-okno)))
502 (sieve-manage-erase)
503 rsp))
505 (defun sieve-manage-parse-capability (str)
506 "Parse managesieve capability string `STR'.
507 Set variable `sieve-manage-capability' to "
508 (let ((capas (delq nil
509 (mapcar #'split-string-and-unquote
510 (split-string str "\n")))))
511 (when (string= "OK" (caar (last capas)))
512 (setq sieve-manage-state 'nonauth))
513 capas))
515 (defun sieve-manage-is-string ()
516 (cond ((looking-at "\"\\([^\"]+\\)\"")
517 (prog1
518 (match-string 1)
519 (goto-char (match-end 0))))
520 ((looking-at (concat "{\\([0-9]+\\+?\\)}" sieve-manage-server-eol))
521 (let ((pos (match-end 0))
522 (len (string-to-number (match-string 1))))
523 (if (< (point-max) (+ pos len))
525 (goto-char (+ pos len))
526 (buffer-substring pos (+ pos len)))))))
528 (defun sieve-manage-parse-string ()
529 (let (rsp)
530 (while (null rsp)
531 (accept-process-output (get-buffer-process (current-buffer)) 1)
532 (goto-char (point-min))
533 (setq rsp (sieve-manage-is-string)))
534 (sieve-manage-erase (point))
535 rsp))
537 (defun sieve-manage-parse-crlf ()
538 (when (looking-at sieve-manage-server-eol)
539 (sieve-manage-erase (match-end 0))))
541 (defun sieve-manage-parse-listscripts ()
542 (let (tmp rsp data)
543 (while (null rsp)
544 (while (null (or (setq rsp (sieve-manage-is-okno))
545 (setq tmp (sieve-manage-is-string))))
546 (accept-process-output (get-buffer-process (current-buffer)) 1)
547 (goto-char (point-min)))
548 (when tmp
549 (while (not (looking-at (concat "\\( ACTIVE\\)?"
550 sieve-manage-server-eol)))
551 (accept-process-output (get-buffer-process (current-buffer)) 1)
552 (goto-char (point-min)))
553 (if (match-string 1)
554 (push (cons 'active tmp) data)
555 (push tmp data))
556 (goto-char (match-end 0))
557 (setq tmp nil)))
558 (sieve-manage-erase)
559 (if (sieve-manage-ok-p rsp)
560 data
561 rsp)))
563 (defun sieve-manage-send (cmdstr)
564 (setq cmdstr (concat cmdstr sieve-manage-client-eol))
565 (and sieve-manage-log
566 (with-current-buffer (get-buffer-create sieve-manage-log)
567 (mm-enable-multibyte)
568 (buffer-disable-undo)
569 (goto-char (point-max))
570 (insert cmdstr)))
571 (process-send-string sieve-manage-process cmdstr))
573 (provide 'sieve-manage)
575 ;; sieve-manage.el ends here