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))
108 (setq length (length string))
110 (delete-region (point) (point-max))))
112 (file-exists-p local-copy))
113 (delete-file local-copy)))
115 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
117 (defun epa-file-write-region (start end file &optional append visit lockname
120 (error "Can't append to the file."))
121 (setq file (expand-file-name file))
122 (let* ((coding-system (or coding-system-for-write
123 (if (boundp 'last-coding-system-used)
125 (write-region (point-min) (point-max) "/")
126 (error last-coding-system-used))
127 buffer-file-coding-system)))
128 (context (epg-make-context))
129 (coding-system-for-write 'binary)
131 (epg-context-set-passphrase-callback
133 (cons #'epa-file-passphrase-callback-function
135 (condition-case error
140 (encode-coding-string start coding-system)
141 (encode-coding-string (buffer-substring start end)
143 (unless (assoc file epa-file-passphrase-alist)
146 "Select recipents for encryption.
147 If no one is selected, symmetric encryption will be performed. "))))
149 (if (setq entry (assoc file epa-file-passphrase-alist))
151 (signal 'file-error (cons "Opening output file" (cdr error)))))
152 (epa-file-run-real-handler
154 (list string nil file append visit lockname mustbenew))
155 (if (boundp 'last-coding-system-used)
156 (setq last-coding-system-used coding-system))
159 (setq buffer-file-name file)
160 (set-visited-file-modtime))
163 (set-visited-file-modtime)
164 (setq buffer-file-name visit))))
168 (message "Wrote %s" buffer-file-name))))
169 (put 'write-region 'epa-file 'epa-file-write-region)
172 (defun epa-file-enable ()
174 (if (memq epa-file-handler file-name-handler-alist)
175 (message "`epa-file' already enabled")
176 (setq file-name-handler-alist
177 (cons epa-file-handler file-name-handler-alist))
178 (message "`epa-file' enabled")))
181 (defun epa-file-disable ()
183 (if (memq epa-file-handler file-name-handler-alist)
185 (setq file-name-handler-alist
186 (delq epa-file-handler file-name-handler-alist))
187 (message "`epa-file' disabled"))
188 (message "`epa-file' already disabled")))
192 ;;; epa-file.el ends here