* pgg-gpg.el (lookup-key-string): Use `call-process' instead of
[elisp/semi.git] / pgg-gpg.el
1 ;;; pgg-gpg.el --- GnuPG support for PGG.
2
3 ;; Copyright (C) 1999 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
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 (eval-when-compile (require 'pgg))
29
30 (defgroup pgg-gpg ()
31   "GnuPG interface"
32   :group 'pgg)
33
34 (defcustom pgg-gpg-program "gpg" 
35   "The GnuPG executable."
36   :group 'pgg-gpg
37   :type 'string)
38
39 (defcustom pgg-gpg-shell-file-name "/bin/sh"
40   "The GnuPG executable."
41   :group 'pgg-gpg
42   :type 'string)
43
44 (defcustom pgg-gpg-extra-args nil
45   "Extra arguments for every GnuPG invocation."
46   :group 'pgg-gpg
47   :type 'string)
48
49 (eval-and-compile
50   (luna-define-class pgg-scheme-gpg (pgg-scheme))
51   )
52   
53 (defvar pgg-gpg-user-id nil
54   "GnuPG ID of your default identity.")
55
56 (defvar pgg-scheme-gpg-instance nil)
57
58 ;;;###autoload
59 (defun pgg-make-scheme-gpg ()
60   (or pgg-scheme-gpg-instance
61       (setq pgg-scheme-gpg-instance
62             (luna-make-entity 'pgg-scheme-gpg))))
63
64 (defun pgg-gpg-process-region (start end passphrase program args)
65   (let* ((errors-file-name
66           (concat temporary-file-directory 
67                   (make-temp-name "pgg-errors")))
68          (status-file-name
69           (concat temporary-file-directory 
70                   (make-temp-name "pgg-status")))
71          (args 
72           (append
73            `("--status-fd" "3"
74              ,@(if passphrase '("--passphrase-fd" "0"))
75              ,@pgg-gpg-extra-args)
76            args
77            (list (concat "2>" errors-file-name)
78                  (concat "3>" status-file-name))))
79          (shell-file-name pgg-gpg-shell-file-name)
80          (output-buffer pgg-output-buffer)
81          (errors-buffer pgg-errors-buffer)
82          (status-buffer pgg-status-buffer)
83          (process-connection-type nil)
84          process status exit-status)
85     (with-current-buffer (get-buffer-create output-buffer)
86       (buffer-disable-undo)
87       (erase-buffer))
88     (setq process
89           (apply #'start-process-shell-command "*GnuPG*" output-buffer
90                  program args))
91     (set-process-sentinel process 'ignore)
92     (when passphrase
93       (process-send-string process (concat passphrase "\n")))
94     (process-send-region process start end)
95     (process-send-eof process)
96     (while (eq 'run (process-status process))
97       (accept-process-output process 5))
98     (setq status (process-status process)
99           exit-status (process-exit-status process))
100     (delete-process process)
101     (with-current-buffer output-buffer
102       (goto-char (point-min))
103       (while (search-forward "\r$" nil t)
104         (replace-match ""))
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
110       (set-buffer (get-buffer-create errors-buffer))
111       (buffer-disable-undo)
112       (erase-buffer)
113       (insert-file-contents errors-file-name)
114       (delete-file errors-file-name)
115       
116       (set-buffer (get-buffer-create status-buffer))
117       (buffer-disable-undo)
118       (erase-buffer)
119       (insert-file-contents status-file-name)
120       (delete-file status-file-name)
121
122       (if (and process (eq 'run (process-status process)))
123           (interrupt-process process))
124       )
125     ))
126
127 (luna-define-method lookup-key-string ((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-current-buffer (get-buffer-create pgg-output-buffer)
133       (buffer-disable-undo)
134       (erase-buffer)
135       (apply #'call-process pgg-gpg-program nil t args)
136       (goto-char (point-min))
137       (when (re-search-forward "^\\(sec\\|pub\\):"  nil t)
138         (substring 
139          (nth 3 (split-string 
140                  (buffer-substring (match-end 0)
141                                    (progn (end-of-line)(point)))
142                  ":"))
143          8)))
144     ))
145
146 (luna-define-method encrypt-region ((scheme pgg-scheme-gpg) 
147                                     start end recipients)
148   (let* ((pgg-gpg-user-id pgg-default-user-id)
149          (passphrase
150           (pgg-read-passphrase 
151            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
152            (luna-send scheme 'lookup-key-string
153                       scheme pgg-gpg-user-id 'encrypt)))
154          (args 
155           `("--batch" "--armor" "--textmode" "--always-trust" "--encrypt"
156             ,@(if recipients
157                   (apply #'append 
158                          (mapcar (lambda (rcpt) 
159                                    (list "--remote-user" 
160                                          (concat "\"" rcpt "\""))) 
161                                  recipients))))))
162     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
163     (pgg-process-when-success
164       (let ((packet 
165              (cdr (assq 1 (pgg-parse-armor-region 
166                            (point-min)(point-max))))))
167         (pgg-add-passphrase-cache 
168          (cdr (assq 'key-identifier packet))
169          passphrase)))
170     ))
171
172 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg) 
173                                     start end)
174   (let* ((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            (luna-send scheme 'lookup-key-string 
179                       scheme pgg-gpg-user-id 'encrypt)))
180          (args '("--batch" "--decrypt")))
181     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
182     (pgg-process-when-success nil)
183     ))
184
185 (luna-define-method sign-region ((scheme pgg-scheme-gpg) 
186                                  start end &optional cleartext)
187   (let* ((pgg-gpg-user-id pgg-default-user-id)
188          (passphrase
189           (pgg-read-passphrase 
190            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
191            (luna-send scheme 'lookup-key-string 
192                       scheme pgg-gpg-user-id 'sign)))
193          (args 
194           (list (if cleartext "--clearsign" "--detach-sign")
195                 "--armor" "--batch" "--verbose" 
196                 "--local-user" pgg-gpg-user-id)))
197     (goto-char start)
198     (setq end (set-marker (make-marker) (point-max)))
199     (while (progn (end-of-line) (> (marker-position end) (point)))
200       (insert "\r")
201       (forward-line 1))
202     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
203     (goto-char start)
204     (while (re-search-forward "\r$" end t)
205       (replace-match ""))
206     (pgg-process-when-success
207       (goto-char (point-min))
208       (while (re-search-forward "\r$" end t)
209         (replace-match ""))
210       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
211         (let ((packet 
212                (cdr (assq 2 (pgg-parse-armor-region 
213                              (progn (beginning-of-line 2)
214                                     (point))
215                              (point-max))))))
216           (pgg-add-passphrase-cache 
217            (cdr (assq 'key-identifier packet))
218            passphrase))))
219     ))
220
221 (luna-define-method verify-region ((scheme pgg-scheme-gpg) 
222                                    start end &optional signature)
223   (let ((args '("--batch" "--verify")))
224     (when (stringp signature)
225       (setq args (append args (list signature))))
226     (pgg-gpg-process-region start end nil pgg-gpg-program args)
227     (save-excursion
228       (set-buffer pgg-status-buffer)
229       (goto-char (point-min))
230       (when (re-search-forward "^\\[GNUPG:] +GOODSIG +" nil t)
231         (set-buffer pgg-errors-buffer)
232         (goto-char (point-min))
233         (while (re-search-forward "^gpg: " nil t)
234           (replace-match ""))
235         (goto-char (point-min))
236         (let ((case-fold-search t))
237           (while (re-search-forward "^warning: " nil t)
238             (delete-region (match-beginning 0)
239                            (progn (beginning-of-line 2) (point)))))
240         (append-to-buffer pgg-output-buffer
241                           (point-min)(point-max))
242         t))
243     ))
244
245 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
246   (let* ((pgg-gpg-user-id pgg-default-user-id)
247          (args (list "--batch" "--export" "--armor" 
248                      (concat "\"" pgg-gpg-user-id "\""))))
249     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
250     (insert-buffer-substring pgg-output-buffer)
251     ))
252
253 (luna-define-method snarf-keys-region ((scheme pgg-scheme-gpg)
254                                        start end)
255   (let ((args '("--import" "--batch" "-")) status)
256     (pgg-gpg-process-region start end nil pgg-gpg-program args)
257     (set-buffer pgg-status-buffer)
258     (goto-char (point-min))
259     (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
260       (setq status (buffer-substring (match-end 0) 
261                                      (progn (end-of-line) 
262                                             (point)))
263             status (vconcat (mapcar #'string-to-int 
264                                     (split-string status))))
265       (erase-buffer)
266       (insert (format "Imported %d key(s).
267 \tArmor contains %d key(s) [%d bad, %d old].\n"
268                       (+ (aref status 2)
269                          (aref status 10))
270                       (aref status 0)
271                       (aref status 1)
272                       (+ (aref status 4)
273                          (aref status 11)))
274               (if (zerop (aref status 9))
275                   ""
276                 "\tSecret keys are imported.\n")))
277     (append-to-buffer pgg-output-buffer
278                       (point-min)(point-max))
279     (with-current-buffer pgg-output-buffer
280       (if (zerop (buffer-size)) nil t))
281     ))
282
283 (provide 'pgg-gpg)
284
285 ;;; pgg-gpg.el ends here
286