* pgg-gpg.el (pgg-gpg-process-region): Revert previous change.
[gnus] / lisp / pgg-pgp5.el
1 ;;; pgg-pgp5.el --- PGP 5.* support for PGG.
2
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
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 '(choice
69           (const :tag "None" nil)
70           (string :tag "Arguments")))
71
72 (defvar pgg-pgp5-user-id nil
73   "PGP 5.* ID of your default identity.")
74
75 (defun pgg-pgp5-process-region (start end passphrase program args)
76   (let* ((errors-file-name
77           (expand-file-name (make-temp-name "pgg-errors")  
78                             pgg-temporary-file-directory))
79          (args
80           (append args
81                   pgg-pgp5-extra-args
82                   (list (concat "2>" errors-file-name))))
83          (shell-file-name pgg-pgp5-shell-file-name)
84          (shell-command-switch pgg-pgp5-shell-command-switch)
85          (process-environment process-environment)
86          (output-buffer pgg-output-buffer)
87          (errors-buffer pgg-errors-buffer)
88          (process-connection-type nil)
89          process status exit-status)
90     (with-current-buffer (get-buffer-create output-buffer)
91       (buffer-disable-undo)
92       (erase-buffer))
93     (when passphrase
94       (setenv "PGPPASSFD" "0"))
95     (unwind-protect
96         (progn
97           (let ((coding-system-for-read 'binary)
98                 (coding-system-for-write 'binary))
99             (setq process
100                   (apply #'funcall
101                          #'start-process-shell-command "*PGP*" output-buffer
102                          program args)))
103           (set-process-sentinel process #'ignore)
104           (when passphrase
105             (process-send-string process (concat passphrase "\n")))
106           (process-send-region process start end)
107           (process-send-eof process)
108           (while (eq 'run (process-status process))
109             (accept-process-output process 5))
110           (setq status (process-status process)
111                 exit-status (process-exit-status process))
112           (delete-process process)
113           (with-current-buffer output-buffer
114             (pgg-convert-lbt-region (point-min)(point-max) 'LF)
115
116             (if (memq status '(stop signal))
117                 (error "%s exited abnormally: '%s'" program exit-status))
118             (if (= 127 exit-status)
119                 (error "%s could not be found" program))
120
121             (set-buffer (get-buffer-create errors-buffer))
122             (buffer-disable-undo)
123             (erase-buffer)
124             (insert-file-contents errors-file-name)))
125       (if (and process (eq 'run (process-status process)))
126           (interrupt-process process))
127       (condition-case nil
128           (delete-file errors-file-name)
129         (file-error nil)))))
130
131 (defun pgg-pgp5-lookup-key (string &optional type)
132   "Search keys associated with STRING."
133   (let ((args (list "+language=en" "-l" string)))
134     (with-current-buffer (get-buffer-create pgg-output-buffer)
135       (buffer-disable-undo)
136       (erase-buffer)
137       (apply #'call-process pgg-pgp5-pgpk-program nil t nil args)
138       (goto-char (point-min))
139       (when (re-search-forward "^sec" nil t)
140         (substring
141          (nth 2 (split-string
142                  (buffer-substring (match-end 0)(progn (end-of-line)(point)))))
143          2)))))
144
145 (defun pgg-pgp5-encrypt-region (start end recipients)
146   "Encrypt the current region between START and END."
147   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
148          (args
149           `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
150             ,@(if recipients
151                   (apply #'append
152                          (mapcar (lambda (rcpt)
153                                    (list "-r"
154                                          (concat "\"" rcpt "\"")))
155                                  (append recipients
156                                          (if pgg-encrypt-for-me
157                                              (list pgg-pgp5-user-id)))))))))
158     (pgg-pgp5-process-region start end nil pgg-pgp5-pgpe-program args)
159     (pgg-process-when-success nil)))
160
161 (defun pgg-pgp5-decrypt-region (start end)
162   "Decrypt the current region between START and END."
163   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
164          (passphrase
165           (pgg-read-passphrase
166            (format "PGP passphrase for %s: " pgg-pgp5-user-id)
167            (pgg-pgp5-lookup-key pgg-pgp5-user-id 'encrypt)))
168          (args
169           '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
170     (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgpv-program args)
171     (pgg-process-when-success nil)))
172
173 (defun pgg-pgp5-sign-region (start end &optional clearsign)
174   "Make detached signature from text between START and END."
175   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
176          (passphrase
177           (pgg-read-passphrase
178            (format "PGP passphrase for %s: " pgg-pgp5-user-id)
179            (pgg-pgp5-lookup-key pgg-pgp5-user-id 'sign)))
180          (args
181           (list (if clearsign "-fat" "-fbat")
182                 "+verbose=1" "+language=us" "+batchmode=1"
183                 "-u" pgg-pgp5-user-id)))
184     (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgps-program args)
185     (pgg-process-when-success
186       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
187         (let ((packet
188                (cdr (assq 2 (pgg-parse-armor-region
189                              (progn (beginning-of-line 2)
190                                     (point))
191                              (point-max))))))
192           (if pgg-cache-passphrase
193               (pgg-add-passphrase-cache
194                (cdr (assq 'key-identifier packet))
195                passphrase)))))))
196
197 (defun pgg-pgp5-verify-region (start end &optional signature)
198   "Verify region between START and END as the detached signature SIGNATURE."
199   (let* ((basename (expand-file-name "pgg" pgg-temporary-file-directory))
200          (orig-file (make-temp-name basename))
201          (args '("+verbose=1" "+batchmode=1" "+language=us"))
202          (orig-mode (default-file-modes)))
203     (unwind-protect
204         (progn
205           (set-default-file-modes 448)
206           (let ((coding-system-for-write 'binary)
207                 jka-compr-compression-info-list jam-zcat-filename-list)
208             (write-region start end orig-file)))
209       (set-default-file-modes orig-mode))
210     (when (stringp signature)
211       (copy-file signature (setq signature (concat orig-file ".asc")))
212       (setq args (append args (list signature))))
213     (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpv-program args)
214     (delete-file orig-file)
215     (if signature (delete-file signature))
216     (with-current-buffer pgg-errors-buffer
217       (goto-char (point-min))
218       (if (re-search-forward "^Good signature" nil t)
219           (progn
220             (set-buffer pgg-output-buffer)
221             (insert-buffer-substring pgg-errors-buffer)
222             t)
223         nil))))
224
225 (defun pgg-pgp5-insert-key ()
226   "Insert public key at point."
227   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
228          (args
229           (list "+verbose=1" "+batchmode=1" "+language=us" "-x"
230                 (concat "\"" pgg-pgp5-user-id "\""))))
231     (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpk-program args)
232     (insert-buffer-substring pgg-output-buffer)))
233
234 (defun pgg-pgp5-snarf-keys-region (start end)
235   "Add all public keys in region between START and END to the keyring."
236   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
237          (basename (expand-file-name "pgg" pgg-temporary-file-directory))
238          (key-file (make-temp-name basename))
239          (args
240           (list "+verbose=1" "+batchmode=1" "+language=us" "-a"
241                 key-file)))
242     (let ((coding-system-for-write 'raw-text-dos))
243       (write-region start end key-file))
244     (pgg-pgp5-process-region start end nil pgg-pgp5-pgpk-program args)
245     (delete-file key-file)
246     (pgg-process-when-success nil)))
247
248 (provide 'pgg-pgp5)
249
250 ;;; pgg-pgp5.el ends here