(x_new_font): Update f->scroll_bar_actual_width.
[bpt/emacs.git] / lisp / epa-file.el
CommitLineData
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
b0f5fd2e
DU
28 "If non-nil, cache passphrase for symmetric encryption.
29
30For security reasons, this option is turned off by default and
0d19d151
DU
31not recommended to use. Instead, consider using public-key
32encryption with gpg-agent which does the same job in a safer
33way."
c154c0be
MO
34 :type 'boolean
35 :group 'epa-file)
36
c154c0be
MO
37(defcustom epa-file-select-keys nil
38 "If non-nil, always asks user to select recipients."
39 :type 'boolean
40 :group 'epa-file)
41
c154c0be
MO
42(defvar epa-file-passphrase-alist nil)
43
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)))
48
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)))
53
c154c0be
MO
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)))
73
6f8a4190 74;;;###autoload
c154c0be 75(defun epa-file-handler (operation &rest args)
dbc9f96d
GM
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)))))
81
c154c0be
MO
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)))
89
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))
68a0d892
DU
94 (insert (if enable-multibyte-characters
95 (string-to-multibyte string)
96 string))
c154c0be
MO
97 (decode-coding-inserted-region
98 (point-min) (point-max)
99 (substring file 0 (string-match epa-file-name-regexp file))
68a0d892 100 visit beg end replace))
c154c0be
MO
101 (insert (epa-file--decode-coding-string string (or coding-system-for-read
102 'undecided)))))
103
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
6ea97db8 111 (condition-case nil
c154c0be
MO
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)
2cb35a56
DU
117 (if visit
118 (setq buffer-file-name file))
c154c0be
MO
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
8e3efc87 147 (delete-region (point) (point-max)))
2cb35a56
DU
148 (if visit
149 (set-visited-file-modtime))))
c154c0be
MO
150 (if (and local-copy
151 (file-exists-p local-copy))
152 (delete-file local-copy)))
153 (list file length)))
154(put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
155
156(defun epa-file-write-region (start end file &optional append visit lockname
157 mustbenew)
158 (if append
5a0c3f56 159 (error "Can't append to the file"))
c154c0be
MO
160 (setq file (expand-file-name file))
161 (let* ((coding-system (or coding-system-for-write
162 (if (fboundp 'select-safe-coding-system)
163 ;; This is needed since Emacs 22 has
164 ;; no-conversion setting for *.gpg in
165 ;; `auto-coding-alist'.
166 (let ((buffer-file-name
167 (file-name-sans-extension file)))
168 (select-safe-coding-system
169 (point-min) (point-max)))
170 buffer-file-coding-system)))
171 (context (epg-make-context))
172 (coding-system-for-write 'binary)
173 string entry
174 (recipients
175 (cond
176 ((listp epa-file-encrypt-to) epa-file-encrypt-to)
177 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))
178 (epg-context-set-passphrase-callback
179 context
180 (cons #'epa-file-passphrase-callback-function
181 file))
182 (epg-context-set-progress-callback context
183 #'epa-progress-callback-function)
184 (epg-context-set-armor context epa-armor)
185 (condition-case error
186 (setq string
187 (epg-encrypt-string
188 context
189 (if (stringp start)
190 (epa-file--encode-coding-string start coding-system)
6843296d 191 (unless start
437eedba
DU
192 (setq start (point-min)
193 end (point-max)))
c154c0be
MO
194 (epa-file--encode-coding-string (buffer-substring start end)
195 coding-system))
196 (if (or epa-file-select-keys
197 (not (local-variable-p 'epa-file-encrypt-to
198 (current-buffer))))
199 (epa-select-keys
200 context
201 "Select recipents for encryption.
202If no one is selected, symmetric encryption will be performed. "
203 recipients)
204 (if epa-file-encrypt-to
205 (epg-list-keys context recipients)))))
206 (error
207 (if (setq entry (assoc file epa-file-passphrase-alist))
208 (setcdr entry nil))
209 (signal 'file-error (cons "Opening output file" (cdr error)))))
210 (epa-file-run-real-handler
211 #'write-region
212 (list string nil file append visit lockname mustbenew))
213 (if (boundp 'last-coding-system-used)
214 (setq last-coding-system-used coding-system))
215 (if (eq visit t)
216 (progn
217 (setq buffer-file-name file)
218 (set-visited-file-modtime))
219 (if (stringp visit)
220 (progn
221 (set-visited-file-modtime)
222 (setq buffer-file-name visit))))
223 (if (or (eq visit t)
224 (eq visit nil)
225 (stringp visit))
226 (message "Wrote %s" buffer-file-name))))
227(put 'write-region 'epa-file 'epa-file-write-region)
228
c154c0be
MO
229(defun epa-file-select-keys ()
230 "Select recipients for encryption."
231 (interactive)
232 (make-local-variable 'epa-file-encrypt-to)
233 (setq epa-file-encrypt-to
fef7aa9e
MO
234 (mapcar
235 (lambda (key)
236 (epg-sub-key-id (car (epg-key-sub-key-list key))))
c154c0be
MO
237 (epa-select-keys
238 (epg-make-context)
239 "Select recipents for encryption.
fef7aa9e 240If no one is selected, symmetric encryption will be performed. "))))
c154c0be
MO
241
242;;;###autoload
243(defun epa-file-enable ()
244 (interactive)
245 (if (memq epa-file-handler file-name-handler-alist)
246 (message "`epa-file' already enabled")
247 (setq file-name-handler-alist
248 (cons epa-file-handler file-name-handler-alist))
bfeee9d1 249 (add-hook 'find-file-hook 'epa-file-find-file-hook)
c154c0be
MO
250 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist))
251 (message "`epa-file' enabled")))
252
253;;;###autoload
254(defun epa-file-disable ()
255 (interactive)
256 (if (memq epa-file-handler file-name-handler-alist)
257 (progn
258 (setq file-name-handler-alist
259 (delq epa-file-handler file-name-handler-alist))
bfeee9d1 260 (remove-hook 'find-file-hook 'epa-file-find-file-hook)
c154c0be
MO
261 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry
262 auto-mode-alist))
263 (message "`epa-file' disabled"))
264 (message "`epa-file' already disabled")))
265
266(provide 'epa-file)
267
37b77401 268;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d
c154c0be 269;;; epa-file.el ends here