2007-12-04 04:09:15 +00:00
|
|
|
;;; password-cache.el --- Read passwords, possibly using a password cache.
|
|
|
|
|
2011-01-25 04:08:28 +00:00
|
|
|
;; Copyright (C) 1999-2000, 2003-2011 Free Software Foundation, Inc.
|
2007-12-04 04:09:15 +00:00
|
|
|
|
|
|
|
;; Author: Simon Josefsson <simon@josefsson.org>
|
|
|
|
;; Created: 2003-12-21
|
|
|
|
;; Keywords: password cache passphrase key
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 08:06:51 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2007-12-04 04:09:15 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 08:06:51 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2007-12-04 04:09:15 +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
|
2008-05-06 08:06:51 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2007-12-04 04:09:15 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Greatly influenced by pgg.el written by Daiki Ueno, with timer
|
|
|
|
;; fixes for XEmacs by Katsumi Yamaoka. In fact, this is mostly just
|
|
|
|
;; a rip-off.
|
|
|
|
;;
|
|
|
|
;; (password-read "Password? " "test")
|
|
|
|
;; ;; Minibuffer prompt for password.
|
|
|
|
;; => "foo"
|
|
|
|
;;
|
|
|
|
;; (password-cache-add "test" "foo")
|
|
|
|
;; => nil
|
|
|
|
|
|
|
|
;; (password-read "Password? " "test")
|
|
|
|
;; ;; No minibuffer prompt
|
|
|
|
;; => "foo"
|
|
|
|
;;
|
|
|
|
;; (password-read "Password? " "test")
|
|
|
|
;; ;; No minibuffer prompt
|
|
|
|
;; => "foo"
|
|
|
|
;;
|
|
|
|
;; ;; Wait `password-cache-expiry' seconds.
|
|
|
|
;;
|
|
|
|
;; (password-read "Password? " "test")
|
|
|
|
;; ;; Minibuffer prompt for password is back.
|
|
|
|
;; => "foo"
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
Stop message.el from loading about 40 libraries it doesn't always need.
The general approach is to autoload rather than require, and to
require in the specific functions rather than the file. (Bug#5642)
* url/url.el: Move mailcap require earlier in the file.
* gnus/gmm-utils.el: Don't require wid-edit.
(widget-create-child-value, widget-convert, widget-default-get):
Autoload.
* gnus/gnus-util.el: Don't require time-date, netrc.
(message-fetch-field, gnus-group-name-decode): Declare rather than
autoloading.
(gnus-fetch-field): Require message.
(gnus-decode-newsgroups): Require gnus-group.
* gnus/ietf-drums.el: Don't require time-date.
* gnus/message.el: Don't require hashcash, canlock, ecomplete.
Do require mail-utils. Require nnheader only when compiling.
(smtpmail-default-smtp-server): Remove declaration.
(message-send-mail-function): Check smtpmail-default-smtp-server
is bound rather than requiring smtpmail.
(message-auto-save-directory, message-insert-signature): Use
expand-file-name rather than nnheader-concat.
(nnheader-insert-file-contents): Autoload.
(hashcash-wait-async): Declare.
(message-send-mail): Only call gnus-setup-posting-charset if
gnus-group-posting-charset-alist is bound. Require hashcash if needed.
(message-send-mail-with-sendmail): Require sendmail.
(canlock-password, canlock-password-for-verify): Declare.
(message-canlock-password): Require canlock.
(nnheader-get-report): Autoload.
(gnus-setup-posting-charset): Declare.
(message-send-news): Require gnus-msg.
(message-make-references, message-make-in-reply-to): Use mail-header-id
rather than the alias mail-header-message-id.
(ecomplete-add-item, ecomplete-save): Declare.
(message-put-addresses-in-ecomplete): Require ecomplete.
(ecomplete-display-matches): Autoload.
* gnus/mm-decode.el: Don't require mailcap, gnus-util.
(gnus-map-function, gnus-replace-in-string, gnus-read-shell-command)
(message-fetch-field, mailcap-parse-mailcaps, mailcap-mime-info):
Autoload.
(mailcap-mime-extensions): Declare.
* gnus/mm-encode.el: Don't require mailcap.
(mailcap-extension-to-mime): Autoload.
* gnus/mml-sec.el: Don't require password-cache.
* gnus/mml.el (gnus-setup-posting-charset): Declare rather than autoload.
(mailcap-parse-mimetypes, mailcap-mime-types): Declare.
(mml-minibuffer-read-type): Require mailcap.
(mml-preview): Require gnus-msg.
* gnus/mml1991.el: Require password-cache.
(password-cache-expiry): Remove declaration.
* gnus/mml2015.el: Require password-cache.
(password-cache-expiry): Remove declaration.
* gnus/nneething.el (mailcap): Require mailcap.
* gnus/nnheader.el: (declare-function): Add compatibility stub.
(message-remove-header): Declare rather than autoload.
(nnheader-replace-header): Require message.
* gnus/nnimap.el (declare-function): Add compatibility stub.
(netrc-parse, netrc-machine-user-or-password): Declare.
(nnimap-open-connection): Require netrc.
* gnus/nntp.el (declare-function): Add compatibility stub.
(netrc-parse, netrc-machine, netrc-get): Declare.
(nntp-send-authinfo): Require netrc.
* gnus/rfc2047.el: Don't require qp.
(quoted-printable-encode-region, quoted-printable-decode-string):
Autoload.
* gnus/sieve-mode.el: Don't require easymenu.
(easy-menu-add-item): Autoload it.
* gnus/spam-stat.el (time-to-number-of-days): Autoload it.
* password-cache.el (password-cache, password-cache-expiry):
Autoload.
2010-03-19 02:55:37 +00:00
|
|
|
;; Options are autoloaded since they are used by eg mml-sec.el.
|
|
|
|
|
|
|
|
;;;###autoload
|
2007-12-04 04:09:15 +00:00
|
|
|
(defcustom password-cache t
|
|
|
|
"Whether to cache passwords."
|
|
|
|
:group 'password
|
|
|
|
:type 'boolean)
|
|
|
|
|
Stop message.el from loading about 40 libraries it doesn't always need.
The general approach is to autoload rather than require, and to
require in the specific functions rather than the file. (Bug#5642)
* url/url.el: Move mailcap require earlier in the file.
* gnus/gmm-utils.el: Don't require wid-edit.
(widget-create-child-value, widget-convert, widget-default-get):
Autoload.
* gnus/gnus-util.el: Don't require time-date, netrc.
(message-fetch-field, gnus-group-name-decode): Declare rather than
autoloading.
(gnus-fetch-field): Require message.
(gnus-decode-newsgroups): Require gnus-group.
* gnus/ietf-drums.el: Don't require time-date.
* gnus/message.el: Don't require hashcash, canlock, ecomplete.
Do require mail-utils. Require nnheader only when compiling.
(smtpmail-default-smtp-server): Remove declaration.
(message-send-mail-function): Check smtpmail-default-smtp-server
is bound rather than requiring smtpmail.
(message-auto-save-directory, message-insert-signature): Use
expand-file-name rather than nnheader-concat.
(nnheader-insert-file-contents): Autoload.
(hashcash-wait-async): Declare.
(message-send-mail): Only call gnus-setup-posting-charset if
gnus-group-posting-charset-alist is bound. Require hashcash if needed.
(message-send-mail-with-sendmail): Require sendmail.
(canlock-password, canlock-password-for-verify): Declare.
(message-canlock-password): Require canlock.
(nnheader-get-report): Autoload.
(gnus-setup-posting-charset): Declare.
(message-send-news): Require gnus-msg.
(message-make-references, message-make-in-reply-to): Use mail-header-id
rather than the alias mail-header-message-id.
(ecomplete-add-item, ecomplete-save): Declare.
(message-put-addresses-in-ecomplete): Require ecomplete.
(ecomplete-display-matches): Autoload.
* gnus/mm-decode.el: Don't require mailcap, gnus-util.
(gnus-map-function, gnus-replace-in-string, gnus-read-shell-command)
(message-fetch-field, mailcap-parse-mailcaps, mailcap-mime-info):
Autoload.
(mailcap-mime-extensions): Declare.
* gnus/mm-encode.el: Don't require mailcap.
(mailcap-extension-to-mime): Autoload.
* gnus/mml-sec.el: Don't require password-cache.
* gnus/mml.el (gnus-setup-posting-charset): Declare rather than autoload.
(mailcap-parse-mimetypes, mailcap-mime-types): Declare.
(mml-minibuffer-read-type): Require mailcap.
(mml-preview): Require gnus-msg.
* gnus/mml1991.el: Require password-cache.
(password-cache-expiry): Remove declaration.
* gnus/mml2015.el: Require password-cache.
(password-cache-expiry): Remove declaration.
* gnus/nneething.el (mailcap): Require mailcap.
* gnus/nnheader.el: (declare-function): Add compatibility stub.
(message-remove-header): Declare rather than autoload.
(nnheader-replace-header): Require message.
* gnus/nnimap.el (declare-function): Add compatibility stub.
(netrc-parse, netrc-machine-user-or-password): Declare.
(nnimap-open-connection): Require netrc.
* gnus/nntp.el (declare-function): Add compatibility stub.
(netrc-parse, netrc-machine, netrc-get): Declare.
(nntp-send-authinfo): Require netrc.
* gnus/rfc2047.el: Don't require qp.
(quoted-printable-encode-region, quoted-printable-decode-string):
Autoload.
* gnus/sieve-mode.el: Don't require easymenu.
(easy-menu-add-item): Autoload it.
* gnus/spam-stat.el (time-to-number-of-days): Autoload it.
* password-cache.el (password-cache, password-cache-expiry):
Autoload.
2010-03-19 02:55:37 +00:00
|
|
|
;;;###autoload
|
2007-12-04 04:09:15 +00:00
|
|
|
(defcustom password-cache-expiry 16
|
|
|
|
"How many seconds passwords are cached, or nil to disable expiring.
|
|
|
|
Whether passwords are cached at all is controlled by `password-cache'."
|
|
|
|
:group 'password
|
|
|
|
:type '(choice (const :tag "Never" nil)
|
|
|
|
(integer :tag "Seconds")))
|
|
|
|
|
|
|
|
(defvar password-data (make-vector 7 0))
|
|
|
|
|
|
|
|
(defun password-read-from-cache (key)
|
|
|
|
"Obtain passphrase for KEY from time-limited passphrase cache.
|
|
|
|
Custom variables `password-cache' and `password-cache-expiry'
|
|
|
|
regulate cache behavior."
|
|
|
|
(and password-cache
|
|
|
|
key
|
|
|
|
(symbol-value (intern-soft key password-data))))
|
|
|
|
|
2011-03-03 13:21:50 +00:00
|
|
|
;;;###autoload
|
2011-02-25 23:52:19 +00:00
|
|
|
(defun password-in-cache-p (key)
|
|
|
|
"Check if KEY is in the cache."
|
|
|
|
(and password-cache
|
|
|
|
key
|
|
|
|
(intern-soft key password-data)))
|
|
|
|
|
2007-12-04 04:09:15 +00:00
|
|
|
(defun password-read (prompt &optional key)
|
|
|
|
"Read password, for use with KEY, from user, or from cache if wanted.
|
|
|
|
KEY indicate the purpose of the password, so the cache can
|
|
|
|
separate passwords. The cache is not used if KEY is nil. It is
|
|
|
|
typically a string.
|
|
|
|
The variable `password-cache' control whether the cache is used."
|
|
|
|
(or (password-read-from-cache key)
|
|
|
|
(read-passwd prompt)))
|
|
|
|
|
|
|
|
(defun password-read-and-add (prompt &optional key)
|
|
|
|
"Read password, for use with KEY, from user, or from cache if wanted.
|
|
|
|
Then store the password in the cache. Uses `password-read' and
|
2007-12-04 04:11:13 +00:00
|
|
|
`password-cache-add'. Custom variables `password-cache' and
|
|
|
|
`password-cache-expiry' regulate cache behavior.
|
|
|
|
|
|
|
|
Warning: the password is cached without checking that it is
|
|
|
|
correct. It is better to check the password before caching. If
|
|
|
|
you must use this function, take care to check passwords and
|
|
|
|
remove incorrect ones from the cache."
|
2007-12-04 04:09:15 +00:00
|
|
|
(let ((password (password-read prompt key)))
|
|
|
|
(when (and password key)
|
|
|
|
(password-cache-add key password))
|
|
|
|
password))
|
|
|
|
|
2007-12-04 04:11:13 +00:00
|
|
|
(make-obsolete 'password-read-and-add 'password-read "23.1")
|
|
|
|
|
2007-12-04 04:09:15 +00:00
|
|
|
(defun password-cache-remove (key)
|
|
|
|
"Remove password indexed by KEY from password cache.
|
2010-05-19 12:52:27 +00:00
|
|
|
This is typically run by a timer setup from `password-cache-add',
|
2007-12-04 04:09:15 +00:00
|
|
|
but can be invoked at any time to forcefully remove passwords
|
|
|
|
from the cache. This may be useful when it has been detected
|
|
|
|
that a password is invalid, so that `password-read' query the
|
|
|
|
user again."
|
|
|
|
(let ((password (symbol-value (intern-soft key password-data))))
|
|
|
|
(when password
|
Merge changes made in Gnus trunk.
auth.texi (Overview, Help for users, Help for developers): Update docs.
(Help for users): Talk about spaces.
sieve-manage.el: Autoload `auth-source-search'.
(sieve-sasl-auth): Use it.
nnimap.el: Autoload `auth-source-forget+'.
(nnimap-open-connection-1): Use it if the connection fails.
auth-source.el: Require `password-cache'.
(auth-source-hide-passwords, auth-source-cache): Remove and mark obsolete.
(auth-source-magic): Marker for `password-cache' keys.
(auth-source-do-cache): Update docstring.
(auth-source-search): Use and check cache.
(auth-source-forget-all-cached, auth-source-remember)
(auth-source-recall, auth-source-forget, auth-source-forget+)
(auth-source-specmatchp): Caching support functions.
(auth-source-forget-user-or-password, auth-source-forget-all-cached): Remove and obsolete.
(auth-source-user-or-password): Remove caching to further discourage using it. Always hide passwords.
password-cache.el (password-cache-remove): Accept secrets that are not strings.
mail-source.el: Autoload `auth-source-search'.
(mail-source-keyword-map): Note order matters.
(mail-source-set-1): Get all the mail-source source values and defaults and search auth-source on those if needed. This can all probably be simplified.
nnimap.el: Autoload `auth-source-search'.
(nnimap-credentials): Use it.
(nnimap-open-connection-1): Ask for the virtual server and physical address in one shot.
nntp.el: Autoload `auth-source-search'.
(nntp-send-authinfo): Use it. Note TODO.
auth-source.el (auth-source-secrets-search, auth-source-user-or-password): Use `append' instead of `nconc'.
(auth-source-user-or-password): Build return list better and protect against nil :secret.
auth-source.el (top): Require 'eieio unconditionally. Autoload `secrets-get-attributes' instead of `secrets-get-attribute'.
(auth-source-secrets-search): Limit search when `max' is greater than number of results.
auth-source.el (auth-source-secrets-search): Add examples.
auth-source.el (auth-sources): Allow for simpler defaults for Secrets API with a string "secrets:collection-name" and with 'default.
(auth-source-backend-parse): Parse "secrets:collection-name" and 'default. Recurse on parses instead of repeating code. Use the Secrets API is the source is not nil and 'ignore otherwise. Emit a message when ignoring a source.
(auth-source-search): List ignored search keys at the top level.
(auth-source-netrc-create): Use `case' instead of `cond'.
(auth-source-secrets-search): Created with TODOs.
(auth-source-secrets-create): Created with TODOs.
(auth-source-retrieve, auth-source-create, auth-source-delete)
(auth-source-protocol-defaults, auth-source-user-or-password-imap)
(auth-source-user-or-password-pop3, auth-source-user-or-password-ssh)
(auth-source-user-or-password-sftp)
(auth-source-user-or-password-smtp): Removed.
(auth-source-user-or-password): Deprecated and modified to be a wrapper around `auth-source-search'. Not tested thoroughly.
auth-source.el: Bring in assoc and eioeio libraries.
(secrets-enabled): New variable to track the status of the Secrets API.
(auth-source-backend): New EIOEIO class to represent a backend.
(auth-source-creation-defaults): New variable to set prompt defaults during token creation (see the `auth-source-search' docstring for details).
(auth-sources): Simplify to allow a simple string as a netrc backend spec.
(auth-source-backend-parse): Parse a backend from an `auth-sources' spec.
(auth-source-backend-parse-parameters): Fill in the backend parameters.
(auth-source-search): Main auth-source API entry point.
(auth-source-delete): Wrapper around `auth-source-search' for deletion.
(auth-source-search-collection): Helper function for searching.
(auth-source-netrc-parse, auth-source-netrc-normalize)
(auth-source-netrc-search, auth-source-netrc-create): Netrc backend. Supports search, create, and delete.
(auth-source-secrets-search, auth-source-secrets-create): Secrets API backend stubs.
(auth-source-user-or-password): Call `auth-source-search' but it's not ready yet.
2011-02-13 00:25:29 +00:00
|
|
|
(when (stringp password)
|
|
|
|
(if (fboundp 'clear-string)
|
|
|
|
(clear-string password)
|
|
|
|
(fillarray password ?_)))
|
2007-12-04 04:09:15 +00:00
|
|
|
(unintern key password-data))))
|
|
|
|
|
|
|
|
(defun password-cache-add (key password)
|
|
|
|
"Add password to cache.
|
2007-12-04 04:11:13 +00:00
|
|
|
The password is removed by a timer after `password-cache-expiry' seconds."
|
2007-12-04 04:09:15 +00:00
|
|
|
(when (and password-cache-expiry (null (intern-soft key password-data)))
|
|
|
|
(run-at-time password-cache-expiry nil
|
|
|
|
#'password-cache-remove
|
|
|
|
key))
|
|
|
|
(set (intern key password-data) password)
|
|
|
|
nil)
|
|
|
|
|
|
|
|
(defun password-reset ()
|
|
|
|
"Clear the password cache."
|
|
|
|
(interactive)
|
|
|
|
(fillarray password-data 0))
|
|
|
|
|
|
|
|
(provide 'password-cache)
|
|
|
|
|
|
|
|
;;; password-cache.el ends here
|