* tramp.texi: Use consistently "Emacs" (instead of "GNU Emacs") and
[emacs/old-mirror.git] / lisp / epa-file.el
blobc5c43a724e8c5549508084e35a012718d4d06525
1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption
2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
7 ;; This file is part of GNU Emacs.
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 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'epa)
25 (require 'epa-hook)
27 (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil
28 "If non-nil, cache passphrase for symmetric encryption.
30 For security reasons, this option is turned off by default and
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."
34 :type 'boolean
35 :group 'epa-file)
37 (defcustom epa-file-select-keys nil
38 "If non-nil, always asks user to select recipients."
39 :type 'boolean
40 :group 'epa-file)
42 (defvar epa-file-passphrase-alist nil)
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)))
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)))
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)))
74 ;;;###autoload
75 (defun epa-file-handler (operation &rest args)
76 (save-match-data
77 (let ((op (get operation 'epa-file)))
78 (if op
79 (apply op args)
80 (epa-file-run-real-handler operation args)))))
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)))
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))
94 (insert (if enable-multibyte-characters
95 (string-to-multibyte string)
96 string))
97 (decode-coding-inserted-region
98 (point-min) (point-max)
99 (substring file 0 (string-match epa-file-name-regexp file))
100 visit beg end replace))
101 (insert (epa-file--decode-coding-string string (or coding-system-for-read
102 'undecided)))))
104 (defvar last-coding-system-used)
105 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
106 (barf-if-buffer-read-only)
107 (if (and visit (or beg end))
108 (error "Attempt to visit less than an entire file"))
109 (setq file (expand-file-name file))
110 (let* ((local-copy
111 (condition-case nil
112 (epa-file-run-real-handler #'file-local-copy (list file))
113 (error)))
114 (local-file (or local-copy file))
115 (context (epg-make-context))
116 string length entry)
117 (if visit
118 (setq buffer-file-name file))
119 (epg-context-set-passphrase-callback
120 context
121 (cons #'epa-file-passphrase-callback-function
122 local-file))
123 (epg-context-set-progress-callback context
124 #'epa-progress-callback-function)
125 (unwind-protect
126 (progn
127 (if replace
128 (goto-char (point-min)))
129 (condition-case error
130 (setq string (epg-decrypt-file context local-file nil))
131 (error
132 (if (setq entry (assoc file epa-file-passphrase-alist))
133 (setcdr entry nil))
134 (signal 'file-error
135 (cons "Opening input file" (cdr error)))))
136 (make-local-variable 'epa-file-encrypt-to)
137 (setq epa-file-encrypt-to
138 (mapcar #'car (epg-context-result-for context 'encrypted-to)))
139 (if (or beg end)
140 (setq string (substring string (or beg 0) end)))
141 (save-excursion
142 (save-restriction
143 (narrow-to-region (point) (point))
144 (epa-file-decode-and-insert string file visit beg end replace)
145 (setq length (- (point-max) (point-min))))
146 (if replace
147 (delete-region (point) (point-max)))
148 (if visit
149 (set-visited-file-modtime))))
150 (if (and local-copy
151 (file-exists-p local-copy))
152 (let ((delete-by-moving-to-trash nil))
153 (delete-file local-copy))))
154 (list file length)))
155 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
157 (defun epa-file-write-region (start end file &optional append visit lockname
158 mustbenew)
159 (if append
160 (error "Can't append to the file"))
161 (setq file (expand-file-name file))
162 (let* ((coding-system (or coding-system-for-write
163 (if (fboundp 'select-safe-coding-system)
164 ;; This is needed since Emacs 22 has
165 ;; no-conversion setting for *.gpg in
166 ;; `auto-coding-alist'.
167 (let ((buffer-file-name
168 (file-name-sans-extension file)))
169 (select-safe-coding-system
170 (point-min) (point-max)))
171 buffer-file-coding-system)))
172 (context (epg-make-context))
173 (coding-system-for-write 'binary)
174 string entry
175 (recipients
176 (cond
177 ((listp epa-file-encrypt-to) epa-file-encrypt-to)
178 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))
179 (epg-context-set-passphrase-callback
180 context
181 (cons #'epa-file-passphrase-callback-function
182 file))
183 (epg-context-set-progress-callback context
184 #'epa-progress-callback-function)
185 (epg-context-set-armor context epa-armor)
186 (condition-case error
187 (setq string
188 (epg-encrypt-string
189 context
190 (if (stringp start)
191 (epa-file--encode-coding-string start coding-system)
192 (unless start
193 (setq start (point-min)
194 end (point-max)))
195 (epa-file--encode-coding-string (buffer-substring start end)
196 coding-system))
197 (if (or epa-file-select-keys
198 (not (local-variable-p 'epa-file-encrypt-to
199 (current-buffer))))
200 (epa-select-keys
201 context
202 "Select recipents for encryption.
203 If no one is selected, symmetric encryption will be performed. "
204 recipients)
205 (if epa-file-encrypt-to
206 (epg-list-keys context recipients)))))
207 (error
208 (if (setq entry (assoc file epa-file-passphrase-alist))
209 (setcdr entry nil))
210 (signal 'file-error (cons "Opening output file" (cdr error)))))
211 (epa-file-run-real-handler
212 #'write-region
213 (list string nil file append visit lockname mustbenew))
214 (if (boundp 'last-coding-system-used)
215 (setq last-coding-system-used coding-system))
216 (if (eq visit t)
217 (progn
218 (setq buffer-file-name file)
219 (set-visited-file-modtime))
220 (if (stringp visit)
221 (progn
222 (set-visited-file-modtime)
223 (setq buffer-file-name visit))))
224 (if (or (eq visit t)
225 (eq visit nil)
226 (stringp visit))
227 (message "Wrote %s" buffer-file-name))))
228 (put 'write-region 'epa-file 'epa-file-write-region)
230 (defun epa-file-select-keys ()
231 "Select recipients for encryption."
232 (interactive)
233 (make-local-variable 'epa-file-encrypt-to)
234 (setq epa-file-encrypt-to
235 (mapcar
236 (lambda (key)
237 (epg-sub-key-id (car (epg-key-sub-key-list key))))
238 (epa-select-keys
239 (epg-make-context)
240 "Select recipents for encryption.
241 If no one is selected, symmetric encryption will be performed. "))))
243 ;;;###autoload
244 (defun epa-file-enable ()
245 (interactive)
246 (if (memq epa-file-handler file-name-handler-alist)
247 (message "`epa-file' already enabled")
248 (setq file-name-handler-alist
249 (cons epa-file-handler file-name-handler-alist))
250 (add-hook 'find-file-hook 'epa-file-find-file-hook)
251 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist))
252 (message "`epa-file' enabled")))
254 ;;;###autoload
255 (defun epa-file-disable ()
256 (interactive)
257 (if (memq epa-file-handler file-name-handler-alist)
258 (progn
259 (setq file-name-handler-alist
260 (delq epa-file-handler file-name-handler-alist))
261 (remove-hook 'find-file-hook 'epa-file-find-file-hook)
262 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry
263 auto-mode-alist))
264 (message "`epa-file' disabled"))
265 (message "`epa-file' already disabled")))
267 (provide 'epa-file)
269 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d
270 ;;; epa-file.el ends here