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