Fixed.
[elisp/epg.git] / epa-file.el
1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption
2 ;; Copyright (C) 2006 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
6
7 ;; This file is part of EasyPG.
8
9 ;; This program 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 2, or (at your option)
12 ;; any later version.
13
14 ;; This program 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
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Code:
25
26 (require 'epa)
27
28 (defgroup epa-file nil
29   "The EasyPG Assistant hooks for transparent file encryption"
30   :group 'epa)
31
32 (defcustom epa-file-name-regexp "\\.gpg\\'"
33   "Regexp which matches filenames to be encrypted with GnuPG."
34   :type 'regexp
35   :group 'epa-file)
36
37 (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil
38   "If non-nil, cache passphrase for symmetric encryption."
39   :type 'boolean
40   :group 'epa-file)
41
42 (defcustom epa-file-inhibit-auto-save t
43   "If non-nil, disable auto-saving when opening an encrypted file."
44   :type 'boolean
45   :group 'epa-file)
46
47 (defvar epa-file-encrypt-to nil
48   "*Recipient(s) used for encrypting files.
49 May either be a string or a list of strings.")
50
51 ;;;###autoload
52 (put 'epa-file-encrypt-to 'safe-local-variable
53      (lambda (val)
54        (or (stringp val)
55            (and (listp val)
56                 (catch 'safe
57                   (mapc (lambda (elt)
58                           (unless (stringp elt)
59                             (throw 'safe nil)))
60                         val)
61                   t)))))
62
63 (defvar epa-file-handler
64   (cons epa-file-name-regexp 'epa-file-handler))
65
66 (defvar epa-file-passphrase-alist nil)
67
68 (if (fboundp 'encode-coding-string)
69     (defalias 'epa-file--encode-coding-string 'encode-coding-string)
70   (defalias 'epa-file--encode-coding-string 'identity))
71
72 (if (fboundp 'decode-coding-string)
73     (defalias 'epa-file--decode-coding-string 'decode-coding-string)
74   (defalias 'epa-file--decode-coding-string 'identity))
75
76 (defun epa-file-passphrase-callback-function (context key-id file)
77   (if (and epa-file-cache-passphrase-for-symmetric-encryption
78            (eq key-id 'SYM))
79       (let ((entry (assoc file epa-file-passphrase-alist))
80             passphrase)
81         (or (copy-sequence (cdr entry))
82             (progn
83               (unless entry
84                 (setq entry (list file)
85                       epa-file-passphrase-alist (cons entry
86                                                  epa-file-passphrase-alist)))
87               (setq passphrase (epa-passphrase-callback-function context
88                                                                  key-id nil))
89               (setcdr entry (copy-sequence passphrase))
90               passphrase)))
91     (epa-passphrase-callback-function context key-id nil)))
92
93 (defun epa-file-handler (operation &rest args)
94   (save-match-data
95     (let ((op (get operation 'epa-file)))
96       (if op
97           (apply op args)
98         (epa-file-run-real-handler operation args)))))
99
100 (defun epa-file-run-real-handler (operation args)
101   (let ((inhibit-file-name-handlers
102          (cons 'epa-file-handler
103                (and (eq inhibit-file-name-operation operation)
104                     inhibit-file-name-handlers)))
105         (inhibit-file-name-operation operation))
106     (apply operation args)))
107
108 (defun epa-file-decode-and-insert (string file visit beg end replace)
109   (if (fboundp 'decode-coding-inserted-region)
110       (save-restriction
111         (narrow-to-region (point) (point))
112         (let ((multibyte enable-multibyte-characters))
113           (set-buffer-multibyte nil)
114           (insert string)
115           (set-buffer-multibyte multibyte)
116           (decode-coding-inserted-region
117            (point-min) (point-max)
118            (substring file 0 (string-match epa-file-name-regexp file))
119            visit beg end replace)))
120     (insert (epa-file--decode-coding-string string (or coding-system-for-read
121                                                        'undecided)))))
122
123 (defvar last-coding-system-used)
124 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
125   (barf-if-buffer-read-only)
126   (if (and visit (or beg end))
127       (error "Attempt to visit less than an entire file"))
128   (if epa-file-inhibit-auto-save
129       (auto-save-mode 0))
130   (setq file (expand-file-name file))
131   (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
132         (context (epg-make-context))
133         string length entry)
134     (if visit
135         (setq buffer-file-name file))
136     (epg-context-set-passphrase-callback
137      context
138      (cons #'epa-file-passphrase-callback-function
139            file))
140     (epg-context-set-progress-callback context
141                                        #'epa-progress-callback-function)
142     (unwind-protect
143         (progn
144           (if replace
145               (goto-char (point-min)))
146           (condition-case error
147               (setq string (epg-decrypt-file context file nil))
148             (error
149              (if (setq entry (assoc file epa-file-passphrase-alist))
150                  (setcdr entry nil))
151              (signal 'file-error
152                      (cons "Opening input file" (cdr error)))))
153           (if (or beg end)
154               (setq string (substring string (or beg 0) end)))
155           (save-excursion
156             (save-restriction
157               (narrow-to-region (point) (point))
158               (epa-file-decode-and-insert string file visit beg end replace)
159               (setq length (- (point-max) (point-min))))
160             (if replace
161                 (delete-region (point) (point-max)))))
162       (if (and local-copy
163                (file-exists-p local-copy))
164           (delete-file local-copy)))
165     (list file length)))
166 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
167
168 (defun epa-file-write-region (start end file &optional append visit lockname
169                                     mustbenew)
170   (if append
171       (error "Can't append to the file."))
172   (setq file (expand-file-name file))
173   (let* ((coding-system (or coding-system-for-write
174                             (if (fboundp 'select-safe-coding-system)
175                                 ;; This is needed since Emacs 22 has
176                                 ;; no-conversion setting for *.gpg in
177                                 ;; `auto-coding-alist'.
178                                 (let ((buffer-file-name
179                                        (file-name-sans-extension file)))
180                                   (select-safe-coding-system
181                                    (point-min) (point-max)))
182                               buffer-file-coding-system)))
183          (context (epg-make-context))
184          (coding-system-for-write 'binary)
185          string entry)
186     (epg-context-set-passphrase-callback
187      context
188      (cons #'epa-file-passphrase-callback-function
189            file))
190     (epg-context-set-progress-callback context
191                                        #'epa-progress-callback-function)
192     (condition-case error
193         (setq string
194               (epg-encrypt-string
195                context
196                (if (stringp start)
197                    (epa-file--encode-coding-string start coding-system)
198                  (epa-file--encode-coding-string (buffer-substring start end)
199                                                  coding-system))
200                (unless (assoc file epa-file-passphrase-alist)
201                  (epa-select-keys
202                   context
203                   "Select recipents for encryption.
204 If no one is selected, symmetric encryption will be performed.  "
205                   (cond
206                    ((listp epa-file-encrypt-to) epa-file-encrypt-to)
207                    ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))))
208       (error
209        (if (setq entry (assoc file epa-file-passphrase-alist))
210            (setcdr entry nil))
211        (signal 'file-error (cons "Opening output file" (cdr error)))))
212     (epa-file-run-real-handler
213      #'write-region
214      (list string nil file append visit lockname mustbenew))
215     (if (boundp 'last-coding-system-used)
216         (setq last-coding-system-used coding-system))
217     (if (eq visit t)
218         (progn
219           (setq buffer-file-name file)
220           (set-visited-file-modtime))
221       (if (stringp visit)
222           (progn
223             (set-visited-file-modtime)
224             (setq buffer-file-name visit))))
225     (if (or (eq visit t)
226             (eq visit nil)
227             (stringp visit))
228         (message "Wrote %s" buffer-file-name))))
229 (put 'write-region 'epa-file 'epa-file-write-region)
230
231 ;;;###autoload
232 (defun epa-file-enable ()
233   (interactive)
234   (if (memq epa-file-handler file-name-handler-alist)
235       (message "`epa-file' already enabled")
236     (setq file-name-handler-alist
237           (cons epa-file-handler file-name-handler-alist))
238     (message "`epa-file' enabled")))
239
240 ;;;###autoload
241 (defun epa-file-disable ()
242   (interactive)
243   (if (memq epa-file-handler file-name-handler-alist)
244       (progn
245         (setq file-name-handler-alist
246               (delq epa-file-handler file-name-handler-alist))
247         (message "`epa-file' disabled"))
248     (message "`epa-file' already disabled")))
249
250 (provide 'epa-file)
251
252 ;;; epa-file.el ends here