* pgg-gpg.el (pgg-gpg-process-region): Undo the last change;
[elisp/semi.git] / pgg-gpg.el
1 ;;; pgg-gpg.el --- GnuPG support for PGG.
2
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Created: 1999/10/28
7 ;; Keywords: PGP, OpenPGP, GnuPG
8
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'mel) ; binary-to-text-funcall
29 (eval-when-compile (require 'pgg))
30
31 (defgroup pgg-gpg ()
32   "GnuPG interface"
33   :group 'pgg)
34
35 (defcustom pgg-gpg-program "gpg" 
36   "The GnuPG executable."
37   :group 'pgg-gpg
38   :type 'string)
39
40 (defcustom pgg-gpg-extra-args nil
41   "Extra arguments for every GnuPG invocation."
42   :group 'pgg-gpg
43   :type 'string)
44
45 (eval-and-compile
46   (luna-define-class pgg-scheme-gpg (pgg-scheme)))
47
48 (defvar pgg-gpg-user-id nil
49   "GnuPG ID of your default identity.")
50
51 (defvar pgg-gpg-messages-coding-system pgg-messages-coding-system
52   "Coding system used when reading from a GnuPG external process.")
53
54 (defvar pgg-scheme-gpg-instance nil)
55
56 ;;;###autoload
57 (defun pgg-make-scheme-gpg ()
58   (or pgg-scheme-gpg-instance
59       (setq pgg-scheme-gpg-instance
60             (luna-make-entity 'pgg-scheme-gpg))))
61
62 (defun pgg-gpg-process-region (start end passphrase program args)
63   (let* ((output-file-name (make-temp-file
64                             (expand-file-name "pgg-output"
65                                               temporary-file-directory)))
66          (args
67           `("--status-fd" "2"
68             ,@(if passphrase '("--passphrase-fd" "0"))
69             "--yes" ; overwrite
70             "--output" ,output-file-name
71             ,@pgg-gpg-extra-args ,@args))
72          (output-buffer pgg-output-buffer)
73          (errors-buffer pgg-errors-buffer)
74          (orig-mode (default-file-modes))
75          (process-connection-type nil)
76          process status exit-status)
77     (with-current-buffer (get-buffer-create errors-buffer)
78       (buffer-disable-undo)
79       (erase-buffer))
80     (unwind-protect
81         (progn
82           (set-default-file-modes 448)
83           (setq process
84                 (apply #'binary-to-text-funcall
85                        pgg-gpg-messages-coding-system
86                        #'start-process "*GnuPG*" errors-buffer
87                        program args))
88           (set-process-sentinel process #'ignore)
89           (when passphrase
90             (process-send-string process (concat passphrase "\n")))
91           (process-send-region process start end)
92           (process-send-eof process)
93           (while (eq 'run (process-status process))
94             (accept-process-output process 5))
95           (setq status (process-status process)
96                 exit-status (process-exit-status process))
97           (delete-process process)
98           (with-current-buffer (get-buffer-create output-buffer)
99             (buffer-disable-undo)
100             (erase-buffer)
101             (if (file-exists-p output-file-name)
102                 (let ((coding-system-for-read 'raw-text-dos))
103                   (insert-file-contents output-file-name)))
104             (set-buffer errors-buffer)
105             (if (memq status '(stop signal))
106                 (error "%s exited abnormally: '%s'" program exit-status))
107             (if (= 127 exit-status)
108                 (error "%s could not be found" program))))
109       (if (and process (eq 'run (process-status process)))
110           (interrupt-process process))
111       (if (file-exists-p output-file-name)
112           (delete-file output-file-name))
113       (set-default-file-modes orig-mode))))
114
115 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
116   (if (and pgg-cache-passphrase
117            (progn
118              (goto-char (point-min))
119              (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
120       (pgg-add-passphrase-cache
121        (progn
122          (goto-char (point-min))
123          (if (re-search-forward
124               "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
125              (substring (match-string 0) -8)))
126        passphrase)))
127
128 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-gpg)
129                                            string &optional type)
130   (let ((args (list "--with-colons" "--no-greeting" "--batch"
131                     (if type "--list-secret-keys" "--list-keys")
132                     string)))
133     (with-temp-buffer
134       (apply #'call-process pgg-gpg-program nil t nil args)
135       (goto-char (point-min))
136       (if (re-search-forward "^\\(sec\\|pub\\):"  nil t)
137           (substring
138            (nth 3 (split-string
139                    (buffer-substring (match-end 0)
140                                      (progn (end-of-line)(point)))
141                    ":")) 8)))))
142
143 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-gpg)
144                                                start end recipients)
145   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
146          (args
147           `("--batch" "--armor" "--always-trust" "--encrypt"
148             ,@(if recipients
149                   (apply #'nconc
150                          (mapcar (lambda (rcpt)
151                                    (list "--remote-user" rcpt))
152                                  (append recipients
153                                          (if pgg-encrypt-for-me
154                                              (list pgg-gpg-user-id)))))))))
155     (pgg-as-lbt start end 'CRLF
156       (pgg-gpg-process-region start end nil pgg-gpg-program args))
157     (pgg-process-when-success)))
158
159 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-gpg)
160                                                start end)
161   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
162          (passphrase
163           (pgg-read-passphrase
164            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
165            (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'encrypt)))
166          (args '("--batch" "--decrypt")))
167     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
168     (with-current-buffer pgg-errors-buffer
169       (pgg-gpg-possibly-cache-passphrase passphrase)
170       (goto-char (point-min))
171       (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
172
173 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-gpg)
174                                             start end &optional cleartext)
175   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
176          (passphrase
177           (pgg-read-passphrase
178            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
179            (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'sign)))
180          (args
181           (list (if cleartext "--clearsign" "--detach-sign")
182                 "--armor" "--batch" "--verbose"
183                 "--local-user" pgg-gpg-user-id))
184          (inhibit-read-only t)
185          buffer-read-only)
186     (pgg-as-lbt start end 'CRLF
187       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
188     (with-current-buffer pgg-errors-buffer
189       (pgg-gpg-possibly-cache-passphrase passphrase))
190     (pgg-process-when-success)))
191
192 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-gpg)
193                                               start end &optional signature)
194   (let ((args '("--batch" "--verify")))
195     (when (stringp signature)
196       (setq args (append args (list signature))))
197     (setq args (append args '("-")))
198     (pgg-gpg-process-region start end nil pgg-gpg-program args)
199     (with-current-buffer pgg-errors-buffer
200       (goto-char (point-min))
201       (while (re-search-forward "^gpg: " nil t)
202         (replace-match ""))
203       (goto-char (point-min))
204       (prog1 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t)
205         (goto-char (point-min))
206         (delete-matching-lines "^warning\\|\\[GNUPG:]")
207         (set-buffer pgg-output-buffer)
208         (insert-buffer-substring pgg-errors-buffer)))))
209
210 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-gpg))
211   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
212          (args (list "--batch" "--export" "--armor"
213                      pgg-gpg-user-id)))
214     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
215     (insert-buffer-substring pgg-output-buffer)))
216
217 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-gpg)
218                                                   start end)
219   (let ((args '("--import" "--batch" "-")) status)
220     (pgg-gpg-process-region start end nil pgg-gpg-program args)
221     (set-buffer pgg-errors-buffer)
222     (goto-char (point-min))
223     (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
224       (setq status (buffer-substring (match-end 0)
225                                      (progn (end-of-line)(point)))
226             status (vconcat (mapcar #'string-to-int (split-string status))))
227       (erase-buffer)
228       (insert (format "Imported %d key(s).
229 \tArmor contains %d key(s) [%d bad, %d old].\n"
230                       (+ (aref status 2)
231                          (aref status 10))
232                       (aref status 0)
233                       (aref status 1)
234                       (+ (aref status 4)
235                          (aref status 11)))
236               (if (zerop (aref status 9))
237                   ""
238                 "\tSecret keys are imported.\n")))
239     (append-to-buffer pgg-output-buffer (point-min)(point-max))
240     (pgg-process-when-success)))
241
242 (provide 'pgg-gpg)
243
244 ;;; pgg-gpg.el ends here