OSDN Git Service

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