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 (epa-passphrase-callback-function context
54 (setcdr entry (copy-sequence passphrase))
56 (epa-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 (defun epa-file-decode-and-insert (string file visit beg end replace)
74 (if (fboundp 'decode-coding-inserted-region)
76 (narrow-to-region (point) (point))
77 (let ((multibyte enable-multibyte-characters))
78 (set-buffer-multibyte nil)
80 (set-buffer-multibyte multibyte)
81 (decode-coding-inserted-region
82 (point-min) (point-max)
83 (substring file 0 (string-match epa-file-name-regexp file))
84 visit beg end replace)))
85 (insert (decode-coding-string string (or coding-system-for-read
88 (defvar last-coding-system-used)
89 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
90 (barf-if-buffer-read-only)
91 (if (and visit (or beg end))
92 (error "Attempt to visit less than an entire file"))
93 (setq file (expand-file-name file))
94 (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
95 (context (epg-make-context))
98 (setq buffer-file-name file))
99 (epg-context-set-passphrase-callback
101 (cons #'epa-file-passphrase-callback-function
103 (epg-context-set-progress-callback context
104 #'epa-progress-callback-function)
108 (goto-char (point-min)))
109 (condition-case error
110 (setq string (epg-decrypt-file context file nil))
112 (if (setq entry (assoc file epa-file-passphrase-alist))
115 (cons "Opening input file" (cdr error)))))
117 (setq string (substring string (or beg 0) end)))
120 (narrow-to-region (point) (point))
121 (epa-file-decode-and-insert string file visit beg end replace)
122 (setq length (- (point-max) (point-min))))
124 (delete-region (point) (point-max)))))
126 (file-exists-p local-copy))
127 (delete-file local-copy)))
129 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
131 (defun epa-file-write-region (start end file &optional append visit lockname
134 (error "Can't append to the file."))
135 (setq file (expand-file-name file))
136 (let* ((coding-system (or coding-system-for-write
137 (if (boundp 'last-coding-system-used)
139 (write-region (point-min) (point-max) "/")
140 (error last-coding-system-used))
141 buffer-file-coding-system)))
142 (context (epg-make-context))
143 (coding-system-for-write 'binary)
145 (epg-context-set-passphrase-callback
147 (cons #'epa-file-passphrase-callback-function
149 (epg-context-set-progress-callback context
150 #'epa-progress-callback-function)
151 (condition-case error
156 (encode-coding-string start coding-system)
157 (encode-coding-string (buffer-substring start end)
159 (unless (assoc file epa-file-passphrase-alist)
162 "Select recipents for encryption.
163 If no one is selected, symmetric encryption will be performed. "))))
165 (if (setq entry (assoc file epa-file-passphrase-alist))
167 (signal 'file-error (cons "Opening output file" (cdr error)))))
168 (epa-file-run-real-handler
170 (list string nil file append visit lockname mustbenew))
171 (if (boundp 'last-coding-system-used)
172 (setq last-coding-system-used coding-system))
175 (setq buffer-file-name file)
176 (set-visited-file-modtime))
179 (set-visited-file-modtime)
180 (setq buffer-file-name visit))))
184 (message "Wrote %s" buffer-file-name))))
185 (put 'write-region 'epa-file 'epa-file-write-region)
188 (defun epa-file-enable ()
190 (if (memq epa-file-handler file-name-handler-alist)
191 (message "`epa-file' already enabled")
192 (setq file-name-handler-alist
193 (cons epa-file-handler file-name-handler-alist))
194 (message "`epa-file' enabled")))
197 (defun epa-file-disable ()
199 (if (memq epa-file-handler file-name-handler-alist)
201 (setq file-name-handler-alist
202 (delq epa-file-handler file-name-handler-alist))
203 (message "`epa-file' disabled"))
204 (message "`epa-file' already disabled")))
208 ;;; epa-file.el ends here