Line data Source code
1 : ;;; password-cache.el --- Read passwords, possibly using a password cache.
2 :
3 : ;; Copyright (C) 1999-2000, 2003-2017 Free Software Foundation, Inc.
4 :
5 : ;; Author: Simon Josefsson <simon@josefsson.org>
6 : ;; Created: 2003-12-21
7 : ;; Keywords: password cache passphrase key
8 :
9 : ;; This file is part of GNU Emacs.
10 :
11 : ;; GNU Emacs is free software: you can redistribute it and/or modify
12 : ;; it under the terms of the GNU General Public License as published by
13 : ;; the Free Software Foundation, either version 3 of the License, or
14 : ;; (at your option) any later version.
15 :
16 : ;; GNU Emacs is distributed in the hope that it will be useful,
17 : ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : ;; GNU General Public License for more details.
20 :
21 : ;; You should have received a copy of the GNU General Public License
22 : ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 :
24 : ;;; Commentary:
25 :
26 : ;; Greatly influenced by pgg.el written by Daiki Ueno, with timer
27 : ;; fixes for XEmacs by Katsumi Yamaoka. In fact, this is mostly just
28 : ;; a rip-off.
29 : ;;
30 : ;; (password-read "Password? " "test")
31 : ;; ;; Minibuffer prompt for password.
32 : ;; => "foo"
33 : ;;
34 : ;; (password-cache-add "test" "foo")
35 : ;; => nil
36 :
37 : ;; (password-read "Password? " "test")
38 : ;; ;; No minibuffer prompt
39 : ;; => "foo"
40 : ;;
41 : ;; (password-read "Password? " "test")
42 : ;; ;; No minibuffer prompt
43 : ;; => "foo"
44 : ;;
45 : ;; ;; Wait `password-cache-expiry' seconds.
46 : ;;
47 : ;; (password-read "Password? " "test")
48 : ;; ;; Minibuffer prompt for password is back.
49 : ;; => "foo"
50 :
51 : ;;; Code:
52 :
53 : ;; Options are autoloaded since they are used by eg mml-sec.el.
54 :
55 : ;;;###autoload
56 : (defcustom password-cache t
57 : "Whether to cache passwords."
58 : :group 'password
59 : :type 'boolean)
60 :
61 : ;;;###autoload
62 : (defcustom password-cache-expiry 16
63 : "How many seconds passwords are cached, or nil to disable expiring.
64 : Whether passwords are cached at all is controlled by `password-cache'."
65 : :group 'password
66 : :type '(choice (const :tag "Never" nil)
67 : (integer :tag "Seconds")))
68 :
69 : (defvar password-data (make-hash-table :test #'equal))
70 :
71 : (defun password-read-from-cache (key)
72 : "Obtain passphrase for KEY from time-limited passphrase cache.
73 : Custom variables `password-cache' and `password-cache-expiry'
74 : regulate cache behavior."
75 0 : (and password-cache
76 0 : key
77 0 : (gethash key password-data)))
78 :
79 : ;;;###autoload
80 : (defun password-in-cache-p (key)
81 : "Check if KEY is in the cache."
82 0 : (and password-cache
83 0 : key
84 0 : (gethash key password-data)))
85 :
86 : (defun password-read (prompt &optional key)
87 : "Read password, for use with KEY, from user, or from cache if wanted.
88 : KEY indicate the purpose of the password, so the cache can
89 : separate passwords. The cache is not used if KEY is nil.
90 : KEY is typically a string but can be anything (compared via `equal').
91 : The variable `password-cache' control whether the cache is used."
92 0 : (or (password-read-from-cache key)
93 0 : (read-passwd prompt)))
94 :
95 : (defun password-read-and-add (prompt &optional key)
96 : "Read password, for use with KEY, from user, or from cache if wanted.
97 : Then store the password in the cache. Uses `password-read' and
98 : `password-cache-add'. Custom variables `password-cache' and
99 : `password-cache-expiry' regulate cache behavior.
100 :
101 : Warning: the password is cached without checking that it is
102 : correct. It is better to check the password before caching. If
103 : you must use this function, take care to check passwords and
104 : remove incorrect ones from the cache."
105 : (declare (obsolete password-read "23.1"))
106 0 : (let ((password (password-read prompt key)))
107 0 : (when (and password key)
108 0 : (password-cache-add key password))
109 0 : password))
110 :
111 : (defun password-cache-remove (key)
112 : "Remove password indexed by KEY from password cache.
113 : This is typically run by a timer setup from `password-cache-add',
114 : but can be invoked at any time to forcefully remove passwords
115 : from the cache. This may be useful when it has been detected
116 : that a password is invalid, so that `password-read' query the
117 : user again."
118 4 : (let ((password (gethash key password-data)))
119 4 : (when (stringp password)
120 0 : (if (fboundp 'clear-string)
121 0 : (clear-string password)
122 4 : (fillarray password ?_)))
123 4 : (remhash key password-data)))
124 :
125 : (defun password-cache-add (key password)
126 : "Add password to cache.
127 : The password is removed by a timer after `password-cache-expiry' seconds."
128 0 : (when (and password-cache-expiry (null (gethash key password-data)))
129 0 : (run-at-time password-cache-expiry nil
130 0 : #'password-cache-remove
131 0 : key))
132 0 : (puthash key password password-data)
133 : nil)
134 :
135 : (defun password-reset ()
136 : "Clear the password cache."
137 : (interactive)
138 11 : (clrhash password-data))
139 :
140 : (provide 'password-cache)
141 :
142 : ;;; password-cache.el ends here
|