1 ;;; epg-config.el --- configuration of the EasyPG Library
3 ;; Copyright (C) 2006-2018 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: PGP, GnuPG
9 ;; This file is part of GNU Emacs.
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.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 (eval-when-compile (require 'cl-lib
))
28 (defconst epg-package-name
"epg"
29 "Name of this package.")
31 (defconst epg-version-number
"1.0.0"
32 "Version number of this package.")
34 (defconst epg-bug-report-address
"ueno@unixuser.org"
35 "Report bugs to this address.")
38 "Interface to the GNU Privacy Guard (GnuPG)."
44 (defcustom epg-gpg-program
(if (executable-find "gpg2")
47 "The `gpg' executable.
48 Setting this variable directly does not take effect;
49 instead use \\[customize] (see the info node `Easy Customization')."
53 (defcustom epg-gpgsm-program
"gpgsm"
54 "The `gpgsm' executable.
55 Setting this variable directly does not take effect;
56 instead use \\[customize] (see the info node `Easy Customization')."
59 (defcustom epg-gpgconf-program
"gpgconf"
60 "The `gpgconf' executable."
64 (defcustom epg-gpg-home-directory nil
65 "The directory which contains the configuration files of `epg-gpg-program'."
66 :type
'(choice (const :tag
"Default" nil
) directory
))
68 (defcustom epg-passphrase-coding-system nil
69 "Coding system to use with messages from `epg-gpg-program'."
72 (define-obsolete-variable-alias
73 'epa-pinentry-mode
'epg-pinentry-mode
"27.1")
75 ;; In the doc string below, we say "symbol `error'" to avoid producing
76 ;; a hyperlink for `error' the function.
77 (defcustom epg-pinentry-mode nil
80 GnuPG 2.1 or later has an option to control the behavior of
81 Pinentry invocation. The value should be the symbol `error',
82 `ask', `cancel', or `loopback'. See the GnuPG manual for the
85 A particularly useful mode is `loopback', which redirects all
86 Pinentry queries to the caller, so Emacs can query passphrase
87 through the minibuffer, instead of external Pinentry program."
88 :type
'(choice (const nil
)
95 (defcustom epg-debug nil
96 "If non-nil, debug output goes to the \" *epg-debug*\" buffer.
97 Note that the buffer name starts with a space."
100 (defconst epg-gpg-minimum-version
"1.4.3")
101 (defconst epg-gpg2-minimum-version
"2.1.6")
103 (defconst epg-config--program-alist
106 ("gpg2" .
,epg-gpg2-minimum-version
)
107 ("gpg" .
((,epg-gpg-minimum-version .
"2.0")
108 ,epg-gpg2-minimum-version
)))
111 ("gpgsm" .
"2.0.4")))
112 "Alist used to obtain the usable configuration of executables.
113 The first element of each entry is protocol symbol, which is
114 either `OpenPGP' or `CMS'. The second element is a symbol where
115 the executable name is remembered. The rest of the entry is an
116 alist mapping executable names to the minimum required version
117 suitable for the use with Emacs.")
119 (defconst epg-config--configuration-constructor-alist
120 '((OpenPGP . epg-config--make-gpg-configuration
)
121 (CMS . epg-config--make-gpgsm-configuration
))
122 "Alist used to obtain the usable configuration of executables.
123 The first element of each entry is protocol symbol, which is
124 either `OpenPGP' or `CMS'. The second element is a function
125 which constructs a configuration object (actually a plist).")
127 (defvar epg--configurations nil
)
130 (defun epg-find-configuration (protocol &optional no-cache program-alist
)
131 "Find or create a usable configuration to handle PROTOCOL.
132 This function first looks at the existing configuration found by
133 the previous invocation of this function, unless NO-CACHE is non-nil.
135 Then it walks through PROGRAM-ALIST or
136 `epg-config--program-alist'. If `epg-gpg-program' or
137 `epg-gpgsm-program' is already set with custom, use it.
138 Otherwise, it tries the programs listed in the entry until the
139 version requirement is met."
140 (unless program-alist
141 (setq program-alist epg-config--program-alist
))
142 (let ((entry (assq protocol program-alist
)))
144 (error "Unknown protocol %S" protocol
))
145 (cl-destructuring-bind (symbol . alist
)
148 (alist-get protocol epg-config--configuration-constructor-alist
)))
149 (or (and (not no-cache
) (alist-get protocol epg--configurations
))
150 ;; If the executable value is already set with M-x
151 ;; customize, use it without checking.
152 (if (and symbol
(or (get symbol
'saved-value
)
153 (get symbol
'customized-value
)))
155 (funcall constructor
(symbol-value symbol
))))
156 (push (cons protocol configuration
) epg--configurations
)
159 (dolist (program-version alist
)
160 (let ((executable (executable-find (car program-version
))))
163 (funcall constructor executable
)))
165 (epg-check-configuration configuration
166 (cdr program-version
))
169 (push (cons protocol configuration
)
170 epg--configurations
))
171 (throw 'found configuration
)))))))))))))
173 ;; Create an `epg-configuration' object for `gpg', using PROGRAM.
174 (defun epg-config--make-gpg-configuration (program)
175 (let (config groups type args
)
177 (apply #'call-process program nil
(list t nil
) nil
178 (append (if epg-gpg-home-directory
179 (list "--homedir" epg-gpg-home-directory
))
180 '("--with-colons" "--list-config")))
181 (goto-char (point-min))
182 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t
)
183 (setq type
(intern (match-string 1))
184 args
(match-string 2))
187 (if (string-match "\\`\\([^:]+\\):" args
)
189 (cons (cons (downcase (match-string 1 args
))
190 (delete "" (split-string
196 (message "Invalid group configuration: %S" args
))))
197 ((memq type
'(pubkey cipher digest compress
))
198 (if (string-match "\\`\\([0-9]+\\)\\(;[0-9]+\\)*" args
)
201 (mapcar #'string-to-number
202 (delete "" (split-string args
";"))))
205 (message "Invalid %S algorithm configuration: %S"
208 (setq config
(cons (cons type args
) config
))))))
209 (push (cons 'program program
) config
)
211 (cons (cons 'groups groups
) config
)
214 ;; Create an `epg-configuration' object for `gpgsm', using PROGRAM.
215 (defun epg-config--make-gpgsm-configuration (program)
217 (call-process program nil
(list t nil
) nil
"--version")
218 (goto-char (point-min))
219 (when (looking-at "\\S-+ (")
220 (goto-char (match-end 0))
223 (skip-syntax-forward "-" (point-at-eol))
224 (list (cons 'program program
)
225 (cons 'version
(buffer-substring (point) (point-at-eol)))))))
228 (defun epg-configuration ()
229 "Return a list of internal configuration parameters of `epg-gpg-program'."
230 (declare (obsolete epg-find-configuration
"25.1"))
231 (epg-config--make-gpg-configuration epg-gpg-program
))
234 (defun epg-check-configuration (config &optional req-versions
)
235 "Verify that a sufficient version of GnuPG is installed.
236 CONFIG should be a `epg-configuration' object (a plist).
237 REQ-VERSIONS should be a list with elements of the form (MIN
238 . MAX) where MIN and MAX are version strings indicating a
239 semi-open range of acceptable versions. REQ-VERSIONS may also be
240 a single minimum version string."
241 (let ((version (alist-get 'version config
)))
242 (unless (stringp version
)
243 (error "Undetermined version: %S" version
))
245 (pcase-dolist ((or `(,min .
,max
)
246 (and min
(let max nil
)))
247 (if (listp req-versions
) req-versions
248 (list req-versions
)))
249 (when (and (version<= (or min epg-gpg-minimum-version
)
252 (version< version max
)))
253 (throw 'version-ok t
)))
254 (error "Unsupported version: %s" version
))))
257 (defun epg-expand-group (config group
)
258 "Look at CONFIG and try to expand GROUP."
259 (let ((entry (assq 'groups config
)))
261 (setq entry
(assoc (downcase group
) (cdr entry
))))
264 (provide 'epg-config
)
266 ;;; epg-config.el ends here