OSDN Git Service

* epa-file.el (epa-file-insert-file-contents): Support partial read.
[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           (setq string
102                 (decode-coding-string string
103                                       (or coding-system-for-read 'undecided)))
104           (if (boundp 'last-coding-system-used)
105               (set-buffer-file-coding-system last-coding-system-used)
106             (set-buffer-file-coding-system default-buffer-file-coding-system))
107           (insert string)
108           (setq length (length string))
109           (if replace
110               (delete-region (point) (point-max))))
111       (if (and local-copy
112                (file-exists-p local-copy))
113           (delete-file local-copy)))
114     (list file length)))
115 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
116
117 (defun epa-file-write-region (start end file &optional append visit lockname
118                                     mustbenew)
119   (if append
120       (error "Can't append to the file."))
121   (setq file (expand-file-name file))
122   (let* ((coding-system (or coding-system-for-write
123                             (if (boundp 'last-coding-system-used)
124                                 (condition-case nil
125                                     (write-region (point-min) (point-max) "/")
126                                   (error last-coding-system-used))
127                               buffer-file-coding-system)))
128          (context (epg-make-context))
129          (coding-system-for-write 'binary)
130          string entry)
131     (epg-context-set-passphrase-callback
132      context
133      (cons #'epa-file-passphrase-callback-function
134            file))
135     (condition-case error
136         (setq string
137               (epg-encrypt-string
138                context
139                (if (stringp start)
140                    (encode-coding-string start coding-system)
141                  (encode-coding-string (buffer-substring start end)
142                                        coding-system))
143                (unless (assoc file epa-file-passphrase-alist)
144                  (epa-select-keys
145                   context
146                   "Select recipents for encryption.
147 If no one is selected, symmetric encryption will be performed.  "))))
148       (error
149        (if (setq entry (assoc file epa-file-passphrase-alist))
150            (setcdr entry nil))
151        (signal 'file-error (cons "Opening output file" (cdr error)))))
152     (epa-file-run-real-handler
153      #'write-region
154      (list string nil file append visit lockname mustbenew))
155     (if (boundp 'last-coding-system-used)
156         (setq last-coding-system-used coding-system))
157     (if (eq visit t)
158         (progn
159           (setq buffer-file-name file)
160           (set-visited-file-modtime))
161       (if (stringp visit)
162           (progn
163             (set-visited-file-modtime)
164             (setq buffer-file-name visit))))
165     (if (or (eq visit t)
166             (eq visit nil)
167             (stringp visit))
168         (message "Wrote %s" buffer-file-name))))
169 (put 'write-region 'epa-file 'epa-file-write-region)
170
171 ;;;###autoload
172 (defun epa-file-enable ()
173   (interactive)
174   (if (memq epa-file-handler file-name-handler-alist)
175       (message "`epa-file' already enabled")
176     (setq file-name-handler-alist
177           (cons epa-file-handler file-name-handler-alist))
178     (message "`epa-file' enabled")))
179
180 ;;;###autoload
181 (defun epa-file-disable ()
182   (interactive)
183   (if (memq epa-file-handler file-name-handler-alist)
184       (progn
185         (setq file-name-handler-alist
186               (delq epa-file-handler file-name-handler-alist))
187         (message "`epa-file' disabled"))
188     (message "`epa-file' already disabled")))
189
190 (provide 'epa-file)
191
192 ;;; epa-file.el ends here