Fix bug#7306; customization of minor-mode variables defined in C.
[emacs.git] / lisp / pgg.el
blobf654f3dc35e658f690430edd991e5d5a6294713f
1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Symmetric encryption added by: Sascha Wilde <wilde@sha-bang.de>
8 ;; Created: 1999/10/28
9 ;; Keywords: PGP
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This file is on its way to obsolescence, waiting for allout.el to
29 ;; switch to EPG.
31 ;;; Code:
33 (require 'pgg-def)
34 (require 'pgg-parse)
35 (autoload 'run-at-time "timer")
37 ;; Don't merge these two `eval-when-compile's.
38 (eval-when-compile
39 ;; For Emacs <22.2 and XEmacs.
40 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
41 (require 'cl))
43 ;;; @ utility functions
44 ;;;
46 (eval-when-compile
47 (when (featurep 'xemacs)
48 (defmacro pgg-run-at-time-1 (time repeat function args)
49 (if (condition-case nil
50 (let ((delete-itimer 'delete-itimer)
51 (itimer-driver-start 'itimer-driver-start)
52 (itimer-value 'itimer-value)
53 (start-itimer 'start-itimer))
54 (unless (or (symbol-value 'itimer-process)
55 (symbol-value 'itimer-timer))
56 (funcall itimer-driver-start))
57 ;; Check whether there is a bug to which the difference of
58 ;; the present time and the time when the itimer driver was
59 ;; woken up is subtracted from the initial itimer value.
60 (let* ((inhibit-quit t)
61 (ctime (current-time))
62 (itimer-timer-last-wakeup
63 (prog1
64 ctime
65 (setcar ctime (1- (car ctime)))))
66 (itimer-list nil)
67 (itimer (funcall start-itimer "pgg-run-at-time"
68 'ignore 5)))
69 (sleep-for 0.1) ;; Accept the timeout interrupt.
70 (prog1
71 (> (funcall itimer-value itimer) 0)
72 (funcall delete-itimer itimer))))
73 (error nil))
74 `(let ((time ,time))
75 (apply #'start-itimer "pgg-run-at-time"
76 ,function (if time (max time 1e-9) 1e-9)
77 ,repeat nil t ,args))
78 `(let ((time ,time)
79 (itimers (list nil)))
80 (setcar
81 itimers
82 (apply #'start-itimer "pgg-run-at-time"
83 (lambda (itimers repeat function &rest args)
84 (let ((itimer (car itimers)))
85 (if repeat
86 (progn
87 (set-itimer-function
88 itimer
89 (lambda (itimer repeat function &rest args)
90 (set-itimer-restart itimer repeat)
91 (set-itimer-function itimer function)
92 (set-itimer-function-arguments itimer args)
93 (apply function args)))
94 (set-itimer-function-arguments
95 itimer
96 (append (list itimer repeat function) args)))
97 (set-itimer-function
98 itimer
99 (lambda (itimer function &rest args)
100 (delete-itimer itimer)
101 (apply function args)))
102 (set-itimer-function-arguments
103 itimer
104 (append (list itimer function) args)))))
105 1e-9 (if time (max time 1e-9) 1e-9)
106 nil t itimers ,repeat ,function ,args)))))))
108 (eval-and-compile
109 (if (featurep 'xemacs)
110 (progn
111 (defun pgg-run-at-time (time repeat function &rest args)
112 "Emulating function run as `run-at-time'.
113 TIME should be nil meaning now, or a number of seconds from now.
114 Return an itimer object which can be used in either `delete-itimer'
115 or `cancel-timer'."
116 (pgg-run-at-time-1 time repeat function args))
117 (defun pgg-cancel-timer (timer)
118 "Emulate cancel-timer for xemacs."
119 (let ((delete-itimer 'delete-itimer))
120 (funcall delete-itimer timer))))
121 (defalias 'pgg-run-at-time 'run-at-time)
122 (defalias 'pgg-cancel-timer 'cancel-timer)))
124 (defun pgg-invoke (func scheme &rest args)
125 (progn
126 (require (intern (format "pgg-%s" scheme)))
127 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
129 (put 'pgg-save-coding-system 'lisp-indent-function 2)
131 (defmacro pgg-save-coding-system (start end &rest body)
132 `(if (called-interactively-p 'interactive)
133 (let ((buffer (current-buffer)))
134 (with-temp-buffer
135 (let (buffer-undo-list)
136 (insert-buffer-substring buffer ,start ,end)
137 (encode-coding-region (point-min)(point-max)
138 buffer-file-coding-system)
139 (prog1 (save-excursion ,@body)
140 (push nil buffer-undo-list)
141 (ignore-errors (undo))))))
142 (save-restriction
143 (narrow-to-region ,start ,end)
144 ,@body)))
146 (defun pgg-temp-buffer-show-function (buffer)
147 (let ((window (or (get-buffer-window buffer 'visible)
148 (split-window-vertically))))
149 (set-window-buffer window buffer)
150 (shrink-window-if-larger-than-buffer window)))
152 ;; XXX `pgg-display-output-buffer' is a horrible name for this function.
153 ;; It should be something like `pgg-situate-output-or-display-error'.
154 (defun pgg-display-output-buffer (start end status)
155 "Situate en/decryption results or pop up an error buffer.
157 Text from START to END is replaced by contents of output buffer if STATUS
158 is true, or else the output buffer is displayed."
159 (if status
160 (pgg-situate-output start end)
161 (pgg-display-error-buffer)))
163 (defun pgg-situate-output (start end)
164 "Place en/decryption result in place of current text from START to END."
165 (delete-region start end)
166 (insert-buffer-substring pgg-output-buffer)
167 (decode-coding-region start (point) buffer-file-coding-system))
169 (defun pgg-display-error-buffer ()
170 "Pop up an error buffer indicating the reason for an en/decryption failure."
171 (let ((temp-buffer-show-function
172 (function pgg-temp-buffer-show-function)))
173 (with-output-to-temp-buffer pgg-echo-buffer
174 (set-buffer standard-output)
175 (insert-buffer-substring pgg-errors-buffer))))
177 (defvar pgg-passphrase-cache (make-vector 7 0))
179 (defvar pgg-pending-timers (make-vector 7 0)
180 "Hash table for managing scheduled pgg cache management timers.
182 We associate key and timer, so the timer can be cancelled if a new
183 timeout for the key is set while an old one is still pending.")
185 (defun pgg-read-passphrase (prompt &optional key notruncate)
186 "Using PROMPT, obtain passphrase for KEY from cache or user.
188 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
189 \(default false).
191 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
192 regulate cache behavior."
193 (or (pgg-read-passphrase-from-cache key notruncate)
194 (read-passwd prompt)))
196 (defun pgg-read-passphrase-from-cache (key &optional notruncate)
197 "Obtain passphrase for KEY from time-limited passphrase cache.
199 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
200 \(default false).
202 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
203 regulate cache behavior."
204 (and pgg-cache-passphrase
205 key (or notruncate
206 (setq key (pgg-truncate-key-identifier key)))
207 (symbol-value (intern-soft key pgg-passphrase-cache))))
209 (defun pgg-add-passphrase-to-cache (key passphrase &optional notruncate)
210 "Associate KEY with PASSPHRASE in time-limited passphrase cache.
212 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
213 \(default false).
215 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
216 regulate cache behavior."
218 (let* ((key (if notruncate key (pgg-truncate-key-identifier key)))
219 (interned-timer-key (intern-soft key pgg-pending-timers))
220 (old-timer (symbol-value interned-timer-key))
221 new-timer)
222 (when old-timer
223 (cancel-timer old-timer)
224 (unintern interned-timer-key pgg-pending-timers))
225 (set (intern key pgg-passphrase-cache)
226 passphrase)
227 (set (intern key pgg-pending-timers)
228 (pgg-run-at-time pgg-passphrase-cache-expiry nil
229 #'pgg-remove-passphrase-from-cache
230 key notruncate))))
232 (if (fboundp 'clear-string)
233 (defalias 'pgg-clear-string 'clear-string)
234 (defun pgg-clear-string (string)
235 (fillarray string ?_)))
237 (declare-function pgg-clear-string "pgg" (string))
239 (defun pgg-remove-passphrase-from-cache (key &optional notruncate)
240 "Omit passphrase associated with KEY in time-limited passphrase cache.
242 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
243 \(default false).
245 This is a no-op if there is not entry for KEY (eg, it's already expired.
247 The memory for the passphrase is filled with underscores to clear any
248 references to it.
250 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
251 regulate cache behavior."
252 (let* ((passphrase (pgg-read-passphrase-from-cache key notruncate))
253 (key (if notruncate key (pgg-truncate-key-identifier key)))
254 (interned-timer-key (intern-soft key pgg-pending-timers))
255 (old-timer (symbol-value interned-timer-key)))
256 (when passphrase
257 (pgg-clear-string passphrase)
258 (unintern key pgg-passphrase-cache))
259 (when old-timer
260 (pgg-cancel-timer old-timer)
261 (unintern interned-timer-key pgg-pending-timers))))
263 (defmacro pgg-convert-lbt-region (start end lbt)
264 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
265 (goto-char ,start)
266 (case ,lbt
267 (CRLF
268 (while (progn
269 (end-of-line)
270 (> (marker-position pgg-conversion-end) (point)))
271 (insert "\r")
272 (forward-line 1)))
274 (while (re-search-forward "\r$" pgg-conversion-end t)
275 (replace-match ""))))))
277 (put 'pgg-as-lbt 'lisp-indent-function 3)
279 (defmacro pgg-as-lbt (start end lbt &rest body)
280 `(let ((inhibit-read-only t)
281 buffer-read-only
282 buffer-undo-list)
283 (pgg-convert-lbt-region ,start ,end ,lbt)
284 (let ((,end (point)))
285 ,@body)
286 (push nil buffer-undo-list)
287 (ignore-errors (undo))))
289 (put 'pgg-process-when-success 'lisp-indent-function 0)
291 (defmacro pgg-process-when-success (&rest body)
292 `(with-current-buffer pgg-output-buffer
293 (if (zerop (buffer-size)) nil ,@body t)))
295 (defalias 'pgg-make-temp-file
296 (if (fboundp 'make-temp-file)
297 'make-temp-file
298 (lambda (prefix &optional dir-flag)
299 (let ((file (expand-file-name
300 (make-temp-name prefix)
301 (if (fboundp 'temp-directory)
302 (temp-directory)
303 temporary-file-directory))))
304 (if dir-flag
305 (make-directory file))
306 file))))
308 ;;; @ interface functions
311 ;;;###autoload
312 (defun pgg-encrypt-region (start end rcpts &optional sign passphrase)
313 "Encrypt the current region between START and END for RCPTS.
315 If optional argument SIGN is non-nil, do a combined sign and encrypt.
317 If optional PASSPHRASE is not specified, it will be obtained from the
318 passphrase cache or user."
319 (interactive
320 (list (region-beginning)(region-end)
321 (split-string (read-string "Recipients: ") "[ \t,]+")))
322 (let ((status
323 (pgg-save-coding-system start end
324 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
325 (point-min) (point-max) rcpts sign passphrase))))
326 (when (called-interactively-p 'interactive)
327 (pgg-display-output-buffer start end status))
328 status))
330 ;;;###autoload
331 (defun pgg-encrypt-symmetric-region (start end &optional passphrase)
332 "Encrypt the current region between START and END symmetric with passphrase.
334 If optional PASSPHRASE is not specified, it will be obtained from the
335 cache or user."
336 (interactive "r")
337 (let ((status
338 (pgg-save-coding-system start end
339 (pgg-invoke "encrypt-symmetric-region"
340 (or pgg-scheme pgg-default-scheme)
341 (point-min) (point-max) passphrase))))
342 (when (called-interactively-p 'interactive)
343 (pgg-display-output-buffer start end status))
344 status))
346 ;;;###autoload
347 (defun pgg-encrypt-symmetric (&optional start end passphrase)
348 "Encrypt the current buffer using a symmetric, rather than key-pair, cipher.
350 If optional arguments START and END are specified, only encrypt within
351 the region.
353 If optional PASSPHRASE is not specified, it will be obtained from the
354 passphrase cache or user."
355 (interactive)
356 (let* ((start (or start (point-min)))
357 (end (or end (point-max)))
358 (status (pgg-encrypt-symmetric-region start end passphrase)))
359 (when (called-interactively-p 'interactive)
360 (pgg-display-output-buffer start end status))
361 status))
363 ;;;###autoload
364 (defun pgg-encrypt (rcpts &optional sign start end passphrase)
365 "Encrypt the current buffer for RCPTS.
367 If optional argument SIGN is non-nil, do a combined sign and encrypt.
369 If optional arguments START and END are specified, only encrypt within
370 the region.
372 If optional PASSPHRASE is not specified, it will be obtained from the
373 passphrase cache or user."
374 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
375 (let* ((start (or start (point-min)))
376 (end (or end (point-max)))
377 (status (pgg-encrypt-region start end rcpts sign passphrase)))
378 (when (called-interactively-p 'interactive)
379 (pgg-display-output-buffer start end status))
380 status))
382 ;;;###autoload
383 (defun pgg-decrypt-region (start end &optional passphrase)
384 "Decrypt the current region between START and END.
386 If optional PASSPHRASE is not specified, it will be obtained from the
387 passphrase cache or user."
388 (interactive "r")
389 (let* ((buf (current-buffer))
390 (status
391 (pgg-save-coding-system start end
392 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
393 (point-min) (point-max) passphrase))))
394 (when (called-interactively-p 'interactive)
395 (pgg-display-output-buffer start end status))
396 status))
398 ;;;###autoload
399 (defun pgg-decrypt (&optional start end passphrase)
400 "Decrypt the current buffer.
402 If optional arguments START and END are specified, only decrypt within
403 the region.
405 If optional PASSPHRASE is not specified, it will be obtained from the
406 passphrase cache or user."
407 (interactive "")
408 (let* ((start (or start (point-min)))
409 (end (or end (point-max)))
410 (status (pgg-decrypt-region start end passphrase)))
411 (when (called-interactively-p 'interactive)
412 (pgg-display-output-buffer start end status))
413 status))
415 ;;;###autoload
416 (defun pgg-sign-region (start end &optional cleartext passphrase)
417 "Make the signature from text between START and END.
419 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
420 a detached signature.
422 If this function is called interactively, CLEARTEXT is enabled
423 and the output is displayed.
425 If optional PASSPHRASE is not specified, it will be obtained from the
426 passphrase cache or user."
427 (interactive "r")
428 (let ((status (pgg-save-coding-system start end
429 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
430 (point-min) (point-max)
431 (or (called-interactively-p 'interactive)
432 cleartext)
433 passphrase))))
434 (when (called-interactively-p 'interactive)
435 (pgg-display-output-buffer start end status))
436 status))
438 ;;;###autoload
439 (defun pgg-sign (&optional cleartext start end passphrase)
440 "Sign the current buffer.
442 If the optional argument CLEARTEXT is non-nil, it does not create a
443 detached signature.
445 If optional arguments START and END are specified, only sign data
446 within the region.
448 If this function is called interactively, CLEARTEXT is enabled
449 and the output is displayed.
451 If optional PASSPHRASE is not specified, it will be obtained from the
452 passphrase cache or user."
453 (interactive "")
454 (let* ((start (or start (point-min)))
455 (end (or end (point-max)))
456 (status (pgg-sign-region start end
457 (or (called-interactively-p 'interactive)
458 cleartext)
459 passphrase)))
460 (when (called-interactively-p 'interactive)
461 (pgg-display-output-buffer start end status))
462 status))
464 ;;;###autoload
465 (defun pgg-verify-region (start end &optional signature fetch)
466 "Verify the current region between START and END.
467 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
468 the detached signature of the current region.
470 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
471 signer's public key from `pgg-default-keyserver-address'."
472 (interactive "r")
473 (let* ((packet
474 (if (null signature) nil
475 (with-temp-buffer
476 (buffer-disable-undo)
477 (unless (featurep 'xemacs)
478 (set-buffer-multibyte nil))
479 (insert-file-contents signature)
480 (cdr (assq 2 (pgg-decode-armor-region
481 (point-min)(point-max)))))))
482 (key (cdr (assq 'key-identifier packet)))
483 status keyserver)
484 (and (stringp key)
485 pgg-query-keyserver
486 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
487 (null (pgg-lookup-key key))
488 (or fetch (called-interactively-p 'interactive))
489 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
490 (setq keyserver
491 (or (cdr (assq 'preferred-key-server packet))
492 pgg-default-keyserver-address))
493 (pgg-fetch-key keyserver key))
494 (setq status
495 (pgg-save-coding-system start end
496 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
497 (point-min) (point-max) signature)))
498 (when (called-interactively-p 'interactive)
499 (let ((temp-buffer-show-function
500 (function pgg-temp-buffer-show-function)))
501 (with-output-to-temp-buffer pgg-echo-buffer
502 (set-buffer standard-output)
503 (insert-buffer-substring (if status pgg-output-buffer
504 pgg-errors-buffer)))))
505 status))
507 ;;;###autoload
508 (defun pgg-verify (&optional signature fetch start end)
509 "Verify the current buffer.
510 If the optional argument SIGNATURE is non-nil, it is treated as
511 the detached signature of the current region.
512 If the optional argument FETCH is non-nil, we attempt to fetch the
513 signer's public key from `pgg-default-keyserver-address'.
514 If optional arguments START and END are specified, only verify data
515 within the region."
516 (interactive "")
517 (let* ((start (or start (point-min)))
518 (end (or end (point-max)))
519 (status (pgg-verify-region start end signature fetch)))
520 (when (called-interactively-p 'interactive)
521 (let ((temp-buffer-show-function
522 (function pgg-temp-buffer-show-function)))
523 (with-output-to-temp-buffer pgg-echo-buffer
524 (set-buffer standard-output)
525 (insert-buffer-substring (if status pgg-output-buffer
526 pgg-errors-buffer)))))
527 status))
529 ;;;###autoload
530 (defun pgg-insert-key ()
531 "Insert the ASCII armored public key."
532 (interactive)
533 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
535 ;;;###autoload
536 (defun pgg-snarf-keys-region (start end)
537 "Import public keys in the current region between START and END."
538 (interactive "r")
539 (pgg-save-coding-system start end
540 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
541 start end)))
543 ;;;###autoload
544 (defun pgg-snarf-keys ()
545 "Import public keys in the current buffer."
546 (interactive "")
547 (pgg-snarf-keys-region (point-min) (point-max)))
549 (defun pgg-lookup-key (string &optional type)
550 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
552 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
554 (defun pgg-insert-url-with-w3 (url)
555 (ignore-errors
556 (require 'url)
557 (let (buffer-file-name)
558 (url-insert-file-contents url))))
560 (defvar pgg-insert-url-extra-arguments nil)
561 (defvar pgg-insert-url-program nil)
563 (defun pgg-insert-url-with-program (url)
564 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
565 process)
566 (insert
567 (with-temp-buffer
568 (setq process
569 (apply #'start-process " *PGG url*" (current-buffer)
570 pgg-insert-url-program (nconc args (list url))))
571 (set-process-sentinel process #'ignore)
572 (while (eq 'run (process-status process))
573 (accept-process-output process 5))
574 (delete-process process)
575 (if (and process (eq 'run (process-status process)))
576 (interrupt-process process))
577 (buffer-string)))))
579 (defun pgg-fetch-key (keyserver key)
580 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
581 (with-current-buffer (get-buffer-create pgg-output-buffer)
582 (buffer-disable-undo)
583 (erase-buffer)
584 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
585 (substring keyserver 0 (1- (match-end 0))))))
586 (save-excursion
587 (funcall pgg-insert-url-function
588 (if proto keyserver
589 (format "http://%s:11371/pks/lookup?op=get&search=%s"
590 keyserver key))))
591 (when (re-search-forward "^-+BEGIN" nil 'last)
592 (delete-region (point-min) (match-beginning 0))
593 (when (re-search-forward "^-+END" nil t)
594 (delete-region (progn (end-of-line) (point))
595 (point-max)))
596 (insert "\n")
597 (with-temp-buffer
598 (insert-buffer-substring pgg-output-buffer)
599 (pgg-snarf-keys-region (point-min)(point-max)))))))
602 (provide 'pgg)
604 ;;; pgg.el ends here