2019-06-09 16:18:46 +00:00
|
|
|
;;; password-cache.el --- Read passwords, possibly using a password cache. -*- lexical-binding: t -*-
|
2007-12-04 04:09:15 +00:00
|
|
|
|
2019-01-01 00:59:58 +00:00
|
|
|
;; Copyright (C) 1999-2000, 2003-2019 Free Software Foundation, Inc.
|
2007-12-04 04:09:15 +00:00
|
|
|
|
|
|
|
;; Author: Simon Josefsson <simon@josefsson.org>
|
|
|
|
;; Created: 2003-12-21
|
2019-06-09 16:18:46 +00:00
|
|
|
;; Keywords: extensions
|
2007-12-04 04:09:15 +00:00
|
|
|
|
|
|
|
;; 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
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://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")))
|
|
|
|
|
2017-07-28 16:27:00 +00:00
|
|
|
(defvar password-data (make-hash-table :test #'equal))
|
2007-12-04 04:09:15 +00:00
|
|
|
|
|
|
|
(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
|
2017-07-28 16:27:00 +00:00
|
|
|
(gethash key password-data)))
|
2007-12-04 04:09:15 +00:00
|
|
|
|
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
|
2019-08-11 23:42:16 +00:00
|
|
|
(not (eq (gethash key password-data 'password-cache-no-data)
|
|
|
|
'password-cache-no-data))))
|
2011-02-25 23:52:19 +00:00
|
|
|
|
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
|
2017-07-28 16:27:00 +00:00
|
|
|
separate passwords. The cache is not used if KEY is nil.
|
|
|
|
KEY is typically a string but can be anything (compared via `equal').
|
2007-12-04 04:09:15 +00:00
|
|
|
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."
|
Use declare forms, where possible, to mark obsolete functions.
* lisp/allout.el (allout-passphrase-hint-string): Likewise.
(allout-init): Use a declare form to mark obsolete.
* lisp/calendar/calendar.el (calendar-version):
* lisp/calendar/icalendar.el (icalendar-extract-ical-from-buffer)
(icalendar-convert-diary-to-ical):
* lisp/cus-edit.el (custom-mode):
* lisp/ansi-color.el (ansi-color-unfontify-region):
* lisp/international/latin1-disp.el (latin1-char-displayable-p):
* lisp/progmodes/cwarn.el (turn-on-cwarn-mode):
* lisp/progmodes/which-func.el (which-func-update-1): Use
define-obsolete-function-alias.
* lisp/bookmark.el (bookmark-jump-noselect): Use a declare form to mark
this function obsolete.
* lisp/calendar/cal-x.el (calendar-two-frame-setup)
(calendar-only-one-frame-setup, calendar-one-frame-setup):
* lisp/calendar/calendar.el (american-calendar, european-calendar)
(calendar-for-loop):
* lisp/comint.el (comint-dynamic-simple-complete)
(comint-dynamic-complete-as-filename, comint-unquote-filename):
* lisp/desktop.el (desktop-load-default):
* lisp/dired-x.el (dired-omit-here-always)
(dired-hack-local-variables, dired-default-directory):
* lisp/emacs-lisp/derived.el (derived-mode-class):
* lisp/emacs-lisp/timer.el (timer-set-time-with-usecs):
* lisp/emacs-lock.el (toggle-emacs-lock):
* lisp/epa.el (epa-display-verify-result):
* lisp/epg.el (epg-sign-keys, epg-start-sign-keys)
(epg-passphrase-callback-function):
* lisp/eshell/esh-util.el (eshell-for):
* lisp/eshell/eshell.el (eshell-remove-from-window-buffer-names)
(eshell-add-to-window-buffer-names):
* lisp/files.el (locate-file-completion):
* lisp/imenu.el (imenu-example--create-c-index)
(imenu-example--create-lisp-index)
(imenu-example--lisp-extract-index-name)
(imenu-example--name-and-position):
* lisp/international/mule-cmds.el (princ-list):
* lisp/international/mule-diag.el (decode-codepage-char):
* lisp/international/mule-util.el (detect-coding-with-priority):
* lisp/iswitchb.el (iswitchb-read-buffer):
* lisp/mail/mailalias.el (mail-complete):
* lisp/mail/sendmail.el (mail-sent-via):
* lisp/mouse.el (mouse-popup-menubar-stuff, mouse-popup-menubar)
(mouse-major-mode-menu):
* lisp/password-cache.el (password-read-and-add):
* lisp/pcomplete.el (pcomplete-parse-comint-arguments):
* lisp/progmodes/sh-script.el (sh-maybe-here-document):
* lisp/replace.el (query-replace-regexp-eval):
* lisp/savehist.el (savehist-load):
* lisp/simple.el (choose-completion-delete-max-match):
* lisp/term.el (term-dynamic-simple-complete):
* lisp/vc/ediff-init.el (ediff-check-version):
* lisp/vc/ediff-wind.el (ediff-choose-window-setup-function-automatically):
* lisp/vc/vc.el (vc-diff-switches-list):
* lisp/view.el (view-return-to-alist-update): Likewise.
* lisp/iswitchb.el (iswitchb-read-buffer): Move code of
iswitchb-define-mode-map here, and delete that obsolete function.
* lisp/subr.el (eval-next-after-load, makehash, insert-string)
(assoc-ignore-representation, assoc-ignore-case): Use declare to
mark obsolete.
(mode-line-inverse-video): Variable deleted.
* lisp/emacs-lisp/byte-run.el (make-obsolete): Doc fix; emphasize that
this applies to functions.
* lisp/erc/erc.el (erc-send-command): Use define-obsolete-function-alias.
* lisp/international/mule-util.el (string-to-sequence): Remove.
* lisp/net/newst-backend.el (newsticker-cache-filename):
* lisp/net/newst-treeview.el (newsticker-groups-filename): Fix
incorrect obsolescence declaration.
* lisp/net/snmp-mode.el (snmp-font-lock-keywords-3): Don't use obsolete
font-lock-reference-face.
* lisp/url/url-parse.el (url-recreate-url-attributes):
* lisp/url/url-util.el (url-generate-unique-filename): Use declare to mark
obsolete.
* src/xdisp.c (mode_line_inverse_video): Delete obsolete variable.
2012-09-25 04:13:02 +00:00
|
|
|
(declare (obsolete password-read "23.1"))
|
2007-12-04 04:09:15 +00:00
|
|
|
(let ((password (password-read prompt key)))
|
|
|
|
(when (and password key)
|
|
|
|
(password-cache-add key password))
|
|
|
|
password))
|
|
|
|
|
|
|
|
(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."
|
2017-07-28 16:27:00 +00:00
|
|
|
(let ((password (gethash key password-data)))
|
|
|
|
(when (stringp password)
|
|
|
|
(if (fboundp 'clear-string)
|
|
|
|
(clear-string password)
|
|
|
|
(fillarray password ?_)))
|
|
|
|
(remhash key password-data)))
|
2007-12-04 04:09:15 +00:00
|
|
|
|
|
|
|
(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."
|
2019-08-11 23:42:16 +00:00
|
|
|
(when (and password-cache-expiry
|
|
|
|
(eq (gethash key password-data 'password-cache-no-data)
|
|
|
|
'password-cache-no-data))
|
2007-12-04 04:09:15 +00:00
|
|
|
(run-at-time password-cache-expiry nil
|
|
|
|
#'password-cache-remove
|
|
|
|
key))
|
2017-07-28 16:27:00 +00:00
|
|
|
(puthash key password password-data)
|
2007-12-04 04:09:15 +00:00
|
|
|
nil)
|
|
|
|
|
|
|
|
(defun password-reset ()
|
|
|
|
"Clear the password cache."
|
|
|
|
(interactive)
|
2017-07-28 16:27:00 +00:00
|
|
|
(clrhash password-data))
|
2007-12-04 04:09:15 +00:00
|
|
|
|
|
|
|
(provide 'password-cache)
|
|
|
|
|
|
|
|
;;; password-cache.el ends here
|