* pgg-def.el (pgg-messages-coding-system): Use `defvar' to define.
[elisp/semi.git] / pgg-pgp.el
1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
2
3 ;; Copyright (C) 1999,2000 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
6 ;; Created: 1999/11/02
7 ;; Keywords: PGP, OpenPGP
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, binary-write-decoded-region
29 (eval-when-compile (require 'pgg))
30
31 (defgroup pgg-pgp ()
32   "PGP 2.* and 6.* interface"
33   :group 'pgg)
34
35 (defcustom pgg-pgp-program "pgp"
36   "PGP 2.* and 6.* executable."
37   :group 'pgg-pgp
38   :type 'string)
39
40 (defcustom pgg-pgp-shell-file-name "/bin/sh"
41   "File name to load inferior shells from.
42 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
43   :group 'pgg-pgp
44   :type 'string)
45
46 (defcustom pgg-pgp-shell-command-switch "-c"
47   "Switch used to have the shell execute its command line argument."
48   :group 'pgg-pgp
49   :type 'string)
50
51 (defcustom pgg-pgp-extra-args nil
52   "Extra arguments for every PGP invocation."
53   :group 'pgg-pgp
54   :type 'string)
55
56 (eval-and-compile
57   (luna-define-class pgg-scheme-pgp (pgg-scheme)))
58
59 (defvar pgg-pgp-user-id nil
60   "PGP ID of your default identity.")
61
62 (defvar pgg-scheme-pgp-instance nil)
63
64 ;;;###autoload
65 (defun pgg-make-scheme-pgp ()
66   (or pgg-scheme-pgp-instance
67       (setq pgg-scheme-pgp-instance
68             (luna-make-entity 'pgg-scheme-pgp))))
69
70 (defun pgg-pgp-process-region (start end passphrase program args)
71   (let* ((errors-file-name
72           (concat temporary-file-directory
73                   (make-temp-name "pgg-errors")))
74          (args
75           (append args
76                   pgg-pgp-extra-args
77                   (list (concat "2>" errors-file-name))))
78          (shell-file-name pgg-pgp-shell-file-name)
79          (shell-command-switch pgg-pgp-shell-command-switch)
80          (process-environment process-environment)
81          (output-buffer pgg-output-buffer)
82          (errors-buffer pgg-errors-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     (when passphrase
89       (setenv "PGPPASSFD" "0"))
90     (unwind-protect
91         (progn
92           (setq process
93                 (apply #'binary-funcall
94                        #'start-process-shell-command "*PGP*" output-buffer
95                        program args))
96           (set-process-sentinel process #'ignore)
97           (when passphrase
98             (process-send-string process (concat passphrase "\n")))
99           (process-send-region process start end)
100           (process-send-eof process)
101           (while (eq 'run (process-status process))
102             (accept-process-output process 5))
103           (setq status (process-status process)
104                 exit-status (process-exit-status process))
105           (delete-process process)
106           (with-current-buffer output-buffer
107             (pgg-convert-lbt-region (point-min)(point-max) 'LF)
108
109             (if (memq status '(stop signal))
110                 (error "%s exited abnormally: '%s'" program exit-status))
111             (if (= 127 exit-status)
112                 (error "%s could not be found" program))
113
114             (set-buffer (get-buffer-create errors-buffer))
115             (buffer-disable-undo)
116             (erase-buffer)
117             (insert-file-contents errors-file-name)))
118       (if (and process (eq 'run (process-status process)))
119           (interrupt-process process))
120       (condition-case nil
121           (delete-file errors-file-name)
122         (file-error nil)))))
123
124 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-pgp)
125                                                   string &optional type)
126   (let ((args (list "+batchmode" "+language=en" "-kv" string)))
127     (with-current-buffer (get-buffer-create pgg-output-buffer)
128       (buffer-disable-undo)
129       (erase-buffer)
130       (apply #'call-process pgg-pgp-program nil t nil args)
131       (goto-char (point-min))
132       (cond
133        ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
134         (buffer-substring (point)(+ 8 (point))))
135        ((re-search-forward "^Type" nil t);PGP 6.*
136         (beginning-of-line 2)
137         (substring
138          (nth 2 (split-string
139                  (buffer-substring (point)(progn (end-of-line) (point)))))
140          2))))))
141
142 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-pgp)
143                                                start end recipients)
144   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
145          (args
146           `("+encrypttoself=off +verbose=1" "+batchmode"
147             "+language=us" "-fate"
148             ,@(if recipients
149                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
150                           (append recipients
151                                   (if pgg-encrypt-for-me
152                                       (list pgg-pgp-user-id))))))))
153     (pgg-pgp-process-region start end nil pgg-pgp-program args)
154     (pgg-process-when-success nil)))
155
156 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-pgp)
157                                                start end)
158   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
159          (passphrase
160           (pgg-read-passphrase
161            (format "PGP passphrase for %s: " pgg-pgp-user-id)
162            (pgg-scheme-lookup-key scheme pgg-pgp-user-id 'encrypt)))
163          (args
164           '("+verbose=1" "+batchmode" "+language=us" "-f")))
165     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
166     (pgg-process-when-success nil)))
167
168 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-pgp)
169                                             start end &optional clearsign)
170   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
171          (passphrase
172           (pgg-read-passphrase
173            (format "PGP passphrase for %s: " pgg-pgp-user-id)
174            (pgg-scheme-lookup-key scheme pgg-pgp-user-id 'sign)))
175          (args
176           (list (if clearsign "-fast" "-fbast")
177                 "+verbose=1" "+language=us" "+batchmode"
178                 "-u" pgg-pgp-user-id)))
179     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
180     (pgg-process-when-success
181       (goto-char (point-min))
182       (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
183         (let ((packet
184                (cdr (assq 2 (pgg-parse-armor-region
185                              (progn (beginning-of-line 2)
186                                     (point))
187                              (point-max))))))
188           (if pgg-cache-passphrase
189               (pgg-add-passphrase-cache
190                (cdr (assq 'key-identifier packet))
191                passphrase)))))))
192
193 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-pgp)
194                                               start end &optional signature)
195   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
196          (orig-file (make-temp-name basename))
197          (args '("+verbose=1" "+batchmode" "+language=us"))
198          (orig-mode (default-file-modes)))
199     (unwind-protect
200         (progn
201           (set-default-file-modes 448)
202           (binary-write-decoded-region start end orig-file))
203       (set-default-file-modes orig-mode))
204     (when (stringp signature)
205       (copy-file signature (setq signature (concat orig-file ".asc")))
206       (setq args (append args (list signature orig-file))))
207     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
208     (delete-file orig-file)
209     (if signature (delete-file signature))
210     (pgg-process-when-success
211       (goto-char (point-min))
212       (let ((case-fold-search t))
213         (while (re-search-forward "^warning: " nil t)
214           (delete-region (match-beginning 0)
215                          (progn (beginning-of-line 2) (point)))))
216       (goto-char (point-min))
217       (when (re-search-forward "^\\.$" nil t)
218         (delete-region (point-min)
219                        (progn (beginning-of-line 2)
220                               (point)))))))
221
222 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-pgp))
223   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
224          (args
225           (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
226                 (concat "\"" pgg-pgp-user-id "\""))))
227     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
228     (insert-buffer-substring pgg-output-buffer)))
229
230 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-pgp)
231                                                   start end)
232   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
233          (basename (expand-file-name "pgg" temporary-file-directory))
234          (key-file (make-temp-name basename))
235          (args
236           (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
237                 key-file)))
238     (let ((coding-system-for-write 'raw-text-dos))
239       (write-region start end key-file))
240     (pgg-pgp-process-region start end nil pgg-pgp-program args)
241     (delete-file key-file)
242     (pgg-process-when-success nil)))
243
244 (provide 'pgg-pgp)
245
246 ;;; pgg-pgp.el ends here