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