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 (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil
38 "If non-nil, cache passphrase for symmetric encryption."
42 (defcustom epa-file-inhibit-auto-save t
43 "If non-nil, disable auto-saving when opening an encrypted file."
47 (defvar epa-file-encrypt-to nil
48 "*Recipient(s) used for encrypting files.
49 May either be a string or a list of strings.")
52 (put 'epa-file-encrypt-to 'safe-local-variable
63 (defvar epa-file-handler
64 (cons epa-file-name-regexp 'epa-file-handler))
66 (defvar epa-file-passphrase-alist nil)
68 (if (fboundp 'encode-coding-string)
69 (defalias 'epa-file--encode-coding-string 'encode-coding-string)
70 (defalias 'epa-file--encode-coding-string 'identity))
72 (if (fboundp 'decode-coding-string)
73 (defalias 'epa-file--decode-coding-string 'decode-coding-string)
74 (defalias 'epa-file--decode-coding-string 'identity))
76 (defun epa-file-passphrase-callback-function (context key-id file)
77 (if (and epa-file-cache-passphrase-for-symmetric-encryption
79 (let ((entry (assoc file epa-file-passphrase-alist))
81 (or (copy-sequence (cdr entry))
84 (setq entry (list file)
85 epa-file-passphrase-alist (cons entry
86 epa-file-passphrase-alist)))
87 (setq passphrase (epa-passphrase-callback-function context
89 (setcdr entry (copy-sequence passphrase))
91 (epa-passphrase-callback-function context key-id nil)))
93 (defun epa-file-handler (operation &rest args)
95 (let ((op (get operation 'epa-file)))
98 (epa-file-run-real-handler operation args)))))
100 (defun epa-file-run-real-handler (operation args)
101 (let ((inhibit-file-name-handlers
102 (cons 'epa-file-handler
103 (and (eq inhibit-file-name-operation operation)
104 inhibit-file-name-handlers)))
105 (inhibit-file-name-operation operation))
106 (apply operation args)))
108 (defun epa-file-decode-and-insert (string file visit beg end replace)
109 (if (fboundp 'decode-coding-inserted-region)
111 (narrow-to-region (point) (point))
112 (let ((multibyte enable-multibyte-characters))
113 (set-buffer-multibyte nil)
115 (set-buffer-multibyte multibyte)
116 (decode-coding-inserted-region
117 (point-min) (point-max)
118 (substring file 0 (string-match epa-file-name-regexp file))
119 visit beg end replace)))
120 (insert (epa-file--decode-coding-string string (or coding-system-for-read
123 (defvar last-coding-system-used)
124 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
125 (barf-if-buffer-read-only)
126 (if (and visit (or beg end))
127 (error "Attempt to visit less than an entire file"))
128 (if epa-file-inhibit-auto-save
130 (setq file (expand-file-name file))
131 (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
132 (context (epg-make-context))
135 (setq buffer-file-name file))
136 (epg-context-set-passphrase-callback
138 (cons #'epa-file-passphrase-callback-function
140 (epg-context-set-progress-callback context
141 #'epa-progress-callback-function)
145 (goto-char (point-min)))
146 (condition-case error
147 (setq string (epg-decrypt-file context file nil))
149 (if (setq entry (assoc file epa-file-passphrase-alist))
152 (cons "Opening input file" (cdr error)))))
154 (setq string (substring string (or beg 0) end)))
157 (narrow-to-region (point) (point))
158 (epa-file-decode-and-insert string file visit beg end replace)
159 (setq length (- (point-max) (point-min))))
161 (delete-region (point) (point-max)))))
163 (file-exists-p local-copy))
164 (delete-file local-copy)))
166 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
168 (defun epa-file-write-region (start end file &optional append visit lockname
171 (error "Can't append to the file."))
172 (setq file (expand-file-name file))
173 (let* ((coding-system (or coding-system-for-write
174 (if (fboundp 'select-safe-coding-system)
175 ;; This is needed since Emacs 22 has
176 ;; no-conversion setting for *.gpg in
177 ;; `auto-coding-alist'.
178 (let ((buffer-file-name
179 (file-name-sans-extension file)))
180 (select-safe-coding-system
181 (point-min) (point-max)))
182 buffer-file-coding-system)))
183 (context (epg-make-context))
184 (coding-system-for-write 'binary)
186 (epg-context-set-passphrase-callback
188 (cons #'epa-file-passphrase-callback-function
190 (epg-context-set-progress-callback context
191 #'epa-progress-callback-function)
192 (condition-case error
197 (epa-file--encode-coding-string start coding-system)
198 (epa-file--encode-coding-string (buffer-substring start end)
200 (unless (assoc file epa-file-passphrase-alist)
203 "Select recipents for encryption.
204 If no one is selected, symmetric encryption will be performed. "
206 ((listp epa-file-encrypt-to) epa-file-encrypt-to)
207 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))))
209 (if (setq entry (assoc file epa-file-passphrase-alist))
211 (signal 'file-error (cons "Opening output file" (cdr error)))))
212 (epa-file-run-real-handler
214 (list string nil file append visit lockname mustbenew))
215 (if (boundp 'last-coding-system-used)
216 (setq last-coding-system-used coding-system))
219 (setq buffer-file-name file)
220 (set-visited-file-modtime))
223 (set-visited-file-modtime)
224 (setq buffer-file-name visit))))
228 (message "Wrote %s" buffer-file-name))))
229 (put 'write-region 'epa-file 'epa-file-write-region)
232 (defun epa-file-enable ()
234 (if (memq epa-file-handler file-name-handler-alist)
235 (message "`epa-file' already enabled")
236 (setq file-name-handler-alist
237 (cons epa-file-handler file-name-handler-alist))
238 (message "`epa-file' enabled")))
241 (defun epa-file-disable ()
243 (if (memq epa-file-handler file-name-handler-alist)
245 (setq file-name-handler-alist
246 (delq epa-file-handler file-name-handler-alist))
247 (message "`epa-file' disabled"))
248 (message "`epa-file' already disabled")))
252 ;;; epa-file.el ends here