Mercurial > emacs
annotate lisp/epa-file.el @ 101494:12b4262374ec
*** empty log message ***
author | Bill Wohler <wohler@newt.com> |
---|---|
date | Sun, 25 Jan 2009 20:12:21 +0000 |
parents | a9dc0e7c3f2b |
children | 15551118906e |
rev | line source |
---|---|
91647 | 1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption |
100908 | 2 ;; Copyright (C) 2006, 2007, 2008, 2009 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 | |
28 "If non-nil, cache passphrase for symmetric encryption." | |
29 :type 'boolean | |
30 :group 'epa-file) | |
31 | |
32 (defcustom epa-file-select-keys nil | |
33 "If non-nil, always asks user to select recipients." | |
34 :type 'boolean | |
35 :group 'epa-file) | |
36 | |
37 (defvar epa-file-passphrase-alist nil) | |
38 | |
39 (eval-and-compile | |
40 (if (fboundp 'encode-coding-string) | |
41 (defalias 'epa-file--encode-coding-string 'encode-coding-string) | |
42 (defalias 'epa-file--encode-coding-string 'identity))) | |
43 | |
44 (eval-and-compile | |
45 (if (fboundp 'decode-coding-string) | |
46 (defalias 'epa-file--decode-coding-string 'decode-coding-string) | |
47 (defalias 'epa-file--decode-coding-string 'identity))) | |
48 | |
49 (defun epa-file-passphrase-callback-function (context key-id file) | |
50 (if (and epa-file-cache-passphrase-for-symmetric-encryption | |
51 (eq key-id 'SYM)) | |
52 (progn | |
53 (setq file (file-truename file)) | |
54 (let ((entry (assoc file epa-file-passphrase-alist)) | |
55 passphrase) | |
56 (or (copy-sequence (cdr entry)) | |
57 (progn | |
58 (unless entry | |
59 (setq entry (list file) | |
60 epa-file-passphrase-alist | |
61 (cons entry | |
62 epa-file-passphrase-alist))) | |
63 (setq passphrase (epa-passphrase-callback-function context | |
64 key-id nil)) | |
65 (setcdr entry (copy-sequence passphrase)) | |
66 passphrase)))) | |
67 (epa-passphrase-callback-function context key-id nil))) | |
68 | |
94512
d10cd2d8d9ae
* epa-file-hook.el: New file split from epa-file.el.
Daiki Ueno <ueno@unixuser.org>
parents:
94423
diff
changeset
|
69 ;;;###autoload |
91647 | 70 (defun epa-file-handler (operation &rest args) |
94423
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
71 (save-match-data |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
72 (let ((op (get operation 'epa-file))) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
73 (if op |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
74 (apply op args) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
75 (epa-file-run-real-handler operation args))))) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
76 |
91647 | 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))) | |
84 | |
85 (defun epa-file-decode-and-insert (string file visit beg end replace) | |
86 (if (fboundp 'decode-coding-inserted-region) | |
87 (save-restriction | |
88 (narrow-to-region (point) (point)) | |
89 (let ((multibyte enable-multibyte-characters)) | |
90 (set-buffer-multibyte nil) | |
91 (insert string) | |
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 | |
98 'undecided))))) | |
99 | |
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)) | |
106 (let* ((local-copy | |
98409
cb5ef7767703
(epa-file-insert-file-contents): Fix typo.
Daiki Ueno <ueno@unixuser.org>
parents:
95478
diff
changeset
|
107 (condition-case nil |
91647 | 108 (epa-file-run-real-handler #'file-local-copy (list file)) |
109 (error))) | |
110 (local-file (or local-copy file)) | |
111 (context (epg-make-context)) | |
112 string length entry) | |
100456
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
113 (if visit |
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
114 (setq buffer-file-name file)) |
91647 | 115 (epg-context-set-passphrase-callback |
116 context | |
117 (cons #'epa-file-passphrase-callback-function | |
118 local-file)) | |
119 (epg-context-set-progress-callback context | |
120 #'epa-progress-callback-function) | |
121 (unwind-protect | |
122 (progn | |
123 (if replace | |
124 (goto-char (point-min))) | |
125 (condition-case error | |
126 (setq string (epg-decrypt-file context local-file nil)) | |
127 (error | |
128 (if (setq entry (assoc file epa-file-passphrase-alist)) | |
129 (setcdr entry nil)) | |
130 (signal 'file-error | |
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))) | |
135 (if (or beg end) | |
136 (setq string (substring string (or beg 0) end))) | |
137 (save-excursion | |
138 (save-restriction | |
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)))) | |
142 (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
|
143 (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
|
144 (if visit |
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
145 (set-visited-file-modtime)))) |
91647 | 146 (if (and local-copy |
147 (file-exists-p local-copy)) | |
148 (delete-file local-copy))) | |
149 (list file length))) | |
150 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents) | |
151 | |
152 (defun epa-file-write-region (start end file &optional append visit lockname | |
153 mustbenew) | |
154 (if append | |
155 (error "Can't append to the file.")) | |
156 (setq file (expand-file-name file)) | |
157 (let* ((coding-system (or coding-system-for-write | |
158 (if (fboundp 'select-safe-coding-system) | |
159 ;; This is needed since Emacs 22 has | |
160 ;; no-conversion setting for *.gpg in | |
161 ;; `auto-coding-alist'. | |
162 (let ((buffer-file-name | |
163 (file-name-sans-extension file))) | |
164 (select-safe-coding-system | |
165 (point-min) (point-max))) | |
166 buffer-file-coding-system))) | |
167 (context (epg-make-context)) | |
168 (coding-system-for-write 'binary) | |
169 string entry | |
170 (recipients | |
171 (cond | |
172 ((listp epa-file-encrypt-to) epa-file-encrypt-to) | |
173 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to))))) | |
174 (epg-context-set-passphrase-callback | |
175 context | |
176 (cons #'epa-file-passphrase-callback-function | |
177 file)) | |
178 (epg-context-set-progress-callback context | |
179 #'epa-progress-callback-function) | |
180 (epg-context-set-armor context epa-armor) | |
181 (condition-case error | |
182 (setq string | |
183 (epg-encrypt-string | |
184 context | |
185 (if (stringp start) | |
186 (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
|
187 (unless start |
95478 | 188 (setq start (point-min) |
189 end (point-max))) | |
91647 | 190 (epa-file--encode-coding-string (buffer-substring start end) |
191 coding-system)) | |
192 (if (or epa-file-select-keys | |
193 (not (local-variable-p 'epa-file-encrypt-to | |
194 (current-buffer)))) | |
195 (epa-select-keys | |
196 context | |
197 "Select recipents for encryption. | |
198 If no one is selected, symmetric encryption will be performed. " | |
199 recipients) | |
200 (if epa-file-encrypt-to | |
201 (epg-list-keys context recipients))))) | |
202 (error | |
203 (if (setq entry (assoc file epa-file-passphrase-alist)) | |
204 (setcdr entry nil)) | |
205 (signal 'file-error (cons "Opening output file" (cdr error))))) | |
206 (epa-file-run-real-handler | |
207 #'write-region | |
208 (list string nil file append visit lockname mustbenew)) | |
209 (if (boundp 'last-coding-system-used) | |
210 (setq last-coding-system-used coding-system)) | |
211 (if (eq visit t) | |
212 (progn | |
213 (setq buffer-file-name file) | |
214 (set-visited-file-modtime)) | |
215 (if (stringp visit) | |
216 (progn | |
217 (set-visited-file-modtime) | |
218 (setq buffer-file-name visit)))) | |
219 (if (or (eq visit t) | |
220 (eq visit nil) | |
221 (stringp visit)) | |
222 (message "Wrote %s" buffer-file-name)))) | |
223 (put 'write-region 'epa-file 'epa-file-write-region) | |
224 | |
225 (defun epa-file-select-keys () | |
226 "Select recipients for encryption." | |
227 (interactive) | |
228 (make-local-variable 'epa-file-encrypt-to) | |
229 (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
|
230 (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
|
231 (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
|
232 (epg-sub-key-id (car (epg-key-sub-key-list key)))) |
91647 | 233 (epa-select-keys |
234 (epg-make-context) | |
235 "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
|
236 If no one is selected, symmetric encryption will be performed. ")))) |
91647 | 237 |
238 ;;;###autoload | |
239 (defun epa-file-enable () | |
240 (interactive) | |
241 (if (memq epa-file-handler file-name-handler-alist) | |
242 (message "`epa-file' already enabled") | |
243 (setq file-name-handler-alist | |
244 (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
|
245 (add-hook 'find-file-hook 'epa-file-find-file-hook) |
91647 | 246 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist)) |
247 (message "`epa-file' enabled"))) | |
248 | |
249 ;;;###autoload | |
250 (defun epa-file-disable () | |
251 (interactive) | |
252 (if (memq epa-file-handler file-name-handler-alist) | |
253 (progn | |
254 (setq file-name-handler-alist | |
255 (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
|
256 (remove-hook 'find-file-hook 'epa-file-find-file-hook) |
91647 | 257 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry |
258 auto-mode-alist)) | |
259 (message "`epa-file' disabled")) | |
260 (message "`epa-file' already disabled"))) | |
261 | |
262 (provide 'epa-file) | |
263 | |
91687 | 264 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d |
91647 | 265 ;;; epa-file.el ends here |