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