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, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
28 (defgroup epa-file nil
29 "The EasyPG Assistant hooks for transparent file encryption"
33 (defun epa-file--file-name-regexp-set (variable value
)
34 (set-default variable value
)
35 (if (fboundp 'epa-file-name-regexp-update
)
36 (epa-file-name-regexp-update)))
38 (defcustom epa-file-name-regexp
"\\.gpg\\(~\\|\\.~[0-9]+~\\)?\\'"
39 "Regexp which matches filenames to be encrypted with GnuPG.
41 If you set this outside Custom while epa-file is already enabled, you
42 have to call `epa-file-name-regexp-update' after setting it to
43 properly update file-name-handler-alist. Setting this through Custom
44 does that automatically."
47 :set
'epa-file--file-name-regexp-set
)
49 (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil
50 "If non-nil, cache passphrase for symmetric encryption."
54 (defcustom epa-file-inhibit-auto-save t
55 "If non-nil, disable auto-saving when opening an encrypted file."
59 (defcustom epa-file-select-keys nil
60 "If non-nil, always asks user to select recipients."
64 (defvar epa-file-encrypt-to nil
65 "*Recipient(s) used for encrypting files.
66 May either be a string or a list of strings.")
69 (put 'epa-file-encrypt-to
'safe-local-variable
81 (put 'epa-file-encrypt-to
'permanent-local t
)
83 (defvar epa-file-handler
84 (cons epa-file-name-regexp
'epa-file-handler
))
86 (defvar epa-file-auto-mode-alist-entry
87 (list epa-file-name-regexp nil
'epa-file
))
89 (defvar epa-file-passphrase-alist nil
)
92 (if (fboundp 'encode-coding-string
)
93 (defalias 'epa-file--encode-coding-string
'encode-coding-string
)
94 (defalias 'epa-file--encode-coding-string
'identity
)))
97 (if (fboundp 'decode-coding-string
)
98 (defalias 'epa-file--decode-coding-string
'decode-coding-string
)
99 (defalias 'epa-file--decode-coding-string
'identity
)))
101 (defun epa-file-name-regexp-update ()
103 (unless (equal (car epa-file-handler
) epa-file-name-regexp
)
104 (setcar epa-file-handler epa-file-name-regexp
)))
106 (defun epa-file-passphrase-callback-function (context key-id file
)
107 (if (and epa-file-cache-passphrase-for-symmetric-encryption
110 (setq file
(file-truename file
))
111 (let ((entry (assoc file epa-file-passphrase-alist
))
113 (or (copy-sequence (cdr entry
))
116 (setq entry
(list file
)
117 epa-file-passphrase-alist
119 epa-file-passphrase-alist
)))
120 (setq passphrase
(epa-passphrase-callback-function context
122 (setcdr entry
(copy-sequence passphrase
))
124 (epa-passphrase-callback-function context key-id nil
)))
126 (defun epa-file-handler (operation &rest args
)
128 (let ((op (get operation
'epa-file
)))
131 (epa-file-run-real-handler operation args
)))))
133 (defun epa-file-run-real-handler (operation args
)
134 (let ((inhibit-file-name-handlers
135 (cons 'epa-file-handler
136 (and (eq inhibit-file-name-operation operation
)
137 inhibit-file-name-handlers
)))
138 (inhibit-file-name-operation operation
))
139 (apply operation args
)))
141 (defun epa-file-decode-and-insert (string file visit beg end replace
)
142 (if (fboundp 'decode-coding-inserted-region
)
144 (narrow-to-region (point) (point))
145 (let ((multibyte enable-multibyte-characters
))
146 (set-buffer-multibyte nil
)
148 (set-buffer-multibyte multibyte
)
149 (decode-coding-inserted-region
150 (point-min) (point-max)
151 (substring file
0 (string-match epa-file-name-regexp file
))
152 visit beg end replace
)))
153 (insert (epa-file--decode-coding-string string
(or coding-system-for-read
156 (defvar last-coding-system-used
)
157 (defun epa-file-insert-file-contents (file &optional visit beg end replace
)
158 (barf-if-buffer-read-only)
159 (if (and visit
(or beg end
))
160 (error "Attempt to visit less than an entire file"))
161 (setq file
(expand-file-name file
))
164 (epa-file-run-real-handler #'file-local-copy
(list file
))
166 (local-file (or local-copy file
))
167 (context (epg-make-context))
170 (setq buffer-file-name file
))
171 (epg-context-set-passphrase-callback
173 (cons #'epa-file-passphrase-callback-function
175 (epg-context-set-progress-callback context
176 #'epa-progress-callback-function
)
180 (goto-char (point-min)))
181 (condition-case error
182 (setq string
(epg-decrypt-file context local-file nil
))
184 (if (setq entry
(assoc file epa-file-passphrase-alist
))
187 (cons "Opening input file" (cdr error
)))))
188 (make-local-variable 'epa-file-encrypt-to
)
189 (setq epa-file-encrypt-to
190 (mapcar #'car
(epg-context-result-for context
'encrypted-to
)))
192 (setq string
(substring string
(or beg
0) end
)))
195 (narrow-to-region (point) (point))
196 (epa-file-decode-and-insert string file visit beg end replace
)
197 (setq length
(- (point-max) (point-min))))
199 (delete-region (point) (point-max)))))
201 (file-exists-p local-copy
))
202 (delete-file local-copy
)))
204 (put 'insert-file-contents
'epa-file
'epa-file-insert-file-contents
)
206 (defun epa-file-write-region (start end file
&optional append visit lockname
209 (error "Can't append to the file."))
210 (setq file
(expand-file-name file
))
211 (let* ((coding-system (or coding-system-for-write
212 (if (fboundp 'select-safe-coding-system
)
213 ;; This is needed since Emacs 22 has
214 ;; no-conversion setting for *.gpg in
215 ;; `auto-coding-alist'.
216 (let ((buffer-file-name
217 (file-name-sans-extension file
)))
218 (select-safe-coding-system
219 (point-min) (point-max)))
220 buffer-file-coding-system
)))
221 (context (epg-make-context))
222 (coding-system-for-write 'binary
)
226 ((listp epa-file-encrypt-to
) epa-file-encrypt-to
)
227 ((stringp epa-file-encrypt-to
) (list epa-file-encrypt-to
)))))
228 (epg-context-set-passphrase-callback
230 (cons #'epa-file-passphrase-callback-function
232 (epg-context-set-progress-callback context
233 #'epa-progress-callback-function
)
234 (epg-context-set-armor context epa-armor
)
235 (condition-case error
240 (epa-file--encode-coding-string start coding-system
)
241 (epa-file--encode-coding-string (buffer-substring start end
)
243 (if (or epa-file-select-keys
244 (not (local-variable-p 'epa-file-encrypt-to
248 "Select recipents for encryption.
249 If no one is selected, symmetric encryption will be performed. "
251 (if epa-file-encrypt-to
252 (epg-list-keys context recipients
)))))
254 (if (setq entry
(assoc file epa-file-passphrase-alist
))
256 (signal 'file-error
(cons "Opening output file" (cdr error
)))))
257 (epa-file-run-real-handler
259 (list string nil file append visit lockname mustbenew
))
260 (if (boundp 'last-coding-system-used
)
261 (setq last-coding-system-used coding-system
))
264 (setq buffer-file-name file
)
265 (set-visited-file-modtime))
268 (set-visited-file-modtime)
269 (setq buffer-file-name visit
))))
273 (message "Wrote %s" buffer-file-name
))))
274 (put 'write-region
'epa-file
'epa-file-write-region
)
276 (defun epa-file-find-file-hook ()
277 (if (and buffer-file-name
278 (string-match epa-file-name-regexp buffer-file-name
)
279 epa-file-inhibit-auto-save
)
281 (set-buffer-modified-p nil
))
283 (defun epa-file-select-keys ()
284 "Select recipients for encryption."
286 (make-local-variable 'epa-file-encrypt-to
)
287 (setq epa-file-encrypt-to
290 (epg-sub-key-id (car (epg-key-sub-key-list key
))))
293 "Select recipents for encryption.
294 If no one is selected, symmetric encryption will be performed. "))))
297 (defun epa-file-enable ()
299 (if (memq epa-file-handler file-name-handler-alist
)
300 (message "`epa-file' already enabled")
301 (setq file-name-handler-alist
302 (cons epa-file-handler file-name-handler-alist
))
303 (add-hook 'find-file-hooks
'epa-file-find-file-hook
)
304 (setq auto-mode-alist
(cons epa-file-auto-mode-alist-entry auto-mode-alist
))
305 (message "`epa-file' enabled")))
308 (defun epa-file-disable ()
310 (if (memq epa-file-handler file-name-handler-alist
)
312 (setq file-name-handler-alist
313 (delq epa-file-handler file-name-handler-alist
))
314 (remove-hook 'find-file-hooks
'epa-file-find-file-hook
)
315 (setq auto-mode-alist
(delq epa-file-auto-mode-alist-entry
317 (message "`epa-file' disabled"))
318 (message "`epa-file' already disabled")))
321 (define-minor-mode epa-file-mode
322 "Toggle automatic file encryption and decryption.
323 With prefix argument ARG, turn auto encryption on if positive, else off.
324 Return the new status of auto encryption (non-nil means on)."
325 :global t
:init-value nil
:group
'epa-file
:version
"23.1"
326 (setq file-name-handler-alist
327 (delq epa-file-handler file-name-handler-alist
))
328 (remove-hook 'find-file-hooks
'epa-file-find-file-hook
)
329 (setq auto-mode-alist
(delq epa-file-auto-mode-alist-entry
332 (setq file-name-handler-alist
333 (cons epa-file-handler file-name-handler-alist
))
334 (add-hook 'find-file-hooks
'epa-file-find-file-hook
)
335 (setq auto-mode-alist
(cons epa-file-auto-mode-alist-entry
340 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d
341 ;;; epa-file.el ends here