Moved pgg/*.el to lisp/ and pgg/*.texi to texi/.
[gnus] / lisp / pgg.el
1 ;;; pgg.el --- glue for the various PGP implementations.
2
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Created: 1999/10/28
7 ;; Keywords: PGP
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
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31
32 (require 'pgg-def)
33 (require 'pgg-parse)
34
35 (eval-when-compile
36   (ignore-errors
37     (require 'w3)
38     (require 'url)))
39
40 (defvar pgg-temporary-file-directory
41   (cond ((fboundp 'temp-directory) (temp-directory))
42         ((boundp 'temporary-file-directory) temporary-file-directory)
43         ("/tmp/")))
44
45 ;;; @ utility functions
46 ;;;
47
48 (defvar pgg-fetch-key-function (function pgg-fetch-key-with-w3))
49
50 (defun pgg-invoke (func scheme &rest args)
51   (progn
52     (require (intern (format "pgg-%s" scheme)))
53     (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
54
55 (put 'pgg-save-coding-system 'lisp-indent-function 2)
56
57 (defmacro pgg-save-coding-system (start end &rest body)
58   `(if (interactive-p)
59        (let ((buffer (current-buffer)))
60          (with-temp-buffer
61            (let (buffer-undo-list)
62              (insert-buffer-substring buffer ,start ,end)
63              (encode-coding-region (point-min)(point-max)
64                                    buffer-file-coding-system)
65              (prog1 (save-excursion ,@body)
66                (push nil buffer-undo-list)
67                (ignore-errors (undo))))))
68      (save-restriction
69        (narrow-to-region ,start ,end)
70        ,@body)))
71
72 (defun pgg-temp-buffer-show-function (buffer)
73   (let ((window (split-window-vertically)))
74     (set-window-buffer window buffer)
75     (shrink-window-if-larger-than-buffer window)))
76
77 (defun pgg-display-output-buffer (start end status)
78   (if status
79       (progn
80         (delete-region start end)
81         (insert-buffer-substring pgg-output-buffer)
82         (decode-coding-region start (point) buffer-file-coding-system))
83     (let ((temp-buffer-show-function
84            (function pgg-temp-buffer-show-function)))
85       (with-output-to-temp-buffer pgg-echo-buffer
86         (set-buffer standard-output)
87         (insert-buffer-substring pgg-errors-buffer)))))
88
89 (defvar pgg-passphrase-cache-expiry 16)
90 (defvar pgg-passphrase-cache (make-vector 7 0))
91
92 (defvar pgg-read-passphrase nil)
93 (defun pgg-read-passphrase (prompt &optional key)
94   (if (not pgg-read-passphrase)
95       (if (functionp 'read-passwd)
96           (setq pgg-read-passphrase 'read-passwd)
97         (if (load "passwd" t)
98             (setq pgg-read-passphrase 'read-passwd)
99           (autoload 'ange-ftp-read-passwd "ange-ftp")
100           (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
101   (or (and pgg-cache-passphrase
102            key (setq key (pgg-truncate-key-identifier key))
103            (symbol-value (intern-soft key pgg-passphrase-cache)))
104       (funcall pgg-read-passphrase prompt)))
105
106 (defun pgg-add-passphrase-cache (key passphrase)
107   (setq key (pgg-truncate-key-identifier key))
108   (set (intern key pgg-passphrase-cache)
109        passphrase)
110   (run-at-time pgg-passphrase-cache-expiry nil
111                #'pgg-remove-passphrase-cache
112                key))
113
114 (defun pgg-remove-passphrase-cache (key)
115   (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
116     (when passphrase
117       (fillarray passphrase ?_)
118       (unintern key pgg-passphrase-cache))))
119
120 (defmacro pgg-convert-lbt-region (start end lbt)
121   `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
122      (goto-char ,start)
123      (case ,lbt
124        (CRLF
125         (while (progn
126                  (end-of-line)
127                  (> (marker-position pgg-conversion-end) (point)))
128           (insert "\r")
129           (forward-line 1)))
130        (LF
131         (while (re-search-forward "\r$" pgg-conversion-end t)
132           (replace-match ""))))))
133
134 (put 'pgg-as-lbt 'lisp-indent-function 3)
135
136 (defmacro pgg-as-lbt (start end lbt &rest body)
137   `(let ((inhibit-read-only t)
138          buffer-read-only
139          buffer-undo-list)
140      (pgg-convert-lbt-region ,start ,end ,lbt)
141      (let ((,end (point)))
142        ,@body)
143      (push nil buffer-undo-list)
144      (ignore-errors (undo))))
145
146 (put 'pgg-process-when-success 'lisp-indent-function 0)
147
148 (defmacro pgg-process-when-success (&rest body)
149   `(with-current-buffer pgg-output-buffer
150      (if (zerop (buffer-size)) nil ,@body t)))
151
152 ;;; @ interface functions
153 ;;;
154
155 ;;;###autoload
156 (defun pgg-encrypt-region (start end rcpts)
157   "Encrypt the current region between START and END for RCPTS."
158   (interactive
159    (list (region-beginning)(region-end)
160          (split-string (read-string "Recipients: ") "[ \t,]+")))
161   (let ((status
162          (pgg-save-coding-system start end
163            (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
164                        (point-min) (point-max) rcpts))))
165     (when (interactive-p)
166       (pgg-display-output-buffer start end status))
167     status))
168
169 ;;;###autoload
170 (defun pgg-decrypt-region (start end)
171   "Decrypt the current region between START and END."
172   (interactive "r")
173   (let ((status
174          (pgg-save-coding-system start end
175            (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
176                        (point-min) (point-max)))))
177     (when (interactive-p)
178       (pgg-display-output-buffer start end status))
179     status))
180
181 ;;;###autoload
182 (defun pgg-sign-region (start end &optional cleartext)
183   "Make the signature from text between START and END.
184 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
185 a detached signature."
186   (interactive "r")
187   (let ((status (pgg-save-coding-system start end
188                   (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
189                               (point-min) (point-max)
190                               (or (interactive-p) cleartext)))))
191     (when (interactive-p)
192       (pgg-display-output-buffer start end status))
193     status))
194
195 ;;;###autoload
196 (defun pgg-verify-region (start end &optional signature fetch)
197   "Verify the current region between START and END.
198 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
199 the detached signature of the current region.
200
201 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
202 signer's public key from `pgg-default-keyserver-address'."
203   (interactive "r")
204   (let* ((packet
205           (if (null signature) nil
206             (with-temp-buffer
207               (buffer-disable-undo)
208               (if (fboundp 'set-buffer-multibyte)
209                   (set-buffer-multibyte nil))
210               (insert-file-contents signature)
211               (cdr (assq 2 (pgg-decode-armor-region
212                             (point-min)(point-max)))))))
213          (key (cdr (assq 'key-identifier packet)))
214          status keyserver)
215     (and (stringp key)
216          (setq key (concat "0x" (pgg-truncate-key-identifier key)))
217          (null (pgg-lookup-key key))
218          (or fetch (interactive-p))
219          (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
220          (setq keyserver
221                (or (cdr (assq 'preferred-key-server packet))
222                    pgg-default-keyserver-address))
223          (pgg-fetch-key keyserver key))
224     (setq status 
225           (pgg-save-coding-system start end
226             (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
227                         (point-min) (point-max) signature)))
228     (when (interactive-p)
229       (let ((temp-buffer-show-function
230              (function pgg-temp-buffer-show-function)))
231         (with-output-to-temp-buffer pgg-echo-buffer
232           (set-buffer standard-output)
233           (insert-buffer-substring (if status pgg-output-buffer
234                                      pgg-errors-buffer)))))
235     status))
236
237 ;;;###autoload
238 (defun pgg-insert-key ()
239   "Insert the ASCII armored public key."
240   (interactive)
241   (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
242
243 ;;;###autoload
244 (defun pgg-snarf-keys-region (start end)
245   "Import public keys in the current region between START and END."
246   (interactive "r")
247   (pgg-save-coding-system start end
248     (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
249                 start end)))
250
251 (defun pgg-lookup-key (string &optional type)
252   (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
253
254 (defvar pgg-insert-url-function  (function pgg-insert-url-with-w3))
255
256 (defun pgg-insert-url-with-w3 (url)
257   (require 'w3)
258   (require 'url)
259   (let (buffer-file-name)
260     (url-insert-file-contents url)))
261
262 (defvar pgg-insert-url-extra-arguments nil)
263 (defvar pgg-insert-url-program nil)
264
265 (defun pgg-insert-url-with-program (url)
266   (let ((args (copy-sequence pgg-insert-url-extra-arguments))
267         process)
268     (insert
269      (with-temp-buffer
270        (setq process
271              (apply #'start-process " *PGG url*" (current-buffer)
272                     pgg-insert-url-program (nconc args (list url))))
273        (set-process-sentinel process #'ignore)
274        (while (eq 'run (process-status process))
275          (accept-process-output process 5))
276        (delete-process process)
277        (if (and process (eq 'run (process-status process)))
278            (interrupt-process process))
279        (buffer-string)))))
280
281 (defun pgg-fetch-key (keyserver key)
282   "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
283   (with-current-buffer (get-buffer-create pgg-output-buffer)
284     (buffer-disable-undo)
285     (erase-buffer)
286     (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
287                      (substring keyserver 0 (1- (match-end 0))))))
288       (save-excursion
289         (funcall pgg-insert-url-function
290                  (if proto keyserver
291                    (format "http://%s:11371/pks/lookup?op=get&search=%s"
292                            keyserver key))))
293       (when (re-search-forward "^-+BEGIN" nil 'last)
294         (delete-region (point-min) (match-beginning 0))
295         (when (re-search-forward "^-+END" nil t)
296           (delete-region (progn (end-of-line) (point))
297                          (point-max)))
298         (insert "\n")
299         (with-temp-buffer
300           (insert-buffer-substring pgg-output-buffer)
301           (pgg-snarf-keys-region (point-min)(point-max)))))))
302
303
304 (provide 'pgg)
305
306 ;;; pgg.el ends here