OSDN Git Service

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