Mercurial > emacs
annotate lisp/epa-file.el @ 109981:55e2c5cbf29c
fontset.c (reorder_font_vector): Prefer a font-spec specifying :otf.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Wed, 25 Aug 2010 15:10:09 +0900 |
parents | 1eca4fd49be3 |
children | 280c8ae2476d |
rev | line source |
---|---|
91647 | 1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption |
106815 | 2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
91647 | 3 |
4 ;; Author: Daiki Ueno <ueno@unixuser.org> | |
5 ;; Keywords: PGP, GnuPG | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94512
diff
changeset
|
9 ;; GNU Emacs is free software: you can redistribute it and/or modify |
91647 | 10 ;; it under the terms of the GNU General Public License as published by |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94512
diff
changeset
|
11 ;; the Free Software Foundation, either version 3 of the License, or |
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94512
diff
changeset
|
12 ;; (at your option) any later version. |
91647 | 13 |
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. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94512
diff
changeset
|
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
91647 | 21 |
22 ;;; Code: | |
23 | |
24 (require 'epa) | |
94755
14eda1195ec3
Provide/require epa-hook, not epa-file-hook.
Eli Zaretskii <eliz@gnu.org>
parents:
94678
diff
changeset
|
25 (require 'epa-hook) |
91647 | 26 |
27 (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil | |
105120
e9b2a988e884
(epa-file-cache-passphrase-for-symmetric-encryption):
Daiki Ueno <ueno@unixuser.org>
parents:
103016
diff
changeset
|
28 "If non-nil, cache passphrase for symmetric encryption. |
e9b2a988e884
(epa-file-cache-passphrase-for-symmetric-encryption):
Daiki Ueno <ueno@unixuser.org>
parents:
103016
diff
changeset
|
29 |
e9b2a988e884
(epa-file-cache-passphrase-for-symmetric-encryption):
Daiki Ueno <ueno@unixuser.org>
parents:
103016
diff
changeset
|
30 For security reasons, this option is turned off by default and |
105121 | 31 not recommended to use. Instead, consider using public-key |
32 encryption with gpg-agent which does the same job in a safer | |
33 way." | |
91647 | 34 :type 'boolean |
35 :group 'epa-file) | |
36 | |
37 (defcustom epa-file-select-keys nil | |
38 "If non-nil, always asks user to select recipients." | |
39 :type 'boolean | |
40 :group 'epa-file) | |
41 | |
42 (defvar epa-file-passphrase-alist nil) | |
43 | |
44 (eval-and-compile | |
45 (if (fboundp 'encode-coding-string) | |
46 (defalias 'epa-file--encode-coding-string 'encode-coding-string) | |
47 (defalias 'epa-file--encode-coding-string 'identity))) | |
48 | |
49 (eval-and-compile | |
50 (if (fboundp 'decode-coding-string) | |
51 (defalias 'epa-file--decode-coding-string 'decode-coding-string) | |
52 (defalias 'epa-file--decode-coding-string 'identity))) | |
53 | |
54 (defun epa-file-passphrase-callback-function (context key-id file) | |
55 (if (and epa-file-cache-passphrase-for-symmetric-encryption | |
56 (eq key-id 'SYM)) | |
57 (progn | |
58 (setq file (file-truename file)) | |
59 (let ((entry (assoc file epa-file-passphrase-alist)) | |
60 passphrase) | |
61 (or (copy-sequence (cdr entry)) | |
62 (progn | |
63 (unless entry | |
64 (setq entry (list file) | |
65 epa-file-passphrase-alist | |
66 (cons entry | |
67 epa-file-passphrase-alist))) | |
68 (setq passphrase (epa-passphrase-callback-function context | |
69 key-id nil)) | |
70 (setcdr entry (copy-sequence passphrase)) | |
71 passphrase)))) | |
72 (epa-passphrase-callback-function context key-id nil))) | |
73 | |
94512
d10cd2d8d9ae
* epa-file-hook.el: New file split from epa-file.el.
Daiki Ueno <ueno@unixuser.org>
parents:
94423
diff
changeset
|
74 ;;;###autoload |
91647 | 75 (defun epa-file-handler (operation &rest args) |
94423
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
76 (save-match-data |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
77 (let ((op (get operation 'epa-file))) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
78 (if op |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
79 (apply op args) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
80 (epa-file-run-real-handler operation args))))) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
81 |
91647 | 82 (defun epa-file-run-real-handler (operation args) |
83 (let ((inhibit-file-name-handlers | |
84 (cons 'epa-file-handler | |
85 (and (eq inhibit-file-name-operation operation) | |
86 inhibit-file-name-handlers))) | |
87 (inhibit-file-name-operation operation)) | |
88 (apply operation args))) | |
89 | |
90 (defun epa-file-decode-and-insert (string file visit beg end replace) | |
91 (if (fboundp 'decode-coding-inserted-region) | |
92 (save-restriction | |
93 (narrow-to-region (point) (point)) | |
103016
15551118906e
epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
94 (insert (if enable-multibyte-characters |
15551118906e
epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
95 (string-to-multibyte string) |
15551118906e
epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
96 string)) |
91647 | 97 (decode-coding-inserted-region |
98 (point-min) (point-max) | |
99 (substring file 0 (string-match epa-file-name-regexp file)) | |
103016
15551118906e
epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
100 visit beg end replace)) |
91647 | 101 (insert (epa-file--decode-coding-string string (or coding-system-for-read |
102 'undecided))))) | |
103 | |
109166
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
104 (defvar epa-file-error nil) |
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
105 (defun epa-file--find-file-not-found-function () |
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
106 (let ((error epa-file-error)) |
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
107 (save-window-excursion |
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
108 (kill-buffer)) |
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
109 (signal 'file-error |
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
110 (cons "Opening input file" (cdr error))))) |
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
111 |
91647 | 112 (defvar last-coding-system-used) |
113 (defun epa-file-insert-file-contents (file &optional visit beg end replace) | |
114 (barf-if-buffer-read-only) | |
115 (if (and visit (or beg end)) | |
116 (error "Attempt to visit less than an entire file")) | |
117 (setq file (expand-file-name file)) | |
118 (let* ((local-copy | |
98409
cb5ef7767703
(epa-file-insert-file-contents): Fix typo.
Daiki Ueno <ueno@unixuser.org>
parents:
95478
diff
changeset
|
119 (condition-case nil |
91647 | 120 (epa-file-run-real-handler #'file-local-copy (list file)) |
121 (error))) | |
122 (local-file (or local-copy file)) | |
123 (context (epg-make-context)) | |
124 string length entry) | |
100456
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
125 (if visit |
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
126 (setq buffer-file-name file)) |
91647 | 127 (epg-context-set-passphrase-callback |
128 context | |
129 (cons #'epa-file-passphrase-callback-function | |
130 local-file)) | |
131 (epg-context-set-progress-callback context | |
132 #'epa-progress-callback-function) | |
133 (unwind-protect | |
134 (progn | |
135 (if replace | |
136 (goto-char (point-min))) | |
137 (condition-case error | |
138 (setq string (epg-decrypt-file context local-file nil)) | |
139 (error | |
140 (if (setq entry (assoc file epa-file-passphrase-alist)) | |
141 (setcdr entry nil)) | |
109535 | 142 ;; Hack to prevent find-file from opening empty buffer |
143 ;; when decryption failed (bug#6568). See the place | |
144 ;; where `find-file-not-found-functions' are called in | |
145 ;; `find-file-noselect-1'. | |
109534
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
146 (when (file-exists-p local-file) |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
147 (make-local-variable 'epa-file-error) |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
148 (setq epa-file-error error) |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
149 (add-hook 'find-file-not-found-functions |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
150 'epa-file--find-file-not-found-function |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
151 nil t)) |
91647 | 152 (signal 'file-error |
153 (cons "Opening input file" (cdr error))))) | |
154 (make-local-variable 'epa-file-encrypt-to) | |
155 (setq epa-file-encrypt-to | |
156 (mapcar #'car (epg-context-result-for context 'encrypted-to))) | |
157 (if (or beg end) | |
158 (setq string (substring string (or beg 0) end))) | |
159 (save-excursion | |
160 (save-restriction | |
161 (narrow-to-region (point) (point)) | |
162 (epa-file-decode-and-insert string file visit beg end replace) | |
163 (setq length (- (point-max) (point-min)))) | |
164 (if replace | |
100304
4dfef179eed3
* epa-hook.el (epa-file-find-file-hook): Don't mark the current
Daiki Ueno <ueno@unixuser.org>
parents:
98409
diff
changeset
|
165 (delete-region (point) (point-max))) |
100456
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
166 (if visit |
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
167 (set-visited-file-modtime)))) |
91647 | 168 (if (and local-copy |
169 (file-exists-p local-copy)) | |
170 (delete-file local-copy))) | |
171 (list file length))) | |
172 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents) | |
173 | |
174 (defun epa-file-write-region (start end file &optional append visit lockname | |
175 mustbenew) | |
176 (if append | |
105170 | 177 (error "Can't append to the file")) |
91647 | 178 (setq file (expand-file-name file)) |
179 (let* ((coding-system (or coding-system-for-write | |
180 (if (fboundp 'select-safe-coding-system) | |
181 ;; This is needed since Emacs 22 has | |
182 ;; no-conversion setting for *.gpg in | |
183 ;; `auto-coding-alist'. | |
184 (let ((buffer-file-name | |
185 (file-name-sans-extension file))) | |
186 (select-safe-coding-system | |
187 (point-min) (point-max))) | |
188 buffer-file-coding-system))) | |
189 (context (epg-make-context)) | |
190 (coding-system-for-write 'binary) | |
191 string entry | |
192 (recipients | |
193 (cond | |
194 ((listp epa-file-encrypt-to) epa-file-encrypt-to) | |
195 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to))))) | |
196 (epg-context-set-passphrase-callback | |
197 context | |
198 (cons #'epa-file-passphrase-callback-function | |
199 file)) | |
200 (epg-context-set-progress-callback context | |
201 #'epa-progress-callback-function) | |
202 (epg-context-set-armor context epa-armor) | |
203 (condition-case error | |
204 (setq string | |
205 (epg-encrypt-string | |
206 context | |
207 (if (stringp start) | |
208 (epa-file--encode-coding-string start coding-system) | |
95477
57f3dde2b9be
(epa-file-write-region): Write the entire buffer
Daiki Ueno <ueno@unixuser.org>
parents:
94755
diff
changeset
|
209 (unless start |
95478 | 210 (setq start (point-min) |
211 end (point-max))) | |
91647 | 212 (epa-file--encode-coding-string (buffer-substring start end) |
213 coding-system)) | |
214 (if (or epa-file-select-keys | |
215 (not (local-variable-p 'epa-file-encrypt-to | |
216 (current-buffer)))) | |
217 (epa-select-keys | |
218 context | |
219 "Select recipents for encryption. | |
220 If no one is selected, symmetric encryption will be performed. " | |
221 recipients) | |
222 (if epa-file-encrypt-to | |
223 (epg-list-keys context recipients))))) | |
224 (error | |
225 (if (setq entry (assoc file epa-file-passphrase-alist)) | |
226 (setcdr entry nil)) | |
227 (signal 'file-error (cons "Opening output file" (cdr error))))) | |
228 (epa-file-run-real-handler | |
229 #'write-region | |
230 (list string nil file append visit lockname mustbenew)) | |
231 (if (boundp 'last-coding-system-used) | |
232 (setq last-coding-system-used coding-system)) | |
233 (if (eq visit t) | |
234 (progn | |
235 (setq buffer-file-name file) | |
236 (set-visited-file-modtime)) | |
237 (if (stringp visit) | |
238 (progn | |
239 (set-visited-file-modtime) | |
240 (setq buffer-file-name visit)))) | |
241 (if (or (eq visit t) | |
242 (eq visit nil) | |
243 (stringp visit)) | |
244 (message "Wrote %s" buffer-file-name)))) | |
245 (put 'write-region 'epa-file 'epa-file-write-region) | |
246 | |
247 (defun epa-file-select-keys () | |
248 "Select recipients for encryption." | |
249 (interactive) | |
250 (make-local-variable 'epa-file-encrypt-to) | |
251 (setq epa-file-encrypt-to | |
93002
d876f5372472
EasyPG: Fix bug with C-x C-s after M-x epa-file-select-keys.
Michael Olson <mwolson@gnu.org>
parents:
91731
diff
changeset
|
252 (mapcar |
d876f5372472
EasyPG: Fix bug with C-x C-s after M-x epa-file-select-keys.
Michael Olson <mwolson@gnu.org>
parents:
91731
diff
changeset
|
253 (lambda (key) |
d876f5372472
EasyPG: Fix bug with C-x C-s after M-x epa-file-select-keys.
Michael Olson <mwolson@gnu.org>
parents:
91731
diff
changeset
|
254 (epg-sub-key-id (car (epg-key-sub-key-list key)))) |
91647 | 255 (epa-select-keys |
256 (epg-make-context) | |
257 "Select recipents for encryption. | |
93002
d876f5372472
EasyPG: Fix bug with C-x C-s after M-x epa-file-select-keys.
Michael Olson <mwolson@gnu.org>
parents:
91731
diff
changeset
|
258 If no one is selected, symmetric encryption will be performed. ")))) |
91647 | 259 |
260 ;;;###autoload | |
261 (defun epa-file-enable () | |
262 (interactive) | |
263 (if (memq epa-file-handler file-name-handler-alist) | |
264 (message "`epa-file' already enabled") | |
265 (setq file-name-handler-alist | |
266 (cons epa-file-handler file-name-handler-alist)) | |
94417
1a8916b995cf
* epa-file.el (auto-encryption-mode): Rename from epa-file-mode.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93002
diff
changeset
|
267 (add-hook 'find-file-hook 'epa-file-find-file-hook) |
91647 | 268 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist)) |
269 (message "`epa-file' enabled"))) | |
270 | |
271 ;;;###autoload | |
272 (defun epa-file-disable () | |
273 (interactive) | |
274 (if (memq epa-file-handler file-name-handler-alist) | |
275 (progn | |
276 (setq file-name-handler-alist | |
277 (delq epa-file-handler file-name-handler-alist)) | |
94417
1a8916b995cf
* epa-file.el (auto-encryption-mode): Rename from epa-file-mode.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
93002
diff
changeset
|
278 (remove-hook 'find-file-hook 'epa-file-find-file-hook) |
91647 | 279 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry |
280 auto-mode-alist)) | |
281 (message "`epa-file' disabled")) | |
282 (message "`epa-file' already disabled"))) | |
283 | |
284 (provide 'epa-file) | |
285 | |
91687 | 286 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d |
91647 | 287 ;;; epa-file.el ends here |