1 ;;; pgg-pgp5.el --- PGP 5.* support for PGG.
3 ;; Copyright (C) 1999 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Keywords: PGP, OpenPGP
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
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.
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.
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.
28 (eval-when-compile (require 'pgg))
34 (defcustom pgg-pgp5-pgpe-program "pgpe"
35 "PGP 5.* 'pgpe' executable."
39 (defcustom pgg-pgp5-pgps-program "pgps"
40 "PGP 5.* 'pgps' executable."
44 (defcustom pgg-pgp5-pgpk-program "pgpk"
45 "PGP 5.* 'pgpk' executable."
49 (defcustom pgg-pgp5-pgpv-program "pgpv"
50 "PGP 5.* 'pgpv' executable."
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>\"."
60 (defcustom pgg-pgp5-shell-command-switch "-c"
61 "Switch used to have the shell execute its command line argument."
65 (defcustom pgg-pgp5-extra-args nil
66 "Extra arguments for every PGP invocation."
71 (luna-define-class pgg-scheme-pgp5 (pgg-scheme))
74 (defvar pgg-pgp5-user-id nil
75 "GnuPG ID of your default identity.")
77 (defvar pgg-scheme-pgp5-instance nil)
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))))
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")))
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 (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)
103 (setenv "PGPPASSFD" "0"))
106 (apply #'start-process-shell-command "*PGP*" output-buffer
108 (set-process-sentinel process 'ignore)
110 (process-send-string process (concat passphrase "\n")))
111 (process-send-region process start end)
112 (process-send-eof process)
113 (while (eq 'run (process-status process))
114 (accept-process-output process 5))
115 (setq status (process-status process)
116 exit-status (process-exit-status process))
117 (delete-process process)
118 (with-current-buffer output-buffer
119 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
121 (if (memq status '(stop signal))
122 (error "%s exited abnormally: '%s'" program exit-status))
123 (if (= 127 exit-status)
124 (error "%s could not be found" program))
126 (set-buffer (get-buffer-create errors-buffer))
127 (buffer-disable-undo)
129 (insert-file-contents errors-file-name)
130 (delete-file errors-file-name)
132 (if (and process (eq 'run (process-status process)))
133 (interrupt-process process))
137 (luna-define-method lookup-key-string ((scheme pgg-scheme-pgp5)
138 string &optional type)
139 (let ((args (list "+language=en" "-l" string)))
140 (with-current-buffer (get-buffer-create pgg-output-buffer)
141 (buffer-disable-undo)
143 (apply #'call-process pgg-pgp5-pgpk-program nil t args)
144 (goto-char (point-min))
145 (when (re-search-forward "^sec" nil t)
148 (buffer-substring (match-end 0)
149 (progn (end-of-line)(point)))
154 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp5)
155 start end recipients)
156 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
158 `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
161 (mapcar (lambda (rcpt)
163 (concat "\"" rcpt "\"")))
165 (if pgg-encrypt-for-me
166 (list pgg-pgp5-user-id)))))))
168 (pgg-pgp5-process-region start end nil
169 pgg-pgp5-pgpe-program args)
170 (pgg-process-when-success nil)
173 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp5)
175 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
178 (format "PGP passphrase for %s: " pgg-pgp5-user-id)
179 (luna-send scheme 'lookup-key-string
180 scheme pgg-pgp5-user-id 'encrypt)))
182 '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
183 (pgg-pgp5-process-region start end passphrase
184 pgg-pgp5-pgpv-program args)
185 (pgg-process-when-success nil)
188 (luna-define-method sign-region ((scheme pgg-scheme-pgp5)
189 start end &optional clearsign)
190 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
193 (format "PGP passphrase for %s: " pgg-pgp5-user-id)
194 (luna-send scheme 'lookup-key-string
195 scheme pgg-pgp5-user-id 'sign)))
197 (list (if clearsign "-fat" "-fbat")
198 "+verbose=1" "+language=us" "+batchmode=1"
199 "-u" pgg-pgp5-user-id)))
200 (pgg-pgp5-process-region start end passphrase
201 pgg-pgp5-pgps-program args)
202 (pgg-process-when-success
203 (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
205 (cdr (assq 2 (pgg-parse-armor-region
206 (progn (beginning-of-line 2)
209 (if pgg-cache-passphrase
210 (pgg-add-passphrase-cache
211 (cdr (assq 'key-identifier packet))
215 (luna-define-method verify-region ((scheme pgg-scheme-pgp5)
216 start end &optional signature)
217 (let* ((basename (expand-file-name "pgg" temporary-file-directory))
218 (orig-file (make-temp-name basename))
219 (args '("+verbose=1" "+batchmode=1" "+language=us"))
220 (orig-mode (default-file-modes)))
223 (set-default-file-modes 448)
224 (write-region-as-binary start end orig-file)
226 (set-default-file-modes orig-mode))
227 (when (stringp signature)
228 (copy-file signature (setq signature (concat orig-file ".asc")))
229 (setq args (append args (list signature)))
231 (pgg-pgp5-process-region (point-min)(point-max) nil
232 pgg-pgp5-pgpv-program args)
233 (delete-file orig-file)
234 (if signature (delete-file signature))
235 (pgg-process-when-success nil)
238 (luna-define-method insert-key ((scheme pgg-scheme-pgp5))
239 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
241 (list "+verbose=1" "+batchmode=1" "+language=us" "-x"
242 (concat "\"" pgg-pgp5-user-id "\""))))
243 (pgg-pgp5-process-region (point)(point) nil
244 pgg-pgp5-pgpk-program args)
245 (insert-buffer-substring pgg-output-buffer)
248 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp5)
250 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
251 (basename (expand-file-name "pgg" temporary-file-directory))
252 (key-file (make-temp-name basename))
254 (list "+verbose=1" "+batchmode=1" "+language=us" "-a"
256 (write-region-as-raw-text-CRLF start end key-file)
257 (pgg-pgp5-process-region start end nil
258 pgg-pgp5-pgpk-program args)
259 (delete-file key-file)
260 (pgg-process-when-success nil)
265 ;;; pgg-pgp5.el ends here