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 t, cache passphrase for symmetric encryption."
39   :type 'boolean
40   :group 'epa-file)
41
42 (defvar epa-file-encrypt-to nil
43   "*Recipient(s) used for encrypting files.
44 May either be a string or a list of strings.")
45
46 ;;;###autoload
47 (put 'epa-file-encrypt-to 'safe-local-variable
48      (lambda (val)
49        (or (stringp val)
50            (and (listp val)
51                 (catch 'safe
52                   (mapc (lambda (elt)
53                           (unless (stringp elt)
54                             (throw 'safe nil)))
55                         val)
56                   t)))))
57
58 (defvar epa-file-handler
59   (cons epa-file-name-regexp 'epa-file-handler))
60
61 (defvar epa-file-passphrase-alist nil)
62
63 (if (fboundp 'encode-coding-string)
64     (defalias 'epa-file--encode-coding-string 'encode-coding-string)
65   (defalias 'epa-file--encode-coding-string 'identity))
66
67 (if (fboundp 'decode-coding-string)
68     (defalias 'epa-file--decode-coding-string 'decode-coding-string)
69   (defalias 'epa-file--decode-coding-string 'identity))
70
71 (defun epa-file-passphrase-callback-function (context key-id file)
72   (if (and epa-file-cache-passphrase-for-symmetric-encryption
73            (eq key-id 'SYM))
74       (let ((entry (assoc file epa-file-passphrase-alist))
75             passphrase)
76         (or (copy-sequence (cdr entry))
77             (progn
78               (unless entry
79                 (setq entry (list file)
80                       epa-file-passphrase-alist (cons entry
81                                                  epa-file-passphrase-alist)))
82               (setq passphrase (epa-passphrase-callback-function context
83                                                                  key-id nil))
84               (setcdr entry (copy-sequence passphrase))
85               passphrase)))
86     (epa-passphrase-callback-function context key-id nil)))
87
88 (defun epa-file-handler (operation &rest args)
89   (save-match-data
90     (let ((op (get operation 'epa-file)))
91       (if op
92           (apply op args)
93         (epa-file-run-real-handler operation args)))))
94
95 (defun epa-file-run-real-handler (operation args)
96   (let ((inhibit-file-name-handlers
97          (cons 'epa-file-handler
98                (and (eq inhibit-file-name-operation operation)
99                     inhibit-file-name-handlers)))
100         (inhibit-file-name-operation operation))
101     (apply operation args)))
102
103 (defun epa-file-decode-and-insert (string file visit beg end replace)
104   (if (fboundp 'decode-coding-inserted-region)
105       (save-restriction
106         (narrow-to-region (point) (point))
107         (let ((multibyte enable-multibyte-characters))
108           (set-buffer-multibyte nil)
109           (insert string)
110           (set-buffer-multibyte multibyte)
111           (decode-coding-inserted-region
112            (point-min) (point-max)
113            (substring file 0 (string-match epa-file-name-regexp file))
114            visit beg end replace)))
115     (insert (epa-file--decode-coding-string string (or coding-system-for-read
116                                                        'undecided)))))
117
118 (defvar last-coding-system-used)
119 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
120   (barf-if-buffer-read-only)
121   (if (and visit (or beg end))
122       (error "Attempt to visit less than an entire file"))
123   (setq file (expand-file-name file))
124   (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
125         (context (epg-make-context))
126         string length entry)
127     (if visit
128         (setq buffer-file-name file))
129     (epg-context-set-passphrase-callback
130      context
131      (cons #'epa-file-passphrase-callback-function
132            file))
133     (epg-context-set-progress-callback context
134                                        #'epa-progress-callback-function)
135     (unwind-protect
136         (progn
137           (if replace
138               (goto-char (point-min)))
139           (condition-case error
140               (setq string (epg-decrypt-file context file nil))
141             (error
142              (if (setq entry (assoc file epa-file-passphrase-alist))
143                  (setcdr entry nil))
144              (signal 'file-error
145                      (cons "Opening input file" (cdr error)))))
146           (if (or beg end)
147               (setq string (substring string (or beg 0) end)))
148           (save-excursion
149             (save-restriction
150               (narrow-to-region (point) (point))
151               (epa-file-decode-and-insert string file visit beg end replace)
152               (setq length (- (point-max) (point-min))))
153             (if replace
154                 (delete-region (point) (point-max)))))
155       (if (and local-copy
156                (file-exists-p local-copy))
157           (delete-file local-copy)))
158     (list file length)))
159 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
160
161 (defun epa-file-write-region (start end file &optional append visit lockname
162                                     mustbenew)
163   (if append
164       (error "Can't append to the file."))
165   (setq file (expand-file-name file))
166   (let* ((coding-system (or coding-system-for-write
167                             (if (boundp 'last-coding-system-used)
168                                 (condition-case nil
169                                     (write-region (point-min) (point-max) "/")
170                                   (error last-coding-system-used))
171                               buffer-file-coding-system)))
172          (context (epg-make-context))
173          (coding-system-for-write 'binary)
174          string entry)
175     (epg-context-set-passphrase-callback
176      context
177      (cons #'epa-file-passphrase-callback-function
178            file))
179     (epg-context-set-progress-callback context
180                                        #'epa-progress-callback-function)
181     (condition-case error
182         (setq string
183               (epg-encrypt-string
184                context
185                (if (stringp start)
186                    (epa-file--encode-coding-string start coding-system)
187                  (epa-file--encode-coding-string (buffer-substring start end)
188                                                  coding-system))
189                (unless (assoc file epa-file-passphrase-alist)
190                  (epa-select-keys
191                   context
192                   "Select recipents for encryption.
193 If no one is selected, symmetric encryption will be performed.  "
194                   (cond
195                    ((listp epa-file-encrypt-to) epa-file-encrypt-to)
196                    ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))))
197       (error
198        (if (setq entry (assoc file epa-file-passphrase-alist))
199            (setcdr entry nil))
200        (signal 'file-error (cons "Opening output file" (cdr error)))))
201     (epa-file-run-real-handler
202      #'write-region
203      (list string nil file append visit lockname mustbenew))
204     (if (boundp 'last-coding-system-used)
205         (setq last-coding-system-used coding-system))
206     (if (eq visit t)
207         (progn
208           (setq buffer-file-name file)
209           (set-visited-file-modtime))
210       (if (stringp visit)
211           (progn
212             (set-visited-file-modtime)
213             (setq buffer-file-name visit))))
214     (if (or (eq visit t)
215             (eq visit nil)
216             (stringp visit))
217         (message "Wrote %s" buffer-file-name))))
218 (put 'write-region 'epa-file 'epa-file-write-region)
219
220 ;;;###autoload
221 (defun epa-file-enable ()
222   (interactive)
223   (if (memq epa-file-handler file-name-handler-alist)
224       (message "`epa-file' already enabled")
225     (setq file-name-handler-alist
226           (cons epa-file-handler file-name-handler-alist))
227     (message "`epa-file' enabled")))
228
229 ;;;###autoload
230 (defun epa-file-disable ()
231   (interactive)
232   (if (memq epa-file-handler file-name-handler-alist)
233       (progn
234         (setq file-name-handler-alist
235               (delq epa-file-handler file-name-handler-alist))
236         (message "`epa-file' disabled"))
237     (message "`epa-file' already disabled")))
238
239 (provide 'epa-file)
240
241 ;;; epa-file.el ends here