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