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 t, cache passphrase for symmetric encryption."
42 (defvar epa-file-encrypt-to nil
43 "*Recipient(s) used for encrypting files.
44 May either be a string or a list of strings.")
47 (put 'epa-file-encrypt-to 'safe-local-variable
58 (defvar epa-file-handler
59 (cons epa-file-name-regexp 'epa-file-handler))
61 (defvar epa-file-passphrase-alist nil)
63 (if (fboundp 'encode-coding-string)
64 (defalias 'epa-file--encode-coding-string 'encode-coding-string)
65 (defalias 'epa-file--encode-coding-string 'identity))
67 (if (fboundp 'decode-coding-string)
68 (defalias 'epa-file--decode-coding-string 'decode-coding-string)
69 (defalias 'epa-file--decode-coding-string 'identity))
71 (defun epa-file-passphrase-callback-function (context key-id file)
72 (if (and epa-file-cache-passphrase-for-symmetric-encryption
74 (let ((entry (assoc file epa-file-passphrase-alist))
76 (or (copy-sequence (cdr entry))
79 (setq entry (list file)
80 epa-file-passphrase-alist (cons entry
81 epa-file-passphrase-alist)))
82 (setq passphrase (epa-passphrase-callback-function context
84 (setcdr entry (copy-sequence passphrase))
86 (epa-passphrase-callback-function context key-id nil)))
88 (defun epa-file-handler (operation &rest args)
90 (let ((op (get operation 'epa-file)))
93 (epa-file-run-real-handler operation args)))))
95 (defun epa-file-run-real-handler (operation args)
96 (let ((inhibit-file-name-handlers
97 (cons 'epa-file-handler
98 (and (eq inhibit-file-name-operation operation)
99 inhibit-file-name-handlers)))
100 (inhibit-file-name-operation operation))
101 (apply operation args)))
103 (defun epa-file-decode-and-insert (string file visit beg end replace)
104 (if (fboundp 'decode-coding-inserted-region)
106 (narrow-to-region (point) (point))
107 (let ((multibyte enable-multibyte-characters))
108 (set-buffer-multibyte nil)
110 (set-buffer-multibyte multibyte)
111 (decode-coding-inserted-region
112 (point-min) (point-max)
113 (substring file 0 (string-match epa-file-name-regexp file))
114 visit beg end replace)))
115 (insert (epa-file--decode-coding-string string (or coding-system-for-read
118 (defvar last-coding-system-used)
119 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
120 (barf-if-buffer-read-only)
121 (if (and visit (or beg end))
122 (error "Attempt to visit less than an entire file"))
123 (setq file (expand-file-name file))
124 (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
125 (context (epg-make-context))
128 (setq buffer-file-name file))
129 (epg-context-set-passphrase-callback
131 (cons #'epa-file-passphrase-callback-function
133 (epg-context-set-progress-callback context
134 #'epa-progress-callback-function)
138 (goto-char (point-min)))
139 (condition-case error
140 (setq string (epg-decrypt-file context file nil))
142 (if (setq entry (assoc file epa-file-passphrase-alist))
145 (cons "Opening input file" (cdr error)))))
147 (setq string (substring string (or beg 0) end)))
150 (narrow-to-region (point) (point))
151 (epa-file-decode-and-insert string file visit beg end replace)
152 (setq length (- (point-max) (point-min))))
154 (delete-region (point) (point-max)))))
156 (file-exists-p local-copy))
157 (delete-file local-copy)))
159 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
161 (defun epa-file-write-region (start end file &optional append visit lockname
164 (error "Can't append to the file."))
165 (setq file (expand-file-name file))
166 (let* ((coding-system (or coding-system-for-write
167 (if (fboundp 'select-safe-coding-system)
168 (select-safe-coding-system
169 (point-min) (point-max))
170 buffer-file-coding-system)))
171 (context (epg-make-context))
172 (coding-system-for-write 'binary)
174 (epg-context-set-passphrase-callback
176 (cons #'epa-file-passphrase-callback-function
178 (epg-context-set-progress-callback context
179 #'epa-progress-callback-function)
180 (condition-case error
185 (epa-file--encode-coding-string start coding-system)
186 (epa-file--encode-coding-string (buffer-substring start end)
188 (unless (assoc file epa-file-passphrase-alist)
191 "Select recipents for encryption.
192 If no one is selected, symmetric encryption will be performed. "
194 ((listp epa-file-encrypt-to) epa-file-encrypt-to)
195 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))))
197 (if (setq entry (assoc file epa-file-passphrase-alist))
199 (signal 'file-error (cons "Opening output file" (cdr error)))))
200 (epa-file-run-real-handler
202 (list string nil file append visit lockname mustbenew))
203 (if (boundp 'last-coding-system-used)
204 (setq last-coding-system-used coding-system))
207 (setq buffer-file-name file)
208 (set-visited-file-modtime))
211 (set-visited-file-modtime)
212 (setq buffer-file-name visit))))
216 (message "Wrote %s" buffer-file-name))))
217 (put 'write-region 'epa-file 'epa-file-write-region)
220 (defun epa-file-enable ()
222 (if (memq epa-file-handler file-name-handler-alist)
223 (message "`epa-file' already enabled")
224 (setq file-name-handler-alist
225 (cons epa-file-handler file-name-handler-alist))
226 (message "`epa-file' enabled")))
229 (defun epa-file-disable ()
231 (if (memq epa-file-handler file-name-handler-alist)
233 (setq file-name-handler-alist
234 (delq epa-file-handler file-name-handler-alist))
235 (message "`epa-file' disabled"))
236 (message "`epa-file' already disabled")))
240 ;;; epa-file.el ends here