]> code.delx.au - gnu-emacs/blob - lisp/epg-config.el
-
[gnu-emacs] / lisp / epg-config.el
1 ;;; epg-config.el --- configuration of the EasyPG Library
2
3 ;; Copyright (C) 2006-2016 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: PGP, GnuPG
7 ;; Package: epg
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Code:
25
26 (defconst epg-package-name "epg"
27 "Name of this package.")
28
29 (defconst epg-version-number "1.0.0"
30 "Version number of this package.")
31
32 (defconst epg-bug-report-address "ueno@unixuser.org"
33 "Report bugs to this address.")
34
35 (defgroup epg ()
36 "Interface to the GNU Privacy Guard (GnuPG)."
37 :tag "EasyPG"
38 :version "23.1"
39 :group 'data
40 :group 'external)
41
42 (defcustom epg-gpg-program (cond ((executable-find "gpg") "gpg")
43 ((executable-find "gpg2") "gpg2")
44 (t "gpg"))
45 "The `gpg' executable."
46 :group 'epg
47 :type 'string)
48
49 (defcustom epg-gpgsm-program "gpgsm"
50 "The `gpgsm' executable."
51 :group 'epg
52 :type 'string)
53
54 (defcustom epg-gpgconf-program "gpgconf"
55 "The `gpgconf' executable."
56 :version "25.1"
57 :group 'epg
58 :type 'string)
59
60 (defcustom epg-gpg-home-directory nil
61 "The directory which contains the configuration files of `epg-gpg-program'."
62 :group 'epg
63 :type '(choice (const :tag "Default" nil) directory))
64
65 (defcustom epg-passphrase-coding-system nil
66 "Coding system to use with messages from `epg-gpg-program'."
67 :group 'epg
68 :type 'symbol)
69
70 (defcustom epg-debug nil
71 "If non-nil, debug output goes to the \" *epg-debug*\" buffer.
72 Note that the buffer name starts with a space."
73 :group 'epg
74 :type 'boolean)
75
76 (defconst epg-gpg-minimum-version "1.4.3")
77
78 ;;;###autoload
79 (defun epg-configuration ()
80 "Return a list of internal configuration parameters of `epg-gpg-program'."
81 (let (config groups type args)
82 (with-temp-buffer
83 (apply #'call-process epg-gpg-program nil (list t nil) nil
84 (append (if epg-gpg-home-directory
85 (list "--homedir" epg-gpg-home-directory))
86 '("--with-colons" "--list-config")))
87 (goto-char (point-min))
88 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
89 (setq type (intern (match-string 1))
90 args (match-string 2))
91 (cond
92 ((eq type 'group)
93 (if (string-match "\\`\\([^:]+\\):" args)
94 (setq groups
95 (cons (cons (downcase (match-string 1 args))
96 (delete "" (split-string
97 (substring args
98 (match-end 0))
99 ";")))
100 groups))
101 (if epg-debug
102 (message "Invalid group configuration: %S" args))))
103 ((memq type '(pubkey cipher digest compress))
104 (if (string-match "\\`\\([0-9]+\\)\\(;[0-9]+\\)*" args)
105 (setq config
106 (cons (cons type
107 (mapcar #'string-to-number
108 (delete "" (split-string args ";"))))
109 config))
110 (if epg-debug
111 (message "Invalid %S algorithm configuration: %S"
112 type args))))
113 (t
114 (setq config (cons (cons type args) config))))))
115 (if groups
116 (cons (cons 'groups groups) config)
117 config)))
118
119 (defun epg-config--parse-version (string)
120 (let ((index 0)
121 version)
122 (while (eq index (string-match "\\([0-9]+\\)\\.?" string index))
123 (setq version (cons (string-to-number (match-string 1 string))
124 version)
125 index (match-end 0)))
126 (nreverse version)))
127
128 (defun epg-config--compare-version (v1 v2)
129 (while (and v1 v2 (= (car v1) (car v2)))
130 (setq v1 (cdr v1) v2 (cdr v2)))
131 (- (or (car v1) 0) (or (car v2) 0)))
132
133 ;;;###autoload
134 (defun epg-check-configuration (config &optional minimum-version)
135 "Verify that a sufficient version of GnuPG is installed."
136 (let ((entry (assq 'version config))
137 version)
138 (unless (and entry
139 (stringp (cdr entry)))
140 (error "Undetermined version: %S" entry))
141 (setq version (epg-config--parse-version (cdr entry))
142 minimum-version (epg-config--parse-version
143 (or minimum-version
144 epg-gpg-minimum-version)))
145 (unless (>= (epg-config--compare-version version minimum-version) 0)
146 (error "Unsupported version: %s" (cdr entry)))))
147
148 ;;;###autoload
149 (defun epg-expand-group (config group)
150 "Look at CONFIG and try to expand GROUP."
151 (let ((entry (assq 'groups config)))
152 (if (and entry
153 (setq entry (assoc (downcase group) (cdr entry))))
154 (cdr entry))))
155
156 (provide 'epg-config)
157
158 ;;; epg-config.el ends here