Use offsetof instead of own definition
[emacs.git] / lisp / epa-file.el
blob5870f23bf13a94e03495e36c694de126322734ca
1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption
2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 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 epa-file-error nil)
105 (defun epa-file--find-file-not-found-function ()
106 (let ((error epa-file-error))
107 (save-window-excursion
108 (kill-buffer))
109 (signal 'file-error
110 (cons "Opening input file" (cdr error)))))
112 (defvar last-coding-system-used)
113 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
114 (barf-if-buffer-read-only)
115 (if (and visit (or beg end))
116 (error "Attempt to visit less than an entire file"))
117 (setq file (expand-file-name file))
118 (let* ((local-copy
119 (condition-case nil
120 (epa-file-run-real-handler #'file-local-copy (list file))
121 (error)))
122 (local-file (or local-copy file))
123 (context (epg-make-context))
124 string length entry)
125 (if visit
126 (setq buffer-file-name file))
127 (epg-context-set-passphrase-callback
128 context
129 (cons #'epa-file-passphrase-callback-function
130 local-file))
131 (epg-context-set-progress-callback context
132 #'epa-progress-callback-function)
133 (unwind-protect
134 (progn
135 (if replace
136 (goto-char (point-min)))
137 (condition-case error
138 (setq string (epg-decrypt-file context local-file nil))
139 (error
140 (if (setq entry (assoc file epa-file-passphrase-alist))
141 (setcdr entry nil))
142 ;; Hack to prevent find-file from opening empty buffer
143 ;; when decryption failed (bug#6568). See the place
144 ;; where `find-file-not-found-functions' are called in
145 ;; `find-file-noselect-1'.
146 (make-local-variable 'epa-file-error)
147 (setq epa-file-error error)
148 (add-hook 'find-file-not-found-functions
149 'epa-file--find-file-not-found-function
150 nil t)
151 (signal 'file-error
152 (cons "Opening input file" (cdr error)))))
153 (make-local-variable 'epa-file-encrypt-to)
154 (setq epa-file-encrypt-to
155 (mapcar #'car (epg-context-result-for context 'encrypted-to)))
156 (if (or beg end)
157 (setq string (substring string (or beg 0) end)))
158 (save-excursion
159 (save-restriction
160 (narrow-to-region (point) (point))
161 (epa-file-decode-and-insert string file visit beg end replace)
162 (setq length (- (point-max) (point-min))))
163 (if replace
164 (delete-region (point) (point-max)))
165 (if visit
166 (set-visited-file-modtime))))
167 (if (and local-copy
168 (file-exists-p local-copy))
169 (delete-file local-copy)))
170 (list file length)))
171 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
173 (defun epa-file-write-region (start end file &optional append visit lockname
174 mustbenew)
175 (if append
176 (error "Can't append to the file"))
177 (setq file (expand-file-name file))
178 (let* ((coding-system (or coding-system-for-write
179 (if (fboundp 'select-safe-coding-system)
180 ;; This is needed since Emacs 22 has
181 ;; no-conversion setting for *.gpg in
182 ;; `auto-coding-alist'.
183 (let ((buffer-file-name
184 (file-name-sans-extension file)))
185 (select-safe-coding-system
186 (point-min) (point-max)))
187 buffer-file-coding-system)))
188 (context (epg-make-context))
189 (coding-system-for-write 'binary)
190 string entry
191 (recipients
192 (cond
193 ((listp epa-file-encrypt-to) epa-file-encrypt-to)
194 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))
195 (epg-context-set-passphrase-callback
196 context
197 (cons #'epa-file-passphrase-callback-function
198 file))
199 (epg-context-set-progress-callback context
200 #'epa-progress-callback-function)
201 (epg-context-set-armor context epa-armor)
202 (condition-case error
203 (setq string
204 (epg-encrypt-string
205 context
206 (if (stringp start)
207 (epa-file--encode-coding-string start coding-system)
208 (unless start
209 (setq start (point-min)
210 end (point-max)))
211 (epa-file--encode-coding-string (buffer-substring start end)
212 coding-system))
213 (if (or epa-file-select-keys
214 (not (local-variable-p 'epa-file-encrypt-to
215 (current-buffer))))
216 (epa-select-keys
217 context
218 "Select recipents for encryption.
219 If no one is selected, symmetric encryption will be performed. "
220 recipients)
221 (if epa-file-encrypt-to
222 (epg-list-keys context recipients)))))
223 (error
224 (if (setq entry (assoc file epa-file-passphrase-alist))
225 (setcdr entry nil))
226 (signal 'file-error (cons "Opening output file" (cdr error)))))
227 (epa-file-run-real-handler
228 #'write-region
229 (list string nil file append visit lockname mustbenew))
230 (if (boundp 'last-coding-system-used)
231 (setq last-coding-system-used coding-system))
232 (if (eq visit t)
233 (progn
234 (setq buffer-file-name file)
235 (set-visited-file-modtime))
236 (if (stringp visit)
237 (progn
238 (set-visited-file-modtime)
239 (setq buffer-file-name visit))))
240 (if (or (eq visit t)
241 (eq visit nil)
242 (stringp visit))
243 (message "Wrote %s" buffer-file-name))))
244 (put 'write-region 'epa-file 'epa-file-write-region)
246 (defun epa-file-select-keys ()
247 "Select recipients for encryption."
248 (interactive)
249 (make-local-variable 'epa-file-encrypt-to)
250 (setq epa-file-encrypt-to
251 (mapcar
252 (lambda (key)
253 (epg-sub-key-id (car (epg-key-sub-key-list key))))
254 (epa-select-keys
255 (epg-make-context)
256 "Select recipents for encryption.
257 If no one is selected, symmetric encryption will be performed. "))))
259 ;;;###autoload
260 (defun epa-file-enable ()
261 (interactive)
262 (if (memq epa-file-handler file-name-handler-alist)
263 (message "`epa-file' already enabled")
264 (setq file-name-handler-alist
265 (cons epa-file-handler file-name-handler-alist))
266 (add-hook 'find-file-hook 'epa-file-find-file-hook)
267 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist))
268 (message "`epa-file' enabled")))
270 ;;;###autoload
271 (defun epa-file-disable ()
272 (interactive)
273 (if (memq epa-file-handler file-name-handler-alist)
274 (progn
275 (setq file-name-handler-alist
276 (delq epa-file-handler file-name-handler-alist))
277 (remove-hook 'find-file-hook 'epa-file-find-file-hook)
278 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry
279 auto-mode-alist))
280 (message "`epa-file' disabled"))
281 (message "`epa-file' already disabled")))
283 (provide 'epa-file)
285 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d
286 ;;; epa-file.el ends here