* 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-name
64                             (expand-file-name "pgg-output"
65                                               temporary-file-directory)))
66          (args
67           `("--status-fd" "2"
68             ,@(if passphrase '("--passphrase-fd" "0"))
69             "--output" ,output-file-name
70             ,@pgg-gpg-extra-args ,@args))
71          (output-buffer pgg-output-buffer)
72          (errors-buffer pgg-errors-buffer)
73          (orig-mode (default-file-modes))
74          (process-connection-type nil)
75          process status exit-status)
76     (with-current-buffer (get-buffer-create errors-buffer)
77       (buffer-disable-undo)
78       (erase-buffer))
79     (unwind-protect
80         (progn
81           (set-default-file-modes 448)
82           (setq process
83                 (apply #'binary-to-text-funcall
84                        pgg-gpg-messages-coding-system
85                        #'start-process "*GnuPG*" errors-buffer
86                        program args))
87           (set-process-sentinel process #'ignore)
88           (when passphrase
89             (process-send-string process (concat passphrase "\n")))
90           (process-send-region process start end)
91           (process-send-eof process)
92           (while (eq 'run (process-status process))
93             (accept-process-output process 5))
94           (setq status (process-status process)
95                 exit-status (process-exit-status process))
96           (delete-process process)
97           (with-current-buffer (get-buffer-create output-buffer)
98             (buffer-disable-undo)
99             (erase-buffer)
100             (if (file-exists-p output-file-name)
101                 (let ((coding-system-for-read 'raw-text-dos))
102                   (insert-file-contents output-file-name)))
103             (set-buffer errors-buffer)
104             (if (memq status '(stop signal))
105                 (error "%s exited abnormally: '%s'" program exit-status))
106             (if (= 127 exit-status)
107                 (error "%s could not be found" program))))
108       (if (and process (eq 'run (process-status process)))
109           (interrupt-process process))
110       (if (file-exists-p output-file-name)
111           (delete-file output-file-name))
112       (set-default-file-modes orig-mode))))
113
114 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
115   (if (and pgg-cache-passphrase
116            (progn
117              (goto-char (point-min))
118              (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
119       (pgg-add-passphrase-cache
120        (progn
121          (goto-char (point-min))
122          (if (re-search-forward
123               "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
124              (substring (match-string 0) -8)))
125        passphrase)))
126
127 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-gpg)
128                                            string &optional type)
129   (let ((args (list "--with-colons" "--no-greeting" "--batch"
130                     (if type "--list-secret-keys" "--list-keys")
131                     string)))
132     (with-temp-buffer
133       (apply #'call-process pgg-gpg-program nil t nil args)
134       (goto-char (point-min))
135       (if (re-search-forward "^\\(sec\\|pub\\):"  nil t)
136           (substring
137            (nth 3 (split-string
138                    (buffer-substring (match-end 0)
139                                      (progn (end-of-line)(point)))
140                    ":")) 8)))))
141
142 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-gpg)
143                                                start end recipients)
144   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
145          (args
146           `("--batch" "--armor" "--always-trust" "--encrypt"
147             ,@(if recipients
148                   (apply #'nconc
149                          (mapcar (lambda (rcpt)
150                                    (list "--remote-user" rcpt))
151                                  (append recipients
152                                          (if pgg-encrypt-for-me
153                                              (list pgg-gpg-user-id)))))))))
154     (pgg-as-lbt start end 'CRLF
155       (pgg-gpg-process-region start end nil pgg-gpg-program args))
156     (pgg-process-when-success)))
157
158 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-gpg)
159                                                start end)
160   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
161          (passphrase
162           (pgg-read-passphrase
163            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
164            (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'encrypt)))
165          (args '("--batch" "--decrypt")))
166     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
167     (with-current-buffer pgg-errors-buffer
168       (pgg-gpg-possibly-cache-passphrase passphrase)
169       (goto-char (point-min))
170       (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
171
172 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-gpg)
173                                             start end &optional cleartext)
174   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
175          (passphrase
176           (pgg-read-passphrase
177            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
178            (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'sign)))
179          (args
180           (list (if cleartext "--clearsign" "--detach-sign")
181                 "--armor" "--batch" "--verbose"
182                 "--local-user" pgg-gpg-user-id))
183          (inhibit-read-only t)
184          buffer-read-only)
185     (pgg-as-lbt start end 'CRLF
186       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
187     (with-current-buffer pgg-errors-buffer
188       (pgg-gpg-possibly-cache-passphrase passphrase))
189     (pgg-process-when-success)))
190
191 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-gpg)
192                                               start end &optional signature)
193   (let ((args '("--batch" "--verify")))
194     (when (stringp signature)
195       (setq args (append args (list signature))))
196     (setq args (append args '("-")))
197     (pgg-gpg-process-region start end nil pgg-gpg-program args)
198     (with-current-buffer pgg-errors-buffer
199       (goto-char (point-min))
200       (while (re-search-forward "^gpg: " nil t)
201         (replace-match ""))
202       (goto-char (point-min))
203       (prog1 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t)
204         (goto-char (point-min))
205         (delete-matching-lines "^warning\\|\\[GNUPG:]")
206         (set-buffer pgg-output-buffer)
207         (insert-buffer-substring pgg-errors-buffer)))))
208
209 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-gpg))
210   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
211          (args (list "--batch" "--export" "--armor"
212                      pgg-gpg-user-id)))
213     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
214     (insert-buffer-substring pgg-output-buffer)))
215
216 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-gpg)
217                                                   start end)
218   (let ((args '("--import" "--batch" "-")) status)
219     (pgg-gpg-process-region start end nil pgg-gpg-program args)
220     (set-buffer pgg-errors-buffer)
221     (goto-char (point-min))
222     (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
223       (setq status (buffer-substring (match-end 0)
224                                      (progn (end-of-line)(point)))
225             status (vconcat (mapcar #'string-to-int (split-string status))))
226       (erase-buffer)
227       (insert (format "Imported %d key(s).
228 \tArmor contains %d key(s) [%d bad, %d old].\n"
229                       (+ (aref status 2)
230                          (aref status 10))
231                       (aref status 0)
232                       (aref status 1)
233                       (+ (aref status 4)
234                          (aref status 11)))
235               (if (zerop (aref status 9))
236                   ""
237                 "\tSecret keys are imported.\n")))
238     (append-to-buffer pgg-output-buffer (point-min)(point-max))
239     (pgg-process-when-success)))
240
241 (provide 'pgg-gpg)
242
243 ;;; pgg-gpg.el ends here