1 ;;; epa-file.el --- the EasyPG Assistant hooks for 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 (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 key-id nil))
53 (setcdr entry (copy-sequence passphrase))
55 (epg-passphrase-callback-function key-id nil)))
57 (defun epa-file-handler (operation &rest args)
59 (let ((op (get operation 'epa-file)))
62 (epa-file-run-real-handler operation args)))))
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)))
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)
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))
82 (setq buffer-file-name file))
83 (epg-context-set-passphrase-callback
85 (cons #'epa-file-passphrase-callback-function
90 (goto-char (point-min)))
92 (setq string (decode-coding-string
93 (epg-decrypt-file context file nil)
96 (if (setq entry (assoc file epa-file-passphrase-alist))
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))
104 (setq length (length string))
106 (delete-region (point) (point-max))))
108 (file-exists-p local-copy))
109 (delete-file local-copy)))
111 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
113 (defun epa-file-write-region (start end file &optional append visit lockname
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)
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)
126 (epg-context-set-passphrase-callback
128 (cons #'epa-file-passphrase-callback-function
130 (condition-case error
135 (encode-coding-string start coding-system)
136 (encode-coding-string (buffer-substring start end)
138 (unless (assoc file epa-file-passphrase-alist)
140 "Select recipents for encryption.
141 If no one is selected, symmetric encryption will be performed. "))))
143 (if (setq entry (assoc file epa-file-passphrase-alist))
145 (signal 'file-error (cons "Opening output file" (cdr error)))))
146 (epa-file-run-real-handler
148 (list string nil file append visit lockname mustbenew))
149 (if (boundp 'last-coding-system-used)
150 (setq last-coding-system-used coding-system))
153 (setq buffer-file-name file)
154 (set-visited-file-modtime))
157 (set-visited-file-modtime)
158 (setq buffer-file-name visit))))
162 (message "Wrote %s" buffer-file-name))))
163 (put 'write-region 'epa-file 'epa-file-write-region)
166 (defun epa-file-enable ()
168 (if (memq epa-file-handler file-name-handler-alist)
169 (message "`epa-file' already enabled")
170 (setq file-name-handler-alist
171 (cons epa-file-handler file-name-handler-alist))
172 (message "`epa-file' enabled")))
175 (defun epa-file-disable ()
177 (if (memq epa-file-handler file-name-handler-alist)
179 (setq file-name-handler-alist
180 (delq epa-file-handler file-name-handler-alist))
181 (message "`epa-file' disabled"))
182 (message "`epa-file' already disabled")))
186 ;;; epa-file.el ends here