1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption
2 ;; Copyright (C) 2006 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
7 ;; This file is part of EasyPG.
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)
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.
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.
28 (defgroup epa-file nil
29 "The EasyPG Assistant hooks for transparent file encryption"
32 (defcustom epa-file-name-regexp "\\.gpg\\'"
33 "Regexp which matches filenames to be encrypted with GnuPG."
37 (defvar epa-file-handler
38 (cons epa-file-name-regexp 'epa-file-handler))
40 (defvar epa-file-passphrase-alist nil)
42 (defun epa-file-passphrase-callback-function (context key-id file)
44 (let ((entry (assoc file epa-file-passphrase-alist))
46 (or (copy-sequence (cdr 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
54 (setcdr entry (copy-sequence passphrase))
56 (epg-passphrase-callback-function context key-id nil)))
58 (defun epa-file-handler (operation &rest args)
60 (let ((op (get operation 'epa-file)))
63 (epa-file-run-real-handler operation args)))))
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)))
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)
77 (error "Can't read the file partially."))
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))
83 (setq buffer-file-name file))
84 (epg-context-set-passphrase-callback
86 (cons #'epa-file-passphrase-callback-function
91 (goto-char (point-min)))
93 (setq string (decode-coding-string
94 (epg-decrypt-file context file nil)
97 (if (setq entry (assoc file epa-file-passphrase-alist))
100 (cons "Opening input file" (cdr error)))))
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))
105 (setq length (length string))
107 (delete-region (point) (point-max))))
109 (file-exists-p local-copy))
110 (delete-file local-copy)))
112 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
114 (defun epa-file-write-region (start end file &optional append visit lockname
117 (error "Can't append to the file."))
118 (setq file (expand-file-name file))
119 (let* ((coding-system (if (boundp 'last-coding-system-used)
121 (write-region (point-min) (point-max) "/")
122 (error last-coding-system-used))
123 buffer-file-coding-system))
124 (context (epg-make-context))
125 (coding-system-for-write 'binary)
127 (epg-context-set-passphrase-callback
129 (cons #'epa-file-passphrase-callback-function
131 (condition-case error
136 (encode-coding-string start coding-system)
137 (encode-coding-string (buffer-substring start end)
139 (unless (assoc file epa-file-passphrase-alist)
142 "Select recipents for encryption.
143 If no one is selected, symmetric encryption will be performed. "))))
145 (if (setq entry (assoc file epa-file-passphrase-alist))
147 (signal 'file-error (cons "Opening output file" (cdr error)))))
148 (epa-file-run-real-handler
150 (list string nil file append visit lockname mustbenew))
151 (if (boundp 'last-coding-system-used)
152 (setq last-coding-system-used coding-system))
155 (setq buffer-file-name file)
156 (set-visited-file-modtime))
159 (set-visited-file-modtime)
160 (setq buffer-file-name visit))))
164 (message "Wrote %s" buffer-file-name))))
165 (put 'write-region 'epa-file 'epa-file-write-region)
168 (defun epa-file-enable ()
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")))
177 (defun epa-file-disable ()
179 (if (memq epa-file-handler file-name-handler-alist)
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")))
188 ;;; epa-file.el ends here