1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-21 10:24:55 +00:00
emacs/lisp/gnus/canlock.el

254 lines
8.7 KiB
EmacsLisp
Raw Normal View History

;;; canlock.el --- functions for Cancel-Lock feature
2005-08-06 19:51:42 +00:00
;; Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
2008-01-07 01:26:07 +00:00
;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
2007-11-28 08:03:03 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
2007-07-25 04:24:43 +00:00
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
2007-11-28 08:03:03 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
2007-11-28 08:03:03 +00:00
;; along with GNU Emacs; see the file COPYING. If not, write to the
2005-07-04 17:55:18 +00:00
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Canlock is a library for generating and verifying Cancel-Lock and/or
;; Cancel-Key header in news articles. This is used to protect articles
;; from rogue cancel, supersede or replace attacks. The method is based
;; on draft-ietf-usefor-cancel-lock-01.txt which was released on November
;; 3rd 1998. For instance, you can add Cancel-Lock (and possibly Cancel-
;; Key) header in a news article by using a hook which will be evaluated
;; just before sending an article as follows:
;;
;; (add-hook '*e**a*e-header-hook 'canlock-insert-header t)
;;
;; Verifying Cancel-Lock is mainly a function of news servers, however,
;; you can verify your own article using the command `canlock-verify' in
;; the (raw) article buffer. You will be prompted for the password for
;; each time if the option `canlock-password' or `canlock-password-for-
;; verify' is nil. Note that setting these options is a bit unsafe.
;;; Code:
(eval-when-compile
(require 'cl))
(require 'sha1)
(defvar mail-header-separator)
(defgroup canlock nil
"The Cancel-Lock feature."
Revision: miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-324 Merge from gnus--rel--5.10 Patches applied: * gnus--rel--5.10 (patch 70) - Update from CVS 2005-05-17 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/canlock.el (canlock): Change the parent group to news. * lisp/gnus/deuglify.el (gnus-outlook-deuglify): Add :group. * lisp/gnus/dig.el (dig): Add :group. * lisp/gnus/gnus-art.el (gnus-inhibit-mime-unbuttonizing): Add :group. * lisp/gnus/gnus-cite.el (gnus-cite-attribution-face): Add :group. (gnus-cite-face-1, gnus-cite-face-2, gnus-cite-face-3): Ditto. (gnus-cite-face-4, gnus-cite-face-5, gnus-cite-face-6): Ditto. (gnus-cite-face-7, gnus-cite-face-8, gnus-cite-face-9): Ditto. (gnus-cite-face-10, gnus-cite-face-11): Ditto. * lisp/gnus/gnus-diary.el (gnus-diary): Add :group. * lisp/gnus/gnus.el (gnus-group-news-1-face): Add :group. (gnus-group-news-1-empty-face): Ditto. (gnus-group-news-2-face, gnus-group-news-2-empty-face): Ditto. (gnus-group-news-3-face, gnus-group-news-3-empty-face): Ditto. (gnus-group-news-4-face, gnus-group-news-4-empty-face): Ditto. (gnus-group-news-5-face, gnus-group-news-5-empty-face): Ditto. (gnus-group-news-6-face, gnus-group-news-6-empty-face): Ditto. (gnus-group-news-low-face, gnus-group-news-low-empty-face): Ditto. (gnus-group-mail-1-face, gnus-group-mail-1-empty-face): Ditto. (gnus-group-mail-2-face, gnus-group-mail-2-empty-face): Ditto. (gnus-group-mail-3-face, gnus-group-mail-3-empty-face): Ditto. (gnus-group-mail-low-face, gnus-group-mail-low-empty-face): Ditto. (gnus-summary-selected-face, gnus-summary-cancelled-face): Ditto. (gnus-summary-high-ticked-face): Ditto. (gnus-summary-low-ticked-face): Ditto. (gnus-summary-normal-ticked-face): Ditto. (gnus-summary-high-ancient-face): Ditto. (gnus-summary-low-ancient-face): Ditto. (gnus-summary-normal-ancient-face): Ditto. (gnus-summary-high-undownloaded-face): Ditto. (gnus-summary-low-undownloaded-face): Ditto. (gnus-summary-normal-undownloaded-face): Ditto. (gnus-summary-high-unread-face): Ditto. (gnus-summary-low-unread-face): Ditto. (gnus-summary-normal-unread-face): Ditto. (gnus-summary-high-read-face, gnus-summary-low-read-face): Diito (gnus-summary-normal-read-face, gnus-splash-face): Ditto. * lisp/gnus/message.el (message-minibuffer-local-map): Add :group. * lisp/gnus/sieve-manage.el (sieve-manage-log): Add :group. (sieve-manage-default-user): Diito. (sieve-manage-server-eol, sieve-manage-client-eol): Ditto. (sieve-manage-streams, sieve-manage-stream-alist): Ditto. (sieve-manage-authenticators): Ditto. (sieve-manage-authenticator-alist): Ditto (sieve-manage-default-port): Ditto. * lisp/gnus/sieve-mode.el (sieve-control-commands-face): Add :group. (sieve-action-commands-face): Ditto. (sieve-test-commands-face): Ditto. (sieve-tagged-arguments-face): Ditto. * lisp/gnus/smime.el (smime): Add :group. * lisp/gnus/spam-report.el (spam-report): Add :group. * lisp/gnus/spam.el (spam, spam-face): Add :group.
2005-05-26 15:03:29 +00:00
:group 'news)
(defcustom canlock-password nil
"Password to use when signing a Cancel-Lock or a Cancel-Key header."
:type '(radio (const :format "Not specified " nil)
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-716 Merge from gnus--rel--5.10 Patches applied: * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-74 Update from CVS 2004-12-02 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/message.el (message-forward-make-body-mml): Remove headers according to message-forward-ignored-headers if a message is decoded. 2004-12-02 Romain Francoise <romain@orebokech.com> * lisp/gnus/message.el (message-forward-make-body-plain): Always remove headers according to message-forward-ignored-headers. 2004-11-26 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/lpath.el: Remove bbdb-create-internal, bbdb-records, spam-BBDB-register-routine and spam-enter-ham-BBDB. * lisp/gnus/nnrss.el (nnrss-string-as-multibyte): Redefine it as a macro in order to silence the byte compiler. * lisp/gnus/pop3.el (pop3-md5): Define it before being used. * lisp/gnus/spam.el: Fix the way to silence the byte compiler, which complained about bbdb-buffer, bbdb-create-internal, bbdb-search-simple, mail-check-payment, spam-BBDB-register-routine, spam-enter-ham-BBDB, spam-stat-buffer-change-to-non-spam, spam-stat-buffer-change-to-spam, spam-stat-buffer-is-non-spam, spam-stat-buffer-is-spam, spam-stat-load, spam-stat-register-ham-routine, spam-stat-register-spam-routine, spam-stat-save and spam-stat-split-fancy. 2004-11-26 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/canlock.el (canlock-password): Remove `:size 0' or `:size 1' which may confuse users. (canlock-password-for-verify): Ditto. * lisp/gnus/deuglify.el (gnus-outlook-deuglify-unwrap-stop-chars): Ditto. * lisp/gnus/gnus-art.el (gnus-emphasis-alist): Ditto. * lisp/gnus/gnus-registry.el (gnus-registry-max-entries): Ditto. * lisp/gnus/gnus-score.el (gnus-adaptive-word-length-limit): Ditto. * lisp/gnus/gnus-start.el (gnus-save-killed-list): Ditto. * lisp/gnus/gnus-sum.el (gnus-thread-hide-subtree): Ditto. (gnus-sum-thread-tree-root): Ditto. (gnus-sum-thread-tree-false-root): Ditto. (gnus-sum-thread-tree-single-indent): Ditto. * lisp/gnus/message.el (message-courtesy-message): Ditto. (message-archive-note): Ditto. (message-subscribed-address-file): Ditto. (message-user-fqdn): Ditto. * lisp/gnus/spam-report.el (spam-report-gmane-regex): Ditto. * lisp/gnus/spam.el (spam-blackhole-good-server-regex): Ditto. 2004-11-25 Reiner Steib <Reiner.Steib@gmx.de> * lisp/gnus/message.el (message-forbidden-properties): Fixed typo in doc string. 2004-11-25 Lars Magne Ingebrigtsen <larsi@gnus.org> * lisp/gnus/message.el (message-strip-forbidden-properties): Bind buffer-read-only (etc) to nil. 2004-11-25 Reiner Steib <Reiner.Steib@gmx.de> * lisp/gnus/gnus-util.el (gnus-replace-in-string): Added doc string. * lisp/gnus/nnmail.el (nnmail-split-header-length-limit): Increase to 2048 to avoid problems when splitting mails with many recipients. 2004-11-23 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/rfc2047.el (rfc2047-header-encoding-alist): Add In-Reply-To to address-mime. Suggested by ARISAWA Akihiro <ari@mbf.ocn.ne.jp>. 2004-11-22 Marek Martin <marek.martin@mum.pri.ee> (tiny change) * lisp/gnus/nnfolder.el (nnfolder-request-create-group): Save current buffer. 2004-11-22 Reiner Steib <Reiner.Steib@gmx.de> * man/message.texi (Various Message Variables): Mention that all mail file variables are derived from `message-directory'. * man/gnus.texi (Splitting Mail): Clarify bogus group. 2004-11-16 Reiner Steib <Reiner.Steib@gmx.de> * man/gnus.texi (Filtering Spam Using The Spam ELisp Package):
2004-12-07 21:56:42 +00:00
(string :tag "Password"))
:group 'canlock)
(defcustom canlock-password-for-verify canlock-password
"Password to use when verifying a Cancel-Lock or a Cancel-Key header."
:type '(radio (const :format "Not specified " nil)
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-716 Merge from gnus--rel--5.10 Patches applied: * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-74 Update from CVS 2004-12-02 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/message.el (message-forward-make-body-mml): Remove headers according to message-forward-ignored-headers if a message is decoded. 2004-12-02 Romain Francoise <romain@orebokech.com> * lisp/gnus/message.el (message-forward-make-body-plain): Always remove headers according to message-forward-ignored-headers. 2004-11-26 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/lpath.el: Remove bbdb-create-internal, bbdb-records, spam-BBDB-register-routine and spam-enter-ham-BBDB. * lisp/gnus/nnrss.el (nnrss-string-as-multibyte): Redefine it as a macro in order to silence the byte compiler. * lisp/gnus/pop3.el (pop3-md5): Define it before being used. * lisp/gnus/spam.el: Fix the way to silence the byte compiler, which complained about bbdb-buffer, bbdb-create-internal, bbdb-search-simple, mail-check-payment, spam-BBDB-register-routine, spam-enter-ham-BBDB, spam-stat-buffer-change-to-non-spam, spam-stat-buffer-change-to-spam, spam-stat-buffer-is-non-spam, spam-stat-buffer-is-spam, spam-stat-load, spam-stat-register-ham-routine, spam-stat-register-spam-routine, spam-stat-save and spam-stat-split-fancy. 2004-11-26 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/canlock.el (canlock-password): Remove `:size 0' or `:size 1' which may confuse users. (canlock-password-for-verify): Ditto. * lisp/gnus/deuglify.el (gnus-outlook-deuglify-unwrap-stop-chars): Ditto. * lisp/gnus/gnus-art.el (gnus-emphasis-alist): Ditto. * lisp/gnus/gnus-registry.el (gnus-registry-max-entries): Ditto. * lisp/gnus/gnus-score.el (gnus-adaptive-word-length-limit): Ditto. * lisp/gnus/gnus-start.el (gnus-save-killed-list): Ditto. * lisp/gnus/gnus-sum.el (gnus-thread-hide-subtree): Ditto. (gnus-sum-thread-tree-root): Ditto. (gnus-sum-thread-tree-false-root): Ditto. (gnus-sum-thread-tree-single-indent): Ditto. * lisp/gnus/message.el (message-courtesy-message): Ditto. (message-archive-note): Ditto. (message-subscribed-address-file): Ditto. (message-user-fqdn): Ditto. * lisp/gnus/spam-report.el (spam-report-gmane-regex): Ditto. * lisp/gnus/spam.el (spam-blackhole-good-server-regex): Ditto. 2004-11-25 Reiner Steib <Reiner.Steib@gmx.de> * lisp/gnus/message.el (message-forbidden-properties): Fixed typo in doc string. 2004-11-25 Lars Magne Ingebrigtsen <larsi@gnus.org> * lisp/gnus/message.el (message-strip-forbidden-properties): Bind buffer-read-only (etc) to nil. 2004-11-25 Reiner Steib <Reiner.Steib@gmx.de> * lisp/gnus/gnus-util.el (gnus-replace-in-string): Added doc string. * lisp/gnus/nnmail.el (nnmail-split-header-length-limit): Increase to 2048 to avoid problems when splitting mails with many recipients. 2004-11-23 Katsumi Yamaoka <yamaoka@jpl.org> * lisp/gnus/rfc2047.el (rfc2047-header-encoding-alist): Add In-Reply-To to address-mime. Suggested by ARISAWA Akihiro <ari@mbf.ocn.ne.jp>. 2004-11-22 Marek Martin <marek.martin@mum.pri.ee> (tiny change) * lisp/gnus/nnfolder.el (nnfolder-request-create-group): Save current buffer. 2004-11-22 Reiner Steib <Reiner.Steib@gmx.de> * man/message.texi (Various Message Variables): Mention that all mail file variables are derived from `message-directory'. * man/gnus.texi (Splitting Mail): Clarify bogus group. 2004-11-16 Reiner Steib <Reiner.Steib@gmx.de> * man/gnus.texi (Filtering Spam Using The Spam ELisp Package):
2004-12-07 21:56:42 +00:00
(string :tag "Password"))
:group 'canlock)
(defcustom canlock-force-insert-header nil
"If non-nil, insert a Cancel-Lock or a Cancel-Key header even if the
buffer does not look like a news message."
:type 'boolean
:group 'canlock)
(eval-when-compile
(defmacro canlock-string-as-unibyte (string)
"Return a unibyte string with the same individual bytes as STRING."
(if (fboundp 'string-as-unibyte)
(list 'string-as-unibyte string)
string)))
(defun canlock-sha1 (message)
"Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
(let (sha1-maximum-internal-length)
(sha1 message nil nil 'binary)))
(defun canlock-make-cancel-key (message-id password)
"Make a Cancel-Key header."
(when (> (length password) 20)
(setq password (canlock-sha1 password)))
(setq password (concat password (make-string (- 64 (length password)) 0)))
(let ((ipad (mapconcat (lambda (byte)
(char-to-string (logxor 54 byte)))
password ""))
(opad (mapconcat (lambda (byte)
(char-to-string (logxor 92 byte)))
password "")))
(base64-encode-string
(canlock-sha1
(concat opad
(canlock-sha1
(concat ipad (canlock-string-as-unibyte message-id))))))))
(defun canlock-narrow-to-header ()
"Narrow the buffer to the head of the message."
(let (case-fold-search)
(narrow-to-region
(goto-char (point-min))
(goto-char (if (re-search-forward
(format "^$\\|^%s$"
(regexp-quote mail-header-separator))
nil t)
(match-beginning 0)
(point-max))))))
(defun canlock-delete-headers ()
"Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
(let ((case-fold-search t))
(goto-char (point-min))
(while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t)
(delete-region (match-beginning 0)
(if (re-search-forward "^[^\t ]" nil t)
(goto-char (match-beginning 0))
(point-max))))))
(defun canlock-fetch-fields (&optional key)
"Return a list of the values of Cancel-Lock header.
If KEY is non-nil, look for a Cancel-Key header instead. The buffer
is expected to be narrowed to just the headers of the message."
(let ((field (mail-fetch-field (if key "Cancel-Key" "Cancel-Lock")))
fields rest
(case-fold-search t))
(when field
(setq fields (split-string field "[\t\n\r ,]+"))
(while fields
(when (string-match "^sha1:" (setq field (pop fields)))
(push (substring field 5) rest)))
(nreverse rest))))
(defun canlock-fetch-id-for-key ()
"Return a Message-ID in Cancel, Supersedes or Replaces header.
The buffer is expected to be narrowed to just the headers of the
message."
(or (let ((cancel (mail-fetch-field "Control")))
(and cancel
(string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
cancel)
(match-string 1 cancel)))
(mail-fetch-field "Supersedes")
(mail-fetch-field "Replaces")))
;;;###autoload
(defun canlock-insert-header (&optional id-for-key id-for-lock password)
"Insert a Cancel-Key and/or a Cancel-Lock header if possible."
(let (news control key-for-key key-for-lock)
(save-excursion
(save-restriction
(canlock-narrow-to-header)
(when (setq news (or canlock-force-insert-header
(mail-fetch-field "Newsgroups")))
(unless id-for-key
(setq id-for-key (canlock-fetch-id-for-key)))
(if (and (setq control (mail-fetch-field "Control"))
(string-match "^cancel[\t ]+<[^\t\n @<>]+@[^\t\n @<>]+>"
control))
(setq id-for-lock nil)
(unless id-for-lock
(setq id-for-lock (mail-fetch-field "Message-ID"))))
(canlock-delete-headers)
(goto-char (point-max))))
(when news
(if (not (or id-for-key id-for-lock))
(message "There are no Message-ID(s)")
(unless password
(setq password (or canlock-password
(read-passwd
"Password for Canlock: "))))
(if (or (not (stringp password)) (zerop (length password)))
(message "Password for Canlock is bad")
(setq key-for-key (when id-for-key
(canlock-make-cancel-key
id-for-key password))
key-for-lock (when id-for-lock
(canlock-make-cancel-key
id-for-lock password)))
(if (not (or key-for-key key-for-lock))
(message "Couldn't insert Canlock header")
(when key-for-key
(insert "Cancel-Key: sha1:" key-for-key "\n"))
(when key-for-lock
(insert "Cancel-Lock: sha1:"
(base64-encode-string (canlock-sha1 key-for-lock))
"\n")))))))))
;;;###autoload
(defun canlock-verify (&optional buffer)
"Verify Cancel-Lock or Cancel-Key in BUFFER.
If BUFFER is nil, the current buffer is assumed. Signal an error if
it fails."
(interactive)
(let (keys locks errmsg id-for-key id-for-lock password
key-for-key key-for-lock match)
(save-excursion
(when buffer
(set-buffer buffer))
(save-restriction
(widen)
(canlock-narrow-to-header)
(setq keys (canlock-fetch-fields 'key)
locks (canlock-fetch-fields))
(if (not (or keys locks))
(setq errmsg
"There are neither Cancel-Lock nor Cancel-Key headers")
(setq id-for-key (canlock-fetch-id-for-key)
id-for-lock (mail-fetch-field "Message-ID"))
(or id-for-key id-for-lock
(setq errmsg "There are no Message-ID(s)")))))
(if errmsg
(error "%s" errmsg)
(setq password (or canlock-password-for-verify
(read-passwd "Password for Canlock: ")))
(if (or (not (stringp password)) (zerop (length password)))
(error "Password for Canlock is bad")
(when keys
(when id-for-key
(setq key-for-key (canlock-make-cancel-key id-for-key password))
(while (and keys (not match))
(setq match (string-equal key-for-key (pop keys)))))
(setq keys (if match "good" "bad")))
(setq match nil)
(when locks
(when id-for-lock
(setq key-for-lock
(base64-encode-string
(canlock-sha1 (canlock-make-cancel-key id-for-lock
password))))
(when (and locks (not match))
(setq match (string-equal key-for-lock (pop locks)))))
(setq locks (if match "good" "bad")))
(prog1
(when (member "bad" (list keys locks))
"bad")
(cond ((and keys locks)
(message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
(locks
(message "Cancel-Lock is %s" locks))
(keys
(message "Cancel-Key is %s" keys))))))))
(provide 'canlock)
;;; arch-tag: 033c4f09-b9f1-459d-bd0d-254430283f78
;;; canlock.el ends here