1 ;;; password.el --- Read passwords from user, possibly using a password cache.
3 ;; Copyright (C) 1999, 2000, 2003 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: password cache passphrase key
9 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Greatly influenced by pgg.el written by Daiki Ueno, with timer
29 ;; fixes for XEmacs by Katsumi Yamaoka. In fact, this is mostly just
32 ;; (password-read "Password? " "test")
33 ;; ;; Minibuffer prompt for password.
36 ;; (password-cache-add "test" "foo")
39 ;; (password-read "Password? " "test")
40 ;; ;; No minibuffer prompt
43 ;; (password-read "Password? " "test")
44 ;; ;; No minibuffer prompt
47 ;; ;; Wait `password-cache-expiry' seconds.
49 ;; (password-read "Password? " "test")
50 ;; ;; Minibuffer prompt for password is back.
55 (autoload 'run-at-time "timer")
60 (defcustom password-cache t
61 "Whether to cache passwords."
65 (defcustom password-cache-expiry 16
66 "How many seconds passwords are cached, or nil to disable expiring.
67 Whether passwords are cached at all is controlled by `password-cache'."
69 :type '(choice (const :tag "Never" nil)
70 (integer :tag "Seconds")))
72 (defvar password-data (make-vector 7 0))
74 (defun password-read (prompt &optional key)
75 "Read password, for use with KEY, from user, or from cache if wanted.
76 KEY indicate the purpose of the password, so the cache can
77 separate passwords. The cache is not used if KEY is nil. It is
79 The variable `password-cache' control whether the cache is used."
80 (or (and password-cache
82 (symbol-value (intern-soft key password-data)))
83 (read-passwd prompt)))
86 (defvar itimer-process)
88 (autoload 'delete-itimer "itimer")
89 (autoload 'itimer-driver-start "itimer")
90 (autoload 'itimer-value "itimer")
91 (autoload 'set-itimer-function "itimer")
92 (autoload 'set-itimer-function-arguments "itimer")
93 (autoload 'set-itimer-restart "itimer")
94 (autoload 'start-itimer "itimer"))
99 (if (featurep 'xemacs)
100 (if (condition-case nil
102 (unless (or itimer-process itimer-timer)
103 (itimer-driver-start))
104 ;; Check whether there is a bug to which the difference of
105 ;; the present time and the time when the itimer driver was
106 ;; woken up is subtracted from the initial itimer value.
107 (let* ((inhibit-quit t)
108 (ctime (current-time))
109 (itimer-timer-last-wakeup
112 (setcar ctime (1- (car ctime)))))
114 (itimer (start-itimer "password-run-at-time" 'ignore 5)))
115 (sleep-for 0.1) ;; Accept the timeout interrupt.
117 (> (itimer-value itimer) 0)
118 (delete-itimer itimer))))
120 (lambda (time repeat function &rest args)
121 "Emulating function run as `run-at-time'.
122 TIME should be nil meaning now, or a number of seconds from now.
123 Return an itimer object which can be used in either `delete-itimer'
125 (apply #'start-itimer "password-run-at-time"
126 function (if time (max time 1e-9) 1e-9)
128 (lambda (time repeat function &rest args)
129 "Emulating function run as `run-at-time' in the right way.
130 TIME should be nil meaning now, or a number of seconds from now.
131 Return an itimer object which can be used in either `delete-itimer'
133 (let ((itimers (list nil)))
136 (apply #'start-itimer "password-run-at-time"
137 (lambda (itimers repeat function &rest args)
138 (let ((itimer (car itimers)))
143 (lambda (itimer repeat function &rest args)
144 (set-itimer-restart itimer repeat)
145 (set-itimer-function itimer function)
146 (set-itimer-function-arguments itimer args)
147 (apply function args)))
148 (set-itimer-function-arguments
150 (append (list itimer repeat function) args)))
153 (lambda (itimer function &rest args)
154 (delete-itimer itimer)
155 (apply function args)))
156 (set-itimer-function-arguments
158 (append (list itimer function) args)))))
159 1e-9 (if time (max time 1e-9) 1e-9)
160 nil t itimers repeat function args)))))
163 (defun password-cache-remove (key)
164 "Remove password indexed by KEY from password cache.
165 This is typically run be a timer setup from `password-cache-add',
166 but can be invoked at any time to forcefully remove passwords
167 from the cache. This may be useful when it has been detected
168 that a password is invalid, so that `password-read' query the
170 (let ((password (symbol-value (intern-soft key password-data))))
172 (fillarray password ?_)
173 (unintern key password-data))))
175 (defun password-cache-add (key password)
176 "Add password to cache.
177 The password is removed by a timer after `password-cache-expiry'
179 (set (intern key password-data) password)
180 (when password-cache-expiry
181 (password-run-at-time password-cache-expiry nil
182 #'password-cache-remove
188 ;;; password.el ends here