Debian Codegraph @

mew-passwd.el

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 ;;; mew-passwd.el
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: May 23, 2006
5
6 ;;; Code:
7
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 ;;;
10 ;;; Variables
11 ;;;
12
13 (defvar mew-prog-passwd "gpg")
14 (defvar mew-passwd-file ".mew-passwd.gpg")
15 (defvar mew-passwd-cipher "AES")
16 (defvar mew-passwd-repeat 3)
17
18 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ;;;
20 ;;; Internal variables
21 ;;;
22
23 (defvar mew-passwd-encryption-name "GPG Encryption")
24 (defvar mew-passwd-decryption-name "GPG Decryption")
25
26 (defvar mew-passwd-master nil)
27 (defvar mew-passwd-alist nil)
28 (defvar mew-passwd-timer-id nil)
29 (defvar mew-passwd-rendezvous nil)
30 (defvar mew-passwd-agent-hack nil)
31
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;;;
34 ;;; External functions
35 ;;;
36
37 (defun mew-passwd-get-passwd (key)
38 (nth 1 (assoc key mew-passwd-alist)))
39
40 (defun mew-passwd-set-passwd (key val)
41 (if (assoc key mew-passwd-alist)
42 (setcar (nthcdr 1 (assoc key mew-passwd-alist)) val)
43 (setq mew-passwd-alist (cons (list key val 0) mew-passwd-alist))))
44
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 ;;;
47 ;;; Functions
48 ;;;
49
50 (defun mew-passwd-get-counter (key)
51 (nth 2 (assoc key mew-passwd-alist)))
52
53 (defun mew-passwd-set-counter (key val)
54 (if (assoc key mew-passwd-alist)
55 (setcar (nthcdr 2 (assoc key mew-passwd-alist)) val)))
56
57 (defun mew-passwd-get-keys ()
58 (mapcar 'car mew-passwd-alist))
59
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;;;
62 ;;; GPG version
63 ;;;
64
65 (defun mew-passwd-gpg-version ()
66 (when (mew-which-exec mew-prog-passwd)
67 (with-temp-buffer
68 (call-process mew-prog-passwd nil t nil "--version")
69 (goto-char (point-min))
70 (re-search-forward "(GnuPG) " nil t)
71 (if (looking-at "\\([0-9]+\\)\\.\\([0-9]+\\)")
72 (list
73 (string-to-number (match-string 1))
74 (string-to-number (match-string 2)))))))
75
76 (defun mew-passwd-check-agent-hack ()
77 (let ((ver (mew-passwd-gpg-version)))
78 (when ver
79 (let ((major (nth 0 ver))
80 (minor (nth 1 ver)))
81 (cond
82 ((= major 1) nil)
83 ((= major 2) (>= minor 1))
84 (t t))))))
85
86 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 ;;;
88 ;;; Setup and cleanup
89 ;;;
90
91 (defun mew-passwd-setup ()
92 (when (and mew-use-cached-passwd (not mew-use-master-passwd))
93 (if mew-passwd-timer-id (cancel-timer mew-passwd-timer-id))
94 (setq mew-passwd-timer-id
95 (mew-timer (* mew-passwd-timer-unit 60) 'mew-passwd-timer))))
96
97 (defun mew-passwd-setup-master ()
98 (when (and (not mew-passwd-master) mew-use-master-passwd)
99 (setq mew-passwd-agent-hack (mew-passwd-check-agent-hack))
100 (let ((file (expand-file-name mew-passwd-file mew-conf-path)))
101 (if (file-exists-p file)
102 (setq mew-passwd-alist (mew-passwd-load))
103 ;; save nil and ask master twice
104 (mew-passwd-save)))
105 (add-hook 'kill-emacs-hook 'mew-passwd-clean-up)))
106
107 (defun mew-passwd-clean-up ()
108 (remove-hook 'kill-emacs-hook 'mew-passwd-clean-up)
109 (when mew-passwd-master
110 (mew-passwd-save))
111 (setq mew-passwd-master nil)
112 (when (and mew-use-cached-passwd (not mew-use-master-passwd))
113 (setq mew-passwd-alist nil)
114 (if mew-passwd-timer-id (cancel-timer mew-passwd-timer-id))
115 (setq mew-passwd-timer-id nil)))
116
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;;;
119 ;;; Timer
120 ;;;
121
122 (defun mew-passwd-timer ()
123 (let ((keys (mew-passwd-get-keys)))
124 (dolist (key keys)
125 (if (< (mew-passwd-get-counter key) mew-passwd-lifetime)
126 (mew-passwd-set-counter key (1+ (mew-passwd-get-counter key)))
127 ;; time out
128 (mew-passwd-set-passwd key nil)
129 (mew-passwd-set-counter key 0)))))
130
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;;;
133 ;;; Input
134 ;;;
135
136 (defun mew-input-passwd (prompt key)
137 (if (and key (or mew-use-cached-passwd mew-use-master-passwd))
138 (progn
139 (mew-passwd-setup-master)
140 (if (mew-passwd-get-passwd key)
141 (progn
142 (mew-timing)
143 (if mew-passwd-reset-timer (mew-passwd-set-counter key 0))
144 (mew-passwd-get-passwd key))
145 (let ((pass (mew-read-passwd prompt)))
146 (mew-passwd-set-passwd key pass)
147 (mew-passwd-set-counter key 0)
148 pass)))
149 (mew-read-passwd prompt)))
150
151 (defun mew-read-passwd (prompt)
152 (let (;; A process filter sets inhibit-quit to t to prevent quitting.
153 ;; Set inhibit-quit to nil so that C-g can be used
154 (inhibit-quit nil))
155 (condition-case nil
156 (read-passwd prompt)
157 ;; If read-passwd causes an error, let's return "" so that
158 ;; the password process will safely fail.
159 (quit "")
160 (error ""))))
161
162 (defun mew-passwd-read-passwd (prompt &optional encrypt-p)
163 (if mew-passwd-master
164 (progn
165 (mew-timing)
166 mew-passwd-master)
167 (let ((pass (mew-read-passwd prompt)))
168 (unless encrypt-p (setq mew-passwd-master pass))
169 pass)))
170
171 (defun mew-passwd-change ()
172 "Change the master password."
173 (interactive)
174 (setq mew-passwd-master nil)
175 (mew-passwd-save))
176
177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178 ;;;
179 ;;; Load and save
180 ;;;
181
182 (defmacro mew-passwd-rendezvous (pro)
183 `(condition-case nil
184 (let ((inhibit-quit nil))
185 (setq mew-passwd-rendezvous t)
186 (while mew-passwd-rendezvous
187 (accept-process-output ,pro 0.1 nil t)))
188 (quit
189 (setq mew-passwd-rendezvous nil))))
190
191 (defun mew-passwd-load ()
192 (let* ((process-connection-type mew-connection-type2)
193 (file (expand-file-name mew-passwd-file mew-conf-path))
194 (tfile (mew-make-temp-name "gpg-load"))
195 (args (mew-passwd-adjust-args (list "-d" "--yes" "--output" tfile file)))
196 (N mew-passwd-repeat)
197 pwds pro)
198 (unwind-protect
199 (with-temp-buffer
200 (catch 'loop
201 (dotimes (_i N) ;; prevent byte-compile warning
202 (when mew-passwd-agent-hack (mew-passwd-clear-passphrase file))
203 (setq pro (apply 'mew-start-process-lang
204 mew-passwd-decryption-name
205 (current-buffer)
206 mew-prog-passwd
207 args))
208 (set-process-filter pro 'mew-passwd-filter)
209 (set-process-sentinel pro 'mew-passwd-sentinel)
210 (mew-passwd-rendezvous pro)
211 (unless (file-exists-p tfile)
212 (setq mew-passwd-master nil))
213 (when mew-passwd-master
214 (let ((coding-system-for-read 'undecided))
215 (insert-file-contents tfile))
216 (condition-case nil
217 (setq pwds (read (current-buffer)))
218 (error ()))
219 (throw 'loop nil)))
220 (message "Master password is wrong!")
221 (mew-let-user-read)))
222 (mew-passwd-delete-file tfile))
223 pwds))
224
225 (defun mew-passwd-save ()
226 (let* ((process-connection-type mew-connection-type2)
227 (file (expand-file-name mew-passwd-file mew-conf-path))
228 (tfile (mew-make-temp-name "gpg-save"))
229 (args (mew-passwd-adjust-args (list "-c"
230 "--cipher-algo" mew-passwd-cipher
231 "--yes" "--output" file tfile)))
232 (N mew-passwd-repeat)
233 pro)
234 (if (file-exists-p file)
235 (rename-file file (concat file mew-backup-suffix) 'override))
236 (unwind-protect
237 (with-temp-buffer
238 (pp mew-passwd-alist (current-buffer))
239 (write-region (point-min) (point-max) tfile nil 'no-msg)
240 (catch 'loop
241 (dotimes (_i N) ;; prevent byte-compile warning
242 (setq pro (apply
243 'mew-start-process-lang
244 mew-passwd-encryption-name
245 (current-buffer)
246 mew-prog-passwd
247 args))
248 (set-process-filter pro 'mew-passwd-filter)
249 (set-process-sentinel pro 'mew-passwd-sentinel)
250 (mew-passwd-rendezvous pro)
251 (if (file-exists-p file) (throw 'loop nil)))
252 (message "Master password is wrong! Passwords not saved")
253 (mew-let-user-read)))
254 (unless (file-exists-p file)
255 (rename-file (concat file mew-backup-suffix) file))
256 (mew-passwd-delete-file tfile))))
257
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259 ;;;
260 ;;; Filter and sentinel
261 ;;;
262
263 (defun mew-passwd-filter (process string)
264 (let* ((name (process-name process))
265 (regex (concat "^" (regexp-quote mew-passwd-encryption-name)))
266 (encrypt-p (string-match regex name)))
267 (with-current-buffer (process-buffer process)
268 (cond
269 ((string-match "invalid passphrase" string)
270 (mew-warn "Master password mismatch!")
271 (setq mew-passwd-master nil))
272 ((string-match "[bB]ad \\(?:session \\)?key" string)
273 (mew-warn "Master password is wrong!")
274 (setq mew-passwd-master nil))
275 ((string-match "Enter passphrase:" string)
276 (process-send-string process (mew-passwd-read-passwd (if encrypt-p "New master password: " "Master password: ") encrypt-p))
277 (process-send-string process "\n"))
278 ((string-match "Repeat passphrase:" string)
279 (process-send-string process (mew-passwd-read-passwd "New master password again: "))
280 (process-send-string process "\n"))
281 ((string-match "exiting" string)
282 (setq mew-passwd-rendezvous nil))))))
283
284 (defun mew-passwd-sentinel (_process _event)
285 (setq mew-passwd-rendezvous nil))
286
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ;;;
289 ;;; Sub functions
290 ;;;
291
292 (defun mew-passwd-delete-file (file)
293 (when (file-exists-p file)
294 (with-temp-buffer
295 (let ((coding-system-for-write 'binary)
296 (size (mew-file-get-size file)))
297 (dotimes (_i size) ;; prevent byte-compile warning
298 (insert 0))
299 (write-region (point-min) (point-max) file nil 'no-msg)))
300 (delete-file file)))
301
302 (defun mew-passwd-get-cache-id (file)
303 (with-temp-buffer
304 (call-process mew-prog-passwd nil t nil "--list-packets" file)
305 (goto-char (point-min))
306 (when (re-search-forward "salt \\([^ ,]+\\)," nil t)
307 (concat "S" (match-string 1)))))
308
309 (defun mew-passwd-clear-passphrase (file)
310 (when (file-exists-p file)
311 (let ((cache-id (mew-passwd-get-cache-id file)))
312 (with-temp-buffer
313 (insert "CLEAR_PASSPHRASE " cache-id "\n")
314 (call-process-region (point-min) (point-max) "gpg-connect-agent")))))
315
316 (defun mew-passwd-adjust-args (args)
317 (if mew-passwd-agent-hack
318 (cons "--pinentry-mode" (cons "loopback" args))
319 args))
320
321 (provide 'mew-passwd)
322
323 ;;; Copyright Notice:
324
325 ;; Copyright (C) 1996-2015 Mew developing team.
326 ;; All rights reserved.
327
328 ;; Redistribution and use in source and binary forms, with or without
329 ;; modification, are permitted provided that the following conditions
330 ;; are met:
331 ;;
332 ;; 1. Redistributions of source code must retain the above copyright
333 ;; notice, this list of conditions and the following disclaimer.
334 ;; 2. Redistributions in binary form must reproduce the above copyright
335 ;; notice, this list of conditions and the following disclaimer in the
336 ;; documentation and/or other materials provided with the distribution.
337 ;; 3. Neither the name of the team nor the names of its contributors
338 ;; may be used to endorse or promote products derived from this software
339 ;; without specific prior written permission.
340 ;;
341 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
342 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
343 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
344 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
345 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
346 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
347 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
348 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
349 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
350 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
351 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
352
353 ;;; mew-passwd.el ends here