1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption
2 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil
28 "If non-nil, cache passphrase for symmetric encryption."
32 (defcustom epa-file-select-keys nil
33 "If non-nil, always asks user to select recipients."
37 (defvar epa-file-passphrase-alist nil
)
40 (if (fboundp 'encode-coding-string
)
41 (defalias 'epa-file--encode-coding-string
'encode-coding-string
)
42 (defalias 'epa-file--encode-coding-string
'identity
)))
45 (if (fboundp 'decode-coding-string
)
46 (defalias 'epa-file--decode-coding-string
'decode-coding-string
)
47 (defalias 'epa-file--decode-coding-string
'identity
)))
49 (defun epa-file-passphrase-callback-function (context key-id file
)
50 (if (and epa-file-cache-passphrase-for-symmetric-encryption
53 (setq file
(file-truename file
))
54 (let ((entry (assoc file epa-file-passphrase-alist
))
56 (or (copy-sequence (cdr entry
))
59 (setq entry
(list file
)
60 epa-file-passphrase-alist
62 epa-file-passphrase-alist
)))
63 (setq passphrase
(epa-passphrase-callback-function context
65 (setcdr entry
(copy-sequence passphrase
))
67 (epa-passphrase-callback-function context key-id nil
)))
70 (defun epa-file-handler (operation &rest args
)
72 (let ((op (get operation
'epa-file
)))
75 (epa-file-run-real-handler operation args
)))))
77 (defun epa-file-run-real-handler (operation args
)
78 (let ((inhibit-file-name-handlers
79 (cons 'epa-file-handler
80 (and (eq inhibit-file-name-operation operation
)
81 inhibit-file-name-handlers
)))
82 (inhibit-file-name-operation operation
))
83 (apply operation args
)))
85 (defun epa-file-decode-and-insert (string file visit beg end replace
)
86 (if (fboundp 'decode-coding-inserted-region
)
88 (narrow-to-region (point) (point))
89 (let ((multibyte enable-multibyte-characters
))
90 (set-buffer-multibyte nil
)
92 (set-buffer-multibyte multibyte
)
93 (decode-coding-inserted-region
94 (point-min) (point-max)
95 (substring file
0 (string-match epa-file-name-regexp file
))
96 visit beg end replace
)))
97 (insert (epa-file--decode-coding-string string
(or coding-system-for-read
100 (defvar last-coding-system-used
)
101 (defun epa-file-insert-file-contents (file &optional visit beg end replace
)
102 (barf-if-buffer-read-only)
103 (if (and visit
(or beg end
))
104 (error "Attempt to visit less than an entire file"))
105 (setq file
(expand-file-name file
))
108 (epa-file-run-real-handler #'file-local-copy
(list file
))
110 (local-file (or local-copy file
))
111 (context (epg-make-context))
114 (setq buffer-file-name file
))
115 (epg-context-set-passphrase-callback
117 (cons #'epa-file-passphrase-callback-function
119 (epg-context-set-progress-callback context
120 #'epa-progress-callback-function
)
124 (goto-char (point-min)))
125 (condition-case error
126 (setq string
(epg-decrypt-file context local-file nil
))
128 (if (setq entry
(assoc file epa-file-passphrase-alist
))
131 (cons "Opening input file" (cdr error
)))))
132 (make-local-variable 'epa-file-encrypt-to
)
133 (setq epa-file-encrypt-to
134 (mapcar #'car
(epg-context-result-for context
'encrypted-to
)))
136 (setq string
(substring string
(or beg
0) end
)))
139 (narrow-to-region (point) (point))
140 (epa-file-decode-and-insert string file visit beg end replace
)
141 (setq length
(- (point-max) (point-min))))
143 (delete-region (point) (point-max)))))
145 (file-exists-p local-copy
))
146 (delete-file local-copy
)))
148 (put 'insert-file-contents
'epa-file
'epa-file-insert-file-contents
)
150 (defun epa-file-write-region (start end file
&optional append visit lockname
153 (error "Can't append to the file."))
154 (setq file
(expand-file-name file
))
155 (let* ((coding-system (or coding-system-for-write
156 (if (fboundp 'select-safe-coding-system
)
157 ;; This is needed since Emacs 22 has
158 ;; no-conversion setting for *.gpg in
159 ;; `auto-coding-alist'.
160 (let ((buffer-file-name
161 (file-name-sans-extension file
)))
162 (select-safe-coding-system
163 (point-min) (point-max)))
164 buffer-file-coding-system
)))
165 (context (epg-make-context))
166 (coding-system-for-write 'binary
)
170 ((listp epa-file-encrypt-to
) epa-file-encrypt-to
)
171 ((stringp epa-file-encrypt-to
) (list epa-file-encrypt-to
)))))
172 (epg-context-set-passphrase-callback
174 (cons #'epa-file-passphrase-callback-function
176 (epg-context-set-progress-callback context
177 #'epa-progress-callback-function
)
178 (epg-context-set-armor context epa-armor
)
179 (condition-case error
184 (epa-file--encode-coding-string start coding-system
)
186 (setq start
(point-min)
188 (epa-file--encode-coding-string (buffer-substring start end
)
190 (if (or epa-file-select-keys
191 (not (local-variable-p 'epa-file-encrypt-to
195 "Select recipents for encryption.
196 If no one is selected, symmetric encryption will be performed. "
198 (if epa-file-encrypt-to
199 (epg-list-keys context recipients
)))))
201 (if (setq entry
(assoc file epa-file-passphrase-alist
))
203 (signal 'file-error
(cons "Opening output file" (cdr error
)))))
204 (epa-file-run-real-handler
206 (list string nil file append visit lockname mustbenew
))
207 (if (boundp 'last-coding-system-used
)
208 (setq last-coding-system-used coding-system
))
211 (setq buffer-file-name file
)
212 (set-visited-file-modtime))
215 (set-visited-file-modtime)
216 (setq buffer-file-name visit
))))
220 (message "Wrote %s" buffer-file-name
))))
221 (put 'write-region
'epa-file
'epa-file-write-region
)
223 (defun epa-file-select-keys ()
224 "Select recipients for encryption."
226 (make-local-variable 'epa-file-encrypt-to
)
227 (setq epa-file-encrypt-to
230 (epg-sub-key-id (car (epg-key-sub-key-list key
))))
233 "Select recipents for encryption.
234 If no one is selected, symmetric encryption will be performed. "))))
237 (defun epa-file-enable ()
239 (if (memq epa-file-handler file-name-handler-alist
)
240 (message "`epa-file' already enabled")
241 (setq file-name-handler-alist
242 (cons epa-file-handler file-name-handler-alist
))
243 (add-hook 'find-file-hook
'epa-file-find-file-hook
)
244 (setq auto-mode-alist
(cons epa-file-auto-mode-alist-entry auto-mode-alist
))
245 (message "`epa-file' enabled")))
248 (defun epa-file-disable ()
250 (if (memq epa-file-handler file-name-handler-alist
)
252 (setq file-name-handler-alist
253 (delq epa-file-handler file-name-handler-alist
))
254 (remove-hook 'find-file-hook
'epa-file-find-file-hook
)
255 (setq auto-mode-alist
(delq epa-file-auto-mode-alist-entry
257 (message "`epa-file' disabled"))
258 (message "`epa-file' already disabled")))
262 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d
263 ;;; epa-file.el ends here