password-cache.el 4.7 KB

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