Sync up with EMIKO 1.13.9.
[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.  Bourne shell or its equivalent
56 \(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 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   "GnuPG 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          (output-buffer pgg-output-buffer)
95          (errors-buffer pgg-errors-buffer)
96          (process-connection-type nil)
97          process status exit-status)
98     (with-current-buffer (get-buffer-create output-buffer)
99       (buffer-disable-undo)
100       (erase-buffer))
101     (when passphrase
102       (setenv "PGPPASSFD" "0"))
103     (as-binary-process
104      (setq process
105            (apply #'start-process-shell-command "*PGP*" output-buffer
106                   program args)))
107     (set-process-sentinel process #'ignore)
108     (when passphrase
109       (process-send-string process (concat passphrase "\n")))
110     (process-send-region process start end)
111     (process-send-eof process)
112     (while (eq 'run (process-status process))
113       (accept-process-output process 5))
114     (setq status (process-status process)
115           exit-status (process-exit-status process))
116     (delete-process process)
117     (with-current-buffer output-buffer
118       (pgg-convert-lbt-region (point-min)(point-max) 'LF)
119
120       (if (memq status '(stop signal))
121           (error "%s exited abnormally: '%s'" program exit-status))
122       (if (= 127 exit-status)
123           (error "%s could not be found" program))
124
125       (set-buffer (get-buffer-create errors-buffer))
126       (buffer-disable-undo)
127       (erase-buffer)
128       (insert-file-contents errors-file-name)
129       (delete-file errors-file-name)
130       
131       (if (and process (eq 'run (process-status process)))
132           (interrupt-process process)))))
133
134 (luna-define-method pgg-scheme-lookup-key-string ((scheme pgg-scheme-pgp5) 
135                                                   string &optional type)
136   (let ((args (list "+language=en" "-l" string)))
137     (with-current-buffer (get-buffer-create pgg-output-buffer)
138       (buffer-disable-undo)
139       (erase-buffer)
140       (apply #'call-process pgg-pgp5-pgpk-program nil t nil args)
141       (goto-char (point-min))
142       (when (re-search-forward "^sec" nil t)
143         (substring 
144          (nth 2 (split-string 
145                  (buffer-substring (match-end 0)(progn (end-of-line)(point)))))
146          2)))))
147
148 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-pgp5) 
149                                                start end recipients)
150   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
151          (args 
152           `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
153             ,@(if recipients
154                   (apply #'append 
155                          (mapcar (lambda (rcpt) 
156                                    (list "-r" 
157                                          (concat "\"" rcpt "\""))) 
158                                  (append recipients
159                                          (if pgg-encrypt-for-me
160                                              (list pgg-pgp5-user-id)))))))))
161     (pgg-pgp5-process-region start end nil pgg-pgp5-pgpe-program args)
162     (pgg-process-when-success nil)))
163
164 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-pgp5) 
165                                                start end)
166   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
167          (passphrase
168           (pgg-read-passphrase 
169            (format "PGP passphrase for %s: " pgg-pgp5-user-id)
170            (luna-send scheme 'lookup-key-string 
171                       scheme pgg-pgp5-user-id 'encrypt)))
172          (args 
173           '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
174     (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgpv-program args)
175     (pgg-process-when-success nil)))
176
177 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-pgp5) 
178                                             start end &optional clearsign)
179   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
180          (passphrase
181           (pgg-read-passphrase 
182            (format "PGP passphrase for %s: " pgg-pgp5-user-id)
183            (luna-send scheme 'lookup-key-string 
184                       scheme pgg-pgp5-user-id 'sign)))
185          (args 
186           (list (if clearsign "-fat" "-fbat")
187                 "+verbose=1" "+language=us" "+batchmode=1"
188                 "-u" pgg-pgp5-user-id)))
189     (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgps-program args)
190     (pgg-process-when-success
191       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
192         (let ((packet 
193                (cdr (assq 2 (pgg-parse-armor-region 
194                              (progn (beginning-of-line 2)
195                                     (point))
196                              (point-max))))))
197           (if pgg-cache-passphrase
198               (pgg-add-passphrase-cache 
199                (cdr (assq 'key-identifier packet))
200                passphrase)))))))
201
202 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-pgp5) 
203                                               start end &optional signature)
204   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
205          (orig-file (make-temp-name basename))
206          (args '("+verbose=1" "+batchmode=1" "+language=us"))
207          (orig-mode (default-file-modes)))
208     (unwind-protect
209         (progn
210           (set-default-file-modes 448)
211           (write-region-as-binary start end orig-file))
212       (set-default-file-modes orig-mode))
213     (when (stringp signature)
214       (copy-file signature (setq signature (concat orig-file ".asc")))
215       (setq args (append args (list signature))))
216     (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpv-program args)
217     (delete-file orig-file)
218     (if signature (delete-file signature))
219     (with-current-buffer pgg-errors-buffer
220       (goto-char (point-min))
221       (re-search-forward "^Good signature" nil t))))
222
223 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-pgp5))
224   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
225          (args
226           (list "+verbose=1" "+batchmode=1" "+language=us" "-x" 
227                 (concat "\"" pgg-pgp5-user-id "\""))))
228     (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpk-program args)
229     (insert-buffer-substring pgg-output-buffer)))
230
231 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-pgp5)
232                                                   start end)
233   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
234          (basename (expand-file-name "pgg" temporary-file-directory))
235          (key-file (make-temp-name basename))
236          (args 
237           (list "+verbose=1" "+batchmode=1" "+language=us" "-a" 
238                 key-file)))
239     (write-region-as-raw-text-CRLF start end key-file)
240     (pgg-pgp5-process-region start end nil pgg-pgp5-pgpk-program args)
241     (delete-file key-file)
242     (pgg-process-when-success nil)))
243
244 (provide 'pgg-pgp5)
245
246 ;;; pgg-pgp5.el ends here