emacs/lisp/erc/erc-sasl.el

446 lines
19 KiB
EmacsLisp
Raw Normal View History

;;; erc-sasl.el --- SASL for ERC -*- lexical-binding: t -*-
2024-01-02 09:47:10 +08:00
;; Copyright (C) 2022-2024 Free Software Foundation, Inc.
;;
;; 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 the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; 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
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This "non-IRCv3" implementation resembles others that have surfaced
;; over the years, the first possibly being from Joseph Gay:
;;
;; https://lists.gnu.org/archive/html/erc-discuss/2012-02/msg00001.html
;;
; Prepare to update ERC version to 5.5 * doc/misc/erc.texi: Mention in various places that ERC is also available from GNU ELPA. * etc/ERC-NEWS: Mention Compat dependency and shorten title for auth-source section. * lisp/erc/erc-backend.el: (erc-server-reconnect-function, erc-tags-format): Update package version to 5.5. (erc--parse-message-tags): Downcase warning "type" to remain consistent with all other ERC warnings. * lisp/erc/erc-button.el: (erc-button-alist): Change package-version to 5.5. * lisp/erc/erc-match.el (erc-match-quote-when-adding): Update package version to 5.5. * lisp/erc/erc-sasl.el: Mention actual info node in Commentary. (erc-sasl): Update package version to 5.5. (erc-sasl-password): Reword doc string. (erc-sasl-auth-source-function): Capitalize "info" in doc string. * lisp/erc/erc-services.el (erc-auth-source-services-function): Update package version to 5.5. Capitalize "info" in doc string. Change choice type from const to function-item. * lisp/erc/erc.el (erc-password): Capitalize "info" in doc string. (erc-inhibit-multiline-input, erc-ask-about-multiline-input, erc-prompt-hidden, erc-hide-prompt, erc-unhide-query-prompt, erc-join-buffer, erc-reconnect-display, erc-kill-server-hook, erc-kill-channel-hook, erc-kill-buffer-hook, erc-url-connect-function): Update package version to 5.5. (erc-auth-source-server-function, erc-auth-source-join-function): Update package version to 5.5. Change choice type from const to function-item. Capitalize "info" in doc string. (erc-tls): Capitalize "info" in doc string.
2022-11-29 22:53:44 -08:00
;; See M-x customize-group RET erc-sasl RET and (info "(erc) SASL")
;; for usage.
;;
;; TODO:
;;
; Prepare to update ERC version to 5.5 * doc/misc/erc.texi: Mention in various places that ERC is also available from GNU ELPA. * etc/ERC-NEWS: Mention Compat dependency and shorten title for auth-source section. * lisp/erc/erc-backend.el: (erc-server-reconnect-function, erc-tags-format): Update package version to 5.5. (erc--parse-message-tags): Downcase warning "type" to remain consistent with all other ERC warnings. * lisp/erc/erc-button.el: (erc-button-alist): Change package-version to 5.5. * lisp/erc/erc-match.el (erc-match-quote-when-adding): Update package version to 5.5. * lisp/erc/erc-sasl.el: Mention actual info node in Commentary. (erc-sasl): Update package version to 5.5. (erc-sasl-password): Reword doc string. (erc-sasl-auth-source-function): Capitalize "info" in doc string. * lisp/erc/erc-services.el (erc-auth-source-services-function): Update package version to 5.5. Capitalize "info" in doc string. Change choice type from const to function-item. * lisp/erc/erc.el (erc-password): Capitalize "info" in doc string. (erc-inhibit-multiline-input, erc-ask-about-multiline-input, erc-prompt-hidden, erc-hide-prompt, erc-unhide-query-prompt, erc-join-buffer, erc-reconnect-display, erc-kill-server-hook, erc-kill-channel-hook, erc-kill-buffer-hook, erc-url-connect-function): Update package version to 5.5. (erc-auth-source-server-function, erc-auth-source-join-function): Update package version to 5.5. Change choice type from const to function-item. Capitalize "info" in doc string. (erc-tls): Capitalize "info" in doc string.
2022-11-29 22:53:44 -08:00
;; - Obfuscate non-auth-source passwords in memory. They're currently
;; visible in backtraces.
;;
;; - Implement a proxy mechanism that chooses the strongest available
;; mechanism for you. Requires CAP 3.2 (see bug#49860).
;;
;; - Integrate with whatever solution ERC eventually settles on to
;; handle user options for different network contexts. At the
;; moment, this does its own thing for stashing and restoring
;; session options, but ERC should make abstractions available for
;; all local modules to use, possibly based on connection-local
;; variables.
;;; Code:
(require 'erc)
(require 'rx)
(require 'sasl)
(require 'sasl-scram-rfc)
(require 'sasl-scram-sha256 nil t) ; not present in Emacs 27
(defgroup erc-sasl nil
"SASL for ERC."
:group 'erc
; Prepare to update ERC version to 5.5 * doc/misc/erc.texi: Mention in various places that ERC is also available from GNU ELPA. * etc/ERC-NEWS: Mention Compat dependency and shorten title for auth-source section. * lisp/erc/erc-backend.el: (erc-server-reconnect-function, erc-tags-format): Update package version to 5.5. (erc--parse-message-tags): Downcase warning "type" to remain consistent with all other ERC warnings. * lisp/erc/erc-button.el: (erc-button-alist): Change package-version to 5.5. * lisp/erc/erc-match.el (erc-match-quote-when-adding): Update package version to 5.5. * lisp/erc/erc-sasl.el: Mention actual info node in Commentary. (erc-sasl): Update package version to 5.5. (erc-sasl-password): Reword doc string. (erc-sasl-auth-source-function): Capitalize "info" in doc string. * lisp/erc/erc-services.el (erc-auth-source-services-function): Update package version to 5.5. Capitalize "info" in doc string. Change choice type from const to function-item. * lisp/erc/erc.el (erc-password): Capitalize "info" in doc string. (erc-inhibit-multiline-input, erc-ask-about-multiline-input, erc-prompt-hidden, erc-hide-prompt, erc-unhide-query-prompt, erc-join-buffer, erc-reconnect-display, erc-kill-server-hook, erc-kill-channel-hook, erc-kill-buffer-hook, erc-url-connect-function): Update package version to 5.5. (erc-auth-source-server-function, erc-auth-source-join-function): Update package version to 5.5. Change choice type from const to function-item. Capitalize "info" in doc string. (erc-tls): Capitalize "info" in doc string.
2022-11-29 22:53:44 -08:00
:package-version '(ERC . "5.5"))
(defcustom erc-sasl-mechanism 'plain
"SASL mechanism to connect with.
Note that any value other than nil or `external' likely requires
`erc-sasl-user' and `erc-sasl-password'."
:type '(choice (const plain)
(const external)
(const scram-sha-1)
(const scram-sha-256)
(const scram-sha-512)
(const ecdsa-nist256p-challenge)))
(defcustom erc-sasl-user :user
"Account username to send when authenticating.
This option specifies the SASL authentication identity, or
\"authcid\". A value of `:user' or `:nick' indicates that the
corresponding connection parameter on file should be used. ERC
typically obtains these from arguments given to its entry-point
commands, `erc' and `erc-tls'."
:type '(choice string (const :user) (const :nick)))
(defcustom erc-sasl-password :password
"Optional account password to send when authenticating.
; Prepare to update ERC version to 5.5 * doc/misc/erc.texi: Mention in various places that ERC is also available from GNU ELPA. * etc/ERC-NEWS: Mention Compat dependency and shorten title for auth-source section. * lisp/erc/erc-backend.el: (erc-server-reconnect-function, erc-tags-format): Update package version to 5.5. (erc--parse-message-tags): Downcase warning "type" to remain consistent with all other ERC warnings. * lisp/erc/erc-button.el: (erc-button-alist): Change package-version to 5.5. * lisp/erc/erc-match.el (erc-match-quote-when-adding): Update package version to 5.5. * lisp/erc/erc-sasl.el: Mention actual info node in Commentary. (erc-sasl): Update package version to 5.5. (erc-sasl-password): Reword doc string. (erc-sasl-auth-source-function): Capitalize "info" in doc string. * lisp/erc/erc-services.el (erc-auth-source-services-function): Update package version to 5.5. Capitalize "info" in doc string. Change choice type from const to function-item. * lisp/erc/erc.el (erc-password): Capitalize "info" in doc string. (erc-inhibit-multiline-input, erc-ask-about-multiline-input, erc-prompt-hidden, erc-hide-prompt, erc-unhide-query-prompt, erc-join-buffer, erc-reconnect-display, erc-kill-server-hook, erc-kill-channel-hook, erc-kill-buffer-hook, erc-url-connect-function): Update package version to 5.5. (erc-auth-source-server-function, erc-auth-source-join-function): Update package version to 5.5. Change choice type from const to function-item. Capitalize "info" in doc string. (erc-tls): Capitalize "info" in doc string.
2022-11-29 22:53:44 -08:00
When `erc-sasl-auth-source-function' is a function, ERC attempts
an auth-source query and prompts for input if it fails.
Otherwise, when the value of this option is a nonempty string,
ERC uses it unconditionally for most mechanisms. Likewise with a
value of `:password', except ERC instead uses the \"session
password\" on file, if any, which often originates from the
entry-point commands `erc' or `erc-tls'. As with auth-source,
ERC prompts for input as a fallback.
; Prepare to update ERC version to 5.5 * doc/misc/erc.texi: Mention in various places that ERC is also available from GNU ELPA. * etc/ERC-NEWS: Mention Compat dependency and shorten title for auth-source section. * lisp/erc/erc-backend.el: (erc-server-reconnect-function, erc-tags-format): Update package version to 5.5. (erc--parse-message-tags): Downcase warning "type" to remain consistent with all other ERC warnings. * lisp/erc/erc-button.el: (erc-button-alist): Change package-version to 5.5. * lisp/erc/erc-match.el (erc-match-quote-when-adding): Update package version to 5.5. * lisp/erc/erc-sasl.el: Mention actual info node in Commentary. (erc-sasl): Update package version to 5.5. (erc-sasl-password): Reword doc string. (erc-sasl-auth-source-function): Capitalize "info" in doc string. * lisp/erc/erc-services.el (erc-auth-source-services-function): Update package version to 5.5. Capitalize "info" in doc string. Change choice type from const to function-item. * lisp/erc/erc.el (erc-password): Capitalize "info" in doc string. (erc-inhibit-multiline-input, erc-ask-about-multiline-input, erc-prompt-hidden, erc-hide-prompt, erc-unhide-query-prompt, erc-join-buffer, erc-reconnect-display, erc-kill-server-hook, erc-kill-channel-hook, erc-kill-buffer-hook, erc-url-connect-function): Update package version to 5.5. (erc-auth-source-server-function, erc-auth-source-join-function): Update package version to 5.5. Change choice type from const to function-item. Capitalize "info" in doc string. (erc-tls): Capitalize "info" in doc string.
2022-11-29 22:53:44 -08:00
Note that, with `:password', ERC forgoes sending a traditional
server password via the IRC \"PASS\" command. Also, when
; Prepare to update ERC version to 5.5 * doc/misc/erc.texi: Mention in various places that ERC is also available from GNU ELPA. * etc/ERC-NEWS: Mention Compat dependency and shorten title for auth-source section. * lisp/erc/erc-backend.el: (erc-server-reconnect-function, erc-tags-format): Update package version to 5.5. (erc--parse-message-tags): Downcase warning "type" to remain consistent with all other ERC warnings. * lisp/erc/erc-button.el: (erc-button-alist): Change package-version to 5.5. * lisp/erc/erc-match.el (erc-match-quote-when-adding): Update package version to 5.5. * lisp/erc/erc-sasl.el: Mention actual info node in Commentary. (erc-sasl): Update package version to 5.5. (erc-sasl-password): Reword doc string. (erc-sasl-auth-source-function): Capitalize "info" in doc string. * lisp/erc/erc-services.el (erc-auth-source-services-function): Update package version to 5.5. Capitalize "info" in doc string. Change choice type from const to function-item. * lisp/erc/erc.el (erc-password): Capitalize "info" in doc string. (erc-inhibit-multiline-input, erc-ask-about-multiline-input, erc-prompt-hidden, erc-hide-prompt, erc-unhide-query-prompt, erc-join-buffer, erc-reconnect-display, erc-kill-server-hook, erc-kill-channel-hook, erc-kill-buffer-hook, erc-url-connect-function): Update package version to 5.5. (erc-auth-source-server-function, erc-auth-source-join-function): Update package version to 5.5. Change choice type from const to function-item. Capitalize "info" in doc string. (erc-tls): Capitalize "info" in doc string.
2022-11-29 22:53:44 -08:00
`erc-sasl-mechanism' is set to `ecdsa-nist256p-challenge', ERC
expects this option to hold the file name of the key."
:type '(choice (const nil) (const :password) string symbol))
(defcustom erc-sasl-auth-source-function nil
"Function to query auth-source for an SASL password.
If provided, this function should expect to be called with any
number of keyword params known to `auth-source-search', even
though ERC itself only specifies `:user' paired with a
\"resolved\" `erc-sasl-user' value. When calling this function,
ERC binds all options defined in this library, such as
`erc-sasl-password', to their values from entry-point invocation.
In return, ERC expects a string to send as the SASL password, or
; Prepare to update ERC version to 5.5 * doc/misc/erc.texi: Mention in various places that ERC is also available from GNU ELPA. * etc/ERC-NEWS: Mention Compat dependency and shorten title for auth-source section. * lisp/erc/erc-backend.el: (erc-server-reconnect-function, erc-tags-format): Update package version to 5.5. (erc--parse-message-tags): Downcase warning "type" to remain consistent with all other ERC warnings. * lisp/erc/erc-button.el: (erc-button-alist): Change package-version to 5.5. * lisp/erc/erc-match.el (erc-match-quote-when-adding): Update package version to 5.5. * lisp/erc/erc-sasl.el: Mention actual info node in Commentary. (erc-sasl): Update package version to 5.5. (erc-sasl-password): Reword doc string. (erc-sasl-auth-source-function): Capitalize "info" in doc string. * lisp/erc/erc-services.el (erc-auth-source-services-function): Update package version to 5.5. Capitalize "info" in doc string. Change choice type from const to function-item. * lisp/erc/erc.el (erc-password): Capitalize "info" in doc string. (erc-inhibit-multiline-input, erc-ask-about-multiline-input, erc-prompt-hidden, erc-hide-prompt, erc-unhide-query-prompt, erc-join-buffer, erc-reconnect-display, erc-kill-server-hook, erc-kill-channel-hook, erc-kill-buffer-hook, erc-url-connect-function): Update package version to 5.5. (erc-auth-source-server-function, erc-auth-source-join-function): Update package version to 5.5. Change choice type from const to function-item. Capitalize "info" in doc string. (erc-tls): Capitalize "info" in doc string.
2022-11-29 22:53:44 -08:00
nil, in which case, ERC prompts for input. See Info node `(erc)
auth-source' for details on ERC's auth-source integration."
:type '(choice (function-item erc-sasl-auth-source-password-as-host)
(function-item erc-auth-source-search)
(const nil)
function))
(defcustom erc-sasl-authzid nil
"SASL authorization identity, likely unneeded for everyday use."
:type '(choice (const nil) string))
;; Analogous to what erc-backend does to persist opening params.
(defvar-local erc-sasl--options nil)
;; Session-local (server buffer) SASL subproto state
(defvar-local erc-sasl--state nil)
(cl-defstruct erc-sasl--state
"Holder for client object and subproto state."
(client nil :type vector)
(step nil :type vector)
(pending nil :type string))
(defun erc-sasl--get-user ()
(pcase (alist-get 'user erc-sasl--options)
(:user erc-session-username)
(:nick (erc-current-nick))
(v v)))
(defun erc-sasl-auth-source-password-as-host (&rest plist)
"Call `erc-auth-source-search' with `erc-sasl-password' as `:host'.
But only do so when it's a string or a non-nil symbol, unless
that symbol is `:password', in which case, use a non-nil
`erc-session-password' instead. Otherwise, just defer to
`erc-auth-source-search' to pick a suitable `:host'. Expect
PLIST to contain keyword params known to `auth-source-search'."
Add helper for restoring local session vars in ERC * lisp/erc/erc-common.el (erc--input-split): Suppress warning for obsolete variable `erc-send-this' in init form. * lisp/erc/erc-goodies.el (erc--keep-place-indicator-setup): Use macro `erc--restore-initialize-priors' to preserve last session's indicator position, if any. (erc-keep-place-indicator-mode, erc-keep-place-indicator-enable): Use convenience function to show missing-dependency notice. * lisp/erc/erc-sasl.el (erc-sasl-auth-source-password-as-host): Merge redundant `when' forms for clarity. (erc-sasl--init): Remove unused function. (erc-sasl-mode, erc-sasl-enable): Use helper to restore `erc-sasl--options', essentially inlining the body of the now defunct `erc-sasl--init'. * lisp/erc/erc.el (erc--restore-initialize-priors): New macro to help local modules and mode hooks prefer existing state over initializing anew. (erc--warn-once-before-connect): Add helper function to display an "error notice" just after module setup. (erc-accidental-paste-threshold-seconds) Improve doc string. * test/lisp/erc/erc-goodies-tests.el (erc-controls-highlight--examples, erc-controls-highlight--inverse, erc-controls-highlight--motd, erc-keep-place-indicator-mode): Remove feature check. For the latter, also start fake process and shadow `erc-connect-pre-hook'. * test/lisp/erc/erc-tests.el (erc--restore-initialize-priors): New test. Also see test/lisp/erc/erc-scenarios-base-local-modules.el for a more realistic exercising of this functionality. (Bug#60936)
2023-05-07 19:43:57 -07:00
(when-let* ((erc-sasl-password)
(host (if (eq :password erc-sasl-password)
(and (not (functionp erc-session-password))
erc-session-password)
erc-sasl-password)))
(setq plist `(,@plist :host ,(format "%s" host))))
(apply #'erc-auth-source-search plist))
(defun erc-sasl--read-password (prompt)
"Return configured option or server password.
If necessary, pass PROMPT to `read-passwd'."
(if-let ((found (pcase (alist-get 'password erc-sasl--options)
((guard (alist-get 'authfn erc-sasl--options))
(let-alist erc-sasl--options
(let ((erc-sasl-user .user)
(erc-sasl-password .password)
(erc-sasl-mechanism .mechanism)
(erc-sasl-authzid .authzid)
(erc-sasl-auth-source-function .authfn))
(funcall .authfn :user (erc-sasl--get-user)))))
(:password erc-session-password)
((and (pred stringp) v) (unless (string-empty-p v) v)))))
(copy-sequence (erc--unfun found))
(read-passwd prompt)))
(defun erc-sasl--plain-response (client steps)
(let ((sasl-read-passphrase #'erc-sasl--read-password))
(sasl-plain-response client steps)))
(declare-function erc-compat--29-sasl-scram--client-final-message "erc-compat"
(hash-fun block-length hash-length client step))
(defun erc-sasl--scram-sha-hack-client-final-message (&rest args)
;; In the future (29+), we'll hopefully be able to call
;; `sasl-scram--client-final-message' directly
(require 'erc-compat)
(let ((sasl-read-passphrase #'erc-sasl--read-password))
(apply #'erc-compat--29-sasl-scram--client-final-message args)))
(defun erc-sasl--scram-sha-1-client-final-message (client step)
(erc-sasl--scram-sha-hack-client-final-message 'sha1 64 20 client step))
(defun erc-sasl--scram-sha-256-client-final-message (client step)
(erc-sasl--scram-sha-hack-client-final-message 'sasl-scram-sha256 64 32
client step))
(defun erc-sasl--scram-sha512 (object &optional start end binary)
(secure-hash 'sha512 object start end binary))
(defun erc-sasl--scram-sha-512-client-final-message (client step)
(erc-sasl--scram-sha-hack-client-final-message #'erc-sasl--scram-sha512
128 64 client step))
(defun erc-sasl--scram-sha-512-authenticate-server (client step)
(sasl-scram--authenticate-server #'erc-sasl--scram-sha512
128 64 client step))
(defun erc-sasl--ecdsa-first (client _step)
"Return CLIENT name."
(sasl-client-name client))
;; FIXME do this with gnutls somehow
(defun erc-sasl--ecdsa-sign (client step)
"Return signed challenge for CLIENT and current STEP."
(let ((challenge (sasl-step-data step)))
(with-temp-buffer
(set-buffer-multibyte nil)
(insert challenge)
(call-process-region (point-min) (point-max)
"openssl" 'delete t nil "pkeyutl" "-inkey"
(sasl-client-property client 'ecdsa-keyfile)
"-sign")
(buffer-string))))
(pcase-dolist
(`(,name . ,steps)
'(("PLAIN"
erc-sasl--plain-response)
("EXTERNAL"
ignore)
("SCRAM-SHA-1"
erc-compat--29-sasl-scram-client-first-message
erc-sasl--scram-sha-1-client-final-message
sasl-scram-sha-1-authenticate-server)
("SCRAM-SHA-256"
erc-compat--29-sasl-scram-client-first-message
erc-sasl--scram-sha-256-client-final-message
sasl-scram-sha-256-authenticate-server)
("SCRAM-SHA-512"
erc-compat--29-sasl-scram-client-first-message
erc-sasl--scram-sha-512-client-final-message
erc-sasl--scram-sha-512-authenticate-server)
("ECDSA-NIST256P-CHALLENGE"
erc-sasl--ecdsa-first
erc-sasl--ecdsa-sign)))
(let ((feature (intern (concat "erc-sasl-" (downcase name)))))
(put feature 'sasl-mechanism (sasl-make-mechanism name steps))
(provide feature)))
(cl-defgeneric erc-sasl--create-client (mechanism)
"Create and return a new SASL client object for MECHANISM."
(let ((sasl-mechanism-alist (copy-sequence sasl-mechanism-alist))
(sasl-mechanisms sasl-mechanisms)
(name (upcase (symbol-name mechanism)))
(feature (intern-soft (concat "erc-sasl-" (symbol-name mechanism))))
client)
(when feature
(setf (alist-get name sasl-mechanism-alist nil nil #'equal) `(,feature))
(cl-pushnew name sasl-mechanisms :test #'equal)
(setq client (sasl-make-client (sasl-find-mechanism (list name))
(erc-sasl--get-user)
"N/A" "N/A"))
(sasl-client-set-property client 'authenticator-name
(alist-get 'authzid erc-sasl--options))
client)))
(cl-defmethod erc-sasl--create-client ((_ (eql plain)))
"Create and return a new PLAIN client object."
;; https://tools.ietf.org/html/rfc4616#section-2.
(let* ((sans (remq (assoc "PLAIN" sasl-mechanism-alist)
sasl-mechanism-alist))
(sasl-mechanism-alist (cons '("PLAIN" erc-sasl-plain) sans))
(authc (erc-sasl--get-user))
(port (if (numberp erc-session-port)
(number-to-string erc-session-port)
"0"))
;; In most cases, `erc-server-announced-name' won't be known.
(host (or erc-server-announced-name erc-session-server))
(mech (sasl-find-mechanism '("PLAIN")))
(client (sasl-make-client mech authc port host)))
(sasl-client-set-property client 'authenticator-name
(alist-get 'authzid erc-sasl--options))
client))
(cl-defmethod erc-sasl--create-client ((_ (eql scram-sha-256)))
"Create and return a new SCRAM-SHA-256 client."
(when (featurep 'sasl-scram-sha256)
(cl-call-next-method)))
(cl-defmethod erc-sasl--create-client ((_ (eql scram-sha-512)))
"Create and return a new SCRAM-SHA-512 client."
(when (featurep 'sasl-scram-sha256)
(cl-call-next-method)))
(cl-defmethod erc-sasl--create-client ((_ (eql ecdsa-nist256p-challenge)))
"Create and return a new ECDSA-NIST256P-CHALLENGE client."
(let ((keyfile (cdr (assq 'password erc-sasl--options))))
;; Better to signal usage errors now than inside a process filter.
(cond ((or (not (stringp keyfile)) (not (file-readable-p keyfile)))
(erc-display-error-notice
nil "`erc-sasl-password' not accessible as a file")
nil)
((not (executable-find "openssl"))
(erc-display-error-notice nil "Could not find openssl program")
nil)
(t
(let ((client (cl-call-next-method)))
(sasl-client-set-property client 'ecdsa-keyfile keyfile)
client)))))
(defun erc-sasl--mechanism-offered-p (offered)
"Return non-nil when OFFERED appears among a list of mechanisms."
(string-match-p (rx-to-string
`(: (| bot ",")
,(symbol-name (alist-get 'mechanism erc-sasl--options))
(| eot ",")))
(downcase offered)))
Define ERC message-formatting templates with defvar * etc/ERC-NEWS: Mention convenience macro being preferred means of defining message templates. Mention renaming of `notify' formatting templates. * lisp/erc/erc-common.el (erc--define-catalog, erc-define-message-format-catalog): New macro and internal variant to replace `erc-define-catalog-entry'. The internal variant allows us to defer reindenting existing definitions until meaningfully edited. * lisp/erc/erc-dcc.el (erc-message-english-dcc-chat-discarded, erc-message-english-dcc-chat-ended, erc-message-english-dcc-chat-no-request, erc-message-english-dcc-chat-offered, erc-message-english-dcc-chat-offer, erc-message-english-dcc-chat-accept, erc-message-english-dcc-chat-privmsg, erc-message-english-dcc-closed, erc-message-english-dcc-command-undefined, erc-message-english-dcc-ctcp-errmsg, erc-message-english-dcc-ctcp-unknown, erc-message-english-dcc-get-bytes-received, erc-message-english-dcc-get-complete, erc-message-english-dcc-get-failed, erc-message-english-dcc-get-cmd-aborted, erc-message-english-dcc-get-file-too-long, erc-message-english-dcc-get-notfound, erc-message-english-dcc-list-head, erc-message-english-dcc-list-line, erc-message-english-dcc-list-item, erc-message-english-dcc-list-end, erc-message-english-dcc-malformed, erc-message-english-dcc-privileged-port, erc-message-english-dcc-request-bogus, erc-message-english-dcc-send-finished, erc-message-english-dcc-send-offered, erc-message-english-dcc-send-offer): Define at top level using `defvar'. * lisp/erc/erc-netsplit.el (erc-netsplit-mode, erc-netsplit-enable): Don't call `erc-netsplit-install-message-catalogs'. (erc-netsplit-install-message-catalogs): Deprecate function. (erc-message-english-netsplit, erc-message-english-netjoin, erc-message-english-netjoin-done, erc-message-english-netsplit-none, erc-message-english-netsplit-wholeft): Define at top level using `defvar'. * lisp/erc/erc-notify.el (erc-notify-install-message-catalogs): Deprecate, and rename all format templates with hyphens instead of underscores. (erc-notify-timer, erc-notify-JOIN, erc-notify-NICK, erc-notify-QUIT): Use hyphenated template names. (erc-cmd-NOTIFY): Use hyphenated template names. Load the module when necessary and emit a warning. Otherwise, people who discover this autoloaded command without being aware of the module's existence may think it's "broken". (pcomplete/erc-mode/NOTIFY): Replace top-level autoload with `require' in function body. Include `erc-notify-list' in list of completions, which makes removal easier if you don't share any channels with a person, and they're not in `erc-server-users'. A better long-term solution might be to WHOIS folks we're unsure about when they're listed in a 303. (erc-message-english-notify_current, erc-message-english-notify_list, erc-message-english-notify_on, erc-message-english-notify_off): Define at top level using `defvar'. Replace nonstandard underscores with hyphens. Alias obsolete names. * lisp/erc/erc-page.el (erc-message-english-CTCP-PAGE): Define at top level using `defvar'. * lisp/erc/erc-sasl.el (erc-message-english-s902, erc-message-english-s904, erc-message-english-s905, erc-message-english-s906, erc-message-english-s907, erc-message-english-s908): Define at top level using `defvar'. * lisp/erc/erc-sound.el (erc-message-english-CTCP-SOUND): Define using `defvar'. * lisp/erc/erc.el (erc--make-message-variable-name): New function to replace `erc-make-message-variable-name' internally, where most uses previously checked whether the returned variable was bound. This helper now does that conditionally, when asked. (erc-make-message-variable-name): Defer to internal variant, `erc--make-message-variable-name'. (erc-define-catalog-entry, erc-define-catalog): Deprecate. (erc-retrieve-catalog-entry): Refactor to favor `default-toplevel-value' of `erc-current-message-catalog' before falling back to `english'. Not doing this was arguably a bug. (erc-message-english-bad-ping-response, erc-message-english-bad-syntax, erc-message-english-incorrect-args, erc-message-english-cannot-find-file, erc-message-english-cannot-read-file, erc-message-english-connect, erc-message-english-country, erc-message-english-country-unknown, erc-message-english-ctcp-empty, erc-message-english-ctcp-request, erc-message-english-ctcp-request-to, erc-message-english-ctcp-too-many, erc-message-english-flood-ctcp-off, erc-message-english-flood-strict-mode, erc-message-english-disconnected, erc-message-english-disconnected-noreconnect, erc-message-english-reconnecting, erc-message-english-reconnect-canceled, erc-message-english-finished, erc-message-english-terminated, erc-message-english-login, erc-message-english-nick-in-use, erc-message-english-nick-too-long, erc-message-english-no-default-channel, erc-message-english-no-invitation, erc-message-english-no-target, erc-message-english-ops, erc-message-english-ops-none, erc-message-english-undefined-ctcp, erc-message-english-user-mode-redundant-add, erc-message-english-user-mode-redundant-drop, erc-message-english-variable-not-bound, erc-message-english-ACTION, erc-message-english-CTCP-CLIENTINFO, erc-message-english-CTCP-ECHO, erc-message-english-CTCP-FINGER, erc-message-english-CTCP-PING, erc-message-english-CTCP-TIME, erc-message-english-CTCP-UNKNOWN, erc-message-english-CTCP-VERSION, erc-message-english-ERROR, erc-message-english-INVITE, erc-message-english-JOIN, erc-message-english-JOIN-you, erc-message-english-KICK, erc-message-english-KICK-you, erc-message-english-KICK-by-you, erc-message-english-MODE, erc-message-english-MODE-nick, erc-message-english-NICK, erc-message-english-NICK-you, erc-message-english-PART, erc-message-english-PING, erc-message-english-PONG, erc-message-english-QUIT, erc-message-english-TOPIC, erc-message-english-WALLOPS, erc-message-english-s004, erc-message-english-s221, erc-message-english-s252, erc-message-english-s253, erc-message-english-s254, erc-message-english-s275, erc-message-english-s301, erc-message-english-s303, erc-message-english-s305, erc-message-english-s306, erc-message-english-s307, erc-message-english-s311, erc-message-english-s312, erc-message-english-s313, erc-message-english-s314, erc-message-english-s317, erc-message-english-s317-on-since, erc-message-english-s319, erc-message-english-s320, erc-message-english-s321, erc-message-english-s322, erc-message-english-s324, erc-message-english-s328, erc-message-english-s329, erc-message-english-s330, erc-message-english-s331, erc-message-english-s332, erc-message-english-s333, erc-message-english-s341, erc-message-english-s352, erc-message-english-s353, erc-message-english-s367, erc-message-english-s367-set-by, erc-message-english-s368, erc-message-english-s379, erc-message-english-s391, erc-message-english-s396, erc-message-english-s401, erc-message-english-s402, erc-message-english-s403, erc-message-english-s404, erc-message-english-s405, erc-message-english-s406, erc-message-english-s412, erc-message-english-s421, erc-message-english-s431, erc-message-english-s432, erc-message-english-s442, erc-message-english-s445, erc-message-english-s446, erc-message-english-s451, erc-message-english-s461, erc-message-english-s462, erc-message-english-s463, erc-message-english-s464, erc-message-english-s465, erc-message-english-s471, erc-message-english-s473, erc-message-english-s474, erc-message-english-s475, erc-message-english-s481, erc-message-english-s482, erc-message-english-s483, erc-message-english-s484, erc-message-english-s485, erc-message-english-s491, erc-message-english-s501, erc-message-english-s502, erc-message-english-s671): Define at top level using `defvar'. * test/lisp/erc/erc-tests.el (erc-tests--string-to-propertized-parts, erc-tests-pp-propertized-parts, erc--make-message-variable-name, erc-retrieve-catalog-entry): New tests along with utility functions and a convenience command for manipulating catalogs. (Bug#67677)
2023-11-28 16:51:36 -08:00
(erc--define-catalog english
((s902 . "ERR_NICKLOCKED nick %n unavailable: %s")
(s904 . "ERR_SASLFAIL (authentication failed) %s")
(s905 . "ERR SASLTOOLONG (credentials too long) %s")
(s906 . "ERR_SASLABORTED (authentication aborted) %s")
(s907 . "ERR_SASLALREADY (already authenticated) %s")
(s908 . "RPL_SASLMECHS (unsupported mechanism: %m) %s")))
(define-erc-module sasl nil
"Non-IRCv3 SASL support for ERC.
This doesn't solicit or validate a suite of supported mechanisms."
;; See bug#49860 for a CAP 3.2-aware WIP implementation.
((unless erc--target
Add helper for restoring local session vars in ERC * lisp/erc/erc-common.el (erc--input-split): Suppress warning for obsolete variable `erc-send-this' in init form. * lisp/erc/erc-goodies.el (erc--keep-place-indicator-setup): Use macro `erc--restore-initialize-priors' to preserve last session's indicator position, if any. (erc-keep-place-indicator-mode, erc-keep-place-indicator-enable): Use convenience function to show missing-dependency notice. * lisp/erc/erc-sasl.el (erc-sasl-auth-source-password-as-host): Merge redundant `when' forms for clarity. (erc-sasl--init): Remove unused function. (erc-sasl-mode, erc-sasl-enable): Use helper to restore `erc-sasl--options', essentially inlining the body of the now defunct `erc-sasl--init'. * lisp/erc/erc.el (erc--restore-initialize-priors): New macro to help local modules and mode hooks prefer existing state over initializing anew. (erc--warn-once-before-connect): Add helper function to display an "error notice" just after module setup. (erc-accidental-paste-threshold-seconds) Improve doc string. * test/lisp/erc/erc-goodies-tests.el (erc-controls-highlight--examples, erc-controls-highlight--inverse, erc-controls-highlight--motd, erc-keep-place-indicator-mode): Remove feature check. For the latter, also start fake process and shadow `erc-connect-pre-hook'. * test/lisp/erc/erc-tests.el (erc--restore-initialize-priors): New test. Also see test/lisp/erc/erc-scenarios-base-local-modules.el for a more realistic exercising of this functionality. (Bug#60936)
2023-05-07 19:43:57 -07:00
(setq erc-sasl--state (make-erc-sasl--state))
;; If the previous attempt failed during registration, this may be
;; non-nil and contain erroneous values, but how can we detect that?
;; What if the server dropped the connection for some other reason?
(erc--restore-initialize-priors erc-sasl-mode
erc-sasl--options `((user . ,erc-sasl-user)
(password . ,erc-sasl-password)
(mechanism . ,erc-sasl-mechanism)
(authfn . ,erc-sasl-auth-source-function)
(authzid . ,erc-sasl-authzid)))
(let* ((mech (alist-get 'mechanism erc-sasl--options))
(client (erc-sasl--create-client mech)))
(unless client
(erc-display-error-notice
Replace some uses of erc-error * lisp/erc/erc-button.el (erc-button--display-error-notice-with-keys-and-warn): Use `erc--lwarn' so the warnings buffer is overridable for testing. * lisp/erc/erc-sasl.el (erc-sasl-mode, erc-sasl-enable): Signal an `error' instead of calling `erc-error', which continues execution. In this special case, the session cannot continue initializing, since connection registration can't reasonably be expected to complete successfully. (erc-sasl--destroy): Don't run `erc-quit-hook', and issue a warning of level `:error' to get users' attention instead of calling `ding'. * lisp/erc/erc-speedbar.el (erc-speedbar--emulate-sidebar-set-window-preserve-size): Don't set window parameters. Doing this basically made `erc-speedbar-toggle-nicknames-window-lock' unusable. (erc-speedbar--toggle-nicknames-sidebar): Manually unlock the window after toggling. (erc-nickbar-mode, erc-nickbar-enable, erc-nickbar-disable): Don't `ding' when called in a non-ERC buffer, and make sure to call `erc-speedbar--ensure' from an ERC buffer. Also, don't disable minor-mode var when speedbar buffer doesn't exist because that doesn't ensure it'll be created the next time around, and users may count on the activation state remaining consistent. (erc-speedbar-toggle-nicknames-window-lock): Make usable from lisp with explicit numeric arg. * lisp/erc/erc-status-sidebar.el (erc-bufbar-mode, erc-bufbar-enable): Only create the side window from an erc-mode buffer to ensure the ratio is preserved when burying the current buffer, e.g., with `custom-buffer-done'. * lisp/erc/erc.el (erc--warnings-buffer-name, erc--lwarn): New function, an analog of `lwarn', that allows for overriding the warnings buffer with the new variable `erc--warnings-buffer-name'. (erc-cmd-SERVER): Add comment. * test/lisp/erc/erc-scenarios-sasl.el (erc-scenarios-sasl--plain-fail): Expect warning instead of error. * test/lisp/erc/erc-scenarios-status-sidebar.el (erc-scenarios-status-sidebar--bufbar): Refresh when interactive as well. * test/lisp/erc/resources/sasl/plain-failed.eld: Expect EOF instead of "CAP END". (Bug#63595)
2023-12-18 20:50:26 -08:00
nil (format "Unknown or unsupported SASL mechanism: `%s'" mech))
(error "Unknown or unsupported SASL mechanism: `%s'" mech))
(setf (erc-sasl--state-client erc-sasl--state) client))))
((kill-local-variable 'erc-sasl--state)
(kill-local-variable 'erc-sasl--options))
'local)
(define-erc-response-handler (AUTHENTICATE)
"Begin or resume an SASL session." nil
(if-let* ((response (car (erc-response.command-args parsed)))
((= 400 (length response))))
(cl-callf (lambda (s) (concat s response))
(erc-sasl--state-pending erc-sasl--state))
(cl-assert response t)
(when (string= "+" response)
(setq response ""))
(setf response (base64-decode-string
(concat (erc-sasl--state-pending erc-sasl--state)
response))
(erc-sasl--state-pending erc-sasl--state) nil)
(let ((client (erc-sasl--state-client erc-sasl--state))
(step (erc-sasl--state-step erc-sasl--state))
data)
(when step
(sasl-step-set-data step response))
(setq step (setf (erc-sasl--state-step erc-sasl--state)
(sasl-next-step client step))
data (sasl-step-data step))
(when (string= data "")
(setq data nil))
(setq data (if data (erc--unfun (base64-encode-string data t)) "+"))
(while (not (string-empty-p data))
(let ((end (min 400 (length data))))
;; For now, assume this is unlikely to block
(erc-server-send (concat "AUTHENTICATE " (substring data 0 end)))
(setq data (concat (substring data end) (and (= end 400) "+"))))))))
(defun erc-sasl--destroy (proc)
Replace some uses of erc-error * lisp/erc/erc-button.el (erc-button--display-error-notice-with-keys-and-warn): Use `erc--lwarn' so the warnings buffer is overridable for testing. * lisp/erc/erc-sasl.el (erc-sasl-mode, erc-sasl-enable): Signal an `error' instead of calling `erc-error', which continues execution. In this special case, the session cannot continue initializing, since connection registration can't reasonably be expected to complete successfully. (erc-sasl--destroy): Don't run `erc-quit-hook', and issue a warning of level `:error' to get users' attention instead of calling `ding'. * lisp/erc/erc-speedbar.el (erc-speedbar--emulate-sidebar-set-window-preserve-size): Don't set window parameters. Doing this basically made `erc-speedbar-toggle-nicknames-window-lock' unusable. (erc-speedbar--toggle-nicknames-sidebar): Manually unlock the window after toggling. (erc-nickbar-mode, erc-nickbar-enable, erc-nickbar-disable): Don't `ding' when called in a non-ERC buffer, and make sure to call `erc-speedbar--ensure' from an ERC buffer. Also, don't disable minor-mode var when speedbar buffer doesn't exist because that doesn't ensure it'll be created the next time around, and users may count on the activation state remaining consistent. (erc-speedbar-toggle-nicknames-window-lock): Make usable from lisp with explicit numeric arg. * lisp/erc/erc-status-sidebar.el (erc-bufbar-mode, erc-bufbar-enable): Only create the side window from an erc-mode buffer to ensure the ratio is preserved when burying the current buffer, e.g., with `custom-buffer-done'. * lisp/erc/erc.el (erc--warnings-buffer-name, erc--lwarn): New function, an analog of `lwarn', that allows for overriding the warnings buffer with the new variable `erc--warnings-buffer-name'. (erc-cmd-SERVER): Add comment. * test/lisp/erc/erc-scenarios-sasl.el (erc-scenarios-sasl--plain-fail): Expect warning instead of error. * test/lisp/erc/erc-scenarios-status-sidebar.el (erc-scenarios-status-sidebar--bufbar): Refresh when interactive as well. * test/lisp/erc/resources/sasl/plain-failed.eld: Expect EOF instead of "CAP END". (Bug#63595)
2023-12-18 20:50:26 -08:00
"Destroy process PROC and warn user that their settings are likely faulty."
(delete-process proc)
Replace some uses of erc-error * lisp/erc/erc-button.el (erc-button--display-error-notice-with-keys-and-warn): Use `erc--lwarn' so the warnings buffer is overridable for testing. * lisp/erc/erc-sasl.el (erc-sasl-mode, erc-sasl-enable): Signal an `error' instead of calling `erc-error', which continues execution. In this special case, the session cannot continue initializing, since connection registration can't reasonably be expected to complete successfully. (erc-sasl--destroy): Don't run `erc-quit-hook', and issue a warning of level `:error' to get users' attention instead of calling `ding'. * lisp/erc/erc-speedbar.el (erc-speedbar--emulate-sidebar-set-window-preserve-size): Don't set window parameters. Doing this basically made `erc-speedbar-toggle-nicknames-window-lock' unusable. (erc-speedbar--toggle-nicknames-sidebar): Manually unlock the window after toggling. (erc-nickbar-mode, erc-nickbar-enable, erc-nickbar-disable): Don't `ding' when called in a non-ERC buffer, and make sure to call `erc-speedbar--ensure' from an ERC buffer. Also, don't disable minor-mode var when speedbar buffer doesn't exist because that doesn't ensure it'll be created the next time around, and users may count on the activation state remaining consistent. (erc-speedbar-toggle-nicknames-window-lock): Make usable from lisp with explicit numeric arg. * lisp/erc/erc-status-sidebar.el (erc-bufbar-mode, erc-bufbar-enable): Only create the side window from an erc-mode buffer to ensure the ratio is preserved when burying the current buffer, e.g., with `custom-buffer-done'. * lisp/erc/erc.el (erc--warnings-buffer-name, erc--lwarn): New function, an analog of `lwarn', that allows for overriding the warnings buffer with the new variable `erc--warnings-buffer-name'. (erc-cmd-SERVER): Add comment. * test/lisp/erc/erc-scenarios-sasl.el (erc-scenarios-sasl--plain-fail): Expect warning instead of error. * test/lisp/erc/erc-scenarios-status-sidebar.el (erc-scenarios-status-sidebar--bufbar): Refresh when interactive as well. * test/lisp/erc/resources/sasl/plain-failed.eld: Expect EOF instead of "CAP END". (Bug#63595)
2023-12-18 20:50:26 -08:00
(erc--lwarn 'erc-sasl :error
"Disconnected from %s; please review SASL settings" proc))
(define-erc-response-handler (902)
"Handle an ERR_NICKLOCKED response." nil
(erc-display-message parsed '(notice error) 'active 's902
?n (car (erc-response.command-args parsed))
?s (erc-response.contents parsed))
(erc-sasl--destroy proc))
(define-erc-response-handler (903)
"Handle a RPL_SASLSUCCESS response." nil
(when erc-sasl-mode
(unless erc-server-connected
(erc-server-send "CAP END")))
(erc-display-message parsed 'notice proc (erc-response.contents parsed)))
(define-erc-response-handler (907)
"Handle a RPL_SASLALREADY response." nil
(erc-display-message parsed '(notice error) 'active 's907
?s (erc-response.contents parsed)))
(define-erc-response-handler (904 905 906)
"Handle various SASL-related error responses." nil
(erc-display-message parsed '(notice error) 'active
(intern (format "s%s" (erc-response.command parsed)))
?s (erc-response.contents parsed))
(erc-sasl--destroy proc))
(define-erc-response-handler (908)
"Handle a RPL_SASLMECHS response." nil
(erc-display-message parsed '(notice error) 'active 's908
?m (alist-get 'mechanism erc-sasl--options)
?s (string-join (cdr (erc-response.command-args parsed))
" "))
(erc-sasl--destroy proc))
(defvar erc-sasl--send-cap-ls nil
"Whether to send an opening \"CAP LS\" command.
This is an escape hatch for picky servers. If you need it turned
into a user option, please let ERC know via \\[erc-bug].
Otherwise, expect it to disappear in subsequent versions.")
(cl-defmethod erc--register-connection (&context (erc-sasl-mode (eql t)))
"Send speculative CAP and pipelined AUTHENTICATE and hope for the best."
(if-let* ((c (erc-sasl--state-client erc-sasl--state))
(m (sasl-mechanism-name (sasl-client-mechanism c))))
(progn
(erc-server-send (if erc-sasl--send-cap-ls "CAP LS" "CAP REQ :sasl"))
(let ((erc-session-password
(and erc-session-password
(not (eq :password
(alist-get 'password erc-sasl--options)))
erc-session-password))
(erc-session-username
;; The username may contain a colon or a space
(if (eq :user (alist-get 'user erc-sasl--options))
(erc-current-nick)
erc-session-username)))
(cl-call-next-method))
(when erc-sasl--send-cap-ls
(erc-server-send "CAP REQ :sasl"))
(erc-server-send (format "AUTHENTICATE %s" m)))
(erc-sasl--destroy erc-server-process)))
(provide 'erc-sasl)
;;; erc-sasl.el ends here
;;
;; Local Variables:
;; generated-autoload-file: "erc-loaddefs.el"
;; End: