Commit | Line | Data |
---|---|---|
c154c0be | 1 | ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption |
ae940284 | 2 | ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
c154c0be MO |
3 | |
4 | ;; Author: Daiki Ueno <ueno@unixuser.org> | |
5 | ;; Keywords: PGP, GnuPG | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
eb3fa2cf | 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
c154c0be | 10 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
11 | ;; the Free Software Foundation, either version 3 of the License, or |
12 | ;; (at your option) any later version. | |
c154c0be MO |
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 | |
eb3fa2cf | 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c154c0be MO |
21 | |
22 | ;;; Code: | |
23 | ||
24 | (require 'epa) | |
3fe68728 | 25 | (require 'epa-hook) |
c154c0be MO |
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 | ||
c154c0be MO |
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 | ||
c154c0be MO |
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 | ||
c154c0be MO |
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 | ||
6f8a4190 | 69 | ;;;###autoload |
c154c0be | 70 | (defun epa-file-handler (operation &rest args) |
dbc9f96d GM |
71 | (save-match-data |
72 | (let ((op (get operation 'epa-file))) | |
73 | (if op | |
74 | (apply op args) | |
75 | (epa-file-run-real-handler operation args))))) | |
76 | ||
c154c0be MO |
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)) | |
68a0d892 DU |
89 | (insert (if enable-multibyte-characters |
90 | (string-to-multibyte string) | |
91 | string)) | |
c154c0be MO |
92 | (decode-coding-inserted-region |
93 | (point-min) (point-max) | |
94 | (substring file 0 (string-match epa-file-name-regexp file)) | |
68a0d892 | 95 | visit beg end replace)) |
c154c0be MO |
96 | (insert (epa-file--decode-coding-string string (or coding-system-for-read |
97 | 'undecided))))) | |
98 | ||
99 | (defvar last-coding-system-used) | |
100 | (defun epa-file-insert-file-contents (file &optional visit beg end replace) | |
101 | (barf-if-buffer-read-only) | |
102 | (if (and visit (or beg end)) | |
103 | (error "Attempt to visit less than an entire file")) | |
104 | (setq file (expand-file-name file)) | |
105 | (let* ((local-copy | |
6ea97db8 | 106 | (condition-case nil |
c154c0be MO |
107 | (epa-file-run-real-handler #'file-local-copy (list file)) |
108 | (error))) | |
109 | (local-file (or local-copy file)) | |
110 | (context (epg-make-context)) | |
111 | string length entry) | |
2cb35a56 DU |
112 | (if visit |
113 | (setq buffer-file-name file)) | |
c154c0be MO |
114 | (epg-context-set-passphrase-callback |
115 | context | |
116 | (cons #'epa-file-passphrase-callback-function | |
117 | local-file)) | |
118 | (epg-context-set-progress-callback context | |
119 | #'epa-progress-callback-function) | |
120 | (unwind-protect | |
121 | (progn | |
122 | (if replace | |
123 | (goto-char (point-min))) | |
124 | (condition-case error | |
125 | (setq string (epg-decrypt-file context local-file nil)) | |
126 | (error | |
127 | (if (setq entry (assoc file epa-file-passphrase-alist)) | |
128 | (setcdr entry nil)) | |
129 | (signal 'file-error | |
130 | (cons "Opening input file" (cdr error))))) | |
131 | (make-local-variable 'epa-file-encrypt-to) | |
132 | (setq epa-file-encrypt-to | |
133 | (mapcar #'car (epg-context-result-for context 'encrypted-to))) | |
134 | (if (or beg end) | |
135 | (setq string (substring string (or beg 0) end))) | |
136 | (save-excursion | |
137 | (save-restriction | |
138 | (narrow-to-region (point) (point)) | |
139 | (epa-file-decode-and-insert string file visit beg end replace) | |
140 | (setq length (- (point-max) (point-min)))) | |
141 | (if replace | |
8e3efc87 | 142 | (delete-region (point) (point-max))) |
2cb35a56 DU |
143 | (if visit |
144 | (set-visited-file-modtime)))) | |
c154c0be MO |
145 | (if (and local-copy |
146 | (file-exists-p local-copy)) | |
147 | (delete-file local-copy))) | |
148 | (list file length))) | |
149 | (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents) | |
150 | ||
151 | (defun epa-file-write-region (start end file &optional append visit lockname | |
152 | mustbenew) | |
153 | (if append | |
154 | (error "Can't append to the file.")) | |
155 | (setq file (expand-file-name file)) | |
156 | (let* ((coding-system (or coding-system-for-write | |
157 | (if (fboundp 'select-safe-coding-system) | |
158 | ;; This is needed since Emacs 22 has | |
159 | ;; no-conversion setting for *.gpg in | |
160 | ;; `auto-coding-alist'. | |
161 | (let ((buffer-file-name | |
162 | (file-name-sans-extension file))) | |
163 | (select-safe-coding-system | |
164 | (point-min) (point-max))) | |
165 | buffer-file-coding-system))) | |
166 | (context (epg-make-context)) | |
167 | (coding-system-for-write 'binary) | |
168 | string entry | |
169 | (recipients | |
170 | (cond | |
171 | ((listp epa-file-encrypt-to) epa-file-encrypt-to) | |
172 | ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to))))) | |
173 | (epg-context-set-passphrase-callback | |
174 | context | |
175 | (cons #'epa-file-passphrase-callback-function | |
176 | file)) | |
177 | (epg-context-set-progress-callback context | |
178 | #'epa-progress-callback-function) | |
179 | (epg-context-set-armor context epa-armor) | |
180 | (condition-case error | |
181 | (setq string | |
182 | (epg-encrypt-string | |
183 | context | |
184 | (if (stringp start) | |
185 | (epa-file--encode-coding-string start coding-system) | |
6843296d | 186 | (unless start |
437eedba DU |
187 | (setq start (point-min) |
188 | end (point-max))) | |
c154c0be MO |
189 | (epa-file--encode-coding-string (buffer-substring start end) |
190 | coding-system)) | |
191 | (if (or epa-file-select-keys | |
192 | (not (local-variable-p 'epa-file-encrypt-to | |
193 | (current-buffer)))) | |
194 | (epa-select-keys | |
195 | context | |
196 | "Select recipents for encryption. | |
197 | If no one is selected, symmetric encryption will be performed. " | |
198 | recipients) | |
199 | (if epa-file-encrypt-to | |
200 | (epg-list-keys context recipients))))) | |
201 | (error | |
202 | (if (setq entry (assoc file epa-file-passphrase-alist)) | |
203 | (setcdr entry nil)) | |
204 | (signal 'file-error (cons "Opening output file" (cdr error))))) | |
205 | (epa-file-run-real-handler | |
206 | #'write-region | |
207 | (list string nil file append visit lockname mustbenew)) | |
208 | (if (boundp 'last-coding-system-used) | |
209 | (setq last-coding-system-used coding-system)) | |
210 | (if (eq visit t) | |
211 | (progn | |
212 | (setq buffer-file-name file) | |
213 | (set-visited-file-modtime)) | |
214 | (if (stringp visit) | |
215 | (progn | |
216 | (set-visited-file-modtime) | |
217 | (setq buffer-file-name visit)))) | |
218 | (if (or (eq visit t) | |
219 | (eq visit nil) | |
220 | (stringp visit)) | |
221 | (message "Wrote %s" buffer-file-name)))) | |
222 | (put 'write-region 'epa-file 'epa-file-write-region) | |
223 | ||
c154c0be MO |
224 | (defun epa-file-select-keys () |
225 | "Select recipients for encryption." | |
226 | (interactive) | |
227 | (make-local-variable 'epa-file-encrypt-to) | |
228 | (setq epa-file-encrypt-to | |
fef7aa9e MO |
229 | (mapcar |
230 | (lambda (key) | |
231 | (epg-sub-key-id (car (epg-key-sub-key-list key)))) | |
c154c0be MO |
232 | (epa-select-keys |
233 | (epg-make-context) | |
234 | "Select recipents for encryption. | |
fef7aa9e | 235 | If no one is selected, symmetric encryption will be performed. ")))) |
c154c0be MO |
236 | |
237 | ;;;###autoload | |
238 | (defun epa-file-enable () | |
239 | (interactive) | |
240 | (if (memq epa-file-handler file-name-handler-alist) | |
241 | (message "`epa-file' already enabled") | |
242 | (setq file-name-handler-alist | |
243 | (cons epa-file-handler file-name-handler-alist)) | |
bfeee9d1 | 244 | (add-hook 'find-file-hook 'epa-file-find-file-hook) |
c154c0be MO |
245 | (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist)) |
246 | (message "`epa-file' enabled"))) | |
247 | ||
248 | ;;;###autoload | |
249 | (defun epa-file-disable () | |
250 | (interactive) | |
251 | (if (memq epa-file-handler file-name-handler-alist) | |
252 | (progn | |
253 | (setq file-name-handler-alist | |
254 | (delq epa-file-handler file-name-handler-alist)) | |
bfeee9d1 | 255 | (remove-hook 'find-file-hook 'epa-file-find-file-hook) |
c154c0be MO |
256 | (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry |
257 | auto-mode-alist)) | |
258 | (message "`epa-file' disabled")) | |
259 | (message "`epa-file' already disabled"))) | |
260 | ||
261 | (provide 'epa-file) | |
262 | ||
37b77401 | 263 | ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d |
c154c0be | 264 | ;;; epa-file.el ends here |