Mercurial > emacs
annotate lisp/epa-file.el @ 94667:43d30a1ea764
Switch to recommended form of GPLv3 permissions notice.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Tue, 06 May 2008 04:27:31 +0000 |
parents | d10cd2d8d9ae |
children | ee5932bf781d |
rev | line source |
---|---|
91647 | 1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption |
2 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Daiki Ueno <ueno@unixuser.org> | |
5 ;; Keywords: PGP, GnuPG | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
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) | |
12 ;; any later version. | |
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 | |
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. | |
23 | |
24 ;;; Code: | |
25 | |
26 (require 'epa) | |
94512
d10cd2d8d9ae
* epa-file-hook.el: New file split from epa-file.el.
Daiki Ueno <ueno@unixuser.org>
parents:
94423
diff
changeset
|
27 (require 'epa-file-hook) |
91647 | 28 |
29 (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil | |
30 "If non-nil, cache passphrase for symmetric encryption." | |
31 :type 'boolean | |
32 :group 'epa-file) | |
33 | |
34 (defcustom epa-file-select-keys nil | |
35 "If non-nil, always asks user to select recipients." | |
36 :type 'boolean | |
37 :group 'epa-file) | |
38 | |
39 (defvar epa-file-passphrase-alist nil) | |
40 | |
41 (eval-and-compile | |
42 (if (fboundp 'encode-coding-string) | |
43 (defalias 'epa-file--encode-coding-string 'encode-coding-string) | |
44 (defalias 'epa-file--encode-coding-string 'identity))) | |
45 | |
46 (eval-and-compile | |
47 (if (fboundp 'decode-coding-string) | |
48 (defalias 'epa-file--decode-coding-string 'decode-coding-string) | |
49 (defalias 'epa-file--decode-coding-string 'identity))) | |
50 | |
51 (defun epa-file-passphrase-callback-function (context key-id file) | |
52 (if (and epa-file-cache-passphrase-for-symmetric-encryption | |
53 (eq key-id 'SYM)) | |
54 (progn | |
55 (setq file (file-truename file)) | |
56 (let ((entry (assoc file epa-file-passphrase-alist)) | |
57 passphrase) | |
58 (or (copy-sequence (cdr entry)) | |
59 (progn | |
60 (unless entry | |
61 (setq entry (list file) | |
62 epa-file-passphrase-alist | |
63 (cons entry | |
64 epa-file-passphrase-alist))) | |
65 (setq passphrase (epa-passphrase-callback-function context | |
66 key-id nil)) | |
67 (setcdr entry (copy-sequence passphrase)) | |
68 passphrase)))) | |
69 (epa-passphrase-callback-function context key-id nil))) | |
70 | |
94512
d10cd2d8d9ae
* epa-file-hook.el: New file split from epa-file.el.
Daiki Ueno <ueno@unixuser.org>
parents:
94423
diff
changeset
|
71 ;;;###autoload |
91647 | 72 (defun epa-file-handler (operation &rest args) |
94423
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
73 (save-match-data |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
74 (let ((op (get operation 'epa-file))) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
75 (if op |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
76 (apply op args) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
77 (epa-file-run-real-handler operation args))))) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
78 |
91647 | 79 (defun epa-file-run-real-handler (operation args) |
80 (let ((inhibit-file-name-handlers | |
81 (cons 'epa-file-handler | |
82 (and (eq inhibit-file-name-operation operation) | |
83 inhibit-file-name-handlers))) | |
84 (inhibit-file-name-operation operation)) | |
85 (apply operation args))) | |
86 | |
87 (defun epa-file-decode-and-insert (string file visit beg end replace) | |
88 (if (fboundp 'decode-coding-inserted-region) | |
89 (save-restriction | |
90 (narrow-to-region (point) (point)) | |
91 (let ((multibyte enable-multibyte-characters)) | |
92 (set-buffer-multibyte nil) | |
93 (insert string) | |
94 (set-buffer-multibyte multibyte) | |
95 (decode-coding-inserted-region | |
96 (point-min) (point-max) | |
97 (substring file 0 (string-match epa-file-name-regexp file)) | |
98 visit beg end replace))) | |
99 (insert (epa-file--decode-coding-string string (or coding-system-for-read | |
100 'undecided))))) | |
101 | |
102 (defvar last-coding-system-used) | |
103 (defun epa-file-insert-file-contents (file &optional visit beg end replace) | |
104 (barf-if-buffer-read-only) | |
105 (if (and visit (or beg end)) | |
106 (error "Attempt to visit less than an entire file")) | |
107 (setq file (expand-file-name file)) | |
108 (let* ((local-copy | |
109 (condition-case inl | |
110 (epa-file-run-real-handler #'file-local-copy (list file)) | |
111 (error))) | |
112 (local-file (or local-copy file)) | |
113 (context (epg-make-context)) | |
114 string length entry) | |
115 (if visit | |
116 (setq buffer-file-name file)) | |
117 (epg-context-set-passphrase-callback | |
118 context | |
119 (cons #'epa-file-passphrase-callback-function | |
120 local-file)) | |
121 (epg-context-set-progress-callback context | |
122 #'epa-progress-callback-function) | |
123 (unwind-protect | |
124 (progn | |
125 (if replace | |
126 (goto-char (point-min))) | |
127 (condition-case error | |
128 (setq string (epg-decrypt-file context local-file nil)) | |
129 (error | |
130 (if (setq entry (assoc file epa-file-passphrase-alist)) | |
131 (setcdr entry nil)) | |
132 (signal 'file-error | |
133 (cons "Opening input file" (cdr error))))) | |
134 (make-local-variable 'epa-file-encrypt-to) | |
135 (setq epa-file-encrypt-to | |
136 (mapcar #'car (epg-context-result-for context 'encrypted-to))) | |
137 (if (or beg end) | |
138 (setq string (substring string (or beg 0) end))) | |
139 (save-excursion | |
140 (save-restriction | |
141 (narrow-to-region (point) (point)) | |
142 (epa-file-decode-and-insert string file visit beg end replace) | |
143 (setq length (- (point-max) (point-min)))) | |
144 (if replace | |
145 (delete-region (point) (point-max))))) | |
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) | |
187 (epa-file--encode-coding-string (buffer-substring start end) | |
188 coding-system)) | |
189 (if (or epa-file-select-keys | |
190 (not (local-variable-p 'epa-file-encrypt-to | |
191 (current-buffer)))) | |
192 (epa-select-keys | |
193 context | |
194 "Select recipents for encryption. | |
195 If no one is selected, symmetric encryption will be performed. " | |
196 recipients) | |
197 (if epa-file-encrypt-to | |
198 (epg-list-keys context recipients))))) | |
199 (error | |
200 (if (setq entry (assoc file epa-file-passphrase-alist)) | |
201 (setcdr entry nil)) | |
202 (signal 'file-error (cons "Opening output file" (cdr error))))) | |
203 (epa-file-run-real-handler | |
204 #'write-region | |
205 (list string nil file append visit lockname mustbenew)) | |
206 (if (boundp 'last-coding-system-used) | |
207 (setq last-coding-system-used coding-system)) | |
208 (if (eq visit t) | |
209 (progn | |
210 (setq buffer-file-name file) | |
211 (set-visited-file-modtime)) | |
212 (if (stringp visit) | |
213 (progn | |
214 (set-visited-file-modtime) | |
215 (setq buffer-file-name visit)))) | |
216 (if (or (eq visit t) | |
217 (eq visit nil) | |
218 (stringp visit)) | |
219 (message "Wrote %s" buffer-file-name)))) | |
220 (put 'write-region 'epa-file 'epa-file-write-region) | |
221 | |
222 (defun epa-file-select-keys () | |
223 "Select recipients for encryption." | |
224 (interactive) | |
225 (make-local-variable 'epa-file-encrypt-to) | |
226 (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
|
227 (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
|
228 (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
|
229 (epg-sub-key-id (car (epg-key-sub-key-list key)))) |
91647 | 230 (epa-select-keys |
231 (epg-make-context) | |
232 "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
|
233 If no one is selected, symmetric encryption will be performed. ")))) |
91647 | 234 |
235 ;;;###autoload | |
236 (defun epa-file-enable () | |
237 (interactive) | |
238 (if (memq epa-file-handler file-name-handler-alist) | |
239 (message "`epa-file' already enabled") | |
240 (setq file-name-handler-alist | |
241 (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
|
242 (add-hook 'find-file-hook 'epa-file-find-file-hook) |
91647 | 243 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist)) |
244 (message "`epa-file' enabled"))) | |
245 | |
246 ;;;###autoload | |
247 (defun epa-file-disable () | |
248 (interactive) | |
249 (if (memq epa-file-handler file-name-handler-alist) | |
250 (progn | |
251 (setq file-name-handler-alist | |
252 (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
|
253 (remove-hook 'find-file-hook 'epa-file-find-file-hook) |
91647 | 254 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry |
255 auto-mode-alist)) | |
256 (message "`epa-file' disabled")) | |
257 (message "`epa-file' already disabled"))) | |
258 | |
259 (provide 'epa-file) | |
260 | |
91687 | 261 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d |
91647 | 262 ;;; epa-file.el ends here |