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