diary-lib comments.
[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
446b12da
DU
70 key-id
71 file))
c154c0be
MO
72 (setcdr entry (copy-sequence passphrase))
73 passphrase))))
446b12da 74 (epa-passphrase-callback-function context key-id file)))
c154c0be 75
6f8a4190 76;;;###autoload
c154c0be 77(defun epa-file-handler (operation &rest args)
dbc9f96d
GM
78 (save-match-data
79 (let ((op (get operation 'epa-file)))
80 (if op
81 (apply op args)
82 (epa-file-run-real-handler operation args)))))
83
c154c0be
MO
84(defun epa-file-run-real-handler (operation args)
85 (let ((inhibit-file-name-handlers
86 (cons 'epa-file-handler
87 (and (eq inhibit-file-name-operation operation)
88 inhibit-file-name-handlers)))
89 (inhibit-file-name-operation operation))
90 (apply operation args)))
91
92(defun epa-file-decode-and-insert (string file visit beg end replace)
93 (if (fboundp 'decode-coding-inserted-region)
94 (save-restriction
95 (narrow-to-region (point) (point))
68a0d892
DU
96 (insert (if enable-multibyte-characters
97 (string-to-multibyte string)
98 string))
c154c0be
MO
99 (decode-coding-inserted-region
100 (point-min) (point-max)
101 (substring file 0 (string-match epa-file-name-regexp file))
68a0d892 102 visit beg end replace))
c154c0be
MO
103 (insert (epa-file--decode-coding-string string (or coding-system-for-read
104 'undecided)))))
105
c0397930
DU
106(defvar epa-file-error nil)
107(defun epa-file--find-file-not-found-function ()
108 (let ((error epa-file-error))
109 (save-window-excursion
110 (kill-buffer))
111 (signal 'file-error
112 (cons "Opening input file" (cdr error)))))
113
c154c0be
MO
114(defvar last-coding-system-used)
115(defun epa-file-insert-file-contents (file &optional visit beg end replace)
116 (barf-if-buffer-read-only)
117 (if (and visit (or beg end))
118 (error "Attempt to visit less than an entire file"))
119 (setq file (expand-file-name file))
120 (let* ((local-copy
6ea97db8 121 (condition-case nil
c154c0be
MO
122 (epa-file-run-real-handler #'file-local-copy (list file))
123 (error)))
124 (local-file (or local-copy file))
125 (context (epg-make-context))
126 string length entry)
2cb35a56
DU
127 (if visit
128 (setq buffer-file-name file))
c154c0be
MO
129 (epg-context-set-passphrase-callback
130 context
131 (cons #'epa-file-passphrase-callback-function
132 local-file))
133 (epg-context-set-progress-callback context
134 #'epa-progress-callback-function)
135 (unwind-protect
136 (progn
137 (if replace
138 (goto-char (point-min)))
139 (condition-case error
140 (setq string (epg-decrypt-file context local-file nil))
141 (error
142 (if (setq entry (assoc file epa-file-passphrase-alist))
143 (setcdr entry nil))
192cfe77
DU
144 ;; Hack to prevent find-file from opening empty buffer
145 ;; when decryption failed (bug#6568). See the place
146 ;; where `find-file-not-found-functions' are called in
147 ;; `find-file-noselect-1'.
4f195cf7 148 (when (file-exists-p local-file)
4f195cf7
DU
149 (make-local-variable 'epa-file-error)
150 (setq epa-file-error error)
151 (add-hook 'find-file-not-found-functions
152 'epa-file--find-file-not-found-function
153 nil t))
c154c0be
MO
154 (signal 'file-error
155 (cons "Opening input file" (cdr error)))))
156 (make-local-variable 'epa-file-encrypt-to)
157 (setq epa-file-encrypt-to
158 (mapcar #'car (epg-context-result-for context 'encrypted-to)))
159 (if (or beg end)
160 (setq string (substring string (or beg 0) end)))
161 (save-excursion
c5fe4acb
DU
162 ;; If visiting, bind off buffer-file-name so that
163 ;; file-locking will not ask whether we should
164 ;; really edit the buffer.
165 (let ((buffer-file-name
166 (if visit nil buffer-file-name)))
167 (save-restriction
168 (narrow-to-region (point) (point))
169 (epa-file-decode-and-insert string file visit beg end replace)
170 (setq length (- (point-max) (point-min))))
171 (if replace
172 (delete-region (point) (point-max))))
2cb35a56
DU
173 (if visit
174 (set-visited-file-modtime))))
c154c0be
MO
175 (if (and local-copy
176 (file-exists-p local-copy))
177 (delete-file local-copy)))
178 (list file length)))
179(put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
180
181(defun epa-file-write-region (start end file &optional append visit lockname
182 mustbenew)
183 (if append
5a0c3f56 184 (error "Can't append to the file"))
c154c0be
MO
185 (setq file (expand-file-name file))
186 (let* ((coding-system (or coding-system-for-write
187 (if (fboundp 'select-safe-coding-system)
188 ;; This is needed since Emacs 22 has
189 ;; no-conversion setting for *.gpg in
190 ;; `auto-coding-alist'.
191 (let ((buffer-file-name
192 (file-name-sans-extension file)))
193 (select-safe-coding-system
194 (point-min) (point-max)))
195 buffer-file-coding-system)))
196 (context (epg-make-context))
197 (coding-system-for-write 'binary)
198 string entry
199 (recipients
200 (cond
201 ((listp epa-file-encrypt-to) epa-file-encrypt-to)
202 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))
203 (epg-context-set-passphrase-callback
204 context
205 (cons #'epa-file-passphrase-callback-function
206 file))
207 (epg-context-set-progress-callback context
208 #'epa-progress-callback-function)
209 (epg-context-set-armor context epa-armor)
210 (condition-case error
211 (setq string
212 (epg-encrypt-string
213 context
214 (if (stringp start)
215 (epa-file--encode-coding-string start coding-system)
6843296d 216 (unless start
437eedba
DU
217 (setq start (point-min)
218 end (point-max)))
c154c0be
MO
219 (epa-file--encode-coding-string (buffer-substring start end)
220 coding-system))
221 (if (or epa-file-select-keys
222 (not (local-variable-p 'epa-file-encrypt-to
223 (current-buffer))))
224 (epa-select-keys
225 context
226 "Select recipents for encryption.
227If no one is selected, symmetric encryption will be performed. "
228 recipients)
229 (if epa-file-encrypt-to
230 (epg-list-keys context recipients)))))
231 (error
232 (if (setq entry (assoc file epa-file-passphrase-alist))
233 (setcdr entry nil))
234 (signal 'file-error (cons "Opening output file" (cdr error)))))
235 (epa-file-run-real-handler
236 #'write-region
237 (list string nil file append visit lockname mustbenew))
238 (if (boundp 'last-coding-system-used)
239 (setq last-coding-system-used coding-system))
240 (if (eq visit t)
241 (progn
242 (setq buffer-file-name file)
243 (set-visited-file-modtime))
244 (if (stringp visit)
245 (progn
246 (set-visited-file-modtime)
247 (setq buffer-file-name visit))))
248 (if (or (eq visit t)
249 (eq visit nil)
250 (stringp visit))
251 (message "Wrote %s" buffer-file-name))))
252(put 'write-region 'epa-file 'epa-file-write-region)
253
c154c0be
MO
254(defun epa-file-select-keys ()
255 "Select recipients for encryption."
256 (interactive)
257 (make-local-variable 'epa-file-encrypt-to)
258 (setq epa-file-encrypt-to
fef7aa9e
MO
259 (mapcar
260 (lambda (key)
261 (epg-sub-key-id (car (epg-key-sub-key-list key))))
c154c0be
MO
262 (epa-select-keys
263 (epg-make-context)
264 "Select recipents for encryption.
fef7aa9e 265If no one is selected, symmetric encryption will be performed. "))))
c154c0be
MO
266
267;;;###autoload
268(defun epa-file-enable ()
269 (interactive)
270 (if (memq epa-file-handler file-name-handler-alist)
271 (message "`epa-file' already enabled")
272 (setq file-name-handler-alist
273 (cons epa-file-handler file-name-handler-alist))
bfeee9d1 274 (add-hook 'find-file-hook 'epa-file-find-file-hook)
c154c0be
MO
275 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist))
276 (message "`epa-file' enabled")))
277
278;;;###autoload
279(defun epa-file-disable ()
280 (interactive)
281 (if (memq epa-file-handler file-name-handler-alist)
282 (progn
283 (setq file-name-handler-alist
284 (delq epa-file-handler file-name-handler-alist))
bfeee9d1 285 (remove-hook 'find-file-hook 'epa-file-find-file-hook)
c154c0be
MO
286 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry
287 auto-mode-alist))
288 (message "`epa-file' disabled"))
289 (message "`epa-file' already disabled")))
290
291(provide 'epa-file)
292
37b77401 293;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d
c154c0be 294;;; epa-file.el ends here