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