Debian Codegraph @

mew-passwd.el

raw ◀ ▶
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 (cond
39 ((mew-passwd-use-auth-source-p)
40 (mew-passwd-auth-source-get-passwd key))
41 (t (nth 1 (assoc key mew-passwd-alist)))))
42
43 (defun mew-passwd-set-passwd (key val)
•44 (cond
45 ((mew-passwd-use-auth-source-p)
46 (mew-passwd-auth-source-set-passwd key val))
47 (t (if (assoc key mew-passwd-alist)
48 (setcar (nthcdr 1 (assoc key mew-passwd-alist)) val)
49 (setq mew-passwd-alist (cons (list key val 0) mew-passwd-alist))))))
50
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
52 ;;;
53 ;;; Functions for auth-source backend
54 ;;;
55
56 (autoload 'auth-source-search "auth-source")
57
58 (defun mew-passwd-use-auth-source-p ()
59 (eq mew-master-passwd-type 'auth-source))
60
61 (defun mew-passwd-auth-source-parse-key (key)
62 (let ((st (split-string key "[@:]")))
63 (list :user (nth 0 st)
64 :host (nth 1 st)
65 :port (nth 2 st))))
66
67 ;;; XXX will be revisited.
68 (defun mew-passwd-auth-source-parse-key-x (key)
69 (if (string-match "\\([^@]+\\)@\\([^:]+\\):\\(.+\\)" key)
70 (list :user (match-string 1 key)
71 :host (match-string 2 key)
72 :port (match-string 3 key))
73 (list :key key
74 :user nil
75 :host nil
76 :port nil)))
77
78 ;;; XXX epa-pinentry-mode is set to 'loopback to avoid GUI pinentry
79 ;;; window. This might not be a recommended way but it works.
80 ;;;
81 ;;; XXX when the backend is an encrypted file and an wrong passphrase
82 ;;; is provided, a password prompt will be displayed. This may be
83 ;;; confusing.
84 (defun mew-passwd-auth-source-get-passwd (key)
85 (let* ((epa-pinentry-mode 'loopback)
86 (slist (mew-passwd-auth-source-parse-key key))
87 found)
88 (cond
89 ((member key mew-pgp-list)
90 ;; Never get GnuPG passphrase. Rely on gpg-agent for caching.
91 (setq found nil))
92 (t
93 (condition-case error
94 (setq found (apply #'auth-source-search (nconc slist)))
95 (error
96 (setq found nil)))))
97 (if found
98 (let ((secret (plist-get (nth 0 found) :secret)))
99 (if (functionp secret)
100 (funcall secret)
101 secret))
102 nil)))
103
104 (defun mew-passwd-auth-source-set-passwd (key val)
105 (cond
106 ((member key mew-pgp-list)
107 ;; Never store GnuPG passphrase. Rely on gpg-agent for caching.
108 )
109 (val
110 ;; Authenticated successfully.
111 (let* ((epa-pinentry-mode 'loopback)
112 (slist (mew-passwd-auth-source-parse-key key))
113 (entry (apply #'auth-source-search
114 (append slist (list
115 :secret val :create t))))
116 (save-function (plist-get (nth 0 entry) :save-function)))
117 (when (functionp save-function)
118 (funcall save-function))))
119 (t
120 ;; Authentication failed.
121 ;;
122 ;; XXX: removal of the wrong password is not supported by any
123 ;; backends (i.e. auth-source-delete is noop).
124 (let* ((epa-pinentry-mode 'loopback)
125 (slist (mew-passwd-auth-source-parse-key key))
126 (num (apply #'auth-source-forget+ (nconc slist)))
127 (entry (apply #'auth-source-delete (nconc slist)))
128 (save-function (plist-get (nth 0 entry) :save-function)))
129 (when (functionp save-function)
130 (funcall save-function))
131 (message "Wrong password is stored. Please remove it manually.")))))
132
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;;;
135 ;;; Functions
136 ;;;
137
138 (defun mew-passwd-get-counter (key)
139 (nth 2 (assoc key mew-passwd-alist)))
140
141 (defun mew-passwd-set-counter (key val)
142 (if (assoc key mew-passwd-alist)
143 (setcar (nthcdr 2 (assoc key mew-passwd-alist)) val)))
144
145 (defun mew-passwd-get-keys ()
146 (mapcar 'car mew-passwd-alist))
147
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;;;
150 ;;; GPG version
151 ;;;
152
153 (defun mew-passwd-gpg-version ()
154 (when (mew-which-exec mew-prog-passwd)
155 (with-temp-buffer
156 (call-process mew-prog-passwd nil t nil "--version")
157 (goto-char (point-min))
158 (re-search-forward "(GnuPG) " nil t)
159 (if (looking-at "\\([0-9]+\\)\\.\\([0-9]+\\)")
160 (list
161 (string-to-number (match-string 1))
162 (string-to-number (match-string 2)))))))
163
164 (defun mew-passwd-check-agent-hack ()
165 (let ((ver (mew-passwd-gpg-version)))
166 (when ver
167 (let ((major (nth 0 ver))
168 (minor (nth 1 ver)))
169 (cond
170 ((= major 1) nil)
171 ((= major 2) (>= minor 1))
172 (t t))))))
173
174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 ;;;
176 ;;; Setup and cleanup
177 ;;;
178
179 (defun mew-passwd-setup ()
180 (when (and mew-use-cached-passwd (not mew-use-master-passwd))
181 (if mew-passwd-timer-id (cancel-timer mew-passwd-timer-id))
182 (setq mew-passwd-timer-id
183 (mew-timer (* mew-passwd-timer-unit 60) 'mew-passwd-timer))))
184
185 (defun mew-passwd-setup-master ()
•186 (cond
187 ((mew-passwd-use-auth-source-p)
188 ;;; do nothing
189 )
190 (t
191 (when (and (not mew-passwd-master) mew-use-master-passwd)
192 (setq mew-passwd-agent-hack (mew-passwd-check-agent-hack))
193 (let ((file (expand-file-name mew-passwd-file mew-conf-path)))
194 (if (file-exists-p file)
195 (setq mew-passwd-alist (mew-passwd-load))
196 ;; save nil and ask master twice
197 (mew-passwd-save)))
•198 (add-hook 'kill-emacs-hook 'mew-passwd-clean-up)))))
199
200 (defun mew-passwd-clean-up ()
201 (remove-hook 'kill-emacs-hook 'mew-passwd-clean-up)
202 (when mew-passwd-master
203 (mew-passwd-save))
204 (setq mew-passwd-master nil)
205 (when (and mew-use-cached-passwd (not mew-use-master-passwd))
206 (setq mew-passwd-alist nil)
207 (if mew-passwd-timer-id (cancel-timer mew-passwd-timer-id))
208 (setq mew-passwd-timer-id nil)))
209
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ;;;
212 ;;; Timer
213 ;;;
214
215 (defun mew-passwd-timer ()
216 (let ((keys (mew-passwd-get-keys)))
217 (dolist (key keys)
218 (if (< (mew-passwd-get-counter key) mew-passwd-lifetime)
219 (mew-passwd-set-counter key (1+ (mew-passwd-get-counter key)))
220 ;; time out
221 (mew-passwd-set-passwd key nil)
222 (mew-passwd-set-counter key 0)))))
223
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225 ;;;
226 ;;; Input
227 ;;;
228
229 (defun mew-input-passwd (prompt key)
230 (if (and key (or mew-use-cached-passwd mew-use-master-passwd))
231 (progn
232 (mew-passwd-setup-master)
233 (if (mew-passwd-get-passwd key)
234 (progn
235 (mew-timing)
236 (if mew-passwd-reset-timer (mew-passwd-set-counter key 0))
237 (mew-passwd-get-passwd key))
238 (let ((pass (mew-read-passwd prompt)))
239 (mew-passwd-set-passwd key pass)
240 (mew-passwd-set-counter key 0)
241 pass)))
242 (mew-read-passwd prompt)))
243
244 (defun mew-read-passwd (prompt)
245 (let (;; A process filter sets inhibit-quit to t to prevent quitting.
246 ;; Set inhibit-quit to nil so that C-g can be used
247 (inhibit-quit nil))
248 (condition-case nil
249 (read-passwd prompt)
250 ;; If read-passwd causes an error, let's return "" so that
251 ;; the password process will safely fail.
252 (quit "")
253 (error ""))))
254
255 (defun mew-passwd-read-passwd (prompt &optional encrypt-p)
256 (if mew-passwd-master
257 (progn
258 (mew-timing)
259 mew-passwd-master)
260 (let ((pass (mew-read-passwd prompt)))
261 (unless encrypt-p (setq mew-passwd-master pass))
262 pass)))
263
264 (defun mew-passwd-change ()
265 "Change the master password."
266 (interactive)
267 (setq mew-passwd-master nil)
268 (mew-passwd-save))
269
270 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271 ;;;
272 ;;; Load and save
273 ;;;
274
275 (defmacro mew-passwd-rendezvous (pro)
276 `(condition-case nil
277 (let ((inhibit-quit nil))
278 (setq mew-passwd-rendezvous t)
279 (while mew-passwd-rendezvous
280 (accept-process-output ,pro 0.1 nil t)))
281 (quit
282 (setq mew-passwd-rendezvous nil))))
283
284 (defun mew-passwd-load ()
285 (let* ((process-connection-type mew-connection-type2)
286 (file (expand-file-name mew-passwd-file mew-conf-path))
287 (tfile (mew-make-temp-name "gpg-load"))
288 (args (mew-passwd-adjust-args (list "-d" "--yes" "--output" tfile file)))
289 (N mew-passwd-repeat)
290 pwds pro)
291 (unwind-protect
292 (with-temp-buffer
293 (catch 'loop
294 (dotimes (_i N) ;; prevent byte-compile warning
295 (when mew-passwd-agent-hack (mew-passwd-clear-passphrase file))
296 (setq pro (apply 'mew-start-process-lang
297 mew-passwd-decryption-name
298 (current-buffer)
299 mew-prog-passwd
300 args))
301 (set-process-filter pro 'mew-passwd-filter)
302 (set-process-sentinel pro 'mew-passwd-sentinel)
303 (mew-passwd-rendezvous pro)
304 (unless (file-exists-p tfile)
305 (setq mew-passwd-master nil))
306 (when mew-passwd-master
307 (let ((coding-system-for-read 'undecided))
308 (insert-file-contents tfile))
309 (condition-case nil
310 (setq pwds (read (current-buffer)))
311 (error ()))
312 (throw 'loop nil)))
313 (message "Master password is wrong!")
314 (mew-let-user-read)))
315 (mew-passwd-delete-file tfile))
316 pwds))
317
318 (defun mew-passwd-save ()
319 (let* ((process-connection-type mew-connection-type2)
320 (file (expand-file-name mew-passwd-file mew-conf-path))
321 (tfile (mew-make-temp-name "gpg-save"))
322 (args (mew-passwd-adjust-args (list "-c"
323 "--cipher-algo" mew-passwd-cipher
324 "--yes" "--output" file tfile)))
325 (N mew-passwd-repeat)
326 pro)
327 (if (file-exists-p file)
328 (rename-file file (concat file mew-backup-suffix) 'override))
329 (unwind-protect
330 (with-temp-buffer
331 (pp mew-passwd-alist (current-buffer))
332 (write-region (point-min) (point-max) tfile nil 'no-msg)
333 (catch 'loop
334 (dotimes (_i N) ;; prevent byte-compile warning
335 (setq pro (apply
336 'mew-start-process-lang
337 mew-passwd-encryption-name
338 (current-buffer)
339 mew-prog-passwd
340 args))
341 (set-process-filter pro 'mew-passwd-filter)
342 (set-process-sentinel pro 'mew-passwd-sentinel)
343 (mew-passwd-rendezvous pro)
344 (if (file-exists-p file) (throw 'loop nil)))
345 (message "Master password is wrong! Passwords not saved")
346 (mew-let-user-read)))
347 (unless (file-exists-p file)
348 (rename-file (concat file mew-backup-suffix) file))
349 (mew-passwd-delete-file tfile))))
350
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352 ;;;
353 ;;; Filter and sentinel
354 ;;;
355
356 (defun mew-passwd-filter (process string)
357 (let* ((name (process-name process))
358 (regex (concat "^" (regexp-quote mew-passwd-encryption-name)))
359 (encrypt-p (string-match regex name)))
360 (with-current-buffer (process-buffer process)
361 (cond
362 ((string-match "invalid passphrase" string)
363 (mew-warn "Master password mismatch!")
364 (setq mew-passwd-master nil))
365 ((string-match "[bB]ad \\(?:session \\)?key" string)
366 (mew-warn "Master password is wrong!")
367 (setq mew-passwd-master nil))
368 ((string-match "Enter passphrase:" string)
369 (process-send-string process (mew-passwd-read-passwd (if encrypt-p "New master password: " "Master password: ") encrypt-p))
370 (process-send-string process "\n"))
371 ((string-match "Repeat passphrase:" string)
372 (process-send-string process (mew-passwd-read-passwd "New master password again: "))
373 (process-send-string process "\n"))
374 ((string-match "exiting" string)
375 (setq mew-passwd-rendezvous nil))))))
376
377 (defun mew-passwd-sentinel (_process _event)
378 (setq mew-passwd-rendezvous nil))
379
380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 ;;;
382 ;;; Sub functions
383 ;;;
384
385 (defun mew-passwd-delete-file (file)
386 (when (file-exists-p file)
387 (with-temp-buffer
388 (let ((coding-system-for-write 'binary)
389 (size (mew-file-get-size file)))
390 (dotimes (_i size) ;; prevent byte-compile warning
391 (insert 0))
392 (write-region (point-min) (point-max) file nil 'no-msg)))
393 (delete-file file)))
394
395 (defun mew-passwd-get-cache-id (file)
396 (with-temp-buffer
397 (call-process mew-prog-passwd nil t nil "--list-packets" file)
398 (goto-char (point-min))
399 (when (re-search-forward "salt \\([^ ,]+\\)," nil t)
400 (concat "S" (match-string 1)))))
401
402 (defun mew-passwd-clear-passphrase (file)
403 (when (file-exists-p file)
404 (let ((cache-id (mew-passwd-get-cache-id file)))
405 (with-temp-buffer
406 (insert "CLEAR_PASSPHRASE " cache-id "\n")
407 (call-process-region (point-min) (point-max) "gpg-connect-agent")))))
408
409 (defun mew-passwd-adjust-args (args)
410 (if mew-passwd-agent-hack
411 (cons "--pinentry-mode" (cons "loopback" args))
412 args))
413
414 (provide 'mew-passwd)
415
416 ;;; Copyright Notice:
417
418 ;; Copyright (C) 1996-2015 Mew developing team.
419 ;; All rights reserved.
420
421 ;; Redistribution and use in source and binary forms, with or without
422 ;; modification, are permitted provided that the following conditions
423 ;; are met:
424 ;;
425 ;; 1. Redistributions of source code must retain the above copyright
426 ;; notice, this list of conditions and the following disclaimer.
427 ;; 2. Redistributions in binary form must reproduce the above copyright
428 ;; notice, this list of conditions and the following disclaimer in the
429 ;; documentation and/or other materials provided with the distribution.
430 ;; 3. Neither the name of the team nor the names of its contributors
431 ;; may be used to endorse or promote products derived from this software
432 ;; without specific prior written permission.
433 ;;
434 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
435 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
436 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
437 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
438 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
439 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
440 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
441 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
442 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
443 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
444 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
445
446 ;;; mew-passwd.el ends here