Mercurial > emacs
annotate lisp/epa-file.el @ 110313:50c8f347bd8e
* lisp/net/rcirc.el (rcirc-server-commands, rcirc-client-commands)
(rcirc-completion-start): New variables.
(rcirc-nick-completions): Rename to rcirc-completions.
(rcirc-nick-completion-start-offset): Delete.
(rcirc-completion-at-point): New function for constructing
completion data for both nicks and irc commands. Add to
completion-at-point-functions in rcirc mode.
(rcirc-complete): Rename from rcirc-nick-complete; use
rcirc-completion-at-point.
(defun-rcirc-command): Update rcirc-client-commands.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Sun, 12 Sep 2010 13:06:19 +0200 |
parents | 280c8ae2476d |
children | 3386110d149b |
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 | |
110015
280c8ae2476d
Add "Package:" file headers to denote built-in packages.
Chong Yidong <cyd@stupidchicken.com>
parents:
109535
diff
changeset
|
6 ;; Package: epa |
91647 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94512
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
91647 | 11 ;; 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
|
12 ;; 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
|
13 ;; (at your option) any later version. |
91647 | 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 | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94512
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
91647 | 22 |
23 ;;; Code: | |
24 | |
25 (require 'epa) | |
94755
14eda1195ec3
Provide/require epa-hook, not epa-file-hook.
Eli Zaretskii <eliz@gnu.org>
parents:
94678
diff
changeset
|
26 (require 'epa-hook) |
91647 | 27 |
28 (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
|
29 "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
|
30 |
e9b2a988e884
(epa-file-cache-passphrase-for-symmetric-encryption):
Daiki Ueno <ueno@unixuser.org>
parents:
103016
diff
changeset
|
31 For security reasons, this option is turned off by default and |
105121 | 32 not recommended to use. Instead, consider using public-key |
33 encryption with gpg-agent which does the same job in a safer | |
34 way." | |
91647 | 35 :type 'boolean |
36 :group 'epa-file) | |
37 | |
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 | |
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 | |
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 | |
94512
d10cd2d8d9ae
* epa-file-hook.el: New file split from epa-file.el.
Daiki Ueno <ueno@unixuser.org>
parents:
94423
diff
changeset
|
75 ;;;###autoload |
91647 | 76 (defun epa-file-handler (operation &rest args) |
94423
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
77 (save-match-data |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
78 (let ((op (get operation 'epa-file))) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
79 (if op |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
80 (apply op args) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
81 (epa-file-run-real-handler operation args))))) |
3898a5665f50
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94419
diff
changeset
|
82 |
91647 | 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)) | |
103016
15551118906e
epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
95 (insert (if enable-multibyte-characters |
15551118906e
epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
96 (string-to-multibyte string) |
15551118906e
epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
97 string)) |
91647 | 98 (decode-coding-inserted-region |
99 (point-min) (point-max) | |
100 (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
|
101 visit beg end replace)) |
91647 | 102 (insert (epa-file--decode-coding-string string (or coding-system-for-read |
103 'undecided))))) | |
104 | |
109166
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
105 (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
|
106 (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
|
107 (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
|
108 (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
|
109 (kill-buffer)) |
b70c159d9e1e
Prevent find-file from opening empty buffer when decryptin failed (bug#6568).
Daiki Ueno <ueno@unixuser.org>
parents:
106815
diff
changeset
|
110 (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
|
111 (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
|
112 |
91647 | 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 | |
98409
cb5ef7767703
(epa-file-insert-file-contents): Fix typo.
Daiki Ueno <ueno@unixuser.org>
parents:
95478
diff
changeset
|
120 (condition-case nil |
91647 | 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) | |
100456
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
126 (if visit |
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
127 (setq buffer-file-name file)) |
91647 | 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)) | |
109535 | 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'. | |
109534
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
147 (when (file-exists-p local-file) |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
148 (make-local-variable 'epa-file-error) |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
149 (setq epa-file-error error) |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
150 (add-hook 'find-file-not-found-functions |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
151 'epa-file--find-file-not-found-function |
52f611292230
Fix *.gpg file creation (bug#6723).
Daiki Ueno <ueno@unixuser.org>
parents:
109166
diff
changeset
|
152 nil t)) |
91647 | 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 | |
161 (save-restriction | |
162 (narrow-to-region (point) (point)) | |
163 (epa-file-decode-and-insert string file visit beg end replace) | |
164 (setq length (- (point-max) (point-min)))) | |
165 (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
|
166 (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
|
167 (if visit |
e4798aef9a53
* epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents:
100304
diff
changeset
|
168 (set-visited-file-modtime)))) |
91647 | 169 (if (and local-copy |
170 (file-exists-p local-copy)) | |
171 (delete-file local-copy))) | |
172 (list file length))) | |
173 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents) | |
174 | |
175 (defun epa-file-write-region (start end file &optional append visit lockname | |
176 mustbenew) | |
177 (if append | |
105170 | 178 (error "Can't append to the file")) |
91647 | 179 (setq file (expand-file-name file)) |
180 (let* ((coding-system (or coding-system-for-write | |
181 (if (fboundp 'select-safe-coding-system) | |
182 ;; This is needed since Emacs 22 has | |
183 ;; no-conversion setting for *.gpg in | |
184 ;; `auto-coding-alist'. | |
185 (let ((buffer-file-name | |
186 (file-name-sans-extension file))) | |
187 (select-safe-coding-system | |
188 (point-min) (point-max))) | |
189 buffer-file-coding-system))) | |
190 (context (epg-make-context)) | |
191 (coding-system-for-write 'binary) | |
192 string entry | |
193 (recipients | |
194 (cond | |
195 ((listp epa-file-encrypt-to) epa-file-encrypt-to) | |
196 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to))))) | |
197 (epg-context-set-passphrase-callback | |
198 context | |
199 (cons #'epa-file-passphrase-callback-function | |
200 file)) | |
201 (epg-context-set-progress-callback context | |
202 #'epa-progress-callback-function) | |
203 (epg-context-set-armor context epa-armor) | |
204 (condition-case error | |
205 (setq string | |
206 (epg-encrypt-string | |
207 context | |
208 (if (stringp start) | |
209 (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
|
210 (unless start |
95478 | 211 (setq start (point-min) |
212 end (point-max))) | |
91647 | 213 (epa-file--encode-coding-string (buffer-substring start end) |
214 coding-system)) | |
215 (if (or epa-file-select-keys | |
216 (not (local-variable-p 'epa-file-encrypt-to | |
217 (current-buffer)))) | |
218 (epa-select-keys | |
219 context | |
220 "Select recipents for encryption. | |
221 If no one is selected, symmetric encryption will be performed. " | |
222 recipients) | |
223 (if epa-file-encrypt-to | |
224 (epg-list-keys context recipients))))) | |
225 (error | |
226 (if (setq entry (assoc file epa-file-passphrase-alist)) | |
227 (setcdr entry nil)) | |
228 (signal 'file-error (cons "Opening output file" (cdr error))))) | |
229 (epa-file-run-real-handler | |
230 #'write-region | |
231 (list string nil file append visit lockname mustbenew)) | |
232 (if (boundp 'last-coding-system-used) | |
233 (setq last-coding-system-used coding-system)) | |
234 (if (eq visit t) | |
235 (progn | |
236 (setq buffer-file-name file) | |
237 (set-visited-file-modtime)) | |
238 (if (stringp visit) | |
239 (progn | |
240 (set-visited-file-modtime) | |
241 (setq buffer-file-name visit)))) | |
242 (if (or (eq visit t) | |
243 (eq visit nil) | |
244 (stringp visit)) | |
245 (message "Wrote %s" buffer-file-name)))) | |
246 (put 'write-region 'epa-file 'epa-file-write-region) | |
247 | |
248 (defun epa-file-select-keys () | |
249 "Select recipients for encryption." | |
250 (interactive) | |
251 (make-local-variable 'epa-file-encrypt-to) | |
252 (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
|
253 (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
|
254 (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
|
255 (epg-sub-key-id (car (epg-key-sub-key-list key)))) |
91647 | 256 (epa-select-keys |
257 (epg-make-context) | |
258 "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
|
259 If no one is selected, symmetric encryption will be performed. ")))) |
91647 | 260 |
261 ;;;###autoload | |
262 (defun epa-file-enable () | |
263 (interactive) | |
264 (if (memq epa-file-handler file-name-handler-alist) | |
265 (message "`epa-file' already enabled") | |
266 (setq file-name-handler-alist | |
267 (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
|
268 (add-hook 'find-file-hook 'epa-file-find-file-hook) |
91647 | 269 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist)) |
270 (message "`epa-file' enabled"))) | |
271 | |
272 ;;;###autoload | |
273 (defun epa-file-disable () | |
274 (interactive) | |
275 (if (memq epa-file-handler file-name-handler-alist) | |
276 (progn | |
277 (setq file-name-handler-alist | |
278 (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
|
279 (remove-hook 'find-file-hook 'epa-file-find-file-hook) |
91647 | 280 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry |
281 auto-mode-alist)) | |
282 (message "`epa-file' disabled")) | |
283 (message "`epa-file' already disabled"))) | |
284 | |
285 (provide 'epa-file) | |
286 | |
91687 | 287 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d |
91647 | 288 ;;; epa-file.el ends here |