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