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)))
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 (if (and (null coding-system-for-read)
106 (fboundp 'decode-coding-inserted-region))
108 (narrow-to-region (point) (point))
110 (decode-coding-inserted-region
111 (point-min) (point-max)
112 (substring file 0 (string-match epa-file-name-regexp file))
113 visit beg end replace)
114 (setq length (- (point-max) (point-min))))
115 (setq string (decode-coding-string string
116 (or coding-system-for-read
118 length (length string))
121 (delete-region (point) (point-max)))))
123 (file-exists-p local-copy))
124 (delete-file local-copy)))
126 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
128 (defun epa-file-write-region (start end file &optional append visit lockname
131 (error "Can't append to the file."))
132 (setq file (expand-file-name file))
133 (let* ((coding-system (or coding-system-for-write
134 (if (boundp 'last-coding-system-used)
136 (write-region (point-min) (point-max) "/")
137 (error last-coding-system-used))
138 buffer-file-coding-system)))
139 (context (epg-make-context))
140 (coding-system-for-write 'binary)
142 (epg-context-set-passphrase-callback
144 (cons #'epa-file-passphrase-callback-function
146 (condition-case error
151 (encode-coding-string start coding-system)
152 (encode-coding-string (buffer-substring start end)
154 (unless (assoc file epa-file-passphrase-alist)
157 "Select recipents for encryption.
158 If no one is selected, symmetric encryption will be performed. "))))
160 (if (setq entry (assoc file epa-file-passphrase-alist))
162 (signal 'file-error (cons "Opening output file" (cdr error)))))
163 (epa-file-run-real-handler
165 (list string nil file append visit lockname mustbenew))
166 (if (boundp 'last-coding-system-used)
167 (setq last-coding-system-used coding-system))
170 (setq buffer-file-name file)
171 (set-visited-file-modtime))
174 (set-visited-file-modtime)
175 (setq buffer-file-name visit))))
179 (message "Wrote %s" buffer-file-name))))
180 (put 'write-region 'epa-file 'epa-file-write-region)
183 (defun epa-file-enable ()
185 (if (memq epa-file-handler file-name-handler-alist)
186 (message "`epa-file' already enabled")
187 (setq file-name-handler-alist
188 (cons epa-file-handler file-name-handler-alist))
189 (message "`epa-file' enabled")))
192 (defun epa-file-disable ()
194 (if (memq epa-file-handler file-name-handler-alist)
196 (setq file-name-handler-alist
197 (delq epa-file-handler file-name-handler-alist))
198 (message "`epa-file' disabled"))
199 (message "`epa-file' already disabled")))
203 ;;; epa-file.el ends here