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-handler
43 (cons epa-file-name-regexp 'epa-file-handler))
45 (defvar epa-file-passphrase-alist nil)
47 (defun epa-file-passphrase-callback-function (context key-id file)
48 (if (and epa-file-cache-passphrase-for-symmetric-encryption
50 (let ((entry (assoc file epa-file-passphrase-alist))
52 (or (copy-sequence (cdr entry))
55 (setq entry (list file)
56 epa-file-passphrase-alist (cons entry
57 epa-file-passphrase-alist)))
58 (setq passphrase (epa-passphrase-callback-function context
60 (setcdr entry (copy-sequence passphrase))
62 (epa-passphrase-callback-function context key-id nil)))
64 (defun epa-file-handler (operation &rest args)
66 (let ((op (get operation 'epa-file)))
69 (epa-file-run-real-handler operation args)))))
71 (defun epa-file-run-real-handler (operation args)
72 (let ((inhibit-file-name-handlers
73 (cons 'epa-file-handler
74 (and (eq inhibit-file-name-operation operation)
75 inhibit-file-name-handlers)))
76 (inhibit-file-name-operation operation))
77 (apply operation args)))
79 (defun epa-file-decode-and-insert (string file visit beg end replace)
80 (if (fboundp 'decode-coding-inserted-region)
82 (narrow-to-region (point) (point))
83 (let ((multibyte enable-multibyte-characters))
84 (set-buffer-multibyte nil)
86 (set-buffer-multibyte multibyte)
87 (decode-coding-inserted-region
88 (point-min) (point-max)
89 (substring file 0 (string-match epa-file-name-regexp file))
90 visit beg end replace)))
91 (insert (decode-coding-string string (or coding-system-for-read
94 (defvar last-coding-system-used)
95 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
96 (barf-if-buffer-read-only)
97 (if (and visit (or beg end))
98 (error "Attempt to visit less than an entire file"))
99 (setq file (expand-file-name file))
100 (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
101 (context (epg-make-context))
104 (setq buffer-file-name file))
105 (epg-context-set-passphrase-callback
107 (cons #'epa-file-passphrase-callback-function
109 (epg-context-set-progress-callback context
110 #'epa-progress-callback-function)
114 (goto-char (point-min)))
115 (condition-case error
116 (setq string (epg-decrypt-file context file nil))
118 (if (setq entry (assoc file epa-file-passphrase-alist))
121 (cons "Opening input file" (cdr error)))))
123 (setq string (substring string (or beg 0) end)))
126 (narrow-to-region (point) (point))
127 (epa-file-decode-and-insert string file visit beg end replace)
128 (setq length (- (point-max) (point-min))))
130 (delete-region (point) (point-max)))))
132 (file-exists-p local-copy))
133 (delete-file local-copy)))
135 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
137 (defun epa-file-write-region (start end file &optional append visit lockname
140 (error "Can't append to the file."))
141 (setq file (expand-file-name file))
142 (let* ((coding-system (or coding-system-for-write
143 (if (boundp 'last-coding-system-used)
145 (write-region (point-min) (point-max) "/")
146 (error last-coding-system-used))
147 buffer-file-coding-system)))
148 (context (epg-make-context))
149 (coding-system-for-write 'binary)
151 (epg-context-set-passphrase-callback
153 (cons #'epa-file-passphrase-callback-function
155 (epg-context-set-progress-callback context
156 #'epa-progress-callback-function)
157 (condition-case error
162 (encode-coding-string start coding-system)
163 (encode-coding-string (buffer-substring start end)
165 (unless (assoc file epa-file-passphrase-alist)
168 "Select recipents for encryption.
169 If no one is selected, symmetric encryption will be performed. "))))
171 (if (setq entry (assoc file epa-file-passphrase-alist))
173 (signal 'file-error (cons "Opening output file" (cdr error)))))
174 (epa-file-run-real-handler
176 (list string nil file append visit lockname mustbenew))
177 (if (boundp 'last-coding-system-used)
178 (setq last-coding-system-used coding-system))
181 (setq buffer-file-name file)
182 (set-visited-file-modtime))
185 (set-visited-file-modtime)
186 (setq buffer-file-name visit))))
190 (message "Wrote %s" buffer-file-name))))
191 (put 'write-region 'epa-file 'epa-file-write-region)
194 (defun epa-file-enable ()
196 (if (memq epa-file-handler file-name-handler-alist)
197 (message "`epa-file' already enabled")
198 (setq file-name-handler-alist
199 (cons epa-file-handler file-name-handler-alist))
200 (message "`epa-file' enabled")))
203 (defun epa-file-disable ()
205 (if (memq epa-file-handler file-name-handler-alist)
207 (setq file-name-handler-alist
208 (delq epa-file-handler file-name-handler-alist))
209 (message "`epa-file' disabled"))
210 (message "`epa-file' already disabled")))
214 ;;; epa-file.el ends here