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