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)
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))
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 (epg-decrypt-file context file nil))
95 (if (setq entry (assoc file epa-file-passphrase-alist))
98 (cons "Opening input file" (cdr error)))))
100 (setq string (substring string (or beg 0) end)))
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))
109 (setq length (length string))
111 (delete-region (point) (point-max)))))
113 (file-exists-p local-copy))
114 (delete-file local-copy)))
116 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
118 (defun epa-file-write-region (start end file &optional append visit lockname
121 (error "Can't append to the file."))
122 (setq file (expand-file-name file))
123 (let* ((coding-system (or coding-system-for-write
124 (if (boundp 'last-coding-system-used)
126 (write-region (point-min) (point-max) "/")
127 (error last-coding-system-used))
128 buffer-file-coding-system)))
129 (context (epg-make-context))
130 (coding-system-for-write 'binary)
132 (epg-context-set-passphrase-callback
134 (cons #'epa-file-passphrase-callback-function
136 (condition-case error
141 (encode-coding-string start coding-system)
142 (encode-coding-string (buffer-substring start end)
144 (unless (assoc file epa-file-passphrase-alist)
147 "Select recipents for encryption.
148 If no one is selected, symmetric encryption will be performed. "))))
150 (if (setq entry (assoc file epa-file-passphrase-alist))
152 (signal 'file-error (cons "Opening output file" (cdr error)))))
153 (epa-file-run-real-handler
155 (list string nil file append visit lockname mustbenew))
156 (if (boundp 'last-coding-system-used)
157 (setq last-coding-system-used coding-system))
160 (setq buffer-file-name file)
161 (set-visited-file-modtime))
164 (set-visited-file-modtime)
165 (setq buffer-file-name visit))))
169 (message "Wrote %s" buffer-file-name))))
170 (put 'write-region 'epa-file 'epa-file-write-region)
173 (defun epa-file-enable ()
175 (if (memq epa-file-handler file-name-handler-alist)
176 (message "`epa-file' already enabled")
177 (setq file-name-handler-alist
178 (cons epa-file-handler file-name-handler-alist))
179 (message "`epa-file' enabled")))
182 (defun epa-file-disable ()
184 (if (memq epa-file-handler file-name-handler-alist)
186 (setq file-name-handler-alist
187 (delq epa-file-handler file-name-handler-alist))
188 (message "`epa-file' disabled"))
189 (message "`epa-file' already disabled")))
193 ;;; epa-file.el ends here