1 ;;; epa-file.el --- the EasyPG Assistant hooks for 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-passphrase-alist nil)
39 (defun epa-file-passphrase-callback-function (key-id file)
41 (let ((entry (assoc file epa-file-passphrase-alist))
43 (or (copy-sequence (cdr entry))
46 (setq entry (list file)
47 epa-file-passphrase-alist (cons entry
48 epa-file-passphrase-alist)))
49 (setq passphrase (epg-passphrase-callback-function key-id nil))
50 (setcdr entry (copy-sequence passphrase))
52 (epg-passphrase-callback-function key-id nil)))
54 (defun epa-file-handler (operation &rest args)
56 (let ((op (get operation 'epa-file)))
59 (epa-file-run-real-handler operation args)))))
61 (defun epa-file-run-real-handler (operation args)
62 (let ((inhibit-file-name-handlers
63 (cons 'epa-file-handler
64 (and (eq inhibit-file-name-operation operation)
65 inhibit-file-name-handlers)))
66 (inhibit-file-name-operation operation))
67 (apply operation args)))
69 (defvar last-coding-system-used)
70 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
71 (barf-if-buffer-read-only)
73 (error "Can't read the file partially."))
74 (setq file (expand-file-name file))
75 (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
76 (context (epg-make-context))
79 (setq buffer-file-name file))
80 (epg-context-set-passphrase-callback
82 (cons #'epa-file-passphrase-callback-function
87 (goto-char (point-min)))
89 (setq string (decode-coding-string
90 (epg-decrypt-file context file nil)
93 (if (setq entry (assoc file epa-file-passphrase-alist))
96 (cons "Opening input file" (nthcdr 2 error)))))
97 (if (boundp 'last-coding-system-used)
98 (set-buffer-file-coding-system last-coding-system-used)
99 (set-buffer-file-coding-system default-buffer-file-coding-system))
101 (setq length (length string))
103 (delete-region (point) (point-max))))
105 (file-exists-p local-copy))
106 (delete-file local-copy)))
108 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
110 (defun epa-file-write-region (start end file &optional append visit lockname
113 (error "Can't append to the file."))
114 (setq file (expand-file-name file))
115 (let* ((coding-system (if (boundp 'last-coding-system-used)
117 (write-region (point-min) (point-max) "/")
118 (error last-coding-system-used))
119 buffer-file-coding-system))
120 (context (epg-make-context))
121 (coding-system-for-write 'binary)
123 (epg-context-set-passphrase-callback
125 (cons #'epa-file-passphrase-callback-function
127 (condition-case error
131 (encode-coding-string (buffer-string) coding-system)
132 (mapcar (lambda (key)
133 (epg-sub-key-id (car (epg-key-sub-key-list key))))
134 (unless (assoc file epa-file-passphrase-alist)
136 "Select recipents for encryption.
137 If no one is selected, symmetric encryption will be performed. ")))))
139 (if (setq entry (assoc file epa-file-passphrase-alist))
141 (signal 'file-error (cons "Opening output file" (nthcdr 2 error)))))
142 (epa-file-run-real-handler
144 (list string nil file append visit lockname mustbenew))
145 (if (boundp 'last-coding-system-used)
146 (setq last-coding-system-used coding-system))
149 (setq buffer-file-name file)
150 (set-visited-file-modtime))
153 (set-visited-file-modtime)
154 (setq buffer-file-name visit))))
158 (message "Wrote %s" buffer-file-name))))
159 (put 'write-region 'epa-file 'epa-file-write-region)
163 ;;; epa-file.el ends here