Merge changes made in Gnus trunk.
[bpt/emacs.git] / lisp / epa-file.el
CommitLineData
c154c0be 1;;; epa-file.el --- the EasyPG Assistant, transparent file encryption
114f9c96 2;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
c154c0be
MO
3
4;; Author: Daiki Ueno <ueno@unixuser.org>
5;; Keywords: PGP, GnuPG
bd78fa1d 6;; Package: epa
c154c0be
MO
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
c154c0be 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
c154c0be
MO
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c154c0be
MO
22
23;;; Code:
24
25(require 'epa)
3fe68728 26(require 'epa-hook)
c154c0be
MO
27
28(defcustom epa-file-cache-passphrase-for-symmetric-encryption nil
b0f5fd2e
DU
29 "If non-nil, cache passphrase for symmetric encryption.
30
31For security reasons, this option is turned off by default and
0d19d151
DU
32not recommended to use. Instead, consider using public-key
33encryption with gpg-agent which does the same job in a safer
34way."
c154c0be
MO
35 :type 'boolean
36 :group 'epa-file)
37
c154c0be
MO
38(defcustom epa-file-select-keys nil
39 "If non-nil, always asks user to select recipients."
40 :type 'boolean
41 :group 'epa-file)
42
c154c0be
MO
43(defvar epa-file-passphrase-alist nil)
44
45(eval-and-compile
46 (if (fboundp 'encode-coding-string)
47 (defalias 'epa-file--encode-coding-string 'encode-coding-string)
48 (defalias 'epa-file--encode-coding-string 'identity)))
49
50(eval-and-compile
51 (if (fboundp 'decode-coding-string)
52 (defalias 'epa-file--decode-coding-string 'decode-coding-string)
53 (defalias 'epa-file--decode-coding-string 'identity)))
54
c154c0be
MO
55(defun epa-file-passphrase-callback-function (context key-id file)
56 (if (and epa-file-cache-passphrase-for-symmetric-encryption
57 (eq key-id 'SYM))
58 (progn
59 (setq file (file-truename file))
60 (let ((entry (assoc file epa-file-passphrase-alist))
61 passphrase)
62 (or (copy-sequence (cdr entry))
63 (progn
64 (unless entry
65 (setq entry (list file)
66 epa-file-passphrase-alist
67 (cons entry
68 epa-file-passphrase-alist)))
69 (setq passphrase (epa-passphrase-callback-function context
70 key-id nil))
71 (setcdr entry (copy-sequence passphrase))
72 passphrase))))
73 (epa-passphrase-callback-function context key-id nil)))
74
6f8a4190 75;;;###autoload
c154c0be 76(defun epa-file-handler (operation &rest args)
dbc9f96d
GM
77 (save-match-data
78 (let ((op (get operation 'epa-file)))
79 (if op
80 (apply op args)
81 (epa-file-run-real-handler operation args)))))
82
c154c0be
MO
83(defun epa-file-run-real-handler (operation args)
84 (let ((inhibit-file-name-handlers
85 (cons 'epa-file-handler
86 (and (eq inhibit-file-name-operation operation)
87 inhibit-file-name-handlers)))
88 (inhibit-file-name-operation operation))
89 (apply operation args)))
90
91(defun epa-file-decode-and-insert (string file visit beg end replace)
92 (if (fboundp 'decode-coding-inserted-region)
93 (save-restriction
94 (narrow-to-region (point) (point))
68a0d892
DU
95 (insert (if enable-multibyte-characters
96 (string-to-multibyte string)
97 string))
c154c0be
MO
98 (decode-coding-inserted-region
99 (point-min) (point-max)
100 (substring file 0 (string-match epa-file-name-regexp file))
68a0d892 101 visit beg end replace))
c154c0be
MO
102 (insert (epa-file--decode-coding-string string (or coding-system-for-read
103 'undecided)))))
104
c0397930
DU
105(defvar epa-file-error nil)
106(defun epa-file--find-file-not-found-function ()
107 (let ((error epa-file-error))
108 (save-window-excursion
109 (kill-buffer))
110 (signal 'file-error
111 (cons "Opening input file" (cdr error)))))
112
c154c0be
MO
113(defvar last-coding-system-used)
114(defun epa-file-insert-file-contents (file &optional visit beg end replace)
115 (barf-if-buffer-read-only)
116 (if (and visit (or beg end))
117 (error "Attempt to visit less than an entire file"))
118 (setq file (expand-file-name file))
119 (let* ((local-copy
6ea97db8 120 (condition-case nil
c154c0be
MO
121 (epa-file-run-real-handler #'file-local-copy (list file))
122 (error)))
123 (local-file (or local-copy file))
124 (context (epg-make-context))
125 string length entry)
2cb35a56
DU
126 (if visit
127 (setq buffer-file-name file))
c154c0be
MO
128 (epg-context-set-passphrase-callback
129 context
130 (cons #'epa-file-passphrase-callback-function
131 local-file))
132 (epg-context-set-progress-callback context
133 #'epa-progress-callback-function)
134 (unwind-protect
135 (progn
136 (if replace
137 (goto-char (point-min)))
138 (condition-case error
139 (setq string (epg-decrypt-file context local-file nil))
140 (error
141 (if (setq entry (assoc file epa-file-passphrase-alist))
142 (setcdr entry nil))
192cfe77
DU
143 ;; Hack to prevent find-file from opening empty buffer
144 ;; when decryption failed (bug#6568). See the place
145 ;; where `find-file-not-found-functions' are called in
146 ;; `find-file-noselect-1'.
4f195cf7 147 (when (file-exists-p local-file)
4f195cf7
DU
148 (make-local-variable 'epa-file-error)
149 (setq epa-file-error error)
150 (add-hook 'find-file-not-found-functions
151 'epa-file--find-file-not-found-function
152 nil t))
c154c0be
MO
153 (signal 'file-error
154 (cons "Opening input file" (cdr error)))))
155 (make-local-variable 'epa-file-encrypt-to)
156 (setq epa-file-encrypt-to
157 (mapcar #'car (epg-context-result-for context 'encrypted-to)))
158 (if (or beg end)
159 (setq string (substring string (or beg 0) end)))
160 (save-excursion
c5fe4acb
DU
161 ;; If visiting, bind off buffer-file-name so that
162 ;; file-locking will not ask whether we should
163 ;; really edit the buffer.
164 (let ((buffer-file-name
165 (if visit nil buffer-file-name)))
166 (save-restriction
167 (narrow-to-region (point) (point))
168 (epa-file-decode-and-insert string file visit beg end replace)
169 (setq length (- (point-max) (point-min))))
170 (if replace
171 (delete-region (point) (point-max))))
2cb35a56
DU
172 (if visit
173 (set-visited-file-modtime))))
c154c0be
MO
174 (if (and local-copy
175 (file-exists-p local-copy))
176 (delete-file local-copy)))
177 (list file length)))
178(put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
179
180(defun epa-file-write-region (start end file &optional append visit lockname
181 mustbenew)
182 (if append
5a0c3f56 183 (error "Can't append to the file"))
c154c0be
MO
184 (setq file (expand-file-name file))
185 (let* ((coding-system (or coding-system-for-write
186 (if (fboundp 'select-safe-coding-system)
187 ;; This is needed since Emacs 22 has
188 ;; no-conversion setting for *.gpg in
189 ;; `auto-coding-alist'.
190 (let ((buffer-file-name
191 (file-name-sans-extension file)))
192 (select-safe-coding-system
193 (point-min) (point-max)))
194 buffer-file-coding-system)))
195 (context (epg-make-context))
196 (coding-system-for-write 'binary)
197 string entry
198 (recipients
199 (cond
200 ((listp epa-file-encrypt-to) epa-file-encrypt-to)
201 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))
202 (epg-context-set-passphrase-callback
203 context
204 (cons #'epa-file-passphrase-callback-function
205 file))
206 (epg-context-set-progress-callback context
207 #'epa-progress-callback-function)
208 (epg-context-set-armor context epa-armor)
209 (condition-case error
210 (setq string
211 (epg-encrypt-string
212 context
213 (if (stringp start)
214 (epa-file--encode-coding-string start coding-system)
6843296d 215 (unless start
437eedba
DU
216 (setq start (point-min)
217 end (point-max)))
c154c0be
MO
218 (epa-file--encode-coding-string (buffer-substring start end)
219 coding-system))
220 (if (or epa-file-select-keys
221 (not (local-variable-p 'epa-file-encrypt-to
222 (current-buffer))))
223 (epa-select-keys
224 context
225 "Select recipents for encryption.
226If no one is selected, symmetric encryption will be performed. "
227 recipients)
228 (if epa-file-encrypt-to
229 (epg-list-keys context recipients)))))
230 (error
231 (if (setq entry (assoc file epa-file-passphrase-alist))
232 (setcdr entry nil))
233 (signal 'file-error (cons "Opening output file" (cdr error)))))
234 (epa-file-run-real-handler
235 #'write-region
236 (list string nil file append visit lockname mustbenew))
237 (if (boundp 'last-coding-system-used)
238 (setq last-coding-system-used coding-system))
239 (if (eq visit t)
240 (progn
241 (setq buffer-file-name file)
242 (set-visited-file-modtime))
243 (if (stringp visit)
244 (progn
245 (set-visited-file-modtime)
246 (setq buffer-file-name visit))))
247 (if (or (eq visit t)
248 (eq visit nil)
249 (stringp visit))
250 (message "Wrote %s" buffer-file-name))))
251(put 'write-region 'epa-file 'epa-file-write-region)
252
c154c0be
MO
253(defun epa-file-select-keys ()
254 "Select recipients for encryption."
255 (interactive)
256 (make-local-variable 'epa-file-encrypt-to)
257 (setq epa-file-encrypt-to
fef7aa9e
MO
258 (mapcar
259 (lambda (key)
260 (epg-sub-key-id (car (epg-key-sub-key-list key))))
c154c0be
MO
261 (epa-select-keys
262 (epg-make-context)
263 "Select recipents for encryption.
fef7aa9e 264If no one is selected, symmetric encryption will be performed. "))))
c154c0be
MO
265
266;;;###autoload
267(defun epa-file-enable ()
268 (interactive)
269 (if (memq epa-file-handler file-name-handler-alist)
270 (message "`epa-file' already enabled")
271 (setq file-name-handler-alist
272 (cons epa-file-handler file-name-handler-alist))
bfeee9d1 273 (add-hook 'find-file-hook 'epa-file-find-file-hook)
c154c0be
MO
274 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist))
275 (message "`epa-file' enabled")))
276
277;;;###autoload
278(defun epa-file-disable ()
279 (interactive)
280 (if (memq epa-file-handler file-name-handler-alist)
281 (progn
282 (setq file-name-handler-alist
283 (delq epa-file-handler file-name-handler-alist))
bfeee9d1 284 (remove-hook 'find-file-hook 'epa-file-find-file-hook)
c154c0be
MO
285 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry
286 auto-mode-alist))
287 (message "`epa-file' disabled"))
288 (message "`epa-file' already disabled")))
289
290(provide 'epa-file)
291
37b77401 292;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d
c154c0be 293;;; epa-file.el ends here